@amritk/nish-aarch64-linux 0.38.0 → 0.39.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 (58) hide show
  1. package/bin/nish +0 -0
  2. package/docs/AI.md +348 -37
  3. package/package.json +1 -1
  4. package/runtime/core/host.d.ts +11 -0
  5. package/runtime/core/host.mjs +35 -0
  6. package/runtime/nish.d.ts +301 -18
  7. package/runtime/nish.h +74 -10
  8. package/runtime/nish.mjs +69 -6
  9. package/runtime/node.mjs +117 -0
  10. package/runtime/runtime-aes.c +12 -4
  11. package/runtime/runtime-async.c +43 -10
  12. package/runtime/runtime-checksum.c +207 -15
  13. package/runtime/runtime-net.c +32 -3
  14. package/runtime/runtime-os.c +174 -27
  15. package/runtime/runtime-region.c +94 -10
  16. package/runtime/runtime-sandbox.c +377 -17
  17. package/runtime/runtime-sha256.c +11 -4
  18. package/runtime/runtime-wasm.c +15 -0
  19. package/runtime/runtime.c +5 -0
  20. package/scripts/build.sh +27 -8
  21. package/std/README.md +5 -4
  22. package/std/compress/checksum.ts +13 -0
  23. package/std/compress/deflate.ts +0 -2
  24. package/std/crypto/p256.ts +247 -74
  25. package/std/crypto/p384.ts +3 -1
  26. package/std/crypto/pki.ts +35 -9
  27. package/std/crypto/x25519.ts +153 -64
  28. package/std/crypto/x509.ts +113 -10
  29. package/std/json-tokenizer.ts +20 -0
  30. package/std/json-typed.ts +217 -8
  31. package/std/net/handler.ts +130 -16
  32. package/std/net/http1-codec.ts +499 -150
  33. package/std/net/http1-server.ts +495 -22
  34. package/std/net/http2-cleartext.ts +526 -0
  35. package/std/net/http2-tls.ts +48 -0
  36. package/std/net/http2.ts +84 -0
  37. package/std/net/postgres-async.ts +1304 -86
  38. package/std/net/postgres-carrier.ts +343 -10
  39. package/std/net/postgres.ts +122 -7
  40. package/std/net/prefork.ts +195 -11
  41. package/std/net/quic-datagram.ts +30 -0
  42. package/std/net/quic-listener.ts +38 -12
  43. package/std/net/quic-packet.ts +19 -6
  44. package/std/net/quic-recovery.ts +134 -19
  45. package/std/net/quic-stream.ts +174 -36
  46. package/std/net/quic.ts +418 -18
  47. package/std/net/router.ts +39 -17
  48. package/std/net/serve.ts +162 -58
  49. package/std/net/tls/codec.ts +110 -32
  50. package/std/net/tls/record-server.ts +37 -7
  51. package/std/net/tls-tcp.ts +56 -1
  52. package/std/net/tls.ts +196 -50
  53. package/std/net/websocket.ts +55 -18
  54. package/std/net/webtransport.ts +179 -1
  55. package/std/regex.ts +69 -47
  56. package/std/region.ts +62 -11
  57. package/std/secret.ts +26 -59
  58. package/std/text.ts +233 -0
package/std/net/serve.ts CHANGED
@@ -37,7 +37,7 @@
37
37
  * **The task reads its own copy.** The task copies the request's head and
38
38
  * body out of the server's slot into its own memory before `handle` runs,
39
39
  * and when `handle` answers it writes the status, the content type and the
40
- * body back into the slot's fixed buffers, element by element, and wakes the
40
+ * body back into the slot's fixed buffers, in bulk, and wakes the
41
41
  * loop. The loop then sends the answer and frees the slot. Nothing the task
42
42
  * allocated outlives it, and the loop's arena does not grow per request once
43
43
  * its slots are warm: `tests/link/net_serve_pipelined_flat` pins
@@ -50,6 +50,14 @@
50
50
  * order they came: a later request's task starts only after the earlier one
51
51
  * is answered, so it can never finish first.
52
52
  *
