From 62e182c89c43c83efbdbf6cfa4d3b9a27bf2efd8 Mon Sep 17 00:00:00 2001 From: Lucas Santos Date: Thu, 25 Aug 2022 01:20:56 +0200 Subject: [PATCH] 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 Reviewed-By: Matteo Collina Reviewed-By: Daijiro Wachi --- doc/api/webstreams.md | 128 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) diff --git a/doc/api/webstreams.md b/doc/api/webstreams.md index 6a73a160ee9f67..f7294693abe64c 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)`