sourcemap-codec.umd.js 17 KB

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