pax_global_header00006660000000000000000000000064132376132550014521gustar00rootroot0000000000000052 comment=97c3a7bc023287f0b927421210484371123ef9bf sourcemap-codec-1.4.0/000077500000000000000000000000001323761325500145745ustar00rootroot00000000000000sourcemap-codec-1.4.0/.eslintrc000066400000000000000000000022431323761325500164210ustar00rootroot00000000000000{ "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.0/.gitignore000066400000000000000000000000661323761325500165660ustar00rootroot00000000000000.DS_Store node_modules dist coverage package-lock.jsonsourcemap-codec-1.4.0/.travis.yml000066400000000000000000000002131323761325500167010ustar00rootroot00000000000000sudo: false language: node_js node_js: - "6" - "stable" env: global: - BUILD_TIMEOUT=10000 install: npm install script: npm test sourcemap-codec-1.4.0/CHANGELOG.md000066400000000000000000000006721323761325500164120ustar00rootroot00000000000000# sourcemap-codec changelog ## 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.0/LICENSE000066400000000000000000000020601323761325500155770ustar00rootroot00000000000000The 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.0/README.md000066400000000000000000000034531323761325500160600ustar00rootroot00000000000000# 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.0/appveyor.yml000066400000000000000000000007311323761325500171650ustar00rootroot00000000000000# Test against this version of Node.js environment: matrix: # node.js - nodejs_version: "5" # 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.0/package.json000066400000000000000000000034321323761325500170640ustar00rootroot00000000000000{ "name": "sourcemap-codec", "version": "1.4.0", "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": { "vlq": "^1.0.0" }, "devDependencies": { "codecov.io": "^0.1.6", "console-group": "^0.3.3", "eslint": "^3.19.0", "eslint-plugin-import": "^2.2.0", "istanbul": "^0.4.5", "mocha": "^3.2.0", "remap-istanbul": "^0.9.5", "rollup": "^0.54.0", "rollup-plugin-node-resolve": "^3.0.0", "rollup-plugin-typescript": "^0.8.1", "typescript": "^2.7.1" }, "files": [ "dist/*.js", "dist/**/*.d.ts", "README.md" ] } sourcemap-codec-1.4.0/rollup.config.js000066400000000000000000000007461323761325500177220ustar00rootroot00000000000000import 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.0/src/000077500000000000000000000000001323761325500153635ustar00rootroot00000000000000sourcemap-codec-1.4.0/src/sourcemap-codec.ts000066400000000000000000000060721323761325500210110ustar00rootroot00000000000000import { decode as decodeVlq, encode as encodeVlq } from 'vlq'; export type SourceMapSegment = [number] | [number, number, number, number] | [number, number, number, number, number]; export type SourceMapLine = SourceMapSegment[]; export type SourceMapMappings = SourceMapLine[]; function decodeSegments ( encodedSegments: string[] ): number[][] { let i = encodedSegments.length; const segments = new Array( i ); while ( i-- ) segments[i] = decodeVlq( encodedSegments[i] ); return segments; } export function decode ( mappings: string ): SourceMapMappings { let sourceFileIndex = 0; // second field let sourceCodeLine = 0; // third field let sourceCodeColumn = 0; // fourth field let nameIndex = 0; // fifth field const lines = mappings.split( ';' ); const numLines = lines.length; const decoded = new Array( numLines ); let i: number; let j: number; let line: string; let generatedCodeColumn: number; let decodedLine: SourceMapLine; let segments: number[][]; let segment: number[]; let result: SourceMapSegment; for ( i = 0; i < numLines; i += 1 ) { line = lines[i]; generatedCodeColumn = 0; // first field - reset each time decodedLine = []; segments = decodeSegments( line.split( ',' ) ); for ( j = 0; j < segments.length; j += 1 ) { segment = segments[j]; if ( !segment.length ) { break; } generatedCodeColumn += segment[0]; result = [ generatedCodeColumn ]; decodedLine.push( result ); if ( segment.length === 1 ) { // only one field! continue; } sourceFileIndex += segment[1]; sourceCodeLine += segment[2]; sourceCodeColumn += segment[3]; result.push( sourceFileIndex, sourceCodeLine, sourceCodeColumn ); if ( segment.length === 5 ) { nameIndex += segment[4]; result.push( nameIndex ); } } decoded[i] = decodedLine; } return decoded; } export function encode ( decoded: SourceMapMappings ): string { const offsets = { generatedCodeColumn: 0, sourceFileIndex: 0, // second field sourceCodeLine: 0, // third field sourceCodeColumn: 0, // fourth field nameIndex: 0 // fifth field }; return decoded.map( line => { offsets.generatedCodeColumn = 0; // first field - reset each time return line.map( encodeSegment ).join( ',' ); }).join( ';' ); function encodeSegment ( segment: SourceMapSegment ): string { if ( !segment.length ) { return ''; } const result = new Array( segment.length ); result[0] = segment[0] - offsets.generatedCodeColumn; offsets.generatedCodeColumn = segment[0]; if ( segment.length === 1 ) { // only one field! return encodeVlq( result ); } result[1] = segment[1] - offsets.sourceFileIndex; result[2] = segment[2] - offsets.sourceCodeLine; result[3] = segment[3] - offsets.sourceCodeColumn; offsets.sourceFileIndex = segment[1]; offsets.sourceCodeLine = segment[2]; offsets.sourceCodeColumn = segment[3]; if ( segment.length === 5 ) { result[4] = segment[4] - offsets.nameIndex; offsets.nameIndex = segment[4]; } return encodeVlq( result ); } } sourcemap-codec-1.4.0/test/000077500000000000000000000000001323761325500155535ustar00rootroot00000000000000sourcemap-codec-1.4.0/test/test.js000066400000000000000000000023351323761325500170730ustar00rootroot00000000000000var decode = require( '../' ).decode; var encode = require( '../' ).encode; var assert = require( 'assert' ); require( 'console-group' ).install(); describe( 'sourcemap-codec', function () { // TODO more tests var 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 ] ] ] } ]; var filtered = tests.filter( function ( test ) { return test.solo; }); tests = filtered.length ? filtered : tests; describe( 'decode()', function () { tests.forEach( function ( test, i ) { it( 'decodes sample ' + i, function () { assert.deepEqual( decode( test.encoded ), test.decoded ); }); }); }); describe( 'encode()', function () { tests.forEach( function ( test, i ) { it( 'encodes sample ' + i, function () { assert.deepEqual( encode( test.decoded ), test.encoded ); }); }); }); }); sourcemap-codec-1.4.0/tsconfig.json000066400000000000000000000004311323761325500173010ustar00rootroot00000000000000{ "compilerOptions": { "noImplicitAny": true, "diagnostics": true, "noImplicitThis": true, "noEmitOnError": true, "target": "es5", "lib": ["es5", "es6"], "declaration": true, "outDir": "dist/types" }, "include": [ "src" ], "exclude": [ "node_modules" ] }