@solidjs/start 2.0.0-alpha.2 → 2.0.0-alpha.3

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.
Files changed (108) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/dist/config/constants.d.ts +1 -1
  3. package/dist/config/constants.js +1 -1
  4. package/dist/config/dev-server.js +9 -1
  5. package/dist/config/env.d.ts +19 -0
  6. package/dist/config/env.js +81 -0
  7. package/dist/config/fs-routes/fs-watcher.js +14 -20
  8. package/dist/config/fs-routes/index.js +2 -2
  9. package/dist/config/fs-routes/router.js +2 -2
  10. package/dist/config/index.d.ts +16 -1
  11. package/dist/config/index.js +25 -27
  12. package/dist/config/lazy.js +2 -2
  13. package/dist/config/manifest.js +37 -4
  14. package/dist/config/vite-utils.d.ts +1 -1
  15. package/dist/directives/bubble-function-declaration.d.ts +3 -0
  16. package/dist/directives/bubble-function-declaration.js +23 -0
  17. package/dist/directives/compile.d.ts +9 -0
  18. package/dist/directives/compile.js +40 -0
  19. package/dist/directives/generate-unique-name.d.ts +3 -0
  20. package/dist/directives/generate-unique-name.js +16 -0
  21. package/dist/directives/get-descriptive-name.d.ts +2 -0
  22. package/dist/directives/get-descriptive-name.js +37 -0
  23. package/dist/directives/get-import-identifier.d.ts +4 -0
  24. package/dist/directives/get-import-identifier.js +19 -0
  25. package/dist/directives/get-root-statement-path.d.ts +2 -0
  26. package/dist/directives/get-root-statement-path.js +12 -0
  27. package/dist/directives/index.d.ts +14 -0
  28. package/dist/directives/index.js +169 -0
  29. package/dist/directives/is-statement-top-level.d.ts +3 -0
  30. package/dist/directives/is-statement-top-level.js +9 -0
  31. package/dist/directives/paths.d.ts +9 -0
  32. package/dist/directives/paths.js +36 -0
  33. package/dist/directives/plugin.d.ts +21 -0
  34. package/dist/directives/plugin.js +302 -0
  35. package/dist/directives/remove-unused-variables.d.ts +3 -0
  36. package/dist/directives/remove-unused-variables.js +53 -0
  37. package/dist/directives/types.d.ts +10 -0
  38. package/dist/directives/xxhash32.d.ts +6 -0
  39. package/dist/directives/xxhash32.js +193 -0
  40. package/dist/{server/server-runtime.d.ts → fns/client.d.ts} +1 -1
  41. package/dist/fns/client.js +106 -0
  42. package/dist/fns/handler.d.ts +5 -0
  43. package/dist/{server/server-functions-handler.js → fns/handler.js} +55 -96
  44. package/dist/fns/registration.d.ts +2 -0
  45. package/dist/fns/registration.js +12 -0
  46. package/dist/fns/serialization.d.ts +6 -0
  47. package/dist/fns/serialization.js +208 -0
  48. package/dist/fns/server.d.ts +7 -0
  49. package/dist/{server/server-fns-runtime.js → fns/server.js} +7 -1
  50. package/dist/fns/shared.d.ts +17 -0
  51. package/dist/fns/shared.js +105 -0
  52. package/dist/http/index.d.ts +1 -1
  53. package/dist/middleware/index.d.ts +17 -0
  54. package/dist/middleware/index.js +15 -0
  55. package/dist/server/handler.d.ts +61 -3
  56. package/dist/server/handler.js +17 -15
  57. package/dist/server/spa/handler.d.ts +30 -1
  58. package/dist/server/types.d.ts +1 -1
  59. package/dist/shared/dev-overlay/CodeView.jsx +4 -4
  60. package/dist/shared/dev-overlay/DevOverlayDialog.jsx +34 -34
  61. package/dist/shared/dev-overlay/index.jsx +2 -2
  62. package/dist/shared/dev-overlay/styles.css +40 -40
  63. package/package.json +27 -20
  64. package/src/config/constants.ts +1 -1
  65. package/src/config/env.ts +105 -0
  66. package/src/config/fs-routes/fs-watcher.ts +17 -24
  67. package/src/config/fs-routes/index.ts +2 -2
  68. package/src/config/fs-routes/router.ts +2 -2
  69. package/src/config/index.ts +39 -37
  70. package/src/config/lazy.ts +2 -2
  71. package/src/config/manifest.ts +37 -7
  72. package/src/config/vite-utils.ts +1 -1
  73. package/src/directives/bubble-function-declaration.ts +33 -0
  74. package/src/directives/compile.ts +54 -0
  75. package/src/directives/generate-unique-name.ts +22 -0
  76. package/src/directives/get-descriptive-name.ts +39 -0
  77. package/src/directives/get-import-identifier.ts +34 -0
  78. package/src/directives/get-root-statement-path.ts +14 -0
  79. package/src/directives/index.ts +232 -0
  80. package/src/directives/is-statement-top-level.ts +13 -0
  81. package/src/directives/paths.ts +63 -0
  82. package/src/directives/plugin.ts +389 -0
  83. package/src/directives/remove-unused-variables.ts +55 -0
  84. package/src/directives/types.ts +12 -0
  85. package/src/directives/xxhash32.ts +214 -0
  86. package/src/env.d.ts +1 -1
  87. package/src/fns/client.ts +141 -0
  88. package/src/fns/handler.spec.ts +85 -0
  89. package/src/{server/server-functions-handler.ts → fns/handler.ts} +65 -117
  90. package/src/fns/registration.ts +19 -0
  91. package/src/fns/serialization.ts +250 -0
  92. package/src/{server/server-fns-runtime.ts → fns/server.ts} +17 -2
  93. package/src/fns/shared.ts +116 -0
  94. package/src/middleware/index.ts +17 -0
  95. package/src/server/handler.ts +18 -16
  96. package/src/server/types.ts +1 -1
  97. package/src/shared/dev-overlay/CodeView.tsx +5 -7
  98. package/src/shared/dev-overlay/DevOverlayDialog.tsx +34 -34
  99. package/src/shared/dev-overlay/get-source-map.ts +1 -1
  100. package/src/shared/dev-overlay/index.tsx +4 -4
  101. package/src/shared/dev-overlay/styles.css +40 -40
  102. package/tsconfig.json +1 -0
  103. package/dist/server/fetchEvent.spec.js +0 -65
  104. package/dist/server/server-fns-runtime.d.ts +0 -1
  105. package/dist/server/server-functions-handler.d.ts +0 -2
  106. package/dist/server/server-runtime.js +0 -194
  107. package/src/server/server-runtime.ts +0 -240
  108. /package/dist/{server/fetchEvent.spec.d.ts → directives/types.js} +0 -0
