@amritk/nish-aarch64-linux 0.49.1 → 0.51.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 (107) hide show
  1. package/INSTALL.md +1 -1
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +237 -160
  4. package/package.json +1 -1
  5. package/runtime/gfx/nish-gfx.h +373 -61
  6. package/runtime/nish.d.ts +94 -11
  7. package/runtime/nish.h +5 -0
  8. package/runtime/runtime-host.c +21 -5
  9. package/std/README.md +14 -11
  10. package/std/base64.ts +74 -35
  11. package/std/bytes.ts +207 -97
  12. package/std/cli-args.ts +150 -107
  13. package/std/compress/checksum.ts +68 -10
  14. package/std/compress/deflate.ts +213 -115
  15. package/std/compress/gzip.ts +124 -49
  16. package/std/compress/zlib.ts +57 -15
  17. package/std/crypto/aes.ts +51 -9
  18. package/std/crypto/base64url.ts +93 -66
  19. package/std/crypto/bcrypt.ts +25 -21
  20. package/std/crypto/ed25519.ts +12 -10
  21. package/std/crypto/hkdf.ts +405 -283
  22. package/std/crypto/hmac.ts +484 -149
  23. package/std/crypto/jwt.ts +51 -40
  24. package/std/crypto/p256.ts +14 -19
  25. package/std/crypto/pki-roots.ts +35 -12
  26. package/std/crypto/pki.ts +43 -55
  27. package/std/crypto/rsa-sign.ts +1635 -0
  28. package/std/crypto/rsa.ts +26 -4
  29. package/std/crypto/sha256.ts +212 -90
  30. package/std/crypto/sha512.ts +135 -49
  31. package/std/crypto/x25519.ts +12 -11
  32. package/std/crypto/x509.ts +164 -40
  33. package/std/gpu/sys.ts +490 -0
  34. package/std/json-equal.ts +33 -18
  35. package/std/json-format.ts +39 -8
  36. package/std/json-number.ts +15 -3
  37. package/std/json-tokenizer.ts +4 -3
  38. package/std/json-typed.ts +12 -6
  39. package/std/json-value.ts +236 -119
  40. package/std/json.ts +81 -46
  41. package/std/net/buffer-pool.ts +25 -14
  42. package/std/net/dns-config.ts +18 -5
  43. package/std/net/dns-message.ts +60 -27
  44. package/std/net/dns-resolver.ts +35 -16
  45. package/std/net/dns.ts +3 -2
  46. package/std/net/fetch.ts +224 -61
  47. package/std/net/handler.ts +189 -88
  48. package/std/net/hpack.ts +216 -103
  49. package/std/net/http-compress.ts +104 -42
  50. package/std/net/http-fields.ts +327 -31
  51. package/std/net/http-server.ts +326 -24
  52. package/std/net/http1-client-codec.ts +159 -55
  53. package/std/net/http1-codec.ts +427 -237
  54. package/std/net/http1-server.ts +220 -38
  55. package/std/net/http2-cleartext.ts +10 -4
  56. package/std/net/http2-frame.ts +2 -2
  57. package/std/net/http2-tls.ts +3 -3
  58. package/std/net/http2.ts +69 -15
  59. package/std/net/http3-frame.ts +11 -14
  60. package/std/net/http3-server.ts +139 -60
  61. package/std/net/http3.ts +140 -79
  62. package/std/net/multipart.ts +592 -0
  63. package/std/net/outbound.ts +24 -8
  64. package/std/net/postgres-async.ts +97 -64
  65. package/std/net/postgres-carrier.ts +28 -4
  66. package/std/net/postgres.ts +166 -65
  67. package/std/net/prefork.ts +86 -5
  68. package/std/net/protobuf.ts +6 -2
  69. package/std/net/qpack.ts +147 -40
  70. package/std/net/quic-conn-ack.ts +27 -7
  71. package/std/net/quic-conn-cid.ts +13 -7
  72. package/std/net/quic-datagram.ts +6 -5
  73. package/std/net/quic-frame.ts +67 -23
  74. package/std/net/quic-listener.ts +61 -39
  75. package/std/net/quic-packet.ts +91 -50
  76. package/std/net/quic-recovery.ts +11 -9
  77. package/std/net/quic-stream.ts +36 -27
  78. package/std/net/quic.ts +84 -54
  79. package/std/net/router.ts +136 -24
  80. package/std/net/serve.ts +59 -5
  81. package/std/net/static.ts +86 -36
  82. package/std/net/tls/client.ts +81 -34
  83. package/std/net/tls/codec.ts +3 -4
  84. package/std/net/tls/record.ts +80 -35
  85. package/std/net/tls/schedule.ts +339 -149
  86. package/std/net/tls/ticket.ts +6 -5
  87. package/std/net/tls-tcp.ts +69 -30
  88. package/std/net/tls.ts +194 -50
  89. package/std/net/upstream.ts +83 -38
  90. package/std/net/url.ts +170 -94
  91. package/std/net/websocket.ts +19 -20
  92. package/std/net/webtransport.ts +123 -33
  93. package/std/raster.ts +12 -2
  94. package/std/regex.ts +600 -388
  95. package/std/sqlite.ts +37 -19
  96. package/std/sync/ring.ts +232 -115
  97. package/std/sync/shm.ts +11 -4
  98. package/std/temporal.ts +232 -133
  99. package/std/term-io.ts +12 -6
  100. package/std/term-pixels.ts +176 -24
  101. package/std/term-rate.ts +8 -3
  102. package/std/term.ts +4 -4
  103. package/std/text.ts +38 -16
  104. package/std/yaml-json.ts +238 -106
  105. package/std/yaml-lines.ts +47 -27
  106. package/std/yaml-write.ts +58 -24
  107. package/std/yaml.ts +808 -395
