pax_global_header00006660000000000000000000000064136101303200014477gustar00rootroot0000000000000052 comment=b549c1ee22f79bdf777d7bfe6471fe4293cd6391 sourcemap-codec-1.4.8/000077500000000000000000000000001361013032000145625ustar00rootroot00000000000000sourcemap-codec-1.4.8/.eslintrc000066400000000000000000000022431361013032000164070ustar00rootroot00000000000000{ "root": true, "rules": { "indent": [ 2, "tab", { "SwitchCase": 1 } ], "semi": [ 2, "always" ], "keyword-spacing": [ 2, { "before": true, "after": true } ], "space-before-blocks": [ 2, "always" ], "space-before-function-paren": [ 2, "always" ], "no-mixed-spaces-and-tabs": [ 2, "smart-tabs" ], "no-cond-assign": 0, "no-unused-vars": 2, "object-shorthand": [ 2, "always" ], "no-const-assign": 2, "no-class-assign": 2, "no-this-before-super": 2, "no-var": 2, "no-unreachable": 2, "valid-typeof": 2, "quote-props": [ 2, "as-needed" ], "one-var": [ 2, "never" ], "prefer-arrow-callback": 2, "prefer-const": [ 2, { "destructuring": "all" } ], "arrow-spacing": 2, "no-inner-declarations": 0 }, "env": { "es6": true, "browser": true, "node": true, "mocha": true }, "extends": [ "eslint:recommended", "plugin:import/errors", "plugin:import/warnings" ], "parserOptions": { "ecmaVersion": 6, "sourceType": "module" } } sourcemap-codec-1.4.8/.gitignore000066400000000000000000000000751361013032000165540ustar00rootroot00000000000000.DS_Store node_modules dist coverage package-lock.json .idea sourcemap-codec-1.4.8/.travis.yml000066400000000000000000000002131361013032000166670ustar00rootroot00000000000000sudo: false language: node_js node_js: - "6" - "stable" env: global: - BUILD_TIMEOUT=10000 install: npm install script: npm test sourcemap-codec-1.4.8/CHANGELOG.md000066400000000000000000000023411361013032000163730ustar00rootroot00000000000000# sourcemap-codec changelog ## 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.8/LICENSE000066400000000000000000000020601361013032000155650ustar00rootroot00000000000000The 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.8/README.md000066400000000000000000000034531361013032000160460ustar00rootroot00000000000000# 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.8/appveyor.yml000066400000000000000000000007361361013032000171600ustar00rootroot00000000000000# 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.8/package.json000066400000000000000000000034311361013032000170510ustar00rootroot00000000000000{ "name": "sourcemap-codec", "version": "1.4.8", "description": "Encode/decode sourcemap mappings", "main": "dist/sourcemap-codec.umd.js", "module": "dist/sourcemap-codec.es.js", "types": "dist/types/sourcemap-codec.d.ts", "scripts": { "test": "mocha", "build": "rm -rf dist && rollup -c && tsc", "pretest": "npm run build", "prepublish": "npm test", "lint": "eslint src", "pretest-coverage": "npm run build", "test-coverage": "rm -rf coverage/* && istanbul cover --report json node_modules/.bin/_mocha -- -u exports -R spec test/test.js", "posttest-coverage": "remap-istanbul -i coverage/coverage-final.json -o coverage/coverage-remapped.json -b dist && remap-istanbul -i coverage/coverage-final.json -o coverage/coverage-remapped.lcov -t lcovonly -b dist && remap-istanbul -i coverage/coverage-final.json -o coverage/coverage-remapped -t html -b dist", "ci": "npm run test-coverage && codecov < coverage/coverage-remapped.lcov" }, "repository": { "type": "git", "url": "https://github.com/Rich-Harris/sourcemap-codec" }, "keywords": [ "sourcemap", "vlq" ], "author": "Rich Harris", "license": "MIT", "bugs": { "url": "https://github.com/Rich-Harris/sourcemap-codec/issues" }, "homepage": "https://github.com/Rich-Harris/sourcemap-codec", "dependencies": {}, "devDependencies": { "codecov.io": "^0.1.6", "console-group": "^0.3.3", "eslint": "^6.0.1", "eslint-plugin-import": "^2.18.0", "istanbul": "^0.4.5", "mocha": "^6.1.4", "remap-istanbul": "^0.13.0", "rollup": "^1.16.4", "rollup-plugin-node-resolve": "^5.2.0", "rollup-plugin-typescript": "^1.0.1", "typescript": "^3.5.2" }, "files": [ "dist/*.js", "dist/*.js.map", "dist/**/*.d.ts", "README.md" ] } sourcemap-codec-1.4.8/rollup.config.js000066400000000000000000000007461361013032000177100ustar00rootroot00000000000000import typescript from 'rollup-plugin-typescript'; import resolve from 'rollup-plugin-node-resolve'; const pkg = require( './package.json' ); export default { input: 'src/sourcemap-codec.ts', plugins: [ typescript({ exclude: 'node_modules/**', typescript: require('typescript') }), resolve({ jsnext: true }) ], output: [{ file: pkg.main, format: 'umd', name: 'sourcemapCodec', sourcemap: true }, { file: pkg.module, format: 'es', sourcemap: true }] }; sourcemap-codec-1.4.8/src/000077500000000000000000000000001361013032000153515ustar00rootroot00000000000000sourcemap-codec-1.4.8/src/sourcemap-codec.ts000066400000000000000000000074501361013032000210000ustar00rootroot00000000000000export type SourceMapSegment = | [number] | [number, number, number, number] | [number, number, number, number, number]; export type SourceMapLine = SourceMapSegment[]; export type SourceMapMappings = SourceMapLine[]; const charToInteger: { [charCode: number]: number } = {}; const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; for (let i = 0; i < chars.length; i++) { charToInteger[chars.charCodeAt(i)] = i; } export function decode(mappings: string): SourceMapMappings { const decoded: SourceMapMappings = []; let line: SourceMapLine = []; const segment: SourceMapSegment = [ 0, // generated code column 0, // source file index 0, // source code line 0, // source code column 0, // name index ]; let j = 0; for (let i = 0, shift = 0, value = 0; i < mappings.length; i++) { const c = mappings.charCodeAt(i); if (c === 44) { // "," segmentify(line, segment, j); j = 0; } else if (c === 59) { // ";" segmentify(line, segment, j); j = 0; decoded.push(line); line = []; segment[0] = 0; } else { let integer = charToInteger[c]; if (integer === undefined) { throw new Error('Invalid character (' + String.fromCharCode(c) + ')'); } const hasContinuationBit = integer & 32; integer &= 31; value += integer << shift; if (hasContinuationBit) { shift += 5; } else { const shouldNegate = value & 1; value >>>= 1; if (shouldNegate) { value = value === 0 ? -0x80000000 : -value; } segment[j] += value; j++; value = shift = 0; // reset } } } segmentify(line, segment, j); decoded.push(line); return decoded; } function segmentify(line: SourceMapSegment[], segment: SourceMapSegment, j: number) { // This looks ugly, but we're creating specialized arrays with a specific // length. This is much faster than creating a new array (which v8 expands to // a capacity of 17 after pushing the first item), or slicing out a subarray // (which is slow). Length 4 is assumed to be the most frequent, followed by // length 5 (since not everything will have an associated name), followed by // length 1 (it's probably rare for a source substring to not have an // associated segment data). if (j === 4) line.push([segment[0], segment[1], segment[2], segment[3]]); else if (j === 5) line.push([segment[0], segment[1], segment[2], segment[3], segment[4]]); else if (j === 1) line.push([segment[0]]); } export function encode(decoded: SourceMapMappings): string { let sourceFileIndex = 0; // second field let sourceCodeLine = 0; // third field let sourceCodeColumn = 0; // fourth field let nameIndex = 0; // fifth field let mappings = ''; for (let i = 0; i < decoded.length; i++) { const line = decoded[i]; if (i > 0) mappings += ';'; if (line.length === 0) continue; let generatedCodeColumn = 0; // first field const lineMappings: string[] = []; for (const segment of line) { let segmentMappings = encodeInteger(segment[0] - generatedCodeColumn); generatedCodeColumn = segment[0]; if (segment.length > 1) { segmentMappings += encodeInteger(segment[1] - sourceFileIndex) + encodeInteger(segment[2] - sourceCodeLine) + encodeInteger(segment[3] - sourceCodeColumn); sourceFileIndex = segment[1]; sourceCodeLine = segment[2]; sourceCodeColumn = segment[3]; } if (segment.length === 5) { segmentMappings += encodeInteger(segment[4] - nameIndex); nameIndex = segment[4]; } lineMappings.push(segmentMappings); } mappings += lineMappings.join(','); } return mappings; } function encodeInteger(num: number): string { var result = ''; num = num < 0 ? (-num << 1) | 1 : num << 1; do { var clamped = num & 31; num >>>= 5; if (num > 0) { clamped |= 32; } result += chars[clamped]; } while (num > 0); return result; } sourcemap-codec-1.4.8/test/000077500000000000000000000000001361013032000155415ustar00rootroot00000000000000sourcemap-codec-1.4.8/test/test.js000066400000000000000000000076751361013032000170750ustar00rootroot00000000000000const decode = require('../').decode; const encode = require('../').encode; const assert = require('assert'); require('console-group').install(); 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,IAAIA,eAAW,oCAAoCC,cACpC,aACdC,OAAOC,MAAOH,eAAU,GCFzB,5PAAIA,iBAAW,sBAAsBC,cACtB,aACdC,OAAOC,MAAOH,iBAAU,GCCzBI,IACAC", decoded: [ [ [13, 0, 0, 0], [17, 0, 0, 4, 0], [32, 0, 0, 15], [68, 0, 0, 51, 1], [82, 0, 1, 15], [95, 0, 2, 1, 2], [102, 0, 2, 8, 3], [108, 0, 2, 15, 0], [123, 0, 2, 25], [126, 1, 0, 0], [-126, 1, 0, 4, 0], [-109, 1, 0, 15], [-87, 1, 0, 37, 1], [-73, 1, 1, 15], [-60, 1, 2, 1, 2], [-53, 1, 2, 8, 3], [-47, 1, 2, 15, 0], [-30, 1, 2, 25], [-27, 2, 3, 0, 4], [-23, 2, 4, 0, 5] ] ] }, { 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); }); }); }); describe('encode()', () => { tests.forEach((test, i) => { it('encodes sample ' + i, () => { assert.deepEqual(encode(test.decoded), test.encoded); }); }); }); }); sourcemap-codec-1.4.8/tsconfig.json000066400000000000000000000004311361013032000172670ustar00rootroot00000000000000{ "compilerOptions": { "noImplicitAny": true, "diagnostics": true, "noImplicitThis": true, "noEmitOnError": true, "target": "es5", "lib": ["es5", "es6"], "declaration": true, "outDir": "dist/types" }, "include": [ "src" ], "exclude": [ "node_modules" ] }