Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80522 views
1
var stream = require("readable-stream");
2
3
var duplex2 = module.exports = function duplex2(options, writable, readable) {
4
return new DuplexWrapper(options, writable, readable);
5
};
6
7
var DuplexWrapper = exports.DuplexWrapper = function DuplexWrapper(options, writable, readable) {
8
if (typeof readable === "undefined") {
9
readable = writable;
10
writable = options;
11
options = null;
12
}
13
14
options = options || {};
15
options.objectMode = true;
16
17
stream.Duplex.call(this, options);
18
19
this._bubbleErrors = (typeof options.bubbleErrors === "undefined") || !!options.bubbleErrors;
20
21
this._writable = writable;
22
this._readable = readable;
23
24
var self = this;
25
26
writable.once("finish", function() {
27
self.end();
28
});
29
30
this.once("finish", function() {
31
writable.end();
32
});
33
34
readable.on("data", function(e) {
35
if (!self.push(e)) {
36
readable.pause();
37
}
38
});
39
40
readable.once("end", function() {
41
return self.push(null);
42
});
43
44
if (this._bubbleErrors) {
45
writable.on("error", function(err) {
46
return self.emit("error", err);
47
});
48
49
readable.on("error", function(err) {
50
return self.emit("error", err);
51
});
52
}
53
};
54
DuplexWrapper.prototype = Object.create(stream.Duplex.prototype, {constructor: {value: DuplexWrapper}});
55
56
DuplexWrapper.prototype._write = function _write(input, encoding, done) {
57
this._writable.write(input, encoding, done);
58
};
59
60
DuplexWrapper.prototype._read = function _read(n) {
61
this._readable.resume();
62
};
63
64