@netifyjs/netify 1.0.0-alpha.4 → 1.0.0-alpha.5
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/README.md +6 -6
- package/package.json +1 -1
- package/src/bootstrap/client/index.ts +1 -1
- package/src/bootstrap/dispatcher/index.ts +7 -0
- package/src/bootstrap/reconnect/index.ts +14 -4
- package/src/bootstrap/server/index.ts +10 -4
- package/src/buffer/index.ts +229 -88
- package/src/buffer/quantizer/index.ts +16 -3
- package/src/channel/context/index.ts +18 -11
- package/src/channel/handler/index.ts +1 -1
- package/src/channel/index.ts +30 -7
- package/src/channel/outbound/index.ts +28 -12
- package/src/channel/pipeline/index.ts +45 -5
- package/src/channel/queue/index.ts +3 -2
- package/src/channel/resolved/index.ts +13 -3
- package/src/channel/server/index.ts +19 -10
- package/src/cluster/index.ts +73 -32
- package/src/codec/binary/index.ts +19 -0
- package/src/codec/decoder/index.ts +223 -83
- package/src/codec/delimiter/index.ts +95 -37
- package/src/codec/encoder/index.ts +30 -5
- package/src/codec/fixed/index.ts +5 -1
- package/src/codec/http/aggregator/index.ts +45 -16
- package/src/codec/http/decoder/object/index.ts +156 -40
- package/src/codec/http/decoder/request/index.ts +6 -2
- package/src/codec/http/encoder/object/index.ts +2 -7
- package/src/codec/http/encoder/request/index.ts +5 -0
- package/src/codec/http/headers/index.ts +45 -2
- package/src/codec/http/message/full/request/index.ts +1 -1
- package/src/codec/http/message/full/response/index.ts +1 -1
- package/src/codec/http/message/request/index.ts +14 -1
- package/src/codec/http/message/response/index.ts +1 -1
- package/src/codec/http/version/index.ts +3 -0
- package/src/codec/length/index.ts +66 -44
- package/src/codec/message/decoder/index.ts +18 -1
- package/src/codec/message/encoder/index.ts +85 -8
- package/src/codec/prepender/index.ts +4 -1
- package/src/codec/string/decoder/index.ts +6 -1
- package/src/codec/websocket/decoder/index.ts +78 -3
- package/src/codec/websocket/encoder/index.ts +90 -6
- package/src/codec/websocket/frame/base/index.ts +19 -10
- package/src/codec/websocket/frame/text/index.ts +2 -2
- package/src/codec/websocket/protocol/index.ts +12 -0
- package/src/datagram/bootstrap/index.ts +1 -1
- package/src/datagram/transport/index.ts +30 -1
- package/src/handler/flush/index.ts +25 -5
- package/src/handler/idle/index.ts +120 -26
- package/src/handler/logging/index.ts +13 -1
- package/src/handler/metrics/index.ts +49 -16
- package/src/handler/offload/index.ts +7 -0
- package/src/handler/timeout/read/index.ts +1 -1
- package/src/handler/timeout/write/index.ts +104 -23
- package/src/handler/traffic/index.ts +251 -38
- package/src/index.ts +1 -0
- package/src/reliable/cipher/index.ts +49 -5
- package/src/reliable/cookie/index.ts +64 -2
- package/src/reliable/handler/index.ts +11 -15
- package/src/reliable/index.ts +7 -1
- package/src/reliable/lane/index.ts +277 -58
- package/src/reliable/peer/index.ts +125 -38
- package/src/serve/bootstrap/index.ts +82 -135
- package/src/serve/channel/index.ts +84 -2
- package/src/serve/gate/index.ts +215 -0
- package/src/serve/index.ts +6 -2
- package/src/serve/outbox/index.ts +63 -2
- package/src/serve/transport/index.ts +33 -3
- package/src/serve/writer/index.ts +4 -3
- package/src/shard/gateway/index.ts +102 -29
- package/src/shard/link/index.ts +15 -0
- package/src/worker/group/index.ts +231 -13
- package/src/worker/pool/index.ts +100 -19
- package/src/worker/service/index.ts +9 -1
|
@@ -62,6 +62,8 @@ export class WebSocketFrameEncoder extends MessageToByteEncoder<WebSocketFrame>
|
|
|
62
62
|
* @param offset first byte to write in the target
|
|
63
63
|
* @param key any array with the four key bytes in it
|
|
64
64
|
* @param start position of the key within that array
|
|
65
|
+
* @param from first payload byte in the source
|
|
66
|
+
* @param length payload bytes to mask
|
|
65
67
|
*/
|
|
66
68
|
private static xor(
|
|
67
69
|
source: Uint8Array,
|
|
@@ -69,27 +71,28 @@ export class WebSocketFrameEncoder extends MessageToByteEncoder<WebSocketFrame>
|
|
|
69
71
|
offset: number,
|
|
70
72
|
key: Uint8Array,
|
|
71
73
|
start: number,
|
|
74
|
+
from = 0,
|
|
75
|
+
length: number = source.length,
|
|
72
76
|
): void {
|
|
73
77
|
const first = key[start] ?? 0
|
|
74
78
|
const second = key[start + 1] ?? 0
|
|
75
79
|
const third = key[start + 2] ?? 0
|
|
76
80
|
const fourth = key[start + 3] ?? 0
|
|
77
|
-
const length = source.length
|
|
78
81
|
const whole = length - (length & 3)
|
|
79
82
|
|
|
80
83
|
let index = 0
|
|
81
84
|
for (; index < whole; index += 4) {
|
|
82
|
-
target[offset + index] = (source[index] ?? 0) ^ first
|
|
83
|
-
target[offset + index + 1] = (source[index + 1] ?? 0) ^ second
|
|
84
|
-
target[offset + index + 2] = (source[index + 2] ?? 0) ^ third
|
|
85
|
-
target[offset + index + 3] = (source[index + 3] ?? 0) ^ fourth
|
|
85
|
+
target[offset + index] = (source[from + index] ?? 0) ^ first
|
|
86
|
+
target[offset + index + 1] = (source[from + index + 1] ?? 0) ^ second
|
|
87
|
+
target[offset + index + 2] = (source[from + index + 2] ?? 0) ^ third
|
|
88
|
+
target[offset + index + 3] = (source[from + index + 3] ?? 0) ^ fourth
|
|
86
89
|
}
|
|
87
90
|
|
|
88
91
|
for (; index < length; index++) {
|
|
89
92
|
const byte = index & 3
|
|
90
93
|
const part =
|
|
91
94
|
byte === 0 ? first : byte === 1 ? second : byte === 2 ? third : fourth
|
|
92
|
-
target[offset + index] = (source[index] ?? 0) ^ part
|
|
95
|
+
target[offset + index] = (source[from + index] ?? 0) ^ part
|
|
93
96
|
}
|
|
94
97
|
}
|
|
95
98
|
|
|
@@ -118,6 +121,87 @@ export class WebSocketFrameEncoder extends MessageToByteEncoder<WebSocketFrame>
|
|
|
118
121
|
return message instanceof WebSocketFrame
|
|
119
122
|
}
|
|
120
123
|
|
|
124
|
+
/**
|
|
125
|
+
* Writes a small frame in place
|
|
126
|
+
* @param context where the write is going
|
|
127
|
+
* @param message frame going out
|
|
128
|
+
*/
|
|
129
|
+
public override writeSync(
|
|
130
|
+
context: HandlerContext,
|
|
131
|
+
message: unknown,
|
|
132
|
+
): boolean {
|
|
133
|
+
if (!(message instanceof WebSocketFrame)) return false
|
|
134
|
+
if (!context.writesToSocket) return super.writeSync(context, message)
|
|
135
|
+
|
|
136
|
+
const content = message.content
|
|
137
|
+
const length = content.readableBytes
|
|
138
|
+
const control = message.isControl
|
|
139
|
+
if (length > 0xffff || (control && length > 125)) {
|
|
140
|
+
return super.writeSync(context, message)
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (control && !message.finalFragment) {
|
|
144
|
+
return super.writeSync(context, message)
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const extended = length < 126 ? 0 : 2
|
|
148
|
+
const header = 2 + extended + (this.maskPayload ? 4 : 0)
|
|
149
|
+
const channel = context.channel
|
|
150
|
+
const offset = channel.reserve(header + length)
|
|
151
|
+
if (offset < 0) return super.writeSync(context, message)
|
|
152
|
+
|
|
153
|
+
const target = channel.stagingBuffer
|
|
154
|
+
const mask = this.maskPayload ? 0x80 : 0
|
|
155
|
+
target[offset] = (message.finalFragment ? 0x80 : 0) | message.opcode
|
|
156
|
+
if (extended === 0) {
|
|
157
|
+
target[offset + 1] = mask | length
|
|
158
|
+
} else {
|
|
159
|
+
target[offset + 1] = mask | 126
|
|
160
|
+
target[offset + 2] = length >>> 8
|
|
161
|
+
target[offset + 3] = length & 0xff
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
this.stagePayload(content, target, offset + 2 + extended, length)
|
|
165
|
+
channel.commitStaged()
|
|
166
|
+
return true
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Copies the payload in
|
|
171
|
+
* @param content the frame's payload
|
|
172
|
+
* @param target the outbound buffer
|
|
173
|
+
* @param offset where the key or payload goes
|
|
174
|
+
* @param length payload bytes
|
|
175
|
+
*/
|
|
176
|
+
private stagePayload(
|
|
177
|
+
content: ByteBuf,
|
|
178
|
+
target: Uint8Array,
|
|
179
|
+
offset: number,
|
|
180
|
+
length: number,
|
|
181
|
+
): void {
|
|
182
|
+
const from = content.arrayOffset + content.readerIndex
|
|
183
|
+
if (!this.maskPayload) {
|
|
184
|
+
ByteBuf.copyBytes(content.array, from, target, offset, length)
|
|
185
|
+
return
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const start = WebSocketFrameEncoder.nextKey()
|
|
189
|
+
const entropy = WebSocketFrameEncoder.entropy
|
|
190
|
+
for (let index = 0; index < 4; index++) {
|
|
191
|
+
target[offset + index] = entropy[start + index] ?? 0
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
WebSocketFrameEncoder.xor(
|
|
195
|
+
content.array,
|
|
196
|
+
target,
|
|
197
|
+
offset + 4,
|
|
198
|
+
entropy,
|
|
199
|
+
start,
|
|
200
|
+
from,
|
|
201
|
+
length,
|
|
202
|
+
)
|
|
203
|
+
}
|
|
204
|
+
|
|
121
205
|
/**
|
|
122
206
|
* Writes the header, the mask key if any and the payload in one buffer
|
|
123
207
|
* @param context unused here
|
|
@@ -16,9 +16,14 @@ export abstract class WebSocketFrame {
|
|
|
16
16
|
public readonly finalFragment: boolean
|
|
17
17
|
|
|
18
18
|
/**
|
|
19
|
-
*
|
|
19
|
+
* Text the frame was built from, or null
|
|
20
|
+
*/
|
|
21
|
+
protected readonly source: string | null
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Payload bytes, or null
|
|
20
25
|
*/
|
|
21
|
-
|
|
26
|
+
private buffer: ByteBuf | null
|
|
22
27
|
|
|
23
28
|
/**
|
|
24
29
|
* Stores the payload and FIN flag
|
|
@@ -26,19 +31,23 @@ export abstract class WebSocketFrame {
|
|
|
26
31
|
* @param finalFragment the FIN bit
|
|
27
32
|
*/
|
|
28
33
|
protected constructor(content: FrameSource, finalFragment: boolean) {
|
|
29
|
-
this.content = WebSocketFrame.bufferOf(content)
|
|
30
34
|
this.finalFragment = finalFragment
|
|
35
|
+
|
|
36
|
+
if (typeof content === 'string') {
|
|
37
|
+
this.source = content
|
|
38
|
+
this.buffer = null
|
|
39
|
+
} else {
|
|
40
|
+
this.source = null
|
|
41
|
+
this.buffer = content instanceof ByteBuf ? content : ByteBuf.wrap(content)
|
|
42
|
+
}
|
|
31
43
|
}
|
|
32
44
|
|
|
33
45
|
/**
|
|
34
|
-
*
|
|
35
|
-
* @param source what to convert
|
|
46
|
+
* Payload bytes, never masked
|
|
36
47
|
*/
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
return ByteBuf.wrap(source)
|
|
48
|
+
public get content(): ByteBuf {
|
|
49
|
+
this.buffer ??= ByteBuf.fromString(this.source ?? '')
|
|
50
|
+
return this.buffer
|
|
42
51
|
}
|
|
43
52
|
|
|
44
53
|
/**
|
|
@@ -23,9 +23,9 @@ export class TextWebSocketFrame extends WebSocketFrame {
|
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
/**
|
|
26
|
-
*
|
|
26
|
+
* Text it came from, or the payload as UTF-8
|
|
27
27
|
*/
|
|
28
28
|
public text(): string {
|
|
29
|
-
return this.content.toString()
|
|
29
|
+
return this.source ?? this.content.toString()
|
|
30
30
|
}
|
|
31
31
|
}
|
|
@@ -66,6 +66,18 @@ export abstract class WebSocketProtocolHandler extends DuplexHandlerAdapter {
|
|
|
66
66
|
return this.sendClose(context, message)
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
+
/**
|
|
70
|
+
* Passes all but a close on without a promise
|
|
71
|
+
* @param context where writes go
|
|
72
|
+
* @param message anything heading out
|
|
73
|
+
*/
|
|
74
|
+
public writeSync(context: HandlerContext, message: unknown): boolean {
|
|
75
|
+
if (message instanceof CloseWebSocketFrame) return false
|
|
76
|
+
|
|
77
|
+
context.writeSync(message)
|
|
78
|
+
return true
|
|
79
|
+
}
|
|
80
|
+
|
|
69
81
|
/**
|
|
70
82
|
* Sends the close code a corrupt frame calls for and hangs up, then passes the error on
|
|
71
83
|
* @param context the handler's context
|
|
@@ -196,7 +196,36 @@ export class DatagramTransport implements Transport {
|
|
|
196
196
|
packets.push(bytes, recipient.port, recipient.hostname)
|
|
197
197
|
}
|
|
198
198
|
|
|
199
|
-
|
|
199
|
+
const socket = this.handle.socket
|
|
200
|
+
let sent = 0
|
|
201
|
+
while (sent < recipients.length) {
|
|
202
|
+
sent += DatagramTransport.burst(
|
|
203
|
+
socket,
|
|
204
|
+
sent === 0 ? packets : packets.slice(sent * 3),
|
|
205
|
+
)
|
|
206
|
+
if (sent >= recipients.length) break
|
|
207
|
+
if (!this.send(bytes, recipients[sent] ?? null)) break
|
|
208
|
+
sent += 1
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return sent
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Sends one batch, or 0
|
|
216
|
+
* @param socket sends from here
|
|
217
|
+
* @param packets payload, port and host triples
|
|
218
|
+
*/
|
|
219
|
+
private static burst(
|
|
220
|
+
socket: udp.Socket<'uint8array'>,
|
|
221
|
+
packets: (Uint8Array | number | string)[],
|
|
222
|
+
): number {
|
|
223
|
+
try {
|
|
224
|
+
return socket.sendMany(packets)
|
|
225
|
+
} catch (cause) {
|
|
226
|
+
if (DatagramTransport.isRefused(cause)) return 0
|
|
227
|
+
throw cause
|
|
228
|
+
}
|
|
200
229
|
}
|
|
201
230
|
|
|
202
231
|
/**
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { DuplexHandlerAdapter } from '../../channel/handler/duplex/index.ts'
|
|
2
2
|
import type {} from '../../channel/handler/index.ts'
|
|
3
3
|
import type { HandlerContext } from '../../channel/context/index.ts'
|
|
4
|
+
import { resolved } from '../../channel/resolved/index.ts'
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* Knobs for how aggressively flushes get batched
|
|
@@ -42,6 +43,16 @@ export class FlushConsolidationHandler extends DuplexHandlerAdapter {
|
|
|
42
43
|
*/
|
|
43
44
|
private reading: boolean
|
|
44
45
|
|
|
46
|
+
/**
|
|
47
|
+
* Reads still being handled
|
|
48
|
+
*/
|
|
49
|
+
private unfinished: number
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Counts a read back down
|
|
53
|
+
*/
|
|
54
|
+
private readonly finished: () => void
|
|
55
|
+
|
|
45
56
|
/**
|
|
46
57
|
* Stops us queuing more than one microtask flush at a time
|
|
47
58
|
*/
|
|
@@ -53,7 +64,7 @@ export class FlushConsolidationHandler extends DuplexHandlerAdapter {
|
|
|
53
64
|
private waiting: PromiseWithResolvers<undefined> | null
|
|
54
65
|
|
|
55
66
|
/**
|
|
56
|
-
*
|
|
67
|
+
* Batches flushes for one connection
|
|
57
68
|
* @param options batching settings, all optional
|
|
58
69
|
*/
|
|
59
70
|
public constructor(options: FlushConsolidationOptions = {}) {
|
|
@@ -68,12 +79,16 @@ export class FlushConsolidationHandler extends DuplexHandlerAdapter {
|
|
|
68
79
|
this.outsideReads = options.consolidateWhenNoReadInProgress ?? false
|
|
69
80
|
this.held = 0
|
|
70
81
|
this.reading = false
|
|
82
|
+
this.unfinished = 0
|
|
71
83
|
this.scheduled = false
|
|
72
84
|
this.waiting = null
|
|
85
|
+
this.finished = (): void => {
|
|
86
|
+
this.unfinished -= 1
|
|
87
|
+
}
|
|
73
88
|
}
|
|
74
89
|
|
|
75
90
|
/**
|
|
76
|
-
*
|
|
91
|
+
* Marks a read in progress
|
|
77
92
|
* @param context where the read carries on
|
|
78
93
|
* @param message whatever came in
|
|
79
94
|
*/
|
|
@@ -83,7 +98,12 @@ export class FlushConsolidationHandler extends DuplexHandlerAdapter {
|
|
|
83
98
|
): Promise<void> {
|
|
84
99
|
this.reading = true
|
|
85
100
|
|
|
86
|
-
|
|
101
|
+
const result = context.fireChannelRead(message)
|
|
102
|
+
if (result === resolved) return result
|
|
103
|
+
|
|
104
|
+
this.unfinished += 1
|
|
105
|
+
this.release(context)
|
|
106
|
+
return result.finally(this.finished)
|
|
87
107
|
}
|
|
88
108
|
|
|
89
109
|
/**
|
|
@@ -120,7 +140,7 @@ export class FlushConsolidationHandler extends DuplexHandlerAdapter {
|
|
|
120
140
|
* @param context where real flushes get sent
|
|
121
141
|
*/
|
|
122
142
|
public override flush(context: HandlerContext): Promise<void> {
|
|
123
|
-
if (this.reading) {
|
|
143
|
+
if (this.reading && this.unfinished === 0) {
|
|
124
144
|
this.held += 1
|
|
125
145
|
if (this.held < this.limit) return this.hold()
|
|
126
146
|
return this.forward(context)
|
|
@@ -166,7 +186,7 @@ export class FlushConsolidationHandler extends DuplexHandlerAdapter {
|
|
|
166
186
|
}
|
|
167
187
|
|
|
168
188
|
/**
|
|
169
|
-
*
|
|
189
|
+
* Passes the flush on
|
|
170
190
|
* @param context where the flush gets sent
|
|
171
191
|
*/
|
|
172
192
|
private release(context: HandlerContext): void {
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { DuplexHandlerAdapter } from '../../channel/handler/duplex/index.ts'
|
|
2
2
|
import type {} from '../../channel/handler/index.ts'
|
|
3
3
|
import type { HandlerContext } from '../../channel/context/index.ts'
|
|
4
|
+
import { resolved } from '../../channel/resolved/index.ts'
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* Reads only, writes only, or neither
|
|
@@ -17,7 +18,7 @@ export type IdleOptions = {
|
|
|
17
18
|
readonly readerIdle?: number
|
|
18
19
|
|
|
19
20
|
/**
|
|
20
|
-
*
|
|
21
|
+
* Write idle time before firing
|
|
21
22
|
*/
|
|
22
23
|
readonly writerIdle?: number
|
|
23
24
|
|
|
@@ -58,6 +59,11 @@ export class IdleStateEvent {
|
|
|
58
59
|
* @class
|
|
59
60
|
*/
|
|
60
61
|
export class IdleStateHandler extends DuplexHandlerAdapter {
|
|
62
|
+
/**
|
|
63
|
+
* Longest timeout a timer can hold, in ms
|
|
64
|
+
*/
|
|
65
|
+
public static readonly longest: number = 2 ** 31 - 1
|
|
66
|
+
|
|
61
67
|
/**
|
|
62
68
|
* Resolved option values, 0 means that one is off
|
|
63
69
|
*/
|
|
@@ -69,12 +75,32 @@ export class IdleStateHandler extends DuplexHandlerAdapter {
|
|
|
69
75
|
private readonly timers: Map<IdleState, Timer>
|
|
70
76
|
|
|
71
77
|
/**
|
|
72
|
-
*
|
|
78
|
+
* Stamps a finished write
|
|
79
|
+
*/
|
|
80
|
+
private readonly wrote: () => void
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Reader idle hasn't fired yet
|
|
84
|
+
*/
|
|
85
|
+
private readerFirst: boolean
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Writer idle hasn't fired yet
|
|
89
|
+
*/
|
|
90
|
+
private writerFirst: boolean
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* All idle hasn't fired yet
|
|
73
94
|
*/
|
|
74
|
-
private
|
|
95
|
+
private allFirst: boolean
|
|
75
96
|
|
|
76
97
|
/**
|
|
77
|
-
*
|
|
98
|
+
* Inside a read batch
|
|
99
|
+
*/
|
|
100
|
+
private reading: boolean
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* End of the last read batch
|
|
78
104
|
*/
|
|
79
105
|
private lastRead: number
|
|
80
106
|
|
|
@@ -83,13 +109,18 @@ export class IdleStateHandler extends DuplexHandlerAdapter {
|
|
|
83
109
|
*/
|
|
84
110
|
private lastWrite: number
|
|
85
111
|
|
|
112
|
+
/**
|
|
113
|
+
* Last write promise hooked, or null
|
|
114
|
+
*/
|
|
115
|
+
private tracked: Promise<void> | null
|
|
116
|
+
|
|
86
117
|
/**
|
|
87
118
|
* Guards against starting the timers twice
|
|
88
119
|
*/
|
|
89
120
|
private started: boolean
|
|
90
121
|
|
|
91
122
|
/**
|
|
92
|
-
*
|
|
123
|
+
* Watches one connection for idle time
|
|
93
124
|
* @param options which timeouts to watch
|
|
94
125
|
*/
|
|
95
126
|
public constructor(options: IdleOptions) {
|
|
@@ -104,23 +135,49 @@ export class IdleStateHandler extends DuplexHandlerAdapter {
|
|
|
104
135
|
['all', options.allIdle ?? 0],
|
|
105
136
|
])
|
|
106
137
|
this.timers = new Map<IdleState, Timer>()
|
|
107
|
-
this.
|
|
138
|
+
this.readerFirst = true
|
|
139
|
+
this.writerFirst = true
|
|
140
|
+
this.allFirst = true
|
|
141
|
+
this.reading = false
|
|
108
142
|
this.lastRead = 0
|
|
109
143
|
this.lastWrite = 0
|
|
144
|
+
this.tracked = null
|
|
110
145
|
this.started = false
|
|
146
|
+
this.wrote = (): void => {
|
|
147
|
+
this.lastWrite = performance.now()
|
|
148
|
+
this.writerFirst = true
|
|
149
|
+
this.allFirst = true
|
|
150
|
+
}
|
|
111
151
|
}
|
|
112
152
|
|
|
113
153
|
/**
|
|
114
|
-
*
|
|
154
|
+
* Ignores a failed write
|
|
155
|
+
*/
|
|
156
|
+
private static readonly ignore: () => undefined = (): undefined => undefined
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Error text for a timeout no timer can hold, or null
|
|
115
160
|
* @param options timeouts to check
|
|
116
161
|
*/
|
|
117
162
|
private static refusal(options: IdleOptions): string | null {
|
|
118
|
-
const values = [
|
|
163
|
+
const values = [
|
|
164
|
+
options.readerIdle ?? 0,
|
|
165
|
+
options.writerIdle ?? 0,
|
|
166
|
+
options.allIdle ?? 0,
|
|
167
|
+
]
|
|
119
168
|
|
|
120
|
-
if (values.some((value
|
|
169
|
+
if (values.some((value: number): boolean => value < 0)) {
|
|
121
170
|
return 'Timeout is negative'
|
|
122
171
|
}
|
|
123
172
|
|
|
173
|
+
if (
|
|
174
|
+
values.some(
|
|
175
|
+
(value: number): boolean => !(value <= IdleStateHandler.longest),
|
|
176
|
+
)
|
|
177
|
+
) {
|
|
178
|
+
return 'Timeout is invalid'
|
|
179
|
+
}
|
|
180
|
+
|
|
124
181
|
return null
|
|
125
182
|
}
|
|
126
183
|
|
|
@@ -160,19 +217,30 @@ export class IdleStateHandler extends DuplexHandlerAdapter {
|
|
|
160
217
|
}
|
|
161
218
|
|
|
162
219
|
/**
|
|
163
|
-
*
|
|
220
|
+
* Marks a read as started
|
|
164
221
|
* @param context where the read carries on
|
|
165
222
|
* @param message passed along untouched
|
|
166
223
|
*/
|
|
167
|
-
public override
|
|
224
|
+
public override channelRead(
|
|
168
225
|
context: HandlerContext,
|
|
169
226
|
message: unknown,
|
|
170
227
|
): Promise<void> {
|
|
228
|
+
this.reading = true
|
|
229
|
+
this.readerFirst = true
|
|
230
|
+
this.allFirst = true
|
|
231
|
+
|
|
232
|
+
return context.fireChannelRead(message)
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Restarts the reader and all clocks
|
|
237
|
+
* @param context where the event carries on
|
|
238
|
+
*/
|
|
239
|
+
public override channelReadComplete(context: HandlerContext): Promise<void> {
|
|
240
|
+
this.reading = false
|
|
171
241
|
this.lastRead = performance.now()
|
|
172
|
-
this.firsts.set('reader', true)
|
|
173
|
-
this.firsts.set('all', true)
|
|
174
242
|
|
|
175
|
-
|
|
243
|
+
return context.fireChannelReadComplete()
|
|
176
244
|
}
|
|
177
245
|
|
|
178
246
|
/**
|
|
@@ -185,15 +253,10 @@ export class IdleStateHandler extends DuplexHandlerAdapter {
|
|
|
185
253
|
message: unknown,
|
|
186
254
|
): Promise<void> {
|
|
187
255
|
const promise = context.write(message)
|
|
256
|
+
if (promise === resolved || promise === this.tracked) return promise
|
|
188
257
|
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
this.lastWrite = performance.now()
|
|
192
|
-
this.firsts.set('writer', true)
|
|
193
|
-
this.firsts.set('all', true)
|
|
194
|
-
},
|
|
195
|
-
(): undefined => undefined,
|
|
196
|
-
)
|
|
258
|
+
this.tracked = promise
|
|
259
|
+
void promise.then(this.wrote, IdleStateHandler.ignore)
|
|
197
260
|
return promise
|
|
198
261
|
}
|
|
199
262
|
|
|
@@ -220,9 +283,12 @@ export class IdleStateHandler extends DuplexHandlerAdapter {
|
|
|
220
283
|
const now = performance.now()
|
|
221
284
|
this.lastRead = now
|
|
222
285
|
this.lastWrite = now
|
|
286
|
+
this.reading = false
|
|
287
|
+
this.readerFirst = true
|
|
288
|
+
this.writerFirst = true
|
|
289
|
+
this.allFirst = true
|
|
223
290
|
for (const [state, timeout] of this.timeouts) {
|
|
224
291
|
if (timeout === 0) continue
|
|
225
|
-
this.firsts.set(state, true)
|
|
226
292
|
this.schedule(context, state, timeout)
|
|
227
293
|
}
|
|
228
294
|
}
|
|
@@ -270,6 +336,11 @@ export class IdleStateHandler extends DuplexHandlerAdapter {
|
|
|
270
336
|
if (!this.started) return
|
|
271
337
|
|
|
272
338
|
const timeout = this.timeouts.get(state) ?? 0
|
|
339
|
+
if (this.reading && state !== 'writer') {
|
|
340
|
+
this.schedule(context, state, timeout)
|
|
341
|
+
return
|
|
342
|
+
}
|
|
343
|
+
|
|
273
344
|
const remaining = timeout - (performance.now() - this.lastActivity(state))
|
|
274
345
|
if (remaining > 0) {
|
|
275
346
|
this.schedule(context, state, remaining)
|
|
@@ -277,9 +348,32 @@ export class IdleStateHandler extends DuplexHandlerAdapter {
|
|
|
277
348
|
}
|
|
278
349
|
|
|
279
350
|
this.schedule(context, state, timeout)
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
351
|
+
await this.channelIdle(
|
|
352
|
+
context,
|
|
353
|
+
new IdleStateEvent(state, this.spend(state)),
|
|
354
|
+
)
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* Claims the first event since activity
|
|
359
|
+
* @param state which timeout fired
|
|
360
|
+
*/
|
|
361
|
+
private spend(state: IdleState): boolean {
|
|
362
|
+
if (state === 'reader') {
|
|
363
|
+
const first = this.readerFirst
|
|
364
|
+
this.readerFirst = false
|
|
365
|
+
return first
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
if (state === 'writer') {
|
|
369
|
+
const first = this.writerFirst
|
|
370
|
+
this.writerFirst = false
|
|
371
|
+
return first
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
const first = this.allFirst
|
|
375
|
+
this.allFirst = false
|
|
376
|
+
return first
|
|
283
377
|
}
|
|
284
378
|
|
|
285
379
|
/**
|
|
@@ -47,12 +47,24 @@ export class LoggingHandler extends DuplexHandlerAdapter {
|
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
if (typeof message === 'object' && message !== null) {
|
|
50
|
-
return message
|
|
50
|
+
return LoggingHandler.className(message)
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
return typeof message
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
+
/**
|
|
57
|
+
* Class name of a message
|
|
58
|
+
* @param message whatever's passing through
|
|
59
|
+
*/
|
|
60
|
+
private static className(message: object): string {
|
|
61
|
+
const prototype: unknown = Object.getPrototypeOf(message)
|
|
62
|
+
if (typeof prototype !== 'object' || prototype === null) return 'Object'
|
|
63
|
+
|
|
64
|
+
const type: unknown = Reflect.get(prototype, 'constructor')
|
|
65
|
+
return typeof type === 'function' && type.name !== '' ? type.name : 'Object'
|
|
66
|
+
}
|
|
67
|
+
|
|
56
68
|
/**
|
|
57
69
|
* Logs it, then carries on
|
|
58
70
|
* @param context where the event carries on
|