Skip to content

Commit

Permalink
Revert "better fix for solidjs#1165"
Browse files Browse the repository at this point in the history
This reverts commit 3443ba0.
  • Loading branch information
edivados committed Jan 8, 2024
1 parent 527016e commit e1df5b5
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions packages/start/config/server-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ import { provideRequestEvent } from "solid-js/web/storage";
import invariant from "vinxi/lib/invariant";
import {
eventHandler,
getHeader,
getRequestURL,
readBody,
readRawBody,
setHeader
} from "vinxi/server";
import { getFetchEvent } from "../server/middleware";
Expand Down Expand Up @@ -56,14 +60,12 @@ function serializeToStream(id, value) {
});
}

async function handleServerFunction(h3Event) {
invariant(h3Event.method === "POST", `Invalid method ${h3Event.method}. Expected POST.`);
const event = getFetchEvent(h3Event);
const request = event.request;
async function handleServerFunction(event) {
invariant(event.method === "POST", `Invalid method ${event.method}. Expected POST.`);

const serverReference = request.headers.get("x-server-id");
const instance = request.headers.get("x-server-instance");
const url = new URL(request.url);
const serverReference = getHeader(event, "x-server-id");
const instance = getHeader(event, "x-server-instance");
const url = getRequestURL(event);
let filepath, name;
if (serverReference) {
invariant(typeof serverReference === "string", "Invalid server function");
Expand All @@ -84,14 +86,22 @@ async function handleServerFunction(h3Event) {
const args = url.searchParams.get("args");
if (args) JSON.parse(args).forEach(arg => parsed.push(arg));
}
const contentType = request.headers.get("content-type");
const contentType = getHeader(event, "content-type");
if (
contentType.startsWith("multipart/form-data") ||
contentType.startsWith("application/x-www-form-urlencoded")
) {
// Temporary workaround until https://github.com/unjs/nitro/issues/1721 is resolved
// parsed.push(await readFormData(event));

const request = new Request(getRequestURL(event), {
method: event.method,
headers: event.headers,
body: await readRawBody(event)
});
parsed.push(await request.formData());
} else {
parsed = fromJSON(await request.json(), {
parsed = fromJSON(await readBody(event), {
plugins: [
CustomEventPlugin,
DOMExceptionPlugin,
Expand All @@ -107,16 +117,17 @@ async function handleServerFunction(h3Event) {
});
}
try {
const result = await provideRequestEvent(event, () => {
const evt = getFetchEvent(event);
const result = await provideRequestEvent(evt, () => {
/* @ts-ignore */
sharedConfig.context = { event };
sharedConfig.context = { event: evt };
return action(...parsed);
});

// handle no JS success case
if (!instance) {
const isError = result instanceof Error;
const refererUrl = new URL(request.headers.get("referer"));
const refererUrl = new URL(getHeader(event, "referer"));
return new Response(null, {
status: 302,
headers: {
Expand All @@ -135,7 +146,7 @@ async function handleServerFunction(h3Event) {
});
}
if (typeof result === "string") return new Response(result);
setHeader(h3Event, "content-type", "text/javascript");
setHeader(event, "content-type", "text/javascript");
return serializeToStream(instance, result);
} catch (x) {
if (x instanceof Response && x.status === 302) {
Expand Down

0 comments on commit e1df5b5

Please sign in to comment.