Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upstreams Fix server functions hanging on edge functions solidjs/solid-start#1255 #123

Merged
merged 4 commits into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/red-tips-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"vinxi": patch
---

upstreams Fix server functions hanging on edge functions solidjs/solid-start#1255
49 changes: 48 additions & 1 deletion packages/vinxi/runtime/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ import {
toNodeListener,
toPlainHandler,
toWebHandler,
toWebRequest as toWebRequestH3,
unsealSession, // updateSession,
use,
useBase, // useSession,
Expand Down Expand Up @@ -132,6 +131,46 @@ 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),
Expand All @@ -140,6 +179,14 @@ export function toWebRequest(/** @type {import('h3').H3Event} */ 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: "/",
Expand Down
Loading