@solidjs/web 2.0.0-beta.2 → 2.0.0-beta.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/README.md +27 -4
  2. package/dist/dev.cjs +790 -223
  3. package/dist/dev.js +764 -217
  4. package/dist/server.cjs +742 -186
  5. package/dist/server.js +714 -183
  6. package/dist/web.cjs +779 -196
  7. package/dist/web.js +753 -190
  8. package/package.json +193 -38
  9. package/serialization/dist/serialization.cjs +83 -0
  10. package/serialization/dist/serialization.js +75 -0
  11. package/serialization/package.json +20 -0
  12. package/serialization/types/index.d.ts +139 -0
  13. package/serialization/types-cjs/index.d.cts +139 -0
  14. package/serialization/types-cjs/package.json +3 -0
  15. package/server-functions/dist/client.cjs +448 -0
  16. package/server-functions/dist/client.js +435 -0
  17. package/server-functions/dist/server.cjs +632 -0
  18. package/server-functions/dist/server.js +615 -0
  19. package/server-functions/package.json +30 -0
  20. package/storage/package.json +8 -3
  21. package/storage/types/index.d.ts +26 -0
  22. package/storage/types-cjs/index.d.cts +28 -0
  23. package/storage/types-cjs/package.json +3 -0
  24. package/types/client.d.ts +64 -21
  25. package/types/core.d.ts +3 -3
  26. package/types/index.d.ts +156 -24
  27. package/types/jsx-properties.d.ts +93 -0
  28. package/types/jsx.d.ts +4135 -1
  29. package/types/response.d.ts +93 -0
  30. package/types/serializer.d.ts +139 -0
  31. package/types/server-functions/client.d.ts +137 -0
  32. package/types/server-functions/server.d.ts +307 -0
  33. package/types/server-functions/shared.d.ts +342 -0
  34. package/types/server-mock.d.ts +89 -0
  35. package/types/server.d.ts +123 -28
  36. package/types-cjs/client.d.cts +131 -0
  37. package/types-cjs/core.d.cts +3 -0
  38. package/types-cjs/index.d.cts +178 -0
  39. package/types-cjs/jsx-properties.d.cts +93 -0
  40. package/types-cjs/jsx.d.cts +4135 -0
  41. package/types-cjs/package.json +3 -0
  42. package/types-cjs/response.d.cts +93 -0
  43. package/types-cjs/serializer.d.cts +139 -0
  44. package/types-cjs/server-functions/client.d.cts +137 -0
  45. package/types-cjs/server-functions/server.d.cts +307 -0
  46. package/types-cjs/server-functions/shared.d.cts +342 -0
  47. package/types-cjs/server-mock.d.cts +161 -0
  48. package/types-cjs/server.d.cts +251 -0
  49. package/storage/types/src/client.d.ts +0 -1
  50. package/storage/types/src/index.d.ts +0 -46
  51. package/storage/types/src/server-mock.d.ts +0 -72
  52. package/storage/types/storage/src/index.d.ts +0 -2
