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