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:
authorErick Wendel <erick.workspace@gmail.com>2022-04-28 10:53:52 +0300
committerGitHub <noreply@github.com>2022-04-28 10:53:52 +0300
commitbc47eb38491e65cf5730bba1fcb7de19c74dbdf9 (patch)
tree0e00777a9befefe93f54aef89130e02bd5fb91d6 /doc
parentfc485a9cbe552902e2c1880210f83170d9d951aa (diff)
doc,test: add tests and docs for duplex.fromWeb and duplex.toWeb
PR-URL: https://github.com/nodejs/node/pull/42738 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Mestery <mestery@protonmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Diffstat (limited to 'doc')
-rw-r--r--doc/api/stream.md106
1 files changed, 106 insertions, 0 deletions
diff --git a/doc/api/stream.md b/doc/api/stream.md
index a696ffd543a..c78f802d4a9 100644
--- a/doc/api/stream.md
+++ b/doc/api/stream.md
@@ -2887,6 +2887,67 @@ added: v17.0.0
* `signal` {AbortSignal}
* Returns: {stream.Duplex}
+```mjs
+import { Duplex } from 'node:stream';
+import {
+ ReadableStream,
+ WritableStream
+} from 'node:stream/web';
+
+const readable = new ReadableStream({
+ start(controller) {
+ controller.enqueue('world');
+ },
+});
+
+const writable = new WritableStream({
+ write(chunk) {
+ console.log('writable', chunk);
+ }
+});
+
+const pair = {
+ readable,
+ writable
+};
+const duplex = Duplex.fromWeb(pair, { encoding: 'utf8', objectMode: true });
+
+duplex.write('hello');
+
+for await (const chunk of duplex) {
+ console.log('readable', chunk);
+}
+```
+
+```cjs
+const { Duplex } = require('node:stream');
+const {
+ ReadableStream,
+ WritableStream
+} = require('node:stream/web');
+
+const readable = new ReadableStream({
+ start(controller) {
+ controller.enqueue('world');
+ },
+});
+
+const writable = new WritableStream({
+ write(chunk) {
+ console.log('writable', chunk);
+ }
+});
+
+const pair = {
+ readable,
+ writable
+};
+const duplex = Duplex.fromWeb(pair, { encoding: 'utf8', objectMode: true });
+
+duplex.write('hello');
+duplex.once('readable', () => console.log('readable', duplex.read()));
+```
+
### `stream.Duplex.toWeb(streamDuplex)`
<!-- YAML
@@ -2900,6 +2961,51 @@ added: v17.0.0
* `readable` {ReadableStream}
* `writable` {WritableStream}
+```mjs
+import { Duplex } from 'node:stream';
+
+const duplex = Duplex({
+ objectMode: true,
+ read() {
+ this.push('world');
+ this.push(null);
+ },
+ write(chunk, encoding, callback) {
+ console.log('writable', chunk);
+ callback();
+ }
+});
+
+const { readable, writable } = Duplex.toWeb(duplex);
+writable.getWriter().write('hello');
+
+const { value } = await readable.getReader().read();
+console.log('readable', value);
+```
+
+```cjs
+const { Duplex } = require('node:stream');
+
+const duplex = Duplex({
+ objectMode: true,
+ read() {
+ this.push('world');
+ this.push(null);
+ },
+ write(chunk, encoding, callback) {
+ console.log('writable', chunk);
+ callback();
+ }
+});
+
+const { readable, writable } = Duplex.toWeb(duplex);
+writable.getWriter().write('hello');
+
+readable.getReader().read().then((result) => {
+ console.log('readable', result.value);
+});
+```
+
### `stream.addAbortSignal(signal, stream)`
<!-- YAML