@amritk/nish-x86_64-linux 0.35.0 → 0.36.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/INSTALL.md +6 -1
- package/bin/nish +0 -0
- package/docs/AI.md +9 -2
- package/package.json +1 -1
- package/runtime/nish.d.ts +4612 -430
- package/runtime/vite.d.ts +30 -0
- package/runtime/vite.mjs +150 -0
- package/scripts/build.sh +108 -13
- package/std/base64.ts +34 -5
- package/std/bytes.ts +256 -40
- package/std/cli-args.ts +296 -70
- package/std/cli-flags.ts +184 -19
- package/std/cli.ts +475 -54
- package/std/collections.ts +121 -23
- package/std/compress/checksum.ts +44 -14
- package/std/compress/deflate.ts +224 -111
- package/std/compress/gzip.ts +217 -53
- package/std/compress/zlib.ts +63 -7
- package/std/crypto/aes.ts +80 -9
- package/std/crypto/base64url.ts +22 -0
- package/std/crypto/bcrypt-batch.ts +88 -21
- package/std/crypto/bcrypt.ts +60 -0
- package/std/crypto/chacha20poly1305.ts +53 -0
- package/std/crypto/ct.ts +32 -13
- package/std/crypto/ed25519.ts +47 -0
- package/std/crypto/hkdf.ts +198 -13
- package/std/crypto/hmac.ts +235 -31
- package/std/crypto/jwt.ts +192 -63
- package/std/crypto/p256.ts +159 -16
- package/std/crypto/p384.ts +28 -3
- package/std/crypto/pki-roots.ts +32 -2
- package/std/crypto/pki.ts +81 -15
- package/std/crypto/rsa.ts +33 -3
- package/std/crypto/sha1.ts +3 -0
- package/std/crypto/sha256.ts +24 -1
- package/std/crypto/sha512.ts +99 -14
- package/std/crypto/x25519.ts +152 -14
- package/std/crypto/x509-random.ts +10 -0
- package/std/crypto/x509.ts +67 -7
- package/std/json-equal.ts +19 -0
- package/std/json-format.ts +112 -13
- package/std/json-number.ts +64 -14
- package/std/json-pointer.ts +23 -1
- package/std/json-tokenizer.ts +100 -29
- package/std/json-typed.ts +713 -66
- package/std/json-value.ts +400 -124
- package/std/json.ts +138 -10
- package/std/map.ts +25 -7
- package/std/math.ts +36 -4
- package/std/net/buffer-pool.ts +42 -11
- package/std/net/dns-config.ts +109 -23
- package/std/net/dns-message.ts +62 -7
- package/std/net/dns-resolver.ts +247 -53
- package/std/net/dns.ts +203 -35
- package/std/net/fetch.ts +151 -61
- package/std/net/handler.ts +278 -80
- package/std/net/hpack.ts +262 -52
- package/std/net/http-compress.ts +125 -15
- package/std/net/http-fields.ts +85 -9
- package/std/net/http1-client-codec.ts +100 -19
- package/std/net/http1-codec.ts +235 -27
- package/std/net/http1-server.ts +343 -85
- package/std/net/http2-frame.ts +272 -35
- package/std/net/http2-tls.ts +135 -34
- package/std/net/http2.ts +247 -106
- package/std/net/http3-frame.ts +154 -17
- package/std/net/http3-server.ts +246 -96
- package/std/net/http3.ts +307 -115
- package/std/net/outbound.ts +37 -3
- package/std/net/postgres-carrier.ts +144 -8
- package/std/net/postgres.ts +723 -103
- package/std/net/prefork.ts +164 -31
- package/std/net/qpack.ts +105 -18
- package/std/net/quic-conn-ack.ts +59 -12
- package/std/net/quic-conn-cid.ts +124 -27
- package/std/net/quic-conn-params.ts +70 -10
- package/std/net/quic-datagram.ts +43 -3
- package/std/net/quic-frame.ts +286 -12
- package/std/net/quic-listener.ts +101 -32
- package/std/net/quic-packet.ts +302 -21
- package/std/net/quic-recovery.ts +228 -44
- package/std/net/quic-stream.ts +353 -91
- package/std/net/quic.ts +489 -171
- package/std/net/router.ts +87 -40
- package/std/net/tls/client.ts +166 -51
- package/std/net/tls/codec.ts +413 -56
- package/std/net/tls/record-client.ts +110 -55
- package/std/net/tls/record-server.ts +159 -59
- package/std/net/tls/record.ts +109 -12
- package/std/net/tls/schedule.ts +235 -26
- package/std/net/tls/ticket.ts +159 -40
- package/std/net/tls/verify.ts +49 -3
- package/std/net/tls-tcp.ts +250 -45
- package/std/net/tls.ts +221 -59
- package/std/net/upstream.ts +81 -45
- package/std/net/url.ts +97 -13
- package/std/net/websocket.ts +122 -12
- package/std/net/webtransport.ts +220 -89
- package/std/pair.ts +10 -2
- package/std/random.ts +34 -9
- package/std/regex.ts +352 -198
- package/std/region.ts +70 -15
- package/std/secret.ts +52 -11
- package/std/sort.ts +17 -2
- package/std/sqlite.ts +256 -35
- package/std/sync/ring.ts +205 -35
- package/std/sync/shm.ts +194 -16
- package/std/term-io.ts +137 -64
- package/std/term-pixels.ts +144 -19
- package/std/term-probe.ts +66 -25
- package/std/term-rate.ts +128 -57
- package/std/term.ts +332 -121
- package/std/testing.ts +130 -32
- package/std/text.ts +73 -7
- package/std/threads.ts +229 -41
- package/std/yaml-json.ts +94 -82
- package/std/yaml-lines.ts +47 -15
- package/std/yaml-write.ts +24 -16
- package/std/yaml.ts +764 -384
package/std/net/websocket.ts
CHANGED
|
@@ -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
|
-
/**
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
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) {
|
|
@@ -610,9 +661,19 @@ export const websocketFrameSize = (len: i32, masked: boolean): i32 => {
|
|
|
610
661
|
* it does not fit, or `WS_REFUSED` for an opcode RFC 6455 does not define, a
|
|
611
662
|
* control frame that is not FIN or is longer than 125 bytes, and a mask that
|
|
612
663
|
* is not 4 bytes; on either refusal `out` is untouched. A window outside
|
|
613
|
-
* `payload` panics
|
|
614
|
-
*
|
|
615
|
-
* message.
|
|
664
|
+
* `payload` panics, and so does a payload longer than 2^31 - 15 bytes. A text
|
|
665
|
+
* frame's payload is not checked here, because a fragment may end inside a
|
|
666
|
+
* character; `websocketIsUtf8` checks a whole message.
|
|
667
|
+
*
|
|
668
|
+
* @param out The buffer the frame is written into.
|
|
669
|
+
* @param at The index in `out` the frame starts at; one outside `out` answers `WS_NO_ROOM`.
|
|
670
|
+
* @param fin Whether this is the message's last frame; a control frame must set it.
|
|
671
|
+
* @param opcode The frame's opcode, such as `WS_OP_TEXT` or `WS_OP_PING`.
|
|
672
|
+
* @param payload The array holding the payload; it is not changed, even when masked.
|
|
673
|
+
* @param off Where the payload starts in `payload`.
|
|
674
|
+
* @param len The payload's length in bytes.
|
|
675
|
+
* @param mask The 4-byte mask key for a client's frame, or null for a server's.
|
|
676
|
+
* @returns The index in `out` just past the frame, or `WS_NO_ROOM` or `WS_REFUSED`.
|
|
616
677
|
*/
|
|
617
678
|
export const websocketWriteFrame = (
|
|
618
679
|
out: u8[],
|
|
@@ -692,6 +753,14 @@ export const websocketWriteFrame = (
|
|
|
692
753
|
/**
|
|
693
754
|
* One frame, as `websocketWriteFrame` writes it, in an array of its own; the
|
|
694
755
|
* same refusals answer `null`. A window outside `payload` panics.
|
|
756
|
+
*
|
|
757
|
+
* @param fin Whether this is the message's last frame; a control frame must set it.
|
|
758
|
+
* @param opcode The frame's opcode, such as `WS_OP_TEXT` or `WS_OP_PING`.
|
|
759
|
+
* @param payload The array holding the payload; it is not changed, even when masked.
|
|
760
|
+
* @param off Where the payload starts in `payload`.
|
|
761
|
+
* @param len The payload's length in bytes.
|
|
762
|
+
* @param mask The 4-byte mask key for a client's frame, or null for a server's.
|
|
763
|
+
* @returns The whole frame, `websocketFrameSize(len, mask !== null)` bytes, or null for a frame `websocketWriteFrame` refuses.
|
|
695
764
|
*/
|
|
696
765
|
export const websocketFrame = (
|
|
697
766
|
fin: boolean,
|
|
@@ -718,6 +787,10 @@ export const websocketFrame = (
|
|
|
718
787
|
* Answers `null` for a code that may not be sent (1005, 1006 and 1015 among
|
|
719
788
|
* them; §7.4), a reason that is not UTF-8, and one longer than 123 bytes,
|
|
720
789
|
* which would take the frame past 125.
|
|
790
|
+
*
|
|
791
|
+
* @param code The close status code, such as 1000 for a normal closure.
|
|
792
|
+
* @param reason Why the connection closes, for a person to read; "" for no reason.
|
|
793
|
+
* @returns The payload for a `WS_OP_CLOSE` frame, the code big-endian and then the reason's bytes, or null when refused.
|
|
721
794
|
*/
|
|
722
795
|
export const websocketClosePayload = (code: i32, reason: string): u8[] | null => {
|
|
723
796
|
const n: i32 = toI32(reason.length)
|
|
@@ -773,6 +846,9 @@ const websocketSwapBytes = (text: string, from1: i32, to1: i32, from2: i32, to2:
|
|
|
773
846
|
/**
|
|
774
847
|
* `Sec-WebSocket-Accept` for the client's `Sec-WebSocket-Key` (§4.2.2 item
|
|
775
848
|
* 5.4): the SHA-1 of the key and `WS_GUID`, in padded standard base64.
|
|
849
|
+
*
|
|
850
|
+
* @param key The key as the client sent it; it is not checked here, so check it with `websocketKeyIsValid` first.
|
|
851
|
+
* @returns The 28-character accept value.
|
|
776
852
|
*/
|
|
777
853
|
export const websocketAcceptKey = (key: string): string =>
|
|
778
854
|
base64StdPadEncode(sha1(websocketBytesOf(`${key}${WS_GUID}`)))
|
|
@@ -781,6 +857,9 @@ export const websocketAcceptKey = (key: string): string =>
|
|
|
781
857
|
* Whether `key` is a valid `Sec-WebSocket-Key` (§4.2.1 item 5): padded
|
|
782
858
|
* standard base64 that decodes to exactly 16 bytes, in its one canonical
|
|
783
859
|
* spelling.
|
|
860
|
+
*
|
|
861
|
+
* @param key The field's value, as the client sent it.
|
|
862
|
+
* @returns True for a 24-character key ending in `==` whose other characters decode to 16 bytes.
|
|
784
863
|
*/
|
|
785
864
|
export const websocketKeyIsValid = (key: string): boolean => {
|
|
786
865
|
if (toI32(key.length) !== 24 || !key.endsWith("==")) {
|
|
@@ -802,6 +881,9 @@ export const websocketKeyIsValid = (key: string): boolean => {
|
|
|
802
881
|
* (alone, in any case), at version 13, with a valid key. Otherwise `null`, and
|
|
803
882
|
* the server answers 400 (or 426 with `Sec-WebSocket-Version: 13` when only the
|
|
804
883
|
* version is wrong, §4.4).
|
|
884
|
+
*
|
|
885
|
+
* @param p The server's parser, at the `HTTP1_HEAD` of the request.
|
|
886
|
+
* @returns The `Sec-WebSocket-Key` to pass to `websocketUpgradeResponse`, or null when the request is not a valid handshake.
|
|
805
887
|
*/
|
|
806
888
|
export const websocketRequestKey = (p: Http1Parser): string | null => {
|
|
807
889
|
if (!p.methodIs("GET") || p.minor !== 1 || !p.upgradeIs("websocket")) {
|
|
@@ -822,6 +904,10 @@ export const websocketRequestKey = (p: Http1Parser): string | null => {
|
|
|
822
904
|
* `Connection`, `Sec-WebSocket-Accept`, and `Sec-WebSocket-Protocol` when
|
|
823
905
|
* `protocol` is not empty (the caller picks it from the client's list).
|
|
824
906
|
* Answers `null` for an invalid key or a protocol that is not a token.
|
|
907
|
+
*
|
|
908
|
+
* @param key The client's `Sec-WebSocket-Key`, as `websocketRequestKey` gave it.
|
|
909
|
+
* @param protocol The subprotocol the server chose from `websocketOfferedProtocols`, or "" for none.
|
|
910
|
+
* @returns The whole response head, ending in the empty line, or null when refused.
|
|
825
911
|
*/
|
|
826
912
|
export const websocketUpgradeResponse = (key: string, protocol: string): u8[] | null => {
|
|
827
913
|
if (!websocketKeyIsValid(key)) {
|
|
@@ -844,6 +930,9 @@ export const websocketUpgradeResponse = (key: string, protocol: string): u8[] |
|
|
|
844
930
|
* empty ones dropped (RFC 9110 §5.6.1). Answers `[]` when there is no such
|
|
845
931
|
* field, and `null` when an element is not a token: a server picks one of
|
|
846
932
|
* these, or none, for `websocketUpgradeResponse`.
|
|
933
|
+
*
|
|
934
|
+
* @param p The server's parser, at the `HTTP1_HEAD` of the request.
|
|
935
|
+
* @returns The offered subprotocols in order, `[]` when none are offered, or null when one is not a token.
|
|
847
936
|
*/
|
|
848
937
|
export const websocketOfferedProtocols = (p: Http1Parser): string[] | null => {
|
|
849
938
|
const out: string[] = []
|
|
@@ -941,6 +1030,9 @@ const websocketListHas = (value: string, word: string): boolean => {
|
|
|
941
1030
|
* `crypto.getRandomValues` and fresh for every connection: a key that repeats
|
|
942
1031
|
* or can be guessed lets a cache or an intermediary replay an old handshake's
|
|
943
1032
|
* answer. This module draws no randomness itself.
|
|
1033
|
+
*
|
|
1034
|
+
* @param nonce 16 fresh random bytes; the caller keeps the returned key to check the response with.
|
|
1035
|
+
* @returns The 24-character key, or null when `nonce` is not 16 bytes.
|
|
944
1036
|
*/
|
|
945
1037
|
export const websocketClientKey = (nonce: u8[]): string | null => {
|
|
946
1038
|
if (toI32(nonce.length) !== 16) {
|
|
@@ -959,6 +1051,12 @@ export const websocketClientKey = (nonce: u8[]): string | null => {
|
|
|
959
1051
|
* one, or one holding a comma, a space, CR or LF), a protocol offered twice, an
|
|
960
1052
|
* empty host, and everything the request writer refuses (CR, LF or another
|
|
961
1053
|
* control byte in `host`, a `target` that is not a request target).
|
|
1054
|
+
*
|
|
1055
|
+
* @param host The `Host` field's value: the server's host, and its port when it is not the scheme's default.
|
|
1056
|
+
* @param target The request target, such as `/chat?room=1`.
|
|
1057
|
+
* @param key The client's key, from `websocketClientKey`.
|
|
1058
|
+
* @param protocols The subprotocols the client offers, in order of preference; `[]` for none.
|
|
1059
|
+
* @returns The whole request head, ending in the empty line, or null when refused.
|
|
962
1060
|
*/
|
|
963
1061
|
export const websocketUpgradeRequest = (
|
|
964
1062
|
host: string,
|
|
@@ -1012,8 +1110,14 @@ export const WS_HS_PROTOCOL: i32 = 7
|
|
|
1012
1110
|
export class WsHandshake {
|
|
1013
1111
|
/** The subprotocol the server chose, or `""` when it chose none. */
|
|
1014
1112
|
protocol: string = ""
|
|
1113
|
+
/** `WS_HS_OK`, or the `WS_HS_*` rule the response broke. */
|
|
1015
1114
|
error: i32 = 0
|
|
1016
1115
|
|
|
1116
|
+
/**
|
|
1117
|
+
* The answer to one handshake check.
|
|
1118
|
+
* @param error `WS_HS_OK`, or the rule the response broke.
|
|
1119
|
+
* @param protocol The subprotocol the server chose, or `""`.
|
|
1120
|
+
*/
|
|
1017
1121
|
constructor(error: i32, protocol: string) {
|
|
1018
1122
|
this.error = error
|
|
1019
1123
|
this.protocol = protocol
|
|
@@ -1027,13 +1131,19 @@ export class WsHandshake {
|
|
|
1027
1131
|
* a status other than 101 (`WS_HS_STATUS`; a redirect is not followed), a
|
|
1028
1132
|
* version other than HTTP/1.1, `Upgrade` absent, repeated or not `websocket`,
|
|
1029
1133
|
* no `Connection` listing `upgrade`, `Sec-WebSocket-Accept` absent, repeated
|
|
1030
|
-
* or not `websocketAcceptKey(key)` (a forged or replayed answer
|
|
1134
|
+
* or not `websocketAcceptKey(key)` (a forged or replayed answer, or any answer
|
|
1135
|
+
* when `key` is not a valid key), any
|
|
1031
1136
|
* `Sec-WebSocket-Extensions` (this client offers none, so none may be in use),
|
|
1032
1137
|
* and a `Sec-WebSocket-Protocol` that is present when nothing was offered,
|
|
1033
1138
|
* repeated, a list, or not one of `offered`. On `WS_HS_OK` the parser's next
|
|
1034
1139
|
* events are `HTTP1_END` and `HTTP1_UPGRADE`; its `tunnelBytes()` go to
|
|
1035
1140
|
* `WsDecoder(false, …)`, and every frame the client sends is masked with a
|
|
1036
1141
|
* fresh mask key from `crypto.getRandomValues` (§5.3).
|
|
1142
|
+
*
|
|
1143
|
+
* @param response The client's parser, at the `HTTP1_HEAD` of the response.
|
|
1144
|
+
* @param key The key the request sent, from `websocketClientKey`.
|
|
1145
|
+
* @param offered The subprotocols the request offered, as passed to `websocketUpgradeRequest`.
|
|
1146
|
+
* @returns `WS_HS_OK` and the chosen subprotocol, or the first `WS_HS_*` rule the response broke.
|
|
1037
1147
|
*/
|
|
1038
1148
|
export const websocketCheckResponse = (
|
|
1039
1149
|
response: Http1ResponseParser,
|