Skip to content

Commit a6e1e7a

Browse files
ErickWendeltargos
authored andcommitted
doc,test: add tests and docs for duplex.fromWeb and duplex.toWeb
PR-URL: #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>
1 parent fe65996 commit a6e1e7a

File tree

2 files changed

+185
-1
lines changed

2 files changed

+185
-1
lines changed

doc/api/stream.md

+106
Original file line numberDiff line numberDiff line change
@@ -2869,6 +2869,67 @@ added: v17.0.0
28692869
* `signal` {AbortSignal}
28702870
* Returns: {stream.Duplex}
28712871

2872+
```mjs
2873+
import { Duplex } from 'node:stream';
2874+
import {
2875+
ReadableStream,
2876+
WritableStream
2877+
} from 'node:stream/web';
2878+
2879+
const readable = new ReadableStream({
2880+
start(controller) {
2881+
controller.enqueue('world');
2882+
},
2883+
});
2884+
2885+
const writable = new WritableStream({
2886+
write(chunk) {
2887+
console.log('writable', chunk);
2888+
}
2889+
});
2890+
2891+
const pair = {
2892+
readable,
2893+
writable
2894+
};
2895+
const duplex = Duplex.fromWeb(pair, { encoding: 'utf8', objectMode: true });
2896+
2897+
duplex.write('hello');
2898+
2899+
for await (const chunk of duplex) {
2900+
console.log('readable', chunk);
2901+
}
2902+
```
2903+
2904+
```cjs
2905+
const { Duplex } = require('node:stream');
2906+
const {
2907+
ReadableStream,
2908+
WritableStream
2909+
} = require('node:stream/web');
2910+
2911+
const readable = new ReadableStream({
2912+
start(controller) {
2913+
controller.enqueue('world');
2914+
},
2915+
});
2916+
2917+
const writable = new WritableStream({
2918+
write(chunk) {
2919+
console.log('writable', chunk);
2920+
}
2921+
});
2922+
2923+
const pair = {
2924+
readable,
2925+
writable
2926+
};
2927+
const duplex = Duplex.fromWeb(pair, { encoding: 'utf8', objectMode: true });
2928+
2929+
duplex.write('hello');
2930+
duplex.once('readable', () => console.log('readable', duplex.read()));
2931+
```
2932+
28722933
### `stream.Duplex.toWeb(streamDuplex)`
28732934

28742935
<!-- YAML
@@ -2882,6 +2943,51 @@ added: v17.0.0
28822943
* `readable` {ReadableStream}
28832944
* `writable` {WritableStream}
28842945

2946+
```mjs
2947+
import { Duplex } from 'node:stream';
2948+
2949+
const duplex = Duplex({
2950+
objectMode: true,
2951+
read() {
2952+
this.push('world');
2953+
this.push(null);
2954+
},
2955+
write(chunk, encoding, callback) {
2956+
console.log('writable', chunk);
2957+
callback();
2958+
}
2959+
});
2960+
2961+
const { readable, writable } = Duplex.toWeb(duplex);
2962+
writable.getWriter().write('hello');
2963+
2964+
const { value } = await readable.getReader().read();
2965+
console.log('readable', value);
2966+
```
2967+
2968+
```cjs
2969+
const { Duplex } = require('node:stream');
2970+
2971+
const duplex = Duplex({
2972+
objectMode: true,
2973+
read() {
2974+
this.push('world');
2975+
this.push(null);
2976+
},
2977+
write(chunk, encoding, callback) {
2978+
console.log('writable', chunk);
2979+
callback();
2980+
}
2981+
});
2982+
2983+
const { readable, writable } = Duplex.toWeb(duplex);
2984+
writable.getWriter().write('hello');
2985+
2986+
readable.getReader().read().then((result) => {
2987+
console.log('readable', result.value);
2988+
});
2989+
```
2990+
28852991
### `stream.addAbortSignal(signal, stream)`
28862992

28872993
<!-- YAML

test/parallel/test-stream-duplex.js

+79-1
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121
'use strict';
2222

23-
require('../common');
23+
const common = require('../common');
2424
const assert = require('assert');
2525
const Duplex = require('stream').Duplex;
26+
const { ReadableStream, WritableStream } = require('stream/web');
2627

2728
const stream = new Duplex({ objectMode: true });
2829

@@ -53,3 +54,80 @@ process.on('exit', () => {
5354
assert.strictEqual(read.val, 1);
5455
assert.strictEqual(written.val, 2);
5556
});
57+
58+
// Duplex.fromWeb
59+
{
60+
const dataToRead = Buffer.from('hello');
61+
const dataToWrite = Buffer.from('world');
62+
63+
const readable = new ReadableStream({
64+
start(controller) {
65+
controller.enqueue(dataToRead);
66+
},
67+
});
68+
69+
const writable = new WritableStream({
70+
write: common.mustCall((chunk) => {
71+
assert.strictEqual(chunk, dataToWrite);
72+
})
73+
});
74+
75+
const pair = { readable, writable };
76+
const duplex = Duplex.fromWeb(pair);
77+
78+
duplex.write(dataToWrite);
79+
duplex.once('data', common.mustCall((chunk) => {
80+
assert.strictEqual(chunk, dataToRead);
81+
}));
82+
}
83+
84+
// Duplex.fromWeb - using utf8 and objectMode
85+
{
86+
const dataToRead = 'hello';
87+
const dataToWrite = 'world';
88+
89+
const readable = new ReadableStream({
90+
start(controller) {
91+
controller.enqueue(dataToRead);
92+
},
93+
});
94+
95+
const writable = new WritableStream({
96+
write: common.mustCall((chunk) => {
97+
assert.strictEqual(chunk, dataToWrite);
98+
})
99+
});
100+
101+
const pair = {
102+
readable,
103+
writable
104+
};
105+
const duplex = Duplex.fromWeb(pair, { encoding: 'utf8', objectMode: true });
106+
107+
duplex.write(dataToWrite);
108+
duplex.once('data', common.mustCall((chunk) => {
109+
assert.strictEqual(chunk, dataToRead);
110+
}));
111+
}
112+
// Duplex.toWeb
113+
{
114+
const dataToRead = Buffer.from('hello');
115+
const dataToWrite = Buffer.from('world');
116+
117+
const duplex = Duplex({
118+
read() {
119+
this.push(dataToRead);
120+
this.push(null);
121+
},
122+
write: common.mustCall((chunk) => {
123+
assert.strictEqual(chunk, dataToWrite);
124+
})
125+
});
126+
127+
const { writable, readable } = Duplex.toWeb(duplex);
128+
writable.getWriter().write(dataToWrite);
129+
130+
readable.getReader().read().then(common.mustCall((result) => {
131+
assert.deepStrictEqual(Buffer.from(result.value), dataToRead);
132+
}));
133+
}

0 commit comments

Comments
 (0)