@solidjs/start 2.0.0-beta.1 → 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 +7 -0
- package/dist/client/StartClient.d.ts +7 -0
- package/{src/client/StartClient.tsx → dist/client/StartClient.jsx} +7 -15
- package/dist/client/index.jsx +2 -0
- package/dist/client/mount.d.ts +1 -0
- package/dist/client/mount.js +1 -0
- package/dist/client/spa/index.d.ts +4 -0
- package/dist/client/spa/index.jsx +5 -0
- package/dist/config/constants.d.ts +16 -0
- package/dist/config/constants.js +16 -0
- package/dist/config/dev-server.d.ts +2 -0
- package/dist/config/dev-server.js +128 -0
- package/dist/config/env.d.ts +19 -0
- package/dist/config/env.js +81 -0
- package/dist/config/fs-router.d.ts +37 -0
- package/dist/config/fs-router.js +141 -0
- package/dist/config/fs-routes/fs-watcher.d.ts +3 -0
- package/dist/config/fs-routes/fs-watcher.js +53 -0
- package/dist/config/fs-routes/index.d.ts +7 -0
- package/dist/config/fs-routes/index.js +107 -0
- package/dist/config/fs-routes/router.d.ts +42 -0
- package/dist/config/fs-routes/router.js +148 -0
- package/dist/config/fs-routes/tree-shake.d.ts +2 -0
- package/dist/config/fs-routes/tree-shake.js +305 -0
- package/dist/config/index.d.ts +25 -0
- package/dist/config/index.js +217 -0
- package/dist/config/lazy.d.ts +3 -0
- package/dist/config/lazy.js +114 -0
- package/dist/config/manifest.d.ts +3 -0
- package/dist/config/manifest.js +109 -0
- package/dist/config/utils.d.ts +4 -0
- package/dist/config/utils.js +6 -0
- package/dist/config/vite-utils.d.ts +21 -0
- package/dist/config/vite-utils.js +85 -0
- package/dist/directives/bubble-function-declaration.d.ts +3 -0
- package/dist/directives/bubble-function-declaration.js +23 -0
- package/dist/directives/compile.d.ts +9 -0
- package/dist/directives/compile.js +40 -0
- package/dist/directives/generate-unique-name.d.ts +3 -0
- package/dist/directives/generate-unique-name.js +16 -0
- package/dist/directives/get-descriptive-name.d.ts +2 -0
- package/dist/directives/get-descriptive-name.js +37 -0
- package/dist/directives/get-import-identifier.d.ts +4 -0
- package/dist/directives/get-import-identifier.js +19 -0
- package/dist/directives/get-root-statement-path.d.ts +2 -0
- package/dist/directives/get-root-statement-path.js +12 -0
- package/dist/directives/index.d.ts +14 -0
- package/dist/directives/index.js +170 -0
- package/dist/directives/is-statement-top-level.d.ts +3 -0
- package/dist/directives/is-statement-top-level.js +9 -0
- package/dist/directives/paths.d.ts +9 -0
- package/dist/directives/paths.js +36 -0
- package/dist/directives/plugin.d.ts +21 -0
- package/dist/directives/plugin.js +302 -0
- package/dist/directives/remove-unused-variables.d.ts +3 -0
- package/dist/directives/remove-unused-variables.js +66 -0
- package/{src/directives/types.ts → dist/directives/types.d.ts} +5 -7
- package/dist/directives/types.js +1 -0
- package/dist/directives/xxhash32.d.ts +6 -0
- package/dist/directives/xxhash32.js +193 -0
- package/dist/fns/client.d.ts +3 -0
- package/dist/fns/client.js +106 -0
- package/dist/fns/handler.d.ts +5 -0
- package/dist/fns/handler.js +269 -0
- package/dist/fns/registration.d.ts +2 -0
- package/dist/fns/registration.js +12 -0
- package/dist/fns/serialization.d.ts +6 -0
- package/dist/fns/serialization.js +208 -0
- package/dist/fns/server.d.ts +7 -0
- package/dist/fns/server.js +38 -0
- package/dist/fns/shared.d.ts +17 -0
- package/dist/fns/shared.js +105 -0
- package/dist/http/index.d.ts +81 -0
- package/dist/http/index.js +129 -0
- package/{src/index.tsx → dist/index.d.ts} +0 -2
- package/dist/index.jsx +6 -0
- package/dist/middleware/index.d.ts +38 -0
- package/dist/middleware/index.js +59 -0
- package/dist/router.d.ts +6 -0
- package/dist/router.jsx +29 -0
- package/dist/server/StartServer.d.ts +9 -0
- package/dist/server/StartServer.jsx +34 -0
- package/dist/server/assets/PatchVirtualDevStyles.d.ts +4 -0
- package/{src/server/assets/PatchVirtualDevStyles.tsx → dist/server/assets/PatchVirtualDevStyles.jsx} +7 -10
- package/dist/server/assets/index.d.ts +2 -0
- package/dist/server/assets/index.js +52 -0
- package/dist/server/assets/render.d.ts +19 -0
- package/dist/server/assets/render.jsx +14 -0
- package/dist/server/collect-styles.d.ts +3 -0
- package/dist/server/collect-styles.js +57 -0
- package/dist/server/fetchEvent.d.ts +7 -0
- package/dist/server/fetchEvent.js +26 -0
- package/dist/server/handler.d.ts +64 -0
- package/dist/server/handler.js +222 -0
- package/dist/server/index.d.ts +9 -0
- package/{src/server/index.tsx → dist/server/index.jsx} +4 -19
- package/dist/server/manifest/client-manifest.d.ts +9 -0
- package/dist/server/manifest/client-manifest.js +6 -0
- package/dist/server/manifest/dev-client-manifest.d.ts +4 -0
- package/dist/server/manifest/dev-client-manifest.js +24 -0
- package/dist/server/manifest/dev-ssr-manifest.d.ts +5 -0
- package/dist/server/manifest/dev-ssr-manifest.js +23 -0
- package/dist/server/manifest/prod-client-manifest.d.ts +5 -0
- package/dist/server/manifest/prod-client-manifest.js +26 -0
- package/dist/server/manifest/prod-ssr-manifest.d.ts +6 -0
- package/dist/server/manifest/prod-ssr-manifest.js +85 -0
- package/dist/server/manifest/ssr-manifest.d.ts +4 -0
- package/dist/server/manifest/ssr-manifest.js +6 -0
- package/dist/server/routes.d.ts +23 -0
- package/dist/server/routes.js +74 -0
- package/dist/server/spa/StartServer.d.ts +9 -0
- package/dist/server/spa/StartServer.jsx +25 -0
- package/dist/server/spa/handler.d.ts +36 -0
- package/dist/server/spa/handler.js +20 -0
- package/{src/server/spa/index.tsx → dist/server/spa/index.d.ts} +0 -1
- package/dist/server/spa/index.jsx +5 -0
- package/dist/server/types.d.ts +75 -0
- package/dist/server/types.js +1 -0
- package/dist/server/util.d.ts +2 -0
- package/dist/server/util.js +8 -0
- package/dist/shared/ErrorBoundary.d.ts +3 -0
- package/dist/shared/ErrorBoundary.jsx +36 -0
- package/dist/shared/GET.d.ts +5 -0
- package/dist/shared/GET.js +8 -0
- package/dist/shared/HttpHeader.d.ts +10 -0
- package/dist/shared/HttpHeader.jsx +38 -0
- package/dist/shared/HttpStatusCode.d.ts +9 -0
- package/dist/shared/HttpStatusCode.js +18 -0
- package/dist/shared/clientOnly.d.ts +12 -0
- package/dist/shared/clientOnly.js +27 -0
- package/dist/shared/dev-overlay/CodeView.d.ts +7 -0
- package/dist/shared/dev-overlay/CodeView.jsx +62 -0
- package/dist/shared/dev-overlay/DevOverlayDialog.d.ts +9 -0
- package/dist/shared/dev-overlay/DevOverlayDialog.jsx +226 -0
- package/dist/shared/dev-overlay/createStackFrame.d.ts +9 -0
- package/dist/shared/dev-overlay/createStackFrame.js +56 -0
- package/dist/shared/dev-overlay/download.d.ts +1 -0
- package/dist/shared/dev-overlay/download.js +6 -0
- package/dist/shared/dev-overlay/get-source-map.d.ts +2 -0
- package/dist/shared/dev-overlay/get-source-map.js +25 -0
- package/dist/shared/dev-overlay/icons.d.ts +28 -0
- package/dist/shared/dev-overlay/icons.jsx +150 -0
- package/dist/shared/dev-overlay/index.d.ts +5 -0
- package/dist/shared/dev-overlay/index.jsx +39 -0
- package/dist/shared/lazy.d.ts +3 -0
- package/dist/shared/lazy.js +42 -0
- package/dist/shared/serverFunction.d.ts +6 -0
- package/dist/shared/serverFunction.js +8 -0
- package/dist/utils/debounce.d.ts +4 -0
- package/dist/utils/debounce.js +10 -0
- package/package.json +7 -2
- package/scripts/build.js +0 -15
- package/scripts/validate-imports.js +0 -316
- 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 -161
- 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/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/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/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/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/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/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/client/index.tsx → dist/client/index.d.ts} +0 -0
- /package/{src → dist}/shared/dev-overlay/styles.css +0 -0
package/src/fns/client.ts
DELETED
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
import { type Component } from "solid-js";
|
|
2
|
-
import {
|
|
3
|
-
// serializeToJSONStream,
|
|
4
|
-
serializeToJSONString,
|
|
5
|
-
} from "./serialization.ts";
|
|
6
|
-
import {
|
|
7
|
-
BODY_FORMAT_KEY,
|
|
8
|
-
BodyFormat,
|
|
9
|
-
extractBody,
|
|
10
|
-
getHeadersAndBody,
|
|
11
|
-
} from "./shared.ts";
|
|
12
|
-
|
|
13
|
-
let INSTANCE = 0;
|
|
14
|
-
|
|
15
|
-
function createRequest(base: string, id: string, instance: string, options: RequestInit) {
|
|
16
|
-
return fetch(base, {
|
|
17
|
-
method: "POST",
|
|
18
|
-
...options,
|
|
19
|
-
headers: {
|
|
20
|
-
...options.headers,
|
|
21
|
-
"X-Server-Id": id,
|
|
22
|
-
"X-Server-Instance": instance,
|
|
23
|
-
},
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
async function initializeResponse(
|
|
28
|
-
base: string,
|
|
29
|
-
id: string,
|
|
30
|
-
instance: string,
|
|
31
|
-
options: RequestInit,
|
|
32
|
-
args: any[],
|
|
33
|
-
) {
|
|
34
|
-
// No args, skip serialization
|
|
35
|
-
if (args.length === 0) {
|
|
36
|
-
return createRequest(base, id, instance, options);
|
|
37
|
-
}
|
|
38
|
-
// For single arguments, we can directly encode as body
|
|
39
|
-
if (args.length === 1) {
|
|
40
|
-
const body = args[0];
|
|
41
|
-
const result = getHeadersAndBody(body);
|
|
42
|
-
if (result) {
|
|
43
|
-
return createRequest(base, id, instance, {
|
|
44
|
-
...options,
|
|
45
|
-
body: result.body,
|
|
46
|
-
headers: {
|
|
47
|
-
...options.headers,
|
|
48
|
-
...result.headers,
|
|
49
|
-
},
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
// Fallback to seroval
|
|
54
|
-
return createRequest(base, id, instance, {
|
|
55
|
-
...options,
|
|
56
|
-
// TODO(Alexis): move to serializeToJSONStream
|
|
57
|
-
body: await serializeToJSONString(args),
|
|
58
|
-
// duplex: 'half',
|
|
59
|
-
// body: serializeToJSONStream(args),
|
|
60
|
-
headers: {
|
|
61
|
-
...options.headers,
|
|
62
|
-
"Content-Type": "text/plain",
|
|
63
|
-
[BODY_FORMAT_KEY]: BodyFormat.Seroval,
|
|
64
|
-
},
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
async function fetchServerFunction(
|
|
69
|
-
base: string,
|
|
70
|
-
id: string,
|
|
71
|
-
options: Omit<RequestInit, "body">,
|
|
72
|
-
args: any[],
|
|
73
|
-
) {
|
|
74
|
-
const instance = `server-fn:${INSTANCE++}`;
|
|
75
|
-
|
|
76
|
-
const response = await initializeResponse(base, id, instance, options, args);
|
|
77
|
-
|
|
78
|
-
if (
|
|
79
|
-
response.headers.has("Location") ||
|
|
80
|
-
response.headers.has("X-Revalidate") ||
|
|
81
|
-
response.headers.has("X-Single-Flight")
|
|
82
|
-
) {
|
|
83
|
-
if (response.body) {
|
|
84
|
-
/* @ts-ignore-next-line */
|
|
85
|
-
response.customBody = async () => {
|
|
86
|
-
return await extractBody(instance, true, response.clone());
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
return response;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
const result = await extractBody(instance, true, response.clone());
|
|
93
|
-
if (response.headers.has("X-Error")) {
|
|
94
|
-
throw result;
|
|
95
|
-
}
|
|
96
|
-
return result;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
export function cloneServerReference(id: string) {
|
|
100
|
-
let baseURL = import.meta.env.BASE_URL ?? "/";
|
|
101
|
-
if (!baseURL.endsWith("/")) baseURL += "/";
|
|
102
|
-
|
|
103
|
-
const fn = (...args: any[]) => fetchServerFunction(`${baseURL}_server`, id, {}, args);
|
|
104
|
-
|
|
105
|
-
return new Proxy(fn, {
|
|
106
|
-
get(target, prop, receiver) {
|
|
107
|
-
if (prop === "url") {
|
|
108
|
-
return `${baseURL}_server?id=${encodeURIComponent(id)}`;
|
|
109
|
-
}
|
|
110
|
-
if (prop === "GET") {
|
|
111
|
-
return receiver.withOptions({ method: "GET" });
|
|
112
|
-
}
|
|
113
|
-
if (prop === "withOptions") {
|
|
114
|
-
const url = `${baseURL}_server?id=${encodeURIComponent(id)}`;
|
|
115
|
-
return (options: RequestInit) => {
|
|
116
|
-
const fn = async (...args: any[]) => {
|
|
117
|
-
const encodeArgs = options.method && options.method.toUpperCase() === "GET";
|
|
118
|
-
return fetchServerFunction(
|
|
119
|
-
encodeArgs
|
|
120
|
-
? url +
|
|
121
|
-
(args.length
|
|
122
|
-
? `&args=${encodeURIComponent(await serializeToJSONString(args))}`
|
|
123
|
-
: "")
|
|
124
|
-
: `${baseURL}_server`,
|
|
125
|
-
id,
|
|
126
|
-
options,
|
|
127
|
-
encodeArgs ? [] : args,
|
|
128
|
-
);
|
|
129
|
-
};
|
|
130
|
-
fn.url = url;
|
|
131
|
-
return fn;
|
|
132
|
-
};
|
|
133
|
-
}
|
|
134
|
-
return (target as any)[prop];
|
|
135
|
-
},
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
export function createClientReference(Component: Component<any>, id: string) {
|
|
140
|
-
return Component;
|
|
141
|
-
}
|
package/src/fns/handler.spec.ts
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it, vi, beforeEach } from "vitest";
|
|
2
|
-
import type { FetchEvent } from "./types.ts";
|
|
3
|
-
|
|
4
|
-
vi.mock("h3", () => ({
|
|
5
|
-
parseCookies: vi.fn(() => ({})),
|
|
6
|
-
parseSetCookie: vi.fn(),
|
|
7
|
-
}));
|
|
8
|
-
|
|
9
|
-
vi.mock("solid-js/web", () => ({
|
|
10
|
-
renderToString: vi.fn(),
|
|
11
|
-
provideRequestEvent: vi.fn((_event, fn) => fn()),
|
|
12
|
-
}));
|
|
13
|
-
|
|
14
|
-
vi.mock("solid-start:server-fn-manifest", () => ({
|
|
15
|
-
getServerFnById: vi.fn(),
|
|
16
|
-
}));
|
|
17
|
-
|
|
18
|
-
vi.mock("./handler.ts", () => ({
|
|
19
|
-
createPageEvent: vi.fn(),
|
|
20
|
-
}));
|
|
21
|
-
|
|
22
|
-
vi.mock("./fetchEvent.ts", () => ({
|
|
23
|
-
getFetchEvent: vi.fn(),
|
|
24
|
-
mergeResponseHeaders: vi.fn(),
|
|
25
|
-
}));
|
|
26
|
-
|
|
27
|
-
function createMockFetchEvent(headers: Record<string, string> = {}): FetchEvent {
|
|
28
|
-
return {
|
|
29
|
-
request: new Request("http://localhost/test", { headers }),
|
|
30
|
-
response: {
|
|
31
|
-
headers: {
|
|
32
|
-
getSetCookie: () => [],
|
|
33
|
-
},
|
|
34
|
-
},
|
|
35
|
-
nativeEvent: {},
|
|
36
|
-
locals: {},
|
|
37
|
-
} as FetchEvent;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
describe("createSingleFlightHeaders", () => {
|
|
41
|
-
let createSingleFlightHeaders: (sourceEvent: FetchEvent) => Headers;
|
|
42
|
-
|
|
43
|
-
beforeEach(async () => {
|
|
44
|
-
vi.clearAllMocks();
|
|
45
|
-
const module = await import("./server-functions-handler.ts");
|
|
46
|
-
createSingleFlightHeaders = module.createSingleFlightHeaders;
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
it("should create a new Headers object instead of returning the original", () => {
|
|
50
|
-
const sourceEvent = createMockFetchEvent({
|
|
51
|
-
"content-type": "application/json",
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
const result = createSingleFlightHeaders(sourceEvent);
|
|
55
|
-
|
|
56
|
-
expect(result).not.toBe(sourceEvent.request.headers);
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
it("should not mutate the original request headers", () => {
|
|
60
|
-
const originalHeaders = new Headers({
|
|
61
|
-
"content-type": "application/json",
|
|
62
|
-
"cookie": "session=abc123",
|
|
63
|
-
"cf-ray": "abc123",
|
|
64
|
-
"cf-cache-status": "HIT",
|
|
65
|
-
});
|
|
66
|
-
const sourceEvent: FetchEvent = {
|
|
67
|
-
request: new Request("http://localhost/test", { headers: originalHeaders }),
|
|
68
|
-
response: {
|
|
69
|
-
headers: {
|
|
70
|
-
getSetCookie: () => [],
|
|
71
|
-
},
|
|
72
|
-
},
|
|
73
|
-
nativeEvent: {},
|
|
74
|
-
locals: {},
|
|
75
|
-
} as FetchEvent;
|
|
76
|
-
|
|
77
|
-
const originalCookieHeader = sourceEvent.request.headers.get("cookie");
|
|
78
|
-
const originalCfRay = sourceEvent.request.headers.get("cf-ray");
|
|
79
|
-
|
|
80
|
-
createSingleFlightHeaders(sourceEvent);
|
|
81
|
-
|
|
82
|
-
expect(sourceEvent.request.headers.get("cookie")).toBe(originalCookieHeader);
|
|
83
|
-
expect(sourceEvent.request.headers.get("cf-ray")).toBe(originalCfRay);
|
|
84
|
-
});
|
|
85
|
-
});
|
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
|
-
}
|