@amritk/nish-aarch64-linux 0.44.0 → 0.46.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 +3 -1
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +18 -16
  4. package/package.json +1 -1
  5. package/runtime/core/numbers.mjs +3 -1
  6. package/runtime/core/strings.mjs +90 -38
  7. package/runtime/core.d.ts +8 -0
  8. package/runtime/core.mjs +16 -0
  9. package/runtime/nish.d.ts +88 -5
  10. package/runtime/nish.h +10 -0
  11. package/runtime/nish.mjs +5 -0
  12. package/runtime/runtime-host.c +15 -0
  13. package/runtime/runtime-net.c +223 -4
  14. package/runtime/runtime-os.c +15 -1
  15. package/runtime/runtime-uring.c +291 -31
  16. package/runtime/sqlite/nish-sqlite.c +301 -128
  17. package/std/README.md +15 -15
  18. package/std/array.ts +10 -2
  19. package/std/bytes.ts +239 -26
  20. package/std/cli-args.ts +1072 -606
  21. package/std/cli-flags.ts +180 -98
  22. package/std/cli.ts +21 -13
  23. package/std/collections.ts +245 -127
  24. package/std/compress/checksum.ts +32 -37
  25. package/std/compress/deflate.ts +1102 -542
  26. package/std/compress/gzip.ts +290 -145
  27. package/std/compress/zlib.ts +57 -21
  28. package/std/crypto/aes.ts +153 -75
  29. package/std/crypto/base64url.ts +84 -61
  30. package/std/crypto/bcrypt-batch.ts +164 -90
  31. package/std/crypto/bcrypt.ts +185 -107
  32. package/std/crypto/chacha20poly1305.ts +84 -68
  33. package/std/crypto/ct.ts +9 -7
  34. package/std/crypto/ed25519.ts +64 -27
  35. package/std/crypto/hkdf.ts +105 -63
  36. package/std/crypto/hmac.ts +95 -89
  37. package/std/crypto/jwt.ts +248 -126
  38. package/std/crypto/p256.ts +758 -444
  39. package/std/crypto/p384.ts +20 -13
  40. package/std/crypto/pki-roots.ts +95 -38
  41. package/std/crypto/pki.ts +164 -60
  42. package/std/crypto/rsa.ts +266 -223
  43. package/std/crypto/scram.ts +3 -4
  44. package/std/crypto/sha1.ts +107 -65
  45. package/std/crypto/sha256.ts +502 -422
  46. package/std/crypto/sha512.ts +204 -135
  47. package/std/crypto/x509-random.ts +9 -3
  48. package/std/crypto/x509.ts +331 -171
  49. package/std/json-equal.ts +35 -19
  50. package/std/json-format.ts +215 -108
  51. package/std/json-number.ts +126 -72
  52. package/std/json-pointer.ts +42 -13
  53. package/std/json-tokenizer.ts +498 -307
  54. package/std/json-typed.ts +115 -36
  55. package/std/json-value.ts +341 -152
  56. package/std/json.ts +300 -158
  57. package/std/net/buffer-pool.ts +17 -7
  58. package/std/net/dns-config.ts +427 -236
  59. package/std/net/dns-message.ts +114 -47
  60. package/std/net/dns-resolver.ts +736 -237
  61. package/std/net/dns.ts +205 -52
  62. package/std/net/fetch.ts +320 -184
  63. package/std/net/grpc.ts +948 -0
  64. package/std/net/handler.ts +1315 -222
  65. package/std/net/hpack.ts +395 -180
  66. package/std/net/http-compress.ts +502 -65
  67. package/std/net/http-fields.ts +85 -49
  68. package/std/net/http-server.ts +1764 -602
  69. package/std/net/http1-client-codec.ts +372 -161
  70. package/std/net/http1-codec.ts +665 -331
  71. package/std/net/http1-server.ts +660 -278
  72. package/std/net/http2-cleartext.ts +165 -95
  73. package/std/net/http2-frame.ts +232 -89
  74. package/std/net/http2-tls.ts +42 -12
  75. package/std/net/http2.ts +327 -101
  76. package/std/net/http3-frame.ts +68 -33
  77. package/std/net/http3-server.ts +647 -192
  78. package/std/net/http3.ts +667 -189
  79. package/std/net/oidc.ts +63 -24
  80. package/std/net/outbound.ts +207 -142
  81. package/std/net/postgres-async.ts +1165 -443
  82. package/std/net/postgres-carrier.ts +575 -372
  83. package/std/net/postgres.ts +1459 -559
  84. package/std/net/prefork.ts +254 -98
  85. package/std/net/protobuf.ts +1017 -0
  86. package/std/net/qpack.ts +143 -92
  87. package/std/net/quic-conn-ack.ts +50 -18
  88. package/std/net/quic-conn-cid.ts +66 -26
  89. package/std/net/quic-conn-params.ts +13 -7
  90. package/std/net/quic-datagram.ts +90 -27
  91. package/std/net/quic-frame.ts +225 -124
  92. package/std/net/quic-listener.ts +197 -89
  93. package/std/net/quic-packet.ts +289 -163
  94. package/std/net/quic-recovery.ts +279 -128
  95. package/std/net/quic-stream.ts +466 -167
  96. package/std/net/quic.ts +568 -257
  97. package/std/net/router.ts +450 -99
  98. package/std/net/serve.ts +651 -232
  99. package/std/net/static.ts +1458 -0
  100. package/std/net/tls/client.ts +140 -45
  101. package/std/net/tls/codec.ts +510 -260
  102. package/std/net/tls/record-client.ts +268 -76
  103. package/std/net/tls/record-server.ts +236 -79
  104. package/std/net/tls/record.ts +173 -47
  105. package/std/net/tls/schedule.ts +58 -39
  106. package/std/net/tls/ticket.ts +295 -103
  107. package/std/net/tls/verify.ts +1 -1
  108. package/std/net/tls-tcp.ts +218 -75
  109. package/std/net/tls.ts +260 -152
  110. package/std/net/upstream.ts +283 -86
  111. package/std/net/url.ts +277 -102
  112. package/std/net/websocket.ts +370 -186
  113. package/std/net/webtransport.ts +755 -436
  114. package/std/regex.ts +1659 -829
  115. package/std/sqlite.ts +171 -0
  116. package/std/sync/ring.ts +320 -156
  117. package/std/sync/shm.ts +53 -6
  118. package/std/text.ts +205 -124
  119. package/std/threads.ts +22 -2
@@ -76,6 +76,7 @@
76
76
  */
77
77
  import { base64StdPadDecodedLength, base64StdPadEncode, base64StdPadEncodeInto } from "nish/crypto/base64url"
78
78
  import { SHA1_SIZE, Sha1 } from "nish/crypto/sha1"
