Skip to content

Commit

Permalink
Meta tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Mar 5, 2019
1 parent 9ddd965 commit 32a3c39
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 32 deletions.
4 changes: 2 additions & 2 deletions buffer-stream.js
@@ -1,5 +1,5 @@
'use strict';
const {PassThrough} = require('stream');
const {PassThrough: PassThroughStream} = require('stream');

module.exports = options => {
options = Object.assign({}, options);
Expand All @@ -21,7 +21,7 @@ module.exports = options => {

let len = 0;
const ret = [];
const stream = new PassThrough({objectMode});
const stream = new PassThroughStream({objectMode});

if (encoding) {
stream.setEncoding(encoding);
Expand Down
1 change: 1 addition & 0 deletions index.js
Expand Up @@ -24,6 +24,7 @@ function getStream(inputStream, options) {
if (error) { // A null check
error.bufferedData = stream.getBufferedValue();
}

reject(error);
};

Expand Down
6 changes: 3 additions & 3 deletions package.json
Expand Up @@ -39,8 +39,8 @@
"pump": "^3.0.0"
},
"devDependencies": {
"ava": "*",
"into-stream": "^3.0.0",
"xo": "*"
"ava": "^1.2.1",
"into-stream": "^4.0.0",
"xo": "^0.24.0"
}
}
50 changes: 23 additions & 27 deletions test.js
@@ -1,27 +1,27 @@
import fs from 'fs';
import {Readable} from 'stream';
import {Readable as ReadableStream} from 'stream';
import test from 'ava';
import intoStream from 'into-stream';
import m from '.';
import getStream from '.';

function makeSetup(intoStream) {
const setup = (streamDef, opts) => m(intoStream(streamDef), opts);
setup.array = (streamDef, opts) => m.array(intoStream(streamDef), opts);
setup.buffer = (streamDef, opts) => m.buffer(intoStream(streamDef), opts);
const setup = (streamDef, options) => getStream(intoStream(streamDef), options);
setup.array = (streamDef, opts) => getStream.array(intoStream(streamDef), opts);
setup.buffer = (streamDef, opts) => getStream.buffer(intoStream(streamDef), opts);
return setup;
}

const setup = makeSetup(intoStream);
setup.obj = makeSetup(intoStream.obj);

test('get stream as a buffer', async t => {
t.true((await m.buffer(fs.createReadStream('fixture'))).equals(Buffer.from('unicorn\n')));
t.true((await getStream.buffer(fs.createReadStream('fixture'))).equals(Buffer.from('unicorn\n')));
});

test('get stream as an array', async t => {
const fixture = fs.createReadStream('index.js', 'utf8');
fixture.setEncoding('utf8');
t.is(typeof (await m.array(fixture))[0], 'string');
t.is(typeof (await getStream.array(fixture))[0], 'string');
});

test('get object stream as an array', async t => {
Expand All @@ -43,35 +43,35 @@ test('getStream should not affect additional listeners attached to the stream',
t.plan(3);
const fixture = intoStream(['foo', 'bar']);
fixture.on('data', chunk => t.true(Buffer.isBuffer(chunk)));
t.is(await m(fixture), 'foobar');
t.is(await getStream(fixture), 'foobar');
});

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

await t.throws(setup.buffer(['abcd'], {maxBuffer: 3}));
await t.notThrows(setup.buffer(['abc'], {maxBuffer: 3}));
await t.throwsAsync(setup.buffer(['abcd'], {maxBuffer: 3}));
await t.notThrowsAsync(setup.buffer(['abc'], {maxBuffer: 3}));
});

test('maxBuffer applies to length of arrays when in objectMode', async t => {
await t.throws(m.array(intoStream.obj([{a: 1}, {b: 2}, {c: 3}, {d: 4}]), {maxBuffer: 3}), /maxBuffer exceeded/);
await t.notThrows(m.array(intoStream.obj([{a: 1}, {b: 2}, {c: 3}]), {maxBuffer: 3}));
await t.throwsAsync(getStream.array(intoStream.obj([{a: 1}, {b: 2}, {c: 3}, {d: 4}]), {maxBuffer: 3}), /maxBuffer exceeded/);
await t.notThrowsAsync(getStream.array(intoStream.obj([{a: 1}, {b: 2}, {c: 3}]), {maxBuffer: 3}));
});

test('maxBuffer applies to length of data when not in objectMode', async t => {
await t.throws(setup.array(['ab', 'cd', 'ef'], {encoding: 'utf8', maxBuffer: 5}), /maxBuffer exceeded/);
await t.notThrows(setup.array(['ab', 'cd', 'ef'], {encoding: 'utf8', maxBuffer: 6}));
await t.throws(setup.array(['ab', 'cd', 'ef'], {encoding: 'buffer', maxBuffer: 5}), /maxBuffer exceeded/);
await t.notThrows(setup.array(['ab', 'cd', 'ef'], {encoding: 'buffer', maxBuffer: 6}));
await t.throwsAsync(setup.array(['ab', 'cd', 'ef'], {encoding: 'utf8', maxBuffer: 5}), /maxBuffer exceeded/);
await t.notThrowsAsync(setup.array(['ab', 'cd', 'ef'], {encoding: 'utf8', maxBuffer: 6}));
await t.throwsAsync(setup.array(['ab', 'cd', 'ef'], {encoding: 'buffer', maxBuffer: 5}), /maxBuffer exceeded/);
await t.notThrowsAsync(setup.array(['ab', 'cd', 'ef'], {encoding: 'buffer', maxBuffer: 6}));
});

test('maxBuffer throws a MaxBufferError', async t => {
await t.throws(setup(['abcd'], {maxBuffer: 3}), m.MaxBufferError);
await t.throwsAsync(setup(['abcd'], {maxBuffer: 3}), getStream.MaxBufferError);
});

test('Promise rejects when input stream emits an error', async t => {
const readable = new Readable();
const readable = new ReadableStream();
const data = 'invisible pink unicorn';
const error = new Error('Made up error');
const reads = data.match(/.{1,5}/g);
Expand All @@ -87,11 +87,7 @@ test('Promise rejects when input stream emits an error', async t => {
this.push(reads.shift());
};

try {
await m(readable);
t.fail('should throw');
} catch (error2) {
t.is(error2, error);
t.is(error2.bufferedData, data);
}
const error2 = await t.throwsAsync(getStream(readable));
t.is(error2, error);
t.is(error2.bufferedData, data);
});

0 comments on commit 32a3c39

Please sign in to comment.