@@ -155,6 +155,9 @@ function maybeCacheSourceMap(filename, content, moduleInstance, isGeneratedSourc |
155 | 155 | } |
156 | 156 |
|
157 | 157 | 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 | + |
158 | 161 | const entry = { |
159 | 162 | __proto__: null, |
160 | 163 | lineLengths: lineLengths(content), |
@@ -277,6 +280,8 @@ function sourceMapFromDataUrl(sourceURL, url) { |
277 | 280 | const parsedData = JSONParse(decodedData); |
278 | 281 | return sourcesToAbsolute(sourceURL, parsedData); |
279 | 282 | } catch (err) { |
| 283 | + // TODO(legendecas): warn about invalid source map JSON string. |
| 284 | + // But it could be verbose. |
280 | 285 | debug(err); |
281 | 286 | return null; |
282 | 287 | } |
@@ -331,24 +336,38 @@ function sourceMapCacheToObject() { |
331 | 336 |
|
332 | 337 | /** |
333 | 338 | * 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. |
334 | 342 | * @param {string} sourceURL - actual source URL or path |
335 | 343 | * @returns {import('internal/source_map/source_map').SourceMap | undefined} a source map or undefined if not found |
336 | 344 | */ |
337 | 345 | function findSourceMap(sourceURL) { |
338 | | - if (RegExpPrototypeExec(kLeadingProtocol, sourceURL) === null) { |
339 | | - sourceURL = pathToFileURL(sourceURL).href; |
| 346 | + if (typeof sourceURL !== 'string') { |
| 347 | + return undefined; |
340 | 348 | } |
| 349 | + |
341 | 350 | 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); |
344 | 369 | return undefined; |
345 | 370 | } |
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; |
352 | 371 | } |
353 | 372 |
|
354 | 373 | module.exports = { |
|