@netifyjs/netify 0.0.0-stage → 1.0.0-alpha.1

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 (143) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +105 -2
  3. package/package.json +65 -4
  4. package/src/bootstrap/client/index.ts +191 -0
  5. package/src/bootstrap/dispatcher/index.ts +204 -0
  6. package/src/bootstrap/index.ts +22 -0
  7. package/src/bootstrap/reconnect/index.ts +334 -0
  8. package/src/bootstrap/server/index.ts +284 -0
  9. package/src/buffer/bits/index.ts +2 -0
  10. package/src/buffer/bits/reader/index.ts +64 -0
  11. package/src/buffer/bits/writer/index.ts +79 -0
  12. package/src/buffer/index.ts +1375 -0
  13. package/src/buffer/pool/index.ts +46 -0
  14. package/src/buffer/quantizer/index.ts +101 -0
  15. package/src/channel/attribute/index.ts +89 -0
  16. package/src/channel/context/index.ts +621 -0
  17. package/src/channel/group/index.ts +256 -0
  18. package/src/channel/handler/duplex/index.ts +105 -0
  19. package/src/channel/handler/inbound/index.ts +78 -0
  20. package/src/channel/handler/index.ts +135 -0
  21. package/src/channel/handler/outbound/index.ts +33 -0
  22. package/src/channel/index.ts +619 -0
  23. package/src/channel/initializer/index.ts +77 -0
  24. package/src/channel/outbound/index.ts +611 -0
  25. package/src/channel/pipeline/head/index.ts +100 -0
  26. package/src/channel/pipeline/index.ts +414 -0
  27. package/src/channel/pipeline/tail/index.ts +45 -0
  28. package/src/channel/pipeline/tracker/index.ts +120 -0
  29. package/src/channel/queue/index.ts +203 -0
  30. package/src/channel/resolved/index.ts +34 -0
  31. package/src/channel/server/index.ts +164 -0
  32. package/src/channel/shutdown/index.ts +25 -0
  33. package/src/channel/simple/index.ts +38 -0
  34. package/src/channel/transport/index.ts +74 -0
  35. package/src/channel/transport/socket/index.ts +80 -0
  36. package/src/cluster/index.ts +360 -0
  37. package/src/codec/binary/index.ts +102 -0
  38. package/src/codec/decoder/index.ts +479 -0
  39. package/src/codec/delimiter/index.ts +189 -0
  40. package/src/codec/encoder/index.ts +48 -0
  41. package/src/codec/error/index.ts +33 -0
  42. package/src/codec/fixed/index.ts +45 -0
  43. package/src/codec/http/aggregator/index.ts +236 -0
  44. package/src/codec/http/client/index.ts +120 -0
  45. package/src/codec/http/decoder/index.ts +4 -0
  46. package/src/codec/http/decoder/object/index.ts +644 -0
  47. package/src/codec/http/decoder/request/index.ts +49 -0
  48. package/src/codec/http/decoder/response/index.ts +59 -0
  49. package/src/codec/http/encoder/index.ts +3 -0
  50. package/src/codec/http/encoder/object/index.ts +196 -0
  51. package/src/codec/http/encoder/request/index.ts +24 -0
  52. package/src/codec/http/encoder/response/index.ts +26 -0
  53. package/src/codec/http/headers/index.ts +287 -0
  54. package/src/codec/http/index.ts +33 -0
  55. package/src/codec/http/message/content/index.ts +36 -0
  56. package/src/codec/http/message/full/index.ts +10 -0
  57. package/src/codec/http/message/full/request/index.ts +45 -0
  58. package/src/codec/http/message/full/response/index.ts +43 -0
  59. package/src/codec/http/message/head/index.ts +79 -0
  60. package/src/codec/http/message/index.ts +16 -0
  61. package/src/codec/http/message/last/index.ts +27 -0
  62. package/src/codec/http/message/request/index.ts +39 -0
  63. package/src/codec/http/message/response/index.ts +31 -0
  64. package/src/codec/http/method/index.ts +101 -0
  65. package/src/codec/http/server/index.ts +81 -0
  66. package/src/codec/http/status/index.ts +170 -0
  67. package/src/codec/http/version/index.ts +70 -0
  68. package/src/codec/index.ts +83 -0
  69. package/src/codec/length/index.ts +330 -0
  70. package/src/codec/line/index.ts +45 -0
  71. package/src/codec/message/decoder/index.ts +84 -0
  72. package/src/codec/message/encoder/index.ts +56 -0
  73. package/src/codec/message/index.ts +2 -0
  74. package/src/codec/msgpack/index.ts +51 -0
  75. package/src/codec/prepender/index.ts +296 -0
  76. package/src/codec/protobuf/index.ts +57 -0
  77. package/src/codec/string/decoder/index.ts +47 -0
  78. package/src/codec/string/encoder/index.ts +26 -0
  79. package/src/codec/string/index.ts +2 -0
  80. package/src/codec/websocket/client/index.ts +235 -0
  81. package/src/codec/websocket/decoder/index.ts +371 -0
  82. package/src/codec/websocket/encoder/index.ts +170 -0
  83. package/src/codec/websocket/error/index.ts +42 -0
  84. package/src/codec/websocket/frame/base/index.ts +55 -0
  85. package/src/codec/websocket/frame/binary/index.ts +24 -0
  86. package/src/codec/websocket/frame/close/index.ts +82 -0
  87. package/src/codec/websocket/frame/continuation/index.ts +31 -0
  88. package/src/codec/websocket/frame/index.ts +10 -0
  89. package/src/codec/websocket/frame/opcode/index.ts +35 -0
  90. package/src/codec/websocket/frame/ping/index.ts +24 -0
  91. package/src/codec/websocket/frame/pong/index.ts +24 -0
  92. package/src/codec/websocket/frame/text/index.ts +31 -0
  93. package/src/codec/websocket/handshake/index.ts +38 -0
  94. package/src/codec/websocket/index.ts +25 -0
  95. package/src/codec/websocket/key/index.ts +41 -0
  96. package/src/codec/websocket/protocol/index.ts +147 -0
  97. package/src/codec/websocket/server/index.ts +233 -0
  98. package/src/codec/websocket/status/index.ts +97 -0
  99. package/src/codec/websocket/upgrade/index.ts +116 -0
  100. package/src/datagram/bootstrap/index.ts +269 -0
  101. package/src/datagram/channel/index.ts +103 -0
  102. package/src/datagram/index.ts +10 -0
  103. package/src/datagram/packet/index.ts +72 -0
  104. package/src/datagram/transport/index.ts +239 -0
  105. package/src/handler/flush/index.ts +187 -0
  106. package/src/handler/idle/index.ts +295 -0
  107. package/src/handler/index.ts +22 -0
  108. package/src/handler/logging/index.ts +195 -0
  109. package/src/handler/metrics/index.ts +300 -0
  110. package/src/handler/offload/index.ts +141 -0
  111. package/src/handler/timeout/error/index.ts +29 -0
  112. package/src/handler/timeout/index.ts +3 -0
  113. package/src/handler/timeout/read/index.ts +44 -0
  114. package/src/handler/timeout/write/index.ts +89 -0
  115. package/src/handler/traffic/index.ts +259 -0
  116. package/src/index.ts +258 -0
  117. package/src/logger/index.ts +47 -0
  118. package/src/reliable/bootstrap/index.ts +154 -0
  119. package/src/reliable/cipher/index.ts +227 -0
  120. package/src/reliable/cookie/index.ts +78 -0
  121. package/src/reliable/handler/index.ts +1202 -0
  122. package/src/reliable/handshake/index.ts +144 -0
  123. package/src/reliable/index.ts +38 -0
  124. package/src/reliable/lane/index.ts +420 -0
  125. package/src/reliable/message/index.ts +102 -0
  126. package/src/reliable/packet/index.ts +117 -0
  127. package/src/reliable/peer/index.ts +929 -0
  128. package/src/reliable/registry/index.ts +107 -0
  129. package/src/serve/bootstrap/index.ts +606 -0
  130. package/src/serve/channel/index.ts +246 -0
  131. package/src/serve/index.ts +17 -0
  132. package/src/serve/outbox/index.ts +292 -0
  133. package/src/serve/transport/index.ts +96 -0
  134. package/src/serve/writer/index.ts +119 -0
  135. package/src/shard/envelope/index.ts +44 -0
  136. package/src/shard/gateway/index.ts +119 -0
  137. package/src/shard/index.ts +11 -0
  138. package/src/shard/link/index.ts +743 -0
  139. package/src/shard/map/index.ts +91 -0
  140. package/src/worker/group/index.ts +175 -0
  141. package/src/worker/index.ts +10 -0
  142. package/src/worker/pool/index.ts +235 -0
  143. package/src/worker/service/index.ts +55 -0
