pax_global_header00006660000000000000000000000064126176012740014520gustar00rootroot0000000000000052 comment=7d008c10499de01ba85636b5f77085ced015c37a duplexer2-0.1.4/000077500000000000000000000000001261760127400134345ustar00rootroot00000000000000duplexer2-0.1.4/.gitignore000066400000000000000000000000161261760127400154210ustar00rootroot00000000000000/node_modules duplexer2-0.1.4/.travis.yml000066400000000000000000000001151261760127400155420ustar00rootroot00000000000000sudo: false language: node_js node_js: - "0.10" - "0.12" - "4" - "5" duplexer2-0.1.4/LICENSE.md000066400000000000000000000027771261760127400150550ustar00rootroot00000000000000Copyright (c) 2013, Deoxxa Development ====================================== All rights reserved. -------------------- Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of Deoxxa Development nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY DEOXXA DEVELOPMENT ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DEOXXA DEVELOPMENT BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. duplexer2-0.1.4/README.md000066400000000000000000000051421261760127400147150ustar00rootroot00000000000000# duplexer2 [![Build Status](https://travis-ci.org/deoxxa/duplexer2.svg?branch=master)](https://travis-ci.org/deoxxa/duplexer2) [![Coverage Status](https://coveralls.io/repos/deoxxa/duplexer2/badge.svg?branch=master&service=github)](https://coveralls.io/github/deoxxa/duplexer2?branch=master) Like [duplexer](https://github.com/Raynos/duplexer) but using Streams3 ```javascript var stream = require("stream"); var duplexer2 = require("duplexer2"); var writable = new stream.Writable({objectMode: true}), readable = new stream.Readable({objectMode: true}); writable._write = function _write(input, encoding, done) { if (readable.push(input)) { return done(); } else { readable.once("drain", done); } }; readable._read = function _read(n) { // no-op }; // simulate the readable thing closing after a bit writable.once("finish", function() { setTimeout(function() { readable.push(null); }, 500); }); var duplex = duplexer2(writable, readable); duplex.on("data", function(e) { console.log("got data", JSON.stringify(e)); }); duplex.on("finish", function() { console.log("got finish event"); }); duplex.on("end", function() { console.log("got end event"); }); duplex.write("oh, hi there", function() { console.log("finished writing"); }); duplex.end(function() { console.log("finished ending"); }); ``` ``` got data "oh, hi there" finished writing got finish event finished ending got end event ``` ## Overview This is a reimplementation of [duplexer](https://www.npmjs.com/package/duplexer) using the Streams3 API which is standard in Node as of v4. Everything largely works the same. ## Installation [Available via `npm`](https://docs.npmjs.com/cli/install): ``` $ npm i duplexer2 ``` ## API ### duplexer2 Creates a new `DuplexWrapper` object, which is the actual class that implements most of the fun stuff. All that fun stuff is hidden. DON'T LOOK. ```javascript duplexer2([options], writable, readable) ``` ```javascript const duplex = duplexer2(new stream.Writable(), new stream.Readable()); ``` Arguments * __options__ - an object specifying the regular `stream.Duplex` options, as well as the properties described below. * __writable__ - a writable stream * __readable__ - a readable stream Options * __bubbleErrors__ - a boolean value that specifies whether to bubble errors from the underlying readable/writable streams. Default is `true`. ## License 3-clause BSD. [A copy](./LICENSE) is included with the source. ## Contact * GitHub ([deoxxa](http://github.com/deoxxa)) * Twitter ([@deoxxa](http://twitter.com/deoxxa)) * Email ([deoxxa@fknsrs.biz](mailto:deoxxa@fknsrs.biz)) duplexer2-0.1.4/example.js000077500000000000000000000017341261760127400154350ustar00rootroot00000000000000#!/usr/bin/env node var stream = require("readable-stream"); var duplexer2 = require("./"); var writable = new stream.Writable({objectMode: true}), readable = new stream.Readable({objectMode: true}); writable._write = function _write(input, encoding, done) { if (readable.push(input)) { return done(); } else { readable.once("drain", done); } }; readable._read = function _read(n) { // no-op }; // simulate the readable thing closing after a bit writable.once("finish", function() { setTimeout(function() { readable.push(null); }, 500); }); var duplex = duplexer2(writable, readable); duplex.on("data", function(e) { console.log("got data", JSON.stringify(e)); }); duplex.on("finish", function() { console.log("got finish event"); }); duplex.on("end", function() { console.log("got end event"); }); duplex.write("oh, hi there", function() { console.log("finished writing"); }); duplex.end(function() { console.log("finished ending"); }); duplexer2-0.1.4/index.js000066400000000000000000000032141261760127400151010ustar00rootroot00000000000000"use strict"; var stream = require("readable-stream"); function DuplexWrapper(options, writable, readable) { if (typeof readable === "undefined") { readable = writable; writable = options; options = null; } stream.Duplex.call(this, options); if (typeof readable.read !== "function") { readable = (new stream.Readable(options)).wrap(readable); } this._writable = writable; this._readable = readable; this._waiting = false; var self = this; writable.once("finish", function() { self.end(); }); this.once("finish", function() { writable.end(); }); readable.on("readable", function() { if (self._waiting) { self._waiting = false; self._read(); } }); readable.once("end", function() { self.push(null); }); if (!options || typeof options.bubbleErrors === "undefined" || options.bubbleErrors) { writable.on("error", function(err) { self.emit("error", err); }); readable.on("error", function(err) { self.emit("error", err); }); } } DuplexWrapper.prototype = Object.create(stream.Duplex.prototype, {constructor: {value: DuplexWrapper}}); DuplexWrapper.prototype._write = function _write(input, encoding, done) { this._writable.write(input, encoding, done); }; DuplexWrapper.prototype._read = function _read() { var buf; var reads = 0; while ((buf = this._readable.read()) !== null) { this.push(buf); reads++; } if (reads === 0) { this._waiting = true; } }; module.exports = function duplex2(options, writable, readable) { return new DuplexWrapper(options, writable, readable); }; module.exports.DuplexWrapper = DuplexWrapper; duplexer2-0.1.4/package.json000066400000000000000000000010261261760127400157210ustar00rootroot00000000000000{ "name": "duplexer2", "version": "0.1.4", "description": "Like duplexer but using streams3", "files": [ "index.js" ], "scripts": { "test": "mocha -R tap" }, "repository": "deoxxa/duplexer2", "keywords": [ "duplex", "duplexer", "stream", "stream3", "join", "combine" ], "author": "Conrad Pankoff (http://www.fknsrs.biz/)", "license": "BSD-3-Clause", "dependencies": { "readable-stream": "^2.0.2" }, "devDependencies": { "mocha": "^2.2.5" } } duplexer2-0.1.4/test/000077500000000000000000000000001261760127400144135ustar00rootroot00000000000000duplexer2-0.1.4/test/tests.js000066400000000000000000000132121261760127400161120ustar00rootroot00000000000000"use strict"; var assert = require("assert"); var stream = require("readable-stream"); var duplexer2 = require("../"); describe("duplexer2", function() { var writable, readable; beforeEach(function() { writable = new stream.Writable({objectMode: true}); readable = new stream.Readable({objectMode: true}); writable._write = function _write(input, encoding, done) { return done(); }; readable._read = function _read(n) { }; }); it("should interact with the writable stream properly for writing", function(done) { var duplex = duplexer2(writable, readable); writable._write = function _write(input, encoding, _done) { assert.strictEqual(input.toString(), "well hello there"); return done(); }; duplex.write("well hello there"); }); it("should interact with the readable stream properly for reading", function(done) { var duplex = duplexer2(writable, readable); duplex.on("data", function(e) { assert.strictEqual(e.toString(), "well hello there"); return done(); }); readable.push("well hello there"); }); it("should end the writable stream, causing it to finish", function(done) { var duplex = duplexer2(writable, readable); writable.once("finish", done); duplex.end(); }); it("should finish when the writable stream finishes", function(done) { var duplex = duplexer2(writable, readable); duplex.once("finish", done); writable.end(); }); it("should end when the readable stream ends", function(done) { var duplex = duplexer2(writable, readable); // required to let "end" fire without reading duplex.resume(); duplex.once("end", done); readable.push(null); }); it("should bubble errors from the writable stream when no behaviour is specified", function(done) { var duplex = duplexer2(writable, readable); var originalErr = Error("testing"); duplex.on("error", function(err) { assert.strictEqual(err, originalErr); return done(); }); writable.emit("error", originalErr); }); it("should bubble errors from the readable stream when no behaviour is specified", function(done) { var duplex = duplexer2(writable, readable); var originalErr = Error("testing"); duplex.on("error", function(err) { assert.strictEqual(err, originalErr); return done(); }); readable.emit("error", originalErr); }); it("should bubble errors from the writable stream when bubbleErrors is true", function(done) { var duplex = duplexer2({bubbleErrors: true}, writable, readable); var originalErr = Error("testing"); duplex.on("error", function(err) { assert.strictEqual(err, originalErr); return done(); }); writable.emit("error", originalErr); }); it("should bubble errors from the readable stream when bubbleErrors is true", function(done) { var duplex = duplexer2({bubbleErrors: true}, writable, readable); var originalErr = Error("testing"); duplex.on("error", function(err) { assert.strictEqual(err, originalErr); return done(); }); readable.emit("error", originalErr); }); it("should not bubble errors from the writable stream when bubbleErrors is false", function(done) { var duplex = duplexer2({bubbleErrors: false}, writable, readable); var timeout = setTimeout(done, 25); duplex.on("error", function(err) { clearTimeout(timeout); return done(Error("shouldn't bubble error")); }); // prevent uncaught error exception writable.on("error", function() {}); writable.emit("error", Error("testing")); }); it("should not bubble errors from the readable stream when bubbleErrors is false", function(done) { var duplex = duplexer2({bubbleErrors: false}, writable, readable); var timeout = setTimeout(done, 25); duplex.on("error", function(err) { clearTimeout(timeout); return done(Error("shouldn't bubble error")); }); // prevent uncaught error exception readable.on("error", function() {}); readable.emit("error", Error("testing")); }); it("should work with streams1", function(done) { var readable1 = new stream; readable1.readable = true; readable1._read = function() {} var duplex = duplexer2(writable, readable1); duplex.on("readable", function(e) { e = duplex.read() assert.strictEqual(e.toString(), "well hello there"); return done(); }); // making sure we're on compat mode. readable1.on('end', function() {}) readable1.push("well hello there"); }); it("should export the DuplexWrapper constructor", function() { assert.equal(typeof duplexer2.DuplexWrapper, "function"); }); it("should not force flowing-mode", function(done) { var writable = new stream.PassThrough(); var readable = new stream.PassThrough(); assert.equal(readable._readableState.flowing, null); var duplexStream = duplexer2(writable, readable); duplexStream.end("aaa"); assert.equal(readable._readableState.flowing, null); var transformStream = new stream.Transform({ transform: function(chunk, encoding, cb) { this.push(String(chunk).toUpperCase()); cb(); } }); writable.pipe(transformStream).pipe(readable); assert.equal(readable._readableState.flowing, null); setTimeout(function() { assert.equal(readable._readableState.flowing, null); var src = ""; duplexStream.on("data", function(buf) { src += String(buf); }); duplexStream.on("end", function() { assert.equal(src, "AAA"); done(); }); assert.equal(readable._readableState.flowing, null); }); }); });