Skip to content

Commit 5b79f38

Browse files
committed
fix
1 parent 0677a3c commit 5b79f38

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

.vscode/launch.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"type": "node",
66
"request": "launch",
77
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/_mocha",
8+
"runtimeArgs": ["--timeout", "999999999"],
89
"outputCapture": "std",
910
"skipFiles": [
1011
"<node_internals>/**/*.js"

source-map-support.js

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,20 @@ function hasGlobalProcessEventEmitter() {
129129
return ((typeof process === 'object') && (process !== null) && (typeof process.on === 'function'));
130130
}
131131

132+
function tryFileURLToPath(v) {
133+
// TODO technically, file URL can omit /s.
134+
// Copy the isFileURL util from resolve-uri?
135+
if(v.startsWith('file:/')) {
136+
return fileURLToPath(v);
137+
}
138+
return v;
139+
}
140+
132141
// #region Caches
133142
/** @param {string} pathOrFileUrl */
134143
function getCacheKey(pathOrFileUrl) {
144+
if(pathOrFileUrl.startsWith('node:')) return pathOrFileUrl;
145+
// TODO unify with isFileURL checks elsewhere? as helper fn?
135146
if(pathOrFileUrl.startsWith('file:/')) {
136147
// Must normalize spaces to %20, stuff like that
137148
return new URL(pathOrFileUrl).toString();
@@ -224,9 +235,26 @@ sharedData.internalRetrieveFileHandlers.push(function(path) {
224235
return setFileContentsCache(path, contents);
225236
});
226237

238+
// TODO un-copy these from resolve-uri; see if they can be exported from that lib
239+
function isAbsoluteUrl(input) {
240+
return schemeRegex.test(input);
241+
}
242+
// Matches the scheme of a URL, eg "http://"
243+
const schemeRegex = /^[\w+.-]+:\/\//;
244+
function isSchemeRelativeUrl(input) {
245+
return input.startsWith('//');
246+
}
247+
227248
// Support URLs relative to a directory, but be careful about a protocol prefix
228249
// in case we are in the browser (i.e. directories may start with "http://" or "file:///")
229250
function supportRelativeURL(file, url) {
251+
// We want to preserve path style.
252+
// resolveUri cannot handle windows paths.
253+
// Therefore, special-case when output will be a windows path
254+
if(process.platform === 'win32' && path.isAbsolute(file) && !isAbsoluteUrl(url) && !isSchemeRelativeUrl(url)) {
255+
const dir = path.dirname(file);
256+
return path.resolve(dir, url);
257+
}
230258
return resolveUri(url, file);
231259
}
232260

@@ -251,7 +279,7 @@ function retrieveSourceMapURL(source) {
251279
}
252280

253281
// Get the URL of the source map
254-
fileData = retrieveFile(source);
282+
fileData = retrieveFile(tryFileURLToPath(source));
255283
var re = /(?:\/\/[@#][\s]*sourceMappingURL=([^\s'"]+)[\s]*$)|(?:\/\*[@#][\s]*sourceMappingURL=([^\s*'"]+)[\s]*(?:\*\/)[\s]*$)/mg;
256284
// Keep executing the search to find the *last* sourceMappingURL to avoid
257285
// picking up sourceMappingURLs from comments, strings, etc.
@@ -282,7 +310,7 @@ sharedData.internalRetrieveMapHandlers.push(function(source) {
282310
} else {
283311
// Support source map URLs relative to the source URL
284312
sourceMappingURL = supportRelativeURL(source, sourceMappingURL);
285-
sourceMapData = retrieveFile(sourceMappingURL);
313+
sourceMapData = retrieveFile(tryFileURLToPath(sourceMappingURL));
286314
}
287315

288316
if (!sourceMapData) {
@@ -581,7 +609,7 @@ function createPrepareStackTrace(hookState) {
581609
// Generate position and snippet of original source with pointer
582610
function getErrorSource(error) {
583611
// TODO this is not robust enough
584-
var match = /\n at [^(]+ \((?:file:\/{0,2})?(.*):(\d+):(\d+)\)/.exec(error.stack);
612+
var match = /\n at [^(]+ \((.*):(\d+):(\d+)\)/.exec(error.stack);
585613
if (match) {
586614
var source = match[1];
587615
var line = +match[2];
@@ -590,6 +618,8 @@ function getErrorSource(error) {
590618
// Support the inline sourceContents inside the source map
591619
var contents = getFileContentsCache(source);
592620

621+
source = tryFileURLToPath(source);
622+
593623
// Support files on disk
594624
if (!contents && fs && fs.existsSync(source)) {
595625
try {

test.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ function namedExportDeclaration() {
3333
* Example: ` at Module.exports.test (`...
3434
*/
3535
function stackFrameAtTest(sourceMapSupportInstalled = true) {
36-
console.log(extension);
3736
if(semver.gte(process.versions.node, '18.0.0')) {
3837
return extension === 'mjs' ? 'Module\\.test' : sourceMapSupportInstalled ? 'Module\\.exports\\.test' : 'exports\\.test';
3938
} else {
@@ -88,7 +87,7 @@ function compareLines(actual, expected) {
8887
for (var i = 0; i < expected.length; i++) {
8988
// Some tests are regular expressions because the output format changed slightly between node v0.9.2 and v0.9.3
9089
if (expected[i] instanceof RegExp) {
91-
assert(expected[i].test(actual[i]), JSON.stringify(actual[i]) + ' does not match ' + expected[i]);
90+
assert(expected[i].test(actual[i]), JSON.stringify(actual[i]) + ' does not match ' + expected[i] + '\n' + JSON.stringify({actual, expected: expected.map(v => typeof v === 'string' ? v : v.toString())}, null, 2));
9291
} else {
9392
assert.equal(actual[i], expected[i]);
9493
}
@@ -211,7 +210,9 @@ function compareStdout(done, sourceMap, source, expected) {
211210
(stdout + stderr)
212211
.trim()
213212
.split(/\r\n|\n/)
214-
.filter(function (line) { return line !== '' }), // Empty lines are not relevant.
213+
// Empty lines are not relevant.
214+
// Running in a debugger causes additional output.
215+
.filter(function (line) { return line !== '' && line !== 'Debugger attached.' }),
215216
expected
216217
);
217218
} catch (e) {

0 commit comments

Comments
 (0)