79
+ import { tryCopyBytes } from "nish/bytes"
79
80
  import {
80
81
  HTTP1_NO_ROOM,
81
82
  HTTP1_REFUSED,
@@ -178,6 +179,35 @@ const websocketIsKnownOpcode = (opcode: i32): boolean =>
178
179
  */
179
180
  const WS_CONTINUE: i32 = -1
180
181
 
182
+ /**
183
+ * `a + b`, worked in `i64` and held to `i32`. Every caller adds an offset
184
+ * and a length that lie inside one array, so the sum is an index into it or
185
+ * just past it and always fits.
186
+ */
187
+ const websocketAdd = (a: i32, b: i32): i32 => toI32(toI64(a) + toI64(b))
188
+
189
+ /** `a - b`, worked as `websocketAdd` is, for offsets inside one array. */
190
+ const websocketSub = (a: i32, b: i32): i32 => toI32(toI64(a) - toI64(b))
191
+
192
+ /**
193
+ * A zeroed array of `n` bytes, or of none when `n` is below zero. Up to
194
+ * 65535 bytes is one allocation; past that the array grows a byte at a time
195
+ * from a 65535-byte start, which an `i32` length cannot make fail.
196
+ */
197
+ const websocketBytes = (n: i32): u8[] => {
198
+ if (n < 0) {
199
+ return []
200
+ }
201
+ if (n <= 65535) {
202
+ return new Array<u8>(toU16(n))
203
+ }
204
+ const out: u8[] = new Array<u8>(65535)
205
+ for (let k: i32 = 65535; k < n; k += 1) {
206
+ out.push(0)
207
+ }
208
+ return out
209
+ }
210
+
181
211
  /**
182
212
  * Runs `buf[off .. off + len)` through the check `state` holds. Answers false
183
213
  * at the first byte that cannot continue well-formed UTF-8 (RFC 3629 §4: no
@@ -189,7 +219,8 @@ const websocketUtf8Run = (state: WsUtf8, buf: u8[], off: i32, len: i32): boolean
189
219
  let need: i32 = state.need
190
220
  let lo: i32 = state.lo
191
221
  let hi: i32 = state.hi
192
- for (let k: i32 = off; k >= 0 && k < off + len && k < toI32(buf.length); k += 1) {
222
+ const stop: i64 = toI64(off) + toI64(len)
223
+ for (let k: i32 = off; k >= 0 && toI64(k) < stop && k < toI32(buf.length); k += 1) {
193
224
  const b: i32 = toI32(buf[k])
194
225
  if (need > 0) {
195
226
  if (b < lo || b > hi) {
@@ -327,22 +358,12 @@ export class WsDecoder {
327
358
  if (this.state !== WS_S_FRAMES) {
328
359
  return
329
360
  }
330
- websocketReserve(this, len)
331
- const buf: u8[] = this.buf
332
- const at: i32 = this.end
333
- // A whole array is one `set`; `set` takes no window of its source.
334
- if (off === 0 && len === size) {
335
- buf.set(data, at)
336
- this.end = at + len
361
+ // More than 2^31 - 1 bytes waiting is more than any message.
362
+ if (!websocketReserve(this, len) || !tryCopyBytes(this.buf, this.end, data, off, len)) {
363
+ websocketFail(this, WS_CLOSE_TOO_BIG, "more than 2^31 - 1 bytes of input waiting")
337
364
  return
338
365
  }
339
- for (let k: i32 = 0; k < len; k += 1) {
340
- const to: i32 = at + k
341
- if (to >= 0 && to < toI32(buf.length)) {
342
- buf[to] = data[off + k]
343
- }
344
- }
345
- this.end = at + len
366
+ this.end = websocketAdd(this.end, len)
346
367
  }
347
368
 
348
369
  /**
@@ -374,29 +395,48 @@ export class WsDecoder {
374
395
  }
375
396
  }
376
397
 
377
- /** Makes room for `len` more input bytes at `d.end`, moving the live bytes down first. */
378
- const websocketReserve = (d: WsDecoder, len: i32): void => {
398
+ /**
399
+ * Makes room for `len` more input bytes at `d.end`, moving the live bytes
400
+ * down first. Answers false, changing nothing, when the live bytes and `len`
401
+ * together pass 2^31 - 1.
402
+ */
403
+ const websocketReserve = (d: WsDecoder, len: i32): boolean => {
379
404
  const old: u8[] = d.buf
380
405
  const capacity: i32 = toI32(old.length)
381
- if (len <= capacity - d.end) {
382
- return
406
+ const start: i32 = d.start
407
+ const end: i32 = d.end
408
+ if (len < 0 || start < 0 || end < 0 || end < start) {
409
+ return false
410
+ }
411
+ if (len <= capacity - end) {
412
+ return true
413
+ }
414
+ const live: i32 = end - start
415
+ if (toI64(len) > toI64(2147483647) - toI64(live)) {
416
+ return false
383
417
  }
384
- const live: i32 = d.end - d.start
418
+ const need: i32 = websocketAdd(live, len)
419
+ // Room enough moves the live bytes down in place; otherwise the buffer
420
+ // at least doubles.
385
421
  let size: i32 = capacity
386
- while (size - live < len) {
387
- if (size > 1073741823) {
388
- panic("WsDecoder: more than 2^31 - 1 bytes buffered")
422
+ if (need > capacity) {
423
+ if (capacity > 1073741823) {
424
+ size = 2147483647
425
+ } else {
426
+ size = capacity * 2
427
+ }
428
+ if (size < need) {
429
+ size = need
389
430
  }
390
- size = size * 2
391
431
  }
392
- const buf: u8[] = size === capacity ? old : new Array<u8>(size)
393
- const from: i32 = d.start
394
- for (let k: i32 = 0; k < live && k < toI32(buf.length); k += 1) {
395
- buf[k] = old[from + k]
432
+ const buf: u8[] = size === capacity ? old : websocketBytes(size)
433
+ if (!tryCopyBytes(buf, 0, old, start, live)) {
434
+ return false
396
435
  }
397
436
  d.buf = buf
398
437
  d.start = 0
399
438
  d.end = live
439
+ return true
400
440
  }
401
441
 
402
442
  /** Refuses the input; the connection is to be closed with `code`. Answers `WS_ERROR`. */
@@ -413,6 +453,7 @@ const websocketFail = (d: WsDecoder, code: i32, reason: string): i32 => {
413
453
  * unmasking it with the key at `buf[maskAt .. maskAt + 4)` when `masked`
414
454
  * (§5.3: byte i is XORed with key byte i mod 4). `phase` is how far into the
415
455
  * payload `from` is, so a payload can be unmasked a piece at a time.
456
+ * Answers false, writing nothing, when a window lies outside its array.
416
457
  */
417
458
  const websocketUnmaskInto = (
418
459
  buf: u8[],
@@ -423,24 +464,37 @@ const websocketUnmaskInto = (
423
464
  phase: i32,
424
465
  to: u8[],
425
466
  at: i32
426
- ): void => {
467
+ ): boolean => {
468
+ if (!masked) {
469
+ return tryCopyBytes(to, at, buf, from, len)
470
+ }
471
+ const bufLength: i32 = toI32(buf.length)
472
+ const toLength: i32 = toI32(to.length)
473
+ if (len < 0 || from < 0 || at < 0 || from > bufLength - len || at > toLength - len) {
474
+ return false
475
+ }
476
+ if (maskAt < 0 || maskAt >= bufLength - 3) {
477
+ return false
478
+ }
427
479
  // The four key bytes in one word, high byte first, rotated a byte left
428
480
  // after each payload byte, so its top byte is always key byte k mod 4.
429
- let key: u32 = 0
430
- if (masked) {
431
- key =
432
- (toU32(buf[maskAt]) << 24) |
433
- (toU32(buf[maskAt + 1]) << 16) |
434
- (toU32(buf[maskAt + 2]) << 8) |
435
- toU32(buf[maskAt + 3])
436
- }
481
+ let key: u32 =
482
+ (toU32(buf[maskAt]) << 24) |
483
+ (toU32(buf[maskAt + 1]) << 16) |
484
+ (toU32(buf[maskAt + 2]) << 8) |
485
+ toU32(buf[maskAt + 3])
437
486
  for (let r: i32 = 0; r < (phase & 3); r += 1) {
438
487
  key = (key << 8) | (key >>> 24)
439
488
  }
440
- for (let k: i32 = 0; k < len; k += 1) {
441
- to[at + k] = buf[from + k] ^ toU8(key >>> 24)
489
+ let s: i32 = from
490
+ let t: i32 = at
491
+ for (let k: i32 = 0; k < len && s >= 0 && s < bufLength && t >= 0 && t < toLength; k += 1) {
492
+ to[t] = buf[s] ^ toU8(key >>> 24)
442
493
  key = (key << 8) | (key >>> 24)
494
+ s += 1
495
+ t += 1
443
496
  }
497
+ return true
444
498
  }
445
499
 
446
500
  /**
@@ -449,18 +503,20 @@ const websocketUnmaskInto = (
449
503
  */
450
504
  const websocketMessageRoom = (d: WsDecoder, need: i32): void => {
451
505
  const old: u8[] = d.message
452
- let size: i32 = toI32(old.length)
453
- if (need <= size) {
506
+ const oldLength: i32 = toI32(old.length)
507
+ if (need <= oldLength) {
454
508
  return
455
509
  }
456
- while (size < need) {
457
- size = size > 1073741823 ? 2147483647 : size * 2
510
+ let size: i64 = toI64(oldLength) * toI64(2)
511
+ if (size < toI64(need)) {
512
+ size = toI64(need)
458
513
  }
459
- const grown: u8[] = new Array<u8>(size)
460
- const live: i32 = d.messageLen + d.frameChecked
461
- for (let k: i32 = 0; k < live && k < toI32(grown.length) && k < toI32(old.length); k += 1) {
462
- grown[k] = old[k]
514
+ if (size > toI64(2147483647)) {
515
+ size = toI64(2147483647)
463
516
  }
517
+ const grown: u8[] = websocketBytes(toI32(size))
518
+ const live: i32 = websocketAdd(d.messageLen, d.frameChecked)
519
+ tryCopyBytes(grown, 0, old, 0, live < oldLength ? live : oldLength)
464
520
  d.message = grown
465
521
  }
466
522
 
@@ -480,6 +536,10 @@ const websocketReadClose = (d: WsDecoder, len: i32): i32 => {
480
536
  if (len === 1) {
481
537
  return websocketFail(d, WS_CLOSE_PROTOCOL_ERROR, "a close frame with a one-byte payload")
482
538
  }
539
+ // `control` holds 125 bytes and the frame's payload was copied into it.
540
+ if (len < 2 || toI32(payload.length) < 2) {
541
+ return websocketFail(d, WS_CLOSE_INTERNAL_ERROR, "a close frame payload that was not kept")
542
+ }
483
543
  const code: i32 = (toI32(payload[0]) << 8) | toI32(payload[1])
484
544
  if (!websocketIsSendableCode(code)) {
485
545
  return websocketFail(d, WS_CLOSE_PROTOCOL_ERROR, "a close status code that may not be sent")
@@ -507,8 +567,13 @@ const websocketReadClose = (d: WsDecoder, len: i32): i32 => {
507
567
  const websocketFrameEvent = (d: WsDecoder): i32 => {
508
568
  const buf: u8[] = d.buf
509
569
  const at: i32 = d.start
510
- const ready: i32 = d.end - at
511
- if (ready < 2 || at < 0 || at + 1 >= toI32(buf.length)) {
570
+ const end: i32 = d.end
571
+ const bufLength: i32 = toI32(buf.length)
572
+ if (at < 0 || end < 0 || end < at || end > bufLength || at >= bufLength - 1) {
573
+ return WS_NEED_MORE
574
+ }
575
+ const ready: i32 = end - at
576
+ if (ready < 2) {
512
577
  return WS_NEED_MORE
513
578
  }
514
579
  const b0: i32 = toI32(buf[at])
@@ -524,10 +589,16 @@ const websocketFrameEvent = (d: WsDecoder): i32 => {
524
589
  } else if (short === 127) {
525
590
  extended = 8
526
591
  }
527
- const headerLength: i32 = 2 + extended + (masked ? 4 : 0)
592
+ let keyLength: i32 = 0
593
+ if (masked) {
594
+ keyLength = 4
595
+ }
596
+ const headerLength: i32 = 2 + extended + keyLength
528
597
  if (ready < headerLength) {
529
598
  return WS_NEED_MORE
530
599
  }
600
+ // What of the payload has arrived; at least zero, past the guard above.
601
+ const body: i32 = ready - headerLength
531
602
  if ((b0 & 0x70) !== 0) {
532
603
  return websocketFail(d, WS_CLOSE_PROTOCOL_ERROR, "a reserved bit set with no extension negotiated")
533
604
  }
@@ -543,22 +614,27 @@ const websocketFrameEvent = (d: WsDecoder): i32 => {
543
614
  )
544
615
  }
545
616
  // The payload length (§5.2), held to `i32` and refused before it is used.
617
+ // `ready` covers the header, so its bytes lie inside `buf`; the checks on
618
+ // `at` say so in the form the index proofs read.
546
619
  let length: i32 = short
547
620
  if (extended === 2) {
621
+ if (at >= bufLength - 3) {
622
+ return WS_NEED_MORE
623
+ }
548
624
  length = (toI32(buf[at + 2]) << 8) | toI32(buf[at + 3])
549
625
  if (length < 126) {
550
626
  return websocketFail(d, WS_CLOSE_PROTOCOL_ERROR, "a 16-bit length under 126")
551
627
  }
552
628
  } else if (extended === 8) {
629
+ if (at >= bufLength - 9) {
630
+ return WS_NEED_MORE
631
+ }
553
632
  if ((toI32(buf[at + 2]) & 0x80) !== 0) {
554
633
  return websocketFail(d, WS_CLOSE_PROTOCOL_ERROR, "a 64-bit length with its top bit set")
555
634
  }
556
635
  // Anything in the high four bytes, or a top bit in the low four, is more
557
636
  // than an `i32` holds and so more than any `maxMessage`.
558
- let high: i32 = 0
559
- for (let k: i32 = 2; k < 6; k += 1) {
560
- high = high | toI32(buf[at + k])
561
- }
637
+ const high: i32 = toI32(buf[at + 2]) | toI32(buf[at + 3]) | toI32(buf[at + 4]) | toI32(buf[at + 5])
562
638
  if (high !== 0 || (toI32(buf[at + 6]) & 0x80) !== 0) {
563
639
  return websocketFail(d, WS_CLOSE_TOO_BIG, "a frame longer than 2^31 - 1 bytes")
564
640
  }
@@ -582,19 +658,19 @@ const websocketFrameEvent = (d: WsDecoder): i32 => {
582
658
  if (opcode !== WS_OP_CONTINUATION && d.messageOpcode !== 0) {
583
659
  return websocketFail(d, WS_CLOSE_PROTOCOL_ERROR, "a new message before the last one ended")
584
660
  }
585
- if (length > d.maxMessage - d.messageLen) {
661
+ if (toI64(length) > toI64(d.maxMessage) - toI64(d.messageLen)) {
586
662
  return websocketFail(d, WS_CLOSE_TOO_BIG, "a message longer than the limit")
587
663
  }
588
664
  }
589
- const payloadAt: i32 = at + headerLength
590
- const maskAt: i32 = payloadAt - 4
665
+ const payloadAt: i32 = websocketAdd(at, headerLength)
666
+ const maskAt: i32 = websocketAdd(at, headerLength - 4)
591
667
  const text: boolean =
592
668
  opcode === WS_OP_TEXT || (opcode === WS_OP_CONTINUATION && d.messageOpcode === WS_OP_TEXT)
593
669
  if (text) {
594
670
  // A text frame is unmasked and checked as its bytes arrive, so a byte
595
671
  // that cannot continue UTF-8 fails the connection at once (§8.1), not
596
672
  // when the frame or the message ends.
597
- const arrived: i32 = ready - headerLength < length ? ready - headerLength : length
673
+ const arrived: i32 = body < length ? body : length
598
674
  const from: i32 = d.messageLen
599
675
  const done: i32 = d.frameChecked
600
676
  if (done === 0 && opcode === WS_OP_TEXT) {
@@ -605,22 +681,39 @@ const websocketFrameEvent = (d: WsDecoder): i32 => {
605
681
  if (arrived > done) {
606
682
  // Room for the bytes that have arrived, not the length the header
607
683
  // declares: a peer that declares much and sends little costs little.
608
- websocketMessageRoom(d, from + arrived)
609
- websocketUnmaskInto(buf, payloadAt + done, arrived - done, masked, maskAt, done, d.message, from + done)
684
+ const fresh: i32 = websocketSub(arrived, done)
685
+ const freshAt: i32 = websocketAdd(from, done)
686
+ websocketMessageRoom(d, websocketAdd(from, arrived))
687
+ if (
688
+ !websocketUnmaskInto(
689
+ buf,
690
+ websocketAdd(payloadAt, done),
691
+ fresh,
692
+ masked,
693
+ maskAt,
694
+ done,
695
+ d.message,
696
+ freshAt
697
+ )
698
+ ) {
699
+ return websocketFail(d, WS_CLOSE_INTERNAL_ERROR, "a frame outside the decoder's buffers")
700
+ }
610
701
  d.frameChecked = arrived
611
- if (!websocketUtf8Run(d.utf8, d.message, from + done, arrived - done)) {
702
+ if (!websocketUtf8Run(d.utf8, d.message, freshAt, fresh)) {
612
703
  return websocketFail(d, WS_CLOSE_INVALID_DATA, "a text message that is not UTF-8")
613
704
  }
614
705
  }
615
706
  }
616
- if (ready - headerLength < length) {
707
+ if (body < length) {
617
708
  return WS_NEED_MORE
618
709
  }
619
710
  // The whole frame is here: take it out of the input.
620
- d.start = payloadAt + length
711
+ d.start = websocketAdd(payloadAt, length)
621
712
  d.frameChecked = 0
622
713
  if (control) {
623
- websocketUnmaskInto(buf, payloadAt, length, masked, maskAt, 0, d.control, 0)
714
+ if (!websocketUnmaskInto(buf, payloadAt, length, masked, maskAt, 0, d.control, 0)) {
715
+ return websocketFail(d, WS_CLOSE_INTERNAL_ERROR, "a frame outside the decoder's buffers")
716
+ }
624
717
  d.opcode = opcode
625
718
  d.data = d.control
626
719
  d.dataLen = length
@@ -633,12 +726,15 @@ const websocketFrameEvent = (d: WsDecoder): i32 => {
633
726
  d.messageOpcode = opcode
634
727
  }
635
728
  const from: i32 = d.messageLen
729
+ const total: i32 = websocketAdd(from, length)
636
730
  if (!text) {
637
731
  // A text frame's payload is already in `message`, unmasked and checked.
638
- websocketMessageRoom(d, from + length)
639
- websocketUnmaskInto(buf, payloadAt, length, masked, maskAt, 0, d.message, from)
732
+ websocketMessageRoom(d, total)
733
+ if (!websocketUnmaskInto(buf, payloadAt, length, masked, maskAt, 0, d.message, from)) {
734
+ return websocketFail(d, WS_CLOSE_INTERNAL_ERROR, "a frame outside the decoder's buffers")
735
+ }
640
736
  }
641
- d.messageLen = from + length
737
+ d.messageLen = total
642
738
  if (!fin) {
643
739
  return WS_CONTINUE
644
740
  }
@@ -682,16 +778,28 @@ export const WS_REFUSED: i32 = -2
682
778
  *
683
779
  * @param len The payload's length in bytes, at least 0.
684
780
  * @param masked Whether the frame carries a 4-byte mask key, as every frame a client sends does.
685
- * @returns The frame's size in bytes: 2 to 14 bytes of header, then `len`.
781
+ * @returns The frame's size in bytes: 2 to 14 bytes of header, then `len`; `WS_REFUSED` for a negative `len` or one past 2^31 - 15, whose frame `websocketWriteFrame` refuses.
686
782
  */
687
783
  export const websocketFrameSize = (len: i32, masked: boolean): i32 => {
688
- let extended: i32 = 8
784
+ if (len < 0 || len > WS_MAX_FRAME_PAYLOAD) {
785
+ return WS_REFUSED
786
+ }
787
+ return websocketAdd(websocketHeaderLength(len, masked), len)
788
+ }
789
+
790
+ /** A frame's header length for a payload of `len` bytes, 0 to `WS_MAX_FRAME_PAYLOAD`: 2 to 14. */
791
+ const websocketHeaderLength = (len: i32, masked: boolean): i32 => {
792
+ let key: i32 = 0
793
+ if (masked) {
794
+ key = 4
795
+ }
689
796
  if (len < 126) {
690
- extended = 0
691
- } else if (len < 65536) {
692
- extended = 2
797
+ return 2 + key
693
798
  }
694
- return 2 + extended + (masked ? 4 : 0) + len
799
+ if (len < 65536) {
800
+ return 4 + key
801
+ }
802
+ return 10 + key
695
803
  }
696
804
 
697
805
  /**
@@ -702,12 +810,12 @@ export const websocketFrameSize = (len: i32, masked: boolean): i32 => {
702
810
  * shortest of its three forms.
703
811
  *
704
812
  * Answers where the frame ends, `WS_NO_ROOM` when it does not fit, or
705
- * `WS_REFUSED` for an opcode RFC 6455 does not define, a control frame that is
706
- * not FIN or is longer than 125 bytes, and a mask that is not 4 bytes; on
707
- * either refusal `out` is untouched. A window outside `payload` panics, and so
708
- * does a payload longer than 2^31 - 15 bytes. A text frame's payload is not
709
- * checked here, because a fragment may end inside a character;
710
- * `websocketIsUtf8` checks a whole message.
813
+ * `WS_REFUSED` for a window outside `payload`, a payload longer than
814
+ * 2^31 - 15 bytes, an opcode RFC 6455 does not define, a control frame that
815
+ * is not FIN or is longer than 125 bytes, and a mask that is not 4 bytes; on
816
+ * either refusal `out` is untouched. A text frame's payload is not checked
817
+ * here, because a fragment may end inside a character; `websocketIsUtf8`
818
+ * checks a whole message.
711
819
  *
712
820
  * @param out The buffer the frame is written into.
713
821
  * @param at The index in `out` the frame starts at; one outside `out` answers `WS_NO_ROOM`.
@@ -730,8 +838,8 @@ export const websocketWriteFrame = (
730
838
  mask: u8[] | null
731
839
  ): i32 => {
732
840
  const size: i32 = toI32(payload.length)
733
- if (off < 0 || len < 0 || off > size || len > size - off) {
734
- panic("websocketWriteFrame: the window is outside the buffer")
841
+ if (off < 0 || len < 0 || off > size || len > size - off || len > WS_MAX_FRAME_PAYLOAD) {
842
+ return WS_REFUSED
735
843
  }
736
844
  if (!websocketIsKnownOpcode(opcode)) {
737
845
  return WS_REFUSED
@@ -743,60 +851,69 @@ export const websocketWriteFrame = (
743
851
  if (mask !== null && toI32(mask.length) !== 4) {
744
852
  return WS_REFUSED
745
853
  }
746
- if (len > WS_MAX_FRAME_PAYLOAD) {
747
- panic("websocketWriteFrame: a frame longer than 2^31 - 1 bytes")
748
- }
749
- const frameSize: i32 = websocketFrameSize(len, mask !== null)
854
+ const headerLength: i32 = websocketHeaderLength(len, mask !== null)
750
855
  const outLength: i32 = toI32(out.length)
751
- if (at < 0 || at > outLength || frameSize > outLength - at) {
856
+ if (at < 0 || at > outLength || toI64(headerLength) + toI64(len) > toI64(outLength) - toI64(at)) {
752
857
  return WS_NO_ROOM
753
858
  }
859
+ const finBit: i32 = fin ? 0x80 : 0
860
+ const maskBit: i32 = mask === null ? 0 : 0x80
861
+ // The room check covers the header; each form's own check on `at` says so
862
+ // in the form the index proofs read.
754
863
  let extended: i32 = 8
755
864
  if (len < 126) {
756
865
  extended = 0
866
+ if (at >= outLength - 1) {
867
+ return WS_NO_ROOM
868
+ }
869
+ out[at] = toU8(finBit | opcode)
870
+ out[at + 1] = toU8(maskBit | len)
757
871
  } else if (len < 65536) {
758
872
  extended = 2
759
- }
760
- const headerLength: i32 = frameSize - len
761
- const finBit: i32 = fin ? 0x80 : 0
762
- out[at] = toU8(finBit | opcode)
763
- const maskBit: i32 = mask === null ? 0 : 0x80
764
- if (extended === 0) {
765
- out[at + 1] = toU8(maskBit | len)
766
- } else if (extended === 2) {
873
+ if (at >= outLength - 3) {
874
+ return WS_NO_ROOM
875
+ }
876
+ out[at] = toU8(finBit | opcode)
767
877
  out[at + 1] = toU8(maskBit | 126)
768
878
  out[at + 2] = toU8(len >> 8)
769
879
  out[at + 3] = toU8(len)
770
880
  } else {
881
+ if (at >= outLength - 9) {
882
+ return WS_NO_ROOM
883
+ }
884
+ out[at] = toU8(finBit | opcode)
771
885
  out[at + 1] = toU8(maskBit | 127)
772
886
  // The top four bytes are zero: `len` is an `i32`.
773
- for (let k: i32 = 2; k < 6; k += 1) {
774
- out[at + k] = 0
775
- }
887
+ out[at + 2] = 0
888
+ out[at + 3] = 0
889
+ out[at + 4] = 0
890
+ out[at + 5] = 0
776
891
  out[at + 6] = toU8(len >> 24)
777
892
  out[at + 7] = toU8(len >> 16)
778
893
  out[at + 8] = toU8(len >> 8)
779
894
  out[at + 9] = toU8(len)
780
895
  }
781
- const payloadAt: i32 = at + headerLength
782
- for (let k: i32 = 0; k < len && payloadAt + k < outLength; k += 1) {
783
- out[payloadAt + k] = payload[off + k]
896
+ const payloadAt: i32 = websocketAdd(at, headerLength)
897
+ if (!tryCopyBytes(out, payloadAt, payload, off, len)) {
898
+ return WS_NO_ROOM
784
899
  }
785
900
  if (mask !== null) {
786
- const keyAt: i32 = at + 2 + extended
787
- for (let k: i32 = 0; k < 4 && k < toI32(mask.length); k += 1) {
788
- out[keyAt + k] = mask[k]
789
- }
901
+ const keyAt: i32 = websocketAdd(at, 2 + extended)
790
902
  // Masking and unmasking are the same XOR, so the copied payload goes
791
903
  // through the decoder's own loop, reading the key just written.
792
- websocketUnmaskInto(out, payloadAt, len, true, keyAt, 0, out, payloadAt)
904
+ if (
905
+ !tryCopyBytes(out, keyAt, mask, 0, 4) ||
906
+ !websocketUnmaskInto(out, payloadAt, len, true, keyAt, 0, out, payloadAt)
907
+ ) {
908
+ return WS_NO_ROOM
909
+ }
793
910
  }
794
- return payloadAt + len
911
+ return websocketAdd(payloadAt, len)
795
912
  }
796
913
 
797
914
  /**
798
915
  * One frame, as `websocketWriteFrame` writes it, in an array of its own; the
799
- * same refusals answer `null`. A window outside `payload` panics.
916
+ * same refusals, a window outside `payload` among them, answer `null`.
800
917
  *
801
918
  * @param fin Whether this is the message's last frame; a control frame must set it.
802
919
  * @param opcode The frame's opcode, such as `WS_OP_TEXT` or `WS_OP_PING`.
@@ -815,13 +932,10 @@ export const websocketFrame = (
815
932
  mask: u8[] | null
816
933
  ): u8[] | null => {
817
934
  const size: i32 = toI32(payload.length)
818
- if (off < 0 || len < 0 || off > size || len > size - off) {
819
- panic("websocketFrame: the window is outside the buffer")
820
- }
821
- if (len > WS_MAX_FRAME_PAYLOAD) {
822
- panic("websocketFrame: a frame longer than 2^31 - 1 bytes")
935
+ if (off < 0 || len < 0 || off > size || len > size - off || len > WS_MAX_FRAME_PAYLOAD) {
936
+ return null
823
937
  }
824
- const out: u8[] = new Array<u8>(websocketFrameSize(len, mask !== null))
938
+ const out: u8[] = websocketBytes(websocketFrameSize(len, mask !== null))
825
939
  const at: i32 = 0
826
940
  return websocketWriteFrame(out, at, fin, opcode, payload, off, len, mask) < 0 ? null : out
827
941
  }
@@ -841,12 +955,9 @@ export const websocketClosePayload = (code: i32, reason: string): u8[] | null =>
841
955
  if (!websocketIsSendableCode(code) || n > WS_MAX_CONTROL - 2) {
842
956
  return null
843
957
  }
844
- const out: u8[] = new Array<u8>(n + 2)
845
- const outLength: i32 = toI32(out.length)
846
- out[0] = toU8(code >> 8)
847
- out[1] = toU8(code)
848
- for (let k: i32 = 0; k < n && k + 2 < outLength; k += 1) {
849
- out[k + 2] = toU8(reason.charCodeAt(k))
958
+ const out: u8[] = [toU8(code >> 8), toU8(code)]
959
+ for (let k: i32 = 0; k < n; k += 1) {
960
+ out.push(toU8(reason.charCodeAt(k)))
850
961
  }
851
962
  const reasonStart: i32 = 2
852
963
  if (!websocketIsUtf8(out, reasonStart, n)) {
@@ -860,7 +971,7 @@ export const websocketClosePayload = (code: i32, reason: string): u8[] | null =>
860
971
  /** The bytes of `text`, as the language holds a string. */
861
972
  const websocketBytesOf = (text: string): u8[] => {
862
973
  const n: i32 = toI32(text.length)
863
- const out: u8[] = new Array<u8>(n)
974
+ const out: u8[] = websocketBytes(n)
864
975
  const outLength: i32 = toI32(out.length)
865
976
  for (let k: i32 = 0; k < outLength && k < n; k += 1) {
866
977
  out[k] = toU8(text.charCodeAt(k))
@@ -883,6 +994,7 @@ const WS_ACCEPT_LENGTH: i32 = 28
883
994
  */
884
995
  const websocketKeySpanIsValid = (text: u8[], start: i32, end: i32): boolean =>
885
996
  start >= 0 &&
997
+ end >= 0 &&
886
998
  end <= toI32(text.length) &&
887
999
  end - start === WS_KEY_LENGTH &&
888
1000
  base64StdPadDecodedLength(text, start, WS_KEY_LENGTH) === 16
@@ -911,11 +1023,17 @@ export class WsAcceptKey {
911
1023
  /** Storage for one accept value, its hasher and buffers allocated. */
912
1024
  constructor() {
913
1025
  this.hasher = new Sha1()
914
- const guid: i32 = toI32(WS_GUID.length)
915
- this.input = new Array<u8>(WS_KEY_LENGTH + guid)
916
- for (let k: i32 = 0; k < guid; k += 1) {
917
- this.input[WS_KEY_LENGTH + k] = toU8(WS_GUID.charCodeAt(k))
1026
+ const text: string = WS_GUID
1027
+ const guid: i32 = toI32(text.length)
1028
+ // The 24-character key and the 36-character GUID.
1029
+ const input: u8[] = new Array<u8>(60)
1030
+ const inputLength: i32 = toI32(input.length)
1031
+ let t: i32 = WS_KEY_LENGTH
1032
+ for (let k: i32 = 0; k < guid && t >= 0 && t < inputLength; k += 1) {
1033
+ input[t] = toU8(text.charCodeAt(k))
1034
+ t += 1
918
1035
  }
1036
+ this.input = input
919
1037
  this.digest = new Array<u8>(SHA1_SIZE)
920
1038
  this.value = new Array<u8>(WS_ACCEPT_LENGTH)
921
1039
  }
@@ -934,8 +1052,9 @@ export class WsAcceptKey {
934
1052
  return false
935
1053
  }
936
1054
  const input: u8[] = this.input
937
- for (let k: i32 = 0; k < WS_KEY_LENGTH && k < toI32(input.length); k += 1) {
938
- input[k] = text[start + k]
1055
+ // A valid key is 24 bytes inside `text`, so the copy fits.
1056
+ if (!tryCopyBytes(input, WS_ZERO, text, start, WS_KEY_LENGTH)) {
1057
+ return false
939
1058
  }
940
1059
  this.hasher.digestInto(input, WS_ZERO, toI32(input.length), this.digest, WS_ZERO)
941
1060
  base64StdPadEncodeInto(this.digest, WS_ZERO, SHA1_SIZE, this.value, WS_ZERO)
@@ -994,10 +1113,8 @@ export const websocketRequestKeyField = (p: Http1Parser): i32 => {
994
1113
  }
995
1114
  const i: i32 = p.headerIndex("sec-websocket-key")
996
1115
  const spans: i32[] = p.spans
997
- if (i < 0 || 4 * i + 3 >= toI32(spans.length)) {
998
- return -1
999
- }
1000
- if (!websocketKeySpanIsValid(p.head, spans[4 * i + 2], spans[4 * i + 3])) {
1116
+ const at: i32 = websocketValueAt(spans, i)
1117
+ if (at < 0 || at >= toI32(spans.length) - 1 || !websocketKeySpanIsValid(p.head, spans[at], spans[at + 1])) {
1001
1118
  return -1
1002
1119
  }
1003
1120
  return i
@@ -1013,21 +1130,40 @@ export const websocketRequestKeyField = (p: Http1Parser): i32 => {
1013
1130
  * @returns The `Sec-WebSocket-Key` to pass to `websocketUpgradeResponse`, or null when the request is not a valid handshake.
1014
1131
  */
1015
1132
  export const websocketRequestKey = (p: Http1Parser): string | null => {
1016
- const i: i32 = websocketRequestKeyField(p)
1017
1133
  const spans: i32[] = p.spans
1018
- if (i < 0 || 4 * i + 3 >= toI32(spans.length)) {
1134
+ const at: i32 = websocketValueAt(spans, websocketRequestKeyField(p))
1135
+ if (at < 0 || at >= toI32(spans.length) - 1) {
1019
1136
  return null
1020
1137
  }
1021
- return http1Text(p.head, spans[4 * i + 2], spans[4 * i + 3], false)
1138
+ return http1Text(p.head, spans[at], spans[at + 1], false)
1139
+ }
1140
+
1141
+ /**
1142
+ * Where field `i`'s value span starts in `spans` (four entries a field: the
1143
+ * name's start and end, then the value's), or -1 when `i` is not a field
1144
+ * `spans` holds; `spans[at]` and `spans[at + 1]` are then the value's ends.
1145
+ */
1146
+ const websocketValueAt = (spans: i32[], i: i32): i32 => {
1147
+ if (i < 0 || i > 536870910) {
1148
+ return -1
1149
+ }
1150
+ const at: i32 = 4 * i + 2
1151
+ if (at >= toI32(spans.length) - 1) {
1152
+ return -1
1153
+ }
1154
+ return at
1022
1155
  }
1023
1156
 
1024
1157
  /** Writes `text` into `out` from `at`; the caller has checked it fits. */
1025
1158
  const websocketPutText = (out: u8[], at: i32, text: string): i32 => {
1026
1159
  const n: i32 = toI32(text.length)
1027
- for (let k: i32 = 0; k < n; k += 1) {
1028
- out[at + k] = toU8(text.charCodeAt(k))
1160
+ const outLength: i32 = toI32(out.length)
1161
+ let j: i32 = at
1162
+ for (let k: i32 = 0; k < n && j >= 0 && j < outLength; k += 1) {
1163
+ out[j] = toU8(text.charCodeAt(k))
1164
+ j += 1
1029
1165
  }
1030
- return at + n
1166
+ return j
1031
1167
  }
1032
1168
 
1033
1169
  /** The 101's status line and its first two fields, which never change. */
@@ -1049,20 +1185,26 @@ const WS_SWITCHING: string =
1049
1185
  */
1050
1186
  export const websocketWriteUpgradeResponse = (out: u8[], at: i32, accept: u8[], protocol: string): i32 => {
1051
1187
  const p: i32 = toI32(protocol.length)
1052
- if (toI32(accept.length) !== WS_ACCEPT_LENGTH || (p > 0 && !http1IsToken(protocol)) || p > 65536) {
1188
+ if (p < 0 || p > 65536) {
1189
+ return HTTP1_REFUSED
1190
+ }
1191
+ if (toI32(accept.length) !== WS_ACCEPT_LENGTH || (p > 0 && !http1IsToken(protocol))) {
1053
1192
  return HTTP1_REFUSED
1054
1193
  }
1055
1194
  const fixed: i32 = toI32(WS_SWITCHING.length)
1056
- const named: i32 = p > 0 ? 26 + p : 0
1057
- const length: i32 = fixed + WS_ACCEPT_LENGTH + 2 + named + 2
1058
- if (at < 0 || at > toI32(out.length) - length) {
1195
+ let named: i32 = 0
1196
+ if (p > 0) {
1197
+ named = 26 + p
1198
+ }
1199
+ const length: i32 = websocketAdd(fixed, named + WS_ACCEPT_LENGTH + 4)
1200
+ if (at < 0 || toI64(at) + toI64(length) > toI64(toI32(out.length))) {
1059
1201
  return HTTP1_NO_ROOM
1060
1202
  }
1061
1203
  let j: i32 = websocketPutText(out, at, WS_SWITCHING)
1062
- for (let k: i32 = 0; k < toI32(accept.length); k += 1) {
1063
- out[j + k] = accept[k]
1204
+ if (!tryCopyBytes(out, j, accept, WS_ZERO, WS_ACCEPT_LENGTH)) {
1205
+ return HTTP1_NO_ROOM
1064
1206
  }
1065
- j = websocketPutText(out, j + WS_ACCEPT_LENGTH, "\r\n")
1207
+ j = websocketPutText(out, websocketAdd(j, WS_ACCEPT_LENGTH), "\r\n")
1066
1208
  if (p > 0) {
1067
1209
  j = websocketPutText(out, j, "Sec-WebSocket-Protocol: ")
1068
1210
  j = websocketPutText(out, j, protocol)
@@ -1090,12 +1232,14 @@ export const websocketUpgradeResponse = (key: string, protocol: string): u8[] |
1090
1232
  if (!accept.compute(bytes, WS_ZERO, toI32(bytes.length))) {
1091
1233
  return null
1092
1234
  }
1093
- const out: u8[] = new Array<u8>(toI32(WS_SWITCHING.length) + WS_ACCEPT_LENGTH + 30 + toI32(protocol.length))
1235
+ const out: u8[] = websocketBytes(
1236
+ websocketAdd(websocketAdd(toI32(WS_SWITCHING.length), WS_ACCEPT_LENGTH + 30), toI32(protocol.length))
1237
+ )
1094
1238
  const end: i32 = websocketWriteUpgradeResponse(out, WS_ZERO, accept.value, protocol)
1095
1239
  if (end < 0) {
1096
1240
  return null
1097
1241
  }
1098
- const head: u8[] = new Array<u8>(end)
1242
+ const head: u8[] = websocketBytes(end)
1099
1243
  for (let k: i32 = 0; k < toI32(head.length) && k < toI32(out.length); k++) {
1100
1244
  head[k] = out[k]
1101
1245
  }
@@ -1117,9 +1261,15 @@ export const websocketOfferedProtocols = (p: Http1Parser): string[] | null => {
1117
1261
  const out: string[] = []
1118
1262
  const head: u8[] = p.head
1119
1263
  const spans: i32[] = p.spans
1120
- for (let i: i32 = 0; i < p.headerCount && 4 * i + 3 < toI32(spans.length); i += 1) {
1121
- if (websocketSpanIs(head, spans[4 * i], spans[4 * i + 1], "sec-websocket-protocol")) {
1122
- websocketListInto(head, spans[4 * i + 2], spans[4 * i + 3], out)
1264
+ // `s` steps through the four spans of each field in turn.
1265
+ let s: i32 = 0
1266
+ for (let i: i32 = 0; i < p.headerCount && s >= 0 && s < toI32(spans.length) - 3; i += 1) {
1267
+ const valueStart: i32 = spans[s + 2]
1268
+ const valueEnd: i32 = spans[s + 3]
1269
+ const offers: boolean = websocketSpanIs(head, spans[s], spans[s + 1], "sec-websocket-protocol")
1270
+ s += 4
1271
+ if (offers) {
1272
+ websocketListInto(head, valueStart, valueEnd, out)
1123
1273
  }
1124
1274
  }
1125
1275
  for (let k: i32 = 0; k < toI32(out.length); k += 1) {
@@ -1153,20 +1303,24 @@ export const websocketOfferedProtocols = (p: Http1Parser): string[] | null => {
1153
1303
  export const websocketOriginAllowed = (p: Http1Parser, allowed: string[]): boolean => {
1154
1304
  const head: u8[] = p.head
1155
1305
  const spans: i32[] = p.spans
1306
+ // `s` steps through the four spans of each field in turn; `found` is where
1307
+ // the `Origin` field's spans start.
1156
1308
  let found: i32 = -1
1157
- for (let i: i32 = 0; i < p.headerCount && 4 * i + 3 < toI32(spans.length); i += 1) {
1158
- if (websocketSpanIs(head, spans[4 * i], spans[4 * i + 1], "origin")) {
1309
+ let s: i32 = 0
1310
+ for (let i: i32 = 0; i < p.headerCount && s >= 0 && s < toI32(spans.length) - 3; i += 1) {
1311
+ if (websocketSpanIs(head, spans[s], spans[s + 1], "origin")) {
1159
1312
  if (found >= 0) {
1160
1313
  return false
1161
1314
  }
1162
- found = i
1315
+ found = s
1163
1316
  }
1317
+ s += 4
1164
1318
  }
1165
- if (found < 0) {
1319
+ if (found < 0 || found >= toI32(spans.length) - 3) {
1166
1320
  return false
1167
1321
  }
1168
1322
  for (let k: i32 = 0; k < toI32(allowed.length); k += 1) {
1169
- if (websocketSpanFolds(head, spans[4 * found + 2], spans[4 * found + 3], allowed[k])) {
1323
+ if (websocketSpanFolds(head, spans[found + 2], spans[found + 3], allowed[k])) {
1170
1324
  return true
1171
1325
  }
1172
1326
  }
@@ -1179,11 +1333,11 @@ export const websocketOriginAllowed = (p: Http1Parser, allowed: string[]): boole
1179
1333
  */
1180
1334
  const websocketSpanFolds = (bytes: u8[], a: i32, b: i32, text: string): boolean => {
1181
1335
  const n: i32 = toI32(text.length)
1182
- if (a < 0 || b - a !== n || b > toI32(bytes.length)) {
1336
+ if (a < 0 || b < 0 || b - a !== n || b > toI32(bytes.length)) {
1183
1337
  return false
1184
1338
  }
1339
+ let at: i32 = a
1185
1340
  for (let k: i32 = 0; k < n; k += 1) {
1186
- const at: i32 = a + k
1187
1341
  if (at < 0 || at >= toI32(bytes.length) || k >= toI32(text.length)) {
1188
1342
  return false
1189
1343
  }
@@ -1198,6 +1352,7 @@ const websocketSpanFolds = (bytes: u8[], a: i32, b: i32, text: string): boolean
1198
1352
  if (c !== w) {
1199
1353
  return false
1200
1354
  }
1355
+ at += 1
1201
1356
  }
1202
1357
  return true
1203
1358
  }
@@ -1205,11 +1360,11 @@ const websocketSpanFolds = (bytes: u8[], a: i32, b: i32, text: string): boolean
1205
1360
  /** Whether `bytes[a .. b)` is `word`, which is lower case, in any ASCII case. Allocates nothing. */
1206
1361
  const websocketSpanIs = (bytes: u8[], a: i32, b: i32, word: string): boolean => {
1207
1362
  const n: i32 = toI32(word.length)
1208
- if (a < 0 || b - a !== n || b > toI32(bytes.length)) {
1363
+ if (a < 0 || b < 0 || b - a !== n || b > toI32(bytes.length)) {
1209
1364
  return false
1210
1365
  }
1366
+ let at: i32 = a
1211
1367
  for (let k: i32 = 0; k < n; k += 1) {
1212
- const at: i32 = a + k
1213
1368
  if (at < 0 || at >= toI32(bytes.length) || k >= toI32(word.length)) {
1214
1369
  return false
1215
1370
  }
@@ -1220,6 +1375,7 @@ const websocketSpanIs = (bytes: u8[], a: i32, b: i32, word: string): boolean =>
1220
1375
  if (c !== toI32(word.charCodeAt(k))) {
1221
1376
  return false
1222
1377
  }
1378
+ at += 1
1223
1379
  }
1224
1380
  return true
1225
1381
  }
@@ -1229,49 +1385,71 @@ const websocketSpanIs = (bytes: u8[], a: i32, b: i32, word: string): boolean =>
1229
1385
  * (RFC 9110 §5.6.1) to `out`, each trimmed of OWS, the empty ones dropped.
1230
1386
  */
1231
1387
  const websocketListInto = (bytes: u8[], from: i32, to: i32, out: string[]): void => {
1232
- if (from < 0 || to > toI32(bytes.length)) {
1388
+ const n: i32 = toI32(bytes.length)
1389
+ if (from < 0 || to > n) {
1233
1390
  return
1234
1391
  }
1235
1392
  let start: i32 = from
1236
- for (let k: i32 = from; k <= to; k += 1) {
1237
- if (k === to || (k >= 0 && k < toI32(bytes.length) && toI32(bytes[k]) === 44)) {
1238
- let a: i32 = start
1239
- let b: i32 = k
1240
- while (a < b && a >= 0 && a < toI32(bytes.length) && http1IsOws(toI32(bytes[a]))) {
1241
- a += 1
1242
- }
1243
- while (b > a && b - 1 >= 0 && b - 1 < toI32(bytes.length) && http1IsOws(toI32(bytes[b - 1]))) {
1244
- b -= 1
1245
- }
1246
- if (b > a) {
1247
- out.push(http1Text(bytes, a, b, false))
1248
- }
1393
+ for (let k: i32 = from; k >= 0 && k < to && k < toI32(bytes.length); k += 1) {
1394
+ if (toI32(bytes[k]) === 44) {
1395
+ websocketElementInto(bytes, start, k, out)
1249
1396
  start = k + 1
1250
1397
  }
1251
1398
  }
1399
+ websocketElementInto(bytes, start, to, out)
1400
+ }
1401
+
1402
+ /** Appends `bytes[a .. b)`, trimmed of OWS, to `out` unless it is empty. */
1403
+ const websocketElementInto = (bytes: u8[], a: i32, b: i32, out: string[]): void => {
1404
+ const n: i32 = toI32(bytes.length)
1405
+ let x: i32 = a
1406
+ let y: i32 = b
1407
+ while (x < y && x >= 0 && x < n && http1IsOws(toI32(bytes[x]))) {
1408
+ x += 1
1409
+ }
1410
+ while (y > x && y > 0 && y <= n) {
1411
+ const c: i32 = y - 1
1412
+ if (c < 0 || c >= n || !http1IsOws(toI32(bytes[c]))) {
1413
+ break
1414
+ }
1415
+ y = c
1416
+ }
1417
+ if (y > x) {
1418
+ out.push(http1Text(bytes, x, y, false))
1419
+ }
1252
1420
  }
1253
1421
 
1254
1422
  /** Whether the comma-separated list `value` (RFC 9110 §5.6.1) has an element that is `word`, which is lower case, in any ASCII case. */
1255
1423
  const websocketListHas = (value: string, word: string): boolean => {
1256
1424
  const n: i32 = toI32(value.length)
1257
1425
  let start: i32 = 0
1258
- for (let k: i32 = 0; k <= n; k += 1) {
1259
- if (k === n || (k >= 0 && k < n && toI32(value.charCodeAt(k)) === 44)) {
1260
- let a: i32 = start
1261
- let b: i32 = k
1262
- while (a < b && a >= 0 && a < n && http1IsOws(toI32(value.charCodeAt(a)))) {
1263
- a += 1
1264
- }
1265
- while (b > a && b - 1 >= 0 && b - 1 < n && http1IsOws(toI32(value.charCodeAt(b - 1)))) {
1266
- b -= 1
1267
- }
1268
- if (http1SameWord(value, a, b, word)) {
1426
+ for (let k: i32 = 0; k >= 0 && k < n; k += 1) {
1427
+ if (toI32(value.charCodeAt(k)) === 44) {
1428
+ if (websocketElementIs(value, start, k, word)) {
1269
1429
  return true
1270
1430
  }
1271
1431
  start = k + 1
1272
1432
  }
1273
1433
  }
1274
- return false
1434
+ return websocketElementIs(value, start, n, word)
1435
+ }
1436
+
1437
+ /** Whether `value[a .. b)`, trimmed of OWS, is `word` in any ASCII case. */
1438
+ const websocketElementIs = (value: string, a: i32, b: i32, word: string): boolean => {
1439
+ const n: i32 = toI32(value.length)
1440
+ let x: i32 = a
1441
+ let y: i32 = b
1442
+ while (x < y && x >= 0 && x < n && http1IsOws(toI32(value.charCodeAt(x)))) {
1443
+ x += 1
1444
+ }
1445
+ while (y > x && y > 0 && y <= n) {
1446
+ const c: i32 = y - 1
1447
+ if (c < 0 || c >= n || !http1IsOws(toI32(value.charCodeAt(c)))) {
1448
+ break
1449
+ }
1450
+ y = c
1451
+ }
1452
+ return http1SameWord(value, x, y, word)
1275
1453
  }
1276
1454
 
1277
1455
  /**
@@ -1424,19 +1602,25 @@ export const websocketCheckResponse = (
1424
1602
  const name: string = names[i]
1425
1603
  const value: string = values[i]
1426
1604
  if (name === "upgrade") {
1427
- upgrades += 1
1605
+ if (upgrades < 2) {
1606
+ upgrades += 1
1607
+ }
1428
1608
  upgradeOk = http1SameWord(value, 0, toI32(value.length), "websocket")
1429
1609
  } else if (name === "connection") {
1430
1610
  if (websocketListHas(value, "upgrade")) {
1431
1611
  connection = true
1432
1612
  }
1433
1613
  } else if (name === "sec-websocket-accept") {
1434
- accepts += 1
1614
+ if (accepts < 2) {
1615
+ accepts += 1
1616
+ }
1435
1617
  acceptOk = toI32(expected.length) > 0 && value === expected
1436
1618
  } else if (name === "sec-websocket-extensions") {
1437
1619
  extensions = true
1438
1620
  } else if (name === "sec-websocket-protocol") {
1439
- protocols += 1
1621
+ if (protocols < 2) {
1622
+ protocols += 1
1623
+ }
1440
1624
  protocol = value
1441
1625
  }
1442
1626
  }