@amritk/nish-aarch64-linux 0.35.0 → 0.37.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 (128) hide show
  1. package/INSTALL.md +73 -59
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +50 -4
  4. package/package.json +1 -1
  5. package/runtime/async.d.mts +2 -0
  6. package/runtime/async.mjs +7 -0
  7. package/runtime/nish.d.ts +4849 -430
  8. package/runtime/nish.h +25 -0
  9. package/runtime/page.d.ts +53 -0
  10. package/runtime/page.mjs +146 -0
  11. package/runtime/runtime-async.c +557 -0
  12. package/runtime/runtime-wasm.c +63 -7
  13. package/runtime/vite.d.ts +30 -0
  14. package/runtime/vite.mjs +150 -0
  15. package/scripts/build.sh +252 -15
  16. package/std/README.md +107 -82
  17. package/std/base64.ts +34 -5
  18. package/std/bytes.ts +279 -59
  19. package/std/cli-args.ts +296 -70
  20. package/std/cli-flags.ts +184 -19
  21. package/std/cli.ts +475 -54
  22. package/std/collections.ts +121 -23
  23. package/std/compress/checksum.ts +44 -14
  24. package/std/compress/deflate.ts +224 -111
  25. package/std/compress/gzip.ts +217 -53
  26. package/std/compress/zlib.ts +63 -7
  27. package/std/crypto/aes.ts +128 -52
  28. package/std/crypto/base64url.ts +29 -5
  29. package/std/crypto/bcrypt-batch.ts +101 -34
  30. package/std/crypto/bcrypt.ts +93 -27
  31. package/std/crypto/chacha20poly1305.ts +69 -14
  32. package/std/crypto/ct.ts +32 -13
  33. package/std/crypto/ed25519.ts +66 -16
  34. package/std/crypto/hkdf.ts +198 -13
  35. package/std/crypto/hmac.ts +242 -37
  36. package/std/crypto/jwt.ts +204 -72
  37. package/std/crypto/p256.ts +222 -72
  38. package/std/crypto/p384.ts +51 -23
  39. package/std/crypto/pki-roots.ts +32 -2
  40. package/std/crypto/pki.ts +99 -30
  41. package/std/crypto/rsa.ts +56 -22
  42. package/std/crypto/sha1.ts +3 -0
  43. package/std/crypto/sha256.ts +24 -1
  44. package/std/crypto/sha512.ts +99 -14
  45. package/std/crypto/x25519.ts +163 -24
  46. package/std/crypto/x509-random.ts +10 -0
  47. package/std/crypto/x509.ts +133 -66
  48. package/std/host/ring.ts +124 -0
  49. package/std/json-equal.ts +31 -11
  50. package/std/json-format.ts +112 -13
  51. package/std/json-number.ts +76 -26
  52. package/std/json-pointer.ts +23 -1
  53. package/std/json-tokenizer.ts +126 -51
  54. package/std/json-typed.ts +757 -96
  55. package/std/json-value.ts +438 -157
  56. package/std/json.ts +170 -38
  57. package/std/map.ts +25 -7
  58. package/std/math.ts +36 -4
  59. package/std/net/buffer-pool.ts +42 -11
  60. package/std/net/dns-config.ts +109 -23
  61. package/std/net/dns-message.ts +62 -7
  62. package/std/net/dns-resolver.ts +247 -53
  63. package/std/net/dns.ts +203 -35
  64. package/std/net/fetch.ts +1267 -290
  65. package/std/net/handler.ts +296 -80
  66. package/std/net/hpack.ts +290 -77
  67. package/std/net/http-compress.ts +152 -36
  68. package/std/net/http-fields.ts +85 -9
  69. package/std/net/http1-client-codec.ts +129 -44
  70. package/std/net/http1-codec.ts +279 -114
  71. package/std/net/http1-server.ts +409 -139
  72. package/std/net/http2-frame.ts +289 -51
  73. package/std/net/http2-tls.ts +152 -49
  74. package/std/net/http2.ts +314 -160
  75. package/std/net/http3-frame.ts +165 -27
  76. package/std/net/http3-server.ts +282 -131
  77. package/std/net/http3.ts +377 -173
  78. package/std/net/outbound.ts +37 -3
  79. package/std/net/postgres-carrier.ts +144 -8
  80. package/std/net/postgres.ts +898 -260
  81. package/std/net/prefork.ts +202 -65
  82. package/std/net/qpack.ts +120 -32
  83. package/std/net/quic-conn-ack.ts +60 -12
  84. package/std/net/quic-conn-cid.ts +135 -27
  85. package/std/net/quic-conn-params.ts +109 -22
  86. package/std/net/quic-datagram.ts +50 -3
  87. package/std/net/quic-frame.ts +333 -23
  88. package/std/net/quic-listener.ts +161 -75
  89. package/std/net/quic-packet.ts +373 -73
  90. package/std/net/quic-recovery.ts +287 -65
  91. package/std/net/quic-stream.ts +436 -146
  92. package/std/net/quic.ts +693 -323
  93. package/std/net/router.ts +87 -40
  94. package/std/net/tls/client.ts +167 -51
  95. package/std/net/tls/codec.ts +494 -68
  96. package/std/net/tls/record-client.ts +140 -73
  97. package/std/net/tls/record-server.ts +233 -105
  98. package/std/net/tls/record.ts +142 -36
  99. package/std/net/tls/schedule.ts +235 -26
  100. package/std/net/tls/ticket.ts +161 -40
  101. package/std/net/tls/verify.ts +57 -10
  102. package/std/net/tls-tcp.ts +308 -95
  103. package/std/net/tls.ts +276 -102
  104. package/std/net/upstream.ts +81 -45
  105. package/std/net/url.ts +109 -22
  106. package/std/net/websocket.ts +140 -26
  107. package/std/net/webtransport.ts +243 -106
  108. package/std/pair.ts +10 -2
  109. package/std/random.ts +42 -15
  110. package/std/regex.ts +422 -257
  111. package/std/region.ts +70 -15
  112. package/std/secret.ts +52 -11
  113. package/std/sort.ts +17 -2
  114. package/std/sqlite.ts +256 -35
  115. package/std/sync/ring.ts +229 -56
  116. package/std/sync/shm.ts +203 -23
  117. package/std/term-io.ts +154 -78
  118. package/std/term-pixels.ts +144 -19
  119. package/std/term-probe.ts +77 -35
  120. package/std/term-rate.ts +199 -118
  121. package/std/term.ts +400 -175
  122. package/std/testing.ts +130 -32
  123. package/std/text.ts +84 -17
  124. package/std/threads.ts +248 -56
  125. package/std/yaml-json.ts +111 -97
  126. package/std/yaml-lines.ts +47 -15
  127. package/std/yaml-write.ts +45 -30
  128. package/std/yaml.ts +764 -384
