Skip to content

Commit

Permalink
Allow maxBuffer option to be set to Buffer MAX_LENGTH (#40)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <[email protected]>
  • Loading branch information
gpoole and sindresorhus authored Aug 9, 2020
1 parent 7184983 commit 127d83c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
const {constants: BufferConstants} = require('buffer');
const pump = require('pump');
const bufferStream = require('./buffer-stream');

Expand All @@ -24,7 +25,8 @@ async function getStream(inputStream, options) {
let stream;
await new Promise((resolve, reject) => {
const rejectPromise = error => {
if (error) { // A null check
// Don't retrieve an oversized buffer.
if (error && stream.getBufferedLength() <= BufferConstants.MAX_LENGTH) {
error.bufferedData = stream.getBufferedValue();
}

Expand Down
11 changes: 11 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from 'fs';
import {constants as BufferConstants} from 'buffer';
import {Readable as ReadableStream} from 'stream';
import test from 'ava';
import intoStream from 'into-stream';
Expand Down Expand Up @@ -70,6 +71,16 @@ test('maxBuffer throws a MaxBufferError', async t => {
await t.throwsAsync(setup(['abcd'], {maxBuffer: 3}), getStream.MaxBufferError);
});

test('maxBuffer throws a MaxBufferError even if the stream is larger than Buffer MAX_LENGTH', async t => {
// Create a stream 1 byte larger than the maximum size a buffer is allowed to be
function * largeStream() {
yield Buffer.allocUnsafe(BufferConstants.MAX_LENGTH);
yield Buffer.allocUnsafe(1);
}

await t.throwsAsync(setup(largeStream(), {maxBuffer: BufferConstants.MAX_LENGTH, encoding: 'buffer'}), /maxBuffer exceeded/);
});

test('Promise rejects when input stream emits an error', async t => {
const readable = new ReadableStream();
const data = 'invisible pink unicorn';
Expand Down

0 comments on commit 127d83c

Please sign in to comment.