Skip to content

Commit 9e6359a

Browse files
committed
stream/duplex: add tests and docs for duplex.fromWeb and duplex.toWeb
1 parent 6847fec commit 9e6359a

File tree

2 files changed

+133
-1
lines changed

2 files changed

+133
-1
lines changed

doc/api/stream.md

+54
Original file line numberDiff line numberDiff line change
@@ -2874,6 +2874,38 @@ added: v17.0.0
28742874
* `signal` {AbortSignal}
28752875
* Returns: {stream.Duplex}
28762876

2877+
```mjs
2878+
import { Duplex } from 'stream';
2879+
import {
2880+
ReadableStream,
2881+
WritableStream
2882+
} from 'stream/web';
2883+
2884+
const readable = new ReadableStream({
2885+
start(controller) {
2886+
controller.enqueue('world');
2887+
},
2888+
});
2889+
2890+
const writable = new WritableStream({
2891+
write(chunk) {
2892+
console.log('writable', chunk);
2893+
}
2894+
});
2895+
2896+
const pair = {
2897+
readable,
2898+
writable
2899+
};
2900+
const duplex = Duplex.fromWeb(pair, { encoding: 'utf8', objectMode: true });
2901+
2902+
duplex.write('hello');
2903+
2904+
for await (const chunk of duplex) {
2905+
console.log('readable', chunk);
2906+
}
2907+
```
2908+
28772909
### `stream.Duplex.toWeb(streamDuplex)`
28782910

28792911
<!-- YAML
@@ -2887,6 +2919,28 @@ added: v17.0.0
28872919
* `readable` {ReadableStream}
28882920
* `writable` {WritableStream}
28892921

2922+
```mjs
2923+
import { Duplex } from 'stream';
2924+
2925+
const duplex = Duplex({
2926+
objectMode: true,
2927+
read() {
2928+
this.push('world');
2929+
this.push(null);
2930+
},
2931+
write: (chunk, encoding, callback) => {
2932+
console.log('writable', chunk);
2933+
callback();
2934+
}
2935+
});
2936+
2937+
const { readable, writable } = Duplex.toWeb(duplex);
2938+
writable.getWriter().write('hello');
2939+
2940+
const { value } = await readable.getReader().read();
2941+
console.log('readable', value);
2942+
```
2943+
28902944
### `stream.addAbortSignal(signal, stream)`
28912945

28922946
<!-- 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 stream = 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: stream, writable: 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 stream = 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: stream,
103+
writable: 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 webDuplex = Duplex.toWeb(duplex);
128+
webDuplex.writable.getWriter().write(dataToWrite);
129+
130+
webDuplex.readable.getReader().read().then(common.mustCall((result) => {
131+
assert.deepStrictEqual(Buffer.from(result.value), dataToRead);
132+
}));
133+
}

0 commit comments

Comments
 (0)