@@ -0,0 +1,193 @@
1
+ // @ts-nocheck
2
+ /**
3
+ * Copyright (c) 2019 Jason Dent
4
+ * https://github.com/Jason3S/xxhash
5
+ */
6
+ const PRIME32_1 = 2654435761;
7
+ const PRIME32_2 = 2246822519;
8
+ const PRIME32_3 = 3266489917;
9
+ const PRIME32_4 = 668265263;
10
+ const PRIME32_5 = 374761393;
11
+ function toUtf8(text) {
12
+ const bytes = [];
13
+ for (let i = 0, n = text.length; i < n; ++i) {
14
+ const c = text.charCodeAt(i);
15
+ if (c < 0x80) {
16
+ bytes.push(c);
17
+ }
18
+ else if (c < 0x800) {
19
+ bytes.push(0xc0 | (c >> 6), 0x80 | (c & 0x3f));
20
+ }
21
+ else if (c < 0xd800 || c >= 0xe000) {
22
+ bytes.push(0xe0 | (c >> 12), 0x80 | ((c >> 6) & 0x3f), 0x80 | (c & 0x3f));
23
+ }
24
+ else {
25
+ const cp = 0x10000 + (((c & 0x3ff) << 10) | (text.charCodeAt(++i) & 0x3ff));
26
+ bytes.push(0xf0 | ((cp >> 18) & 0x7), 0x80 | ((cp >> 12) & 0x3f), 0x80 | ((cp >> 6) & 0x3f), 0x80 | (cp & 0x3f));
27
+ }
28
+ }
29
+ return new Uint8Array(bytes);
30
+ }
31
+ /**
32
+ *
33
+ * @param buffer - byte array or string
34
+ * @param seed - optional seed (32-bit unsigned);
35
+ */
36
+ export default function xxHash32(buffer, seed = 0) {
37
+ buffer = typeof buffer === 'string' ? toUtf8(buffer) : buffer;
38
+ const b = buffer;
39
+ /*
40
+ Step 1. Initialize internal accumulators
41
+ Each accumulator gets an initial value based on optional seed input.
42
+ Since the seed is optional, it can be 0.
43
+ ```
44
+ u32 acc1 = seed + PRIME32_1 + PRIME32_2;
45
+ u32 acc2 = seed + PRIME32_2;
46
+ u32 acc3 = seed + 0;
47
+ u32 acc4 = seed - PRIME32_1;
48
+ ```
49
+ Special case : input is less than 16 bytes
50
+ When input is too small (< 16 bytes), the algorithm will not process any stripe.
51
+ Consequently, it will not make use of parallel accumulators.
52
+ In which case, a simplified initialization is performed, using a single accumulator :
53
+ u32 acc = seed + PRIME32_5;
54
+ The algorithm then proceeds directly to step 4.
55
+ */
56
+ let acc = (seed + PRIME32_5) & 0xffffffff;
57
+ let offset = 0;
58
+ if (b.length >= 16) {
59
+ const accN = [
60
+ (seed + PRIME32_1 + PRIME32_2) & 0xffffffff,
61
+ (seed + PRIME32_2) & 0xffffffff,
62
+ (seed + 0) & 0xffffffff,
63
+ (seed - PRIME32_1) & 0xffffffff,
64
+ ];
65
+ /*
66
+ Step 2. Process stripes
67
+ A stripe is a contiguous segment of 16 bytes. It is evenly divided into 4 lanes,
68
+ of 4 bytes each. The first lane is used to update accumulator 1, the second lane
69
+ is used to update accumulator 2, and so on. Each lane read its associated 32-bit
70
+ value using little-endian convention. For each {lane, accumulator}, the update
71
+ process is called a round, and applies the following formula :
72
+ ```
73
+ accN = accN + (laneN * PRIME32_2);
74
+ accN = accN <<< 13;
75
+ accN = accN * PRIME32_1;
76
+ ```
77
+ This shuffles the bits so that any bit from input lane impacts several bits in
78
+ output accumulator. All operations are performed modulo 2^32.
79
+ Input is consumed one full stripe at a time. Step 2 is looped as many times as
80
+ necessary to consume the whole input, except the last remaining bytes which cannot
81
+ form a stripe (< 16 bytes). When that happens, move to step 3.
82
+ */
83
+ const b = buffer;
84
+ const limit = b.length - 16;
85
+ let lane = 0;
86
+ for (offset = 0; (offset & 0xfffffff0) <= limit; offset += 4) {
87
+ const i = offset;
88
+ const laneN0 = b[i + 0] + (b[i + 1] << 8);
89
+ const laneN1 = b[i + 2] + (b[i + 3] << 8);
90
+ const laneNP = laneN0 * PRIME32_2 + ((laneN1 * PRIME32_2) << 16);
91
+ let acc = (accN[lane] + laneNP) & 0xffffffff;
92
+ acc = (acc << 13) | (acc >>> 19);
93
+ const acc0 = acc & 0xffff;
94
+ const acc1 = acc >>> 16;
95
+ accN[lane] = (acc0 * PRIME32_1 + ((acc1 * PRIME32_1) << 16)) & 0xffffffff;
96
+ lane = (lane + 1) & 0x3;
97
+ }
98
+ /*
99
+ Step 3. Accumulator convergence
100
+ All 4 lane accumulators from previous steps are merged to produce a
101
+ single remaining accumulator
102
+ of same width (32-bit). The associated formula is as follows :
103
+ ```
104
+ acc = (acc1 <<< 1) + (acc2 <<< 7) + (acc3 <<< 12) + (acc4 <<< 18);
105
+ ```
106
+ */
107
+ acc =
108
+ (((accN[0] << 1) | (accN[0] >>> 31)) +
109
+ ((accN[1] << 7) | (accN[1] >>> 25)) +
110
+ ((accN[2] << 12) | (accN[2] >>> 20)) +
111
+ ((accN[3] << 18) | (accN[3] >>> 14))) &
112
+ 0xffffffff;
113
+ }
114
+ /*
115
+ Step 4. Add input length
116
+ The input total length is presumed known at this stage.
117
+ This step is just about adding the length to
118
+ accumulator, so that it participates to final mixing.
119
+ ```
120
+ acc = acc + (u32)inputLength;
121
+ ```
122
+ */
123
+ acc = (acc + buffer.length) & 0xffffffff;
124
+ /*
125
+ Step 5. Consume remaining input
126
+ There may be up to 15 bytes remaining to consume from the input.
127
+ The final stage will digest them according
128
+ to following pseudo-code :
129
+ ```
130
+ while (remainingLength >= 4) {
131
+ lane = read_32bit_little_endian(input_ptr);
132
+ acc = acc + lane * PRIME32_3;
133
+ acc = (acc <<< 17) * PRIME32_4;
134
+ input_ptr += 4; remainingLength -= 4;
135
+ }
136
+ ```
137
+ This process ensures that all input bytes are present in the final mix.
138
+ */
139
+ const limit = buffer.length - 4;
140
+ for (; offset <= limit; offset += 4) {
141
+ const i = offset;
142
+ const laneN0 = b[i + 0] + (b[i + 1] << 8);
143
+ const laneN1 = b[i + 2] + (b[i + 3] << 8);
144
+ const laneP = laneN0 * PRIME32_3 + ((laneN1 * PRIME32_3) << 16);
145
+ acc = (acc + laneP) & 0xffffffff;
146
+ acc = (acc << 17) | (acc >>> 15);
147
+ acc =
148
+ ((acc & 0xffff) * PRIME32_4 + (((acc >>> 16) * PRIME32_4) << 16)) &
149
+ 0xffffffff;
150
+ }
151
+ /*
152
+ ```
153
+ while (remainingLength >= 1) {
154
+ lane = read_byte(input_ptr);
155
+ acc = acc + lane * PRIME32_5;
156
+ acc = (acc <<< 11) * PRIME32_1;
157
+ input_ptr += 1; remainingLength -= 1;
158
+ }
159
+ ```
160
+ */
161
+ for (; offset < b.length; ++offset) {
162
+ const lane = b[offset];
163
+ acc += lane * PRIME32_5;
164
+ acc = (acc << 11) | (acc >>> 21);
165
+ acc =
166
+ ((acc & 0xffff) * PRIME32_1 + (((acc >>> 16) * PRIME32_1) << 16)) &
167
+ 0xffffffff;
168
+ }
169
+ /*
170
+ Step 6. Final mix (avalanche)
171
+ The final mix ensures that all input bits have a chance to impact any bit in
172
+ the output digest, resulting in an unbiased distribution. This is also called
173
+ avalanche effect.
174
+ ```
175
+ acc = acc xor (acc >> 15);
176
+ acc = acc * PRIME32_2;
177
+ acc = acc xor (acc >> 13);
178
+ acc = acc * PRIME32_3;
179
+ acc = acc xor (acc >> 16);
180
+ ```
181
+ */
182
+ acc ^= acc >>> 15;
183
+ acc =
184
+ (((acc & 0xffff) * PRIME32_2) & 0xffffffff) +
185
+ (((acc >>> 16) * PRIME32_2) << 16);
186
+ acc ^= acc >>> 13;
187
+ acc =
188
+ (((acc & 0xffff) * PRIME32_3) & 0xffffffff) +
189
+ (((acc >>> 16) * PRIME32_3) << 16);
190
+ acc ^= acc >>> 16;
191
+ // turn any negatives back into a positive number;
192
+ return acc < 0 ? acc + 4294967296 : acc;
193
+ }
@@ -1,3 +1,3 @@
1
1
  import { type Component } from "solid-js";
