From fff4c18a365c8872486b070430b3118d8c01715c Mon Sep 17 00:00:00 2001 From: ehmicky Date: Mon, 7 Aug 2023 01:02:40 +0100 Subject: [PATCH 1/2] Document `node:stream/consumers` --- readme.md | 12 +++++++++--- test.js | 11 ++++++++--- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/readme.md b/readme.md index 8738bac..3f71a36 100644 --- a/readme.md +++ b/readme.md @@ -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 diff --git a/test.js b/test.js index 7713b42..2b8dce4 100644 --- a/test.js +++ b/test.js @@ -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'; @@ -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'))); +}); From 4e53ffdec29e332d5c356b92ddd099333ac868e2 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Mon, 7 Aug 2023 02:16:25 +0200 Subject: [PATCH 2/2] Update readme.md --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 3f71a36..0e5290c 100644 --- a/readme.md +++ b/readme.md @@ -101,7 +101,7 @@ 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'; +import {text, buffer} from 'node:stream/consumers'; const stream = fs.createReadStream('unicorn.txt', {encoding: 'utf8'});