sourcemap-codec.umd.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  3. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  4. (global = global || self, factory(global.sourcemapCodec = {}));
  5. }(this, function (exports) { 'use strict';
  6. var charToInteger = {};
  7. var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  8. for (var i = 0; i < chars.length; i++) {
  9. charToInteger[chars.charCodeAt(i)] = i;
  10. }
  11. function decode(mappings) {
  12. var decoded = [];
  13. var line = [];
  14. var segment = [
  15. 0,
  16. 0,
  17. 0,
  18. 0,
  19. 0,
  20. ];
  21. var j = 0;
  22. for (var i = 0, shift = 0, value = 0; i < mappings.length; i++) {
  23. var c = mappings.charCodeAt(i);
  24. if (c === 44) { // ","
  25. segmentify(line, segment, j);
  26. j = 0;
  27. }
  28. else if (c === 59) { // ";"
  29. segmentify(line, segment, j);
  30. j = 0;
  31. decoded.push(line);
  32. line = [];
  33. segment[0] = 0;
  34. }
  35. else {
  36. var integer = charToInteger[c];
  37. if (integer === undefined) {
  38. throw new Error('Invalid character (' + String.fromCharCode(c) + ')');
  39. }
  40. var hasContinuationBit = integer & 32;
  41. integer &= 31;
  42. value += integer << shift;
  43. if (hasContinuationBit) {
  44. shift += 5;
  45. }
  46. else {
  47. var shouldNegate = value & 1;
  48. value >>>= 1;
  49. if (shouldNegate) {
  50. value = value === 0 ? -0x80000000 : -value;
  51. }
  52. segment[j] += value;
  53. j++;
  54. value = shift = 0; // reset
  55. }
  56. }
  57. }
  58. segmentify(line, segment, j);
  59. decoded.push(line);
  60. return decoded;
  61. }
  62. function segmentify(line, segment, j) {
  63. // This looks ugly, but we're creating specialized arrays with a specific
  64. // length. This is much faster than creating a new array (which v8 expands to
  65. // a capacity of 17 after pushing the first item), or slicing out a subarray
  66. // (which is slow). Length 4 is assumed to be the most frequent, followed by
  67. // length 5 (since not everything will have an associated name), followed by
  68. // length 1 (it's probably rare for a source substring to not have an
  69. // associated segment data).
  70. if (j === 4)
  71. line.push([segment[0], segment[1], segment[2], segment[3]]);
  72. else if (j === 5)
  73. line.push([segment[0], segment[1], segment[2], segment[3], segment[4]]);
  74. else if (j === 1)
  75. line.push([segment[0]]);
  76. }
  77. function encode(decoded) {
  78. var sourceFileIndex = 0; // second field
  79. var sourceCodeLine = 0; // third field
  80. var sourceCodeColumn = 0; // fourth field
  81. var nameIndex = 0; // fifth field
  82. var mappings = '';
  83. for (var i = 0; i < decoded.length; i++) {
  84. var line = decoded[i];
  85. if (i > 0)
  86. mappings += ';';
  87. if (line.length === 0)
  88. continue;
  89. var generatedCodeColumn = 0; // first field
  90. var lineMappings = [];
  91. for (var _i = 0, line_1 = line; _i < line_1.length; _i++) {
  92. var segment = line_1[_i];
  93. var segmentMappings = encodeInteger(segment[0] - generatedCodeColumn);
  94. generatedCodeColumn = segment[0];
  95. if (segment.length > 1) {
  96. segmentMappings +=
  97. encodeInteger(segment[1] - sourceFileIndex) +
  98. encodeInteger(segment[2] - sourceCodeLine) +
  99. encodeInteger(segment[3] - sourceCodeColumn);
  100. sourceFileIndex = segment[1];
  101. sourceCodeLine = segment[2];
  102. sourceCodeColumn = segment[3];
  103. }
  104. if (segment.length === 5) {
  105. segmentMappings += encodeInteger(segment[4] - nameIndex);
  106. nameIndex = segment[4];
  107. }
  108. lineMappings.push(segmentMappings);
  109. }
  110. mappings += lineMappings.join(',');
  111. }
  112. return mappings;
  113. }
  114. function encodeInteger(num) {
  115. var result = '';
  116. num = num < 0 ? (-num << 1) | 1 : num << 1;
  117. do {
  118. var clamped = num & 31;
  119. num >>>= 5;
  120. if (num > 0) {
  121. clamped |= 32;
  122. }
  123. result += chars[clamped];
  124. } while (num > 0);
  125. return result;
  126. }
  127. exports.decode = decode;
  128. exports.encode = encode;
  129. Object.defineProperty(exports, '__esModule', { value: true });
  130. }));
  131. //# sourceMappingURL=sourcemap-codec.umd.js.map