@solidjs/start 2.0.0-alpha.1 → 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.
- package/CHANGELOG.md +33 -0
- package/dist/config/constants.d.ts +1 -1
- package/dist/config/constants.js +1 -1
- package/dist/config/dev-server.js +18 -0
- package/dist/config/env.d.ts +19 -0
- package/dist/config/env.js +81 -0
- package/dist/config/fs-routes/fs-watcher.js +14 -20
- package/dist/config/fs-routes/index.js +2 -2
- package/dist/config/fs-routes/router.js +7 -8
- package/dist/config/index.d.ts +16 -1
- package/dist/config/index.js +30 -29
- package/dist/config/lazy.js +35 -5
- package/dist/config/manifest.js +40 -6
- package/dist/config/vite-utils.d.ts +6 -1
- package/dist/config/vite-utils.js +7 -3
- 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 +169 -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 +53 -0
- package/dist/directives/types.d.ts +10 -0
- package/dist/directives/xxhash32.d.ts +6 -0
- package/dist/directives/xxhash32.js +193 -0
- package/dist/{server/server-runtime.d.ts → fns/client.d.ts} +1 -1
- package/dist/fns/client.js +106 -0
- package/dist/fns/handler.d.ts +5 -0
- package/dist/{server/server-functions-handler.js → fns/handler.js} +55 -96
- 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/{server/server-fns-runtime.js → fns/server.js} +7 -1
- package/dist/fns/shared.d.ts +17 -0
- package/dist/fns/shared.js +105 -0
- package/dist/http/index.d.ts +1 -1
- package/dist/middleware/index.d.ts +20 -0
- package/dist/middleware/index.js +21 -1
- package/dist/server/StartServer.jsx +2 -0
- package/dist/server/assets/PatchVirtualDevStyles.d.ts +4 -0
- package/dist/server/assets/PatchVirtualDevStyles.jsx +28 -0
- package/dist/server/collect-styles.js +9 -21
- package/dist/server/handler.d.ts +61 -3
- package/dist/server/handler.js +18 -15
- package/dist/server/spa/StartServer.jsx +2 -0
- package/dist/server/spa/handler.d.ts +30 -1
- package/dist/server/types.d.ts +1 -1
- package/dist/shared/dev-overlay/CodeView.jsx +4 -4
- package/dist/shared/dev-overlay/DevOverlayDialog.jsx +34 -34
- package/dist/shared/dev-overlay/index.jsx +2 -2
- package/dist/shared/dev-overlay/styles.css +40 -40
- package/package.json +27 -20
- package/src/config/constants.ts +1 -1
- package/src/config/dev-server.ts +12 -0
- package/src/config/env.ts +105 -0
- package/src/config/fs-routes/fs-watcher.ts +17 -24
- package/src/config/fs-routes/index.ts +2 -2
- package/src/config/fs-routes/router.ts +7 -8
- package/src/config/index.ts +44 -40
- package/src/config/lazy.ts +36 -5
- package/src/config/manifest.ts +40 -9
- package/src/config/vite-utils.ts +8 -4
- package/src/directives/bubble-function-declaration.ts +33 -0
- package/src/directives/compile.ts +54 -0
- package/src/directives/generate-unique-name.ts +22 -0
- package/src/directives/get-descriptive-name.ts +39 -0
- package/src/directives/get-import-identifier.ts +34 -0
- package/src/directives/get-root-statement-path.ts +14 -0
- package/src/directives/index.ts +232 -0
- package/src/directives/is-statement-top-level.ts +13 -0
- package/src/directives/paths.ts +63 -0
- package/src/directives/plugin.ts +389 -0
- package/src/directives/remove-unused-variables.ts +55 -0
- package/src/directives/types.ts +12 -0
- package/src/directives/xxhash32.ts +214 -0
- package/src/env.d.ts +1 -1
- package/src/fns/client.ts +141 -0
- package/src/fns/handler.spec.ts +85 -0
- package/src/{server/server-functions-handler.ts → fns/handler.ts} +65 -117
- package/src/fns/registration.ts +19 -0
- package/src/fns/serialization.ts +250 -0
- package/src/{server/server-fns-runtime.ts → fns/server.ts} +17 -2
- package/src/fns/shared.ts +116 -0
- package/src/middleware/index.ts +21 -1
- package/src/server/StartServer.tsx +2 -0
- package/src/server/assets/PatchVirtualDevStyles.tsx +31 -0
- package/src/server/collect-styles.ts +9 -26
- package/src/server/handler.ts +20 -16
- package/src/server/spa/StartServer.tsx +2 -0
- package/src/server/types.ts +1 -1
- package/src/shared/dev-overlay/CodeView.tsx +5 -7
- package/src/shared/dev-overlay/DevOverlayDialog.tsx +34 -34
- package/src/shared/dev-overlay/get-source-map.ts +1 -1
- package/src/shared/dev-overlay/index.tsx +4 -4
- package/src/shared/dev-overlay/styles.css +40 -40
- package/tsconfig.json +1 -0
- package/dist/server/fetchEvent.spec.js +0 -65
- package/dist/server/server-fns-runtime.d.ts +0 -1
- package/dist/server/server-functions-handler.d.ts +0 -2
- package/dist/server/server-runtime.js +0 -194
- package/src/server/server-runtime.ts +0 -240
- /package/dist/{server/fetchEvent.spec.d.ts → directives/types.js} +0 -0
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
[data-start-dev-overlay] {
|
|
2
2
|
position: fixed;
|
|
3
3
|
inset: 0px;
|
|
4
4
|
z-index: 50;
|
|
5
5
|
overflow-y: auto;
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
[data-start-dev-overlay]>div {
|
|
9
9
|
position: relative;
|
|
10
10
|
display: flex;
|
|
11
11
|
align-items: center;
|
|
@@ -13,13 +13,13 @@
|
|
|
13
13
|
min-height: 100vh;
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
[data-start-dev-overlay-background] {
|
|
17
17
|
position: absolute;
|
|
18
18
|
inset: 0px;
|
|
19
19
|
background-color: rgb(17 24 39 / 0.5);
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
[data-start-dev-overlay-panel] {
|
|
23
23
|
display: flex;
|
|
24
24
|
flex-direction: column;
|
|
25
25
|
width: 75vw;
|
|
@@ -30,12 +30,12 @@
|
|
|
30
30
|
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
[data-start-dev-overlay-panel-container] {
|
|
34
34
|
margin: 2rem;
|
|
35
35
|
z-index: 10;
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
|
|
38
|
+
[data-start-dev-overlay-navbar] {
|
|
39
39
|
display: flex;
|
|
40
40
|
flex-direction: row;
|
|
41
41
|
align-items: center;
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
gap: 0.5rem;
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
|
|
46
|
+
[data-start-dev-overlay-pagination] {
|
|
47
47
|
display: flex;
|
|
48
48
|
flex-direction: row;
|
|
49
49
|
align-items: center;
|
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
padding: 0.25rem;
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
-
|
|
57
|
+
[data-start-dev-overlay-button] {
|
|
58
58
|
display: flex;
|
|
59
59
|
padding: 0.125rem;
|
|
60
60
|
border-radius: 9999px;
|
|
@@ -68,44 +68,44 @@
|
|
|
68
68
|
transition-duration: 150ms;
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
-
|
|
71
|
+
[data-start-dev-overlay-button]:hover {
|
|
72
72
|
background-color: rgb(229 231 235);
|
|
73
73
|
color: rgb(55 65 81);
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
-
|
|
76
|
+
[data-start-dev-overlay-button]:focus {
|
|
77
77
|
outline: 2px solid transparent;
|
|
78
78
|
outline-offset: 2px
|
|
79
79
|
}
|
|
80
80
|
|
|
81
|
-
|
|
81
|
+
[data-start-dev-overlay-button]:focus-visible {
|
|
82
82
|
box-shadow: 0 0 0 calc(3px) rgb(17 24 39 / 0.75);
|
|
83
83
|
}
|
|
84
84
|
|
|
85
|
-
|
|
85
|
+
[data-start-dev-overlay-button]:active {
|
|
86
86
|
background-color: rgb(243 244 246);
|
|
87
87
|
color: rgb(31 41 55);
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
-
|
|
90
|
+
[data-start-dev-overlay-button]>svg {
|
|
91
91
|
height: 1.5rem;
|
|
92
92
|
width: 1.5rem;
|
|
93
93
|
}
|
|
94
94
|
|
|
95
|
-
|
|
95
|
+
[data-start-dev-overlay-page-counter] {
|
|
96
96
|
font-size: 0.875rem;
|
|
97
97
|
line-height: 1.25rem;
|
|
98
98
|
font-weight: 600;
|
|
99
99
|
}
|
|
100
100
|
|
|
101
|
-
|
|
101
|
+
[data-start-dev-overlay-navbar-left] {
|
|
102
102
|
display: flex;
|
|
103
103
|
flex-direction: row;
|
|
104
104
|
gap: 0.5rem;
|
|
105
105
|
}
|
|
106
106
|
|
|
107
107
|
|
|
108
|
-
|
|
108
|
+
[data-start-dev-overlay-controls] {
|
|
109
109
|
display: flex;
|
|
110
110
|
flex-direction: row;
|
|
111
111
|
align-items: center;
|
|
@@ -116,7 +116,7 @@
|
|
|
116
116
|
padding: 0.25rem;
|
|
117
117
|
}
|
|
118
118
|
|
|
119
|
-
|
|
119
|
+
[data-start-dev-overlay-version] {
|
|
120
120
|
display: flex;
|
|
121
121
|
flex-direction: row;
|
|
122
122
|
align-items: center;
|
|
@@ -130,17 +130,17 @@
|
|
|
130
130
|
font-weight: 600;
|
|
131
131
|
}
|
|
132
132
|
|
|
133
|
-
|
|
133
|
+
[data-start-dev-overlay-version]>div {
|
|
134
134
|
height: 1.5rem;
|
|
135
135
|
width: 1.5rem;
|
|
136
136
|
}
|
|
137
137
|
|
|
138
|
-
|
|
138
|
+
[data-start-dev-overlay-version]>div>svg {
|
|
139
139
|
height: 1.5rem;
|
|
140
140
|
width: 1.5rem;
|
|
141
141
|
}
|
|
142
142
|
|
|
143
|
-
|
|
143
|
+
[data-start-dev-overlay-content] {
|
|
144
144
|
display: flex;
|
|
145
145
|
flex-direction: column;
|
|
146
146
|
gap: 0.75rem;
|
|
@@ -149,30 +149,30 @@
|
|
|
149
149
|
padding: 1rem;
|
|
150
150
|
}
|
|
151
151
|
|
|
152
|
-
|
|
152
|
+
[data-start-dev-overlay-error-info] {
|
|
153
153
|
display: flex;
|
|
154
154
|
flex-direction: column;
|
|
155
155
|
gap: 0.25rem;
|
|
156
156
|
}
|
|
157
157
|
|
|
158
|
-
|
|
158
|
+
[data-start-dev-overlay-error-info-name] {
|
|
159
159
|
font-weight: 700;
|
|
160
160
|
}
|
|
161
161
|
|
|
162
|
-
|
|
162
|
+
[data-start-dev-overlay-error-info-message] {
|
|
163
163
|
font-size: 1.5rem;
|
|
164
164
|
line-height: 2rem;
|
|
165
165
|
color: rgb(239 68 68);
|
|
166
166
|
font-weight: 700;
|
|
167
167
|
}
|
|
168
168
|
|
|
169
|
-
|
|
169
|
+
[data-start-dev-overlay-stack-frames-content] {
|
|
170
170
|
display: flex;
|
|
171
171
|
flex-direction: column;
|
|
172
172
|
gap: 0.75rem;
|
|
173
173
|
}
|
|
174
174
|
|
|
175
|
-
|
|
175
|
+
[data-start-dev-overlay-stack-frames] {
|
|
176
176
|
flex: 1 1 0%;
|
|
177
177
|
display: flex;
|
|
178
178
|
flex-direction: column;
|
|
@@ -185,7 +185,7 @@
|
|
|
185
185
|
border-radius: 0.5rem;
|
|
186
186
|
}
|
|
187
187
|
|
|
188
|
-
|
|
188
|
+
[data-start-dev-overlay-stack-frame] {
|
|
189
189
|
display: flex;
|
|
190
190
|
flex-direction: row;
|
|
191
191
|
align-items: center;
|
|
@@ -203,18 +203,18 @@
|
|
|
203
203
|
outline: none;
|
|
204
204
|
}
|
|
205
205
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
206
|
+
[data-start-dev-overlay-stack-frame][tc-active],
|
|
207
|
+
[data-start-dev-overlay-stack-frame][tc-selected],
|
|
208
|
+
[data-start-dev-overlay-stack-frame]:focus {
|
|
209
209
|
background-color: rgb(219 234 254);
|
|
210
210
|
color: rgb(30 58 138);
|
|
211
211
|
}
|
|
212
212
|
|
|
213
|
-
|
|
213
|
+
[data-start-dev-overlay-stack-frame-function] {
|
|
214
214
|
font-weight: 700;
|
|
215
215
|
}
|
|
216
216
|
|
|
217
|
-
|
|
217
|
+
[data-start-dev-overlay-stack-frame-file] {
|
|
218
218
|
white-space: nowrap;
|
|
219
219
|
font-size: 0.75rem;
|
|
220
220
|
/* 12px */
|
|
@@ -222,7 +222,7 @@
|
|
|
222
222
|
/* 16px */
|
|
223
223
|
}
|
|
224
224
|
|
|
225
|
-
|
|
225
|
+
[data-start-dev-overlay-stack-frames-code] {
|
|
226
226
|
padding: 0.5rem 1rem;
|
|
227
227
|
gap: 0.25rem;
|
|
228
228
|
flex: 1 1 0%;
|
|
@@ -234,7 +234,7 @@
|
|
|
234
234
|
background-color: rgb(17 24 39);
|
|
235
235
|
}
|
|
236
236
|
|
|
237
|
-
|
|
237
|
+
[data-start-dev-overlay-stack-frames-code-fallback] {
|
|
238
238
|
display: flex;
|
|
239
239
|
align-items: center;
|
|
240
240
|
justify-content: center;
|
|
@@ -244,13 +244,13 @@
|
|
|
244
244
|
line-height: 1.25rem;
|
|
245
245
|
}
|
|
246
246
|
|
|
247
|
-
|
|
247
|
+
[data-start-dev-overlay-stack-frames-code-source] {
|
|
248
248
|
font-size: 0.875rem;
|
|
249
249
|
line-height: 1.25rem;
|
|
250
250
|
color: rgb(249 250 251);
|
|
251
251
|
}
|
|
252
252
|
|
|
253
|
-
|
|
253
|
+
[data-start-dev-overlay-stack-frames-code-container] {
|
|
254
254
|
width: 100%;
|
|
255
255
|
overflow: hidden;
|
|
256
256
|
min-width: 100%;
|
|
@@ -260,13 +260,13 @@
|
|
|
260
260
|
border-top-color: rgb(249 250 251);
|
|
261
261
|
}
|
|
262
262
|
|
|
263
|
-
|
|
263
|
+
[data-start-dev-overlay-code-view] {
|
|
264
264
|
overflow: auto;
|
|
265
265
|
min-width: 100%;
|
|
266
266
|
min-height: 16rem;
|
|
267
267
|
}
|
|
268
268
|
|
|
269
|
-
|
|
269
|
+
[data-start-dev-overlay-code-view]>.shiki {
|
|
270
270
|
float: left;
|
|
271
271
|
min-width: 100%;
|
|
272
272
|
overflow-x: auto;
|
|
@@ -277,7 +277,7 @@
|
|
|
277
277
|
background-color: transparent !important;
|
|
278
278
|
}
|
|
279
279
|
|
|
280
|
-
|
|
280
|
+
[data-start-dev-overlay-code-view]>.shiki .line::before {
|
|
281
281
|
content: counter(step);
|
|
282
282
|
counter-increment: step;
|
|
283
283
|
width: 1rem;
|
|
@@ -287,6 +287,6 @@
|
|
|
287
287
|
color: rgba(115, 138, 148, .4)
|
|
288
288
|
}
|
|
289
289
|
|
|
290
|
-
|
|
290
|
+
[data-start-dev-overlay-error-line] {
|
|
291
291
|
background-color: rgba(239, 68, 68, .4);
|
|
292
|
-
}
|
|
292
|
+
}
|
package/tsconfig.json
CHANGED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import * as h3 from "h3";
|
|
2
|
-
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
3
|
-
import { createFetchEvent, getFetchEvent, mergeResponseHeaders } from "./fetchEvent.js";
|
|
4
|
-
vi.mock(import("h3"), async (mod) => {
|
|
5
|
-
return {
|
|
6
|
-
...(await mod()),
|
|
7
|
-
getRequestIP: vi.fn(),
|
|
8
|
-
};
|
|
9
|
-
});
|
|
10
|
-
const mockedH3 = vi.mocked(h3);
|
|
11
|
-
const createMockH3Event = () => {
|
|
12
|
-
const event = new h3.H3Event(new Request("http://localhost/test"));
|
|
13
|
-
event.res.status = 200;
|
|
14
|
-
event.res.statusText = "OK";
|
|
15
|
-
return event;
|
|
16
|
-
};
|
|
17
|
-
describe("fetchEvent", () => {
|
|
18
|
-
let mockH3Event;
|
|
19
|
-
beforeEach(() => {
|
|
20
|
-
mockH3Event = createMockH3Event();
|
|
21
|
-
vi.clearAllMocks();
|
|
22
|
-
mockedH3.getRequestIP.mockReturnValue("127.0.0.1");
|
|
23
|
-
});
|
|
24
|
-
describe("createFetchEvent", () => {
|
|
25
|
-
it("should create a FetchEvent from H3Event", () => {
|
|
26
|
-
const fetchEvent = createFetchEvent(mockH3Event);
|
|
27
|
-
expect(fetchEvent).toEqual({
|
|
28
|
-
request: mockH3Event.req,
|
|
29
|
-
response: expect.any(Object),
|
|
30
|
-
clientAddress: "127.0.0.1",
|
|
31
|
-
locals: {},
|
|
32
|
-
nativeEvent: mockH3Event,
|
|
33
|
-
});
|
|
34
|
-
});
|
|
35
|
-
it("should create response stub with correct properties", () => {
|
|
36
|
-
const fetchEvent = createFetchEvent(mockH3Event);
|
|
37
|
-
expect(fetchEvent.response).toHaveProperty("status");
|
|
38
|
-
expect(fetchEvent.response).toHaveProperty("statusText");
|
|
39
|
-
expect(fetchEvent.response).toHaveProperty("headers");
|
|
40
|
-
});
|
|
41
|
-
});
|
|
42
|
-
describe("getFetchEvent", () => {
|
|
43
|
-
it("should create and cache FetchEvent on first call", () => {
|
|
44
|
-
const fetchEvent = getFetchEvent(mockH3Event);
|
|
45
|
-
expect(mockH3Event.context.solidFetchEvent).toBe(fetchEvent);
|
|
46
|
-
expect(fetchEvent.nativeEvent).toBe(mockH3Event);
|
|
47
|
-
});
|
|
48
|
-
it("should return cached FetchEvent on subsequent calls", () => {
|
|
49
|
-
const firstCall = getFetchEvent(mockH3Event);
|
|
50
|
-
const secondCall = getFetchEvent(mockH3Event);
|
|
51
|
-
expect(firstCall).toBe(secondCall);
|
|
52
|
-
});
|
|
53
|
-
});
|
|
54
|
-
describe("mergeResponseHeaders", () => {
|
|
55
|
-
it("should merge headers from Headers object to H3Event", () => {
|
|
56
|
-
const headers = new Headers({
|
|
57
|
-
"content-type": "application/json",
|
|
58
|
-
"x-custom": "value",
|
|
59
|
-
});
|
|
60
|
-
mergeResponseHeaders(mockH3Event, headers);
|
|
61
|
-
expect(headers.get("content-type")).toBe("application/json");
|
|
62
|
-
expect(headers.get("x-custom")).toBe("value");
|
|
63
|
-
});
|
|
64
|
-
});
|
|
65
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function createServerReference(fn: Function, id: string): Function;
|
|
@@ -1,194 +0,0 @@
|
|
|
1
|
-
import { deserialize, toJSONAsync } from "seroval";
|
|
2
|
-
import { CustomEventPlugin, DOMExceptionPlugin, EventPlugin, FormDataPlugin, HeadersPlugin, ReadableStreamPlugin, RequestPlugin, ResponsePlugin, URLPlugin, URLSearchParamsPlugin, } from "seroval-plugins/web";
|
|
3
|
-
class SerovalChunkReader {
|
|
4
|
-
reader;
|
|
5
|
-
buffer;
|
|
6
|
-
done;
|
|
7
|
-
constructor(stream) {
|
|
8
|
-
this.reader = stream.getReader();
|
|
9
|
-
this.buffer = new Uint8Array(0);
|
|
10
|
-
this.done = false;
|
|
11
|
-
}
|
|
12
|
-
async readChunk() {
|
|
13
|
-
// if there's no chunk, read again
|
|
14
|
-
const chunk = await this.reader.read();
|
|
15
|
-
if (!chunk.done) {
|
|
16
|
-
// repopulate the buffer
|
|
17
|
-
let newBuffer = new Uint8Array(this.buffer.length + chunk.value.length);
|
|
18
|
-
newBuffer.set(this.buffer);
|
|
19
|
-
newBuffer.set(chunk.value, this.buffer.length);
|
|
20
|
-
this.buffer = newBuffer;
|
|
21
|
-
}
|
|
22
|
-
else {
|
|
23
|
-
this.done = true;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
async next() {
|
|
27
|
-
// Check if the buffer is empty
|
|
28
|
-
if (this.buffer.length === 0) {
|
|
29
|
-
// if we are already done...
|
|
30
|
-
if (this.done) {
|
|
31
|
-
return {
|
|
32
|
-
done: true,
|
|
33
|
-
value: undefined,
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
// Otherwise, read a new chunk
|
|
37
|
-
await this.readChunk();
|
|
38
|
-
return await this.next();
|
|
39
|
-
}
|
|
40
|
-
// Read the "byte header"
|
|
41
|
-
// The byte header tells us how big the expected data is
|
|
42
|
-
// so we know how much data we should wait before we
|
|
43
|
-
// deserialize the data
|
|
44
|
-
const head = new TextDecoder().decode(this.buffer.subarray(1, 11));
|
|
45
|
-
const bytes = Number.parseInt(head, 16); // ;0x00000000;
|
|
46
|
-
// Check if the buffer has enough bytes to be parsed
|
|
47
|
-
while (bytes > this.buffer.length - 12) {
|
|
48
|
-
// If it's not enough, and the reader is done
|
|
49
|
-
// then the chunk is invalid.
|
|
50
|
-
if (this.done) {
|
|
51
|
-
throw new Error("Malformed server function stream.");
|
|
52
|
-
}
|
|
53
|
-
// Otherwise, we read more chunks
|
|
54
|
-
await this.readChunk();
|
|
55
|
-
}
|
|
56
|
-
// Extract the exact chunk as defined by the byte header
|
|
57
|
-
const partial = new TextDecoder().decode(this.buffer.subarray(12, 12 + bytes));
|
|
58
|
-
// The rest goes to the buffer
|
|
59
|
-
this.buffer = this.buffer.subarray(12 + bytes);
|
|
60
|
-
// Deserialize the chunk
|
|
61
|
-
return {
|
|
62
|
-
done: false,
|
|
63
|
-
value: deserialize(partial),
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
async drain() {
|
|
67
|
-
while (true) {
|
|
68
|
-
const result = await this.next();
|
|
69
|
-
if (result.done) {
|
|
70
|
-
break;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
async function deserializeStream(id, response) {
|
|
76
|
-
if (!response.body) {
|
|
77
|
-
throw new Error("missing body");
|
|
78
|
-
}
|
|
79
|
-
const reader = new SerovalChunkReader(response.body);
|
|
80
|
-
const result = await reader.next();
|
|
81
|
-
if (!result.done) {
|
|
82
|
-
reader.drain().then(() => {
|
|
83
|
-
// @ts-ignore
|
|
84
|
-
delete $R[id];
|
|
85
|
-
}, () => {
|
|
86
|
-
// no-op
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
return result.value;
|
|
90
|
-
}
|
|
91
|
-
let INSTANCE = 0;
|
|
92
|
-
function createRequest(base, id, instance, options) {
|
|
93
|
-
return fetch(base, {
|
|
94
|
-
method: "POST",
|
|
95
|
-
...options,
|
|
96
|
-
headers: {
|
|
97
|
-
...options.headers,
|
|
98
|
-
"X-Server-Id": id,
|
|
99
|
-
"X-Server-Instance": instance,
|
|
100
|
-
},
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
const plugins = [
|
|
104
|
-
CustomEventPlugin,
|
|
105
|
-
DOMExceptionPlugin,
|
|
106
|
-
EventPlugin,
|
|
107
|
-
FormDataPlugin,
|
|
108
|
-
HeadersPlugin,
|
|
109
|
-
ReadableStreamPlugin,
|
|
110
|
-
RequestPlugin,
|
|
111
|
-
ResponsePlugin,
|
|
112
|
-
URLSearchParamsPlugin,
|
|
113
|
-
URLPlugin,
|
|
114
|
-
];
|
|
115
|
-
async function fetchServerFunction(base, id, options, args) {
|
|
116
|
-
const instance = `server-fn:${INSTANCE++}`;
|
|
117
|
-
const response = await (args.length === 0
|
|
118
|
-
? createRequest(base, id, instance, options)
|
|
119
|
-
: args.length === 1 && args[0] instanceof FormData
|
|
120
|
-
? createRequest(base, id, instance, { ...options, body: args[0] })
|
|
121
|
-
: args.length === 1 && args[0] instanceof URLSearchParams
|
|
122
|
-
? createRequest(base, id, instance, {
|
|
123
|
-
...options,
|
|
124
|
-
body: args[0],
|
|
125
|
-
headers: { ...options.headers, "Content-Type": "application/x-www-form-urlencoded" },
|
|
126
|
-
})
|
|
127
|
-
: createRequest(base, id, instance, {
|
|
128
|
-
...options,
|
|
129
|
-
body: JSON.stringify(await Promise.resolve(toJSONAsync(args, { plugins }))),
|
|
130
|
-
headers: { ...options.headers, "Content-Type": "application/json" },
|
|
131
|
-
}));
|
|
132
|
-
if (response.headers.has("Location") ||
|
|
133
|
-
response.headers.has("X-Revalidate") ||
|
|
134
|
-
response.headers.has("X-Single-Flight")) {
|
|
135
|
-
if (response.body) {
|
|
136
|
-
/* @ts-ignore-next-line */
|
|
137
|
-
response.customBody = () => {
|
|
138
|
-
return deserializeStream(instance, response);
|
|
139
|
-
};
|
|
140
|
-
}
|
|
141
|
-
return response;
|
|
142
|
-
}
|
|
143
|
-
const contentType = response.headers.get("Content-Type");
|
|
144
|
-
let result;
|
|
145
|
-
if (contentType && contentType.startsWith("text/plain")) {
|
|
146
|
-
result = await response.text();
|
|
147
|
-
}
|
|
148
|
-
else if (contentType && contentType.startsWith("application/json")) {
|
|
149
|
-
result = await response.json();
|
|
150
|
-
}
|
|
151
|
-
else {
|
|
152
|
-
result = await deserializeStream(instance, response);
|
|
153
|
-
}
|
|
154
|
-
if (response.headers.has("X-Error")) {
|
|
155
|
-
throw result;
|
|
156
|
-
}
|
|
157
|
-
return result;
|
|
158
|
-
}
|
|
159
|
-
export function createServerReference(id) {
|
|
160
|
-
let baseURL = import.meta.env.BASE_URL ?? "/";
|
|
161
|
-
if (!baseURL.endsWith("/"))
|
|
162
|
-
baseURL += "/";
|
|
163
|
-
const fn = (...args) => fetchServerFunction(`${baseURL}_server`, id, {}, args);
|
|
164
|
-
return new Proxy(fn, {
|
|
165
|
-
get(target, prop, receiver) {
|
|
166
|
-
if (prop === "url") {
|
|
167
|
-
return `${baseURL}_server?id=${encodeURIComponent(id)}`;
|
|
168
|
-
}
|
|
169
|
-
if (prop === "GET") {
|
|
170
|
-
return receiver.withOptions({ method: "GET" });
|
|
171
|
-
}
|
|
172
|
-
if (prop === "withOptions") {
|
|
173
|
-
const url = `${baseURL}_server?id=${encodeURIComponent(id)}`;
|
|
174
|
-
return (options) => {
|
|
175
|
-
const fn = async (...args) => {
|
|
176
|
-
const encodeArgs = options.method && options.method.toUpperCase() === "GET";
|
|
177
|
-
return fetchServerFunction(encodeArgs
|
|
178
|
-
? url +
|
|
179
|
-
(args.length
|
|
180
|
-
? `&args=${encodeURIComponent(JSON.stringify(await Promise.resolve(toJSONAsync(args, { plugins }))))}`
|
|
181
|
-
: "")
|
|
182
|
-
: `${baseURL}_server`, id, options, encodeArgs ? [] : args);
|
|
183
|
-
};
|
|
184
|
-
fn.url = url;
|
|
185
|
-
return fn;
|
|
186
|
-
};
|
|
187
|
-
}
|
|
188
|
-
return target[prop];
|
|
189
|
-
},
|
|
190
|
-
});
|
|
191
|
-
}
|
|
192
|
-
export function createClientReference(Component, id) {
|
|
193
|
-
return Component;
|
|
194
|
-
}
|