@solidjs/start 2.0.0-beta.1 → 2.0.0-beta.10

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 (238) hide show
  1. package/CHANGELOG.md +64 -0
  2. package/dist/client/StartClient.d.ts +7 -0
  3. package/{src/client/StartClient.tsx → dist/client/StartClient.jsx} +7 -15
  4. package/dist/client/index.jsx +2 -0
  5. package/dist/client/mount.d.ts +1 -0
  6. package/dist/client/mount.js +1 -0
  7. package/dist/client/spa/index.d.ts +4 -0
  8. package/dist/client/spa/index.jsx +5 -0
  9. package/dist/config/boundary-modules.d.ts +14 -0
  10. package/dist/config/boundary-modules.js +41 -0
  11. package/dist/config/constants.d.ts +16 -0
  12. package/dist/config/constants.js +16 -0
  13. package/dist/config/dev-server.d.ts +4 -0
  14. package/dist/config/dev-server.js +146 -0
  15. package/dist/config/env.d.ts +19 -0
  16. package/dist/config/env.js +85 -0
  17. package/dist/config/fs-router.d.ts +37 -0
  18. package/dist/config/fs-router.js +148 -0
  19. package/dist/config/fs-routes/fs-watcher.d.ts +3 -0
  20. package/dist/config/fs-routes/fs-watcher.js +53 -0
  21. package/dist/config/fs-routes/index.d.ts +7 -0
  22. package/dist/config/fs-routes/index.js +107 -0
  23. package/dist/config/fs-routes/router.d.ts +43 -0
  24. package/dist/config/fs-routes/router.js +146 -0
  25. package/dist/config/fs-routes/tree-shake.d.ts +2 -0
  26. package/dist/config/fs-routes/tree-shake.js +305 -0
  27. package/dist/config/index.d.ts +25 -0
  28. package/dist/config/index.js +252 -0
  29. package/dist/config/lazy.d.ts +3 -0
  30. package/dist/config/lazy.js +114 -0
  31. package/dist/config/manifest.d.ts +3 -0
  32. package/dist/config/manifest.js +109 -0
  33. package/dist/config/utils.d.ts +4 -0
  34. package/dist/config/utils.js +6 -0
  35. package/dist/config/vite-utils.d.ts +21 -0
  36. package/dist/config/vite-utils.js +85 -0
  37. package/dist/directives/bubble-function-declaration.d.ts +3 -0
  38. package/dist/directives/bubble-function-declaration.js +23 -0
  39. package/dist/directives/compile.d.ts +9 -0
  40. package/dist/directives/compile.js +40 -0
  41. package/dist/directives/generate-unique-name.d.ts +3 -0
  42. package/dist/directives/generate-unique-name.js +16 -0
  43. package/dist/directives/get-descriptive-name.d.ts +2 -0
  44. package/dist/directives/get-descriptive-name.js +37 -0
  45. package/dist/directives/get-import-identifier.d.ts +4 -0
  46. package/dist/directives/get-import-identifier.js +19 -0
  47. package/dist/directives/get-root-statement-path.d.ts +2 -0
  48. package/dist/directives/get-root-statement-path.js +12 -0
  49. package/dist/directives/index.d.ts +14 -0
  50. package/dist/directives/index.js +202 -0
  51. package/dist/directives/is-statement-top-level.d.ts +3 -0
  52. package/dist/directives/is-statement-top-level.js +9 -0
  53. package/dist/directives/paths.d.ts +9 -0
  54. package/dist/directives/paths.js +36 -0
  55. package/dist/directives/plugin.d.ts +21 -0
  56. package/dist/directives/plugin.js +302 -0
  57. package/dist/directives/remove-unused-variables.d.ts +3 -0
  58. package/dist/directives/remove-unused-variables.js +66 -0
  59. package/{src/directives/types.ts → dist/directives/types.d.ts} +5 -7
  60. package/dist/directives/types.js +1 -0
  61. package/dist/directives/xxhash32.d.ts +6 -0
  62. package/dist/directives/xxhash32.js +193 -0
  63. package/dist/fns/client.d.ts +3 -0
  64. package/dist/fns/client.js +106 -0
  65. package/dist/fns/handler.d.ts +5 -0
  66. package/dist/fns/handler.js +305 -0
  67. package/dist/fns/registration.d.ts +3 -0
  68. package/dist/fns/registration.js +15 -0
  69. package/dist/fns/serialization.d.ts +6 -0
  70. package/dist/fns/serialization.js +208 -0
  71. package/dist/fns/server.d.ts +7 -0
  72. package/dist/fns/server.js +38 -0
  73. package/dist/fns/shared.d.ts +17 -0
  74. package/dist/fns/shared.js +105 -0
  75. package/dist/http/http.d.ts +81 -0
  76. package/dist/http/http.js +129 -0
  77. package/dist/http/index.d.ts +2 -0
  78. package/dist/http/index.js +6 -0
  79. package/{src/index.tsx → dist/index.d.ts} +0 -2
  80. package/dist/index.jsx +6 -0
  81. package/dist/middleware/index.d.ts +39 -0
  82. package/dist/middleware/index.js +60 -0
  83. package/dist/router.d.ts +6 -0
  84. package/dist/router.jsx +29 -0
  85. package/dist/server/StartServer.d.ts +9 -0
  86. package/dist/server/StartServer.jsx +34 -0
  87. package/dist/server/assets/PatchVirtualDevStyles.d.ts +4 -0
  88. package/{src/server/assets/PatchVirtualDevStyles.tsx → dist/server/assets/PatchVirtualDevStyles.jsx} +7 -10
  89. package/dist/server/assets/index.d.ts +2 -0
  90. package/dist/server/assets/index.js +52 -0
  91. package/dist/server/assets/render.d.ts +19 -0
  92. package/dist/server/assets/render.jsx +14 -0
  93. package/dist/server/collect-styles.d.ts +3 -0
  94. package/dist/server/collect-styles.js +57 -0
  95. package/dist/server/fetchEvent.d.ts +7 -0
  96. package/dist/server/fetchEvent.js +26 -0
  97. package/dist/server/handler.d.ts +6 -0
  98. package/dist/server/handler.js +218 -0
  99. package/dist/server/index.d.ts +9 -0
  100. package/{src/server/index.tsx → dist/server/index.jsx} +4 -19
  101. package/dist/server/manifest/client-manifest.d.ts +9 -0
  102. package/dist/server/manifest/client-manifest.js +6 -0
  103. package/dist/server/manifest/dev-client-manifest.d.ts +4 -0
  104. package/dist/server/manifest/dev-client-manifest.js +24 -0
  105. package/dist/server/manifest/dev-ssr-manifest.d.ts +5 -0
  106. package/dist/server/manifest/dev-ssr-manifest.js +23 -0
  107. package/dist/server/manifest/prod-client-manifest.d.ts +5 -0
  108. package/dist/server/manifest/prod-client-manifest.js +26 -0
  109. package/dist/server/manifest/prod-ssr-manifest.d.ts +6 -0
  110. package/dist/server/manifest/prod-ssr-manifest.js +85 -0
  111. package/dist/server/manifest/ssr-manifest.d.ts +4 -0
  112. package/dist/server/manifest/ssr-manifest.js +6 -0
  113. package/dist/server/routes.d.ts +23 -0
  114. package/dist/server/routes.js +74 -0
  115. package/dist/server/spa/StartServer.d.ts +9 -0
  116. package/dist/server/spa/StartServer.jsx +25 -0
  117. package/dist/server/spa/handler.d.ts +8 -0
  118. package/dist/server/spa/handler.js +20 -0
  119. package/{src/server/spa/index.tsx → dist/server/spa/index.d.ts} +0 -1
  120. package/dist/server/spa/index.jsx +5 -0
  121. package/dist/server/types.d.ts +75 -0
  122. package/dist/server/types.js +1 -0
  123. package/dist/server/util.d.ts +2 -0
  124. package/dist/server/util.js +8 -0
  125. package/dist/server/web-stream.d.ts +9 -0
  126. package/dist/server/web-stream.js +26 -0
  127. package/dist/shared/ErrorBoundary.d.ts +3 -0
  128. package/dist/shared/ErrorBoundary.jsx +36 -0
  129. package/dist/shared/GET.d.ts +5 -0
  130. package/dist/shared/GET.js +8 -0
  131. package/dist/shared/HttpHeader.d.ts +10 -0
  132. package/dist/shared/HttpHeader.jsx +38 -0
  133. package/dist/shared/HttpStatusCode.d.ts +9 -0
  134. package/dist/shared/HttpStatusCode.js +18 -0
  135. package/dist/shared/clientOnly.d.ts +12 -0
  136. package/dist/shared/clientOnly.js +27 -0
  137. package/dist/shared/dev-overlay/CodeView.d.ts +7 -0
  138. package/dist/shared/dev-overlay/CodeView.jsx +62 -0
  139. package/dist/shared/dev-overlay/DevOverlayDialog.d.ts +9 -0
  140. package/dist/shared/dev-overlay/DevOverlayDialog.jsx +226 -0
  141. package/dist/shared/dev-overlay/createStackFrame.d.ts +9 -0
  142. package/dist/shared/dev-overlay/createStackFrame.js +56 -0
  143. package/dist/shared/dev-overlay/download.d.ts +1 -0
  144. package/dist/shared/dev-overlay/download.js +6 -0
  145. package/dist/shared/dev-overlay/get-source-map.d.ts +2 -0
  146. package/dist/shared/dev-overlay/get-source-map.js +25 -0
  147. package/dist/shared/dev-overlay/icons.d.ts +28 -0
  148. package/dist/shared/dev-overlay/icons.jsx +150 -0
  149. package/dist/shared/dev-overlay/index.d.ts +5 -0
  150. package/dist/shared/dev-overlay/index.jsx +39 -0
  151. package/dist/shared/lazy.d.ts +3 -0
  152. package/dist/shared/lazy.js +42 -0
  153. package/dist/shared/serverFunction.d.ts +6 -0
  154. package/dist/shared/serverFunction.js +8 -0
  155. package/dist/utils/debounce.d.ts +4 -0
  156. package/dist/utils/debounce.js +10 -0
  157. package/env.d.ts +12 -0
  158. package/package.json +10 -5
  159. package/scripts/build.js +0 -15
  160. package/scripts/validate-imports.js +0 -316
  161. package/src/client/mount.ts +0 -1
  162. package/src/client/spa/index.tsx +0 -9
  163. package/src/config/constants.ts +0 -19
  164. package/src/config/dev-server.ts +0 -161
  165. package/src/config/env.ts +0 -105
  166. package/src/config/fs-router.ts +0 -162
  167. package/src/config/fs-routes/fs-watcher.ts +0 -66
  168. package/src/config/fs-routes/index.ts +0 -136
  169. package/src/config/fs-routes/router.ts +0 -184
  170. package/src/config/fs-routes/tree-shake.ts +0 -324
  171. package/src/config/index.ts +0 -239
  172. package/src/config/lazy.ts +0 -132
  173. package/src/config/manifest.ts +0 -120
  174. package/src/config/utils.ts +0 -5
  175. package/src/config/vite-utils.ts +0 -113
  176. package/src/directives/bubble-function-declaration.ts +0 -33
  177. package/src/directives/compile.ts +0 -54
  178. package/src/directives/generate-unique-name.ts +0 -22
  179. package/src/directives/get-descriptive-name.ts +0 -39
  180. package/src/directives/get-import-identifier.ts +0 -34
  181. package/src/directives/get-root-statement-path.ts +0 -14
  182. package/src/directives/index.ts +0 -233
  183. package/src/directives/is-statement-top-level.ts +0 -13
  184. package/src/directives/paths.ts +0 -63
  185. package/src/directives/plugin.ts +0 -389
  186. package/src/directives/remove-unused-variables.ts +0 -70
  187. package/src/directives/xxhash32.ts +0 -214
  188. package/src/env.d.ts +0 -17
  189. package/src/fns/client.ts +0 -141
  190. package/src/fns/handler.spec.ts +0 -85
  191. package/src/fns/handler.ts +0 -287
  192. package/src/fns/registration.ts +0 -19
  193. package/src/fns/serialization.ts +0 -250
  194. package/src/fns/server.ts +0 -46
  195. package/src/fns/shared.ts +0 -116
  196. package/src/http/index.ts +0 -216
  197. package/src/internal.d.ts +0 -14
  198. package/src/middleware/index.ts +0 -92
  199. package/src/router.tsx +0 -37
  200. package/src/server/StartServer.tsx +0 -58
  201. package/src/server/assets/index.ts +0 -70
  202. package/src/server/assets/render.tsx +0 -46
  203. package/src/server/collect-styles.ts +0 -70
  204. package/src/server/fetchEvent.spec.ts +0 -85
  205. package/src/server/fetchEvent.ts +0 -36
  206. package/src/server/handler.ts +0 -261
  207. package/src/server/manifest/client-manifest.ts +0 -8
  208. package/src/server/manifest/dev-client-manifest.ts +0 -21
  209. package/src/server/manifest/dev-ssr-manifest.ts +0 -23
  210. package/src/server/manifest/prod-client-manifest.ts +0 -21
  211. package/src/server/manifest/prod-ssr-manifest.ts +0 -109
  212. package/src/server/manifest/ssr-manifest.ts +0 -10
  213. package/src/server/routes.ts +0 -126
  214. package/src/server/spa/StartServer.tsx +0 -42
  215. package/src/server/spa/handler.ts +0 -29
  216. package/src/server/types.ts +0 -82
  217. package/src/server/util.ts +0 -12
  218. package/src/shared/ErrorBoundary.tsx +0 -52
  219. package/src/shared/GET.ts +0 -8
  220. package/src/shared/HttpHeader.tsx +0 -42
  221. package/src/shared/HttpStatusCode.ts +0 -27
  222. package/src/shared/clientOnly.ts +0 -44
  223. package/src/shared/dev-overlay/CodeView.tsx +0 -90
  224. package/src/shared/dev-overlay/DevOverlayDialog.tsx +0 -316
  225. package/src/shared/dev-overlay/createStackFrame.ts +0 -74
  226. package/src/shared/dev-overlay/download.ts +0 -6
  227. package/src/shared/dev-overlay/env.d.ts +0 -4
  228. package/src/shared/dev-overlay/get-source-map.ts +0 -33
  229. package/src/shared/dev-overlay/icons.tsx +0 -456
  230. package/src/shared/dev-overlay/index.tsx +0 -63
  231. package/src/shared/lazy.ts +0 -52
  232. package/src/shared/serverFunction.ts +0 -10
  233. package/src/utils/debounce.ts +0 -10
  234. package/src/virtual.d.ts +0 -27
  235. package/tsconfig.json +0 -24
  236. package/vitest.config.ts +0 -9
  237. /package/{src/client/index.tsx → dist/client/index.d.ts} +0 -0
  238. /package/{src → dist}/shared/dev-overlay/styles.css +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
