Skip to content

Commit bc47eb3

Browse files
authored
doc,test: add tests and docs for duplex.fromWeb and duplex.toWeb
PR-URL: #42738 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Mestery <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Juan José Arboleda <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
1 parent fc485a9 commit bc47eb3

File tree

2 files changed

+185
-1
lines changed

2 files changed

+185
-1
lines changed

doc/api/stream.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2887,6 +2887,67 @@ added: v17.0.0
28872887
* `signal` {AbortSignal}
28882888
* Returns: {stream.Duplex}
28892889

2890+
```mjs
2891+
import { Duplex } from 'node:stream';
2892+
import {
2893+
ReadableStream,
2894+
WritableStream
2895+
} from 'node:stream/web';
2896+
2897+
const readable = new ReadableStream({
2898+
start(controller) {
2899+
controller.enqueue('world');
2900+
},
2901+
});
2902+
2903+
const writable = new WritableStream({
2904+
write(chunk) {
2905+
console.log('writable', chunk);
2906+
}
2907+
});
2908+
2909+
const pair = {
2910+
readable,
2911+
writable
2912+
};
2913+
const duplex = Duplex.fromWeb(pair, { encoding: 'utf8', objectMode: true });
2914+
2915+
duplex.write('hello');
2916+
2917+
for await (const chunk of duplex) {
2918+
console.log('readable', chunk);
2919+
}
2920+
```
2921+
2922+
```cjs
2923+
const { Duplex } = require('node:stream');
2924+
const {
2925+
ReadableStream,
2926+
WritableStream
2927+
} = require('node:stream/web');
2928+
2929+
const readable = new ReadableStream({
2930+
start(controller) {
2931+
controller.enqueue('world');
2932+
},
2933+
});
2934+
2935+
const writable = new WritableStream({
2936+
write(chunk) {
2937+
console.log('writable', chunk);
2938+
}
2939+
});
2940+
2941+
const pair = {
2942+
readable,
2943+
writable
2944+
};
2945+
const duplex = Duplex.fromWeb(pair, { encoding: 'utf8', objectMode: true });
2946+
2947+
duplex.write('hello');
2948+
duplex.once('readable', () => console.log('readable', duplex.read()));
2949+
```
2950+
28902951
### `stream.Duplex.toWeb(streamDuplex)`
28912952

28922953
<!-- YAML
@@ -2900,6 +2961,51 @@ added: v17.0.0
29002961
* `readable` {ReadableStream}
29012962
* `writable` {WritableStream}
29022963

2964+
```mjs
2965+
import { Duplex } from 'node:stream';
2966+
2967+
const duplex = Duplex({
2968+
objectMode: true,
2969+
read() {
2970+
this.push('world');
2971+
this.push(null);
2972+
},
2973+
write(chunk, encoding, callback) {
2974+
console.log('writable', chunk);
2975+
callback();
2976+
}
2977+
});
2978+
2979+
const { readable, writable } = Duplex.toWeb(duplex);
2980+
writable.getWriter().write('hello');
2981+
2982+
const { value } = await readable.getReader().read();
2983+
console.log('readable', value);
2984+
```
2985+
2986+
```cjs
2987+
const { Duplex } = require('node:stream');
2988+
2989+
const duplex = Duplex({
2990+
objectMode: true,
2991+
read() {
2992+
this.push('world');
2993+
this.push(null);
2994+
},
2995+
write(chunk, encoding, callback) {
2996+
console.log('writable', chunk);
2997+
callback();
2998+
}
2999+
});
3000+
3001+
const { readable, writable } = Duplex.toWeb(duplex);
3002+
writable.getWriter().write('hello');
3003+
3004+
readable.getReader().read().then((result) => {
3005+
console.log('readable', result.value);
3006+
});
3007+
```
3008+
29033009
### `stream.addAbortSignal(signal, stream)`
29043010

29053011
<!-- YAML

test/parallel/test-stream-duplex.js

Lines changed: 79 additions & 1 deletion
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)