nish 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 (129) hide show
  1. package/docs/AI.md +50 -4
  2. package/docs/INSTALL.md +73 -59
  3. package/package.json +19 -5
  4. package/runtime/async.d.mts +2 -0
  5. package/runtime/async.mjs +7 -0
  6. package/runtime/nish.d.ts +4849 -430
  7. package/runtime/nish.h +25 -0
  8. package/runtime/page.d.ts +53 -0
  9. package/runtime/page.mjs +146 -0
  10. package/runtime/runtime-async.c +557 -0
  11. package/runtime/runtime-wasm.c +63 -7
  12. package/runtime/vite.d.ts +30 -0
  13. package/runtime/vite.mjs +150 -0
  14. package/scripts/build.sh +252 -15
  15. package/scripts/fuzz.sh +1 -0
  16. package/scripts/undocumented.txt +14 -0
  17. package/std/README.md +107 -82
  18. package/std/base64.ts +34 -5
  19. package/std/bytes.ts +279 -59
  20. package/std/cli-args.ts +296 -70
  21. package/std/cli-flags.ts +184 -19
  22. package/std/cli.ts +475 -54
  23. package/std/collections.ts +121 -23
  24. package/std/compress/checksum.ts +44 -14
  25. package/std/compress/deflate.ts +224 -111
  26. package/std/compress/gzip.ts +217 -53
  27. package/std/compress/zlib.ts +63 -7
  28. package/std/crypto/aes.ts +128 -52
  29. package/std/crypto/base64url.ts +29 -5
  30. package/std/crypto/bcrypt-batch.ts +101 -34
  31. package/std/crypto/bcrypt.ts +93 -27
  32. package/std/crypto/chacha20poly1305.ts +69 -14
  33. package/std/crypto/ct.ts +32 -13
  34. package/std/crypto/ed25519.ts +66 -16
  35. package/std/crypto/hkdf.ts +198 -13
  36. package/std/crypto/hmac.ts +242 -37
  37. package/std/crypto/jwt.ts +204 -72
  38. package/std/crypto/p256.ts +222 -72
  39. package/std/crypto/p384.ts +51 -23
  40. package/std/crypto/pki-roots.ts +32 -2
  41. package/std/crypto/pki.ts +99 -30
  42. package/std/crypto/rsa.ts +56 -22
  43. package/std/crypto/sha1.ts +3 -0
  44. package/std/crypto/sha256.ts +24 -1
  45. package/std/crypto/sha512.ts +99 -14
  46. package/std/crypto/x25519.ts +163 -24
  47. package/std/crypto/x509-random.ts +10 -0
  48. package/std/crypto/x509.ts +133 -66
  49. package/std/host/ring.ts +124 -0
  50. package/std/json-equal.ts +31 -11
  51. package/std/json-format.ts +112 -13
  52. package/std/json-number.ts +76 -26
  53. package/std/json-pointer.ts +23 -1
  54. package/std/json-tokenizer.ts +126 -51
  55. package/std/json-typed.ts +757 -96
  56. package/std/json-value.ts +438 -157
  57. package/std/json.ts +170 -38
  58. package/std/map.ts +25 -7
  59. package/std/math.ts +36 -4
  60. package/std/net/buffer-pool.ts +42 -11
  61. package/std/net/dns-config.ts +109 -23
  62. package/std/net/dns-message.ts +62 -7
  63. package/std/net/dns-resolver.ts +247 -53
  64. package/std/net/dns.ts +203 -35
  65. package/std/net/fetch.ts +1267 -290
  66. package/std/net/handler.ts +296 -80
  67. package/std/net/hpack.ts +290 -77
  68. package/std/net/http-compress.ts +152 -36
  69. package/std/net/http-fields.ts +85 -9
  70. package/std/net/http1-client-codec.ts +129 -44
  71. package/std/net/http1-codec.ts +279 -114
  72. package/std/net/http1-server.ts +409 -139
  73. package/std/net/http2-frame.ts +289 -51
  74. package/std/net/http2-tls.ts +152 -49
  75. package/std/net/http2.ts +314 -160
  76. package/std/net/http3-frame.ts +165 -27
  77. package/std/net/http3-server.ts +282 -131
  78. package/std/net/http3.ts +377 -173
  79. package/std/net/outbound.ts +37 -3
  80. package/std/net/postgres-carrier.ts +144 -8
  81. package/std/net/postgres.ts +898 -260
  82. package/std/net/prefork.ts +202 -65
  83. package/std/net/qpack.ts +120 -32
  84. package/std/net/quic-conn-ack.ts +60 -12
  85. package/std/net/quic-conn-cid.ts +135 -27
  86. package/std/net/quic-conn-params.ts +109 -22
  87. package/std/net/quic-datagram.ts +50 -3
  88. package/std/net/quic-frame.ts +333 -23
  89. package/std/net/quic-listener.ts +161 -75
  90. package/std/net/quic-packet.ts +373 -73
  91. package/std/net/quic-recovery.ts +287 -65
  92. package/std/net/quic-stream.ts +436 -146
  93. package/std/net/quic.ts +693 -323
  94. package/std/net/router.ts +87 -40
  95. package/std/net/tls/client.ts +167 -51
  96. package/std/net/tls/codec.ts +494 -68
  97. package/std/net/tls/record-client.ts +140 -73
  98. package/std/net/tls/record-server.ts +233 -105
  99. package/std/net/tls/record.ts +142 -36
  100. package/std/net/tls/schedule.ts +235 -26
  101. package/std/net/tls/ticket.ts +161 -40
  102. package/std/net/tls/verify.ts +57 -10
  103. package/std/net/tls-tcp.ts +308 -95
  104. package/std/net/tls.ts +276 -102
  105. package/std/net/upstream.ts +81 -45
  106. package/std/net/url.ts +109 -22
  107. package/std/net/websocket.ts +140 -26
  108. package/std/net/webtransport.ts +243 -106
  109. package/std/pair.ts +10 -2
  110. package/std/random.ts +42 -15
  111. package/std/regex.ts +422 -257
  112. package/std/region.ts +70 -15
  113. package/std/secret.ts +52 -11
  114. package/std/sort.ts +17 -2
  115. package/std/sqlite.ts +256 -35
  116. package/std/sync/ring.ts +229 -56
  117. package/std/sync/shm.ts +203 -23
  118. package/std/term-io.ts +154 -78
  119. package/std/term-pixels.ts +144 -19
  120. package/std/term-probe.ts +77 -35
  121. package/std/term-rate.ts +199 -118
  122. package/std/term.ts +400 -175
  123. package/std/testing.ts +130 -32
  124. package/std/text.ts +84 -17
  125. package/std/threads.ts +248 -56
  126. package/std/yaml-json.ts +111 -97
  127. package/std/yaml-lines.ts +47 -15
  128. package/std/yaml-write.ts +45 -30
  129. package/std/yaml.ts +764 -384
