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

Set error.bufferedData when stream errors #63

Merged
merged 1 commit into from
Aug 7, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,24 @@ export default async function getStream(inputStream, options = {}) {

const getBufferedValue = () => isBuffer ? Buffer.concat(chunks, length) : chunks.join('');

for await (const chunk of newStream) {
chunks.push(chunk);
length += chunk.length;
try {
for await (const chunk of newStream) {
chunks.push(chunk);
length += chunk.length;

if (length > maxBuffer) {
const error = new MaxBufferError();

if (length <= BufferConstants.MAX_LENGTH) {
error.bufferedData = getBufferedValue();
if (length > maxBuffer) {
throw new MaxBufferError();
}
}

throw error;
return getBufferedValue();
} catch (error) {
if (length <= BufferConstants.MAX_LENGTH) {
error.bufferedData = getBufferedValue();
}
}

return getBufferedValue();
throw error;
}
}

export async function getStreamAsBuffer(stream, options) {
Expand Down
29 changes: 25 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ const fixtureString = await readFile('fixture', 'utf8');
const fixtureBuffer = Buffer.from(fixtureString);
const fixtureHex = fixtureBuffer.toString('hex');

const shortString = 'abc';
const longString = `${shortString}d`;
const maxBuffer = shortString.length;

function makeSetup(intoStream) {
const setup = (streamDef, options) => getStream(intoStream(streamDef), options);
setup.buffer = (streamDef, options) => getStreamAsBuffer(intoStream(streamDef), options);
Expand Down Expand Up @@ -39,10 +43,27 @@ test('getStream should not affect additional listeners attached to the stream',
});

test('maxBuffer throws when size is exceeded', async t => {
await t.throwsAsync(setup(['abcd'], {maxBuffer: 3}), {instanceOf: MaxBufferError});
await t.notThrowsAsync(setup(['abc'], {maxBuffer: 3}));
await t.throwsAsync(setup.buffer(['abcd'], {maxBuffer: 3}), {instanceOf: MaxBufferError});
await t.notThrowsAsync(setup.buffer(['abc'], {maxBuffer: 3}));
await t.throwsAsync(setup([longString], {maxBuffer}), {instanceOf: MaxBufferError});
await t.notThrowsAsync(setup([shortString], {maxBuffer}));
await t.throwsAsync(setup.buffer([longString], {maxBuffer}), {instanceOf: MaxBufferError});
await t.notThrowsAsync(setup.buffer([shortString], {maxBuffer}));
});

test('set error.bufferedData when `maxBuffer` is hit', async t => {
const error = await t.throwsAsync(setup([longString], {maxBuffer}), {instanceOf: MaxBufferError});
t.is(error.bufferedData, longString);
});

const errorStream = async function * () {
yield shortString;
await setTimeout(0);
throw new Error('test');
};

test('set error.bufferedData when stream errors', async t => {
const stream = compose(errorStream());
const error = await t.throwsAsync(getStream(stream));
t.is(error.bufferedData, shortString);
});

const infiniteIteration = async function * () {
Expand Down