Skip to content

Commit

Permalink
doc: include code examples for webstreams consumers
Browse files Browse the repository at this point in the history
Add missing examples for webstreams consumers

Doc URL: https://nodejs.org/api/webstreams.html#streamconsumerstextstream

PR-URL: #44387
Reviewed-By: Erick Wendel <erick.workspace@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com>
  • Loading branch information
khaosdoctor authored and RafaelGSS committed Sep 26, 2022
1 parent 7d9de70 commit d537b61
Showing 1 changed file with 128 additions and 0 deletions.
128 changes: 128 additions & 0 deletions doc/api/webstreams.md
Original file line number Diff line number Diff line change
Expand Up @@ -1457,6 +1457,32 @@ added: v16.7.0
* Returns: {Promise} Fulfills with an `ArrayBuffer` containing the full
contents of the stream.
```mjs
import { buffer as arrayBuffer } from 'node:stream/consumers';
import { Readable } from 'node:stream';
import { TextEncoder } from 'node:util';

const encoder = new TextEncoder();
const dataArray = encoder.encode('hello world from consumers!');

const readable = Readable.from(dataArray);
const data = await arrayBuffer(readable);
console.log(`from readable: ${data.byteLength}`);
```
```cjs
const { arrayBuffer } = require('node:stream/consumers');
const { Readable } = require('stream');
const { TextEncoder } = require('util');

const encoder = new TextEncoder();
const dataArray = encoder.encode(['hello world from consumers!']);
const readable = Readable.from(dataArray);
arrayBuffer(readable).then((data) => {
console.log(`from readable: ${data.byteLength}`);
});
```
#### `streamConsumers.blob(stream)`
<!-- YAML
Expand All @@ -1467,6 +1493,27 @@ added: v16.7.0
* Returns: {Promise} Fulfills with a {Blob} containing the full contents
of the stream.
```mjs
import { blob } from 'node:stream/consumers';

const dataBlob = new Blob(['hello world from consumers!']);

const readable = dataBlob.stream();
const data = await blob(readable);
console.log(`from readable: ${data.size}`);
```
```cjs
const { blob } = require('node:stream/consumers');

const dataBlob = new Blob(['hello world from consumers!']);

const readable = dataBlob.stream();
blob(readable).then((data) => {
console.log(`from readable: ${data.size}`);
});
```
#### `streamConsumers.buffer(stream)`
<!-- YAML
Expand All @@ -1477,6 +1524,31 @@ added: v16.7.0
* Returns: {Promise} Fulfills with a {Buffer} containing the full
contents of the stream.
```mjs
import { buffer } from 'node:stream/consumers';
import { Readable } from 'node:stream';
import { Buffer } from 'node:buffer';

const dataBuffer = Buffer.from('hello world from consumers!');

const readable = Readable.from(dataBuffer);
const data = await buffer(readable);
console.log(`from readable: ${data.length}`);
```
```cjs
const { buffer } = require('node:stream/consumers');
const { Readable } = require('node:stream');
const { Buffer } = require('node:buffer');

const dataBuffer = Buffer.from('hello world from consumers!');

const readable = Readable.from(dataBuffer);
buffer(readable).then((data) => {
console.log(`from readable: ${data.length}`);
});
```
#### `streamConsumers.json(stream)`
<!-- YAML
Expand All @@ -1487,6 +1559,43 @@ added: v16.7.0
* Returns: {Promise} Fulfills with the contents of the stream parsed as a
UTF-8 encoded string that is then passed through `JSON.parse()`.
```mjs
import { json } from 'node:stream/consumers';
import { Readable } from 'node:stream';

const items = Array.from(
{
length: 100
},
() => ({
message: 'hello world from consumers!'
})
);

const readable = Readable.from(JSON.stringify(items));
const data = await json(readable);
console.log(`from readable: ${data.length}`);
```
```cjs
const { json } = require('node:stream/consumers');
const { Readable } = require('node:stream');

const items = Array.from(
{
length: 100
},
() => ({
message: 'hello world from consumers!'
})
);

const readable = Readable.from(JSON.stringify(items));
json(readable).then((data) => {
console.log(`from readable: ${data.length}`);
});
```
#### `streamConsumers.text(stream)`
<!-- YAML
Expand All @@ -1497,5 +1606,24 @@ added: v16.7.0
* Returns: {Promise} Fulfills with the contents of the stream parsed as a
UTF-8 encoded string.
```mjs
import { json, text, blob, buffer } from 'node:stream/consumers';
import { Readable } from 'node:stream';

const readable = Readable.from('Hello world from consumers!');
const data = await text(readable);
console.log(`from readable: ${data.length}`);
```
```cjs
const { text } = require('node:stream/consumers');
const { Readable } = require('node:stream');

const readable = Readable.from('Hello world from consumers!');
text(readable).then((data) => {
console.log(`from readable: ${data.length}`);
});
```
[Streams]: stream.md
[WHATWG Streams Standard]: https://streams.spec.whatwg.org/

0 comments on commit d537b61

Please sign in to comment.