@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,256 @@
|
|
|
1
|
+
import { ByteBuf } from '../../buffer/index.ts'
|
|
2
|
+
import type { Channel } from '../index.ts'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Filter for group operations, return true to include a channel
|
|
6
|
+
*/
|
|
7
|
+
export type ChannelMatcher = (channel: Channel) => boolean
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* One entry in GroupResult.failed
|
|
11
|
+
*/
|
|
12
|
+
export type GroupFailure = {
|
|
13
|
+
/**
|
|
14
|
+
* Which channel it went wrong on
|
|
15
|
+
*/
|
|
16
|
+
readonly channel: Channel
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Whatever that channel rejected with
|
|
20
|
+
*/
|
|
21
|
+
readonly error: unknown
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* What came back from a group operation, split by outcome
|
|
26
|
+
*/
|
|
27
|
+
export type GroupResult = {
|
|
28
|
+
/**
|
|
29
|
+
* Channels where it worked
|
|
30
|
+
*/
|
|
31
|
+
readonly succeeded: readonly Channel[]
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Channels where it didn't, each with its error
|
|
35
|
+
*/
|
|
36
|
+
readonly failed: readonly GroupFailure[]
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Handy for chat rooms and broadcasts, closed channels drop out on their own
|
|
41
|
+
* @class
|
|
42
|
+
*/
|
|
43
|
+
export class ChannelGroup implements Iterable<Channel> {
|
|
44
|
+
/**
|
|
45
|
+
* Current members
|
|
46
|
+
*/
|
|
47
|
+
private readonly channels: Set<Channel>
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Starts empty
|
|
51
|
+
*/
|
|
52
|
+
public constructor() {
|
|
53
|
+
this.channels = new Set<Channel>()
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The default matcher, includes everyone
|
|
58
|
+
*/
|
|
59
|
+
public static all(): ChannelMatcher {
|
|
60
|
+
return (): boolean => true
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Everyone apart from one, like the sender of a chat message
|
|
65
|
+
* @param excluded channel to leave out
|
|
66
|
+
*/
|
|
67
|
+
public static except(excluded: Channel): ChannelMatcher {
|
|
68
|
+
return (channel: Channel): boolean => channel !== excluded
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* How many channels are in here right now
|
|
73
|
+
*/
|
|
74
|
+
public get size(): number {
|
|
75
|
+
return this.channels.size
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Lets you for-of over the members
|
|
80
|
+
*/
|
|
81
|
+
public [Symbol.iterator](): Iterator<Channel> {
|
|
82
|
+
return this.channels.values()
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Adding twice is harmless, and the channel leaves by itself on close
|
|
87
|
+
* @param channel one to include
|
|
88
|
+
*/
|
|
89
|
+
public add(channel: Channel): this {
|
|
90
|
+
if (this.channels.has(channel)) return this
|
|
91
|
+
this.channels.add(channel)
|
|
92
|
+
|
|
93
|
+
void channel.closed.then((): void => {
|
|
94
|
+
this.channels.delete(channel)
|
|
95
|
+
})
|
|
96
|
+
return this
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Takes it out early without closing it, false if it wasn't there
|
|
101
|
+
* @param channel one to drop
|
|
102
|
+
*/
|
|
103
|
+
public remove(channel: Channel): boolean {
|
|
104
|
+
return this.channels.delete(channel)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* True while the channel is still a member
|
|
109
|
+
* @param channel one to look for
|
|
110
|
+
*/
|
|
111
|
+
public has(channel: Channel): boolean {
|
|
112
|
+
return this.channels.has(channel)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Linear scan over the members, null if nobody has that id
|
|
117
|
+
* @param id the channel's numeric id
|
|
118
|
+
*/
|
|
119
|
+
public find(id: number): Channel | null {
|
|
120
|
+
for (const channel of this.channels) {
|
|
121
|
+
if (channel.id === id) return channel
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return null
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Queues the message on each channel without flushing
|
|
129
|
+
* @param message what to send, ByteBufs get duplicated per channel
|
|
130
|
+
* @param matcher who to send to, everyone by default
|
|
131
|
+
*/
|
|
132
|
+
public write(
|
|
133
|
+
message: unknown,
|
|
134
|
+
matcher: ChannelMatcher = ChannelGroup.all(),
|
|
135
|
+
): Promise<GroupResult> {
|
|
136
|
+
return this.each(matcher, (channel: Channel): Promise<void> =>
|
|
137
|
+
channel.write(ChannelGroup.share(message)),
|
|
138
|
+
)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Pushes out whatever earlier writes queued
|
|
143
|
+
* @param matcher who to flush, everyone by default
|
|
144
|
+
*/
|
|
145
|
+
public flush(
|
|
146
|
+
matcher: ChannelMatcher = ChannelGroup.all(),
|
|
147
|
+
): Promise<GroupResult> {
|
|
148
|
+
return this.each(matcher, (channel: Channel): Promise<void> =>
|
|
149
|
+
channel.flush(),
|
|
150
|
+
)
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* The usual way to send to a group, each message goes through every pipeline
|
|
155
|
+
* @param message what to send, ByteBufs get duplicated per channel
|
|
156
|
+
* @param matcher who to send to, everyone by default
|
|
157
|
+
*/
|
|
158
|
+
public writeAndFlush(
|
|
159
|
+
message: unknown,
|
|
160
|
+
matcher: ChannelMatcher = ChannelGroup.all(),
|
|
161
|
+
): Promise<GroupResult> {
|
|
162
|
+
return this.each(matcher, (channel: Channel): Promise<void> =>
|
|
163
|
+
channel.writeAndFlush(ChannelGroup.share(message)),
|
|
164
|
+
)
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Sends the same bytes to every matching channel without making any promises, for game ticks
|
|
169
|
+
* @param bytes already encoded
|
|
170
|
+
* @param matcher which channels, all of them by default
|
|
171
|
+
*/
|
|
172
|
+
public broadcastSync(
|
|
173
|
+
bytes: Uint8Array | ByteBuf,
|
|
174
|
+
matcher: ChannelMatcher | null = null,
|
|
175
|
+
): number {
|
|
176
|
+
const shared = bytes instanceof ByteBuf ? bytes.toUint8Array() : bytes
|
|
177
|
+
|
|
178
|
+
let count = 0
|
|
179
|
+
for (const channel of this.channels) {
|
|
180
|
+
if (matcher !== null && !matcher(channel)) continue
|
|
181
|
+
channel.writeAndFlushEncodedSync(shared)
|
|
182
|
+
count += 1
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return count
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Faster fan-out for bytes you've already encoded, pipelines are skipped
|
|
190
|
+
* @param bytes ready to go on the wire as they are
|
|
191
|
+
* @param matcher who to send to, everyone by default
|
|
192
|
+
*/
|
|
193
|
+
public broadcast(
|
|
194
|
+
bytes: Uint8Array | ByteBuf,
|
|
195
|
+
matcher: ChannelMatcher = ChannelGroup.all(),
|
|
196
|
+
): Promise<GroupResult> {
|
|
197
|
+
const shared = bytes instanceof ByteBuf ? bytes.toUint8Array() : bytes
|
|
198
|
+
|
|
199
|
+
return this.each(matcher, (channel: Channel): Promise<void> =>
|
|
200
|
+
channel.writeAndFlushEncoded(shared),
|
|
201
|
+
)
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Closed channels also leave the group on their own
|
|
206
|
+
* @param matcher who to close, everyone by default
|
|
207
|
+
*/
|
|
208
|
+
public close(
|
|
209
|
+
matcher: ChannelMatcher = ChannelGroup.all(),
|
|
210
|
+
): Promise<GroupResult> {
|
|
211
|
+
return this.each(matcher, (channel: Channel): Promise<void> =>
|
|
212
|
+
channel.close(),
|
|
213
|
+
)
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Duplicates ByteBufs so one channel reading doesn't move another's index
|
|
218
|
+
* @param message anything, only ByteBufs get copied
|
|
219
|
+
*/
|
|
220
|
+
private static share(message: unknown): unknown {
|
|
221
|
+
return message instanceof ByteBuf ? message.duplicate() : message
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Snapshot taken up front so closes mid-loop don't upset iteration
|
|
226
|
+
* @param matcher filter to apply
|
|
227
|
+
*/
|
|
228
|
+
private matching(matcher: ChannelMatcher): Channel[] {
|
|
229
|
+
return [...this.channels].filter(matcher)
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Runs the operation everywhere at once and sorts the outcomes
|
|
234
|
+
* @param matcher filter to apply
|
|
235
|
+
* @param operation what to do to each channel
|
|
236
|
+
*/
|
|
237
|
+
private async each(
|
|
238
|
+
matcher: ChannelMatcher,
|
|
239
|
+
operation: (channel: Channel) => Promise<void>,
|
|
240
|
+
): Promise<GroupResult> {
|
|
241
|
+
const channels = this.matching(matcher)
|
|
242
|
+
|
|
243
|
+
const outcomes = await Promise.allSettled(channels.map(operation))
|
|
244
|
+
const succeeded: Channel[] = []
|
|
245
|
+
const failed: GroupFailure[] = []
|
|
246
|
+
outcomes.forEach(
|
|
247
|
+
(outcome: PromiseSettledResult<void>, index: number): void => {
|
|
248
|
+
const channel = channels[index]
|
|
249
|
+
if (channel === undefined) return
|
|
250
|
+
if (outcome.status === 'fulfilled') succeeded.push(channel)
|
|
251
|
+
else failed.push({ channel, error: outcome.reason })
|
|
252
|
+
},
|
|
253
|
+
)
|
|
254
|
+
return { succeeded, failed }
|
|
255
|
+
}
|
|
256
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import type { HandlerContext } from '../../context/index.ts'
|
|
2
|
+
import type { InboundHandler, OutboundHandler } from '../index.ts'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Base for handlers that see both directions, passes everything on until you override it
|
|
6
|
+
* @class
|
|
7
|
+
*/
|
|
8
|
+
export class DuplexHandlerAdapter<I = unknown, O = unknown>
|
|
9
|
+
implements InboundHandler<I>, OutboundHandler<O>
|
|
10
|
+
{
|
|
11
|
+
/**
|
|
12
|
+
* Passes activation on
|
|
13
|
+
* @param context where to pass it
|
|
14
|
+
*/
|
|
15
|
+
public channelActive(context: HandlerContext): void | Promise<void> {
|
|
16
|
+
return context.fireChannelActive()
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Passes deactivation on
|
|
21
|
+
* @param context where to pass it
|
|
22
|
+
*/
|
|
23
|
+
public channelInactive(context: HandlerContext): void | Promise<void> {
|
|
24
|
+
return context.fireChannelInactive()
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Passes the message on untouched
|
|
29
|
+
* @param context where to pass it
|
|
30
|
+
* @param message whatever came in
|
|
31
|
+
*/
|
|
32
|
+
public channelRead(
|
|
33
|
+
context: HandlerContext,
|
|
34
|
+
message: I,
|
|
35
|
+
): void | Promise<void> {
|
|
36
|
+
return context.fireChannelRead(message)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Passes the end of a read batch on
|
|
41
|
+
* @param context where to pass it
|
|
42
|
+
*/
|
|
43
|
+
public channelReadComplete(context: HandlerContext): void | Promise<void> {
|
|
44
|
+
return context.fireChannelReadComplete()
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Passes the writability change on
|
|
49
|
+
* @param context where to pass it
|
|
50
|
+
*/
|
|
51
|
+
public channelWritabilityChanged(
|
|
52
|
+
context: HandlerContext,
|
|
53
|
+
): void | Promise<void> {
|
|
54
|
+
return context.fireChannelWritabilityChanged()
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Passes the event on
|
|
59
|
+
* @param context where to pass it
|
|
60
|
+
* @param event your own event object
|
|
61
|
+
*/
|
|
62
|
+
public userEventTriggered(
|
|
63
|
+
context: HandlerContext,
|
|
64
|
+
event: unknown,
|
|
65
|
+
): void | Promise<void> {
|
|
66
|
+
return context.fireUserEventTriggered(event)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Passes the error on, the tail logs it if nobody handles it
|
|
71
|
+
* @param context where to pass it
|
|
72
|
+
* @param error whatever was thrown
|
|
73
|
+
*/
|
|
74
|
+
public exceptionCaught(
|
|
75
|
+
context: HandlerContext,
|
|
76
|
+
error: unknown,
|
|
77
|
+
): void | Promise<void> {
|
|
78
|
+
return context.fireExceptionCaught(error)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Passes the write on untouched
|
|
83
|
+
* @param context where to pass it
|
|
84
|
+
* @param message what's being sent
|
|
85
|
+
*/
|
|
86
|
+
public write(context: HandlerContext, message: O): Promise<void> {
|
|
87
|
+
return context.write(message)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Passes the flush on
|
|
92
|
+
* @param context where to pass it
|
|
93
|
+
*/
|
|
94
|
+
public flush(context: HandlerContext): Promise<void> {
|
|
95
|
+
return context.flush()
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Passes the close on
|
|
100
|
+
* @param context where to pass it
|
|
101
|
+
*/
|
|
102
|
+
public close(context: HandlerContext): Promise<void> {
|
|
103
|
+
return context.close()
|
|
104
|
+
}
|
|
105
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { HandlerContext } from '../../context/index.ts'
|
|
2
|
+
import type { InboundHandler } from '../index.ts'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Base for inbound handlers, passes everything on until you override it
|
|
6
|
+
* @class
|
|
7
|
+
*/
|
|
8
|
+
export class InboundHandlerAdapter<I = unknown> implements InboundHandler<I> {
|
|
9
|
+
/**
|
|
10
|
+
* Passes activation on
|
|
11
|
+
* @param context where to pass it
|
|
12
|
+
*/
|
|
13
|
+
public channelActive(context: HandlerContext): void | Promise<void> {
|
|
14
|
+
return context.fireChannelActive()
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Passes deactivation on
|
|
19
|
+
* @param context where to pass it
|
|
20
|
+
*/
|
|
21
|
+
public channelInactive(context: HandlerContext): void | Promise<void> {
|
|
22
|
+
return context.fireChannelInactive()
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Passes the message on untouched
|
|
27
|
+
* @param context where to pass it
|
|
28
|
+
* @param message whatever came in
|
|
29
|
+
*/
|
|
30
|
+
public channelRead(
|
|
31
|
+
context: HandlerContext,
|
|
32
|
+
message: I,
|
|
33
|
+
): void | Promise<void> {
|
|
34
|
+
return context.fireChannelRead(message)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Passes the end of a read batch on
|
|
39
|
+
* @param context where to pass it
|
|
40
|
+
*/
|
|
41
|
+
public channelReadComplete(context: HandlerContext): void | Promise<void> {
|
|
42
|
+
return context.fireChannelReadComplete()
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Passes the writability change on
|
|
47
|
+
* @param context where to pass it
|
|
48
|
+
*/
|
|
49
|
+
public channelWritabilityChanged(
|
|
50
|
+
context: HandlerContext,
|
|
51
|
+
): void | Promise<void> {
|
|
52
|
+
return context.fireChannelWritabilityChanged()
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Passes the event on
|
|
57
|
+
* @param context where to pass it
|
|
58
|
+
* @param event your own event object
|
|
59
|
+
*/
|
|
60
|
+
public userEventTriggered(
|
|
61
|
+
context: HandlerContext,
|
|
62
|
+
event: unknown,
|
|
63
|
+
): void | Promise<void> {
|
|
64
|
+
return context.fireUserEventTriggered(event)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Passes the error on, the tail logs it if nobody handles it
|
|
69
|
+
* @param context where to pass it
|
|
70
|
+
* @param error whatever was thrown
|
|
71
|
+
*/
|
|
72
|
+
public exceptionCaught(
|
|
73
|
+
context: HandlerContext,
|
|
74
|
+
error: unknown,
|
|
75
|
+
): void | Promise<void> {
|
|
76
|
+
return context.fireExceptionCaught(error)
|
|
77
|
+
}
|
|
78
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import type { HandlerContext } from '../context/index.ts'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Hooks every handler can have, inbound or outbound
|
|
5
|
+
*/
|
|
6
|
+
export type LifecycleCallbacks = {
|
|
7
|
+
/**
|
|
8
|
+
* Good place to set up timers or per-channel state
|
|
9
|
+
* @param context the slot the handler was just given
|
|
10
|
+
*/
|
|
11
|
+
handlerAdded?(context: HandlerContext): void | Promise<void>
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Clean up anything handlerAdded started
|
|
15
|
+
* @param context the slot that's going away
|
|
16
|
+
*/
|
|
17
|
+
handlerRemoved?(context: HandlerContext): void | Promise<void>
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Sees things travelling from the socket up towards the app
|
|
22
|
+
*/
|
|
23
|
+
export type InboundHandler<I = unknown> = {
|
|
24
|
+
/**
|
|
25
|
+
* The connection is up and ready to use
|
|
26
|
+
* @param context pipeline position
|
|
27
|
+
*/
|
|
28
|
+
channelActive?(context: HandlerContext): void | Promise<void>
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The connection has gone, no more reads or writes
|
|
32
|
+
* @param context pipeline position
|
|
33
|
+
*/
|
|
34
|
+
channelInactive?(context: HandlerContext): void | Promise<void>
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Called once per message, bytes or whatever earlier decoders produced
|
|
38
|
+
* @param context where to pass things on
|
|
39
|
+
* @param message whatever came in
|
|
40
|
+
*/
|
|
41
|
+
channelRead?(context: HandlerContext, message: I): void | Promise<void>
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Fires after the reads from one socket chunk are done
|
|
45
|
+
* @param context where to pass things on
|
|
46
|
+
*/
|
|
47
|
+
channelReadComplete?(context: HandlerContext): void | Promise<void>
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Check channel.isWritable to see which way it went
|
|
51
|
+
* @param context where to pass things on
|
|
52
|
+
*/
|
|
53
|
+
channelWritabilityChanged?(context: HandlerContext): void | Promise<void>
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Catches custom events like idle notifications
|
|
57
|
+
* @param context where to pass things on
|
|
58
|
+
* @param event anything fired with fireUserEventTriggered
|
|
59
|
+
*/
|
|
60
|
+
userEventTriggered?(
|
|
61
|
+
context: HandlerContext,
|
|
62
|
+
event: unknown,
|
|
63
|
+
): void | Promise<void>
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Something upstream threw or rejected
|
|
67
|
+
* @param context where to pass the error on if you don't want it
|
|
68
|
+
* @param error whatever was thrown
|
|
69
|
+
*/
|
|
70
|
+
exceptionCaught?(
|
|
71
|
+
context: HandlerContext,
|
|
72
|
+
error: unknown,
|
|
73
|
+
): void | Promise<void>
|
|
74
|
+
} & LifecycleCallbacks
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Sees things travelling from the app down towards the socket
|
|
78
|
+
*/
|
|
79
|
+
export type OutboundHandler<O = unknown> = {
|
|
80
|
+
/**
|
|
81
|
+
* Transform or inspect a message on its way out, then pass it on
|
|
82
|
+
* @param context where to pass the write on
|
|
83
|
+
* @param message what's being sent
|
|
84
|
+
*/
|
|
85
|
+
write?(context: HandlerContext, message: O): Promise<void>
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Takes a fire-and-forget write on the spot, false to get it through write instead
|
|
89
|
+
* @param context where the write is going
|
|
90
|
+
* @param message what's being sent
|
|
91
|
+
*/
|
|
92
|
+
writeSync?(context: HandlerContext, message: O): boolean
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Asked to push queued writes out to the socket
|
|
96
|
+
* @param context where to pass the flush on
|
|
97
|
+
*/
|
|
98
|
+
flush?(context: HandlerContext): Promise<void>
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Asked to shut the connection down
|
|
102
|
+
* @param context where to pass the close on
|
|
103
|
+
*/
|
|
104
|
+
close?(context: HandlerContext): Promise<void>
|
|
105
|
+
} & LifecycleCallbacks
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Anything you can add to a pipeline
|
|
109
|
+
*/
|
|
110
|
+
export type ChannelHandler = InboundHandler | OutboundHandler
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* For handlers that sit on both directions, like codecs
|
|
114
|
+
*/
|
|
115
|
+
export type DuplexHandler = InboundHandler & OutboundHandler
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Method names like channelRead or exceptionCaught
|
|
119
|
+
*/
|
|
120
|
+
export type InboundEvent = Exclude<
|
|
121
|
+
keyof InboundHandler,
|
|
122
|
+
keyof LifecycleCallbacks
|
|
123
|
+
>
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Method names like write, flush and close
|
|
127
|
+
*/
|
|
128
|
+
export type OutboundEvent = Exclude<
|
|
129
|
+
keyof OutboundHandler,
|
|
130
|
+
keyof LifecycleCallbacks
|
|
131
|
+
>
|
|
132
|
+
|
|
133
|
+
export { DuplexHandlerAdapter } from './duplex/index.ts'
|
|
134
|
+
export { InboundHandlerAdapter } from './inbound/index.ts'
|
|
135
|
+
export { OutboundHandlerAdapter } from './outbound/index.ts'
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { HandlerContext } from '../../context/index.ts'
|
|
2
|
+
import type { OutboundHandler } from '../index.ts'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Base for outbound handlers, passes everything on until you override it
|
|
6
|
+
* @class
|
|
7
|
+
*/
|
|
8
|
+
export class OutboundHandlerAdapter<O = unknown> implements OutboundHandler<O> {
|
|
9
|
+
/**
|
|
10
|
+
* Passes the write on untouched
|
|
11
|
+
* @param context where to pass it
|
|
12
|
+
* @param message what's being sent
|
|
13
|
+
*/
|
|
14
|
+
public write(context: HandlerContext, message: O): Promise<void> {
|
|
15
|
+
return context.write(message)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Passes the flush on
|
|
20
|
+
* @param context where to pass it
|
|
21
|
+
*/
|
|
22
|
+
public flush(context: HandlerContext): Promise<void> {
|
|
23
|
+
return context.flush()
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Passes the close on
|
|
28
|
+
* @param context where to pass it
|
|
29
|
+
*/
|
|
30
|
+
public close(context: HandlerContext): Promise<void> {
|
|
31
|
+
return context.close()
|
|
32
|
+
}
|
|
33
|
+
}
|