Skip to content

Commit 70571f8

Browse files
committedMay 26, 2023
Require Node.js 16 and move to ESM
1 parent 8f004f9 commit 70571f8

File tree

8 files changed

+207
-359
lines changed

8 files changed

+207
-359
lines changed
 

‎.github/workflows/main.yml

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ jobs:
1010
fail-fast: false
1111
matrix:
1212
node-version:
13-
- 14
14-
- 12
15-
- 10
13+
- 20
14+
- 18
15+
- 16
1616
steps:
17-
- uses: actions/checkout@v2
18-
- uses: actions/setup-node@v1
17+
- uses: actions/checkout@v3
18+
- uses: actions/setup-node@v3
1919
with:
2020
node-version: ${{ matrix.node-version }}
2121
- run: npm install

‎buffer-stream.js

-52
This file was deleted.

‎index.d.ts

+65-93
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,77 @@
1-
/// <reference types="node"/>
2-
import {Stream} from 'stream';
1+
import {type Stream} from 'node:stream';
2+
import {type Buffer} from 'node:buffer';
33

4-
declare class MaxBufferErrorClass extends Error {
4+
export class MaxBufferError extends Error {
55
readonly name: 'MaxBufferError';
66
constructor();
77
}
88

9-
declare namespace getStream {
10-
interface Options {
11-
/**
12-
Maximum length of the returned string. If it exceeds this value before the stream ends, the promise will be rejected with a `MaxBufferError` error.
13-
14-
@default Infinity
15-
*/
16-
readonly maxBuffer?: number;
17-
}
18-
19-
interface OptionsWithEncoding<EncodingType = BufferEncoding> extends Options {
20-
/**
21-
[Encoding](https://nodejs.org/api/buffer.html#buffer_buffer) of the incoming stream.
22-
23-
@default 'utf8'
24-
*/
25-
readonly encoding?: EncodingType;
26-
}
27-
28-
type MaxBufferError = MaxBufferErrorClass;
29-
}
30-
31-
declare const getStream: {
32-
/**
33-
Get the `stream` as a string.
34-
35-
@returns A promise that resolves when the end event fires on the stream, indicating that there is no more data to be read. The stream is switched to flowing mode.
36-
37-
@example
38-
```
39-
import * as fs from 'fs';
40-
import getStream = require('get-stream');
41-
42-
(async () => {
43-
const stream = fs.createReadStream('unicorn.txt');
44-
45-
console.log(await getStream(stream));
46-
// ,,))))))));,
47-
// __)))))))))))))),
48-
// \|/ -\(((((''''((((((((.
49-
// -*-==//////(('' . `)))))),
50-
// /|\ ))| o ;-. '((((( ,(,
51-
// ( `| / ) ;))))' ,_))^;(~
52-
// | | | ,))((((_ _____------~~~-. %,;(;(>';'~
53-
// o_); ; )))(((` ~---~ `:: \ %%~~)(v;(`('~
54-
// ; ''''```` `: `:::|\,__,%% );`'; ~
55-
// | _ ) / `:|`----' `-'
56-
// ______/\/~ | / /
57-
// /~;;.____/;;' / ___--,-( `;;;/
58-
// / // _;______;'------~~~~~ /;;/\ /
59-
// // | | / ; \;;,\
60-
// (<_ | ; /',/-----' _>
61-
// \_| ||_ //~;~~~~~~~~~
62-
// `\_| (,~~
63-
// \~\
64-
// ~~
65-
})();
66-
```
67-
*/
68-
(stream: Stream, options?: getStream.OptionsWithEncoding): Promise<string>;
69-
9+
export type Options = {
7010
/**
71-
Get the `stream` as a buffer.
11+
Maximum length of the returned string. If it exceeds this value before the stream ends, the promise will be rejected with a `MaxBufferError` error.
7212
73-
It honors the `maxBuffer` option as above, but it refers to byte length rather than string length.
13+
@default Infinity
7414
*/
75-
buffer(
76-
stream: Stream,
77-
options?: getStream.Options
78-
): Promise<Buffer>;
15+
readonly maxBuffer?: number;
16+
};
7917

18+
export type OptionsWithEncoding<EncodingType = BufferEncoding> = {
8019
/**
81-
Get the `stream` as an array of values.
20+
The [encoding](https://nodejs.org/api/buffer.html#buffers-and-character-encodings) of the incoming stream.
8221
83-
It honors both the `maxBuffer` and `encoding` options. The behavior changes slightly based on the encoding chosen:
84-
85-
- When `encoding` is unset, it assumes an [object mode stream](https://nodesource.com/blog/understanding-object-streams/) and collects values emitted from `stream` unmodified. In this case `maxBuffer` refers to the number of items in the array (not the sum of their sizes).
86-
- When `encoding` is set to `buffer`, it collects an array of buffers. `maxBuffer` refers to the summed byte lengths of every buffer in the array.
87-
- When `encoding` is set to anything else, it collects an array of strings. `maxBuffer` refers to the summed character lengths of every string in the array.
22+
@default 'utf8'
8823
*/
89-
array<StreamObjectModeType>(
90-
stream: Stream,
91-
options?: getStream.Options
92-
): Promise<StreamObjectModeType[]>;
93-
array(
94-
stream: Stream,
95-
options: getStream.OptionsWithEncoding<'buffer'>
96-
): Promise<Buffer[]>;
97-
array(
98-
stream: Stream,
99-
options: getStream.OptionsWithEncoding<BufferEncoding>
100-
): Promise<string[]>;
101-
102-
MaxBufferError: typeof MaxBufferErrorClass;
103-
};
104-
105-
export = getStream;
24+
readonly encoding?: EncodingType;
25+
} & Options;
26+
27+
/**
28+
Get the given `stream` as a string.
29+
30+
@returns A promise that resolves when the end event fires on the stream, indicating that there is no more data to be read. The stream is switched to flowing mode.
31+
32+
@example
33+
```
34+
import fs from 'node:fs';
35+
import getStream from 'get-stream';
36+
37+
const stream = fs.createReadStream('unicorn.txt');
38+
39+
console.log(await getStream(stream));
40+
// ,,))))))));,
41+
// __)))))))))))))),
42+
// \|/ -\(((((''''((((((((.
43+
// -*-==//////(('' . `)))))),
44+
// /|\ ))| o ;-. '((((( ,(,
45+
// ( `| / ) ;))))' ,_))^;(~
46+
// | | | ,))((((_ _____------~~~-. %,;(;(>';'~
47+
// o_); ; )))(((` ~---~ `:: \ %%~~)(v;(`('~
48+
// ; ''''```` `: `:::|\,__,%% );`'; ~
49+
// | _ ) / `:|`----' `-'
50+
// ______/\/~ | / /
51+
// /~;;.____/;;' / ___--,-( `;;;/
52+
// / // _;______;'------~~~~~ /;;/\ /
53+
// // | | / ; \;;,\
54+
// (<_ | ; /',/-----' _>
55+
// \_| ||_ //~;~~~~~~~~~
56+
// `\_| (,~~
57+
// \~\
58+
// ~~
59+
```
60+
*/
61+
export default function getStream(stream: Stream, options?: OptionsWithEncoding): Promise<string>;
62+
63+
/**
64+
Get the given `stream` as a buffer.
65+
66+
It honors the `maxBuffer` option as above, but it refers to byte length rather than string length.
67+
68+
@example
69+
```
70+
import {getStreamAsBuffer} from 'get-stream';
71+
72+
const stream = fs.createReadStream('unicorn.png');
73+
74+
console.log(await getStreamAsBuffer(stream));
75+
```
76+
*/
77+
export function getStreamAsBuffer(stream: Stream, options?: Options): Promise<Buffer>;

‎index.js

+41-38
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,64 @@
1-
'use strict';
2-
const {constants: BufferConstants} = require('buffer');
3-
const stream = require('stream');
4-
const {promisify} = require('util');
5-
const bufferStream = require('./buffer-stream');
1+
import {Buffer, constants as BufferConstants} from 'node:buffer';
2+
import {PassThrough as PassThroughStream} from 'node:stream';
3+
import {pipeline as streamPipeline} from 'node:stream/promises';
64

7-
const streamPipelinePromisified = promisify(stream.pipeline);
5+
export class MaxBufferError extends Error {
6+
name = 'MaxBufferError';
87

9-
class MaxBufferError extends Error {
108
constructor() {
119
super('maxBuffer exceeded');
12-
this.name = 'MaxBufferError';
1310
}
1411
}
1512

16-
async function getStream(inputStream, options) {
13+
export default async function getStream(inputStream, options) {
1714
if (!inputStream) {
1815
throw new Error('Expected a stream');
1916
}
2017

2118
options = {
22-
maxBuffer: Infinity,
23-
...options
19+
maxBuffer: Number.POSITIVE_INFINITY,
20+
...options,
2421
};
2522

2623
const {maxBuffer} = options;
27-
const stream = bufferStream(options);
24+
let {encoding = 'utf8'} = options;
25+
const isBuffer = encoding === 'buffer';
2826

29-
await new Promise((resolve, reject) => {
30-
const rejectPromise = error => {
31-
// Don't retrieve an oversized buffer.
32-
if (error && stream.getBufferedLength() <= BufferConstants.MAX_LENGTH) {
33-
error.bufferedData = stream.getBufferedValue();
34-
}
27+
if (isBuffer) {
28+
encoding = null;
29+
}
3530

36-
reject(error);
37-
};
31+
const stream = new PassThroughStream();
3832

39-
(async () => {
40-
try {
41-
await streamPipelinePromisified(inputStream, stream);
42-
resolve();
43-
} catch (error) {
44-
rejectPromise(error);
45-
}
46-
})();
33+
if (encoding) {
34+
stream.setEncoding(encoding);
35+
}
36+
37+
await streamPipeline(inputStream, stream);
38+
39+
let length = 0;
40+
const chunks = [];
4741

48-
stream.on('data', () => {
49-
if (stream.getBufferedLength() > maxBuffer) {
50-
rejectPromise(new MaxBufferError());
42+
const getBufferedValue = () => isBuffer ? Buffer.concat(chunks, length) : chunks.join('');
43+
44+
for await (const chunk of stream) {
45+
chunks.push(chunk);
46+
length += chunk.length;
47+
48+
if (length > maxBuffer) {
49+
const error = new MaxBufferError();
50+
51+
if (length <= BufferConstants.MAX_LENGTH) {
52+
error.bufferedData = getBufferedValue();
5153
}
52-
});
53-
});
5454

55-
return stream.getBufferedValue();
55+
throw error;
56+
}
57+
}
58+
59+
return getBufferedValue();
5660
}
5761

58-
module.exports = getStream;
59-
module.exports.buffer = (stream, options) => getStream(stream, {...options, encoding: 'buffer'});
60-
module.exports.array = (stream, options) => getStream(stream, {...options, array: true});
61-
module.exports.MaxBufferError = MaxBufferError;
62+
export async function getStreamAsBuffer(stream, options) {
63+
return getStream(stream, {...options, encoding: 'buffer'});
64+
}

‎index.test-d.ts

+7-18
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,17 @@
1-
import * as fs from 'fs';
1+
import {type Buffer} from 'node:buffer';
2+
import {type Stream} from 'node:stream';
3+
import fs from 'node:fs';
24
import {expectType} from 'tsd';
3-
import getStream = require('.');
4-
import {MaxBufferError} from '.';
5+
import getStream, {getStreamAsBuffer, MaxBufferError} from './index.js';
56

6-
const stream = fs.createReadStream('foo');
7+
const stream = fs.createReadStream('foo') as Stream;
78

89
expectType<Promise<string>>(getStream(stream));
910
expectType<Promise<string>>(getStream(stream, {maxBuffer: 10}));
1011
expectType<Promise<string>>(getStream(stream, {encoding: 'utf8'}));
1112

12-
expectType<Promise<Buffer>>(getStream.buffer(stream));
13-
expectType<Promise<Buffer>>(getStream.buffer(stream, {maxBuffer: 10}));
14-
15-
expectType<Promise<unknown[]>>(getStream.array(stream));
16-
expectType<Promise<{}[]>>(getStream.array<{}>(stream));
17-
expectType<Promise<unknown[]>>(getStream.array(stream, {maxBuffer: 10}));
18-
expectType<Promise<Buffer[]>>(getStream.array(stream, {encoding: 'buffer'}));
19-
expectType<Promise<Buffer[]>>(
20-
getStream.array(stream, {maxBuffer: 10, encoding: 'buffer'})
21-
);
22-
expectType<Promise<string[]>>(getStream.array(stream, {encoding: 'utf8'}));
23-
expectType<Promise<string[]>>(
24-
getStream.array(stream, {maxBuffer: 10, encoding: 'utf8'})
25-
);
13+
expectType<Promise<Buffer>>(getStreamAsBuffer(stream));
14+
expectType<Promise<Buffer>>(getStreamAsBuffer(stream, {maxBuffer: 10}));
2615

2716
const maxBufferError = new MaxBufferError();
2817
expectType<MaxBufferError>(maxBufferError);

‎package.json

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "get-stream",
33
"version": "6.0.1",
4-
"description": "Get a stream as a string, buffer, or array",
4+
"description": "Get a stream as a string or buffer",
55
"license": "MIT",
66
"repository": "sindresorhus/get-stream",
77
"funding": "https://github.com/sponsors/sindresorhus",
@@ -10,16 +10,17 @@
1010
"email": "sindresorhus@gmail.com",
1111
"url": "https://sindresorhus.com"
1212
},
13+
"type": "module",
14+
"exports": "./index.js",
1315
"engines": {
14-
"node": ">=10"
16+
"node": ">=16"
1517
},
1618
"scripts": {
1719
"test": "xo && ava && tsd"
1820
},
1921
"files": [
2022
"index.js",
21-
"index.d.ts",
22-
"buffer-stream.js"
23+
"index.d.ts"
2324
],
2425
"keywords": [
2526
"get",
@@ -34,14 +35,14 @@
3435
"consume",
3536
"readable",
3637
"readablestream",
37-
"array",
38-
"object"
38+
"object",
39+
"concat"
3940
],
4041
"devDependencies": {
41-
"@types/node": "^14.0.27",
42-
"ava": "^2.4.0",
43-
"into-stream": "^5.0.0",
44-
"tsd": "^0.13.1",
45-
"xo": "^0.24.0"
42+
"@types/node": "^20.2.4",
43+
"ava": "^5.3.0",
44+
"into-stream": "^8.0.0",
45+
"tsd": "^0.28.1",
46+
"xo": "^0.54.2"
4647
}
4748
}

‎readme.md

+61-66
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,43 @@
11
# get-stream
22

3-
> Get a stream as a string, buffer, or array
3+
> Get a stream as a string or buffer
44
55
## Install
66

7-
```
8-
$ npm install get-stream
7+
```sh
8+
npm install get-stream
99
```
1010

1111
## Usage
1212

1313
```js
14-
const fs = require('fs');
15-
const getStream = require('get-stream');
16-
17-
(async () => {
18-
const stream = fs.createReadStream('unicorn.txt');
19-
20-
console.log(await getStream(stream));
21-
/*
22-
,,))))))));,
23-
__)))))))))))))),
24-
\|/ -\(((((''''((((((((.
25-
-*-==//////(('' . `)))))),
26-
/|\ ))| o ;-. '((((( ,(,
27-
( `| / ) ;))))' ,_))^;(~
28-
| | | ,))((((_ _____------~~~-. %,;(;(>';'~
29-
o_); ; )))(((` ~---~ `:: \ %%~~)(v;(`('~
30-
; ''''```` `: `:::|\,__,%% );`'; ~
31-
| _ ) / `:|`----' `-'
32-
______/\/~ | / /
33-
/~;;.____/;;' / ___--,-( `;;;/
34-
/ // _;______;'------~~~~~ /;;/\ /
35-
// | | / ; \;;,\
36-
(<_ | ; /',/-----' _>
37-
\_| ||_ //~;~~~~~~~~~
38-
`\_| (,~~
39-
\~\
40-
~~
41-
*/
42-
})();
14+
import fs from 'node:fs';
15+
import getStream from 'get-stream';
16+
17+
const stream = fs.createReadStream('unicorn.txt');
18+
19+
console.log(await getStream(stream));
20+
/*
21+
,,))))))));,
22+
__)))))))))))))),
23+
\|/ -\(((((''''((((((((.
24+
-*-==//////(('' . `)))))),
25+
/|\ ))| o ;-. '((((( ,(,
26+
( `| / ) ;))))' ,_))^;(~
27+
| | | ,))((((_ _____------~~~-. %,;(;(>';'~
28+
o_); ; )))(((` ~---~ `:: \ %%~~)(v;(`('~
29+
; ''''```` `: `:::|\,__,%% );`'; ~
30+
| _ ) / `:|`----' `-'
31+
______/\/~ | / /
32+
/~;;.____/;;' / ___--,-( `;;;/
33+
/ // _;______;'------~~~~~ /;;/\ /
34+
// | | / ; \;;,\
35+
(<_ | ; /',/-----' _>
36+
\_| ||_ //~;~~~~~~~~~
37+
`\_| (,~~
38+
\~\
39+
~~
40+
*/
4341
```
4442

4543
## API
@@ -48,7 +46,7 @@ The methods returns a promise that resolves when the `end` event fires on the st
4846

4947
### getStream(stream, options?)
5048

51-
Get the `stream` as a string.
49+
Get the given `stream` as a string.
5250

5351
#### options
5452

@@ -59,67 +57,64 @@ Type: `object`
5957
Type: `string`\
6058
Default: `'utf8'`
6159

62-
[Encoding](https://nodejs.org/api/buffer.html#buffer_buffer) of the incoming stream.
60+
The [encoding](https://nodejs.org/api/buffer.html#buffers-and-character-encodings) of the incoming stream.
6361

6462
##### maxBuffer
6563

6664
Type: `number`\
6765
Default: `Infinity`
6866

69-
Maximum length of the returned string. If it exceeds this value before the stream ends, the promise will be rejected with a `getStream.MaxBufferError` error.
67+
Maximum length of the returned string. If it exceeds this value before the stream ends, the promise will be rejected with a `MaxBufferError` error.
7068

71-
### getStream.buffer(stream, options?)
69+
### getStreamAsBuffer(stream, options?)
7270

73-
Get the `stream` as a buffer.
71+
Get the given `stream` as a buffer.
7472

7573
It honors the `maxBuffer` option as above, but it refers to byte length rather than string length.
7674

77-
### getStream.array(stream, options?)
75+
```js
76+
import {getStreamAsBuffer} from 'get-stream';
7877

79-
Get the `stream` as an array of values.
78+
const stream = fs.createReadStream('unicorn.png');
8079

81-
It honors both the `maxBuffer` and `encoding` options. The behavior changes slightly based on the encoding chosen:
80+
console.log(await getStreamAsBuffer(stream));
81+
```
8282

83-
- When `encoding` is unset, it assumes an [object mode stream](https://nodesource.com/blog/understanding-object-streams/) and collects values emitted from `stream` unmodified. In this case `maxBuffer` refers to the number of items in the array (not the sum of their sizes).
83+
## Errors
8484

85-
- When `encoding` is set to `buffer`, it collects an array of buffers. `maxBuffer` refers to the summed byte lengths of every buffer in the array.
85+
If the input stream emits an `error` event, the promise will be rejected with the error. The buffered data will be attached to the `bufferedData` property of the error.
8686

87-
- When `encoding` is set to anything else, it collects an array of strings. `maxBuffer` refers to the summed character lengths of every string in the array.
87+
```js
88+
import getStream from 'get-stream';
89+
90+
try {
91+
await getStream(streamThatErrorsAtTheEnd('unicorn'));
92+
} catch (error) {
93+
console.log(error.bufferedData);
94+
//=> 'unicorn'
95+
}
96+
```
8897

89-
## Errors
98+
## Tip
9099

91-
If the input stream emits an `error` event, the promise will be rejected with the error. The buffered data will be attached to the `bufferedData` property of the error.
100+
You may not need this package if all you need is a string:
92101

93102
```js
94-
(async () => {
95-
try {
96-
await getStream(streamThatErrorsAtTheEnd('unicorn'));
97-
} catch (error) {
98-
console.log(error.bufferedData);
99-
//=> 'unicorn'
100-
}
101-
})()
103+
import fs from 'node:fs';
104+
105+
const stream = fs.createReadStream('unicorn.txt', {encoding: 'utf8'});
106+
const array = await stream.toArray();
107+
108+
console.log(array.join(''));
102109
```
103110

104111
## FAQ
105112

106113
### How is this different from [`concat-stream`](https://github.com/maxogden/concat-stream)?
107114

108-
This module accepts a stream instead of being one and returns a promise instead of using a callback. The API is simpler and it only supports returning a string, buffer, or array. It doesn't have a fragile type inference. You explicitly choose what you want. And it doesn't depend on the huge `readable-stream` package.
115+
This module accepts a stream instead of being one and returns a promise instead of using a callback. The API is simpler and it only supports returning a string or buffer. It doesn't have a fragile type inference. You explicitly choose what you want. And it doesn't depend on the huge `readable-stream` package.
109116

110117
## Related
111118

112119
- [get-stdin](https://github.com/sindresorhus/get-stdin) - Get stdin as a string or buffer
113120
- [into-stream](https://github.com/sindresorhus/into-stream) - The opposite of this package
114-
115-
---
116-
117-
<div align="center">
118-
<b>
119-
<a href="https://tidelift.com/subscription/pkg/npm-get-stream?utm_source=npm-get-stream&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
120-
</b>
121-
<br>
122-
<sub>
123-
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
124-
</sub>
125-
</div>

‎test.js

+16-76
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,26 @@
1-
import fs from 'fs';
2-
import {constants as BufferConstants} from 'buffer';
3-
import {Readable as ReadableStream} from 'stream';
1+
import fs from 'node:fs';
2+
import {Buffer} from 'node:buffer';
43
import test from 'ava';
54
import intoStream from 'into-stream';
6-
import getStream from '.';
5+
import getStream, {getStreamAsBuffer, MaxBufferError} from './index.js';
76

87
function makeSetup(intoStream) {
98
const setup = (streamDef, options) => getStream(intoStream(streamDef), options);
10-
setup.array = (streamDef, options) => getStream.array(intoStream(streamDef), options);
11-
setup.buffer = (streamDef, options) => getStream.buffer(intoStream(streamDef), options);
9+
setup.buffer = (streamDef, options) => getStreamAsBuffer(intoStream(streamDef), options);
1210
return setup;
1311
}
1412

1513
const setup = makeSetup(intoStream);
1614
setup.object = makeSetup(intoStream.object);
1715

18-
test('get stream as a buffer', async t => {
19-
t.true((await getStream.buffer(fs.createReadStream('fixture'))).equals(Buffer.from('unicorn\n')));
20-
});
21-
22-
test('get stream as an array', async t => {
23-
const fixture = fs.createReadStream('index.js', 'utf8');
24-
fixture.setEncoding('utf8');
25-
t.is(typeof (await getStream.array(fixture))[0], 'string');
16+
test('get stream', async t => {
17+
const result = await getStream(fs.createReadStream('fixture'));
18+
t.is(result, 'unicorn\n');
2619
});
2720

28-
test('get object stream as an array', async t => {
29-
const result = await setup.object.array([{foo: true}, {bar: false}]);
30-
t.deepEqual(result, [{foo: true}, {bar: false}]);
31-
});
32-
33-
test('get non-object stream as an array of strings', async t => {
34-
const result = await setup.array(['foo', 'bar'], {encoding: 'utf8'});
35-
t.deepEqual(result, ['foo', 'bar']);
36-
});
37-
38-
test('get non-object stream as an array of Buffers', async t => {
39-
const result = await setup.array(['foo', 'bar'], {encoding: 'buffer'});
40-
t.deepEqual(result, [Buffer.from('foo'), Buffer.from('bar')]);
21+
test('get stream as a buffer', async t => {
22+
const result = await getStreamAsBuffer(fs.createReadStream('fixture'));
23+
t.true(result.equals(Buffer.from('unicorn\n')));
4124
});
4225

4326
test('getStream should not affect additional listeners attached to the stream', async t => {
@@ -48,57 +31,14 @@ test('getStream should not affect additional listeners attached to the stream',
4831
});
4932

5033
test('maxBuffer throws when size is exceeded', async t => {
51-
await t.throwsAsync(setup(['abcd'], {maxBuffer: 3}));
34+
await t.throwsAsync(setup(['abcd'], {maxBuffer: 3}), {instanceOf: MaxBufferError});
5235
await t.notThrowsAsync(setup(['abc'], {maxBuffer: 3}));
53-
54-
await t.throwsAsync(setup.buffer(['abcd'], {maxBuffer: 3}));
36+
await t.throwsAsync(setup.buffer(['abcd'], {maxBuffer: 3}), {instanceOf: MaxBufferError});
5537
await t.notThrowsAsync(setup.buffer(['abc'], {maxBuffer: 3}));
5638
});
5739

58-
test('maxBuffer applies to length of arrays when in objectMode', async t => {
59-
await t.throwsAsync(getStream.array(intoStream.object([{a: 1}, {b: 2}, {c: 3}, {d: 4}]), {maxBuffer: 3}), /maxBuffer exceeded/);
60-
await t.notThrowsAsync(getStream.array(intoStream.object([{a: 1}, {b: 2}, {c: 3}]), {maxBuffer: 3}));
61-
});
62-
63-
test('maxBuffer applies to length of data when not in objectMode', async t => {
64-
await t.throwsAsync(setup.array(['ab', 'cd', 'ef'], {encoding: 'utf8', maxBuffer: 5}), /maxBuffer exceeded/);
65-
await t.notThrowsAsync(setup.array(['ab', 'cd', 'ef'], {encoding: 'utf8', maxBuffer: 6}));
66-
await t.throwsAsync(setup.array(['ab', 'cd', 'ef'], {encoding: 'buffer', maxBuffer: 5}), /maxBuffer exceeded/);
67-
await t.notThrowsAsync(setup.array(['ab', 'cd', 'ef'], {encoding: 'buffer', maxBuffer: 6}));
68-
});
69-
70-
test('maxBuffer throws a MaxBufferError', async t => {
71-
await t.throwsAsync(setup(['abcd'], {maxBuffer: 3}), getStream.MaxBufferError);
72-
});
73-
74-
test('maxBuffer throws a MaxBufferError even if the stream is larger than Buffer MAX_LENGTH', async t => {
75-
// Create a stream 1 byte larger than the maximum size a buffer is allowed to be
76-
function * largeStream() {
77-
yield Buffer.allocUnsafe(BufferConstants.MAX_LENGTH);
78-
yield Buffer.allocUnsafe(1);
79-
}
80-
81-
await t.throwsAsync(setup(largeStream(), {maxBuffer: BufferConstants.MAX_LENGTH, encoding: 'buffer'}), /maxBuffer exceeded/);
82-
});
83-
84-
test('Promise rejects when input stream emits an error', async t => {
85-
const readable = new ReadableStream();
86-
const data = 'invisible pink unicorn';
87-
const error = new Error('Made up error');
88-
const reads = data.match(/.{1,5}/g);
89-
90-
readable._read = function () {
91-
if (reads.length === 0) {
92-
setImmediate(() => {
93-
this.emit('error', error);
94-
});
95-
return;
96-
}
97-
98-
this.push(reads.shift());
99-
};
100-
101-
const error2 = await t.throwsAsync(getStream(readable));
102-
t.is(error2, error);
103-
t.is(error2.bufferedData, data);
40+
test('native', async t => {
41+
const array = await fs.createReadStream('fixture', {encoding: 'utf8'}).toArray();
42+
const result = array.join('');
43+
t.is(result, 'unicorn\n');
10444
});

0 commit comments

Comments
 (0)
Please sign in to comment.