Skip to content

Commit 21489fd

Browse files
committed
errors: cache source if loaded from disk
Refs: #41541
1 parent 807c7e1 commit 21489fd

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

lib/internal/source_map/prepare_stack_trace.js

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const {
4+
SafeMap,
45
ArrayPrototypeIndexOf,
56
ArrayPrototypeJoin,
67
ArrayPrototypeMap,
@@ -170,6 +171,8 @@ function getErrorSource(
170171
return exceptionLine;
171172
}
172173

174+
// Only load source from disk once:
175+
const sourcesCache = new SafeMap();
173176
function getOriginalSource(payload, originalSourcePath) {
174177
let source;
175178
const originalSourcePathNoScheme =
@@ -181,11 +184,14 @@ function getOriginalSource(payload, originalSourcePath) {
181184
// First we check if the original source content was provided in the
182185
// source map itself:
183186
source = payload.sourcesContent[sourceContentIndex];
187+
} else if (sourcesCache.has(originalSourcePathNoScheme)) {
188+
return sourcesCache.get(originalSourcePathNoScheme);
184189
} else {
185190
// If no sourcesContent was found, attempt to load the original source
186191
// from disk:
187192
try {
188193
source = readFileSync(originalSourcePathNoScheme, 'utf8');
194+
sourcesCache.set(originalSourcePathNoScheme, source);
189195
} catch (err) {
190196
debug(err);
191197
source = '';

0 commit comments

Comments
 (0)