@solidjs/web 2.0.0-beta.2 → 2.0.0-beta.20
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 +783 -223
- package/dist/dev.js +757 -217
- package/dist/server.cjs +742 -186
- package/dist/server.js +714 -183
- package/dist/web.cjs +772 -196
- package/dist/web.js +746 -190
- package/package.json +193 -38
- package/serialization/dist/serialization.cjs +83 -0
- package/serialization/dist/serialization.js +75 -0
- package/serialization/package.json +20 -0
- package/serialization/types/index.d.ts +139 -0
- package/serialization/types-cjs/index.d.cts +139 -0
- package/serialization/types-cjs/package.json +3 -0
- package/server-functions/dist/client.cjs +370 -0
- package/server-functions/dist/client.js +363 -0
- package/server-functions/dist/server.cjs +542 -0
- package/server-functions/dist/server.js +531 -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 +64 -21
- package/types/core.d.ts +3 -3
- package/types/index.d.ts +156 -24
- package/types/jsx-properties.d.ts +93 -0
- package/types/jsx.d.ts +4135 -1
- package/types/response.d.ts +93 -0
- package/types/serializer.d.ts +139 -0
- package/types/server-functions/client.d.ts +63 -0
- package/types/server-functions/server.d.ts +188 -0
- package/types/server-functions/shared.d.ts +171 -0
- package/types/server-mock.d.ts +89 -0
- package/types/server.d.ts +123 -28
- package/types-cjs/client.d.cts +131 -0
- package/types-cjs/core.d.cts +3 -0
- package/types-cjs/index.d.cts +178 -0
- package/types-cjs/jsx-properties.d.cts +93 -0
- package/types-cjs/jsx.d.cts +4135 -0
- package/types-cjs/package.json +3 -0
- package/types-cjs/response.d.cts +93 -0
- package/types-cjs/serializer.d.cts +139 -0
- package/types-cjs/server-functions/client.d.cts +63 -0
- package/types-cjs/server-functions/server.d.cts +188 -0
- package/types-cjs/server-functions/shared.d.cts +171 -0
- package/types-cjs/server-mock.d.cts +161 -0
- package/types-cjs/server.d.cts +251 -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,542 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var solidJs = require('solid-js');
|
|
4
|
+
var seroval = require('seroval');
|
|
5
|
+
var web = require('seroval-plugins/web');
|
|
6
|
+
|
|
7
|
+
const ENVELOPE = Symbol.for("solid.ResponseEnvelope");
|
|
8
|
+
function isResponseEnvelope(value) {
|
|
9
|
+
return !!(value && typeof value === "object" && value[ENVELOPE]);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
seroval.Feature.AggregateError | seroval.Feature.BigIntTypedArray;
|
|
13
|
+
const DEFAULT_WEB_PLUGINS = Object.freeze([web.AbortSignalPlugin,
|
|
14
|
+
web.CustomEventPlugin, web.DOMExceptionPlugin, web.EventPlugin,
|
|
15
|
+
web.FormDataPlugin, web.HeadersPlugin, web.ReadableStreamPlugin, web.RequestPlugin, web.ResponsePlugin, web.URLSearchParamsPlugin, web.URLPlugin]);
|
|
16
|
+
function resolveSerializerPlugins(customPlugins) {
|
|
17
|
+
return customPlugins ? [...customPlugins, ...DEFAULT_WEB_PLUGINS] : [...DEFAULT_WEB_PLUGINS];
|
|
18
|
+
}
|
|
19
|
+
const JSON_CODEC_DISABLED_FEATURES = seroval.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
|
+
return seroval.toCrossJSONStream(value, {
|
|
39
|
+
onParse,
|
|
40
|
+
onDone,
|
|
41
|
+
onError,
|
|
42
|
+
...resolveCodecOptions(codecOptions)
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
function createJSONDeserializer(options) {
|
|
46
|
+
const refs = new Map();
|
|
47
|
+
const resolved = resolveCodecOptions(options);
|
|
48
|
+
return function deserializeJSONChunk(node) {
|
|
49
|
+
return seroval.fromCrossJSON(node, {
|
|
50
|
+
refs,
|
|
51
|
+
...resolved
|
|
52
|
+
});
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const RequestContext = Symbol.for("solid.RequestContext");
|
|
57
|
+
function getRequestEvent() {
|
|
58
|
+
return globalThis[RequestContext] ? globalThis[RequestContext].getStore() || solidJs.sharedConfig.context && solidJs.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;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const codecConfig = {
|
|
62
|
+
codec: undefined
|
|
63
|
+
};
|
|
64
|
+
function configureServerFunctionsCodec(codec) {
|
|
65
|
+
codecConfig.codec = codec;
|
|
66
|
+
}
|
|
67
|
+
function getServerFunctionsCodec() {
|
|
68
|
+
return codecConfig.codec;
|
|
69
|
+
}
|
|
70
|
+
const FUNCTION_HEADER = "X-Server-Function-Id";
|
|
71
|
+
const INSTANCE_HEADER = "X-Server-Function-Instance";
|
|
72
|
+
const BODY_FORMAT_HEADER = "X-Server-Function-Format";
|
|
73
|
+
const FILE_FORM_KEY = "__server_function_file__";
|
|
74
|
+
const BodyFormat = {
|
|
75
|
+
Serialized: "0",
|
|
76
|
+
String: "1",
|
|
77
|
+
FormData: "2",
|
|
78
|
+
URLSearchParams: "3",
|
|
79
|
+
Blob: "4",
|
|
80
|
+
File: "5",
|
|
81
|
+
ArrayBuffer: "6",
|
|
82
|
+
Uint8Array: "7"
|
|
83
|
+
};
|
|
84
|
+
function getHeadersAndBody(body) {
|
|
85
|
+
switch (true) {
|
|
86
|
+
case typeof body === "string":
|
|
87
|
+
return {
|
|
88
|
+
headers: {
|
|
89
|
+
"Content-Type": "text/plain",
|
|
90
|
+
[BODY_FORMAT_HEADER]: BodyFormat.String
|
|
91
|
+
},
|
|
92
|
+
body
|
|
93
|
+
};
|
|
94
|
+
case body instanceof FormData:
|
|
95
|
+
return {
|
|
96
|
+
headers: {
|
|
97
|
+
[BODY_FORMAT_HEADER]: BodyFormat.FormData
|
|
98
|
+
},
|
|
99
|
+
body
|
|
100
|
+
};
|
|
101
|
+
case body instanceof URLSearchParams:
|
|
102
|
+
return {
|
|
103
|
+
headers: {
|
|
104
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
105
|
+
[BODY_FORMAT_HEADER]: BodyFormat.URLSearchParams
|
|
106
|
+
},
|
|
107
|
+
body
|
|
108
|
+
};
|
|
109
|
+
case typeof File !== "undefined" && body instanceof File:
|
|
110
|
+
{
|
|
111
|
+
const formData = new FormData();
|
|
112
|
+
formData.append(FILE_FORM_KEY, body, body.name);
|
|
113
|
+
return {
|
|
114
|
+
headers: {
|
|
115
|
+
[BODY_FORMAT_HEADER]: BodyFormat.File
|
|
116
|
+
},
|
|
117
|
+
body: formData
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
case body instanceof Blob:
|
|
121
|
+
return {
|
|
122
|
+
headers: {
|
|
123
|
+
[BODY_FORMAT_HEADER]: BodyFormat.Blob
|
|
124
|
+
},
|
|
125
|
+
body
|
|
126
|
+
};
|
|
127
|
+
case body instanceof ArrayBuffer:
|
|
128
|
+
return {
|
|
129
|
+
headers: {
|
|
130
|
+
[BODY_FORMAT_HEADER]: BodyFormat.ArrayBuffer
|
|
131
|
+
},
|
|
132
|
+
body
|
|
133
|
+
};
|
|
134
|
+
case body instanceof Uint8Array:
|
|
135
|
+
return {
|
|
136
|
+
headers: {
|
|
137
|
+
[BODY_FORMAT_HEADER]: BodyFormat.Uint8Array
|
|
138
|
+
},
|
|
139
|
+
body: new Uint8Array(body)
|
|
140
|
+
};
|
|
141
|
+
default:
|
|
142
|
+
return undefined;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
async function extractBody(source, codecOptions) {
|
|
146
|
+
const contentType = source.headers.get("content-type");
|
|
147
|
+
const format = source.headers.get(BODY_FORMAT_HEADER);
|
|
148
|
+
const clone = source.clone();
|
|
149
|
+
switch (true) {
|
|
150
|
+
case format === BodyFormat.Serialized:
|
|
151
|
+
return await deserializeStream(clone, codecOptions);
|
|
152
|
+
case format === BodyFormat.String:
|
|
153
|
+
return await clone.text();
|
|
154
|
+
case format === BodyFormat.File:
|
|
155
|
+
{
|
|
156
|
+
const formData = await clone.formData();
|
|
157
|
+
return formData.get(FILE_FORM_KEY);
|
|
158
|
+
}
|
|
159
|
+
case format === BodyFormat.FormData:
|
|
160
|
+
case contentType && contentType.startsWith("multipart/form-data"):
|
|
161
|
+
return await clone.formData();
|
|
162
|
+
case format === BodyFormat.URLSearchParams:
|
|
163
|
+
case contentType && contentType.startsWith("application/x-www-form-urlencoded"):
|
|
164
|
+
return new URLSearchParams(await clone.text());
|
|
165
|
+
case format === BodyFormat.Blob:
|
|
166
|
+
return await clone.blob();
|
|
167
|
+
case format === BodyFormat.ArrayBuffer:
|
|
168
|
+
return await clone.arrayBuffer();
|
|
169
|
+
case format === BodyFormat.Uint8Array:
|
|
170
|
+
return new Uint8Array(await clone.arrayBuffer());
|
|
171
|
+
}
|
|
172
|
+
return undefined;
|
|
173
|
+
}
|
|
174
|
+
function createChunk(data) {
|
|
175
|
+
const encodeData = new TextEncoder().encode(data);
|
|
176
|
+
const bytes = encodeData.length;
|
|
177
|
+
const baseHex = bytes.toString(16);
|
|
178
|
+
const totalHex = "00000000".substring(0, 8 - baseHex.length) + baseHex;
|
|
179
|
+
const head = new TextEncoder().encode(`;0x${totalHex};`);
|
|
180
|
+
const chunk = new Uint8Array(12 + bytes);
|
|
181
|
+
chunk.set(head);
|
|
182
|
+
chunk.set(encodeData, 12);
|
|
183
|
+
return chunk;
|
|
184
|
+
}
|
|
185
|
+
class ChunkReader {
|
|
186
|
+
constructor(stream) {
|
|
187
|
+
this.reader = stream.getReader();
|
|
188
|
+
this.buffer = new Uint8Array(0);
|
|
189
|
+
this.done = false;
|
|
190
|
+
}
|
|
191
|
+
async readChunk() {
|
|
192
|
+
const chunk = await this.reader.read();
|
|
193
|
+
if (!chunk.done) {
|
|
194
|
+
const newBuffer = new Uint8Array(this.buffer.length + chunk.value.length);
|
|
195
|
+
newBuffer.set(this.buffer);
|
|
196
|
+
newBuffer.set(chunk.value, this.buffer.length);
|
|
197
|
+
this.buffer = newBuffer;
|
|
198
|
+
} else {
|
|
199
|
+
this.done = true;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
async next() {
|
|
203
|
+
if (this.buffer.length === 0) {
|
|
204
|
+
if (this.done) {
|
|
205
|
+
return {
|
|
206
|
+
done: true,
|
|
207
|
+
value: undefined
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
await this.readChunk();
|
|
211
|
+
return await this.next();
|
|
212
|
+
}
|
|
213
|
+
const head = new TextDecoder().decode(this.buffer.subarray(1, 11));
|
|
214
|
+
const bytes = Number.parseInt(head, 16);
|
|
215
|
+
if (Number.isNaN(bytes)) {
|
|
216
|
+
throw new Error("Malformed server function stream.");
|
|
217
|
+
}
|
|
218
|
+
while (bytes > this.buffer.length - 12) {
|
|
219
|
+
if (this.done) {
|
|
220
|
+
throw new Error("Malformed server function stream.");
|
|
221
|
+
}
|
|
222
|
+
await this.readChunk();
|
|
223
|
+
}
|
|
224
|
+
const partial = new TextDecoder().decode(this.buffer.subarray(12, 12 + bytes));
|
|
225
|
+
this.buffer = this.buffer.subarray(12 + bytes);
|
|
226
|
+
return {
|
|
227
|
+
done: false,
|
|
228
|
+
value: partial
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
async drain(interpret) {
|
|
232
|
+
while (true) {
|
|
233
|
+
const result = await this.next();
|
|
234
|
+
if (result.done) {
|
|
235
|
+
break;
|
|
236
|
+
}
|
|
237
|
+
interpret(result.value);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
function serializeStream(value, codecOptions) {
|
|
242
|
+
return new ReadableStream({
|
|
243
|
+
start(controller) {
|
|
244
|
+
serializeJSON(value, {
|
|
245
|
+
...codecOptions,
|
|
246
|
+
onParse(node) {
|
|
247
|
+
controller.enqueue(createChunk(JSON.stringify(node)));
|
|
248
|
+
},
|
|
249
|
+
onDone() {
|
|
250
|
+
controller.close();
|
|
251
|
+
},
|
|
252
|
+
onError(error) {
|
|
253
|
+
controller.error(error);
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
async function deserializeStream(source, codecOptions) {
|
|
260
|
+
if (!source.body) {
|
|
261
|
+
throw new Error("missing body");
|
|
262
|
+
}
|
|
263
|
+
const reader = new ChunkReader(source.body);
|
|
264
|
+
const result = await reader.next();
|
|
265
|
+
if (!result.done) {
|
|
266
|
+
const deserializeChunk = createJSONDeserializer(codecOptions);
|
|
267
|
+
function interpretChunk(chunk) {
|
|
268
|
+
return deserializeChunk(JSON.parse(chunk));
|
|
269
|
+
}
|
|
270
|
+
void reader.drain(interpretChunk);
|
|
271
|
+
return interpretChunk(result.value);
|
|
272
|
+
}
|
|
273
|
+
return undefined;
|
|
274
|
+
}
|
|
275
|
+
async function deserializeString(text, codecOptions) {
|
|
276
|
+
return await deserializeStream(new Response(text), codecOptions);
|
|
277
|
+
}
|
|
278
|
+
async function decodeResponse(response, codecOptions) {
|
|
279
|
+
if (!response.body) return undefined;
|
|
280
|
+
return await extractBody(response, codecOptions === undefined ? codecConfig.codec : codecOptions);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
const config = {
|
|
284
|
+
provideEvent: undefined,
|
|
285
|
+
endpoint: "/_server"
|
|
286
|
+
};
|
|
287
|
+
function configureServerFunctionsServer({
|
|
288
|
+
provideEvent,
|
|
289
|
+
endpoint,
|
|
290
|
+
codec
|
|
291
|
+
} = {}) {
|
|
292
|
+
if (provideEvent !== undefined) config.provideEvent = provideEvent;
|
|
293
|
+
if (endpoint !== undefined) config.endpoint = endpoint;
|
|
294
|
+
if (codec !== undefined) configureServerFunctionsCodec(codec);
|
|
295
|
+
}
|
|
296
|
+
function provideEvent(event, fn) {
|
|
297
|
+
if (config.provideEvent) return config.provideEvent(event, fn);
|
|
298
|
+
const ctx = globalThis[RequestContext];
|
|
299
|
+
if (ctx) return ctx.run(event, fn);
|
|
300
|
+
throw new Error("No request event provider. Configure one with configureServerFunctionsServer({ provideEvent }).");
|
|
301
|
+
}
|
|
302
|
+
const REGISTRATIONS = new Map();
|
|
303
|
+
function registerServerFunction(id, callback) {
|
|
304
|
+
REGISTRATIONS.set(id, callback);
|
|
305
|
+
return callback;
|
|
306
|
+
}
|
|
307
|
+
function getServerFunction(id) {
|
|
308
|
+
const fn = REGISTRATIONS.get(id);
|
|
309
|
+
if (fn) {
|
|
310
|
+
return fn;
|
|
311
|
+
}
|
|
312
|
+
throw new Error("invalid server function: " + id);
|
|
313
|
+
}
|
|
314
|
+
function registerServerReference(id, fn) {
|
|
315
|
+
registerServerFunction(id, fn);
|
|
316
|
+
return {
|
|
317
|
+
id,
|
|
318
|
+
fn
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
function createServerReference({
|
|
322
|
+
id,
|
|
323
|
+
fn
|
|
324
|
+
}) {
|
|
325
|
+
if (typeof fn !== "function") throw new Error("Export from a 'use server' module must be a function");
|
|
326
|
+
return new Proxy(fn, {
|
|
327
|
+
get(target, prop, receiver) {
|
|
328
|
+
if (prop === "url") {
|
|
329
|
+
return `${config.endpoint}?id=${encodeURIComponent(id)}`;
|
|
330
|
+
}
|
|
331
|
+
if (prop === "GET") return receiver;
|
|
332
|
+
return target[prop];
|
|
333
|
+
},
|
|
334
|
+
apply(target, thisArg, args) {
|
|
335
|
+
const ogEvt = getRequestEvent();
|
|
336
|
+
if (!ogEvt) throw new Error("Cannot call server function outside of a request");
|
|
337
|
+
const evt = {
|
|
338
|
+
...ogEvt
|
|
339
|
+
};
|
|
340
|
+
evt.locals.serverFunctionMeta = {
|
|
341
|
+
id
|
|
342
|
+
};
|
|
343
|
+
evt.serverOnly = true;
|
|
344
|
+
return provideEvent(evt, () => {
|
|
345
|
+
return fn.apply(thisArg, args);
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
function getServerFunctionMeta() {
|
|
351
|
+
const event = getRequestEvent();
|
|
352
|
+
return event && event.locals.serverFunctionMeta;
|
|
353
|
+
}
|
|
354
|
+
function resolveFunctionId(request, url) {
|
|
355
|
+
const reference = request.headers.get(FUNCTION_HEADER);
|
|
356
|
+
if (reference) {
|
|
357
|
+
return reference.split("#")[0];
|
|
358
|
+
}
|
|
359
|
+
return url.searchParams.get("id");
|
|
360
|
+
}
|
|
361
|
+
async function parseArguments(request, url, instance, codec) {
|
|
362
|
+
const parsed = [];
|
|
363
|
+
if (!instance || request.method === "GET") {
|
|
364
|
+
const args = url.searchParams.get("args");
|
|
365
|
+
if (args) {
|
|
366
|
+
const result = args.startsWith(";0x") ? await deserializeString(args, codec) : JSON.parse(args);
|
|
367
|
+
for (const arg of result) {
|
|
368
|
+
parsed.push(arg);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
if (request.method === "POST" && request.body !== null) {
|
|
373
|
+
const format = request.headers.get(BODY_FORMAT_HEADER);
|
|
374
|
+
const decoded = await extractBody(request.clone(), codec);
|
|
375
|
+
if (format === BodyFormat.Serialized) {
|
|
376
|
+
return decoded;
|
|
377
|
+
}
|
|
378
|
+
parsed.push(decoded);
|
|
379
|
+
}
|
|
380
|
+
return parsed;
|
|
381
|
+
}
|
|
382
|
+
function serializedResponse(value, headers, codec) {
|
|
383
|
+
headers.set(BODY_FORMAT_HEADER, BodyFormat.Serialized);
|
|
384
|
+
headers.set("Content-Type", "text/plain");
|
|
385
|
+
return new Response(serializeStream(value, codec), {
|
|
386
|
+
headers
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
function encodeResult(value, headers, status, codec) {
|
|
390
|
+
const direct = getHeadersAndBody(value);
|
|
391
|
+
if (direct) {
|
|
392
|
+
for (const [key, val] of Object.entries(direct.headers || {})) {
|
|
393
|
+
headers.set(key, val);
|
|
394
|
+
}
|
|
395
|
+
return new Response(direct.body, {
|
|
396
|
+
status,
|
|
397
|
+
headers
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
const response = serializedResponse(value, headers, codec);
|
|
401
|
+
return status === 200 ? response : new Response(response.body, {
|
|
402
|
+
status,
|
|
403
|
+
headers
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
async function handleServerFunctionRequest(request, options = {}) {
|
|
407
|
+
const codec = options.codec !== undefined ? options.codec : getServerFunctionsCodec();
|
|
408
|
+
const url = new URL(request.url);
|
|
409
|
+
const instance = request.headers.get(INSTANCE_HEADER);
|
|
410
|
+
const functionId = resolveFunctionId(request, url);
|
|
411
|
+
if (!functionId) {
|
|
412
|
+
return new Response(process.env.NODE_ENV === "development" ? "Server function not found" : null, {
|
|
413
|
+
status: 404
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
let serverFunction;
|
|
417
|
+
try {
|
|
418
|
+
serverFunction = getServerFunction(functionId);
|
|
419
|
+
} catch {
|
|
420
|
+
return new Response(process.env.NODE_ENV === "development" ? `Unknown server function: ${functionId}` : null, {
|
|
421
|
+
status: 404
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
const event = options.createEvent ? options.createEvent(request) : {
|
|
425
|
+
request,
|
|
426
|
+
locals: {}
|
|
427
|
+
};
|
|
428
|
+
const provide = options.provideEvent || provideEvent;
|
|
429
|
+
const parsed = await parseArguments(request, url, instance, codec);
|
|
430
|
+
const headers = new Headers();
|
|
431
|
+
try {
|
|
432
|
+
let result = await provide(event, async () => {
|
|
433
|
+
event.locals.serverFunctionMeta = {
|
|
434
|
+
id: functionId
|
|
435
|
+
};
|
|
436
|
+
return serverFunction(...parsed);
|
|
437
|
+
});
|
|
438
|
+
if (options.transformResult) {
|
|
439
|
+
result = await options.transformResult(event, result, {
|
|
440
|
+
instance,
|
|
441
|
+
request
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
let status = 200;
|
|
445
|
+
if (isResponseEnvelope(result)) {
|
|
446
|
+
const {
|
|
447
|
+
response,
|
|
448
|
+
value
|
|
449
|
+
} = result;
|
|
450
|
+
if (!instance && !options.handleNoJS && response && response.body) {
|
|
451
|
+
return response;
|
|
452
|
+
}
|
|
453
|
+
if (response && response.headers) {
|
|
454
|
+
response.headers.forEach((val, key) => headers.append(key, val));
|
|
455
|
+
}
|
|
456
|
+
if (response && response.status && (response.status < 300 || response.status >= 400)) {
|
|
457
|
+
status = response.status;
|
|
458
|
+
}
|
|
459
|
+
result = value;
|
|
460
|
+
} else if (result instanceof Response) {
|
|
461
|
+
if (result.headers && result.headers.has("X-Content-Raw")) return result;
|
|
462
|
+
if (instance) {
|
|
463
|
+
if (result.headers) {
|
|
464
|
+
result.headers.forEach((value, key) => headers.append(key, value));
|
|
465
|
+
}
|
|
466
|
+
if (result.status && (result.status < 300 || result.status >= 400)) {
|
|
467
|
+
status = result.status;
|
|
468
|
+
}
|
|
469
|
+
if (result.body == null) {
|
|
470
|
+
result = null;
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
if (!instance) {
|
|
475
|
+
if (options.handleNoJS) return options.handleNoJS(result, request, parsed);
|
|
476
|
+
if (result instanceof Response) return result;
|
|
477
|
+
return encodeResult(result, headers, 200, codec);
|
|
478
|
+
}
|
|
479
|
+
return encodeResult(result, headers, status, codec);
|
|
480
|
+
} catch (x) {
|
|
481
|
+
if (x instanceof Response || isResponseEnvelope(x)) {
|
|
482
|
+
if (options.transformResult) {
|
|
483
|
+
x = await options.transformResult(event, x, {
|
|
484
|
+
instance,
|
|
485
|
+
request,
|
|
486
|
+
thrown: true
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
let status = 200;
|
|
490
|
+
if (isResponseEnvelope(x)) {
|
|
491
|
+
const {
|
|
492
|
+
response,
|
|
493
|
+
value
|
|
494
|
+
} = x;
|
|
495
|
+
if (response && response.headers) {
|
|
496
|
+
response.headers.forEach((val, key) => headers.append(key, val));
|
|
497
|
+
}
|
|
498
|
+
if (response && response.status && (!instance || response.status < 300 || response.status >= 400)) {
|
|
499
|
+
status = response.status;
|
|
500
|
+
}
|
|
501
|
+
x = value;
|
|
502
|
+
} else if (x instanceof Response) {
|
|
503
|
+
if (x.headers) {
|
|
504
|
+
x.headers.forEach((value, key) => headers.append(key, value));
|
|
505
|
+
}
|
|
506
|
+
if (x.status && (!instance || x.status < 300 || x.status >= 400)) {
|
|
507
|
+
status = x.status;
|
|
508
|
+
}
|
|
509
|
+
if (x.body == null) {
|
|
510
|
+
x = null;
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
headers.set("X-Server-Function-Error", "true");
|
|
514
|
+
if (!instance) {
|
|
515
|
+
if (options.handleNoJS) return options.handleNoJS(x, request, parsed, true);
|
|
516
|
+
if (x instanceof Response) return x;
|
|
517
|
+
}
|
|
518
|
+
return encodeResult(x, headers, status, codec);
|
|
519
|
+
}
|
|
520
|
+
if (!instance) {
|
|
521
|
+
if (options.handleNoJS) return options.handleNoJS(x, request, parsed, true);
|
|
522
|
+
const message = x instanceof Error ? x.message : String(x);
|
|
523
|
+
return new Response(process.env.NODE_ENV === "development" ? message : null, {
|
|
524
|
+
status: 500
|
|
525
|
+
});
|
|
526
|
+
}
|
|
527
|
+
const error = x instanceof Error ? x.message : typeof x === "string" ? x : "true";
|
|
528
|
+
headers.set("X-Server-Function-Error", error.replace(/[\r\n]+/g, ""));
|
|
529
|
+
return encodeResult(x, headers, 200, codec);
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
exports.FUNCTION_HEADER = FUNCTION_HEADER;
|
|
534
|
+
exports.INSTANCE_HEADER = INSTANCE_HEADER;
|
|
535
|
+
exports.configureServerFunctionsServer = configureServerFunctionsServer;
|
|
536
|
+
exports.createServerReference = createServerReference;
|
|
537
|
+
exports.decodeResponse = decodeResponse;
|
|
538
|
+
exports.getServerFunction = getServerFunction;
|
|
539
|
+
exports.getServerFunctionMeta = getServerFunctionMeta;
|
|
540
|
+
exports.handleServerFunctionRequest = handleServerFunctionRequest;
|
|
541
|
+
exports.registerServerFunction = registerServerFunction;
|
|
542
|
+
exports.registerServerReference = registerServerReference;
|