get-stream
Advanced tools
Comparing version 2.1.0 to 2.2.0
114
index.js
'use strict'; | ||
var PassThrough = require('stream').PassThrough; | ||
var Promise = require('pinkie-promise'); | ||
var objectAssign = require('object-assign'); | ||
module.exports = function (stream, opts) { | ||
if (!stream) { | ||
function getStream(inputStream, opts) { | ||
if (!inputStream) { | ||
return Promise.reject(new Error('Expected a stream')); | ||
} | ||
opts = opts || {}; | ||
opts = objectAssign({maxBuffer: Infinity}, opts); | ||
var ret = ''; | ||
var stream; | ||
var array = opts.array; | ||
var encoding = opts.encoding; | ||
var maxBuffer = opts.maxBuffer; | ||
function onData(chunk) { | ||
ret += chunk; | ||
var buffer = encoding === 'buffer'; | ||
var objectMode = false; | ||
if (array) { | ||
objectMode = !(encoding || buffer); | ||
} else { | ||
encoding = encoding || 'utf8'; | ||
} | ||
var p = new Promise(function (resolve, reject) { | ||
stream.setEncoding(opts.encoding || 'utf8'); | ||
stream.on('data', onData); | ||
stream.on('error', reject); | ||
stream.on('end', resolve); | ||
}); | ||
if (buffer) { | ||
encoding = null; | ||
} | ||
var clean = function () { | ||
stream.removeListener('data', onData); | ||
}; | ||
var len = 0; | ||
var ret = []; | ||
var clean; | ||
p.then(clean, clean); | ||
var p = new Promise(function (resolve, reject) { | ||
stream = new PassThrough({objectMode: objectMode}); | ||
inputStream.pipe(stream); | ||
return p.then(function () { | ||
return ret; | ||
}); | ||
}; | ||
if (encoding) { | ||
stream.setEncoding(encoding); | ||
} | ||
module.exports.buffer = function (stream) { | ||
if (!stream) { | ||
return Promise.reject(new Error('Expected a stream')); | ||
} | ||
var onData = function (chunk) { | ||
ret.push(chunk); | ||
var ret = []; | ||
var len = 0; | ||
if (objectMode) { | ||
len = ret.length; | ||
} else { | ||
len += chunk.length; | ||
} | ||
function onData(chunk) { | ||
ret.push(chunk); | ||
len += chunk.length; | ||
} | ||
if (len > maxBuffer) { | ||
reject(new Error('maxBuffer exceeded')); | ||
} | ||
}; | ||
var p = new Promise(function (resolve, reject) { | ||
stream.on('data', onData); | ||
stream.on('error', reject); | ||
stream.on('end', resolve); | ||
clean = function () { | ||
stream.removeListener('data', onData); | ||
}; | ||
}); | ||
var clean = function () { | ||
stream.removeListener('data', onData); | ||
}; | ||
p.then(clean, clean); | ||
return p.then(function () { | ||
return Buffer.concat(ret, len); | ||
}); | ||
}; | ||
if (array) { | ||
return ret; | ||
} | ||
module.exports.array = function (stream) { | ||
if (!stream) { | ||
return Promise.reject(new Error('Expected a stream')); | ||
} | ||
var ret = []; | ||
function onData(obj) { | ||
ret.push(obj); | ||
} | ||
var p = new Promise(function (resolve, reject) { | ||
stream.on('data', onData); | ||
stream.on('error', reject); | ||
stream.on('end', resolve); | ||
return buffer ? Buffer.concat(ret, len) : ret.join(''); | ||
}); | ||
} | ||
var clean = function () { | ||
stream.removeListener('data', onData); | ||
}; | ||
module.exports = getStream; | ||
p.then(clean, clean); | ||
module.exports.buffer = function (stream, opts) { | ||
return getStream(stream, objectAssign({}, opts, {encoding: 'buffer'})); | ||
}; | ||
return p.then(function () { | ||
return ret; | ||
}); | ||
module.exports.array = function (stream, opts) { | ||
return getStream(stream, objectAssign({}, opts, {array: true})); | ||
}; |
{ | ||
"name": "get-stream", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "Get a stream as a string, buffer, or array", | ||
@@ -38,2 +38,3 @@ "license": "MIT", | ||
"dependencies": { | ||
"object-assign": "^4.0.1", | ||
"pinkie-promise": "^2.0.0" | ||
@@ -40,0 +41,0 @@ }, |
@@ -64,13 +64,28 @@ # get-stream [![Build Status](https://travis-ci.org/sindresorhus/get-stream.svg?branch=master)](https://travis-ci.org/sindresorhus/get-stream) | ||
### getStream.buffer(stream) | ||
##### maxBuffer | ||
Type: `number`<br> | ||
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. | ||
### getStream.array(stream) | ||
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. | ||
Especially useful for [object mode streams](https://nodesource.com/blog/understanding-object-streams/). | ||
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. | ||
## FAQ | ||
@@ -77,0 +92,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
7181
105
2
65
+ Addedobject-assign@^4.0.1
+ Addedobject-assign@4.1.1(transitive)