pax_global_header00006660000000000000000000000064133546327700014524gustar00rootroot0000000000000052 comment=9ddd965a50132bd70ad42befbcce08a9005c78f7 get-stream-4.1.0/000077500000000000000000000000001335463277000135765ustar00rootroot00000000000000get-stream-4.1.0/.editorconfig000066400000000000000000000002571335463277000162570ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.yml] indent_style = space indent_size = 2 get-stream-4.1.0/.gitattributes000066400000000000000000000000231335463277000164640ustar00rootroot00000000000000* text=auto eol=lf get-stream-4.1.0/.gitignore000066400000000000000000000000271335463277000155650ustar00rootroot00000000000000node_modules yarn.lock get-stream-4.1.0/.npmrc000066400000000000000000000000231335463277000147110ustar00rootroot00000000000000package-lock=false get-stream-4.1.0/.travis.yml000066400000000000000000000000641335463277000157070ustar00rootroot00000000000000language: node_js node_js: - '10' - '8' - '6' get-stream-4.1.0/buffer-stream.js000066400000000000000000000015111335463277000166740ustar00rootroot00000000000000'use strict'; const {PassThrough} = require('stream'); module.exports = options => { options = Object.assign({}, options); const {array} = options; let {encoding} = options; 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-4.1.0/fixture000066400000000000000000000000101335463277000151760ustar00rootroot00000000000000unicorn get-stream-4.1.0/index.js000066400000000000000000000023431335463277000152450ustar00rootroot00000000000000'use strict'; const pump = require('pump'); const bufferStream = require('./buffer-stream'); class MaxBufferError extends Error { constructor() { super('maxBuffer exceeded'); this.name = 'MaxBufferError'; } } function getStream(inputStream, options) { if (!inputStream) { return Promise.reject(new Error('Expected a stream')); } options = Object.assign({maxBuffer: Infinity}, options); const {maxBuffer} = options; let stream; return new Promise((resolve, reject) => { const rejectPromise = error => { if (error) { // A null check error.bufferedData = stream.getBufferedValue(); } reject(error); }; stream = pump(inputStream, bufferStream(options), error => { if (error) { rejectPromise(error); return; } resolve(); }); stream.on('data', () => { if (stream.getBufferedLength() > maxBuffer) { rejectPromise(new MaxBufferError()); } }); }).then(() => stream.getBufferedValue()); } module.exports = getStream; module.exports.buffer = (stream, options) => getStream(stream, Object.assign({}, options, {encoding: 'buffer'})); module.exports.array = (stream, options) => getStream(stream, Object.assign({}, options, {array: true})); module.exports.MaxBufferError = MaxBufferError; get-stream-4.1.0/license000066400000000000000000000021251335463277000151430ustar00rootroot00000000000000MIT License 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-4.1.0/package.json000066400000000000000000000013111335463277000160600ustar00rootroot00000000000000{ "name": "get-stream", "version": "4.1.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": ">=6" }, "scripts": { "test": "xo && ava" }, "files": [ "index.js", "buffer-stream.js" ], "keywords": [ "get", "stream", "promise", "concat", "string", "text", "buffer", "read", "data", "consume", "readable", "readablestream", "array", "object" ], "dependencies": { "pump": "^3.0.0" }, "devDependencies": { "ava": "*", "into-stream": "^3.0.0", "xo": "*" } } get-stream-4.1.0/readme.md000066400000000000000000000075731335463277000153710ustar00rootroot00000000000000# 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 get-stream ``` ## Usage ```js const fs = require('fs'); const getStream = require('get-stream'); (async () => { const stream = fs.createReadStream('unicorn.txt'); console.log(await getStream(stream)); /* ,,))))))));, __)))))))))))))), \|/ -\(((((''''((((((((. -*-==//////(('' . `)))))), /|\ ))| 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 Type: `Object` ##### 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 with a `getStream.MaxBufferError` error. ### 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 (async () => { try { await getStream(streamThatErrorsAtTheEnd('unicorn')); } catch (error) { console.log(error.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-4.1.0/test.js000066400000000000000000000064071335463277000151220ustar00rootroot00000000000000import 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(Buffer.from('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, [Buffer.from('foo'), Buffer.from('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', async t => { await t.throws(setup(['abcd'], {maxBuffer: 3})); await t.notThrows(setup(['abc'], {maxBuffer: 3})); await t.throws(setup.buffer(['abcd'], {maxBuffer: 3})); await t.notThrows(setup.buffer(['abc'], {maxBuffer: 3})); }); test('maxBuffer applies to length of arrays when in objectMode', async t => { await t.throws(m.array(intoStream.obj([{a: 1}, {b: 2}, {c: 3}, {d: 4}]), {maxBuffer: 3}), /maxBuffer exceeded/); await 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', async t => { await t.throws(setup.array(['ab', 'cd', 'ef'], {encoding: 'utf8', maxBuffer: 5}), /maxBuffer exceeded/); await t.notThrows(setup.array(['ab', 'cd', 'ef'], {encoding: 'utf8', maxBuffer: 6})); await t.throws(setup.array(['ab', 'cd', 'ef'], {encoding: 'buffer', maxBuffer: 5}), /maxBuffer exceeded/); await t.notThrows(setup.array(['ab', 'cd', 'ef'], {encoding: 'buffer', maxBuffer: 6})); }); test('maxBuffer throws a MaxBufferError', async t => { await t.throws(setup(['abcd'], {maxBuffer: 3}), m.MaxBufferError); }); 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 (error2) { t.is(error2, error); t.is(error2.bufferedData, data); } });