get-stream
Advanced tools
Comparing version 3.0.0 to 4.0.0
'use strict'; | ||
const PassThrough = require('stream').PassThrough; | ||
const {PassThrough} = require('stream'); | ||
module.exports = opts => { | ||
opts = Object.assign({}, opts); | ||
module.exports = options => { | ||
options = Object.assign({}, options); | ||
const array = opts.array; | ||
let encoding = opts.encoding; | ||
const {array} = options; | ||
let {encoding} = options; | ||
const buffer = encoding === 'buffer'; | ||
@@ -10,0 +10,0 @@ let objectMode = false; |
53
index.js
'use strict'; | ||
const pump = require('pump'); | ||
const bufferStream = require('./buffer-stream'); | ||
function getStream(inputStream, opts) { | ||
function getStream(inputStream, options) { | ||
if (!inputStream) { | ||
@@ -9,44 +10,34 @@ return Promise.reject(new Error('Expected a stream')); | ||
opts = Object.assign({maxBuffer: Infinity}, opts); | ||
options = Object.assign({maxBuffer: Infinity}, options); | ||
const maxBuffer = opts.maxBuffer; | ||
const {maxBuffer} = options; | ||
let stream; | ||
let clean; | ||
return new Promise((resolve, reject) => { | ||
const rejectPromise = error => { | ||
if (error) { // A null check | ||
error.bufferedData = stream.getBufferedValue(); | ||
} | ||
reject(error); | ||
}; | ||
const p = new Promise((resolve, reject) => { | ||
const error = err => { | ||
if (err) { // null check | ||
err.bufferedData = stream.getBufferedValue(); | ||
stream = pump(inputStream, bufferStream(options), error => { | ||
if (error) { | ||
rejectPromise(error); | ||
return; | ||
} | ||
reject(err); | ||
}; | ||
resolve(); | ||
}); | ||
stream = bufferStream(opts); | ||
inputStream.once('error', error); | ||
inputStream.pipe(stream); | ||
stream.on('data', () => { | ||
if (stream.getBufferedLength() > maxBuffer) { | ||
reject(new Error('maxBuffer exceeded')); | ||
rejectPromise(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()); | ||
}).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})); | ||
module.exports.buffer = (stream, options) => getStream(stream, Object.assign({}, options, {encoding: 'buffer'})); | ||
module.exports.array = (stream, options) => getStream(stream, Object.assign({}, options, {array: true})); |
{ | ||
"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 | ||
} | ||
"name": "get-stream", | ||
"version": "4.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": ">=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": "*" | ||
} | ||
} |
@@ -9,3 +9,3 @@ # get-stream [![Build Status](https://travis-ci.org/sindresorhus/get-stream.svg?branch=master)](https://travis-ci.org/sindresorhus/get-stream) | ||
``` | ||
$ npm install --save get-stream | ||
$ npm install get-stream | ||
``` | ||
@@ -19,6 +19,7 @@ | ||
const getStream = require('get-stream'); | ||
const stream = fs.createReadStream('unicorn.txt'); | ||
getStream(stream).then(str => { | ||
console.log(str); | ||
(async () => { | ||
const stream = fs.createReadStream('unicorn.txt'); | ||
console.log(await getStream(stream)); | ||
/* | ||
@@ -45,3 +46,3 @@ ,,))))))));, | ||
*/ | ||
}); | ||
})(); | ||
``` | ||
@@ -60,2 +61,4 @@ | ||
Type: `Object` | ||
##### encoding | ||
@@ -99,7 +102,10 @@ | ||
```js | ||
getStream(streamThatErrorsAtTheEnd('unicorn')) | ||
.catch(err => { | ||
console.log(err.bufferedData); | ||
(async () => { | ||
try { | ||
await getStream(streamThatErrorsAtTheEnd('unicorn')); | ||
} catch (error) { | ||
console.log(error.bufferedData); | ||
//=> 'unicorn' | ||
}); | ||
} | ||
})() | ||
``` | ||
@@ -106,0 +112,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
124
7677
1
73
+ Addedpump@^3.0.0
+ Addedend-of-stream@1.4.4(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedpump@3.0.2(transitive)
+ Addedwrappy@1.0.2(transitive)