3 var stream = require("stream");
5 function DuplexWrapper(options, writable, readable) {
6 if (typeof readable === "undefined") {
12 stream.Duplex.call(this, options);
14 if (typeof readable.read !== "function") {
15 readable = (new stream.Readable(options)).wrap(readable);
18 this._writable = writable;
19 this._readable = readable;
20 this._waiting = false;
24 writable.once("finish", function() {
28 this.once("finish", function() {
32 readable.on("readable", function() {
34 self._waiting = false;
39 readable.once("end", function() {
43 if (!options || typeof options.bubbleErrors === "undefined" || options.bubbleErrors) {
44 writable.on("error", function(err) {
45 self.emit("error", err);
48 readable.on("error", function(err) {
49 self.emit("error", err);
54 DuplexWrapper.prototype = Object.create(stream.Duplex.prototype, {constructor: {value: DuplexWrapper}});
56 DuplexWrapper.prototype._write = function _write(input, encoding, done) {
57 this._writable.write(input, encoding, done);
60 DuplexWrapper.prototype._read = function _read() {
63 while ((buf = this._readable.read()) !== null) {
72 module.exports = function duplex2(options, writable, readable) {
73 return new DuplexWrapper(options, writable, readable);
76 module.exports.DuplexWrapper = DuplexWrapper;