@@ -84,23 +84,37 @@ import { Http1ResponseParser, http1UpgradeRequestHead } from "nish/net/http1-cli
84
84
  export const WS_GUID: string = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
85
85
 
86
86
  // Opcodes (§5.2).
87
+ /** A frame that continues the message before it. */
87
88
  export const WS_OP_CONTINUATION: i32 = 0
89
+ /** A text message, UTF-8. */
88
90
  export const WS_OP_TEXT: i32 = 1
91
+ /** A binary message. */
89
92
  export const WS_OP_BINARY: i32 = 2
93
+ /** A close frame: an optional status code, then an optional UTF-8 reason. */
90
94
  export const WS_OP_CLOSE: i32 = 8
95
+ /** A ping, which the peer answers with a pong carrying the same payload. */
91
96
  export const WS_OP_PING: i32 = 9
97
+ /** A pong, the answer to a ping or an unasked heartbeat. */
92
98
  export const WS_OP_PONG: i32 = 10
93
99
 
94
100
  // Close status codes (§7.4.1).
101
+ /** The purpose of the connection is fulfilled. */
95
102
  export const WS_CLOSE_NORMAL: i32 = 1000
103
+ /** The endpoint is going away: a server shutting down, or a page closed. */
96
104
  export const WS_CLOSE_GOING_AWAY: i32 = 1001
