From dc6c53824e9bae8e7747ef02427ff6345d2cb661 Mon Sep 17 00:00:00 2001 From: Ciro Spaciari Date: Fri, 6 Sep 2024 16:56:42 -0700 Subject: [PATCH] await flush --- src/js/node/http.ts | 6 +++--- test/js/node/http/node-http.test.ts | 3 +++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/js/node/http.ts b/src/js/node/http.ts index 68aec0068e..730970a4dc 100644 --- a/src/js/node/http.ts +++ b/src/js/node/http.ts @@ -1260,17 +1260,17 @@ function flushFirstWrite(self) { new Response( new ReadableStream({ type: "direct", - pull: controller => { + pull: async controller => { self[controllerSymbol] = controller; if (firstWrite) { controller.write(firstWrite); - controller.flush(); // flush the first write + await controller.flush(); // flush the first write } firstWrite = undefined; if (!self[finishedSymbol]) { const { promise, resolve } = $newPromiseCapability(GlobalPromise); self[deferredSymbol] = resolve; - return promise; + return await promise; } }, }), diff --git a/test/js/node/http/node-http.test.ts b/test/js/node/http/node-http.test.ts index a1ab2114f5..fb9c88462b 100644 --- a/test/js/node/http/node-http.test.ts +++ b/test/js/node/http/node-http.test.ts @@ -2477,7 +2477,10 @@ it("response body streaming is immediate (#13696)", async () => { // chunks before real-time streaming started working. expect(new Date().getTime() - Number.parseInt(decoder.decode(value).trimEnd(), 10)).toBeLessThan(acceptableDelay); } + // Verify that the correct number of chunks were sent (in case server + // decides to send no chunks at all). expect(receivedChunks).toEqual(totalChunks); + // Also verify the total size in case some data was lost. expect(receivedSize).toEqual(totalSize); } finally { server.close();