fetch: settle a buffered body's read a job after its chunks

This commit is contained in:
Chris Tate
2026-11-30 09:16:16 -06:00
committed by GitHub
parent fe710682da
commit ba61ed3eb9
+28 -4
View File
@@ -98,6 +98,8 @@ struct SfCollector {
* none). The type is read from it when the read settles, as the body
* readers do: a Content-Type set after the call applies. */
SfHeaders *blob_headers;
/* The body already held every byte it will have when the read began. */
bool buffered;
};
static SfHeaders *sf_headers_retain(SfHeaders *h);
@@ -144,6 +146,11 @@ struct SfStream {
/* A zero high-water mark (a TransformStream's readable side): pull only
* while a read is pending. */
bool hwm_zero;
/* Built from bytes the constructor already held (a string, Blob,
* ArrayBuffer or JSON body) rather than produced over time by a source:
* Node's buffered read path settles such a body a job after the chunks
* are handed over. */
bool buffered_body;
ScrDyn *error;
SfReader *reader; /* owned while locked */
SfCollector *collector; /* owned while a body reader is active */
@@ -1884,14 +1891,22 @@ static void sf_collector_settle_drop(void *ctx) {
static const ScrPromiseReaction sf_collector_settle = {
sf_collector_settle_react, sf_collector_settle_drop};
/* A body reader awaits the body's stream before it builds its value, so a
* read settles one microtask after the body's last chunk, and a blob()
* reads the Content-Type at that settle. The completion job runs there;
* the queue is FIFO, so reads keep the order their bodies completed in. */
/* A buffered body (one the constructor already held the bytes of) keeps
* Node's buffered ordering: its read settles a job after the chunks are
* handed over. A body produced over time settles where its last chunk is
* processed, as Node's reader does — an extra job there would put
* `new Response(stream).text()` behind the writer's own close
* continuation, which Node settles it ahead of. The queue is FIFO either
* way, so reads keep the order their bodies completed in. */
static void sf_collector_finish(SfStream *s) {
SfCollector *c = s->collector;
if (!c) return;
s->collector = NULL;
if (!c->buffered) {
sf_collector_settle_react(c, NULL);
sf_collector_free(c);
return;
}
ScrPromise *hop = scr_promise_new();
scr_promise_fulfill_void(hop);
scr_promise_react(hop, &sf_collector_settle, c);
@@ -2938,6 +2953,12 @@ static ScrPromise *sf_stream_collect(SfStream *s, int mode, SfHeaders *blob_head
c->held_stream = sf_stream_retain(s);
c->mode = mode;
c->blob_headers = blob_headers ? sf_headers_retain(blob_headers) : NULL;
/* Node's buffered path answers a read whose body already holds its
* bytes: one the constructor kept (a string, Blob, ArrayBuffer or JSON
* body), or one whose first chunk is already queued or which has already
* ended. A body still being produced settles where its last chunk is
* processed instead. */
c->buffered = s->buffered_body || s->queued > 0 || s->closed;
s->collector = c;
sf_stream_drain(s);
/* The collector reads to the end: a queue it emptied asks the source
@@ -7774,6 +7795,7 @@ static ScrDyn *sf_response_construct(ScrDyn *body, ScrDyn *init,
scr_str_release(encoding);
scr_dyn_release(serialized);
stream = sf_stream_new_native();
stream->buffered_body = true;
light_length_bytes = body_bytes->len;
sf_stream_enqueue_bytes(stream, body_bytes);
sf_stream_close(stream);
@@ -7781,6 +7803,7 @@ static ScrDyn *sf_response_construct(ScrDyn *body, ScrDyn *init,
body_bytes = NULL;
} else if (null_body) {
stream = sf_stream_new_native();
stream->buffered_body = true;
sf_stream_close(stream);
} else if (body->kind == SCR_DYN_HANDLE &&
body->v.handle.tag == SCR_DYNH_WEB_STREAM) {
@@ -7805,6 +7828,7 @@ static ScrDyn *sf_response_construct(ScrDyn *body, ScrDyn *init,
#endif
} else {
stream = sf_stream_new_native();
stream->buffered_body = true;
light_length_bytes = body_bytes->len;
sf_stream_enqueue_bytes(stream, body_bytes);
sf_stream_close(stream);