@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.
- package/LICENSE +21 -0
- package/README.md +105 -2
- package/package.json +65 -4
- package/src/bootstrap/client/index.ts +191 -0
- package/src/bootstrap/dispatcher/index.ts +204 -0
- package/src/bootstrap/index.ts +22 -0
- package/src/bootstrap/reconnect/index.ts +334 -0
- package/src/bootstrap/server/index.ts +284 -0
- package/src/buffer/bits/index.ts +2 -0
- package/src/buffer/bits/reader/index.ts +64 -0
- package/src/buffer/bits/writer/index.ts +79 -0
- package/src/buffer/index.ts +1375 -0
- package/src/buffer/pool/index.ts +46 -0
- package/src/buffer/quantizer/index.ts +101 -0
- package/src/channel/attribute/index.ts +89 -0
- package/src/channel/context/index.ts +621 -0
- package/src/channel/group/index.ts +256 -0
- package/src/channel/handler/duplex/index.ts +105 -0
- package/src/channel/handler/inbound/index.ts +78 -0
- package/src/channel/handler/index.ts +135 -0
- package/src/channel/handler/outbound/index.ts +33 -0
- package/src/channel/index.ts +619 -0
- package/src/channel/initializer/index.ts +77 -0
- package/src/channel/outbound/index.ts +611 -0
- package/src/channel/pipeline/head/index.ts +100 -0
- package/src/channel/pipeline/index.ts +414 -0
- package/src/channel/pipeline/tail/index.ts +45 -0
- package/src/channel/pipeline/tracker/index.ts +120 -0
- package/src/channel/queue/index.ts +203 -0
- package/src/channel/resolved/index.ts +34 -0
- package/src/channel/server/index.ts +164 -0
- package/src/channel/shutdown/index.ts +25 -0
- package/src/channel/simple/index.ts +38 -0
- package/src/channel/transport/index.ts +74 -0
- package/src/channel/transport/socket/index.ts +80 -0
- package/src/cluster/index.ts +360 -0
- package/src/codec/binary/index.ts +102 -0
- package/src/codec/decoder/index.ts +479 -0
- package/src/codec/delimiter/index.ts +189 -0
- package/src/codec/encoder/index.ts +48 -0
- package/src/codec/error/index.ts +33 -0
- package/src/codec/fixed/index.ts +45 -0
- package/src/codec/http/aggregator/index.ts +236 -0
- package/src/codec/http/client/index.ts +120 -0
- package/src/codec/http/decoder/index.ts +4 -0
- package/src/codec/http/decoder/object/index.ts +644 -0
- package/src/codec/http/decoder/request/index.ts +49 -0
- package/src/codec/http/decoder/response/index.ts +59 -0
- package/src/codec/http/encoder/index.ts +3 -0
- package/src/codec/http/encoder/object/index.ts +196 -0
- package/src/codec/http/encoder/request/index.ts +24 -0
- package/src/codec/http/encoder/response/index.ts +26 -0
- package/src/codec/http/headers/index.ts +287 -0
- package/src/codec/http/index.ts +33 -0
- package/src/codec/http/message/content/index.ts +36 -0
- package/src/codec/http/message/full/index.ts +10 -0
- package/src/codec/http/message/full/request/index.ts +45 -0
- package/src/codec/http/message/full/response/index.ts +43 -0
- package/src/codec/http/message/head/index.ts +79 -0
- package/src/codec/http/message/index.ts +16 -0
- package/src/codec/http/message/last/index.ts +27 -0
- package/src/codec/http/message/request/index.ts +39 -0
- package/src/codec/http/message/response/index.ts +31 -0
- package/src/codec/http/method/index.ts +101 -0
- package/src/codec/http/server/index.ts +81 -0
- package/src/codec/http/status/index.ts +170 -0
- package/src/codec/http/version/index.ts +70 -0
- package/src/codec/index.ts +83 -0
- package/src/codec/length/index.ts +330 -0
- package/src/codec/line/index.ts +45 -0
- package/src/codec/message/decoder/index.ts +84 -0
- package/src/codec/message/encoder/index.ts +56 -0
- package/src/codec/message/index.ts +2 -0
- package/src/codec/msgpack/index.ts +51 -0
- package/src/codec/prepender/index.ts +296 -0
- package/src/codec/protobuf/index.ts +57 -0
- package/src/codec/string/decoder/index.ts +47 -0
- package/src/codec/string/encoder/index.ts +26 -0
- package/src/codec/string/index.ts +2 -0
- package/src/codec/websocket/client/index.ts +235 -0
- package/src/codec/websocket/decoder/index.ts +371 -0
- package/src/codec/websocket/encoder/index.ts +170 -0
- package/src/codec/websocket/error/index.ts +42 -0
- package/src/codec/websocket/frame/base/index.ts +55 -0
- package/src/codec/websocket/frame/binary/index.ts +24 -0
- package/src/codec/websocket/frame/close/index.ts +82 -0
- package/src/codec/websocket/frame/continuation/index.ts +31 -0
- package/src/codec/websocket/frame/index.ts +10 -0
- package/src/codec/websocket/frame/opcode/index.ts +35 -0
- package/src/codec/websocket/frame/ping/index.ts +24 -0
- package/src/codec/websocket/frame/pong/index.ts +24 -0
- package/src/codec/websocket/frame/text/index.ts +31 -0
- package/src/codec/websocket/handshake/index.ts +38 -0
- package/src/codec/websocket/index.ts +25 -0
- package/src/codec/websocket/key/index.ts +41 -0
- package/src/codec/websocket/protocol/index.ts +147 -0
- package/src/codec/websocket/server/index.ts +233 -0
- package/src/codec/websocket/status/index.ts +97 -0
- package/src/codec/websocket/upgrade/index.ts +116 -0
- package/src/datagram/bootstrap/index.ts +269 -0
- package/src/datagram/channel/index.ts +103 -0
- package/src/datagram/index.ts +10 -0
- package/src/datagram/packet/index.ts +72 -0
- package/src/datagram/transport/index.ts +239 -0
- package/src/handler/flush/index.ts +187 -0
- package/src/handler/idle/index.ts +295 -0
- package/src/handler/index.ts +22 -0
- package/src/handler/logging/index.ts +195 -0
- package/src/handler/metrics/index.ts +300 -0
- package/src/handler/offload/index.ts +141 -0
- package/src/handler/timeout/error/index.ts +29 -0
- package/src/handler/timeout/index.ts +3 -0
- package/src/handler/timeout/read/index.ts +44 -0
- package/src/handler/timeout/write/index.ts +89 -0
- package/src/handler/traffic/index.ts +259 -0
- package/src/index.ts +258 -0
- package/src/logger/index.ts +47 -0
- package/src/reliable/bootstrap/index.ts +154 -0
- package/src/reliable/cipher/index.ts +227 -0
- package/src/reliable/cookie/index.ts +78 -0
- package/src/reliable/handler/index.ts +1202 -0
- package/src/reliable/handshake/index.ts +144 -0
- package/src/reliable/index.ts +38 -0
- package/src/reliable/lane/index.ts +420 -0
- package/src/reliable/message/index.ts +102 -0
- package/src/reliable/packet/index.ts +117 -0
- package/src/reliable/peer/index.ts +929 -0
- package/src/reliable/registry/index.ts +107 -0
- package/src/serve/bootstrap/index.ts +606 -0
- package/src/serve/channel/index.ts +246 -0
- package/src/serve/index.ts +17 -0
- package/src/serve/outbox/index.ts +292 -0
- package/src/serve/transport/index.ts +96 -0
- package/src/serve/writer/index.ts +119 -0
- package/src/shard/envelope/index.ts +44 -0
- package/src/shard/gateway/index.ts +119 -0
- package/src/shard/index.ts +11 -0
- package/src/shard/link/index.ts +743 -0
- package/src/shard/map/index.ts +91 -0
- package/src/worker/group/index.ts +175 -0
- package/src/worker/index.ts +10 -0
- package/src/worker/pool/index.ts +235 -0
- package/src/worker/service/index.ts +55 -0
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import type { ServerWebSocket } from 'bun'
|
|
2
|
+
|
|
3
|
+
import { ByteBuf, ByteBufPool } from '../../buffer/index.ts'
|
|
4
|
+
import { Channel, type ChannelOptions } from '../../channel/index.ts'
|
|
5
|
+
import { resolved } from '../../channel/resolved/index.ts'
|
|
6
|
+
import { BinaryWebSocketFrame } from '../../codec/websocket/frame/binary/index.ts'
|
|
7
|
+
import {
|
|
8
|
+
WebSocketOutbox,
|
|
9
|
+
type OutgoingMessage,
|
|
10
|
+
type SendResult,
|
|
11
|
+
} from '../outbox/index.ts'
|
|
12
|
+
import { WebSocketTransport, type WebSocketState } from '../transport/index.ts'
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* A connection on a native Bun WebSocket server, with topics for cheap fan-out
|
|
16
|
+
* @class
|
|
17
|
+
*/
|
|
18
|
+
export class WebSocketChannel extends Channel {
|
|
19
|
+
/**
|
|
20
|
+
* Home for frame contents, keeps nothing so released bytes aren't held
|
|
21
|
+
*/
|
|
22
|
+
private static readonly discard: ByteBufPool = new ByteBufPool(0)
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The native socket
|
|
26
|
+
*/
|
|
27
|
+
public readonly socket: ServerWebSocket<WebSocketState>
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Set while Bun is holding sends back, cleared once the outbox empties
|
|
31
|
+
*/
|
|
32
|
+
private blocked: boolean
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Holds sends in order while Bun is backed up
|
|
36
|
+
*/
|
|
37
|
+
private readonly outbox: WebSocketOutbox
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Whether binary frames are reused
|
|
41
|
+
*/
|
|
42
|
+
private readonly pooled: boolean
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Frame ready for the next binary message, or null
|
|
46
|
+
*/
|
|
47
|
+
private spare: BinaryWebSocketFrame | null
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Normally built by WebSocketBootstrap
|
|
51
|
+
* @param socket the native socket
|
|
52
|
+
* @param options same knobs as any other channel
|
|
53
|
+
* @param pooled whether binary frames are reused
|
|
54
|
+
*/
|
|
55
|
+
public constructor(
|
|
56
|
+
socket: ServerWebSocket<WebSocketState>,
|
|
57
|
+
options: ChannelOptions,
|
|
58
|
+
pooled = true,
|
|
59
|
+
) {
|
|
60
|
+
super(new WebSocketTransport(socket), options)
|
|
61
|
+
|
|
62
|
+
this.socket = socket
|
|
63
|
+
this.pooled = pooled
|
|
64
|
+
this.spare = null
|
|
65
|
+
this.blocked = false
|
|
66
|
+
this.outbox = new WebSocketOutbox(
|
|
67
|
+
socket,
|
|
68
|
+
options.maxPendingBytes ?? Channel.defaultMaxPendingBytes,
|
|
69
|
+
{
|
|
70
|
+
/**
|
|
71
|
+
* Bun pushed back
|
|
72
|
+
*/
|
|
73
|
+
blocked: (): void => {
|
|
74
|
+
this.setBlocked(true)
|
|
75
|
+
},
|
|
76
|
+
/**
|
|
77
|
+
* Everything waiting has gone
|
|
78
|
+
*/
|
|
79
|
+
cleared: (): void => {
|
|
80
|
+
this.setBlocked(false)
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Hands a binary message to the pipeline in a reused frame when it can
|
|
88
|
+
* @param bytes the message
|
|
89
|
+
*/
|
|
90
|
+
public handleBinary(bytes: Uint8Array): void {
|
|
91
|
+
if (!this.pooled) {
|
|
92
|
+
void this.handleMessage(
|
|
93
|
+
new BinaryWebSocketFrame(ByteBuf.wrap(bytes)),
|
|
94
|
+
bytes.length,
|
|
95
|
+
)
|
|
96
|
+
return
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const frame =
|
|
100
|
+
this.spare ??
|
|
101
|
+
new BinaryWebSocketFrame(ByteBuf.forPool(WebSocketChannel.discard))
|
|
102
|
+
this.spare = null
|
|
103
|
+
ByteBuf.wrapInto(bytes, frame.content)
|
|
104
|
+
|
|
105
|
+
const result = this.handleMessage(frame, bytes.length)
|
|
106
|
+
if (result === resolved) {
|
|
107
|
+
this.recycle(frame)
|
|
108
|
+
return
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
void result.then(
|
|
112
|
+
(): void => {
|
|
113
|
+
this.recycle(frame)
|
|
114
|
+
},
|
|
115
|
+
(): void => {
|
|
116
|
+
this.recycle(frame)
|
|
117
|
+
},
|
|
118
|
+
)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Keeps the frame for the next message unless someone retained its content
|
|
123
|
+
* @param frame the delivered frame
|
|
124
|
+
*/
|
|
125
|
+
private recycle(frame: BinaryWebSocketFrame): void {
|
|
126
|
+
if (frame.content.release() && !ByteBuf.debugFrames) this.spare = frame
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Whatever the upgrade hook attached, like the logged in user, or null
|
|
131
|
+
*/
|
|
132
|
+
public get data(): unknown {
|
|
133
|
+
return this.socket.data.data
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* False while Bun is holding sends back for this socket
|
|
138
|
+
*/
|
|
139
|
+
public override get isWritable(): boolean {
|
|
140
|
+
return !this.blocked && super.isWritable
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Bytes the channel and Bun have taken since it opened
|
|
145
|
+
*/
|
|
146
|
+
public override get bytesWritten(): number {
|
|
147
|
+
return super.bytesWritten + this.outbox.writtenBytes
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Bytes waiting in netify for Bun to take
|
|
152
|
+
*/
|
|
153
|
+
public override get pendingBytes(): number {
|
|
154
|
+
return super.pendingBytes + this.outbox.pendingBytes
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Path and query the client asked for
|
|
159
|
+
*/
|
|
160
|
+
public get uri(): string {
|
|
161
|
+
return this.socket.data.uri
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Bytes Bun is still holding for this socket
|
|
166
|
+
*/
|
|
167
|
+
public get bufferedAmount(): number {
|
|
168
|
+
return this.socket.getBufferedAmount()
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Sends now, or queues in order if Bun is backed up, the writer calls this
|
|
173
|
+
* @param message what to send
|
|
174
|
+
*/
|
|
175
|
+
public sendNative(message: OutgoingMessage): SendResult {
|
|
176
|
+
return this.outbox.send(message)
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* sendNative for binary, with nothing to allocate on the way
|
|
181
|
+
* @param bytes what to send
|
|
182
|
+
*/
|
|
183
|
+
public sendBinary(bytes: Uint8Array): SendResult {
|
|
184
|
+
return this.outbox.sendBinary(bytes)
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Bun has room again, so send what's waiting
|
|
189
|
+
*/
|
|
190
|
+
public override handleDrain(): void {
|
|
191
|
+
super.handleDrain()
|
|
192
|
+
this.outbox.drain()
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Starts getting whatever is published to a topic
|
|
197
|
+
* @param topic the topic
|
|
198
|
+
*/
|
|
199
|
+
public subscribe(topic: string): boolean {
|
|
200
|
+
return this.socket.subscribe(topic)
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Stops getting a topic
|
|
205
|
+
* @param topic the topic
|
|
206
|
+
*/
|
|
207
|
+
public unsubscribe(topic: string): boolean {
|
|
208
|
+
return this.socket.unsubscribe(topic)
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Whether it's subscribed to a topic
|
|
213
|
+
* @param topic the topic
|
|
214
|
+
*/
|
|
215
|
+
public isSubscribed(topic: string): boolean {
|
|
216
|
+
return this.socket.isSubscribed(topic)
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Sends to everyone else on a topic, fanned out natively
|
|
221
|
+
* @param topic the topic
|
|
222
|
+
* @param content bytes go as binary, strings as text
|
|
223
|
+
*/
|
|
224
|
+
public publish(
|
|
225
|
+
topic: string,
|
|
226
|
+
content: ByteBuf | Uint8Array | string,
|
|
227
|
+
): number {
|
|
228
|
+
return this.socket.publish(
|
|
229
|
+
topic,
|
|
230
|
+
content instanceof ByteBuf ? content.toUint8Array() : content,
|
|
231
|
+
)
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Records the backed up state and tells the pipeline when it changes
|
|
236
|
+
* @param blocked whether Bun is holding sends back
|
|
237
|
+
*/
|
|
238
|
+
private setBlocked(blocked: boolean): void {
|
|
239
|
+
if (this.blocked === blocked) return
|
|
240
|
+
this.blocked = blocked
|
|
241
|
+
|
|
242
|
+
void this.execute((): Promise<void> =>
|
|
243
|
+
this.pipeline.fireChannelWritabilityChanged(),
|
|
244
|
+
)
|
|
245
|
+
}
|
|
246
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export { WebSocketBootstrap, WebSocketServer } from './bootstrap/index.ts'
|
|
2
|
+
export type {
|
|
3
|
+
UpgradeHandler,
|
|
4
|
+
UpgradeVerdict,
|
|
5
|
+
WebSocketBindOptions,
|
|
6
|
+
WebSocketBootstrapOptions,
|
|
7
|
+
} from './bootstrap/index.ts'
|
|
8
|
+
export { WebSocketChannel } from './channel/index.ts'
|
|
9
|
+
export { WebSocketOutbox } from './outbox/index.ts'
|
|
10
|
+
export type {
|
|
11
|
+
OutboxEvents,
|
|
12
|
+
OutgoingMessage,
|
|
13
|
+
SendResult,
|
|
14
|
+
} from './outbox/index.ts'
|
|
15
|
+
export { WebSocketTransport } from './transport/index.ts'
|
|
16
|
+
export type { WebSocketState } from './transport/index.ts'
|
|
17
|
+
export { WebSocketFrameWriter } from './writer/index.ts'
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import type { ServerWebSocket } from 'bun'
|
|
2
|
+
|
|
3
|
+
import type { WebSocketState } from '../transport/index.ts'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* One message waiting for the native socket
|
|
7
|
+
*/
|
|
8
|
+
export type OutgoingMessage =
|
|
9
|
+
| {
|
|
10
|
+
/**
|
|
11
|
+
* Binary data, or a ping or pong
|
|
12
|
+
*/
|
|
13
|
+
readonly kind: 'binary' | 'ping' | 'pong'
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The payload
|
|
17
|
+
*/
|
|
18
|
+
readonly bytes: Uint8Array
|
|
19
|
+
}
|
|
20
|
+
| {
|
|
21
|
+
/**
|
|
22
|
+
* Text
|
|
23
|
+
*/
|
|
24
|
+
readonly kind: 'text'
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The payload
|
|
28
|
+
*/
|
|
29
|
+
readonly text: string
|
|
30
|
+
}
|
|
31
|
+
| {
|
|
32
|
+
/**
|
|
33
|
+
* A close, sent once everything before it has gone
|
|
34
|
+
*/
|
|
35
|
+
readonly kind: 'close'
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Close status
|
|
39
|
+
*/
|
|
40
|
+
readonly code: number
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Close reason
|
|
44
|
+
*/
|
|
45
|
+
readonly reason: string
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* What happened to a send
|
|
50
|
+
*/
|
|
51
|
+
export type SendResult = 'sent' | 'queued' | 'full' | 'closed'
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Hooks for when the socket backs up and clears
|
|
55
|
+
*/
|
|
56
|
+
export type OutboxEvents = {
|
|
57
|
+
/**
|
|
58
|
+
* Bun pushed back, so further sends queue here
|
|
59
|
+
*/
|
|
60
|
+
readonly blocked: () => void
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Bun drained and everything queued has gone
|
|
64
|
+
*/
|
|
65
|
+
readonly cleared: () => void
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Holds sends in order while Bun is backed up, so nothing gets dropped
|
|
70
|
+
* @class
|
|
71
|
+
*/
|
|
72
|
+
export class WebSocketOutbox {
|
|
73
|
+
/**
|
|
74
|
+
* The native socket
|
|
75
|
+
*/
|
|
76
|
+
private readonly socket: ServerWebSocket<WebSocketState>
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Most bytes allowed to wait here
|
|
80
|
+
*/
|
|
81
|
+
private readonly limit: number
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Backed up and cleared hooks
|
|
85
|
+
*/
|
|
86
|
+
private readonly events: OutboxEvents
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Messages waiting, from head onwards
|
|
90
|
+
*/
|
|
91
|
+
private queue: OutgoingMessage[]
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* First message still waiting
|
|
95
|
+
*/
|
|
96
|
+
private head: number
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Bytes waiting
|
|
100
|
+
*/
|
|
101
|
+
private queuedBytes: number
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Bytes Bun has taken so far
|
|
105
|
+
*/
|
|
106
|
+
private written: number
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Set once Bun pushes back, cleared on drain
|
|
110
|
+
*/
|
|
111
|
+
private backed: boolean
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Starts empty
|
|
115
|
+
* @param socket the native socket
|
|
116
|
+
* @param limit most bytes allowed to wait
|
|
117
|
+
* @param events backed up and cleared hooks
|
|
118
|
+
*/
|
|
119
|
+
public constructor(
|
|
120
|
+
socket: ServerWebSocket<WebSocketState>,
|
|
121
|
+
limit: number,
|
|
122
|
+
events: OutboxEvents,
|
|
123
|
+
) {
|
|
124
|
+
this.socket = socket
|
|
125
|
+
this.limit = limit
|
|
126
|
+
this.events = events
|
|
127
|
+
this.queue = []
|
|
128
|
+
this.head = 0
|
|
129
|
+
this.queuedBytes = 0
|
|
130
|
+
this.written = 0
|
|
131
|
+
this.backed = false
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Bytes waiting to go to Bun
|
|
136
|
+
*/
|
|
137
|
+
public get pendingBytes(): number {
|
|
138
|
+
return this.queuedBytes
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Bytes Bun has taken since the socket opened
|
|
143
|
+
*/
|
|
144
|
+
public get writtenBytes(): number {
|
|
145
|
+
return this.written
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* send for binary, only making a message object when it has to queue
|
|
150
|
+
* @param bytes what to send
|
|
151
|
+
*/
|
|
152
|
+
public sendBinary(bytes: Uint8Array): SendResult {
|
|
153
|
+
if (this.socket.readyState !== 1) return 'closed'
|
|
154
|
+
if (this.backed || this.head < this.queue.length) {
|
|
155
|
+
return this.enqueue({ kind: 'binary', bytes })
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const status = this.socket.sendBinary(bytes)
|
|
159
|
+
if (status > 0 || (status === 0 && bytes.length === 0)) {
|
|
160
|
+
this.written += bytes.length
|
|
161
|
+
return 'sent'
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
this.backUp()
|
|
165
|
+
if (status < 0) this.written += bytes.length
|
|
166
|
+
return status < 0 ? 'sent' : this.enqueue({ kind: 'binary', bytes })
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Sends now if Bun has room, otherwise queues behind what's already waiting
|
|
171
|
+
* @param message what to send
|
|
172
|
+
*/
|
|
173
|
+
public send(message: OutgoingMessage): SendResult {
|
|
174
|
+
if (this.socket.readyState !== 1) return 'closed'
|
|
175
|
+
if (this.backed || this.head < this.queue.length) {
|
|
176
|
+
return this.enqueue(message)
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const status = this.attempt(message)
|
|
180
|
+
if (status === 'sent') return 'sent'
|
|
181
|
+
if (status === 'taken') {
|
|
182
|
+
this.backUp()
|
|
183
|
+
return 'sent'
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
this.backUp()
|
|
187
|
+
return this.enqueue(message)
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Bun has room again, so send what's waiting until it pushes back
|
|
192
|
+
*/
|
|
193
|
+
public drain(): void {
|
|
194
|
+
this.backed = false
|
|
195
|
+
|
|
196
|
+
const queue = this.queue
|
|
197
|
+
while (this.head < queue.length) {
|
|
198
|
+
const message = queue[this.head]
|
|
199
|
+
if (message === undefined) break
|
|
200
|
+
const status = this.attempt(message)
|
|
201
|
+
if (status === 'refused') {
|
|
202
|
+
this.backed = true
|
|
203
|
+
return
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
this.head += 1
|
|
207
|
+
this.queuedBytes -= WebSocketOutbox.sizeOf(message)
|
|
208
|
+
if (status === 'taken') {
|
|
209
|
+
this.backed = true
|
|
210
|
+
return
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
this.queue = []
|
|
215
|
+
this.head = 0
|
|
216
|
+
this.queuedBytes = 0
|
|
217
|
+
this.events.cleared()
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Hands one message to Bun, taken means accepted but over the limit, refused means dropped
|
|
222
|
+
* @param message what to send
|
|
223
|
+
*/
|
|
224
|
+
private attempt(message: OutgoingMessage): 'sent' | 'taken' | 'refused' {
|
|
225
|
+
const socket = this.socket
|
|
226
|
+
let status: number
|
|
227
|
+
|
|
228
|
+
switch (message.kind) {
|
|
229
|
+
case 'binary':
|
|
230
|
+
status = socket.sendBinary(message.bytes)
|
|
231
|
+
break
|
|
232
|
+
case 'text':
|
|
233
|
+
status = socket.sendText(message.text)
|
|
234
|
+
break
|
|
235
|
+
case 'ping':
|
|
236
|
+
status = socket.ping(message.bytes)
|
|
237
|
+
break
|
|
238
|
+
case 'pong':
|
|
239
|
+
status = socket.pong(message.bytes)
|
|
240
|
+
break
|
|
241
|
+
case 'close':
|
|
242
|
+
socket.close(message.code, message.reason)
|
|
243
|
+
return 'sent'
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
if (status > 0 || (status === 0 && WebSocketOutbox.sizeOf(message) === 0)) {
|
|
247
|
+
this.written += WebSocketOutbox.sizeOf(message)
|
|
248
|
+
return 'sent'
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
if (status < 0) this.written += WebSocketOutbox.sizeOf(message)
|
|
252
|
+
return status < 0 ? 'taken' : 'refused'
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Queues a message if there's room
|
|
257
|
+
* @param message what to queue
|
|
258
|
+
*/
|
|
259
|
+
private enqueue(message: OutgoingMessage): SendResult {
|
|
260
|
+
const size = WebSocketOutbox.sizeOf(message)
|
|
261
|
+
if (this.queuedBytes + size > this.limit) return 'full'
|
|
262
|
+
|
|
263
|
+
this.queue.push(message)
|
|
264
|
+
this.queuedBytes += size
|
|
265
|
+
return 'queued'
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Notes that Bun pushed back and says so once
|
|
270
|
+
*/
|
|
271
|
+
private backUp(): void {
|
|
272
|
+
if (this.backed) return
|
|
273
|
+
this.backed = true
|
|
274
|
+
|
|
275
|
+
this.events.blocked()
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Bytes a message holds, roughly for text
|
|
280
|
+
* @param message the message
|
|
281
|
+
*/
|
|
282
|
+
private static sizeOf(message: OutgoingMessage): number {
|
|
283
|
+
switch (message.kind) {
|
|
284
|
+
case 'text':
|
|
285
|
+
return message.text.length
|
|
286
|
+
case 'close':
|
|
287
|
+
return message.reason.length
|
|
288
|
+
default:
|
|
289
|
+
return message.bytes.length
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import type { ServerWebSocket } from 'bun'
|
|
2
|
+
|
|
3
|
+
import type { Transport } from '../../channel/transport/index.ts'
|
|
4
|
+
import type { WebSocketChannel } from '../channel/index.ts'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* What each native WebSocket carries, the channel is filled in once it opens
|
|
8
|
+
*/
|
|
9
|
+
export type WebSocketState = {
|
|
10
|
+
/**
|
|
11
|
+
* The channel, null until open runs
|
|
12
|
+
*/
|
|
13
|
+
channel: WebSocketChannel | null
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Path and query the client asked for
|
|
17
|
+
*/
|
|
18
|
+
readonly uri: string
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Request headers from the upgrade
|
|
22
|
+
*/
|
|
23
|
+
readonly headers: Headers
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Whatever the upgrade hook attached, or null
|
|
27
|
+
*/
|
|
28
|
+
readonly data: unknown
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Sends raw bytes as binary messages on a native Bun WebSocket
|
|
33
|
+
* @class
|
|
34
|
+
*/
|
|
35
|
+
export class WebSocketTransport implements Transport {
|
|
36
|
+
/**
|
|
37
|
+
* Each write is its own message, so writes are never glued together
|
|
38
|
+
*/
|
|
39
|
+
public readonly datagram: boolean
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Bun doesn't give the peer port for WebSockets
|
|
43
|
+
*/
|
|
44
|
+
public readonly remotePort: number
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The native socket
|
|
48
|
+
*/
|
|
49
|
+
public readonly socket: ServerWebSocket<WebSocketState>
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Wraps the socket
|
|
53
|
+
* @param socket the native socket
|
|
54
|
+
*/
|
|
55
|
+
public constructor(socket: ServerWebSocket<WebSocketState>) {
|
|
56
|
+
this.datagram = true
|
|
57
|
+
this.remotePort = 0
|
|
58
|
+
this.socket = socket
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Peer IP
|
|
63
|
+
*/
|
|
64
|
+
public get remoteAddress(): string {
|
|
65
|
+
return this.socket.remoteAddress
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Sends one binary message, Bun queues it under backpressure
|
|
70
|
+
* @param bytes the message
|
|
71
|
+
*/
|
|
72
|
+
public write(bytes: Uint8Array): number {
|
|
73
|
+
return this.socket.sendBinary(bytes) === 0 ? -1 : bytes.length
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Closes with a normal status
|
|
78
|
+
*/
|
|
79
|
+
public end(): void {
|
|
80
|
+
this.socket.close()
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Bun can't stop reading a WebSocket, so this does nothing
|
|
85
|
+
*/
|
|
86
|
+
public pause(): void {
|
|
87
|
+
return
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Nothing to undo
|
|
92
|
+
*/
|
|
93
|
+
public resume(): void {
|
|
94
|
+
return
|
|
95
|
+
}
|
|
96
|
+
}
|