2
- export declare function createServerReference(id: string): (...args: any[]) => Promise<any>;
2
+ export declare function cloneServerReference(id: string): (...args: any[]) => Promise<unknown>;
3
3
  export declare function createClientReference(Component: Component<any>, id: string): Component<any>;
@@ -0,0 +1,106 @@
1
+ import {
2
+ // serializeToJSONStream,
3
+ serializeToJSONString, } from "./serialization.js";
4
+ import { BODY_FORMAT_KEY, BodyFormat, extractBody, getHeadersAndBody, } from "./shared.js";
5
+ let INSTANCE = 0;
6
+ function createRequest(base, id, instance, options) {
7
+ return fetch(base, {
8
+ method: "POST",
9
+ ...options,
10
+ headers: {
11
+ ...options.headers,
12
+ "X-Server-Id": id,
13
+ "X-Server-Instance": instance,
14
+ },
15
+ });
16
+ }
17
+ async function initializeResponse(base, id, instance, options, args) {
18
+ // No args, skip serialization
19
+ if (args.length === 0) {
20
+ return createRequest(base, id, instance, options);
21
+ }
22
+ // For single arguments, we can directly encode as body
23
+ if (args.length === 1) {
24
+ const body = args[0];
25
+ const result = getHeadersAndBody(body);
26
+ if (result) {
27
+ return createRequest(base, id, instance, {
28
+ ...options,
29
+ body: result.body,
30
+ headers: {
31
+ ...options.headers,
32
+ ...result.headers,
33
+ },
34
+ });
35
+ }
36
+ }
37
+ // Fallback to seroval
38
+ return createRequest(base, id, instance, {
39
+ ...options,
40
+ // TODO(Alexis): move to serializeToJSONStream
41
+ body: await serializeToJSONString(args),
42
+ // duplex: 'half',
43
+ // body: serializeToJSONStream(args),
44
+ headers: {
45
+ ...options.headers,
46
+ "Content-Type": "text/plain",
47
+ [BODY_FORMAT_KEY]: BodyFormat.Seroval,
48
+ },
49
+ });
50
+ }
51
+ async function fetchServerFunction(base, id, options, args) {
52
+ const instance = `server-fn:${INSTANCE++}`;
53
+ const response = await initializeResponse(base, id, instance, options, args);
54
+ if (response.headers.has("Location") ||
55
+ response.headers.has("X-Revalidate") ||
56
+ response.headers.has("X-Single-Flight")) {
57
+ if (response.body) {
58
+ /* @ts-ignore-next-line */
59
+ response.customBody = async () => {
60
+ return await extractBody(instance, true, response.clone());
61
+ };
62
+ }
63
+ return response;
64
+ }
65
+ const result = await extractBody(instance, true, response.clone());
66
+ if (response.headers.has("X-Error")) {
67
+ throw result;
68
+ }
69
+ return result;
70
+ }
71
+ export function cloneServerReference(id) {
72
+ let baseURL = import.meta.env.BASE_URL ?? "/";
73
+ if (!baseURL.endsWith("/"))
74
+ baseURL += "/";
75
+ const fn = (...args) => fetchServerFunction(`${baseURL}_server`, id, {}, args);
76
+ return new Proxy(fn, {
77
+ get(target, prop, receiver) {
78
+ if (prop === "url") {
79
+ return `${baseURL}_server?id=${encodeURIComponent(id)}`;
80
+ }
81
+ if (prop === "GET") {
82
+ return receiver.withOptions({ method: "GET" });
83
+ }
84
+ if (prop === "withOptions") {
85
+ const url = `${baseURL}_server?id=${encodeURIComponent(id)}`;
86
+ return (options) => {
87
+ const fn = async (...args) => {
88
+ const encodeArgs = options.method && options.method.toUpperCase() === "GET";
89
+ return fetchServerFunction(encodeArgs
90
+ ? url +
91
+ (args.length
92
+ ? `&args=${encodeURIComponent(await serializeToJSONString(args))}`
93
+ : "")
94
+ : `${baseURL}_server`, id, options, encodeArgs ? [] : args);
95
+ };
96
+ fn.url = url;
97
+ return fn;
98
+ };
99
+ }
100
+ return target[prop];
101
+ },
102
+ });
103
+ }
104
+ export function createClientReference(Component, id) {
105
+ return Component;
106
+ }
@@ -0,0 +1,5 @@
1
+ import { type H3Event } from "h3";
2
+ import "solid-start:server-fn-manifest";
3
+ import type { FetchEvent } from "../server/types.ts";
4
+ export declare function handleServerFunction(h3Event: H3Event): Promise<unknown>;
5
+ export declare function createSingleFlightHeaders(sourceEvent: FetchEvent): Headers;
@@ -1,55 +1,15 @@
1
- import { getServerFnById } from "solidstart:server-fn-manifest";
2
1
  import { parseSetCookie } from "cookie-es";