@@ -89,7 +89,7 @@
89
89
  * Written from RFC 9110, RFC 9112 and RFC 9457, not ported from another
90
90
  * implementation.
91
91
  */
92
- import { copyBytes } from "nish/bytes"
92
+ import { tryCopyBytes } from "nish/bytes"
93
93
  import { JsonWriter } from "nish/json"
94
94
  import { httpFieldBytes } from "nish/net/http-fields"
95
95
  import { HTTP1_CHUNKED, HTTP1_NO_BODY, http1HexValue } from "nish/net/http1-codec"
@@ -133,8 +133,10 @@ import {
133
133
  HTTP_CODING_IDENTITY,
134
134
  HTTP_CODING_NONE,
135
135
  HTTP_DECODE_TOO_LARGE,
136
+ HTTP_CODING_BAD_WINDOW,
136
137
  HttpBodyDecoder,
137
- httpContentEncoding,
138
+ httpBodyDecoderFault,
139
+ httpTryContentEncoding,
138
140
  } from "nish/net/http-compress"
139
141
 
140
142
  /** `HandlerLimits.maxWaitMs`' most: a second, the interval `bodyTimeout` is checked at. */
@@ -153,6 +155,12 @@ export const HANDLER_NEXT: i32 = -22
153
155
  export const HANDLER_ANSWERED: i32 = -23
154
156
  /** The most fields `field` adds to one answer. */
155
157
  export const HANDLER_MAX_ADDED: i32 = 16
158
+ /**
159
+ * `wait`: the handler has no readiness loop, because its constructor was
160
+ * given a failed `pollCreate`, so no socket can ever be ready. Stop calling
161
+ * `wait`; a loop that keeps calling it only spins.
162
+ */
163
+ export const HANDLER_NO_LOOP: i32 = -25
156
164
 
157
165
  /** The most descriptors `watch` takes beside the handler's own. */
158
166
  export const HANDLER_MAX_WATCHED: i32 = 16