53
+ * **A pipelined burst goes out in one write.** The loop answers what the
54
+ * tasks wrote and takes what the handler queued until neither moves, so a
55
+ * handler that answers without waiting serves a whole burst in one turn.
56
+ * The handler holds each answer's send while the next request on its
57
+ * connection is queued and the output has room, and the loop flushes once
58
+ * before it parks: one write per connection per turn, and no wake byte for a
59
+ * task that answers while the loop runs.
60
+ *
53
61
  * **Every cap answers.** `ServeLimits` bounds the connections, the requests
54
62
  * in flight (503 past it), a request's header section (431) and body (413),
55
63
  * and the response a task may write (500 past it). `docs/security/serve.md`
@@ -61,6 +69,7 @@
61
69
  * Private helpers share the importing program's flat symbol namespace
62
70
  * (`docs/wp26-stdlib.md` §3e), which is why each one carries the module's name.
63
71
  */
72
+ import { copyBytes } from "nish/bytes"
64
73
  import { HandlerLimits, HttpHandler, HttpRequest } from "nish/net/handler"
65
74
  import { ROUTER_BAD_ESCAPE, Router } from "nish/net/router"
66
75
  import { cancel, cancelAfter, readable, sleep, spawn, taskId } from "nish:async"
@@ -146,6 +155,8 @@ export class ServeStats {
146
155
  usedWarm: i64 = 0
147
156
  /** `Arena.used()` in the server's loop once `maxRequests` were answered, before draining. */
148
157
  usedEnd: i64 = 0
158
+ /** The writes the server began to send answers, counted at the end of each loop turn: a pipelined burst that fits the output is one. */
159
+ sends: i64 = 0
149
160
  }
150
161
 
151
162
  /**
@@ -339,10 +350,14 @@ class ServeState {
339
350
  tasks: i64[]
340
351
  /** Whether a slot's task was cancelled, so it is cancelled once. */
341
352
  cancelled: boolean[]
342
- /** `ServeLimits.deadline`, which each task arms on itself as it starts. */
343
- deadline: i32 = 0
344
- /** Set when the server begins to stop: each task still running, and each that starts later, is cancelled. */
345
- stopping: boolean = false
353
+ /** The free slots, `free[0 .. freeCount)`, the last freed on top. */
354
+ free: i32[]
355
+ /** The slots whose task has answered since the loop last looked, `finished[0 .. finishedCount)`, in the order they answered. */
356
+ finished: i32[]
357
+ /** The slots whose task the loop has started and not yet seen answer, `running[0 .. runningCount)`: the ones a hang-up can cancel. */
358
+ running: i32[]
359
+ /** Whether a slot is in `running`, so a slot answered and reused within a turn is listed once. */
360
+ listed: boolean[]
346
361
  /** The status each task answered, or `SERVE_REFUSE`. */
347
362
  status: i32[]
348
363
  /** The bytes each task wrote into `bodies`, or -1 when its body broke `responseBytes`. */
