sourcemap-codec.mjs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 charToInteger = new Uint8Array(128); // z is 122 in ASCII
  6. for (let i = 0; i < chars.length; i++) {
  7. const c = chars.charCodeAt(i);
  8. charToInteger[c] = i;
  9. intToChar[i] = c;
  10. }
  11. // Provide a fallback for older environments.
  12. const td = typeof TextDecoder !== 'undefined'
  13. ? 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 line = [];
  34. let sorted = true;
  35. let lastCol = 0;
  36. for (let i = 0; i < mappings.length;) {
  37. const c = mappings.charCodeAt(i);
  38. if (c === comma) {
  39. i++;
  40. }
  41. else if (c === semicolon) {
  42. state[0] = lastCol = 0;
  43. if (!sorted)
  44. sort(line);
  45. sorted = true;
  46. decoded.push(line);
  47. line = [];
  48. i++;
  49. }
  50. else {
  51. i = decodeInteger(mappings, i, state, 0); // generatedCodeColumn
  52. const col = state[0];
  53. if (col < lastCol)
  54. sorted = false;
  55. lastCol = col;
  56. if (!hasMoreSegments(mappings, i)) {
  57. line.push([col]);
  58. continue;
  59. }
  60. i = decodeInteger(mappings, i, state, 1); // sourceFileIndex
  61. i = decodeInteger(mappings, i, state, 2); // sourceCodeLine
  62. i = decodeInteger(mappings, i, state, 3); // sourceCodeColumn
  63. if (!hasMoreSegments(mappings, i)) {
  64. line.push([col, state[1], state[2], state[3]]);
  65. continue;
  66. }
  67. i = decodeInteger(mappings, i, state, 4); // nameIndex
  68. line.push([col, state[1], state[2], state[3], state[4]]);
  69. }
  70. }
  71. if (!sorted)
  72. sort(line);
  73. decoded.push(line);
  74. return decoded;
  75. }
  76. function decodeInteger(mappings, pos, state, j) {
  77. let value = 0;
  78. let shift = 0;
  79. let integer = 0;
  80. do {
  81. const c = mappings.charCodeAt(pos++);
  82. integer = charToInteger[c];
  83. value |= (integer & 31) << shift;
  84. shift += 5;
  85. } while (integer & 32);
  86. const shouldNegate = value & 1;
  87. value >>>= 1;
  88. if (shouldNegate) {
  89. value = -0x80000000 | -value;
  90. }
  91. state[j] += value;
  92. return pos;
  93. }
  94. function hasMoreSegments(mappings, i) {
  95. if (i >= mappings.length)
  96. return false;
  97. const c = mappings.charCodeAt(i);
  98. if (c === comma || c === semicolon)
  99. return false;
  100. return true;
  101. }
  102. function sort(line) {
  103. line.sort(sortComparator);
  104. }
  105. function sortComparator(a, b) {
  106. return a[0] - b[0];
  107. }
  108. function encode(decoded) {
  109. const state = new Int32Array(5);
  110. let buf = new Uint8Array(1024);
  111. let pos = 0;
  112. for (let i = 0; i < decoded.length; i++) {
  113. const line = decoded[i];
  114. if (i > 0) {
  115. buf = reserve(buf, pos, 1);
  116. buf[pos++] = semicolon;
  117. }
  118. if (line.length === 0)
  119. continue;
  120. state[0] = 0;
  121. for (let j = 0; j < line.length; j++) {
  122. const segment = line[j];
  123. // We can push up to 5 ints, each int can take at most 7 chars, and we
  124. // may push a comma.
  125. buf = reserve(buf, pos, 36);
  126. if (j > 0)
  127. buf[pos++] = comma;
  128. pos = encodeInteger(buf, pos, state, segment, 0); // generatedCodeColumn
  129. if (segment.length === 1)
  130. continue;
  131. pos = encodeInteger(buf, pos, state, segment, 1); // sourceFileIndex
  132. pos = encodeInteger(buf, pos, state, segment, 2); // sourceCodeLine
  133. pos = encodeInteger(buf, pos, state, segment, 3); // sourceCodeColumn
  134. if (segment.length === 4)
  135. continue;
  136. pos = encodeInteger(buf, pos, state, segment, 4); // nameIndex
  137. }
  138. }
  139. return td.decode(buf.subarray(0, pos));
  140. }
  141. function reserve(buf, pos, count) {
  142. if (buf.length > pos + count)
  143. return buf;
  144. const swap = new Uint8Array(buf.length * 2);
  145. swap.set(buf);
  146. return swap;
  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. export { decode, encode };
  163. //# sourceMappingURL=sourcemap-codec.mjs.map