[Pkg-javascript-commits] [node-stream-splicer] 01/71: seems to work now

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


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

rouca pushed a commit to branch master
in repository node-stream-splicer.

commit 9b7ba8a7915c8b57f1e43e5898ac428251fe5bab
Author: James Halliday <mail at substack.net>
Date:   Sun Jun 8 03:13:27 2014 -0700

    seems to work now
---
 index.js | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 100 insertions(+)

diff --git a/index.js b/index.js
new file mode 100644
index 0000000..27096a0
--- /dev/null
+++ b/index.js
@@ -0,0 +1,100 @@
+var isarray = require('isarray');
+var Duplex = require('readable-stream').Duplex;
+var Pass = require('readable-stream').PassThrough;
+var inherits = require('inherits');
+
+module.exports = Pipeline;
+inherits(Pipeline, Duplex);
+
+function Pipeline (streams, opts) {
+    if (!(this instanceof Pipeline)) return new Pipeline(streams, opts);
+    if (!opts) opts = {};
+    Duplex.call(this, opts);
+    
+    var self = this;
+    this._options = opts;
+    this._streams = [];
+    
+    for (var i = 0; i < streams.length; i++) {
+        this.push(streams[i]);
+    }
+    
+    this.once('finish', function () {
+        self._streams[0].end();
+    });
+}
+
+Pipeline.prototype._read = function () {
+    var self = this;
+    if (this._streams.length === 0) {
+        this._streams.push(new Pass(this._options));
+    }
+    var r = this._streams[this._streams.length-1];
+    var buf, reads = 0;
+    while ((buf = r.read()) !== null) {
+        Duplex.prototype.push.call(this, buf);
+        reads ++;
+    }
+    if (reads === 0) {
+        var onreadable = function () {
+            r.removeListener('readable', onreadable);
+            self.removeListener('_mutate', onreadable);
+            self._read()
+        };
+        r.once('readable', onreadable);
+        self.once('_mutate', onreadable);
+    }
+};
+
+Pipeline.prototype._write = function (buf, enc, next) {
+    if (this._streams.length === 0) {
+        this._streams.push(new Pass(this._options));
+    }
+    this._streams[0]._write(buf, enc, next);
+};
+
+Pipeline.prototype.push = function (stream) {
+    var self = this;
+    stream.on('error', function (err) {
+        err.stream = this;
+        self.emit('error', err);
+    });
+    stream = wrapStream(stream);
+    
+    if (this._streams.length > 0) {
+        this._streams[this._streams.length-1].pipe(stream);
+    }
+    this._streams.push(stream);
+    
+    stream.once('end', function () {
+        var ix = self._streams.indexOf(stream);
+        if (ix === self._streams.length - 1) {
+            Duplex.prototype.push.call(self, null);
+        }
+    });
+    this.emit('_mutate', stream);
+    
+    return this;
+};
+
+Pipeline.prototype.pop = function () {
+    var s = this._streams.pop();
+    if (this._streams.length > 0) {
+        this._streams[this._streams.length-1].unpipe(s);
+    }
+    return s;
+};
+
+Pipeline.prototype.indexOf = function (stream) {
+    return this._streams.indexOf(stream);
+};
+
+function wrapStream (stream) {
+    if (typeof stream.read === 'function') return stream;
+    var d = (new Duplex).wrap(stream);
+    d._write = function (buf, enc, next) {
+        stream.write(buf);
+        next();
+    };
+    return d;
+}

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



More information about the Pkg-javascript-commits mailing list