/usr/lib/nodejs/stream-splicer/index.js is in node-stream-splicer 2.0.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | var Duplex = require('stream').Duplex;
var PassThrough = require('stream').PassThrough;
var Readable = require('stream').Readable;
var inherits = require('inherits');
var nextTick = typeof setImmediate !== 'undefined'
? setImmediate : process.nextTick
;
module.exports = Pipeline;
inherits(Pipeline, Duplex);
module.exports.obj = function (streams, opts) {
if (!opts && !Array.isArray(streams)) {
opts = streams;
streams = [];
}
if (!streams) streams = [];
if (!opts) opts = {};
opts.objectMode = true;
return new Pipeline(streams, opts);
};
function Pipeline (streams, opts) {
if (!(this instanceof Pipeline)) return new Pipeline(streams, opts);
if (!opts && !Array.isArray(streams)) {
opts = streams;
streams = [];
}
if (!streams) streams = [];
if (!opts) opts = {};
Duplex.call(this, opts);
var self = this;
this._options = opts;
this._wrapOptions = { objectMode: opts.objectMode !== false };
this._streams = [];
this.splice.apply(this, [ 0, 0 ].concat(streams));
this.once('finish', function () {
self._notEmpty();
self._streams[0].end();
});
}
Pipeline.prototype._read = function () {
var self = this;
this._notEmpty();
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) {
this._notEmpty();
this._streams[0]._write(buf, enc, next);
};
Pipeline.prototype._notEmpty = function () {
var self = this;
if (this._streams.length > 0) return;
var stream = new PassThrough(this._options);
stream.once('end', function () {
var ix = self._streams.indexOf(stream);
if (ix >= 0 && ix === self._streams.length - 1) {
Duplex.prototype.push.call(self, null);
}
});
this._streams.push(stream);
this.length = this._streams.length;
};
Pipeline.prototype.push = function (stream) {
var args = [ this._streams.length, 0 ].concat([].slice.call(arguments));
this.splice.apply(this, args);
return this._streams.length;
};
Pipeline.prototype.pop = function () {
return this.splice(this._streams.length-1,1)[0];
};
Pipeline.prototype.shift = function () {
return this.splice(0,1)[0];
};
Pipeline.prototype.unshift = function () {
this.splice.apply(this, [0,0].concat([].slice.call(arguments)));
return this._streams.length;
};
Pipeline.prototype.splice = function (start, removeLen) {
var self = this;
var len = this._streams.length;
start = start < 0 ? len - start : start;
if (removeLen === undefined) removeLen = len - start;
removeLen = Math.max(0, Math.min(len - start, removeLen));
for (var i = start; i < start + removeLen; i++) {
if (self._streams[i-1]) {
self._streams[i-1].unpipe(self._streams[i]);
}
}
if (self._streams[i-1] && self._streams[i]) {
self._streams[i-1].unpipe(self._streams[i]);
}
var end = i;
var reps = [], args = arguments;
for (var j = 2; j < args.length; j++) (function (stream) {
if (Array.isArray(stream)) {
stream = new Pipeline(stream, self._options);
}
stream.on('error', function (err) {
err.stream = this;
self.emit('error', err);
});
stream = self._wrapStream(stream);
stream.once('end', function () {
var ix = self._streams.indexOf(stream);
if (ix >= 0 && ix === self._streams.length - 1) {
Duplex.prototype.push.call(self, null);
}
});
reps.push(stream);
})(arguments[j]);
for (var i = 0; i < reps.length - 1; i++) {
reps[i].pipe(reps[i+1]);
}
if (reps.length && self._streams[end]) {
reps[reps.length-1].pipe(self._streams[end]);
}
if (reps[0] && self._streams[start-1]) {
self._streams[start-1].pipe(reps[0]);
}
var sargs = [start,removeLen].concat(reps);
var removed = self._streams.splice.apply(self._streams, sargs);
for (var i = 0; i < reps.length; i++) {
reps[i].read(0);
}
this.emit('_mutate');
this.length = this._streams.length;
return removed;
};
Pipeline.prototype.get = function () {
if (arguments.length === 0) return undefined;
var base = this;
for (var i = 0; i < arguments.length; i++) {
var index = arguments[i];
if (index < 0) {
base = base._streams[base._streams.length + index];
}
else {
base = base._streams[index];
}
if (!base) return undefined;
}
return base;
};
Pipeline.prototype.indexOf = function (stream) {
return this._streams.indexOf(stream);
};
Pipeline.prototype._wrapStream = function (stream) {
if (typeof stream.read === 'function') return stream;
var w = new Readable(this._wrapOptions).wrap(stream);
w._write = function (buf, enc, next) {
if (stream.write(buf) === false) {
stream.once('drain', next);
}
else nextTick(next);
};
return w;
};
|