From 21489fd36dd7929c95f268487df12170c5711148 Mon Sep 17 00:00:00 2001 From: "bencoe@google.com" Date: Sun, 16 Jan 2022 01:11:29 +0000 Subject: [PATCH] errors: cache source if loaded from disk Refs: #41541 --- lib/internal/source_map/prepare_stack_trace.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/internal/source_map/prepare_stack_trace.js b/lib/internal/source_map/prepare_stack_trace.js index 9502cfef6fe029..ca841f1de954f5 100644 --- a/lib/internal/source_map/prepare_stack_trace.js +++ b/lib/internal/source_map/prepare_stack_trace.js @@ -1,6 +1,7 @@ 'use strict'; const { + SafeMap, ArrayPrototypeIndexOf, ArrayPrototypeJoin, ArrayPrototypeMap, @@ -170,6 +171,8 @@ function getErrorSource( return exceptionLine; } +// Only load source from disk once: +const sourcesCache = new SafeMap(); function getOriginalSource(payload, originalSourcePath) { let source; const originalSourcePathNoScheme = @@ -181,11 +184,14 @@ function getOriginalSource(payload, originalSourcePath) { // First we check if the original source content was provided in the // source map itself: source = payload.sourcesContent[sourceContentIndex]; + } else if (sourcesCache.has(originalSourcePathNoScheme)) { + return sourcesCache.get(originalSourcePathNoScheme); } else { // If no sourcesContent was found, attempt to load the original source // from disk: try { source = readFileSync(originalSourcePathNoScheme, 'utf8'); + sourcesCache.set(originalSourcePathNoScheme, source); } catch (err) { debug(err); source = '';