105
+ /** The peer broke the framing rules. */
97
106
  export const WS_CLOSE_PROTOCOL_ERROR: i32 = 1002
107
+ /** The peer sent a kind of data the endpoint cannot take. */
98
108
  export const WS_CLOSE_UNSUPPORTED: i32 = 1003
99
109
  /** Reported for a close frame with no status; never sent. */
100
110
  export const WS_CLOSE_NO_STATUS: i32 = 1005
111
+ /** A message's data did not fit its type, such as a text message that is not UTF-8. */
101
112
  export const WS_CLOSE_INVALID_DATA: i32 = 1007
113
+ /** A message broke the endpoint's policy, when no other code fits. */
102
114
  export const WS_CLOSE_POLICY: i32 = 1008
115
+ /** A message was longer than the endpoint takes. */
103
116
  export const WS_CLOSE_TOO_BIG: i32 = 1009
117
+ /** The endpoint met a condition it could not handle. */
104
118
  export const WS_CLOSE_INTERNAL_ERROR: i32 = 1011
105
119
 
106
120
  /** The longest payload a control frame may carry (§5.5). */
@@ -131,8 +145,11 @@ const WS_S_ERROR: i32 = 2
131
145
  * Table 3-7), so that a sequence cut between two fragments is checked whole.
132
146
  */
133
147
  class WsUtf8 {
148
+ /** @internal Validator state `websocketUtf8Run` reads and writes. */
134
149
  need: i32 = 0
150
+ /** @internal Validator state `websocketUtf8Run` reads and writes. */
135
151
  lo: i32 = 0x80
152
+ /** @internal Validator state `websocketUtf8Run` reads and writes. */
136
153
  hi: i32 = 0xbf
137
154
  }
138
155
 
@@ -196,7 +213,16 @@ const websocketUtf8Run = (state: WsUtf8, buf: u8[], off: i32, len: i32): boolean
196
213
  return true
197
214
  }
198
215
 
