nish 0.34.1 → 0.36.0

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 (127) hide show
  1. package/README.md +7 -6
  2. package/docs/AI.md +86 -16
  3. package/docs/INSTALL.md +22 -13
  4. package/llms.txt +5 -2
  5. package/package.json +15 -5
  6. package/runtime/async.d.mts +49 -0
  7. package/runtime/async.mjs +27 -0
  8. package/runtime/nish.d.ts +4681 -427
  9. package/runtime/nish.h +6 -2
  10. package/runtime/nish.mjs +4 -0
  11. package/runtime/runtime-net.c +22 -0
  12. package/runtime/runtime-shm.c +93 -2
  13. package/runtime/vite.d.ts +30 -0
  14. package/runtime/vite.mjs +150 -0
  15. package/scripts/build.sh +108 -13
  16. package/std/README.md +131 -131
  17. package/std/base64.ts +34 -5
  18. package/std/bytes.ts +258 -42
  19. package/std/cli-args.ts +296 -70
  20. package/std/cli-flags.ts +184 -19
  21. package/std/cli.ts +475 -54
  22. package/std/collections.ts +121 -23
  23. package/std/compress/checksum.ts +44 -14
  24. package/std/compress/deflate.ts +224 -111
  25. package/std/compress/gzip.ts +217 -53
  26. package/std/compress/zlib.ts +63 -7
  27. package/std/crypto/aes.ts +80 -9
  28. package/std/crypto/base64url.ts +22 -0
  29. package/std/crypto/bcrypt-batch.ts +88 -21
  30. package/std/crypto/bcrypt.ts +60 -0
  31. package/std/crypto/chacha20poly1305.ts +53 -0
  32. package/std/crypto/ct.ts +32 -13
  33. package/std/crypto/ed25519.ts +47 -0
  34. package/std/crypto/hkdf.ts +198 -13
  35. package/std/crypto/hmac.ts +235 -31
  36. package/std/crypto/jwt.ts +192 -63
  37. package/std/crypto/p256.ts +159 -16
  38. package/std/crypto/p384.ts +28 -3
  39. package/std/crypto/pki-roots.ts +32 -2
  40. package/std/crypto/pki.ts +81 -15
  41. package/std/crypto/rsa.ts +33 -3
  42. package/std/crypto/sha1.ts +3 -0
  43. package/std/crypto/sha256.ts +24 -1
  44. package/std/crypto/sha512.ts +99 -14
  45. package/std/crypto/x25519.ts +152 -14
  46. package/std/crypto/x509-random.ts +10 -0
  47. package/std/crypto/x509.ts +67 -7
  48. package/std/json-equal.ts +19 -0
  49. package/std/json-format.ts +112 -13
  50. package/std/json-number.ts +64 -14
  51. package/std/json-pointer.ts +23 -1
  52. package/std/json-tokenizer.ts +100 -29
  53. package/std/json-typed.ts +713 -66
  54. package/std/json-value.ts +400 -124
  55. package/std/json.ts +138 -10
  56. package/std/map.ts +25 -7
  57. package/std/math.ts +36 -4
  58. package/std/net/buffer-pool.ts +42 -11
  59. package/std/net/dns-config.ts +109 -23
  60. package/std/net/dns-message.ts +62 -7
  61. package/std/net/dns-resolver.ts +247 -53
  62. package/std/net/dns.ts +203 -35
  63. package/std/net/fetch.ts +151 -61
  64. package/std/net/handler.ts +278 -80
  65. package/std/net/hpack.ts +262 -52
  66. package/std/net/http-compress.ts +125 -15
  67. package/std/net/http-fields.ts +85 -9
  68. package/std/net/http1-client-codec.ts +100 -19
  69. package/std/net/http1-codec.ts +297 -77
  70. package/std/net/http1-server.ts +430 -94
  71. package/std/net/http2-frame.ts +272 -35
  72. package/std/net/http2-tls.ts +135 -34
  73. package/std/net/http2.ts +247 -106
  74. package/std/net/http3-frame.ts +154 -17
  75. package/std/net/http3-server.ts +275 -101
  76. package/std/net/http3.ts +307 -115
  77. package/std/net/outbound.ts +37 -3
  78. package/std/net/postgres-carrier.ts +144 -8
  79. package/std/net/postgres.ts +801 -112
  80. package/std/net/prefork.ts +164 -31
  81. package/std/net/qpack.ts +105 -18
  82. package/std/net/quic-conn-ack.ts +59 -12
  83. package/std/net/quic-conn-cid.ts +124 -27
  84. package/std/net/quic-conn-params.ts +70 -10
  85. package/std/net/quic-datagram.ts +43 -3
  86. package/std/net/quic-frame.ts +286 -12
  87. package/std/net/quic-listener.ts +101 -32
  88. package/std/net/quic-packet.ts +302 -21
  89. package/std/net/quic-recovery.ts +264 -52
  90. package/std/net/quic-stream.ts +353 -91
  91. package/std/net/quic.ts +1124 -201
  92. package/std/net/router.ts +87 -40
  93. package/std/net/tls/client.ts +166 -51
  94. package/std/net/tls/codec.ts +413 -56
  95. package/std/net/tls/record-client.ts +110 -55
  96. package/std/net/tls/record-server.ts +159 -59
  97. package/std/net/tls/record.ts +109 -12
  98. package/std/net/tls/schedule.ts +235 -26
  99. package/std/net/tls/ticket.ts +159 -40
  100. package/std/net/tls/verify.ts +49 -3
  101. package/std/net/tls-tcp.ts +250 -45
  102. package/std/net/tls.ts +221 -59
  103. package/std/net/upstream.ts +81 -45
  104. package/std/net/url.ts +97 -13
  105. package/std/net/websocket.ts +122 -12
  106. package/std/net/webtransport.ts +220 -89
  107. package/std/pair.ts +10 -2
  108. package/std/random.ts +34 -9
  109. package/std/regex.ts +352 -198
  110. package/std/region.ts +70 -15
  111. package/std/secret.ts +52 -11
  112. package/std/sort.ts +17 -2
  113. package/std/sqlite.ts +256 -35
  114. package/std/sync/ring.ts +430 -40
  115. package/std/sync/shm.ts +222 -15
  116. package/std/term-io.ts +137 -64
  117. package/std/term-pixels.ts +144 -19
  118. package/std/term-probe.ts +66 -25
  119. package/std/term-rate.ts +128 -57
  120. package/std/term.ts +332 -121
  121. package/std/testing.ts +130 -32
  122. package/std/text.ts +73 -7
  123. package/std/threads.ts +229 -41
  124. package/std/yaml-json.ts +94 -82
  125. package/std/yaml-lines.ts +47 -15
  126. package/std/yaml-write.ts +24 -16
  127. package/std/yaml.ts +764 -384
