evikit 2.8.0 → 2.8.1
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/CONTRIBUTING.md +24 -0
- package/README.md +2 -2
- package/index.d.ts +158 -158
- package/index.js +231 -230
- package/package.json +1 -1
- package/query.js +9 -8
- package/server.js +668 -644
- package/use-api.d.ts +13 -12
- package/use-api.js +119 -105
package/server.js
CHANGED
|
@@ -1,644 +1,668 @@
|
|
|
1
|
-
import { Busboy } from "@fastify/busboy";
|
|
2
|
-
import { toJsonSchema } from "@valibot/to-json-schema";
|
|
3
|
-
import acceptLanguage from "accept-language";
|
|
4
|
-
import { po } from "gettext-parser";
|
|
5
|
-
import cluster from "node:cluster";
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
process.env["
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
)
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
res.
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
res.
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
res.
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
const port = Number(process.env["PORT"]) || 5173;
|
|
213
|
-
if (cluster.isPrimary && parallelism > 1) {
|
|
214
|
-
/** @type {{ on(event: "message", callback: (message: any) => void): void, send(message: any, _: any, options: { keepOpen: boolean }): void }[]} */
|
|
215
|
-
const forks = [];
|
|
216
|
-
for (let i = 0; i < parallelism; i++) {
|
|
217
|
-
forks.push(cluster.fork({ i }));
|
|
218
|
-
}
|
|
219
|
-
for (const fork of forks) {
|
|
220
|
-
fork.on("message", (maybeMessage) => {
|
|
221
|
-
const message = /** @type {any} */ (maybeMessage);
|
|
222
|
-
for (let i = 0; i < forks.length; i++) {
|
|
223
|
-
if (i != message.i) {
|
|
224
|
-
forks[i]?.send(message, undefined, { keepOpen: true });
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
});
|
|
228
|
-
}
|
|
229
|
-
} else {
|
|
230
|
-
const server = app.listen(port, () => {
|
|
231
|
-
if (cluster.isPrimary || process.env["i"] == "0") {
|
|
232
|
-
console.log(
|
|
233
|
-
`Server started at ${process.env["ORIGIN"] || `http://localhost:${port}`}`,
|
|
234
|
-
);
|
|
235
|
-
}
|
|
236
|
-
});
|
|
237
|
-
server.on("error", (error) => {
|
|
238
|
-
console.error(error);
|
|
239
|
-
process.exitCode = 1;
|
|
240
|
-
});
|
|
241
|
-
process.on("SIGTERM", () => {
|
|
242
|
-
server.close();
|
|
243
|
-
});
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
/** @param {{ html: string, prerender({ lang, url }: { lang: string, url: string }): Promise<{ data: {}, head: { elements: Set<{ props: { [k: string]: string }, type: string }>, lang: string, title: string }, html: string }> }} props */
|
|
248
|
-
export function production({ html, prerender }) {
|
|
249
|
-
/**
|
|
250
|
-
* @param {import("node:http").IncomingMessage} req
|
|
251
|
-
* @param {import("node:http").ServerResponse<import("node:http").IncomingMessage>} res
|
|
252
|
-
*/
|
|
253
|
-
return async function onRequest(req, res) {
|
|
254
|
-
try {
|
|
255
|
-
const rendered = await prerender({
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
res.
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
.replace(
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
.
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
(
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
"
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
res.
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
* @param {
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
*
|
|
335
|
-
* @param {
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
const
|
|
348
|
-
|
|
349
|
-
.
|
|
350
|
-
.
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
:
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
* @param {import("node:http").
|
|
482
|
-
* @param {()
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
const
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
res.
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
res.setHeader("
|
|
518
|
-
res.setHeader("
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
return
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
1
|
+
import { Busboy } from "@fastify/busboy";
|
|
2
|
+
import { toJsonSchema } from "@valibot/to-json-schema";
|
|
3
|
+
import acceptLanguage from "accept-language";
|
|
4
|
+
import { po } from "gettext-parser";
|
|
5
|
+
import cluster from "node:cluster";
|
|
6
|
+
import { createHash } from "node:crypto";
|
|
7
|
+
import { readFile } from "node:fs/promises";
|
|
8
|
+
import { availableParallelism } from "node:os";
|
|
9
|
+
import { h } from "preact";
|
|
10
|
+
import { renderToString } from "preact-render-to-string";
|
|
11
|
+
import {
|
|
12
|
+
object,
|
|
13
|
+
optional,
|
|
14
|
+
parse,
|
|
15
|
+
picklist,
|
|
16
|
+
record,
|
|
17
|
+
string,
|
|
18
|
+
union,
|
|
19
|
+
ValiError,
|
|
20
|
+
} from "valibot";
|
|
21
|
+
|
|
22
|
+
import { Po } from "./i18n.js";
|
|
23
|
+
|
|
24
|
+
/** @param {() => () => void} callback */
|
|
25
|
+
export function daemon(callback) {
|
|
26
|
+
if (
|
|
27
|
+
(process.env["npm_command"] == "start" &&
|
|
28
|
+
(parallelism() == 1 ? cluster.isPrimary : process.env["i"] == "0")) ||
|
|
29
|
+
process.env["npm_lifecycle_event"] == "dev"
|
|
30
|
+
) {
|
|
31
|
+
/** @type {any} */
|
|
32
|
+
const self = globalThis;
|
|
33
|
+
if (self.clearDaemon) {
|
|
34
|
+
self.clearDaemon();
|
|
35
|
+
}
|
|
36
|
+
self.clearDaemon = callback();
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** @type {import("./server-generic.js").ServerEndpoint} */
|
|
41
|
+
export function endpoint({ input, outputs }, handler) {
|
|
42
|
+
const arrayOf = ArrayOf(input);
|
|
43
|
+
const schema = union(
|
|
44
|
+
Object.entries(outputs).map(([status, schema]) =>
|
|
45
|
+
object({
|
|
46
|
+
contentType: optional(string()),
|
|
47
|
+
cookies: optional(record(string(), object({ value: string() }))),
|
|
48
|
+
headers: optional(record(string(), string())),
|
|
49
|
+
output: schema,
|
|
50
|
+
status: picklist([Number(status)]),
|
|
51
|
+
}),
|
|
52
|
+
),
|
|
53
|
+
);
|
|
54
|
+
return {
|
|
55
|
+
handler: async (req, res) => {
|
|
56
|
+
try {
|
|
57
|
+
/** @type {Record<string, string | File | (string | File)[]>} */
|
|
58
|
+
const body = {};
|
|
59
|
+
if (req.headers["content-type"]) {
|
|
60
|
+
const busboy = new Busboy({
|
|
61
|
+
headers: /** @type {any} */ (req.headers),
|
|
62
|
+
});
|
|
63
|
+
busboy.on("field", (name, value) => {
|
|
64
|
+
body[name] =
|
|
65
|
+
typeof body[name] == "undefined"
|
|
66
|
+
? value
|
|
67
|
+
: Array.isArray(body[name])
|
|
68
|
+
? [...body[name], value]
|
|
69
|
+
: [body[name], value];
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
if (
|
|
73
|
+
Object.values(input.entries).some(
|
|
74
|
+
(entry) => entry.type == "file" || arrayOf(entry, "file"),
|
|
75
|
+
)
|
|
76
|
+
) {
|
|
77
|
+
busboy.on("file", (name, stream, filename) => {
|
|
78
|
+
const entry = input.entries[name];
|
|
79
|
+
if (entry && (entry.type == "file" || arrayOf(entry, "file"))) {
|
|
80
|
+
/** @type {ArrayBuffer[]} */
|
|
81
|
+
const buffers = [];
|
|
82
|
+
stream.on("data", (data) => {
|
|
83
|
+
buffers.push(data);
|
|
84
|
+
});
|
|
85
|
+
stream.on("end", () => {
|
|
86
|
+
const value = new File(buffers, filename);
|
|
87
|
+
body[name] =
|
|
88
|
+
typeof body[name] == "undefined"
|
|
89
|
+
? value
|
|
90
|
+
: Array.isArray(body[name])
|
|
91
|
+
? [...body[name], value]
|
|
92
|
+
: [body[name], value];
|
|
93
|
+
});
|
|
94
|
+
} else {
|
|
95
|
+
stream.resume();
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
await new Promise((resolve, reject) => {
|
|
101
|
+
busboy.on("error", reject).on("finish", resolve);
|
|
102
|
+
req.pipe(busboy);
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const { contentType, cookies, headers, output, status } = parse(
|
|
107
|
+
schema,
|
|
108
|
+
await handler({
|
|
109
|
+
input: parse(input, {
|
|
110
|
+
...(req.headers.cookie
|
|
111
|
+
? Object.fromEntries(
|
|
112
|
+
req.headers.cookie
|
|
113
|
+
.split("; ")
|
|
114
|
+
.map((row) => row.split("=").map(decodeURIComponent)),
|
|
115
|
+
)
|
|
116
|
+
: undefined),
|
|
117
|
+
...Object.fromEntries(
|
|
118
|
+
Object.entries(
|
|
119
|
+
Object.groupBy(
|
|
120
|
+
[
|
|
121
|
+
...new URL(
|
|
122
|
+
`http://${req.headers.host}${req.url}`,
|
|
123
|
+
).searchParams.entries(),
|
|
124
|
+
],
|
|
125
|
+
([name]) => name || "",
|
|
126
|
+
),
|
|
127
|
+
).map(([name, group]) => {
|
|
128
|
+
const values = (group || []).map((value) => value[1]);
|
|
129
|
+
return [name, values.length == 1 ? values[0] : values];
|
|
130
|
+
}),
|
|
131
|
+
),
|
|
132
|
+
...body,
|
|
133
|
+
}),
|
|
134
|
+
}),
|
|
135
|
+
);
|
|
136
|
+
|
|
137
|
+
const location = headers
|
|
138
|
+
? Object.entries(headers).find(
|
|
139
|
+
([name]) => name.toLowerCase() == "location",
|
|
140
|
+
)?.[1]
|
|
141
|
+
: undefined;
|
|
142
|
+
const opaqueRedirectWorkaround =
|
|
143
|
+
req.headers.accept == "application/json" && location
|
|
144
|
+
? { location, status }
|
|
145
|
+
: undefined;
|
|
146
|
+
const workaroundHeaders = headers
|
|
147
|
+
? Object.entries(headers).filter(([name]) => {
|
|
148
|
+
const lower = name.toLowerCase();
|
|
149
|
+
return (
|
|
150
|
+
lower != "content-type" &&
|
|
151
|
+
(lower != "location" || !opaqueRedirectWorkaround)
|
|
152
|
+
);
|
|
153
|
+
})
|
|
154
|
+
: [];
|
|
155
|
+
|
|
156
|
+
if (cookies) {
|
|
157
|
+
workaroundHeaders.push([
|
|
158
|
+
"set-cookie",
|
|
159
|
+
Object.entries(cookies || "")
|
|
160
|
+
.map(
|
|
161
|
+
([name, { value }]) =>
|
|
162
|
+
`${encodeURIComponent(name)}=${encodeURIComponent(value)}; path=/`,
|
|
163
|
+
)
|
|
164
|
+
.join("; "),
|
|
165
|
+
]);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (contentType == "application/json") {
|
|
169
|
+
res.statusCode = status;
|
|
170
|
+
res.setHeader("content-type", "application/json; charset=utf-8");
|
|
171
|
+
for (const [name, value] of workaroundHeaders) {
|
|
172
|
+
res.setHeader(name, value);
|
|
173
|
+
}
|
|
174
|
+
res.end(JSON.stringify(output));
|
|
175
|
+
} else if (opaqueRedirectWorkaround) {
|
|
176
|
+
res.setHeader("content-type", "application/json; charset=utf-8");
|
|
177
|
+
for (const [name, value] of workaroundHeaders) {
|
|
178
|
+
res.setHeader(name, value);
|
|
179
|
+
}
|
|
180
|
+
res.end(JSON.stringify({ opaqueRedirectWorkaround }));
|
|
181
|
+
} else {
|
|
182
|
+
res.statusCode = status;
|
|
183
|
+
res.setHeader("content-type", "text/plain; charset=utf-8");
|
|
184
|
+
for (const [name, value] of workaroundHeaders) {
|
|
185
|
+
res.setHeader(name, value);
|
|
186
|
+
}
|
|
187
|
+
res.end(output);
|
|
188
|
+
}
|
|
189
|
+
} catch (/** @type {any} */ error) {
|
|
190
|
+
console.error(error);
|
|
191
|
+
res.statusCode = error instanceof ValiError ? 400 : 500;
|
|
192
|
+
res.setHeader("content-type", "text/plain; charset=utf-8");
|
|
193
|
+
res.end(error.message);
|
|
194
|
+
}
|
|
195
|
+
},
|
|
196
|
+
input,
|
|
197
|
+
outputs,
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** @type {import("./server-generic.js").ServerJson} */
|
|
202
|
+
export function json(output) {
|
|
203
|
+
return {
|
|
204
|
+
contentType: /** @type {any} */ ("application/json"),
|
|
205
|
+
output,
|
|
206
|
+
status: 200,
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** @param {{ listen: (port: number, callback: () => void) => { close(): void, on(event: "error", callback: (error: unknown) => void ): void } }} app */
|
|
211
|
+
export function listen(app) {
|
|
212
|
+
const port = Number(process.env["PORT"]) || 5173;
|
|
213
|
+
if (cluster.isPrimary && parallelism() > 1) {
|
|
214
|
+
/** @type {{ on(event: "message", callback: (message: any) => void): void, send(message: any, _: any, options: { keepOpen: boolean }): void }[]} */
|
|
215
|
+
const forks = [];
|
|
216
|
+
for (let i = 0; i < parallelism(); i++) {
|
|
217
|
+
forks.push(cluster.fork({ i }));
|
|
218
|
+
}
|
|
219
|
+
for (const fork of forks) {
|
|
220
|
+
fork.on("message", (maybeMessage) => {
|
|
221
|
+
const message = /** @type {any} */ (maybeMessage);
|
|
222
|
+
for (let i = 0; i < forks.length; i++) {
|
|
223
|
+
if (i != message.i) {
|
|
224
|
+
forks[i]?.send(message, undefined, { keepOpen: true });
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
} else {
|
|
230
|
+
const server = app.listen(port, () => {
|
|
231
|
+
if (cluster.isPrimary || process.env["i"] == "0") {
|
|
232
|
+
console.log(
|
|
233
|
+
`Server started at ${process.env["ORIGIN"] || `http://localhost:${port}`}`,
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
server.on("error", (error) => {
|
|
238
|
+
console.error(error);
|
|
239
|
+
process.exitCode = 1;
|
|
240
|
+
});
|
|
241
|
+
process.on("SIGTERM", () => {
|
|
242
|
+
server.close();
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/** @param {{ html: string, prerender({ cookie, lang, url }: { cookie?: string | undefined, lang: string, url: string }): Promise<{ data: {}, head: { elements: Set<{ props: { [k: string]: string }, type: string }>, lang: string, title: string }, html: string }> }} props */
|
|
248
|
+
export function production({ html, prerender }) {
|
|
249
|
+
/**
|
|
250
|
+
* @param {import("node:http").IncomingMessage} req
|
|
251
|
+
* @param {import("node:http").ServerResponse<import("node:http").IncomingMessage>} res
|
|
252
|
+
*/
|
|
253
|
+
return async function onRequest(req, res) {
|
|
254
|
+
try {
|
|
255
|
+
const rendered = await prerender({
|
|
256
|
+
cookie: req.headers.cookie,
|
|
257
|
+
lang: acceptLanguage.get(req.headers["accept-language"]) || "en",
|
|
258
|
+
url: req.url || "/",
|
|
259
|
+
});
|
|
260
|
+
res.statusCode =
|
|
261
|
+
// https://indieweb.org/meta_http-equiv_status
|
|
262
|
+
Number(
|
|
263
|
+
[...rendered.head.elements.values()]
|
|
264
|
+
.find(
|
|
265
|
+
({ props, type }) =>
|
|
266
|
+
type == "meta" && props["http-equiv"] == "status",
|
|
267
|
+
)
|
|
268
|
+
?.props["content"]?.replace(/ .*/, ""),
|
|
269
|
+
) || 200;
|
|
270
|
+
res.setHeader("content-type", "text/html; charset=utf-8");
|
|
271
|
+
res.end(
|
|
272
|
+
html
|
|
273
|
+
.replace(/lang=".+?"/, () => `lang="${rendered.head.lang}"`)
|
|
274
|
+
.replace(/<title>.+<\/title>/, () =>
|
|
275
|
+
renderToString(h("title", null, rendered.head.title)),
|
|
276
|
+
)
|
|
277
|
+
.replace(/<!--app-head-->[\s\S]+?<body/, (match) =>
|
|
278
|
+
match
|
|
279
|
+
.replace(/<link[^>]+>/g, (link) =>
|
|
280
|
+
/data-hoofd/.test(link) ? "" : link,
|
|
281
|
+
)
|
|
282
|
+
.replace(/<meta[^>]+>/g, ""),
|
|
283
|
+
)
|
|
284
|
+
.replace(
|
|
285
|
+
`<!--app-head-->`,
|
|
286
|
+
[...rendered.head.elements.values()]
|
|
287
|
+
.map(
|
|
288
|
+
({ props, type }) =>
|
|
289
|
+
`<${type} ` +
|
|
290
|
+
Object.entries(props)
|
|
291
|
+
.map(([name, value]) => `${name}="${value}"`)
|
|
292
|
+
.join(" ") +
|
|
293
|
+
(type == "script" ? `</${type}>` : " />"),
|
|
294
|
+
)
|
|
295
|
+
.join(""),
|
|
296
|
+
)
|
|
297
|
+
.replace(
|
|
298
|
+
/(<div id="app">)[\s\S]+<!--app-html-->/,
|
|
299
|
+
(_, div) =>
|
|
300
|
+
div +
|
|
301
|
+
rendered.html +
|
|
302
|
+
'<script type="application/json" id="preact-prerender-data">' +
|
|
303
|
+
JSON.stringify(rendered.data).replace(/\//g, "\\/") +
|
|
304
|
+
"</script>",
|
|
305
|
+
),
|
|
306
|
+
);
|
|
307
|
+
} catch (error) {
|
|
308
|
+
console.error(error);
|
|
309
|
+
res.statusCode = 500;
|
|
310
|
+
res.end("Internal Server Error");
|
|
311
|
+
}
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/** @type {import("./server-generic.js").ServerRedirect} */
|
|
316
|
+
export function redirect(location, status, init) {
|
|
317
|
+
return {
|
|
318
|
+
contentType: /** @type {any} */ ("text/plain"),
|
|
319
|
+
cookies: init?.cookies,
|
|
320
|
+
headers: { location },
|
|
321
|
+
output: location,
|
|
322
|
+
status,
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* @param {string} path
|
|
328
|
+
* @param {import("./server-generic.js").MethodServer} controller */
|
|
329
|
+
export function route(path, controller) {
|
|
330
|
+
return { controller, path, pattern: new URLPattern(`http://*:*${path}`) };
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
335
|
+
* @param {{ db?: import("./db.js").Db<any>, pkg: { name: string, version: string } }} props
|
|
336
|
+
* @param {ReturnType<typeof route>[]} routes
|
|
337
|
+
*/
|
|
338
|
+
export function router({ db, pkg }, ...routes) {
|
|
339
|
+
const openapi = {
|
|
340
|
+
info: { title: pkg.name, version: pkg.version },
|
|
341
|
+
openapi: "3.1.0",
|
|
342
|
+
paths: Object.fromEntries(
|
|
343
|
+
Object.values(routes).map(({ controller, path }) => [
|
|
344
|
+
path.replace(/:(\w+)/g, "{$1}"),
|
|
345
|
+
Object.fromEntries(
|
|
346
|
+
Object.entries(controller).map(([method, { input, outputs }]) => {
|
|
347
|
+
const arrayOf = ArrayOf(input);
|
|
348
|
+
const parameters = path
|
|
349
|
+
.split("/")
|
|
350
|
+
.filter((component) => /^:/.test(component))
|
|
351
|
+
.map((component) => component.slice(1));
|
|
352
|
+
return [
|
|
353
|
+
method,
|
|
354
|
+
{
|
|
355
|
+
parameters: [
|
|
356
|
+
...parameters.map((name) => ({
|
|
357
|
+
in: "path",
|
|
358
|
+
name,
|
|
359
|
+
required: true,
|
|
360
|
+
schema: { type: "string" },
|
|
361
|
+
})),
|
|
362
|
+
...(method == "post"
|
|
363
|
+
? []
|
|
364
|
+
: Object.entries(input.entries)
|
|
365
|
+
.filter(
|
|
366
|
+
([name]) =>
|
|
367
|
+
name != "lang" && !parameters.includes(name),
|
|
368
|
+
)
|
|
369
|
+
.map(([name, entry]) => ({
|
|
370
|
+
in: "query",
|
|
371
|
+
name,
|
|
372
|
+
required: !entry.expects.includes("undefined"),
|
|
373
|
+
schema: { type: "string" },
|
|
374
|
+
}))),
|
|
375
|
+
],
|
|
376
|
+
...(method == "post"
|
|
377
|
+
? {
|
|
378
|
+
requestBody: {
|
|
379
|
+
content: {
|
|
380
|
+
[Object.values(input.entries).some(
|
|
381
|
+
(entry) =>
|
|
382
|
+
entry.type == "file" || arrayOf(entry, "file"),
|
|
383
|
+
)
|
|
384
|
+
? "multipart/form-data"
|
|
385
|
+
: "application/x-www-form-urlencoded"]: {
|
|
386
|
+
schema: toJsonSchema(
|
|
387
|
+
object(
|
|
388
|
+
Object.fromEntries(
|
|
389
|
+
Object.entries(input.entries).filter(
|
|
390
|
+
([name]) =>
|
|
391
|
+
name != "lang" &&
|
|
392
|
+
!parameters.includes(name),
|
|
393
|
+
),
|
|
394
|
+
),
|
|
395
|
+
),
|
|
396
|
+
{
|
|
397
|
+
ignoreActions: ["trim"],
|
|
398
|
+
overrideSchema: (context) =>
|
|
399
|
+
context.valibotSchema.type == "file"
|
|
400
|
+
? { format: "binary", type: "string" }
|
|
401
|
+
: context.jsonSchema,
|
|
402
|
+
},
|
|
403
|
+
),
|
|
404
|
+
},
|
|
405
|
+
},
|
|
406
|
+
},
|
|
407
|
+
}
|
|
408
|
+
: undefined),
|
|
409
|
+
responses: Object.fromEntries(
|
|
410
|
+
Object.entries(outputs).map(([status, schema]) => [
|
|
411
|
+
status,
|
|
412
|
+
{
|
|
413
|
+
content: {
|
|
414
|
+
["application/json"]: { schema: toJsonSchema(schema) },
|
|
415
|
+
},
|
|
416
|
+
description:
|
|
417
|
+
status == "200"
|
|
418
|
+
? "OK"
|
|
419
|
+
: status == "302"
|
|
420
|
+
? "Found"
|
|
421
|
+
: "Bad Request",
|
|
422
|
+
},
|
|
423
|
+
]),
|
|
424
|
+
),
|
|
425
|
+
},
|
|
426
|
+
];
|
|
427
|
+
}),
|
|
428
|
+
),
|
|
429
|
+
]),
|
|
430
|
+
),
|
|
431
|
+
servers: [{ url: process.env["ORIGIN"] || "" }],
|
|
432
|
+
};
|
|
433
|
+
|
|
434
|
+
Po.set(async (lang) =>
|
|
435
|
+
po.parse(await readFile(`po/${encodeURIComponent(lang)}.po`, "utf-8")),
|
|
436
|
+
);
|
|
437
|
+
|
|
438
|
+
const poPattern = new URLPattern(`http://*:*/po/:lang`);
|
|
439
|
+
|
|
440
|
+
/** @type {Map<(req: any, res: any) => void, string[]>} */
|
|
441
|
+
const TablesInOutput = new Map();
|
|
442
|
+
for (const { controller } of Object.values(routes)) {
|
|
443
|
+
for (const [method, { handler, outputs }] of Object.entries(controller)) {
|
|
444
|
+
if (method == "get") {
|
|
445
|
+
/** @type {string[]} */
|
|
446
|
+
let tablesInOutput = [];
|
|
447
|
+
|
|
448
|
+
/** @param {import("valibot").ArraySchema<any, any> | import("valibot").ObjectSchema<import("valibot").ObjectEntries, any> | import("valibot").StringSchema<any>} schema */
|
|
449
|
+
function walk(schema) {
|
|
450
|
+
if (schema.type == "array") {
|
|
451
|
+
walk(schema.item);
|
|
452
|
+
} else if (schema.type == "object") {
|
|
453
|
+
for (const entry of Object.values(schema.entries)) {
|
|
454
|
+
walk(/** @type {any} */ (entry));
|
|
455
|
+
}
|
|
456
|
+
} else if (db) {
|
|
457
|
+
for (const [name, table] of Object.entries(
|
|
458
|
+
/** @type {Record<string, import("valibot").ObjectSchema<import("valibot").ObjectEntries, any>>} */ (
|
|
459
|
+
db.schema
|
|
460
|
+
),
|
|
461
|
+
)) {
|
|
462
|
+
for (const column of Object.values(table.entries)) {
|
|
463
|
+
if (column == schema) {
|
|
464
|
+
tablesInOutput.push(name);
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
for (const schema of Object.values(outputs)) {
|
|
471
|
+
walk(schema);
|
|
472
|
+
}
|
|
473
|
+
if (tablesInOutput.length) {
|
|
474
|
+
TablesInOutput.set(handler, [...new Set(tablesInOutput)]);
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/**
|
|
481
|
+
* @param {import("node:http").IncomingMessage} req
|
|
482
|
+
* @param {import("node:http").ServerResponse<import("node:http").IncomingMessage>} res
|
|
483
|
+
* @param {() => void} [next]
|
|
484
|
+
*/
|
|
485
|
+
async function onRequest(req, res, next) {
|
|
486
|
+
const lowercase = req.method?.toLowerCase();
|
|
487
|
+
const url = new URL(`http://${req.headers.host}${req.url}`);
|
|
488
|
+
|
|
489
|
+
const pathname = poPattern.exec(url)?.pathname;
|
|
490
|
+
if (pathname) {
|
|
491
|
+
const lang = acceptLanguage.get(pathname.groups["lang"]) || "en";
|
|
492
|
+
if (lang != "en") {
|
|
493
|
+
res.setHeader("content-type", "application/json");
|
|
494
|
+
res.end(JSON.stringify(await Po.get(lang)));
|
|
495
|
+
return;
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
for (const { controller, pattern } of Object.values(routes)) {
|
|
500
|
+
const pathname = pattern.exec(url)?.pathname;
|
|
501
|
+
if (pathname) {
|
|
502
|
+
for (const [method, { handler }] of Object.entries(controller)) {
|
|
503
|
+
if (method == lowercase) {
|
|
504
|
+
const lang =
|
|
505
|
+
acceptLanguage.get(req.headers["accept-language"]) || "en";
|
|
506
|
+
if (!url.searchParams.has(lang)) {
|
|
507
|
+
url.searchParams.set("lang", lang);
|
|
508
|
+
}
|
|
509
|
+
for (const [name, value] of Object.entries(pathname.groups)) {
|
|
510
|
+
if (!url.searchParams.has(name)) {
|
|
511
|
+
url.searchParams.set(name, value || "");
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
req.url = url.pathname + (url.search || "");
|
|
515
|
+
|
|
516
|
+
if (method == "get" && req.headers.accept == "text/event-stream") {
|
|
517
|
+
res.setHeader("x-accel-buffering", "no");
|
|
518
|
+
res.setHeader("content-type", "text/event-stream");
|
|
519
|
+
res.setHeader("cache-control", "no-cache");
|
|
520
|
+
const tablesInOutput = TablesInOutput.get(handler);
|
|
521
|
+
if (tablesInOutput && db) {
|
|
522
|
+
let busy = false;
|
|
523
|
+
let queue = 0;
|
|
524
|
+
let startedAt = 0;
|
|
525
|
+
let endedAt = 0;
|
|
526
|
+
let lastHash = "";
|
|
527
|
+
const interval = setInterval(() => {
|
|
528
|
+
const now = Date.now();
|
|
529
|
+
if (
|
|
530
|
+
!busy &&
|
|
531
|
+
queue &&
|
|
532
|
+
endedAt < now - (endedAt - startedAt) * 3
|
|
533
|
+
) {
|
|
534
|
+
busy = true;
|
|
535
|
+
queue = 0;
|
|
536
|
+
startedAt = now;
|
|
537
|
+
/** @type {Record<string, string>} */
|
|
538
|
+
const headers = {};
|
|
539
|
+
let statusCode = 200;
|
|
540
|
+
handler(req, {
|
|
541
|
+
end(body) {
|
|
542
|
+
const data = JSON.stringify({
|
|
543
|
+
body,
|
|
544
|
+
headers,
|
|
545
|
+
statusCode,
|
|
546
|
+
});
|
|
547
|
+
const hash = createHash("sha256")
|
|
548
|
+
.update(data)
|
|
549
|
+
.digest("hex");
|
|
550
|
+
if (lastHash != hash) {
|
|
551
|
+
lastHash = hash;
|
|
552
|
+
res.write(`data: ${data}\n\n`);
|
|
553
|
+
const expressCompressionResponse =
|
|
554
|
+
/** @type {any} */ (res);
|
|
555
|
+
if (
|
|
556
|
+
typeof expressCompressionResponse.flush ==
|
|
557
|
+
"function"
|
|
558
|
+
) {
|
|
559
|
+
expressCompressionResponse.flush();
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
endedAt = Date.now();
|
|
563
|
+
busy = false;
|
|
564
|
+
},
|
|
565
|
+
setHeader(name, value) {
|
|
566
|
+
headers[name] = value;
|
|
567
|
+
},
|
|
568
|
+
set statusCode(/** @type {number} */ value) {
|
|
569
|
+
statusCode = value;
|
|
570
|
+
},
|
|
571
|
+
});
|
|
572
|
+
}
|
|
573
|
+
}, 100);
|
|
574
|
+
const unsubscribe = db.subscribe(({ table }) => {
|
|
575
|
+
if (tablesInOutput.includes(table)) {
|
|
576
|
+
queue++;
|
|
577
|
+
}
|
|
578
|
+
});
|
|
579
|
+
req.on("close", () => {
|
|
580
|
+
unsubscribe();
|
|
581
|
+
clearInterval(interval);
|
|
582
|
+
});
|
|
583
|
+
} else {
|
|
584
|
+
res.end("data: null\n\n");
|
|
585
|
+
}
|
|
586
|
+
return;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
handler(req, res);
|
|
590
|
+
return;
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
if (lowercase == "options") {
|
|
595
|
+
res.statusCode = 204;
|
|
596
|
+
res.setHeader(
|
|
597
|
+
"allow",
|
|
598
|
+
[
|
|
599
|
+
...new Set([
|
|
600
|
+
"OPTIONS",
|
|
601
|
+
...Object.keys(controller).map((x) => x.toUpperCase()),
|
|
602
|
+
]),
|
|
603
|
+
].join(", "),
|
|
604
|
+
);
|
|
605
|
+
res.end();
|
|
606
|
+
return;
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
res.statusCode = 405;
|
|
610
|
+
res.end("Method Not Allowed");
|
|
611
|
+
return;
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
if (req.url == "/api" || req.url?.startsWith("/api/")) {
|
|
616
|
+
res.statusCode = 404;
|
|
617
|
+
res.end("Not Found");
|
|
618
|
+
return;
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
if (req.url == "/openapi.json") {
|
|
622
|
+
res.setHeader("content-type", "application/json");
|
|
623
|
+
res.end(JSON.stringify(openapi));
|
|
624
|
+
return;
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
if (!next) {
|
|
628
|
+
res.statusCode = 404;
|
|
629
|
+
res.end("Not Found");
|
|
630
|
+
return;
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
next();
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
return { onRequest, openapi };
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
/** @type {import("./server-generic.js").ServerText} */
|
|
640
|
+
export function text(output, status) {
|
|
641
|
+
return {
|
|
642
|
+
contentType: /** @type {any} */ ("text/plain"),
|
|
643
|
+
output,
|
|
644
|
+
status,
|
|
645
|
+
};
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
/** @param {import("valibot").ObjectSchema<any, any>} input */
|
|
649
|
+
function ArrayOf(input) {
|
|
650
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
651
|
+
const entries = Object.values(input);
|
|
652
|
+
/**
|
|
653
|
+
* @param {typeof entries[0]} entry
|
|
654
|
+
* @param {string} type
|
|
655
|
+
*/
|
|
656
|
+
return function arrayOf(entry, type) {
|
|
657
|
+
if (entry.type != "array") {
|
|
658
|
+
return false;
|
|
659
|
+
}
|
|
660
|
+
/** @type {import("valibot").ArraySchema<any, any>} */
|
|
661
|
+
const array = /** @type {any} */ (entry);
|
|
662
|
+
return array.item.type == type;
|
|
663
|
+
};
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
function parallelism() {
|
|
667
|
+
return Number(process.env["PARALLELISM"]) || availableParallelism();
|
|
668
|
+
}
|