[Pkg-javascript-commits] [node-module-deps] 250/444: transform stream

Bastien Roucariès rouca at moszumanska.debian.org
Fri Dec 15 09:48:00 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-module-deps.

commit 999b5c8d0b0cdd9c4af3b4c35a25eba86559e936
Author: James Halliday <mail at substack.net>
Date:   Sun Jul 20 03:03:02 2014 -0700

    transform stream
---
 example/deps.js |  8 +++---
 index.js        | 78 ++++++++++++++++-----------------------------------------
 2 files changed, 25 insertions(+), 61 deletions(-)

diff --git a/example/deps.js b/example/deps.js
index 1e0cca4..437c5e1 100644
--- a/example/deps.js
+++ b/example/deps.js
@@ -1,8 +1,6 @@
 var mdeps = require('../');
 var JSONStream = require('JSONStream');
 
-var stringify = JSONStream.stringify();
-stringify.pipe(process.stdout);
-
-var file = __dirname + '/files/main.js';
-mdeps(file).pipe(stringify);
+var md = mdeps();
+md.pipe(JSONStream.stringify()).pipe(process.stdout);
+md.end({ file: __dirname + '/files/main.js' });
diff --git a/index.js b/index.js
index fd75033..0557c54 100644
--- a/index.js
+++ b/index.js
@@ -12,18 +12,17 @@ var combine = require('stream-combiner');
 var duplexer = require('duplexer2');
 
 var inherits = require('inherits');
-var Readable = require('readable-stream').Readable;
+var Transform = require('readable-stream').Transform;
 
 module.exports = Deps;
-inherits(Deps, Readable);
+inherits(Deps, Transform);
 
-function Deps (mains, opts) {
+function Deps (opts) {
     var self = this;
-    if (!(this instanceof Deps)) return new Deps(mains, opts);
-    Readable.call(this, { objectMode: true });
+    if (!(this instanceof Deps)) return new Deps(opts);
+    Transform.call(this, { objectMode: true });
     
     if (!opts) opts = {};
-    if (!Array.isArray(mains)) mains = [ mains ].filter(Boolean);
     
     this.basedir = opts.basedir || process.cwd();
     this.cache = opts.cache;
@@ -32,6 +31,7 @@ function Deps (mains, opts) {
     this.pkgFileCachePending = {};
     this.visited = {};
     this.walking = {};
+    this.entries = [];
     
     this.paths = opts.paths || process.env.NODE_PATH;
     if (typeof this.paths === 'string') {
@@ -39,69 +39,35 @@ function Deps (mains, opts) {
     }
     if (!this.paths) this.paths = [];
     
-    this.entries = [];
-    this.mains = [];
-    
     this.transforms = [].concat(opts.transform).filter(Boolean);
     this.resolver = opts.resolve || browserResolve;
     this.options = opts;
     this.pending = 0;
     this.top = { id: '/', filename: '/', paths: this.paths };
-    
-    mains.forEach(function (file) { self.add(file) });
 }
 
-Deps.prototype._read = function () {
-    if (this._started) return;
-    this._started = true;
-    this._start();
-};
-
-Deps.prototype._start = function () {
+Deps.prototype._transform = function (row, enc, next) {
     var self = this;
+    self.pending ++;
+    if (row.entry) self.entries.push(row.file);
     
-    if (this.entries.length === 0) {
-        return this.push(null);
-    }
-    
-    for (var i = 0; i < this.entries.length; i++) (function (i) {
-        var main = self.mains[i];
-        var file = self.entries[i];
-        
-        self.lookupPackage(file, function (err, pkg) {
-            if (err) return self.emit('error', err)
-            else start(main, file, pkg)
-        });
-    })(i);
+    self.lookupPackage(row.file, function (err, pkg) {
+        if (err) return self.emit('error', err)
+        self.pending --;
+        start(pkg)
+    });
+    next();
     
-    function start (main, file, pkg) {
+    function start (pkg) {
         if (!pkg) pkg = {};
-        if (!pkg.__dirname) pkg.__dirname = path.dirname(file);
-        
-        if (typeof main === 'object') {
-            self.walk({ stream: main, file: main.path || file }, main);
-        }
-        else self.walk(main, self.top);
+        if (!pkg.__dirname) pkg.__dirname = path.dirname(row.file);
+        self.walk(row.file, self.top);
     }
 };
 
-Deps.prototype.add = function (main) {
-    var self = this;
-    
-    var file;
-    if (typeof main.pipe === 'function') {
-        var n = Math.floor(Math.pow(16,8) * Math.random()).toString(16);
-        file = path.join(this.basedir, 'fake_' + n + '.js');
-        if (typeof main.read !== 'function') {
-            var old = main;
-            main = Readable().wrap(main);
-            if (old.path) main.path = old.path;
-        }
-    }
-    else file = main;
-    file = path.resolve(file);
-    this.mains.push(main);
-    this.entries.push(file);
+Deps.prototype._flush = function () {
+    if (this.pending === 0) this.push(null);
+    this._ended = true;
 };
 
 Deps.prototype.resolve = function (id, parent, cb) {
@@ -319,7 +285,7 @@ Deps.prototype.walk = function (id, parent, cb) {
             self.push(rec);
             
             if (cb) cb(null, file);
-            if (-- self.pending === 0) self.push(null);
+            if (-- self.pending === 0 && self._ended) self.push(null);
         }
     }
 };

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-module-deps.git



More information about the Pkg-javascript-commits mailing list