From b29002284e1a3231aade3b32027aad9e53f63f99 Mon Sep 17 00:00:00 2001 From: Nikhil Saraf Date: Sun, 14 Jan 2024 16:46:02 +0530 Subject: [PATCH 1/3] upstreams Fix server functions hanging on edge functions solidjs/solid-start#1255 --- packages/vinxi/runtime/server.js | 56 ++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 3 deletions(-) diff --git a/packages/vinxi/runtime/server.js b/packages/vinxi/runtime/server.js index e34c9556..026abeaa 100644 --- a/packages/vinxi/runtime/server.js +++ b/packages/vinxi/runtime/server.js @@ -95,7 +95,6 @@ import { toNodeListener, toPlainHandler, toWebHandler, - toWebRequest as toWebRequestH3, unsealSession, // updateSession, use, useBase, // useSession, @@ -132,14 +131,65 @@ export function defineMiddleware(options) { return options; } + + +/** + * The web request utils are copied from `h3` with a few bug fixes regaring multiple access to + * `readBody` and when the body is an ArrayBuffer, such as in Deno, Edge Functions, etc. + * + * We intend to remove this section once this is upstreamed in h3. + */ + +function toWebRequestH3(/** @type {import('h3').H3Event} */ event) { + /** + * @type {ReadableStream | undefined} + */ + let readableStream; + + const url = getRequestURL(event); + const base = { + // @ts-ignore Undici option + duplex: "half", + method: event.method, + headers: event.headers, + }; + + if (event.node.req.body instanceof ArrayBuffer) { + return new Request(url, { + ...base, + body: event.node.req.body, + }); + } + + return new Request(url, { + ...base, + get body() { + if (readableStream) { + return readableStream; + } + readableStream = getRequestWebStream(event); + return readableStream; + }, + }); +} + export function toWebRequest(/** @type {import('h3').H3Event} */ event) { - event.web ??= { - request: toWebRequestH3(event), + const request = toWebRequestH3(event); + event.web ??= { + request, url: getRequestURL(event), }; return event.web.request; } +/** + * The session utils are copied from `h3` with a few bug fixe regaring locking when sealing happens + * so things dont get stuck. + * + * We intend to remove this section once this is upstreamed in h3. + * + */ + const DEFAULT_NAME = "h3"; const DEFAULT_COOKIE = { path: "/", From 77afd12cf7e9bb67ef80983d1c55337b4a8e8603 Mon Sep 17 00:00:00 2001 From: Nikhil Saraf Date: Sun, 14 Jan 2024 16:49:10 +0530 Subject: [PATCH 2/3] fix --- packages/vinxi/runtime/server.js | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/packages/vinxi/runtime/server.js b/packages/vinxi/runtime/server.js index 026abeaa..6177b61c 100644 --- a/packages/vinxi/runtime/server.js +++ b/packages/vinxi/runtime/server.js @@ -131,12 +131,10 @@ export function defineMiddleware(options) { return options; } - - /** - * The web request utils are copied from `h3` with a few bug fixes regaring multiple access to + * The web request utils are copied from `h3` with a few bug fixes regaring multiple access to * `readBody` and when the body is an ArrayBuffer, such as in Deno, Edge Functions, etc. - * + * * We intend to remove this section once this is upstreamed in h3. */ @@ -174,9 +172,8 @@ function toWebRequestH3(/** @type {import('h3').H3Event} */ event) { } export function toWebRequest(/** @type {import('h3').H3Event} */ event) { - const request = toWebRequestH3(event); - event.web ??= { - request, + event.web ??= { + request: toWebRequestH3(event), url: getRequestURL(event), }; return event.web.request; @@ -185,9 +182,9 @@ export function toWebRequest(/** @type {import('h3').H3Event} */ event) { /** * The session utils are copied from `h3` with a few bug fixe regaring locking when sealing happens * so things dont get stuck. - * + * * We intend to remove this section once this is upstreamed in h3. - * + * */ const DEFAULT_NAME = "h3"; From c45633cec3827156a6da698847914caf7455ef34 Mon Sep 17 00:00:00 2001 From: Nikhil Saraf Date: Sun, 14 Jan 2024 16:49:27 +0530 Subject: [PATCH 3/3] Create red-tips-mate.md --- .changeset/red-tips-mate.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/red-tips-mate.md diff --git a/.changeset/red-tips-mate.md b/.changeset/red-tips-mate.md new file mode 100644 index 00000000..adb187b8 --- /dev/null +++ b/.changeset/red-tips-mate.md @@ -0,0 +1,5 @@ +--- +"vinxi": patch +--- + +upstreams Fix server functions hanging on edge functions solidjs/solid-start#1255