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

Document node:stream/consumers #58

Merged
merged 2 commits 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
12 changes: 9 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,21 @@ try {

## Tip

You may not need this package if all you need is a string:
You may not need this package if you do not use any [options](#options).

```js
import fs from 'node:fs';
import {text, buffer} from 'node:stream/consumers';

const stream = fs.createReadStream('unicorn.txt', {encoding: 'utf8'});
const array = await stream.toArray();

console.log(array.join(''));
console.log(await text(stream))
```

or:

```js
console.log(await buffer(stream))
```

## FAQ
Expand Down
11 changes: 8 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'node:fs';
import {Buffer} from 'node:buffer';
import {text, buffer} from 'node:stream/consumers';
import test from 'ava';
import intoStream from 'into-stream';
import getStream, {getStreamAsBuffer, MaxBufferError} from './index.js';
Expand Down Expand Up @@ -37,8 +38,12 @@ test('maxBuffer throws when size is exceeded', async t => {
await t.notThrowsAsync(setup.buffer(['abc'], {maxBuffer: 3}));
});

test('native', async t => {
const array = await fs.createReadStream('fixture', {encoding: 'utf8'}).toArray();
const result = array.join('');
test('native string', async t => {
const result = await text(fs.createReadStream('fixture', {encoding: 'utf8'}));
t.is(result, 'unicorn\n');
});

test('native buffer', async t => {
const result = await buffer(fs.createReadStream('fixture', {encoding: 'utf8'}));
t.true(result.equals(Buffer.from('unicorn\n')));
});