Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
doc,test: add tests and docs for duplex.fromWeb and duplex.toWeb
  • Loading branch information
ErickWendel committed Apr 14, 2022
commit 3a1c6aeeb4f5062a5ca3f50f64add68e4877f5a4
54 changes: 54 additions & 0 deletions doc/api/stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -2874,6 +2874,38 @@ added: v17.0.0
* `signal` {AbortSignal}
* Returns: {stream.Duplex}

```mjs
import { Duplex } from 'stream';
Comment thread
ErickWendel marked this conversation as resolved.
Outdated
import {
ReadableStream,
WritableStream
} from 'stream/web';
Comment thread
ErickWendel marked this conversation as resolved.
Outdated

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);
}
```

### `stream.Duplex.toWeb(streamDuplex)`

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

```mjs
import { Duplex } from 'stream';
Comment thread
ErickWendel marked this conversation as resolved.
Outdated

const duplex = Duplex({
objectMode: true,
read() {
this.push('world');
this.push(null);
},
write: (chunk, encoding, callback) => {
console.log('writable', chunk);
callback();
}
Comment thread
ErickWendel marked this conversation as resolved.
Outdated
});

const { readable, writable } = Duplex.toWeb(duplex);
writable.getWriter().write('hello');

const { value } = await readable.getReader().read();
console.log('readable', value);
```

### `stream.addAbortSignal(signal, stream)`

<!-- YAML
Expand Down
80 changes: 79 additions & 1 deletion test/parallel/test-stream-duplex.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });

Expand Down Expand Up @@ -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 stream = new ReadableStream({
Comment thread
ErickWendel marked this conversation as resolved.
Outdated
start(controller) {
controller.enqueue(dataToRead);
},
});

const writable = new WritableStream({
write: common.mustCall((chunk) => {
assert.strictEqual(chunk, dataToWrite);
})
});

const pair = { readable: stream, writable: writable };
Comment thread
ErickWendel marked this conversation as resolved.
Outdated
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 stream = new ReadableStream({
Comment thread
ErickWendel marked this conversation as resolved.
Outdated
start(controller) {
controller.enqueue(dataToRead);
},
});

const writable = new WritableStream({
write: common.mustCall((chunk) => {
assert.strictEqual(chunk, dataToWrite);
})
});

const pair = {
readable: stream,
writable: writable
};
Comment thread
ErickWendel marked this conversation as resolved.
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 webDuplex = Duplex.toWeb(duplex);
webDuplex.writable.getWriter().write(dataToWrite);

webDuplex.readable.getReader().read().then(common.mustCall((result) => {
assert.deepStrictEqual(Buffer.from(result.value), dataToRead);
}));
}