package/package.json000644 001750 001750 0000000774 12415546666013046 0ustar00000000 000000 { "name": "streamsink", "version": "1.2.0", "description": "pipe to a buffer, then create readable streams from it", "main": "index.js", "scripts": { "test": "node test.js" }, "repository": { "type": "git", "url": "git://github.com/andrewrk/node-streamsink.git" }, "author": "Andrew Kelley ", "license": "MIT", "bugs": { "url": "https://github.com/andrewrk/node-streamsink/issues" }, "homepage": "https://github.com/andrewrk/node-streamsink" } package/README.md000644 001750 001750 0000001226 12415546505012020 0ustar00000000 000000 # node-streamsink Pipe a stream to a `StreamSink`, and then you can create a `ReadableStream`, `String`, or `Buffer` from the `StreamSink`. ## Usage ```js var StreamSink = require('streamsink'); var sink = new StreamSink(); fs.createReadStream("foo.txt").pipe(sink); sink.on('finish', function() { // sink has now buffered foo.txt sink.createReadStream().pipe(someDestination); // or use toString([encoding], [start], [end]) console.log(sink.toString('utf8')); // or use toBuffer() sink.toBuffer(); }); // you can also create instances from a list of buffers var sink = StreamSink.fromBufferList([new Buffer("aoeu"), new Buffer("foo")]); ``` package/LICENSE000644 001750 001750 0000002072 12415544667011555 0ustar00000000 000000 The MIT License (Expat) Copyright (c) 2014 Andrew Kelley 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. package/index.js000644 001750 001750 0000002411 12415546350012201 0ustar00000000 000000 var stream = require('stream'); var util = require('util'); module.exports = StreamSink; StreamSink.fromBuffer = fromBuffer; StreamSink.fromBufferList = fromBufferList; function fromBuffer(buffer, options) { return fromBufferList([buffer], options); } function fromBufferList(bufferList, options) { var sink = new StreamSink(options); sink.buffer = bufferList; sink.length = 0; bufferList.forEach(function(buffer) { sink.length += buffer.length; }); return sink; } util.inherits(StreamSink, stream.Writable); function StreamSink(options) { stream.Writable.call(this, options); this.buffer = []; this.length = 0; } StreamSink.prototype._write = function(chunk, encoding, callback) { this.buffer.push(chunk); this.length += chunk.length; callback(); }; StreamSink.prototype.createReadStream = function(options) { var s = new stream.Readable(options); s.buffer = this.buffer; s._read = function(size) { for (var i = 0; i < s.buffer.length; i += 1) { s.push(s.buffer[i]); } s.push(null); }; return s; }; StreamSink.prototype.toString = function(encoding, start, end) { return this.toBuffer().toString(encoding, start, end); }; StreamSink.prototype.toBuffer = function() { return Buffer.concat(this.buffer, this.length); }; package/test.js000644 001750 001750 0000001377 12415546622012065 0ustar00000000 000000 var StreamSink = require('./'); var assert = require('assert'); var sink = new StreamSink(); sink.on('finish', function() { var s = sink.createReadStream(); var newSink = new StreamSink(); newSink.on('finish', function() { assert.strictEqual(newSink.toString(), "hi"); var buf = sink.toBuffer(); assert.strictEqual(buf.length, 2); assert.strictEqual(buf[0], 104); assert.strictEqual(buf[1], 105); var s2 = StreamSink.fromBuffer(new Buffer("aoeu")); assert.strictEqual(s2.toString(), "aoeu"); var s3 = StreamSink.fromBufferList([new Buffer("aoeu"), new Buffer("asdf")]); assert.strictEqual(s3.toString(), "aoeuasdf"); console.log("OK"); }); sink.createReadStream().pipe(newSink); }); sink.write("hi"); sink.end();