Repository navigation
Expand file tree
/
Copy pathsource-map.ts
More file actions
539 lines (495 loc) · 15.2 KB
/
Copy pathsource-map.ts
File metadata and controls
539 lines (495 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import type { EncodedSourceMap } from '@ampproject/remapping';
import { existsSync, readFileSync } from 'node:fs';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
export const SOURCEMAP_COMMENT_PREFIX = '//# sourceMappingURL=';
export const SOURCEMAP_COMMENT_BYTES = Buffer.from(SOURCEMAP_COMMENT_PREFIX);
/**
* Checks for a single trailing `//# sourceMappingURL=` comment on a raw buffer.
*
* @param data The raw byte buffer to inspect.
* @returns An object containing the sliced code buffer and URL snippet if a single trailing comment exists,
* `null` if no sourcemap comment exists in the buffer, or `undefined` if multiple/non-trailing comments exist.
*/
export function findTrailingSourceMapComment(
data: Uint8Array,
): { code: Uint8Array; urlLine: string } | null | undefined {
const dataBuffer = Buffer.isBuffer(data)
? data
: Buffer.from(data.buffer, data.byteOffset, data.byteLength);
const firstIndex = dataBuffer.indexOf(SOURCEMAP_COMMENT_BYTES);
if (firstIndex === -1) {
return null;
}
const lastIndex = dataBuffer.lastIndexOf(SOURCEMAP_COMMENT_BYTES);
// Skip any preceding horizontal whitespace (spaces/tabs) to find the start of the line.
let prevIdx = lastIndex - 1;
while (prevIdx >= 0 && (dataBuffer[prevIdx] === 32 || dataBuffer[prevIdx] === 9)) {
prevIdx--;
}
// Ensure the comment starts at the beginning of a line or the start of the file,
// preventing false positives for occurrences inside inline string literals or code.
const isLineStart = prevIdx < 0 || dataBuffer[prevIdx] === 10 || dataBuffer[prevIdx] === 13;
if (firstIndex === lastIndex && isLineStart) {
const urlLine = dataBuffer
.subarray(lastIndex + SOURCEMAP_COMMENT_BYTES.length)
.toString('utf-8');
return {
code: dataBuffer.subarray(0, prevIdx < 0 ? 0 : prevIdx + 1),
urlLine,
};
}
return undefined;
}
/**
* Removes `//# sourceMappingURL=` comments safely from the given JavaScript code,
* ignoring any occurrences that are inside string literals, template literals, regex literals,
* or block comments.
*
* For raw Uint8Array / Buffer inputs, it optimizes performance by inspecting trailing byte sequences
* to slice the buffer directly without full string decoding.
*
* @param code The JavaScript source code as a string or Uint8Array.
* @returns The code with top-level sourcemap comments removed.
*/
export function removeSourceMappingURL(code: string): string;
export function removeSourceMappingURL(code: Uint8Array): Uint8Array;
export function removeSourceMappingURL(code: string | Uint8Array): string | Uint8Array {
if (typeof code === 'string') {
return removeSourceMappingURLFromString(code);
}
const trailing = findTrailingSourceMapComment(code);
if (trailing === null) {
return code;
}
if (trailing && isTrailingSourceMapComment(trailing.urlLine)) {
return trailing.code;
}
// Fallback to full decode and state-machine stripping for multiple comments or non-trailing comments
const dataBuffer = Buffer.isBuffer(code)
? code
: Buffer.from(code.buffer, code.byteOffset, code.byteLength);
const text = dataBuffer.toString('utf-8');
const stripped = removeSourceMappingURLFromString(text);
return stripped === text ? code : Buffer.from(stripped, 'utf-8');
}
const REGEX_PRECEDING_KEYWORDS = new Set([
'await',
'case',
'delete',
'do',
'else',
'in',
'instanceof',
'new',
'of',
'return',
'throw',
'typeof',
'void',
'yield',
]);
const REGEX_PRECEDING_PUNCTUATION = new Set([
'(',
',',
'=',
':',
'[',
'!',
'&',
'|',
'?',
'{',
'}',
';',
'+',
'-',
'*',
'%',
'~',
'^',
'<',
'>',
'/',
]);
function isIdentifierChar(char: string): boolean {
const code = char.charCodeAt(0);
return (
(code >= 97 && code <= 122) || // a-z
(code >= 65 && code <= 90) || // A-Z
(code >= 48 && code <= 57) || // 0-9
code === 95 || // _
code === 36 // $
);
}
function removeSourceMappingURLFromString(code: string): string {
if (!code.includes(SOURCEMAP_COMMENT_PREFIX)) {
return code;
}
const result: string[] = [];
let lastCopiedIndex = 0;
let i = 0;
const len = code.length;
// Stack to track template literal state and curly brace depth for nested interpolations.
const stack: { type: 'template'; braceDepth: number }[] = [];
let currentState:
| 'normal'
| 'string_double'
| 'string_single'
| 'template'
| 'regex'
| 'comment_block'
| 'comment_line' = 'normal';
let regexAllowed = true;
let inRegexCharClass = false;
while (i < len) {
const char = code[i];
const nextChar = code[i + 1];
if (currentState === 'normal') {
if (char === '/' && nextChar === '*') {
currentState = 'comment_block';
i += 2;
continue;
}
if (char === '/' && nextChar === '/') {
// Detect if the comment is escaped (e.g. inside a regex literal like `/\/\/#/`).
let isEscaped = false;
let prevIdx = i - 1;
while (prevIdx >= 0 && /\s/.test(code[prevIdx])) {
prevIdx--;
}
if (prevIdx >= 0 && code[prevIdx] === '\\') {
let bsCount = 0;
while (prevIdx >= 0 && code[prevIdx] === '\\') {
bsCount++;
prevIdx--;
}
if (bsCount % 2 === 1) {
isEscaped = true;
}
}
if (!isEscaped && code.startsWith(SOURCEMAP_COMMENT_PREFIX, i)) {
if (i > lastCopiedIndex) {
result.push(code.slice(lastCopiedIndex, i));
}
// Skip the rest of the comment line up to the newline
i += SOURCEMAP_COMMENT_PREFIX.length;
while (i < len && code[i] !== '\n' && code[i] !== '\r') {
i++;
}
lastCopiedIndex = i;
continue;
} else {
currentState = 'comment_line';
i += 2;
continue;
}
}
if (char === '"') {
currentState = 'string_double';
regexAllowed = false;
i++;
continue;
}
if (char === "'") {
currentState = 'string_single';
regexAllowed = false;
i++;
continue;
}
if (char === '`') {
currentState = 'template';
stack.push({ type: 'template', braceDepth: 0 });
regexAllowed = false;
i++;
continue;
}
if (char === '{') {
const top = stack[stack.length - 1];
if (top) {
top.braceDepth++;
}
regexAllowed = true;
i++;
continue;
}
if (char === '}') {
const top = stack[stack.length - 1];
if (top) {
top.braceDepth--;
if (top.braceDepth < 0) {
// Exiting a template literal interpolation ${ ... }
stack.pop();
currentState = 'template';
regexAllowed = false;
i++;
continue;
}
}
regexAllowed = true;
i++;
continue;
}
if (char === '/') {
if (regexAllowed) {
currentState = 'regex';
inRegexCharClass = false;
regexAllowed = false;
} else {
regexAllowed = true;
}
i++;
continue;
}
if (char === ' ' || char === '\t' || char === '\n' || char === '\r') {
i++;
continue;
}
if (isIdentifierChar(char)) {
const start = i;
i++;
while (i < len && isIdentifierChar(code[i])) {
i++;
}
const word = code.slice(start, i);
let prevIdx = start - 1;
while (
prevIdx >= 0 &&
(code[prevIdx] === ' ' ||
code[prevIdx] === '\t' ||
code[prevIdx] === '\n' ||
code[prevIdx] === '\r')
) {
prevIdx--;
}
regexAllowed =
(prevIdx < 0 || code[prevIdx] !== '.') && REGEX_PRECEDING_KEYWORDS.has(word);
continue;
}
regexAllowed = REGEX_PRECEDING_PUNCTUATION.has(char);
i++;
} else if (currentState === 'string_double') {
if (char === '\\') {
i += 2;
continue;
}
if (char === '"') {
currentState = 'normal';
regexAllowed = false;
}
i++;
} else if (currentState === 'string_single') {
if (char === '\\') {
i += 2;
continue;
}
if (char === "'") {
currentState = 'normal';
regexAllowed = false;
}
i++;
} else if (currentState === 'template') {
if (char === '\\') {
i += 2;
continue;
}
if (char === '$' && nextChar === '{') {
// Entering template literal interpolation context
currentState = 'normal';
stack.push({ type: 'template', braceDepth: 0 });
regexAllowed = true;
i += 2;
continue;
}
if (char === '`') {
stack.pop();
currentState = 'normal';
regexAllowed = false;
}
i++;
} else if (currentState === 'regex') {
if (char === '\\') {
if (nextChar === '\n' || nextChar === '\r') {
currentState = 'normal';
inRegexCharClass = false;
regexAllowed = true;
}
i += 2;
continue;
}
if (char === '\n' || char === '\r') {
currentState = 'normal';
inRegexCharClass = false;
regexAllowed = true;
i++;
continue;
}
if (char === '[') {
inRegexCharClass = true;
} else if (char === ']' && inRegexCharClass) {
inRegexCharClass = false;
} else if (char === '/' && !inRegexCharClass) {
currentState = 'normal';
regexAllowed = false;
}
i++;
} else if (currentState === 'comment_block') {
if (char === '*' && nextChar === '/') {
currentState = 'normal';
i += 2;
continue;
}
i++;
} else if (currentState === 'comment_line') {
if (char === '\n' || char === '\r') {
currentState = 'normal';
}
i++;
}
}
if (lastCopiedIndex === 0) {
return code;
}
if (lastCopiedIndex < len) {
result.push(code.slice(lastCopiedIndex));
}
return result.join('');
}
/**
* Extracts the base64 payload from an inline sourcemap data URI line and verifies
* that only trailing whitespace follows the payload.
*
* @returns The base64 payload string if valid and trailing, or `undefined` otherwise.
*/
function extractTrailingBase64Payload(urlLine: string): string | undefined {
if (!urlLine.startsWith('data:application/json;')) {
return undefined;
}
const base64StartIndex = urlLine.indexOf('base64,');
if (base64StartIndex === -1) {
return undefined;
}
const payloadStart = base64StartIndex + 7;
let payloadEnd = urlLine.length;
// Find the first trailing whitespace character that marks the end of the base64 payload.
for (let i = payloadStart; i < urlLine.length; i++) {
const char = urlLine[i];
if (char === ' ' || char === '\r' || char === '\n' || char === '\t') {
payloadEnd = i;
break;
}
}
// Verify that everything after the base64 payload is trailing whitespace
// to ensure this is a valid trailing sourceMappingURL comment at the end of the file.
for (let i = payloadEnd; i < urlLine.length; i++) {
const char = urlLine[i];
if (char !== ' ' && char !== '\r' && char !== '\n' && char !== '\t') {
return undefined;
}
}
return urlLine.slice(payloadStart, payloadEnd);
}
/**
* Extracts the URL from an external sourcemap comment line and verifies
* that only trailing whitespace follows the URL.
*
* @returns The URL string if valid and trailing, or `undefined` otherwise.
*/
function extractTrailingUrl(urlLine: string): string | undefined {
if (urlLine.startsWith('data:')) {
return undefined;
}
const urlMatch = /^([^\r\n\s'"`]+)/.exec(urlLine);
if (!urlMatch) {
return undefined;
}
const remaining = urlLine.slice(urlMatch[1].length);
if (!/^\s*$/.test(remaining)) {
return undefined;
}
return urlMatch[1];
}
/**
* Checks whether a `//# sourceMappingURL=` URL line snippet represents a valid trailing comment at the end of the file.
*/
export function isTrailingSourceMapComment(urlLine: string): boolean {
return (
extractTrailingBase64Payload(urlLine) !== undefined || extractTrailingUrl(urlLine) !== undefined
);
}
/**
* Resolves and loads the input sourcemap referenced in a `//# sourceMappingURL=` URL line snippet.
* Supports inline base64 data URIs, local absolute file URLs, and relative/absolute filesystem paths.
*/
export function loadInputSourceMapFromUrl(
filename: string,
urlLine: string,
): EncodedSourceMap | undefined {
// Inline base64-encoded sourcemaps can be extremely large (up to megabytes).
// Parse them without regular expressions to avoid heavy backtracking and allocations.
const base64Payload = extractTrailingBase64Payload(urlLine);
if (base64Payload !== undefined) {
try {
return JSON.parse(Buffer.from(base64Payload, 'base64').toString('utf-8')) as EncodedSourceMap;
} catch {
return undefined;
}
}
const url = extractTrailingUrl(urlLine);
if (!url) {
return undefined;
}
if (url.startsWith('file://')) {
// Local absolute file URL scheme.
try {
const mapPath = fileURLToPath(url);
if (existsSync(mapPath)) {
return JSON.parse(readFileSync(mapPath, 'utf8')) as EncodedSourceMap;
}
} catch {}
} else if (!/^[a-z]+:\/\//i.test(url)) {
// Local relative or absolute filesystem path (percent-decoded as it originates from a URI).
try {
const mapPath = resolve(dirname(filename), decodeURIComponent(url));
if (existsSync(mapPath)) {
return JSON.parse(readFileSync(mapPath, 'utf8')) as EncodedSourceMap;
}
} catch {}
}
return undefined;
}
/**
* Finds, resolves, and loads the input sourcemap referenced in the code's trailing
* sourceMappingURL comment, if present. Supports inline base64 data URIs, local absolute
* file URLs, and relative/absolute filesystem paths.
*/
export function loadInputSourceMap(filename: string, code: string): EncodedSourceMap | undefined {
// Locate the last sourceMappingURL comment using lastIndexOf to avoid scanning
// the entire file with a regular expression (significant for large files).
const lastSourceMapIndex = code.lastIndexOf(SOURCEMAP_COMMENT_PREFIX);
if (lastSourceMapIndex === -1) {
return undefined;
}
if (lastSourceMapIndex > 0) {
// Skip any preceding horizontal whitespace (spaces/tabs) to find the start of the line.
let prevIdx = lastSourceMapIndex - 1;
while (prevIdx >= 0 && (code[prevIdx] === ' ' || code[prevIdx] === '\t')) {
prevIdx--;
}
// Ensure the comment starts at the beginning of a line, preventing false positives within code or strings.
if (prevIdx >= 0 && code[prevIdx] !== '\n' && code[prevIdx] !== '\r') {
return undefined;
}
}
return loadInputSourceMapFromUrl(
filename,
code.slice(lastSourceMapIndex + SOURCEMAP_COMMENT_PREFIX.length),
);
}