-
Notifications
You must be signed in to change notification settings - Fork 417
Expand file tree
/
Copy pathserver-runtime.ts
More file actions
156 lines (148 loc) · 4.28 KB
/
server-runtime.ts
File metadata and controls
156 lines (148 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { type Component } from "solid-js";
import { createIslandReference } from "../server/islands/index";
import {
deserializeJSONStream,
deserializeJSStream,
serializeToJSONString,
} from "./serialization";
let INSTANCE = 0;
function createRequest(
base: string,
id: string,
instance: string,
options: RequestInit,
) {
return fetch(base, {
method: "POST",
...options,
headers: {
...options.headers,
"X-Server-Id": encodeURIComponent(id),
"X-Server-Instance": instance
}
});
}
async function fetchServerFunction(
base: string,
id: string,
options: Omit<RequestInit, "body">,
args: any[],
) {
const instance = `server-fn:${INSTANCE++}`;
const response = await (args.length === 0
? createRequest(base, id, instance, options)
: args.length === 1 && args[0] instanceof FormData
? createRequest(base, id, instance, { ...options, body: args[0] })
: args.length === 1 && args[0] instanceof URLSearchParams
? createRequest(base, id, instance, {
...options,
body: args[0],
headers: {
...options.headers,
"Content-Type": "application/x-www-form-urlencoded",
},
})
: createRequest(base, id, instance, {
...options,
body: await serializeToJSONString(args),
// duplex: 'half',
// body: serializeToJSONStream(args),
headers: {
...options.headers,
"x-serialized": "true",
"Content-Type": "text/plain"
},
}));
if (
response.headers.has("Location") ||
response.headers.has("X-Revalidate") ||
response.headers.has("X-Single-Flight")
) {
if (response.body) {
/* @ts-ignore-next-line */
response.customBody = () => {
if (import.meta.env.SEROVAL_MODE === "js") {
return deserializeJSStream(instance, response.clone());
}
return deserializeJSONStream(response.clone());
};
}
return response;
}
const contentType = response.headers.get("Content-Type");
const cloned = response.clone();
let result;
if (response.headers.get("x-serialized")) {
if (import.meta.env.SEROVAL_MODE === "js") {
result = await deserializeJSStream(instance, cloned);
} else {
result = await deserializeJSONStream(cloned);
}
} else if (contentType && contentType.startsWith("text/plain")) {
result = await cloned.text();
} else if (contentType && contentType.startsWith("application/json")) {
result = await cloned.json();
}
if (response.headers.has("X-Error")) {
throw result;
}
return result;
}
export function createServerReference(fn: Function, id: string, name: string) {
const baseURL = import.meta.env.SERVER_BASE_URL;
return new Proxy(fn, {
get(target, prop, receiver) {
if (prop === "url") {
return `${baseURL}/_server?id=${encodeURIComponent(id)}&name=${encodeURIComponent(name)}`;
}
if (prop === "GET") {
return receiver.withOptions({ method: "GET" });
}
if (prop === "withOptions") {
const url = `${baseURL}/_server/?id=${encodeURIComponent(id)}&name=${encodeURIComponent(
name,
)}`;
return (options: RequestInit) => {
const fn = async (...args: any[]) => {
const encodeArgs =
options.method && options.method.toUpperCase() === "GET";
return fetchServerFunction(
encodeArgs
? url +
(args.length
? `&args=${encodeURIComponent(
await serializeToJSONString(args),
)}`
: "")
: `${baseURL}/_server`,
`${id}#${name}`,
options,
encodeArgs ? [] : args,
);
};
fn.url = url;
return fn;
};
}
return (target as any)[prop];
},
apply(target, thisArg, args) {
return fetchServerFunction(
`${baseURL}/_server`,
`${id}#${name}`,
{},
args,
);
},
});
}
export function createClientReference(
Component: Component<any>,
id: string,
name: string,
) {
if (typeof Component === "function") {
return createIslandReference(Component, id, name);
}
return Component;
}