pax_global_header00006660000000000000000000000064131007006520014504gustar00rootroot0000000000000052 comment=76ad327e216f34cf93fee45f32dde9f158bf8b2c lead-1.0.0/000077500000000000000000000000001310070065200124075ustar00rootroot00000000000000lead-1.0.0/.editorconfig000066400000000000000000000003051310070065200150620ustar00rootroot00000000000000# http://editorconfig.org root = true [*] indent_style = space indent_size = 2 charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.md] trim_trailing_whitespace = false lead-1.0.0/.eslintrc000066400000000000000000000000301310070065200142240ustar00rootroot00000000000000{ "extends": "gulp" } lead-1.0.0/.gitignore000066400000000000000000000012541310070065200144010ustar00rootroot00000000000000# Logs logs *.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 # Compiled binary addons (http://nodejs.org/api/addons.html) build/Release # Dependency directory # Commenting this out is preferred by some people, see # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git- node_modules # Users Environment Variables .lock-wscript # Garbage files .DS_Store # Generated by integration tests test/fixtures/tmp test/fixtures/out lead-1.0.0/.jscsrc000066400000000000000000000000271310070065200136760ustar00rootroot00000000000000{ "preset": "gulp" } lead-1.0.0/.travis.yml000066400000000000000000000001711310070065200145170ustar00rootroot00000000000000sudo: false language: node_js node_js: - '6' - '5' - '4' - '0.12' - '0.10' after_script: - npm run coveralls lead-1.0.0/LICENSE000066400000000000000000000022141310070065200134130ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2017 Blaine Bublitz , Eric Schoffstall and other contributors 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. lead-1.0.0/README.md000066400000000000000000000035101310070065200136650ustar00rootroot00000000000000

