get-stream
Advanced tools
Comparing version 2.3.1 to 3.0.0
@@ -1,13 +0,12 @@ | ||
var PassThrough = require('stream').PassThrough; | ||
var objectAssign = require('object-assign'); | ||
'use strict'; | ||
const PassThrough = require('stream').PassThrough; | ||
module.exports = function (opts) { | ||
opts = objectAssign({}, opts); | ||
module.exports = opts => { | ||
opts = Object.assign({}, opts); | ||
var array = opts.array; | ||
var encoding = opts.encoding; | ||
const array = opts.array; | ||
let encoding = opts.encoding; | ||
const buffer = encoding === 'buffer'; | ||
let objectMode = false; | ||
var buffer = encoding === 'buffer'; | ||
var objectMode = false; | ||
if (array) { | ||
@@ -23,7 +22,6 @@ objectMode = !(encoding || buffer); | ||
var len = 0; | ||
var ret = []; | ||
let len = 0; | ||
const ret = []; | ||
const stream = new PassThrough({objectMode}); | ||
var stream = new PassThrough({objectMode: objectMode}); | ||
if (encoding) { | ||
@@ -33,3 +31,3 @@ stream.setEncoding(encoding); | ||
stream.on('data', function (chunk) { | ||
stream.on('data', chunk => { | ||
ret.push(chunk); | ||
@@ -44,14 +42,13 @@ | ||
stream.getBufferedValue = function () { | ||
stream.getBufferedValue = () => { | ||
if (array) { | ||
return ret; | ||
} | ||
return buffer ? Buffer.concat(ret, len) : ret.join(''); | ||
}; | ||
stream.getBufferedLength = function () { | ||
return len; | ||
}; | ||
stream.getBufferedLength = () => len; | ||
return stream; | ||
}; |
50
index.js
'use strict'; | ||
var Promise = require('pinkie-promise'); | ||
var objectAssign = require('object-assign'); | ||
var bufferStream = require('./buffer-stream'); | ||
const bufferStream = require('./buffer-stream'); | ||
@@ -11,8 +9,17 @@ function getStream(inputStream, opts) { | ||
opts = objectAssign({maxBuffer: Infinity}, opts); | ||
var maxBuffer = opts.maxBuffer; | ||
var stream; | ||
var clean; | ||
opts = Object.assign({maxBuffer: Infinity}, opts); | ||
var p = new Promise(function (resolve, reject) { | ||
const maxBuffer = opts.maxBuffer; | ||
let stream; | ||
let clean; | ||
const p = new Promise((resolve, reject) => { | ||
const error = err => { | ||
if (err) { // null check | ||
err.bufferedData = stream.getBufferedValue(); | ||
} | ||
reject(err); | ||
}; | ||
stream = bufferStream(opts); | ||
@@ -22,3 +29,3 @@ inputStream.once('error', error); | ||
stream.on('data', function () { | ||
stream.on('data', () => { | ||
if (stream.getBufferedLength() > maxBuffer) { | ||
@@ -31,4 +38,4 @@ reject(new Error('maxBuffer exceeded')); | ||
clean = function () { | ||
// some streams doesn't implement the stream.Readable interface correctly | ||
clean = () => { | ||
// some streams doesn't implement the `stream.Readable` interface correctly | ||
if (inputStream.unpipe) { | ||
@@ -38,9 +45,2 @@ inputStream.unpipe(stream); | ||
}; | ||
function error(err) { | ||
if (err) { // null check | ||
err.bufferedData = stream.getBufferedValue(); | ||
} | ||
reject(err); | ||
} | ||
}); | ||
@@ -50,15 +50,7 @@ | ||
return p.then(function () { | ||
return stream.getBufferedValue(); | ||
}); | ||
return p.then(() => stream.getBufferedValue()); | ||
} | ||
module.exports = getStream; | ||
module.exports.buffer = function (stream, opts) { | ||
return getStream(stream, objectAssign({}, opts, {encoding: 'buffer'})); | ||
}; | ||
module.exports.array = function (stream, opts) { | ||
return getStream(stream, objectAssign({}, opts, {array: true})); | ||
}; | ||
module.exports.buffer = (stream, opts) => getStream(stream, Object.assign({}, opts, {encoding: 'buffer'})); | ||
module.exports.array = (stream, opts) => getStream(stream, Object.assign({}, opts, {array: true})); |
{ | ||
"name": "get-stream", | ||
"version": "2.3.1", | ||
"version": "3.0.0", | ||
"description": "Get a stream as a string, buffer, or array", | ||
@@ -13,3 +13,3 @@ "license": "MIT", | ||
"engines": { | ||
"node": ">=0.10.0" | ||
"node": ">=4" | ||
}, | ||
@@ -34,2 +34,3 @@ "scripts": { | ||
"data", | ||
"consume", | ||
"readable", | ||
@@ -41,12 +42,10 @@ "readablestream", | ||
], | ||
"dependencies": { | ||
"object-assign": "^4.0.1", | ||
"pinkie-promise": "^2.0.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "*", | ||
"buffer-equals": "^1.0.3", | ||
"into-stream": "^2.0.1", | ||
"into-stream": "^3.0.0", | ||
"xo": "*" | ||
}, | ||
"xo": { | ||
"esnext": true | ||
} | ||
} |
@@ -49,3 +49,3 @@ # get-stream [![Build Status](https://travis-ci.org/sindresorhus/get-stream.svg?branch=master)](https://travis-ci.org/sindresorhus/get-stream) | ||
The methods returns a promise that is resolved 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. | ||
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. | ||
@@ -97,4 +97,6 @@ ### getStream(stream, [options]) | ||
getStream(streamThatErrorsAtTheEnd('unicorn')) | ||
.catch(err => console.log(err.bufferedData)); | ||
// unicorn | ||
.catch(err => { | ||
console.log(err.bufferedData); | ||
//=> 'unicorn' | ||
}); | ||
``` | ||
@@ -101,0 +103,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
0
3
118
7880
79
- Removedobject-assign@^4.0.1
- Removedpinkie-promise@^2.0.0
- Removedobject-assign@4.1.1(transitive)
- Removedpinkie@2.0.4(transitive)
- Removedpinkie-promise@2.0.1(transitive)