@@ -3,6 +3,7 @@
3
3
  * starts one worker process per core, restarts one that dies, and stops them
4
4
  * all on SIGTERM or SIGINT (`docs/wp42-multicore-server.md` §2.3).
5
5
  *
6
+ * import { tcpListenFlags } from "nish:net";
6
7
  * import { preforkWorkers } from "nish/net/prefork";
7
8
  *
8
9
  * export const main = (): number => {
@@ -222,6 +223,12 @@ const preforkDigits = (s: string): boolean => {
222
223
  * How many workers to start: `env` (`NISH_WORKERS`, `null` when unset) when it
223
224
  * is a positive whole number, else `requested` when positive, else the
224
225
  * platform's default.
226
+ *
227
+ * @param requested the program's own choice, `PreforkConfig.workers`; 0 or
228
+ * less leaves it to the environment or the platform
229
+ * @param env the raw `NISH_WORKERS` text, or `null` when it is unset; text
230
+ * that is not a positive whole number is ignored
231
+ * @returns how many worker processes the supervisor starts, always 1 or more
225
232
  */
226
233
  export const preforkWorkerCount = (requested: i32, env: string | null): i32 => {
227
234
  if (env !== null && preforkDigits(env)) {
@@ -240,7 +247,13 @@ export const preforkWorkerCount = (requested: i32, env: string | null): i32 => {
240
247
  return cores > 0 ? cores : 1
241
248
  }
242
249
 
243
- /** The worker index `args` carries in its last element, or -1 when none. */
250
+ /**
251
+ * The worker index `args` carries in its last element, or -1 when none.
252
+ *
253
+ * @param args the process's arguments, `process.argv`
254
+ * @returns this worker's index, 0 to `N - 1`, or -1 in the supervisor (or any
255
+ * process not started by one)
256
+ */
244
257
  export const preforkIndex = (args: string[]): i32 => {
245
258
  if (args.length < 2) {
246
259
  return -1
@@ -256,6 +269,10 @@ export const preforkIndex = (args: string[]): i32 => {
256
269
  /**
257
270
  * `args` (`process.argv`) without the worker flag and the channels and
258
271
  * listener flags before it, the same in every process.
272
+ *
273
+ * @param args the process's arguments, `process.argv`
274
+ * @returns a new list of the arguments the user gave, without the flags the
275
+ * supervisor appended, so a worker reads the same ones the supervisor did
259
276
  */
260
277
  export const preforkArgs = (args: string[]): string[] => {
261
278
  let keep: i32 = toI32(args.length)
@@ -299,17 +316,41 @@ const preforkChannel = (args: string[], at: i32): i32 => {
299
316
  return preforkDigits(field) ? parseInt(field) : -1
300
317
  }
301
318
 
302
- /** In a worker, its end of its supervisor channel; -1 when it has none. */
319
+ /**
320
+ * In a worker, its end of its supervisor channel; -1 when it has none.
321
+ *
322
+ * @param args the worker's arguments, `process.argv`
323
+ * @returns the descriptor the worker talks to the supervisor through, or -1
324
+ * without `supervisorChannels` or outside a worker
325
+ */
303
326
  export const preforkSupervisorChannel = (args: string[]): i32 => preforkChannel(args, 0)
304
327
 
305
- /** In a worker, its end of its channel to the service worker; -1 in the service worker or when none. */
328
+ /**
329
+ * In a worker, its end of its channel to the service worker; -1 in the service worker or when none.
330
+ *
331
+ * @param args the worker's arguments, `process.argv`
332
+ * @returns the descriptor this worker reaches the service worker through, or -1
333
+ */
306
334
  export const preforkServiceChannel = (args: string[]): i32 => preforkChannel(args, 1)
307
335
 
308
- /** In the service worker, its end of the channel to worker `index`; -1 for itself or elsewhere. */
336
+ /**
337
+ * In the service worker, its end of the channel to worker `index`; -1 for itself or elsewhere.
338
+ *
339
+ * @param args the service worker's arguments, `process.argv`
340
+ * @param index the other worker whose channel to look up, 0 to `N - 1`
341
+ * @returns the descriptor the service worker reaches worker `index` through,
342
+ * or -1 for its own index, an index out of range, or any other process
343
+ */
309
344
  export const preforkServiceChannelOf = (args: string[], index: i32): i32 =>
310
345
  index < 0 || index > 2147483645 ? -1 : preforkChannel(args, 2 + index)
311
346
 
312
- /** In a worker, the listener the supervisor bound for it (`listenPort`); -1 when none. */
347
+ /**
348
+ * In a worker, the listener the supervisor bound for it (`listenPort`); -1 when none.
349
+ *
350
+ * @param args the worker's arguments, `process.argv`
351
+ * @returns the listening socket's descriptor to serve instead of binding one,
352
+ * or -1 when the worker should bind its own
353
+ */
313
354
  export const preforkListener = (args: string[]): i32 => {
314
355
  if (preforkIndex(args) < 0) {
315
356
  return -1
@@ -333,40 +374,89 @@ const PREFORK_SIGKILL: i32 = 9
333
374
 
334
375
  /** One worker's slot in the supervisor. */
335
376
  class PreforkSlot {
336
- /** When the running worker started, or when the waiting one may start. */
377
+ /**
378
+ * When the running worker started, or when the waiting one may start.
379
+ *
380
+ * @internal Read and set by `PreforkSupervisor`'s start, backoff and restart to time a worker's restart.
381
+ */
337
382
  at: i64 = 0
338
- /** The running worker's pid, 0 while it waits to be started again. */
383
+ /**
384
+ * The running worker's pid, 0 while it waits to be started again.
385
+ *
386
+ * @internal Read by `PreforkSupervisor` to match a reaped child to its slot and to signal it.
387
+ */
339
388
  pid: i32 = 0
340
- /** The wait before the last restart, 0 after a worker that ran long. */
389
+ /**
390
+ * The wait before the last restart, 0 after a worker that ran long.
391
+ *
392
+ * @internal Read and set by `PreforkSupervisor.backoff` to double the wait after a quick exit.
393
+ */
341
394
  delay: i32 = 0
342
- /** The worker exited with `restart` off, and is never started again. */
395
+ /**
396
+ * The worker exited with `restart` off, and is never started again.
397
+ *
398
+ * @internal Read by `PreforkSupervisor`'s restarts so a worker that exited with `restart` off stays gone.
399
+ */
343
400
  gone: boolean = false
344
- /** The supervisor's end of the worker's channel, -1 when it has none. */
401
+ /**
402
+ * The supervisor's end of the worker's channel, -1 when it has none.
403
+ *
404
+ * @internal Read by `preforkSupervise` to hand the end to the program's callback, and closed by `PreforkSupervisor.closeChannel`.
405
+ */
345
406
  channel: i32 = -1
346
407
  }
347
408
 
348
409
  /** The supervisor's state while it runs. */
349
410
  class PreforkSupervisor {
350
- config: PreforkConfig
411
+ private config: PreforkConfig
412
+ /**
413
+ * The executable each worker is spawned from: `/proc/self/exe` resolved on
414
+ * Linux, else the program's first argument.
415
+ *
416
+ * @internal Read by `preforkSupervise` to name the program when its first
417
+ * start fails.
418
+ */
351
419
  program: string
352
- args: string[]
420
+ private args: string[]
421
+ /**
422
+ * One slot per worker, by index.
423
+ *
424
+ * @internal Read by `preforkSupervise` to find a worker's channel from the
425
+ * token its readiness event carries.
426
+ */
353
427
  slots: PreforkSlot[]
354
428
  /** While the group starts: each worker's end to the service worker, -1 for none. */
355
- toService: i32[]
429
+ private toService: i32[]
356
430
  /** While the group starts: the service worker's end to each worker, -1 for none. */
357
- fromWorkers: i32[]
431
+ private fromWorkers: i32[]
358
432
  /** When a group still running gets SIGKILL. */
359
- cascadeAt: i64 = 0
433
+ private cascadeAt: i64 = 0
434
+ /**
435
+ * How many workers are running now, started and not yet reaped.
436
+ *
437
+ * @internal Read by `preforkSupervise` to see when, with `restart` off,
438
+ * every worker has exited.
439
+ */
360
440
  running: i32 = 0
361
441
  /** The readiness loop each supervisor channel is added to. */
362
- loop: i32
442
+ private loop: i32
363
443
  /** `config.serviceWorker`: with it at 0 or above, the workers are one group. */
364
- service: i32
444
+ private service: i32
365
445
  /** The group is being stopped, to be started again once none of it is left. */
366
- cascading: boolean = false
446
+ private cascading: boolean = false
367
447
  /** Worker i's listener at i, held for the whole run; empty without `listenPort`. */
368
- listeners: i32[]
448
+ private listeners: i32[]
369
449
 
450
+ /**
451
+ * A supervisor with one empty slot per worker and no worker started yet.
452
+ *
453
+ * @param config the checked settings it runs by, kept and read throughout
454
+ * @param args the supervisor's own arguments, `process.argv`, which every
455
+ * worker is started with
456
+ * @param count how many workers to keep running
457
+ * @param loop the readiness loop each supervisor channel is added to; the
458
+ * caller owns and closes it
459
+ */
370
460
  constructor(config: PreforkConfig, args: string[], count: i32, loop: i32) {
371
461
  this.config = config
372
462
  this.loop = loop
@@ -391,7 +481,7 @@ class PreforkSupervisor {
391
481
  * Start worker `index` with its channel ends, the service ones from
392
482
  * `pairs`; false when a socketpair, the loop or the spawn refused it.
393
483
  */
394
- start(index: i32): boolean {
484
+ private start(index: i32): boolean {
395
485
  const fds: i32[] = []
396
486
  let own: i32 = -1
397
487
  if (this.config.supervisorChannels) {
@@ -458,7 +548,7 @@ class PreforkSupervisor {
458
548
  }
459
549
 
460
550
  /** The slot `pid` ran in, or -1. */
461
- find(pid: i32): i32 {
551
+ private find(pid: i32): i32 {
462
552
  for (let i: i32 = 0; i < toI32(this.slots.length); i++) {
463
553
  if (this.slots[i].pid === pid) {
464
554
  return i
@@ -472,7 +562,7 @@ class PreforkSupervisor {
472
562
  * at least `quickExitMs`, else, or when `failed` (`spawn` refused it), after
473
563
  * a wait that starts at `backoffMs` and doubles up to `backoffCapMs`.
474
564
  */
475
- backoff(slot: PreforkSlot, now: i64, failed: boolean): void {
565
+ private backoff(slot: PreforkSlot, now: i64, failed: boolean): void {
476
566
  if (failed || now - slot.at < toI64(this.config.quickExitMs)) {
477
567
  const cap: i32 = this.config.backoffCapMs
478
568
  if (slot.delay === 0) {
@@ -493,6 +583,8 @@ class PreforkSupervisor {
493
583
  * program spawned itself before calling `preforkWith`) is reaped too, and
494
584
  * its status is discarded. Answers false when `waitPid` fails, which means
495
585
  * no child is left to wait for.
586
+ *
587
+ * @internal Called by `preforkSupervise` on every tick, and by `stop`.
496
588
  */
497
589
  reap(stopping: boolean): boolean {
498
590
  while (this.running > 0) {
@@ -529,7 +621,11 @@ class PreforkSupervisor {
529
621
  return true
530
622
  }
531
623
 
532
- /** Close the supervisor's end of `slot`'s channel, which takes it off the loop. */
624
+ /**
625
+ * Close the supervisor's end of `slot`'s channel, which takes it off the loop.
626
+ *
627
+ * @internal Called by `preforkSupervise` after a channel hangs up.
628
+ */
533
629
  closeChannel(slot: PreforkSlot): void {
534
630
  if (slot.channel >= 0) {
535
631
  netClose(slot.channel)
@@ -538,13 +634,17 @@ class PreforkSupervisor {
538
634
  }
539
635
 
540
636
  /** Stop the group so it starts again whole: SIGTERM now, SIGKILL after `graceMs`. */
541
- cascade(now: i64): void {
637
+ private cascade(now: i64): void {
542
638
  this.cascading = true
543
639
  this.cascadeAt = now + toI64(this.config.graceMs)
544
640
  this.signal(PREFORK_SIGTERM)
545
641
  }
546
642
 
547
- /** SIGKILL a stopping group that outlived `graceMs`, again each `graceMs`. */
643
+ /**
644
+ * SIGKILL a stopping group that outlived `graceMs`, again each `graceMs`.
645
+ *
646
+ * @internal Called by `preforkSupervise` on every tick.
647
+ */
548
648
  escalate(): void {
549
649
  if (!this.cascading || this.running <= 0) {
550
650
  return
@@ -557,7 +657,7 @@ class PreforkSupervisor {
557
657
  }
558
658
 
559
659
  /** A pair from each worker to the service worker; false when one failed. */
560
- pairs(): boolean {
660
+ private pairs(): boolean {
561
661
  for (let i: i32 = 0; i < toI32(this.slots.length); i++) {
562
662
  if (i !== this.service) {
563
663
  const pair: i32[] = [-1, -1]
@@ -572,7 +672,7 @@ class PreforkSupervisor {
572
672
  }
573
673
 
574
674
  /** Close every end `pairs` made: the workers hold their copies now. */
575
- closePairs(): void {
675
+ private closePairs(): void {
576
676
  for (let i: i32 = 0; i < toI32(this.slots.length); i++) {
577
677
  if (this.toService[i] >= 0) {
578
678
  netClose(this.toService[i])
@@ -591,7 +691,7 @@ class PreforkSupervisor {
591
691
  * false when `first`; otherwise it backs off the slot that failed and stops
592
692
  * the workers it started, to be tried again whole.
593
693
  */
594
- restartGroup(first: boolean): boolean {
694
+ private restartGroup(first: boolean): boolean {
595
695
  if (this.running > 0) {
596
696
  return true
597
697
  }
@@ -631,6 +731,8 @@ class PreforkSupervisor {
631
731
  * Start every waiting worker whose time has come. When `first`, answer false
632
732
  * at the first `spawn` that fails; otherwise a failed start waits on the
633
733
  * same backoff as a worker that exited at once, and the answer is true.
734
+ *
735
+ * @internal Called by `preforkSupervise` for the first start and on every tick.
634
736
  */
635
737
  restart(first: boolean): boolean {
636
738
  if (this.service >= 0) {
@@ -653,6 +755,8 @@ class PreforkSupervisor {
653
755
  /**
654
756
  * One listener per worker, in index order, which is the order steering
655
757
  * counts CPUs in; false, with the ones made closed, when one cannot bind.
758
+ *
759
+ * @internal Called by `preforkSupervise` before the first start when `listenPort` is set.
656
760
  */
657
761
  listen(steer: boolean): boolean {
658
762
  const config: PreforkConfig = this.config
@@ -673,7 +777,11 @@ class PreforkSupervisor {
673
777
  return true
674
778
  }
675
779
 
676
- /** Close the listeners `listen` made. */
780
+ /**
781
+ * Close the listeners `listen` made.
782
+ *
783
+ * @internal Called by `preforkSupervise` once the workers are stopped.
784
+ */
677
785
  closeListeners(): void {
678
786
  for (let i: i32 = 0; i < toI32(this.listeners.length); i++) {
679
787
  netClose(this.listeners[i])
@@ -682,7 +790,7 @@ class PreforkSupervisor {
682
790
  }
683
791
 
684
792
  /** Send `sig` to every running worker. */
685
- signal(sig: i32): void {
793
+ private signal(sig: i32): void {
686
794
  for (let i: i32 = 0; i < toI32(this.slots.length); i++) {
687
795
  if (this.slots[i].pid > 0) {
688
796
  kill(this.slots[i].pid, sig)
@@ -697,6 +805,8 @@ class PreforkSupervisor {
697
805
  * child that is not a worker is skipped in both phases, and the SIGKILL
698
806
  * phase ends only when the last worker is reaped or no child is left. The
699
807
  * supervisor's channel ends are closed first, so only `signals` wakes it.
808
+ *
809
+ * @internal Called by `preforkSupervise` once, on shutdown.
700
810
  */
701
811
  stop(loop: i32, signals: i32, ready: i32[]): void {
702
812
  for (let i: i32 = 0; i < toI32(this.slots.length); i++) {
@@ -778,6 +888,13 @@ const preforkDiscard = (buf: u8[], _index: i32, fd: i32, _events: i32): void =>
778
888
  * listener could be made (no worker is started). Panics on a config
779
889
  * `preforkInvalid` names a field of. Supervisor channels are read and
780
890
  * dropped; `preforkSupervise` hands them to the program.
891
+ *
892
+ * @param args the process's arguments, `process.argv`
893
+ * @param config the supervisor's settings, every field checked (a bad one
894
+ * panics) in the supervisor and each worker alike; past that check a
895
+ * worker reads only `pin`
896
+ * @returns in a worker, its index at once; in the supervisor, once it is done,
897
+ * `PREFORK_STOPPED`, `PREFORK_WORKERS_EXITED` or `PREFORK_SPAWN_FAILED`
781
898
  */
782
899
  export const preforkWith = (args: string[], config: PreforkConfig): i32 => {
783
900
  const buf: u8[] = new Array<u8>(512)
@@ -798,6 +915,16 @@ export const preforkWith = (args: string[], config: PreforkConfig): i32 => {
798
915
  * `availableParallelism()`; and, through `preforkInvalid`, on seqpacket
799
916
  * channels off Linux. A listener that cannot bind answers
800
917
  * `PREFORK_SPAWN_FAILED` before any worker starts.
918
+ *
919
+ * @param args the process's arguments, `process.argv`
920
+ * @param config the supervisor's settings, every field checked (a bad one
921
+ * panics) in the supervisor and each worker alike; past that check a
922
+ * worker reads only `pin`
923
+ * @param state the program's own value, passed to every `onChannel` call
924
+ * @param onChannel what the supervisor runs when worker `index`'s channel
925
+ * `fd` wakes it, with `pollWait`'s `events`
926
+ * @returns in a worker, its index at once; in the supervisor, once it is done,
927
+ * `PREFORK_STOPPED`, `PREFORK_WORKERS_EXITED` or `PREFORK_SPAWN_FAILED`
801
928
  */
802
929
  export const preforkSupervise = <S>(
803
930
  args: string[],
@@ -888,5 +1015,11 @@ export const preforkSupervise = <S>(
888
1015
  return result
889
1016
  }
890
1017
 
891
- /** `preforkWith` with the default `PreforkConfig`. */
1018
+ /**
1019
+ * `preforkWith` with the default `PreforkConfig`.
1020
+ *
1021
+ * @param args the process's arguments, `process.argv`
1022
+ * @returns in a worker, its index at once; in the supervisor, once it is done,
1023
+ * `PREFORK_STOPPED` or `PREFORK_SPAWN_FAILED`
1024
+ */
892
1025
  export const preforkWorkers = (args: string[]): i32 => preforkWith(args, new PreforkConfig())
package/std/net/qpack.ts CHANGED
@@ -8,6 +8,9 @@
8
8
  *
9
9
  * import { QpackDecoder, QpackEncoder, QPACK_OK } from "nish/net/qpack";
10
10
  *
11
+ * const name: u8[] = [58, 112, 97, 116, 104]; // ":path"
12
+ * const value: u8[] = [47]; // "/"
13
+ *
11
14
  * const enc = new QpackEncoder(); // one per connection
12
15
  * const section: u8[] = [];
13
16
  * enc.beginSection(section);
@@ -598,6 +601,9 @@ const qpackPushString = (
598
601
  * Appends a Stream Cancellation for `streamId` (§4.4.2), the one instruction a
599
602
  * decoder of capacity 0 may send on its decoder stream, and may also omit.
600
603
  * A stream ID outside 0 to 2^62 - 1 panics.
604
+ * @param out Where the instruction goes, appended after what it holds: one byte for a stream ID under 63,
605
+ * else one plus its continuation bytes.
606
+ * @param streamId The ID of the request stream whose section the peer's encoder may forget, 0 to 2^62 - 1.
601
607
  */
602
608
  export const qpackPushStreamCancellation = (out: u8[], streamId: i64): void => {
603
609
  qpackPushInteger(out, 64, 6, streamId)
@@ -730,21 +736,41 @@ const qpackReadString = (d: QpackDecoder, src: u8[], prefixBits: i32): i32 => {
730
736
  * `decode` answers a section's fields as windows into `bytes`: field `i`'s
731
737
  * name is `bytes[nameStart[i], + nameLength[i])`, its value likewise, and
732
738
  * `neverIndexed[i]` is its N bit, which an intermediary forwarding it must
733
- * keep (§4.5.4). All of them are replaced by every call.
739
+ * keep (§4.5.4). All of them are replaced by every `decode`.
734
740
  */
735
741
  export class QpackDecoder {
742
+ /** @internal The Huffman decoder's tables, read by this module's `qpackReadString`; not for callers. */
736
743
  huffman: HpackHuffman
737
- /** The connection error a call met, which every later call answers; `QPACK_OK` until then. */
744
+ /**
745
+ * The connection error a call met, which every later call answers; `QPACK_OK` until then.
746
+ * @internal Reset by `http3.ts`'s `restart`; callers read the answers `decode` and
747
+ * `receiveEncoderStream` give.
748
+ */
738
749
  failed: i64
750
+ /**
751
+ * Every name and value of the last section `decode` answered, back to back; emptied by every
752
+ * `decode` call, including one that refuses, and valid until the next.
753
+ */
739
754
  bytes: u8[]
755
+ /** Where field `i`'s name starts in `bytes`, for `i` below `count`. */
740
756
  nameStart: i32[]
757
+ /** How many bytes field `i`'s name has in `bytes`. */
741
758
  nameLength: i32[]
759
+ /** Where field `i`'s value starts in `bytes`, for `i` below `count`. */
742
760
  valueStart: i32[]
761
+ /** How many bytes field `i`'s value has in `bytes`. */
743
762
  valueLength: i32[]
763
+ /**
764
+ * Field `i`'s N bit (§4.5.4): true when the peer marked it never to be indexed; always false
765
+ * for an Indexed Field Line, which has no such bit.
766
+ */
744
767
  neverIndexed: boolean[]
745
768
  /** SETTINGS_MAX_FIELD_SECTION_SIZE as RFC 9114 §4.2.2 counts it. */
746
769
  maxFieldSectionSize: i32
747
- /** Which rule `failed` broke, a `QPACK_REASON_*`. */
770
+ /**
771
+ * Which rule the connection error broke, a `QPACK_REASON_*`; `QPACK_REASON_NONE` until a call
772
+ * answers QPACK_DECOMPRESSION_FAILED or QPACK_ENCODER_STREAM_ERROR. Reset by `http3.ts`'s `restart`.
773
+ */
748
774
  reason: i32
749
775
  /** How many Set Dynamic Table Capacity 0 instructions the encoder stream carried, saturating at 2^31 - 1. */
750
776
  capacityInstructions: i32
@@ -754,12 +780,25 @@ export class QpackDecoder {
754
780
  * What the section being decoded may still add to `bytes` and its fields'
755
781
  * overhead before it is past `maxFieldSectionSize`: every append is checked
756
782
  * against it first, so a section past the limit never grows `bytes`.
783
+ * @internal Read and lowered by this module's `qpackReadString`; not for callers.
757
784
  */
758
785
  room: i64
759
- /** Where the next read of the section being decoded starts, and where it ends. */
786
+ /**
787
+ * @internal Where the next read of the section being decoded starts; read and advanced by this
788
+ * module's `qpackHasByte`, `qpackReadInteger` and `qpackReadString`.
789
+ */
760
790
  at: i32
791
+ /**
792
+ * @internal Where the section being decoded ends; read by this module's `qpackHasByte` and
793
+ * `qpackReadString`.
794
+ */
761
795
  end: i32
762
796
 
797
+ /**
798
+ * Makes a decoder with no fields, no connection error and no capacity instructions counted.
799
+ * @param maxFieldSectionSize The largest field section `decode` accepts, as RFC 9114 §4.2.2
800
+ * counts it: each field's name and value in bytes plus 32. A negative limit panics.
801
+ */
763
802
  constructor(maxFieldSectionSize: i32) {
764
803
  if (maxFieldSectionSize < 0) {
765
804
  panic(`QpackDecoder: a field-section limit of ${maxFieldSectionSize}`)
@@ -787,6 +826,13 @@ export class QpackDecoder {
787
826
  * module comment lists what each means. A decoder whose encoder stream was
788
827
  * refused answers QPACK_ENCODER_STREAM_ERROR again instead, since the
789
828
  * connection is closing. Anything but `QPACK_OK` leaves no fields.
829
+ * @param section The buffer holding the section; only read, and its names and values are
830
+ * copied into `bytes`, so it may be reused once the call returns.
831
+ * @param off Where the section starts in `section`.
832
+ * @param len How many bytes the section has; a window outside `section` panics.
833
+ * @returns `QPACK_OK` with `count` fields; `QPACK_SECTION_TOO_LARGE`, which does not stick;
834
+ * QPACK_DECOMPRESSION_FAILED, which sticks; or, without reading anything, the
835
+ * QPACK_DECOMPRESSION_FAILED or QPACK_ENCODER_STREAM_ERROR an earlier call met.
790
836
  */
791
837
  decode(section: u8[], off: i32, len: i32): i64 {
792
838
  qpackCheckWindow("QpackDecoder.decode", section, off, len)
@@ -813,6 +859,12 @@ export class QpackDecoder {
813
859
  * instruction a peer may send is Set Dynamic Table Capacity 0, the single
814
860
  * byte `0x20`, so every other first byte is refused on sight: its top bits
815
861
  * already say which instruction it begins, and none of them can be valid.
862
+ * @param buf The bytes the encoder stream carried; only read, nothing is kept.
863
+ * @param off Where they start in `buf`.
864
+ * @param len How many there are; zero is accepted, and a window outside `buf` panics.
865
+ * @returns `QPACK_OK` when every byte was a Set Dynamic Table Capacity 0; QPACK_ENCODER_STREAM_ERROR,
866
+ * which sticks, at the first byte that is not; or, without reading anything, the
867
+ * QPACK_DECOMPRESSION_FAILED or QPACK_ENCODER_STREAM_ERROR an earlier call met.
816
868
  */
817
869
  receiveEncoderStream(buf: u8[], off: i32, len: i32): i64 {
818
870
  qpackCheckWindow("QpackDecoder.receiveEncoderStream", buf, off, len)
@@ -842,7 +894,7 @@ export class QpackDecoder {
842
894
  }
843
895
 
844
896
  /** Empties the fields, keeping the arrays' storage for the next section. */
845
- clear(): void {
897
+ private clear(): void {
846
898
  while (this.bytes.length > 0) {
847
899
  this.bytes.pop()
848
900
  }
@@ -868,7 +920,7 @@ export class QpackDecoder {
868
920
  * Takes `cost` from what the section has room for and answers true, or
869
921
  * answers false and takes nothing when the section would pass its limit.
870
922
  */
871
- take(cost: i64): boolean {
923
+ private take(cost: i64): boolean {
872
924
  if (cost > this.room) {
873
925
  return false
874
926
  }
@@ -877,7 +929,7 @@ export class QpackDecoder {
877
929
  }
878
930
 
879
931
  /** The answer for a string read: `QPACK_OK`, past the limit, or the refusal `status` names. */
880
- stringResult(status: i32): i64 {
932
+ private stringResult(status: i32): i64 {
881
933
  if (status === QPACK_STRING_PAST_LIMIT) {
882
934
  return QPACK_SECTION_TOO_LARGE
883
935
  }
@@ -885,18 +937,18 @@ export class QpackDecoder {
885
937
  }
886
938
 
887
939
  /** Records `reason` and answers QPACK_DECOMPRESSION_FAILED. */
888
- refuse(reason: i32): i64 {
940
+ private refuse(reason: i32): i64 {
889
941
  this.reason = reason
890
942
  return QPACK_DECOMPRESSION_FAILED
891
943
  }
892
944
 
893
945
  /** The refusal for a negative `qpackReadInteger` answer. */
894
- refuseInteger(status: i64): i64 {
946
+ private refuseInteger(status: i64): i64 {
895
947
  return this.refuse(qpackIntegerReason(status))
896
948
  }
897
949
 
898
950
  /** Records a field whose name and value end where `bytes` ends now. */
899
- addField(nameStart: i32, valueStart: i32, neverIndexed: boolean): void {
951
+ private addField(nameStart: i32, valueStart: i32, neverIndexed: boolean): void {
900
952
  this.nameStart.push(nameStart)
901
953
  this.nameLength.push(valueStart - nameStart)
902
954
  this.valueStart.push(valueStart)
@@ -909,7 +961,7 @@ export class QpackDecoder {
909
961
  * The body of `decode`: the prefix, then one pass over the field lines,
910
962
  * each told apart by its first byte's pattern (§4.5.2 to §4.5.6).
911
963
  */
912
- decodeLines(src: u8[]): i64 {
964
+ private decodeLines(src: u8[]): i64 {
913
965
  const insertCount: i64 = qpackReadInteger(this, src, 8)
914
966
  if (insertCount < 0) {
915
967
  return this.refuseInteger(insertCount)
@@ -1015,18 +1067,33 @@ export class QpackDecoder {
1015
1067
  * anything on its own encoder stream once the stream type is written.
1016
1068
  */
1017
1069
  export class QpackEncoder {
1018
- huffman: HpackHuffman
1019
- /** The connection error the decoder stream met, which every later call answers. */
1070
+ private huffman: HpackHuffman
1071
+ /**
1072
+ * The connection error the decoder stream met, which every later call answers; `QPACK_OK` until then.
1073
+ * @internal Reset by `http3.ts`'s `restart`; callers read the answers `receiveDecoderStream` gives.
1074
+ */
1020
1075
  failed: i64
1021
- /** Which rule `failed` broke, a `QPACK_REASON_*`. */
1076
+ /**
1077
+ * Which rule the decoder stream broke, a `QPACK_REASON_*`; `QPACK_REASON_NONE` until
1078
+ * `receiveDecoderStream` answers QPACK_DECODER_STREAM_ERROR. Reset by `http3.ts`'s `restart`.
1079
+ */
1022
1080
  reason: i32
1023
- /** How many Stream Cancellations the decoder stream carried, saturating at 2^31 - 1, and the last one's stream. */
1081
+ /** How many Stream Cancellations the decoder stream carried, saturating at 2^31 - 1. */
1024
1082
  cancellations: i32
1083
+ /** The stream ID the last Stream Cancellation named; -1 until one arrives. */
1025
1084
  lastCancelled: i64
1026
- /** The stream ID of a Stream Cancellation still being read, and its next shift; -1 between instructions. */
1085
+ /**
1086
+ * The stream ID of a Stream Cancellation still being read.
1087
+ * @internal Reset by `http3.ts`'s `restart`; not for callers.
1088
+ */
1027
1089
  pendingId: i64
1090
+ /**
1091
+ * The shift of the next byte of a Stream Cancellation still being read; -1 between instructions.
1092
+ * @internal Reset by `http3.ts`'s `restart`; not for callers.
1093
+ */
1028
1094
  pendingShift: i32
1029
1095
 
1096
+ /** Makes an encoder whose decoder stream has carried nothing yet. */
1030
1097
  constructor() {
1031
1098
  this.huffman = new HpackHuffman()
1032
1099
  this.failed = QPACK_OK
@@ -1040,6 +1107,7 @@ export class QpackEncoder {
1040
1107
  /**
1041
1108
  * Appends the prefix every field section of this encoder begins with: a
1042
1109
  * Required Insert Count of 0 and a Base of 0, the bytes `00 00`.
1110
+ * @param out The buffer the section is being written to; the two bytes go after what it holds.
1043
1111
  */
1044
1112
  beginSection(out: u8[]): void {
1045
1113
  out.push(0)
@@ -1057,6 +1125,18 @@ export class QpackEncoder {
1057
1125
  * A field marked `neverIndexed` is always a literal with the N bit set, even
1058
1126
  * when the static table holds it, so that an intermediary re-encoding it
1059
1127
  * keeps it out of every dynamic table downstream (§4.5.4, §7.1.3).
1128
+ *
1129
+ * A window outside its buffer panics.
1130
+ * @param out The buffer the section is being written to; the field line goes after what it holds.
1131
+ * @param name The buffer holding the name; only read. It matches a static entry only byte for
1132
+ * byte, so it should already be lowercase (RFC 9114 §4.2).
1133
+ * @param nameOff Where the name starts in `name`.
1134
+ * @param nameLen How many bytes the name has.
1135
+ * @param value The buffer holding the value; only read.
1136
+ * @param valueOff Where the value starts in `value`.
1137
+ * @param valueLen How many bytes the value has.
1138
+ * @param neverIndexed Whether to send the field as a literal with the N bit set.
1139
+ * @param huffman Whether a string may be Huffman-coded; it is only when that is shorter.
1060
1140
  */
1061
1141
  encodeField(
1062
1142
  out: u8[],
@@ -1104,6 +1184,13 @@ export class QpackEncoder {
1104
1184
  * split it. A Section Acknowledgment and an Insert Count Increment are
1105
1185
  * refused on their first byte: this encoder sent no section that needs
1106
1186
  * acknowledging and inserted nothing to count.
1187
+ * @param buf The bytes the decoder stream carried; only read, nothing is kept.
1188
+ * @param off Where they start in `buf`.
1189
+ * @param len How many there are; zero is accepted, and a window outside `buf` panics.
1190
+ * @returns `QPACK_OK` when every byte belonged to a Stream Cancellation, finished or not;
1191
+ * QPACK_DECODER_STREAM_ERROR, which sticks, for a Section Acknowledgment, an Insert Count
1192
+ * Increment, a stream ID past 2^62 - 1, or one whose encoding runs past ten bytes; or that
1193
+ * same error again, without reading anything, once an earlier call met it.
1107
1194
  */
1108
1195
  receiveDecoderStream(buf: u8[], off: i32, len: i32): i64 {
1109
1196
  qpackCheckWindow("QpackEncoder.receiveDecoderStream", buf, off, len)
@@ -1147,7 +1234,7 @@ export class QpackEncoder {
1147
1234
  }
1148
1235
 
1149
1236
  /** Counts a Stream Cancellation for `streamId`. */
1150
- cancelled(streamId: i64): void {
1237
+ private cancelled(streamId: i64): void {
1151
1238
  if (this.cancellations < QPACK_COUNT_MAX) {
1152
1239
  this.cancellations += 1
1153
1240
  }
@@ -1155,7 +1242,7 @@ export class QpackEncoder {
1155
1242
  }
1156
1243
 
1157
1244
  /** Records `reason` and answers QPACK_DECODER_STREAM_ERROR, which sticks. */
1158
- refuse(reason: i32): i64 {
1245
+ private refuse(reason: i32): i64 {
1159
1246
  this.reason = reason
1160
1247
  this.failed = QPACK_DECODER_STREAM_ERROR
1161
1248
  return this.failed