Skip to content

Commit 12bcd81

Browse files
legendecasdevjiwonchoi
authored andcommitted
lib: suppress source map lookup exceptions
When the source map data are invalid json strings, skip construct `SourceMap` on it. Additionally, suppress exceptions on source map lookups and fix test runners crash on invalid source maps. PR-URL: #56299 Refs: #56296 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Xuguang Mei <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Chemi Atlow <[email protected]> Reviewed-By: Pietro Marchini <[email protected]>
1 parent 068c439 commit 12bcd81

File tree

2 files changed

+42
-13
lines changed

2 files changed

+42
-13
lines changed

lib/internal/source_map/source_map_cache.js

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ function maybeCacheSourceMap(filename, content, moduleInstance, isGeneratedSourc
157157
}
158158

159159
const data = dataFromUrl(filename, sourceMapURL);
160+
// `data` could be null if the source map is invalid.
161+
// In this case, create a cache entry with null data with source url for test coverage.
162+
160163
const entry = {
161164
__proto__: null,
162165
lineLengths: lineLengths(content),
@@ -279,6 +282,8 @@ function sourceMapFromDataUrl(sourceURL, url) {
279282
const parsedData = JSONParse(decodedData);
280283
return sourcesToAbsolute(sourceURL, parsedData);
281284
} catch (err) {
285+
// TODO(legendecas): warn about invalid source map JSON string.
286+
// But it could be verbose.
282287
debug(err);
283288
return null;
284289
}
@@ -333,26 +338,38 @@ function sourceMapCacheToObject() {
333338

334339
/**
335340
* Find a source map for a given actual source URL or path.
341+
*
342+
* This function may be invoked from user code or test runner, this must not throw
343+
* any exceptions.
336344
* @param {string} sourceURL - actual source URL or path
337345
* @returns {import('internal/source_map/source_map').SourceMap | undefined} a source map or undefined if not found
338346
*/
339347
function findSourceMap(sourceURL) {
340-
if (RegExpPrototypeExec(kLeadingProtocol, sourceURL) === null) {
341-
sourceURL = pathToFileURL(sourceURL).href;
342-
}
343-
if (!SourceMap) {
344-
SourceMap = require('internal/source_map/source_map').SourceMap;
345-
}
346-
const entry = getModuleSourceMapCache().get(sourceURL) ?? generatedSourceMapCache.get(sourceURL);
347-
if (entry === undefined) {
348+
if (typeof sourceURL !== 'string') {
348349
return undefined;
349350
}
350-
let sourceMap = entry.sourceMap;
351-
if (sourceMap === undefined) {
352-
sourceMap = new SourceMap(entry.data, { lineLengths: entry.lineLengths });
353-
entry.sourceMap = sourceMap;
351+
352+
SourceMap ??= require('internal/source_map/source_map').SourceMap;
353+
try {
354+
if (RegExpPrototypeExec(kLeadingProtocol, sourceURL) === null) {
355+
// If the sourceURL is an invalid path, this will throw an error.
356+
sourceURL = pathToFileURL(sourceURL).href;
357+
}
358+
const entry = getModuleSourceMapCache().get(sourceURL) ?? generatedSourceMapCache.get(sourceURL);
359+
if (entry?.data == null) {
360+
return undefined;
361+
}
362+
363+
let sourceMap = entry.sourceMap;
364+
if (sourceMap === undefined) {
365+
sourceMap = new SourceMap(entry.data, { lineLengths: entry.lineLengths });
366+
entry.sourceMap = sourceMap;
367+
}
368+
return sourceMap;
369+
} catch (err) {
370+
debug(err);
371+
return undefined;
354372
}
355-
return sourceMap;
356373
}
357374

358375
module.exports = {

test/parallel/test-runner-source-maps-invalid-json.js

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)