pax_global_header00006660000000000000000000000064137756236560014536gustar00rootroot0000000000000052 comment=e5160858c7fe1a317c0ba47b256cf750ac3dd866 cloneable-readable-2.1.0/000077500000000000000000000000001377562365600152175ustar00rootroot00000000000000cloneable-readable-2.1.0/.github/000077500000000000000000000000001377562365600165575ustar00rootroot00000000000000cloneable-readable-2.1.0/.github/workflows/000077500000000000000000000000001377562365600206145ustar00rootroot00000000000000cloneable-readable-2.1.0/.github/workflows/ci.yml000066400000000000000000000010151377562365600217270ustar00rootroot00000000000000name: ci on: [push, pull_request] env: CI: true jobs: test: runs-on: ${{ matrix.os }} strategy: matrix: node-version: [6.x, 8.x, 10.x, 12.x, 14.x] os: [ubuntu-latest, windows-latest, macOS-latest] steps: - uses: actions/checkout@v2 - name: Use Node.js uses: actions/setup-node@v1 with: node-version: ${{ matrix.node-version }} - name: Install run: npm install --ignore-scripts - name: Run tests run: npm run test cloneable-readable-2.1.0/.gitignore000066400000000000000000000010611377562365600172050ustar00rootroot00000000000000# Logs logs *.log npm-debug.log* # Runtime data pids *.pid *.seed # Directory for instrumented libs generated by jscoverage/JSCover lib-cov # Coverage directory used by tools like istanbul coverage # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) .grunt # node-waf configuration .lock-wscript # Compiled binary addons (http://nodejs.org/api/addons.html) build/Release # Dependency directory node_modules # Optional npm cache directory .npm # Optional REPL history .node_repl_history out .vscode package-lock.json cloneable-readable-2.1.0/.npmignore000066400000000000000000000010321377562365600172120ustar00rootroot00000000000000# Logs logs *.log npm-debug.log* # Runtime data pids *.pid *.seed # Directory for instrumented libs generated by jscoverage/JSCover lib-cov # Coverage directory used by tools like istanbul coverage # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) .grunt # node-waf configuration .lock-wscript # Compiled binary addons (http://nodejs.org/api/addons.html) build/Release # Dependency directory node_modules # Optional npm cache directory .npm # Optional REPL history .node_repl_history out big cloneable-readable-2.1.0/LICENSE000066400000000000000000000020711377562365600162240ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2016 Matteo Collina 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. cloneable-readable-2.1.0/README.md000066400000000000000000000026111377562365600164760ustar00rootroot00000000000000# cloneable-readable [![Greenkeeper badge](https://badges.greenkeeper.io/mcollina/cloneable-readable.svg)](https://greenkeeper.io/) [![Build Status](https://travis-ci.org/mcollina/cloneable-readable.svg?branch=master)](https://travis-ci.org/mcollina/cloneable-readable) Clone a Readable stream, safely. ```js 'use strict' var cloneable = require('cloneable-readable') var fs = require('fs') var pump = require('pump') var stream = cloneable(fs.createReadStream('./package.json')) pump(stream.clone(), fs.createWriteStream('./out1')) // simulate some asynchronicity setImmediate(function () { pump(stream, fs.createWriteStream('./out2')) }) ``` **cloneable-readable** automatically handles `objectMode: true`. This module comes out of an healthy discussion on the 'right' way to clone a Readable in https://github.com/gulpjs/vinyl/issues/85 and https://github.com/nodejs/readable-stream/issues/202. This is my take. **YOU MUST PIPE ALL CLONES TO START THE FLOW** You can also attach `'data'` and `'readable'` events to them. ## API ### cloneable(stream) Create a `Cloneable` stream. A Cloneable has a `clone()` method to create more clones. All clones must be resumed/piped to start the flow. ### cloneable.isCloneable(stream) Check if `stream` needs to be wrapped in a `Cloneable` or not. ## Acknowledgements This project was kindly sponsored by [nearForm](http://nearform.com). ## License MIT cloneable-readable-2.1.0/big000066400000000000000000040000001377562365600156750ustar00rootroot00000000000000cloneable-readable-2.1.0/example.js000066400000000000000000000005111377562365600172050ustar00rootroot00000000000000'use strict' const cloneable = require('./') const fs = require('fs') const pump = require('pump') const stream = cloneable(fs.createReadStream('./package.json')) pump(stream.clone(), fs.createWriteStream('./out1')) // simulate some asynchronicity setImmediate(function () { pump(stream, fs.createWriteStream('./out2')) }) cloneable-readable-2.1.0/index.js000066400000000000000000000072711377562365600166730ustar00rootroot00000000000000'use strict' const { PassThrough } = require('readable-stream') const inherits = require('inherits') const { nextTick } = require('process') function Cloneable (stream, opts) { if (!(this instanceof Cloneable)) { return new Cloneable(stream, opts) } const objectMode = stream._readableState.objectMode this._original = stream this._clonesCount = 1 this._internalPipe = false opts = opts || {} opts.objectMode = objectMode PassThrough.call(this, opts) forwardDestroy(stream, this) this.on('newListener', onData) this.on('resume', onResume) this._hasListener = true } inherits(Cloneable, PassThrough) function onData (event, listener) { if (event === 'data' || event === 'readable') { this._hasListener = false this.removeListener('newListener', onData) this.removeListener('resume', onResume) nextTick(clonePiped, this) } } function onResume () { this._hasListener = false this.removeListener('newListener', onData) this.removeListener('resume', onResume) nextTick(clonePiped, this) } Cloneable.prototype.resume = function () { if (this._internalPipe) return PassThrough.prototype.resume.call(this) } Cloneable.prototype.clone = function () { if (!this._original) { throw new Error('already started') } this._clonesCount++ // the events added by the clone should not count // for starting the flow this.removeListener('newListener', onData) this.removeListener('resume', onResume) const clone = new Clone(this) if (this._hasListener) { this.on('newListener', onData) this.on('resume', onResume) } return clone } Cloneable.prototype._destroy = function (err, cb) { if (!err) { this.push(null) this.end() } nextTick(cb, err) } function forwardDestroy (src, dest) { src.on('error', destroy) src.on('close', onClose) function destroy (err) { src.removeListener('close', onClose) dest.destroy(err) } function onClose () { dest.end() } } function clonePiped (that) { if (--that._clonesCount === 0 && !that._readableState.destroyed) { that._original.pipe(that) that._original = undefined } } function Clone (parent, opts) { if (!(this instanceof Clone)) { return new Clone(parent, opts) } const objectMode = parent._readableState.objectMode opts = opts || {} opts.objectMode = objectMode this.parent = parent PassThrough.call(this, opts) forwardDestroy(parent, this) // setting _internalPipe flag to prevent this pipe from starting // the flow. we have also overridden resume to do nothing when // this pipe tries to start the flow parent._internalPipe = true parent.pipe(this) parent._internalPipe = false // the events added by the clone should not count // for starting the flow // so we add the newListener handle after we are done this.on('newListener', onDataClone) this.once('resume', onResumeClone) } function onDataClone (event, listener) { // We start the flow once all clones are piped or destroyed if (event === 'data' || event === 'readable' || event === 'close') { nextTick(clonePiped, this.parent) this.removeListener('newListener', onDataClone) this.removeListener('resume', onResumeClone) } } function onResumeClone () { this.removeListener('newListener', onDataClone) this.removeListener('resume', onResumeClone) nextTick(clonePiped, this.parent) } inherits(Clone, PassThrough) Clone.prototype.clone = function () { return this.parent.clone() } Cloneable.isCloneable = function (stream) { return stream instanceof Cloneable || stream instanceof Clone } Clone.prototype._destroy = function (err, cb) { if (!err) { this.push(null) this.end() } nextTick(cb, err) } module.exports = Cloneable cloneable-readable-2.1.0/package.json000066400000000000000000000016041377562365600175060ustar00rootroot00000000000000{ "name": "cloneable-readable", "version": "2.1.0", "description": "Clone a Readable stream, safely", "main": "index.js", "scripts": { "test": "standard && tape test.js | tap-spec" }, "precommit": "test", "repository": { "type": "git", "url": "git+https://github.com/mcollina/cloneable-readable.git" }, "keywords": [ "readable", "stream", "clone" ], "author": "Matteo Collina ", "license": "MIT", "bugs": { "url": "https://github.com/mcollina/cloneable-readable/issues" }, "homepage": "https://github.com/mcollina/cloneable-readable#readme", "devDependencies": { "flush-write-stream": "^2.0.0", "from2": "^2.1.1", "pre-commit": "^1.1.2", "standard": "^16.0.1", "tap-spec": "^5.0.0", "tape": "^5.0.0" }, "dependencies": { "inherits": "^2.0.1", "readable-stream": "^3.3.0" } } cloneable-readable-2.1.0/test.js000066400000000000000000000364751377562365600165530ustar00rootroot00000000000000'use strict' const fs = require('fs') const path = require('path') const { test } = require('tape') const from = require('from2') const crypto = require('crypto') const sink = require('flush-write-stream') const cloneable = require('./') const pipeline = require('readable-stream').pipeline const Readable = require('readable-stream').Readable test('basic passthrough', function (t) { t.plan(2) let read = false const source = from(function (size, next) { if (read) { this.push(null) } else { read = true this.push('hello world') } next() }) const instance = cloneable(source) t.notOk(read, 'stream not started') instance.pipe(sink(function (chunk, enc, cb) { t.equal(chunk.toString(), 'hello world', 'chunk matches') cb() })) }) test('clone sync', function (t) { t.plan(4) let read = false const source = from(function (size, next) { if (read) { this.push(null) } else { read = true this.push('hello world') } next() }) const instance = cloneable(source) t.notOk(read, 'stream not started') const cloned = instance.clone() t.notOk(read, 'stream not started') instance.pipe(sink(function (chunk, enc, cb) { t.equal(chunk.toString(), 'hello world', 'chunk matches') cb() })) cloned.pipe(sink(function (chunk, enc, cb) { t.equal(chunk.toString(), 'hello world', 'chunk matches') cb() })) }) test('clone sync delayed', function (t) { t.plan(4) let read = false const source = from(function (size, next) { if (read) { this.push(null) } else { read = true this.push('hello world') } next() }) const instance = cloneable(source) t.notOk(read, 'stream not started') const cloned = instance.clone() t.notOk(read, 'stream not started') setTimeout(() => { cloned.pipe(sink(function (chunk, enc, cb) { t.equal(chunk.toString(), 'hello world', 'chunk matches') cb() })) setTimeout(() => { instance.pipe(sink(function (chunk, enc, cb) { t.equal(chunk.toString(), 'hello world', 'chunk matches') cb() })) }, 10) }, 10) }) test('clone async', function (t) { t.plan(4) let read = false const source = from(function (size, next) { if (read) { this.push(null) } else { read = true this.push('hello world') } next() }) const instance = cloneable(source) t.notOk(read, 'stream not started') const cloned = instance.clone() t.notOk(read, 'stream not started') instance.pipe(sink(function (chunk, enc, cb) { t.equal(chunk.toString(), 'hello world', 'chunk matches') cb() })) setImmediate(function () { cloned.pipe(sink(function (chunk, enc, cb) { t.equal(chunk.toString(), 'hello world', 'chunk matches') cb() })) }) }) test('basic passthrough in obj mode', function (t) { t.plan(2) let read = false const source = from.obj(function (size, next) { if (read) { return this.push(null) } else { read = true this.push({ hello: 'world' }) } next() }) const instance = cloneable(source) t.notOk(read, 'stream not started') instance.pipe(sink.obj(function (chunk, enc, cb) { t.deepEqual(chunk, { hello: 'world' }, 'chunk matches') cb() })) }) test('multiple clone in object mode', function (t) { t.plan(4) let read = false const source = from.obj(function (size, next) { if (read) { return this.push(null) } else { read = true this.push({ hello: 'world' }) } next() }) const instance = cloneable(source) t.notOk(read, 'stream not started') const cloned = instance.clone() t.notOk(read, 'stream not started') instance.pipe(sink.obj(function (chunk, enc, cb) { t.deepEqual(chunk, { hello: 'world' }, 'chunk matches') cb() })) setImmediate(function () { cloned.pipe(sink.obj(function (chunk, enc, cb) { t.deepEqual(chunk, { hello: 'world' }, 'chunk matches') cb() })) }) }) test('basic passthrough with data event', function (t) { t.plan(2) let read = false const source = from(function (size, next) { if (read) { this.push(null) } else { read = true this.push('hello world') } next() }) const instance = cloneable(source) t.notOk(read, 'stream not started') let data = '' instance.on('data', function (chunk) { data += chunk.toString() }) instance.on('end', function () { t.equal(data, 'hello world', 'chunk matches') }) }) test('basic passthrough with data event on clone', function (t) { t.plan(3) let read = false const source = from(function (size, next) { if (read) { this.push(null) } else { read = true this.push('hello world') } next() }) const instance = cloneable(source) const cloned = instance.clone() t.notOk(read, 'stream not started') let data = '' cloned.on('data', function (chunk) { data += chunk.toString() }) cloned.on('end', function () { t.equal(data, 'hello world', 'chunk matches in clone') }) instance.pipe(sink(function (chunk, enc, cb) { t.equal(chunk.toString(), 'hello world', 'chunk matches in instance') cb() })) }) test('errors if cloned after start', function (t) { t.plan(2) const source = from(function (size, next) { this.push('hello world') this.push(null) next() }) const instance = cloneable(source) instance.pipe(sink(function (chunk, enc, cb) { t.equal(chunk.toString(), 'hello world', 'chunk matches') t.throws(function () { instance.clone() }, 'throws if cloned after start') cb() })) }) test('basic passthrough with readable event', function (t) { t.plan(2) let read = false const source = from(function (size, next) { if (read) { this.push(null) } else { read = true this.push('hello world') } next() }) const instance = cloneable(source) t.notOk(read, 'stream not started') let data = '' instance.on('readable', function () { let chunk while ((chunk = this.read()) !== null) { data += chunk.toString() } }) instance.on('end', function () { t.equal(data, 'hello world', 'chunk matches') }) }) test('basic passthrough with readable event on clone', function (t) { t.plan(3) let read = false const source = from(function (size, next) { if (read) { this.push(null) } else { read = true this.push('hello world') } next() }) const instance = cloneable(source) const cloned = instance.clone() t.notOk(read, 'stream not started') let data = '' cloned.on('readable', function () { let chunk while ((chunk = this.read()) !== null) { data += chunk.toString() } }) cloned.on('end', function () { t.equal(data, 'hello world', 'chunk matches in clone') }) instance.pipe(sink(function (chunk, enc, cb) { t.equal(chunk.toString(), 'hello world', 'chunk matches in instance') cb() })) }) test('source error destroys all', function (t) { t.plan(3) const source = from() const instance = cloneable(source) const clone = instance.clone() source.on('error', function (err) { t.ok(err, 'source errors') instance.on('error', function (err2) { t.ok(err === err2, 'instance receives same error') }) clone.on('error', function (err3) { t.ok(err === err3, 'clone receives same error') }) }) source.emit('error', new Error()) }) test('source destroy destroys all', function (t) { t.plan(2) const source = from() const instance = cloneable(source) const clone = instance.clone() instance.on('end', function () { t.pass('instance has ended') }) clone.on('end', function () { t.pass('clone has ended') }) clone.resume() instance.resume() source.destroy() }) test('instance error destroys all but the source', function (t) { t.plan(4) const source = from() const instance = cloneable(source) const clone = instance.clone() source.on('close', function () { t.fail('source should not be closed') }) instance.on('error', function (err) { t.is(err.message, 'beep', 'instance errors') }) instance.on('close', function () { t.pass('close should be emitted') }) clone.on('error', function (err) { t.is(err.message, 'beep', 'instance errors') }) clone.on('close', function () { t.pass('close should be emitted') }) instance.destroy(new Error('beep')) }) test('instance destroy destroys all but the source', function (t) { t.plan(2) const source = from() const instance = cloneable(source) const clone = instance.clone() source.on('close', function () { t.fail('source should not be closed') }) instance.on('end', function () { t.pass('instance has ended') }) clone.on('end', function () { t.pass('clone has ended') }) instance.resume() clone.resume() instance.destroy() }) test('clone destroy does not affect other clones, cloneable or source', function (t) { t.plan(1) const source = from() const instance = cloneable(source) const clone = instance.clone() const other = instance.clone() source.on('close', function () { t.fail('source should not be closed') }) instance.on('close', function () { t.fail('instance should not be closed') }) other.on('close', function () { t.fail('other clone should not be closed') }) clone.on('close', function () { t.pass('clone is closed') }) clone.destroy() }) test('clone remains readable if other is destroyed', function (t) { t.plan(3) let read = false const source = from(function (size, next) { if (read) { this.push(null) } else { read = true this.push('hello') } next() }) const instance = cloneable(source) const clone = instance.clone() const other = instance.clone() instance.pipe(sink.obj(function (chunk, enc, cb) { t.deepEqual(chunk.toString(), 'hello', 'instance chunk matches') cb() })) clone.pipe(sink.obj(function (chunk, enc, cb) { t.deepEqual(chunk.toString(), 'hello', 'clone chunk matches') cb() })) clone.on('close', function () { t.fail('clone should not be closed') }) instance.on('close', function () { t.fail('instance should not be closed') }) other.on('close', function () { t.pass('other is closed') }) other.destroy() }) test('clone of clone', function (t) { t.plan(6) let read = false const source = from(function (size, next) { if (read) { this.push(null) } else { read = true this.push('hello world') } next() }) const instance = cloneable(source) t.notOk(read, 'stream not started') const cloned = instance.clone() t.notOk(read, 'stream not started') const replica = cloned.clone() t.notOk(read, 'stream not started') instance.pipe(sink(function (chunk, enc, cb) { t.equal(chunk.toString(), 'hello world', 'chunk matches') cb() })) cloned.pipe(sink(function (chunk, enc, cb) { t.equal(chunk.toString(), 'hello world', 'chunk matches') cb() })) replica.pipe(sink(function (chunk, enc, cb) { t.equal(chunk.toString(), 'hello world', 'chunk matches') cb() })) }) test('from vinyl', function (t) { t.plan(3) const source = from(['wa', 'dup']) const instance = cloneable(source) const clone = instance.clone() let data = '' let data2 = '' let ends = 2 function latch () { if (--ends === 0) { t.equal(data, data2) } } instance.on('data', function (chunk) { data += chunk.toString() }) process.nextTick(function () { t.equal('', data, 'nothing was written yet') t.equal('', data2, 'nothing was written yet') clone.on('data', function (chunk) { data2 += chunk.toString() }) }) instance.on('end', latch) clone.on('end', latch) }) test('waits till all are flowing', function (t) { t.plan(1) const source = from(['wa', 'dup']) const instance = cloneable(source) // we create a clone instance.clone() instance.on('data', function (chunk) { t.fail('this should never happen') }) process.nextTick(function () { t.pass('wait till nextTick') }) }) test('isCloneable', function (t) { t.plan(4) const source = from(['hello', ' ', 'world']) t.notOk(cloneable.isCloneable(source), 'a generic readable is not cloneable') const instance = cloneable(source) t.ok(cloneable.isCloneable(instance), 'a cloneable is cloneable') const clone = instance.clone() t.ok(cloneable.isCloneable(clone), 'a clone is cloneable') const cloneClone = clone.clone() t.ok(cloneable.isCloneable(cloneClone), 'a clone of a clone is cloneable') }) test('emits finish', function (t) { const chunks = ['a', 'b', 'c', 'd', null] const e1 = ['a', 'b', 'c', 'd'] const e2 = ['a', 'b', 'c', 'd'] t.plan(2 + e1.length + e2.length) const source = from(function (size, next) { setImmediate(next, null, chunks.shift()) }) const instance = cloneable(source) const clone = instance.clone() clone.on('finish', t.pass.bind(null, 'clone emits finish')) instance.on('finish', t.pass.bind(null, 'main emits finish')) instance.pipe(sink(function (chunk, enc, cb) { t.equal(chunk.toString(), e1.shift(), 'chunk matches') cb() })) clone.on('data', function (chunk) { t.equal(chunk.toString(), e2.shift(), 'chunk matches') }) }) test('clone async w resume', function (t) { t.plan(4) let read = false const source = from(function (size, next) { if (read) { this.push(null) } else { read = true this.push('hello world') } next() }) const instance = cloneable(source) t.notOk(read, 'stream not started') const cloned = instance.clone() t.notOk(read, 'stream not started') instance.on('end', t.pass.bind(null, 'end emitted')) instance.resume() setImmediate(function () { cloned.on('end', t.pass.bind(null, 'end emitted')) cloned.resume() }) }) test('big file', function (t) { t.plan(13) const stream = cloneable(fs.createReadStream(path.join(__dirname, 'big'))) const hash = crypto.createHash('sha1') hash.setEncoding('hex') let toCheck fs.createReadStream(path.join(__dirname, 'big')) .pipe(hash) .once('readable', function () { toCheck = hash.read() t.ok(toCheck) }) function pipe (s, num) { s.on('end', function () { t.pass('end for ' + num) }) const dest = path.join(__dirname, 'out') s.pipe(fs.createWriteStream(dest)) .on('finish', function () { t.pass('finish for ' + num) const destHash = crypto.createHash('sha1') destHash.setEncoding('hex') fs.createReadStream(dest) .pipe(destHash) .once('readable', function () { const hash = destHash.read() t.ok(hash) t.equal(hash, toCheck) }) }) } // Pipe in another event loop tick <-- this one finished only, it's the original cloneable. setImmediate(pipe.bind(null, stream, 1)) // Pipe in the same event loop tick pipe(stream.clone(), 0) // Pipe a long time after setTimeout(pipe.bind(null, stream.clone(), 2), 1000) }) test('pipeline error', function (t) { t.plan(1) const err = new Error('kaboom') pipeline([ cloneable(new Readable({ read: function () { this.destroy(err) } })), sink(function (chunk, enc, cb) { t.fail('this should not be called') }) ], function (_err) { t.equal(_err, err) }) })