@@ -865,8 +873,8 @@ export class HttpHandler {
865
873
  private allowBytes: u8[][]
866
874
  private jsonType: u8[]
867
875
  private problemType: u8[]
868
- /** Decodes `gzip` and `deflate` bodies under `HandlerLimits.maxDecoded`. */
869
- private decoder: HttpBodyDecoder
876
+ /** Decodes `gzip` and `deflate` bodies under `HandlerLimits.maxDecoded`; null while that is 0. */
877
+ private decoder: HttpBodyDecoder | null
870
878
  private noFields: u8[][]
871
879
  /** Per length: the array `start` gathers an answer's fields and the fields `field` added into, kept for the answers after. */
872
880
  private namesBy: u8[][][]
@@ -924,14 +932,16 @@ export class HttpHandler {
924
932
  * @param router the routes, which the handler keeps and consults at each request head
925
933
  * @param loop a readiness loop from `pollCreate` for the handler to own, or
926
934
  * -1 (the default) to make one here; any other negative is a failed
927
- * `pollCreate` passed through. Panics on limits below their least and on
928
- * a failed loop, each a contract of this constructor: use
929
- * `openHttpHandler` for a `Result`.
935
+ * `pollCreate` passed through. A failed loop, passed in or made here,
936
+ * leaves the handler dead: it watches nothing and each `wait` answers
937
+ * `HANDLER_NO_LOOP` at once. Panics on limits below their least, a
938
+ * contract of this constructor: use `openHttpHandler` for a `Result`,
939
+ * which also answers a failed `pollCreate` as an `Err`.
930
940
  */
931
941
  constructor(limits: HandlerLimits, listener: i32, router: Router, loop: i32 = -1) {
932
942
  handlerCheckLimits(limits)
933
943
  this.maxDecoded = limits.maxDecoded
934
- this.decoder = new HttpBodyDecoder(limits.maxDecoded > 0 ? limits.maxDecoded : 1)
944
+ this.decoder = handlerDecoder(limits.maxDecoded)
935
945
  this.bodyTimeout = toI64(limits.bodyTimeout) * HANDLER_NANOS_PER_MS
936
946
  this.minEvents = limits.minEvents
937
947
  this.maxWaitMs = limits.maxWaitMs
@@ -966,10 +976,10 @@ export class HttpHandler {
966
976
  this.namesBy = []
967
977
  this.valuesBy = []
968
978
  this.loop = loop === -1 ? pollCreate() : loop
969
- if (this.loop < 0) {
970
- panic(`HttpHandler: pollCreate failed: ${this.loop}`)
979
+ // A failed loop leaves the handler dead, and `wait` says so.
980
+ if (this.loop >= 0) {
981
+ pollAdd(this.loop, listener, H1_WANT_READ, HANDLER_LISTENER)
971
982
  }
972
- pollAdd(this.loop, listener, H1_WANT_READ, HANDLER_LISTENER)
973
983
  }
974
984
 
975
985
  // ---- The loop -------------------------------------------------------------------
@@ -993,12 +1003,15 @@ export class HttpHandler {
993
1003
  * `watch`, with an id outside the range answered as an `Err` instead of a panic.
994
1004
  * @param fd the program's descriptor to watch for reading; it stays the program's to read and close
995
1005
  * @param id the name `fired` asks about it by, from 0 to `HANDLER_MAX_WATCHED - 1`
996
- * @returns `Ok(true)` once `fd` is watched, or `Err` naming the id outside the range
1006
+ * @returns `Ok(true)` once `fd` is watched, or `Err` naming the id outside the range or the handler's lack of a readiness loop
997
1007
  */
998
1008
  tryWatch(fd: i32, id: i32): Result<boolean, string> {
999
1009
  if (id < 0 || id >= HANDLER_MAX_WATCHED) {
1000
1010
  return Err(`HttpHandler.watch: id ${id} is outside 0 to ${HANDLER_MAX_WATCHED - 1}`)
1001
1011
  }
1012
+ if (this.loop < 0) {
1013
+ return Err("HttpHandler.watch: the handler has no readiness loop")
1014
+ }
1002
1015
  pollAdd(this.loop, fd, H1_WANT_READ, -2 - id)
1003
1016
  return Ok(true)
1004
1017
  }
@@ -1020,9 +1033,14 @@ export class HttpHandler {
1020
1033
  * connections past their timeouts, and answers how many whole requests
1021
1034
  * are waiting for `take`.
1022
1035
  * @param timeoutMs the longest to block, in milliseconds, or -1 to block until something is ready
1023
- * @returns how many whole requests are queued for `take` after this turn
1036
+ * @returns how many whole requests are queued for `take` after this turn,
1037
+ * or `HANDLER_NO_LOOP` at once, without blocking, from a handler whose
1038
+ * constructor was given a failed `pollCreate`
1024
1039
  */
1025
1040
  wait(timeoutMs: i32): i32 {
1041
+ if (this.loop < 0) {
1042
+ return HANDLER_NO_LOOP
1043
+ }
1026
1044
  this.flush()
1027
1045
  const fired: boolean[] = this.watchedFired
1028
1046
  for (let id: i32 = 0; id < HANDLER_MAX_WATCHED && id < toI32(fired.length); id++) {
@@ -1121,21 +1139,25 @@ export class HttpHandler {
1121
1139
  for (let s: i32 = 0; s < toI32(open.length) && s < toI32(current.length); s++) {
1122
1140
  const isOpen: boolean = open[s]
1123
1141
  const idle: boolean = current[s] === -1
1124
- if (
1125
- isOpen &&
1126
- this.server.holds(s) &&
1127
- idle &&
1128
- (this.server.resting(s) || !this.server.connection(s).wantsWrite())
1129
- ) {
1142
+ if (isOpen && this.server.holds(s) && idle && (this.server.resting(s) || !this.owesOutput(s))) {
1130
1143
  this.closeSlot(s)
1131
1144
  } else if (isOpen && this.server.holds(s)) {
1132
1145
  // Its exchange is its last: the answer says `Connection: close` if
1133
1146
  // it is not yet started, and no pipelined request is read behind it.
1134
- this.server.connection(s).closeAfter = true
1147
+ const conn: Http1Connection | null = this.server.tryConnection(s)
1148
+ if (conn !== null) {
1149
+ conn.closeAfter = true
1150
+ }
1135
1151
  }
1136
1152
  }
1137
1153
  }
1138
1154
 
1155
+ /** Whether `slot`'s connection has output waiting for its socket; false when the server gives it none. */
1156
+ private owesOutput(slot: i32): boolean {
1157
+ const conn: Http1Connection | null = this.server.tryConnection(slot)
1158
+ return conn !== null && conn.wantsWrite()
1159
+ }
1160
+
1139
1161
  /** Takes every waiting connection. */
1140
1162
  private acceptAll(): void {
1141
1163
  if (this.draining) {
@@ -1179,8 +1201,10 @@ export class HttpHandler {
1179
1201
  continue
1180
1202
  }
1181
1203
  owing[slot] = false
1182
- if (open[slot] && this.server.holds(slot)) {
1183
- this.settle(slot, this.server.connection(slot))
1204
+ const conn: Http1Connection | null =
1205
+ open[slot] && this.server.holds(slot) ? this.server.tryConnection(slot) : null
1206
+ if (conn !== null) {
1207
+ this.settle(slot, conn)
1184
1208
  }
1185
1209
  }
1186
1210
  }
@@ -1200,7 +1224,12 @@ export class HttpHandler {
1200
1224
  * the send waits for `flush`, so the answers behind this one go out with it.
1201
1225
  */
1202
1226
  private drive(slot: i32): void {
1203
- const conn: Http1Connection = this.server.connection(slot)
1227
+ const conn: Http1Connection | null = this.server.tryConnection(slot)
1228
+ if (conn === null) {
1229
+ // The server could build no connection for it: nothing to drive.
1230
+ this.closeSlot(slot)
1231
+ return
1232
+ }
1204
1233
  let event: i32 = this.unended(slot) ? H1_NEED_MORE : this.server.next(slot)
1205
1234
  while (event !== H1_NEED_MORE && event !== H1_ERROR) {
1206
1235
  if (event === H1_REQUEST) {
@@ -1298,9 +1327,9 @@ export class HttpHandler {
1298
1327
  /** Sends what `slot` holds and re-arms it, or closes it once it is over; enqueues a parked request once there is room. */
1299
1328
  private settle(slot: i32, conn: Http1Connection): void {
1300
1329
  if (conn.wantsWrite()) {
1301
- // Worked in `u64`, whose wrapping sum has the bits of the signed one: a
1302
- // count of sends is centuries short of 2^63.
1303
- this.sent = toI64(toU64(this.sent) + toU64(1))
1330
+ // Saturates at the last `i64` as an instant does, which a count of
1331
+ // sends is centuries short of; it starts at 0, so it is never negative.
1332
+ this.sent = handlerLater(this.sent, toI64(1))
1304
1333
  }
1305
1334
  // `flush` arms a held write for writing even once its send emptied the output.
1306
1335
  const wants: i32 = this.server.flush(slot)
@@ -1346,7 +1375,14 @@ export class HttpHandler {
1346
1375
  if (toI32(r.head.length) < length) {
1347
1376
  r.head = handlerBytes(handlerGrown(toI32(r.head.length), length))
1348
1377
  }
1349
- copyBytes(r.head, HANDLER_ZERO, p.head, HANDLER_ZERO, length)
1378
+ // `r.head` was grown to `length` above and the parser's head holds it,
1379
+ // so the copy fits; were it refused, the request goes back unread and
1380
+ // the connection closes, since its head cannot be answered.
1381
+ if (!tryCopyBytes(r.head, HANDLER_ZERO, p.head, HANDLER_ZERO, length)) {
1382
+ this.free.push(r.index)
1383
+ this.closeSlot(slot)
1384
+ return
1385
+ }
1350
1386
  // Only the first `4 * headerCount` spans are this head's, and only those
1351
1387
  // are ever read, so only those are copied.
1352
1388
  const spanCount: i32 = handlerSpanSlot(p.headerCount, 0)
@@ -1388,7 +1424,7 @@ export class HttpHandler {
1388
1424
  */
1389
1425
  private route(r: HttpRequest): void {
1390
1426
  const params: i32[] = r.params
1391
- let route: i32 = this.router.match(
1427
+ let route: i32 = this.router.tryMatch(
1392
1428
  r.head,
1393
1429
  r.methodStart,
1394
1430
  r.methodEnd,
@@ -1404,7 +1440,7 @@ export class HttpHandler {
1404
1440
  get < toI32(this.router.methodStart.length) &&
1405
1441
  get < toI32(this.router.methodEnd.length)
1406
1442
  ) {
1407
- const viaGet: i32 = this.router.match(
1443
+ const viaGet: i32 = this.router.tryMatch(
1408
1444
  this.router.methodBytes,
1409
1445
  this.router.methodStart[get],
1410
1446
  this.router.methodEnd[get],
@@ -1419,7 +1455,9 @@ export class HttpHandler {
1419
1455
  }
1420
1456
  }
1421
1457
  r.route = route
1422
- r.paramCount = route >= 0 ? this.router.paramCount(route) : 0
1458
+ // A route `tryMatch` answered is one the table holds, so the count is never -1.
1459
+ const count: i32 = route >= 0 ? this.router.tryParamCount(route) : 0
1460
+ r.paramCount = count > 0 ? count : 0
1423
1461
  }
1424
1462
 
1425
1463
  /** Body bytes: appended to the request's body, which the carrier's cap bounds. */
@@ -1436,10 +1474,18 @@ export class HttpHandler {
1436
1474
  }
1437
1475
  if (toI32(r.body.length) < need) {
1438
1476
  const grown: u8[] = handlerBytes(handlerGrown(toI32(r.body.length), need))
1439
- copyBytes(grown, HANDLER_ZERO, r.body, HANDLER_ZERO, r.bodyLength)
1477
+ // `grown` holds `need`, past `bodyLength`, so the copy fits; were it
1478
+ // refused, the body is lost, so the connection closes.
1479
+ if (!tryCopyBytes(grown, HANDLER_ZERO, r.body, HANDLER_ZERO, r.bodyLength)) {
1480
+ this.closeSlot(slot)
1481
+ return
1482
+ }
1440
1483
  r.body = grown
1441
1484
  }
1442
- copyBytes(r.body, r.bodyLength, conn.data, conn.dataStart, n)
1485
+ if (!tryCopyBytes(r.body, r.bodyLength, conn.data, conn.dataStart, n)) {
1486
+ this.closeSlot(slot)
1487
+ return
1488
+ }
1443
1489
  r.bodyLength = need
1444
1490
  }
1445
1491
 
@@ -1477,7 +1523,12 @@ export class HttpHandler {
1477
1523
  const valueStart: i32 = spans[at + 2]
1478
1524
  const valueEnd: i32 = spans[at + 3]
1479
1525
  if (handlerSpanIs(r.head, spans[at], spans[at + 1], "content-encoding")) {
1480
- const named: i32 = httpContentEncoding(r.head, valueStart, valueEnd)
1526
+ const named: i32 = httpTryContentEncoding(r.head, valueStart, valueEnd)
1527
+ if (named === HTTP_CODING_BAD_WINDOW) {
1528
+ // The spans are the parser's own, so this is a head the handler cannot read.
1529
+ r.refusal = toI32(400)
1530
+ return
1531
+ }
1481
1532
  // A second field stacks its codings on the first's (RFC 9110 §5.3).
1482
1533
  coding = coding === HTTP_CODING_IDENTITY ? named : HTTP_CODING_NONE
1483
1534
  }
@@ -1485,14 +1536,19 @@ export class HttpHandler {
1485
1536
  if (coding !== HTTP_CODING_GZIP && coding !== HTTP_CODING_DEFLATE) {
1486
1537
  return
1487
1538
  }
1488
- const got: i32 = this.decoder.decode(coding, r.body, HANDLER_ZERO, r.bodyLength)
1539
+ const decoder = this.decoder
1540
+ if (decoder === null) {
1541
+ return
1542
+ }
1543
+ // The window is the body's own, so `tryDecode`'s bad-call answer, a 400 below, cannot come.
1544
+ const got: i32 = decoder.tryDecode(coding, r.body, HANDLER_ZERO, r.bodyLength)
1489
1545
  if (got < 0) {
1490
1546
  r.refusal = got === HTTP_DECODE_TOO_LARGE ? toI32(413) : toI32(400)
1491
1547
  return
1492
1548
  }
1493
1549
  // The buffers change hands, so neither is copied or grown twice.
1494
- const decoded: u8[] = this.decoder.output
1495
- this.decoder.output = r.body
1550
+ const decoded: u8[] = decoder.output
1551
+ decoder.output = r.body
1496
1552
  r.body = decoded
1497
1553
  r.bodyLength = got
1498
1554
  r.decodedFrom = coding
@@ -1539,11 +1595,15 @@ export class HttpHandler {
1539
1595
 
1540
1596
  /** Hands the carrier as much of `r`'s pending bytes as it takes. */
1541
1597
  private pushPending(r: HttpRequest, conn: Http1Connection): void {
1542
- const left: i32 = handlerSub(r.pendingEnd, r.pendingStart)
1543
- if (left === 0) {
1598
+ const pending: u8[] = r.pending
1599
+ const start: i32 = r.pendingStart
1600
+ const end: i32 = r.pendingEnd
1601
+ // `keep` holds `start <= end <= pending.length`; the test says so where
1602
+ // the checker reads it, and an empty window has nothing to hand over.
1603
+ if (start < 0 || start >= end || end > toI32(pending.length)) {
1544
1604
  return
1545
1605
  }
1546
- const taken: i32 = conn.write(r.pending, r.pendingStart, left)
1606
+ const taken: i32 = conn.write(pending, start, end - start)
1547
1607
  if (taken > 0) {
1548
1608
  r.pendingStart = handlerAdd(r.pendingStart, taken)
1549
1609
  }
@@ -1788,8 +1848,11 @@ export class HttpHandler {
1788
1848
  if (left === 0) {
1789
1849
  return HANDLER_NEXT
1790
1850
  }
1791
- // The lowest bit set, `left & -left`, negated in wrapping unsigned arithmetic.
1792
- const low: i32 = toI32(toU32(left) & (toU32(0) - toU32(left)))
1851
+ // The lowest bit set, `left & -left`, kept in `u32`, where the negation
1852
+ // wraps by its type and has the bits of the two's complement one.
1853
+ const bits: u32 = toU32(left)
1854
+ const lowest: u32 = bits & (toU32(0) - bits)
1855
+ const low: i32 = toI32(lowest)
1793
1856
  r.stepsLeft = left ^ low
1794
1857
  r.stepsRan = r.stepsRan | low
1795
1858
  return toI32(31) - toI32(Math.clz32(toU32(low)))
@@ -1854,7 +1917,11 @@ export class HttpHandler {
1854
1917
  * @returns true once `r`'s connection is closed or reused, or its peer's stream has ended
1855
1918
  */
1856
1919
  hungUp(r: HttpRequest): boolean {
1857
- return !this.live(r) || this.server.connection(r.slot).ended()
1920
+ if (!this.live(r)) {
1921
+ return true
1922
+ }
1923
+ const conn: Http1Connection | null = this.server.tryConnection(r.slot)
1924
+ return conn === null || conn.ended()
1858
1925
  }
1859
1926
 
1860
1927
  /** Whether `r`'s connection is the one it was read on, still open. */
@@ -1941,10 +2008,10 @@ export class HttpHandler {
1941
2008
  return this.refused(`HttpHandler.start: body length ${bodyLength}`)
1942
2009
  }
1943
2010
  r.state = HANDLER_STREAMING
1944
- if (!this.live(r)) {
2011
+ const conn: Http1Connection | null = this.live(r) ? this.server.tryConnection(r.slot) : null
2012
+ if (conn === null) {
1945
2013
  return HANDLER_DROPPED
1946
2014
  }
1947
- const conn: Http1Connection = this.server.connection(r.slot)
1948
2015
  const framing: i32 = status === 204 || status === 304 ? HTTP1_NO_BODY : bodyLength
1949
2016
  let outNames: u8[][] = names
1950
2017
  let outValues: u8[][] = values
@@ -2029,10 +2096,10 @@ export class HttpHandler {
2029
2096
  this.abandon(r)
2030
2097
  return this.refused(`HttpHandler.write: the window ${off} + ${len} is outside its ${buf.length} bytes`)
2031
2098
  }
2032
- if (!this.live(r)) {
2099
+ const conn: Http1Connection | null = this.live(r) ? this.server.tryConnection(r.slot) : null
2100
+ if (conn === null) {
2033
2101
  return HANDLER_DROPPED
2034
2102
  }
2035
- const conn: Http1Connection = this.server.connection(r.slot)
2036
2103
  // The carrier checks the length of what it takes; what is pending it has
2037
2104
  // not seen, so the check counts it here.
2038
2105
  const owed: i32 = handlerSub(conn.responseLeft, handlerSub(r.pendingEnd, r.pendingStart))
@@ -2044,7 +2111,16 @@ export class HttpHandler {
2044
2111
  }
2045
2112
  let taken: i32 = 0
2046
2113
  if (r.pendingEnd === r.pendingStart) {
2047
- taken = conn.write(buf, off, len)
2114
+ // `handlerOutside` refused this window above; the test is here again,
2115
+ // next to the call, where the checker reads it.
2116
+ const end: i32 = handlerAdd(off, len)
2117
+ if (off < 0 || off > end || end > toI32(buf.length)) {
2118
+ this.abandon(r)
2119
+ return this.refused(
2120
+ `HttpHandler.write: the window ${off} + ${len} is outside its ${buf.length} bytes`
2121
+ )
2122
+ }
2123
+ taken = conn.write(buf, off, end - off)
2048
2124
  if (taken === H1_AGAIN) {
2049
2125
  taken = 0
2050
2126
  } else if (taken < 0) {
@@ -2056,14 +2132,21 @@ export class HttpHandler {
2056
2132
  )
2057
2133
  }
2058
2134
  }
2059
- if (taken < len) {
2060
- this.keep(r, buf, handlerAdd(off, taken), handlerSub(len, taken))
2135
+ if (taken < len && !this.keep(r, buf, handlerAdd(off, taken), handlerSub(len, taken))) {
2136
+ // `put` checked the window, so `keep` copies inside it; were it
2137
+ // refused, the body cannot follow the head as declared.
2138
+ this.abandon(r)
2139
+ return this.refused(`HttpHandler.write: the ${len} byte(s) could not be kept to send`)
2061
2140
  }
2062
2141
  return HANDLER_SENT
2063
2142
  }
2064
2143
 
2065
- /** Appends bytes to `r`'s pending buffer, growing it once to the largest response it has held. */
2066
- private keep(r: HttpRequest, buf: u8[], off: i32, len: i32): void {
2144
+ /**
2145
+ * Appends bytes to `r`'s pending buffer, growing it once to the largest
2146
+ * response it has held. Answers `false`, with the pending bytes as they
2147
+ * were, where a copy is refused.
2148
+ */
2149
+ private keep(r: HttpRequest, buf: u8[], off: i32, len: i32): boolean {
2067
2150
  const live: i32 = handlerSub(r.pendingEnd, r.pendingStart)
2068
2151
  const want: i32 = handlerAdd(live, len)
2069
2152
  if (handlerSub(toI32(r.pending.length), r.pendingEnd) < len) {
@@ -2072,14 +2155,19 @@ export class HttpHandler {
2072
2155
  toI32(r.pending.length) < want
2073
2156
  ? handlerBytes(handlerGrown(toI32(r.pending.length), want))
2074
2157
  : r.pending
2075
- copyBytes(target, HANDLER_ZERO, r.pending, r.pendingStart, live)
2158
+ if (!tryCopyBytes(target, HANDLER_ZERO, r.pending, r.pendingStart, live)) {
2159
+ return false
2160
+ }
2076
2161
  r.pending = target
2077
2162
  r.pendingStart = 0
2078
2163
  r.pendingEnd = live
2079
2164
  }
2080
2165
  }
2081
- copyBytes(r.pending, r.pendingEnd, buf, off, len)
2166
+ if (!tryCopyBytes(r.pending, r.pendingEnd, buf, off, len)) {
2167
+ return false
2168
+ }
2082
2169
  r.pendingEnd = handlerAdd(r.pendingEnd, len)
2170
+ return true
2083
2171
  }
2084
2172
 
2085
2173
  /**
@@ -2112,12 +2200,12 @@ export class HttpHandler {
2112
2200
  if (r.state !== HANDLER_STREAMING) {
2113
2201
  return this.refused(`HttpHandler.end: the response is not started (state ${r.state})`)
2114
2202
  }
2115
- if (!this.live(r)) {
2203
+ const slot: i32 = r.slot
2204
+ const conn: Http1Connection | null = this.live(r) ? this.server.tryConnection(slot) : null
2205
+ if (conn === null) {
2116
2206
  this.release(r)
2117
2207
  return HANDLER_DROPPED
2118
2208
  }
2119
- const slot: i32 = r.slot
2120
- const conn: Http1Connection = this.server.connection(slot)
2121
2209
  const short: i32 = handlerSub(conn.responseLeft, handlerSub(r.pendingEnd, r.pendingStart))
2122
2210
  if (conn.responseFraming >= 0 && short > 0 && !conn.responseEmpty) {
2123
2211
  this.abandon(r)
@@ -2519,7 +2607,7 @@ export class HttpHandler {
2519
2607
  for (let m: i32 = 0; m < methods && m < toI32(starts.length) && m < toI32(ends.length); m++) {
2520
2608
  const start: i32 = starts[m]
2521
2609
  const end: i32 = ends[m]
2522
- const route: i32 = router.match(
2610
+ const route: i32 = router.tryMatch(
2523
2611
  router.methodBytes,
2524
2612
  start,
2525
2613
  end,
@@ -2575,45 +2663,34 @@ const handlerAllowAppend = (value: u8[], bytes: u8[], start: i32, end: i32): voi
2575
2663
  }
2576
2664
 
2577
2665
  /**
2578
- * The first `n` elements' length as a `u16`, at most 65535, and 0 when `n` is
2579
- * not positive: an allocation of a `u16` length is not an `array-length`
2580
- * site, so the helpers below allocate once up to it and push only past it.
2666
+ * `n` copies of `fill`, none when `n` is not positive: one allocation, its
2667
+ * length proven non-negative by the guard.
2581
2668
  */
2582
- const handlerHead = (n: i32): u16 => {
2669
+ const handlerInts = (n: i32, fill: i32): i32[] => {
2583
2670
  if (n <= 0) {
2584
- return 0
2671
+ return []
2585
2672
  }
2586
- return toU16(n < 65535 ? n : toI32(65535))
2587
- }
2588
-
2589
- /** `n` copies of `fill`, none when `n` is not positive: one allocation up to 65535, pushed past it. */
2590
- const handlerInts = (n: i32, fill: i32): i32[] => {
2591
- const out: i32[] = new Array<i32>(handlerHead(n))
2673
+ const out: i32[] = new Array<i32>(n)
2592
2674
  if (fill !== 0) {
2593
2675
  out.fill(fill)
2594
2676
  }
2595
- for (let k: i32 = 65535; k < n; k++) {
2596
- out.push(fill)
2597
- }
2598
2677
  return out
2599
2678
  }
2600
2679
 
2601
2680
  /** `n` flags, each false, made as `handlerInts` makes its numbers. */
2602
2681
  const handlerFlags = (n: i32): boolean[] => {
2603
- const out: boolean[] = new Array<boolean>(handlerHead(n))
2604
- for (let k: i32 = 65535; k < n; k++) {
2605
- out.push(false)
2682
+ if (n <= 0) {
2683
+ return []
2606
2684
  }
2607
- return out
2685
+ return new Array<boolean>(n)
2608
2686
  }
2609
2687
 
2610
2688
  /** `n` zero bytes, made as `handlerInts` makes its numbers. */
2611
2689
  const handlerBytes = (n: i32): u8[] => {
2612
- const out: u8[] = new Array<u8>(handlerHead(n))
2613
- for (let k: i32 = 65535; k < n; k++) {
2614
- out.push(0)
2690
+ if (n <= 0) {
2691
+ return []
2615
2692
  }
2616
- return out
2693
+ return new Array<u8>(n)
2617
2694
  }
2618
2695
 
2619
2696
  /** The length of `Router.match`'s scratch for routes of up to `maxParams` parameters: two a parameter and two more. */
@@ -2621,17 +2698,28 @@ const handlerProbeLength = (maxParams: i32): i32 => toI32(toI64(maxParams) * toI
2621
2698
 
2622
2699
  /**
2623
2700
  * The nanoseconds from the `monotonicNanos` instant `from` to `to`, or 0 when
2624
- * `to` is not after it. Worked in `u64`, which has the bits of the signed
2625
- * difference once `to` is the later.
2701
+ * `to` is not after it. An instant is never negative, so the difference of
2702
+ * the later and a `from` of at least 0 fits an `i64`; a negative `from`
2703
+ * answers 0 too.
2626
2704
  */
2627
- const handlerSince = (from: i64, to: i64): i64 => (to > from ? toI64(toU64(to) - toU64(from)) : toI64(0))
2705
+ const handlerSince = (from: i64, to: i64): i64 => (to > from && from >= 0 ? to - from : toI64(0))
2628
2706
 
2629
2707
  /**
2630
- * The `monotonicNanos` instant `span` nanoseconds after `now`. Worked in
2631
- * `u64`, whose wrapping sum has the bits of the signed one: an instant is
2632
- * centuries short of 2^63 and the span a constant of a second or less.
2708
+ * The `monotonicNanos` instant `span` nanoseconds after `now`, or the last
2709
+ * `i64` when the sum would pass it, which no clock reading does: an instant
2710
+ * is centuries short of 2^63 and the span a constant of a second or less.
2711
+ * Both are at least 0, so they are summed in `u64`, where two values below
2712
+ * 2^63 cannot wrap, and the sum is turned back only once it is known to fit.
2713
+ * A negative `now` or `span`, which no caller passes, answers `now`.
2633
2714
  */
2634
- const handlerLater = (now: i64, span: i64): i64 => toI64(toU64(now) + toU64(span))
2715
+ const handlerLater = (now: i64, span: i64): i64 => {
2716
+ if (now < 0 || span < 0) {
2717
+ return now
2718
+ }
2719
+ const sum: u64 = toU64(now) + toU64(span)
2720
+ const top: u64 = (toU64(1) << toU64(63)) - toU64(1)
2721
+ return sum > top ? toI64(top) : toI64(sum)
2722
+ }
2635
2723
 
2636
2724
  /** Place `start + k` of a ring of `size` places, in `[0, size)`; -1 for an empty ring or a negative place. */
2637
2725
  const handlerRing = (start: i32, k: i32, size: i32): i32 => {
@@ -2646,8 +2734,9 @@ const handlerRing = (start: i32, k: i32, size: i32): i32 => {
2646
2734
 
2647
2735
  /**
2648
2736
  * A handler as `new HttpHandler(limits, listener, router)` makes one, with
2649
- * what that constructor panics on answered as an `Err`: limits below their
2650
- * least, or a readiness loop `pollCreate` could not make.
2737
+ * limits below their least, which that constructor panics on, answered as an
2738
+ * `Err`, and a readiness loop `pollCreate` could not make, which would leave
2739
+ * that constructor's handler dead, answered as an `Err` too.
2651
2740
  * @param limits the caps and timeouts, as the constructor reads them
2652
2741
  * @param listener the listening socket's descriptor, still the program's to close
2653
2742
  * @param router the routes, which the handler keeps and consults at each request head
@@ -2669,6 +2758,18 @@ export const openHttpHandler = (
2669
2758
  return Ok(new HttpHandler(limits, listener, router, loop))
2670
2759
  }
2671
2760
 
2761
+ /**
2762
+ * The body decoder for a `maxDecoded` of `limit`, or null for 0, which
2763
+ * decodes nothing (`handlerCheckLimits` has refused a negative one).
2764
+ */
2765
+ const handlerDecoder = (limit: i32): HttpBodyDecoder | null => {
2766
+ const fault: string = httpBodyDecoderFault(limit)
2767
+ if (fault.length > 0) {
2768
+ return null
2769
+ }
2770
+ return new HttpBodyDecoder(limit)
2771
+ }
2772
+
2672
2773
  /** Panics unless `limits` can make a handler: a limit below its least is the program's mistake. */
2673
2774
  const handlerCheckLimits = (limits: HandlerLimits): void => {
2674
2775
  const fault: string = handlerLimitsFault(limits)