+ }
@@ -0,0 +1,3 @@
1
+ import { type Component } from "solid-js";
2
+ export declare function cloneServerReference(id: string): (...args: any[]) => Promise<unknown>;
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;
@@ -0,0 +1,305 @@
1
+ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
2
+ if (typeof path === "string" && /^\.\.?\//.test(path)) {
3
+ return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
4
+ return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
5
+ });
6
+ }
7
+ return path;
8
+ };
9
+ import { parseSetCookie } from "cookie-es";
10
+ import { parseCookies } from "h3";
11
+ import { sharedConfig } from "solid-js";
12
+ import { renderToString } from "solid-js/web";
13
+ import { provideRequestEvent } from "solid-js/web/storage";
14
+ import { getFetchEvent, mergeResponseHeaders } from "../server/fetchEvent.js";
15
+ import { createPageEvent } from "../server/handler.js";
16
+ import { deserializeFromJSONString, serializeToJSONStream, serializeToJSStream, } from "./serialization.js";
17
+ import { BODY_FORMAT_KEY, BodyFormat, extractBody, getHeadersAndBody } from "./shared.js";
18
+ import "solid-start:server-fn-manifest";
19
+ import { getServerFunction, hasServerFunction } from "./registration.js";
20
+ import { getExpectedRedirectStatus } from "../server/util.js";
21
+ export async function handleServerFunction(h3Event) {
22
+ const event = getFetchEvent(h3Event);
23
+ const request = event.request;
24
+ const serverReference = request.headers.get("X-Server-Id");
25
+ const instance = request.headers.get("X-Server-Instance");
26
+ const singleFlight = request.headers.has("X-Single-Flight");
27
+ const url = new URL(request.url);
28
+ let functionId;
29
+ if (serverReference) {
30
+ // invariant(typeof serverReference === "string", "Invalid server function");
31
+ [functionId] = serverReference.split("#");
32
+ }
33
+ else {
34
+ functionId = url.searchParams.get("id");
35
+ if (!functionId) {
36
+ return process.env.NODE_ENV === "development"
37
+ ? new Response("Server function not found", { status: 404 })
38
+ : new Response(null, { status: 404 });
39
+ }
40
+ }
41
+ if (import.meta.env.DEV && !hasServerFunction(functionId)) {
42
+ // The module that owns this function has not been evaluated in the server
43
+ // environment yet (e.g. the route was reached by client-side navigation, so
44
+ // it was only ever loaded in the browser). Ask the dev server to resolve the
45
+ // function id back to its owning module and import it, which registers it.
46
+ // Resolved by the "solid-start:server-functions/preload" plugin.
47
+ try {
48
+ // the /@id/ prefix routes the request through the plugin container so the
49
+ // virtual module can be resolved (bare ids get node-resolved by the runner)
50
+ await import(__rewriteRelativeImportExtension(
51
+ /* @vite-ignore */ `/@id/solid-start:server-fn-lookup?id=${encodeURIComponent(functionId)}`, true));
52
+ }
53
+ catch (error) {
54
+ // fall through to getServerFunction, which reports the missing function
55
+ console.error("[solid-start] server function lookup failed:", error);
56
+ }
57
+ }
58
+ const serverFunction = getServerFunction(functionId);
59
+ let parsed = [];
60
+ // grab bound arguments from url when no JS
61
+ if (!instance || request.method === "GET") {
62
+ const args = url.searchParams.get("args");
63
+ if (args) {
64
+ const result = (await deserializeFromJSONString(args));
65
+ for (const arg of result) {
66
+ parsed.push(arg);
67
+ }
68
+ }
69
+ }
70
+ if (request.method === "POST" && request.body !== null) {
71
+ const bodyFormat = request.headers.get(BODY_FORMAT_KEY);
72
+ const decoded = await extractBody("", false, request.clone());
73
+ if (bodyFormat === BodyFormat.Seroval) {
74
+ parsed = decoded;
75
+ }
76
+ else {
77
+ parsed.push(decoded);
78
+ }
79
+ }
80
+ try {
81
+ let result = await provideRequestEvent(event, async () => {
82
+ /* @ts-expect-error */
83
+ sharedConfig.context = { event };
84
+ event.locals.serverFunctionMeta = {
85
+ id: functionId,
86
+ };
87
+ return serverFunction(...parsed);
88
+ });
89
+ if (singleFlight && instance) {
90
+ result = await handleSingleFlight(event, result);
91
+ }
92
+ // handle responses
93
+ if (result instanceof Response) {
94
+ if (result.headers && result.headers.has("X-Content-Raw"))
95
+ return result;
96
+ if (instance) {
97
+ // forward headers
98
+ if (result.headers)
99
+ mergeResponseHeaders(h3Event, result.headers);
100
+ // forward non-redirect statuses
101
+ if (result.status && (result.status < 300 || result.status >= 400))
102
+ h3Event.res.status = result.status;
103
+ if (result.customBody) {
104
+ result = await result.customBody();
105
+ }
106
+ else if (result.body == null)
107
+ result = null;
108
+ }
109
+ }
110
+ // handle no JS success case
111
+ if (!instance)
112
+ return handleNoJS(result, request, parsed);
113
+ const body = getHeadersAndBody(result);
114
+ if (body) {
115
+ return new Response(body.body, {
116
+ headers: body.headers,
117
+ });
118
+ }
119
+ h3Event.res.headers.set(BODY_FORMAT_KEY, BodyFormat.Seroval);
120
+ if (import.meta.env.SEROVAL_MODE === "js") {
121
+ h3Event.res.headers.set("content-type", "text/javascript");
122
+ return serializeToJSStream(instance, result);
123
+ }
124
+ h3Event.res.headers.set("content-type", "text/plain");
125
+ return serializeToJSONStream(result);
126
+ }
127
+ catch (x) {
128
+ if (x instanceof Response) {
129
+ if (singleFlight && instance) {
130
+ x = await handleSingleFlight(event, x);
131
+ }
132
+ // forward headers
133
+ if (x.headers)
134
+ mergeResponseHeaders(h3Event, x.headers);
135
+ // forward non-redirect statuses
136
+ if (x.status && (!instance || x.status < 300 || x.status >= 400))
137
+ h3Event.res.status = x.status;
138
+ if (x.customBody) {
139
+ x = await x.customBody();
140
+ }
141
+ else if (x.body == null)
142
+ x = null;
143
+ h3Event.res.headers.set("X-Error", "true");
144
+ }
145
+ else if (instance) {
146
+ const error = x instanceof Error ? x.message : typeof x === "string" ? x : "true";
147
+ h3Event.res.headers.set("X-Error", toHeaderValue(error));
148
+ }
149
+ else {
150
+ x = handleNoJS(x, request, parsed, true);
151
+ }
152
+ if (instance) {
153
+ const body = getHeadersAndBody(x);
154
+ if (body) {
155
+ const headers = new Headers(body.headers);
156
+ const errorHeader = h3Event.res.headers.get("X-Error");
157
+ if (errorHeader !== null) {
158
+ headers.set("X-Error", errorHeader);
159
+ }
160
+ return new Response(body.body, {
161
+ headers,
162
+ });
163
+ }
164
+ h3Event.res.headers.set(BODY_FORMAT_KEY, BodyFormat.Seroval);
165
+ if (import.meta.env.SEROVAL_MODE === "js") {
166
+ h3Event.res.headers.set("content-type", "text/javascript");
167
+ return serializeToJSStream(instance, x);
168
+ }
169
+ h3Event.res.headers.set("content-type", "text/plain");
170
+ return serializeToJSONStream(x);
171
+ }
172
+ return x;
173
+ }
174
+ }
175
+ // header values must be ByteString-safe (no chars > 0xFF), otherwise Headers.set throws
176
+ function toHeaderValue(value) {
177
+ const stripped = value.replace(/[\r\n]+/g, "");
178
+ try {
179
+ return /[^\x00-\xFF]/.test(stripped) ? encodeURIComponent(stripped) : stripped;
180
+ }
181
+ catch {
182
+ // encodeURIComponent throws on lone surrogates
183
+ return "true";
184
+ }
185
+ }
186
+ function handleNoJS(result, request, parsed, thrown) {
187
+ const url = new URL(request.url);
188
+ const isError = result instanceof Error;
189
+ let statusCode = 302;
190
+ let headers;
191
+ if (result instanceof Response) {
192
+ headers = new Headers(result.headers);
193
+ if (result.headers.has("Location")) {
194
+ headers.set(`Location`, new URL(result.headers.get("Location"), url.origin + import.meta.env.BASE_URL).toString());
195
+ statusCode = getExpectedRedirectStatus(result);
196
+ }
197
+ }
198
+ else
199
+ headers = new Headers({
200
+ Location: new URL(request.headers.get("referer")).toString(),
201
+ });
202
+ if (result) {
203
+ headers.append("Set-Cookie", `flash=${encodeURIComponent(JSON.stringify({
204
+ url: url.pathname + url.search,
205
+ result: isError ? result.message : result,
206
+ thrown: thrown,
207
+ error: isError,
208
+ input: [...parsed.slice(0, -1), [...parsed[parsed.length - 1].entries()]],
209
+ }))}; Secure; HttpOnly;`);
210
+ }
211
+ return new Response(null, {
212
+ status: statusCode,
213
+ headers,
214
+ });
215
+ }
216
+ let App;
217
+ export function createSingleFlightHeaders(sourceEvent) {
218
+ // cookie handling logic is pretty simplistic so this might be imperfect
219
+ // unclear if h3 internals are available on all platforms but we need a way to
220
+ // update request headers on the underlying H3 event.
221
+ const headers = new Headers(sourceEvent.request.headers);
222
+ const cookies = parseCookies(sourceEvent.nativeEvent);
223
+ const SetCookies = sourceEvent.response.headers.getSetCookie();
224
+ headers.delete("cookie");
225
+ // let useH3Internals = false;
226
+ // if (sourceEvent.nativeEvent.node?.req) {
227
+ // useH3Internals = true;
228
+ // sourceEvent.nativeEvent.node.req.headers.cookie = "";
229
+ // }
230
+ SetCookies.forEach(cookie => {
231
+ if (!cookie)
232
+ return;
233
+ const { maxAge, expires, name, value } = parseSetCookie(cookie);
234
+ if (maxAge != null && maxAge <= 0) {
235
+ delete cookies[name];
236
+ return;
237
+ }
238
+ if (expires != null && expires.getTime() <= Date.now()) {
239
+ delete cookies[name];
240
+ return;
241
+ }
242
+ cookies[name] = value;
243
+ });
244
+ Object.entries(cookies).forEach(([key, value]) => {
245
+ headers.append("cookie", `${key}=${value}`);
246
+ // useH3Internals &&
247
+ // (sourceEvent.nativeEvent.node.req.headers.cookie += `${key}=${value};`);
248
+ });
249
+ return headers;
250
+ }
251
+ async function handleSingleFlight(sourceEvent, result) {
252
+ let revalidate;
253
+ let url = new URL(sourceEvent.request.headers.get("referer")).toString();
254
+ if (result instanceof Response) {
255
+ if (result.headers.has("X-Revalidate"))
256
+ revalidate = result.headers.get("X-Revalidate").split(",");
257
+ if (result.headers.has("Location"))
258
+ url = new URL(result.headers.get("Location"), new URL(sourceEvent.request.url).origin + import.meta.env.BASE_URL).toString();
259
+ }
260
+ const event = { ...sourceEvent };
261
+ event.request = new Request(url, {
262
+ headers: createSingleFlightHeaders(sourceEvent),
263
+ });
264
+ return await provideRequestEvent(event, async () => {
265
+ await createPageEvent(event);
266
+ App || (App = (await import("solid-start:app")).default);
267
+ /* @ts-expect-error */
268
+ event.router.dataOnly = revalidate || true;
269
+ /* @ts-expect-error */
270
+ event.router.previousUrl = sourceEvent.request.headers.get("referer");
271
+ try {
272
+ renderToString(() => {
273
+ /* @ts-expect-error */
274
+ sharedConfig.context.event = event;
275
+ App();
276
+ });
277
+ }
278
+ catch (e) {
279
+ console.log(e);
280
+ }
281
+ /* @ts-expect-error */
282
+ const body = event.router.data;
283
+ if (!body)
284
+ return result;
285
+ let containsKey = false;
286
+ for (const key in body) {
287
+ if (body[key] === undefined)
288
+ delete body[key];
289
+ else
290
+ containsKey = true;
291
+ }
292
+ if (!containsKey)
293
+ return result;
294
+ if (!(result instanceof Response)) {
295
+ body["_$value"] = result;
296
+ result = new Response(null, { status: 200 });
297
+ }
298
+ else if (result.customBody) {
299
+ body["_$value"] = result.customBody();
300
+ }
301
+ result.customBody = () => body;
302
+ result.headers.set("X-Single-Flight", "true");
303
+ return result;
304
+ });
305
+ }
@@ -0,0 +1,3 @@
1
+ export declare function registerServerFunction<T extends any[], R>(id: string, callback: (...args: T) => Promise<R>): (...args: T) => Promise<R>;
2
+ export declare function hasServerFunction(id: string): boolean;
3
+ export declare function getServerFunction<T extends any[], R>(id: string): (...args: T) => Promise<R>;
@@ -0,0 +1,15 @@
1
+ const REGISTRATIONS = new Map();
2
+ export function registerServerFunction(id, callback) {
3
+ REGISTRATIONS.set(id, callback);
4
+ return callback;
5
+ }
6
+ export function hasServerFunction(id) {
7
+ return REGISTRATIONS.has(id);
8
+ }
9
+ export function getServerFunction(id) {
10
+ const fn = REGISTRATIONS.get(id);
11
+ if (fn) {
12
+ return fn;
13
+ }
14
+ throw new Error("invalid server function: " + id);
15
+ }
@@ -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>;