199
- /** Whether `buf[off .. off + len)` is well-formed UTF-8. A window outside `buf` panics. */
216
+ /**
217
+ * Whether `buf[off .. off + len)` is well-formed UTF-8: what a whole text
218
+ * message's payload, or a close frame's reason, must be (§8.1). A window
219
+ * outside `buf` panics.
220
+ *
221
+ * @param buf The array holding the bytes.
222
+ * @param off Where the bytes start in `buf`.
223
+ * @param len How many bytes to check; 0 is valid UTF-8.
224
+ * @returns True when the bytes are UTF-8 with no overlong form, surrogate or code point past U+10FFFF, and no character cut short at the end.
225
+ */
200
226
  export const websocketIsUtf8 = (buf: u8[], off: i32, len: i32): boolean => {
201
227
  const size: i32 = toI32(buf.length)
202
228
  if (off < 0 || len < 0 || off > size || len > size - off) {
@@ -226,30 +252,42 @@ export class WsDecoder {
226
252
  data: u8[]
227
253
  /** The peer's close reason after `WS_CLOSE`, or what was wrong after `WS_ERROR`. */
228
254
  closeReason: string = ""
229
- /** The bytes fed and not yet consumed are `buf[start .. end)`. */
255
+ /** The bytes fed and not yet consumed are `buf[start .. end)`. @internal */
230
256
  buf: u8[]
231
- /** The message being assembled; its first `messageLen` bytes are live. */
257
+ /** The message being assembled; its first `messageLen` bytes are live. @internal */
232
258
  message: u8[]
233
- /** A control frame's payload, kept apart so a ping cannot disturb a message in progress. */
259
+ /** A control frame's payload, kept apart so a ping cannot disturb a message in progress. @internal */
234
260
  control: u8[]
235
- /** The UTF-8 check of the text message being assembled. */
261
+ /** The UTF-8 check of the text message being assembled. @internal */
236
262
  utf8: WsUtf8
237
263
 
264
+ /** The longest message accepted, in bytes; a longer one is refused with 1009. */
238
265
  maxMessage: i32 = 0
266
+ /** How many bytes of `data` the last event filled. */
239
267
  dataLen: i32 = 0
240
268
  /** The last event's opcode: `WS_OP_TEXT` or `WS_OP_BINARY` for a message, the control opcode otherwise. */
241
269
  opcode: i32 = 0
242
270
  /** The peer's status code after `WS_CLOSE` (`WS_CLOSE_NO_STATUS` when it sent none), or the one to send after `WS_ERROR`. */
243
271
  closeCode: i32 = 0
272
+ /** @internal The unread bytes are `buf[start .. end)`; `nish/net/http1-server` reads them to size its input. */
244
273
  start: i32 = 0
274
+ /** @internal The end of the unread bytes; `nish/net/http1-server` reads it with `start`. */
245
275
  end: i32 = 0
276
+ /** @internal Decoder state the module's `websocket` helpers read and write. */
246
277
  state: i32 = 0
247
- /** The opcode of the message whose fragments are arriving, or 0 between messages. */
278
+ /** The opcode of the message whose fragments are arriving, or 0 between messages. @internal */
248
279
  messageOpcode: i32 = 0
280
+ /** @internal Decoder state the module's `websocket` helpers read and write. */
249
281
  messageLen: i32 = 0
250
282
 
283
+ /** Whether every frame must be masked, as a server requires of a client. */
251
284
  expectMasked: boolean = false
252
285
 
286
+ /**
287
+ * A decoder with nothing fed yet.
288
+ * @param expectMasked True for a server, whose client masks every frame; false for a client.
289
+ * @param maxMessage The longest message accepted, in bytes; below zero is read as zero.
290
+ */
253
291
  constructor(expectMasked: boolean, maxMessage: i32) {
254
292
  this.expectMasked = expectMasked
255
293
  // Below zero allows only empty messages, as zero does.
@@ -268,6 +306,9 @@ export class WsDecoder {
268
306
  /**
269
307
  * Appends `data[off .. off + len)` to the input. A window outside `data`
270
308
  * panics; after `WS_CLOSE` or `WS_ERROR` the input is dropped.
309
+ * @param data The bytes read from the connection.
310
+ * @param off Where the new bytes start in `data`.
311
+ * @param len How many bytes to append.
271
312
  */
272
313
  feed(data: u8[], off: i32, len: i32): void {
273
314
  const size: i32 = toI32(data.length)
@@ -295,7 +336,10 @@ export class WsDecoder {
295
336
  this.end = at + len
296
337
  }
297
338
 
298
- /** The next event the input holds; see the module comment. */
339
+ /**
340
+ * The next event the input holds; see the module comment.
341
+ * @returns A `WS_` event, `WS_NEED_MORE` when the input runs out.
342
+ */
299
343
  next(): i32 {
300
344
  return websocketNext(this)
301
345
  }
@@ -590,7 +634,14 @@ export const WS_NO_ROOM: i32 = -1
590
634
  /** `websocketWriteFrame`: a frame the encoder refuses to write. */
591
635
  export const WS_REFUSED: i32 = -2
592
636
 
593
- /** How many bytes a frame of `len` payload bytes takes, its header included. */
637
+ /**
638
+ * How many bytes a frame of `len` payload bytes takes, its header included,
639
+ * so a caller can size the buffer `websocketWriteFrame` writes into.
640
+ *
641
+ * @param len The payload's length in bytes, at least 0.
642
+ * @param masked Whether the frame carries a 4-byte mask key, as every frame a client sends does.
643
+ * @returns The frame's size in bytes: 2 to 14 bytes of header, then `len`.
644
+ */
594
645
  export const websocketFrameSize = (len: i32, masked: boolean): i32 => {
595
646
  let extended: i32 = 8
596
647
  if (len < 126) {
@@ -606,13 +657,25 @@ export const websocketFrameSize = (len: i32, masked: boolean): i32 => {
606
657
  * `payload[off .. off + len)` and, when `mask` is not null, masked with that
607
658
  * 4-byte key. A client masks every frame with a fresh key from
608
659
  * `crypto.getRandomValues` (§5.3); a server passes null. The length takes the
609
- * shortest of its three forms. Answers where the frame ends, `WS_NO_ROOM` when
610
- * it does not fit, or `WS_REFUSED` for an opcode RFC 6455 does not define, a
611
- * control frame that is not FIN or is longer than 125 bytes, and a mask that
612
- * is not 4 bytes; on either refusal `out` is untouched. A window outside
613
- * `payload` panics. A text frame's payload is not checked here, because a
614
- * fragment may end inside a character; `websocketIsUtf8` checks a whole
615
- * message.
660
+ * shortest of its three forms.
661
+ *
662
+ * Answers where the frame ends, `WS_NO_ROOM` when it does not fit, or
663
+ * `WS_REFUSED` for an opcode RFC 6455 does not define, a control frame that is
664
+ * not FIN or is longer than 125 bytes, and a mask that is not 4 bytes; on
665
+ * either refusal `out` is untouched. A window outside `payload` panics, and so
666
+ * does a payload longer than 2^31 - 15 bytes. A text frame's payload is not
667
+ * checked here, because a fragment may end inside a character;
668
+ * `websocketIsUtf8` checks a whole message.
669
+ *
670
+ * @param out The buffer the frame is written into.
671
+ * @param at The index in `out` the frame starts at; one outside `out` answers `WS_NO_ROOM`.
672
+ * @param fin Whether this is the message's last frame; a control frame must set it.
673
+ * @param opcode The frame's opcode, such as `WS_OP_TEXT` or `WS_OP_PING`.
674
+ * @param payload The array holding the payload; it is not changed, even when masked.
675
+ * @param off Where the payload starts in `payload`.
676
+ * @param len The payload's length in bytes.
677
+ * @param mask The 4-byte mask key for a client's frame, or null for a server's.
678
+ * @returns The index in `out` just past the frame, or `WS_NO_ROOM` or `WS_REFUSED`.
616
679
  */
617
680
  export const websocketWriteFrame = (
618
681
  out: u8[],
@@ -692,6 +755,14 @@ export const websocketWriteFrame = (
692
755
  /**
693
756
  * One frame, as `websocketWriteFrame` writes it, in an array of its own; the
694
757
  * same refusals answer `null`. A window outside `payload` panics.
758
+ *
759
+ * @param fin Whether this is the message's last frame; a control frame must set it.
760
+ * @param opcode The frame's opcode, such as `WS_OP_TEXT` or `WS_OP_PING`.
761
+ * @param payload The array holding the payload; it is not changed, even when masked.
762
+ * @param off Where the payload starts in `payload`.
763
+ * @param len The payload's length in bytes.
764
+ * @param mask The 4-byte mask key for a client's frame, or null for a server's.
765
+ * @returns The whole frame, `websocketFrameSize(len, mask !== null)` bytes, or null for a frame `websocketWriteFrame` refuses.
695
766
  */
696
767
  export const websocketFrame = (
697
768
  fin: boolean,
@@ -718,6 +789,10 @@ export const websocketFrame = (
718
789
  * Answers `null` for a code that may not be sent (1005, 1006 and 1015 among
719
790
  * them; §7.4), a reason that is not UTF-8, and one longer than 123 bytes,
720
791
  * which would take the frame past 125.
792
+ *
793
+ * @param code The close status code, such as 1000 for a normal closure.
794
+ * @param reason Why the connection closes, for a person to read; "" for no reason.
795
+ * @returns The payload for a `WS_OP_CLOSE` frame, the code big-endian and then the reason's bytes, or null when refused.
721
796
  */
722
797
  export const websocketClosePayload = (code: i32, reason: string): u8[] | null => {
723
798
  const n: i32 = toI32(reason.length)
@@ -773,6 +848,9 @@ const websocketSwapBytes = (text: string, from1: i32, to1: i32, from2: i32, to2:
773
848
  /**
774
849
  * `Sec-WebSocket-Accept` for the client's `Sec-WebSocket-Key` (§4.2.2 item
775
850
  * 5.4): the SHA-1 of the key and `WS_GUID`, in padded standard base64.
851
+ *
852
+ * @param key The key as the client sent it; it is not checked here, so check it with `websocketKeyIsValid` first.
853
+ * @returns The 28-character accept value.
776
854
  */
777
855
  export const websocketAcceptKey = (key: string): string =>
778
856
  base64StdPadEncode(sha1(websocketBytesOf(`${key}${WS_GUID}`)))
@@ -781,6 +859,9 @@ export const websocketAcceptKey = (key: string): string =>
781
859
  * Whether `key` is a valid `Sec-WebSocket-Key` (§4.2.1 item 5): padded
782
860
  * standard base64 that decodes to exactly 16 bytes, in its one canonical
783
861
  * spelling.
862
+ *
863
+ * @param key The field's value, as the client sent it.
864
+ * @returns True for a 24-character key ending in `==` whose other characters decode to 16 bytes.
784
865
  */
785
866
  export const websocketKeyIsValid = (key: string): boolean => {
786
867
  if (toI32(key.length) !== 24 || !key.endsWith("==")) {
@@ -802,6 +883,9 @@ export const websocketKeyIsValid = (key: string): boolean => {
802
883
  * (alone, in any case), at version 13, with a valid key. Otherwise `null`, and
803
884
  * the server answers 400 (or 426 with `Sec-WebSocket-Version: 13` when only the
804
885
  * version is wrong, §4.4).
886
+ *
887
+ * @param p The server's parser, at the `HTTP1_HEAD` of the request.
888
+ * @returns The `Sec-WebSocket-Key` to pass to `websocketUpgradeResponse`, or null when the request is not a valid handshake.
805
889
  */
806
890
  export const websocketRequestKey = (p: Http1Parser): string | null => {
807
891
  if (!p.methodIs("GET") || p.minor !== 1 || !p.upgradeIs("websocket")) {
@@ -822,6 +906,10 @@ export const websocketRequestKey = (p: Http1Parser): string | null => {
822
906
  * `Connection`, `Sec-WebSocket-Accept`, and `Sec-WebSocket-Protocol` when
823
907
  * `protocol` is not empty (the caller picks it from the client's list).
824
908
  * Answers `null` for an invalid key or a protocol that is not a token.
909
+ *
910
+ * @param key The client's `Sec-WebSocket-Key`, as `websocketRequestKey` gave it.
911
+ * @param protocol The subprotocol the server chose from `websocketOfferedProtocols`, or "" for none.
912
+ * @returns The whole response head, ending in the empty line, or null when refused.
825
913
  */
826
914
  export const websocketUpgradeResponse = (key: string, protocol: string): u8[] | null => {
827
915
  if (!websocketKeyIsValid(key)) {
@@ -844,6 +932,9 @@ export const websocketUpgradeResponse = (key: string, protocol: string): u8[] |
844
932
  * empty ones dropped (RFC 9110 §5.6.1). Answers `[]` when there is no such
845
933
  * field, and `null` when an element is not a token: a server picks one of
846
934
  * these, or none, for `websocketUpgradeResponse`.
935
+ *
936
+ * @param p The server's parser, at the `HTTP1_HEAD` of the request.
937
+ * @returns The offered subprotocols in order, `[]` when none are offered, or null when one is not a token.
847
938
  */
848
939
  export const websocketOfferedProtocols = (p: Http1Parser): string[] | null => {
849
940
  const out: string[] = []
@@ -941,6 +1032,9 @@ const websocketListHas = (value: string, word: string): boolean => {
941
1032
  * `crypto.getRandomValues` and fresh for every connection: a key that repeats
942
1033
  * or can be guessed lets a cache or an intermediary replay an old handshake's
943
1034
  * answer. This module draws no randomness itself.
1035
+ *
1036
+ * @param nonce 16 fresh random bytes; the caller keeps the returned key to check the response with.
1037
+ * @returns The 24-character key, or null when `nonce` is not 16 bytes.
944
1038
  */
945
1039
  export const websocketClientKey = (nonce: u8[]): string | null => {
946
1040
  if (toI32(nonce.length) !== 16) {
@@ -959,6 +1053,12 @@ export const websocketClientKey = (nonce: u8[]): string | null => {
959
1053
  * one, or one holding a comma, a space, CR or LF), a protocol offered twice, an
960
1054
  * empty host, and everything the request writer refuses (CR, LF or another
961
1055
  * control byte in `host`, a `target` that is not a request target).
1056
+ *
1057
+ * @param host The `Host` field's value: the server's host, and its port when it is not the scheme's default.
1058
+ * @param target The request target, such as `/chat?room=1`.
1059
+ * @param key The client's key, from `websocketClientKey`.
1060
+ * @param protocols The subprotocols the client offers, in order of preference; `[]` for none.
1061
+ * @returns The whole request head, ending in the empty line, or null when refused.
962
1062
  */
963
1063
  export const websocketUpgradeRequest = (
964
1064
  host: string,
@@ -1012,8 +1112,14 @@ export const WS_HS_PROTOCOL: i32 = 7
1012
1112
  export class WsHandshake {
1013
1113
  /** The subprotocol the server chose, or `""` when it chose none. */
1014
1114
  protocol: string = ""
1115
+ /** `WS_HS_OK`, or the `WS_HS_*` rule the response broke. */
1015
1116
  error: i32 = 0
1016
1117
 
1118
+ /**
1119
+ * The answer to one handshake check.
1120
+ * @param error `WS_HS_OK`, or the rule the response broke.
1121
+ * @param protocol The subprotocol the server chose, or `""`.
1122
+ */
1017
1123
  constructor(error: i32, protocol: string) {
1018
1124
  this.error = error
1019
1125
  this.protocol = protocol
@@ -1023,17 +1129,25 @@ export class WsHandshake {
1023
1129
  /**
1024
1130
  * Reads the server's answer to `websocketUpgradeRequest(…, key, offered)`
1025
1131
  * (§4.1, the client's checks of the response): call it at the parser's
1026
- * `HTTP1_HEAD`. It fails closed, and the connection is closed on any failure:
1027
- * a status other than 101 (`WS_HS_STATUS`; a redirect is not followed), a
1028
- * version other than HTTP/1.1, `Upgrade` absent, repeated or not `websocket`,
1029
- * no `Connection` listing `upgrade`, `Sec-WebSocket-Accept` absent, repeated
1030
- * or not `websocketAcceptKey(key)` (a forged or replayed answer), any
1031
- * `Sec-WebSocket-Extensions` (this client offers none, so none may be in use),
1032
- * and a `Sec-WebSocket-Protocol` that is present when nothing was offered,
1033
- * repeated, a list, or not one of `offered`. On `WS_HS_OK` the parser's next
1034
- * events are `HTTP1_END` and `HTTP1_UPGRADE`; its `tunnelBytes()` go to
1035
- * `WsDecoder(false, …)`, and every frame the client sends is masked with a
1036
- * fresh mask key from `crypto.getRandomValues` (§5.3).
1132
+ * `HTTP1_HEAD`.
1133
+ *
1134
+ * It fails closed, and the connection is closed on any failure: a status other
1135
+ * than 101 (`WS_HS_STATUS`; a redirect is not followed), a version other than
1136
+ * HTTP/1.1, `Upgrade` absent, repeated or not `websocket`, no `Connection`
1137
+ * listing `upgrade`, `Sec-WebSocket-Accept` absent, repeated or not
1138
+ * `websocketAcceptKey(key)` (a forged or replayed answer, or any answer when
1139
+ * `key` is not a valid key), any `Sec-WebSocket-Extensions` (this client offers
1140
+ * none, so none may be in use), and a `Sec-WebSocket-Protocol` that is present
1141
+ * when nothing was offered, repeated, a list, or not one of `offered`.
1142
+ *
1143
+ * On `WS_HS_OK` the parser's next events are `HTTP1_END` and `HTTP1_UPGRADE`;
1144
+ * its `tunnelBytes()` go to `WsDecoder(false, …)`, and every frame the client
1145
+ * sends is masked with a fresh mask key from `crypto.getRandomValues` (§5.3).
1146
+ *
1147
+ * @param response The client's parser, at the `HTTP1_HEAD` of the response.
1148
+ * @param key The key the request sent, from `websocketClientKey`.
1149
+ * @param offered The subprotocols the request offered, as passed to `websocketUpgradeRequest`.
1150
+ * @returns `WS_HS_OK` and the chosen subprotocol, or the first `WS_HS_*` rule the response broke.
1037
1151
  */
1038
1152
  export const websocketCheckResponse = (
1039
1153
  response: Http1ResponseParser,