Skip to content
Open
Changes from all commits
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
18 changes: 18 additions & 0 deletions fetch.bs
Original file line number Diff line number Diff line change
Expand Up @@ -8032,6 +8032,7 @@ interface mixin Body {
[NewObject] Promise<FormData> formData();
[NewObject] Promise<any> json();
[NewObject] Promise<USVString> text();
[NewObject] ReadableStream textStream();
};</pre>

<p class=note>Formats you would not want a network layer to be dependent upon, such as
Expand Down Expand Up @@ -8073,6 +8074,9 @@ due course.

<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 a {{ReadableStream}} with <var>requestOrResponse</var>'s body as string chunks.
</dl>

<hr>
Expand Down Expand Up @@ -8246,6 +8250,20 @@ of running <a for=Body>consume body</a> with <a>this</a> and <a>parse JSON from
of running <a for=Body>consume body</a> with <a>this</a> and <a>UTF-8 decode</a>.
</div>

<div algorithm>
<p>The <dfn method for=Body><code>textStream()</code></dfn> method steps are:</p>
Copy link
Member

Choose a reason for hiding this comment

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

I think for the null case it'd be nicer to return an empty stream, similar to what text() does.

Another thing that's not entirely clear to me is whether piping ends up making the underlying stream unusable so that you can't invoke text() after textStream() for instance. (That's a property we want.)

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, I didn't realize what text() does if this.body is null. I agree an empty stream would be better, code that doesn't consider this case would likely just work.

I thought this should behave like when you try to use both blob() and bytes(), or indeed call one of those twice. That throws a TypeError in the first step of https://fetch.spec.whatwg.org/#concept-body-consume-body.

Since it's not possible to call text() twice, should it really be possible to call text() after textStream()?

Copy link
Member

Choose a reason for hiding this comment

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

It should not be possible.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good, that should be easy to achieve, even though the current spec text doesn't make clear what happens.


<ol>
<li><p>If <span>this</span>'s <a for=Body>body</a> is null, throw a {{TypeError}}.

<li><p>Let <var>stream</var> be <a>this</a>'s <a for=Body>body</a>'s <a for=body>stream</a>.

<li><p>Let <var>decoder</var> be a new {{TextDecoderStream}} object.
Copy link
Member Author

Choose a reason for hiding this comment

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

This and the next step would need improving if there's interest in this method.

The constructor steps for TextDecoderStream aren't implicitly run by just saying "new object", but what I'm after is an UTF-8 decoder with the default settings.

Copy link
Member

Choose a reason for hiding this comment

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

@MattiasBuelens or @ricea might have thoughts, but ideally we don't use public API for internal algorithms.

Choose a reason for hiding this comment

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

We should probably expose an algorithm to create a TextDecoderStream from other specifications, like we do for Streams.


<li><p>Return the result of piping <var>stream</var> through <var>decoder</var>.
Copy link
Member Author

Choose a reason for hiding this comment

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

Here "pipe through" would need to link to something. I'm not sure if there are other APIs that internally do something similar that I could copy?

Copy link

@MattiasBuelens MattiasBuelens Oct 2, 2025

Choose a reason for hiding this comment

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

You should be able to use the pipe through algorithm exposed for other specifications. [=piping through=] should work. 😉

</ol>
</div>


<h3 id=request-class>Request class</h3>

Expand Down