Skip to content

Add a textStream() method to the Body mixin#1862

Merged
annevk merged 6 commits into
mainfrom
foolip/textStream
Jun 5, 2026
Merged

Add a textStream() method to the Body mixin#1862
annevk merged 6 commits into
mainfrom
foolip/textStream

Conversation

@foolip

@foolip foolip commented Oct 2, 2025

Copy link
Copy Markdown
Member

textStream() is a convenience method that pipes a body through a utf-8 TextDecoderStream,
as it's a common use case for e.g. HTML streaming or any other text streaming.

Fixes #1861.

(See WHATWG Working Mode: Changes for more details.)


Preview | Diff

Comment thread fetch.bs Outdated
Comment thread fetch.bs Outdated
Comment thread fetch.bs Outdated
Comment thread fetch.bs
@noamr

noamr commented May 17, 2026

Copy link
Copy Markdown
Contributor

See corresponding FileAPI PR: w3c/FileAPI#221

@reillyeon

Copy link
Copy Markdown

If an HTTP response includes a Content-Type header with charset to something other than utf-8 should this method use that when decoding the byte stream to a text stream? It seems like in some other places we explicitly say that charset is ignored and the data is decoded as UTF-8 so we probably need something similar here to disambiguate.

@noamr

noamr commented May 19, 2026

Copy link
Copy Markdown
Contributor

If an HTTP response includes a Content-Type header with charset to something other than utf-8 should this method use that when decoding the byte stream to a text stream? It seems like in some other places we explicitly say that charset is ignored and the data is decoded as UTF-8 so we probably need something similar here to disambiguate.

Yes, this is discussed on the issue (#1861)

@reillyeon

Copy link
Copy Markdown

Ack, I just don't see any mention of it in the text added by the PR, while other methods like text() and formData() are explicit about it.

@annevk

annevk commented May 20, 2026

Copy link
Copy Markdown
Member

A note similar to what we have for formData() would probably be good.

annevk pushed a commit to whatwg/encoding that referenced this pull request May 21, 2026
One (the constructor) for use by JS and one (the set up steps) for use by other specifications.

See whatwg/fetch#1862 (review).
@noamr noamr force-pushed the foolip/textStream branch from 0150222 to b7d8c9d Compare May 21, 2026 09:52
@noamr

noamr commented May 21, 2026

Copy link
Copy Markdown
Contributor

A note similar to what we have for formData() would probably be good.

Done

@noamr noamr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the new "set up" algo and also resolved some other comments.

Comment thread fetch.bs
Comment thread fetch.bs Outdated
Comment thread fetch.bs Outdated
Comment thread fetch.bs Outdated
<dt><code><var>requestOrResponse</var> . <a method for=Body>text</a>()</code>
<dd><p>Returns a promise fulfilled with <var>requestOrResponse</var>'s body as string.

<dt><code><var>requestOrResponse</var> . <a method for=Body>textStream</a>()</code> <dd><p>Returns

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this another specfmt bug?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I double checked the formatting and then ran specfmt, should do it in reverse order next time. Anyway, fixed

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd recommend committing, then running specfmt and reviewing the diff.

Comment thread fetch.bs Outdated
Comment thread fetch.bs Outdated
Comment thread fetch.bs Outdated
Comment thread fetch.bs Outdated

@annevk annevk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The emptyStream thing is both correct and annoying.

Comment thread fetch.bs Outdated

<li>
<p><a data-lt="set up a text decoder stream">Set up</a> <var>decoder</var> with <a
for=/>UTF-8</a>.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no newline inside phrasing

Comment thread fetch.bs Outdated
chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this pull request May 28, 2026
This is a convenience method to pipe a byte stream through a
TextDecoderStream.

- Extracted the "Transformer" internals from TextDecoderStream into core/
- Call it from both the TextDecoderStream constructor and
  Body.textStream()

Still TODO: implement this for Blob.

See https://chromestatus.com/feature/5146752165478400
and whatwg/fetch#1862

Bug: 514448226
Change-Id: Ibf29c2780cfc5fe8b438de94fd6be342997c975c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7864305
Commit-Queue: Noam Rosenthal <nrosenthal@google.com>
Reviewed-by: Philip Jägenstedt <foolip@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1637750}
beckysiegel pushed a commit to chromium/chromium that referenced this pull request May 28, 2026
This is a convenience method to pipe a byte stream through a
TextDecoderStream.

