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