Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

module-deps

Package Overview
Dependencies
Maintainers
1
Versions
139
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

module-deps - npm Package Compare versions

Comparing version 0.2.6 to 0.3.0

test/files/tr_module/f.js

186

index.js
var fs = require('fs');
var path = require('path');
var spawn = require('child_process').spawn;
var parseShell = require('shell-quote').parse;
var duplexer = require('duplexer');
var required = require('required');
var browserResolve = require('browser-resolve');
var nodeResolve = require('resolve');
var detective = require('detective');
var through = require('through');

@@ -13,5 +18,5 @@

var files = {};
var visited = {};
var pending = 0;
var cache = {};
var pending = 0;

@@ -21,65 +26,144 @@ var output = through();

if (!opts) opts = {};
if (opts.cache === undefined) opts.cache = cache;
opts.includeSource = true;
var transforms = [].concat(opts.transform).filter(Boolean);
mains.forEach(function (file) {
var resolve = opts.resolve || browserResolve;
var top = { id: '/', filename: '/', paths: [] };
mains.forEach(function (main) { walk(main, top) });
if (mains.length === 0) {
process.nextTick(output.queue.bind(output, null));
}
return output;
function walk (id, parent, cb) {
pending ++;
var p = 2, src, rows;
var trx = [];
parent.packageFilter = function (pkg) {
if (opts.packageFilter) pkg = opts.packageFilter(pkg);
if (opts.transformKey) {
var n = pkg;
opts.transformKey.forEach(function (key) {
if (n && typeof n === 'object') n = n[key];
});
trx = [].concat(n).filter(Boolean);
}
return pkg;
};
resolve(id, parent, function (err, file) {
if (err) return output.emit('error', err);
if (cb) cb(file);
if (visited[file]) { --pending; return };
visited[file] = true;
fs.readFile(file, 'utf8', function (err, src) {
if (err) return output.emit('error', err);
applyTransforms(file, trx, src);
});
});
}
function applyTransforms (file, trx, src) {
var isTopLevel = mains.some(function (main) {
var m = path.relative(path.dirname(main), file);
return m.split('/').indexOf('node_modules') < 0;
});
var transf = (isTopLevel ? transforms : []).concat(trx);
if (transf.length === 0) return done();
(function ap (trs) {
if (trs.length === 0) return done();
var s = makeTransform(file, trs[0]);
s.on('error', output.emit.bind(output, 'error'));
var data = '';
s.on('data', function (buf) { data += buf });
s.on('end', function () {
src = data;
ap(trs.slice(1));
});
s.end(src);
})(transf);
function done () {
if (!files[file]) {
files[file] = {
id: file,
source: src,
entry: true,
deps: rows.reduce(function (acc, dep) {
acc[dep.id] = dep.filename;
return acc;
}, {})
};
output.queue(files[file]);
parseDeps(file, src);
}
}
function parseDeps (file, src) {
var deps = detective(src);
var p = deps.length;
var current = { id: file, filename: file, paths: [] };
var resolved = {};
deps.forEach(function (id) {
walk(id, current, function (r) {
resolved[id] = r;
if (--p === 0) done();
});
});
if (deps.length === 0) done();
function done () {
var rec = {
id: file,
source: src,
deps: resolved
};
walk(rows);
if (mains.indexOf(file) >= 0) {
rec.entry = true;
}
output.queue(rec);
if (--pending === 0) output.queue(null);
}
}
function makeTransform (file, tr) {
if (/\s/.test(tr)) return cmdTransform(file, tr);
fs.readFile(file, 'utf8', function (err, s) {
var tout = through(), tin = through();
tin.pause();
var params = { basedir: path.dirname(file) };
nodeResolve(tr, params, function (err, res) {
if (err) return output.emit('error', err);
src = s;
if (--p === 0) done();
var t = res
? require(res)(file)
: cmdTransform(file, tr)
;
t.pipe(tout);
tin.pipe(t);
tin.resume();
});
return duplexer(tin, tout);
}
function cmdTransform (file, tr) {
var cmd = parseShell(tr);
var env = Object.create(process.env);
env._ = tr;
env.FILENAME = file;
var current = { id: file, filename: file, paths: [] };
required(file, opts, function (err, r) {
if (err) return output.emit('error', err);
rows = r;
if (--p === 0) done();
var ps = spawn(cmd[0], cmd.slice(1), {
cwd: path.dirname(file),
env: env
});
});
if (pending === 0) process.nextTick(function () {
output.queue(null);
});
return output;
function walk (rows) {
rows.forEach(function (row) {
if (files[row.filename]) return;
var r = files[row.filename] = {
id: row.filename,
source: row.source,
deps: (row.deps || []).reduce(function (acc, dep) {
acc[dep.id] = dep.filename;
return acc;
}, {})
};
if (mains.indexOf(row.filename) >= 0) {
r.entry = true;
var error = '';
ps.stderr.on('data', function (buf) { error += buf });
ps.on('close', function (code) {
if (code !== 0) {
return output.emit('error', [
'process ' + tr + ' exited with code ' + code,
' while parsing ' + file + '\n',
error.split('\n').join('\n ')
].join(''));
}
output.queue(r);
walk(row.deps || []);
});
return duplexer(ps.stdin, ps.stdout);
}
};
{
"name": "module-deps",
"version": "0.2.6",
"version": "0.3.0",
"description": "walk the dependency graph to generate json output that can be fed into browser-pack",

@@ -10,5 +10,9 @@ "main": "index.js",

"dependencies": {
"through": "~2.2.0",
"duplexer": "~0.0.3",
"JSONStream": "~0.4.3",
"required": "~0.3.0",
"through": "~2.2.0"
"browser-resolve": "~0.0.3",
"resolve": "~0.3.0",
"shell-quote": "~0.0.1",
"detective": "~1.1.0"
},

@@ -15,0 +19,0 @@ "devDependencies": {

@@ -9,2 +9,3 @@ var parser = require('../');

var p = parser(__dirname + '/files/main.js');
p.on('error', t.fail.bind(t));
var pack = packer();

@@ -11,0 +12,0 @@

Sorry, the diff of this file is not supported yet

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