mirror of
https://github.com/nodejs/node.git
synced 2026-11-25 23:01:06 -05:00
errors: do not call resolve on URLs with schemes
We were incorrectly trying to run path.resolve on absolute sources URLs. This was breaking webpack:// URLs in stack trace output. Refs: https://github.com/nodejs/node/issues/35325 PR-URL: https://github.com/nodejs/node/pull/35903 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
1 parent
b11c7378e3
commit
7c8b5e5e0e
5 files changed
+72
-36
No files matched your search
@@ -25,7 +25,6 @@ const { Buffer } = require('buffer');
|
||||
let debug = require('internal/util/debuglog').debuglog('source_map', (fn) => {
|
||||
debug = fn;
|
||||
});
|
||||
const { dirname, resolve } = require('path');
|
||||
const fs = require('fs');
|
||||
const { getOptionValue } = require('internal/options');
|
||||
const {
|
||||
@@ -63,10 +62,8 @@ function getSourceMapsEnabled() {
|
||||
function maybeCacheSourceMap(filename, content, cjsModuleInstance) {
|
||||
const sourceMapsEnabled = getSourceMapsEnabled();
|
||||
if (!(process.env.NODE_V8_COVERAGE || sourceMapsEnabled)) return;
|
||||
let basePath;
|
||||
try {
|
||||
filename = normalizeReferrerURL(filename);
|
||||
basePath = dirname(fileURLToPath(filename));
|
||||
} catch (err) {
|
||||
// This is most likely an [eval]-wrapper, which is currently not
|
||||
// supported.
|
||||
@@ -76,7 +73,7 @@ function maybeCacheSourceMap(filename, content, cjsModuleInstance) {
|
||||
|
||||
const match = content.match(/\/[*/]#\s+sourceMappingURL=(?<sourceMappingURL>[^\s]+)/);
|
||||
if (match) {
|
||||
const data = dataFromUrl(basePath, match.groups.sourceMappingURL);
|
||||
const data = dataFromUrl(filename, match.groups.sourceMappingURL);
|
||||
const url = data ? null : match.groups.sourceMappingURL;
|
||||
if (cjsModuleInstance) {
|
||||
if (!Module) Module = require('internal/modules/cjs/loader').Module;
|
||||
@@ -98,12 +95,12 @@ function maybeCacheSourceMap(filename, content, cjsModuleInstance) {
|
||||
}
|
||||
}
|
||||
|
||||
function dataFromUrl(basePath, sourceMappingURL) {
|
||||
function dataFromUrl(sourceURL, sourceMappingURL) {
|
||||
try {
|
||||
const url = new URL(sourceMappingURL);
|
||||
switch (url.protocol) {
|
||||
case 'data:':
|
||||
return sourceMapFromDataUrl(basePath, url.pathname);
|
||||
return sourceMapFromDataUrl(sourceURL, url.pathname);
|
||||
default:
|
||||
debug(`unknown protocol ${url.protocol}`);
|
||||
return null;
|
||||
@@ -111,8 +108,8 @@ function dataFromUrl(basePath, sourceMappingURL) {
|
||||
} catch (err) {
|
||||
debug(err.stack);
|
||||
// If no scheme is present, we assume we are dealing with a file path.
|
||||
const sourceMapFile = resolve(basePath, sourceMappingURL);
|
||||
return sourceMapFromFile(sourceMapFile);
|
||||
const mapURL = new URL(sourceMappingURL, sourceURL).href;
|
||||
return sourceMapFromFile(mapURL);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,11 +125,11 @@ function lineLengths(content) {
|
||||
});
|
||||
}
|
||||
|
||||
function sourceMapFromFile(sourceMapFile) {
|
||||
function sourceMapFromFile(mapURL) {
|
||||
try {
|
||||
const content = fs.readFileSync(sourceMapFile, 'utf8');
|
||||
const content = fs.readFileSync(fileURLToPath(mapURL), 'utf8');
|
||||
const data = JSONParse(content);
|
||||
return sourcesToAbsolute(dirname(sourceMapFile), data);
|
||||
return sourcesToAbsolute(mapURL, data);
|
||||
} catch (err) {
|
||||
debug(err.stack);
|
||||
return null;
|
||||
@@ -141,7 +138,7 @@ function sourceMapFromFile(sourceMapFile) {
|
||||
|
||||
// data:[<mediatype>][;base64],<data> see:
|
||||
// https://tools.ietf.org/html/rfc2397#section-2
|
||||
function sourceMapFromDataUrl(basePath, url) {
|
||||
function sourceMapFromDataUrl(sourceURL, url) {
|
||||
const [format, data] = url.split(',');
|
||||
const splitFormat = format.split(';');
|
||||
const contentType = splitFormat[0];
|
||||
@@ -151,7 +148,7 @@ function sourceMapFromDataUrl(basePath, url) {
|
||||
Buffer.from(data, 'base64').toString('utf8') : data;
|
||||
try {
|
||||
const parsedData = JSONParse(decodedData);
|
||||
return sourcesToAbsolute(basePath, parsedData);
|
||||
return sourcesToAbsolute(sourceURL, parsedData);
|
||||
} catch (err) {
|
||||
debug(err.stack);
|
||||
return null;
|
||||
@@ -165,14 +162,10 @@ function sourceMapFromDataUrl(basePath, url) {
|
||||
// If the sources are not absolute URLs after prepending of the "sourceRoot",
|
||||
// the sources are resolved relative to the SourceMap (like resolving script
|
||||
// src in a html document).
|
||||
function sourcesToAbsolute(base, data) {
|
||||
function sourcesToAbsolute(baseURL, data) {
|
||||
data.sources = data.sources.map((source) => {
|
||||
source = (data.sourceRoot || '') + source;
|
||||
if (!/^[\\/]/.test(source[0])) {
|
||||
source = resolve(base, source);
|
||||
}
|
||||
if (!source.startsWith('file://')) source = `file://${source}`;
|
||||
return source;
|
||||
return new URL(source, baseURL).href;
|
||||
});
|
||||
// The sources array is now resolved to absolute URLs, sourceRoot should
|
||||
// be updated to noop.
|
||||
|
||||
Reference in new issue
Block a user