@solidjs/start 2.0.0-beta.0 → 2.0.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/dist/config/dev-server.js +3 -1
- package/dist/config/fs-router.d.ts +1 -1
- package/dist/http/index.d.ts +4 -4
- package/package.json +11 -5
- package/scripts/build.js +0 -15
- package/scripts/validate-imports.js +0 -316
- package/src/client/StartClient.tsx +0 -34
- package/src/client/index.tsx +0 -2
- package/src/client/mount.ts +0 -1
- package/src/client/spa/index.tsx +0 -9
- package/src/config/constants.ts +0 -19
- package/src/config/dev-server.ts +0 -157
- package/src/config/env.ts +0 -105
- package/src/config/fs-router.ts +0 -162
- package/src/config/fs-routes/fs-watcher.ts +0 -66
- package/src/config/fs-routes/index.ts +0 -136
- package/src/config/fs-routes/router.ts +0 -184
- package/src/config/fs-routes/tree-shake.ts +0 -324
- package/src/config/index.ts +0 -239
- package/src/config/lazy.ts +0 -132
- package/src/config/manifest.ts +0 -120
- package/src/config/utils.ts +0 -5
- package/src/config/vite-utils.ts +0 -113
- package/src/directives/bubble-function-declaration.ts +0 -33
- package/src/directives/compile.ts +0 -54
- package/src/directives/generate-unique-name.ts +0 -22
- package/src/directives/get-descriptive-name.ts +0 -39
- package/src/directives/get-import-identifier.ts +0 -34
- package/src/directives/get-root-statement-path.ts +0 -14
- package/src/directives/index.ts +0 -233
- package/src/directives/is-statement-top-level.ts +0 -13
- package/src/directives/paths.ts +0 -63
- package/src/directives/plugin.ts +0 -389
- package/src/directives/remove-unused-variables.ts +0 -70
- package/src/directives/types.ts +0 -12
- package/src/directives/xxhash32.ts +0 -214
- package/src/env.d.ts +0 -17
- package/src/fns/client.ts +0 -141
- package/src/fns/handler.spec.ts +0 -85
- package/src/fns/handler.ts +0 -287
- package/src/fns/registration.ts +0 -19
- package/src/fns/serialization.ts +0 -250
- package/src/fns/server.ts +0 -46
- package/src/fns/shared.ts +0 -116
- package/src/http/index.ts +0 -216
- package/src/index.tsx +0 -7
- package/src/internal.d.ts +0 -14
- package/src/middleware/index.ts +0 -92
- package/src/router.tsx +0 -37
- package/src/server/StartServer.tsx +0 -58
- package/src/server/assets/PatchVirtualDevStyles.tsx +0 -31
- package/src/server/assets/index.ts +0 -70
- package/src/server/assets/render.tsx +0 -46
- package/src/server/collect-styles.ts +0 -70
- package/src/server/fetchEvent.spec.ts +0 -85
- package/src/server/fetchEvent.ts +0 -36
- package/src/server/handler.ts +0 -261
- package/src/server/index.tsx +0 -29
- package/src/server/manifest/client-manifest.ts +0 -8
- package/src/server/manifest/dev-client-manifest.ts +0 -21
- package/src/server/manifest/dev-ssr-manifest.ts +0 -23
- package/src/server/manifest/prod-client-manifest.ts +0 -21
- package/src/server/manifest/prod-ssr-manifest.ts +0 -109
- package/src/server/manifest/ssr-manifest.ts +0 -10
- package/src/server/routes.ts +0 -126
- package/src/server/spa/StartServer.tsx +0 -42
- package/src/server/spa/handler.ts +0 -29
- package/src/server/spa/index.tsx +0 -5
- package/src/server/types.ts +0 -82
- package/src/server/util.ts +0 -12
- package/src/shared/ErrorBoundary.tsx +0 -52
- package/src/shared/GET.ts +0 -8
- package/src/shared/HttpHeader.tsx +0 -42
- package/src/shared/HttpStatusCode.ts +0 -27
- package/src/shared/clientOnly.ts +0 -44
- package/src/shared/dev-overlay/CodeView.tsx +0 -90
- package/src/shared/dev-overlay/DevOverlayDialog.tsx +0 -316
- package/src/shared/dev-overlay/createStackFrame.ts +0 -74
- package/src/shared/dev-overlay/download.ts +0 -6
- package/src/shared/dev-overlay/env.d.ts +0 -4
- package/src/shared/dev-overlay/get-source-map.ts +0 -33
- package/src/shared/dev-overlay/icons.tsx +0 -456
- package/src/shared/dev-overlay/index.tsx +0 -63
- package/src/shared/dev-overlay/styles.css +0 -292
- package/src/shared/lazy.ts +0 -52
- package/src/shared/serverFunction.ts +0 -10
- package/src/utils/debounce.ts +0 -10
- package/src/virtual.d.ts +0 -27
- package/tsconfig.json +0 -24
- package/vitest.config.ts +0 -9
package/src/fns/handler.ts
DELETED
|
@@ -1,287 +0,0 @@
|
|
|
1
|
-
import { parseSetCookie } from "cookie-es";
|
|
2
|
-
import { type H3Event, parseCookies } from "h3";
|
|
3
|
-
import { sharedConfig } from "solid-js";
|
|
4
|
-
import { renderToString } from "solid-js/web";
|
|
5
|
-
import { provideRequestEvent } from "solid-js/web/storage";
|
|
6
|
-
|
|
7
|
-
import { getFetchEvent, mergeResponseHeaders } from "../server/fetchEvent.ts";
|
|
8
|
-
import { createPageEvent } from "../server/handler.ts";
|
|
9
|
-
import {
|
|
10
|
-
deserializeFromJSONString,
|
|
11
|
-
serializeToJSONStream,
|
|
12
|
-
serializeToJSStream,
|
|
13
|
-
} from "./serialization.ts";
|
|
14
|
-
import {
|
|
15
|
-
BODY_FORMAT_KEY,
|
|
16
|
-
BodyFormat,
|
|
17
|
-
extractBody,
|
|
18
|
-
getHeadersAndBody,
|
|
19
|
-
} from "./shared.ts";
|
|
20
|
-
import "solid-start:server-fn-manifest";
|
|
21
|
-
|
|
22
|
-
import { getServerFunction } from "./registration.ts";
|
|
23
|
-
import type { FetchEvent, PageEvent } from "../server/types.ts";
|
|
24
|
-
import { getExpectedRedirectStatus } from "../server/util.ts";
|
|
25
|
-
|
|
26
|
-
export async function handleServerFunction(h3Event: H3Event) {
|
|
27
|
-
const event = getFetchEvent(h3Event);
|
|
28
|
-
const request = event.request;
|
|
29
|
-
|
|
30
|
-
const serverReference = request.headers.get("X-Server-Id");
|
|
31
|
-
const instance = request.headers.get("X-Server-Instance");
|
|
32
|
-
const singleFlight = request.headers.has("X-Single-Flight");
|
|
33
|
-
const url = new URL(request.url);
|
|
34
|
-
let functionId: string | undefined | null;
|
|
35
|
-
if (serverReference) {
|
|
36
|
-
// invariant(typeof serverReference === "string", "Invalid server function");
|
|
37
|
-
[functionId] = serverReference.split("#");
|
|
38
|
-
} else {
|
|
39
|
-
functionId = url.searchParams.get("id");
|
|
40
|
-
|
|
41
|
-
if (!functionId) {
|
|
42
|
-
return process.env.NODE_ENV === "development"
|
|
43
|
-
? new Response("Server function not found", { status: 404 })
|
|
44
|
-
: new Response(null, { status: 404 });
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const serverFunction = getServerFunction(functionId!);
|
|
49
|
-
|
|
50
|
-
let parsed: any[] = [];
|
|
51
|
-
|
|
52
|
-
// grab bound arguments from url when no JS
|
|
53
|
-
if (!instance || request.method === "GET") {
|
|
54
|
-
const args = url.searchParams.get("args");
|
|
55
|
-
if (args) {
|
|
56
|
-
const result = (await deserializeFromJSONString(args)) as any[];
|
|
57
|
-
for (const arg of result) {
|
|
58
|
-
parsed.push(arg);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
if (request.method === "POST" && request.body !== null) {
|
|
63
|
-
const bodyFormat = request.headers.get(BODY_FORMAT_KEY);
|
|
64
|
-
const decoded = await extractBody("", false, request.clone());
|
|
65
|
-
if (bodyFormat === BodyFormat.Seroval) {
|
|
66
|
-
parsed = decoded as any[];
|
|
67
|
-
} else {
|
|
68
|
-
parsed.push(decoded);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
try {
|
|
72
|
-
let result = await provideRequestEvent(event, async () => {
|
|
73
|
-
/* @ts-expect-error */
|
|
74
|
-
sharedConfig.context = { event };
|
|
75
|
-
event.locals.serverFunctionMeta = {
|
|
76
|
-
id: functionId,
|
|
77
|
-
};
|
|
78
|
-
return serverFunction(...parsed);
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
if (singleFlight && instance) {
|
|
82
|
-
result = await handleSingleFlight(event, result);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// handle responses
|
|
86
|
-
if (result instanceof Response) {
|
|
87
|
-
if (result.headers && result.headers.has("X-Content-Raw")) return result;
|
|
88
|
-
if (instance) {
|
|
89
|
-
// forward headers
|
|
90
|
-
if (result.headers) mergeResponseHeaders(h3Event, result.headers);
|
|
91
|
-
// forward non-redirect statuses
|
|
92
|
-
if (result.status && (result.status < 300 || result.status >= 400))
|
|
93
|
-
h3Event.res.status = result.status;
|
|
94
|
-
if ((result as any).customBody) {
|
|
95
|
-
result = await (result as any).customBody();
|
|
96
|
-
} else if (result.body == null) result = null;
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// handle no JS success case
|
|
101
|
-
if (!instance) return handleNoJS(result, request, parsed);
|
|
102
|
-
|
|
103
|
-
const body = getHeadersAndBody(result);
|
|
104
|
-
if (body) {
|
|
105
|
-
return new Response(body.body, {
|
|
106
|
-
headers: body.headers,
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
h3Event.res.headers.set(BODY_FORMAT_KEY, BodyFormat.Seroval);
|
|
110
|
-
if (import.meta.env.SEROVAL_MODE === "js") {
|
|
111
|
-
h3Event.res.headers.set("content-type", "text/javascript");
|
|
112
|
-
return serializeToJSStream(instance, result);
|
|
113
|
-
}
|
|
114
|
-
h3Event.res.headers.set("content-type", "text/plain");
|
|
115
|
-
return serializeToJSONStream(result);
|
|
116
|
-
} catch (x) {
|
|
117
|
-
if (x instanceof Response) {
|
|
118
|
-
if (singleFlight && instance) {
|
|
119
|
-
x = await handleSingleFlight(event, x);
|
|
120
|
-
}
|
|
121
|
-
// forward headers
|
|
122
|
-
if ((x as any).headers) mergeResponseHeaders(h3Event, (x as any).headers);
|
|
123
|
-
// forward non-redirect statuses
|
|
124
|
-
if ((x as any).status && (!instance || (x as any).status < 300 || (x as any).status >= 400))
|
|
125
|
-
h3Event.res.status = (x as any).status;
|
|
126
|
-
if ((x as any).customBody) {
|
|
127
|
-
x = await (x as any).customBody();
|
|
128
|
-
} else if ((x as any).body == null) x = null;
|
|
129
|
-
h3Event.res.headers.set("X-Error", "true");
|
|
130
|
-
} else if (instance) {
|
|
131
|
-
const error = x instanceof Error ? x.message : typeof x === "string" ? x : "true";
|
|
132
|
-
|
|
133
|
-
h3Event.res.headers.set("X-Error", error.replace(/[\r\n]+/g, ""));
|
|
134
|
-
} else {
|
|
135
|
-
x = handleNoJS(x, request, parsed, true);
|
|
136
|
-
}
|
|
137
|
-
if (instance) {
|
|
138
|
-
const body = getHeadersAndBody(x);
|
|
139
|
-
if (body) {
|
|
140
|
-
const headers = new Headers(body.headers as HeadersInit);
|
|
141
|
-
const errorHeader = h3Event.res.headers.get("X-Error");
|
|
142
|
-
if (errorHeader !== null) {
|
|
143
|
-
headers.set("X-Error", errorHeader);
|
|
144
|
-
}
|
|
145
|
-
return new Response(body.body, {
|
|
146
|
-
headers: body.headers,
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
h3Event.res.headers.set(BODY_FORMAT_KEY, BodyFormat.Seroval);
|
|
150
|
-
if (import.meta.env.SEROVAL_MODE === "js") {
|
|
151
|
-
h3Event.res.headers.set("content-type", "text/javascript");
|
|
152
|
-
return serializeToJSStream(instance, x);
|
|
153
|
-
}
|
|
154
|
-
h3Event.res.headers.set("content-type", "text/plain");
|
|
155
|
-
return serializeToJSONStream(x);
|
|
156
|
-
}
|
|
157
|
-
return x;
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
function handleNoJS(result: any, request: Request, parsed: any[], thrown?: boolean) {
|
|
162
|
-
const url = new URL(request.url);
|
|
163
|
-
const isError = result instanceof Error;
|
|
164
|
-
let statusCode = 302;
|
|
165
|
-
let headers: Headers;
|
|
166
|
-
if (result instanceof Response) {
|
|
167
|
-
headers = new Headers(result.headers);
|
|
168
|
-
if (result.headers.has("Location")) {
|
|
169
|
-
headers.set(
|
|
170
|
-
`Location`,
|
|
171
|
-
new URL(result.headers.get("Location")!, url.origin + import.meta.env.BASE_URL).toString(),
|
|
172
|
-
);
|
|
173
|
-
statusCode = getExpectedRedirectStatus(result);
|
|
174
|
-
}
|
|
175
|
-
} else
|
|
176
|
-
headers = new Headers({
|
|
177
|
-
Location: new URL(request.headers.get("referer")!).toString(),
|
|
178
|
-
});
|
|
179
|
-
if (result) {
|
|
180
|
-
headers.append(
|
|
181
|
-
"Set-Cookie",
|
|
182
|
-
`flash=${encodeURIComponent(
|
|
183
|
-
JSON.stringify({
|
|
184
|
-
url: url.pathname + url.search,
|
|
185
|
-
result: isError ? result.message : result,
|
|
186
|
-
thrown: thrown,
|
|
187
|
-
error: isError,
|
|
188
|
-
input: [...parsed.slice(0, -1), [...parsed[parsed.length - 1].entries()]],
|
|
189
|
-
}),
|
|
190
|
-
)}; Secure; HttpOnly;`,
|
|
191
|
-
);
|
|
192
|
-
}
|
|
193
|
-
return new Response(null, {
|
|
194
|
-
status: statusCode,
|
|
195
|
-
headers,
|
|
196
|
-
});
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
let App: any;
|
|
200
|
-
export function createSingleFlightHeaders(sourceEvent: FetchEvent) {
|
|
201
|
-
// cookie handling logic is pretty simplistic so this might be imperfect
|
|
202
|
-
// unclear if h3 internals are available on all platforms but we need a way to
|
|
203
|
-
// update request headers on the underlying H3 event.
|
|
204
|
-
|
|
205
|
-
const headers = new Headers(sourceEvent.request.headers);
|
|
206
|
-
const cookies = parseCookies(sourceEvent.nativeEvent);
|
|
207
|
-
const SetCookies = sourceEvent.response.headers.getSetCookie();
|
|
208
|
-
headers.delete("cookie");
|
|
209
|
-
// let useH3Internals = false;
|
|
210
|
-
// if (sourceEvent.nativeEvent.node?.req) {
|
|
211
|
-
// useH3Internals = true;
|
|
212
|
-
// sourceEvent.nativeEvent.node.req.headers.cookie = "";
|
|
213
|
-
// }
|
|
214
|
-
SetCookies.forEach(cookie => {
|
|
215
|
-
if (!cookie) return;
|
|
216
|
-
const { maxAge, expires, name, value } = parseSetCookie(cookie);
|
|
217
|
-
if (maxAge != null && maxAge <= 0) {
|
|
218
|
-
delete cookies[name];
|
|
219
|
-
return;
|
|
220
|
-
}
|
|
221
|
-
if (expires != null && expires.getTime() <= Date.now()) {
|
|
222
|
-
delete cookies[name];
|
|
223
|
-
return;
|
|
224
|
-
}
|
|
225
|
-
cookies[name] = value;
|
|
226
|
-
});
|
|
227
|
-
Object.entries(cookies).forEach(([key, value]) => {
|
|
228
|
-
headers.append("cookie", `${key}=${value}`);
|
|
229
|
-
// useH3Internals &&
|
|
230
|
-
// (sourceEvent.nativeEvent.node.req.headers.cookie += `${key}=${value};`);
|
|
231
|
-
});
|
|
232
|
-
|
|
233
|
-
return headers;
|
|
234
|
-
}
|
|
235
|
-
async function handleSingleFlight(sourceEvent: FetchEvent, result: any): Promise<Response> {
|
|
236
|
-
let revalidate: string[];
|
|
237
|
-
let url = new URL(sourceEvent.request.headers.get("referer")!).toString();
|
|
238
|
-
if (result instanceof Response) {
|
|
239
|
-
if (result.headers.has("X-Revalidate"))
|
|
240
|
-
revalidate = result.headers.get("X-Revalidate")!.split(",");
|
|
241
|
-
if (result.headers.has("Location"))
|
|
242
|
-
url = new URL(
|
|
243
|
-
result.headers.get("Location")!,
|
|
244
|
-
new URL(sourceEvent.request.url).origin + import.meta.env.BASE_URL,
|
|
245
|
-
).toString();
|
|
246
|
-
}
|
|
247
|
-
const event = { ...sourceEvent } as PageEvent;
|
|
248
|
-
event.request = new Request(url, {
|
|
249
|
-
headers: createSingleFlightHeaders(sourceEvent),
|
|
250
|
-
});
|
|
251
|
-
return await provideRequestEvent(event, async () => {
|
|
252
|
-
await createPageEvent(event);
|
|
253
|
-
App || (App = (await import("solid-start:app")).default);
|
|
254
|
-
/* @ts-expect-error */
|
|
255
|
-
event.router.dataOnly = revalidate || true;
|
|
256
|
-
/* @ts-expect-error */
|
|
257
|
-
event.router.previousUrl = sourceEvent.request.headers.get("referer");
|
|
258
|
-
try {
|
|
259
|
-
renderToString(() => {
|
|
260
|
-
/* @ts-expect-error */
|
|
261
|
-
sharedConfig.context.event = event;
|
|
262
|
-
App();
|
|
263
|
-
});
|
|
264
|
-
} catch (e) {
|
|
265
|
-
console.log(e);
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
/* @ts-expect-error */
|
|
269
|
-
const body = event.router.data;
|
|
270
|
-
if (!body) return result;
|
|
271
|
-
let containsKey = false;
|
|
272
|
-
for (const key in body) {
|
|
273
|
-
if (body[key] === undefined) delete body[key];
|
|
274
|
-
else containsKey = true;
|
|
275
|
-
}
|
|
276
|
-
if (!containsKey) return result;
|
|
277
|
-
if (!(result instanceof Response)) {
|
|
278
|
-
body["_$value"] = result;
|
|
279
|
-
result = new Response(null, { status: 200 });
|
|
280
|
-
} else if ((result as any).customBody) {
|
|
281
|
-
body["_$value"] = (result as any).customBody();
|
|
282
|
-
}
|
|
283
|
-
result.customBody = () => body;
|
|
284
|
-
result.headers.set("X-Single-Flight", "true");
|
|
285
|
-
return result;
|
|
286
|
-
});
|
|
287
|
-
}
|
package/src/fns/registration.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
const REGISTRATIONS = new Map<string, Function>();
|
|
2
|
-
|
|
3
|
-
export function registerServerFunction<T extends any[], R>(
|
|
4
|
-
id: string,
|
|
5
|
-
callback: (...args: T) => Promise<R>,
|
|
6
|
-
) {
|
|
7
|
-
REGISTRATIONS.set(id, callback);
|
|
8
|
-
return callback;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export function getServerFunction<T extends any[], R>(
|
|
12
|
-
id: string,
|
|
13
|
-
): ((...args: T) => Promise<R>) {
|
|
14
|
-
const fn = REGISTRATIONS.get(id) as ((...args: T) => Promise<R>) | undefined;
|
|
15
|
-
if (fn) {
|
|
16
|
-
return fn;
|
|
17
|
-
}
|
|
18
|
-
throw new Error('invalid server function: ' + id);
|
|
19
|
-
}
|
package/src/fns/serialization.ts
DELETED
|
@@ -1,250 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
crossSerializeStream,
|
|
3
|
-
deserialize,
|
|
4
|
-
Feature,
|
|
5
|
-
fromCrossJSON,
|
|
6
|
-
getCrossReferenceHeader,
|
|
7
|
-
type SerovalNode,
|
|
8
|
-
toCrossJSONStream,
|
|
9
|
-
} from "seroval";
|
|
10
|
-
import {
|
|
11
|
-
AbortSignalPlugin,
|
|
12
|
-
CustomEventPlugin,
|
|
13
|
-
DOMExceptionPlugin,
|
|
14
|
-
EventPlugin,
|
|
15
|
-
FormDataPlugin,
|
|
16
|
-
HeadersPlugin,
|
|
17
|
-
ReadableStreamPlugin,
|
|
18
|
-
RequestPlugin,
|
|
19
|
-
ResponsePlugin,
|
|
20
|
-
URLPlugin,
|
|
21
|
-
URLSearchParamsPlugin,
|
|
22
|
-
} from "seroval-plugins/web";
|
|
23
|
-
|
|
24
|
-
// TODO(Alexis): if we can, allow providing an option to extend these.
|
|
25
|
-
const DEFAULT_PLUGINS = [
|
|
26
|
-
AbortSignalPlugin,
|
|
27
|
-
CustomEventPlugin,
|
|
28
|
-
DOMExceptionPlugin,
|
|
29
|
-
EventPlugin,
|
|
30
|
-
FormDataPlugin,
|
|
31
|
-
HeadersPlugin,
|
|
32
|
-
ReadableStreamPlugin,
|
|
33
|
-
RequestPlugin,
|
|
34
|
-
ResponsePlugin,
|
|
35
|
-
URLSearchParamsPlugin,
|
|
36
|
-
URLPlugin,
|
|
37
|
-
];
|
|
38
|
-
const MAX_SERIALIZATION_DEPTH_LIMIT = 64;
|
|
39
|
-
const DISABLED_FEATURES = Feature.RegExp;
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Alexis:
|
|
43
|
-
*
|
|
44
|
-
* A "chunk" is a piece of data emitted by the streaming serializer.
|
|
45
|
-
* Each chunk is represented by a 32-bit value (encoded in hexadecimal),
|
|
46
|
-
* followed by the encoded string (8-bit representation). This format
|
|
47
|
-
* is important so we know how much of the chunk being streamed we
|
|
48
|
-
* are expecting before parsing the entire string data.
|
|
49
|
-
*
|
|
50
|
-
* This is sort of a bootleg "multipart/form-data" except it's bad at
|
|
51
|
-
* handling File/Blob LOL
|
|
52
|
-
*
|
|
53
|
-
* The format is as follows:
|
|
54
|
-
* ;0xFFFFFFFF;<string data>
|
|
55
|
-
*/
|
|
56
|
-
function createChunk(data: string): Uint8Array {
|
|
57
|
-
const encodeData = new TextEncoder().encode(data);
|
|
58
|
-
const bytes = encodeData.length;
|
|
59
|
-
const baseHex = bytes.toString(16);
|
|
60
|
-
const totalHex = "00000000".substring(0, 8 - baseHex.length) + baseHex; // 32-bit
|
|
61
|
-
const head = new TextEncoder().encode(`;0x${totalHex};`);
|
|
62
|
-
|
|
63
|
-
const chunk = new Uint8Array(12 + bytes);
|
|
64
|
-
chunk.set(head);
|
|
65
|
-
chunk.set(encodeData, 12);
|
|
66
|
-
return chunk;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
export function serializeToJSStream(id: string, value: any) {
|
|
70
|
-
return new ReadableStream({
|
|
71
|
-
start(controller) {
|
|
72
|
-
crossSerializeStream(value, {
|
|
73
|
-
scopeId: id,
|
|
74
|
-
plugins: DEFAULT_PLUGINS,
|
|
75
|
-
onSerialize(data: string, initial: boolean) {
|
|
76
|
-
controller.enqueue(
|
|
77
|
-
createChunk(initial ? `(${getCrossReferenceHeader(id)},${data})` : data),
|
|
78
|
-
);
|
|
79
|
-
},
|
|
80
|
-
onDone() {
|
|
81
|
-
controller.close();
|
|
82
|
-
},
|
|
83
|
-
onError(error: any) {
|
|
84
|
-
controller.error(error);
|
|
85
|
-
},
|
|
86
|
-
});
|
|
87
|
-
},
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
export function serializeToJSONStream(value: any) {
|
|
92
|
-
return new ReadableStream({
|
|
93
|
-
start(controller) {
|
|
94
|
-
toCrossJSONStream(value, {
|
|
95
|
-
disabledFeatures: DISABLED_FEATURES,
|
|
96
|
-
depthLimit: MAX_SERIALIZATION_DEPTH_LIMIT,
|
|
97
|
-
plugins: DEFAULT_PLUGINS,
|
|
98
|
-
onParse(node) {
|
|
99
|
-
controller.enqueue(createChunk(JSON.stringify(node)));
|
|
100
|
-
},
|
|
101
|
-
onDone() {
|
|
102
|
-
controller.close();
|
|
103
|
-
},
|
|
104
|
-
onError(error) {
|
|
105
|
-
controller.error(error);
|
|
106
|
-
},
|
|
107
|
-
});
|
|
108
|
-
},
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
class SerovalChunkReader {
|
|
113
|
-
reader: ReadableStreamDefaultReader<Uint8Array>;
|
|
114
|
-
buffer: Uint8Array;
|
|
115
|
-
done: boolean;
|
|
116
|
-
constructor(stream: ReadableStream<Uint8Array>) {
|
|
117
|
-
this.reader = stream.getReader();
|
|
118
|
-
this.buffer = new Uint8Array(0);
|
|
119
|
-
this.done = false;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
async readChunk() {
|
|
123
|
-
// if there's no chunk, read again
|
|
124
|
-
const chunk = await this.reader.read();
|
|
125
|
-
if (!chunk.done) {
|
|
126
|
-
// repopulate the buffer
|
|
127
|
-
const newBuffer = new Uint8Array(this.buffer.length + chunk.value.length);
|
|
128
|
-
newBuffer.set(this.buffer);
|
|
129
|
-
newBuffer.set(chunk.value, this.buffer.length);
|
|
130
|
-
this.buffer = newBuffer;
|
|
131
|
-
} else {
|
|
132
|
-
this.done = true;
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
async next(): Promise<{ done: true; value: undefined } | { done: false; value: string }> {
|
|
137
|
-
// Check if the buffer is empty
|
|
138
|
-
if (this.buffer.length === 0) {
|
|
139
|
-
// if we are already done...
|
|
140
|
-
if (this.done) {
|
|
141
|
-
return {
|
|
142
|
-
done: true,
|
|
143
|
-
value: undefined,
|
|
144
|
-
};
|
|
145
|
-
}
|
|
146
|
-
// Otherwise, read a new chunk
|
|
147
|
-
await this.readChunk();
|
|
148
|
-
return await this.next();
|
|
149
|
-
}
|
|
150
|
-
// Read the "byte header"
|
|
151
|
-
// The byte header tells us how big the expected data is
|
|
152
|
-
// so we know how much data we should wait before we
|
|
153
|
-
// deserialize the data
|
|
154
|
-
const head = new TextDecoder().decode(this.buffer.subarray(1, 11));
|
|
155
|
-
const bytes = Number.parseInt(head, 16); // ;0x00000000;
|
|
156
|
-
if (Number.isNaN(bytes)) {
|
|
157
|
-
throw new Error("Malformed server function stream.");
|
|
158
|
-
}
|
|
159
|
-
// Check if the buffer has enough bytes to be parsed
|
|
160
|
-
while (bytes > this.buffer.length - 12) {
|
|
161
|
-
// If it's not enough, and the reader is done
|
|
162
|
-
// then the chunk is invalid.
|
|
163
|
-
if (this.done) {
|
|
164
|
-
throw new Error("Malformed server function stream.");
|
|
165
|
-
}
|
|
166
|
-
// Otherwise, we read more chunks
|
|
167
|
-
await this.readChunk();
|
|
168
|
-
}
|
|
169
|
-
// Extract the exact chunk as defined by the byte header
|
|
170
|
-
const partial = new TextDecoder().decode(this.buffer.subarray(12, 12 + bytes));
|
|
171
|
-
// The rest goes to the buffer
|
|
172
|
-
this.buffer = this.buffer.subarray(12 + bytes);
|
|
173
|
-
|
|
174
|
-
// Deserialize the chunk
|
|
175
|
-
return {
|
|
176
|
-
done: false,
|
|
177
|
-
value: partial,
|
|
178
|
-
};
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
async drain(interpret: (chunk: string) => void) {
|
|
182
|
-
while (true) {
|
|
183
|
-
const result = await this.next();
|
|
184
|
-
if (result.done) {
|
|
185
|
-
break;
|
|
186
|
-
} else {
|
|
187
|
-
interpret(result.value);
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
export async function serializeToJSONString(value: any) {
|
|
194
|
-
const response = new Response(serializeToJSONStream(value));
|
|
195
|
-
return await response.text();
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
export async function deserializeFromJSONString(json: string) {
|
|
199
|
-
const blob = new Response(json);
|
|
200
|
-
return await deserializeJSONStream(blob);
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
export async function deserializeJSONStream(response: Response | Request) {
|
|
204
|
-
if (!response.body) {
|
|
205
|
-
throw new Error("missing body");
|
|
206
|
-
}
|
|
207
|
-
const reader = new SerovalChunkReader(response.body);
|
|
208
|
-
const result = await reader.next();
|
|
209
|
-
if (!result.done) {
|
|
210
|
-
const refs = new Map();
|
|
211
|
-
|
|
212
|
-
function interpretChunk(chunk: string): unknown {
|
|
213
|
-
const value = fromCrossJSON(JSON.parse(chunk) as SerovalNode, {
|
|
214
|
-
refs,
|
|
215
|
-
disabledFeatures: DISABLED_FEATURES,
|
|
216
|
-
depthLimit: MAX_SERIALIZATION_DEPTH_LIMIT,
|
|
217
|
-
plugins: DEFAULT_PLUGINS,
|
|
218
|
-
});
|
|
219
|
-
return value;
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
void reader.drain(interpretChunk);
|
|
223
|
-
|
|
224
|
-
return interpretChunk(result.value);
|
|
225
|
-
}
|
|
226
|
-
return undefined;
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
export async function deserializeJSStream(id: string, response: Request | Response) {
|
|
230
|
-
if (!response.body) {
|
|
231
|
-
throw new Error("missing body");
|
|
232
|
-
}
|
|
233
|
-
const reader = new SerovalChunkReader(response.body);
|
|
234
|
-
|
|
235
|
-
const result = await reader.next();
|
|
236
|
-
|
|
237
|
-
if (!result.done) {
|
|
238
|
-
reader.drain(deserialize).then(
|
|
239
|
-
() => {
|
|
240
|
-
// @ts-ignore
|
|
241
|
-
delete $R[id];
|
|
242
|
-
},
|
|
243
|
-
() => {
|
|
244
|
-
// no-op
|
|
245
|
-
},
|
|
246
|
-
);
|
|
247
|
-
return deserialize(result.value);
|
|
248
|
-
}
|
|
249
|
-
return undefined;
|
|
250
|
-
}
|
package/src/fns/server.ts
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { getRequestEvent } from "solid-js/web";
|
|
2
|
-
import { provideRequestEvent } from "solid-js/web/storage";
|
|
3
|
-
import { registerServerFunction } from "./registration.ts";
|
|
4
|
-
|
|
5
|
-
interface Registration<T extends any[], R> {
|
|
6
|
-
id: string;
|
|
7
|
-
fn: (...args: T) => Promise<R>;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export function createServerReference<T extends any[], R>(
|
|
11
|
-
id: string,
|
|
12
|
-
fn: (...args: T) => Promise<R>,
|
|
13
|
-
) {
|
|
14
|
-
const registration: Registration<T, R> = { id, fn };
|
|
15
|
-
registerServerFunction(id, fn);
|
|
16
|
-
return registration;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function cloneServerReference<T extends any[], R>({ id, fn }: Registration<T, R>) {
|
|
20
|
-
if (typeof fn !== "function")
|
|
21
|
-
throw new Error("Export from a 'use server' module must be a function");
|
|
22
|
-
let baseURL = import.meta.env.BASE_URL ?? "/";
|
|
23
|
-
if (!baseURL.endsWith("/")) baseURL += "/";
|
|
24
|
-
|
|
25
|
-
return new Proxy(fn, {
|
|
26
|
-
get(target, prop, receiver) {
|
|
27
|
-
if (prop === "url") {
|
|
28
|
-
return `${baseURL}_server?id=${encodeURIComponent(id)}`;
|
|
29
|
-
}
|
|
30
|
-
if (prop === "GET") return receiver;
|
|
31
|
-
return (target as any)[prop];
|
|
32
|
-
},
|
|
33
|
-
apply(target, thisArg, args) {
|
|
34
|
-
const ogEvt = getRequestEvent();
|
|
35
|
-
if (!ogEvt) throw new Error("Cannot call server function outside of a request");
|
|
36
|
-
const evt = { ...ogEvt };
|
|
37
|
-
evt.locals.serverFunctionMeta = {
|
|
38
|
-
id,
|
|
39
|
-
};
|
|
40
|
-
evt.serverOnly = true;
|
|
41
|
-
return provideRequestEvent(evt, () => {
|
|
42
|
-
return fn.apply(thisArg, args as T);
|
|
43
|
-
});
|
|
44
|
-
},
|
|
45
|
-
});
|
|
46
|
-
}
|