1 | const comma = ','.charCodeAt(0);
|
---|
2 | const semicolon = ';'.charCodeAt(0);
|
---|
3 | const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
---|
4 | const intToChar = new Uint8Array(64); // 64 possible chars.
|
---|
5 | const charToInt = new Uint8Array(128); // z is 122 in ASCII
|
---|
6 | for (let i = 0; i < chars.length; i++) {
|
---|
7 | const c = chars.charCodeAt(i);
|
---|
8 | intToChar[i] = c;
|
---|
9 | charToInt[c] = i;
|
---|
10 | }
|
---|
11 | function decodeInteger(reader, relative) {
|
---|
12 | let value = 0;
|
---|
13 | let shift = 0;
|
---|
14 | let integer = 0;
|
---|
15 | do {
|
---|
16 | const c = reader.next();
|
---|
17 | integer = charToInt[c];
|
---|
18 | value |= (integer & 31) << shift;
|
---|
19 | shift += 5;
|
---|
20 | } while (integer & 32);
|
---|
21 | const shouldNegate = value & 1;
|
---|
22 | value >>>= 1;
|
---|
23 | if (shouldNegate) {
|
---|
24 | value = -0x80000000 | -value;
|
---|
25 | }
|
---|
26 | return relative + value;
|
---|
27 | }
|
---|
28 | function encodeInteger(builder, num, relative) {
|
---|
29 | let delta = num - relative;
|
---|
30 | delta = delta < 0 ? (-delta << 1) | 1 : delta << 1;
|
---|
31 | do {
|
---|
32 | let clamped = delta & 0b011111;
|
---|
33 | delta >>>= 5;
|
---|
34 | if (delta > 0)
|
---|
35 | clamped |= 0b100000;
|
---|
36 | builder.write(intToChar[clamped]);
|
---|
37 | } while (delta > 0);
|
---|
38 | return num;
|
---|
39 | }
|
---|
40 | function hasMoreVlq(reader, max) {
|
---|
41 | if (reader.pos >= max)
|
---|
42 | return false;
|
---|
43 | return reader.peek() !== comma;
|
---|
44 | }
|
---|
45 |
|
---|
46 | const bufLength = 1024 * 16;
|
---|
47 | // Provide a fallback for older environments.
|
---|
48 | const td = typeof TextDecoder !== 'undefined'
|
---|
49 | ? /* #__PURE__ */ new TextDecoder()
|
---|
50 | : typeof Buffer !== 'undefined'
|
---|
51 | ? {
|
---|
52 | decode(buf) {
|
---|
53 | const out = Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength);
|
---|
54 | return out.toString();
|
---|
55 | },
|
---|
56 | }
|
---|
57 | : {
|
---|
58 | decode(buf) {
|
---|
59 | let out = '';
|
---|
60 | for (let i = 0; i < buf.length; i++) {
|
---|
61 | out += String.fromCharCode(buf[i]);
|
---|
62 | }
|
---|
63 | return out;
|
---|
64 | },
|
---|
65 | };
|
---|
66 | class StringWriter {
|
---|
67 | constructor() {
|
---|
68 | this.pos = 0;
|
---|
69 | this.out = '';
|
---|
70 | this.buffer = new Uint8Array(bufLength);
|
---|
71 | }
|
---|
72 | write(v) {
|
---|
73 | const { buffer } = this;
|
---|
74 | buffer[this.pos++] = v;
|
---|
75 | if (this.pos === bufLength) {
|
---|
76 | this.out += td.decode(buffer);
|
---|
77 | this.pos = 0;
|
---|
78 | }
|
---|
79 | }
|
---|
80 | flush() {
|
---|
81 | const { buffer, out, pos } = this;
|
---|
82 | return pos > 0 ? out + td.decode(buffer.subarray(0, pos)) : out;
|
---|
83 | }
|
---|
84 | }
|
---|
85 | class StringReader {
|
---|
86 | constructor(buffer) {
|
---|
87 | this.pos = 0;
|
---|
88 | this.buffer = buffer;
|
---|
89 | }
|
---|
90 | next() {
|
---|
91 | return this.buffer.charCodeAt(this.pos++);
|
---|
92 | }
|
---|
93 | peek() {
|
---|
94 | return this.buffer.charCodeAt(this.pos);
|
---|
95 | }
|
---|
96 | indexOf(char) {
|
---|
97 | const { buffer, pos } = this;
|
---|
98 | const idx = buffer.indexOf(char, pos);
|
---|
99 | return idx === -1 ? buffer.length : idx;
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | const EMPTY = [];
|
---|
104 | function decodeOriginalScopes(input) {
|
---|
105 | const { length } = input;
|
---|
106 | const reader = new StringReader(input);
|
---|
107 | const scopes = [];
|
---|
108 | const stack = [];
|
---|
109 | let line = 0;
|
---|
110 | for (; reader.pos < length; reader.pos++) {
|
---|
111 | line = decodeInteger(reader, line);
|
---|
112 | const column = decodeInteger(reader, 0);
|
---|
113 | if (!hasMoreVlq(reader, length)) {
|
---|
114 | const last = stack.pop();
|
---|
115 | last[2] = line;
|
---|
116 | last[3] = column;
|
---|
117 | continue;
|
---|
118 | }
|
---|
119 | const kind = decodeInteger(reader, 0);
|
---|
120 | const fields = decodeInteger(reader, 0);
|
---|
121 | const hasName = fields & 0b0001;
|
---|
122 | const scope = (hasName ? [line, column, 0, 0, kind, decodeInteger(reader, 0)] : [line, column, 0, 0, kind]);
|
---|
123 | let vars = EMPTY;
|
---|
124 | if (hasMoreVlq(reader, length)) {
|
---|
125 | vars = [];
|
---|
126 | do {
|
---|
127 | const varsIndex = decodeInteger(reader, 0);
|
---|
128 | vars.push(varsIndex);
|
---|
129 | } while (hasMoreVlq(reader, length));
|
---|
130 | }
|
---|
131 | scope.vars = vars;
|
---|
132 | scopes.push(scope);
|
---|
133 | stack.push(scope);
|
---|
134 | }
|
---|
135 | return scopes;
|
---|
136 | }
|
---|
137 | function encodeOriginalScopes(scopes) {
|
---|
138 | const writer = new StringWriter();
|
---|
139 | for (let i = 0; i < scopes.length;) {
|
---|
140 | i = _encodeOriginalScopes(scopes, i, writer, [0]);
|
---|
141 | }
|
---|
142 | return writer.flush();
|
---|
143 | }
|
---|
144 | function _encodeOriginalScopes(scopes, index, writer, state) {
|
---|
145 | const scope = scopes[index];
|
---|
146 | const { 0: startLine, 1: startColumn, 2: endLine, 3: endColumn, 4: kind, vars } = scope;
|
---|
147 | if (index > 0)
|
---|
148 | writer.write(comma);
|
---|
149 | state[0] = encodeInteger(writer, startLine, state[0]);
|
---|
150 | encodeInteger(writer, startColumn, 0);
|
---|
151 | encodeInteger(writer, kind, 0);
|
---|
152 | const fields = scope.length === 6 ? 0b0001 : 0;
|
---|
153 | encodeInteger(writer, fields, 0);
|
---|
154 | if (scope.length === 6)
|
---|
155 | encodeInteger(writer, scope[5], 0);
|
---|
156 | for (const v of vars) {
|
---|
157 | encodeInteger(writer, v, 0);
|
---|
158 | }
|
---|
159 | for (index++; index < scopes.length;) {
|
---|
160 | const next = scopes[index];
|
---|
161 | const { 0: l, 1: c } = next;
|
---|
162 | if (l > endLine || (l === endLine && c >= endColumn)) {
|
---|
163 | break;
|
---|
164 | }
|
---|
165 | index = _encodeOriginalScopes(scopes, index, writer, state);
|
---|
166 | }
|
---|
167 | writer.write(comma);
|
---|
168 | state[0] = encodeInteger(writer, endLine, state[0]);
|
---|
169 | encodeInteger(writer, endColumn, 0);
|
---|
170 | return index;
|
---|
171 | }
|
---|
172 | function decodeGeneratedRanges(input) {
|
---|
173 | const { length } = input;
|
---|
174 | const reader = new StringReader(input);
|
---|
175 | const ranges = [];
|
---|
176 | const stack = [];
|
---|
177 | let genLine = 0;
|
---|
178 | let definitionSourcesIndex = 0;
|
---|
179 | let definitionScopeIndex = 0;
|
---|
180 | let callsiteSourcesIndex = 0;
|
---|
181 | let callsiteLine = 0;
|
---|
182 | let callsiteColumn = 0;
|
---|
183 | let bindingLine = 0;
|
---|
184 | let bindingColumn = 0;
|
---|
185 | do {
|
---|
186 | const semi = reader.indexOf(';');
|
---|
187 | let genColumn = 0;
|
---|
188 | for (; reader.pos < semi; reader.pos++) {
|
---|
189 | genColumn = decodeInteger(reader, genColumn);
|
---|
190 | if (!hasMoreVlq(reader, semi)) {
|
---|
191 | const last = stack.pop();
|
---|
192 | last[2] = genLine;
|
---|
193 | last[3] = genColumn;
|
---|
194 | continue;
|
---|
195 | }
|
---|
196 | const fields = decodeInteger(reader, 0);
|
---|
197 | const hasDefinition = fields & 0b0001;
|
---|
198 | const hasCallsite = fields & 0b0010;
|
---|
199 | const hasScope = fields & 0b0100;
|
---|
200 | let callsite = null;
|
---|
201 | let bindings = EMPTY;
|
---|
202 | let range;
|
---|
203 | if (hasDefinition) {
|
---|
204 | const defSourcesIndex = decodeInteger(reader, definitionSourcesIndex);
|
---|
205 | definitionScopeIndex = decodeInteger(reader, definitionSourcesIndex === defSourcesIndex ? definitionScopeIndex : 0);
|
---|
206 | definitionSourcesIndex = defSourcesIndex;
|
---|
207 | range = [genLine, genColumn, 0, 0, defSourcesIndex, definitionScopeIndex];
|
---|
208 | }
|
---|
209 | else {
|
---|
210 | range = [genLine, genColumn, 0, 0];
|
---|
211 | }
|
---|
212 | range.isScope = !!hasScope;
|
---|
213 | if (hasCallsite) {
|
---|
214 | const prevCsi = callsiteSourcesIndex;
|
---|
215 | const prevLine = callsiteLine;
|
---|
216 | callsiteSourcesIndex = decodeInteger(reader, callsiteSourcesIndex);
|
---|
217 | const sameSource = prevCsi === callsiteSourcesIndex;
|
---|
218 | callsiteLine = decodeInteger(reader, sameSource ? callsiteLine : 0);
|
---|
219 | callsiteColumn = decodeInteger(reader, sameSource && prevLine === callsiteLine ? callsiteColumn : 0);
|
---|
220 | callsite = [callsiteSourcesIndex, callsiteLine, callsiteColumn];
|
---|
221 | }
|
---|
222 | range.callsite = callsite;
|
---|
223 | if (hasMoreVlq(reader, semi)) {
|
---|
224 | bindings = [];
|
---|
225 | do {
|
---|
226 | bindingLine = genLine;
|
---|
227 | bindingColumn = genColumn;
|
---|
228 | const expressionsCount = decodeInteger(reader, 0);
|
---|
229 | let expressionRanges;
|
---|
230 | if (expressionsCount < -1) {
|
---|
231 | expressionRanges = [[decodeInteger(reader, 0)]];
|
---|
232 | for (let i = -1; i > expressionsCount; i--) {
|
---|
233 | const prevBl = bindingLine;
|
---|
234 | bindingLine = decodeInteger(reader, bindingLine);
|
---|
235 | bindingColumn = decodeInteger(reader, bindingLine === prevBl ? bindingColumn : 0);
|
---|
236 | const expression = decodeInteger(reader, 0);
|
---|
237 | expressionRanges.push([expression, bindingLine, bindingColumn]);
|
---|
238 | }
|
---|
239 | }
|
---|
240 | else {
|
---|
241 | expressionRanges = [[expressionsCount]];
|
---|
242 | }
|
---|
243 | bindings.push(expressionRanges);
|
---|
244 | } while (hasMoreVlq(reader, semi));
|
---|
245 | }
|
---|
246 | range.bindings = bindings;
|
---|
247 | ranges.push(range);
|
---|
248 | stack.push(range);
|
---|
249 | }
|
---|
250 | genLine++;
|
---|
251 | reader.pos = semi + 1;
|
---|
252 | } while (reader.pos < length);
|
---|
253 | return ranges;
|
---|
254 | }
|
---|
255 | function encodeGeneratedRanges(ranges) {
|
---|
256 | if (ranges.length === 0)
|
---|
257 | return '';
|
---|
258 | const writer = new StringWriter();
|
---|
259 | for (let i = 0; i < ranges.length;) {
|
---|
260 | i = _encodeGeneratedRanges(ranges, i, writer, [0, 0, 0, 0, 0, 0, 0]);
|
---|
261 | }
|
---|
262 | return writer.flush();
|
---|
263 | }
|
---|
264 | function _encodeGeneratedRanges(ranges, index, writer, state) {
|
---|
265 | const range = ranges[index];
|
---|
266 | const { 0: startLine, 1: startColumn, 2: endLine, 3: endColumn, isScope, callsite, bindings, } = range;
|
---|
267 | if (state[0] < startLine) {
|
---|
268 | catchupLine(writer, state[0], startLine);
|
---|
269 | state[0] = startLine;
|
---|
270 | state[1] = 0;
|
---|
271 | }
|
---|
272 | else if (index > 0) {
|
---|
273 | writer.write(comma);
|
---|
274 | }
|
---|
275 | state[1] = encodeInteger(writer, range[1], state[1]);
|
---|
276 | const fields = (range.length === 6 ? 0b0001 : 0) | (callsite ? 0b0010 : 0) | (isScope ? 0b0100 : 0);
|
---|
277 | encodeInteger(writer, fields, 0);
|
---|
278 | if (range.length === 6) {
|
---|
279 | const { 4: sourcesIndex, 5: scopesIndex } = range;
|
---|
280 | if (sourcesIndex !== state[2]) {
|
---|
281 | state[3] = 0;
|
---|
282 | }
|
---|
283 | state[2] = encodeInteger(writer, sourcesIndex, state[2]);
|
---|
284 | state[3] = encodeInteger(writer, scopesIndex, state[3]);
|
---|
285 | }
|
---|
286 | if (callsite) {
|
---|
287 | const { 0: sourcesIndex, 1: callLine, 2: callColumn } = range.callsite;
|
---|
288 | if (sourcesIndex !== state[4]) {
|
---|
289 | state[5] = 0;
|
---|
290 | state[6] = 0;
|
---|
291 | }
|
---|
292 | else if (callLine !== state[5]) {
|
---|
293 | state[6] = 0;
|
---|
294 | }
|
---|
295 | state[4] = encodeInteger(writer, sourcesIndex, state[4]);
|
---|
296 | state[5] = encodeInteger(writer, callLine, state[5]);
|
---|
297 | state[6] = encodeInteger(writer, callColumn, state[6]);
|
---|
298 | }
|
---|
299 | if (bindings) {
|
---|
300 | for (const binding of bindings) {
|
---|
301 | if (binding.length > 1)
|
---|
302 | encodeInteger(writer, -binding.length, 0);
|
---|
303 | const expression = binding[0][0];
|
---|
304 | encodeInteger(writer, expression, 0);
|
---|
305 | let bindingStartLine = startLine;
|
---|
306 | let bindingStartColumn = startColumn;
|
---|
307 | for (let i = 1; i < binding.length; i++) {
|
---|
308 | const expRange = binding[i];
|
---|
309 | bindingStartLine = encodeInteger(writer, expRange[1], bindingStartLine);
|
---|
310 | bindingStartColumn = encodeInteger(writer, expRange[2], bindingStartColumn);
|
---|
311 | encodeInteger(writer, expRange[0], 0);
|
---|
312 | }
|
---|
313 | }
|
---|
314 | }
|
---|
315 | for (index++; index < ranges.length;) {
|
---|
316 | const next = ranges[index];
|
---|
317 | const { 0: l, 1: c } = next;
|
---|
318 | if (l > endLine || (l === endLine && c >= endColumn)) {
|
---|
319 | break;
|
---|
320 | }
|
---|
321 | index = _encodeGeneratedRanges(ranges, index, writer, state);
|
---|
322 | }
|
---|
323 | if (state[0] < endLine) {
|
---|
324 | catchupLine(writer, state[0], endLine);
|
---|
325 | state[0] = endLine;
|
---|
326 | state[1] = 0;
|
---|
327 | }
|
---|
328 | else {
|
---|
329 | writer.write(comma);
|
---|
330 | }
|
---|
331 | state[1] = encodeInteger(writer, endColumn, state[1]);
|
---|
332 | return index;
|
---|
333 | }
|
---|
334 | function catchupLine(writer, lastLine, line) {
|
---|
335 | do {
|
---|
336 | writer.write(semicolon);
|
---|
337 | } while (++lastLine < line);
|
---|
338 | }
|
---|
339 |
|
---|
340 | function decode(mappings) {
|
---|
341 | const { length } = mappings;
|
---|
342 | const reader = new StringReader(mappings);
|
---|
343 | const decoded = [];
|
---|
344 | let genColumn = 0;
|
---|
345 | let sourcesIndex = 0;
|
---|
346 | let sourceLine = 0;
|
---|
347 | let sourceColumn = 0;
|
---|
348 | let namesIndex = 0;
|
---|
349 | do {
|
---|
350 | const semi = reader.indexOf(';');
|
---|
351 | const line = [];
|
---|
352 | let sorted = true;
|
---|
353 | let lastCol = 0;
|
---|
354 | genColumn = 0;
|
---|
355 | while (reader.pos < semi) {
|
---|
356 | let seg;
|
---|
357 | genColumn = decodeInteger(reader, genColumn);
|
---|
358 | if (genColumn < lastCol)
|
---|
359 | sorted = false;
|
---|
360 | lastCol = genColumn;
|
---|
361 | if (hasMoreVlq(reader, semi)) {
|
---|
362 | sourcesIndex = decodeInteger(reader, sourcesIndex);
|
---|
363 | sourceLine = decodeInteger(reader, sourceLine);
|
---|
364 | sourceColumn = decodeInteger(reader, sourceColumn);
|
---|
365 | if (hasMoreVlq(reader, semi)) {
|
---|
366 | namesIndex = decodeInteger(reader, namesIndex);
|
---|
367 | seg = [genColumn, sourcesIndex, sourceLine, sourceColumn, namesIndex];
|
---|
368 | }
|
---|
369 | else {
|
---|
370 | seg = [genColumn, sourcesIndex, sourceLine, sourceColumn];
|
---|
371 | }
|
---|
372 | }
|
---|
373 | else {
|
---|
374 | seg = [genColumn];
|
---|
375 | }
|
---|
376 | line.push(seg);
|
---|
377 | reader.pos++;
|
---|
378 | }
|
---|
379 | if (!sorted)
|
---|
380 | sort(line);
|
---|
381 | decoded.push(line);
|
---|
382 | reader.pos = semi + 1;
|
---|
383 | } while (reader.pos <= length);
|
---|
384 | return decoded;
|
---|
385 | }
|
---|
386 | function sort(line) {
|
---|
387 | line.sort(sortComparator);
|
---|
388 | }
|
---|
389 | function sortComparator(a, b) {
|
---|
390 | return a[0] - b[0];
|
---|
391 | }
|
---|
392 | function encode(decoded) {
|
---|
393 | const writer = new StringWriter();
|
---|
394 | let sourcesIndex = 0;
|
---|
395 | let sourceLine = 0;
|
---|
396 | let sourceColumn = 0;
|
---|
397 | let namesIndex = 0;
|
---|
398 | for (let i = 0; i < decoded.length; i++) {
|
---|
399 | const line = decoded[i];
|
---|
400 | if (i > 0)
|
---|
401 | writer.write(semicolon);
|
---|
402 | if (line.length === 0)
|
---|
403 | continue;
|
---|
404 | let genColumn = 0;
|
---|
405 | for (let j = 0; j < line.length; j++) {
|
---|
406 | const segment = line[j];
|
---|
407 | if (j > 0)
|
---|
408 | writer.write(comma);
|
---|
409 | genColumn = encodeInteger(writer, segment[0], genColumn);
|
---|
410 | if (segment.length === 1)
|
---|
411 | continue;
|
---|
412 | sourcesIndex = encodeInteger(writer, segment[1], sourcesIndex);
|
---|
413 | sourceLine = encodeInteger(writer, segment[2], sourceLine);
|
---|
414 | sourceColumn = encodeInteger(writer, segment[3], sourceColumn);
|
---|
415 | if (segment.length === 4)
|
---|
416 | continue;
|
---|
417 | namesIndex = encodeInteger(writer, segment[4], namesIndex);
|
---|
418 | }
|
---|
419 | }
|
---|
420 | return writer.flush();
|
---|
421 | }
|
---|
422 |
|
---|
423 | export { decode, decodeGeneratedRanges, decodeOriginalScopes, encode, encodeGeneratedRanges, encodeOriginalScopes };
|
---|
424 | //# sourceMappingURL=sourcemap-codec.mjs.map
|
---|