From 2254fe03f9adce3342b8e5453a8007d3bc1682d4 Mon Sep 17 00:00:00 2001 From: Mattias Buelens Date: Thu, 4 Apr 2019 22:50:11 +0200 Subject: [PATCH 1/3] ReadableStreamTee: do not read when already pulling Currently, when one branch calls pull(), it is possible that we call read() - even though a read() is still pending from a previous pull() call by the other branch. This can lead to reading too many chunks from the original streams, and unnecessarily filling up the queues of the branches past their high water mark. This PR fixes that issue by only starting a new read() if the previous read() was resolved. As a bonus, there will now be only one read() that resolves with { done: true } when the original stream closes, so we no longer need to keep track of whether we already closed the two branches. --- src/lib/readable-stream.ts | 19 +++++++++++++------ test/web-platform-tests | 2 +- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/lib/readable-stream.ts b/src/lib/readable-stream.ts index 5df62c9..879982b 100644 --- a/src/lib/readable-stream.ts +++ b/src/lib/readable-stream.ts @@ -656,7 +656,7 @@ function ReadableStreamTee(stream: ReadableStream, const reader = AcquireReadableStreamDefaultReader(stream); - let closed = false; + let reading = false; let canceled1 = false; let canceled2 = false; let reason1: any; @@ -670,10 +670,14 @@ function ReadableStreamTee(stream: ReadableStream, }); function pullAlgorithm(): Promise { - return ReadableStreamDefaultReaderRead(reader).then(result => { - if (closed === true) { - return; - } + if (reading === true) { + return Promise.resolve(); + } + + reading = true; + + const readPromise = ReadableStreamDefaultReaderRead(reader).then(result => { + reading = false; assert(typeIsObject(result)); const done = result.done; @@ -686,7 +690,6 @@ function ReadableStreamTee(stream: ReadableStream, if (canceled2 === false) { ReadableStreamDefaultControllerClose(branch2._readableStreamController as ReadableStreamDefaultController); } - closed = true; return; } @@ -714,6 +717,10 @@ function ReadableStreamTee(stream: ReadableStream, ); } }); + + readPromise.catch(rethrowAssertionErrorRejection); + + return Promise.resolve(); } function cancel1Algorithm(reason: any): Promise { diff --git a/test/web-platform-tests b/test/web-platform-tests index de6f8fc..05e7031 160000 --- a/test/web-platform-tests +++ b/test/web-platform-tests @@ -1 +1 @@ -Subproject commit de6f8fcf9b87e80811e9267a886cf891f6f864e0 +Subproject commit 05e7031bec9a03261cbf112b575a7998b7c76b3c From 1a8cbe6fd34fb344580a0582a9bf26be23fbca42 Mon Sep 17 00:00:00 2001 From: Mattias Buelens Date: Thu, 4 Apr 2019 22:56:25 +0200 Subject: [PATCH 2/3] Update commit references in readme [skip ci] --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index ca73646..28887f4 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ The `polyfill/es2018` and `ponyfill/es2018` variants work in any ES2018-compatib ### Compliance -The polyfill implements [version `2c8f35e` (21 Feb 2019)][spec-snapshot] of the streams specification. +The polyfill implements [version `6f94580` (1 Apr 2019)][spec-snapshot] of the streams specification. The polyfill is tested against the same [web platform tests][wpt] that are used by browsers to test their native implementations. The polyfill aims to pass all tests, although it allows some exceptions for practical reasons: @@ -99,12 +99,12 @@ Thanks to these people for their work on [the original polyfill][creatorrr-polyf [promise-support]: https://kangax.github.io/compat-table/es6/#test-Promise [promise-polyfill]: https://www.npmjs.com/package/promise-polyfill [rs-asynciterator]: https://streams.spec.whatwg.org/#rs-asynciterator -[spec-snapshot]: https://streams.spec.whatwg.org/commit-snapshots/2c8f35ed23451ffc9b32ec37b56def4a5349abb1/ -[wpt]: https://github.com/web-platform-tests/wpt/tree/de6f8fcf9b87e80811e9267a886cf891f6f864e0/streams -[wpt-detached-buffer]: https://github.com/web-platform-tests/wpt/blob/de6f8fcf9b87e80811e9267a886cf891f6f864e0/streams/readable-byte-streams/detached-buffers.any.js +[spec-snapshot]: https://streams.spec.whatwg.org/commit-snapshots/6f94580f6731d1e017c516af097d47c45aad1f56/ +[wpt]: https://github.com/web-platform-tests/wpt/tree/05e7031bec9a03261cbf112b575a7998b7c76b3c/streams +[wpt-detached-buffer]: https://github.com/web-platform-tests/wpt/blob/05e7031bec9a03261cbf112b575a7998b7c76b3c/streams/readable-byte-streams/detached-buffers.any.js [proposal-arraybuffer-transfer]: https://github.com/domenic/proposal-arraybuffer-transfer -[ref-impl-transferarraybuffer]: https://github.com/whatwg/streams/blob/2c8f35ed23451ffc9b32ec37b56def4a5349abb1/reference-implementation/lib/helpers.js#L119 +[ref-impl-transferarraybuffer]: https://github.com/whatwg/streams/blob/6f94580f6731d1e017c516af097d47c45aad1f56/reference-implementation/lib/helpers.js#L119 [issue-3]: https://github.com/MattiasBuelens/web-streams-polyfill/issues/3 -[wpt-async-iterator-prototype]: https://github.com/web-platform-tests/wpt/blob/de6f8fcf9b87e80811e9267a886cf891f6f864e0/streams/readable-streams/async-iterator.any.js#L17 +[wpt-async-iterator-prototype]: https://github.com/web-platform-tests/wpt/blob/05e7031bec9a03261cbf112b575a7998b7c76b3c/streams/readable-streams/async-iterator.any.js#L17 [stub-async-iterator-prototype]: https://github.com/MattiasBuelens/web-streams-polyfill/blob/v2.0.0/src/target/es5/stub/async-iterator-prototype.ts [creatorrr-polyfill]: https://github.com/creatorrr/web-streams-polyfill From c2238c9075c83974e626b5086f769bd470e448a2 Mon Sep 17 00:00:00 2001 From: Mattias Buelens Date: Thu, 4 Apr 2019 22:56:52 +0200 Subject: [PATCH 3/3] Update changelog [skip ci] --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a075af7..3334b98 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ ## Unreleased +* 👓 Align with [spec version `6f94580`](https://github.com/whatwg/streams/tree/6f94580f6731d1e017c516af097d47c45aad1f56/) * 🏠 Run web platform tests on ES5 variant ([#19](https://github.com/MattiasBuelens/web-streams-polyfill/pull/19)) ## v2.0.2 (2019-03-17)