blob: bec92a9c61a0cd462aa53fa4a2f0cf94a6f8436e [file] [log] [blame]
Samuel Shuert274a4d62023-12-01 15:04:55 -05001(function (global, factory) {
2 typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3 typeof define === 'function' && define.amd ? define(['exports'], factory) :
4 (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.sourcemapCodec = {}));
5})(this, (function (exports) { 'use strict';
6
7 const comma = ','.charCodeAt(0);
8 const semicolon = ';'.charCodeAt(0);
9 const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
10 const intToChar = new Uint8Array(64); // 64 possible chars.
11 const charToInt = new Uint8Array(128); // z is 122 in ASCII
12 for (let i = 0; i < chars.length; i++) {
13 const c = chars.charCodeAt(i);
14 intToChar[i] = c;
15 charToInt[c] = i;
16 }
17 // Provide a fallback for older environments.
18 const td = typeof TextDecoder !== 'undefined'
19 ? /* #__PURE__ */ new TextDecoder()
20 : typeof Buffer !== 'undefined'
21 ? {
22 decode(buf) {
23 const out = Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength);
24 return out.toString();
25 },
26 }
27 : {
28 decode(buf) {
29 let out = '';
30 for (let i = 0; i < buf.length; i++) {
31 out += String.fromCharCode(buf[i]);
32 }
33 return out;
34 },
35 };
36 function decode(mappings) {
37 const state = new Int32Array(5);
38 const decoded = [];
39 let index = 0;
40 do {
41 const semi = indexOf(mappings, index);
42 const line = [];
43 let sorted = true;
44 let lastCol = 0;
45 state[0] = 0;
46 for (let i = index; i < semi; i++) {
47 let seg;
48 i = decodeInteger(mappings, i, state, 0); // genColumn
49 const col = state[0];
50 if (col < lastCol)
51 sorted = false;
52 lastCol = col;
53 if (hasMoreVlq(mappings, i, semi)) {
54 i = decodeInteger(mappings, i, state, 1); // sourcesIndex
55 i = decodeInteger(mappings, i, state, 2); // sourceLine
56 i = decodeInteger(mappings, i, state, 3); // sourceColumn
57 if (hasMoreVlq(mappings, i, semi)) {
58 i = decodeInteger(mappings, i, state, 4); // namesIndex
59 seg = [col, state[1], state[2], state[3], state[4]];
60 }
61 else {
62 seg = [col, state[1], state[2], state[3]];
63 }
64 }
65 else {
66 seg = [col];
67 }
68 line.push(seg);
69 }
70 if (!sorted)
71 sort(line);
72 decoded.push(line);
73 index = semi + 1;
74 } while (index <= mappings.length);
75 return decoded;
76 }
77 function indexOf(mappings, index) {
78 const idx = mappings.indexOf(';', index);
79 return idx === -1 ? mappings.length : idx;
80 }
81 function decodeInteger(mappings, pos, state, j) {
82 let value = 0;
83 let shift = 0;
84 let integer = 0;
85 do {
86 const c = mappings.charCodeAt(pos++);
87 integer = charToInt[c];
88 value |= (integer & 31) << shift;
89 shift += 5;
90 } while (integer & 32);
91 const shouldNegate = value & 1;
92 value >>>= 1;
93 if (shouldNegate) {
94 value = -0x80000000 | -value;
95 }
96 state[j] += value;
97 return pos;
98 }
99 function hasMoreVlq(mappings, i, length) {
100 if (i >= length)
101 return false;
102 return mappings.charCodeAt(i) !== comma;
103 }
104 function sort(line) {
105 line.sort(sortComparator);
106 }
107 function sortComparator(a, b) {
108 return a[0] - b[0];
109 }
110 function encode(decoded) {
111 const state = new Int32Array(5);
112 const bufLength = 1024 * 16;
113 const subLength = bufLength - 36;
114 const buf = new Uint8Array(bufLength);
115 const sub = buf.subarray(0, subLength);
116 let pos = 0;
117 let out = '';
118 for (let i = 0; i < decoded.length; i++) {
119 const line = decoded[i];
120 if (i > 0) {
121 if (pos === bufLength) {
122 out += td.decode(buf);
123 pos = 0;
124 }
125 buf[pos++] = semicolon;
126 }
127 if (line.length === 0)
128 continue;
129 state[0] = 0;
130 for (let j = 0; j < line.length; j++) {
131 const segment = line[j];
132 // We can push up to 5 ints, each int can take at most 7 chars, and we
133 // may push a comma.
134 if (pos > subLength) {
135 out += td.decode(sub);
136 buf.copyWithin(0, subLength, pos);
137 pos -= subLength;
138 }
139 if (j > 0)
140 buf[pos++] = comma;
141 pos = encodeInteger(buf, pos, state, segment, 0); // genColumn
142 if (segment.length === 1)
143 continue;
144 pos = encodeInteger(buf, pos, state, segment, 1); // sourcesIndex
145 pos = encodeInteger(buf, pos, state, segment, 2); // sourceLine
146 pos = encodeInteger(buf, pos, state, segment, 3); // sourceColumn
147 if (segment.length === 4)
148 continue;
149 pos = encodeInteger(buf, pos, state, segment, 4); // namesIndex
150 }
151 }
152 return out + td.decode(buf.subarray(0, pos));
153 }
154 function encodeInteger(buf, pos, state, segment, j) {
155 const next = segment[j];
156 let num = next - state[j];
157 state[j] = next;
158 num = num < 0 ? (-num << 1) | 1 : num << 1;
159 do {
160 let clamped = num & 0b011111;
161 num >>>= 5;
162 if (num > 0)
163 clamped |= 0b100000;
164 buf[pos++] = intToChar[clamped];
165 } while (num > 0);
166 return pos;
167 }
168
169 exports.decode = decode;
170 exports.encode = encode;
171
172 Object.defineProperty(exports, '__esModule', { value: true });
173
174}));
175//# sourceMappingURL=sourcemap-codec.umd.js.map