pax_global_header00006660000000000000000000000064133127441560014520gustar00rootroot0000000000000052 comment=a1e4b43ea17148b6db982eb11013cbb7f0c346e6 fast-stream-to-buffer-1.0.0/000077500000000000000000000000001331274415600156335ustar00rootroot00000000000000fast-stream-to-buffer-1.0.0/.gitignore000066400000000000000000000000151331274415600176170ustar00rootroot00000000000000node_modules fast-stream-to-buffer-1.0.0/.travis.yml000066400000000000000000000000721331274415600177430ustar00rootroot00000000000000language: node_js node_js: - '10' - '9' - '8' - '6' - '4' fast-stream-to-buffer-1.0.0/LICENSE000066400000000000000000000020761331274415600166450ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2018 Thomas Watson Steen 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. fast-stream-to-buffer-1.0.0/README.md000066400000000000000000000030711331274415600171130ustar00rootroot00000000000000# fast-stream-to-buffer Consume a stream of data into a binary Buffer as efficiently as possible. [![Build status](https://travis-ci.org/watson/fast-stream-to-buffer.svg?branch=master)](https://travis-ci.org/watson/fast-stream-to-buffer) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://github.com/feross/standard) ## Installation ``` npm install fast-stream-to-buffer --save ``` ## Usage Process an abitrary readable stream: ```js const streamToBuffer = require('fast-stream-to-buffer') streamToBuffer(stream, function (err, buf) { if (err) throw err console.log(buf.toString()) }) ``` Or use the `onStream()` helper function: ```js const http = require('http') const streamToBuffer = require('fast-stream-to-buffer') // `http.get` expects a callback as the 2nd argument that will be called // with a readable stream of the response http.get('http://example.com', streamToBuffer.onStream(function (err, buf) { if (err) throw err console.log(buf.toString('utf8')) }) ``` ## API ### `streamToBuffer(stream, callback)` Arguments: - `stream` - Any readable stream - `callback` - A callback function which will be called with an optional error object as the first argument and a buffer containing the content of the `stream` as the 2nd ### `fn = streamToBuffer.onStream(callback)` Returns a function `fn` which expects a readable stream as its only argument. When called, it will automatically call `streamToBuffer()` with the stream as the first argument and the `callback` as the second. ## License MIT fast-stream-to-buffer-1.0.0/index.js000066400000000000000000000011271331274415600173010ustar00rootroot00000000000000'use strict' const eos = require('end-of-stream') module.exports = streamToBuffer streamToBuffer.onStream = onStream function streamToBuffer (stream, cb) { const buffers = [] stream.on('data', buffers.push.bind(buffers)) eos(stream, function (err) { switch (buffers.length) { case 0: cb(err, Buffer.allocUnsafe(0), stream) break case 1: cb(err, buffers[0], stream) break default: cb(err, Buffer.concat(buffers), stream) } }) } function onStream (cb) { return function (stream) { streamToBuffer(stream, cb) } } fast-stream-to-buffer-1.0.0/package.json000066400000000000000000000016341331274415600201250ustar00rootroot00000000000000{ "name": "fast-stream-to-buffer", "version": "1.0.0", "description": "Consume a stream of data into a binary Buffer as efficiently as possible", "main": "index.js", "dependencies": { "end-of-stream": "^1.4.1" }, "devDependencies": { "standard": "^11.0.1", "tape": "^4.9.1" }, "scripts": { "test": "standard && node test.js" }, "repository": { "type": "git", "url": "git+https://github.com/watson/fast-stream-to-buffer.git" }, "keywords": [ "stream", "readable", "buffer", "string", "convert", "converter", "ReadableStream", "concat" ], "author": "Thomas Watson (https://twitter.com/wa7son)", "license": "MIT", "bugs": { "url": "https://github.com/watson/fast-stream-to-buffer/issues" }, "homepage": "https://github.com/watson/fast-stream-to-buffer#readme", "coordinates": [ 55.6773705, 12.5526636 ] } fast-stream-to-buffer-1.0.0/test.js000066400000000000000000000050021331274415600171450ustar00rootroot00000000000000'use strict' const PassThrough = require('stream').PassThrough const test = require('tape') const streamToBuffer = require('./') test('streamToBuffer() - no buffers', function (t) { const stream = new PassThrough() streamToBuffer(stream, function (err, buf) { t.error(err) t.ok(Buffer.isBuffer(buf)) t.equal(buf.length, 0) t.end() }) stream.end() }) test('streamToBuffer() - single buffer', function (t) { const stream = new PassThrough() streamToBuffer(stream, function (err, buf) { t.error(err) t.ok(Buffer.isBuffer(buf)) t.equal(buf.length, 11) t.equal(buf.toString(), 'hello world') t.end() }) stream.end('hello world') }) test('streamToBuffer() - multiple buffers', function (t) { const stream = new PassThrough() streamToBuffer(stream, function (err, buf) { t.error(err) t.ok(Buffer.isBuffer(buf)) t.equal(buf.length, 11) t.equal(buf.toString(), 'hello world') t.end() }) stream.write('hello') stream.end(' world') }) test('streamToBuffer() - error', function (t) { const stream = new PassThrough() streamToBuffer(stream, function (err, buf) { t.ok(err) t.equal(err.message, 'foo') t.end() }) stream.emit('error', new Error('foo')) }) test('streamToBuffer.onStream() - no buffers', function (t) { const stream = new PassThrough() const onStream = streamToBuffer.onStream(function (err, buf) { t.error(err) t.ok(Buffer.isBuffer(buf)) t.equal(buf.length, 0) t.end() }) onStream(stream) stream.end() }) test('streamToBuffer.onStream() - single buffer', function (t) { const stream = new PassThrough() const onStream = streamToBuffer.onStream(function (err, buf) { t.error(err) t.ok(Buffer.isBuffer(buf)) t.equal(buf.length, 11) t.equal(buf.toString(), 'hello world') t.end() }) onStream(stream) stream.end('hello world') }) test('streamToBuffer.onStream() - multiple buffers', function (t) { const stream = new PassThrough() const onStream = streamToBuffer.onStream(function (err, buf) { t.error(err) t.ok(Buffer.isBuffer(buf)) t.equal(buf.length, 11) t.equal(buf.toString(), 'hello world') t.end() }) onStream(stream) stream.write('hello') stream.end(' world') }) test('streamToBuffer.onStream() - error', function (t) { const stream = new PassThrough() const onStream = streamToBuffer.onStream(function (err, buf) { t.ok(err) t.equal(err.message, 'foo') t.end() }) onStream(stream) stream.emit('error', new Error('foo')) })