@solidjs/web 2.0.0-beta.18 → 2.0.0-beta.19

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.
@@ -0,0 +1,370 @@
1
+ 'use strict';
2
+
3
+ var seroval = require('seroval');
4
+ var web = require('seroval-plugins/web');
5
+
6
+ seroval.Feature.AggregateError | seroval.Feature.BigIntTypedArray;
7
+ const DEFAULT_WEB_PLUGINS = Object.freeze([web.AbortSignalPlugin,
8
+ web.CustomEventPlugin, web.DOMExceptionPlugin, web.EventPlugin,
9
+ web.FormDataPlugin, web.HeadersPlugin, web.ReadableStreamPlugin, web.RequestPlugin, web.ResponsePlugin, web.URLSearchParamsPlugin, web.URLPlugin]);
10
+ function resolveSerializerPlugins(customPlugins) {
11
+ return customPlugins ? [...customPlugins, ...DEFAULT_WEB_PLUGINS] : [...DEFAULT_WEB_PLUGINS];
12
+ }
13
+ const JSON_CODEC_DISABLED_FEATURES = seroval.Feature.RegExp;
14
+ const JSON_CODEC_DEPTH_LIMIT = 64;
15
+ function resolveCodecOptions({
16
+ plugins,
17
+ disabledFeatures,
18
+ depthLimit
19
+ } = {}) {
20
+ return {
21
+ plugins: resolveSerializerPlugins(plugins),
22
+ disabledFeatures: disabledFeatures === undefined ? JSON_CODEC_DISABLED_FEATURES : disabledFeatures,
23
+ depthLimit: depthLimit === undefined ? JSON_CODEC_DEPTH_LIMIT : depthLimit
24
+ };
25
+ }
26
+ function serializeJSON(value, {
27
+ onParse,
28
+ onDone,
29
+ onError,
30
+ ...codecOptions
31
+ }) {
32
+ return seroval.toCrossJSONStream(value, {
33
+ onParse,
34
+ onDone,
35
+ onError,
36
+ ...resolveCodecOptions(codecOptions)
37
+ });
38
+ }
39
+ function createJSONDeserializer(options) {
40
+ const refs = new Map();
41
+ const resolved = resolveCodecOptions(options);
42
+ return function deserializeJSONChunk(node) {
43
+ return seroval.fromCrossJSON(node, {
44
+ refs,
45
+ ...resolved
46
+ });
47
+ };
48
+ }
49
+
50
+ const codecConfig = {
51
+ codec: undefined
52
+ };
53
+ function configureServerFunctionsCodec(codec) {
54
+ codecConfig.codec = codec;
55
+ }
56
+ function getServerFunctionsCodec() {
57
+ return codecConfig.codec;
58
+ }
59
+ const FUNCTION_HEADER = "X-Server-Function-Id";
60
+ const INSTANCE_HEADER = "X-Server-Function-Instance";
61
+ const BODY_FORMAT_HEADER = "X-Server-Function-Format";
62
+ const FILE_FORM_KEY = "__server_function_file__";
63
+ const BodyFormat = {
64
+ Serialized: "0",
65
+ String: "1",
66
+ FormData: "2",
67
+ URLSearchParams: "3",
68
+ Blob: "4",
69
+ File: "5",
70
+ ArrayBuffer: "6",
71
+ Uint8Array: "7"
72
+ };
73
+ function getHeadersAndBody(body) {
74
+ switch (true) {
75
+ case typeof body === "string":
76
+ return {
77
+ headers: {
78
+ "Content-Type": "text/plain",
79
+ [BODY_FORMAT_HEADER]: BodyFormat.String
80
+ },
81
+ body
82
+ };
83
+ case body instanceof FormData:
84
+ return {
85
+ headers: {
86
+ [BODY_FORMAT_HEADER]: BodyFormat.FormData
87
+ },
88
+ body
89
+ };
90
+ case body instanceof URLSearchParams:
91
+ return {
92
+ headers: {
93
+ "Content-Type": "application/x-www-form-urlencoded",
94
+ [BODY_FORMAT_HEADER]: BodyFormat.URLSearchParams
95
+ },
96
+ body
97
+ };
98
+ case typeof File !== "undefined" && body instanceof File:
99
+ {
100
+ const formData = new FormData();
101
+ formData.append(FILE_FORM_KEY, body, body.name);
102
+ return {
103
+ headers: {
104
+ [BODY_FORMAT_HEADER]: BodyFormat.File
105
+ },
106
+ body: formData
107
+ };
108
+ }
109
+ case body instanceof Blob:
110
+ return {
111
+ headers: {
112
+ [BODY_FORMAT_HEADER]: BodyFormat.Blob
113
+ },
114
+ body
115
+ };
116
+ case body instanceof ArrayBuffer:
117
+ return {
118
+ headers: {
119
+ [BODY_FORMAT_HEADER]: BodyFormat.ArrayBuffer
120
+ },
121
+ body
122
+ };
123
+ case body instanceof Uint8Array:
124
+ return {
125
+ headers: {
126
+ [BODY_FORMAT_HEADER]: BodyFormat.Uint8Array
127
+ },
128
+ body: new Uint8Array(body)
129
+ };
130
+ default:
131
+ return undefined;
132
+ }
133
+ }
134
+ async function extractBody(source, codecOptions) {
135
+ const contentType = source.headers.get("content-type");
136
+ const format = source.headers.get(BODY_FORMAT_HEADER);
137
+ const clone = source.clone();
138
+ switch (true) {
139
+ case format === BodyFormat.Serialized:
140
+ return await deserializeStream(clone, codecOptions);
141
+ case format === BodyFormat.String:
142
+ return await clone.text();
143
+ case format === BodyFormat.File:
144
+ {
145
+ const formData = await clone.formData();
146
+ return formData.get(FILE_FORM_KEY);
147
+ }
148
+ case format === BodyFormat.FormData:
149
+ case contentType && contentType.startsWith("multipart/form-data"):
150
+ return await clone.formData();
151
+ case format === BodyFormat.URLSearchParams:
152
+ case contentType && contentType.startsWith("application/x-www-form-urlencoded"):
153
+ return new URLSearchParams(await clone.text());
154
+ case format === BodyFormat.Blob:
155
+ return await clone.blob();
156
+ case format === BodyFormat.ArrayBuffer:
157
+ return await clone.arrayBuffer();
158
+ case format === BodyFormat.Uint8Array:
159
+ return new Uint8Array(await clone.arrayBuffer());
160
+ }
161
+ return undefined;
162
+ }
163
+ function createChunk(data) {
164
+ const encodeData = new TextEncoder().encode(data);
165
+ const bytes = encodeData.length;
166
+ const baseHex = bytes.toString(16);
167
+ const totalHex = "00000000".substring(0, 8 - baseHex.length) + baseHex;
168
+ const head = new TextEncoder().encode(`;0x${totalHex};`);
169
+ const chunk = new Uint8Array(12 + bytes);
170
+ chunk.set(head);
171
+ chunk.set(encodeData, 12);
172
+ return chunk;
173
+ }
174
+ class ChunkReader {
175
+ constructor(stream) {
176
+ this.reader = stream.getReader();
177
+ this.buffer = new Uint8Array(0);
178
+ this.done = false;
179
+ }
180
+ async readChunk() {
181
+ const chunk = await this.reader.read();
182
+ if (!chunk.done) {
183
+ const newBuffer = new Uint8Array(this.buffer.length + chunk.value.length);
184
+ newBuffer.set(this.buffer);
185
+ newBuffer.set(chunk.value, this.buffer.length);
186
+ this.buffer = newBuffer;
187
+ } else {
188
+ this.done = true;
189
+ }
190
+ }
191
+ async next() {
192
+ if (this.buffer.length === 0) {
193
+ if (this.done) {
194
+ return {
195
+ done: true,
196
+ value: undefined
197
+ };
198
+ }
199
+ await this.readChunk();
200
+ return await this.next();
201
+ }
202
+ const head = new TextDecoder().decode(this.buffer.subarray(1, 11));
203
+ const bytes = Number.parseInt(head, 16);
204
+ if (Number.isNaN(bytes)) {
205
+ throw new Error("Malformed server function stream.");
206
+ }
207
+ while (bytes > this.buffer.length - 12) {
208
+ if (this.done) {
209
+ throw new Error("Malformed server function stream.");
210
+ }
211
+ await this.readChunk();
212
+ }
213
+ const partial = new TextDecoder().decode(this.buffer.subarray(12, 12 + bytes));
214
+ this.buffer = this.buffer.subarray(12 + bytes);
215
+ return {
216
+ done: false,
217
+ value: partial
218
+ };
219
+ }
220
+ async drain(interpret) {
221
+ while (true) {
222
+ const result = await this.next();
223
+ if (result.done) {
224
+ break;
225
+ }
226
+ interpret(result.value);
227
+ }
228
+ }
229
+ }
230
+ function serializeStream(value, codecOptions) {
231
+ return new ReadableStream({
232
+ start(controller) {
233
+ serializeJSON(value, {
234
+ ...codecOptions,
235
+ onParse(node) {
236
+ controller.enqueue(createChunk(JSON.stringify(node)));
237
+ },
238
+ onDone() {
239
+ controller.close();
240
+ },
241
+ onError(error) {
242
+ controller.error(error);
243
+ }
244
+ });
245
+ }
246
+ });
247
+ }
248
+ async function serializeString(value, codecOptions) {
249
+ const response = new Response(serializeStream(value, codecOptions));
250
+ return await response.text();
251
+ }
252
+ async function deserializeStream(source, codecOptions) {
253
+ if (!source.body) {
254
+ throw new Error("missing body");
255
+ }
256
+ const reader = new ChunkReader(source.body);
257
+ const result = await reader.next();
258
+ if (!result.done) {
259
+ const deserializeChunk = createJSONDeserializer(codecOptions);
260
+ function interpretChunk(chunk) {
261
+ return deserializeChunk(JSON.parse(chunk));
262
+ }
263
+ void reader.drain(interpretChunk);
264
+ return interpretChunk(result.value);
265
+ }
266
+ return undefined;
267
+ }
268
+ async function decodeResponse(response, codecOptions) {
269
+ if (!response.body) return undefined;
270
+ return await extractBody(response, codecOptions === undefined ? codecConfig.codec : codecOptions);
271
+ }
272
+
273
+ const config = {
274
+ endpoint: "/_server"
275
+ };
276
+ function configureServerFunctionsClient({
277
+ endpoint,
278
+ codec
279
+ } = {}) {
280
+ if (endpoint !== undefined) config.endpoint = endpoint;
281
+ if (codec !== undefined) configureServerFunctionsCodec(codec);
282
+ }
283
+ let INSTANCE = 0;
284
+ function createRequest(base, id, instance, options) {
285
+ return fetch(base, {
286
+ method: "POST",
287
+ ...options,
288
+ headers: {
289
+ ...options.headers,
290
+ [FUNCTION_HEADER]: id,
291
+ [INSTANCE_HEADER]: instance
292
+ }
293
+ });
294
+ }
295
+ async function initializeResponse(base, id, instance, options, args) {
296
+ if (args.length === 0) {
297
+ return createRequest(base, id, instance, options);
298
+ }
299
+ if (args.length === 1) {
300
+ const result = getHeadersAndBody(args[0]);
301
+ if (result) {
302
+ return createRequest(base, id, instance, {
303
+ ...options,
304
+ body: result.body,
305
+ headers: {
306
+ ...options.headers,
307
+ ...result.headers
308
+ }
309
+ });
310
+ }
311
+ }
312
+ return createRequest(base, id, instance, {
313
+ ...options,
314
+ body: await serializeString(args, getServerFunctionsCodec()),
315
+ headers: {
316
+ ...options.headers,
317
+ "Content-Type": "text/plain",
318
+ [BODY_FORMAT_HEADER]: BodyFormat.Serialized
319
+ }
320
+ });
321
+ }
322
+ async function fetchServerFunction(base, id, options, args) {
323
+ const instance = `server-function:${INSTANCE++}`;
324
+ const response = await initializeResponse(base, id, instance, options, args);
325
+ if (response.headers.has("Location") || response.headers.has("X-Revalidate") || response.headers.has("X-Single-Flight")) {
326
+ return response;
327
+ }
328
+ const result = await decodeResponse(response.clone());
329
+ if (response.headers.has("X-Server-Function-Error")) {
330
+ throw result;
331
+ }
332
+ return result;
333
+ }
334
+ function createServerReference(id) {
335
+ const fn = (...args) => fetchServerFunction(config.endpoint, id, {}, args);
336
+ return new Proxy(fn, {
337
+ get(target, prop, receiver) {
338
+ if (prop === "url") {
339
+ return `${config.endpoint}?id=${encodeURIComponent(id)}`;
340
+ }
341
+ if (prop === "GET") {
342
+ return receiver.withOptions({
343
+ method: "GET"
344
+ });
345
+ }
346
+ if (prop === "withOptions") {
347
+ const url = `${config.endpoint}?id=${encodeURIComponent(id)}`;
348
+ return options => {
349
+ const wrapped = async (...args) => {
350
+ const encodeArgs = options.method && options.method.toUpperCase() === "GET";
351
+ return fetchServerFunction(encodeArgs ? url + (args.length ? `&args=${encodeURIComponent(await serializeString(args, getServerFunctionsCodec()))}` : "") : config.endpoint, id, options, encodeArgs ? [] : args);
352
+ };
353
+ wrapped.url = url;
354
+ return wrapped;
355
+ };
356
+ }
357
+ return target[prop];
358
+ }
359
+ });
360
+ }
361
+ function registerServerReference() {
362
+ throw new Error("registerServerReference must not be called in the client build");
363
+ }
364
+
365
+ exports.FUNCTION_HEADER = FUNCTION_HEADER;
366
+ exports.INSTANCE_HEADER = INSTANCE_HEADER;
367
+ exports.configureServerFunctionsClient = configureServerFunctionsClient;
368
+ exports.createServerReference = createServerReference;
369
+ exports.decodeResponse = decodeResponse;
370
+ exports.registerServerReference = registerServerReference;