Skip to content

Commit

Permalink
errors: cache source if loaded from disk
Browse files Browse the repository at this point in the history
Refs: #41541
  • Loading branch information
bcoe committed Jan 16, 2022
1 parent 807c7e1 commit 21489fd
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/internal/source_map/prepare_stack_trace.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const {
SafeMap,
ArrayPrototypeIndexOf,
ArrayPrototypeJoin,
ArrayPrototypeMap,
Expand Down Expand Up @@ -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 =
Expand All @@ -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 = '';
Expand Down

0 comments on commit 21489fd

Please sign in to comment.