@solidjs/web 2.0.0-beta.3 → 2.0.0-beta.30
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/README.md +27 -4
- package/dist/dev.cjs +1211 -205
- package/dist/dev.js +1175 -199
- package/dist/server.cjs +1342 -234
- package/dist/server.js +1304 -231
- package/dist/web.cjs +1195 -196
- package/dist/web.js +1159 -190
- package/frames/dist/client.cjs +1746 -0
- package/frames/dist/client.dev.cjs +1759 -0
- package/frames/dist/client.dev.js +1747 -0
- package/frames/dist/client.js +1734 -0
- package/frames/dist/server.cjs +2426 -0
- package/frames/dist/server.js +2414 -0
- package/frames/package.json +30 -0
- package/package.json +287 -38
- package/serialization/dist/serialization.cjs +169 -0
- package/serialization/dist/serialization.js +159 -0
- package/serialization/package.json +20 -0
- package/serialization/types/index.d.ts +157 -0
- package/serialization/types-cjs/index.d.cts +157 -0
- package/serialization/types-cjs/package.json +3 -0
- package/server-functions/dist/client.cjs +613 -0
- package/server-functions/dist/client.js +585 -0
- package/server-functions/dist/server.cjs +904 -0
- package/server-functions/dist/server.js +875 -0
- package/server-functions/package.json +30 -0
- package/storage/package.json +8 -3
- package/storage/types/index.d.ts +26 -0
- package/storage/types-cjs/index.d.cts +28 -0
- package/storage/types-cjs/package.json +3 -0
- package/types/client.d.ts +125 -21
- package/types/core.d.ts +4 -3
- package/types/frames/client.d.ts +20 -0
- package/types/frames/frame-client.d.ts +270 -0
- package/types/frames/frame-sink.d.ts +168 -0
- package/types/frames/frame-transport.d.ts +196 -0
- package/types/frames/serializer.d.ts +157 -0
- package/types/frames/server.d.ts +30 -0
- package/types/index.d.ts +211 -26
- package/types/jsx-properties.d.ts +93 -0
- package/types/jsx.d.ts +4150 -1
- package/types/response.d.ts +129 -0
- package/types/serializer.d.ts +157 -0
- package/types/server-functions/client.d.ts +200 -0
- package/types/server-functions/flash.d.ts +38 -0
- package/types/server-functions/server.d.ts +490 -0
- package/types/server-functions/shared.d.ts +445 -0
- package/types/server-mock.d.ts +93 -0
- package/types/server.d.ts +221 -28
- package/types-cjs/client.d.cts +192 -0
- package/types-cjs/core.d.cts +4 -0
- package/types-cjs/frames/client.d.cts +20 -0
- package/types-cjs/frames/frame-client.d.cts +270 -0
- package/types-cjs/frames/frame-sink.d.cts +168 -0
- package/types-cjs/frames/frame-transport.d.cts +196 -0
- package/types-cjs/frames/serializer.d.cts +157 -0
- package/types-cjs/frames/server.d.cts +30 -0
- package/types-cjs/index.d.cts +231 -0
- package/types-cjs/jsx-properties.d.cts +93 -0
- package/types-cjs/jsx.d.cts +4150 -0
- package/types-cjs/package.json +3 -0
- package/types-cjs/response.d.cts +129 -0
- package/types-cjs/serializer.d.cts +157 -0
- package/types-cjs/server-functions/client.d.cts +200 -0
- package/types-cjs/server-functions/flash.d.cts +38 -0
- package/types-cjs/server-functions/server.d.cts +490 -0
- package/types-cjs/server-functions/shared.d.cts +445 -0
- package/types-cjs/server-mock.d.cts +165 -0
- package/types-cjs/server.d.cts +349 -0
- package/storage/types/src/client.d.ts +0 -1
- package/storage/types/src/index.d.ts +0 -46
- package/storage/types/src/server-mock.d.ts +0 -72
- package/storage/types/storage/src/index.d.ts +0 -2
|
@@ -0,0 +1,875 @@
|
|
|
1
|
+
import { sharedConfig } from 'solid-js';
|
|
2
|
+
import { fromCrossJSON, Feature, toCrossJSONStream } from 'seroval';
|
|
3
|
+
import { AbortSignalPlugin, CustomEventPlugin, DOMExceptionPlugin, EventPlugin, FormDataPlugin, HeadersPlugin, ReadableStreamPlugin, RequestPlugin, ResponsePlugin, URLSearchParamsPlugin, URLPlugin } from 'seroval-plugins/web';
|
|
4
|
+
|
|
5
|
+
const ENVELOPE = Symbol.for("solid.ResponseEnvelope");
|
|
6
|
+
function isResponseEnvelope(value) {
|
|
7
|
+
return !!(value && typeof value === "object" && value[ENVELOPE]);
|
|
8
|
+
}
|
|
9
|
+
const REVALIDATE_HEADER = "X-Revalidate";
|
|
10
|
+
|
|
11
|
+
Feature.AggregateError | Feature.BigIntTypedArray;
|
|
12
|
+
const serializeOnlyDisabledFeatures = () => process.env.NODE_ENV === "development" ? 0 : Feature.ErrorPrototypeStack;
|
|
13
|
+
const DEFAULT_WEB_PLUGINS = Object.freeze([AbortSignalPlugin,
|
|
14
|
+
CustomEventPlugin, DOMExceptionPlugin, EventPlugin,
|
|
15
|
+
FormDataPlugin, HeadersPlugin, ReadableStreamPlugin, RequestPlugin, ResponsePlugin, URLSearchParamsPlugin, URLPlugin]);
|
|
16
|
+
function resolveSerializerPlugins(customPlugins) {
|
|
17
|
+
return customPlugins ? [...customPlugins, ...DEFAULT_WEB_PLUGINS] : [...DEFAULT_WEB_PLUGINS];
|
|
18
|
+
}
|
|
19
|
+
const JSON_CODEC_DISABLED_FEATURES = Feature.RegExp;
|
|
20
|
+
const JSON_CODEC_DEPTH_LIMIT = 64;
|
|
21
|
+
function resolveCodecOptions({
|
|
22
|
+
plugins,
|
|
23
|
+
disabledFeatures,
|
|
24
|
+
depthLimit
|
|
25
|
+
} = {}) {
|
|
26
|
+
return {
|
|
27
|
+
plugins: resolveSerializerPlugins(plugins),
|
|
28
|
+
disabledFeatures: disabledFeatures === undefined ? JSON_CODEC_DISABLED_FEATURES : disabledFeatures,
|
|
29
|
+
depthLimit: depthLimit === undefined ? JSON_CODEC_DEPTH_LIMIT : depthLimit
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
function serializeJSON(value, {
|
|
33
|
+
onParse,
|
|
34
|
+
onDone,
|
|
35
|
+
onError,
|
|
36
|
+
...codecOptions
|
|
37
|
+
}) {
|
|
38
|
+
const resolved = resolveCodecOptions(codecOptions);
|
|
39
|
+
return toCrossJSONStream(value, {
|
|
40
|
+
onParse,
|
|
41
|
+
onDone,
|
|
42
|
+
onError,
|
|
43
|
+
...resolved,
|
|
44
|
+
disabledFeatures: resolved.disabledFeatures | serializeOnlyDisabledFeatures()
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
function createJSONDeserializer(options) {
|
|
48
|
+
const refs = new Map();
|
|
49
|
+
const resolved = resolveCodecOptions(options);
|
|
50
|
+
return function deserializeJSONChunk(node) {
|
|
51
|
+
return fromCrossJSON(node, {
|
|
52
|
+
refs,
|
|
53
|
+
...resolved
|
|
54
|
+
});
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const RequestContext = Symbol.for("solid.RequestContext");
|
|
59
|
+
function getRequestEvent() {
|
|
60
|
+
return globalThis[RequestContext] ? globalThis[RequestContext].getStore() || sharedConfig.context && sharedConfig.context.event || console.warn("RequestEvent is missing. This is most likely due to accessing `getRequestEvent` non-managed async scope in a partially polyfilled environment. Try moving it above all `await` calls.") : undefined;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const codecConfig = {
|
|
64
|
+
codec: undefined
|
|
65
|
+
};
|
|
66
|
+
function configureServerFunctionsCodec(codec) {
|
|
67
|
+
codecConfig.codec = codec;
|
|
68
|
+
}
|
|
69
|
+
function getServerFunctionsCodec() {
|
|
70
|
+
return codecConfig.codec;
|
|
71
|
+
}
|
|
72
|
+
function subscribeFlightData(consumer) {
|
|
73
|
+
return () => {
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
const SERVER_FUNCTION_METADATA = Symbol.for("solid.ServerFunctionMetadata");
|
|
77
|
+
function getServerFunctionMetadata(fn) {
|
|
78
|
+
if (typeof fn !== "function") return undefined;
|
|
79
|
+
return fn[SERVER_FUNCTION_METADATA] || undefined;
|
|
80
|
+
}
|
|
81
|
+
function isServerFunction(fn) {
|
|
82
|
+
return typeof fn === "function" && !!fn[SERVER_FUNCTION_METADATA];
|
|
83
|
+
}
|
|
84
|
+
function withMeta(fn, meta) {
|
|
85
|
+
const metadata = getServerFunctionMetadata(fn);
|
|
86
|
+
if (!metadata) {
|
|
87
|
+
throw new Error("withMeta expects a server function reference");
|
|
88
|
+
}
|
|
89
|
+
Object.assign(metadata, meta);
|
|
90
|
+
return fn;
|
|
91
|
+
}
|
|
92
|
+
const FUNCTION_HEADER = "X-Server-Function-Id";
|
|
93
|
+
const ERROR_HEADER = "X-Server-Function-Error";
|
|
94
|
+
const ERROR_HEADER_MARKER = "=?1?";
|
|
95
|
+
const NEEDS_ENCODING = /[^\x20-\x7e\xa0-\xff]/;
|
|
96
|
+
function encodeErrorHeaderValue(value) {
|
|
97
|
+
let stripped = String(value).replace(/[\r\n]+/g, "");
|
|
98
|
+
if (!NEEDS_ENCODING.test(stripped) && !stripped.startsWith(ERROR_HEADER_MARKER) && stripped === stripped.trim()) {
|
|
99
|
+
return stripped;
|
|
100
|
+
}
|
|
101
|
+
if (typeof stripped.toWellFormed === "function") {
|
|
102
|
+
stripped = stripped.toWellFormed();
|
|
103
|
+
} else {
|
|
104
|
+
stripped = stripped.replace(/[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/g, "\uFFFD");
|
|
105
|
+
}
|
|
106
|
+
return ERROR_HEADER_MARKER + encodeURIComponent(stripped);
|
|
107
|
+
}
|
|
108
|
+
function decodeErrorHeaderValue(value) {
|
|
109
|
+
if (typeof value !== "string" || !value.startsWith(ERROR_HEADER_MARKER)) {
|
|
110
|
+
return value;
|
|
111
|
+
}
|
|
112
|
+
try {
|
|
113
|
+
return decodeURIComponent(value.slice(ERROR_HEADER_MARKER.length));
|
|
114
|
+
} catch {
|
|
115
|
+
return value;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
const INSTANCE_HEADER = "X-Server-Function-Instance";
|
|
119
|
+
const BODY_FORMAT_HEADER = "X-Server-Function-Format";
|
|
120
|
+
const SINGLE_FLIGHT_HEADER = "X-Single-Flight";
|
|
121
|
+
const FILE_FORM_KEY = "__server_function_file__";
|
|
122
|
+
const FLASH_COOKIE = "flash";
|
|
123
|
+
const FLASH_MATCHER = new RegExp(`(?:^|;\\s*)${FLASH_COOKIE}=([^;]+)`);
|
|
124
|
+
function hasFlashCookie(cookieHeader) {
|
|
125
|
+
return !!cookieHeader && FLASH_MATCHER.test(cookieHeader);
|
|
126
|
+
}
|
|
127
|
+
function matchFlashCookie(cookieHeader) {
|
|
128
|
+
const match = cookieHeader && cookieHeader.match(FLASH_MATCHER);
|
|
129
|
+
return match ? match[1] : undefined;
|
|
130
|
+
}
|
|
131
|
+
function clearFlashCookie() {
|
|
132
|
+
return `${FLASH_COOKIE}=; Max-Age=0; Path=/`;
|
|
133
|
+
}
|
|
134
|
+
const BodyFormat = {
|
|
135
|
+
Serialized: "0",
|
|
136
|
+
String: "1",
|
|
137
|
+
FormData: "2",
|
|
138
|
+
URLSearchParams: "3",
|
|
139
|
+
Blob: "4",
|
|
140
|
+
File: "5",
|
|
141
|
+
ArrayBuffer: "6",
|
|
142
|
+
Uint8Array: "7",
|
|
143
|
+
Json: "8"
|
|
144
|
+
};
|
|
145
|
+
function getHeadersAndBody(body) {
|
|
146
|
+
switch (true) {
|
|
147
|
+
case typeof body === "string":
|
|
148
|
+
return {
|
|
149
|
+
headers: {
|
|
150
|
+
"Content-Type": "text/plain",
|
|
151
|
+
[BODY_FORMAT_HEADER]: BodyFormat.String
|
|
152
|
+
},
|
|
153
|
+
body
|
|
154
|
+
};
|
|
155
|
+
case body instanceof FormData:
|
|
156
|
+
return {
|
|
157
|
+
headers: {
|
|
158
|
+
[BODY_FORMAT_HEADER]: BodyFormat.FormData
|
|
159
|
+
},
|
|
160
|
+
body
|
|
161
|
+
};
|
|
162
|
+
case body instanceof URLSearchParams:
|
|
163
|
+
return {
|
|
164
|
+
headers: {
|
|
165
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
166
|
+
[BODY_FORMAT_HEADER]: BodyFormat.URLSearchParams
|
|
167
|
+
},
|
|
168
|
+
body
|
|
169
|
+
};
|
|
170
|
+
case typeof File !== "undefined" && body instanceof File:
|
|
171
|
+
{
|
|
172
|
+
const formData = new FormData();
|
|
173
|
+
formData.append(FILE_FORM_KEY, body, body.name);
|
|
174
|
+
return {
|
|
175
|
+
headers: {
|
|
176
|
+
[BODY_FORMAT_HEADER]: BodyFormat.File
|
|
177
|
+
},
|
|
178
|
+
body: formData
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
case body instanceof Blob:
|
|
182
|
+
return {
|
|
183
|
+
headers: {
|
|
184
|
+
[BODY_FORMAT_HEADER]: BodyFormat.Blob
|
|
185
|
+
},
|
|
186
|
+
body
|
|
187
|
+
};
|
|
188
|
+
case body instanceof ArrayBuffer:
|
|
189
|
+
return {
|
|
190
|
+
headers: {
|
|
191
|
+
[BODY_FORMAT_HEADER]: BodyFormat.ArrayBuffer
|
|
192
|
+
},
|
|
193
|
+
body
|
|
194
|
+
};
|
|
195
|
+
case body instanceof Uint8Array:
|
|
196
|
+
return {
|
|
197
|
+
headers: {
|
|
198
|
+
[BODY_FORMAT_HEADER]: BodyFormat.Uint8Array
|
|
199
|
+
},
|
|
200
|
+
body: new Uint8Array(body)
|
|
201
|
+
};
|
|
202
|
+
default:
|
|
203
|
+
return undefined;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
async function extractBody(source, codecOptions) {
|
|
207
|
+
const contentType = source.headers.get("content-type");
|
|
208
|
+
const format = source.headers.get(BODY_FORMAT_HEADER);
|
|
209
|
+
const clone = source.clone();
|
|
210
|
+
switch (true) {
|
|
211
|
+
case format === BodyFormat.Serialized:
|
|
212
|
+
return await deserializeStream(clone, codecOptions);
|
|
213
|
+
case format === BodyFormat.Json:
|
|
214
|
+
return JSON.parse(await clone.text());
|
|
215
|
+
case format === BodyFormat.String:
|
|
216
|
+
return await clone.text();
|
|
217
|
+
case format === BodyFormat.File:
|
|
218
|
+
{
|
|
219
|
+
const formData = await clone.formData();
|
|
220
|
+
return formData.get(FILE_FORM_KEY);
|
|
221
|
+
}
|
|
222
|
+
case format === BodyFormat.FormData:
|
|
223
|
+
case contentType && contentType.startsWith("multipart/form-data"):
|
|
224
|
+
return await clone.formData();
|
|
225
|
+
case format === BodyFormat.URLSearchParams:
|
|
226
|
+
case contentType && contentType.startsWith("application/x-www-form-urlencoded"):
|
|
227
|
+
return new URLSearchParams(await clone.text());
|
|
228
|
+
case format === BodyFormat.Blob:
|
|
229
|
+
return await clone.blob();
|
|
230
|
+
case format === BodyFormat.ArrayBuffer:
|
|
231
|
+
return await clone.arrayBuffer();
|
|
232
|
+
case format === BodyFormat.Uint8Array:
|
|
233
|
+
return new Uint8Array(await clone.arrayBuffer());
|
|
234
|
+
}
|
|
235
|
+
return undefined;
|
|
236
|
+
}
|
|
237
|
+
function createChunk(data) {
|
|
238
|
+
const encodeData = new TextEncoder().encode(data);
|
|
239
|
+
const bytes = encodeData.length;
|
|
240
|
+
const baseHex = bytes.toString(16);
|
|
241
|
+
const totalHex = "00000000".substring(0, 8 - baseHex.length) + baseHex;
|
|
242
|
+
const head = new TextEncoder().encode(`;0x${totalHex};`);
|
|
243
|
+
const chunk = new Uint8Array(12 + bytes);
|
|
244
|
+
chunk.set(head);
|
|
245
|
+
chunk.set(encodeData, 12);
|
|
246
|
+
return chunk;
|
|
247
|
+
}
|
|
248
|
+
class ChunkReader {
|
|
249
|
+
constructor(stream) {
|
|
250
|
+
this.reader = stream.getReader();
|
|
251
|
+
this.buffer = new Uint8Array(0);
|
|
252
|
+
this.done = false;
|
|
253
|
+
}
|
|
254
|
+
async readChunk() {
|
|
255
|
+
const chunk = await this.reader.read();
|
|
256
|
+
if (!chunk.done) {
|
|
257
|
+
const newBuffer = new Uint8Array(this.buffer.length + chunk.value.length);
|
|
258
|
+
newBuffer.set(this.buffer);
|
|
259
|
+
newBuffer.set(chunk.value, this.buffer.length);
|
|
260
|
+
this.buffer = newBuffer;
|
|
261
|
+
} else {
|
|
262
|
+
this.done = true;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
async next() {
|
|
266
|
+
while (this.buffer.length < 12) {
|
|
267
|
+
if (this.done) {
|
|
268
|
+
if (this.buffer.length === 0) return {
|
|
269
|
+
done: true,
|
|
270
|
+
value: undefined
|
|
271
|
+
};
|
|
272
|
+
throw new Error("Malformed server function stream.");
|
|
273
|
+
}
|
|
274
|
+
await this.readChunk();
|
|
275
|
+
}
|
|
276
|
+
const head = new TextDecoder().decode(this.buffer.subarray(1, 11));
|
|
277
|
+
const bytes = Number.parseInt(head, 16);
|
|
278
|
+
if (Number.isNaN(bytes)) {
|
|
279
|
+
throw new Error("Malformed server function stream.");
|
|
280
|
+
}
|
|
281
|
+
while (bytes > this.buffer.length - 12) {
|
|
282
|
+
if (this.done) {
|
|
283
|
+
throw new Error("Malformed server function stream.");
|
|
284
|
+
}
|
|
285
|
+
await this.readChunk();
|
|
286
|
+
}
|
|
287
|
+
const partial = new TextDecoder().decode(this.buffer.subarray(12, 12 + bytes));
|
|
288
|
+
this.buffer = this.buffer.subarray(12 + bytes);
|
|
289
|
+
return {
|
|
290
|
+
done: false,
|
|
291
|
+
value: partial
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
async drain(interpret) {
|
|
295
|
+
while (true) {
|
|
296
|
+
const result = await this.next();
|
|
297
|
+
if (result.done) {
|
|
298
|
+
break;
|
|
299
|
+
}
|
|
300
|
+
interpret(result.value);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
function serializeStream(value, codecOptions) {
|
|
305
|
+
return new ReadableStream({
|
|
306
|
+
start(controller) {
|
|
307
|
+
serializeJSON(value, {
|
|
308
|
+
...codecOptions,
|
|
309
|
+
onParse(node) {
|
|
310
|
+
controller.enqueue(createChunk(JSON.stringify(node)));
|
|
311
|
+
},
|
|
312
|
+
onDone() {
|
|
313
|
+
controller.close();
|
|
314
|
+
},
|
|
315
|
+
onError(error) {
|
|
316
|
+
controller.error(error);
|
|
317
|
+
}
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
async function deserializeStream(source, codecOptions) {
|
|
323
|
+
if (!source.body) {
|
|
324
|
+
throw new Error("missing body");
|
|
325
|
+
}
|
|
326
|
+
const reader = new ChunkReader(source.body);
|
|
327
|
+
const result = await reader.next();
|
|
328
|
+
if (!result.done) {
|
|
329
|
+
const deserializeChunk = createJSONDeserializer(codecOptions);
|
|
330
|
+
function interpretChunk(chunk) {
|
|
331
|
+
return deserializeChunk(JSON.parse(chunk));
|
|
332
|
+
}
|
|
333
|
+
void reader.drain(interpretChunk);
|
|
334
|
+
return interpretChunk(result.value);
|
|
335
|
+
}
|
|
336
|
+
return undefined;
|
|
337
|
+
}
|
|
338
|
+
async function deserializeString(text, codecOptions) {
|
|
339
|
+
return await deserializeStream(new Response(text), codecOptions);
|
|
340
|
+
}
|
|
341
|
+
async function decodeResponse(response, codecOptions) {
|
|
342
|
+
if (!response.body) return undefined;
|
|
343
|
+
return await extractBody(response, codecOptions === undefined ? codecConfig.codec : codecOptions);
|
|
344
|
+
}
|
|
345
|
+
async function decodeResponsePayload(response, codecOptions) {
|
|
346
|
+
const decoded = await decodeResponse(response, codecOptions);
|
|
347
|
+
if (decoded !== undefined && response.headers.has(SINGLE_FLIGHT_HEADER)) {
|
|
348
|
+
return {
|
|
349
|
+
value: decoded.value,
|
|
350
|
+
flightData: decoded.data
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
return {
|
|
354
|
+
value: decoded
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
function encodeInputValue(value) {
|
|
359
|
+
if (value instanceof FormData) return {
|
|
360
|
+
$f: [...value.entries()].filter(([, v]) => typeof v === "string")
|
|
361
|
+
};
|
|
362
|
+
if (value instanceof URLSearchParams) return {
|
|
363
|
+
$u: [...value.entries()]
|
|
364
|
+
};
|
|
365
|
+
return value;
|
|
366
|
+
}
|
|
367
|
+
function decodeInputValue(value) {
|
|
368
|
+
if (value && typeof value === "object") {
|
|
369
|
+
if (Array.isArray(value.$f)) {
|
|
370
|
+
const form = new FormData();
|
|
371
|
+
for (const [k, v] of value.$f) form.append(k, v);
|
|
372
|
+
return form;
|
|
373
|
+
}
|
|
374
|
+
if (Array.isArray(value.$u)) return new URLSearchParams(value.$u);
|
|
375
|
+
}
|
|
376
|
+
return value;
|
|
377
|
+
}
|
|
378
|
+
function encodeFlashCookie(url, result, input, thrown) {
|
|
379
|
+
const isError = result instanceof Error;
|
|
380
|
+
const payload = {
|
|
381
|
+
url,
|
|
382
|
+
result: isError ? result.message : result,
|
|
383
|
+
error: isError,
|
|
384
|
+
thrown: !!thrown,
|
|
385
|
+
input: input.map(encodeInputValue)
|
|
386
|
+
};
|
|
387
|
+
return `${FLASH_COOKIE}=${encodeURIComponent(JSON.stringify(payload))}; Secure; HttpOnly; Path=/`;
|
|
388
|
+
}
|
|
389
|
+
function decodeFlashCookie(cookieHeader) {
|
|
390
|
+
const match = matchFlashCookie(cookieHeader);
|
|
391
|
+
if (!match) return;
|
|
392
|
+
try {
|
|
393
|
+
const payload = JSON.parse(decodeURIComponent(match));
|
|
394
|
+
if (!payload || !payload.result) return;
|
|
395
|
+
const result = payload.error ? new Error(payload.result) : payload.result;
|
|
396
|
+
return {
|
|
397
|
+
input: Array.isArray(payload.input) ? payload.input.map(decodeInputValue) : [],
|
|
398
|
+
url: payload.url,
|
|
399
|
+
result: payload.thrown ? undefined : result,
|
|
400
|
+
error: payload.thrown ? result : undefined
|
|
401
|
+
};
|
|
402
|
+
} catch (error) {
|
|
403
|
+
console.error(error);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
const config = {
|
|
408
|
+
provideEvent: undefined,
|
|
409
|
+
collectFlightData: undefined,
|
|
410
|
+
transformResult: undefined,
|
|
411
|
+
transformFlightResult: undefined,
|
|
412
|
+
transformDirectResult: undefined,
|
|
413
|
+
handleNoJS: undefined,
|
|
414
|
+
endpoint: "/_server"
|
|
415
|
+
};
|
|
416
|
+
function configureServerFunctionsServer({
|
|
417
|
+
provideEvent,
|
|
418
|
+
collectFlightData,
|
|
419
|
+
transformResult,
|
|
420
|
+
transformFlightResult,
|
|
421
|
+
transformDirectResult,
|
|
422
|
+
handleNoJS,
|
|
423
|
+
endpoint,
|
|
424
|
+
codec
|
|
425
|
+
} = {}) {
|
|
426
|
+
if (provideEvent !== undefined) config.provideEvent = provideEvent;
|
|
427
|
+
if (collectFlightData !== undefined) config.collectFlightData = collectFlightData;
|
|
428
|
+
if (transformResult !== undefined) config.transformResult = transformResult;
|
|
429
|
+
if (transformFlightResult !== undefined) config.transformFlightResult = transformFlightResult;
|
|
430
|
+
if (transformDirectResult !== undefined) config.transformDirectResult = transformDirectResult;
|
|
431
|
+
if (handleNoJS !== undefined) config.handleNoJS = handleNoJS;
|
|
432
|
+
if (endpoint !== undefined) config.endpoint = endpoint;
|
|
433
|
+
if (codec !== undefined) configureServerFunctionsCodec(codec);
|
|
434
|
+
}
|
|
435
|
+
function provideEvent(event, fn) {
|
|
436
|
+
if (config.provideEvent) return config.provideEvent(event, fn);
|
|
437
|
+
const ctx = globalThis[RequestContext];
|
|
438
|
+
if (ctx) return ctx.run(event, fn);
|
|
439
|
+
throw new Error("No request event provider. Configure one with configureServerFunctionsServer({ provideEvent }).");
|
|
440
|
+
}
|
|
441
|
+
const REGISTRATIONS = new Map();
|
|
442
|
+
const METHODS = new Map();
|
|
443
|
+
const INVOCATIONS = new WeakMap();
|
|
444
|
+
function registerServerFunction(id, callback) {
|
|
445
|
+
REGISTRATIONS.set(id, callback);
|
|
446
|
+
return callback;
|
|
447
|
+
}
|
|
448
|
+
function getServerFunction(id) {
|
|
449
|
+
const fn = REGISTRATIONS.get(id);
|
|
450
|
+
if (fn) {
|
|
451
|
+
return fn;
|
|
452
|
+
}
|
|
453
|
+
throw new Error("invalid server function: " + id);
|
|
454
|
+
}
|
|
455
|
+
function registerServerReference(id, fn, name) {
|
|
456
|
+
registerServerFunction(id, fn);
|
|
457
|
+
return {
|
|
458
|
+
id,
|
|
459
|
+
fn,
|
|
460
|
+
name
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
function createServerReference({
|
|
464
|
+
id,
|
|
465
|
+
fn,
|
|
466
|
+
name
|
|
467
|
+
}) {
|
|
468
|
+
if (typeof fn !== "function") throw new Error("Export from a 'use server' module must be a function");
|
|
469
|
+
const metadata = name === undefined ? {} : {
|
|
470
|
+
name
|
|
471
|
+
};
|
|
472
|
+
return new Proxy(fn, {
|
|
473
|
+
get(target, prop) {
|
|
474
|
+
if (prop === "id") return id;
|
|
475
|
+
if (prop === "url") {
|
|
476
|
+
return `${config.endpoint}?id=${encodeURIComponent(id)}`;
|
|
477
|
+
}
|
|
478
|
+
if (prop === SERVER_FUNCTION_METADATA) return metadata;
|
|
479
|
+
return target[prop];
|
|
480
|
+
},
|
|
481
|
+
apply(target, thisArg, args) {
|
|
482
|
+
const ogEvt = getRequestEvent();
|
|
483
|
+
if (!ogEvt) throw new Error("Cannot call server function outside of a request");
|
|
484
|
+
const evt = {
|
|
485
|
+
...ogEvt
|
|
486
|
+
};
|
|
487
|
+
INVOCATIONS.set(evt, {
|
|
488
|
+
id
|
|
489
|
+
});
|
|
490
|
+
evt.serverOnly = true;
|
|
491
|
+
const result = provideEvent(evt, () => {
|
|
492
|
+
return fn.apply(thisArg, args);
|
|
493
|
+
});
|
|
494
|
+
const transform = config.transformDirectResult;
|
|
495
|
+
if (transform && result && typeof result.then === "function") {
|
|
496
|
+
return result.then(value => transform(value, {
|
|
497
|
+
id,
|
|
498
|
+
args,
|
|
499
|
+
event: evt
|
|
500
|
+
}));
|
|
501
|
+
}
|
|
502
|
+
return transform ? transform(result, {
|
|
503
|
+
id,
|
|
504
|
+
args,
|
|
505
|
+
event: evt
|
|
506
|
+
}) : result;
|
|
507
|
+
}
|
|
508
|
+
});
|
|
509
|
+
}
|
|
510
|
+
function GET(fn) {
|
|
511
|
+
if (!isServerFunction(fn) || typeof fn.id !== "string") {
|
|
512
|
+
throw new Error("GET expects a server function reference");
|
|
513
|
+
}
|
|
514
|
+
METHODS.set(fn.id, "GET");
|
|
515
|
+
return withMeta(fn, {
|
|
516
|
+
method: "GET"
|
|
517
|
+
});
|
|
518
|
+
}
|
|
519
|
+
function getServerFunctionInvocation() {
|
|
520
|
+
return getEventServerFunctionInvocation(getRequestEvent());
|
|
521
|
+
}
|
|
522
|
+
function getEventServerFunctionInvocation(event) {
|
|
523
|
+
return event && INVOCATIONS.get(event);
|
|
524
|
+
}
|
|
525
|
+
function resolveFunctionId(request, url) {
|
|
526
|
+
const reference = request.headers.get(FUNCTION_HEADER);
|
|
527
|
+
if (reference) {
|
|
528
|
+
return reference.split("#")[0];
|
|
529
|
+
}
|
|
530
|
+
return url.searchParams.get("id");
|
|
531
|
+
}
|
|
532
|
+
async function parseArguments(request, url, instance, codec) {
|
|
533
|
+
const parsed = [];
|
|
534
|
+
const bodyFormat = request.method === "POST" ? request.headers.get(BODY_FORMAT_HEADER) : null;
|
|
535
|
+
if (!instance || request.method === "GET" || bodyFormat !== BodyFormat.Serialized) {
|
|
536
|
+
const args = url.searchParams.get("args");
|
|
537
|
+
if (args) {
|
|
538
|
+
const result = args.startsWith(";0x") ? await deserializeString(args, codec) : JSON.parse(args);
|
|
539
|
+
for (const arg of result) {
|
|
540
|
+
parsed.push(arg);
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
if (request.method === "POST" && request.body !== null) {
|
|
545
|
+
const decoded = await extractBody(request.clone(), codec);
|
|
546
|
+
if (bodyFormat === BodyFormat.Serialized || bodyFormat === BodyFormat.Json) {
|
|
547
|
+
return decoded;
|
|
548
|
+
}
|
|
549
|
+
parsed.push(decoded);
|
|
550
|
+
}
|
|
551
|
+
return parsed;
|
|
552
|
+
}
|
|
553
|
+
async function foldFlightData(hook, event, headers, outcome, context = {}) {
|
|
554
|
+
if (outcome.value instanceof Response && outcome.value.body) return outcome.value;
|
|
555
|
+
digestOutcome(event, outcome);
|
|
556
|
+
const data = await hook(event, outcome);
|
|
557
|
+
if (data === undefined) return outcome.value;
|
|
558
|
+
headers.set(SINGLE_FLIGHT_HEADER, "true");
|
|
559
|
+
if (context.transformFlightResult) {
|
|
560
|
+
const transformed = await context.transformFlightResult(event, {
|
|
561
|
+
value: outcome.value,
|
|
562
|
+
data
|
|
563
|
+
}, context);
|
|
564
|
+
if (transformed !== undefined) {
|
|
565
|
+
for (const cookie of headers.getSetCookie()) transformed.headers.append("Set-Cookie", cookie);
|
|
566
|
+
headers.forEach((value, key) => {
|
|
567
|
+
if (key !== "set-cookie" && !transformed.headers.has(key)) {
|
|
568
|
+
transformed.headers.set(key, value);
|
|
569
|
+
}
|
|
570
|
+
});
|
|
571
|
+
return transformed;
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
return {
|
|
575
|
+
value: outcome.value,
|
|
576
|
+
data
|
|
577
|
+
};
|
|
578
|
+
}
|
|
579
|
+
function digestOutcome(event, outcome) {
|
|
580
|
+
const {
|
|
581
|
+
request,
|
|
582
|
+
response
|
|
583
|
+
} = outcome;
|
|
584
|
+
outcome.revalidateKeys = response?.headers.get(REVALIDATE_HEADER)?.split(",");
|
|
585
|
+
outcome.foldedHeaders = foldSetCookies(request.headers, [...(event.response?.headers?.getSetCookie() ?? []), ...(response?.headers?.getSetCookie() ?? [])]);
|
|
586
|
+
try {
|
|
587
|
+
const referrer = request.headers.get("referer");
|
|
588
|
+
if (referrer) {
|
|
589
|
+
const location = response?.headers.get("Location");
|
|
590
|
+
const target = location ? new URL(location, request.url) : new URL(referrer);
|
|
591
|
+
if (target.origin === new URL(request.url).origin) outcome.targetUrl = target.toString();
|
|
592
|
+
}
|
|
593
|
+
} catch {
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
function parseSetCookie(setCookie) {
|
|
597
|
+
const [pair, ...attributes] = setCookie.split(";");
|
|
598
|
+
const eq = pair.indexOf("=");
|
|
599
|
+
if (eq < 0) return undefined;
|
|
600
|
+
const parsed = {
|
|
601
|
+
name: pair.slice(0, eq).trim(),
|
|
602
|
+
value: pair.slice(eq + 1).trim()
|
|
603
|
+
};
|
|
604
|
+
for (const attribute of attributes) {
|
|
605
|
+
const attrEq = attribute.indexOf("=");
|
|
606
|
+
const key = (attrEq < 0 ? attribute : attribute.slice(0, attrEq)).trim().toLowerCase();
|
|
607
|
+
const value = attrEq < 0 ? "" : attribute.slice(attrEq + 1).trim();
|
|
608
|
+
if (key === "max-age") parsed.maxAge = Number(value);else if (key === "expires") parsed.expires = new Date(value);
|
|
609
|
+
}
|
|
610
|
+
return parsed;
|
|
611
|
+
}
|
|
612
|
+
function foldSetCookies(headers, setCookies) {
|
|
613
|
+
const folded = new Headers(headers);
|
|
614
|
+
if (!setCookies.length) return folded;
|
|
615
|
+
const cookies = {};
|
|
616
|
+
for (const pair of folded.get("cookie")?.split(";") ?? []) {
|
|
617
|
+
const eq = pair.indexOf("=");
|
|
618
|
+
if (eq > -1) cookies[pair.slice(0, eq).trim()] = pair.slice(eq + 1).trim();
|
|
619
|
+
}
|
|
620
|
+
for (const setCookie of setCookies) {
|
|
621
|
+
const parsed = parseSetCookie(setCookie);
|
|
622
|
+
if (!parsed) continue;
|
|
623
|
+
if (parsed.maxAge != null && parsed.maxAge <= 0 || parsed.expires != null && parsed.expires.getTime() <= Date.now()) {
|
|
624
|
+
delete cookies[parsed.name];
|
|
625
|
+
} else {
|
|
626
|
+
cookies[parsed.name] = parsed.value;
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
folded.delete("cookie");
|
|
630
|
+
const serialized = Object.entries(cookies).map(([name, value]) => `${name}=${value}`).join("; ");
|
|
631
|
+
if (serialized) folded.set("cookie", serialized);
|
|
632
|
+
return folded;
|
|
633
|
+
}
|
|
634
|
+
const validRedirectStatuses = new Set([301, 302, 303, 307, 308]);
|
|
635
|
+
function createNoJSHandler({
|
|
636
|
+
base = ""
|
|
637
|
+
} = {}) {
|
|
638
|
+
return function handleNoJS(result, request, args, thrown) {
|
|
639
|
+
const url = new URL(request.url);
|
|
640
|
+
let back = new URL(base || "/", url.origin).toString();
|
|
641
|
+
try {
|
|
642
|
+
const referer = request.headers.get("referer");
|
|
643
|
+
if (referer) back = new URL(referer).toString();
|
|
644
|
+
} catch {}
|
|
645
|
+
let status = 303;
|
|
646
|
+
let headers;
|
|
647
|
+
if (result instanceof Response) {
|
|
648
|
+
headers = new Headers(result.headers);
|
|
649
|
+
if (result.headers.has("Location")) {
|
|
650
|
+
headers.set("Location", new URL(result.headers.get("Location"), url.origin + base).toString());
|
|
651
|
+
if (validRedirectStatuses.has(result.status)) status = result.status;
|
|
652
|
+
} else {
|
|
653
|
+
headers.set("Location", back);
|
|
654
|
+
}
|
|
655
|
+
headers.delete("Content-Type");
|
|
656
|
+
headers.delete("Content-Length");
|
|
657
|
+
} else {
|
|
658
|
+
headers = new Headers({
|
|
659
|
+
Location: back
|
|
660
|
+
});
|
|
661
|
+
}
|
|
662
|
+
if (result && !(result instanceof Response)) {
|
|
663
|
+
headers.append("Set-Cookie", encodeFlashCookie(url.pathname + url.search, result, args, thrown));
|
|
664
|
+
}
|
|
665
|
+
return new Response(null, {
|
|
666
|
+
status,
|
|
667
|
+
headers
|
|
668
|
+
});
|
|
669
|
+
};
|
|
670
|
+
}
|
|
671
|
+
let defaultNoJSHandler;
|
|
672
|
+
function isFormPost(request) {
|
|
673
|
+
if (request.method !== "POST" || request.headers.has(BODY_FORMAT_HEADER)) return false;
|
|
674
|
+
const type = request.headers.get("content-type") || "";
|
|
675
|
+
return type.startsWith("application/x-www-form-urlencoded") || type.startsWith("multipart/form-data");
|
|
676
|
+
}
|
|
677
|
+
function serializedResponse(value, headers, codec) {
|
|
678
|
+
headers.set(BODY_FORMAT_HEADER, BodyFormat.Serialized);
|
|
679
|
+
headers.set("Content-Type", "text/plain");
|
|
680
|
+
return new Response(serializeStream(value, codec), {
|
|
681
|
+
headers
|
|
682
|
+
});
|
|
683
|
+
}
|
|
684
|
+
function encodeResult(value, headers, status, codec) {
|
|
685
|
+
const direct = getHeadersAndBody(value);
|
|
686
|
+
if (direct) {
|
|
687
|
+
for (const [key, val] of Object.entries(direct.headers || {})) {
|
|
688
|
+
headers.set(key, val);
|
|
689
|
+
}
|
|
690
|
+
return new Response(direct.body, {
|
|
691
|
+
status,
|
|
692
|
+
headers
|
|
693
|
+
});
|
|
694
|
+
}
|
|
695
|
+
const response = serializedResponse(value, headers, codec);
|
|
696
|
+
return status === 200 ? response : new Response(response.body, {
|
|
697
|
+
status,
|
|
698
|
+
headers
|
|
699
|
+
});
|
|
700
|
+
}
|
|
701
|
+
async function handleServerFunctionRequest(request, options = {}) {
|
|
702
|
+
const codec = options.codec !== undefined ? options.codec : getServerFunctionsCodec();
|
|
703
|
+
const url = new URL(request.url);
|
|
704
|
+
const instance = request.headers.get(INSTANCE_HEADER);
|
|
705
|
+
const functionId = resolveFunctionId(request, url);
|
|
706
|
+
if (!functionId) {
|
|
707
|
+
return new Response(process.env.NODE_ENV === "development" ? "Server function not found" : null, {
|
|
708
|
+
status: 404
|
|
709
|
+
});
|
|
710
|
+
}
|
|
711
|
+
let serverFunction;
|
|
712
|
+
try {
|
|
713
|
+
serverFunction = getServerFunction(functionId);
|
|
714
|
+
} catch {
|
|
715
|
+
return new Response(process.env.NODE_ENV === "development" ? `Unknown server function: ${functionId}` : null, {
|
|
716
|
+
status: 404
|
|
717
|
+
});
|
|
718
|
+
}
|
|
719
|
+
if (request.method === "GET" && METHODS.get(functionId) !== "GET") {
|
|
720
|
+
return new Response(process.env.NODE_ENV === "development" ? `Method not allowed for server function: ${functionId}` : null, {
|
|
721
|
+
status: 405,
|
|
722
|
+
headers: {
|
|
723
|
+
Allow: "POST"
|
|
724
|
+
}
|
|
725
|
+
});
|
|
726
|
+
}
|
|
727
|
+
const event = options.createEvent ? options.createEvent(request) : {
|
|
728
|
+
request,
|
|
729
|
+
locals: {}
|
|
730
|
+
};
|
|
731
|
+
const provide = options.provideEvent || provideEvent;
|
|
732
|
+
const flightHook = options.collectFlightData !== undefined ? options.collectFlightData : config.collectFlightData;
|
|
733
|
+
const transformResult = options.transformResult !== undefined ? options.transformResult : config.transformResult;
|
|
734
|
+
const transformFlightResult = options.transformFlightResult !== undefined ? options.transformFlightResult : config.transformFlightResult;
|
|
735
|
+
const handleNoJS = options.handleNoJS !== undefined ? options.handleNoJS : config.handleNoJS !== undefined ? config.handleNoJS : isFormPost(request) ? defaultNoJSHandler || (defaultNoJSHandler = createNoJSHandler()) : undefined;
|
|
736
|
+
const collectsFlight = !!(flightHook && instance && request.headers.has(SINGLE_FLIGHT_HEADER));
|
|
737
|
+
const parsed = await parseArguments(request, url, instance, codec);
|
|
738
|
+
const flightContext = {
|
|
739
|
+
id: functionId,
|
|
740
|
+
args: parsed,
|
|
741
|
+
instance,
|
|
742
|
+
request,
|
|
743
|
+
collectsFlight,
|
|
744
|
+
codec,
|
|
745
|
+
transformFlightResult
|
|
746
|
+
};
|
|
747
|
+
const headers = new Headers();
|
|
748
|
+
try {
|
|
749
|
+
let result = await provide(event, async () => {
|
|
750
|
+
INVOCATIONS.set(event, {
|
|
751
|
+
id: functionId
|
|
752
|
+
});
|
|
753
|
+
return serverFunction(...parsed);
|
|
754
|
+
});
|
|
755
|
+
if (transformResult) {
|
|
756
|
+
result = await transformResult(event, result, flightContext);
|
|
757
|
+
}
|
|
758
|
+
let status = 200;
|
|
759
|
+
let metadata;
|
|
760
|
+
if (isResponseEnvelope(result)) {
|
|
761
|
+
const {
|
|
762
|
+
response,
|
|
763
|
+
value
|
|
764
|
+
} = result;
|
|
765
|
+
if (!instance && !handleNoJS && response && response.body) {
|
|
766
|
+
return response;
|
|
767
|
+
}
|
|
768
|
+
if (response && response.headers) {
|
|
769
|
+
response.headers.forEach((val, key) => headers.append(key, val));
|
|
770
|
+
}
|
|
771
|
+
if (response && response.status && (response.status < 300 || response.status >= 400)) {
|
|
772
|
+
status = response.status;
|
|
773
|
+
}
|
|
774
|
+
metadata = response;
|
|
775
|
+
result = value;
|
|
776
|
+
} else if (result instanceof Response) {
|
|
777
|
+
if (result.headers && result.headers.has("X-Content-Raw")) return result;
|
|
778
|
+
if (instance) {
|
|
779
|
+
if (result.headers) {
|
|
780
|
+
result.headers.forEach((value, key) => headers.append(key, value));
|
|
781
|
+
}
|
|
782
|
+
if (result.status && (result.status < 300 || result.status >= 400)) {
|
|
783
|
+
status = result.status;
|
|
784
|
+
}
|
|
785
|
+
metadata = result;
|
|
786
|
+
if (result.body == null) {
|
|
787
|
+
result = null;
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
if (collectsFlight) {
|
|
792
|
+
result = await foldFlightData(flightHook, event, headers, {
|
|
793
|
+
id: functionId,
|
|
794
|
+
value: result,
|
|
795
|
+
response: metadata,
|
|
796
|
+
request,
|
|
797
|
+
thrown: false
|
|
798
|
+
}, flightContext);
|
|
799
|
+
if (result instanceof Response && result.headers.has("X-Content-Raw")) return result;
|
|
800
|
+
}
|
|
801
|
+
if (!instance) {
|
|
802
|
+
if (handleNoJS) return handleNoJS(result, request, parsed);
|
|
803
|
+
if (result instanceof Response) return result;
|
|
804
|
+
return encodeResult(result, headers, 200, codec);
|
|
805
|
+
}
|
|
806
|
+
return encodeResult(result, headers, status, codec);
|
|
807
|
+
} catch (x) {
|
|
808
|
+
if (x instanceof Response || isResponseEnvelope(x)) {
|
|
809
|
+
if (transformResult) {
|
|
810
|
+
x = await transformResult(event, x, {
|
|
811
|
+
...flightContext,
|
|
812
|
+
thrown: true
|
|
813
|
+
});
|
|
814
|
+
}
|
|
815
|
+
let status = 200;
|
|
816
|
+
let metadata;
|
|
817
|
+
if (isResponseEnvelope(x)) {
|
|
818
|
+
const {
|
|
819
|
+
response,
|
|
820
|
+
value
|
|
821
|
+
} = x;
|
|
822
|
+
if (response && response.headers) {
|
|
823
|
+
response.headers.forEach((val, key) => headers.append(key, val));
|
|
824
|
+
}
|
|
825
|
+
if (response && response.status && (!instance || response.status < 300 || response.status >= 400)) {
|
|
826
|
+
status = response.status;
|
|
827
|
+
}
|
|
828
|
+
metadata = response;
|
|
829
|
+
x = value;
|
|
830
|
+
} else if (x instanceof Response) {
|
|
831
|
+
if (x.headers) {
|
|
832
|
+
x.headers.forEach((value, key) => headers.append(key, value));
|
|
833
|
+
}
|
|
834
|
+
if (x.status && (!instance || x.status < 300 || x.status >= 400)) {
|
|
835
|
+
status = x.status;
|
|
836
|
+
}
|
|
837
|
+
metadata = x;
|
|
838
|
+
if (x.body == null) {
|
|
839
|
+
x = null;
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
if (collectsFlight) {
|
|
843
|
+
x = await foldFlightData(flightHook, event, headers, {
|
|
844
|
+
id: functionId,
|
|
845
|
+
value: x,
|
|
846
|
+
response: metadata,
|
|
847
|
+
request,
|
|
848
|
+
thrown: true
|
|
849
|
+
}, flightContext);
|
|
850
|
+
if (x instanceof Response && x.headers.has("X-Content-Raw")) {
|
|
851
|
+
x.headers.set(ERROR_HEADER, "true");
|
|
852
|
+
return x;
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
headers.set(ERROR_HEADER, "true");
|
|
856
|
+
if (!instance) {
|
|
857
|
+
if (handleNoJS) return handleNoJS(x ?? metadata, request, parsed, true);
|
|
858
|
+
if (x instanceof Response) return x;
|
|
859
|
+
}
|
|
860
|
+
return encodeResult(x, headers, status, codec);
|
|
861
|
+
}
|
|
862
|
+
if (!instance) {
|
|
863
|
+
if (handleNoJS) return handleNoJS(x, request, parsed, true);
|
|
864
|
+
const message = x instanceof Error ? x.message : String(x);
|
|
865
|
+
return new Response(process.env.NODE_ENV === "development" ? message : null, {
|
|
866
|
+
status: 500
|
|
867
|
+
});
|
|
868
|
+
}
|
|
869
|
+
const error = x instanceof Error ? x.message : typeof x === "string" ? x : "true";
|
|
870
|
+
headers.set(ERROR_HEADER, encodeErrorHeaderValue(error));
|
|
871
|
+
return encodeResult(x, headers, 200, codec);
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
export { ERROR_HEADER, FLASH_COOKIE, FUNCTION_HEADER, GET, INSTANCE_HEADER, SINGLE_FLIGHT_HEADER, clearFlashCookie, configureServerFunctionsServer, createNoJSHandler, createServerReference, decodeErrorHeaderValue, decodeFlashCookie, decodeResponse, decodeResponsePayload, encodeErrorHeaderValue, encodeFlashCookie, foldSetCookies, getEventServerFunctionInvocation, getServerFunction, getServerFunctionInvocation, getServerFunctionMetadata, handleServerFunctionRequest, hasFlashCookie, isServerFunction, registerServerFunction, registerServerReference, subscribeFlightData, withMeta };
|