Skip to content

Commit d1b009b

Browse files
legendecasaduh95
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 7819bfe commit d1b009b

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

lib/internal/source_map/source_map_cache.js

+29-10
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ function maybeCacheSourceMap(filename, content, moduleInstance, isGeneratedSourc
155155
}
156156

157157
const data = dataFromUrl(filename, sourceMapURL);
158+
// `data` could be null if the source map is invalid.
159+
// In this case, create a cache entry with null data with source url for test coverage.
160+
158161
const entry = {
159162
__proto__: null,
160163
lineLengths: lineLengths(content),
@@ -277,6 +280,8 @@ function sourceMapFromDataUrl(sourceURL, url) {
277280
const parsedData = JSONParse(decodedData);
278281
return sourcesToAbsolute(sourceURL, parsedData);
279282
} catch (err) {
283+
// TODO(legendecas): warn about invalid source map JSON string.
284+
// But it could be verbose.
280285
debug(err);
281286
return null;
282287
}
@@ -331,24 +336,38 @@ function sourceMapCacheToObject() {
331336

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

354373
module.exports = {

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

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)