@netifyjs/netify 0.0.0-stage → 1.0.0-alpha.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/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/index.ts +10 -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,619 @@
|
|
|
1
|
+
import { ByteBuf } from '../buffer/index.ts'
|
|
2
|
+
import { AttributeMap } from './attribute/index.ts'
|
|
3
|
+
import { OutboundBuffer } from './outbound/index.ts'
|
|
4
|
+
import { Pipeline } from './pipeline/index.ts'
|
|
5
|
+
import { HeadHandler } from './pipeline/head/index.ts'
|
|
6
|
+
import type { ChannelSetup } from './initializer/index.ts'
|
|
7
|
+
import { EventQueue } from './queue/index.ts'
|
|
8
|
+
import { pending, resolved } from './resolved/index.ts'
|
|
9
|
+
import type { Transport } from './transport/index.ts'
|
|
10
|
+
import { ConsoleLogger, type Logger } from '../logger/index.ts'
|
|
11
|
+
import type { PacketAddress } from '../datagram/packet/index.ts'
|
|
12
|
+
|
|
13
|
+
export { AttributeKey, AttributeMap } from './attribute/index.ts'
|
|
14
|
+
export { HandlerContext } from './context/index.ts'
|
|
15
|
+
export {
|
|
16
|
+
DuplexHandlerAdapter,
|
|
17
|
+
InboundHandlerAdapter,
|
|
18
|
+
OutboundHandlerAdapter,
|
|
19
|
+
} from './handler/index.ts'
|
|
20
|
+
export { ChannelInitializer } from './initializer/index.ts'
|
|
21
|
+
export type { ChannelSetup } from './initializer/index.ts'
|
|
22
|
+
export { ChannelGroup } from './group/index.ts'
|
|
23
|
+
export type {
|
|
24
|
+
ChannelMatcher,
|
|
25
|
+
GroupFailure,
|
|
26
|
+
GroupResult,
|
|
27
|
+
} from './group/index.ts'
|
|
28
|
+
export type {
|
|
29
|
+
ChannelHandler,
|
|
30
|
+
DuplexHandler,
|
|
31
|
+
InboundHandler,
|
|
32
|
+
LifecycleCallbacks,
|
|
33
|
+
OutboundHandler,
|
|
34
|
+
} from './handler/index.ts'
|
|
35
|
+
export { OutboundBuffer } from './outbound/index.ts'
|
|
36
|
+
export type { OutboundOptions } from './outbound/index.ts'
|
|
37
|
+
export {
|
|
38
|
+
HeadHandler,
|
|
39
|
+
Pipeline,
|
|
40
|
+
TailHandler,
|
|
41
|
+
WriteTracker,
|
|
42
|
+
} from './pipeline/index.ts'
|
|
43
|
+
export type { Sink } from './pipeline/index.ts'
|
|
44
|
+
export { EventQueue } from './queue/index.ts'
|
|
45
|
+
export type { QueueEvents } from './queue/index.ts'
|
|
46
|
+
export { pending, resolved } from './resolved/index.ts'
|
|
47
|
+
export { ServerChannel } from './server/index.ts'
|
|
48
|
+
export { ShutdownEvent } from './shutdown/index.ts'
|
|
49
|
+
export { SimpleInboundHandler } from './simple/index.ts'
|
|
50
|
+
export type { ServerAddress } from './server/index.ts'
|
|
51
|
+
export { SocketTransport } from './transport/index.ts'
|
|
52
|
+
export type { Transport } from './transport/index.ts'
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Per-channel settings, all optional
|
|
56
|
+
*/
|
|
57
|
+
export type ChannelOptions = {
|
|
58
|
+
/**
|
|
59
|
+
* Swap in pino or similar, the console is used otherwise
|
|
60
|
+
*/
|
|
61
|
+
readonly logger?: Logger
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Queued bytes at which the channel stops being writable, 64 KiB by default
|
|
65
|
+
*/
|
|
66
|
+
readonly highWaterMark?: number
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Has to drop below this to become writable again, 32 KiB by default
|
|
70
|
+
*/
|
|
71
|
+
readonly lowWaterMark?: number
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Hard cap on queued outbound bytes, writes past it fail, 64 MiB by default
|
|
75
|
+
*/
|
|
76
|
+
readonly maxPendingBytes?: number
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* One connection, with its own pipeline and an event queue that keeps things in order
|
|
81
|
+
* @class
|
|
82
|
+
*/
|
|
83
|
+
export class Channel {
|
|
84
|
+
/**
|
|
85
|
+
* 64 KiB, used when highWaterMark isn't given
|
|
86
|
+
*/
|
|
87
|
+
public static readonly defaultHighWaterMark: number = 64 * 1024
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* 32 KiB, used when lowWaterMark isn't given
|
|
91
|
+
*/
|
|
92
|
+
public static readonly defaultLowWaterMark: number = 32 * 1024
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* 64 MiB of queued writes before they start failing
|
|
96
|
+
*/
|
|
97
|
+
public static readonly defaultMaxPendingBytes: number = 64 * 1024 * 1024
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Counter for ids, process-wide
|
|
101
|
+
*/
|
|
102
|
+
private static nextId = 1
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Unique within this process, handy in logs
|
|
106
|
+
*/
|
|
107
|
+
public readonly id: number
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Where you add your handlers
|
|
111
|
+
*/
|
|
112
|
+
public readonly pipeline: Pipeline
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Per-connection state handlers can share, like a session or user id
|
|
116
|
+
*/
|
|
117
|
+
public readonly attributes: AttributeMap
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Resolves once the socket's gone and channelInactive has finished
|
|
121
|
+
*/
|
|
122
|
+
public readonly closed: Promise<void>
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* TCP, Unix or UDP socket underneath
|
|
126
|
+
*/
|
|
127
|
+
private readonly transport: Transport
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Holds writes until the socket takes them
|
|
131
|
+
*/
|
|
132
|
+
private readonly outbound: OutboundBuffer
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Keeps inbound events and executed tasks strictly in order
|
|
136
|
+
*/
|
|
137
|
+
private readonly queue: EventQueue
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Resolver behind closed
|
|
141
|
+
*/
|
|
142
|
+
private readonly closing: PromiseWithResolvers<undefined>
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Bound once up front so handleData doesn't make a closure per chunk
|
|
146
|
+
*/
|
|
147
|
+
private readonly readBytes: (bytes: Uint8Array) => Promise<void>
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Same trick as readBytes, for UDP packets
|
|
151
|
+
*/
|
|
152
|
+
private readonly readMessage: (message: unknown) => Promise<void>
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Bound once for the read without read complete
|
|
156
|
+
*/
|
|
157
|
+
private readonly readOnly: (message: unknown) => Promise<void>
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Bound once for the deferred read complete
|
|
161
|
+
*/
|
|
162
|
+
private readonly completeRead: () => void
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Bound once so the queue can run read complete without a closure
|
|
166
|
+
*/
|
|
167
|
+
private readonly fireReadComplete: () => Promise<void>
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* True while a read complete is waiting for the loop turn to end
|
|
171
|
+
*/
|
|
172
|
+
private completing: boolean
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Bytes read from the socket so far
|
|
176
|
+
*/
|
|
177
|
+
private received: number
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* True from channelActive until the socket closes
|
|
181
|
+
*/
|
|
182
|
+
private active: boolean
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Set once on close, nothing new gets in after that
|
|
186
|
+
*/
|
|
187
|
+
private finished: boolean
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Usually built by a bootstrap, activate has to be called before it's live
|
|
191
|
+
* @param transport socket to drive
|
|
192
|
+
* @param options logger and water marks
|
|
193
|
+
*/
|
|
194
|
+
public constructor(transport: Transport, options: ChannelOptions = {}) {
|
|
195
|
+
const logger = options.logger ?? new ConsoleLogger()
|
|
196
|
+
|
|
197
|
+
this.id = Channel.nextId++
|
|
198
|
+
this.transport = transport
|
|
199
|
+
this.active = false
|
|
200
|
+
this.finished = false
|
|
201
|
+
this.received = 0
|
|
202
|
+
this.closing = Promise.withResolvers<undefined>()
|
|
203
|
+
this.closed = this.closing.promise
|
|
204
|
+
this.attributes = new AttributeMap()
|
|
205
|
+
this.readBytes = (bytes: Uint8Array): Promise<void> =>
|
|
206
|
+
this.read(ByteBuf.wrap(bytes))
|
|
207
|
+
this.readMessage = (message: unknown): Promise<void> => this.read(message)
|
|
208
|
+
this.readOnly = (message: unknown): Promise<void> =>
|
|
209
|
+
this.pipeline.fireChannelRead(message)
|
|
210
|
+
this.completing = false
|
|
211
|
+
this.fireReadComplete = (): Promise<void> =>
|
|
212
|
+
this.pipeline.fireChannelReadComplete()
|
|
213
|
+
this.completeRead = (): void => {
|
|
214
|
+
this.completing = false
|
|
215
|
+
if (this.finished) return
|
|
216
|
+
|
|
217
|
+
void this.queue.runWith(this.fireReadComplete, undefined)
|
|
218
|
+
}
|
|
219
|
+
this.queue = new EventQueue({
|
|
220
|
+
/**
|
|
221
|
+
* Handlers are behind, so stop reading until they catch up
|
|
222
|
+
*/
|
|
223
|
+
onBusy: (): void => {
|
|
224
|
+
transport.pause()
|
|
225
|
+
},
|
|
226
|
+
/**
|
|
227
|
+
* Backlog's cleared, start reading again
|
|
228
|
+
*/
|
|
229
|
+
onIdle: (): void => {
|
|
230
|
+
transport.resume()
|
|
231
|
+
},
|
|
232
|
+
})
|
|
233
|
+
this.outbound = new OutboundBuffer(transport, {
|
|
234
|
+
/**
|
|
235
|
+
* Falls back to 64 KiB
|
|
236
|
+
*/
|
|
237
|
+
highWaterMark: options.highWaterMark ?? Channel.defaultHighWaterMark,
|
|
238
|
+
/**
|
|
239
|
+
* Falls back to 32 KiB
|
|
240
|
+
*/
|
|
241
|
+
lowWaterMark: options.lowWaterMark ?? Channel.defaultLowWaterMark,
|
|
242
|
+
/**
|
|
243
|
+
* Hard cap on queued bytes
|
|
244
|
+
*/
|
|
245
|
+
maxPendingBytes:
|
|
246
|
+
options.maxPendingBytes ?? Channel.defaultMaxPendingBytes,
|
|
247
|
+
/**
|
|
248
|
+
* Goes through the queue so it lands in order with other events
|
|
249
|
+
*/
|
|
250
|
+
onWritabilityChanged: (): void => {
|
|
251
|
+
void this.execute((): Promise<void> =>
|
|
252
|
+
this.pipeline.fireChannelWritabilityChanged(),
|
|
253
|
+
)
|
|
254
|
+
},
|
|
255
|
+
/**
|
|
256
|
+
* No promise to reject, so it ends up in exceptionCaught instead
|
|
257
|
+
*/
|
|
258
|
+
onSyncRefused: (error: Error): void => {
|
|
259
|
+
void this.handleError(error)
|
|
260
|
+
},
|
|
261
|
+
})
|
|
262
|
+
this.pipeline = new Pipeline(
|
|
263
|
+
this,
|
|
264
|
+
new HeadHandler({
|
|
265
|
+
/**
|
|
266
|
+
* Copied from the transport
|
|
267
|
+
*/
|
|
268
|
+
datagram: transport.datagram,
|
|
269
|
+
/**
|
|
270
|
+
* Into the outbound buffer, not the socket yet
|
|
271
|
+
*/
|
|
272
|
+
send: (
|
|
273
|
+
bytes: Uint8Array,
|
|
274
|
+
recipient: PacketAddress | null,
|
|
275
|
+
): Promise<void> => this.send(bytes, recipient),
|
|
276
|
+
/**
|
|
277
|
+
* Tells the tracker first so late writes still get flushed
|
|
278
|
+
*/
|
|
279
|
+
flush: (): Promise<void> => {
|
|
280
|
+
this.pipeline.tracker.requestFlush()
|
|
281
|
+
return this.outbound.flush()
|
|
282
|
+
},
|
|
283
|
+
/**
|
|
284
|
+
* Ends the socket and winds the channel down
|
|
285
|
+
*/
|
|
286
|
+
close: (): Promise<void> => this.shutdown(),
|
|
287
|
+
}),
|
|
288
|
+
logger,
|
|
289
|
+
)
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* True once set up and connected, false again after close
|
|
294
|
+
*/
|
|
295
|
+
public get isActive(): boolean {
|
|
296
|
+
return this.active
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* False while too much is queued to write, so hold off until it flips back
|
|
301
|
+
*/
|
|
302
|
+
public get isWritable(): boolean {
|
|
303
|
+
return this.outbound.isWritable
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Bytes read from the socket since it opened
|
|
308
|
+
*/
|
|
309
|
+
public get bytesRead(): number {
|
|
310
|
+
return this.received
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Bytes the socket has taken since it opened
|
|
315
|
+
*/
|
|
316
|
+
public get bytesWritten(): number {
|
|
317
|
+
return this.outbound.writtenBytes
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Bytes still waiting for the socket
|
|
322
|
+
*/
|
|
323
|
+
public get pendingBytes(): number {
|
|
324
|
+
return this.outbound.pendingBytes
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Peer IP, empty for an unconnected UDP socket
|
|
329
|
+
*/
|
|
330
|
+
public get remoteAddress(): string {
|
|
331
|
+
return this.transport.remoteAddress
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Peer port, 0 for an unconnected UDP socket
|
|
336
|
+
*/
|
|
337
|
+
public get remotePort(): number {
|
|
338
|
+
return this.transport.remotePort
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* Runs something in line with inbound events, errors go to exceptionCaught
|
|
343
|
+
* @param task work that must not overlap with handlers
|
|
344
|
+
*/
|
|
345
|
+
public execute(task: () => void | Promise<void>): Promise<void> {
|
|
346
|
+
return this.queue.run((): void | Promise<void> => this.guard(task))
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* Runs setup and fires channelActive, all before the first read is let through
|
|
351
|
+
* @param setup adds the handlers, or null if they're already there
|
|
352
|
+
*/
|
|
353
|
+
public activate(setup: ChannelSetup | null = null): Promise<void> {
|
|
354
|
+
return this.queue.run(async (): Promise<void> => {
|
|
355
|
+
if (!this.canActivate()) return
|
|
356
|
+
|
|
357
|
+
if (setup !== null) await setup(this)
|
|
358
|
+
await this.pipeline.settle()
|
|
359
|
+
if (!this.canActivate()) return
|
|
360
|
+
|
|
361
|
+
this.active = true
|
|
362
|
+
await this.pipeline.fireChannelActive()
|
|
363
|
+
})
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Goes through every outbound handler but waits for a flush to hit the socket
|
|
368
|
+
* @param message anything your encoders understand
|
|
369
|
+
*/
|
|
370
|
+
public write(message: unknown): Promise<void> {
|
|
371
|
+
return this.pipeline.write(message)
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* Cheaper write for when you won't await it
|
|
376
|
+
* @param message anything your encoders understand
|
|
377
|
+
*/
|
|
378
|
+
public writeSync(message: unknown): void {
|
|
379
|
+
this.pipeline.writeSync(message)
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* Fire-and-forget version of writeAndFlush
|
|
384
|
+
* @param message anything your encoders understand
|
|
385
|
+
*/
|
|
386
|
+
public writeAndFlushSync(message: unknown): void {
|
|
387
|
+
this.pipeline.writeAndFlushSync(message)
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* Sends whatever's been written so far
|
|
392
|
+
*/
|
|
393
|
+
public flush(): Promise<void> {
|
|
394
|
+
return this.pipeline.flush()
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* The one you'll want most of the time
|
|
399
|
+
* @param message anything your encoders understand
|
|
400
|
+
*/
|
|
401
|
+
public writeAndFlush(message: unknown): Promise<void> {
|
|
402
|
+
return this.pipeline.writeAndFlush(message)
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
/**
|
|
406
|
+
* Fast path for bytes you've already encoded, no handler sees them
|
|
407
|
+
* @param bytes ready for the wire
|
|
408
|
+
*/
|
|
409
|
+
public writeEncoded(bytes: Uint8Array | ByteBuf): Promise<void> {
|
|
410
|
+
return this.send(
|
|
411
|
+
bytes instanceof ByteBuf ? bytes.toUint8Array() : bytes,
|
|
412
|
+
null,
|
|
413
|
+
)
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* Fire-and-forget version of writeAndFlushEncoded, no promise is made
|
|
418
|
+
* @param bytes already encoded, nothing in the pipeline touches them
|
|
419
|
+
*/
|
|
420
|
+
public writeAndFlushEncodedSync(bytes: Uint8Array | ByteBuf): void {
|
|
421
|
+
void this.outbound.add(
|
|
422
|
+
bytes instanceof ByteBuf ? bytes.toUint8Array() : bytes,
|
|
423
|
+
null,
|
|
424
|
+
true,
|
|
425
|
+
)
|
|
426
|
+
void this.outbound.flush()
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* writeEncoded plus a flush, what ChannelGroup.broadcast uses
|
|
431
|
+
* @param bytes ready for the wire
|
|
432
|
+
*/
|
|
433
|
+
public writeAndFlushEncoded(bytes: Uint8Array | ByteBuf): Promise<void> {
|
|
434
|
+
const promise = this.writeEncoded(bytes)
|
|
435
|
+
|
|
436
|
+
void this.flush()
|
|
437
|
+
return promise
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* Gives outbound handlers a look before the socket is ended
|
|
442
|
+
*/
|
|
443
|
+
public close(): Promise<void> {
|
|
444
|
+
return this.pipeline.close()
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
* Bun's data callback lands here, dropped once we're closed
|
|
449
|
+
* @param bytes one chunk straight off the socket
|
|
450
|
+
*/
|
|
451
|
+
public handleData(bytes: Uint8Array): Promise<void> {
|
|
452
|
+
if (this.finished) return resolved
|
|
453
|
+
|
|
454
|
+
this.received += bytes.length
|
|
455
|
+
return this.queue.runWith(this.readBytes, bytes)
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/**
|
|
459
|
+
* Same as handleData for things that are already messages, like UDP packets
|
|
460
|
+
* @param message passed into the pipeline as it is
|
|
461
|
+
* @param size bytes it took on the wire
|
|
462
|
+
*/
|
|
463
|
+
public handleMessage(message: unknown, size = 0): Promise<void> {
|
|
464
|
+
if (this.finished) return resolved
|
|
465
|
+
|
|
466
|
+
this.received += size
|
|
467
|
+
return this.queue.runWith(this.readMessage, message)
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
/**
|
|
471
|
+
* Reads a message now and fires one read complete after the loop turn
|
|
472
|
+
* @param message passed into the pipeline as it is
|
|
473
|
+
* @param size bytes it took on the wire
|
|
474
|
+
*/
|
|
475
|
+
public handleBurst(message: unknown, size = 0): Promise<void> {
|
|
476
|
+
if (this.finished) return resolved
|
|
477
|
+
|
|
478
|
+
this.received += size
|
|
479
|
+
if (!this.completing) {
|
|
480
|
+
this.completing = true
|
|
481
|
+
setImmediate(this.completeRead)
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
return this.queue.runWith(this.readOnly, message)
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
/**
|
|
488
|
+
* Socket has room again, so try the queued writes
|
|
489
|
+
*/
|
|
490
|
+
public handleDrain(): void {
|
|
491
|
+
this.outbound.drain()
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
/**
|
|
495
|
+
* The socket is gone, wind everything down
|
|
496
|
+
*/
|
|
497
|
+
public handleClose(): Promise<void> {
|
|
498
|
+
return this.finish()
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
/**
|
|
502
|
+
* Socket errors go through the queue to exceptionCaught
|
|
503
|
+
* @param error from Bun or the outbound buffer
|
|
504
|
+
*/
|
|
505
|
+
public handleError(error: unknown): Promise<void> {
|
|
506
|
+
return this.execute((): Promise<void> =>
|
|
507
|
+
this.pipeline.fireExceptionCaught(error),
|
|
508
|
+
)
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
/**
|
|
512
|
+
* Turns a throw or rejection into exceptionCaught instead of a lost error
|
|
513
|
+
* @param task what execute was given
|
|
514
|
+
*/
|
|
515
|
+
private guard(task: () => void | Promise<void>): void | Promise<void> {
|
|
516
|
+
let result: void | Promise<void>
|
|
517
|
+
try {
|
|
518
|
+
result = task()
|
|
519
|
+
} catch (error) {
|
|
520
|
+
return this.pipeline.fireExceptionCaught(error)
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
const waiting = pending(result)
|
|
524
|
+
if (waiting === null) return resolved
|
|
525
|
+
return waiting.catch((error: unknown): Promise<void> =>
|
|
526
|
+
this.pipeline.fireExceptionCaught(error),
|
|
527
|
+
)
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
/**
|
|
531
|
+
* channelRead then channelReadComplete, skipping the extra await when it can
|
|
532
|
+
* @param message bytes or a packet from the transport
|
|
533
|
+
*/
|
|
534
|
+
private read(message: unknown): Promise<void> {
|
|
535
|
+
const reading = this.pipeline.fireChannelRead(message)
|
|
536
|
+
|
|
537
|
+
if (reading === resolved) return this.pipeline.fireChannelReadComplete()
|
|
538
|
+
return reading.then((): Promise<void> =>
|
|
539
|
+
this.pipeline.fireChannelReadComplete(),
|
|
540
|
+
)
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
/**
|
|
544
|
+
* Checked again after setup, since the socket can close while it runs
|
|
545
|
+
*/
|
|
546
|
+
private canActivate(): boolean {
|
|
547
|
+
return !this.active && !this.finished
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
/**
|
|
551
|
+
* Where reserve hands out room, valid until the next reserve
|
|
552
|
+
*/
|
|
553
|
+
public get stagingBuffer(): Uint8Array {
|
|
554
|
+
return this.outbound.stagingBuffer
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
/**
|
|
558
|
+
* Makes room for a small sync write straight in the outbound buffer, returns the offset into stagingBuffer or -1
|
|
559
|
+
* @param size bytes about to be written
|
|
560
|
+
*/
|
|
561
|
+
public reserve(size: number): number {
|
|
562
|
+
return this.outbound.reserve(size)
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
/**
|
|
566
|
+
* Finishes a staged write, flushing if a flush is waiting on it
|
|
567
|
+
*/
|
|
568
|
+
public commitStaged(): void {
|
|
569
|
+
if (this.pipeline.tracker.flushCarried) {
|
|
570
|
+
void this.outbound.flush().catch((): undefined => undefined)
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
/**
|
|
575
|
+
* Queues bytes for the socket and flushes if a flush is waiting on them
|
|
576
|
+
* @param bytes encoded bytes
|
|
577
|
+
* @param recipient UDP target, or null
|
|
578
|
+
*/
|
|
579
|
+
private send(
|
|
580
|
+
bytes: Uint8Array,
|
|
581
|
+
recipient: PacketAddress | null,
|
|
582
|
+
): Promise<void> {
|
|
583
|
+
const promise = this.outbound.add(bytes, recipient, this.pipeline.syncing)
|
|
584
|
+
|
|
585
|
+
if (this.pipeline.tracker.flushCarried) {
|
|
586
|
+
void this.outbound.flush().catch((): undefined => undefined)
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
return promise
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
/**
|
|
593
|
+
* What close turns into at the head, resolves without waiting for closed
|
|
594
|
+
*/
|
|
595
|
+
private shutdown(): Promise<void> {
|
|
596
|
+
if (!this.finished) this.transport.end()
|
|
597
|
+
|
|
598
|
+
void this.finish()
|
|
599
|
+
return Promise.resolve()
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
/**
|
|
603
|
+
* Safe to call more than once, rejects pending writes and fires channelInactive if we were active
|
|
604
|
+
*/
|
|
605
|
+
private finish(): Promise<void> {
|
|
606
|
+
if (this.finished) return this.closed
|
|
607
|
+
this.finished = true
|
|
608
|
+
|
|
609
|
+
const wasActive = this.active
|
|
610
|
+
this.active = false
|
|
611
|
+
this.outbound.fail(new Error('Channel is closed'))
|
|
612
|
+
void this.execute(async (): Promise<void> => {
|
|
613
|
+
if (wasActive) await this.pipeline.fireChannelInactive()
|
|
614
|
+
}).finally((): void => {
|
|
615
|
+
this.closing.resolve(undefined)
|
|
616
|
+
})
|
|
617
|
+
return this.closed
|
|
618
|
+
}
|
|
619
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { InboundHandlerAdapter, type ChannelHandler } from '../handler/index.ts'
|
|
2
|
+
import type { Channel } from '../index.ts'
|
|
3
|
+
import type { HandlerContext } from '../context/index.ts'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Your pipeline setup for a new channel, may be async
|
|
7
|
+
*/
|
|
8
|
+
export type ChannelSetup<C extends Channel = Channel> = (
|
|
9
|
+
channel: C,
|
|
10
|
+
) => void | Promise<void>
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Sets up a channel's pipeline once, then gets out of the way
|
|
14
|
+
* @class
|
|
15
|
+
*/
|
|
16
|
+
export class ChannelInitializer<
|
|
17
|
+
C extends Channel = Channel,
|
|
18
|
+
> extends InboundHandlerAdapter {
|
|
19
|
+
/**
|
|
20
|
+
* What to run against each new channel
|
|
21
|
+
*/
|
|
22
|
+
private readonly setup: ChannelSetup<C>
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Wraps your setup so it can sit in a pipeline like any handler
|
|
26
|
+
* @param setup adds handlers to the channel it's given
|
|
27
|
+
*/
|
|
28
|
+
public constructor(setup: ChannelSetup<C>) {
|
|
29
|
+
super()
|
|
30
|
+
|
|
31
|
+
this.setup = setup
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Turns a setup function, an initializer or a plain handler into an initializer
|
|
36
|
+
* @param handler what the bootstrap was given
|
|
37
|
+
*/
|
|
38
|
+
public static of<C extends Channel>(
|
|
39
|
+
handler: ChannelSetup<C> | ChannelHandler,
|
|
40
|
+
): ChannelInitializer<C> {
|
|
41
|
+
if (handler instanceof ChannelInitializer) {
|
|
42
|
+
return handler as ChannelInitializer<C>
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (typeof handler === 'function') {
|
|
46
|
+
return new ChannelInitializer<C>(handler)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const name =
|
|
50
|
+
handler.constructor.name === '' ? 'handler' : handler.constructor.name
|
|
51
|
+
return new ChannelInitializer<C>((channel: C): void => {
|
|
52
|
+
channel.pipeline.addLast<ChannelHandler>(name, handler)
|
|
53
|
+
})
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Runs the setup against a channel
|
|
58
|
+
* @param channel the channel being set up
|
|
59
|
+
*/
|
|
60
|
+
public initialize(channel: C): void | Promise<void> {
|
|
61
|
+
return this.setup(channel)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Runs the setup when added by hand, then takes itself back out
|
|
66
|
+
* @param context where it was added
|
|
67
|
+
*/
|
|
68
|
+
public async handlerAdded(context: HandlerContext): Promise<void> {
|
|
69
|
+
try {
|
|
70
|
+
await this.setup(context.channel as C)
|
|
71
|
+
} finally {
|
|
72
|
+
if (context.pipeline.context(context.name) === context) {
|
|
73
|
+
context.pipeline.remove<ChannelInitializer<C>>(context.name)
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|