get-stream
Advanced tools
Comparing version 2.2.0 to 2.2.1
53
index.js
'use strict'; | ||
var PassThrough = require('stream').PassThrough; | ||
var Promise = require('pinkie-promise'); | ||
var objectAssign = require('object-assign'); | ||
var bufferStream = require('./buffer-stream'); | ||
@@ -12,48 +12,15 @@ function getStream(inputStream, opts) { | ||
opts = objectAssign({maxBuffer: Infinity}, opts); | ||
var maxBuffer = opts.maxBuffer; | ||
var stream; | ||
var array = opts.array; | ||
var encoding = opts.encoding; | ||
var maxBuffer = opts.maxBuffer; | ||
var buffer = encoding === 'buffer'; | ||
var objectMode = false; | ||
if (array) { | ||
objectMode = !(encoding || buffer); | ||
} else { | ||
encoding = encoding || 'utf8'; | ||
} | ||
if (buffer) { | ||
encoding = null; | ||
} | ||
var len = 0; | ||
var ret = []; | ||
var clean; | ||
var p = new Promise(function (resolve, reject) { | ||
stream = new PassThrough({objectMode: objectMode}); | ||
stream = bufferStream(opts); | ||
inputStream.pipe(stream); | ||
if (encoding) { | ||
stream.setEncoding(encoding); | ||
} | ||
var onData = function (chunk) { | ||
ret.push(chunk); | ||
if (objectMode) { | ||
len = ret.length; | ||
} else { | ||
len += chunk.length; | ||
} | ||
if (len > maxBuffer) { | ||
stream.on('data', function () { | ||
if (stream.getBufferedLength() > maxBuffer) { | ||
reject(new Error('maxBuffer exceeded')); | ||
} | ||
}; | ||
stream.on('data', onData); | ||
}); | ||
stream.on('error', reject); | ||
@@ -63,3 +30,3 @@ stream.on('end', resolve); | ||
clean = function () { | ||
stream.removeListener('data', onData); | ||
inputStream.unpipe(stream); | ||
}; | ||
@@ -71,7 +38,3 @@ }); | ||
return p.then(function () { | ||
if (array) { | ||
return ret; | ||
} | ||
return buffer ? Buffer.concat(ret, len) : ret.join(''); | ||
return stream.getBufferedValue(); | ||
}); | ||
@@ -78,0 +41,0 @@ } |
{ | ||
"name": "get-stream", | ||
"version": "2.2.0", | ||
"version": "2.2.1", | ||
"description": "Get a stream as a string, buffer, or array", | ||
@@ -19,3 +19,4 @@ "license": "MIT", | ||
"files": [ | ||
"index.js" | ||
"index.js", | ||
"buffer-stream.js" | ||
], | ||
@@ -25,2 +26,3 @@ "keywords": [ | ||
"stream", | ||
"promise", | ||
"concat", | ||
@@ -27,0 +29,0 @@ "string", |
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
7594
5
79