@@ -6,7 +6,7 @@
6
6
  * socket gave it, of any length and cut anywhere, and then calls `next()`
7
7
  * until it answers `HTTP1_NEED_MORE`:
8
8
  *
9
- * import { Http1Parser, HTTP1_HEAD, HTTP1_BODY, HTTP1_END, HTTP1_ERROR } from "nish/net/http1-codec";
9
+ * import { Http1Parser, HTTP1_NEED_MORE, HTTP1_HEAD, HTTP1_BODY, HTTP1_END, HTTP1_ERROR } from "nish/net/http1-codec";
10
10
  *
11
11
  * const p = new Http1Parser(maxTarget, maxHeaderBytes, maxHeaders, maxBody);
12
12
  * p.feed(received, 0, n);
@@ -86,7 +86,6 @@ import {
86
86
  any,
87
87
  bitmask,
88
88
  eq,
89
- extract,
90
89
  gt,
91
90
  loadU8x16,
92
91
  lt,
@@ -155,8 +154,20 @@ const HTTP1_S_ERROR: i32 = 10
155
154
  * has, how many of them are the word asked about, and whether the last is.
156
155
  */
157
156
  class Http1ListTally {
157
+ /**
158
+ * How many elements the list has.
159
+ * @internal Written by `http1Tally` and read by the field checks in this module.
160
+ */
158
161
  elements: i32 = 0
162
+ /**
163
+ * How many elements are the word asked about.
164
+ * @internal Written by `http1Tally` and read by the field checks in this module.
165
+ */
159
166
  matches: i32 = 0
167
+ /**
168
+ * Whether the last element is the word asked about.
169
+ * @internal Written by `http1Tally` and read by the field checks in this module.
170
+ */
160
171
  lastMatches: boolean = false
161
172
  }
162
173
 
@@ -175,7 +186,12 @@ export class Http1Parser {
175
186
  // the struct is the 200 bytes its fields sum to, rounded to 8.
176
187
 
177
188
  // --- the request's buffers -------------------------------------------------
178
- /** The bytes fed and not yet consumed are `buf[start .. end)`. */
189
+ /**
190
+ * The bytes fed and not yet consumed are `buf[start .. end)`.
191
+ * @internal Filled by `feed` in this module, and swapped for a pooled buffer
192
+ * by std/net/http1-server.ts when it parks or resumes the input; written
193
+ * directly by bench/http1-conn.
194
+ */
179
195
  buf: u8[]
180
196
  /**
181
197
  * The request line and the kept field lines of the request, copied out of
@@ -190,13 +206,42 @@ export class Http1Parser {
190
206
  body: u8[]
191
207
 
192
208
  // --- where the parser is ----------------------------------------------------
209
+ /**
210
+ * Where the unconsumed input starts in `buf`.
211
+ * @internal Moved by the parser's free functions in this module, and reset by
212
+ * std/net/http1-server.ts when it parks the input buffer, and read there to
213
+ * hand the bytes after an upgrade to the WebSocket reader.
214
+ */
193
215
  start: i32 = 0
216
+ /**
217
+ * Where the unconsumed input ends in `buf`.
218
+ * @internal Moved by `feed` in this module, reset by
219
+ * std/net/http1-server.ts when it parks the input buffer, and moved directly
220
+ * by bench/http1-conn.
221
+ */
194
222
  end: i32 = 0
195
- /** Where the search for the next LF resumes, so a line fed a byte at a time is scanned once. */
223
+ /**
224
+ * Where the search for the next LF resumes, so a line fed a byte at a time is scanned once.
225
+ * @internal Moved by `http1TakeLine` in this module, and reset by
226
+ * std/net/http1-server.ts when it parks the input buffer.
227
+ */
196
228
  scan: i32 = 0
229
+ /**
230
+ * Which kind of thing the parser waits for, one of the `HTTP1_S_` states.
231
+ * @internal Read and written by the parser's free functions in this module to drive the state machine.
232
+ */
197
233
  state: i32 = 0
198
- /** The line `http1TakeLine` found is `buf[lineStart .. lineEnd)`, its CRLF dropped. */
234
+ /**
235
+ * The line `http1TakeLine` found is `buf[lineStart .. lineEnd)`, its CRLF dropped.
236
+ * @internal Set by `http1TakeLine` in this module, and reset by
237
+ * std/net/http1-server.ts when it parks the input buffer.
238
+ */
199
239
  lineStart: i32 = 0
240
+ /**
241
+ * Where the line `http1TakeLine` found ends, before its CRLF.
242
+ * @internal Set by `http1TakeLine` in this module, and reset by
243
+ * std/net/http1-server.ts when it parks the input buffer.
244
+ */
200
245
  lineEnd: i32 = 0
201
246
  /** How many fields the request's header section kept: the spans of field `i` are `spans[4 * i ..]`. */
202
247
  headerCount: i32 = 0
@@ -207,15 +252,23 @@ export class Http1Parser {
207
252
  * control byte other than HTAB, or DEL (RFC 9110 §5.5). `http1TakeLine`
208
253
  * sets it as it looks for the line's end, so a field line's bytes are read
209
254
  * once, not once for the LF and again for the check.
255
+ * @internal Set by `http1TakeLine` and read by the field-line parser in this module.
210
256
  */
211
257
  lineControl: boolean = false
258
+ /** Whether the request's body is chunked (`Transfer-Encoding: chunked`), so it has no `contentLength`. */
212
259
  chunked: boolean = false
260
+ /**
261
+ * Whether the connection stays open after this request: HTTP/1.1 unless it
262
+ * sent `Connection: close`, HTTP/1.0 only when it sent `keep-alive`. A
263
+ * server closes after its response when this is false.
264
+ */
213
265
  keepAlive: boolean = false
214
266
  /** Whether the request asked to switch protocols (`upgrade` is its value when `keepText`). */
215
267
  upgrading: boolean = false
216
268
  /**
217
269
  * Whether a kept-alive request ended and its head is still held for the
218
270
  * response: the next `next()` clears it before it reads a byte more.
271
+ * @internal Read and written by the parser's free functions in this module to keep a finished request's head readable.
219
272
  */
220
273
  headHeld: boolean = false
221
274
  /**
@@ -226,53 +279,97 @@ export class Http1Parser {
226
279
  keepText: boolean = true
227
280
 
228
281
  // --- the request's numbers: valid from its `HTTP1_HEAD` until the `next()` after its `HTTP1_END`
229
- /** Bytes of the header or trailer section so far, against `maxHeaderBytes`. */
282
+ /**
283
+ * Bytes of the header or trailer section so far, against `maxHeaderBytes`.
284
+ * @internal Read and written by the parser's free functions in this module to enforce the 431 limit.
285
+ */
230
286
  sectionBytes: i32 = 0
231
- /** Field lines of the request so far, its trailers included, against `maxHeaders`. */
287
+ /**
288
+ * Field lines of the request so far, its trailers included, against `maxHeaders`.
289
+ * @internal Read and written by the parser's free functions in this module to enforce the 431 limit.
290
+ */
232
291
  fieldCount: i32 = 0
233
292
  /** The minor version: 0 for HTTP/1.0, 1 for HTTP/1.1 and anything above it. */
234
293
  minor: i32 = 1
235
- /** Bytes left in a length-delimited body or in the current chunk. */
294
+ /**
295
+ * Bytes left in a length-delimited body or in the current chunk.
296
+ * @internal Read and written by the parser's free functions in this module to count body bytes rather than guess them.
297
+ */
236
298
  left: i32 = 0
237
299
  /** The declared body length, or -1 when the request has none or is chunked. */
238
300
  contentLength: i32 = -1
301
+ /** Where the bytes the last `HTTP1_BODY` handed over start in `body`. */
239
302
  bodyOff: i32 = 0
303
+ /** How many bytes the last `HTTP1_BODY` handed over, from `body[bodyOff]`; 0 before the first. */
240
304
  bodyLen: i32 = 0
241
305
  /** The method is `head[methodStart .. methodEnd)` and the target `head[targetStart .. targetEnd)`. */
242
306
  methodStart: i32 = 0
307
+ /** Where the method ends in `head`, one past its last byte. */
243
308
  methodEnd: i32 = 0
309
+ /** Where the request target starts in `head`. */
244
310
  targetStart: i32 = 0
311
+ /** Where the request target ends in `head`, one past its last byte. */
245
312
  targetEnd: i32 = 0
246
313
 
247
314
  // --- limits, the caller's --------------------------------------------------
315
+ /**
316
+ * The longest request target, in bytes, before a 414.
317
+ * @internal Set by the constructor and read by the request-line parser in this module.
318
+ */
248
319
  maxTarget: i32 = 0
320
+ /**
321
+ * The longest header or trailer section, in bytes, before a 431.
322
+ * @internal Set by the constructor and read by the field-line parser in this module.
323
+ */
249
324
  maxHeaderBytes: i32 = 0
325
+ /**
326
+ * The most field lines one request may carry, before a 431.
327
+ * @internal Set by the constructor and read by the field-line parser in this module.
328
+ */
250
329
  maxHeaders: i32 = 0
330
+ /**
331
+ * The longest body, declared or de-chunked, in bytes, before a 413.
332
+ * @internal Set by the constructor and read by the body readers in this module.
333
+ */
251
334
  maxBody: i32 = 0
252
335
  /** The most body bytes one `HTTP1_BODY` hands over; a caller's number, 2^30 unless set. */
253
336
  maxChunk: i32 = 1073741824
254
337
 
255
338
  // --- what `keepText`, an error or a chunked body reads ----------------------
339
+ /** The request's method as text when `keepText` is on, else empty. */
256
340
  method: string = ""
341
+ /** The request target (path and query) as text when `keepText` is on, else empty. */
257
342
  target: string = ""
258
343
  /** The field names, in lowercase, in the order they arrived; `values` beside them. */
259
344
  names: string[]
345
+ /** The field values, in the order they arrived, each at its name's index in `names`; filled only when `keepText`. */
260
346
  values: string[]
261
347
  /** The `Upgrade` field when `Connection` asked for it, else empty. */
262
348
  upgrade: string = ""
263
349
  /** What was wrong, once `next()` said `HTTP1_ERROR`; for a log, not for the response. */
264
350
  reason: string = ""
265
- /** Reused by every list `http1Tally` walks. */
351
+ /**
352
+ * Reused by every list `http1Tally` walks.
353
+ * @internal Filled by `http1Tally` and read by the field checks in this module, so a list costs no allocation.
354
+ */
266
355
  tally: Http1ListTally
267
356
  /** The status code to answer once `next()` said `HTTP1_ERROR`. */
268
357
  status: i32 = 0
269
- /** De-chunked body bytes so far, against `maxBody`. */
358
+ /**
359
+ * De-chunked body bytes so far, against `maxBody`.
360
+ * @internal Read and written by the parser's free functions in this module to enforce the 413 limit on a chunked body.
361
+ */
270
362
  bodyTotal: i32 = 0
271
363
 
272
364
  /**
273
365
  * Each limit is clamped into 0 to 2^30, so that the sums the parser makes
274
366
  * of them (a request line is the method, the target and ten bytes more)
275
367
  * cannot overflow an `i32`.
368
+ *
369
+ * @param maxTarget the longest request target in bytes; a longer one is a 414
370
+ * @param maxHeaderBytes the longest header or trailer section in bytes; a longer one is a 431
371
+ * @param maxHeaders the most field lines a request may carry, trailers included; more is a 431
372
+ * @param maxBody the longest body in bytes, declared or de-chunked; a longer one is a 413
276
373
  */
277
374
  constructor(maxTarget: i32, maxHeaderBytes: i32, maxHeaders: i32, maxBody: i32) {
278
375
  this.maxTarget = http1Clamp(maxTarget)
@@ -301,6 +398,10 @@ export class Http1Parser {
301
398
  * panics, as the crypto modules' `update` does. Once the parser has closed
302
399
  * or refused, the input is dropped; once it has upgraded, it is kept for
303
400
  * `upgradeBytes()`.
401
+ *
402
+ * @param data the bytes the socket gave, which the parser copies and does not keep
403
+ * @param off where in `data` the new bytes start
404
+ * @param len how many bytes from `off` to append
304
405
  */
305
406
  feed(data: u8[], off: i32, len: i32): void {
306
407
  const size: i32 = toI32(data.length)
@@ -319,7 +420,10 @@ export class Http1Parser {
319
420
  this.end = at + len
320
421
  }
321
422
 
322
- /** The next event the input holds; see the module comment. */
423
+ /**
424
+ * The next event the input holds; see the module comment.
425
+ * @returns one of the `HTTP1_` events, `HTTP1_NEED_MORE` when the input must grow before anything more can happen
426
+ */
323
427
  next(): i32 {
324
428
  return http1Next(this)
325
429
  }
@@ -328,6 +432,9 @@ export class Http1Parser {
328
432
  * The value of the first field named `name` (any case), or `null`. A field
329
433
  * that may repeat is read through the spans. The answer is a fresh string;
330
434
  * `headerIndex` and `headerIs` allocate nothing.
435
+ *
436
+ * @param name the field name to look for, compared in any case
437
+ * @returns the first such field's value with its surrounding whitespace trimmed, or `null` when the request has none
331
438
  */
332
439
  header(name: string): string | null {
333
440
  const i: i32 = this.headerIndex(name)
@@ -345,7 +452,11 @@ export class Http1Parser {
345
452
  return http1Text(this.head, spans[at + 2], spans[at + 3], false)
346
453
  }
347
454
 
348
- /** The index of the first field named `name` (any case), or -1. */
455
+ /**
456
+ * The index of the first field named `name` (any case), or -1.
457
+ * @param name the field name to look for, compared in any case
458
+ * @returns which field it is, so its spans are `spans[4 * i ..]`, or -1 when the request has none
459
+ */
349
460
  headerIndex(name: string): i32 {
350
461
  const spans: i32[] = this.spans
351
462
  const count: i32 = this.headerCount
@@ -357,7 +468,12 @@ export class Http1Parser {
357
468
  return -1
358
469
  }
359
470
 
360
- /** Whether the first field named `name` (any case) has exactly the value `value`. */
471
+ /**
472
+ * Whether the first field named `name` (any case) has exactly the value `value`.
473
+ * @param name the field name to look for, compared in any case
474
+ * @param value the value to compare with, byte for byte
475
+ * @returns true when that field exists and its trimmed value is `value`, with nothing allocated
476
+ */
361
477
  headerIs(name: string, value: string): boolean {
362
478
  const i: i32 = this.headerIndex(name)
363
479
  const spans: i32[] = this.spans
@@ -367,12 +483,20 @@ export class Http1Parser {
367
483
  return http1HeadIs(this.head, spans[4 * i + 2], spans[4 * i + 3], value, false)
368
484
  }
369
485
 
370
- /** Whether the method is exactly `text` (methods are case-sensitive, RFC 9110 §9.1). */
486
+ /**
487
+ * Whether the method is exactly `text` (methods are case-sensitive, RFC 9110 §9.1).
488
+ * @param text the method to compare with, such as `"GET"`
489
+ * @returns true when the request's method is those bytes in that case
490
+ */
371
491
  methodIs(text: string): boolean {
372
492
  return http1HeadIs(this.head, this.methodStart, this.methodEnd, text, false)
373
493
  }
374
494
 
375
- /** Whether the request target is exactly `text`. */
495
+ /**
496
+ * Whether the request target is exactly `text`.
497
+ * @param text the target to compare with, such as `"/index.html"`
498
+ * @returns true when the request's target is those bytes, compared without decoding
499
+ */
376
500
  targetIs(text: string): boolean {
377
501
  return http1HeadIs(this.head, this.targetStart, this.targetEnd, text, false)
378
502
  }
@@ -380,6 +504,9 @@ export class Http1Parser {
380
504
  /**
381
505
  * Whether the request asked to switch to exactly `protocol` (any case): one
382
506
  * `Upgrade` field holding that one token, with `upgrade` in `Connection`.
507
+ *
508
+ * @param protocol the protocol token to look for, such as `"websocket"`, compared in any case
509
+ * @returns true only when the request asks for that protocol and nothing else
383
510
  */
384
511
  upgradeIs(protocol: string): boolean {
385
512
  if (!this.upgrading) {
@@ -406,17 +533,25 @@ export class Http1Parser {
406
533
  * so its input and head buffers hold nothing it needs: a caller may lend
407
534
  * them out and leave empty arrays in `buf`, `body` and `head`, which the
408
535
  * next feed and head make or the caller puts back first.
536
+ *
537
+ * @returns true when the parser holds no bytes it still needs, so its buffers may be lent out
409
538
  */
410
539
  resting(): boolean {
411
540
  return this.state === HTTP1_S_REQUEST_LINE && this.end === this.start && !this.headHeld
412
541
  }
413
542
 
414
- /** How many bytes were fed and not consumed yet: what a caller bounds its reads by. */
543
+ /**
544
+ * How many bytes were fed and not consumed yet: what a caller bounds its reads by.
545
+ * @returns the count of unread input bytes, at `buf[start ..]`
546
+ */
415
547
  buffered(): i32 {
416
548
  return this.end - this.start
417
549
  }
418
550
 
419
- /** After `HTTP1_UPGRADE`: every byte fed past the request, as a fresh array. */
551
+ /**
552
+ * After `HTTP1_UPGRADE`: every byte fed past the request, as a fresh array.
553
+ * @returns a copy the caller owns of the bytes that belong to the next protocol, empty when none arrived yet
554
+ */
420
555
  upgradeBytes(): u8[] {
421
556
  let n: i32 = this.end - this.start
422
557
  if (n < 0) {
@@ -535,73 +670,6 @@ const http1StartRequest = (p: Http1Parser): void => {
535
670
  p.headHeld = false
536
671
  }
537
672
 
538
- /**
539
- * The bytes of `x` below 32 or equal to 127 — a control byte or DEL, which
540
- * `http1TakeLine` stops at — as their high bits: zero when there is none,
541
- * and else the lowest set bit is the first such byte's. Two of the bit
542
- * tricks of "Bit Twiddling Hacks" (S. E. Anderson, public domain): a byte
543
- * below `n` leaves its high bit set in `(x - n * ones) & ~x`, for `n` up to
544
- * 128, and `127` is the byte that is zero in `x ^ 127 * ones`. A byte from
545
- * 128 up has its high bit set in `x`, so `~x` clears it: obs-text never
546
- * matches. A borrow only runs up from a byte that matched, so the bits above
547
- * the first match may be false and the lowest is exact, in each mask and so
548
- * in their union. Each subtraction is masked to 64 bits because a `u64` is a
549
- * BigInt under Node, which does not wrap; natively the mask is a no-op.
550
- */
551
- const http1ControlBits = (x: u64): u64 => {
552
- const ones32: u64 = toU64(16843009)
553
- const ones: u64 = (ones32 << toU64(32)) | ones32
554
- const low32: u64 = 0xffffffff
555
- const all: u64 = (low32 << toU64(32)) | low32
556
- const high: u64 = ones * toU64(128)
557
- const below: u64 = (x - ones * toU64(32)) & all & ~x & high
558
- const y: u64 = x ^ (ones * toU64(127))
559
- const del: u64 = (y - ones) & all & ~y & high
560
- return below | del
561
- }
562
-
563
- /** Which byte of a word `bits`'s lowest set bit is in, 0 to 7; `bits` is not zero. */
564
- const http1FirstByte = (bits: u64): i32 => {
565
- const mask32: u64 = 0xffffffff
566
- const lo: u32 = toU32(bits & mask32)
567
- if (lo !== toU32(0)) {
568
- return (toI32(31) - toI32(Math.clz32(lo & (toU32(0) - lo)))) >> toI32(3)
569
- }
570
- const hi: u32 = toU32(bits >> toU64(32))
571
- return toI32(4) + ((toI32(31) - toI32(Math.clz32(hi & (toU32(0) - hi)))) >> toI32(3))
572
- }
573
-
574
- /**
575
- * Lanes `at` to `at + 7` of `v`, the first the lowest byte: eight bytes of a
576
- * sixteen-byte load as one word, which `opt` folds to a bitcast of the
577
- * vector's half. One load is one bounds check for sixteen bytes, where eight
578
- * reads of `buf[i + k]` would be eight.
579
- */
580
- const http1Lanes = (v: u8x16, at: i32): u64 => {
581
- if (at === 0) {
582
- return (
583
- toU64(extract(v, 0)) |
584
- (toU64(extract(v, 1)) << toU64(8)) |
585
- (toU64(extract(v, 2)) << toU64(16)) |
586
- (toU64(extract(v, 3)) << toU64(24)) |
587
- (toU64(extract(v, 4)) << toU64(32)) |
588
- (toU64(extract(v, 5)) << toU64(40)) |
589
- (toU64(extract(v, 6)) << toU64(48)) |
590
- (toU64(extract(v, 7)) << toU64(56))
591
- )
592
- }
593
- return (
594
- toU64(extract(v, 8)) |
595
- (toU64(extract(v, 9)) << toU64(8)) |
596
- (toU64(extract(v, 10)) << toU64(16)) |
597
- (toU64(extract(v, 11)) << toU64(24)) |
598
- (toU64(extract(v, 12)) << toU64(32)) |
599
- (toU64(extract(v, 13)) << toU64(40)) |
600
- (toU64(extract(v, 14)) << toU64(48)) |
601
- (toU64(extract(v, 15)) << toU64(56))
602
- )
603
- }
604
-
605
673
  /** `http1TakeLine`'s answer when a whole line is ready; never an event. */
606
674
  const HTTP1_LINE: i32 = -1
607
675
 
@@ -634,18 +702,12 @@ const http1TakeLine = (p: Http1Parser, limit: i32, overStatus: i32): i32 => {
634
702
  let found: boolean = false
635
703
  while (!found && i >= 0 && i <= stop - 16 && i <= toI32(buf.length) - 16) {
636
704
  const v: u8x16 = loadU8x16(buf, i)
637
- const low: u64 = http1ControlBits(http1Lanes(v, 0))
638
- if (low !== toU64(0)) {
639
- i += http1FirstByte(low)
705
+ const bad: i32 = bitmask(or(lt(v, splatU8x16(32)), eq(v, splatU8x16(127))))
706
+ if (bad !== 0) {
707
+ i += toI32(31) - toI32(Math.clz32(toU32(bad & -bad)))
640
708
  found = true
641
709
  } else {
642
- const high: u64 = http1ControlBits(http1Lanes(v, 8))
643
- if (high !== toU64(0)) {
644
- i += 8 + http1FirstByte(high)
645
- found = true
646
- } else {
647
- i += 16
648
- }
710
+ i += 16
649
711
  }
650
712
  }
651
713
  while (!found && i >= 0 && i < stop && i < toI32(buf.length) && buf[i] >= 32 && buf[i] !== 127) {
@@ -707,7 +769,11 @@ const HTTP1_TCHAR_32: i32 = 67071226
707
769
  const HTTP1_TCHAR_64: i32 = -939524098
708
770
  const HTTP1_TCHAR_96: i32 = 1476395007
709
771
 
710
- /** Whether `c` is a tchar of RFC 9110 §5.6.2, the bytes a token is made of. */
772
+ /**
773
+ * Whether `c` is a tchar of RFC 9110 §5.6.2, the bytes a token is made of.
774
+ * @param c the byte to test, as a number 0 to 255
775
+ * @returns true when `c` may stand in a method, a field name or another token
776
+ */
711
777
  export const http1IsTokenByte = (c: i32): boolean => {
712
778
  const low: u64 = toU64(toU32(HTTP1_TCHAR_32)) << toU64(32)
713
779
  const high: u64 = (toU64(toU32(HTTP1_TCHAR_96)) << toU64(32)) | toU64(toU32(HTTP1_TCHAR_64))
@@ -775,10 +841,21 @@ const http1LowerName = (head: u8[], k0: i32, stop: i32): i32 => {
775
841
  return k
776
842
  }
777
843
 
778
- /** Whether `c` may appear in a field value (RFC 9110 §5.5): HTAB, SP, VCHAR or obs-text. */
844
+ /**
845
+ * Whether `c` may appear in a field value (RFC 9110 §5.5): HTAB, SP, VCHAR or obs-text.
846
+ * @param c the byte to test, as a number 0 to 255
847
+ * @returns true when `c` cannot end the line early or smuggle a control byte into the value
848
+ */
779
849
  export const http1IsValueByte = (c: i32): boolean => c === 9 || (c >= 32 && c !== 127)
780
850
 
781
- /** `buf[from .. to)` as a string, each upper-case ASCII letter lowered when `lower`. */
851
+ /**
852
+ * `buf[from .. to)` as a string, each upper-case ASCII letter lowered when `lower`.
853
+ * @param buf the bytes to read from
854
+ * @param from where the text starts in `buf`
855
+ * @param to where the text ends in `buf`, exclusive
856
+ * @param lower true to lowercase ASCII letters, as a field name wants
857
+ * @returns a fresh string of those bytes, one character per byte
858
+ */
782
859
  export const http1Text = (buf: u8[], from: i32, to: i32, lower: boolean): string => {
783
860
  const parts: string[] = []
784
861
  for (let k: i32 = from; k >= 0 && k < to && k < toI32(buf.length); k += 1) {
@@ -791,10 +868,21 @@ export const http1Text = (buf: u8[], from: i32, to: i32, lower: boolean): string
791
868
  return parts.join("")
792
869
  }
793
870
 
794
- /** Whether `c` is optional whitespace, a space or a tab (OWS, RFC 9110 §5.6.3). */
871
+ /**
872
+ * Whether `c` is optional whitespace, a space or a tab (OWS, RFC 9110 §5.6.3).
873
+ * @param c the byte to test, as a number 0 to 255
874
+ * @returns true when `c` is whitespace a field value is trimmed of
875
+ */
795
876
  export const http1IsOws = (c: i32): boolean => c === 32 || c === 9
796
877
 
797
- /** Whether `text[a .. b)` is `word`, which is lowercase, in any ASCII case. */
878
+ /**
879
+ * Whether `text[a .. b)` is `word`, which is lowercase, in any ASCII case.
880
+ * @param text the string holding the candidate
881
+ * @param a where the candidate starts in `text`
882
+ * @param b where the candidate ends in `text`, exclusive
883
+ * @param word the lowercase word to compare with
884
+ * @returns true when the slice is `word` with any letters in either case
885
+ */
798
886
  export const http1SameWord = (text: string, a: i32, b: i32, word: string): boolean => {
799
887
  const n: i32 = toI32(word.length)
800
888
  if (a < 0 || b - a !== n || b > toI32(text.length)) {
@@ -1038,7 +1126,23 @@ const http1RequestLine = (p: Http1Parser): i32 => {
1038
1126
  const methodEnd: i32 = i
1039
1127
  i += 1
1040
1128
  const targetStart: i32 = i
1041
- while (i >= 0 && i < b && i < toI32(buf.length) && buf[i] > 32 && buf[i] < 127) {
1129
+ // The target is visible ASCII, 33 to 126: sixteen bytes at a time, as far
1130
+ // as the first that is not or the line's end, and a byte at a time for a
1131
+ // tail shorter than a vector before `buf`'s end.
1132
+ let found: boolean = false
1133
+ while (!found && i >= 0 && i < b && i <= toI32(buf.length) - 16) {
1134
+ const bad: i32 = bitmask(gt(subWrapping(loadU8x16(buf, i), splatU8x16(33)), splatU8x16(93)))
1135
+ if (bad !== 0) {
1136
+ i += toI32(31) - toI32(Math.clz32(toU32(bad & -bad)))
1137
+ found = true
1138
+ } else {
1139
+ i += 16
1140
+ }
1141
+ }
1142
+ if (i > b) {
1143
+ i = b
1144
+ }
1145
+ while (!found && i >= 0 && i < b && i < toI32(buf.length) && buf[i] > 32 && buf[i] < 127) {
1042
1146
  i += 1
1043
1147
  }
1044
1148
  if (i - targetStart > p.maxTarget) {
@@ -1286,7 +1390,11 @@ const http1ContentLength = (bytes: u8[], from: i32, to: i32, limit: i32): i32 =>
1286
1390
  return over ? -2 : value
1287
1391
  }
1288
1392
 
1289
- /** The value of the hex digit `c`, or -1. */
1393
+ /**
1394
+ * The value of the hex digit `c`, or -1.
1395
+ * @param c the byte to read, as a number 0 to 255
1396
+ * @returns 0 to 15 for a hex digit in either case, or -1 when `c` is not one
1397
+ */
1290
1398
  export const http1HexValue = (c: i32): i32 => {
1291
1399
  if (c >= 48 && c <= 57) {
1292
1400
  return c - 48
@@ -1496,7 +1604,11 @@ const http1BytesOf = (text: string): u8[] => {
1496
1604
  return out
1497
1605
  }
1498
1606
 
1499
- /** Whether every byte of `text` is a tchar, and there is at least one. */
1607
+ /**
1608
+ * Whether every byte of `text` is a tchar, and there is at least one.
1609
+ * @param text the candidate token, such as a field name a caller wants to send
1610
+ * @returns true when `text` is a valid token, false for an empty string
1611
+ */
1500
1612
  export const http1IsToken = (text: string): boolean => {
1501
1613
  const n: i32 = toI32(text.length)
1502
1614
  if (n === 0) {
@@ -1510,7 +1622,11 @@ export const http1IsToken = (text: string): boolean => {
1510
1622
  return true
1511
1623
  }
1512
1624
 
1513
- /** Whether every byte of `text` may stand in a field value or a reason phrase. */
1625
+ /**
1626
+ * Whether every byte of `text` may stand in a field value or a reason phrase.
1627
+ * @param text the candidate value or reason phrase
1628
+ * @returns true when nothing in `text` could end the line early; an empty string passes
1629
+ */
1514
1630
  export const http1IsFieldText = (text: string): boolean => {
1515
1631
  const n: i32 = toI32(text.length)
1516
1632
  for (let k: i32 = 0; k < n; k += 1) {
@@ -1535,6 +1651,13 @@ export const http1IsFieldText = (text: string): boolean => {
1535
1651
  * caller's fields, since the framing is decided here; a 1xx or 204 response
1536
1652
  * with a body length (RFC 9110 §15.2, §15.3.5); and any other `bodyLength`
1537
1653
  * below zero.
1654
+ *
1655
+ * @param status the status code, 100 to 599
1656
+ * @param reason the reason phrase after the code, which may be empty
1657
+ * @param names the caller's field names, one per field, beside `values`
1658
+ * @param values the caller's field values, `values[i]` for `names[i]`
1659
+ * @param bodyLength the body's length in bytes, or `HTTP1_CHUNKED` or `HTTP1_NO_BODY`
1660
+ * @returns the head's bytes in a fresh array the caller owns, or `null` when the head is refused
1538
1661
  */
1539
1662
  export const http1ResponseHead = (
1540
1663
  status: i32,
@@ -1596,6 +1719,13 @@ const http1HexDigits = (len: i32): i32 => {
1596
1719
  * CRLF — and answers where it ends, or `HTTP1_NO_ROOM` when it does not fit,
1597
1720
  * with `out` untouched. An empty window writes nothing and answers `at`,
1598
1721
  * because a chunk of size zero ends the body. A window outside `data` panics.
1722
+ *
1723
+ * @param out the caller's buffer the chunk is written into
1724
+ * @param at where in `out` the chunk starts
1725
+ * @param data the body bytes to send
1726
+ * @param off where in `data` the chunk's bytes start
1727
+ * @param len how many bytes of `data` the chunk carries
1728
+ * @returns the offset in `out` just past the chunk, or `HTTP1_NO_ROOM` when it does not fit
1599
1729
  */
1600
1730
  export const http1WriteChunk = (out: u8[], at: i32, data: readonly u8[], off: i32, len: i32): i32 => {
1601
1731
  const size: i32 = toI32(data.length)
@@ -1633,6 +1763,11 @@ export const http1WriteChunk = (out: u8[], at: i32, data: readonly u8[], off: i3
1633
1763
  * An empty window answers an empty array, because a chunk of size zero is the
1634
1764
  * end of the body and only `http1LastChunk` writes that. A window outside
1635
1765
  * `data` panics.
1766
+ *
1767
+ * @param data the body bytes to send
1768
+ * @param off where in `data` the chunk's bytes start
1769
+ * @param len how many bytes of `data` the chunk carries
1770
+ * @returns the chunk's bytes, framing included, in a fresh array the caller owns
1636
1771
  */
1637
1772
  export const http1Chunk = (data: u8[], off: i32, len: i32): u8[] => {
1638
1773
  const size: i32 = toI32(data.length)
@@ -1647,12 +1782,19 @@ export const http1Chunk = (data: u8[], off: i32, len: i32): u8[] => {
1647
1782
  return out
1648
1783
  }
1649
1784
 
1650
- /** The last chunk and the empty trailer section that end a chunked body: `0\r\n\r\n`. */
1785
+ /**
1786
+ * The last chunk and the empty trailer section that end a chunked body: `0\r\n\r\n`.
1787
+ * @returns those five bytes in a fresh array the caller owns
1788
+ */
1651
1789
  export const http1LastChunk = (): u8[] => [48, 13, 10, 13, 10]
1652
1790
 
1653
1791
  /**
1654
1792
  * Writes `http1LastChunk`'s five bytes into `out` at `at` and answers where
1655
1793
  * they end, or `HTTP1_NO_ROOM` with `out` untouched.
1794
+ *
1795
+ * @param out the caller's buffer the last chunk is written into
1796
+ * @param at where in `out` the last chunk starts
1797
+ * @returns the offset in `out` just past the five bytes, or `HTTP1_NO_ROOM` when they do not fit
1656
1798
  */
1657
1799
  export const http1WriteLastChunk = (out: u8[], at: i32): i32 => {
1658
1800
  if (at < 0 || at > toI32(out.length) - 5) {
@@ -1711,7 +1853,12 @@ const http1BytesAreFieldText = (bytes: u8[]): boolean => {
1711
1853
  return k === n || http1LanesAreFieldText(loadU8x16(bytes, n - 16))
1712
1854
  }
1713
1855
 
1714
- /** Whether the octets of `bytes` spell `word`, which is lowercase, in any ASCII case: a field name compared as RFC 9110 §5.1 says. */
1856
+ /**
1857
+ * Whether the octets of `bytes` spell `word`, which is lowercase, in any ASCII case: a field name compared as RFC 9110 §5.1 says.
1858
+ * @param bytes the field name as octets, as `nish/net/http-fields` holds it
1859
+ * @param word the lowercase name to compare with
1860
+ * @returns true when `bytes` is `word` with any letters in either case
1861
+ */
1715
1862
  export const http1BytesAre = (bytes: u8[], word: string): boolean =>
1716
1863
  http1HeadIs(bytes, 0, toI32(bytes.length), word, true)
1717
1864
 
@@ -1735,7 +1882,13 @@ const http1DecimalDigits = (n: i32): i32 => {
1735
1882
  return digits
1736
1883
  }
1737
1884
 
1738
- /** Writes `text` at `out[at ..)`, which the caller has made sure holds it, and answers where it ends. */
1885
+ /**
1886
+ * Writes `text` at `out[at ..)`, which the caller has made sure holds it, and answers where it ends.
1887
+ * @param out the caller's buffer
1888
+ * @param at where in `out` the text starts
1889
+ * @param text the text to write, one byte per character
1890
+ * @returns the offset in `out` just past the text
1891
+ */
1739
1892
  export const http1PutText = (out: u8[], at: i32, text: string): i32 => {
1740
1893
  const n: i32 = toI32(text.length)
1741
1894
  if (at >= 0 && at <= toI32(out.length) - n) {
@@ -1844,19 +1997,31 @@ const http1PutDecimal = (out: u8[], at: i32, n: i32): i32 => {
1844
1997
  }
1845
1998
 
1846
1999
  /**
1847
- * `http1ResponseHead`'s status line and header section, written into `out`
1848
- * at `at` with nothing allocated: field names and values are octets (as
1849
- * `nish/net/http-fields` holds them), and `connection`, when not empty, is
1850
- * sent as a `Connection` field after the caller's. `date`, when not empty, is
1851
- * sent as a `Date` field between the caller's fields and the framing ones
1852
- * (RFC 9110 §6.6.1), on a final status only, and not when the caller's fields
1853
- * already hold a `date` in any case: the caller's wins, so a response carries
1854
- * one. Answers where the head ends; `HTTP1_REFUSED` for everything
2000
+ * `http1ResponseHead`'s status line and header section, written into `out` at
2001
+ * `at` with nothing allocated: field names and values are octets (as
2002
+ * `nish/net/http-fields` holds them), and `connection`, when not empty, is sent
2003
+ * as a `Connection` field after the caller's. `date`, when not empty, is sent
2004
+ * as a `Date` field between the caller's fields and the framing ones (RFC 9110
2005
+ * §6.6.1), on a final status only, and not when the caller's fields already
2006
+ * hold a `date` in any case: the caller's wins, so a response carries one.
2007
+ *
2008
+ * Answers where the head ends; `HTTP1_REFUSED` for everything
1855
2009
  * `http1ResponseHead` refuses, a `Connection` among the caller's fields when
1856
2010
  * `connection` is given, a `connection` that is not field text, and a `date`
1857
2011
  * that is not field text on a final status, the only one it is sent on;
1858
2012
  * `HTTP1_NO_ROOM` when it is well formed but does not fit, the `Date` line
1859
2013
  * counted when it is sent. Either way `out` is untouched.
2014
+ *
2015
+ * @param out the caller's buffer the head is written into
2016
+ * @param at where in `out` the head starts
2017
+ * @param status the status code, 100 to 599
2018
+ * @param reason the reason phrase after the code, which may be empty
2019
+ * @param names the caller's field names as octets, beside `values`
2020
+ * @param values the caller's field values as octets, `values[i]` for `names[i]`
2021
+ * @param bodyLength the body's length in bytes, or `HTTP1_CHUNKED` or `HTTP1_NO_BODY`
2022
+ * @param connection the `Connection` field's value to send, such as `"close"`, or empty for none
2023
+ * @param date the IMF-fixdate bytes to send as `Date`, or empty for none
2024
+ * @returns the offset in `out` just past the head, `HTTP1_REFUSED` for a head it will not write, or `HTTP1_NO_ROOM`
1860
2025
  */
1861
2026
  export const http1WriteResponseHead = (
1862
2027
  out: u8[],