1 | var charToInteger = {};
|
---|
2 | var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
|
---|
3 | for (var i = 0; i < chars.length; i++) {
|
---|
4 | charToInteger[chars.charCodeAt(i)] = i;
|
---|
5 | }
|
---|
6 | function decode(mappings) {
|
---|
7 | var decoded = [];
|
---|
8 | var line = [];
|
---|
9 | var segment = [
|
---|
10 | 0,
|
---|
11 | 0,
|
---|
12 | 0,
|
---|
13 | 0,
|
---|
14 | 0,
|
---|
15 | ];
|
---|
16 | var j = 0;
|
---|
17 | for (var i = 0, shift = 0, value = 0; i < mappings.length; i++) {
|
---|
18 | var c = mappings.charCodeAt(i);
|
---|
19 | if (c === 44) { // ","
|
---|
20 | segmentify(line, segment, j);
|
---|
21 | j = 0;
|
---|
22 | }
|
---|
23 | else if (c === 59) { // ";"
|
---|
24 | segmentify(line, segment, j);
|
---|
25 | j = 0;
|
---|
26 | decoded.push(line);
|
---|
27 | line = [];
|
---|
28 | segment[0] = 0;
|
---|
29 | }
|
---|
30 | else {
|
---|
31 | var integer = charToInteger[c];
|
---|
32 | if (integer === undefined) {
|
---|
33 | throw new Error('Invalid character (' + String.fromCharCode(c) + ')');
|
---|
34 | }
|
---|
35 | var hasContinuationBit = integer & 32;
|
---|
36 | integer &= 31;
|
---|
37 | value += integer << shift;
|
---|
38 | if (hasContinuationBit) {
|
---|
39 | shift += 5;
|
---|
40 | }
|
---|
41 | else {
|
---|
42 | var shouldNegate = value & 1;
|
---|
43 | value >>>= 1;
|
---|
44 | if (shouldNegate) {
|
---|
45 | value = value === 0 ? -0x80000000 : -value;
|
---|
46 | }
|
---|
47 | segment[j] += value;
|
---|
48 | j++;
|
---|
49 | value = shift = 0; // reset
|
---|
50 | }
|
---|
51 | }
|
---|
52 | }
|
---|
53 | segmentify(line, segment, j);
|
---|
54 | decoded.push(line);
|
---|
55 | return decoded;
|
---|
56 | }
|
---|
57 | function segmentify(line, segment, j) {
|
---|
58 | // This looks ugly, but we're creating specialized arrays with a specific
|
---|
59 | // length. This is much faster than creating a new array (which v8 expands to
|
---|
60 | // a capacity of 17 after pushing the first item), or slicing out a subarray
|
---|
61 | // (which is slow). Length 4 is assumed to be the most frequent, followed by
|
---|
62 | // length 5 (since not everything will have an associated name), followed by
|
---|
63 | // length 1 (it's probably rare for a source substring to not have an
|
---|
64 | // associated segment data).
|
---|
65 | if (j === 4)
|
---|
66 | line.push([segment[0], segment[1], segment[2], segment[3]]);
|
---|
67 | else if (j === 5)
|
---|
68 | line.push([segment[0], segment[1], segment[2], segment[3], segment[4]]);
|
---|
69 | else if (j === 1)
|
---|
70 | line.push([segment[0]]);
|
---|
71 | }
|
---|
72 | function encode(decoded) {
|
---|
73 | var sourceFileIndex = 0; // second field
|
---|
74 | var sourceCodeLine = 0; // third field
|
---|
75 | var sourceCodeColumn = 0; // fourth field
|
---|
76 | var nameIndex = 0; // fifth field
|
---|
77 | var mappings = '';
|
---|
78 | for (var i = 0; i < decoded.length; i++) {
|
---|
79 | var line = decoded[i];
|
---|
80 | if (i > 0)
|
---|
81 | mappings += ';';
|
---|
82 | if (line.length === 0)
|
---|
83 | continue;
|
---|
84 | var generatedCodeColumn = 0; // first field
|
---|
85 | var lineMappings = [];
|
---|
86 | for (var _i = 0, line_1 = line; _i < line_1.length; _i++) {
|
---|
87 | var segment = line_1[_i];
|
---|
88 | var segmentMappings = encodeInteger(segment[0] - generatedCodeColumn);
|
---|
89 | generatedCodeColumn = segment[0];
|
---|
90 | if (segment.length > 1) {
|
---|
91 | segmentMappings +=
|
---|
92 | encodeInteger(segment[1] - sourceFileIndex) +
|
---|
93 | encodeInteger(segment[2] - sourceCodeLine) +
|
---|
94 | encodeInteger(segment[3] - sourceCodeColumn);
|
---|
95 | sourceFileIndex = segment[1];
|
---|
96 | sourceCodeLine = segment[2];
|
---|
97 | sourceCodeColumn = segment[3];
|
---|
98 | }
|
---|
99 | if (segment.length === 5) {
|
---|
100 | segmentMappings += encodeInteger(segment[4] - nameIndex);
|
---|
101 | nameIndex = segment[4];
|
---|
102 | }
|
---|
103 | lineMappings.push(segmentMappings);
|
---|
104 | }
|
---|
105 | mappings += lineMappings.join(',');
|
---|
106 | }
|
---|
107 | return mappings;
|
---|
108 | }
|
---|
109 | function encodeInteger(num) {
|
---|
110 | var result = '';
|
---|
111 | num = num < 0 ? (-num << 1) | 1 : num << 1;
|
---|
112 | do {
|
---|
113 | var clamped = num & 31;
|
---|
114 | num >>>= 5;
|
---|
115 | if (num > 0) {
|
---|
116 | clamped |= 32;
|
---|
117 | }
|
---|
118 | result += chars[clamped];
|
---|
119 | } while (num > 0);
|
---|
120 | return result;
|
---|
121 | }
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Copyright 2019 The AMP HTML Authors. All Rights Reserved.
|
---|
125 | *
|
---|
126 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
127 | * you may not use this file except in compliance with the License.
|
---|
128 | * You may obtain a copy of the License at
|
---|
129 | *
|
---|
130 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
131 | *
|
---|
132 | * Unless required by applicable law or agreed to in writing, software
|
---|
133 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
134 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
135 | * See the License for the specific language governing permissions and
|
---|
136 | * limitations under the License.
|
---|
137 | */
|
---|
138 | /**
|
---|
139 | * Creates a brand new (prototype-less) object with the enumerable-own
|
---|
140 | * properties of `target`. Any enumerable-own properties from `source` which
|
---|
141 | * are not present on `target` will be copied as well.
|
---|
142 | */
|
---|
143 | function defaults(target, source) {
|
---|
144 | return Object.assign(Object.create(null), source, target);
|
---|
145 | }
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Copyright 2019 The AMP HTML Authors. All Rights Reserved.
|
---|
149 | *
|
---|
150 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
151 | * you may not use this file except in compliance with the License.
|
---|
152 | * You may obtain a copy of the License at
|
---|
153 | *
|
---|
154 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
155 | *
|
---|
156 | * Unless required by applicable law or agreed to in writing, software
|
---|
157 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
158 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
159 | * See the License for the specific language governing permissions and
|
---|
160 | * limitations under the License.
|
---|
161 | */
|
---|
162 | /**
|
---|
163 | * Decodes an input sourcemap into a `DecodedSourceMap` sourcemap object.
|
---|
164 | *
|
---|
165 | * Valid input maps include a `DecodedSourceMap`, a `RawSourceMap`, or JSON
|
---|
166 | * representations of either type.
|
---|
167 | */
|
---|
168 | function decodeSourceMap(map) {
|
---|
169 | if (typeof map === 'string') {
|
---|
170 | map = JSON.parse(map);
|
---|
171 | }
|
---|
172 | let { mappings } = map;
|
---|
173 | if (typeof mappings === 'string') {
|
---|
174 | mappings = sortMappings(decode(mappings), true);
|
---|
175 | }
|
---|
176 | else {
|
---|
177 | // Clone the Line so that we can sort it. We don't want to mutate an array
|
---|
178 | // that we don't own directly.
|
---|
179 | mappings = sortMappings(mappings, false);
|
---|
180 | }
|
---|
181 | return defaults({ mappings }, map);
|
---|
182 | }
|
---|
183 | function firstUnsortedSegmentLine(mappings) {
|
---|
184 | for (let i = 0; i < mappings.length; i++) {
|
---|
185 | const segments = mappings[i];
|
---|
186 | for (let j = 1; j < segments.length; j++) {
|
---|
187 | if (segments[j][0] < segments[j - 1][0]) {
|
---|
188 | return i;
|
---|
189 | }
|
---|
190 | }
|
---|
191 | }
|
---|
192 | return mappings.length;
|
---|
193 | }
|
---|
194 | function sortMappings(mappings, owned) {
|
---|
195 | const unosrtedIndex = firstUnsortedSegmentLine(mappings);
|
---|
196 | if (unosrtedIndex === mappings.length)
|
---|
197 | return mappings;
|
---|
198 | if (!owned)
|
---|
199 | mappings = mappings.slice();
|
---|
200 | for (let i = unosrtedIndex; i < mappings.length; i++) {
|
---|
201 | mappings[i] = sortSegments(mappings[i], owned);
|
---|
202 | }
|
---|
203 | return mappings;
|
---|
204 | }
|
---|
205 | function sortSegments(segments, owned) {
|
---|
206 | if (!owned)
|
---|
207 | segments = segments.slice();
|
---|
208 | return segments.sort(segmentComparator);
|
---|
209 | }
|
---|
210 | function segmentComparator(a, b) {
|
---|
211 | return a[0] - b[0];
|
---|
212 | }
|
---|
213 |
|
---|
214 | /**
|
---|
215 | * Copyright 2019 The AMP HTML Authors. All Rights Reserved.
|
---|
216 | *
|
---|
217 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
218 | * you may not use this file except in compliance with the License.
|
---|
219 | * You may obtain a copy of the License at
|
---|
220 | *
|
---|
221 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
222 | *
|
---|
223 | * Unless required by applicable law or agreed to in writing, software
|
---|
224 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
225 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
226 | * See the License for the specific language governing permissions and
|
---|
227 | * limitations under the License.
|
---|
228 | */
|
---|
229 | /**
|
---|
230 | * A "leaf" node in the sourcemap tree, representing an original, unmodified
|
---|
231 | * source file. Recursive segment tracing ends at the `OriginalSource`.
|
---|
232 | */
|
---|
233 | class OriginalSource {
|
---|
234 | constructor(filename, content) {
|
---|
235 | this.filename = filename;
|
---|
236 | this.content = content;
|
---|
237 | }
|
---|
238 | /**
|
---|
239 | * Tracing a `SourceMapSegment` ends when we get to an `OriginalSource`,
|
---|
240 | * meaning this line/column location originated from this source file.
|
---|
241 | */
|
---|
242 | traceSegment(line, column, name) {
|
---|
243 | return { column, line, name, source: this };
|
---|
244 | }
|
---|
245 | }
|
---|
246 |
|
---|
247 | /* istanbul ignore next */
|
---|
248 | const Url = (typeof URL !== 'undefined' ? URL : require('url').URL);
|
---|
249 | // Matches "..", which must be preceeded by "/" or the start of the string, and
|
---|
250 | // must be followed by a "/". We do not eat the following "/", so that the next
|
---|
251 | // iteration can match on it.
|
---|
252 | const parentRegex = /(^|\/)\.\.(?=\/|$)/g;
|
---|
253 | function isAbsoluteUrl(url) {
|
---|
254 | try {
|
---|
255 | return !!new Url(url);
|
---|
256 | }
|
---|
257 | catch (e) {
|
---|
258 | return false;
|
---|
259 | }
|
---|
260 | }
|
---|
261 | /**
|
---|
262 | * Creates a directory name that is guaranteed to not be in `str`.
|
---|
263 | */
|
---|
264 | function uniqInStr(str) {
|
---|
265 | let uniq = String(Math.random()).slice(2);
|
---|
266 | while (str.indexOf(uniq) > -1) {
|
---|
267 | /* istanbul ignore next */
|
---|
268 | uniq += uniq;
|
---|
269 | }
|
---|
270 | return uniq;
|
---|
271 | }
|
---|
272 | /**
|
---|
273 | * Removes the filename from the path (everything trailing the last "/"). This
|
---|
274 | * is only safe to call on a path, never call with an absolute or protocol
|
---|
275 | * relative URL.
|
---|
276 | */
|
---|
277 | function stripPathFilename(path) {
|
---|
278 | path = normalizePath(path);
|
---|
279 | const index = path.lastIndexOf('/');
|
---|
280 | return path.slice(0, index + 1);
|
---|
281 | }
|
---|
282 | /**
|
---|
283 | * Normalizes a protocol-relative URL, but keeps it protocol relative by
|
---|
284 | * stripping out the protocl before returning it.
|
---|
285 | */
|
---|
286 | function normalizeProtocolRelative(input, absoluteBase) {
|
---|
287 | const { href, protocol } = new Url(input, absoluteBase);
|
---|
288 | return href.slice(protocol.length);
|
---|
289 | }
|
---|
290 | /**
|
---|
291 | * Normalizes a simple path (one that has no ".."s, or is absolute so ".."s can
|
---|
292 | * be normalized absolutely).
|
---|
293 | */
|
---|
294 | function normalizeSimplePath(input) {
|
---|
295 | const { href } = new Url(input, 'https://foo.com/');
|
---|
296 | return href.slice('https://foo.com/'.length);
|
---|
297 | }
|
---|
298 | /**
|
---|
299 | * Normalizes a path, ensuring that excess ".."s are preserved for relative
|
---|
300 | * paths in the output.
|
---|
301 | *
|
---|
302 | * If the input is absolute, this will return an absolutey normalized path, but
|
---|
303 | * it will not have a leading "/".
|
---|
304 | *
|
---|
305 | * If the input has a leading "..", the output will have a leading "..".
|
---|
306 | *
|
---|
307 | * If the input has a leading ".", the output will not have a leading "."
|
---|
308 | * unless there are too many ".."s, in which case there will be a leading "..".
|
---|
309 | */
|
---|
310 | function normalizePath(input) {
|
---|
311 | // If there are no ".."s, we can treat this as if it were an absolute path.
|
---|
312 | // The return won't be an absolute path, so it's easy.
|
---|
313 | if (!parentRegex.test(input))
|
---|
314 | return normalizeSimplePath(input);
|
---|
315 | // We already found one "..". Let's see how many there are.
|
---|
316 | let total = 1;
|
---|
317 | while (parentRegex.test(input))
|
---|
318 | total++;
|
---|
319 | // If there are ".."s, we need to prefix the the path with the same number of
|
---|
320 | // unique directories. This is to ensure that we "remember" how many parent
|
---|
321 | // directories we are accessing. Eg, "../../.." must keep 3, and "foo/../.."
|
---|
322 | // must keep 1.
|
---|
323 | const uniqDirectory = `z${uniqInStr(input)}/`;
|
---|
324 | // uniqDirectory is just a "z", followed by numbers, followed by a "/". So
|
---|
325 | // generating a runtime regex from it is safe. We'll use this search regex to
|
---|
326 | // strip out our uniq directory names and insert any needed ".."s.
|
---|
327 | const search = new RegExp(`^(?:${uniqDirectory})*`);
|
---|
328 | // Now we can resolve the total path. If there are excess ".."s, they will
|
---|
329 | // eliminate one or more of the unique directories we prefix with.
|
---|
330 | const relative = normalizeSimplePath(uniqDirectory.repeat(total) + input);
|
---|
331 | // We can now count the number of unique directories that were eliminated. If
|
---|
332 | // there were 3, and 1 was eliminated, we know we only need to add 1 "..". If
|
---|
333 | // 2 were eliminated, we need to insert 2 ".."s. If all 3 were eliminated,
|
---|
334 | // then we need 3, etc. This replace is guranteed to match (it may match 0 or
|
---|
335 | // more times), and we can count the total match to see how many were eliminated.
|
---|
336 | return relative.replace(search, (all) => {
|
---|
337 | const leftover = all.length / uniqDirectory.length;
|
---|
338 | return '../'.repeat(total - leftover);
|
---|
339 | });
|
---|
340 | }
|
---|
341 | /**
|
---|
342 | * Attempts to resolve `input` URL relative to `base`.
|
---|
343 | */
|
---|
344 | function resolve(input, base) {
|
---|
345 | if (!base)
|
---|
346 | base = '';
|
---|
347 | // Absolute URLs are very easy to resolve right.
|
---|
348 | if (isAbsoluteUrl(input))
|
---|
349 | return new Url(input).href;
|
---|
350 | if (base) {
|
---|
351 | // Absolute URLs are easy...
|
---|
352 | if (isAbsoluteUrl(base))
|
---|
353 | return new Url(input, base).href;
|
---|
354 | // If base is protocol relative, we'll resolve with it but keep the result
|
---|
355 | // protocol relative.
|
---|
356 | if (base.startsWith('//'))
|
---|
357 | return normalizeProtocolRelative(input, `https:${base}`);
|
---|
358 | }
|
---|
359 | // Normalize input, but keep it protocol relative. We know base doesn't supply
|
---|
360 | // a protocol, because that would have been handled above.
|
---|
361 | if (input.startsWith('//'))
|
---|
362 | return normalizeProtocolRelative(input, 'https://foo.com/');
|
---|
363 | // We now know that base (if there is one) and input are paths. We've handled
|
---|
364 | // both absolute and protocol-relative variations above.
|
---|
365 | // Absolute paths don't need any special handling, because they cannot have
|
---|
366 | // extra "." or ".."s. That'll all be stripped away. Input takes priority here,
|
---|
367 | // because if input is an absolute path, base path won't affect it in any way.
|
---|
368 | if (input.startsWith('/'))
|
---|
369 | return '/' + normalizeSimplePath(input);
|
---|
370 | // Since input and base are paths, we need to join them to do any further
|
---|
371 | // processing. Paths are joined at the directory level, so we need to remove
|
---|
372 | // the base's filename before joining. We also know that input does not have a
|
---|
373 | // leading slash, and that the stripped base will have a trailing slash if
|
---|
374 | // there are any directories (or it'll be empty).
|
---|
375 | const joined = stripPathFilename(base) + input;
|
---|
376 | // If base is an absolute path, then input will be relative to it.
|
---|
377 | if (base.startsWith('/'))
|
---|
378 | return '/' + normalizeSimplePath(joined);
|
---|
379 | // We now know both base (if there is one) and input are relative paths.
|
---|
380 | const relative = normalizePath(joined);
|
---|
381 | // If base started with a leading ".", or there is no base and input started
|
---|
382 | // with a ".", then we need to ensure that the relative path starts with a
|
---|
383 | // ".". We don't know if relative starts with a "..", though, so check before
|
---|
384 | // prepending.
|
---|
385 | if ((base || input).startsWith('.') && !relative.startsWith('.')) {
|
---|
386 | return './' + relative;
|
---|
387 | }
|
---|
388 | return relative;
|
---|
389 | }
|
---|
390 |
|
---|
391 | /**
|
---|
392 | * Copyright 2019 The AMP HTML Authors. All Rights Reserved.
|
---|
393 | *
|
---|
394 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
395 | * you may not use this file except in compliance with the License.
|
---|
396 | * You may obtain a copy of the License at
|
---|
397 | *
|
---|
398 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
399 | *
|
---|
400 | * Unless required by applicable law or agreed to in writing, software
|
---|
401 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
402 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
403 | * See the License for the specific language governing permissions and
|
---|
404 | * limitations under the License.
|
---|
405 | */
|
---|
406 | function resolve$1(input, base) {
|
---|
407 | // The base is always treated as a directory, if it's not empty.
|
---|
408 | // https://github.com/mozilla/source-map/blob/8cb3ee57/lib/util.js#L327
|
---|
409 | // https://github.com/chromium/chromium/blob/da4adbb3/third_party/blink/renderer/devtools/front_end/sdk/SourceMap.js#L400-L401
|
---|
410 | if (base && !base.endsWith('/'))
|
---|
411 | base += '/';
|
---|
412 | return resolve(input, base);
|
---|
413 | }
|
---|
414 |
|
---|
415 | /**
|
---|
416 | * Copyright 2019 The AMP HTML Authors. All Rights Reserved.
|
---|
417 | *
|
---|
418 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
419 | * you may not use this file except in compliance with the License.
|
---|
420 | * You may obtain a copy of the License at
|
---|
421 | *
|
---|
422 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
423 | *
|
---|
424 | * Unless required by applicable law or agreed to in writing, software
|
---|
425 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
426 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
427 | * See the License for the specific language governing permissions and
|
---|
428 | * limitations under the License.
|
---|
429 | */
|
---|
430 | /**
|
---|
431 | * A binary search implementation that returns the index if a match is found,
|
---|
432 | * or the negated index of where the `needle` should be inserted.
|
---|
433 | *
|
---|
434 | * The `comparator` callback receives both the `item` under comparison and the
|
---|
435 | * needle we are searching for. It must return `0` if the `item` is a match,
|
---|
436 | * any negative number if `item` is too small (and we must search after it), or
|
---|
437 | * any positive number if the `item` is too large (and we must search before
|
---|
438 | * it).
|
---|
439 | *
|
---|
440 | * If no match is found, a negated index of where to insert the `needle` is
|
---|
441 | * returned. This negated index is guaranteed to be less than 0. To insert an
|
---|
442 | * item, negate it (again) and splice:
|
---|
443 | *
|
---|
444 | * ```js
|
---|
445 | * const array = [1, 3];
|
---|
446 | * const needle = 2;
|
---|
447 | * const index = binarySearch(array, needle, (item, needle) => item - needle);
|
---|
448 | *
|
---|
449 | * assert.equal(index, -2);
|
---|
450 | * assert.equal(~index, 1);
|
---|
451 | * array.splice(~index, 0, needle);
|
---|
452 | * assert.deepEqual(array, [1, 2, 3]);
|
---|
453 | * ```
|
---|
454 | */
|
---|
455 | function binarySearch(haystack, needle, comparator, low, high) {
|
---|
456 | low = Math.max(low, 0);
|
---|
457 | while (low <= high) {
|
---|
458 | const mid = low + ((high - low) >> 1);
|
---|
459 | const cmp = comparator(haystack[mid], needle);
|
---|
460 | if (cmp === 0) {
|
---|
461 | return mid;
|
---|
462 | }
|
---|
463 | if (cmp < 0) {
|
---|
464 | low = mid + 1;
|
---|
465 | }
|
---|
466 | else {
|
---|
467 | high = mid - 1;
|
---|
468 | }
|
---|
469 | }
|
---|
470 | return ~low;
|
---|
471 | }
|
---|
472 |
|
---|
473 | /**
|
---|
474 | * Copyright 2019 The AMP HTML Authors. All Rights Reserved.
|
---|
475 | *
|
---|
476 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
477 | * you may not use this file except in compliance with the License.
|
---|
478 | * You may obtain a copy of the License at
|
---|
479 | *
|
---|
480 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
481 | *
|
---|
482 | * Unless required by applicable law or agreed to in writing, software
|
---|
483 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
484 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
485 | * See the License for the specific language governing permissions and
|
---|
486 | * limitations under the License.
|
---|
487 | */
|
---|
488 | /**
|
---|
489 | * FastStringArray acts like a `Set` (allowing only one occurrence of a string
|
---|
490 | * `key`), but provides the index of the `key` in the backing array.
|
---|
491 | *
|
---|
492 | * This is designed to allow synchronizing a second array with the contents of
|
---|
493 | * the backing array, like how `sourcesContent[i]` is the source content
|
---|
494 | * associated with `source[i]`, and there are never duplicates.
|
---|
495 | */
|
---|
496 | class FastStringArray {
|
---|
497 | constructor() {
|
---|
498 | this.indexes = Object.create(null);
|
---|
499 | this.array = [];
|
---|
500 | }
|
---|
501 | /**
|
---|
502 | * Puts `key` into the backing array, if it is not already present. Returns
|
---|
503 | * the index of the `key` in the backing array.
|
---|
504 | */
|
---|
505 | put(key) {
|
---|
506 | const { array, indexes } = this;
|
---|
507 | // The key may or may not be present. If it is present, it's a number.
|
---|
508 | let index = indexes[key];
|
---|
509 | // If it's not yet present, we need to insert it and track the index in the
|
---|
510 | // indexes.
|
---|
511 | if (index === undefined) {
|
---|
512 | index = indexes[key] = array.length;
|
---|
513 | array.push(key);
|
---|
514 | }
|
---|
515 | return index;
|
---|
516 | }
|
---|
517 | }
|
---|
518 |
|
---|
519 | /**
|
---|
520 | * Copyright 2019 The AMP HTML Authors. All Rights Reserved.
|
---|
521 | *
|
---|
522 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
523 | * you may not use this file except in compliance with the License.
|
---|
524 | * You may obtain a copy of the License at
|
---|
525 | *
|
---|
526 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
527 | *
|
---|
528 | * Unless required by applicable law or agreed to in writing, software
|
---|
529 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
530 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
531 | * See the License for the specific language governing permissions and
|
---|
532 | * limitations under the License.
|
---|
533 | */
|
---|
534 | /**
|
---|
535 | * SourceMapTree represents a single sourcemap, with the ability to trace
|
---|
536 | * mappings into its child nodes (which may themselves be SourceMapTrees).
|
---|
537 | */
|
---|
538 | class SourceMapTree {
|
---|
539 | constructor(map, sources) {
|
---|
540 | this.map = map;
|
---|
541 | this.sources = sources;
|
---|
542 | this.lastLine = 0;
|
---|
543 | this.lastColumn = 0;
|
---|
544 | this.lastIndex = 0;
|
---|
545 | }
|
---|
546 | /**
|
---|
547 | * traceMappings is only called on the root level SourceMapTree, and begins
|
---|
548 | * the process of resolving each mapping in terms of the original source
|
---|
549 | * files.
|
---|
550 | */
|
---|
551 | traceMappings() {
|
---|
552 | const mappings = [];
|
---|
553 | const names = new FastStringArray();
|
---|
554 | const sources = new FastStringArray();
|
---|
555 | const sourcesContent = [];
|
---|
556 | const { mappings: rootMappings, names: rootNames } = this.map;
|
---|
557 | for (let i = 0; i < rootMappings.length; i++) {
|
---|
558 | const segments = rootMappings[i];
|
---|
559 | const tracedSegments = [];
|
---|
560 | let lastTraced = undefined;
|
---|
561 | for (let j = 0; j < segments.length; j++) {
|
---|
562 | const segment = segments[j];
|
---|
563 | // 1-length segments only move the current generated column, there's no
|
---|
564 | // source information to gather from it.
|
---|
565 | if (segment.length === 1)
|
---|
566 | continue;
|
---|
567 | const source = this.sources[segment[1]];
|
---|
568 | const traced = source.traceSegment(segment[2], segment[3], segment.length === 5 ? rootNames[segment[4]] : '');
|
---|
569 | if (!traced)
|
---|
570 | continue;
|
---|
571 | // So we traced a segment down into its original source file. Now push a
|
---|
572 | // new segment pointing to this location.
|
---|
573 | const { column, line, name } = traced;
|
---|
574 | const { content, filename } = traced.source;
|
---|
575 | // Store the source location, and ensure we keep sourcesContent up to
|
---|
576 | // date with the sources array.
|
---|
577 | const sourceIndex = sources.put(filename);
|
---|
578 | sourcesContent[sourceIndex] = content;
|
---|
579 | if (lastTraced &&
|
---|
580 | lastTraced[1] === sourceIndex &&
|
---|
581 | lastTraced[2] === line &&
|
---|
582 | lastTraced[3] === column) {
|
---|
583 | // This is a duplicate mapping pointing at the exact same starting point in the source file.
|
---|
584 | // It doesn't provide any new information, and only bloats the sourcemap.
|
---|
585 | continue;
|
---|
586 | }
|
---|
587 | // This looks like unnecessary duplication, but it noticeably increases
|
---|
588 | // performance. If we were to push the nameIndex onto length-4 array, v8
|
---|
589 | // would internally allocate 22 slots! That's 68 wasted bytes! Array
|
---|
590 | // literals have the same capacity as their length, saving memory.
|
---|
591 | if (name) {
|
---|
592 | lastTraced = [segment[0], sourceIndex, line, column, names.put(name)];
|
---|
593 | }
|
---|
594 | else {
|
---|
595 | lastTraced = [segment[0], sourceIndex, line, column];
|
---|
596 | }
|
---|
597 | tracedSegments.push(lastTraced);
|
---|
598 | }
|
---|
599 | mappings.push(tracedSegments);
|
---|
600 | }
|
---|
601 | // TODO: Make all sources relative to the sourceRoot.
|
---|
602 | return defaults({
|
---|
603 | mappings,
|
---|
604 | names: names.array,
|
---|
605 | sources: sources.array,
|
---|
606 | sourcesContent,
|
---|
607 | }, this.map);
|
---|
608 | }
|
---|
609 | /**
|
---|
610 | * traceSegment is only called on children SourceMapTrees. It recurses down
|
---|
611 | * into its own child SourceMapTrees, until we find the original source map.
|
---|
612 | */
|
---|
613 | traceSegment(line, column, name) {
|
---|
614 | const { mappings, names } = this.map;
|
---|
615 | // It's common for parent sourcemaps to have pointers to lines that have no
|
---|
616 | // mapping (like a "//# sourceMappingURL=") at the end of the child file.
|
---|
617 | if (line >= mappings.length)
|
---|
618 | return null;
|
---|
619 | const segments = mappings[line];
|
---|
620 | if (segments.length === 0)
|
---|
621 | return null;
|
---|
622 | let low = 0;
|
---|
623 | let high = segments.length - 1;
|
---|
624 | if (line === this.lastLine) {
|
---|
625 | if (column >= this.lastColumn) {
|
---|
626 | low = this.lastIndex;
|
---|
627 | }
|
---|
628 | else {
|
---|
629 | high = this.lastIndex;
|
---|
630 | }
|
---|
631 | }
|
---|
632 | let index = binarySearch(segments, column, segmentComparator$1, low, high);
|
---|
633 | this.lastLine = line;
|
---|
634 | this.lastColumn = column;
|
---|
635 | if (index === -1) {
|
---|
636 | this.lastIndex = index;
|
---|
637 | return null; // we come before any mapped segment
|
---|
638 | }
|
---|
639 | // If we can't find a segment that lines up to this column, we use the
|
---|
640 | // segment before.
|
---|
641 | if (index < 0) {
|
---|
642 | index = ~index - 1;
|
---|
643 | }
|
---|
644 | this.lastIndex = index;
|
---|
645 | const segment = segments[index];
|
---|
646 | // 1-length segments only move the current generated column, there's no
|
---|
647 | // source information to gather from it.
|
---|
648 | if (segment.length === 1)
|
---|
649 | return null;
|
---|
650 | const source = this.sources[segment[1]];
|
---|
651 | // So now we can recurse down, until we hit the original source file.
|
---|
652 | return source.traceSegment(segment[2], segment[3],
|
---|
653 | // A child map's recorded name for this segment takes precedence over the
|
---|
654 | // parent's mapped name. Imagine a mangler changing the name over, etc.
|
---|
655 | segment.length === 5 ? names[segment[4]] : name);
|
---|
656 | }
|
---|
657 | }
|
---|
658 | function segmentComparator$1(segment, column) {
|
---|
659 | return segment[0] - column;
|
---|
660 | }
|
---|
661 |
|
---|
662 | /**
|
---|
663 | * Copyright 2019 The AMP HTML Authors. All Rights Reserved.
|
---|
664 | *
|
---|
665 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
666 | * you may not use this file except in compliance with the License.
|
---|
667 | * You may obtain a copy of the License at
|
---|
668 | *
|
---|
669 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
670 | *
|
---|
671 | * Unless required by applicable law or agreed to in writing, software
|
---|
672 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
673 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
674 | * See the License for the specific language governing permissions and
|
---|
675 | * limitations under the License.
|
---|
676 | */
|
---|
677 | /**
|
---|
678 | * Removes the filename from a path.
|
---|
679 | */
|
---|
680 | function stripFilename(path) {
|
---|
681 | if (!path)
|
---|
682 | return '';
|
---|
683 | const index = path.lastIndexOf('/');
|
---|
684 | return path.slice(0, index + 1);
|
---|
685 | }
|
---|
686 |
|
---|
687 | /**
|
---|
688 | * Copyright 2019 The AMP HTML Authors. All Rights Reserved.
|
---|
689 | *
|
---|
690 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
691 | * you may not use this file except in compliance with the License.
|
---|
692 | * You may obtain a copy of the License at
|
---|
693 | *
|
---|
694 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
695 | *
|
---|
696 | * Unless required by applicable law or agreed to in writing, software
|
---|
697 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
698 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
699 | * See the License for the specific language governing permissions and
|
---|
700 | * limitations under the License.
|
---|
701 | */
|
---|
702 | function asArray(value) {
|
---|
703 | if (Array.isArray(value))
|
---|
704 | return value;
|
---|
705 | return [value];
|
---|
706 | }
|
---|
707 | /**
|
---|
708 | * Recursively builds a tree structure out of sourcemap files, with each node
|
---|
709 | * being either an `OriginalSource` "leaf" or a `SourceMapTree` composed of
|
---|
710 | * `OriginalSource`s and `SourceMapTree`s.
|
---|
711 | *
|
---|
712 | * Every sourcemap is composed of a collection of source files and mappings
|
---|
713 | * into locations of those source files. When we generate a `SourceMapTree` for
|
---|
714 | * the sourcemap, we attempt to load each source file's own sourcemap. If it
|
---|
715 | * does not have an associated sourcemap, it is considered an original,
|
---|
716 | * unmodified source file.
|
---|
717 | */
|
---|
718 | function buildSourceMapTree(input, loader, relativeRoot) {
|
---|
719 | const maps = asArray(input).map(decodeSourceMap);
|
---|
720 | const map = maps.pop();
|
---|
721 | for (let i = 0; i < maps.length; i++) {
|
---|
722 | if (maps[i].sources.length !== 1) {
|
---|
723 | throw new Error(`Transformation map ${i} must have exactly one source file.\n` +
|
---|
724 | 'Did you specify these with the most recent transformation maps first?');
|
---|
725 | }
|
---|
726 | }
|
---|
727 | const { sourceRoot, sources, sourcesContent } = map;
|
---|
728 | const children = sources.map((sourceFile, i) => {
|
---|
729 | // Each source file is loaded relative to the sourcemap's own sourceRoot,
|
---|
730 | // which is itself relative to the sourcemap's parent.
|
---|
731 | const uri = resolve$1(sourceFile || '', resolve$1(sourceRoot || '', stripFilename(relativeRoot)));
|
---|
732 | // Use the provided loader callback to retrieve the file's sourcemap.
|
---|
733 | // TODO: We should eventually support async loading of sourcemap files.
|
---|
734 | const sourceMap = loader(uri);
|
---|
735 | // If there is no sourcemap, then it is an unmodified source file.
|
---|
736 | if (!sourceMap) {
|
---|
737 | // The source file's actual contents must be included in the sourcemap
|
---|
738 | // (done when generating the sourcemap) for it to be included as a
|
---|
739 | // sourceContent in the output sourcemap.
|
---|
740 | const sourceContent = sourcesContent ? sourcesContent[i] : null;
|
---|
741 | return new OriginalSource(uri, sourceContent);
|
---|
742 | }
|
---|
743 | // Else, it's a real sourcemap, and we need to recurse into it to load its
|
---|
744 | // source files.
|
---|
745 | return buildSourceMapTree(decodeSourceMap(sourceMap), loader, uri);
|
---|
746 | });
|
---|
747 | let tree = new SourceMapTree(map, children);
|
---|
748 | for (let i = maps.length - 1; i >= 0; i--) {
|
---|
749 | tree = new SourceMapTree(maps[i], [tree]);
|
---|
750 | }
|
---|
751 | return tree;
|
---|
752 | }
|
---|
753 |
|
---|
754 | /**
|
---|
755 | * Copyright 2019 The AMP HTML Authors. All Rights Reserved.
|
---|
756 | *
|
---|
757 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
758 | * you may not use this file except in compliance with the License.
|
---|
759 | * You may obtain a copy of the License at
|
---|
760 | *
|
---|
761 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
762 | *
|
---|
763 | * Unless required by applicable law or agreed to in writing, software
|
---|
764 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
765 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
766 | * See the License for the specific language governing permissions and
|
---|
767 | * limitations under the License.
|
---|
768 | */
|
---|
769 | /**
|
---|
770 | * A SourceMap v3 compatible sourcemap, which only includes fields that were
|
---|
771 | * provided to it.
|
---|
772 | */
|
---|
773 | class SourceMap {
|
---|
774 | constructor(map, options) {
|
---|
775 | this.version = 3; // SourceMap spec says this should be first.
|
---|
776 | if ('file' in map)
|
---|
777 | this.file = map.file;
|
---|
778 | this.mappings = options.decodedMappings ? map.mappings : encode(map.mappings);
|
---|
779 | this.names = map.names;
|
---|
780 | // TODO: We first need to make all source URIs relative to the sourceRoot
|
---|
781 | // before we can support a sourceRoot.
|
---|
782 | // if ('sourceRoot' in map) this.sourceRoot = map.sourceRoot;
|
---|
783 | this.sources = map.sources;
|
---|
784 | if (!options.excludeContent && 'sourcesContent' in map) {
|
---|
785 | this.sourcesContent = map.sourcesContent;
|
---|
786 | }
|
---|
787 | }
|
---|
788 | toString() {
|
---|
789 | return JSON.stringify(this);
|
---|
790 | }
|
---|
791 | }
|
---|
792 |
|
---|
793 | /**
|
---|
794 | * Copyright 2019 The AMP HTML Authors. All Rights Reserved.
|
---|
795 | *
|
---|
796 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
797 | * you may not use this file except in compliance with the License.
|
---|
798 | * You may obtain a copy of the License at
|
---|
799 | *
|
---|
800 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
801 | *
|
---|
802 | * Unless required by applicable law or agreed to in writing, software
|
---|
803 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
804 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
805 | * See the License for the specific language governing permissions and
|
---|
806 | * limitations under the License.
|
---|
807 | */
|
---|
808 | /**
|
---|
809 | * Traces through all the mappings in the root sourcemap, through the sources
|
---|
810 | * (and their sourcemaps), all the way back to the original source location.
|
---|
811 | *
|
---|
812 | * `loader` will be called every time we encounter a source file. If it returns
|
---|
813 | * a sourcemap, we will recurse into that sourcemap to continue the trace. If
|
---|
814 | * it returns a falsey value, that source file is treated as an original,
|
---|
815 | * unmodified source file.
|
---|
816 | *
|
---|
817 | * Pass `excludeContent` to exclude any self-containing source file content
|
---|
818 | * from the output sourcemap.
|
---|
819 | *
|
---|
820 | * Pass `decodedMappings` to receive a SourceMap with decoded (instead of
|
---|
821 | * VLQ encoded) mappings.
|
---|
822 | */
|
---|
823 | function remapping(input, loader, options) {
|
---|
824 | const opts = typeof options === 'object' ? options : { excludeContent: !!options, decodedMappings: false };
|
---|
825 | const graph = buildSourceMapTree(input, loader);
|
---|
826 | return new SourceMap(graph.traceMappings(), opts);
|
---|
827 | }
|
---|
828 |
|
---|
829 | export default remapping;
|
---|
830 | //# sourceMappingURL=remapping.mjs.map
|
---|