mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
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>
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -20,9 +20,10 @@
|
||||
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
'use strict';
|
||||
|
||||
require('../common');
|
||||
const common = require('../common');
|
||||
const assert = require('assert');
|
||||
const Duplex = require('stream').Duplex;
|
||||
const { ReadableStream, WritableStream } = require('stream/web');
|
||||
|
||||
const stream = new Duplex({ objectMode: true });
|
||||
|
||||
@@ -53,3 +54,80 @@ process.on('exit', () => {
|
||||
assert.strictEqual(read.val, 1);
|
||||
assert.strictEqual(written.val, 2);
|
||||
});
|
||||
|
||||
// Duplex.fromWeb
|
||||
{
|
||||
const dataToRead = Buffer.from('hello');
|
||||
const dataToWrite = Buffer.from('world');
|
||||
|
||||
const readable = new ReadableStream({
|
||||
start(controller) {
|
||||
controller.enqueue(dataToRead);
|
||||
},
|
||||
});
|
||||
|
||||
const writable = new WritableStream({
|
||||
write: common.mustCall((chunk) => {
|
||||
assert.strictEqual(chunk, dataToWrite);
|
||||
})
|
||||
});
|
||||
|
||||
const pair = { readable, writable };
|
||||
const duplex = Duplex.fromWeb(pair);
|
||||
|
||||
duplex.write(dataToWrite);
|
||||
duplex.once('data', common.mustCall((chunk) => {
|
||||
assert.strictEqual(chunk, dataToRead);
|
||||
}));
|
||||
}
|
||||
|
||||
// Duplex.fromWeb - using utf8 and objectMode
|
||||
{
|
||||
const dataToRead = 'hello';
|
||||
const dataToWrite = 'world';
|
||||
|
||||
const readable = new ReadableStream({
|
||||
start(controller) {
|
||||
controller.enqueue(dataToRead);
|
||||
},
|
||||
});
|
||||
|
||||
const writable = new WritableStream({
|
||||
write: common.mustCall((chunk) => {
|
||||
assert.strictEqual(chunk, dataToWrite);
|
||||
})
|
||||
});
|
||||
|
||||
const pair = {
|
||||
readable,
|
||||
writable
|
||||
};
|
||||
const duplex = Duplex.fromWeb(pair, { encoding: 'utf8', objectMode: true });
|
||||
|
||||
duplex.write(dataToWrite);
|
||||
duplex.once('data', common.mustCall((chunk) => {
|
||||
assert.strictEqual(chunk, dataToRead);
|
||||
}));
|
||||
}
|
||||
// Duplex.toWeb
|
||||
{
|
||||
const dataToRead = Buffer.from('hello');
|
||||
const dataToWrite = Buffer.from('world');
|
||||
|
||||
const duplex = Duplex({
|
||||
read() {
|
||||
this.push(dataToRead);
|
||||
this.push(null);
|
||||
},
|
||||
write: common.mustCall((chunk) => {
|
||||
assert.strictEqual(chunk, dataToWrite);
|
||||
})
|
||||
});
|
||||
|
||||
const { writable, readable } = Duplex.toWeb(duplex);
|
||||
writable.getWriter().write(dataToWrite);
|
||||
|
||||
readable.getReader().read().then(common.mustCall((result) => {
|
||||
assert.deepStrictEqual(Buffer.from(result.value), dataToRead);
|
||||
}));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user