@amritk/nish-aarch64-linux 0.35.0 → 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 (119) hide show
  1. package/INSTALL.md +6 -1
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +9 -2
  4. package/package.json +1 -1
  5. package/runtime/nish.d.ts +4612 -430
  6. package/runtime/vite.d.ts +30 -0
  7. package/runtime/vite.mjs +150 -0
  8. package/scripts/build.sh +108 -13
  9. package/std/base64.ts +34 -5
  10. package/std/bytes.ts +256 -40
  11. package/std/cli-args.ts +296 -70
  12. package/std/cli-flags.ts +184 -19
  13. package/std/cli.ts +475 -54
  14. package/std/collections.ts +121 -23
  15. package/std/compress/checksum.ts +44 -14
  16. package/std/compress/deflate.ts +224 -111
  17. package/std/compress/gzip.ts +217 -53
  18. package/std/compress/zlib.ts +63 -7
  19. package/std/crypto/aes.ts +80 -9
  20. package/std/crypto/base64url.ts +22 -0
  21. package/std/crypto/bcrypt-batch.ts +88 -21
  22. package/std/crypto/bcrypt.ts +60 -0
  23. package/std/crypto/chacha20poly1305.ts +53 -0
  24. package/std/crypto/ct.ts +32 -13
  25. package/std/crypto/ed25519.ts +47 -0
  26. package/std/crypto/hkdf.ts +198 -13
  27. package/std/crypto/hmac.ts +235 -31
  28. package/std/crypto/jwt.ts +192 -63
  29. package/std/crypto/p256.ts +159 -16
  30. package/std/crypto/p384.ts +28 -3
  31. package/std/crypto/pki-roots.ts +32 -2
  32. package/std/crypto/pki.ts +81 -15
  33. package/std/crypto/rsa.ts +33 -3
  34. package/std/crypto/sha1.ts +3 -0
  35. package/std/crypto/sha256.ts +24 -1
  36. package/std/crypto/sha512.ts +99 -14
  37. package/std/crypto/x25519.ts +152 -14
  38. package/std/crypto/x509-random.ts +10 -0
  39. package/std/crypto/x509.ts +67 -7
  40. package/std/json-equal.ts +19 -0
  41. package/std/json-format.ts +112 -13
  42. package/std/json-number.ts +64 -14
  43. package/std/json-pointer.ts +23 -1
  44. package/std/json-tokenizer.ts +100 -29
  45. package/std/json-typed.ts +713 -66
  46. package/std/json-value.ts +400 -124
  47. package/std/json.ts +138 -10
  48. package/std/map.ts +25 -7
  49. package/std/math.ts +36 -4
  50. package/std/net/buffer-pool.ts +42 -11
  51. package/std/net/dns-config.ts +109 -23
  52. package/std/net/dns-message.ts +62 -7
  53. package/std/net/dns-resolver.ts +247 -53
  54. package/std/net/dns.ts +203 -35
  55. package/std/net/fetch.ts +151 -61
  56. package/std/net/handler.ts +278 -80
  57. package/std/net/hpack.ts +262 -52
  58. package/std/net/http-compress.ts +125 -15
  59. package/std/net/http-fields.ts +85 -9
  60. package/std/net/http1-client-codec.ts +100 -19
  61. package/std/net/http1-codec.ts +235 -27
  62. package/std/net/http1-server.ts +343 -85
  63. package/std/net/http2-frame.ts +272 -35
  64. package/std/net/http2-tls.ts +135 -34
  65. package/std/net/http2.ts +247 -106
  66. package/std/net/http3-frame.ts +154 -17
  67. package/std/net/http3-server.ts +246 -96
  68. package/std/net/http3.ts +307 -115
  69. package/std/net/outbound.ts +37 -3
  70. package/std/net/postgres-carrier.ts +144 -8
  71. package/std/net/postgres.ts +723 -103
  72. package/std/net/prefork.ts +164 -31
  73. package/std/net/qpack.ts +105 -18
  74. package/std/net/quic-conn-ack.ts +59 -12
  75. package/std/net/quic-conn-cid.ts +124 -27
  76. package/std/net/quic-conn-params.ts +70 -10
  77. package/std/net/quic-datagram.ts +43 -3
  78. package/std/net/quic-frame.ts +286 -12
  79. package/std/net/quic-listener.ts +101 -32
  80. package/std/net/quic-packet.ts +302 -21
  81. package/std/net/quic-recovery.ts +228 -44
  82. package/std/net/quic-stream.ts +353 -91
  83. package/std/net/quic.ts +489 -171
  84. package/std/net/router.ts +87 -40
  85. package/std/net/tls/client.ts +166 -51
  86. package/std/net/tls/codec.ts +413 -56
  87. package/std/net/tls/record-client.ts +110 -55
  88. package/std/net/tls/record-server.ts +159 -59
  89. package/std/net/tls/record.ts +109 -12
  90. package/std/net/tls/schedule.ts +235 -26
  91. package/std/net/tls/ticket.ts +159 -40
  92. package/std/net/tls/verify.ts +49 -3
  93. package/std/net/tls-tcp.ts +250 -45
  94. package/std/net/tls.ts +221 -59
  95. package/std/net/upstream.ts +81 -45
  96. package/std/net/url.ts +97 -13
  97. package/std/net/websocket.ts +122 -12
  98. package/std/net/webtransport.ts +220 -89
  99. package/std/pair.ts +10 -2
  100. package/std/random.ts +34 -9
  101. package/std/regex.ts +352 -198
  102. package/std/region.ts +70 -15
  103. package/std/secret.ts +52 -11
  104. package/std/sort.ts +17 -2
  105. package/std/sqlite.ts +256 -35
  106. package/std/sync/ring.ts +205 -35
  107. package/std/sync/shm.ts +194 -16
  108. package/std/term-io.ts +137 -64
  109. package/std/term-pixels.ts +144 -19
  110. package/std/term-probe.ts +66 -25
  111. package/std/term-rate.ts +128 -57
  112. package/std/term.ts +332 -121
  113. package/std/testing.ts +130 -32
  114. package/std/text.ts +73 -7
  115. package/std/threads.ts +229 -41
  116. package/std/yaml-json.ts +94 -82
  117. package/std/yaml-lines.ts +47 -15
  118. package/std/yaml-write.ts +24 -16
  119. package/std/yaml.ts +764 -384