@@ -353,6 +368,12 @@ class ServeState {
353
368
  types: u8[][]
354
369
  /** The bytes each task wrote into `types`, or -1 when the type was too long. */
355
370
  typeLengths: i32[]
371
+ /** Per slot: the index in `typeBytes` of the type its task answered, or -1 when the task copied it into `types`. */
372
+ typeIndex: i32[]
373
+ /** Each content type the server has sent, as the bytes the loop writes into a head. */
374
+ typeBytes: u8[][]
375
+ /** The same content types as text, in `typeBytes`'s order: what a task compares its answer's type with. */
376
+ typeTexts: string[]
356
377
  /** One byte, written to the wake socket. */
357
378
  one: u8[]
358
379
  /** The fields of an answer with no content type: `ServeLimits.server`'s, or none. */
@@ -361,10 +382,24 @@ class ServeState {
361
382
  /** The fields of an answer with a content type: `ServeLimits.server`'s, then `content-type`, whose value is set per answer. */
362
383
  typedNames: u8[][]
363
384
  typedValues: u8[][]
385
+ /** How many of `free`'s entries are free slots. */
386
+ freeCount: i32 = 0
387
+ /** How many of `finished`'s entries are answered slots the loop has not yet looked at. */
388
+ finishedCount: i32 = 0
389
+ /** How many of `running`'s entries are slots whose task is running. */
390
+ runningCount: i32 = 0
391
+ /** `ServeLimits.deadline`, which each task arms on itself as it starts. */
392
+ deadline: i32 = 0
364
393
  /** The wake socket's end the tasks write to. */
365
394
  wakeFd: i32 = 0
395
+ /** Set when the server begins to stop: each task still running, and each that starts later, is cancelled. */
396
+ stopping: boolean = false
366
397
  /** Whether a byte is waiting in the wake socket. */
367
398
  signalled: boolean = false
399
+ /** Whether the loop is running rather than parked: a task that answers now needs no wake byte, as the loop looks again before it parks. */
400
+ awake: boolean = false
401
+ /** Set by each task that answers, and cleared by the loop as it looks for answers. */
402
+ fresh: boolean = false
368
403
  /** Set when the loop has stopped: the ticker ends. */
369
404
  stopped: boolean = false
370
405
 
@@ -374,11 +409,18 @@ class ServeState {
374
409
  this.done = []
375
410
  this.tasks = []
376
411
  this.cancelled = []
412
+ this.free = []
413
+ this.finished = []
414
+ this.running = []
415
+ this.listed = []
377
416
  this.status = []
378
417
  this.lengths = []
379
418
  this.bodies = []
380
419
  this.types = []
381
420
  this.typeLengths = []
421
+ this.typeIndex = []
422
+ this.typeBytes = []
423
+ this.typeTexts = []
382
424
  this.one = [toU8(1)]
383
425
  this.plainNames = []
384
426
  this.plainValues = []
@@ -407,14 +449,13 @@ const serveFields = (st: ServeState, server: string): void => {
407
449
  st.typedValues.push(type)
408
450
  }
409
451
 
410
- /** The lowest free slot, making one when every slot is busy and `inFlight` allows; -1 when none. */
452
+ /** The last freed slot, making one when every slot is busy and `inFlight` allows; -1 when none. */
411
453
  const serveFreeSlot = (st: ServeState, limits: ServeLimits): i32 => {
412
- const n: i32 = toI32(st.busy.length)
413
- for (let k: i32 = 0; k < n; k++) {
414
- if (!st.busy[k]) {
415
- return k
416
- }
454
+ if (st.freeCount > 0) {
455
+ st.freeCount -= 1
456
+ return st.free[st.freeCount]
417
457
  }
458
+ const n: i32 = toI32(st.busy.length)
418
459
  if (n >= limits.inFlight) {
419
460
  return -1
420
461
  }
@@ -428,6 +469,11 @@ const serveFreeSlot = (st: ServeState, limits: ServeLimits): i32 => {
428
469
  st.bodies.push(new Array<u8>(limits.responseBytes))
429
470
  st.types.push(new Array<u8>(SERVE_TYPE_BYTES))
430
471
  st.typeLengths.push(toI32(0))
472
+ st.typeIndex.push(toI32(-1))
473
+ st.free.push(toI32(0))
474
+ st.finished.push(toI32(0))
475
+ st.running.push(toI32(0))
476
+ st.listed.push(false)
431
477
  return n
432
478
  }
433
479
 
@@ -453,7 +499,7 @@ const serveAnyBusy = (st: ServeState): boolean => {
453
499
  }
454
500
 
455
501
  /** Sends slot `k`'s answer, as its task wrote it. */
456
- const serveAnswer = (h: HttpHandler, st: ServeState, k: i32, typeCache: u8[][], stats: ServeStats): void => {
502
+ const serveAnswer = (h: HttpHandler, st: ServeState, k: i32, stats: ServeStats): void => {
457
503
  const r: HttpRequest = st.reqs[k]
458
504
  const status: i32 = st.status[k]
459
505
  if (status === SERVE_REFUSE) {
@@ -474,19 +520,20 @@ const serveAnswer = (h: HttpHandler, st: ServeState, k: i32, typeCache: u8[][],
474
520
  }
475
521
  return
476
522
  }
477
- const type: i32 = serveType(typeCache, st.types[k], st.typeLengths[k])
523
+ const type: i32 = st.typeIndex[k] >= 0 ? st.typeIndex[k] : serveType(st, st.types[k], st.typeLengths[k])
478
524
  if (type < 0) {
479
525
  stats.failed += 1
480
526
  h.problem(r, SERVE_FAILED, "Internal Server Error", "")
481
527
  return
482
528
  }
483
529
  const last: i32 = toI32(st.typedValues.length) - 1
484
- st.typedValues[last] = typeCache[type]
530
+ st.typedValues[last] = st.typeBytes[type]
485
531
  h.sendFields(r, status, st.typedNames, st.typedValues, st.bodies[k], toI32(0), length)
486
532
  }
487
533
 
488
- /** The index in `cache` of the bytes `type[0 .. len)`, adding them while there is room; -1 when full or when they hold a control byte. */
489
- const serveType = (cache: u8[][], type: u8[], len: i32): i32 => {
534
+ /** The index in `st.typeBytes` of the bytes `type[0 .. len)`, adding them while there is room; -1 when full or when they hold a control byte. */
535
+ const serveType = (st: ServeState, type: u8[], len: i32): i32 => {
536
+ const cache: u8[][] = st.typeBytes
490
537
  if (len < 0 || len > toI32(type.length)) {
491
538
  return -1
492
539
  }
@@ -517,6 +564,7 @@ const serveType = (cache: u8[][], type: u8[], len: i32): i32 => {
517
564
  copy[j] = type[j]
518
565
  }
519
566
  cache.push(copy)
567
+ st.typeTexts.push(String.fromBytes(copy, 0, len))
520
568
  return toI32(cache.length) - 1
521
569
  }
522
570
 
@@ -548,32 +596,27 @@ const serveTask = async (
548
596
  // one into the other.
549
597
  const src: HttpRequest = st.reqs[k]
550
598
  const r: HttpRequest = new HttpRequest(-1)
551
- let end: i32 = src.targetEnd
552
- for (let i: i32 = 0; i < 4 * src.headerCount; i++) {
553
- if (src.spans[i] > end) {
554
- end = src.spans[i]
555
- }
556
- }
557
- const head: u8[] = new Array<u8>(end)
558
- for (let i: i32 = 0; i < end; i++) {
559
- head[i] = src.head[i]
560
- }
599
+ const head: u8[] = new Array<u8>(src.headLength)
600
+ copyBytes(head, 0, src.head, 0, src.headLength)
561
601
  const spans: i32[] = new Array<i32>(4 * src.headerCount)
562
- for (let i: i32 = 0; i < 4 * src.headerCount; i++) {
602
+ for (let i: i32 = 0; i < toI32(spans.length) && i < toI32(src.spans.length); i++) {
563
603
  spans[i] = src.spans[i]
564
604
  }
565
- const params: i32[] = new Array<i32>(2 * src.paramCount)
566
- for (let i: i32 = 0; i < 2 * src.paramCount; i++) {
567
- params[i] = src.params[i]
605
+ // No parameters and no body keep the empty arrays the constructor made.
606
+ if (src.paramCount > 0) {
607
+ const params: i32[] = new Array<i32>(2 * src.paramCount)
608
+ for (let i: i32 = 0; i < toI32(params.length) && i < toI32(src.params.length); i++) {
609
+ params[i] = src.params[i]
610
+ }
611
+ r.params = params
568
612
  }
569
- const body: u8[] = new Array<u8>(src.bodyLength)
570
- for (let i: i32 = 0; i < src.bodyLength; i++) {
571
- body[i] = src.body[i]
613
+ if (src.bodyLength > 0) {
614
+ const body: u8[] = new Array<u8>(src.bodyLength)
615
+ copyBytes(body, 0, src.body, 0, src.bodyLength)
616
+ r.body = body
572
617
  }
573
618
  r.head = head
574
619
  r.spans = spans
575
- r.params = params
576
- r.body = body
577
620
  r.route = src.route
578
621
  r.paramCount = src.paramCount
579
622
  r.methodStart = src.methodStart
@@ -581,31 +624,55 @@ const serveTask = async (
581
624
  r.targetStart = src.targetStart
582
625
  r.targetEnd = src.targetEnd
583
626
  r.pathEnd = src.pathEnd
627
+ r.headLength = src.headLength
584
628
  r.headerCount = src.headerCount
585
629
  r.bodyLength = src.bodyLength
586
630
  const res: Response = await handle(new Request(r))
587
631
  st.status[k] = res.status
588
632
  const out: u8[] = st.bodies[k]
589
- if (res.length > toI32(out.length)) {
633
+ // A length the body cannot hold, negative or past either array, is the
634
+ // handler's mistake: the loop answers 500 for it, as for an over-long body.
635
+ if (res.length < 0 || res.length > toI32(out.length) || res.length > toI32(res.body.length)) {
590
636
  st.lengths[k] = -1
591
637
  } else {
592
- for (let i: i32 = 0; i < res.length; i++) {
593
- out[i] = res.body[i]
638
+ // The whole body is copied when the slot holds it, as one `set`: what lies
639
+ // past `length` is never sent. Only a body larger than the slot is copied
640
+ // byte by byte, up to `length`.
641
+ if (res.body.length <= out.length) {
642
+ out.set(res.body)
643
+ } else {
644
+ for (let i: i32 = 0; i < res.length && i < toI32(out.length) && i < toI32(res.body.length); i++) {
645
+ out[i] = res.body[i]
646
+ }
594
647
  }
595
648
  st.lengths[k] = res.length
596
649
  }
597
- const type: u8[] = st.types[k]
650
+ // A type the server has sent before is found by its text, with no copy;
651
+ // the loop copies a new one's bytes into `typeBytes` as it sends it.
598
652
  const n: i32 = toI32(res.type.length)
599
- if (n > toI32(type.length)) {
653
+ let known: i32 = -1
654
+ for (let i: i32 = 0; n > 0 && known < 0 && i < toI32(st.typeTexts.length); i++) {
655
+ if (st.typeTexts[i] === res.type) {
656
+ known = i
657
+ }
658
+ }
659
+ st.typeIndex[k] = known
660
+ const type: u8[] = st.types[k]
661
+ if (known >= 0) {
662
+ st.typeLengths[k] = n
663
+ } else if (n > toI32(type.length)) {
600
664
  st.typeLengths[k] = -1
601
665
  } else {
602
- for (let i: i32 = 0; i < n; i++) {
666
+ for (let i: i32 = 0; i < n && i < toI32(type.length); i++) {
603
667
  type[i] = toU8(res.type.charCodeAt(i))
604
668
  }
605
669
  st.typeLengths[k] = n
606
670
  }
607
671
  st.done[k] = true
608
- if (!st.signalled) {
672
+ st.finished[st.finishedCount] = k
673
+ st.finishedCount += 1
674
+ st.fresh = true
675
+ if (!st.awake && !st.signalled) {
609
676
  st.signalled = true
610
677
  netWrite(st.wakeFd, st.one, 0, 1)
611
678
  }
@@ -652,6 +719,7 @@ export const serveOn = async (
652
719
  hl.http.headTimeout = limits.headTimeout
653
720
  hl.http.idleTimeout = limits.idleTimeout
654
721
  const h: HttpHandler = new HttpHandler(hl, listener, router)
722
+ h.deferSends()
655
723
  const st: ServeState = new ServeState()
656
724
  st.deadline = limits.deadline
657
725
  serveFields(st, limits.server)
@@ -663,10 +731,10 @@ export const serveOn = async (
663
731
  st.wakeFd = fds[1]
664
732
  h.watch(fds[0], SERVE_WAKE)
665
733
  const drained: u8[] = new Array<u8>(64)
666
- const typeCache: u8[][] = []
667
734
  spawn(serveTick(st))
668
735
  let draining: boolean = false
669
736
  while (true) {
737
+ st.awake = true
670
738
  h.wait(0)
671
739
  if (h.fired(SERVE_WAKE)) {
672
740
  st.signalled = false
@@ -674,24 +742,29 @@ export const serveOn = async (
674
742
  // Each read takes up to 64 wake bytes; the loop empties the socket.
675
743
  }
676
744
  }
745
+ // Each pass answers what the tasks wrote and takes what the handler
746
+ // queued: a pipelined request is queued once the one before it is
747
+ // answered, so a burst on a connection is taken and answered in request
748
+ // order within this turn, and the handler sends the answers together.
677
749
  let moved: boolean = true
678
- while (moved) {
750
+ while (moved || st.fresh) {
679
751
  moved = false
680
- for (let k: i32 = 0; k < toI32(st.busy.length); k++) {
681
- if (st.busy[k] && st.done[k]) {
682
- serveAnswer(h, st, k, typeCache, stats)
683
- st.busy[k] = false
684
- st.done[k] = false
685
- st.cancelled[k] = false
686
- st.tasks[k] = 0
687
- stats.answered += 1
688
- moved = true
689
- } else if (st.busy[k] && (st.stopping || h.hungUp(st.reqs[k]))) {
690
- // Its client hung up, or the server is stopping: nobody waits for
691
- // this answer, so the task is told to stop waiting too.
692
- serveCancel(st, k)
693
- }
752
+ st.fresh = false
753
+ // A task appends its slot as it answers; none runs while the loop
754
+ // does, so the list is read whole and emptied.
755
+ for (let i: i32 = 0; i < st.finishedCount && i < toI32(st.finished.length); i++) {
756
+ const k: i32 = st.finished[i]
757
+ serveAnswer(h, st, k, stats)
758
+ st.busy[k] = false
759
+ st.done[k] = false
760
+ st.cancelled[k] = false
761
+ st.tasks[k] = 0
762
+ st.free[st.freeCount] = k
763
+ st.freeCount += 1
764
+ stats.answered += 1
765
+ moved = true
694
766
  }
767
+ st.finishedCount = 0
695
768
  while (h.ready()) {
696
769
  const r: HttpRequest = h.take()
697
770
  const k: i32 = serveFreeSlot(st, limits)
@@ -707,9 +780,35 @@ export const serveOn = async (
707
780
  st.done[k] = false
708
781
  st.cancelled[k] = false
709
782
  st.tasks[k] = 0
783
+ if (!st.listed[k]) {
784
+ st.listed[k] = true
785
+ st.running[st.runningCount] = k
786
+ st.runningCount += 1
787
+ }
710
788
  spawn(serveTask(handle, st, k))
711
789
  }
712
790
  }
791
+ // A task still running whose client hung up, or any once the server is
792
+ // stopping: nobody waits for its answer, so it is told to stop waiting
793
+ // too. The answered ones leave the list here.
794
+ let kept: i32 = 0
795
+ for (let i: i32 = 0; i < st.runningCount && i < toI32(st.running.length); i++) {
796
+ const k: i32 = st.running[i]
797
+ if (st.busy[k] && !st.done[k]) {
798
+ if (st.stopping || h.hungUp(st.reqs[k])) {
799
+ serveCancel(st, k)
800
+ }
801
+ st.running[kept] = k
802
+ kept += 1
803
+ } else {
804
+ st.listed[k] = false
805
+ }
806
+ }
807
+ st.runningCount = kept
808
+ // The turn's answers go out together, before the arena is read, so a
809
+ // close the send causes is counted in the turn that caused it.
810
+ h.flush()
811
+ stats.sends = h.sends()
713
812
  stats.slots = toI32(st.busy.length)
714
813
  if (stats.warmAfter > 0 && stats.usedWarm === 0 && stats.answered >= stats.warmAfter) {
715
814
  stats.usedWarm = Arena.used()
@@ -728,6 +827,11 @@ export const serveOn = async (
728
827
  if (draining && h.connections() === 0 && !serveAnyBusy(st)) {
729
828
  break
730
829
  }
830
+ if (h.ready()) {
831
+ // A send let a parked request in: take it before parking.
832
+ continue
833
+ }
834
+ st.awake = false
731
835
  await readable(h.loop)
732
836
  }
733
837
  st.stopped = true