# lead [![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][travis-image]][travis-url] [![AppVeyor Build Status][appveyor-image]][appveyor-url] [![Coveralls Status][coveralls-image]][coveralls-url] [![Gitter chat][gitter-image]][gitter-url] Sink your streams. ## Usage ```js var from = require('from2'); var through = require('through2'); var sink = require('lead'); // Might be used as a Transform or Writeable var maybeThrough = through(function(chunk, enc, cb) { // processing cb(null, chunk); }); from(['hello', 'world']) // Sink it to behave like a Writeable .pipe(sink(maybeThrough)) ``` ## API ### `sink(stream)` Takes a `stream` to sink and returns the same stream. Sets up event listeners to infer if the stream is being used as a `Transform` or `Writeable` stream and sinks it on `nextTick` if necessary. If the stream is being used as a `Transform` stream but becomes unpiped, it will be sunk. Respects `pipe`, `on('data')` and `on('readable')` handlers. ## License MIT [downloads-image]: http://img.shields.io/npm/dm/lead.svg [npm-url]: https://npmjs.com/package/lead [npm-image]: http://img.shields.io/npm/v/lead.svg [travis-url]: https://travis-ci.org/gulpjs/lead [travis-image]: http://img.shields.io/travis/gulpjs/lead.svg?label=travis-ci [appveyor-url]: https://ci.appveyor.com/project/gulpjs/lead [appveyor-image]: https://img.shields.io/appveyor/ci/gulpjs/lead.svg?label=appveyor [coveralls-url]: https://coveralls.io/r/gulpjs/lead [coveralls-image]: http://img.shields.io/coveralls/gulpjs/lead/master.svg [gitter-url]: https://gitter.im/gulpjs/gulp [gitter-image]: https://badges.gitter.im/gulpjs/gulp.png lead-1.0.0/appveyor.yml000066400000000000000000000007211310070065200147770ustar00rootroot00000000000000# http://www.appveyor.com/docs/appveyor-yml # http://www.appveyor.com/docs/lang/nodejs-iojs environment: matrix: # node.js - nodejs_version: "0.10" - nodejs_version: "0.12" - nodejs_version: "4" - nodejs_version: "5" - nodejs_version: "6" install: - ps: Install-Product node $env:nodejs_version - npm install test_script: - node --version - npm --version - cmd: npm test build: off # build version format version: "{build}" lead-1.0.0/index.js000066400000000000000000000022321310070065200140530ustar00rootroot00000000000000'use strict'; var Writable = require('flush-write-stream'); function listenerCount(stream, evt) { return stream.listeners(evt).length; } function hasListeners(stream) { return !!(listenerCount(stream, 'readable') || listenerCount(stream, 'data')); } function sinker(file, enc, callback) { callback(); } function sink(stream) { var sinkAdded = false; var sinkOptions = { objectMode: stream._readableState.objectMode, }; var sinkStream = new Writable(sinkOptions, sinker); function addSink() { if (sinkAdded) { return; } if (hasListeners(stream)) { return; } sinkAdded = true; stream.pipe(sinkStream); } function removeSink(evt) { if (evt !== 'readable' && evt !== 'data') { return; } if (hasListeners(stream)) { sinkAdded = false; stream.unpipe(sinkStream); } } stream.on('newListener', removeSink); stream.on('removeListener', removeSink); stream.on('removeListener', addSink); // Sink the stream to start flowing // Do this on nextTick, it will flow at slowest speed of piped streams process.nextTick(addSink); return stream; } module.exports = sink; lead-1.0.0/package.json000066400000000000000000000020441310070065200146750ustar00rootroot00000000000000{ "name": "lead", "version": "1.0.0", "description": "Sink your streams.", "author": "Gulp Team (http://gulpjs.com/)", "contributors": [ "Blaine Bublitz " ], "repository": "gulpjs/lead", "license": "MIT", "engines": { "node": ">= 0.10" }, "main": "index.js", "files": [ "LICENSE", "index.js" ], "scripts": { "lint": "eslint index.js test/ && jscs index.js test/", "pretest": "npm run lint", "test": "mocha --async-only", "cover": "istanbul cover _mocha --report lcovonly", "coveralls": "npm run cover && istanbul-coveralls" }, "dependencies": { "flush-write-stream": "^1.0.2" }, "devDependencies": { "eslint": "^1.10.3", "eslint-config-gulp": "^2.0.0", "expect": "^1.20.2", "istanbul": "^0.4.3", "istanbul-coveralls": "^1.0.3", "jscs": "^2.4.0", "jscs-preset-gulp": "^1.0.0", "mississippi": "^1.3.0", "mocha": "^3.2.0" }, "keywords": [ "streams", "sink", "through", "writeable" ] } lead-1.0.0/test/000077500000000000000000000000001310070065200133665ustar00rootroot00000000000000lead-1.0.0/test/.eslintrc000066400000000000000000000000351310070065200152100ustar00rootroot00000000000000{ "extends": "gulp/test" } lead-1.0.0/test/index.js000066400000000000000000000113121310070065200150310ustar00rootroot00000000000000'use strict'; var expect = require('expect'); var miss = require('mississippi'); var sink = require('../'); var to = miss.to; var from = miss.from; var pipe = miss.pipe; var through = miss.through; function noop() {} function count(value) { var count = 0; return through.obj(function(file, enc, cb) { count++; cb(null, file); }, function(cb) { expect(count).toEqual(value); cb(); }); } function slowCount(value) { var count = 0; return to.obj(function(file, enc, cb) { count++; setTimeout(function() { cb(null, file); }, 250); }, function(cb) { expect(count).toEqual(value); cb(); }); } describe('lead', function() { it('respects objectMode of wrapped stream', function(done) { var write = sink(through()); function assert(err) { // Forced an object through a non-object stream expect(err).toExist(); done(); } pipe([ from.obj([{}]), // Must be in the Writable position to test this // So concat-stream cannot be used write, ], assert); }); it('does not get clogged by highWaterMark', function(done) { var expectedCount = 17; var highwatermarkObjs = []; for (var idx = 0; idx < expectedCount; idx++) { highwatermarkObjs.push({}); } var write = sink(through.obj()); pipe([ from.obj(highwatermarkObjs), count(expectedCount), // Must be in the Writable position to test this // So concat-stream cannot be used write, ], done); }); it('allows backpressure when piped to another, slower stream', function(done) { this.timeout(20000); var expectedCount = 24; var highwatermarkObjs = []; for (var idx = 0; idx < expectedCount; idx++) { highwatermarkObjs.push({}); } var write = sink(through.obj()); pipe([ from.obj(highwatermarkObjs), count(expectedCount), write, slowCount(expectedCount), ], done); }); it('respects readable listeners on wrapped stream', function(done) { var write = sink(through.obj()); var readables = 0; write.on('readable', function() { var data = write.read(); if (data != null) { readables++; } }); function assert(err) { expect(readables).toEqual(1); done(err); } pipe([ from.obj([{}]), write, ], assert); }); it('respects data listeners on wrapped stream', function(done) { var write = sink(through.obj()); var datas = 0; write.on('data', function() { datas++; }); function assert(err) { expect(datas).toEqual(1); done(err); } pipe([ from.obj([{}]), write, ], assert); }); it('sinks the stream if all the readable event handlers are removed', function(done) { var expectedCount = 17; var highwatermarkObjs = []; for (var idx = 0; idx < expectedCount; idx++) { highwatermarkObjs.push({}); } var write = sink(through.obj()); write.on('readable', noop); pipe([ from.obj(highwatermarkObjs), count(expectedCount), // Must be in the Writable position to test this // So concat-stream cannot be used write, ], done); process.nextTick(function() { write.removeListener('readable', noop); }); }); it('does not sink the stream if an event handler still exists when one is removed', function(done) { var expectedCount = 17; var highwatermarkObjs = []; for (var idx = 0; idx < expectedCount; idx++) { highwatermarkObjs.push({}); } var write = sink(through.obj()); write.on('readable', noop); var readables = 0; write.on('readable', function() { var data = write.read(); if (data != null) { readables++; } }); function assert(err) { expect(readables).toEqual(expectedCount); done(err); } pipe([ from.obj(highwatermarkObjs), count(expectedCount), // Must be in the Writable position to test this // So concat-stream cannot be used write, ], assert); process.nextTick(function() { write.removeListener('readable', noop); }); }); it('sinks the stream if all the data event handlers are removed', function(done) { var expectedCount = 17; var highwatermarkObjs = []; for (var idx = 0; idx < expectedCount; idx++) { highwatermarkObjs.push({}); } var write = sink(through.obj()); write.on('data', noop); pipe([ from.obj(highwatermarkObjs), count(expectedCount), // Must be in the Writable position to test this // So concat-stream cannot be used write, ], done); process.nextTick(function() { write.removeListener('data', noop); }); }); });