Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/doc
diff options
context:
space:
mode:
authorLucas Santos <lhs.santoss@gmail.com>2022-08-25 02:20:56 +0300
committerRafaelGSS <rafael.nunu@hotmail.com>2022-09-27 01:07:46 +0300
commit10a0d75c2644ac2212d4c99f07e82a7d36c30453 (patch)
treeea98fce82fe1eed9edb79605274d0e1283d87b78 /doc
parentce1704c2c740a40e578d92cd87b5e3986c6e52de (diff)
doc: include code examples for webstreams consumers
Add missing examples for webstreams consumers Doc URL: https://nodejs.org/api/webstreams.html#streamconsumerstextstream PR-URL: https://github.com/nodejs/node/pull/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>
Diffstat (limited to 'doc')
-rw-r--r--doc/api/webstreams.md128
1 files changed, 128 insertions, 0 deletions
diff --git a/doc/api/webstreams.md b/doc/api/webstreams.md
index 6a73a160ee9..f7294693abe 100644
--- a/doc/api/webstreams.md
+++ b/doc/api/webstreams.md
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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/