Skip to content

Commit 79724e7

Browse files
committed
add seroval json mode
1 parent d4be1b6 commit 79724e7

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
@@ -121,14 +121,18 @@ export async function handleServerFunction(h3Event: H3Event) {
121121
// forward headers
122122
if ((x as any).headers) mergeResponseHeaders(h3Event, (x as any).headers);
123123
// forward non-redirect statuses
124-
if ((x as any).status && (!instance || (x as any).status < 300 || (x as any).status >= 400))
124+
if (
125+
(x as any).status &&
126+
(!instance || (x as any).status < 300 || (x as any).status >= 400)
127+
)
125128
h3Event.res.status = (x as any).status;
126129
if ((x as any).customBody) {
127130
x = await (x as any).customBody();
128131
} else if ((x as any).body == null) x = null;
129132
h3Event.res.headers.set("X-Error", "true");
130133
} else if (instance) {
131-
const error = x instanceof Error ? x.message : typeof x === "string" ? x : "true";
134+
const error =
135+
x instanceof Error ? x.message : typeof x === "string" ? x : "true";
132136

133137
h3Event.res.headers.set("X-Error", error.replace(/[\r\n]+/g, ""));
134138
} else {
@@ -158,7 +162,12 @@ export async function handleServerFunction(h3Event: H3Event) {
158162
}
159163
}
160164

161-
function handleNoJS(result: any, request: Request, parsed: any[], thrown?: boolean) {
165+
function handleNoJS(
166+
result: any,
167+
request: Request,
168+
parsed: any[],
169+
thrown?: boolean,
170+
) {
162171
const url = new URL(request.url);
163172
const isError = result instanceof Error;
164173
let statusCode = 302;
@@ -168,7 +177,10 @@ function handleNoJS(result: any, request: Request, parsed: any[], thrown?: boole
168177
if (result.headers.has("Location")) {
169178
headers.set(
170179
`Location`,
171-
new URL(result.headers.get("Location")!, url.origin + import.meta.env.BASE_URL).toString(),
180+
new URL(
181+
result.headers.get("Location")!,
182+
url.origin + import.meta.env.BASE_URL,
183+
).toString(),
172184
);
173185
statusCode = getExpectedRedirectStatus(result);
174186
}
@@ -185,7 +197,10 @@ function handleNoJS(result: any, request: Request, parsed: any[], thrown?: boole
185197
result: isError ? result.message : result,
186198
thrown: thrown,
187199
error: isError,
188-
input: [...parsed.slice(0, -1), [...parsed[parsed.length - 1].entries()]],
200+
input: [
201+
...parsed.slice(0, -1),
202+
[...parsed[parsed.length - 1].entries()],
203+
],
189204
}),
190205
)}; Secure; HttpOnly;`,
191206
);
@@ -211,7 +226,7 @@ export function createSingleFlightHeaders(sourceEvent: FetchEvent) {
211226
// useH3Internals = true;
212227
// sourceEvent.nativeEvent.node.req.headers.cookie = "";
213228
// }
214-
SetCookies.forEach(cookie => {
229+
SetCookies.forEach((cookie) => {
215230
if (!cookie) return;
216231
const { maxAge, expires, name, value } = parseSetCookie(cookie);
217232
if (maxAge != null && maxAge <= 0) {
@@ -232,7 +247,10 @@ export function createSingleFlightHeaders(sourceEvent: FetchEvent) {
232247

233248
return headers;
234249
}
235-
async function handleSingleFlight(sourceEvent: FetchEvent, result: any): Promise<Response> {
250+
async function handleSingleFlight(
251+
sourceEvent: FetchEvent,
252+
result: any,
253+
): Promise<Response> {
236254
let revalidate: string[];
237255
let url = new URL(sourceEvent.request.headers.get("referer")!).toString();
238256
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 cloneServerReference(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 cloneServerReference(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)