Socket
Socket
Sign inDemoInstall

sorcery

Package Overview
Dependencies
Maintainers
1
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sorcery - npm Package Compare versions

Comparing version 0.2.5 to 0.3.0

src/utils/trace.js

348

dist/sorcery.js

@@ -9,4 +9,10 @@ 'use strict';

/**
* Encodes a string as base64
* @param {string} str - the string to encode
* @returns {string}
*/
function btoa(str) {
return new Buffer(str).toString("base64");
return new Buffer(str).toString("base64");
}

@@ -25,7 +31,7 @@

SourceMap.prototype = {
toString: function () {
toString: function toString() {
return JSON.stringify(this);
},
toUrl: function () {
toUrl: function toUrl() {
return "data:application/json;charset=utf-8;base64," + btoa(this.toString());

@@ -171,6 +177,23 @@ }

/**
* Decodes a base64 string
* @param {string} base64 - the string to decode
* @returns {string}
*/
function atob(base64) {
return new Buffer(base64, "base64").toString("utf8");
return new Buffer(base64, "base64").toString("utf8");
}
/**
* Turns a sourceMappingURL into a sourcemap
* @param {string} url - the URL (i.e. sourceMappingURL=url). Can
be a base64-encoded data URI
* @param {string} base - the URL against which relative URLS
should be resolved
* @param {boolean} sync - if `true`, return a promise, otherwise
return the sourcemap
* @returns {object} - a version 3 sourcemap
*/
function getMapFromUrl(url, base, sync) {

@@ -200,6 +223,111 @@ var json, map, match;

var trace___slicedToArray = function (arr, i) { if (Array.isArray(arr)) { return arr; } else { var _arr = []; for (var _iterator = arr[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) { _arr.push(_step.value); if (i && _arr.length === i) break; } return _arr; } };
/**
* Traces a segment back to its origin
* @param {object} node - an instance of Node
* @param {number} lineIndex - the zero-based line index of the
segment as found in `node`
* @param {number} columnIndex - the zero-based column index of the
segment as found in `node`
* @param {string || null} - if specified, the name that should be
(eventually) returned, as it is closest to the generated code
* @returns {object}
@property {string} source - the filepath of the source
@property {number} line - the one-based line index
@property {number} column - the zero-based column index
@property {string || null} name - the name corresponding
to the segment being traced
*/
var trace__default = trace;
function trace(node, lineIndex, columnIndex, name) {
var _arguments = arguments,
_this = this,
_shouldContinue,
_result;
do {
_shouldContinue = false;
_result = (function (node, lineIndex, columnIndex, name) {
var segments;
// If this node doesn't have a source map, we have
// to assume it is the original source
if (node.isOriginalSource) {
return {
source: node.file,
line: lineIndex + 1,
column: columnIndex || 0,
name: name
};
}
// Otherwise, we need to figure out what this position in
// the intermediate file corresponds to in *its* source
segments = node.mappings[lineIndex];
if (!segments || segments.length === 0) {
return null;
}
if (columnIndex != null) {
var len = segments.length;
var i = undefined;
for (i = 0; i < len; i += 1) {
var _segments$i = trace___slicedToArray(segments[i], 5);
var _generatedCodeColumn = _segments$i[0];
var _sourceFileIndex = _segments$i[1];
var _sourceCodeLine = _segments$i[2];
var _sourceCodeColumn = _segments$i[3];
var _nameIndex = _segments$i[4];
if (_generatedCodeColumn === columnIndex) {
var _parent = node.sources[_sourceFileIndex];
_arguments = [_parent, _sourceCodeLine, _sourceCodeColumn, node.map.names[_nameIndex] || name];
_this = undefined;
return _shouldContinue = true;
}
if (_generatedCodeColumn > columnIndex) {
break;
}
}
}
// fall back to a line mapping
var _segments$0 = trace___slicedToArray(segments[0], 5);
var generatedCodeColumn = _segments$0[0];
var sourceFileIndex = _segments$0[1];
var sourceCodeLine = _segments$0[2];
var sourceCodeColumn = _segments$0[3];
var nameIndex = _segments$0[4];
var parent = node.sources[sourceFileIndex];
_arguments = [parent, sourceCodeLine, null, node.map.names[nameIndex] || name];
_this = undefined;
return _shouldContinue = true;
}).apply(_this, _arguments);
} while (_shouldContinue);
return _result;
}
var Node___slicedToArray = function (arr, i) { if (Array.isArray(arr)) { return arr; } else { var _arr = []; for (var _iterator = arr[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) { _arr.push(_step.value); if (i && _arr.length === i) break; } return _arr; } };
var Node__Promise = sander.Promise;
var Node = function (file, content) {
this.file = path.resolve(file);
this.content = content;
this.content = content || null; // sometimes exists in sourcesContent, sometimes doesn't
// these get filled in later
this.map = null;
this.mappings = null;
this.sources = null;
this.isOriginalSource = null;
this.lines = null;
this.sourcesContentByPath = {};

@@ -209,18 +337,9 @@ };

Node.prototype = {
_load: function () {
var self = this;
function getContent() {
if (!self.content) {
return sander.readFile(self.file).then(String);
}
return sander.Promise.resolve(self.content);
}
return getContent().then(function (content) {
_load: function _load() {
var _this = this;
return getContent(this).then(function (content) {
var url;
self.content = content;
self.lines = content.split("\n");
_this.content = content;
_this.lines = content.split("\n");

@@ -230,40 +349,30 @@ url = getSourceMappingUrl(content);

if (!url) {
self.isOriginalSource = true;
return self;
} else {
return getMapFromUrl(url, self.file).then(function (map) {
var promises, sourcesContent;
_this.isOriginalSource = true;
return null;
}
self.map = map;
self.mappings = decodeMappings(map.mappings);
sourcesContent = map.sourcesContent || [];
return getMapFromUrl(url, _this.file).then(function (map) {
var promises, sourcesContent;
self.sources = map.sources.map(function (source, i) {
return new Node(resolveSourcePath(self, source), sourcesContent[i]);
});
_this.map = map;
_this.mappings = decodeMappings(map.mappings);
sourcesContent = map.sourcesContent || [];
promises = self.sources.map(function (node) {
return node._load();
});
_this.sources = map.sources.map(function (source, i) {
return new Node(resolveSourcePath(_this, source), sourcesContent[i]);
});
return sander.Promise.all(promises);
}).then(function () {
getSourcesContent(self);
return self;
promises = _this.sources.map(Node__load);
return Node__Promise.all(promises).then(function () {
getSourcesContent(_this);
return _this;
});
}
}).then(function () {
if (!self.isOriginalSource) {
return self;
}
return null;
});
});
},
_loadSync: function () {
var self = this,
url,
map,
sourcesContent;
_loadSync: function _loadSync() {
var _this = this;
var url, map, sourcesContent;

@@ -279,10 +388,10 @@ if (!this.content) {

if (!url) {
self.isOriginalSource = true;
this.isOriginalSource = true;
} else {
self.map = map = getMapFromUrl(url, this.file, true);
self.mappings = decodeMappings(map.mappings);
this.map = map = getMapFromUrl(url, this.file, true);
this.mappings = decodeMappings(map.mappings);
sourcesContent = map.sourcesContent || [];
self.sources = map.sources.map(function (source, i) {
var node = new Node(resolveSourcePath(self, source), sourcesContent[i]);
this.sources = map.sources.map(function (source, i) {
var node = new Node(resolveSourcePath(_this, source), sourcesContent[i]);
node._loadSync();

@@ -293,3 +402,3 @@

getSourcesContent(self);
getSourcesContent(this);
}

@@ -300,11 +409,10 @@

apply: function (options) {
var self = this,
resolved,
names = [],
sources = [],
mappings,
apply: function apply() {
var _this = this;
var options = arguments[0] === undefined ? {} : arguments[0];
var resolved,
allNames = [],
allSources = [],
includeContent;
options = options || {};
includeContent = options.includeContent !== false;

@@ -316,12 +424,12 @@

line.forEach(function (segment) {
var source, traced, newSegment, sourceIndex, nameIndex;
var _segment = Node___slicedToArray(segment, 4);
if (segment.length === 1) {
// TODO not sure what to do here...?
resolved.push([segment[0]]);
return;
}
var generatedCodeColumn = _segment[0];
var sourceFileIndex = _segment[1];
var sourceCodeLine = _segment[2];
var sourceCodeColumn = _segment[3];
var source;var traced;var newSegment;var sourceIndex;var nameIndex;
source = self.sources[segment[1]];
traced = source.trace(segment[2] + 1, segment[3], self.map.names[segment[4]]);
source = _this.sources[sourceFileIndex];
traced = trace__default(source, sourceCodeLine, sourceCodeColumn, _this.map.names[segment[4]]);

@@ -332,15 +440,15 @@ if (!traced) {

sourceIndex = sources.indexOf(traced.source);
sourceIndex = allSources.indexOf(traced.source);
if (! ~sourceIndex) {
sourceIndex = sources.length;
sources.push(traced.source);
sourceIndex = allSources.length;
allSources.push(traced.source);
}
newSegment = [segment[0], sourceIndex, traced.line - 1, traced.column];
newSegment = [generatedCodeColumn, sourceIndex, traced.line - 1, traced.column];
if (traced.name) {
nameIndex = names.indexOf(traced.name);
nameIndex = allNames.indexOf(traced.name);
if (! ~nameIndex) {
nameIndex = names.length;
names.push(traced.name);
nameIndex = allNames.length;
allNames.push(traced.name);
}

@@ -357,62 +465,30 @@

mappings = encodeMappings(resolved);
return new SourceMap({
file: this.file.split("/").pop(),
sources: sources.map(function (source) {
return getRelativePath(options.base || self.file, source);
sources: allSources.map(function (source) {
return getRelativePath(options.base || _this.file, source);
}),
sourcesContent: sources.map(function (source) {
return includeContent ? self.sourcesContentByPath[source] : null;
sourcesContent: allSources.map(function (source) {
return includeContent ? _this.sourcesContentByPath[source] : null;
}),
names: names,
mappings: mappings
names: allNames,
mappings: encodeMappings(resolved)
});
},
trace: function (oneBasedLineIndex, zeroBasedColumnIndex, name) {
var segments, line, segment, len, i, parent, leadingWhitespace;
trace: (function (_trace) {
var _traceWrapper = function trace() {
return _trace.apply(this, arguments);
};
// If this node doesn't have a source map, we treat it as
// the original source
if (this.isOriginalSource) {
return {
source: this.file,
line: oneBasedLineIndex,
column: zeroBasedColumnIndex,
name: name
};
}
_traceWrapper.toString = function () {
return _trace.toString();
};
// Otherwise, we need to figure out what this position in
// the intermediate file corresponds to in *its* source
segments = this.mappings[oneBasedLineIndex - 1];
return _traceWrapper;
})(function (oneBasedLineIndex, zeroBasedColumnIndex) {
return trace__default(this, oneBasedLineIndex - 1, zeroBasedColumnIndex, null);
}),
if (!segments) {
return null;
}
if (zeroBasedColumnIndex === undefined) {
// we only have a line to go on. Use the first non-whitespace character
line = this.lines[oneBasedLineIndex - 1];
zeroBasedColumnIndex = leadingWhitespace ? leadingWhitespace[0].length : 0;
}
len = segments.length;
for (i = 0; i < len; i += 1) {
segment = segments[i];
if (segment[0] === zeroBasedColumnIndex) {
parent = this.sources[segment[1]];
return parent.trace(segment[2] + 1, segment[3], this.map.names[segment[4]] || name);
}
if (segment[0] > zeroBasedColumnIndex) {
return null;
}
}
},
write: function (dest, options) {
write: function write(dest, options) {
var map, url, index, content, promises;

@@ -444,3 +520,3 @@

return sander.Promise.all(promises);
return Node__Promise.all(promises);
}

@@ -451,2 +527,14 @@ };

function Node__load(node) {
return node._load();
}
function getContent(node) {
if (!node.content) {
return sander.readFile(node.file).then(String);
}
return Node__Promise.resolve(node.content);
}
function resolveSourcePath(node, source) {

@@ -467,3 +555,3 @@ // TODO handle sourceRoot

function load(file) {
function index__load(file) {
return new Node(file)._load();

@@ -474,3 +562,3 @@ }function loadSync(file) {

exports.load = load;
exports.load = index__load;
exports.loadSync = loadSync;
{
"name": "sorcery",
"description": "Resolve a chain of sourcemaps back to the original source",
"version": "0.2.5",
"version": "0.3.0",
"author": "Rich Harris",

@@ -6,0 +6,0 @@ "repository": "https://github.com/Rich-Harris/sorcery",

@@ -9,7 +9,17 @@ import path from 'path';

import getMapFromUrl from './utils/getMapFromUrl';
import trace from './utils/trace';
var Promise = sander.Promise;
var Node = function ( file, content ) {
this.file = path.resolve( file );
this.content = content;
this.content = content || null; // sometimes exists in sourcesContent, sometimes doesn't
// these get filled in later
this.map = null;
this.mappings = null;
this.sources = null;
this.isOriginalSource = null;
this.lines = null;
this.sourcesContentByPath = {};

@@ -19,18 +29,8 @@ };

Node.prototype = {
_load: function () {
var self = this;
function getContent () {
if ( !self.content ) {
return sander.readFile( self.file ).then( String );
}
return sander.Promise.resolve( self.content );
}
return getContent().then( function ( content ) {
_load () {
return getContent( this ).then( content => {
var url;
self.content = content;
self.lines = content.split( '\n' );
this.content = content;
this.lines = content.split( '\n' );

@@ -40,37 +40,29 @@ url = getSourceMappingUrl( content );

if ( !url ) {
self.isOriginalSource = true;
return self;
} else {
return getMapFromUrl( url, self.file ).then( function ( map ) {
var promises, sourcesContent;
this.isOriginalSource = true;
return null;
}
self.map = map;
self.mappings = decodeMappings( map.mappings );
sourcesContent = map.sourcesContent || [];
return getMapFromUrl( url, this.file ).then( map => {
var promises, sourcesContent;
self.sources = map.sources.map( function ( source, i ) {
return new Node( resolveSourcePath( self, source ), sourcesContent[i] );
});
this.map = map;
this.mappings = decodeMappings( map.mappings );
sourcesContent = map.sourcesContent || [];
promises = self.sources.map( function ( node ) {
return node._load();
});
this.sources = map.sources.map( ( source, i ) => {
return new Node( resolveSourcePath( this, source ), sourcesContent[i] );
});
return sander.Promise.all( promises );
}).then( function () {
getSourcesContent( self );
return self;
promises = this.sources.map( load );
return Promise.all( promises ).then( () => {
getSourcesContent( this );
return this;
});
}
}).then( function () {
if ( !self.isOriginalSource ) {
return self;
}
return null;
});
});
},
_loadSync: function () {
var self = this, url, map, sourcesContent;
_loadSync () {
var url, map, sourcesContent;

@@ -86,10 +78,10 @@ if ( !this.content ) {

if ( !url ) {
self.isOriginalSource = true;
this.isOriginalSource = true;
} else {
self.map = map = getMapFromUrl( url, this.file, true );
self.mappings = decodeMappings( map.mappings );
this.map = map = getMapFromUrl( url, this.file, true );
this.mappings = decodeMappings( map.mappings );
sourcesContent = map.sourcesContent || [];
self.sources = map.sources.map( function ( source, i) {
var node = new Node( resolveSourcePath( self, source ), sourcesContent[i] );
this.sources = map.sources.map( ( source, i ) => {
var node = new Node( resolveSourcePath( this, source ), sourcesContent[i] );
node._loadSync();

@@ -100,3 +92,3 @@

getSourcesContent( self );
getSourcesContent( this );
}

@@ -107,28 +99,25 @@

apply: function ( options ) {
var self = this,
resolved,
names = [],
sources = [],
mappings,
apply ( options = {} ) {
var resolved,
allNames = [],
allSources = [],
includeContent;
options = options || {};
includeContent = options.includeContent !== false;
resolved = this.mappings.map( function ( line ) {
resolved = this.mappings.map( line => {
var result = [];
line.forEach( function ( segment ) {
var source, traced, newSegment, sourceIndex, nameIndex;
line.forEach( segment => {
var [
generatedCodeColumn,
sourceFileIndex,
sourceCodeLine,
sourceCodeColumn
] = segment,
source, traced, newSegment, sourceIndex, nameIndex;
if ( segment.length === 1 ) {
// TODO not sure what to do here...?
resolved.push([ segment[0] ]);
return;
}
source = this.sources[ sourceFileIndex ];
traced = trace( source, sourceCodeLine, sourceCodeColumn, this.map.names[ segment[4] ] );
source = self.sources[ segment[1] ];
traced = source.trace( segment[2] + 1, segment[3], self.map.names[ segment[4] ] );
if ( !traced ) {

@@ -138,15 +127,15 @@ return;

sourceIndex = sources.indexOf( traced.source );
sourceIndex = allSources.indexOf( traced.source );
if ( !~sourceIndex ) {
sourceIndex = sources.length;
sources.push( traced.source );
sourceIndex = allSources.length;
allSources.push( traced.source );
}
newSegment = [ segment[0], sourceIndex, traced.line - 1, traced.column ];
newSegment = [ generatedCodeColumn, sourceIndex, traced.line - 1, traced.column ];
if ( traced.name ) {
nameIndex = names.indexOf( traced.name );
nameIndex = allNames.indexOf( traced.name );
if ( !~nameIndex ) {
nameIndex = names.length;
names.push( traced.name );
nameIndex = allNames.length;
allNames.push( traced.name );
}

@@ -163,62 +152,20 @@

mappings = encodeMappings( resolved );
return new SourceMap({
file: this.file.split( '/' ).pop(),
sources: sources.map( function ( source ) {
return getRelativePath( options.base || self.file, source );
sources: allSources.map( ( source ) => {
return getRelativePath( options.base || this.file, source );
}),
sourcesContent: sources.map( function ( source ) {
return includeContent ? self.sourcesContentByPath[ source ] : null;
sourcesContent: allSources.map( ( source ) => {
return includeContent ? this.sourcesContentByPath[ source ] : null;
}),
names: names,
mappings: mappings
names: allNames,
mappings: encodeMappings( resolved )
});
},
trace: function ( oneBasedLineIndex, zeroBasedColumnIndex, name ) {
var segments, line, segment, len, i, parent, leadingWhitespace;
// If this node doesn't have a source map, we treat it as
// the original source
if ( this.isOriginalSource ) {
return {
source: this.file,
line: oneBasedLineIndex,
column: zeroBasedColumnIndex,
name: name
};
}
// Otherwise, we need to figure out what this position in
// the intermediate file corresponds to in *its* source
segments = this.mappings[ oneBasedLineIndex - 1 ];
if ( !segments ) {
return null;
}
if ( zeroBasedColumnIndex === undefined ) {
// we only have a line to go on. Use the first non-whitespace character
line = this.lines[ oneBasedLineIndex - 1 ];
zeroBasedColumnIndex = leadingWhitespace ? leadingWhitespace[0].length : 0;
}
len = segments.length;
for ( i = 0; i < len; i += 1 ) {
segment = segments[i];
if ( segment[0] === zeroBasedColumnIndex ) {
parent = this.sources[ segment[1] ];
return parent.trace( segment[2] + 1, segment[3], this.map.names[ segment[4] ] || name );
}
if ( segment[0] > zeroBasedColumnIndex ) {
return null;
}
}
trace ( oneBasedLineIndex, zeroBasedColumnIndex ) {
return trace( this, oneBasedLineIndex - 1, zeroBasedColumnIndex, null );
},
write: function ( dest, options ) {
write ( dest, options ) {
var map, url, index, content, promises;

@@ -250,3 +197,3 @@

return sander.Promise.all( promises );
return Promise.all( promises );
}

@@ -257,2 +204,14 @@ };

function load ( node ) {
return node._load();
}
function getContent ( node ) {
if ( !node.content ) {
return sander.readFile( node.file ).then( String );
}
return Promise.resolve( node.content );
}
function resolveSourcePath ( node, source ) {

@@ -264,6 +223,6 @@ // TODO handle sourceRoot

function getSourcesContent ( node ) {
node.sources.forEach( function ( source ) {
node.sources.forEach( source => {
node.sourcesContentByPath[ source.file ] = source.content;
Object.keys( source.sourcesContentByPath ).forEach( function ( file ) {
Object.keys( source.sourcesContentByPath ).forEach( file => {
node.sourcesContentByPath[ file ] = source.sourcesContentByPath[ file ];

@@ -270,0 +229,0 @@ });

@@ -14,7 +14,7 @@ import btoa from './utils/btoa';

SourceMap.prototype = {
toString: function () {
toString () {
return JSON.stringify( this );
},
toUrl: function () {
toUrl () {
return 'data:application/json;charset=utf-8;base64,' + btoa( this.toString() );

@@ -21,0 +21,0 @@ }

@@ -0,3 +1,8 @@

/**
* Decodes a base64 string
* @param {string} base64 - the string to decode
* @returns {string}
*/
export default function atob ( base64 ) {
return new Buffer( base64, 'base64' ).toString( 'utf8' );
}

@@ -0,3 +1,8 @@

/**
* Encodes a string as base64
* @param {string} str - the string to encode
* @returns {string}
*/
export default function btoa ( str ) {
return new Buffer( str ).toString( 'base64' );
}

@@ -5,2 +5,12 @@ import path from 'path';

/**
* Turns a sourceMappingURL into a sourcemap
* @param {string} url - the URL (i.e. sourceMappingURL=url). Can
be a base64-encoded data URI
* @param {string} base - the URL against which relative URLS
should be resolved
* @param {boolean} sync - if `true`, return a promise, otherwise
return the sourcemap
* @returns {object} - a version 3 sourcemap
*/
export default function getMapFromUrl ( url, base, sync ) {

@@ -7,0 +17,0 @@ var json, map, match;

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc