Skip to content
Permalink
Browse files
Use native stream.pipeline instead of pump package (#39)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
tiagonapoli and sindresorhus committed Aug 10, 2020
1 parent 010d8de commit 5cf31ba3c8e888c317338894c6baa67d7178eab3
Showing with 13 additions and 13 deletions.
  1. +12 −9 index.js
  2. +1 −4 package.json
@@ -1,8 +1,11 @@
'use strict';
const {constants: BufferConstants} = require('buffer');
const pump = require('pump');
const stream = require('stream');
const {promisify} = require('util');
const bufferStream = require('./buffer-stream');

const streamPipelinePromisified = promisify(stream.pipeline);

class MaxBufferError extends Error {
constructor() {
super('maxBuffer exceeded');
@@ -12,7 +15,7 @@ class MaxBufferError extends Error {

async function getStream(inputStream, options) {
if (!inputStream) {
return Promise.reject(new Error('Expected a stream'));
throw new Error('Expected a stream');
}

options = {
@@ -22,7 +25,7 @@ async function getStream(inputStream, options) {

const {maxBuffer} = options;

let stream;
const stream = bufferStream(options);
await new Promise((resolve, reject) => {
const rejectPromise = error => {
// Don't retrieve an oversized buffer.
@@ -33,14 +36,14 @@ async function getStream(inputStream, options) {
reject(error);
};

stream = pump(inputStream, bufferStream(options), error => {
if (error) {
(async () => {
try {
await streamPipelinePromisified(inputStream, stream);
resolve();
} catch (error) {
rejectPromise(error);
return;
}

resolve();
});
})();

stream.on('data', () => {
if (stream.getBufferedLength() > maxBuffer) {
@@ -11,7 +11,7 @@
"url": "https://sindresorhus.com"
},
"engines": {
"node": ">=8"
"node": ">=10"
},
"scripts": {
"test": "xo && ava && tsd"
@@ -37,9 +37,6 @@
"array",
"object"
],
"dependencies": {
"pump": "^3.0.0"
},
"devDependencies": {
"@types/node": "^12.0.7",
"ava": "^2.0.0",

0 comments on commit 5cf31ba

Please sign in to comment.