From 215d4344fca93831f91fe625017296fcd143ffb6 Mon Sep 17 00:00:00 2001 From: Marcos Candeia Date: Fri, 31 May 2024 20:11:49 -0300 Subject: [PATCH] Parallel req/resp body write Signed-off-by: Marcos Candeia --- deno.json | 2 +- handlers.client.ts | 6 ++++-- server.ts | 6 ++++-- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/deno.json b/deno.json index ff7d589..3734581 100644 --- a/deno.json +++ b/deno.json @@ -1,6 +1,6 @@ { "name": "@deco/warp", - "version": "0.2.1", + "version": "0.2.2", "exports": "./mod.ts", "tasks": { "check": "deno fmt && deno lint && deno check mod.ts" diff --git a/handlers.client.ts b/handlers.client.ts index 7f491db..15c7950 100644 --- a/handlers.client.ts +++ b/handlers.client.ts @@ -221,16 +221,18 @@ async function doFetch( }); const body = response?.body; const stream = body ? makeChanStream(body) : undefined; + const messages = []; for await (const chunk of stream?.recv(signal) ?? []) { - await clientCh.send({ + messages.push(clientCh.send({ type: "data", id: request.id, chunk, - }); + })); } if (signal.aborted) { return; } + await Promise.all(messages); await clientCh.send({ type: "data-end", id: request.id, diff --git a/server.ts b/server.ts index e6ee9d5..cec5a33 100644 --- a/server.ts +++ b/server.ts @@ -159,16 +159,18 @@ export const serveHandler = ( const dataChan = req.body ? makeChanStream(req.body) : undefined; (async () => { try { + const messages = []; for await (const chunk of dataChan?.recv(signal) ?? []) { - await ch.out.send({ + messages.push(ch.out.send({ type: "request-data", id: messageId, chunk, - }); + })); } if (signal.aborted) { return; } + await Promise.all(messages); await ch.out.send({ type: "request-end", id: messageId,