@amritk/nish-aarch64-linux 0.38.0 → 0.40.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.
Files changed (86) hide show
  1. package/INSTALL.md +74 -129
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +455 -65
  4. package/package.json +1 -1
  5. package/runtime/core/host.d.ts +11 -0
  6. package/runtime/core/host.mjs +35 -0
  7. package/runtime/nish.d.ts +320 -33
  8. package/runtime/nish.h +84 -10
  9. package/runtime/nish.mjs +69 -6
  10. package/runtime/node.mjs +117 -0
  11. package/runtime/page-frame.d.ts +123 -0
  12. package/runtime/page-frame.mjs +736 -0
  13. package/runtime/page-game.d.ts +132 -0
  14. package/runtime/page-game.mjs +738 -0
  15. package/runtime/runtime-aes.c +97 -12
  16. package/runtime/runtime-async.c +43 -10
  17. package/runtime/runtime-bytes.c +88 -4
  18. package/runtime/runtime-checksum.c +207 -15
  19. package/runtime/runtime-net.c +64 -3
  20. package/runtime/runtime-os.c +174 -27
  21. package/runtime/runtime-region.c +94 -10
  22. package/runtime/runtime-sandbox.c +404 -17
  23. package/runtime/runtime-sha256.c +11 -4
  24. package/runtime/runtime-wasm.c +437 -0
  25. package/runtime/runtime.c +5 -0
  26. package/runtime/vite.mjs +1 -1
  27. package/scripts/build.sh +42 -8
  28. package/std/README.md +78 -91
  29. package/std/array.ts +36 -3
  30. package/std/collections.ts +51 -0
  31. package/std/compress/checksum.ts +13 -0
  32. package/std/compress/deflate.ts +0 -2
  33. package/std/crypto/hmac.ts +37 -10
  34. package/std/crypto/p256.ts +1629 -2732
  35. package/std/crypto/p384.ts +3 -1
  36. package/std/crypto/pki.ts +151 -13
  37. package/std/crypto/sha256.ts +34 -6
  38. package/std/crypto/x25519.ts +153 -64
  39. package/std/crypto/x509.ts +120 -10
  40. package/std/host/game.ts +273 -0
  41. package/std/input.ts +56 -0
  42. package/std/json-tokenizer.ts +344 -111
  43. package/std/json-typed.ts +256 -12
  44. package/std/json-value.ts +144 -3
  45. package/std/json.ts +63 -8
  46. package/std/net/fetch.ts +22 -4
  47. package/std/net/handler.ts +130 -16
  48. package/std/net/http-server.ts +3382 -0
  49. package/std/net/http1-client-codec.ts +6 -2
  50. package/std/net/http1-codec.ts +636 -162
  51. package/std/net/http1-server.ts +569 -42
  52. package/std/net/http2-cleartext.ts +526 -0
  53. package/std/net/http2-tls.ts +48 -0
  54. package/std/net/http2.ts +131 -14
  55. package/std/net/http3-server.ts +29 -3
  56. package/std/net/http3.ts +180 -7
  57. package/std/net/postgres-async.ts +1304 -86
  58. package/std/net/postgres-carrier.ts +343 -10
  59. package/std/net/postgres.ts +122 -7
  60. package/std/net/prefork.ts +195 -11
  61. package/std/net/quic-datagram.ts +30 -0
  62. package/std/net/quic-listener.ts +38 -12
  63. package/std/net/quic-packet.ts +19 -6
  64. package/std/net/quic-recovery.ts +373 -27
  65. package/std/net/quic-stream.ts +195 -37
  66. package/std/net/quic.ts +516 -22
  67. package/std/net/router.ts +407 -38
  68. package/std/net/serve.ts +1022 -110
  69. package/std/net/tls/codec.ts +110 -32
  70. package/std/net/tls/record-server.ts +37 -7
  71. package/std/net/tls/verify.ts +19 -1
  72. package/std/net/tls-tcp.ts +56 -1
  73. package/std/net/tls.ts +196 -50
  74. package/std/net/url.ts +83 -35
  75. package/std/net/websocket.ts +129 -19
  76. package/std/net/webtransport.ts +185 -1
  77. package/std/raster.ts +837 -0
  78. package/std/regex.ts +69 -47
  79. package/std/region.ts +62 -11
  80. package/std/release.ts +593 -0
  81. package/std/secret.ts +26 -59
  82. package/std/synth.ts +143 -0
  83. package/std/text.ts +233 -0
  84. package/std/threads.ts +298 -0
  85. package/std/yaml-json.ts +368 -28
  86. package/std/yaml.ts +122 -27
