From 5cf31ba3c8e888c317338894c6baa67d7178eab3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tiago=20N=C3=A1poli?= Date: Mon, 10 Aug 2020 08:06:09 -0300 Subject: [PATCH] Use native `stream.pipeline` instead of `pump` package (#39) Co-authored-by: Sindre Sorhus --- index.js | 21 ++++++++++++--------- package.json | 5 +---- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/index.js b/index.js index 71f3991..71a1ad9 100644 --- a/index.js +++ b/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'); @@ -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) { diff --git a/package.json b/package.json index e2f1687..a2c0f9c 100644 --- a/package.json +++ b/package.json @@ -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",