- Extracted the "Transformer" internals from TextDecoderStream into core/
- Call it from both the TextDecoderStream constructor and
  Body.textStream()

Still TODO: implement this for Blob.

See https://chromestatus.com/feature/5146752165478400
and whatwg/fetch#1862

Bug: 514448226
Change-Id: Ibf29c2780cfc5fe8b438de94fd6be342997c975c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7864305
Commit-Queue: Noam Rosenthal <nrosenthal@google.com>
Reviewed-by: Philip Jägenstedt <foolip@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1637750}
chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this pull request May 29, 2026
This is a convenience method to pipe a byte stream through a
TextDecoderStream.

- Extracted the "Transformer" internals from TextDecoderStream into core/
- Call it from both the TextDecoderStream constructor and
  Body.textStream()

Still TODO: implement this for Blob.

See https://chromestatus.com/feature/5146752165478400
and whatwg/fetch#1862

Bug: 514448226
Change-Id: Ibf29c2780cfc5fe8b438de94fd6be342997c975c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7864305
Commit-Queue: Noam Rosenthal <nrosenthal@google.com>
Reviewed-by: Philip Jägenstedt <foolip@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1637750}
lando-worker Bot pushed a commit to mozilla-firefox/firefox that referenced this pull request Jun 2, 2026
…a flag, a=testonly

Automatic update from web-platform-tests
Implement BodyMixin.textStream() behind a flag

This is a convenience method to pipe a byte stream through a
TextDecoderStream.

- Extracted the "Transformer" internals from TextDecoderStream into core/
- Call it from both the TextDecoderStream constructor and
  Body.textStream()

Still TODO: implement this for Blob.

See https://chromestatus.com/feature/5146752165478400
and whatwg/fetch#1862

Bug: 514448226
Change-Id: Ibf29c2780cfc5fe8b438de94fd6be342997c975c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7864305
Commit-Queue: Noam Rosenthal <nrosenthal@google.com>
Reviewed-by: Philip Jägenstedt <foolip@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1637750}

--

wpt-commits: cc98dd90af1747562bcdb1af6c6fda329e8a6b01
wpt-pr: 60237
Co-authored-by: Anne van Kesteren <annevk@annevk.nl>
Comment thread fetch.bs Outdated
@noamr

noamr commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

I've ticked all the checkboxes, apart from implementer support. Do we need a whole "Standards position" for this @annevk @zcorpan ?

@annevk

annevk commented Jun 4, 2026

Copy link
Copy Markdown
Member

You can consider WebKit supportive. I left one nit on the test coverage. Thanks by the way for making those tentative PRs easily reviewable on WPT, that's really nice and maybe we should just enshrine that somewhere so that everyone starts doing that.

Once the tests are done feel free to land them and the specification changes, but please be careful to follow the commit message guidelines (ask me if you're unsure).

@noamr

noamr commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

You can consider WebKit supportive. I left one nit on the test coverage. Thanks by the way for making those tentative PRs easily reviewable on WPT, that's really nice and maybe we should just enshrine that somewhere so that everyone starts doing that.

I thought it was the standard way of doing this? It's at least on the WHATWG PR template

@annevk

annevk commented Jun 4, 2026

Copy link
Copy Markdown
Member

It's supposed to be, yes. But people don't always do it well and then you have to dig through Mozilla or Chromium changelogs to find what you want.

@noamr

noamr commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

Once the tests are done feel free to land them and the specification changes, but please be careful to follow the commit message guidelines (ask me if you're unsure).

I don't have permissions to land the spec changes.

noamr added a commit to web-platform-tests/wpt that referenced this pull request Jun 4, 2026
* Un-tentative textStream() tests

See w3c/FileAPI#221
and whatwg/fetch#1862

* Added some tests for null body
@annevk annevk merged commit 30140d0 into main Jun 5, 2026
2 checks passed
@annevk annevk deleted the foolip/textStream branch June 5, 2026 07:47
lando-worker Bot pushed a commit to mozilla-firefox/firefox that referenced this pull request Jun 8, 2026
…Blob` and `Body`), a=testonly

Automatic update from web-platform-tests
Un-tentative `textStream()` tests (for `Blob` and `Body`) (#60356)

* Un-tentative textStream() tests

See w3c/FileAPI#221
and whatwg/fetch#1862

* Added some tests for null body
--

wpt-commits: 15694f7be1515a179dff1cdc7ba42189d795d9d4
wpt-pr: 60356
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Convenience method to stream a response as text

5 participants