aboutsummaryrefslogtreecommitdiff
path: root/node_modules/@jridgewell/sourcemap-codec/dist/sourcemap-codec.umd.js
blob: b5aa0c43c3e8e79d8126c3ed49602f94254c3636 (plain)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
(function (global, factory) {
    typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
    typeof define === 'function' && define.amd ? define(['exports'], factory) :
    (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.sourcemapCodec = {}));
})(this, (function (exports) { 'use strict';

    const comma = ','.charCodeAt(0);
    const semicolon = ';'.charCodeAt(0);
    const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
    const intToChar = new Uint8Array(64); // 64 possible chars.
    const charToInteger = new Uint8Array(128); // z is 122 in ASCII
    for (let i = 0; i < chars.length; i++) {
        const c = chars.charCodeAt(i);
        charToInteger[c] = i;
        intToChar[i] = c;
    }
    // Provide a fallback for older environments.
    const td = typeof TextDecoder !== 'undefined'
        ? new TextDecoder()
        : typeof Buffer !== 'undefined'
            ? {
                decode(buf) {
                    const out = Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength);
                    return out.toString();
                },
            }
            : {
                decode(buf) {
                    let out = '';
                    for (let i = 0; i < buf.length; i++) {
                        out += String.fromCharCode(buf[i]);
                    }
                    return out;
                },
            };
    function decode(mappings) {
        const state = new Int32Array(5);
        const decoded = [];
        let line = [];
        let sorted = true;
        let lastCol = 0;
        for (let i = 0; i < mappings.length;) {
            const c = mappings.charCodeAt(i);
            if (c === comma) {
                i++;
            }
            else if (c === semicolon) {
                state[0] = lastCol = 0;
                if (!sorted)
                    sort(line);
                sorted = true;
                decoded.push(line);
                line = [];
                i++;
            }
            else {
                i = decodeInteger(mappings, i, state, 0); // generatedCodeColumn
                const col = state[0];
                if (col < lastCol)
                    sorted = false;
                lastCol = col;
                if (!hasMoreSegments(mappings, i)) {
                    line.push([col]);
                    continue;
                }
                i = decodeInteger(mappings, i, state, 1); // sourceFileIndex
                i = decodeInteger(mappings, i, state, 2); // sourceCodeLine
                i = decodeInteger(mappings, i, state, 3); // sourceCodeColumn
                if (!hasMoreSegments(mappings, i)) {
                    line.push([col, state[1], state[2], state[3]]);
                    continue;
                }
                i = decodeInteger(mappings, i, state, 4); // nameIndex
                line.push([col, state[1], state[2], state[3], state[4]]);
            }
        }
        if (!sorted)
            sort(line);
        decoded.push(line);
        return decoded;
    }
    function decodeInteger(mappings, pos, state, j) {
        let value = 0;
        let shift = 0;
        let integer = 0;
        do {
            const c = mappings.charCodeAt(pos++);
            integer = charToInteger[c];
            value |= (integer & 31) << shift;
            shift += 5;
        } while (integer & 32);
        const shouldNegate = value & 1;
        value >>>= 1;
        if (shouldNegate) {
            value = -0x80000000 | -value;
        }
        state[j] += value;
        return pos;
    }
    function hasMoreSegments(mappings, i) {
        if (i >= mappings.length)
            return false;
        const c = mappings.charCodeAt(i);
        if (c === comma || c === semicolon)
            return false;
        return true;
    }
    function sort(line) {
        line.sort(sortComparator);
    }
    function sortComparator(a, b) {
        return a[0] - b[0];
    }
    function encode(decoded) {
        const state = new Int32Array(5);
        let buf = new Uint8Array(1024);
        let pos = 0;
        for (let i = 0; i < decoded.length; i++) {
            const line = decoded[i];
            if (i > 0) {
                buf = reserve(buf, pos, 1);
                buf[pos++] = semicolon;
            }
            if (line.length === 0)
                continue;
            state[0] = 0;
            for (let j = 0; j < line.length; j++) {
                const segment = line[j];
                // We can push up to 5 ints, each int can take at most 7 chars, and we
                // may push a comma.
                buf = reserve(buf, pos, 36);
                if (j > 0)
                    buf[pos++] = comma;
                pos = encodeInteger(buf, pos, state, segment, 0); // generatedCodeColumn
                if (segment.length === 1)
                    continue;
                pos = encodeInteger(buf, pos, state, segment, 1); // sourceFileIndex
                pos = encodeInteger(buf, pos, state, segment, 2); // sourceCodeLine
                pos = encodeInteger(buf, pos, state, segment, 3); // sourceCodeColumn
                if (segment.length === 4)
                    continue;
                pos = encodeInteger(buf, pos, state, segment, 4); // nameIndex
            }
        }
        return td.decode(buf.subarray(0, pos));
    }
    function reserve(buf, pos, count) {
        if (buf.length > pos + count)
            return buf;
        const swap = new Uint8Array(buf.length * 2);
        swap.set(buf);
        return swap;
    }
    function encodeInteger(buf, pos, state, segment, j) {
        const next = segment[j];
        let num = next - state[j];
        state[j] = next;
        num = num < 0 ? (-num << 1) | 1 : num << 1;
        do {
            let clamped = num & 0b011111;
            num >>>= 5;
            if (num > 0)
                clamped |= 0b100000;
            buf[pos++] = intToChar[clamped];
        } while (num > 0);
        return pos;
    }

    exports.decode = decode;
    exports.encode = encode;

    Object.defineProperty(exports, '__esModule', { value: true });

}));
//# sourceMappingURL=sourcemap-codec.umd.js.map