pax_global_header00006660000000000000000000000064135002562500014510gustar00rootroot0000000000000052 comment=8f8032881187e3db8ba8f64106daa999f63dc785 duplexify-4.1.1/000077500000000000000000000000001350025625000135245ustar00rootroot00000000000000duplexify-4.1.1/.gitignore000066400000000000000000000000151350025625000155100ustar00rootroot00000000000000node_modules duplexify-4.1.1/.travis.yml000066400000000000000000000000741350025625000156360ustar00rootroot00000000000000language: node_js node_js: - "4" - "6" - "8" - "10" duplexify-4.1.1/LICENSE000066400000000000000000000020661350025625000145350ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2014 Mathias Buus Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.duplexify-4.1.1/README.md000066400000000000000000000052121350025625000150030ustar00rootroot00000000000000# duplexify Turn a writeable and readable stream into a single streams2 duplex stream. Similar to [duplexer2](https://github.com/deoxxa/duplexer2) except it supports both streams2 and streams1 as input and it allows you to set the readable and writable part asynchronously using `setReadable(stream)` and `setWritable(stream)` ``` npm install duplexify ``` [![build status](http://img.shields.io/travis/mafintosh/duplexify.svg?style=flat)](http://travis-ci.org/mafintosh/duplexify) ## Usage Use `duplexify(writable, readable, streamOptions)` (or `duplexify.obj(writable, readable)` to create an object stream) ``` js var duplexify = require('duplexify') // turn writableStream and readableStream into a single duplex stream var dup = duplexify(writableStream, readableStream) dup.write('hello world') // will write to writableStream dup.on('data', function(data) { // will read from readableStream }) ``` You can also set the readable and writable parts asynchronously ``` js var dup = duplexify() dup.write('hello world') // write will buffer until the writable // part has been set // wait a bit ... dup.setReadable(readableStream) // maybe wait some more? dup.setWritable(writableStream) ``` If you call `setReadable` or `setWritable` multiple times it will unregister the previous readable/writable stream. To disable the readable or writable part call `setReadable` or `setWritable` with `null`. If the readable or writable streams emits an error or close it will destroy both streams and bubble up the event. You can also explicitly destroy the streams by calling `dup.destroy()`. The `destroy` method optionally takes an error object as argument, in which case the error is emitted as part of the `error` event. ``` js dup.on('error', function(err) { console.log('readable or writable emitted an error - close will follow') }) dup.on('close', function() { console.log('the duplex stream is destroyed') }) dup.destroy() // calls destroy on the readable and writable part (if present) ``` ## HTTP request example Turn a node core http request into a duplex stream is as easy as ``` js var duplexify = require('duplexify') var http = require('http') var request = function(opts) { var req = http.request(opts) var dup = duplexify(req) req.on('response', function(res) { dup.setReadable(res) }) return dup } var req = request({ method: 'GET', host: 'www.google.com', port: 80 }) req.end() req.pipe(process.stdout) ``` ## License MIT ## Related `duplexify` is part of the [mississippi stream utility collection](https://github.com/maxogden/mississippi) which includes more useful stream modules similar to this one. duplexify-4.1.1/example.js000066400000000000000000000005621350025625000155200ustar00rootroot00000000000000var duplexify = require('duplexify') var http = require('http') var request = function(opts) { var req = http.request(opts) var dup = duplexify() dup.setWritable(req) req.on('response', function(res) { dup.setReadable(res) }) return dup } var req = request({ method: 'GET', host: 'www.google.com', port: 80 }) req.end() req.pipe(process.stdout) duplexify-4.1.1/index.js000066400000000000000000000134751350025625000152030ustar00rootroot00000000000000var stream = require('readable-stream') var eos = require('end-of-stream') var inherits = require('inherits') var shift = require('stream-shift') var SIGNAL_FLUSH = (Buffer.from && Buffer.from !== Uint8Array.from) ? Buffer.from([0]) : new Buffer([0]) var onuncork = function(self, fn) { if (self._corked) self.once('uncork', fn) else fn() } var autoDestroy = function (self, err) { if (self._autoDestroy) self.destroy(err) } var destroyer = function(self, end) { return function(err) { if (err) autoDestroy(self, err.message === 'premature close' ? null : err) else if (end && !self._ended) self.end() } } var end = function(ws, fn) { if (!ws) return fn() if (ws._writableState && ws._writableState.finished) return fn() if (ws._writableState) return ws.end(fn) ws.end() fn() } var noop = function() {} var toStreams2 = function(rs) { return new (stream.Readable)({objectMode:true, highWaterMark:16}).wrap(rs) } var Duplexify = function(writable, readable, opts) { if (!(this instanceof Duplexify)) return new Duplexify(writable, readable, opts) stream.Duplex.call(this, opts) this._writable = null this._readable = null this._readable2 = null this._autoDestroy = !opts || opts.autoDestroy !== false this._forwardDestroy = !opts || opts.destroy !== false this._forwardEnd = !opts || opts.end !== false this._corked = 1 // start corked this._ondrain = null this._drained = false this._forwarding = false this._unwrite = null this._unread = null this._ended = false this.destroyed = false if (writable) this.setWritable(writable) if (readable) this.setReadable(readable) } inherits(Duplexify, stream.Duplex) Duplexify.obj = function(writable, readable, opts) { if (!opts) opts = {} opts.objectMode = true opts.highWaterMark = 16 return new Duplexify(writable, readable, opts) } Duplexify.prototype.cork = function() { if (++this._corked === 1) this.emit('cork') } Duplexify.prototype.uncork = function() { if (this._corked && --this._corked === 0) this.emit('uncork') } Duplexify.prototype.setWritable = function(writable) { if (this._unwrite) this._unwrite() if (this.destroyed) { if (writable && writable.destroy) writable.destroy() return } if (writable === null || writable === false) { this.end() return } var self = this var unend = eos(writable, {writable:true, readable:false}, destroyer(this, this._forwardEnd)) var ondrain = function() { var ondrain = self._ondrain self._ondrain = null if (ondrain) ondrain() } var clear = function() { self._writable.removeListener('drain', ondrain) unend() } if (this._unwrite) process.nextTick(ondrain) // force a drain on stream reset to avoid livelocks this._writable = writable this._writable.on('drain', ondrain) this._unwrite = clear this.uncork() // always uncork setWritable } Duplexify.prototype.setReadable = function(readable) { if (this._unread) this._unread() if (this.destroyed) { if (readable && readable.destroy) readable.destroy() return } if (readable === null || readable === false) { this.push(null) this.resume() return } var self = this var unend = eos(readable, {writable:false, readable:true}, destroyer(this)) var onreadable = function() { self._forward() } var onend = function() { self.push(null) } var clear = function() { self._readable2.removeListener('readable', onreadable) self._readable2.removeListener('end', onend) unend() } this._drained = true this._readable = readable this._readable2 = readable._readableState ? readable : toStreams2(readable) this._readable2.on('readable', onreadable) this._readable2.on('end', onend) this._unread = clear this._forward() } Duplexify.prototype._read = function() { this._drained = true this._forward() } Duplexify.prototype._forward = function() { if (this._forwarding || !this._readable2 || !this._drained) return this._forwarding = true var data while (this._drained && (data = shift(this._readable2)) !== null) { if (this.destroyed) continue this._drained = this.push(data) } this._forwarding = false } Duplexify.prototype.destroy = function(err, cb) { if (!cb) cb = noop if (this.destroyed) return cb(null) this.destroyed = true var self = this process.nextTick(function() { self._destroy(err) cb(null) }) } Duplexify.prototype._destroy = function(err) { if (err) { var ondrain = this._ondrain this._ondrain = null if (ondrain) ondrain(err) else this.emit('error', err) } if (this._forwardDestroy) { if (this._readable && this._readable.destroy) this._readable.destroy() if (this._writable && this._writable.destroy) this._writable.destroy() } this.emit('close') } Duplexify.prototype._write = function(data, enc, cb) { if (this.destroyed) return if (this._corked) return onuncork(this, this._write.bind(this, data, enc, cb)) if (data === SIGNAL_FLUSH) return this._finish(cb) if (!this._writable) return cb() if (this._writable.write(data) === false) this._ondrain = cb else if (!this.destroyed) cb() } Duplexify.prototype._finish = function(cb) { var self = this this.emit('preend') onuncork(this, function() { end(self._forwardEnd && self._writable, function() { // haxx to not emit prefinish twice if (self._writableState.prefinished === false) self._writableState.prefinished = true self.emit('prefinish') onuncork(self, cb) }) }) } Duplexify.prototype.end = function(data, enc, cb) { if (typeof data === 'function') return this.end(null, null, data) if (typeof enc === 'function') return this.end(data, null, enc) this._ended = true if (data) this.write(data) if (!this._writableState.ending) this.write(SIGNAL_FLUSH) return stream.Writable.prototype.end.call(this, cb) } module.exports = Duplexify duplexify-4.1.1/package.json000066400000000000000000000016231350025625000160140ustar00rootroot00000000000000{ "name": "duplexify", "version": "4.1.1", "description": "Turn a writable and readable stream into a streams2 duplex stream with support for async initialization and streams1/streams2 input", "main": "index.js", "dependencies": { "end-of-stream": "^1.4.1", "inherits": "^2.0.3", "readable-stream": "^3.1.1", "stream-shift": "^1.0.0" }, "devDependencies": { "concat-stream": "^1.5.2", "tape": "^4.0.0", "through2": "^2.0.0" }, "scripts": { "test": "tape test.js" }, "repository": { "type": "git", "url": "git://github.com/mafintosh/duplexify" }, "keywords": [ "duplex", "streams2", "streams", "stream", "writable", "readable", "async" ], "author": "Mathias Buus", "license": "MIT", "bugs": { "url": "https://github.com/mafintosh/duplexify/issues" }, "homepage": "https://github.com/mafintosh/duplexify" } duplexify-4.1.1/test.js000066400000000000000000000157651350025625000150570ustar00rootroot00000000000000var tape = require('tape') var through = require('through2') var concat = require('concat-stream') var stream = require('readable-stream') var net = require('net') var duplexify = require('./') var HELLO_WORLD = (Buffer.from && Buffer.from !== Uint8Array.from) ? Buffer.from('hello world') : new Buffer('hello world') tape('passthrough', function(t) { t.plan(2) var pt = through() var dup = duplexify(pt, pt) dup.end('hello world') dup.on('finish', function() { t.ok(true, 'should finish') }) dup.pipe(concat(function(data) { t.same(data.toString(), 'hello world', 'same in as out') })) }) tape('passthrough + double end', function(t) { t.plan(2) var pt = through() var dup = duplexify(pt, pt) dup.end('hello world') dup.end() dup.on('finish', function() { t.ok(true, 'should finish') }) dup.pipe(concat(function(data) { t.same(data.toString(), 'hello world', 'same in as out') })) }) tape('async passthrough + end', function(t) { t.plan(2) var pt = through.obj({highWaterMark:1}, function(data, enc, cb) { setTimeout(function() { cb(null, data) }, 100) }) var dup = duplexify(pt, pt) dup.write('hello ') dup.write('world') dup.end() dup.on('finish', function() { t.ok(true, 'should finish') }) dup.pipe(concat(function(data) { t.same(data.toString(), 'hello world', 'same in as out') })) }) tape('duplex', function(t) { var readExpected = ['read-a', 'read-b', 'read-c'] var writeExpected = ['write-a', 'write-b', 'write-c'] t.plan(readExpected.length+writeExpected.length+2) var readable = through.obj() var writable = through.obj(function(data, enc, cb) { t.same(data, writeExpected.shift(), 'onwrite should match') cb() }) var dup = duplexify.obj(writable, readable) readExpected.slice().forEach(function(data) { readable.write(data) }) readable.end() writeExpected.slice().forEach(function(data) { dup.write(data) }) dup.end() dup.on('data', function(data) { t.same(data, readExpected.shift(), 'ondata should match') }) dup.on('end', function() { t.ok(true, 'should end') }) dup.on('finish', function() { t.ok(true, 'should finish') }) }) tape('async', function(t) { var dup = duplexify() var pt = through() dup.pipe(concat(function(data) { t.same(data.toString(), 'i was async', 'same in as out') t.end() })) dup.write('i') dup.write(' was ') dup.end('async') setTimeout(function() { dup.setWritable(pt) setTimeout(function() { dup.setReadable(pt) }, 50) }, 50) }) tape('destroy', function(t) { t.plan(2) var write = through() var read = through() var dup = duplexify(write, read) write.destroy = function() { t.ok(true, 'write destroyed') } dup.on('close', function() { t.ok(true, 'close emitted') }) dup.destroy() dup.destroy() // should only work once }) tape('destroy both', function(t) { t.plan(3) var write = through() var read = through() var dup = duplexify(write, read) write.destroy = function() { t.ok(true, 'write destroyed') } read.destroy = function() { t.ok(true, 'read destroyed') } dup.on('close', function() { t.ok(true, 'close emitted') }) dup.destroy() dup.destroy() // should only work once }) tape('bubble read errors', function(t) { t.plan(2) var write = through() var read = through() var dup = duplexify(write, read) dup.on('error', function(err) { t.same(err.message, 'read-error', 'received read error') }) dup.on('close', function() { t.ok(true, 'close emitted') }) read.emit('error', new Error('read-error')) write.emit('error', new Error('write-error')) // only emit first error }) tape('bubble write errors', function(t) { t.plan(2) var write = through() var read = through() var dup = duplexify(write, read) dup.on('error', function(err) { t.same(err.message, 'write-error', 'received write error') }) dup.on('close', function() { t.ok(true, 'close emitted') }) write.emit('error', new Error('write-error')) read.emit('error', new Error('read-error')) // only emit first error }) tape('bubble errors from write()', function(t) { t.plan(3) var errored = false var dup = duplexify(new stream.Writable({ write: function(chunk, enc, next) { next(new Error('write-error')) } })) dup.on('error', function(err) { errored = true t.same(err.message, 'write-error', 'received write error') }) dup.on('close', function() { t.pass('close emitted') t.ok(errored, 'error was emitted before close') }) dup.end('123') }) tape('destroy while waiting for drain', function(t) { t.plan(3) var errored = false var dup = duplexify(new stream.Writable({ highWaterMark: 0, write: function() {} })) dup.on('error', function(err) { errored = true t.same(err.message, 'destroy-error', 'received destroy error') }) dup.on('close', function() { t.pass('close emitted') t.ok(errored, 'error was emitted before close') }) dup.write('123') dup.destroy(new Error('destroy-error')) }) tape('reset writable / readable', function(t) { t.plan(3) var toUpperCase = function(data, enc, cb) { cb(null, data.toString().toUpperCase()) } var passthrough = through() var upper = through(toUpperCase) var dup = duplexify(passthrough, passthrough) dup.once('data', function(data) { t.same(data.toString(), 'hello') dup.setWritable(upper) dup.setReadable(upper) dup.once('data', function(data) { t.same(data.toString(), 'HELLO') dup.once('data', function(data) { t.same(data.toString(), 'HI') t.end() }) }) dup.write('hello') dup.write('hi') }) dup.write('hello') }) tape('cork', function(t) { var passthrough = through() var dup = duplexify(passthrough, passthrough) var ok = false dup.on('prefinish', function() { dup.cork() setTimeout(function() { ok = true dup.uncork() }, 100) }) dup.on('finish', function() { t.ok(ok) t.end() }) dup.end() }) tape('prefinish not twice', function(t) { var passthrough = through() var dup = duplexify(passthrough, passthrough) var prefinished = false dup.on('prefinish', function() { t.ok(!prefinished, 'only prefinish once') prefinished = true }) dup.on('finish', function() { t.end() }) dup.end() }) tape('close', function(t) { var passthrough = through() var dup = duplexify(passthrough, passthrough) passthrough.emit('close') dup.on('close', function() { t.ok(true, 'should forward close') t.end() }) }) tape('works with node native streams (net)', function(t) { t.plan(1) var server = net.createServer(function(socket) { var dup = duplexify(socket, socket) dup.once('data', function(chunk) { t.same(chunk, HELLO_WORLD) server.close() socket.end() t.end() }) }) server.listen(0, function () { var socket = net.connect(server.address().port) var dup = duplexify(socket, socket) dup.write(HELLO_WORLD) }) })