-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsourceMapParse.js
More file actions
107 lines (104 loc) · 3.01 KB
/
sourceMapParse.js
File metadata and controls
107 lines (104 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const base64Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
let base64Mapping = {};
for (let i = 0; i < base64Alphabet.length; i++) {
base64Mapping[base64Alphabet[i]] = i;
}
function parse6BitVLQs(base64Part) {
let groups = Array.from(base64Part).map(x => base64Mapping[x]);
let nums = [];
let index = 0;
while (index < groups.length) {
nums.push(parseNumber());
}
return nums;
function parseNumber() {
let num = 0;
let negative = false;
let first = true;
let magnitude = 1;
while (true) {
if (index >= groups.length) {
throw new Error(`Invalid VLQ group`);
}
let group = groups[index++];
let doContinue = !!(group & 0b100000);
let value = group & 0b011111;
if (first) {
negative = !!(value & 0b1);
value = value >> 1;
}
num += value * magnitude;
if (first) {
magnitude = magnitude << 4;
}
else {
magnitude = magnitude << 5;
}
first = false;
if (!doContinue) {
break;
}
}
if (negative) {
num = -num;
}
return num;
}
}
module.exports.encode6BitVLQ = encode6BitVLQ;
function encode6BitVLQ(number) {
let output = "";
let negative = number < 0;
number = Math.abs(number);
let magnitude = 4;
while (true) {
let curValue = number & ((1 << magnitude) - 1);
number = number >> magnitude;
if (output === "") {
curValue = curValue << 1;
if (negative) {
curValue = curValue | 1;
}
magnitude = 5;
}
if (number > 0) {
curValue = curValue | (1 << 5);
}
output += base64Alphabet[curValue];
if (number === 0) {
break;
}
}
return output;
}
module.exports.parseMappingsPart = parseMappingsPart;
function parseMappingsPart(base64Part) {
let values = parse6BitVLQs(base64Part);
// https://sourcemaps.info/spec.html#h.qz3o9nc69um5
let objPart = {
newColumn: values[0],
sourcesIndex: values.length > 1 ? values[1] : 0,
originalLine: values.length > 2 ? values[2] : 0,
originalColumn: values.length > 3 ? values[3] : 0,
namesIndex: values.length > 4 ? values[4] : 0,
};
return objPart;
}
module.exports.encodeMappingsPart = encodeMappingsPart;
function encodeMappingsPart(mapping, first, usesNames) {
let parts = [
encode6BitVLQ(mapping.newColumn),
encode6BitVLQ(mapping.sourcesIndex),
encode6BitVLQ(mapping.originalLine),
encode6BitVLQ(mapping.originalColumn),
];
if (usesNames) {
parts.push(encode6BitVLQ(mapping.namesIndex));
}
if (!first) {
while (parts[parts.length] === "A") {
parts.pop();
}
}
return parts.join("");
}