pax_global_header00006660000000000000000000000064130152017070014506gustar00rootroot0000000000000052 comment=3023bc31dec6680dda4f935a2b320b3a4f18c815 get-stream-3.0.0/000077500000000000000000000000001301520170700135565ustar00rootroot00000000000000get-stream-3.0.0/.editorconfig000066400000000000000000000002761301520170700162400ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [{package.json,*.yml}] indent_style = space indent_size = 2 get-stream-3.0.0/.gitattributes000066400000000000000000000000351301520170700164470ustar00rootroot00000000000000* text=auto *.js text eol=lf get-stream-3.0.0/.gitignore000066400000000000000000000000151301520170700155420ustar00rootroot00000000000000node_modules get-stream-3.0.0/.travis.yml000066400000000000000000000000531301520170700156650ustar00rootroot00000000000000language: node_js node_js: - '6' - '4' get-stream-3.0.0/buffer-stream.js000066400000000000000000000015171301520170700166620ustar00rootroot00000000000000'use strict'; const PassThrough = require('stream').PassThrough; module.exports = opts => { opts = Object.assign({}, opts); const array = opts.array; let encoding = opts.encoding; const buffer = encoding === 'buffer'; let objectMode = false; if (array) { objectMode = !(encoding || buffer); } else { encoding = encoding || 'utf8'; } if (buffer) { encoding = null; } let len = 0; const ret = []; const stream = new PassThrough({objectMode}); if (encoding) { stream.setEncoding(encoding); } stream.on('data', chunk => { ret.push(chunk); if (objectMode) { len = ret.length; } else { len += chunk.length; } }); stream.getBufferedValue = () => { if (array) { return ret; } return buffer ? Buffer.concat(ret, len) : ret.join(''); }; stream.getBufferedLength = () => len; return stream; }; get-stream-3.0.0/fixture000066400000000000000000000000101301520170700151560ustar00rootroot00000000000000unicorn get-stream-3.0.0/index.js000066400000000000000000000023451301520170700152270ustar00rootroot00000000000000'use strict'; const bufferStream = require('./buffer-stream'); function getStream(inputStream, opts) { if (!inputStream) { return Promise.reject(new Error('Expected a stream')); } opts = Object.assign({maxBuffer: Infinity}, opts); const maxBuffer = opts.maxBuffer; let stream; let clean; const p = new Promise((resolve, reject) => { const error = err => { if (err) { // null check err.bufferedData = stream.getBufferedValue(); } reject(err); }; stream = bufferStream(opts); inputStream.once('error', error); inputStream.pipe(stream); stream.on('data', () => { if (stream.getBufferedLength() > maxBuffer) { reject(new Error('maxBuffer exceeded')); } }); stream.once('error', error); stream.on('end', resolve); clean = () => { // some streams doesn't implement the `stream.Readable` interface correctly if (inputStream.unpipe) { inputStream.unpipe(stream); } }; }); p.then(clean, clean); return p.then(() => stream.getBufferedValue()); } module.exports = getStream; module.exports.buffer = (stream, opts) => getStream(stream, Object.assign({}, opts, {encoding: 'buffer'})); module.exports.array = (stream, opts) => getStream(stream, Object.assign({}, opts, {array: true})); get-stream-3.0.0/license000066400000000000000000000021371301520170700151260ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) Sindre Sorhus (sindresorhus.com) 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. get-stream-3.0.0/package.json000066400000000000000000000014301301520170700160420ustar00rootroot00000000000000{ "name": "get-stream", "version": "3.0.0", "description": "Get a stream as a string, buffer, or array", "license": "MIT", "repository": "sindresorhus/get-stream", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "sindresorhus.com" }, "engines": { "node": ">=4" }, "scripts": { "test": "xo && ava" }, "files": [ "index.js", "buffer-stream.js" ], "keywords": [ "get", "stream", "promise", "concat", "string", "str", "text", "buffer", "read", "data", "consume", "readable", "readablestream", "array", "object", "obj" ], "devDependencies": { "ava": "*", "into-stream": "^3.0.0", "xo": "*" }, "xo": { "esnext": true } } get-stream-3.0.0/readme.md000066400000000000000000000074351301520170700153460ustar00rootroot00000000000000# get-stream [![Build Status](https://travis-ci.org/sindresorhus/get-stream.svg?branch=master)](https://travis-ci.org/sindresorhus/get-stream) > Get a stream as a string, buffer, or array ## Install ``` $ npm install --save get-stream ``` ## Usage ```js const fs = require('fs'); const getStream = require('get-stream'); const stream = fs.createReadStream('unicorn.txt'); getStream(stream).then(str => { console.log(str); /* ,,))))))));, __)))))))))))))), \|/ -\(((((''''((((((((. -*-==//////(('' . `)))))), /|\ ))| o ;-. '((((( ,(, ( `| / ) ;))))' ,_))^;(~ | | | ,))((((_ _____------~~~-. %,;(;(>';'~ o_); ; )))(((` ~---~ `:: \ %%~~)(v;(`('~ ; ''''```` `: `:::|\,__,%% );`'; ~ | _ ) / `:|`----' `-' ______/\/~ | / / /~;;.____/;;' / ___--,-( `;;;/ / // _;______;'------~~~~~ /;;/\ / // | | / ; \;;,\ (<_ | ; /',/-----' _> \_| ||_ //~;~~~~~~~~~ `\_| (,~~ \~\ ~~ */ }); ``` ## API The methods returns a promise that resolves when the `end` event fires on the stream, indicating that there is no more data to be read. The stream is switched to flowing mode. ### getStream(stream, [options]) Get the `stream` as a string. #### options ##### encoding Type: `string`
Default: `utf8` [Encoding](https://nodejs.org/api/buffer.html#buffer_buffer) of the incoming stream. ##### maxBuffer Type: `number`
Default: `Infinity` Maximum length of the returned string. If it exceeds this value before the stream ends, the promise will be rejected. ### getStream.buffer(stream, [options]) Get the `stream` as a buffer. It honors the `maxBuffer` option as above, but it refers to byte length rather than string length. ### getStream.array(stream, [options]) Get the `stream` as an array of values. It honors both the `maxBuffer` and `encoding` options. The behavior changes slightly based on the encoding chosen: - When `encoding` is unset, it assumes an [object mode stream](https://nodesource.com/blog/understanding-object-streams/) and collects values emitted from `stream` unmodified. In this case `maxBuffer` refers to the number of items in the array (not the sum of their sizes). - When `encoding` is set to `buffer`, it collects an array of buffers. `maxBuffer` refers to the summed byte lengths of every buffer in the array. - When `encoding` is set to anything else, it collects an array of strings. `maxBuffer` refers to the summed character lengths of every string in the array. ## Errors If the input stream emits an `error` event, the promise will be rejected with the error. The buffered data will be attached to the `bufferedData` property of the error. ```js getStream(streamThatErrorsAtTheEnd('unicorn')) .catch(err => { console.log(err.bufferedData); //=> 'unicorn' }); ``` ## FAQ ### How is this different from [`concat-stream`](https://github.com/maxogden/concat-stream)? This module accepts a stream instead of being one and returns a promise instead of using a callback. The API is simpler and it only supports returning a string, buffer, or array. It doesn't have a fragile type inference. You explicitly choose what you want. And it doesn't depend on the huge `readable-stream` package. ## Related - [get-stdin](https://github.com/sindresorhus/get-stdin) - Get stdin as a string or buffer ## License MIT © [Sindre Sorhus](https://sindresorhus.com) get-stream-3.0.0/test.js000066400000000000000000000060561301520170700151020ustar00rootroot00000000000000import fs from 'fs'; import {Readable} from 'stream'; import test from 'ava'; import intoStream from 'into-stream'; import m from './'; function makeSetup(intoStream) { const setup = (streamDef, opts) => m(intoStream(streamDef), opts); setup.array = (streamDef, opts) => m.array(intoStream(streamDef), opts); setup.buffer = (streamDef, opts) => m.buffer(intoStream(streamDef), opts); return setup; } const setup = makeSetup(intoStream); setup.obj = makeSetup(intoStream.obj); test('get stream as a buffer', async t => { t.true((await m.buffer(fs.createReadStream('fixture'))).equals(new Buffer('unicorn\n'))); }); test('get stream as an array', async t => { const fixture = fs.createReadStream('index.js', 'utf8'); fixture.setEncoding('utf8'); t.is(typeof (await m.array(fixture))[0], 'string'); }); test('get object stream as an array', async t => { const result = await setup.obj.array([{foo: true}, {bar: false}]); t.deepEqual(result, [{foo: true}, {bar: false}]); }); test('get non-object stream as an array of strings', async t => { const result = await setup.array(['foo', 'bar'], {encoding: 'utf8'}); t.deepEqual(result, ['foo', 'bar']); }); test('get non-object stream as an array of Buffers', async t => { const result = await setup.array(['foo', 'bar'], {encoding: 'buffer'}); t.deepEqual(result, [new Buffer('foo'), new Buffer('bar')]); }); test('getStream should not affect additional listeners attached to the stream', async t => { t.plan(3); const fixture = intoStream(['foo', 'bar']); fixture.on('data', chunk => t.true(Buffer.isBuffer(chunk))); t.is(await m(fixture), 'foobar'); }); test('maxBuffer throws when size is exceeded', t => { t.throws(setup(['abcd'], {maxBuffer: 3})); t.notThrows(setup(['abc'], {maxBuffer: 3})); t.throws(setup.buffer(['abcd'], {maxBuffer: 3})); t.notThrows(setup.buffer(['abc'], {maxBuffer: 3})); }); test('maxBuffer applies to length of arrays when in objectMode', t => { t.throws(m.array(intoStream.obj([{a: 1}, {b: 2}, {c: 3}, {d: 4}]), {maxBuffer: 3}), /maxBuffer exceeded/); t.notThrows(m.array(intoStream.obj([{a: 1}, {b: 2}, {c: 3}]), {maxBuffer: 3})); }); test('maxBuffer applies to length of data when not in objectMode', t => { t.throws(setup.array(['ab', 'cd', 'ef'], {encoding: 'utf8', maxBuffer: 5}), /maxBuffer exceeded/); t.notThrows(setup.array(['ab', 'cd', 'ef'], {encoding: 'utf8', maxBuffer: 6})); t.throws(setup.array(['ab', 'cd', 'ef'], {encoding: 'buffer', maxBuffer: 5}), /maxBuffer exceeded/); t.notThrows(setup.array(['ab', 'cd', 'ef'], {encoding: 'buffer', maxBuffer: 6})); }); test('Promise rejects when input stream emits an error', async t => { const readable = new Readable(); const data = 'invisible pink unicorn'; const error = new Error('Made up error'); const reads = data.match(/.{1,5}/g); readable._read = function () { if (reads.length === 0) { setImmediate(() => { this.emit('error', error); }); return; } this.push(reads.shift()); }; try { await m(readable); t.fail('should throw'); } catch (err) { t.is(err, error); t.is(err.bufferedData, data); } });