@@ -105,6 +105,22 @@
105
105
  * state through the QUIC connection's stream index, never by a scan, and
106
106
  * nothing a warm session does allocates. Counters a peer drives saturate.
107
107
  *
108
+ * **The clock.** Each session keeps the time of its last activity
109
+ * (`monotonicNanos`, read once a call of `next`): its request, its accept,
110
+ * a datagram or a stream event either way, a capsule. `expire(now)`, called
111
+ * from the program's loop as `Http1Server.expire` is, ends every session
112
+ * quiet for longer than `WebTransportConfig.idleTimeout`: one never answered
113
+ * is refused with 503, an accepted one is closed (CLOSE with code 0 and the
114
+ * FIN, or a reset both ways when the capsule does not fit), and one already
115
+ * closed whose client never finished its half is let go. Each session it
116
+ * ends that the program still held is reported by the next calls of `next`
117
+ * as WT_CLOSED, `closedByPeer` false, code 0. A session takes at most
118
+ * `maxDatagramsPerSecond` datagrams in each fixed one-second window, the
119
+ * first starting when the session is asked for and the next at the first
120
+ * datagram a second or more after it; the rest are dropped and counted in
121
+ * `datagramsDropped`. The window does not slide, so across a boundary up to
122
+ * twice the cap can arrive within a second.
123
+ *
108
124
  * **Error codes.** A stream's reset and STOP_SENDING carry an HTTP/3 code;
109
125
  * a WebTransport application's 32-bit code maps into the range from
110
126
  * 0x52e4a40fa8db (`wtCodeToHttp3`, `wtCodeFromHttp3`), as Chrome maps it.