@@ -0,0 +1,170 @@
1
+ import { ByteBuf } from '../../../buffer/index.ts'
2
+ import type { HandlerContext } from '../../../channel/context/index.ts'
3
+ import { MessageToByteEncoder } from '../../encoder/index.ts'
4
+ import { EncoderError } from '../../error/index.ts'
5
+ import { WebSocketFrame } from '../frame/base/index.ts'
6
+
7
+ /**
8
+ * Settings for WebSocketFrameEncoder
9
+ */
10
+ export type WebSocketEncoderOptions = {
11
+ /**
12
+ * True on clients, which have to mask every frame
13
+ */
14
+ readonly maskPayload: boolean
15
+ }
16
+
17
+ /**
18
+ * Writes WebSocket frames out in RFC 6455 format
19
+ * @class
20
+ */
21
+ export class WebSocketFrameEncoder extends MessageToByteEncoder<WebSocketFrame> {
22
+ /**
23
+ * Adds a random mask key to each frame when set
24
+ */
25
+ private readonly maskPayload: boolean
26
+
27
+ /**
28
+ * Sets up an encoder for one side of the connection
29
+ * @param options whether to mask
30
+ */
31
+ public constructor(options: WebSocketEncoderOptions) {
32
+ super()
33
+
34
+ this.maskPayload = options.maskPayload
35
+ }
36
+
37
+ /**
38
+ * Pool of random bytes for mask keys, so we don't call into crypto every frame
39
+ */
40
+ private static readonly entropy: Uint8Array = new Uint8Array(4096)
41
+
42
+ /**
43
+ * Offset of the next fresh byte, starting full so the first call fills the pool
44
+ */
45
+ private static cursor = 4096
46
+
47
+ /**
48
+ * XORs the payload with the four byte mask key, which also undoes it
49
+ * @param bytes changed in place
50
+ * @param key four byte mask key
51
+ */
52
+ public static mask(bytes: Uint8Array, key: Uint8Array): Uint8Array {
53
+ WebSocketFrameEncoder.xor(bytes, bytes, 0, key, 0)
54
+
55
+ return bytes
56
+ }
57
+
58
+ /**
59
+ * Masks while copying, four bytes at a time with a tail loop for the rest
60
+ * @param source payload to read from
61
+ * @param target buffer to write into
62
+ * @param offset first byte to write in the target
63
+ * @param key any array with the four key bytes in it
64
+ * @param start position of the key within that array
65
+ */
66
+ private static xor(
67
+ source: Uint8Array,
68
+ target: Uint8Array,
69
+ offset: number,
70
+ key: Uint8Array,
71
+ start: number,
72
+ ): void {
73
+ const first = key[start] ?? 0
74
+ const second = key[start + 1] ?? 0
75
+ const third = key[start + 2] ?? 0
76
+ const fourth = key[start + 3] ?? 0
77
+ const length = source.length
78
+ const whole = length - (length & 3)
79
+
80
+ let index = 0
81
+ for (; index < whole; index += 4) {
82
+ target[offset + index] = (source[index] ?? 0) ^ first
83
+ target[offset + index + 1] = (source[index + 1] ?? 0) ^ second
84
+ target[offset + index + 2] = (source[index + 2] ?? 0) ^ third
85
+ target[offset + index + 3] = (source[index + 3] ?? 0) ^ fourth
86
+ }
87
+
88
+ for (; index < length; index++) {
89
+ const byte = index & 3
90
+ const part =
91
+ byte === 0 ? first : byte === 1 ? second : byte === 2 ? third : fourth
92
+ target[offset + index] = (source[index] ?? 0) ^ part
93
+ }
94
+ }
95
+
96
+ /**
97
+ * Hands out the offset of four fresh bytes in the pool, refilling it when it runs dry
98
+ */
99
+ private static nextKey(): number {
100
+ if (
101
+ WebSocketFrameEncoder.cursor + 4 >
102
+ WebSocketFrameEncoder.entropy.length
103
+ ) {
104
+ crypto.getRandomValues(WebSocketFrameEncoder.entropy)
105
+ WebSocketFrameEncoder.cursor = 0
106
+ }
107
+
108
+ const start = WebSocketFrameEncoder.cursor
109
+ WebSocketFrameEncoder.cursor += 4
110
+ return start
111
+ }
112
+
113
+ /**
114
+ * Takes any WebSocketFrame
115
+ * @param message the outbound message
116
+ */
117
+ protected accepts(message: unknown): message is WebSocketFrame {
118
+ return message instanceof WebSocketFrame
119
+ }
120
+
121
+ /**
122
+ * Writes the header, the mask key if any and the payload in one buffer
123
+ * @param context unused here
124
+ * @param frame frame to send, control frames must be final and 125 bytes or less
125
+ */
126
+ protected encode(context: HandlerContext, frame: WebSocketFrame): ByteBuf {
127
+ const payload = frame.content.toUint8Array()
128
+ const length = payload.length
129
+ if (frame.isControl && (length > 125 || !frame.finalFragment)) {
130
+ throw new EncoderError('Control frame is invalid')
131
+ }
132
+
133
+ const extended = length < 126 ? 0 : length <= 0xffff ? 2 : 8
134
+ const header = 2 + extended + (this.maskPayload ? 4 : 0)
135
+ const bytes = new Uint8Array(header + length)
136
+
137
+ bytes[0] = (frame.finalFragment ? 0x80 : 0) | frame.opcode
138
+ const mask = this.maskPayload ? 0x80 : 0
139
+ if (extended === 0) {
140
+ bytes[1] = mask | length
141
+ } else if (extended === 2) {
142
+ bytes[1] = mask | 126
143
+ bytes[2] = length >>> 8
144
+ bytes[3] = length & 0xff
145
+ } else {
146
+ bytes[1] = mask | 127
147
+ new DataView(bytes.buffer).setBigUint64(2, BigInt(length))
148
+ }
149
+
150
+ if (!this.maskPayload) {
151
+ bytes.set(payload, header)
152
+ return ByteBuf.wrap(bytes)
153
+ }
154
+
155
+ const start = WebSocketFrameEncoder.nextKey()
156
+ const keyAt = 2 + extended
157
+ for (let index = 0; index < 4; index++) {
158
+ bytes[keyAt + index] = WebSocketFrameEncoder.entropy[start + index] ?? 0
159
+ }
160
+
161
+ WebSocketFrameEncoder.xor(
162
+ payload,
163
+ bytes,
164
+ header,
165
+ WebSocketFrameEncoder.entropy,
166
+ start,
167
+ )
168
+ return ByteBuf.wrap(bytes)
169
+ }
170
+ }
@@ -0,0 +1,42 @@
1
+ import { DecoderError } from '../../error/index.ts'
2
+
3
+ /**
4
+ * Thrown when a peer's frame breaks the protocol
5
+ * @class
6
+ */
7
+ export class CorruptedFrameError extends DecoderError {
8
+ /**
9
+ * Close code we should send back before hanging up
10
+ */
11
+ public readonly status: number
12
+
13
+ /**
14
+ * Builds the error with the close code to reply with
15
+ * @param message short description of the problem
16
+ * @param status close code, such as 1002
17
+ * @param options usually carries the underlying cause
18
+ */
19
+ public constructor(message: string, status: number, options?: ErrorOptions) {
20
+ super(message, options)
21
+
22
+ this.name = 'CorruptedFrameError'
23
+ this.status = status
24
+ }
25
+ }
26
+
27
+ /**
28
+ * Thrown when the opening handshake goes wrong
29
+ * @class
30
+ */
31
+ export class HandshakeError extends Error {
32
+ /**
33
+ * Builds a handshake error
34
+ * @param message short description of the problem
35
+ * @param options usually carries the underlying cause
36
+ */
37
+ public constructor(message: string, options?: ErrorOptions) {
38
+ super(message, options)
39
+
40
+ this.name = 'HandshakeError'
41
+ }
42
+ }
@@ -0,0 +1,55 @@
1
+ import { ByteBuf } from '../../../../buffer/index.ts'
2
+
3
+ /**
4
+ * Anything we can turn into a payload, strings go in as UTF-8
5
+ */
6
+ export type FrameSource = ByteBuf | Uint8Array | string
7
+
8
+ /**
9
+ * Base for every WebSocket frame type
10
+ * @class
11
+ */
12
+ export abstract class WebSocketFrame {
13
+ /**
14
+ * False on every fragment but the last
15
+ */
16
+ public readonly finalFragment: boolean
17
+
18
+ /**
19
+ * Payload bytes, never masked
20
+ */
21
+ public readonly content: ByteBuf
22
+
23
+ /**
24
+ * Stores the payload and FIN flag
25
+ * @param content bytes or text
26
+ * @param finalFragment the FIN bit
27
+ */
28
+ protected constructor(content: FrameSource, finalFragment: boolean) {
29
+ this.content = WebSocketFrame.bufferOf(content)
30
+ this.finalFragment = finalFragment
31
+ }
32
+
33
+ /**
34
+ * Turns bytes or text into a ByteBuf, wrapping rather than copying arrays
35
+ * @param source what to convert
36
+ */
37
+ private static bufferOf(source: FrameSource): ByteBuf {
38
+ if (source instanceof ByteBuf) return source
39
+ if (typeof source === 'string') return ByteBuf.fromString(source)
40
+
41
+ return ByteBuf.wrap(source)
42
+ }
43
+
44
+ /**
45
+ * Four bit opcode for the frame header
46
+ */
47
+ public abstract get opcode(): number
48
+
49
+ /**
50
+ * Close, ping and pong, which can't be fragmented
51
+ */
52
+ public get isControl(): boolean {
53
+ return this.opcode >= 0x8
54
+ }
55
+ }
@@ -0,0 +1,24 @@
1
+ import { WebSocketFrame, type FrameSource } from '../base/index.ts'
2
+ import { WebSocketOpcode } from '../opcode/index.ts'
3
+
4
+ /**
5
+ * Binary message, or the first fragment of one
6
+ * @class
7
+ */
8
+ export class BinaryWebSocketFrame extends WebSocketFrame {
9
+ /**
10
+ * Builds a binary frame, unfragmented unless told otherwise
11
+ * @param content bytes to send
12
+ * @param finalFragment false if continuation frames follow
13
+ */
14
+ public constructor(content: FrameSource = '', finalFragment = true) {
15
+ super(content, finalFragment)
16
+ }
17
+
18
+ /**
19
+ * Always 0x2
20
+ */
21
+ public get opcode(): number {
22
+ return WebSocketOpcode.BINARY
23
+ }
24
+ }
@@ -0,0 +1,82 @@
1
+ import { ByteBuf } from '../../../../buffer/index.ts'
2
+ import { WebSocketCloseStatus } from '../../status/index.ts'
3
+ import { WebSocketFrame } from '../base/index.ts'
4
+ import { WebSocketOpcode } from '../opcode/index.ts'
5
+
6
+ /**
7
+ * Close frame, optionally carrying a status code and reason
8
+ * @class
9
+ */
10
+ export class CloseWebSocketFrame extends WebSocketFrame {
11
+ /**
12
+ * Close code, or null when the peer sent none
13
+ */
14
+ public readonly statusCode: number | null
15
+
16
+ /**
17
+ * UTF-8 reason, empty if there isn't one
18
+ */
19
+ public readonly reasonText: string
20
+
21
+ /**
22
+ * Builds a close frame, checking the code and reason first
23
+ * @param statusCode null for an empty close
24
+ * @param reasonText up to 123 bytes once encoded
25
+ */
26
+ public constructor(statusCode: number | null = null, reasonText = '') {
27
+ super(CloseWebSocketFrame.payloadOf(statusCode, reasonText), true)
28
+
29
+ this.statusCode = statusCode
30
+ this.reasonText = reasonText
31
+ }
32
+
33
+ /**
34
+ * Two byte code followed by the reason, or nothing at all
35
+ * @param statusCode close code, or null
36
+ * @param reasonText reason text
37
+ */
38
+ private static payloadOf(
39
+ statusCode: number | null,
40
+ reasonText: string,
41
+ ): ByteBuf {
42
+ const reason = CloseWebSocketFrame.refusal(statusCode, reasonText)
43
+ if (reason !== null) throw new RangeError(reason)
44
+
45
+ const payload = ByteBuf.allocate(0)
46
+ if (statusCode === null) return payload
47
+ payload.writeUint16(statusCode)
48
+ payload.writeString(reasonText)
49
+ return payload
50
+ }
51
+
52
+ /**
53
+ * A reason needs a code, codes have to be sendable and reasons fit in 123 bytes
54
+ * @param statusCode close code, or null
55
+ * @param reasonText reason text
56
+ */
57
+ private static refusal(
58
+ statusCode: number | null,
59
+ reasonText: string,
60
+ ): string | null {
61
+ if (statusCode === null) {
62
+ return reasonText === '' ? null : 'Close reason needs a status'
63
+ }
64
+
65
+ if (!WebSocketCloseStatus.isValid(statusCode)) {
66
+ return 'Close status is invalid'
67
+ }
68
+
69
+ if (new TextEncoder().encode(reasonText).length > 123) {
70
+ return 'Close reason is too long'
71
+ }
72
+
73
+ return null
74
+ }
75
+
76
+ /**
77
+ * Always 0x8
78
+ */
79
+ public get opcode(): number {
80
+ return WebSocketOpcode.CLOSE
81
+ }
82
+ }
@@ -0,0 +1,31 @@
1
+ import { WebSocketFrame, type FrameSource } from '../base/index.ts'
2
+ import { WebSocketOpcode } from '../opcode/index.ts'
3
+
4
+ /**
5
+ * Any fragment after the first in a split message
6
+ * @class
7
+ */
8
+ export class ContinuationWebSocketFrame extends WebSocketFrame {
9
+ /**
10
+ * Builds a continuation, the last one unless told otherwise
11
+ * @param content next piece of the message
12
+ * @param finalFragment true on the final piece
13
+ */
14
+ public constructor(content: FrameSource = '', finalFragment = true) {
15
+ super(content, finalFragment)
16
+ }
17
+
18
+ /**
19
+ * Always 0x0
20
+ */
21
+ public get opcode(): number {
22
+ return WebSocketOpcode.CONTINUATION
23
+ }
24
+
25
+ /**
26
+ * Decodes the payload as UTF-8, for fragments of a text message
27
+ */
28
+ public text(): string {
29
+ return this.content.toString()
30
+ }
31
+ }
@@ -0,0 +1,10 @@
1
+ export { WebSocketFrame } from './base/index.ts'
2
+ export type { FrameSource } from './base/index.ts'
3
+ export { BinaryWebSocketFrame } from './binary/index.ts'
4
+ export { CloseWebSocketFrame } from './close/index.ts'
5
+ export { ContinuationWebSocketFrame } from './continuation/index.ts'
6
+ export { WebSocketOpcode } from './opcode/index.ts'
7
+ export type { WebSocketOpcodeValue } from './opcode/index.ts'
8
+ export { PingWebSocketFrame } from './ping/index.ts'
9
+ export { PongWebSocketFrame } from './pong/index.ts'
10
+ export { TextWebSocketFrame } from './text/index.ts'
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Opcodes from RFC 6455
3
+ */
4
+ export const WebSocketOpcode = {
5
+ /**
6
+ * Carries on a fragmented message
7
+ */
8
+ CONTINUATION: 0x0,
9
+ /**
10
+ * UTF-8 text
11
+ */
12
+ TEXT: 0x1,
13
+ /**
14
+ * Raw bytes
15
+ */
16
+ BINARY: 0x2,
17
+ /**
18
+ * Starts or answers a close
19
+ */
20
+ CLOSE: 0x8,
21
+ /**
22
+ * Asks the peer for a pong
23
+ */
24
+ PING: 0x9,
25
+ /**
26
+ * Answers a ping, or works as a heartbeat
27
+ */
28
+ PONG: 0xa,
29
+ } as const
30
+
31
+ /**
32
+ * Any one of the known opcodes
33
+ */
34
+ export type WebSocketOpcodeValue =
35
+ (typeof WebSocketOpcode)[keyof typeof WebSocketOpcode]
@@ -0,0 +1,24 @@
1
+ import { WebSocketFrame, type FrameSource } from '../base/index.ts'
2
+ import { WebSocketOpcode } from '../opcode/index.ts'
3
+
4
+ /**
5
+ * Ping, which the peer should answer with a pong carrying the same payload
6
+ * @class
7
+ */
8
+ export class PingWebSocketFrame extends WebSocketFrame {
9
+ /**
10
+ * Builds a ping, empty by default
11
+ * @param content up to 125 bytes, echoed back in the pong
12
+ * @param finalFragment control frames can't be fragmented, leave it true
13
+ */
14
+ public constructor(content: FrameSource = '', finalFragment = true) {
15
+ super(content, finalFragment)
16
+ }
17
+
18
+ /**
19
+ * Always 0x9
20
+ */
21
+ public get opcode(): number {
22
+ return WebSocketOpcode.PING
23
+ }
24
+ }
@@ -0,0 +1,24 @@
1
+ import { WebSocketFrame, type FrameSource } from '../base/index.ts'
2
+ import { WebSocketOpcode } from '../opcode/index.ts'
3
+
4
+ /**
5
+ * Pong, sent back for a ping or on its own as a heartbeat
6
+ * @class
7
+ */
8
+ export class PongWebSocketFrame extends WebSocketFrame {
9
+ /**
10
+ * Builds a pong, empty by default
11
+ * @param content usually the payload of the ping it answers
12
+ * @param finalFragment control frames can't be fragmented, leave it true
13
+ */
14
+ public constructor(content: FrameSource = '', finalFragment = true) {
15
+ super(content, finalFragment)
16
+ }
17
+
18
+ /**
19
+ * Always 0xA
20
+ */
21
+ public get opcode(): number {
22
+ return WebSocketOpcode.PONG
23
+ }
24
+ }
@@ -0,0 +1,31 @@
1
+ import { WebSocketFrame, type FrameSource } from '../base/index.ts'
2
+ import { WebSocketOpcode } from '../opcode/index.ts'
3
+
4
+ /**
5
+ * Text message, or the first fragment of one
6
+ * @class
7
+ */
8
+ export class TextWebSocketFrame extends WebSocketFrame {
9
+ /**
10
+ * Builds a text frame, unfragmented unless told otherwise
11
+ * @param content text or UTF-8 bytes
12
+ * @param finalFragment false if continuation frames follow
13
+ */
14
+ public constructor(content: FrameSource = '', finalFragment = true) {
15
+ super(content, finalFragment)
16
+ }
17
+
18
+ /**
19
+ * Always 0x1
20
+ */
21
+ public get opcode(): number {
22
+ return WebSocketOpcode.TEXT
23
+ }
24
+
25
+ /**
26
+ * Decodes the payload as UTF-8
27
+ */
28
+ public text(): string {
29
+ return this.content.toString()
30
+ }
31
+ }
@@ -0,0 +1,38 @@
1
+ import type { HttpHeaders } from '../../http/headers/index.ts'
2
+
3
+ /**
4
+ * Fired as a user event once the handshake is done and frames can flow
5
+ * @class
6
+ */
7
+ export class WebSocketHandshakeComplete {
8
+ /**
9
+ * Path the handshake was made on
10
+ */
11
+ public readonly uri: string
12
+
13
+ /**
14
+ * Headers the peer sent during the handshake
15
+ */
16
+ public readonly headers: HttpHeaders
17
+
18
+ /**
19
+ * Subprotocol both sides agreed on, or null
20
+ */
21
+ public readonly subprotocol: string | null
22
+
23
+ /**
24
+ * Builds the event
25
+ * @param uri path the handshake was made on
26
+ * @param headers what the peer sent
27
+ * @param subprotocol agreed subprotocol, or null
28
+ */
29
+ public constructor(
30
+ uri: string,
31
+ headers: HttpHeaders,
32
+ subprotocol: string | null,
33
+ ) {
34
+ this.uri = uri
35
+ this.headers = headers
36
+ this.subprotocol = subprotocol
37
+ }
38
+ }
@@ -0,0 +1,25 @@
1
+ export { WebSocketClientProtocolHandler } from './client/index.ts'
2
+ export type { WebSocketClientOptions } from './client/index.ts'
3
+ export { WebSocketFrameDecoder } from './decoder/index.ts'
4
+ export type { WebSocketDecoderOptions } from './decoder/index.ts'
5
+ export { WebSocketFrameEncoder } from './encoder/index.ts'
6
+ export type { WebSocketEncoderOptions } from './encoder/index.ts'
7
+ export { CorruptedFrameError, HandshakeError } from './error/index.ts'
8
+ export {
9
+ BinaryWebSocketFrame,
10
+ CloseWebSocketFrame,
11
+ ContinuationWebSocketFrame,
12
+ PingWebSocketFrame,
13
+ PongWebSocketFrame,
14
+ TextWebSocketFrame,
15
+ WebSocketFrame,
16
+ WebSocketOpcode,
17
+ } from './frame/index.ts'
18
+ export type { FrameSource, WebSocketOpcodeValue } from './frame/index.ts'
19
+ export { WebSocketHandshakeComplete } from './handshake/index.ts'
20
+ export { WebSocketKey } from './key/index.ts'
21
+ export { WebSocketProtocolHandler } from './protocol/index.ts'
22
+ export { WebSocketServerProtocolHandler } from './server/index.ts'
23
+ export type { WebSocketServerOptions } from './server/index.ts'
24
+ export { WebSocketCloseStatus } from './status/index.ts'
25
+ export { WebSocketUpgrade } from './upgrade/index.ts'
@@ -0,0 +1,41 @@
1
+ /**
2
+ * The Sec-WebSocket-Key sent in the handshake
3
+ * @class
4
+ */
5
+ export class WebSocketKey {
6
+ /**
7
+ * Fixed GUID from RFC 6455 used to work out the accept value
8
+ */
9
+ public static readonly GUID: string = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
10
+
11
+ /**
12
+ * Base64 key as sent in the header
13
+ */
14
+ public readonly value: string
15
+
16
+ /**
17
+ * Wraps a key from the peer, or makes a random one
18
+ * @param value leave it out for a fresh random key
19
+ */
20
+ public constructor(
21
+ value: string = crypto.getRandomValues(new Uint8Array(16)).toBase64(),
22
+ ) {
23
+ this.value = value
24
+ }
25
+
26
+ /**
27
+ * True if it looks like 16 bytes of base64
28
+ */
29
+ public get isValid(): boolean {
30
+ return /^[A-Za-z0-9+/]{21}[AQgw]==$/.test(this.value)
31
+ }
32
+
33
+ /**
34
+ * Base64 SHA-1 of the key plus the GUID, for Sec-WebSocket-Accept
35
+ */
36
+ public accept(): string {
37
+ return new Bun.CryptoHasher('sha1')
38
+ .update(`${this.value}${WebSocketKey.GUID}`)
39
+ .digest('base64')
40
+ }
41
+ }