Skip to content

Commit

Permalink
lib: suppress source map lookup exceptions
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
legendecas authored and aduh95 committed Jan 2, 2025
1 parent 7819bfe commit d1b009b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
39 changes: 29 additions & 10 deletions lib/internal/source_map/source_map_cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ function maybeCacheSourceMap(filename, content, moduleInstance, isGeneratedSourc
}

const data = dataFromUrl(filename, sourceMapURL);
// `data` could be null if the source map is invalid.
// In this case, create a cache entry with null data with source url for test coverage.

const entry = {
__proto__: null,
lineLengths: lineLengths(content),
Expand Down Expand Up @@ -277,6 +280,8 @@ function sourceMapFromDataUrl(sourceURL, url) {
const parsedData = JSONParse(decodedData);
return sourcesToAbsolute(sourceURL, parsedData);
} catch (err) {
// TODO(legendecas): warn about invalid source map JSON string.
// But it could be verbose.
debug(err);
return null;
}
Expand Down Expand Up @@ -331,24 +336,38 @@ function sourceMapCacheToObject() {

/**
* Find a source map for a given actual source URL or path.
*
* This function may be invoked from user code or test runner, this must not throw
* any exceptions.
* @param {string} sourceURL - actual source URL or path
* @returns {import('internal/source_map/source_map').SourceMap | undefined} a source map or undefined if not found
*/
function findSourceMap(sourceURL) {
if (RegExpPrototypeExec(kLeadingProtocol, sourceURL) === null) {
sourceURL = pathToFileURL(sourceURL).href;
if (typeof sourceURL !== 'string') {
return undefined;
}

SourceMap ??= require('internal/source_map/source_map').SourceMap;
const entry = getModuleSourceMapCache().get(sourceURL) ?? generatedSourceMapCache.get(sourceURL);
if (entry === undefined) {
try {
if (RegExpPrototypeExec(kLeadingProtocol, sourceURL) === null) {
// If the sourceURL is an invalid path, this will throw an error.
sourceURL = pathToFileURL(sourceURL).href;
}
const entry = getModuleSourceMapCache().get(sourceURL) ?? generatedSourceMapCache.get(sourceURL);
if (entry?.data == null) {
return undefined;
}

let sourceMap = entry.sourceMap;
if (sourceMap === undefined) {
sourceMap = new SourceMap(entry.data, { lineLengths: entry.lineLengths });
entry.sourceMap = sourceMap;
}
return sourceMap;
} catch (err) {
debug(err);
return undefined;
}
let sourceMap = entry.sourceMap;
if (sourceMap === undefined) {
sourceMap = new SourceMap(entry.data, { lineLengths: entry.lineLengths });
entry.sourceMap = sourceMap;
}
return sourceMap;
}

module.exports = {
Expand Down
12 changes: 12 additions & 0 deletions test/parallel/test-runner-source-maps-invalid-json.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d1b009b

Please sign in to comment.