sourcemap-codec.es.js 4.1 KB

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