sourcemap-codec-1.4.11/000077500000000000000000000000001420141753500146515ustar00rootroot00000000000000sourcemap-codec-1.4.11/.editorconfig000066400000000000000000000003231420141753500173240ustar00rootroot00000000000000#root = true [*] indent_style = space end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true max_line_length = 100 indent_size = 2 [*.md] trim_trailing_whitespace = false sourcemap-codec-1.4.11/.eslintrc.js000066400000000000000000000014741420141753500171160ustar00rootroot00000000000000module.exports = { root: true, parser: '@typescript-eslint/parser', plugins: ['@typescript-eslint'], extends: [ 'eslint:recommended', 'plugin:@typescript-eslint/recommended', 'prettier', ], rules: { '@typescript-eslint/consistent-type-imports': 'error', '@typescript-eslint/no-duplicate-imports': 'error', '@typescript-eslint/no-explicit-any': 'off', '@typescript-eslint/no-non-null-assertion': 'off', '@typescript-eslint/no-unused-vars': [ 'error', { argsIgnorePattern: '^_', }, ], 'no-constant-condition': 'off', 'no-unused-labels': 'off', }, overrides: [ { files: ['test/**/*.ts'], rules: { '@typescript-eslint/no-empty-function': 'off', '@typescript-eslint/no-explicit-any': 'off', }, }, ], }; sourcemap-codec-1.4.11/.github/000077500000000000000000000000001420141753500162115ustar00rootroot00000000000000sourcemap-codec-1.4.11/.github/workflows/000077500000000000000000000000001420141753500202465ustar00rootroot00000000000000sourcemap-codec-1.4.11/.github/workflows/test.yml000066400000000000000000000016261420141753500217550ustar00rootroot00000000000000name: Node CI on: [push] jobs: build: runs-on: ubuntu-latest strategy: matrix: latest-node-version: [16.x] node-version: [6.x, 8.x, 10.x, 12.x, 14.x, 16.x] steps: - uses: actions/checkout@v1 - name: Build with Node.js ${{ matrix.latest-node-version }} uses: actions/setup-node@v1 with: node-version: ${{ matrix.latest-node-version }} - name: Build and test with latest run: | npm install npm run build npm run test:coverage - name: Switch to Node.js ${{ matrix.node-version }} uses: actions/setup-node@v1 with: node-version: ${{ matrix.node-version }} - name: Test legacy node run: | npm install mocha@6.2.3 npm run test:only - name: Benchmark run: | npm run benchmark:only env: CI: true sourcemap-codec-1.4.11/.gitignore000066400000000000000000000000751420141753500166430ustar00rootroot00000000000000.DS_Store node_modules dist coverage package-lock.json .idea sourcemap-codec-1.4.11/.travis.yml000066400000000000000000000002131420141753500167560ustar00rootroot00000000000000sudo: false language: node_js node_js: - "6" - "stable" env: global: - BUILD_TIMEOUT=10000 install: npm install script: npm test sourcemap-codec-1.4.11/CHANGELOG.md000066400000000000000000000030231420141753500164600ustar00rootroot00000000000000# sourcemap-codec changelog ## 1.4.10 * Sort mappings during decode ([f9d1b33f](https://github.com/jridgewell/sourcemap-codec/commit/f9d1b33f)) ## 1.4.9 * Performance boost ([#86](https://github.com/Rich-Harris/sourcemap-codec/pull/86)) * Add native ESM entry point ([#85](https://github.com/Rich-Harris/sourcemap-codec/pull/85)) ## 1.4.8 * Performance boost ([#80](https://github.com/Rich-Harris/sourcemap-codec/pull/80)) ## 1.4.7 * Include .map files in package ([#73](https://github.com/Rich-Harris/sourcemap-codec/issues/73)) ## 1.4.6 * Use arrays instead of typed arrays ([#79](https://github.com/Rich-Harris/sourcemap-codec/pull/79)) ## 1.4.5 * Handle overflow cases ([#78](https://github.com/Rich-Harris/sourcemap-codec/pull/78)) ## 1.4.4 * Use Uint32Array, yikes ([#77](https://github.com/Rich-Harris/sourcemap-codec/pull/77)) ## 1.4.3 * Use Uint16Array to prevent overflow ([#75](https://github.com/Rich-Harris/sourcemap-codec/pull/75)) ## 1.4.2 * GO EVEN FASTER ([#74](https://github.com/Rich-Harris/sourcemap-codec/pull/74)) ## 1.4.1 * GO FASTER ([#71](https://github.com/Rich-Harris/sourcemap-codec/pull/71)) ## 1.4.0 * Add TypeScript declarations ([#70](https://github.com/Rich-Harris/sourcemap-codec/pull/70)) ## 1.3.1 * Update build process, expose `pkg.module` ## 1.3.0 * Update build process ## 1.2.1 * Add dist files to npm package ## 1.2.0 * Add ES6 build * Update dependencies * Add test coverage ## 1.1.0 * Fix bug with lines containing single-character segments * Add tests ## 1.0.0 * First release sourcemap-codec-1.4.11/LICENSE000066400000000000000000000020601420141753500156540ustar00rootroot00000000000000The MIT License Copyright (c) 2015 Rich Harris Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. sourcemap-codec-1.4.11/README.md000066400000000000000000000034531420141753500161350ustar00rootroot00000000000000# sourcemap-codec Encode/decode the `mappings` property of a [sourcemap](https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit). ## Why? Sourcemaps are difficult to generate and manipulate, because the `mappings` property – the part that actually links the generated code back to the original source – is encoded using an obscure method called [Variable-length quantity](https://en.wikipedia.org/wiki/Variable-length_quantity). On top of that, each segment in the mapping contains offsets rather than absolute indices, which means that you can't look at a segment in isolation – you have to understand the whole sourcemap. This package makes the process slightly easier. ## Installation ```bash npm install sourcemap-codec ``` ## Usage ```js import { encode, decode } from 'sourcemap-codec'; var decoded = decode( ';EAEEA,EAAE,EAAC,CAAE;ECQY,UACC' ); assert.deepEqual( decoded, [ // the first line (of the generated code) has no mappings, // as shown by the starting semi-colon (which separates lines) [], // the second line contains four (comma-separated) segments [ // segments are encoded as you'd expect: // [ generatedCodeColumn, sourceIndex, sourceCodeLine, sourceCodeColumn, nameIndex ] // i.e. the first segment begins at column 2, and maps back to the second column // of the second line (both zero-based) of the 0th source, and uses the 0th // name in the `map.names` array [ 2, 0, 2, 2, 0 ], // the remaining segments are 4-length rather than 5-length, // because they don't map a name [ 4, 0, 2, 4 ], [ 6, 0, 2, 5 ], [ 7, 0, 2, 7 ] ], // the final line contains two segments [ [ 2, 1, 10, 19 ], [ 12, 1, 11, 20 ] ] ]); var encoded = encode( decoded ); assert.equal( encoded, ';EAEEA,EAAE,EAAC,CAAE;ECQY,UACC' ); ``` # License MIT sourcemap-codec-1.4.11/appveyor.yml000066400000000000000000000007361420141753500172470ustar00rootroot00000000000000# Test against this version of Node.js environment: matrix: # node.js - nodejs_version: "stable" # Install scripts. (runs after repo cloning) install: # Get the latest stable version of Node.js or io.js - ps: Install-Product node $env:nodejs_version # install modules - npm install # Post-install test scripts. test_script: # Output useful info for debugging. - node --version - npm --version # run tests - npm test # Don't actually build. build: off sourcemap-codec-1.4.11/package.json000066400000000000000000000040721420141753500171420ustar00rootroot00000000000000{ "name": "@jridgewell/sourcemap-codec", "version": "1.4.11", "description": "Encode/decode sourcemap mappings", "keywords": [ "sourcemap", "vlq" ], "main": "dist/sourcemap-codec.umd.js", "module": "dist/sourcemap-codec.mjs", "typings": "dist/types/sourcemap-codec.d.ts", "files": [ "dist" ], "exports": { ".": { "browser": "./dist/sourcemap-codec.umd.js", "import": "./dist/sourcemap-codec.mjs", "require": "./dist/sourcemap-codec.umd.js" }, "./package.json": "./package.json" }, "scripts": { "benchmark": "run-s build:rollup benchmark:only", "benchmark:only": "node benchmark/index.js", "build": "run-s -n build:*", "build:rollup": "rollup -c rollup.config.js", "build:ts": "tsc --project tsconfig.build.json", "lint": "run-s -n lint:*", "lint:prettier": "npm run test:lint:prettier -- --write", "lint:ts": "npm run test:lint:ts -- --fix", "prebuild": "rm -rf dist", "prepublishOnly": "npm run preversion", "preversion": "run-s test build", "pretest": "run-s build:rollup", "test": "run-s -n test:lint test:only", "test:debug": "mocha --inspect-brk", "test:lint": "run-s -n test:lint:*", "test:lint:prettier": "prettier --check '{src,test}/**/*.ts'", "test:lint:ts": "eslint '{src,test}/**/*.ts'", "test:only": "mocha", "test:coverage": "c8 mocha", "test:watch": "mocha --watch" }, "repository": { "type": "git", "url": "git+https://github.com/jridgewell/sourcemap-codec.git" }, "author": "Rich Harris", "license": "MIT", "devDependencies": { "@rollup/plugin-typescript": "8.3.0", "@types/node": "17.0.15", "@typescript-eslint/eslint-plugin": "5.10.0", "@typescript-eslint/parser": "5.10.0", "benchmark": "2.1.4", "c8": "7.11.0", "eslint": "8.7.0", "eslint-config-prettier": "8.3.0", "mocha": "9.2.0", "npm-run-all": "4.1.5", "prettier": "2.5.1", "rollup": "2.64.0", "source-map": "0.6.1", "source-map-js": "1.0.2", "sourcemap-codec": "1.4.8", "typescript": "4.5.4" } } sourcemap-codec-1.4.11/prettier.config.js000066400000000000000000000001511420141753500203060ustar00rootroot00000000000000module.exports = { endOfLine: 'lf', printWidth: 100, singleQuote: true, trailingComma: 'all', }; sourcemap-codec-1.4.11/rollup.config.js000066400000000000000000000013471420141753500177750ustar00rootroot00000000000000import typescript from '@rollup/plugin-typescript'; function configure(esm) { return { input: 'src/sourcemap-codec.ts', output: esm ? { format: 'es', dir: 'dist', entryFileNames: '[name].mjs', sourcemap: true, exports: 'named', } : { format: 'umd', name: 'sourcemapCodec', dir: 'dist', entryFileNames: '[name].umd.js', sourcemap: true, exports: 'named', }, plugins: [ typescript({ tsconfig: './tsconfig.build.json', tslib: './throw-when-needed', }), ], watch: { include: 'src/**', }, }; } export default [configure(false), configure(true)]; sourcemap-codec-1.4.11/src/000077500000000000000000000000001420141753500154405ustar00rootroot00000000000000sourcemap-codec-1.4.11/src/sourcemap-codec.ts000066400000000000000000000121051420141753500210600ustar00rootroot00000000000000export type SourceMapSegment = | [number] | [number, number, number, number] | [number, number, number, number, number]; export type SourceMapLine = SourceMapSegment[]; export type SourceMapMappings = SourceMapLine[]; 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: Uint8Array) { const out = Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength); return out.toString(); }, } : { decode(buf: Uint8Array) { let out = ''; for (let i = 0; i < buf.length; i++) { out += String.fromCharCode(buf[i]); } return out; }, }; export function decode(mappings: string): SourceMapMappings { const state: [number, number, number, number, number] = new Int32Array(5) as any; const decoded: SourceMapMappings = []; let line: SourceMapLine = []; 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: string, pos: number, state: SourceMapSegment, j: number): number { 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: string, i: number): boolean { if (i >= mappings.length) return false; const c = mappings.charCodeAt(i); if (c === comma || c === semicolon) return false; return true; } function sort(line: SourceMapSegment[]) { line.sort(sortComparator); } function sortComparator(a: SourceMapSegment, b: SourceMapSegment): number { return a[0] - b[0]; } export function encode(decoded: SourceMapMappings): string { const state: [number, number, number, number, number] = new Int32Array(5) as any; 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: Uint8Array, pos: number, count: number): Uint8Array { if (buf.length > pos + count) return buf; const swap = new Uint8Array(buf.length * 2); swap.set(buf); return swap; } function encodeInteger( buf: Uint8Array, pos: number, state: SourceMapSegment, segment: SourceMapSegment, j: number, ): number { 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; } sourcemap-codec-1.4.11/test/000077500000000000000000000000001420141753500156305ustar00rootroot00000000000000sourcemap-codec-1.4.11/test/test.js000066400000000000000000000105211420141753500171440ustar00rootroot00000000000000/* eslint-env node, mocha */ const decode = require('../').decode; const encode = require('../').encode; const assert = require('assert'); describe('sourcemap-codec', () => { // TODO more tests let tests = [ { encoded: 'AAAA', decoded: [[[0, 0, 0, 0]]], }, { encoded: ';;;', decoded: [[], [], [], []], }, { encoded: 'A,AAAA;;AACDE;', decoded: [[[0], [0, 0, 0, 0]], [], [[0, 0, 1, -1, 2]], []], }, { encoded: ';;;;EAEEA,EAAE,EAAC,CAAE;ECQY,UACC', decoded: [ [], [], [], [], [ [2, 0, 2, 2, 0], [4, 0, 2, 4], [6, 0, 2, 5], [7, 0, 2, 7], ], [ [2, 1, 10, 19], [12, 1, 11, 20], ], ], }, { encoded: 'AAAA,aAEA,IAAIA,eAAiB,oCAAoCC,cACzD,SAASC,IACRC,OAAOC,MAAOJ,eAAgB,GAG/B,IAAIK,iBAAmB,sBAAsBJ,cAC7C,SAASK,IACRH,OAAOC,MAAOC,iBAAkB,GAGjCH,IACAI', decoded: [ [ [0, 0, 0, 0], [13, 0, 2, 0], [17, 0, 2, 4, 0], [32, 0, 2, 21], [68, 0, 2, 57, 1], [82, 0, 3, 0], [91, 0, 3, 9, 2], [95, 0, 4, 1, 3], [102, 0, 4, 8, 4], [108, 0, 4, 15, 0], [123, 0, 4, 31], [126, 0, 7, 0], [130, 0, 7, 4, 5], [147, 0, 7, 23], [169, 0, 7, 45, 1], [183, 0, 8, 0], [192, 0, 8, 9, 6], [196, 0, 9, 1, 3], [203, 0, 9, 8, 4], [209, 0, 9, 15, 5], [226, 0, 9, 33], [229, 0, 12, 0, 2], [233, 0, 13, 0, 6], ], ], }, { encoded: 'CAAC,SAAUA,EAAQC,GACC,iBAAZC,SAA0C,oBAAXC,OAAyBF,IAC7C,mBAAXG,QAAyBA,OAAOC,IAAMD,OAAOH,GACnDA,IAHF,CAIEK,EAAM,WAAe,aAEtB,IAAIC,EAAiB,oCAAoCC,cAKzD,IAAIC,EAAmB,sBAAsBD,cAH5CE,OAAOC,MAAOJ,EAAgB,GAK9BG,OAAOC,MAAOF,EAAkB', decoded: [ [ [1, 0, 0, 1], [10, 0, 0, 11, 0], [12, 0, 0, 19, 1], [15, 0, 1, 20], [32, 0, 1, 8, 2], [41, 0, 1, 50], [61, 0, 1, 39, 3], [68, 0, 1, 64, 1], [72, 0, 2, 19], [91, 0, 2, 8, 4], [99, 0, 2, 33, 4], [106, 0, 2, 40, 5], [110, 0, 2, 46, 4], [117, 0, 2, 53, 1], [120, 0, 3, 2, 1], [124, 0, 0, 0], [125, 0, 4, 2, 6], [127, 0, 4, 8], [138, 0, 4, 23], [151, 0, 6, 1], [155, 0, 6, 5, 7], [157, 0, 6, 22], [193, 0, 6, 58, 8], [207, 0, 11, 1], [211, 0, 11, 5, 9], [213, 0, 11, 24], [235, 0, 11, 46, 8], [249, 0, 8, 2, 10], [256, 0, 8, 9, 11], [262, 0, 8, 16, 7], [264, 0, 8, 32], [267, 0, 13, 2, 10], [274, 0, 13, 9, 11], [280, 0, 13, 16, 9], [282, 0, 13, 34], ], ], }, { // Make sure Int16 isn't being used encoded: 'gw+BAAAA,w+BAAAA,w+BAAAA,w+BAAAA', decoded: [ [ [32000, 0, 0, 0, 0], [33000, 0, 0, 0, 0], [34000, 0, 0, 0, 0], [35000, 0, 0, 0, 0], ], ], }, { // Handle largest 32bit int encoded: '+/////D', decoded: [[[2147483647]]], }, { // Handle smallest 32bit int encoded: 'B', decoded: [[[-2147483648]]], }, ]; const filtered = tests.filter((test) => { return test.solo; }); tests = filtered.length ? filtered : tests; describe('decode()', () => { tests.forEach((test, i) => { it('decodes sample ' + i, () => { assert.deepEqual(decode(test.encoded), test.decoded); }); }); it('sorts during decoding', () => { const mappings = [ [[1], [2], [3]], [[1], [3], [2]], [[2], [1], [3]], [[2], [3], [1]], [[3], [1], [2]], [[3], [2], [1]], ]; mappings.sort(() => (Math.random() < 0.5 ? 1 : -1)); const decoded = decode(encode(mappings)); const expected = mappings.map(() => { return [[1], [2], [3]]; }); assert.deepEqual(decoded, expected); }); }); describe('encode()', () => { tests.forEach((test, i) => { it('encodes sample ' + i, () => { assert.deepEqual(encode(test.decoded), test.encoded); }); }); }); }); sourcemap-codec-1.4.11/tsconfig.build.json000066400000000000000000000002231420141753500204530ustar00rootroot00000000000000{ "extends": "./tsconfig.json", "compilerOptions": { "emitDeclarationOnly": true, "rootDir": "src" }, "include": [ "src" ] } sourcemap-codec-1.4.11/tsconfig.json000066400000000000000000000006641420141753500173660ustar00rootroot00000000000000{ "compilerOptions": { "moduleResolution": "node", "esModuleInterop": true, "target": "es2015", "module": "es2015", "lib": ["es2015", "dom"], "strict": true, "sourceMap": true, "declaration": true, "allowSyntheticDefaultImports": true, "declarationDir": "dist/types", "outDir": "dist/lib", "typeRoots": ["node_modules/@types"] }, "exclude": ["dist"], "include": ["src", "test"] }