3
2
  import { parseCookies } from "h3";
4
- import { crossSerializeStream, fromJSON, getCrossReferenceHeader } from "seroval";
5
- import { CustomEventPlugin, DOMExceptionPlugin, EventPlugin, FormDataPlugin, HeadersPlugin, ReadableStreamPlugin, RequestPlugin, ResponsePlugin, URLPlugin, URLSearchParamsPlugin, } from "seroval-plugins/web";
6
3
  import { sharedConfig } from "solid-js";
7
4
  import { renderToString } from "solid-js/web";
8
5
  import { provideRequestEvent } from "solid-js/web/storage";
9
- import { getFetchEvent, mergeResponseHeaders } from "./fetchEvent.js";
10
- import { createPageEvent } from "./handler.js";
11
- import { getExpectedRedirectStatus } from "./util.js";
12
- function createChunk(data) {
13
- const encodeData = new TextEncoder().encode(data);
14
- const bytes = encodeData.length;
15
- const baseHex = bytes.toString(16);
16
- const totalHex = "00000000".substring(0, 8 - baseHex.length) + baseHex; // 32-bit
17
- const head = new TextEncoder().encode(`;0x${totalHex};`);
18
- const chunk = new Uint8Array(12 + bytes);
19
- chunk.set(head);
20
- chunk.set(encodeData, 12);
21
- return chunk;
22
- }
23
- function serializeToStream(id, value) {
24
- return new ReadableStream({
25
- start(controller) {
26
- crossSerializeStream(value, {
27
- scopeId: id,
28
- plugins: [
29
- CustomEventPlugin,
30
- DOMExceptionPlugin,
31
- EventPlugin,
32
- FormDataPlugin,
33
- HeadersPlugin,
34
- ReadableStreamPlugin,
35
- RequestPlugin,
36
- ResponsePlugin,
37
- URLSearchParamsPlugin,
38
- URLPlugin,
39
- ],
40
- onSerialize(data, initial) {
41
- controller.enqueue(createChunk(initial ? `(${getCrossReferenceHeader(id)},${data})` : data));
42
- },
43
- onDone() {
44
- controller.close();
45
- },
46
- onError(error) {
47
- controller.error(error);
48
- },
49
- });
50
- },
51
- });
52
- }
6
+ import { getFetchEvent, mergeResponseHeaders } from "../server/fetchEvent.js";
7
+ import { createPageEvent } from "../server/handler.js";
8
+ import { deserializeFromJSONString, serializeToJSONStream, serializeToJSStream, } from "./serialization.js";
9
+ import { BODY_FORMAT_KEY, BodyFormat, extractBody, getHeadersAndBody, } from "./shared.js";
10
+ import "solid-start:server-fn-manifest";
11
+ import { getServerFunction } from "./registration.js";
12
+ import { getExpectedRedirectStatus } from "../server/util.js";
53
13
  export async function handleServerFunction(h3Event) {
54
14
  const event = getFetchEvent(h3Event);
55
15
  const request = event.request;
@@ -70,54 +30,26 @@ export async function handleServerFunction(h3Event) {
70
30
  : new Response(null, { status: 404 });
71
31
  }
72
32
  }
