@@ -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 */
337345function 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
354373module . exports = {
0 commit comments