sourcemap-codec.mjs 14 KB

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