| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent bb9951e commit e575971
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -106,6 +106,28 @@ For more details refer to the relevant documentation: | |||
| 106 | 106 | ||
| 107 | 107 | ## API | |
| 108 | 108 | ||
| 109 | + ### `ReadableStreamTee(stream[, cloneForBranch2])` | ||
| 110 | + | ||
| 111 | + <!-- YAML | ||
| 112 | + added: REPLACEME | ||
| 113 | + --> | ||
| 114 | + | ||
| 115 | + > Stability: 1 - Experimental | ||
| 116 | + | ||
| 117 | + * `stream` {ReadableStream} | ||
| 118 | + * `cloneForBranch2` {boolean} When `true`, chunks enqueued into the second | ||
| 119 | + branch are cloned from chunks enqueued into the first branch. **Default:** | ||
| 120 | + `false`. | ||
| 121 | + * Returns: {ReadableStream\[]} Two {ReadableStream} branches. | ||
| 122 | + | ||
| 123 | + Runs the WHATWG `ReadableStreamTee` abstract operation on `stream`. | ||
| 124 | + | ||
| 125 | + This differs from `readableStream.tee()` only when `cloneForBranch2` is | ||
| 126 | + `true`. The `tee()` method always passes `false`, while other web platform | ||
| 127 | + specifications, such as Fetch body cloning, pass `true` so that the second | ||
| 128 | + branch receives cloned chunks and consumption of one branch cannot mutate chunks | ||
| 129 | + seen by the other. | ||
| 130 | + | ||
| 109 | 131 | ### Class: `ReadableStream` | |
| 110 | 132 | ||
| 111 | 133 | <!-- YAML | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -18,8 +18,20 @@ const { | |||
| 18 | 18 | ReadableStreamBYOBRequest, | |
| 19 | 19 | ReadableByteStreamController, | |
| 20 | 20 | ReadableStreamDefaultController, | |
| 21 | + isReadableStream, | ||
| 22 | + readableStreamTee, | ||
| 21 | 23 | } = require('internal/webstreams/readablestream'); | |
| 22 | 24 | ||
| 25 | + const { | ||
| 26 | + codes: { | ||
| 27 | + ERR_INVALID_ARG_TYPE, | ||
| 28 | + }, | ||
| 29 | + } = require('internal/errors'); | ||
| 30 | + | ||
| 31 | + const { | ||
| 32 | + validateBoolean, | ||
| 33 | + } = require('internal/validators'); | ||
| 34 | + | ||
| 23 | 35 | const { | |
| 24 | 36 | ByteLengthQueuingStrategy, | |
| 25 | 37 | CountQueuingStrategy, | |
@@ -35,8 +47,17 @@ const { | |||
| 35 | 47 | DecompressionStream, | |
| 36 | 48 | } = require('internal/webstreams/compression'); | |
| 37 | 49 | ||
| 50 | + function ReadableStreamTee(stream, cloneForBranch2 = false) { | ||
| 51 | + if (!isReadableStream(stream)) { | ||
| 52 | + throw new ERR_INVALID_ARG_TYPE('stream', 'ReadableStream', stream); | ||
| 53 | + } | ||
| 54 | + validateBoolean(cloneForBranch2, 'cloneForBranch2'); | ||
| 55 | + return readableStreamTee(stream, cloneForBranch2); | ||
| 56 | + } | ||
| 57 | + | ||
| 38 | 58 | module.exports = { | |
| 39 | 59 | ReadableStream, | |
| 60 | + ReadableStreamTee, | ||
| 40 | 61 | ReadableStreamDefaultReader, | |
| 41 | 62 | ReadableStreamBYOBReader, | |
| 42 | 63 | ReadableStreamBYOBRequest, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,6 +15,7 @@ const { | |||
| 15 | 15 | ByteLengthQueuingStrategy, | |
| 16 | 16 | CountQueuingStrategy, | |
| 17 | 17 | ReadableStream, | |
| 18 | + ReadableStreamTee, | ||
| 18 | 19 | ReadableStreamDefaultReader, | |
| 19 | 20 | ReadableStreamDefaultController, | |
| 20 | 21 | ReadableByteStreamController, | |
@@ -1527,6 +1528,28 @@ class Source { | |||
| 1527 | 1528 | }).then(common.mustCall()); | |
| 1528 | 1529 | } | |
| 1529 | 1530 | ||
| 1531 | + { | ||
| 1532 | + // Test public ReadableStreamTee() cloneForBranch2 argument | ||
| 1533 | + assert.strictEqual(typeof ReadableStreamTee, 'function'); | ||
| 1534 | + const chunk = new Uint8Array([65]); | ||
| 1535 | + const readable = new ReadableStream({ | ||
| 1536 | + start(controller) { | ||
| 1537 | + controller.enqueue(chunk); | ||
| 1538 | + controller.close(); | ||
| 1539 | + }, | ||
| 1540 | + }); | ||
| 1541 | + const [r1, r2] = ReadableStreamTee(readable, true); | ||
| 1542 | + | ||
| 1543 | + (async () => { | ||
| 1544 | + const { value: value1 } = await r1.getReader().read(); | ||
| 1545 | + assert.strictEqual(value1[0], 65); | ||
| 1546 | + value1[0] = 66; | ||
| 1547 | + | ||
| 1548 | + const { value: value2 } = await r2.getReader().read(); | ||
| 1549 | + assert.strictEqual(value2[0], 65); | ||
| 1550 | + })().then(common.mustCall()); | ||
| 1551 | + } | ||
| 1552 | + | ||
| 1530 | 1553 | { | |
| 1531 | 1554 | // Test tee() cloneForBranch2 argument | |
| 1532 | 1555 | const readable = new ReadableStream({ | |
| Back | FazBrowse Home | New Git URL |
0 commit comments