Skip to content

Commit 29ad188

Browse files
committed
avoid firefox bug with sourceMappingURL parsing
1 parent 202fa31 commit 29ad188

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

code.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,31 @@
101101
}
102102

103103
async function finishLoadingCodeWithEmbeddedSourceMap(code, file) {
104-
// Check for both "//" and "/*" comments
105-
let match = /\/(\/)[#@] *sourceMappingURL=([^\s]+)/.exec(code);
106-
if (!match) match = /\/(\*)[#@] *sourceMappingURL=((?:[^\s*]|\*[^/])+)(?:[^*]|\*[^/])*\*\//.exec(code);
104+
let url;
105+
106+
// Check for both "//" and "/*" comments. This is mostly done manually
107+
// instead of doing it all with a regular expression because Firefox's
108+
// regular expression engine crashes with an internal error when the
109+
// match is too big.
110+
for (let regex = /\/([*/])[#@] *sourceMappingURL=/g, match; match = regex.exec(code);) {
111+
const start = match.index + match[0].length;
112+
const n = code.length;
113+
let end = start;
114+
while (end < n && code.charCodeAt(end) > 32) {
115+
end++;
116+
}
117+
if (end > start && (match[1] === '/' || code.slice(end).indexOf('*/') > 0)) {
118+
url = code.slice(start, end);
119+
break;
120+
}
121+
}
107122

108123
// Check for a non-empty data URL payload
109-
if (match && match[2]) {
124+
if (url) {
110125
let map;
111126
try {
112127
// Use "new URL" to ensure that the URL has a protocol (e.g. "data:" or "https:")
113-
map = await fetch(new URL(match[2])).then(r => r.text());
128+
map = await fetch(new URL(url)).then(r => r.text());
114129
} catch (e) {
115130
showLoadingError(`Failed to parse the URL in the "/${match[1]}# sourceMappingURL=" comment: ${e && e.message || e}`);
116131
return;

0 commit comments

Comments
 (0)