Skip to content

Commit 8ee5e91

Browse files
committed
add seroval json mode
1 parent 926336c commit 8ee5e91

2 files changed

Lines changed: 35 additions & 10 deletions

File tree

packages/start/src/server/server-functions-handler.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,18 @@ export async function handleServerFunction(h3Event: H3Event) {
119119
// forward headers
120120
if ((x as any).headers) mergeResponseHeaders(h3Event, (x as any).headers);
121121
// forward non-redirect statuses
122-
if ((x as any).status && (!instance || (x as any).status < 300 || (x as any).status >= 400))
122+
if (
123+
(x as any).status &&
124+
(!instance || (x as any).status < 300 || (x as any).status >= 400)
125+
)
123126
h3Event.res.status = (x as any).status;
124127
if ((x as any).customBody) {
125128
x = await (x as any).customBody();
126129
} else if ((x as any).body == null) x = null;
127130
h3Event.res.headers.set("X-Error", "true");
128131
} else if (instance) {
129-
const error = x instanceof Error ? x.message : typeof x === "string" ? x : "true";
132+
const error =
133+
x instanceof Error ? x.message : typeof x === "string" ? x : "true";
130134

131135
h3Event.res.headers.set("X-Error", error.replace(/[\r\n]+/g, ""));
132136
} else {
@@ -156,7 +160,12 @@ export async function handleServerFunction(h3Event: H3Event) {
156160
}
157161
}
158162

159-
function handleNoJS(result: any, request: Request, parsed: any[], thrown?: boolean) {
163+
function handleNoJS(
164+
result: any,
165+
request: Request,
166+
parsed: any[],
167+
thrown?: boolean,
168+
) {
160169
const url = new URL(request.url);
161170
const isError = result instanceof Error;
162171
let statusCode = 302;
@@ -166,7 +175,10 @@ function handleNoJS(result: any, request: Request, parsed: any[], thrown?: boole
166175
if (result.headers.has("Location")) {
167176
headers.set(
168177
`Location`,
169-
new URL(result.headers.get("Location")!, url.origin + import.meta.env.BASE_URL).toString(),
178+
new URL(
179+
result.headers.get("Location")!,
180+
url.origin + import.meta.env.BASE_URL,
181+
).toString(),
170182
);
171183
statusCode = getExpectedRedirectStatus(result);
172184
}
@@ -183,7 +195,10 @@ function handleNoJS(result: any, request: Request, parsed: any[], thrown?: boole
183195
result: isError ? result.message : result,
184196
thrown: thrown,
185197
error: isError,
186-
input: [...parsed.slice(0, -1), [...parsed[parsed.length - 1].entries()]],
198+
input: [
199+
...parsed.slice(0, -1),
200+
[...parsed[parsed.length - 1].entries()],
201+
],
187202
}),
188203
)}; Secure; HttpOnly;`,
189204
);
@@ -209,7 +224,7 @@ function createSingleFlightHeaders(sourceEvent: FetchEvent) {
209224
// useH3Internals = true;
210225
// sourceEvent.nativeEvent.node.req.headers.cookie = "";
211226
// }
212-
SetCookies.forEach(cookie => {
227+
SetCookies.forEach((cookie) => {
213228
if (!cookie) return;
214229
const { maxAge, expires, name, value } = parseSetCookie(cookie);
215230
if (maxAge != null && maxAge <= 0) {
@@ -230,7 +245,10 @@ function createSingleFlightHeaders(sourceEvent: FetchEvent) {
230245

231246
return headers;
232247
}
233-
async function handleSingleFlight(sourceEvent: FetchEvent, result: any): Promise<Response> {
248+
async function handleSingleFlight(
249+
sourceEvent: FetchEvent,
250+
result: any,
251+
): Promise<Response> {
234252
let revalidate: string[];
235253
let url = new URL(sourceEvent.request.headers.get("referer")!).toString();
236254
if (result instanceof Response) {

packages/start/src/server/server-runtime.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ import {
1212

1313
let INSTANCE = 0;
1414

15-
function createRequest(base: string, id: string, instance: string, options: RequestInit) {
15+
function createRequest(
16+
base: string,
17+
id: string,
18+
instance: string,
19+
options: RequestInit,
20+
) {
1621
return fetch(base, {
1722
method: "POST",
1823
...options,
@@ -100,7 +105,8 @@ export function createServerReference(id: string) {
100105
let baseURL = import.meta.env.BASE_URL ?? "/";
101106
if (!baseURL.endsWith("/")) baseURL += "/";
102107

103-
const fn = (...args: any[]) => fetchServerFunction(`${baseURL}_server`, id, {}, args);
108+
const fn = (...args: any[]) =>
109+
fetchServerFunction(`${baseURL}_server`, id, {}, args);
104110

105111
return new Proxy(fn, {
106112
get(target, prop, receiver) {
@@ -114,7 +120,8 @@ export function createServerReference(id: string) {
114120
const url = `${baseURL}_server?id=${encodeURIComponent(id)}`;
115121
return (options: RequestInit) => {
116122
const fn = async (...args: any[]) => {
117-
const encodeArgs = options.method && options.method.toUpperCase() === "GET";
123+
const encodeArgs =
124+
options.method && options.method.toUpperCase() === "GET";
118125
return fetchServerFunction(
119126
encodeArgs
120127
? url +

0 commit comments

Comments
 (0)