@@ -162,7 +162,10 @@ export class HandlerLimits {
162
162
  http: Http1Config
163
163
  /** The most connections at once. */
164
164
  slots: i32 = 256
165
- /** The JSON scratch buffer's size: the largest body `json` and `problem` can write. */
165
+ /**
166
+ * Bytes in the JSON scratch buffer: the largest body `json` and `problem`
167
+ * can write. A value past it makes `sendJson` answer 500. At least 256.
168
+ */
166
169
  jsonBytes: i32 = 65536
167
170
  /** The most readiness events one `wait` takes. */
168
171
  maxEvents: i32 = 256
@@ -176,6 +179,11 @@ export class HandlerLimits {
176
179
  */
177
180
  bodyTimeout: i32 = 30000
178
181
 
182
+ /**
183
+ * The defaults: 256 slots, a 64 KiB JSON buffer, 256 events a turn, a 30
184
+ * second body timeout, and the carrier's own defaults with the body cap
185
+ * lowered to one MiB. Change a field after construction to tune one.
186
+ */
179
187
  constructor() {
180
188
  this.http = new Http1Config()
181
189
  this.http.maxBody = HANDLER_ONE_MIB
@@ -190,26 +198,48 @@ export class HandlerLimits {
190
198
  export class HttpRequest {
191
199
  /** The method, the target and the header section, as the parser kept them (field names lowercased). */
192
200
  head: u8[]
193
- /** Four per field, into `head`: name start and end, value start and end. */
201
+ /**
202
+ * Four per field, into `head`: name start and end, value start and end.
203
+ * @internal Filled by `HttpHandler` in this module when it copies the head, and read by `header`, `valueStart` and `valueEnd`.
204
+ */
194
205
  spans: i32[]
195
- /** The route parameters as `[start, end)` pairs into `head`, in the pattern's order. */
206
+ /**
207
+ * The route parameters as `[start, end)` pairs into `head`, in the pattern's order.
208
+ * @internal Filled by `HttpHandler` in this module from `Router.match`; programs read them through `paramStart`, `param` and the rest.
209
+ */
196
210
  params: i32[]
197
211
  /** The body, `body[0 .. bodyLength)`. */
198
212
  body: u8[]
199
- /** What the carrier had no room for yet: `pending[pendingStart .. pendingEnd)`. */
213
+ /**
214
+ * What the carrier had no room for yet: `pending[pendingStart .. pendingEnd)`.
215
+ * @internal Written and flushed by `HttpHandler` in this module while it streams the response.
216
+ */
200
217
  pending: u8[]
201
- /** When the head was read, as the handler's turn saw it (`monotonicNanos`); `bodyTimeout` runs from here. */
218
+ /**
219
+ * When the head was read, as the handler's turn saw it (`monotonicNanos`); `bodyTimeout` runs from here.
220
+ * @internal Stamped and checked by `HttpHandler` in this module to close a connection whose body trickles past `bodyTimeout`.
221
+ */
202
222
  since: i64 = 0
203
223
  /** The index `Router.add` answered for the route, or a negative `ROUTER_*` code. */
204
224
  route: i32 = 0
205
- /** How many parameter pairs `params` holds. */
225
+ /**
226
+ * How many parameter pairs `params` holds.
227
+ * @internal Set by `HttpHandler` in this module after routing; the param accessors check `k` against it.
228
+ */
206
229
  paramCount: i32 = 0
207
230
  methodStart: i32 = 0
208
231
  methodEnd: i32 = 0
209
232
  targetStart: i32 = 0
210
233
  targetEnd: i32 = 0
211
- /** Where the path ends: the target's first `?`, or its end. */
234
+ /**
235
+ * Where the path ends: the target's first `?`, or its end.
236
+ * @internal Set by `HttpHandler` in this module when it copies the head, and read by `pathIs` and `queryIndex`.
237
+ */
212
238
  pathEnd: i32 = 0
239
+ /**
240
+ * How many fields `spans` describes.
241
+ * @internal Set by `HttpHandler` in this module when it copies the head, and read by `header`.
242
+ */
213
243
  headerCount: i32 = 0
214
244
  bodyLength: i32 = 0
215
245
  /** The connection's slot, and its generation when this request was read. */
@@ -217,10 +247,27 @@ export class HttpRequest {
217
247
  gen: i32 = 0
218
248
  /** Its index in the handler's pool. */
219
249
  index: i32 = 0
250
+ /**
251
+ * Where the request is in its life, one of the module's private `HANDLER_*` states.
252
+ * @internal Moved by `HttpHandler` in this module from the free list through reading, queued, held and streaming, and back.
253
+ */
220
254
  state: i32 = 0
255
+ /**
256
+ * Where the unsent bytes in `pending` start.
257
+ * @internal Advanced by `HttpHandler` in this module as the carrier takes pending bytes.
258
+ */
221
259
  pendingStart: i32 = 0
260
+ /**
261
+ * Where the unsent bytes in `pending` end.
262
+ * @internal Advanced by `HttpHandler` in this module as `write` keeps what the carrier had no room for.
263
+ */
222
264
  pendingEnd: i32 = 0
223
265
 
266
+ /**
267
+ * An empty request for the handler's pool; its buffers grow to the first
268
+ * request it holds and are kept for the ones after.
269
+ * @param index its place in the handler's pool, which the free list and the queue name it by
270
+ */
224
271
  constructor(index: i32) {
225
272
  this.index = index
226
273
  this.head = []
@@ -230,29 +277,50 @@ export class HttpRequest {
230
277
  this.pending = []
231
278
  }
232
279
 
233
- /** Whether the method is exactly `method` (methods are case-sensitive, RFC 9110 §9.1). */
280
+ /**
281
+ * Whether the method is exactly `method` (methods are case-sensitive, RFC 9110 §9.1).
282
+ * @param method the method name to compare with, such as `"GET"`
283
+ * @returns true when the request's method is byte for byte `method`
284
+ */
234
285
  methodIs(method: string): boolean {
235
286
  return handlerSpanIs(this.head, this.methodStart, this.methodEnd, method)
236
287
  }
237
288
 
238
- /** Whether the path, the target up to its `?`, is exactly `path`. */
289
+ /**
290
+ * Whether the path, the target up to its `?`, is exactly `path`.
291
+ * @param path the raw path to compare with, still percent-encoded as it came on the wire
292
+ * @returns true when the target's path, without its query, is byte for byte `path`
293
+ */
239
294
  pathIs(path: string): boolean {
240
295
  return handlerSpanIs(this.head, this.targetStart, this.pathEnd, path)
241
296
  }
242
297
 
243
- /** Where parameter `k` starts in `head`. Panics on a parameter the route does not have. */
298
+ /**
299
+ * Where parameter `k` starts in `head`. Panics on a parameter the route does not have.
300
+ * @param k the parameter's position in the route pattern, from 0
301
+ * @returns the byte offset in `head` of the parameter's first raw byte
302
+ */
244
303
  paramStart(k: i32): i32 {
245
304
  this.checkParam(k)
246
305
  return this.params[2 * k]
247
306
  }
248
307
 
249
- /** Where parameter `k` ends in `head`. */
308
+ /**
309
+ * Where parameter `k` ends in `head`.
310
+ * @param k the parameter's position in the route pattern, from 0
311
+ * @returns the byte offset in `head` just past the parameter's last raw byte
312
+ */
250
313
  paramEnd(k: i32): i32 {
251
314
  this.checkParam(k)
252
315
  return this.params[2 * k + 1]
253
316
  }
254
317
 
255
- /** Whether parameter `k`, raw, is exactly `text`. */
318
+ /**
319
+ * Whether parameter `k`, raw, is exactly `text`.
320
+ * @param k the parameter's position in the route pattern, from 0
321
+ * @param text the value to compare with, as it would appear still percent-encoded
322
+ * @returns true when the raw parameter bytes are exactly `text`
323
+ */
256
324
  paramIs(k: i32, text: string): boolean {
257
325
  this.checkParam(k)
258
326
  return handlerSpanIs(this.head, this.params[2 * k], this.params[2 * k + 1], text)
@@ -262,19 +330,26 @@ export class HttpRequest {
262
330
  * Parameter `k` percent-decoded into `out` from 0 (`routerDecode`): its
263
331
  * length, `ROUTER_BAD_ESCAPE` for a malformed escape, or `ROUTER_NO_ROOM`
264
332
  * when `out` is too short. Nothing is written on a code.
333
+ * @param k the parameter's position in the route pattern, from 0
334
+ * @param out where the decoded bytes go, from index 0; the caller's buffer
335
+ * @returns how many decoded bytes were written, or a negative `ROUTER_*` code
265
336
  */
266
337
  param(k: i32, out: u8[]): i32 {
267
338
  this.checkParam(k)
268
339
  return routerDecode(this.head, this.params[2 * k], this.params[2 * k + 1], out)
269
340
  }
270
341
 
271
- /** Parameter `k` read as a decimal number that is not negative and fits an `i32`, or -1. */
342
+ /**
343
+ * Parameter `k` read as a decimal number that is not negative and fits an `i32`, or -1.
344
+ * @param k the parameter's position in the route pattern, from 0
345
+ * @returns the number the parameter's digits spell, or -1 when it is empty, holds a non-digit or overflows
346
+ */
272
347
  paramDecimal(k: i32): i32 {
273
348
  this.checkParam(k)
274
349
  return handlerDecimal(this.head, this.params[2 * k], this.params[2 * k + 1])
275
350
  }
276
351
 
277
- checkParam(k: i32): void {
352
+ private checkParam(k: i32): void {
278
353
  if (k < 0 || k >= this.paramCount) {
279
354
  panic(`HttpRequest.param: the route has ${this.paramCount} parameter(s), not one at ${k}`)
280
355
  }
@@ -287,6 +362,9 @@ export class HttpRequest {
287
362
  * decoded too. `HANDLER_ABSENT` when no parameter has the name,
288
363
  * `ROUTER_BAD_ESCAPE` for a malformed escape in its value, `ROUTER_NO_ROOM`
289
364
  * when `out` is too short. A name with no `=` has an empty value.
365
+ * @param name the parameter name to look for, compared with each name after decoding
366
+ * @param out where the decoded value goes, from index 0; the caller's buffer
367
+ * @returns how many decoded bytes were written, or `HANDLER_ABSENT` or a negative `ROUTER_*` code
290
368
  */
291
369
  query(name: string, out: u8[]): i32 {
292
370
  const at: i32 = this.queryIndex(name)
@@ -296,7 +374,11 @@ export class HttpRequest {
296
374
  return handlerFormDecode(this.head, this.queryValueStart, this.queryValueEnd, out)
297
375
  }
298
376
 
299
- /** Whether the query holds a parameter named `name`. */
377
+ /**
378
+ * Whether the query holds a parameter named `name`.
379
+ * @param name the parameter name to look for, compared with each name after decoding
380
+ * @returns true when some pair in the query has that name, even with an empty value
381
+ */
300
382
  hasQuery(name: string): boolean {
301
383
  return this.queryIndex(name) >= 0
302
384
  }
@@ -305,7 +387,11 @@ export class HttpRequest {
305
387
  queryValueStart: i32 = 0
306
388
  queryValueEnd: i32 = 0
307
389
 
308
- /** Finds the first parameter named `name`: where its pair starts, or -1; its raw value goes to `queryValue...`. */
390
+ /**
391
+ * Finds the first parameter named `name`: where its pair starts, or -1; its raw value goes to `queryValue...`.
392
+ * @param name the parameter name to look for, compared with each name after decoding
393
+ * @returns the byte offset in `head` where the matching `name=value` pair starts, or -1 when none matches
394
+ */
309
395
  queryIndex(name: string): i32 {
310
396
  let at: i32 = this.pathEnd + 1
311
397
  const end: i32 = this.targetEnd
@@ -328,7 +414,11 @@ export class HttpRequest {
328
414
  return -1
329
415
  }
330
416
 
331
- /** The index of the first field named `name`, which is lowercase, or `HANDLER_ABSENT`. */
417
+ /**
418
+ * The index of the first field named `name`, which is lowercase, or `HANDLER_ABSENT`.
419
+ * @param name the field name in lowercase, since the parser lowercased every name it kept
420
+ * @returns the field's index for `valueStart` and `valueEnd`, or `HANDLER_ABSENT` when no field has the name
421
+ */
332
422
  header(name: string): i32 {
333
423
  for (let i: i32 = 0; i < this.headerCount; i++) {
334
424
  if (handlerSpanIs(this.head, this.spans[4 * i], this.spans[4 * i + 1], name)) {
@@ -338,17 +428,30 @@ export class HttpRequest {
338
428
  return HANDLER_ABSENT
339
429
  }
340
430
 
341
- /** Where field `i`'s value starts in `head`. */
431
+ /**
432
+ * Where field `i`'s value starts in `head`.
433
+ * @param i a field index, as `header` answered it
434
+ * @returns the byte offset in `head` of the value's first byte
435
+ */
342
436
  valueStart(i: i32): i32 {
343
437
  return this.spans[4 * i + 2]
344
438
  }
345
439
 
346
- /** Where field `i`'s value ends in `head`. */
440
+ /**
441
+ * Where field `i`'s value ends in `head`.
442
+ * @param i a field index, as `header` answered it
443
+ * @returns the byte offset in `head` just past the value's last byte
444
+ */
347
445
  valueEnd(i: i32): i32 {
348
446
  return this.spans[4 * i + 3]
349
447
  }
350
448
 
351
- /** Whether the first field named `name` (lowercase) has exactly the value `value`. */
449
+ /**
450
+ * Whether the first field named `name` (lowercase) has exactly the value `value`.
451
+ * @param name the field name in lowercase
452
+ * @param value the value to compare with, byte for byte and case-sensitive
453
+ * @returns true when the field is present and its first occurrence has exactly that value
454
+ */
352
455
  headerIs(name: string, value: string): boolean {
353
456
  const i: i32 = this.header(name)
354
457
  return i >= 0 && handlerSpanIs(this.head, this.spans[4 * i + 2], this.spans[4 * i + 3], value)
@@ -361,63 +464,76 @@ export class HttpRequest {
361
464
  */
362
465
  export class HttpHandler {
363
466
  server: Http1Server
364
- router: Router
467
+ private router: Router
365
468
  /** Every request the pool has made, and the indices of the free ones. */
366
- requests: HttpRequest[]
367
- free: i32[]
469
+ private requests: HttpRequest[]
470
+ private free: i32[]
368
471
  /** Whole requests waiting for `take`, a ring of `queue.length` with `queueCount` from `queueStart`. */
369
- queue: i32[]
472
+ private queue: i32[]
370
473
  /** Per slot: its generation, its request (-1 for none), and whether it holds a connection. */
371
- slotGen: i32[]
372
- current: i32[]
373
- open: boolean[]
474
+ private slotGen: i32[]
475
+ private current: i32[]
476
+ private open: boolean[]
374
477
  /** `pollWait`'s output. */
375
- readyFds: i32[]
478
+ private readyFds: i32[]
376
479
  /** Per watched id: whether its descriptor was ready in the last `wait`. */
377
- watchedFired: boolean[]
480
+ private watchedFired: boolean[]
378
481
  /** `json` and `problem` write here. */
379
482
  scratch: u8[]
380
483
  writer: JsonWriter
381
484
  /** `refuse`'s scratch for `Router.match`. */
382
- probe: i32[]
485
+ private probe: i32[]
383
486
  /** One field for `send`; two for a 405. */
384
- typeName: u8[][]
385
- typeValue: u8[][]
386
- allowNames: u8[][]
387
- allowValues: u8[][]
487
+ private typeName: u8[][]
488
+ private typeValue: u8[][]
489
+ private allowNames: u8[][]
490
+ private allowValues: u8[][]
388
491
  /** Each `Allow` value made, by the bit mask of the methods it lists: made once a mask and kept. */
389
- allowMasks: i64[]
390
- allowBytes: u8[][]
391
- jsonType: u8[]
392
- problemType: u8[]
393
- noFields: u8[][]
492
+ private allowMasks: i64[]
493
+ private allowBytes: u8[][]
494
+ private jsonType: u8[]
495
+ private problemType: u8[]
496
+ private noFields: u8[][]
394
497
  /**
395
498
  * When an accept failed for want of a resource (EMFILE, ENFILE, ENOMEM):
396
499
  * the listener is out of the loop until then, which it would otherwise wake
397
500
  * every turn while its connection waits. 0 while it is in the loop.
398
501
  */
399
- acceptRestUntil: i64 = 0
502
+ private acceptRestUntil: i64 = 0
400
503
  /** `bodyTimeout` in nanoseconds. */
401
- bodyTimeout: i64 = 0
402
- /** When the current turn's sockets were ready (`monotonicNanos`): a head read in it is stamped with this. */
504
+ private bodyTimeout: i64 = 0
505
+ /**
506
+ * When the current turn's sockets were ready (`monotonicNanos`): a head read in it is stamped with this.
507
+ * @internal Read by std/net/upstream.ts, whose `pump` compares it with the last turn it saw to run once per handler turn.
508
+ */
403
509
  turnAt: i64 = 0
404
510
  /** When `wait` next looks for a body past `bodyTimeout`. */
405
- bodyCheckAt: i64 = 0
406
- listener: i32 = 0
511
+ private bodyCheckAt: i64 = 0
512
+ private listener: i32 = 0
513
+ /**
514
+ * The readiness loop's descriptor, from `pollCreate`.
515
+ * @internal Read by std/net/upstream.ts to add its upstream sockets to the same loop, so one `wait` serves both.
516
+ */
407
517
  loop: i32 = 0
408
- queueStart: i32 = 0
409
- queueCount: i32 = 0
410
- outputSize: i32 = 0
518
+ private queueStart: i32 = 0
519
+ private queueCount: i32 = 0
520
+ private outputSize: i32 = 0
411
521
  /** Set by `drain`: accept nothing more and close each connection once it is idle. */
412
- draining: boolean = false
522
+ private draining: boolean = false
413
523
  /** Whether `json` gave out the writer for a value `sendJson` has not yet sent. */
414
- jsonBegun: boolean = false
524
+ private jsonBegun: boolean = false
415
525
 
416
526
  /**
417
527
  * A handler over `listener`, a listening socket the program made
418
528
  * (`tcpListen`, or `tcpListenFlags` with SO_REUSEPORT for a worker of
419
529
  * `nish/net/prefork`), routing through `router`, whose routes are added
420
530
  * before the first `wait`.
531
+ * @param limits the caps and timeouts. `slots`, `jsonBytes`, `maxEvents`
532
+ * and `bodyTimeout` are read once here, so changing them later has no
533
+ * effect; `http` is kept by the carrier, which reads its timeouts and
534
+ * caps live
535
+ * @param listener the listening socket's descriptor, still the program's to close
536
+ * @param router the routes, which the handler keeps and consults at each request head
421
537
  */
422
538
  constructor(limits: HandlerLimits, listener: i32, router: Router) {
423
539
  if (limits.slots < 1 || limits.jsonBytes < 256 || limits.maxEvents < 1 || limits.bodyTimeout < 1) {
@@ -469,6 +585,8 @@ export class HttpHandler {
469
585
  * `HANDLER_MAX_WATCHED - 1`): `fired(id)` says whether it was ready in the
470
586
  * last `wait`. For a program's own descriptors — `signalFd()` for a
471
587
  * graceful stop, a pipe from another thread.
588
+ * @param fd the program's descriptor to watch for reading; it stays the program's to read and close
589
+ * @param id the name `fired` asks about it by, from 0 to `HANDLER_MAX_WATCHED - 1`
472
590
  */
473
591
  watch(fd: i32, id: i32): void {
474
592
  if (id < 0 || id >= HANDLER_MAX_WATCHED) {
@@ -477,7 +595,11 @@ export class HttpHandler {
477
595
  pollAdd(this.loop, fd, H1_WANT_READ, -2 - id)
478
596
  }
479
597
 
480
- /** Whether watched descriptor `id` was ready in the last `wait`. */
598
+ /**
599
+ * Whether watched descriptor `id` was ready in the last `wait`.
600
+ * @param id the name the descriptor was given to `watch`
601
+ * @returns true when it was readable in the last turn; false for an id outside the range
602
+ */
481
603
  fired(id: i32): boolean {
482
604
  return id >= 0 && id < HANDLER_MAX_WATCHED && this.watchedFired[id]
483
605
  }
@@ -487,6 +609,8 @@ export class HttpHandler {
487
609
  * socket to be ready, accepts, reads and writes what is, closes the
488
610
  * connections past their timeouts, and answers how many whole requests
489
611
  * are waiting for `take`.
612
+ * @param timeoutMs the longest to block, in milliseconds, or -1 to block until something is ready
613
+ * @returns how many whole requests are queued for `take` after this turn
490
614
  */
491
615
  wait(timeoutMs: i32): i32 {
492
616
  for (let id: i32 = 0; id < HANDLER_MAX_WATCHED; id++) {
@@ -569,7 +693,7 @@ export class HttpHandler {
569
693
  }
570
694
 
571
695
  /** Takes every waiting connection. */
572
- acceptAll(): void {
696
+ private acceptAll(): void {
573
697
  if (this.draining) {
574
698
  return
575
699
  }
@@ -594,7 +718,7 @@ export class HttpHandler {
594
718
  * Takes `slot`'s events until it needs the socket again, then sends what it
595
719
  * holds and re-arms it, or closes it once it is over.
596
720
  */
597
- drive(slot: i32): void {
721
+ private drive(slot: i32): void {
598
722
  const conn: Http1Connection = this.server.connection(slot)
599
723
  let event: i32 = this.server.next(slot)
600
724
  while (event !== H1_NEED_MORE && event !== H1_ERROR) {
@@ -633,12 +757,12 @@ export class HttpHandler {
633
757
  }
634
758
 
635
759
  /** Whether `conn`'s output holds at most half its size unsent, so a response head fits behind it. */
636
- roomy(conn: Http1Connection): boolean {
760
+ private roomy(conn: Http1Connection): boolean {
637
761
  return (conn.outputEnd - conn.outputStart) * 2 <= this.outputSize
638
762
  }
639
763
 
640
764
  /** A head: a request from the free list, the head copied into it, and its route. */
641
- begin(slot: i32, conn: Http1Connection): void {
765
+ private begin(slot: i32, conn: Http1Connection): void {
642
766
  if (this.draining) {
643
767
  // A head read during the drain is the connection's last.
644
768
  conn.closeAfter = true
@@ -683,7 +807,7 @@ export class HttpHandler {
683
807
  * that route, since a GET resource answers HEAD (RFC 9110 §9.3.2) and the
684
808
  * carrier sends no body for it.
685
809
  */
686
- route(r: HttpRequest): void {
810
+ private route(r: HttpRequest): void {
687
811
  const params: i32[] = r.params
688
812
  let route: i32 = this.router.match(
689
813
  r.head,
@@ -716,7 +840,7 @@ export class HttpHandler {
716
840
  }
717
841
 
718
842
  /** Body bytes: appended to the request's body, which the carrier's cap bounds. */
719
- gather(slot: i32, conn: Http1Connection): void {
843
+ private gather(slot: i32, conn: Http1Connection): void {
720
844
  const at: i32 = this.current[slot]
721
845
  if (at < 0) {
722
846
  return
@@ -734,7 +858,7 @@ export class HttpHandler {
734
858
  }
735
859
 
736
860
  /** The request is whole: queued for `take`, or parked while the response before it is still mostly unsent. */
737
- whole(slot: i32, conn: Http1Connection): void {
861
+ private whole(slot: i32, conn: Http1Connection): void {
738
862
  const at: i32 = this.current[slot]
739
863
  if (at < 0) {
740
864
  return
@@ -747,7 +871,7 @@ export class HttpHandler {
747
871
  }
748
872
  }
749
873
 
750
- enqueue(r: HttpRequest): void {
874
+ private enqueue(r: HttpRequest): void {
751
875
  r.state = HANDLER_QUEUED
752
876
  const size: i32 = toI32(this.queue.length)
753
877
  if (this.queueCount === size) {
@@ -763,7 +887,7 @@ export class HttpHandler {
763
887
  }
764
888
 
765
889
  /** `H1_WRITE`: the carrier has room again for the response under way. */
766
- resume(slot: i32, conn: Http1Connection): void {
890
+ private resume(slot: i32, conn: Http1Connection): void {
767
891
  const at: i32 = this.current[slot]
768
892
  if (at < 0) {
769
893
  return
@@ -779,7 +903,7 @@ export class HttpHandler {
779
903
  }
780
904
 
781
905
  /** Hands the carrier as much of `r`'s pending bytes as it takes. */
782
- pushPending(r: HttpRequest, conn: Http1Connection): void {
906
+ private pushPending(r: HttpRequest, conn: Http1Connection): void {
783
907
  const left: i32 = r.pendingEnd - r.pendingStart
784
908
  if (left === 0) {
785
909
  return
@@ -795,7 +919,7 @@ export class HttpHandler {
795
919
  }
796
920
 
797
921
  /** Ends `r`'s response once nothing is pending, and hands `r` back; otherwise leaves it flushing. */
798
- finish(r: HttpRequest, conn: Http1Connection): void {
922
+ private finish(r: HttpRequest, conn: Http1Connection): void {
799
923
  r.state = HANDLER_FLUSHING
800
924
  if (r.pendingEnd > r.pendingStart) {
801
925
  return
@@ -809,7 +933,7 @@ export class HttpHandler {
809
933
  }
810
934
 
811
935
  /** A free request, or a new one. */
812
- obtain(): HttpRequest {
936
+ private obtain(): HttpRequest {
813
937
  const count: i32 = toI32(this.free.length)
814
938
  if (count > 0) {
815
939
  const at: i32 = this.free[count - 1]
@@ -822,7 +946,7 @@ export class HttpHandler {
822
946
  return r
823
947
  }
824
948
 
825
- release(r: HttpRequest): void {
949
+ private release(r: HttpRequest): void {
826
950
  if (r.state === HANDLER_FREE) {
827
951
  return
828
952
  }
@@ -831,7 +955,7 @@ export class HttpHandler {
831
955
  }
832
956
 
833
957
  /** Hands back the request `slot` was reading when the carrier refused it. */
834
- releaseReading(slot: i32): void {
958
+ private releaseReading(slot: i32): void {
835
959
  const at: i32 = this.current[slot]
836
960
  if (at >= 0 && this.requests[at].state === HANDLER_READING) {
837
961
  this.current[slot] = -1
@@ -844,13 +968,13 @@ export class HttpHandler {
844
968
  * dropped. A request the handler alone holds goes back now; one queued or
845
969
  * taken goes back when `ready` passes it or the program answers it.
846
970
  */
847
- closeSlot(slot: i32): void {
971
+ private closeSlot(slot: i32): void {
848
972
  this.server.close(slot)
849
973
  this.forget(slot)
850
974
  }
851
975
 
852
976
  /** The bookkeeping of a slot the carrier has closed. */
853
- forget(slot: i32): void {
977
+ private forget(slot: i32): void {
854
978
  this.open[slot] = false
855
979
  this.slotGen[slot] = this.slotGen[slot] + 1
856
980
  const at: i32 = this.current[slot]
@@ -864,7 +988,7 @@ export class HttpHandler {
864
988
  }
865
989
 
866
990
  /** Closes every connection whose request has been reading its body for longer than `bodyTimeout` at `now`. */
867
- expireBodies(now: i64): void {
991
+ private expireBodies(now: i64): void {
868
992
  for (let s: i32 = 0; s < toI32(this.current.length) && s < toI32(this.open.length); s++) {
869
993
  const at: i32 = this.current[s]
870
994
  if (this.open[s] && at >= 0 && at < toI32(this.requests.length)) {
@@ -877,7 +1001,7 @@ export class HttpHandler {
877
1001
  }
878
1002
 
879
1003
  /** Every slot the carrier's `expire` closed. */
880
- sweep(): void {
1004
+ private sweep(): void {
881
1005
  for (let s: i32 = 0; s < toI32(this.open.length); s++) {
882
1006
  if (this.open[s] && !this.server.holds(s)) {
883
1007
  this.forget(s)
@@ -887,7 +1011,10 @@ export class HttpHandler {
887
1011
 
888
1012
  // ---- Requests -------------------------------------------------------------------
889
1013
 
890
- /** Whether a whole request is waiting for `take`. Requests whose connection closed while queued are handed back here. */
1014
+ /**
1015
+ * Whether a whole request is waiting for `take`. Requests whose connection closed while queued are handed back here.
1016
+ * @returns true when `take` will give out a request whose connection is still open
1017
+ */
891
1018
  ready(): boolean {
892
1019
  while (this.queueCount > 0) {
893
1020
  const r: HttpRequest = this.requests[this.queue[this.queueStart]]
@@ -901,7 +1028,10 @@ export class HttpHandler {
901
1028
  return false
902
1029
  }
903
1030
 
904
- /** The next whole request, which the program now owes an answer. Panics when none is `ready`. */
1031
+ /**
1032
+ * The next whole request, which the program now owes an answer. Panics when none is `ready`.
1033
+ * @returns the oldest queued request, the program's until it answers it
1034
+ */
905
1035
  take(): HttpRequest {
906
1036
  if (!this.ready()) {
907
1037
  panic("HttpHandler.take: no request is ready")
@@ -914,16 +1044,22 @@ export class HttpHandler {
914
1044
  }
915
1045
 
916
1046
  /** Whether `r`'s connection is the one it was read on, still open. */
917
- live(r: HttpRequest): boolean {
1047
+ private live(r: HttpRequest): boolean {
918
1048
  return this.server.holds(r.slot) && this.slotGen[r.slot] === r.gen
919
1049
  }
920
1050
 
921
- /** How many requests the pool has made: the most ever in flight at once. */
1051
+ /**
1052
+ * How many requests the pool has made: the most ever in flight at once.
1053
+ * @returns the pool's size, which only grows, so a flat count shows nothing leaks
1054
+ */
922
1055
  pooled(): i32 {
923
1056
  return toI32(this.requests.length)
924
1057
  }
925
1058
 
926
- /** How many connections are open. */
1059
+ /**
1060
+ * How many connections are open.
1061
+ * @returns the number of carrier slots holding a connection, which falls to 0 after `drain`
1062
+ */
927
1063
  connections(): i32 {
928
1064
  return this.server.busy()
929
1065
  }
@@ -936,6 +1072,14 @@ export class HttpHandler {
936
1072
  * `write` the body and `end`. Answers `HANDLER_SENT`, or `HANDLER_DROPPED`
937
1073
  * when `r`'s connection has closed — every later `write` and `end` for it
938
1074
  * then does nothing, and `end` hands it back.
1075
+ * @param r a request taken and not yet answered
1076
+ * @param status the response status, from 200 to 599
1077
+ * @param names the field names, as bytes, beside `values`
1078
+ * @param values each field's value, `values[k]` for `names[k]`
1079
+ * @param bodyLength how many body bytes will follow, or `HTTP1_CHUNKED` for a body of unknown length
1080
+ * @returns `HANDLER_SENT`, or `HANDLER_DROPPED` when the connection closed
1081
+ * first, or when the carrier's output was too full to take the head and
1082
+ * the connection was closed for it
939
1083
  */
940
1084
  start(r: HttpRequest, status: i32, names: u8[][], values: u8[][], bodyLength: i32): i32 {
941
1085
  if (r.state !== HANDLER_HELD) {
@@ -974,6 +1118,11 @@ export class HttpHandler {
974
1118
  * Writes `buf[off .. off + len)` of `r`'s body. What the carrier has no room
975
1119
  * for is copied and sent as room comes, so `buf` is the caller's again on
976
1120
  * return. Answers `HANDLER_SENT` or `HANDLER_DROPPED`.
1121
+ * @param r a request whose response `start` began
1122
+ * @param buf the bytes to send from; the caller's again on return
1123
+ * @param off where in `buf` the body bytes start
1124
+ * @param len how many bytes to send
1125
+ * @returns `HANDLER_SENT`, or `HANDLER_DROPPED` when the connection closed first
977
1126
  */
978
1127
  write(r: HttpRequest, buf: u8[], off: i32, len: i32): i32 {
979
1128
  if (r.state !== HANDLER_STREAMING) {
@@ -1013,7 +1162,7 @@ export class HttpHandler {
1013
1162
  }
1014
1163
 
1015
1164
  /** Appends bytes to `r`'s pending buffer, growing it once to the largest response it has held. */
1016
- keep(r: HttpRequest, buf: u8[], off: i32, len: i32): void {
1165
+ private keep(r: HttpRequest, buf: u8[], off: i32, len: i32): void {
1017
1166
  const live: i32 = r.pendingEnd - r.pendingStart
1018
1167
  if (toI32(r.pending.length) - r.pendingEnd < len) {
1019
1168
  if (r.pendingStart > 0 || toI32(r.pending.length) < live + len) {
@@ -1035,6 +1184,8 @@ export class HttpHandler {
1035
1184
  * Ends `r`'s response. `r` is the handler's again: it goes back once the
1036
1185
  * carrier has taken every byte, and the connection's next request is read.
1037
1186
  * Answers `HANDLER_SENT` or `HANDLER_DROPPED`.
1187
+ * @param r a request whose response `start` began; the handler's again after this call
1188
+ * @returns `HANDLER_SENT`, or `HANDLER_DROPPED` when the connection closed first
1038
1189
  */
1039
1190
  end(r: HttpRequest): i32 {
1040
1191
  if (r.state !== HANDLER_STREAMING) {
@@ -1055,13 +1206,36 @@ export class HttpHandler {
1055
1206
  return HANDLER_SENT
1056
1207
  }
1057
1208
 
1058
- /** Answers `r` with `status`, a `Content-Type` of `contentType` and the body `body[off .. off + len)`, in one call. */
1209
+ /**
1210
+ * Answers `r` with `status`, a `Content-Type` of `contentType` and the body `body[off .. off + len)`, in one call.
1211
+ * @param r a request taken and not yet answered; the handler's again after this call
1212
+ * @param status the response status, from 200 to 599
1213
+ * @param contentType the `Content-Type` value, as bytes
1214
+ * @param body the buffer holding the body; the caller's again on return
1215
+ * @param off where in `body` the body bytes start
1216
+ * @param len how many body bytes to send
1217
+ * @returns `HANDLER_SENT`, or `HANDLER_DROPPED` when the connection closed
1218
+ * first, or when the carrier's output was too full to take the head and
1219
+ * the connection was closed for it
1220
+ */
1059
1221
  send(r: HttpRequest, status: i32, contentType: u8[], body: u8[], off: i32, len: i32): i32 {
1060
1222
  this.typeValue[0] = contentType
1061
1223
  return this.sendFields(r, status, this.typeName, this.typeValue, body, off, len)
1062
1224
  }
1063
1225
 
1064
- /** Answers `r` with `status`, the fields `names[k]`: `values[k]` and the body `body[off .. off + len)`, in one call. */
1226
+ /**
1227
+ * Answers `r` with `status`, the fields `names[k]`: `values[k]` and the body `body[off .. off + len)`, in one call.
1228
+ * @param r a request taken and not yet answered; the handler's again after this call
1229
+ * @param status the response status, from 200 to 599
1230
+ * @param names the field names, as bytes, beside `values`
1231
+ * @param values each field's value, `values[k]` for `names[k]`
1232
+ * @param body the buffer holding the body; the caller's again on return
1233
+ * @param off where in `body` the body bytes start
1234
+ * @param len how many body bytes to send
1235
+ * @returns `HANDLER_SENT`, or `HANDLER_DROPPED` when the connection closed
1236
+ * first, or when the carrier's output was too full to take the head and
1237
+ * the connection was closed for it
1238
+ */
1065
1239
  sendFields(
1066
1240
  r: HttpRequest,
1067
1241
  status: i32,
@@ -1082,7 +1256,14 @@ export class HttpHandler {
1082
1256
  return this.end(r)
1083
1257
  }
1084
1258
 
1085
- /** Answers `r` with `status` and no body: a 204, or a status whose meaning is all there is to say. */
1259
+ /**
1260
+ * Answers `r` with `status` and no body: a 204, or a status whose meaning is all there is to say.
1261
+ * @param r a request taken and not yet answered; the handler's again after this call
1262
+ * @param status the response status, from 200 to 599
1263
+ * @returns `HANDLER_SENT`, or `HANDLER_DROPPED` when the connection closed
1264
+ * first, or when the carrier's output was too full to take the head and
1265
+ * the connection was closed for it
1266
+ */
1086
1267
  empty(r: HttpRequest, status: i32): i32 {
1087
1268
  return this.sendFields(r, status, this.noFields, this.noFields, this.scratch, HANDLER_ZERO, HANDLER_ZERO)
1088
1269
  }
@@ -1091,6 +1272,7 @@ export class HttpHandler {
1091
1272
  * The JSON writer, reset onto the handler's scratch buffer: write one value
1092
1273
  * with it, then `sendJson`. One JSON body at a time, since every request
1093
1274
  * shares the buffer.
1275
+ * @returns the handler's own writer, empty, valid until the next `json`, `sendJson` or `problem`
1094
1276
  */
1095
1277
  json(): JsonWriter {
1096
1278
  this.writer.reset(this.scratch, HANDLER_ZERO)
@@ -1102,6 +1284,11 @@ export class HttpHandler {
1102
1284
  * Answers `r` with `status` and the value `json`'s writer holds, as
1103
1285
  * `application/json`. A value the writer refused or did not finish — it
1104
1286
  * outgrew `jsonBytes`, say — is answered as a 500 problem instead.
1287
+ * @param r a request taken and not yet answered; the handler's again after this call
1288
+ * @param status the response status for a value that fit, from 200 to 599
1289
+ * @returns `HANDLER_SENT`, or `HANDLER_DROPPED` when the connection closed
1290
+ * first, or when the carrier's output was too full to take the head and
1291
+ * the connection was closed for it
1105
1292
  */
1106
1293
  sendJson(r: HttpRequest, status: i32): i32 {
1107
1294
  if (!this.jsonBegun) {
@@ -1120,6 +1307,13 @@ export class HttpHandler {
1120
1307
  * `status` and, when not empty, `detail`, a sentence for a person about
1121
1308
  * this occurrence. Nothing of the request is echoed, so a detail is the
1122
1309
  * program's own words.
1310
+ * @param r a request taken and not yet answered; the handler's again after this call
1311
+ * @param status the response status, from 200 to 599, also written into the object
1312
+ * @param title the status's reason phrase, or empty to leave it out
1313
+ * @param detail a sentence for a person about this occurrence, or empty to leave it out
1314
+ * @returns `HANDLER_SENT`, or `HANDLER_DROPPED` when the connection closed
1315
+ * first, or when the carrier's output was too full to take the head and
1316
+ * the connection was closed for it
1123
1317
  */
1124
1318
  problem(r: HttpRequest, status: i32, title: string, detail: string): i32 {
1125
1319
  const length: i32 = this.writeProblem(status, title, detail)
@@ -1131,7 +1325,7 @@ export class HttpHandler {
1131
1325
  * One too large for it is written without its detail, and then without its
1132
1326
  * title, which leaves room enough in the least `jsonBytes`.
1133
1327
  */
1134
- writeProblem(status: i32, title: string, detail: string): i32 {
1328
+ private writeProblem(status: i32, title: string, detail: string): i32 {
1135
1329
  let length: i32 = this.writeProblemObject(status, title, detail)
1136
1330
  if (length < 0) {
1137
1331
  length = this.writeProblemObject(status, title, "")
@@ -1144,7 +1338,7 @@ export class HttpHandler {
1144
1338
  }
1145
1339
 
1146
1340
  /** `writeProblem`'s one try: the object's length, or -1 when it did not fit. */
1147
- writeProblemObject(status: i32, title: string, detail: string): i32 {
1341
+ private writeProblemObject(status: i32, title: string, detail: string): i32 {
1148
1342
  const w: JsonWriter = this.json()
1149
1343
  w.beginObject()
1150
1344
  w.key("type")
@@ -1168,6 +1362,10 @@ export class HttpHandler {
1168
1362
  * field naming the methods the path has (RFC 9110 §15.5.6), and 404 for
1169
1363
  * anything else. A parameter's malformed escape is the program's to refuse,
1170
1364
  * when it decodes the parameter.
1365
+ * @param r a request whose `route` is negative, taken and not yet answered; the handler's again after this call
1366
+ * @returns `HANDLER_SENT`, or `HANDLER_DROPPED` when the connection closed
1367
+ * first, or when the carrier's output was too full to take the head and
1368
+ * the connection was closed for it
1171
1369
  */
1172
1370
  refuse(r: HttpRequest): i32 {
1173
1371
  if (r.route === ROUTER_METHOD_NOT_ALLOWED) {
@@ -1191,7 +1389,7 @@ export class HttpHandler {
1191
1389
  * HEAD wherever GET is, since a GET route answers HEAD too. Made once for
1192
1390
  * each set of methods.
1193
1391
  */
1194
- allowFor(r: HttpRequest): u8[] {
1392
+ private allowFor(r: HttpRequest): u8[] {
1195
1393
  const router: Router = this.router
1196
1394
  const methods: i32 = toI32(router.methodStart.length)
1197
1395
  let mask: i64 = 0