@@ -155,6 +171,7 @@ import {
155
171
  import { QUIC_DATAGRAM_ERR_FULL, QUIC_DATAGRAM_NONE, QUIC_DATAGRAM_OK, QuicConnection } from "nish/net/quic"
156
172
  import { quicPacketCopy, quicVarintSize } from "nish/net/quic-packet"
157
173
  import { quicStreamIsUni } from "nish/net/quic-stream"
174
+ import { monotonicNanos } from "nish:process"
158
175
 
159
176
  /** A session asked for: `sessionId`, `fields`. */
160
177
  export const WT_SESSION: i32 = 16
@@ -198,6 +215,15 @@ export const WT_FIRST_APP_CODE: i64 = 0x52e4a40fa8db
198
215
  /** The most a configuration may set `maxStreams` and `maxPending` to. */
199
216
  const WT_MAX_CAP: i32 = 4096
200
217
 
218
+ /** The longest `idleTimeout`, a day in milliseconds, and the highest `maxDatagramsPerSecond`. */
219
+ const WT_MAX_IDLE: i32 = 86400000
220
+ const WT_MAX_RATE: i32 = 1048576
221
+ /** Nanoseconds in a millisecond, and in a second. */
222
+ const WT_MILLI: i64 = 1000000
223
+ const WT_SECOND: i64 = 1000000000
224
+ /** The status a session asked for and never answered gets when `expire` ends it. */
225
+ const WT_STATUS_UNANSWERED: i32 = 503
226
+
201
227
  /** Session states. */
202
228
  const WT_FREE: i32 = 0
203
229
  const WT_ASKED: i32 = 1
@@ -234,6 +260,20 @@ export class WebTransportConfig {
234
260
  maxStreams: i32 = 64
235
261
  /** Streams that may wait at once for a session not yet answered or not yet arrived (draft-02 §4.5). */
236
262
  maxPending: i32 = 16
263
+ /**
264
+ * Milliseconds a session may go without activity before `expire` ends it, 1 to 86,400,000: a
265
+ * session asked for counts from its request, an accepted one from its last datagram or stream
266
+ * event either way, and one this side closed from its close.
267
+ */
268
+ idleTimeout: i32 = 60000
269
+ /**
270
+ * Datagrams one session may deliver in a second, 1 to 1,048,576; the rest are dropped and counted
271
+ * in `datagramsDropped`. Dropping is the whole defence: no count of drops closes the session.
272
+ * Datagrams are unreliable by contract (RFC 9297), so a dropped one is a loss the application
273
+ * already handles; a drop costs O(1); a close would punish a browser that bursts; and a client
274
+ * past the cap gains nothing but the CPU its datagrams cost to drop.
275
+ */
276
+ maxDatagramsPerSecond: i32 = 1024
237
277
  }
238
278
 
239
279
  /**
@@ -463,8 +503,18 @@ export class WebTransport {
463
503
  datagramsIn: i32[]
464
504
  /** How many datagrams each session sent, saturating at 2^31 - 1; zeroed when a session is asked for. */
465
505
  datagramsOut: i32[]
506
+ /** When each session was last active, and when its datagram second began and how many it took (`monotonicNanos`). */
507
+ private lastActive: i64[]
508
+ private rateSince: i64[]
509
+ private rateCount: i32[]
466
510
  /** The free sessions, a stack. */
467
511
  private free: i32[]
512
+ /** Sessions `expire` ended that `next` has not yet reported, `expiredCount` of them, with their IDs. */
513
+ private expiredSessions: i32[]
514
+ private expiredIds: i64[]
515
+ /** The time `next` last read, which stamps activity; and no session can expire before `expireAfter`. */
516
+ private now: i64 = 0
517
+ private expireAfter: i64 = 0
468
518
  // Per QUIC stream slot.
469
519
  /** The session whose CONNECT stream the slot holds, or -1. */
470
520
  sessionAt: i32[]
@@ -495,6 +545,8 @@ export class WebTransport {
495
545
  * WT_CLOSED, `errorCode` itself.
496
546
  */
497
547
  appCode: i64 = -1
548
+ /** How many of `expiredSessions` `next` has still to report. */
549
+ private expiredCount: i32 = 0
498
550
  /** Where the capsules of a DATA window are being read: `h3.data[capAt .. capEnd)`, for session `capSession`. */
499
551
  private capAt: i32 = 0
500
552
  private capEnd: i32 = 0
@@ -527,7 +579,8 @@ export class WebTransport {
527
579
  /**
528
580
  * The WebTransport layer of `h3`, whose `Http3Config` must have
529
581
  * `webtransportSessions` above 0. A cap out of range panics.
530
- * @param config The caps: `maxStreams` 1 to 4096 and `maxPending` 0 to 4096; kept, not copied.
582
+ * @param config The caps: `maxStreams` 1 to 4096, `maxPending` 0 to 4096, `idleTimeout` 1 to
583
+ * 86,400,000 and `maxDatagramsPerSecond` 1 to 1,048,576; kept, not copied.
531
584
  * @param h3 The connection this layer reads and writes, kept for the layer's life; its restarts
532
585
  * restart this layer too.
533
586
  */
@@ -539,6 +592,8 @@ export class WebTransport {
539
592
  }
540
593
  webtransportCheckCap("maxStreams", config.maxStreams, 1, WT_MAX_CAP)
541
594
  webtransportCheckCap("maxPending", config.maxPending, 0, WT_MAX_CAP)
595
+ webtransportCheckCap("idleTimeout", config.idleTimeout, 1, WT_MAX_IDLE)
596
+ webtransportCheckCap("maxDatagramsPerSecond", config.maxDatagramsPerSecond, 1, WT_MAX_RATE)
542
597
  this.config = config
543
598
  this.h3 = h3
544
599
  this.quic = h3.quic
@@ -566,7 +621,13 @@ export class WebTransport {
566
621
  this.capFill = new Array<i32>(sessions)
567
622
  this.datagramsIn = new Array<i32>(sessions)
568
623
  this.datagramsOut = new Array<i32>(sessions)
624
+ this.lastActive = new Array<i64>(sessions)
625
+ this.rateSince = new Array<i64>(sessions)
626
+ this.rateCount = new Array<i32>(sessions)
569
627
  this.free = new Array<i32>(sessions)
628
+ this.expiredSessions = new Array<i32>(sessions)
629
+ this.expiredIds = new Array<i64>(sessions)
630
+ this.now = monotonicNanos()
570
631
  const n: i32 = toI32(h3.quic.streams.slots.length)
571
632
  const slots: i32 = n > 0 ? n : WT_ONE
572
633
  this.sessionAt = new Array<i32>(slots)
@@ -595,6 +656,8 @@ export class WebTransport {
595
656
  }
596
657
  this.waiting.reset()
597
658
  this.adoptCount = 0
659
+ this.expiredCount = 0
660
+ this.expireAfter = 0
598
661
  this.capAt = 0
599
662
  this.capEnd = 0
600
663
  this.capSession = -1
@@ -659,6 +722,16 @@ export class WebTransport {
659
722
  */
660
723
  next(): i32 {
661
724
  this.current()
725
+ const clock: i64 = monotonicNanos()
726
+ // The clock never runs back, so a session's deadline only moves later.
727
+ this.now = clock > this.now ? clock : this.now
728
+ if (this.expiredCount > 0) {
729
+ this.expiredCount = this.expiredCount - 1
730
+ const id: i64 = this.expiredIds[this.expiredCount]
731
+ this.sessionId = id
732
+ this.stream = id
733
+ return this.closedEvent(this.expiredSessions[this.expiredCount], WT_ZERO64, WT_ZERO, false)
734
+ }
662
735
  for (let guard: i32 = 0; guard < 1000000; guard++) {
663
736
  if (this.adoptCount > 0) {
664
737
  const event: i32 = this.adoptWaiting()
@@ -717,6 +790,17 @@ export class WebTransport {
717
790
  this.datagramsDropped = h3Count(this.datagramsDropped)
718
791
  return WT_ONE
719
792
  }
793
+ if (this.now - this.rateSince[s] >= WT_SECOND) {
794
+ this.rateSince[s] = this.now
795
+ this.rateCount[s] = 0
796
+ }
797
+ if (this.rateCount[s] >= this.config.maxDatagramsPerSecond) {
798
+ // Past the session's datagrams a second: dropped, as one QUIC could not queue would be.
799
+ this.datagramsDropped = h3Count(this.datagramsDropped)
800
+ return WT_ONE
801
+ }
802
+ this.rateCount[s] = this.rateCount[s] + 1
803
+ this.lastActive[s] = this.now
720
804
  const at: i32 = h3VarintLength(this.datagram, WT_ZERO)
721
805
  this.datagramsIn[s] = h3Count(this.datagramsIn[s])
722
806
  this.session = s
@@ -761,6 +845,7 @@ export class WebTransport {
761
845
  return event === H3_STOPPED ? H3_NEED_MORE : event
762
846
  }
763
847
  const t: i32 = this.streamSession[k]
848
+ this.touch(k)
764
849
  this.stream = id
765
850
  this.session = t
766
851
  this.sessionId = t >= 0 && t < toI32(this.sessionIds.length) ? this.sessionIds[t] : WT_NONE64
@@ -796,6 +881,7 @@ export class WebTransport {
796
881
  /** An event of session `s`'s CONNECT stream. */
797
882
  private connectEvent(s: i32, event: i32): i32 {
798
883
  const h3: Http3Connection = this.h3
884
+ this.lastActive[s] = this.now
799
885
  this.session = s
800
886
  this.sessionId = this.sessionIds[s]
801
887
  this.stream = this.sessionIds[s]
@@ -914,6 +1000,9 @@ export class WebTransport {
914
1000
  this.capFill[s] = 0
915
1001
  this.datagramsIn[s] = 0
916
1002
  this.datagramsOut[s] = 0
1003
+ this.lastActive[s] = this.now
1004
+ this.rateSince[s] = this.now
1005
+ this.rateCount[s] = 0
917
1006
  this.sessionAt[k] = s
918
1007
  this.session = s
919
1008
  this.sessionId = id
@@ -952,6 +1041,7 @@ export class WebTransport {
952
1041
  return result
953
1042
  }
954
1043
  this.states[s] = WT_OPEN
1044
+ this.lastActive[s] = this.now
955
1045
  // The streams that waited for it are taken by the next calls of `next`, each with its WT_STREAM.
956
1046
  if (this.waiting.first(id) >= 0 && this.adoptCount < toI32(this.adoptions.length)) {
957
1047
  this.adoptions[this.adoptCount] = s
@@ -1021,6 +1111,8 @@ export class WebTransport {
1021
1111
  this.streamCount[s] = 0
1022
1112
  this.dropPending(this.sessionIds[s])
1023
1113
  this.states[s] = WT_GONE
1114
+ // A closed session's clock restarts: the client has `idleTimeout` to finish its half.
1115
+ this.lastActive[s] = this.now
1024
1116
  }
1025
1117
 
1026
1118
  /** Refuses with WT_SESSION_GONE every stream waiting for session `id`. */
@@ -1099,6 +1191,94 @@ export class WebTransport {
1099
1191
  return this.h3.writeDataWhole(id, out, WT_ZERO, p, false)
1100
1192
  }
1101
1193
 
1194
+ /** Stamps the session of the stream in QUIC slot `k` as active now. */
1195
+ private touch(k: i32): void {
1196
+ const t: i32 = this.streamSession[k]
1197
+ if (t >= 0 && t < toI32(this.lastActive.length)) {
1198
+ this.lastActive[t] = this.now
1199
+ }
1200
+ }
1201
+
1202
+ // ---- The clock -------------------------------------------------------------------
1203
+
1204
+ /**
1205
+ * Ends every session quiet for longer than `idleTimeout` at `now` (see
1206
+ * the module comment): one never answered is refused with 503, an
1207
+ * accepted one closed with code 0, and one already closed whose client
1208
+ * never finished its half let go. Call it from the program's loop; it
1209
+ * returns at once before the soonest deadline it last saw. The next calls
1210
+ * of `next` report each session the program held as WT_CLOSED.
1211
+ * @param now The current time, from `monotonicNanos`.
1212
+ * @returns How many sessions it ended.
1213
+ */
1214
+ expire(now: i64): i32 {
1215
+ this.current()
1216
+ if (now < this.expireAfter) {
1217
+ return 0
1218
+ }
1219
+ this.now = now > this.now ? now : this.now
1220
+ const idle: i64 = toI64(this.config.idleTimeout) * WT_MILLI
1221
+ let soonest: i64 = now + idle
1222
+ let n: i32 = 0
1223
+ for (let s: i32 = 0; s < toI32(this.states.length); s++) {
1224
+ const state: i32 = this.states[s]
1225
+ if (state !== WT_FREE) {
1226
+ const deadline: i64 = this.lastActive[s] + idle
1227
+ if (now > deadline) {
1228
+ this.timeOut(s, state)
1229
+ n = n + 1
1230
+ } else if (deadline < soonest) {
1231
+ soonest = deadline
1232
+ }
1233
+ }
1234
+ }
1235
+ this.expireAfter = soonest
1236
+ return n
1237
+ }
1238
+
1239
+ /**
1240
+ * The soonest `expire` could end a session, at or after `now`: activity
1241
+ * only moves a session's deadline later.
1242
+ * @param now The current time, from `monotonicNanos`.
1243
+ * @returns The earliest `monotonicNanos` instant at which `expire` could end a session; `now` plus
1244
+ * `idleTimeout` when no session is held.
1245
+ */
1246
+ expiresNoSooner(now: i64): i64 {
1247
+ const idle: i64 = toI64(this.config.idleTimeout) * WT_MILLI
1248
+ let soonest: i64 = now + idle
1249
+ for (let s: i32 = 0; s < toI32(this.states.length); s++) {
1250
+ if (this.states[s] !== WT_FREE && this.lastActive[s] + idle < soonest) {
1251
+ soonest = this.lastActive[s] + idle
1252
+ }
1253
+ }
1254
+ return soonest > now ? soonest : now
1255
+ }
1256
+
1257
+ /** Ends session `s`, in `state`, for its idleness, and queues its WT_CLOSED unless the program let go of it already. */
1258
+ private timeOut(s: i32, state: i32): void {
1259
+ const id: i64 = this.sessionIds[s]
1260
+ if (state === WT_ASKED) {
1261
+ this.answer(id, WT_STATUS_UNANSWERED)
1262
+ this.release(s)
1263
+ } else if (state === WT_OPEN) {
1264
+ if (this.close(id, WT_ZERO64, this.reason, WT_ZERO, WT_ZERO) !== 0) {
1265
+ // No room for the capsule: reset the CONNECT stream both ways, after which no event ends the session.
1266
+ this.h3.reset(id, H3_NO_ERROR)
1267
+ this.release(s)
1268
+ }
1269
+ } else {
1270
+ // Closed here, or by the client's CLOSE, and the client never finished its half: stop reading it.
1271
+ this.h3.discardRequest(id, H3_NO_ERROR)
1272
+ this.release(s)
1273
+ return
1274
+ }
1275
+ if (this.expiredCount < toI32(this.expiredSessions.length)) {
1276
+ this.expiredSessions[this.expiredCount] = s
1277
+ this.expiredIds[this.expiredCount] = id
1278
+ this.expiredCount = this.expiredCount + 1
1279
+ }
1280
+ }
1281
+
1102
1282
  // ---- Capsules --------------------------------------------------------------------
1103
1283
 
1104
1284
  /**
@@ -1368,6 +1548,9 @@ export class WebTransport {
1368
1548
  return H3_CLOSED
1369
1549
  }
1370
1550
  const n: i32 = this.h3.writeStream(id, buf, off, len, fin)
1551
+ if (n >= 0) {
1552
+ this.touch(k)
1553
+ }
1371
1554
  if (fin && n === len) {
1372
1555
  this.settle(k, WT_SENT)
1373
1556
  }
@@ -1463,6 +1646,7 @@ export class WebTransport {
1463
1646
  quicPacketCopy(out, p, buf, off, len)
1464
1647
  const result: i32 = this.quic.sendDatagram(out, WT_ZERO, p + len)
1465
1648
  if (result === QUIC_DATAGRAM_OK) {
1649
+ this.lastActive[s] = this.now
1466
1650
  this.datagramsOut[s] = h3Count(this.datagramsOut[s])
1467
1651
  return 0
1468
1652
  }