From 3e43e1c15a002494bde3efaac0f5fa95dacd7ba5 Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Thu, 15 Sep 2022 13:47:07 +0000 Subject: [PATCH] lib: don't match `sourceMappingURL` in strings Prior to this change `sourceMappingURL` in string where being matched by the RegExp which caused sourcemaps not be loaded when using the `--enable-source-maps` flag. This commit changes the RegExp to match the last occurrence. Fixes: https://github.com/nodejs/node/issues/44654 --- lib/internal/source_map/source_map_cache.js | 4 ++-- .../typescript-sourcemapping_url_string.js | 4 ++++ .../typescript-sourcemapping_url_string.js.map | 1 + test/message/source_map_sourcemapping_url_string.js | 13 +++++++++++++ .../message/source_map_sourcemapping_url_string.out | 3 +++ 5 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/source-map/typescript-sourcemapping_url_string.js create mode 100644 test/fixtures/source-map/typescript-sourcemapping_url_string.js.map create mode 100644 test/message/source_map_sourcemapping_url_string.js create mode 100644 test/message/source_map_sourcemapping_url_string.out diff --git a/lib/internal/source_map/source_map_cache.js b/lib/internal/source_map/source_map_cache.js index 60852a4f439037..8298c0bd6602a2 100644 --- a/lib/internal/source_map/source_map_cache.js +++ b/lib/internal/source_map/source_map_cache.js @@ -41,8 +41,8 @@ const esmSourceMapCache = new SafeMap(); // The generated sources is not mutable, so we can use a Map without memory concerns: const generatedSourceMapCache = new SafeMap(); const kLeadingProtocol = /^\w+:\/\//; -const kSourceMappingURLMagicComment = /\/[*/]#\s+sourceMappingURL=(?[^\s]+)/; -const kSourceURLMagicComment = /\/[*/]#\s+sourceURL=(?[^\s]+)/; +const kSourceMappingURLMagicComment = /\/[*/]#\s+sourceMappingURL=(?[^\s]+)\s*[*/]\/\s*$/; +const kSourceURLMagicComment = /\/[*/]#\s+sourceURL=(?[^\s]+)\s*[*/]\/\s*$/; const { fileURLToPath, pathToFileURL, URL } = require('internal/url'); let SourceMap; diff --git a/test/fixtures/source-map/typescript-sourcemapping_url_string.js b/test/fixtures/source-map/typescript-sourcemapping_url_string.js new file mode 100644 index 00000000000000..4fd62da9424ac3 --- /dev/null +++ b/test/fixtures/source-map/typescript-sourcemapping_url_string.js @@ -0,0 +1,4 @@ +"use strict"; +const content = '/*# sourceMappingURL='; +throw new Error('an exception.'); +//# sourceMappingURL=typescript-sourcemapping_url_string.js.map diff --git a/test/fixtures/source-map/typescript-sourcemapping_url_string.js.map b/test/fixtures/source-map/typescript-sourcemapping_url_string.js.map new file mode 100644 index 00000000000000..cbb2d46f169df2 --- /dev/null +++ b/test/fixtures/source-map/typescript-sourcemapping_url_string.js.map @@ -0,0 +1 @@ +{"version":3,"file":"typescript-sourcemapping_url_string.js","sourceRoot":"","sources":["typescript-sourcemapping_url_string.ts"],"names":[],"mappings":";AAAA,MAAM,OAAO,GAAG,uBAAuB,CAAC;AAExC,MAAM,IAAI,KAAK,CAAC,eAAe,CAAC,CAAC"} diff --git a/test/message/source_map_sourcemapping_url_string.js b/test/message/source_map_sourcemapping_url_string.js new file mode 100644 index 00000000000000..254d18bd080e53 --- /dev/null +++ b/test/message/source_map_sourcemapping_url_string.js @@ -0,0 +1,13 @@ +// Flags: --enable-source-maps + +'use strict'; +require('../common'); +Error.stackTraceLimit = 2; + +try { + require('../fixtures/source-map/typescript-sourcemapping_url_string'); +} catch (err) { + setTimeout(() => { + console.info(err); + }, 10); +} diff --git a/test/message/source_map_sourcemapping_url_string.out b/test/message/source_map_sourcemapping_url_string.out new file mode 100644 index 00000000000000..0978f773b4001d --- /dev/null +++ b/test/message/source_map_sourcemapping_url_string.out @@ -0,0 +1,3 @@ +reachable +Error: an exception + at *typescript-sourcemapping_url_string.ts:3:7*