Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use native stream.pipeline instead of pump package #39

Merged
merged 8 commits into from Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved
};

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