-
Notifications
You must be signed in to change notification settings - Fork 417
Expand file tree
/
Copy pathclient.ts
More file actions
141 lines (130 loc) · 3.58 KB
/
client.ts
File metadata and controls
141 lines (130 loc) · 3.58 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
import { type Component } from "solid-js";
import {
// serializeToJSONStream,
serializeToJSONString,
} from "./serialization.ts";
import {
BODY_FORMAT_KEY,
BodyFormat,
extractBody,
getHeadersAndBody,
} from "./shared.ts";
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": id,
"X-Server-Instance": instance,
},
});
}
async function initializeResponse(
base: string,
id: string,
instance: string,
options: RequestInit,
args: any[],
) {
// No args, skip serialization
if (args.length === 0) {
return createRequest(base, id, instance, options);
}
// For single arguments, we can directly encode as body
if (args.length === 1) {
const body = args[0];
const result = getHeadersAndBody(body);
if (result) {
return createRequest(base, id, instance, {
...options,
body: result.body,
headers: {
...options.headers,
...result.headers,
},
});
}
}
// Fallback to seroval
return createRequest(base, id, instance, {
...options,
// TODO(Alexis): move to serializeToJSONStream
body: await serializeToJSONString(args),
// duplex: 'half',
// body: serializeToJSONStream(args),
headers: {
...options.headers,
"Content-Type": "text/plain",
[BODY_FORMAT_KEY]: BodyFormat.Seroval,
},
});
}
async function fetchServerFunction(
base: string,
id: string,
options: Omit<RequestInit, "body">,
args: any[],
) {
const instance = `server-fn:${INSTANCE++}`;
const response = await initializeResponse(base, id, instance, options, args);
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 = async () => {
return await extractBody(instance, true, response.clone());
};
}
return response;
}
const result = await extractBody(instance, true, response.clone());
if (response.headers.has("X-Error")) {
throw result;
}
return result;
}
export function cloneServerReference(id: string) {
let baseURL = import.meta.env.BASE_URL ?? "/";
if (!baseURL.endsWith("/")) baseURL += "/";
const fn = (...args: any[]) => fetchServerFunction(`${baseURL}_server`, id, {}, args);
return new Proxy(fn, {
get(target, prop, receiver) {
if (prop === "url") {
return `${baseURL}_server?id=${encodeURIComponent(id)}`;
}
if (prop === "GET") {
return receiver.withOptions({ method: "GET" });
}
if (prop === "withOptions") {
const url = `${baseURL}_server?id=${encodeURIComponent(id)}`;
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,
options,
encodeArgs ? [] : args,
);
};
fn.url = url;
return fn;
};
}
return (target as any)[prop];
},
});
}
export function createClientReference(Component: Component<any>, id: string) {
return Component;
}