73
- const serverFunction = await getServerFnById(functionId);
33
+ const serverFunction = getServerFunction(functionId);
74
34
  let parsed = [];
75
35
  // grab bound arguments from url when no JS
76
- if (!instance || h3Event.method === "GET") {
36
+ if (!instance || request.method === "GET") {
77
37
  const args = url.searchParams.get("args");
78
38
  if (args) {
79
- const json = JSON.parse(args);
80
- (json.t
81
- ? fromJSON(json, {
82
- plugins: [
83
- CustomEventPlugin,
84
- DOMExceptionPlugin,
85
- EventPlugin,
86
- FormDataPlugin,
87
- HeadersPlugin,
88
- ReadableStreamPlugin,
89
- RequestPlugin,
90
- ResponsePlugin,
91
- URLSearchParamsPlugin,
92
- URLPlugin,
93
- ],
94
- })
95
- : json).forEach((arg) => {
39
+ const result = (await deserializeFromJSONString(args));
40
+ for (const arg of result) {
96
41
  parsed.push(arg);
97
- });
42
+ }
98
43
  }
99
44
  }
100
- if (h3Event.method === "POST") {
101
- const contentType = request.headers.get("content-type");
102
- if (contentType?.startsWith("multipart/form-data") ||
103
- contentType?.startsWith("application/x-www-form-urlencoded")) {
104
- parsed.push(await event.request.formData());
45
+ if (request.method === "POST" && request.body !== null) {
46
+ const bodyFormat = request.headers.get(BODY_FORMAT_KEY);
47
+ const decoded = await extractBody("", false, request.clone());
48
+ if (bodyFormat === BodyFormat.Seroval) {
49
+ parsed = decoded;
105
50
  }
106
- else if (contentType?.startsWith("application/json")) {
107
- parsed = fromJSON(await event.request.json(), {
108
- plugins: [
109
- CustomEventPlugin,
110
- DOMExceptionPlugin,
111
- EventPlugin,
112
- FormDataPlugin,
113
- HeadersPlugin,
114
- ReadableStreamPlugin,
115
- RequestPlugin,
116
- ResponsePlugin,
117
- URLSearchParamsPlugin,
118
- URLPlugin,
119
- ],
120
- });
51
+ else {
52
+ parsed.push(decoded);
121
53
  }
122
54
  }
123
55
  try {
@@ -146,15 +78,26 @@ export async function handleServerFunction(h3Event) {
146
78
  if (result.customBody) {
147
79
  result = await result.customBody();
148
80
  }
149
- else if (result.body == undefined)
81
+ else if (result.body == null)
150
82
  result = null;
151
83
  }
152
84
  }
153
85
  // handle no JS success case
154
86
  if (!instance)
155
87
  return handleNoJS(result, request, parsed);
156
- h3Event.res.headers.set("content-type", "text/javascript");
157
- return serializeToStream(instance, result);
88
+ const body = getHeadersAndBody(result);
89
+ if (body) {
90
+ return new Response(body.body, {
91
+ headers: body.headers,
92
+ });
93
+ }
94
+ h3Event.res.headers.set(BODY_FORMAT_KEY, BodyFormat.Seroval);
95
+ if (import.meta.env.SEROVAL_MODE === "js") {
96
+ h3Event.res.headers.set("content-type", "text/javascript");
97
+ return serializeToJSStream(instance, result);
98
+ }
99
+ h3Event.res.headers.set("content-type", "text/plain");
100
+ return serializeToJSONStream(result);
158
101
  }
159
102
  catch (x) {
160
103
  if (x instanceof Response) {
@@ -168,9 +111,9 @@ export async function handleServerFunction(h3Event) {
168
111
  if (x.status && (!instance || x.status < 300 || x.status >= 400))
169
112
  h3Event.res.status = x.status;
170
113
  if (x.customBody) {
171
- x = x.customBody();
114
+ x = await x.customBody();
172
115
  }
173
- else if (x.body === undefined)
116
+ else if (x.body == null)
174
117
  x = null;
175
118
  h3Event.res.headers.set("X-Error", "true");
176
119
  }
@@ -182,8 +125,24 @@ export async function handleServerFunction(h3Event) {
182
125
  x = handleNoJS(x, request, parsed, true);
183
126
  }
184
127
  if (instance) {
185
- h3Event.res.headers.set("content-type", "text/javascript");
186
- return serializeToStream(instance, x);
128
+ const body = getHeadersAndBody(x);
129
+ if (body) {
130
+ const headers = new Headers(body.headers);
131
+ const errorHeader = h3Event.res.headers.get("X-Error");
132
+ if (errorHeader !== null) {
133
+ headers.set("X-Error", errorHeader);
134
+ }
135
+ return new Response(body.body, {
136
+ headers: body.headers,
137
+ });
138
+ }
139
+ h3Event.res.headers.set(BODY_FORMAT_KEY, BodyFormat.Seroval);
140
+ if (import.meta.env.SEROVAL_MODE === "js") {
141
+ h3Event.res.headers.set("content-type", "text/javascript");
142
+ return serializeToJSStream(instance, x);
143
+ }
144
+ h3Event.res.headers.set("content-type", "text/plain");
145
+ return serializeToJSONStream(x);
187
146
  }
188
147
  return x;
189
148
  }
@@ -219,11 +178,11 @@ function handleNoJS(result, request, parsed, thrown) {
219
178
  });
220
179
  }
221
180
  let App;
222
- function createSingleFlightHeaders(sourceEvent) {
181
+ export function createSingleFlightHeaders(sourceEvent) {
223
182
  // cookie handling logic is pretty simplistic so this might be imperfect
224
183
  // unclear if h3 internals are available on all platforms but we need a way to
225
184
  // update request headers on the underlying H3 event.
226
- const headers = sourceEvent.request.headers;
185
+ const headers = new Headers(sourceEvent.request.headers);
227
186
  const cookies = parseCookies(sourceEvent.nativeEvent);
228
187
  const SetCookies = sourceEvent.response.headers.getSetCookie();
229
188
  headers.delete("cookie");
@@ -0,0 +1,2 @@
1
+ export declare function registerServerFunction<T extends any[], R>(id: string, callback: (...args: T) => Promise<R>): (...args: T) => Promise<R>;
2
+ export declare function getServerFunction<T extends any[], R>(id: string): ((...args: T) => Promise<R>);
@@ -0,0 +1,12 @@
1
+ const REGISTRATIONS = new Map();
2
+ export function registerServerFunction(id, callback) {
3
+ REGISTRATIONS.set(id, callback);
4
+ return callback;
5
+ }
6
+ export function getServerFunction(id) {
7
+ const fn = REGISTRATIONS.get(id);
8
+ if (fn) {
9
+ return fn;
10
+ }
11
+ throw new Error('invalid server function: ' + id);
12
+ }
@@ -0,0 +1,6 @@
1
+ export declare function serializeToJSStream(id: string, value: any): ReadableStream<any>;
2
+ export declare function serializeToJSONStream(value: any): ReadableStream<any>;
3
+ export declare function serializeToJSONString(value: any): Promise<string>;
4
+ export declare function deserializeFromJSONString(json: string): Promise<unknown>;
5
+ export declare function deserializeJSONStream(response: Response | Request): Promise<unknown>;
6
+ export declare function deserializeJSStream(id: string, response: Request | Response): Promise<unknown>;