get-stream
Advanced tools
Comparing version 4.1.0 to 5.0.0
'use strict'; | ||
const {PassThrough} = require('stream'); | ||
const {PassThrough: PassThroughStream} = require('stream'); | ||
module.exports = options => { | ||
options = Object.assign({}, options); | ||
options = {...options}; | ||
const {array} = options; | ||
let {encoding} = options; | ||
const buffer = encoding === 'buffer'; | ||
const isBuffer = encoding === 'buffer'; | ||
let objectMode = false; | ||
if (array) { | ||
objectMode = !(encoding || buffer); | ||
objectMode = !(encoding || isBuffer); | ||
} else { | ||
@@ -18,9 +18,7 @@ encoding = encoding || 'utf8'; | ||
if (buffer) { | ||
if (isBuffer) { | ||
encoding = null; | ||
} | ||
let len = 0; | ||
const ret = []; | ||
const stream = new PassThrough({objectMode}); | ||
const stream = new PassThroughStream({objectMode}); | ||
@@ -31,9 +29,12 @@ if (encoding) { | ||
let length = 0; | ||
const chunks = []; | ||
stream.on('data', chunk => { | ||
ret.push(chunk); | ||
chunks.push(chunk); | ||
if (objectMode) { | ||
len = ret.length; | ||
length = chunks.length; | ||
} else { | ||
len += chunk.length; | ||
length += chunk.length; | ||
} | ||
@@ -44,11 +45,11 @@ }); | ||
if (array) { | ||
return ret; | ||
return chunks; | ||
} | ||
return buffer ? Buffer.concat(ret, len) : ret.join(''); | ||
return isBuffer ? Buffer.concat(chunks, length) : chunks.join(''); | ||
}; | ||
stream.getBufferedLength = () => len; | ||
stream.getBufferedLength = () => length; | ||
return stream; | ||
}; |
19
index.js
@@ -12,3 +12,3 @@ 'use strict'; | ||
function getStream(inputStream, options) { | ||
async function getStream(inputStream, options) { | ||
if (!inputStream) { | ||
@@ -18,3 +18,6 @@ return Promise.reject(new Error('Expected a stream')); | ||
options = Object.assign({maxBuffer: Infinity}, options); | ||
options = { | ||
maxBuffer: Infinity, | ||
...options | ||
}; | ||
@@ -24,3 +27,3 @@ const {maxBuffer} = options; | ||
let stream; | ||
return new Promise((resolve, reject) => { | ||
await new Promise((resolve, reject) => { | ||
const rejectPromise = error => { | ||
@@ -30,2 +33,3 @@ if (error) { // A null check | ||
} | ||
reject(error); | ||
@@ -48,8 +52,11 @@ }; | ||
}); | ||
}).then(() => stream.getBufferedValue()); | ||
}); | ||
return 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.default = getStream; | ||
module.exports.buffer = (stream, options) => getStream(stream, {...options, encoding: 'buffer'}); | ||
module.exports.array = (stream, options) => getStream(stream, {...options, array: true}); | ||
module.exports.MaxBufferError = MaxBufferError; |
{ | ||
"name": "get-stream", | ||
"version": "4.1.0", | ||
"version": "5.0.0", | ||
"description": "Get a stream as a string, buffer, or array", | ||
@@ -13,9 +13,10 @@ "license": "MIT", | ||
"engines": { | ||
"node": ">=6" | ||
"node": ">=8" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
"test": "xo && ava && tsd-check" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"index.d.ts", | ||
"buffer-stream.js" | ||
@@ -43,6 +44,8 @@ ], | ||
"devDependencies": { | ||
"ava": "*", | ||
"into-stream": "^3.0.0", | ||
"xo": "*" | ||
"@types/node": "^11.10.5", | ||
"ava": "^1.3.1", | ||
"into-stream": "^4.0.0", | ||
"tsd-check": "^0.3.0", | ||
"xo": "^0.24.0" | ||
} | ||
} |
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
10319
6
144
5