@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,621 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ChannelHandler,
|
|
3
|
+
DuplexHandler,
|
|
4
|
+
InboundEvent,
|
|
5
|
+
OutboundEvent,
|
|
6
|
+
} from '../handler/index.ts'
|
|
7
|
+
import { DuplexHandlerAdapter } from '../handler/duplex/index.ts'
|
|
8
|
+
import { InboundHandlerAdapter } from '../handler/inbound/index.ts'
|
|
9
|
+
import { OutboundHandlerAdapter } from '../handler/outbound/index.ts'
|
|
10
|
+
import type { Pipeline } from '../pipeline/index.ts'
|
|
11
|
+
import type { WriteTracker } from '../pipeline/tracker/index.ts'
|
|
12
|
+
import { ByteBuf } from '../../buffer/index.ts'
|
|
13
|
+
import { pending, protect, resolved } from '../resolved/index.ts'
|
|
14
|
+
import type { Channel } from '../index.ts'
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* One link in the pipeline's chain, what handlers use to pass things along
|
|
18
|
+
* @class
|
|
19
|
+
*/
|
|
20
|
+
export class HandlerContext {
|
|
21
|
+
/**
|
|
22
|
+
* One bit per callback, so a lookup is a single AND
|
|
23
|
+
*/
|
|
24
|
+
private static readonly bits: Readonly<
|
|
25
|
+
Record<InboundEvent | OutboundEvent, number>
|
|
26
|
+
> = {
|
|
27
|
+
channelActive: 1,
|
|
28
|
+
channelInactive: 2,
|
|
29
|
+
channelRead: 4,
|
|
30
|
+
channelReadComplete: 8,
|
|
31
|
+
channelWritabilityChanged: 16,
|
|
32
|
+
userEventTriggered: 32,
|
|
33
|
+
exceptionCaught: 64,
|
|
34
|
+
write: 128,
|
|
35
|
+
flush: 256,
|
|
36
|
+
close: 512,
|
|
37
|
+
writeSync: 1024,
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The base classes' pass-through methods, which never need calling
|
|
42
|
+
*/
|
|
43
|
+
private static readonly defaults: ReadonlySet<unknown> =
|
|
44
|
+
HandlerContext.collectDefaults()
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Unique within the pipeline, used for lookups and removal
|
|
48
|
+
*/
|
|
49
|
+
public readonly name: string
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Pipeline this link belongs to
|
|
53
|
+
*/
|
|
54
|
+
public readonly pipeline: Pipeline
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Neighbour towards the head, null for the head itself
|
|
58
|
+
*/
|
|
59
|
+
public previous: HandlerContext | null
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Neighbour towards the tail, null for the tail itself
|
|
63
|
+
*/
|
|
64
|
+
public next: HandlerContext | null
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Same handler typed as duplex so we can probe for any callback
|
|
68
|
+
*/
|
|
69
|
+
private readonly target: DuplexHandler
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Which callbacks this handler really implements, defaults left out
|
|
73
|
+
*/
|
|
74
|
+
private readonly mask: number
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Whether the handler has writeSync
|
|
78
|
+
*/
|
|
79
|
+
private readonly takesSync: boolean
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Pipeline version the cached lookups belong to
|
|
83
|
+
*/
|
|
84
|
+
private cachedAt: number
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Next context that reads, cached
|
|
88
|
+
*/
|
|
89
|
+
private reader: HandlerContext | null
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Next context that wants read complete, cached
|
|
93
|
+
*/
|
|
94
|
+
private completer: HandlerContext | null
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Next context that writes, cached
|
|
98
|
+
*/
|
|
99
|
+
private writer: HandlerContext | null
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Next context that flushes, cached
|
|
103
|
+
*/
|
|
104
|
+
private flusher: HandlerContext | null
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Pipeline does the linking, this only stores the pieces
|
|
108
|
+
* @param pipeline owner of this link
|
|
109
|
+
* @param name label for the handler
|
|
110
|
+
* @param handler the thing doing the work
|
|
111
|
+
*/
|
|
112
|
+
public constructor(
|
|
113
|
+
pipeline: Pipeline,
|
|
114
|
+
name: string,
|
|
115
|
+
handler: ChannelHandler,
|
|
116
|
+
) {
|
|
117
|
+
this.name = name
|
|
118
|
+
this.pipeline = pipeline
|
|
119
|
+
this.previous = null
|
|
120
|
+
this.next = null
|
|
121
|
+
this.target = handler
|
|
122
|
+
this.mask = HandlerContext.maskOf(handler)
|
|
123
|
+
this.takesSync = (this.mask & HandlerContext.bits.writeSync) !== 0
|
|
124
|
+
this.cachedAt = -1
|
|
125
|
+
this.reader = null
|
|
126
|
+
this.completer = null
|
|
127
|
+
this.writer = null
|
|
128
|
+
this.flusher = null
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Masks by prototype, so each handler class is worked out once
|
|
133
|
+
*/
|
|
134
|
+
private static readonly masks: WeakMap<object, number> = new WeakMap<
|
|
135
|
+
object,
|
|
136
|
+
number
|
|
137
|
+
>()
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Every callback name the mask covers
|
|
141
|
+
*/
|
|
142
|
+
private static readonly events: readonly string[] = Object.keys(
|
|
143
|
+
HandlerContext.bits,
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Gathers the pass-through methods from the base classes
|
|
148
|
+
*/
|
|
149
|
+
private static collectDefaults(): ReadonlySet<unknown> {
|
|
150
|
+
const prototypes: readonly object[] = [
|
|
151
|
+
InboundHandlerAdapter.prototype,
|
|
152
|
+
OutboundHandlerAdapter.prototype,
|
|
153
|
+
DuplexHandlerAdapter.prototype,
|
|
154
|
+
]
|
|
155
|
+
|
|
156
|
+
const defaults = new Set<unknown>()
|
|
157
|
+
for (const prototype of prototypes) {
|
|
158
|
+
for (const event of Object.keys(HandlerContext.bits)) {
|
|
159
|
+
defaults.add(Reflect.get(prototype, event) as unknown)
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return defaults
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Works out which callbacks a handler overrides
|
|
168
|
+
* @param handler the handler being added
|
|
169
|
+
*/
|
|
170
|
+
private static maskOf(handler: DuplexHandler): number {
|
|
171
|
+
const prototype: unknown = Object.getPrototypeOf(handler)
|
|
172
|
+
const shared =
|
|
173
|
+
typeof prototype === 'object' &&
|
|
174
|
+
prototype !== null &&
|
|
175
|
+
prototype !== Object.prototype &&
|
|
176
|
+
!HandlerContext.hasOwnCallbacks(handler)
|
|
177
|
+
if (shared) {
|
|
178
|
+
const cached = HandlerContext.masks.get(prototype)
|
|
179
|
+
if (cached !== undefined) return cached
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const mask = HandlerContext.computeMask(handler)
|
|
183
|
+
if (shared) HandlerContext.masks.set(prototype, mask)
|
|
184
|
+
return mask
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Whether a handler carries callbacks as its own properties, which rules out caching
|
|
189
|
+
* @param handler the handler being added
|
|
190
|
+
*/
|
|
191
|
+
private static hasOwnCallbacks(handler: DuplexHandler): boolean {
|
|
192
|
+
for (const event of HandlerContext.events) {
|
|
193
|
+
if (Object.hasOwn(handler, event)) return true
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return false
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Builds the mask by looking up each callback
|
|
201
|
+
* @param handler the handler being added
|
|
202
|
+
*/
|
|
203
|
+
private static computeMask(handler: DuplexHandler): number {
|
|
204
|
+
let mask = 0
|
|
205
|
+
|
|
206
|
+
for (const [event, bit] of Object.entries(HandlerContext.bits)) {
|
|
207
|
+
const method = Reflect.get(handler, event) as unknown
|
|
208
|
+
if (method !== undefined && !HandlerContext.defaults.has(method)) {
|
|
209
|
+
mask |= bit
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
return mask
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Whatever was added to the pipeline under this name
|
|
218
|
+
*/
|
|
219
|
+
public get handler(): ChannelHandler {
|
|
220
|
+
return this.target
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Shortcut for pipeline.channel
|
|
225
|
+
*/
|
|
226
|
+
public get channel(): Channel {
|
|
227
|
+
return this.pipeline.channel
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Tells the next inbound handler the connection is up
|
|
232
|
+
*/
|
|
233
|
+
public fireChannelActive(): Promise<void> {
|
|
234
|
+
return this.dispatch('channelActive', undefined)
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Tells the next inbound handler the connection has gone
|
|
239
|
+
*/
|
|
240
|
+
public fireChannelInactive(): Promise<void> {
|
|
241
|
+
return this.dispatch('channelInactive', undefined)
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Hands a message to the next handler that reads
|
|
246
|
+
* @param message whatever you want the next handler to see
|
|
247
|
+
*/
|
|
248
|
+
public fireChannelRead(message: unknown): Promise<void> {
|
|
249
|
+
if (this.cachedAt !== this.pipeline.version) this.refresh()
|
|
250
|
+
const target = this.reader
|
|
251
|
+
|
|
252
|
+
return target === null ? resolved : target.invokeRead(message)
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Lets later handlers know this batch of reads is done
|
|
257
|
+
*/
|
|
258
|
+
public fireChannelReadComplete(): Promise<void> {
|
|
259
|
+
if (this.cachedAt !== this.pipeline.version) this.refresh()
|
|
260
|
+
const target = this.completer
|
|
261
|
+
|
|
262
|
+
return target === null
|
|
263
|
+
? resolved
|
|
264
|
+
: target.invoke('channelReadComplete', undefined)
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Forwards a writability flip to the next handler that cares
|
|
269
|
+
*/
|
|
270
|
+
public fireChannelWritabilityChanged(): Promise<void> {
|
|
271
|
+
return this.dispatch('channelWritabilityChanged', undefined)
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Sends your own event down the pipeline, like an idle notice
|
|
276
|
+
* @param event anything later handlers know how to recognise
|
|
277
|
+
*/
|
|
278
|
+
public fireUserEventTriggered(event: unknown): Promise<void> {
|
|
279
|
+
return this.dispatch('userEventTriggered', event)
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Hands an error to the next handler with exceptionCaught
|
|
284
|
+
* @param error whatever went wrong
|
|
285
|
+
*/
|
|
286
|
+
public fireExceptionCaught(error: unknown): Promise<void> {
|
|
287
|
+
const target = this.findInbound('exceptionCaught')
|
|
288
|
+
|
|
289
|
+
return target === null ? resolved : target.notify(error)
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* Passes a message to the next outbound handler, rejects if none takes it
|
|
294
|
+
* @param message what to send
|
|
295
|
+
*/
|
|
296
|
+
public write(message: unknown): Promise<void> {
|
|
297
|
+
if (message instanceof ByteBuf) message.detach()
|
|
298
|
+
this.pipeline.tracker.forwarded(this)
|
|
299
|
+
if (this.cachedAt !== this.pipeline.version) this.refresh()
|
|
300
|
+
const target = this.writer
|
|
301
|
+
|
|
302
|
+
return protect(
|
|
303
|
+
target === null
|
|
304
|
+
? Promise.reject(new Error('Write is not handled'))
|
|
305
|
+
: target.send(message),
|
|
306
|
+
)
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Asks the outbound handlers ahead of us to push queued writes out
|
|
311
|
+
*/
|
|
312
|
+
public flush(): Promise<void> {
|
|
313
|
+
if (this.cachedAt !== this.pipeline.version) this.refresh()
|
|
314
|
+
const target = this.flusher
|
|
315
|
+
|
|
316
|
+
return protect(target === null ? resolved : target.pushFlush())
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* Whether the next write lands on the socket with no outbound handler in between
|
|
321
|
+
*/
|
|
322
|
+
public get writesToSocket(): boolean {
|
|
323
|
+
if (this.cachedAt !== this.pipeline.version) this.refresh()
|
|
324
|
+
const target = this.writer
|
|
325
|
+
|
|
326
|
+
return target !== null && target.previous === null
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Fire-and-forget write, lets the pipeline skip tracking the promise
|
|
331
|
+
* @param message whatever the next outbound handler expects
|
|
332
|
+
*/
|
|
333
|
+
public writeSync(message: unknown): void {
|
|
334
|
+
const pipeline = this.pipeline
|
|
335
|
+
pipeline.tracker.forwarded(this)
|
|
336
|
+
if (this.cachedAt !== pipeline.version) this.refresh()
|
|
337
|
+
const target = this.writer
|
|
338
|
+
if (target === null) return
|
|
339
|
+
if (target.takesSync && target.takeSync(message)) return
|
|
340
|
+
if (message instanceof ByteBuf) message.detach()
|
|
341
|
+
|
|
342
|
+
const previous = pipeline.syncing
|
|
343
|
+
pipeline.syncing = true
|
|
344
|
+
const result =
|
|
345
|
+
target.previous === null
|
|
346
|
+
? target.sendAtHead(message)
|
|
347
|
+
: target.sendTracked(pipeline.tracker, message)
|
|
348
|
+
pipeline.syncing = previous
|
|
349
|
+
if (result !== resolved) void protect(result)
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* writeSync plus a flush, for when you don't care how it went
|
|
354
|
+
* @param message what to send
|
|
355
|
+
*/
|
|
356
|
+
public writeAndFlushSync(message: unknown): void {
|
|
357
|
+
this.writeSync(message)
|
|
358
|
+
void this.flush()
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* The promise follows the write, the flush isn't awaited
|
|
363
|
+
* @param message what to send
|
|
364
|
+
*/
|
|
365
|
+
public writeAndFlush(message: unknown): Promise<void> {
|
|
366
|
+
const promise = this.write(message)
|
|
367
|
+
|
|
368
|
+
void this.flush()
|
|
369
|
+
return promise
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* Sends a close request towards the head from this point
|
|
374
|
+
*/
|
|
375
|
+
public close(): Promise<void> {
|
|
376
|
+
const target = this.findOutbound('close')
|
|
377
|
+
|
|
378
|
+
const promise =
|
|
379
|
+
target === null
|
|
380
|
+
? Promise.reject(new Error('Close is not handled'))
|
|
381
|
+
: target.shut()
|
|
382
|
+
void promise.catch((): undefined => undefined)
|
|
383
|
+
return promise
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Skips handlers that don't implement the event, resolves if nobody does
|
|
388
|
+
* @param event which callback to look for
|
|
389
|
+
* @param argument message or event payload, undefined for the rest
|
|
390
|
+
*/
|
|
391
|
+
private dispatch(event: InboundEvent, argument: unknown): Promise<void> {
|
|
392
|
+
const target = this.findInbound(event)
|
|
393
|
+
|
|
394
|
+
return target === null ? resolved : target.invoke(event, argument)
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* Looks up the hot neighbours again after the links changed
|
|
399
|
+
*/
|
|
400
|
+
private refresh(): void {
|
|
401
|
+
this.reader = this.findInbound('channelRead')
|
|
402
|
+
this.completer = this.findInbound('channelReadComplete')
|
|
403
|
+
this.writer = this.findOutbound('write')
|
|
404
|
+
this.flusher = this.findOutbound('flush')
|
|
405
|
+
this.cachedAt = this.pipeline.version
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* Walks towards the tail, null if we fall off the end
|
|
410
|
+
* @param event callback the handler must have
|
|
411
|
+
*/
|
|
412
|
+
private findInbound(event: InboundEvent): HandlerContext | null {
|
|
413
|
+
let context = this.next
|
|
414
|
+
|
|
415
|
+
const bit = HandlerContext.bits[event]
|
|
416
|
+
while (context !== null && (context.mask & bit) === 0) {
|
|
417
|
+
context = context.next
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
return context
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* Walks towards the head, null if we fall off the end
|
|
425
|
+
* @param event callback the handler must have
|
|
426
|
+
*/
|
|
427
|
+
private findOutbound(event: OutboundEvent): HandlerContext | null {
|
|
428
|
+
let context = this.previous
|
|
429
|
+
|
|
430
|
+
const bit = HandlerContext.bits[event]
|
|
431
|
+
while (context !== null && (context.mask & bit) === 0) {
|
|
432
|
+
context = context.previous
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
return context
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* Runs our handler's callback, turning throws and rejections into exceptionCaught
|
|
440
|
+
* @param event which callback to run
|
|
441
|
+
* @param argument message or event payload, if any
|
|
442
|
+
*/
|
|
443
|
+
private invoke(event: InboundEvent, argument: unknown): Promise<void> {
|
|
444
|
+
let result: void | Promise<void>
|
|
445
|
+
try {
|
|
446
|
+
result = this.call(event, argument)
|
|
447
|
+
} catch (error) {
|
|
448
|
+
return this.fail(error)
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
const waiting = pending(result)
|
|
452
|
+
if (waiting === null) return resolved
|
|
453
|
+
return waiting.catch((error: unknown): Promise<void> => this.fail(error))
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
/**
|
|
457
|
+
* invoke for channelRead alone, the one callback that runs per message
|
|
458
|
+
* @param message what came in
|
|
459
|
+
*/
|
|
460
|
+
private invokeRead(message: unknown): Promise<void> {
|
|
461
|
+
let result: void | Promise<void>
|
|
462
|
+
try {
|
|
463
|
+
result = this.target.channelRead?.(this, message)
|
|
464
|
+
} catch (error) {
|
|
465
|
+
return this.fail(error)
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
if (result === undefined || result === resolved) return resolved
|
|
469
|
+
return result.catch((error: unknown): Promise<void> => this.fail(error))
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
/**
|
|
473
|
+
* Plain switch so each callback gets the right arguments
|
|
474
|
+
* @param event which callback to run
|
|
475
|
+
* @param argument ignored by the callbacks that take none
|
|
476
|
+
*/
|
|
477
|
+
private call(event: InboundEvent, argument: unknown): void | Promise<void> {
|
|
478
|
+
const handler = this.target
|
|
479
|
+
|
|
480
|
+
switch (event) {
|
|
481
|
+
case 'channelRead':
|
|
482
|
+
return handler.channelRead?.(this, argument)
|
|
483
|
+
case 'channelReadComplete':
|
|
484
|
+
return handler.channelReadComplete?.(this)
|
|
485
|
+
case 'channelActive':
|
|
486
|
+
return handler.channelActive?.(this)
|
|
487
|
+
case 'channelInactive':
|
|
488
|
+
return handler.channelInactive?.(this)
|
|
489
|
+
case 'channelWritabilityChanged':
|
|
490
|
+
return handler.channelWritabilityChanged?.(this)
|
|
491
|
+
case 'userEventTriggered':
|
|
492
|
+
return handler.userEventTriggered?.(this, argument)
|
|
493
|
+
case 'exceptionCaught':
|
|
494
|
+
return handler.exceptionCaught?.(this, argument)
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
/**
|
|
499
|
+
* Our own exceptionCaught gets first go, otherwise it moves down the line
|
|
500
|
+
* @param error what our handler threw
|
|
501
|
+
*/
|
|
502
|
+
private fail(error: unknown): Promise<void> {
|
|
503
|
+
if (this.target.exceptionCaught === undefined) {
|
|
504
|
+
return this.fireExceptionCaught(error)
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
return this.notify(error)
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
/**
|
|
511
|
+
* Runs exceptionCaught, and only logs if that throws too, to avoid loops
|
|
512
|
+
* @param error passed to the handler
|
|
513
|
+
*/
|
|
514
|
+
private notify(error: unknown): Promise<void> {
|
|
515
|
+
let result: void | Promise<void>
|
|
516
|
+
try {
|
|
517
|
+
result = this.target.exceptionCaught?.(this, error)
|
|
518
|
+
} catch (failure) {
|
|
519
|
+
this.report(failure)
|
|
520
|
+
return resolved
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
const waiting = pending(result)
|
|
524
|
+
if (waiting === null) return resolved
|
|
525
|
+
return waiting.catch((failure: unknown): void => {
|
|
526
|
+
this.report(failure)
|
|
527
|
+
})
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
/**
|
|
531
|
+
* Dead end for errors raised inside exceptionCaught
|
|
532
|
+
* @param failure what the error handler threw
|
|
533
|
+
*/
|
|
534
|
+
private report(failure: unknown): void {
|
|
535
|
+
this.pipeline.logger.warn({ err: failure }, 'Error handler failed')
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
/**
|
|
539
|
+
* Runs our write and tells the tracker, so flushes wait for it to forward
|
|
540
|
+
* @param message what to send
|
|
541
|
+
*/
|
|
542
|
+
private send(message: unknown): Promise<void> {
|
|
543
|
+
if (this.previous === null) return this.sendAtHead(message)
|
|
544
|
+
|
|
545
|
+
return this.sendTracked(this.pipeline.tracker, message)
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
/**
|
|
549
|
+
* Runs our write between the tracker's begin and returned
|
|
550
|
+
* @param tracker the pipeline's tracker
|
|
551
|
+
* @param message what to send
|
|
552
|
+
*/
|
|
553
|
+
private sendTracked(tracker: WriteTracker, message: unknown): Promise<void> {
|
|
554
|
+
const pass = tracker.begin(this)
|
|
555
|
+
|
|
556
|
+
let result: Promise<void>
|
|
557
|
+
try {
|
|
558
|
+
result = this.target.write?.(this, message) ?? resolved
|
|
559
|
+
} catch (error) {
|
|
560
|
+
result = Promise.reject(
|
|
561
|
+
error instanceof Error
|
|
562
|
+
? error
|
|
563
|
+
: new Error('Write failed', { cause: error }),
|
|
564
|
+
)
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
tracker.returned(pass, result)
|
|
568
|
+
return result
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
/**
|
|
572
|
+
* Offers our handler the write on the spot, reporting a throw to exceptionCaught
|
|
573
|
+
* @param message what to send
|
|
574
|
+
*/
|
|
575
|
+
private takeSync(message: unknown): boolean {
|
|
576
|
+
try {
|
|
577
|
+
return this.target.writeSync?.(this, message) === true
|
|
578
|
+
} catch (error) {
|
|
579
|
+
void this.fireExceptionCaught(error)
|
|
580
|
+
return true
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
/**
|
|
585
|
+
* Head has nowhere to forward to, so tracking is skipped
|
|
586
|
+
* @param message what to send
|
|
587
|
+
*/
|
|
588
|
+
private sendAtHead(message: unknown): Promise<void> {
|
|
589
|
+
try {
|
|
590
|
+
return this.target.write?.(this, message) ?? resolved
|
|
591
|
+
} catch (error) {
|
|
592
|
+
return Promise.reject(
|
|
593
|
+
error instanceof Error
|
|
594
|
+
? error
|
|
595
|
+
: new Error('Write failed', { cause: error }),
|
|
596
|
+
)
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
/**
|
|
601
|
+
* Runs our flush, turning a throw into a rejection
|
|
602
|
+
*/
|
|
603
|
+
private pushFlush(): Promise<void> {
|
|
604
|
+
try {
|
|
605
|
+
return this.target.flush?.(this) ?? resolved
|
|
606
|
+
} catch (error) {
|
|
607
|
+
return Promise.reject(
|
|
608
|
+
error instanceof Error
|
|
609
|
+
? error
|
|
610
|
+
: new Error('Flush failed', { cause: error }),
|
|
611
|
+
)
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
/**
|
|
616
|
+
* Runs our close, async so a throw comes back as a rejection
|
|
617
|
+
*/
|
|
618
|
+
private async shut(): Promise<void> {
|
|
619
|
+
await this.target.close?.(this)
|
|
620
|
+
}
|
|
621
|
+
}
|