@@ -0,0 +1,435 @@
1
+ import { fromCrossJSON, Feature, toCrossJSONStream } from 'seroval';
2
+ import { AbortSignalPlugin, CustomEventPlugin, DOMExceptionPlugin, EventPlugin, FormDataPlugin, HeadersPlugin, ReadableStreamPlugin, RequestPlugin, ResponsePlugin, URLSearchParamsPlugin, URLPlugin } from 'seroval-plugins/web';
3
+
4
+ Feature.AggregateError | Feature.BigIntTypedArray;
5
+ const DEFAULT_WEB_PLUGINS = Object.freeze([AbortSignalPlugin,
6
+ CustomEventPlugin, DOMExceptionPlugin, EventPlugin,
7
+ FormDataPlugin, HeadersPlugin, ReadableStreamPlugin, RequestPlugin, ResponsePlugin, URLSearchParamsPlugin, URLPlugin]);
8
+ function resolveSerializerPlugins(customPlugins) {
9
+ return customPlugins ? [...customPlugins, ...DEFAULT_WEB_PLUGINS] : [...DEFAULT_WEB_PLUGINS];
10
+ }
11
+ const JSON_CODEC_DISABLED_FEATURES = Feature.RegExp;
12
+ const JSON_CODEC_DEPTH_LIMIT = 64;
13
+ function resolveCodecOptions({
14
+ plugins,
15
+ disabledFeatures,
16
+ depthLimit
17
+ } = {}) {
18
+ return {
19
+ plugins: resolveSerializerPlugins(plugins),
20
+ disabledFeatures: disabledFeatures === undefined ? JSON_CODEC_DISABLED_FEATURES : disabledFeatures,
21
+ depthLimit: depthLimit === undefined ? JSON_CODEC_DEPTH_LIMIT : depthLimit
22
+ };
23
+ }
24
+ function serializeJSON(value, {
25
+ onParse,
26
+ onDone,
27
+ onError,
28
+ ...codecOptions
29
+ }) {
30
+ return toCrossJSONStream(value, {
31
+ onParse,
32
+ onDone,
33
+ onError,
34
+ ...resolveCodecOptions(codecOptions)
35
+ });
36
+ }
37
+ function createJSONDeserializer(options) {
38
+ const refs = new Map();
39
+ const resolved = resolveCodecOptions(options);
40
+ return function deserializeJSONChunk(node) {
41
+ return fromCrossJSON(node, {
42
+ refs,
43
+ ...resolved
44
+ });
45
+ };
46
+ }
47
+
48
+ const codecConfig = {
49
+ codec: undefined
50
+ };
51
+ function configureServerFunctionsCodec(codec) {
52
+ codecConfig.codec = codec;
53
+ }
54
+ function getServerFunctionsCodec() {
55
+ return codecConfig.codec;
56
+ }
57
+ const flightConfig = {
58
+ consumer: undefined
59
+ };
60
+ function subscribeFlightData(consumer) {
61
+ flightConfig.consumer = consumer;
62
+ return () => {
63
+ if (flightConfig.consumer === consumer) flightConfig.consumer = undefined;
64
+ };
65
+ }
66
+ function getFlightDataConsumer() {
67
+ return flightConfig.consumer;
68
+ }
69
+ const SERVER_FUNCTION_METADATA = Symbol.for("solid.ServerFunctionMetadata");
70
+ function getServerFunctionMetadata(fn) {
71
+ if (typeof fn !== "function") return undefined;
72
+ return fn[SERVER_FUNCTION_METADATA] || undefined;
73
+ }
74
+ function isServerFunction(fn) {
75
+ return typeof fn === "function" && !!fn[SERVER_FUNCTION_METADATA];
76
+ }
77
+ function withMeta(fn, meta) {
78
+ const metadata = getServerFunctionMetadata(fn);
79
+ if (!metadata) {
80
+ throw new Error("withMeta expects a server function reference");
81
+ }
82
+ Object.assign(metadata, meta);
83
+ return fn;
84
+ }
85
+ const FUNCTION_HEADER = "X-Server-Function-Id";
86
+ const INSTANCE_HEADER = "X-Server-Function-Instance";
87
+ const BODY_FORMAT_HEADER = "X-Server-Function-Format";
88
+ const SINGLE_FLIGHT_HEADER = "X-Single-Flight";
89
+ const FILE_FORM_KEY = "__server_function_file__";
90
+ const BodyFormat = {
91
+ Serialized: "0",
92
+ String: "1",
93
+ FormData: "2",
94
+ URLSearchParams: "3",
95
+ Blob: "4",
96
+ File: "5",
97
+ ArrayBuffer: "6",
98
+ Uint8Array: "7"
99
+ };
100
+ function getHeadersAndBody(body) {
101
+ switch (true) {
102
+ case typeof body === "string":
103
+ return {
104
+ headers: {
105
+ "Content-Type": "text/plain",
106
+ [BODY_FORMAT_HEADER]: BodyFormat.String
107
+ },
108
+ body
109
+ };
110
+ case body instanceof FormData:
111
+ return {
112
+ headers: {
113
+ [BODY_FORMAT_HEADER]: BodyFormat.FormData
114
+ },
115
+ body
116
+ };
117
+ case body instanceof URLSearchParams:
118
+ return {
119
+ headers: {
120
+ "Content-Type": "application/x-www-form-urlencoded",
121
+ [BODY_FORMAT_HEADER]: BodyFormat.URLSearchParams
122
+ },
123
+ body
124
+ };
125
+ case typeof File !== "undefined" && body instanceof File:
126
+ {
127
+ const formData = new FormData();
128
+ formData.append(FILE_FORM_KEY, body, body.name);
129
+ return {
130
+ headers: {
131
+ [BODY_FORMAT_HEADER]: BodyFormat.File
132
+ },
133
+ body: formData
134
+ };
135
+ }
136
+ case body instanceof Blob:
137
+ return {
138
+ headers: {
139
+ [BODY_FORMAT_HEADER]: BodyFormat.Blob
140
+ },
141
+ body
142
+ };
143
+ case body instanceof ArrayBuffer:
144
+ return {
145
+ headers: {
146
+ [BODY_FORMAT_HEADER]: BodyFormat.ArrayBuffer
147
+ },
148
+ body
149
+ };
150
+ case body instanceof Uint8Array:
151
+ return {
152
+ headers: {
153
+ [BODY_FORMAT_HEADER]: BodyFormat.Uint8Array
154
+ },
155
+ body: new Uint8Array(body)
156
+ };
157
+ default:
158
+ return undefined;
159
+ }
160
+ }
161
+ async function extractBody(source, codecOptions) {
162
+ const contentType = source.headers.get("content-type");
163
+ const format = source.headers.get(BODY_FORMAT_HEADER);
164
+ const clone = source.clone();
165
+ switch (true) {
166
+ case format === BodyFormat.Serialized:
167
+ return await deserializeStream(clone, codecOptions);
168
+ case format === BodyFormat.String:
169
+ return await clone.text();
170
+ case format === BodyFormat.File:
171
+ {
172
+ const formData = await clone.formData();
173
+ return formData.get(FILE_FORM_KEY);
174
+ }
175
+ case format === BodyFormat.FormData:
176
+ case contentType && contentType.startsWith("multipart/form-data"):
177
+ return await clone.formData();
178
+ case format === BodyFormat.URLSearchParams:
179
+ case contentType && contentType.startsWith("application/x-www-form-urlencoded"):
180
+ return new URLSearchParams(await clone.text());
181
+ case format === BodyFormat.Blob:
182
+ return await clone.blob();
183
+ case format === BodyFormat.ArrayBuffer:
184
+ return await clone.arrayBuffer();
185
+ case format === BodyFormat.Uint8Array:
186
+ return new Uint8Array(await clone.arrayBuffer());
187
+ }
188
+ return undefined;
189
+ }
190
+ function createChunk(data) {
191
+ const encodeData = new TextEncoder().encode(data);
192
+ const bytes = encodeData.length;
193
+ const baseHex = bytes.toString(16);
194
+ const totalHex = "00000000".substring(0, 8 - baseHex.length) + baseHex;
195
+ const head = new TextEncoder().encode(`;0x${totalHex};`);
196
+ const chunk = new Uint8Array(12 + bytes);
197
+ chunk.set(head);
198
+ chunk.set(encodeData, 12);
199
+ return chunk;
200
+ }
201
+ class ChunkReader {
202
+ constructor(stream) {
203
+ this.reader = stream.getReader();
204
+ this.buffer = new Uint8Array(0);
205
+ this.done = false;
206
+ }
207
+ async readChunk() {
208
+ const chunk = await this.reader.read();
209
+ if (!chunk.done) {
210
+ const newBuffer = new Uint8Array(this.buffer.length + chunk.value.length);
211
+ newBuffer.set(this.buffer);
212
+ newBuffer.set(chunk.value, this.buffer.length);
213
+ this.buffer = newBuffer;
214
+ } else {
215
+ this.done = true;
216
+ }
217
+ }
218
+ async next() {
219
+ if (this.buffer.length === 0) {
220
+ if (this.done) {
221
+ return {
222
+ done: true,
223
+ value: undefined
224
+ };
225
+ }
226
+ await this.readChunk();
227
+ return await this.next();
228
+ }
229
+ const head = new TextDecoder().decode(this.buffer.subarray(1, 11));
230
+ const bytes = Number.parseInt(head, 16);
231
+ if (Number.isNaN(bytes)) {
232
+ throw new Error("Malformed server function stream.");
233
+ }
234
+ while (bytes > this.buffer.length - 12) {
235
+ if (this.done) {
236
+ throw new Error("Malformed server function stream.");
237
+ }
238
+ await this.readChunk();
239
+ }
240
+ const partial = new TextDecoder().decode(this.buffer.subarray(12, 12 + bytes));
241
+ this.buffer = this.buffer.subarray(12 + bytes);
242
+ return {
243
+ done: false,
244
+ value: partial
245
+ };
246
+ }
247
+ async drain(interpret) {
248
+ while (true) {
249
+ const result = await this.next();
250
+ if (result.done) {
251
+ break;
252
+ }
253
+ interpret(result.value);
254
+ }
255
+ }
256
+ }
257
+ function serializeStream(value, codecOptions) {
258
+ return new ReadableStream({
259
+ start(controller) {
260
+ serializeJSON(value, {
261
+ ...codecOptions,
262
+ onParse(node) {
263
+ controller.enqueue(createChunk(JSON.stringify(node)));
264
+ },
265
+ onDone() {
266
+ controller.close();
267
+ },
268
+ onError(error) {
269
+ controller.error(error);
270
+ }
271
+ });
272
+ }
273
+ });
274
+ }
275
+ async function serializeString(value, codecOptions) {
276
+ const response = new Response(serializeStream(value, codecOptions));
277
+ return await response.text();
278
+ }
279
+ async function deserializeStream(source, codecOptions) {
280
+ if (!source.body) {
281
+ throw new Error("missing body");
282
+ }
283
+ const reader = new ChunkReader(source.body);
284
+ const result = await reader.next();
285
+ if (!result.done) {
286
+ const deserializeChunk = createJSONDeserializer(codecOptions);
287
+ function interpretChunk(chunk) {
288
+ return deserializeChunk(JSON.parse(chunk));
289
+ }
290
+ void reader.drain(interpretChunk);
291
+ return interpretChunk(result.value);
292
+ }
293
+ return undefined;
294
+ }
295
+ async function decodeResponse(response, codecOptions) {
296
+ if (!response.body) return undefined;
297
+ return await extractBody(response, codecOptions === undefined ? codecConfig.codec : codecOptions);
298
+ }
299
+
300
+ const config = {
301
+ endpoint: "/_server",
302
+ prepareRequest: undefined
303
+ };
304
+ function configureServerFunctionsClient({
305
+ endpoint,
306
+ codec,
307
+ prepareRequest
308
+ } = {}) {
309
+ if (endpoint !== undefined) config.endpoint = endpoint;
310
+ if (codec !== undefined) configureServerFunctionsCodec(codec);
311
+ if (prepareRequest !== undefined) config.prepareRequest = prepareRequest;
312
+ }
313
+ let INSTANCE = 0;
314
+ async function createRequest(base, id, instance, options, meta) {
315
+ const headers = {
316
+ ...options.headers,
317
+ [FUNCTION_HEADER]: id,
318
+ [INSTANCE_HEADER]: instance
319
+ };
320
+ if (getFlightDataConsumer() && (!options.method || options.method.toUpperCase() !== "GET")) {
321
+ headers[SINGLE_FLIGHT_HEADER] = "true";
322
+ }
323
+ let init = {
324
+ method: "POST",
325
+ ...options,
326
+ headers
327
+ };
328
+ if (config.prepareRequest) {
329
+ init = (await config.prepareRequest(init, {
330
+ id,
331
+ meta
332
+ })) || init;
333
+ }
334
+ return fetch(base, init);
335
+ }
336
+ async function initializeResponse(base, id, instance, options, args, meta) {
337
+ if (args.length === 0) {
338
+ return createRequest(base, id, instance, options, meta);
339
+ }
340
+ if (args.length === 1) {
341
+ const result = getHeadersAndBody(args[0]);
342
+ if (result) {
343
+ return createRequest(base, id, instance, {
344
+ ...options,
345
+ body: result.body,
346
+ headers: {
347
+ ...options.headers,
348
+ ...result.headers
349
+ }
350
+ }, meta);
351
+ }
352
+ }
353
+ return createRequest(base, id, instance, {
354
+ ...options,
355
+ body: await serializeString(args, getServerFunctionsCodec()),
356
+ headers: {
357
+ ...options.headers,
358
+ "Content-Type": "text/plain",
359
+ [BODY_FORMAT_HEADER]: BodyFormat.Serialized
360
+ }
361
+ }, meta);
362
+ }
363
+ async function fetchServerFunction(base, id, options, args, meta) {
364
+ const instance = `server-function:${INSTANCE++}`;
365
+ const response = await initializeResponse(base, id, instance, options, args, meta);
366
+ if (response.headers.has(SINGLE_FLIGHT_HEADER)) {
367
+ const consumer = getFlightDataConsumer();
368
+ if (consumer) {
369
+ const payload = await decodeResponse(response);
370
+ await consumer(payload.data, {
371
+ response
372
+ });
373
+ if (response.headers.has("X-Server-Function-Error") && !response.headers.has("Location") && !response.headers.has("X-Revalidate")) {
374
+ throw payload.value;
375
+ }
376
+ return payload.value;
377
+ }
378
+ }
379
+ if (response.headers.has("Location") || response.headers.has("X-Revalidate") || response.headers.has(SINGLE_FLIGHT_HEADER)) {
380
+ return response;
381
+ }
382
+ const result = await decodeResponse(response.clone());
383
+ if (response.headers.has("X-Server-Function-Error")) {
384
+ throw result;
385
+ }
386
+ return result;
387
+ }
388
+ function createServerReference(id, name) {
389
+ const metadata = name === undefined ? {} : {
390
+ name
391
+ };
392
+ const fn = (...args) => fetchServerFunction(config.endpoint, id, {}, args, metadata);
393
+ fn[SERVER_FUNCTION_METADATA] = metadata;
394
+ return new Proxy(fn, {
395
+ get(target, prop) {
396
+ if (prop === "id") return id;
397
+ if (prop === "url") {
398
+ return `${config.endpoint}?id=${encodeURIComponent(id)}`;
399
+ }
400
+ return target[prop];
401
+ }
402
+ });
403
+ }
404
+ function GET(fn) {
405
+ if (!isServerFunction(fn)) {
406
+ throw new Error("GET expects a server function reference");
407
+ }
408
+ const id = fn.id;
409
+ const metadata = {
410
+ ...getServerFunctionMetadata(fn)
411
+ };
412
+ const wrapped = async (...args) => {
413
+ let base = `${config.endpoint}?id=${encodeURIComponent(id)}`;
414
+ if (args.length) {
415
+ base += `&args=${encodeURIComponent(await serializeString(args, getServerFunctionsCodec()))}`;
416
+ }
417
+ return fetchServerFunction(base, id, {
418
+ method: "GET"
419
+ }, [], metadata);
420
+ };
421
+ wrapped[SERVER_FUNCTION_METADATA] = metadata;
422
+ wrapped.id = id;
423
+ Object.defineProperty(wrapped, "url", {
424
+ get: () => `${config.endpoint}?id=${encodeURIComponent(id)}`,
425
+ configurable: true
426
+ });
427
+ return withMeta(wrapped, {
428
+ method: "GET"
429
+ });
430
+ }
431
+ function registerServerReference() {
432
+ throw new Error("registerServerReference must not be called in the client build");
433
+ }
434
+
435
+ export { FUNCTION_HEADER, GET, INSTANCE_HEADER, SINGLE_FLIGHT_HEADER, configureServerFunctionsClient, createServerReference, decodeResponse, getServerFunctionMetadata, isServerFunction, registerServerReference, subscribeFlightData, withMeta };