Skip to content

Commit d31e761

Browse files
committed
lib: optimize path to file conversion
It seems `wrapSafe` filename is always absolute path. Certain aspects for validating paths in `pathToFileURL` can be skipped in favor of better performance of CJS synchrnous loading while keeping the origin.
1 parent 22acc3b commit d31e761

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

lib/internal/modules/cjs/loader.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,12 +1005,18 @@ Module.prototype.require = function(id) {
10051005
// (needed for setting breakpoint when called with --inspect-brk)
10061006
let resolvedArgv;
10071007
let hasPausedEntry = false;
1008+
const absolutePathToUrlConverter = new URL('file://');
10081009

10091010
function wrapSafe(filename, content, cjsModuleInstance) {
1011+
let filenameUrl = filename;
1012+
if(path.isAbsolute(filename)) {
1013+
absolutePathToUrlConverter.pathname = filename;
1014+
filenameUrl = absolutePathToUrlConverter.href;
1015+
}
10101016
if (patched) {
10111017
const wrapper = Module.wrap(content);
10121018
return vm.runInThisContext(wrapper, {
1013-
filename,
1019+
filename: filenameUrl,
10141020
lineOffset: 0,
10151021
displayErrors: true,
10161022
importModuleDynamically: async (specifier) => {
@@ -1020,10 +1026,6 @@ function wrapSafe(filename, content, cjsModuleInstance) {
10201026
});
10211027
}
10221028
let compiled;
1023-
let filenameUrl = filename;
1024-
try {
1025-
filenameUrl = normalizeReferrerURL(filename);
1026-
} catch {}
10271029
try {
10281030
compiled = compileFunction(
10291031
content,

lib/internal/source_map/prepare_stack_trace.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,7 @@ const prepareStackTrace = (globalThis, error, trace) => {
8585
const prefix = (name && name !== t.getFunctionName()) ?
8686
`\n -> at ${name}` :
8787
'\n ->';
88-
const originalSourceNoScheme =
89-
StringPrototypeStartsWith(originalSource, 'file://') ?
90-
fileURLToPath(originalSource) : originalSource;
91-
str += `${prefix} (${originalSourceNoScheme}:${originalLine + 1}:` +
88+
str += `${prefix} (${originalSource}:${originalLine + 1}:` +
9289
`${originalColumn + 1})`;
9390
}
9491
}

test/sequential/test-cli-syntax-require.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const common = require('../common');
44
const assert = require('assert');
55
const { exec } = require('child_process');
66
const fixtures = require('../common/fixtures');
7+
const { pathToFileURL } = require('url');
78

89
const node = process.execPath;
910

@@ -29,8 +30,9 @@ const syntaxErrorRE = /^SyntaxError: \b/m;
2930
// stderr should have a syntax error message
3031
assert(syntaxErrorRE.test(stderr), `${syntaxErrorRE} === ${stderr}`);
3132

32-
// stderr should include the filename
33-
assert(stderr.startsWith(file), `${stderr} starts with ${file}`);
33+
// stderr should include the file URL
34+
const fileUrl = pathToFileURL(file);
35+
assert(stderr.startsWith(fileUrl), `${stderr} starts with ${file}`);
3436
}));
3537
});
3638
});

0 commit comments

Comments
 (0)