sourcemap-codec.umd.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. const comma = ','.charCodeAt(0);
  7. const semicolon = ';'.charCodeAt(0);
  8. const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  9. const intToChar = new Uint8Array(64); // 64 possible chars.
  10. const charToInteger = new Uint8Array(128); // z is 122 in ASCII
  11. for (let i = 0; i < chars.length; i++) {
  12. const c = chars.charCodeAt(i);
  13. charToInteger[c] = i;
  14. intToChar[i] = c;
  15. }
  16. // Provide a fallback for older environments.
  17. const td = typeof TextDecoder !== 'undefined'
  18. ? new TextDecoder()
  19. : typeof Buffer !== 'undefined'
  20. ? {
  21. decode(buf) {
  22. const out = Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength);
  23. return out.toString();
  24. },
  25. }
  26. : {
  27. decode(buf) {
  28. let out = '';
  29. for (let i = 0; i < buf.length; i++) {
  30. out += String.fromCharCode(buf[i]);
  31. }
  32. return out;
  33. },
  34. };
  35. function decode(mappings) {
  36. const state = new Int32Array(5);
  37. const decoded = [];
  38. let line = [];
  39. let sorted = true;
  40. let lastCol = 0;
  41. for (let i = 0; i < mappings.length;) {
  42. const c = mappings.charCodeAt(i);
  43. if (c === comma) {
  44. i++;
  45. }
  46. else if (c === semicolon) {
  47. state[0] = lastCol = 0;
  48. if (!sorted)
  49. sort(line);
  50. sorted = true;
  51. decoded.push(line);
  52. line = [];
  53. i++;
  54. }
  55. else {
  56. i = decodeInteger(mappings, i, state, 0); // generatedCodeColumn
  57. const col = state[0];
  58. if (col < lastCol)
  59. sorted = false;
  60. lastCol = col;
  61. if (!hasMoreSegments(mappings, i)) {
  62. line.push([col]);
  63. continue;
  64. }
  65. i = decodeInteger(mappings, i, state, 1); // sourceFileIndex
  66. i = decodeInteger(mappings, i, state, 2); // sourceCodeLine
  67. i = decodeInteger(mappings, i, state, 3); // sourceCodeColumn
  68. if (!hasMoreSegments(mappings, i)) {
  69. line.push([col, state[1], state[2], state[3]]);
  70. continue;
  71. }
  72. i = decodeInteger(mappings, i, state, 4); // nameIndex
  73. line.push([col, state[1], state[2], state[3], state[4]]);
  74. }
  75. }
  76. if (!sorted)
  77. sort(line);
  78. decoded.push(line);
  79. return decoded;
  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 = charToInteger[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 hasMoreSegments(mappings, i) {
  100. if (i >= mappings.length)
  101. return false;
  102. const c = mappings.charCodeAt(i);
  103. if (c === comma || c === semicolon)
  104. return false;
  105. return true;
  106. }
  107. function sort(line) {
  108. line.sort(sortComparator);
  109. }
  110. function sortComparator(a, b) {
  111. return a[0] - b[0];
  112. }
  113. function encode(decoded) {
  114. const state = new Int32Array(5);
  115. let buf = new Uint8Array(1024);
  116. let pos = 0;
  117. for (let i = 0; i < decoded.length; i++) {
  118. const line = decoded[i];
  119. if (i > 0) {
  120. buf = reserve(buf, pos, 1);
  121. buf[pos++] = semicolon;
  122. }
  123. if (line.length === 0)
  124. continue;
  125. state[0] = 0;
  126. for (let j = 0; j < line.length; j++) {
  127. const segment = line[j];
  128. // We can push up to 5 ints, each int can take at most 7 chars, and we
  129. // may push a comma.
  130. buf = reserve(buf, pos, 36);
  131. if (j > 0)
  132. buf[pos++] = comma;
  133. pos = encodeInteger(buf, pos, state, segment, 0); // generatedCodeColumn
  134. if (segment.length === 1)
  135. continue;
  136. pos = encodeInteger(buf, pos, state, segment, 1); // sourceFileIndex
  137. pos = encodeInteger(buf, pos, state, segment, 2); // sourceCodeLine
  138. pos = encodeInteger(buf, pos, state, segment, 3); // sourceCodeColumn
  139. if (segment.length === 4)
  140. continue;
  141. pos = encodeInteger(buf, pos, state, segment, 4); // nameIndex
  142. }
  143. }
  144. return td.decode(buf.subarray(0, pos));
  145. }
  146. function reserve(buf, pos, count) {
  147. if (buf.length > pos + count)
  148. return buf;
  149. const swap = new Uint8Array(buf.length * 2);
  150. swap.set(buf);
  151. return swap;
  152. }
  153. function encodeInteger(buf, pos, state, segment, j) {
  154. const next = segment[j];
  155. let num = next - state[j];
  156. state[j] = next;
  157. num = num < 0 ? (-num << 1) | 1 : num << 1;
  158. do {
  159. let clamped = num & 0b011111;
  160. num >>>= 5;
  161. if (num > 0)
  162. clamped |= 0b100000;
  163. buf[pos++] = intToChar[clamped];
  164. } while (num > 0);
  165. return pos;
  166. }
  167. exports.decode = decode;
  168. exports.encode = encode;
  169. Object.defineProperty(exports, '__esModule', { value: true });
  170. }));
  171. //# sourceMappingURL=sourcemap-codec.umd.js.map