Comparing version 0.3.2 to 0.3.4
#!/usr/bin/env node | ||
var minimist = require( 'minimist' ), | ||
showHelp = require( './showHelp' ), | ||
command, | ||
sorcery = require( '../' ); | ||
var path = require( 'path' ); | ||
var minimist = require( 'minimist' ); | ||
var sander = require( 'sander' ); | ||
var showHelp = require( './showHelp' ); | ||
var command; | ||
var sorcery = require( '../' ); | ||
@@ -36,6 +38,26 @@ command = minimist( process.argv.slice( 2 ), { | ||
else { | ||
sorcery.load( command.input ).then( function ( chain ) { | ||
return chain.write( command.output || command.input, { | ||
inline: command.datauri, | ||
includeContent: !command.excludeContent | ||
sander.stat( command.input ).then( function ( stats ) { | ||
if ( stats.isDirectory() ) { | ||
return sander.lsr( command.input ).then( function ( files ) { | ||
var promises = files.map( function ( file ) { | ||
var input = path.join( command.input, file ); | ||
var output = path.join( command.output || command.input, file ); | ||
return sorcery.load( input ).then( function ( chain ) { | ||
return chain.write( output, { | ||
inline: command.datauri, | ||
includeContent: !command.excludeContent | ||
}); | ||
}); | ||
}); | ||
return sander.Promise.all( promises ); | ||
}); | ||
} | ||
return sorcery.load( command.input ).then( function ( chain ) { | ||
return chain.write( command.output || command.input, { | ||
inline: command.datauri, | ||
includeContent: !command.excludeContent | ||
}); | ||
}); | ||
@@ -47,2 +69,2 @@ }).catch( function ( err ) { | ||
}); | ||
} | ||
} |
# changelog | ||
## 0.3.4 | ||
* Ensure trailing newline on `chain.write()` ([#4](https://github.com/Rich-Harris/sorcery/issues/4)) | ||
* Upgrade dependencies | ||
## 0.3.3 | ||
* Cache decodings for better performance | ||
* Add `node.stat()` method for rudimentary profiling | ||
## 0.3.2 | ||
@@ -4,0 +14,0 @@ |
@@ -7,2 +7,3 @@ 'use strict'; | ||
sander = ('default' in sander ? sander['default'] : sander); | ||
var buffer_crc32 = require('buffer-crc32'); | ||
var vlq = require('vlq'); | ||
@@ -16,2 +17,3 @@ | ||
function btoa(str) { | ||
@@ -32,7 +34,7 @@ return new Buffer(str).toString("base64"); | ||
SourceMap.prototype = { | ||
toString: function toString() { | ||
toString: function () { | ||
return JSON.stringify(this); | ||
}, | ||
toUrl: function toUrl() { | ||
toUrl: function () { | ||
return "data:application/json;charset=utf-8;base64," + btoa(this.toString()); | ||
@@ -42,3 +44,4 @@ } | ||
var separator = /[\/\\]/;function getRelativePath(from, to) { | ||
var separator = /[\/\\]/; | ||
function getRelativePath(from, to) { | ||
var fromParts, toParts, i; | ||
@@ -110,2 +113,4 @@ | ||
var cache = {}; | ||
function decodeSegments(encodedSegments) { | ||
@@ -120,62 +125,69 @@ var i = encodedSegments.length; | ||
return segments; | ||
}function decodeMappings(mappings) { | ||
var sourceFileIndex = 0; // second field | ||
var sourceCodeLine = 0; // third field | ||
var sourceCodeColumn = 0; // fourth field | ||
var nameIndex = 0; // fifth field | ||
} | ||
function decodeMappings(mappings) { | ||
var checksum = buffer_crc32(mappings); | ||
var lines = mappings.split(";"); | ||
var numLines = lines.length; | ||
var decoded = new Array(numLines); | ||
if (!cache[checksum]) { | ||
var sourceFileIndex = 0; // second field | ||
var sourceCodeLine = 0; // third field | ||
var sourceCodeColumn = 0; // fourth field | ||
var nameIndex = 0; // fifth field | ||
var i = undefined, | ||
j = undefined, | ||
line = undefined, | ||
generatedCodeColumn = undefined, | ||
decodedLine = undefined, | ||
segments = undefined, | ||
segment = undefined, | ||
result = undefined; | ||
var lines = mappings.split(";"); | ||
var numLines = lines.length; | ||
var decoded = new Array(numLines); | ||
for (i = 0; i < numLines; i += 1) { | ||
line = lines[i]; | ||
var i = undefined, | ||
j = undefined, | ||
line = undefined, | ||
generatedCodeColumn = undefined, | ||
decodedLine = undefined, | ||
segments = undefined, | ||
segment = undefined, | ||
result = undefined; | ||
generatedCodeColumn = 0; // first field - reset each time | ||
decodedLine = []; | ||
for (i = 0; i < numLines; i += 1) { | ||
line = lines[i]; | ||
segments = decodeSegments(line.split(",")); | ||
generatedCodeColumn = 0; // first field - reset each time | ||
decodedLine = []; | ||
for (j = 0; j < segments.length; j += 1) { | ||
segment = segments[j]; | ||
segments = decodeSegments(line.split(",")); | ||
if (!segment.length) { | ||
break; | ||
} | ||
for (j = 0; j < segments.length; j += 1) { | ||
segment = segments[j]; | ||
generatedCodeColumn += segment[0]; | ||
if (!segment.length) { | ||
break; | ||
} | ||
result = [generatedCodeColumn]; | ||
decodedLine.push(result); | ||
generatedCodeColumn += segment[0]; | ||
if (segment.length === 1) { | ||
// only one field! | ||
break; | ||
} | ||
result = [generatedCodeColumn]; | ||
decodedLine.push(result); | ||
sourceFileIndex += segment[1]; | ||
sourceCodeLine += segment[2]; | ||
sourceCodeColumn += segment[3]; | ||
if (segment.length === 1) { | ||
// only one field! | ||
break; | ||
} | ||
result.push(sourceFileIndex, sourceCodeLine, sourceCodeColumn); | ||
sourceFileIndex += segment[1]; | ||
sourceCodeLine += segment[2]; | ||
sourceCodeColumn += segment[3]; | ||
if (segment.length === 5) { | ||
nameIndex += segment[4]; | ||
result.push(nameIndex); | ||
result.push(sourceFileIndex, sourceCodeLine, sourceCodeColumn); | ||
if (segment.length === 5) { | ||
nameIndex += segment[4]; | ||
result.push(nameIndex); | ||
} | ||
} | ||
decoded[i] = decodedLine; | ||
} | ||
decoded[i] = decodedLine; | ||
cache[checksum] = decoded; | ||
} | ||
return decoded; | ||
return cache[checksum]; | ||
} | ||
@@ -206,2 +218,3 @@ | ||
function atob(base64) { | ||
@@ -262,3 +275,4 @@ return new Buffer(base64, "base64").toString("utf8"); | ||
*/ | ||
var trace__default = trace; | ||
var traceMapping = trace; | ||
function trace(node, lineIndex, columnIndex, name) { | ||
@@ -331,2 +345,10 @@ var segments; | ||
this._stats = { | ||
decodingTime: 0, | ||
encodingTime: 0, | ||
tracingTime: 0, | ||
untraceable: 0 | ||
}; | ||
this.sourcesContentByPath = {}; | ||
@@ -336,4 +358,5 @@ }; | ||
Node.prototype = { | ||
_load: function _load() { | ||
_load: function () { | ||
var _this = this; | ||
return getContent(this).then(function (content) { | ||
@@ -356,3 +379,8 @@ var url; | ||
_this.map = map; | ||
var decodingStart = process.hrtime(); | ||
_this.mappings = decodeMappings(map.mappings); | ||
var decodingTime = process.hrtime(decodingStart); | ||
_this._stats.decodingTime = 1000000000 * decodingTime[0] + decodingTime[1]; | ||
sourcesContent = map.sourcesContent || []; | ||
@@ -374,4 +402,5 @@ | ||
_loadSync: function _loadSync() { | ||
_loadSync: function () { | ||
var _this = this; | ||
var url, map, sourcesContent; | ||
@@ -407,5 +436,7 @@ | ||
apply: function apply() { | ||
apply: function () { | ||
var _this = this; | ||
var options = arguments[0] === undefined ? {} : arguments[0]; | ||
var allNames = [], | ||
@@ -415,3 +446,3 @@ allSources = []; | ||
var applySegment = function (segment, result) { | ||
var traced = trace__default(_this.sources[segment[1]], // source | ||
var traced = traceMapping(_this.sources[segment[1]], // source | ||
segment[2], // source code line | ||
@@ -422,2 +453,3 @@ segment[3], // source code column | ||
if (!traced) { | ||
_this._stats.untraceable += 1; | ||
return; | ||
@@ -444,8 +476,11 @@ } | ||
newSegment.push(nameIndex); | ||
newSegment[4] = nameIndex; | ||
} | ||
result.push(newSegment); | ||
result[result.length] = newSegment; | ||
}; | ||
// Trace mappings | ||
var tracingStart = process.hrtime(); | ||
var i = this.mappings.length; | ||
@@ -467,2 +502,11 @@ var resolved = new Array(i); | ||
var tracingTime = process.hrtime(tracingStart); | ||
this._stats.tracingTime = 1000000000 * tracingTime[0] + tracingTime[1]; | ||
// Encode mappings | ||
var encodingStart = process.hrtime(); | ||
var mappings = encodeMappings(resolved); | ||
var encodingTime = process.hrtime(encodingStart); | ||
this._stats.encodingTime = 1000000000 * encodingTime[0] + encodingTime[1]; | ||
var includeContent = options.includeContent !== false; | ||
@@ -479,21 +523,23 @@ | ||
names: allNames, | ||
mappings: encodeMappings(resolved) | ||
mappings: mappings | ||
}); | ||
}, | ||
trace: (function (_trace) { | ||
var _traceWrapper = function trace() { | ||
return _trace.apply(this, arguments); | ||
}; | ||
stat: function () { | ||
return { | ||
selfDecodingTime: this._stats.decodingTime / 1000000, | ||
totalDecodingTime: (this._stats.decodingTime + tally(this.sources, "decodingTime")) / 1000000, | ||
_traceWrapper.toString = function () { | ||
return _trace.toString(); | ||
encodingTime: this._stats.encodingTime / 1000000, | ||
tracingTime: this._stats.tracingTime / 1000000, | ||
untraceable: this._stats.untraceable | ||
}; | ||
}, | ||
return _traceWrapper; | ||
})(function (oneBasedLineIndex, zeroBasedColumnIndex) { | ||
return trace__default(this, oneBasedLineIndex - 1, zeroBasedColumnIndex, null); | ||
}), | ||
trace: function (oneBasedLineIndex, zeroBasedColumnIndex) { | ||
return traceMapping(this, oneBasedLineIndex - 1, zeroBasedColumnIndex, null); | ||
}, | ||
write: function write(dest, options) { | ||
write: function (dest, options) { | ||
var map, url, index, content, promises; | ||
@@ -517,3 +563,3 @@ | ||
index = this.content.lastIndexOf("sourceMappingURL=") + 17; | ||
content = this.content.substr(0, index) + this.content.substring(index).replace(/^\S+/, url); | ||
content = this.content.substr(0, index) + this.content.substring(index).replace(/^\S+/, url) + "\n"; | ||
@@ -559,5 +605,13 @@ promises = [sander.writeFile(dest, content)]; | ||
function tally(nodes, stat) { | ||
return nodes.reduce(function (total, node) { | ||
return total + node._stats[stat]; | ||
}, 0); | ||
} | ||
function index__load(file) { | ||
return new Node(file)._load(); | ||
}function loadSync(file) { | ||
} | ||
function loadSync(file) { | ||
return new Node(file)._loadSync(); | ||
@@ -564,0 +618,0 @@ } |
{ | ||
"name": "sorcery", | ||
"description": "Resolve a chain of sourcemaps back to the original source", | ||
"version": "0.3.2", | ||
"version": "0.3.4", | ||
"author": "Rich Harris", | ||
@@ -11,4 +11,5 @@ "repository": "https://github.com/Rich-Harris/sorcery", | ||
"dependencies": { | ||
"buffer-crc32": "^0.2.5", | ||
"minimist": "^1.1.0", | ||
"sander": "^0.1.6", | ||
"sander": "^0.2.2", | ||
"vlq": "^0.2.1" | ||
@@ -18,4 +19,4 @@ }, | ||
"coffee-script": "^1.8.0", | ||
"gobble": "^0.7.2", | ||
"gobble-6to5": "^3.0.0", | ||
"gobble": "^0.7.9", | ||
"gobble-babel": "^4.0.1", | ||
"gobble-browserify": "^0.2.0", | ||
@@ -22,0 +23,0 @@ "gobble-coffee": "^0.2.1", |
@@ -9,3 +9,3 @@ import path from 'path'; | ||
import getMapFromUrl from './utils/getMapFromUrl'; | ||
import trace from './utils/trace'; | ||
import traceMapping from './utils/traceMapping'; | ||
@@ -25,2 +25,10 @@ var Promise = sander.Promise; | ||
this._stats = { | ||
decodingTime: 0, | ||
encodingTime: 0, | ||
tracingTime: 0, | ||
untraceable: 0 | ||
}; | ||
this.sourcesContentByPath = {}; | ||
@@ -48,3 +56,8 @@ }; | ||
this.map = map; | ||
let decodingStart = process.hrtime(); | ||
this.mappings = decodeMappings( map.mappings ); | ||
let decodingTime = process.hrtime( decodingStart ); | ||
this._stats.decodingTime = 1e9 * decodingTime[0] + decodingTime[1]; | ||
sourcesContent = map.sourcesContent || []; | ||
@@ -102,3 +115,3 @@ | ||
var applySegment = ( segment, result ) => { | ||
var traced = trace( | ||
var traced = traceMapping( | ||
this.sources[ segment[1] ], // source | ||
@@ -111,2 +124,3 @@ segment[2], // source code line | ||
if ( !traced ) { | ||
this._stats.untraceable += 1; | ||
return; | ||
@@ -137,8 +151,11 @@ } | ||
newSegment.push( nameIndex ); | ||
newSegment[4] = nameIndex; | ||
} | ||
result.push( newSegment ); | ||
result[ result.length ] = newSegment; | ||
}; | ||
// Trace mappings | ||
let tracingStart = process.hrtime(); | ||
let i = this.mappings.length; | ||
@@ -158,2 +175,11 @@ let resolved = new Array( i ); | ||
let tracingTime = process.hrtime( tracingStart ); | ||
this._stats.tracingTime = 1e9 * tracingTime[0] + tracingTime[1]; | ||
// Encode mappings | ||
let encodingStart = process.hrtime(); | ||
let mappings = encodeMappings( resolved ); | ||
let encodingTime = process.hrtime( encodingStart ); | ||
this._stats.encodingTime = 1e9 * encodingTime[0] + encodingTime[1]; | ||
let includeContent = options.includeContent !== false; | ||
@@ -170,8 +196,20 @@ | ||
names: allNames, | ||
mappings: encodeMappings( resolved ) | ||
mappings | ||
}); | ||
}, | ||
stat () { | ||
return { | ||
selfDecodingTime: this._stats.decodingTime / 1e6, | ||
totalDecodingTime: ( this._stats.decodingTime + tally( this.sources, 'decodingTime' ) ) / 1e6, | ||
encodingTime: this._stats.encodingTime / 1e6, | ||
tracingTime: this._stats.tracingTime / 1e6, | ||
untraceable: this._stats.untraceable | ||
}; | ||
}, | ||
trace ( oneBasedLineIndex, zeroBasedColumnIndex ) { | ||
return trace( this, oneBasedLineIndex - 1, zeroBasedColumnIndex, null ); | ||
return traceMapping( this, oneBasedLineIndex - 1, zeroBasedColumnIndex, null ); | ||
}, | ||
@@ -198,3 +236,3 @@ | ||
index = this.content.lastIndexOf( 'sourceMappingURL=' ) + 17; | ||
content = this.content.substr( 0, index ) + this.content.substring( index ).replace( /^\S+/, url ); | ||
content = this.content.substr( 0, index ) + this.content.substring( index ).replace( /^\S+/, url ) + '\n'; | ||
@@ -239,1 +277,7 @@ promises = [ sander.writeFile( dest, content ) ]; | ||
} | ||
function tally ( nodes, stat ) { | ||
return nodes.reduce( ( total, node ) => { | ||
return total + node._stats[ stat ]; | ||
}, 0 ); | ||
} |
@@ -0,3 +1,6 @@ | ||
import * as crc32 from 'buffer-crc32'; | ||
import * as vlq from 'vlq'; | ||
let cache = {}; | ||
function decodeSegments ( encodedSegments ) { | ||
@@ -15,54 +18,60 @@ let i = encodedSegments.length; | ||
export default function decodeMappings ( mappings ) { | ||
let sourceFileIndex = 0; // second field | ||
let sourceCodeLine = 0; // third field | ||
let sourceCodeColumn = 0; // fourth field | ||
let nameIndex = 0; // fifth field | ||
let checksum = crc32( mappings ); | ||
let lines = mappings.split( ';' ); | ||
let numLines = lines.length; | ||
let decoded = new Array( numLines ); | ||
if ( !cache[ checksum ] ) { | ||
let sourceFileIndex = 0; // second field | ||
let sourceCodeLine = 0; // third field | ||
let sourceCodeColumn = 0; // fourth field | ||
let nameIndex = 0; // fifth field | ||
let i, j, line, generatedCodeColumn, decodedLine, segments, segment, result; | ||
let lines = mappings.split( ';' ); | ||
let numLines = lines.length; | ||
let decoded = new Array( numLines ); | ||
for ( i = 0; i < numLines; i += 1 ) { | ||
line = lines[i]; | ||
let i, j, line, generatedCodeColumn, decodedLine, segments, segment, result; | ||
generatedCodeColumn = 0; // first field - reset each time | ||
decodedLine = []; | ||
for ( i = 0; i < numLines; i += 1 ) { | ||
line = lines[i]; | ||
segments = decodeSegments( line.split( ',' ) ); | ||
generatedCodeColumn = 0; // first field - reset each time | ||
decodedLine = []; | ||
for ( j = 0; j < segments.length; j += 1 ) { | ||
segment = segments[j]; | ||
segments = decodeSegments( line.split( ',' ) ); | ||
if ( !segment.length ) { | ||
break; | ||
} | ||
for ( j = 0; j < segments.length; j += 1 ) { | ||
segment = segments[j]; | ||
generatedCodeColumn += segment[0]; | ||
if ( !segment.length ) { | ||
break; | ||
} | ||
result = [ generatedCodeColumn ]; | ||
decodedLine.push( result ); | ||
generatedCodeColumn += segment[0]; | ||
if ( segment.length === 1 ) { | ||
// only one field! | ||
break; | ||
} | ||
result = [ generatedCodeColumn ]; | ||
decodedLine.push( result ); | ||
sourceFileIndex += segment[1]; | ||
sourceCodeLine += segment[2]; | ||
sourceCodeColumn += segment[3]; | ||
if ( segment.length === 1 ) { | ||
// only one field! | ||
break; | ||
} | ||
result.push( sourceFileIndex, sourceCodeLine, sourceCodeColumn ); | ||
sourceFileIndex += segment[1]; | ||
sourceCodeLine += segment[2]; | ||
sourceCodeColumn += segment[3]; | ||
if ( segment.length === 5 ) { | ||
nameIndex += segment[4]; | ||
result.push( nameIndex ); | ||
result.push( sourceFileIndex, sourceCodeLine, sourceCodeColumn ); | ||
if ( segment.length === 5 ) { | ||
nameIndex += segment[4]; | ||
result.push( nameIndex ); | ||
} | ||
} | ||
decoded[i] = decodedLine; | ||
} | ||
decoded[i] = decodedLine; | ||
cache[ checksum ] = decoded; | ||
} | ||
return decoded; | ||
return cache[ checksum ]; | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
37337
974
4
+ Addedbuffer-crc32@^0.2.5
+ Addedbuffer-crc32@0.2.13(transitive)
+ Addedes6-promise@2.3.0(transitive)
+ Addedgraceful-fs@3.0.12(transitive)
+ Addednatives@1.1.6(transitive)
+ Addedsander@0.2.4(transitive)
- Removedes6-promise@1.0.0(transitive)
- Removedsander@0.1.6(transitive)
Updatedsander@^0.2.2