Skip to content

Commit

Permalink
Use native stream.pipeline instead of pump package (#39)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
tiagonapoli and sindresorhus committed Aug 10, 2020
1 parent 010d8de commit 5cf31ba
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
21 changes: 12 additions & 9 deletions index.js
@@ -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');
Expand All @@ -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 = {
Expand All @@ -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.
Expand All @@ -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) {
Expand Down
5 changes: 1 addition & 4 deletions package.json
Expand Up @@ -11,7 +11,7 @@
"url": "https://sindresorhus.com"
},
"engines": {
"node": ">=8"
"node": ">=10"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -37,9 +37,6 @@
"array",
"object"
],
"dependencies": {
"pump": "^3.0.0"
},
"devDependencies": {
"@types/node": "^12.0.7",
"ava": "^2.0.0",
Expand Down

0 comments on commit 5cf31ba

Please sign in to comment.