diff options
Diffstat (limited to 'node_modules/lazystream')
-rw-r--r-- | node_modules/lazystream/.npmignore | 4 | ||||
-rw-r--r-- | node_modules/lazystream/.travis.yml | 9 | ||||
-rw-r--r-- | node_modules/lazystream/LICENSE-MIT | 23 | ||||
-rw-r--r-- | node_modules/lazystream/README.md | 110 | ||||
-rw-r--r-- | node_modules/lazystream/lib/lazystream.js | 54 | ||||
-rw-r--r-- | node_modules/lazystream/package.json | 41 | ||||
-rw-r--r-- | node_modules/lazystream/secret | 59 | ||||
-rw-r--r-- | node_modules/lazystream/test/data.md | 13 | ||||
-rw-r--r-- | node_modules/lazystream/test/fs_test.js | 69 | ||||
-rw-r--r-- | node_modules/lazystream/test/helper.js | 39 | ||||
-rw-r--r-- | node_modules/lazystream/test/pipe_test.js | 36 | ||||
-rw-r--r-- | node_modules/lazystream/test/readable_test.js | 90 | ||||
-rw-r--r-- | node_modules/lazystream/test/writable_test.js | 59 |
13 files changed, 606 insertions, 0 deletions
diff --git a/node_modules/lazystream/.npmignore b/node_modules/lazystream/.npmignore new file mode 100644 index 000000000..baccd1c96 --- /dev/null +++ b/node_modules/lazystream/.npmignore @@ -0,0 +1,4 @@ +.DS_Store +npm-debug.log +node_modules/ +test/tmp/ diff --git a/node_modules/lazystream/.travis.yml b/node_modules/lazystream/.travis.yml new file mode 100644 index 000000000..01987d45c --- /dev/null +++ b/node_modules/lazystream/.travis.yml @@ -0,0 +1,9 @@ +sudo: false +language: node_js +node_js: + - "5.2" + - "4.2" + - "0.12" + - "0.10" +# - "0.8" +# - "0.6" diff --git a/node_modules/lazystream/LICENSE-MIT b/node_modules/lazystream/LICENSE-MIT new file mode 100644 index 000000000..982db1392 --- /dev/null +++ b/node_modules/lazystream/LICENSE-MIT @@ -0,0 +1,23 @@ +Copyright (c) 2013 J. Pommerening, 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. + diff --git a/node_modules/lazystream/README.md b/node_modules/lazystream/README.md new file mode 100644 index 000000000..f42fbac7c --- /dev/null +++ b/node_modules/lazystream/README.md @@ -0,0 +1,110 @@ +# Lazy Streams + +> *Create streams lazily when they are read from or written to.* +> `lazystream: 1.0.0` [](https://travis-ci.org/jpommerening/node-lazystream) + +## Why? + +Sometimes you feel the itch to open *all the files* at once. You want to pass a bunch of streams around, so the consumer does not need to worry where the data comes from. +From a software design point-of-view this sounds entirely reasonable. Then there is that neat little function `fs.createReadStream()` that opens a file and gives you a nice `fs.ReadStream` to pass around, so you use what the mighty creator deities of node bestowed upon you. + +> `Error: EMFILE, too many open files` +> ─ *node* + +This package provides two classes based on the node's Streams3 API (courtesy of `readable-stream` to ensure a stable version). + +## Class: lazystream.Readable + +A wrapper for readable streams. Extends [`stream.PassThrough`](http://nodejs.org/api/stream.html#stream_class_stream_passthrough). + +### new lazystream.Readable(fn [, options]) + +* `fn` *{Function}* + The function that the lazy stream will call to obtain the stream to actually read from. +* `options` *{Object}* + Options for the underlying `PassThrough` stream, accessible by `fn`. + +Creates a new readable stream. Once the stream is accessed (for example when you call its `read()` method, or attach a `data`-event listener) the `fn` function is called with the outer `lazystream.Readable` instance bound to `this`. + +If you pass an `options` object to the constuctor, you can access it in your `fn` function. + +```javascript +new lazystream.Readable(function (options) { + return fs.createReadStream('/dev/urandom'); +}); +``` + +## Class: lazystream.Writable + +A wrapper for writable streams. Extends [`stream.PassThrough`](http://nodejs.org/api/stream.html#stream_class_stream_passthrough). + +### new lazystream.Writable(fn [, options]) + +* `fn` *{Function}* + The function that the lazy stream will call to obtain the stream to actually write to. +* `options` *{Object}* + Options for the underlying `PassThrough` stream, accessible by `fn`. + +Creates a new writable stream. Just like the one above but for writable streams. + +```javascript +new lazystream.Writable(function () { + return fs.createWriteStream('/dev/null'); +}); +``` + +## Install + +```console +$ npm install lazystream --save +lazystream@1.0.0 node_modules/lazystream +└── readable-stream@2.0.5 +``` + +## Changelog + +### v1.0.0 + +- [#2](https://github.com/jpommerening/node-lazystream/pull/2): [unconditionally](https://r.va.gg/2014/06/why-i-dont-use-nodes-core-stream-module.html) use `readable-stream` _2.x_. + +### v0.2.0 + +- [#1](https://github.com/jpommerening/node-lazystream/pull/1): error events are now propagated + +### v0.1.0 + +- _(this was the first release)_ + +## Contributing + +Fork it, branch it, send me a pull request. We'll work out the rest together. + +## Credits + +[Chris Talkington](https://github.com/ctalkington) and his [node-archiver](https://github.com/ctalkington/node-archiver) for providing a use-case. + +## [License](LICENSE-MIT) + +Copyright (c) 2013 J. Pommerening, 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. + diff --git a/node_modules/lazystream/lib/lazystream.js b/node_modules/lazystream/lib/lazystream.js new file mode 100644 index 000000000..d9f617096 --- /dev/null +++ b/node_modules/lazystream/lib/lazystream.js @@ -0,0 +1,54 @@ +var util = require('util'); +var PassThrough = require('readable-stream/passthrough'); + +module.exports = { + Readable: Readable, + Writable: Writable +}; + +util.inherits(Readable, PassThrough); +util.inherits(Writable, PassThrough); + +// Patch the given method of instance so that the callback +// is executed once, before the actual method is called the +// first time. +function beforeFirstCall(instance, method, callback) { + instance[method] = function() { + delete instance[method]; + callback.apply(this, arguments); + return this[method].apply(this, arguments); + }; +} + +function Readable(fn, options) { + if (!(this instanceof Readable)) + return new Readable(fn, options); + + PassThrough.call(this, options); + + beforeFirstCall(this, '_read', function() { + var source = fn.call(this, options); + var emit = this.emit.bind(this, 'error'); + source.on('error', emit); + source.pipe(this); + }); + + this.emit('readable'); +} + +function Writable(fn, options) { + if (!(this instanceof Writable)) + return new Writable(fn, options); + + PassThrough.call(this, options); + + beforeFirstCall(this, '_write', function() { + var destination = fn.call(this, options); + var emit = this.emit.bind(this, 'error'); + destination.on('error', emit); + this.pipe(destination); + }); + + this.emit('writable'); +} + diff --git a/node_modules/lazystream/package.json b/node_modules/lazystream/package.json new file mode 100644 index 000000000..a31a48cdf --- /dev/null +++ b/node_modules/lazystream/package.json @@ -0,0 +1,41 @@ +{ + "name": "lazystream", + "version": "1.0.0", + "description": "Open Node Streams on demand.", + "homepage": "https://github.com/jpommerening/node-lazystream", + "author": { + "name": "Jonas Pommerening", + "email": "jonas.pommerening@gmail.com", + "url": "https://npmjs.org/~jpommerening" + }, + "contributors": [ + "Mario Casciaro <mariocasciaro@gmail.com>" + ], + "repository": { + "type": "git", + "url": "https://github.com/jpommerening/node-lazystream.git" + }, + "bugs": { + "url": "https://github.com/jpommerening/node-lazystream/issues" + }, + "license": "MIT", + "main": "lib/lazystream.js", + "engines": { + "node": ">= 0.6.3" + }, + "scripts": { + "test": "nodeunit test/readable_test.js test/writable_test.js test/pipe_test.js test/fs_test.js" + }, + "dependencies": { + "readable-stream": "^2.0.5" + }, + "devDependencies": { + "nodeunit": "^0.9.1" + }, + "keywords": [ + "emfile", + "lazy", + "streams", + "stream" + ] +} diff --git a/node_modules/lazystream/secret b/node_modules/lazystream/secret new file mode 100644 index 000000000..4ff775143 --- /dev/null +++ b/node_modules/lazystream/secret @@ -0,0 +1,59 @@ +-----BEGIN PGP PRIVATE KEY BLOCK----- +Version: GnuPG v1 + +lQO+BFRFUBsBCACycWpDBPLAf7Pfk3SjZPmFrV/uVJRKoetYDVWgMvmjlm5u/RQH +CI7uMujKLLcWeVxwqRcsc65iqyfgGp/TpGpPg3N7ss9NJN7f5xou2KtZUWN2PTKa +A09tnaBaKACyursPLRqHFRl565/ETiZ2/VpHFYuEw8fXlvo5L5rQFgh4oiSdwWHk +yyCcUJuDa70rkfgmWF/3ZUkX3U57e8rrGQ2xezLk5cI5ijA8VCrXY+EPWTxWtyCb +j1mJ0UjApWOPKjahdvR6K6kHebQLGhDIR+dlKb/VZqZSxj4Dta5jxId8DO6nSsqE +Xt7Y69ud024YApyz22zg0LA+4KXde+SmYLLhABEBAAH+AwMCM3z5FykaX/xgLcWw +RpUUMvwe+cPQBCB17LcP+JU3T/+CnTYpGviTc7T+kpggqq+77cz+9Pz+MXlLaQUG +ztK/WzNUbd5HPjRxUAIbyvfnGXo8oqBd3Fmho/oE61e1jMmpveh2icipvrdXKF8/ +WJYWaThHlT9fwltqsfFzCW7dFW1txZgVD8RKY9/TNw68FHJyuGQNNPscg6Eda3W1 +wMO70c2lDUUnFdIFQT31UHcrDOoeVQ+/R4/P7MmqrumkVHbzQXkWktrMn7XCL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dBQth +48b4kvdvYYWA3BFVEiOvhjcJJ2UN2+0V07mZQnQ854eNKCkgmg57OVGlqePOd8j2 +lUKCy7f8G05NSHZxmsA/GPCkNviWNgp78oULxR+PBGkQNp7oyKvHFo0KesOAy406 +jXGccPrlTHQgGw2A5FEmPc080iCyz3D2Q9hPhU8UpmUNIyis50vRJZmJ+4146bXG +nS1oAo/YpUVs6Olo6ffoJUgBpQKZISuMS+WW18gp0A66jALMX0yPhWrw1etX+XlD +RDSP2PnV74Y9BrlFFSYeSN2vhQIJJvD9ARxP631Bsp0pu6VLiQElBBgBAgAPBQJU +RVAbAhsMBQkJZgGAAAoJEEEwTQNB1Fp8BekH/jqCKoeA+ru4RNLFGifHXNjrhDQf +XW2jdmPbpx5PQTLMOWV2l1DREwdnr3hb6jGj3xFFhSg0B3EGHCD45QUcsVHzJWzW +DHo7Q0dHa2bj8d5fDgYF15XKGpZSe/f39YvI5TdDi6cK+3WCc48zoDycYN/5YxKm +nHVvHa6TYPzuUpJFJFllrrFtoas9/5CnXcYSbjdKLKEbfBqj9YiD69p/raUY2rGB +CVJMv2OTQbGzfnMPfse/4U1XgNUVpF3LMcVHG13KjutzTcBQ/7VbQnIctTg9WBTk +R74+nLrZmKNgwwN6Y3jXEz2JZtxebyY6zG1EvNiO5sAJnCJAk8VGDnIB+sc= +=EeYS +-----END PGP PRIVATE KEY BLOCK----- diff --git a/node_modules/lazystream/test/data.md b/node_modules/lazystream/test/data.md new file mode 100644 index 000000000..fc4822201 --- /dev/null +++ b/node_modules/lazystream/test/data.md @@ -0,0 +1,13 @@ +> Never mind, hey, this is really exciting, so much to find out about, so much to +> look forward to, I'm quite dizzy with anticipation . . . Or is it the wind? +> +> There really is a lot of that now, isn't there? And wow! Hey! What's this thing +> suddenly coming toward me very fast? Very, very fast. So big and flat and round, +> it needs a big wide-sounding name like . . . ow . . . ound . . . round . . . +> ground! That's it! That's a good name- ground! +> +> I wonder if it will be friends with me? +> +> Hello Ground! + +And the rest, after a sudden wet thud, was silence. diff --git a/node_modules/lazystream/test/fs_test.js b/node_modules/lazystream/test/fs_test.js new file mode 100644 index 000000000..149b1c4a0 --- /dev/null +++ b/node_modules/lazystream/test/fs_test.js @@ -0,0 +1,69 @@ + +var stream = require('../lib/lazystream'); +var fs = require('fs'); +var tmpDir = 'test/tmp/'; +var readFile = 'test/data.md'; +var writeFile = tmpDir + 'data.md'; + +exports.fs = { + readwrite: function(test) { + var readfd, writefd; + + var readable = new stream.Readable(function() { + return fs.createReadStream(readFile) + .on('open', function(fd) { + readfd = fd; + }) + .on('close', function() { + readfd = undefined; + step(); + }); + }); + + var writable = new stream.Writable(function() { + return fs.createWriteStream(writeFile) + .on('open', function(fd) { + writefd = fd; + }) + .on('close', function() { + writefd = undefined; + step(); + }); + }); + + test.expect(3); + + test.equal(readfd, undefined, 'Input file should not be opened until read'); + test.equal(writefd, undefined, 'Output file should not be opened until write'); + + if (!fs.existsSync(tmpDir)) { + fs.mkdirSync(tmpDir); + } + if (fs.existsSync(writeFile)) { + fs.unlinkSync(writeFile); + } + + readable.on('end', function() { step(); }); + writable.on('end', function() { step(); }); + + var steps = 0; + function step() { + steps += 1; + if (steps == 4) { + var input = fs.readFileSync(readFile); + var output = fs.readFileSync(writeFile); + + test.ok(input >= output && input <= output, 'Should be equal'); + + fs.unlinkSync(writeFile); + fs.rmdirSync(tmpDir); + + test.done(); + } + }; + + readable.pipe(writable); + } +}; + + diff --git a/node_modules/lazystream/test/helper.js b/node_modules/lazystream/test/helper.js new file mode 100644 index 000000000..9d41191da --- /dev/null +++ b/node_modules/lazystream/test/helper.js @@ -0,0 +1,39 @@ + +var _Readable = require('readable-stream/readable'); +var _Writable = require('readable-stream/writable'); +var util = require('util'); + +module.exports = { + DummyReadable: DummyReadable, + DummyWritable: DummyWritable +}; + +function DummyReadable(strings) { + _Readable.call(this); + this.strings = strings; + this.emit('readable'); +} + +util.inherits(DummyReadable, _Readable); + +DummyReadable.prototype._read = function _read(n) { + if (this.strings.length) { + this.push(new Buffer(this.strings.shift())); + } else { + this.push(null); + } +}; + +function DummyWritable(strings) { + _Writable.call(this); + this.strings = strings; + this.emit('writable'); +} + +util.inherits(DummyWritable, _Writable); + +DummyWritable.prototype._write = function _write(chunk, encoding, callback) { + this.strings.push(chunk.toString()); + if (callback) callback(); +}; + diff --git a/node_modules/lazystream/test/pipe_test.js b/node_modules/lazystream/test/pipe_test.js new file mode 100644 index 000000000..7129e3591 --- /dev/null +++ b/node_modules/lazystream/test/pipe_test.js @@ -0,0 +1,36 @@ + +var stream = require('../lib/lazystream'); +var helper = require('./helper'); + +exports.pipe = { + readwrite: function(test) { + var expected = [ 'line1\n', 'line2\n' ]; + var actual = []; + var readableInstantiated = false; + var writableInstantiated = false; + + test.expect(3); + + var readable = new stream.Readable(function() { + readableInstantiated = true; + return new helper.DummyReadable([].concat(expected)); + }); + + var writable = new stream.Writable(function() { + writableInstantiated = true; + return new helper.DummyWritable(actual); + }); + + test.equal(readableInstantiated, false, 'DummyReadable should only be instantiated when it is needed'); + test.equal(writableInstantiated, false, 'DummyWritable should only be instantiated when it is needed'); + + writable.on('end', function() { + test.equal(actual.join(''), expected.join(''), 'Piping on demand streams should keep data intact'); + test.done(); + }); + + readable.pipe(writable); + } +}; + + diff --git a/node_modules/lazystream/test/readable_test.js b/node_modules/lazystream/test/readable_test.js new file mode 100644 index 000000000..9ae06368d --- /dev/null +++ b/node_modules/lazystream/test/readable_test.js @@ -0,0 +1,90 @@ + +var Readable = require('../lib/lazystream').Readable; +var DummyReadable = require('./helper').DummyReadable; + +exports.readable = { + dummy: function(test) { + var expected = [ 'line1\n', 'line2\n' ]; + var actual = []; + + test.expect(1); + + new DummyReadable([].concat(expected)) + .on('data', function(chunk) { + actual.push(chunk.toString()); + }) + .on('end', function() { + test.equal(actual.join(''), expected.join(''), 'DummyReadable should produce the data it was created with'); + test.done(); + }); + }, + options: function(test) { + test.expect(3); + + var readable = new Readable(function(options) { + test.ok(this instanceof Readable, "Readable should bind itself to callback's this"); + test.equal(options.encoding, "utf-8", "Readable should make options accessible to callback"); + this.ok = true; + return new DummyReadable(["test"]); + }, {encoding: "utf-8"}); + + readable.read(4); + + test.ok(readable.ok); + + test.done(); + }, + streams2: function(test) { + var expected = [ 'line1\n', 'line2\n' ]; + var actual = []; + var instantiated = false; + + test.expect(2); + + var readable = new Readable(function() { + instantiated = true; + return new DummyReadable([].concat(expected)); + }); + + test.equal(instantiated, false, 'DummyReadable should only be instantiated when it is needed'); + + readable.on('readable', function() { + var chunk; + while ((chunk = readable.read())) { + actual.push(chunk.toString()); + } + }); + readable.on('end', function() { + test.equal(actual.join(''), expected.join(''), 'Readable should not change the data of the underlying stream'); + test.done(); + }); + + readable.read(0); + }, + resume: function(test) { + var expected = [ 'line1\n', 'line2\n' ]; + var actual = []; + var instantiated = false; + + test.expect(2); + + var readable = new Readable(function() { + instantiated = true; + return new DummyReadable([].concat(expected)); + }); + + readable.pause(); + + readable.on('data', function(chunk) { + actual.push(chunk.toString()); + }); + readable.on('end', function() { + test.equal(actual.join(''), expected.join(''), 'Readable should not change the data of the underlying stream'); + test.done(); + }); + + test.equal(instantiated, false, 'DummyReadable should only be instantiated when it is needed'); + + readable.resume(); + } +}; diff --git a/node_modules/lazystream/test/writable_test.js b/node_modules/lazystream/test/writable_test.js new file mode 100644 index 000000000..a66384564 --- /dev/null +++ b/node_modules/lazystream/test/writable_test.js @@ -0,0 +1,59 @@ + +var Writable = require('../lib/lazystream').Writable; +var DummyWritable = require('./helper').DummyWritable; + +exports.writable = { + options: function(test) { + test.expect(3); + + var writable = new Writable(function(options) { + test.ok(this instanceof Writable, "Writable should bind itself to callback's this"); + test.equal(options.encoding, "utf-8", "Writable should make options accessible to callback"); + this.ok = true; + return new DummyWritable([]); + }, {encoding: "utf-8"}); + + writable.write("test"); + + test.ok(writable.ok); + + test.done(); + }, + dummy: function(test) { + var expected = [ 'line1\n', 'line2\n' ]; + var actual = []; + + test.expect(0); + + var dummy = new DummyWritable(actual); + + expected.forEach(function(item) { + dummy.write(new Buffer(item)); + }); + test.done(); + }, + streams2: function(test) { + var expected = [ 'line1\n', 'line2\n' ]; + var actual = []; + var instantiated = false; + + test.expect(2); + + var writable = new Writable(function() { + instantiated = true; + return new DummyWritable(actual); + }); + + test.equal(instantiated, false, 'DummyWritable should only be instantiated when it is needed'); + + writable.on('end', function() { + test.equal(actual.join(''), expected.join(''), 'Writable should not change the data of the underlying stream'); + test.done(); + }); + + expected.forEach(function(item) { + writable.write(new Buffer(item)); + }); + writable.end(); + } +}; |