├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── example.js ├── index.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '0.10' 4 | - '0.12' 5 | - '4' 6 | - '6' 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Mathias Buus 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # dev-zero-stream 2 | 3 | Stream just returns blank buffers. Like reading from /dev/zero on Unix. 4 | 5 | ``` 6 | npm install dev-zero-stream 7 | ``` 8 | 9 | [![build status](https://travis-ci.org/mafintosh/dev-zero-stream.svg?branch=master)](http://travis-ci.org/mafintosh/dev-zero-stream) 10 | 11 | ## Usage 12 | 13 | ``` js 14 | var zero = require('dev-zero-stream') 15 | var fs = require('fs') 16 | 17 | var stream = zero(50 * 1024 * 1024) // 50mb of blank data 18 | stream.pipe(fs.createWriteStream('/tmp/test.blank')) 19 | ``` 20 | 21 | You can also create an infinite blank stream 22 | 23 | ``` js 24 | var stream = zero() 25 | 26 | stream.on('data', function (data) { 27 | // will never stop firing 28 | console.log('data:', data) 29 | }) 30 | ``` 31 | 32 | ## API 33 | 34 | #### `var stream = zero(length)` 35 | 36 | Create a new blank stream. Length can be set to the byte length of the stream. 37 | If not provided the stream will have an infinite length. 38 | 39 | ## License 40 | 41 | MIT 42 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | var zero = require('./') 2 | var fs = require('fs') 3 | 4 | var stream = zero(50 * 1024 * 1024) 5 | 6 | stream.pipe(fs.createWriteStream('/tmp/test.blank')) 7 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var stream = require('readable-stream') 2 | var inherits = require('inherits') 3 | 4 | var BLANK = new Buffer(65536) 5 | BLANK.fill(0) 6 | 7 | module.exports = ZeroStream 8 | 9 | function ZeroStream (length, blank) { 10 | if (!(this instanceof ZeroStream)) return new ZeroStream(length, blank) 11 | this.remaining = typeof length === 'number' 12 | ? length 13 | : Infinity 14 | this.blank = blank || BLANK 15 | stream.Readable.call(this) 16 | } 17 | 18 | inherits(ZeroStream, stream.Readable) 19 | 20 | ZeroStream.prototype._read = function () { 21 | if (this.destroyed) return 22 | 23 | if (this.remaining >= this.blank.length) { 24 | this.remaining -= this.blank.length 25 | this.push(this.blank) 26 | } else if (this.remaining) { 27 | var last = this.blank.slice(0, this.remaining) 28 | this.remaining = 0 29 | this.push(last) 30 | } else { 31 | this.push(null) 32 | } 33 | } 34 | 35 | ZeroStream.prototype.destroy = function (err) { 36 | this.destroyed = true 37 | if (err) this.emit('error', err) 38 | this.emit('close') 39 | } 40 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dev-zero-stream", 3 | "version": "1.1.1", 4 | "description": "Stream just returns blank buffers. Like reading from /dev/zero on Unix", 5 | "main": "index.js", 6 | "dependencies": { 7 | "inherits": "^2.0.3", 8 | "readable-stream": "^2.1.5" 9 | }, 10 | "devDependencies": { 11 | "concat-stream": "^1.5.2", 12 | "standard": "^8.5.0", 13 | "tape": "^4.6.2" 14 | }, 15 | "scripts": { 16 | "test": "standard && tape test.js" 17 | }, 18 | "repository": { 19 | "type": "git", 20 | "url": "https://github.com/mafintosh/dev-zero-stream.git" 21 | }, 22 | "author": "Mathias Buus (@mafintosh)", 23 | "license": "MIT", 24 | "bugs": { 25 | "url": "https://github.com/mafintosh/dev-zero-stream/issues" 26 | }, 27 | "homepage": "https://github.com/mafintosh/dev-zero-stream" 28 | } 29 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var zero = require('./') 2 | var concat = require('concat-stream') 3 | var tape = require('tape') 4 | 5 | tape('is blank data', function (t) { 6 | zero(111111).pipe(concat(function (data) { 7 | var blank = new Buffer(111111) 8 | blank.fill(0) 9 | t.same(data, blank) 10 | t.end() 11 | })) 12 | }) 13 | 14 | tape('is blank data (same as internal buffer)', function (t) { 15 | zero(65536).pipe(concat(function (data) { 16 | var blank = new Buffer(65536) 17 | blank.fill(0) 18 | t.same(data, blank) 19 | t.end() 20 | })) 21 | }) 22 | 23 | tape('can be infinite', function (t) { 24 | var inf = zero() 25 | var tick = 1000 26 | 27 | inf.on('data', function () { 28 | tick-- 29 | if (!tick) inf.destroy() 30 | }) 31 | 32 | inf.on('close', function () { 33 | t.pass('emitted 1000 buffers and was destroyed') 34 | t.end() 35 | }) 36 | }) 37 | 38 | tape('can be empty', function (t) { 39 | var empty = zero(0) 40 | empty.on('data', function () { 41 | t.fail() 42 | }) 43 | empty.on('end', function () { 44 | t.pass() 45 | t.end() 46 | }) 47 | }) 48 | --------------------------------------------------------------------------------