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