@netifyjs/protocol 1.0.0-alpha.3 → 1.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (175) hide show
  1. package/package.json +2 -2
  2. package/src/codec/index.ts +51 -221
  3. package/src/dispatch/dispatcher/index.ts +89 -47
  4. package/src/dispatch/session/index.ts +6 -24
  5. package/src/index.ts +2 -4
  6. package/src/photon/balancing/codec/index.ts +27 -53
  7. package/src/photon/balancing/format/authenticate/request/index.ts +52 -6
  8. package/src/photon/balancing/format/friends/request/index.ts +21 -5
  9. package/src/photon/balancing/format/game/create/request/index.ts +45 -8
  10. package/src/photon/balancing/format/game/debug/request/index.ts +19 -4
  11. package/src/photon/balancing/format/game/join/request/index.ts +65 -13
  12. package/src/photon/balancing/format/game/list/request/index.ts +29 -3
  13. package/src/photon/balancing/format/game/random/request/index.ts +58 -10
  14. package/src/photon/balancing/format/groups/request/index.ts +19 -3
  15. package/src/photon/balancing/format/index.ts +7 -0
  16. package/src/photon/balancing/format/leave/request/index.ts +19 -3
  17. package/src/photon/balancing/format/lobby/join/request/index.ts +23 -3
  18. package/src/photon/balancing/format/lobby/stats/request/index.ts +19 -3
  19. package/src/photon/balancing/format/once/request/index.ts +31 -8
  20. package/src/photon/balancing/format/properties/get/request/index.ts +31 -4
  21. package/src/photon/balancing/format/properties/set/request/index.ts +36 -7
  22. package/src/photon/balancing/format/raise/request/index.ts +76 -10
  23. package/src/photon/balancing/format/reader/index.ts +75 -6
  24. package/src/photon/balancing/format/regions/request/index.ts +16 -1
  25. package/src/photon/balancing/format/rpc/request/index.ts +26 -3
  26. package/src/photon/balancing/format/settings/request/index.ts +15 -2
  27. package/src/photon/balancing/format/shared/credentials/index.ts +77 -33
  28. package/src/photon/balancing/format/shared/lobby/index.ts +41 -5
  29. package/src/photon/balancing/format/shared/options/index.ts +56 -24
  30. package/src/photon/balancing/format/table/index.ts +399 -0
  31. package/src/photon/balancing/index.ts +11 -1
  32. package/src/photon/balancing/travel/index.ts +15 -12
  33. package/src/photon/client/connection/index.ts +6 -6
  34. package/src/photon/cluster/back/index.ts +32 -45
  35. package/src/photon/cluster/dresser/index.ts +7 -8
  36. package/src/photon/cluster/front/index.ts +25 -49
  37. package/src/photon/cluster/grid/index.ts +1 -1
  38. package/src/photon/cluster/mover/index.ts +12 -12
  39. package/src/photon/cluster/press/index.ts +2 -18
  40. package/src/photon/cluster/route/game/index.ts +9 -11
  41. package/src/photon/cluster/route/master/index.ts +15 -34
  42. package/src/photon/cluster/route/name/index.ts +4 -21
  43. package/src/photon/cluster/seat/game/index.ts +6 -26
  44. package/src/photon/cluster/seat/master/index.ts +1 -1
  45. package/src/photon/cluster/sessions/index.ts +5 -6
  46. package/src/photon/cluster/spread/index.ts +8 -147
  47. package/src/photon/enet/client/index.ts +1 -8
  48. package/src/photon/enet/gateway/index.ts +5 -15
  49. package/src/photon/enet/index.ts +1 -1
  50. package/src/photon/enet/link/index.ts +34 -15
  51. package/src/photon/enet/outbound/index.ts +279 -0
  52. package/src/photon/enet/peer/index.ts +59 -0
  53. package/src/photon/enet/transport/index.ts +8 -24
  54. package/src/photon/game/admission/index.ts +353 -0
  55. package/src/photon/game/bag/index.ts +13 -0
  56. package/src/photon/game/broadcast/index.ts +79 -0
  57. package/src/photon/game/editor/index.ts +446 -0
  58. package/src/photon/game/gate/index.ts +9 -8
  59. package/src/photon/game/handler/index.ts +30 -3
  60. package/src/photon/game/history/index.ts +234 -0
  61. package/src/photon/game/hooked/index.ts +16 -15
  62. package/src/photon/game/index.ts +12 -1
  63. package/src/photon/game/lifecycle/index.ts +193 -0
  64. package/src/photon/game/peer/index.ts +1 -10
  65. package/src/photon/game/properties/index.ts +342 -0
  66. package/src/photon/game/room/index.ts +435 -1793
  67. package/src/photon/game/rooms/index.ts +2 -1
  68. package/src/photon/game/seats/index.ts +237 -0
  69. package/src/photon/game/server/index.ts +12 -2
  70. package/src/photon/game/services/index.ts +41 -0
  71. package/src/photon/game/settings/index.ts +22 -0
  72. package/src/photon/game/snapshot/index.ts +133 -0
  73. package/src/photon/game/throttle/index.ts +60 -0
  74. package/src/photon/game/touch/index.ts +2 -5
  75. package/src/photon/game/venue/index.ts +31 -0
  76. package/src/photon/master/application/index.ts +7 -0
  77. package/src/photon/master/gate/index.ts +12 -8
  78. package/src/photon/master/handler/index.ts +36 -748
  79. package/src/photon/master/index.ts +5 -0
  80. package/src/photon/master/list/sql/index.ts +37 -3
  81. package/src/photon/master/listing/index.ts +133 -0
  82. package/src/photon/master/lobby/index.ts +9 -0
  83. package/src/photon/master/login/index.ts +424 -0
  84. package/src/photon/master/matchmaking/index.ts +282 -0
  85. package/src/photon/master/query/index.ts +45 -0
  86. package/src/photon/master/redirect/index.ts +8 -7
  87. package/src/photon/master/services/index.ts +35 -0
  88. package/src/photon/master/settings/index.ts +44 -0
  89. package/src/photon/master/sql/cost/index.ts +263 -0
  90. package/src/photon/master/sql/index.ts +1 -0
  91. package/src/photon/message/client/index.ts +14 -11
  92. package/src/photon/message/handler/index.ts +22 -64
  93. package/src/photon/message/pipeline/index.ts +12 -12
  94. package/src/photon/message/server/index.ts +87 -43
  95. package/src/photon/message/watch/index.ts +14 -37
  96. package/src/photon/server/deferral/index.ts +57 -0
  97. package/src/photon/server/host/index.ts +32 -10
  98. package/src/photon/server/index.ts +1 -0
  99. package/src/photon/tcp/client/index.ts +1 -1
  100. package/src/photon/tcp/codec/index.ts +13 -42
  101. package/src/photon/tcp/server/index.ts +4 -4
  102. package/src/photon/websocket/bridge/index.ts +16 -20
  103. package/src/photon/websocket/message/index.ts +27 -39
  104. package/src/photon/websocket/path/index.ts +2 -5
  105. package/src/scale/fanout/index.ts +6 -6
  106. package/src/scale/index.ts +2 -4
  107. package/src/scale/outbound/index.ts +192 -0
  108. package/src/scale/relay/index.ts +20 -31
  109. package/src/scale/sink/index.ts +6 -11
  110. package/src/scale/transport/index.ts +27 -44
  111. package/src/scale/tunnel/index.ts +37 -91
  112. package/src/smartfox/common/bluebox/gateway/index.ts +401 -0
  113. package/src/smartfox/common/bluebox/index.ts +4 -0
  114. package/src/smartfox/{x2 → common}/bluebox/transport/index.ts +36 -23
  115. package/src/smartfox/common/index.ts +5 -0
  116. package/src/smartfox/common/strikes/index.ts +74 -0
  117. package/src/smartfox/pro/bluebox/index.ts +176 -4
  118. package/src/smartfox/pro/codec/index.ts +8 -7
  119. package/src/smartfox/pro/escalation/index.ts +28 -20
  120. package/src/smartfox/pro/flood/index.ts +2 -2
  121. package/src/smartfox/pro/format/extension/raw/index.ts +12 -2
  122. package/src/smartfox/pro/handler/chat/index.ts +11 -8
  123. package/src/smartfox/pro/handler/login/index.ts +2 -25
  124. package/src/smartfox/pro/handler/shared/index.ts +4 -3
  125. package/src/smartfox/pro/index.ts +11 -5
  126. package/src/smartfox/pro/login/gate/index.ts +166 -0
  127. package/src/smartfox/pro/login/index.ts +1 -0
  128. package/src/smartfox/pro/moderation/index.ts +220 -0
  129. package/src/smartfox/pro/options/index.ts +5 -5
  130. package/src/smartfox/pro/protocol/index.ts +62 -8
  131. package/src/smartfox/pro/web/index.ts +33 -26
  132. package/src/smartfox/pro/zone/index.ts +59 -282
  133. package/src/smartfox/pro/zones/index.ts +3 -3
  134. package/src/smartfox/x2/bluebox/index.ts +149 -4
  135. package/src/smartfox/x2/chat/index.ts +9 -19
  136. package/src/smartfox/x2/codec/index.ts +31 -39
  137. package/src/smartfox/x2/config/index.ts +6 -1
  138. package/src/smartfox/x2/data/array/index.ts +96 -60
  139. package/src/smartfox/x2/data/object/index.ts +144 -82
  140. package/src/smartfox/x2/data/serializer/index.ts +41 -23
  141. package/src/smartfox/x2/flood/gate/index.ts +19 -13
  142. package/src/smartfox/x2/format/buddy/init/response/index.ts +4 -3
  143. package/src/smartfox/x2/format/find/rooms/response/index.ts +4 -3
  144. package/src/smartfox/x2/format/find/users/response/index.ts +4 -3
  145. package/src/smartfox/x2/format/group/subscribe/response/index.ts +4 -3
  146. package/src/smartfox/x2/format/login/response/index.ts +4 -3
  147. package/src/smartfox/x2/format/mmo/position/response/index.ts +8 -6
  148. package/src/smartfox/x2/format/room/join/response/index.ts +4 -3
  149. package/src/smartfox/x2/games/index.ts +35 -8
  150. package/src/smartfox/x2/hall/index.ts +0 -6
  151. package/src/smartfox/x2/handler/session/index.ts +1 -1
  152. package/src/smartfox/x2/index.ts +8 -3
  153. package/src/smartfox/x2/member/index.ts +10 -3
  154. package/src/smartfox/x2/packet/login/response/index.ts +12 -0
  155. package/src/smartfox/x2/protocol/index.ts +11 -1
  156. package/src/smartfox/x2/rooms/index.ts +1 -2
  157. package/src/smartfox/x2/sessions/index.ts +6 -0
  158. package/src/smartfox/x2/udp/index.ts +61 -24
  159. package/src/smartfox/x2/web/index.ts +35 -17
  160. package/src/smartfox/x2/websocket/index.ts +18 -36
  161. package/src/smartfox/x2/zone/index.ts +77 -28
  162. package/src/smartfox/x2/zones/index.ts +17 -5
  163. package/src/photon/enet/handler/index.ts +0 -65
  164. package/src/scale/hosted/index.ts +0 -68
  165. package/src/scale/outlet/index.ts +0 -121
  166. package/src/smartfox/pro/bluebox/gateway/index.ts +0 -310
  167. package/src/smartfox/pro/bluebox/session/index.ts +0 -63
  168. package/src/smartfox/pro/bluebox/transport/index.ts +0 -104
  169. package/src/smartfox/x2/bluebox/gateway/index.ts +0 -315
  170. /package/src/smartfox/{x2 → common}/bluebox/session/index.ts +0 -0
  171. /package/src/smartfox/{pro → common}/counters/index.ts +0 -0
  172. /package/src/smartfox/{pro → common}/counters/memory/index.ts +0 -0
  173. /package/src/smartfox/{pro → common}/counters/offender/index.ts +0 -0
  174. /package/src/smartfox/{pro → common}/counters/store/index.ts +0 -0
  175. /package/src/smartfox/{pro → common}/sequence/index.ts +0 -0
@@ -207,15 +207,13 @@ export class WebSocketBridge extends InboundHandlerAdapter {
207
207
  * @param context the connection
208
208
  * @param message a request, or anything else
209
209
  */
210
- public override channelRead(
211
- context: HandlerContext,
212
- message: unknown,
213
- ): void | Promise<void> {
210
+ public override channelRead(context: HandlerContext, message: unknown): void {
214
211
  if (!(message instanceof FullHttpRequest)) {
215
- return context.fireChannelRead(message)
212
+ context.fireChannelRead(message)
213
+ return
216
214
  }
217
215
 
218
- return WebSocketBridge.refuse(context)
216
+ WebSocketBridge.refuse(context)
219
217
  }
220
218
 
221
219
  /**
@@ -223,29 +221,27 @@ export class WebSocketBridge extends InboundHandlerAdapter {
223
221
  * @param context the connection
224
222
  * @param event whatever happened
225
223
  */
226
- public override async userEventTriggered(
224
+ public override userEventTriggered(
227
225
  context: HandlerContext,
228
226
  event: unknown,
229
- ): Promise<void> {
230
- if (event instanceof WebSocketHandshakeComplete) {
231
- await this.open(context, event)
232
- }
227
+ ): void {
228
+ if (event instanceof WebSocketHandshakeComplete) this.open(context, event)
233
229
 
234
- await context.fireUserEventTriggered(event)
230
+ context.fireUserEventTriggered(event)
235
231
  }
236
232
 
237
233
  /**
238
234
  * Answers a plain request and hangs up
239
235
  * @param context the connection
240
236
  */
241
- private static async refuse(context: HandlerContext): Promise<void> {
237
+ private static refuse(context: HandlerContext): void {
242
238
  const status = HttpResponseStatus.UPGRADE_REQUIRED
243
239
  const response = new FullHttpResponse(status, status.reasonPhrase)
244
240
  response.setContentLength(response.content.readableBytes)
245
241
  response.headers.set('Connection', 'close').set('Upgrade', 'websocket')
246
242
 
247
- await context.writeAndFlush(response)
248
- await context.close()
243
+ context.writeAndFlush(response)
244
+ void context.close()
249
245
  }
250
246
 
251
247
  /**
@@ -253,10 +249,10 @@ export class WebSocketBridge extends InboundHandlerAdapter {
253
249
  * @param context the connection
254
250
  * @param event the finished handshake
255
251
  */
256
- private async open(
252
+ private open(
257
253
  context: HandlerContext,
258
254
  event: WebSocketHandshakeComplete,
259
- ): Promise<void> {
255
+ ): void {
260
256
  const channel = context.channel
261
257
  const negotiation = this.negotiate(channel, event)
262
258
  negotiation.attach(channel)
@@ -268,15 +264,15 @@ export class WebSocketBridge extends InboundHandlerAdapter {
268
264
  ? new TextBridge(this.limit)
269
265
  : new BinaryBridge(this.limit),
270
266
  )
271
- await this.setup(channel)
267
+ this.setup(channel)
272
268
 
273
269
  if (negotiation.session !== null) {
274
- await context.writeAndFlush(
270
+ context.writeAndFlush(
275
271
  new TextWebSocketFrame(new TextFraming().encode(negotiation.session)),
276
272
  )
277
273
  }
278
274
 
279
- await context.fireChannelActive()
275
+ context.fireChannelActive()
280
276
  }
281
277
 
282
278
  /**
@@ -50,24 +50,23 @@ export abstract class MessageBridge extends DuplexHandlerAdapter {
50
50
  * @param context the connection
51
51
  * @param message a frame, or anything else
52
52
  */
53
- public override channelRead(
54
- context: HandlerContext,
55
- message: unknown,
56
- ): void | Promise<void> {
53
+ public override channelRead(context: HandlerContext, message: unknown): void {
57
54
  if (!(message instanceof WebSocketFrame) || !this.takes(message)) {
58
- return context.fireChannelRead(message)
55
+ context.fireChannelRead(message)
56
+ return
59
57
  }
60
58
 
61
59
  let whole: Uint8Array | null
62
60
  try {
63
61
  whole = this.reassembly.add(message)
64
62
  } catch (cause) {
65
- void context.writeAndFlush(
63
+ context.writeAndFlush(
66
64
  new CloseWebSocketFrame(WebSocketCloseStatus.MESSAGE_TOO_BIG.code),
67
65
  )
68
- return context.fireExceptionCaught(
66
+ context.fireExceptionCaught(
69
67
  new DecoderError('Message is too large', { cause }),
70
68
  )
69
+ return
71
70
  }
72
71
 
73
72
  if (whole === null) return
@@ -76,12 +75,13 @@ export abstract class MessageBridge extends DuplexHandlerAdapter {
76
75
  try {
77
76
  decoded = this.decode(whole)
78
77
  } catch (cause) {
79
- return context.fireExceptionCaught(
78
+ context.fireExceptionCaught(
80
79
  new DecoderError('Decoding failed', { cause }),
81
80
  )
81
+ return
82
82
  }
83
83
 
84
- return MessageBridge.deliver(context, decoded)
84
+ MessageBridge.deliver(context, decoded, 0)
85
85
  }
86
86
 
87
87
  /**
@@ -89,27 +89,7 @@ export abstract class MessageBridge extends DuplexHandlerAdapter {
89
89
  * @param context the connection
90
90
  * @param message anything heading out
91
91
  */
92
- public override write(
93
- context: HandlerContext,
94
- message: unknown,
95
- ): Promise<void> {
96
- let frame: WebSocketFrame | null
97
- try {
98
- frame = this.encode(message)
99
- } catch (cause) {
100
- void context.close()
101
- return Promise.reject(new EncoderError('Encoding failed', { cause }))
102
- }
103
-
104
- return context.write(frame ?? message)
105
- }
106
-
107
- /**
108
- * Sends without a promise, false for other messages
109
- * @param context the connection
110
- * @param message anything heading out
111
- */
112
- public writeSync(context: HandlerContext, message: unknown): boolean {
92
+ public override write(context: HandlerContext, message: unknown): void {
113
93
  let frame: WebSocketFrame | null
114
94
  try {
115
95
  frame = this.encode(message)
@@ -118,10 +98,7 @@ export abstract class MessageBridge extends DuplexHandlerAdapter {
118
98
  throw new EncoderError('Encoding failed', { cause })
119
99
  }
120
100
 
121
- if (frame === null) return false
122
- context.writeSync(frame)
123
-
124
- return true
101
+ context.write(frame ?? message)
125
102
  }
126
103
 
127
104
  /**
@@ -133,16 +110,27 @@ export abstract class MessageBridge extends DuplexHandlerAdapter {
133
110
  }
134
111
 
135
112
  /**
136
- * Hands messages on one after another
113
+ * Hands messages on in order, stopping while reads are paused
137
114
  * @param context the connection
138
115
  * @param decoded what one WebSocket message held
116
+ * @param from the first one still to go
139
117
  */
140
- private static async deliver(
118
+ private static deliver(
141
119
  context: HandlerContext,
142
120
  decoded: readonly unknown[],
143
- ): Promise<void> {
144
- for (const message of decoded) {
145
- await context.fireChannelRead(message)
121
+ from: number,
122
+ ): void {
123
+ const channel = context.channel
124
+
125
+ for (let index = from; index < decoded.length; index++) {
126
+ if (index > from && channel.isReadPaused && channel.isActive) {
127
+ channel.deferRead((): void => {
128
+ MessageBridge.deliver(context, decoded, index)
129
+ })
130
+ return
131
+ }
132
+
133
+ context.fireChannelRead(decoded[index])
146
134
  }
147
135
  }
148
136
 
@@ -35,10 +35,7 @@ export class UpgradePath extends InboundHandlerAdapter {
35
35
  * @param context the connection
36
36
  * @param message a request, or anything else
37
37
  */
38
- public override channelRead(
39
- context: HandlerContext,
40
- message: unknown,
41
- ): void | Promise<void> {
38
+ public override channelRead(context: HandlerContext, message: unknown): void {
42
39
  if (
43
40
  message instanceof FullHttpRequest &&
44
41
  message.headers.hasToken('upgrade', 'websocket')
@@ -47,6 +44,6 @@ export class UpgradePath extends InboundHandlerAdapter {
47
44
  message.uri = UpgradePath.path
48
45
  }
49
46
 
50
- return context.fireChannelRead(message)
47
+ context.fireChannelRead(message)
51
48
  }
52
49
  }
@@ -280,7 +280,7 @@ export class Fanout<S = unknown> {
280
280
  const shared: Channel[] = []
281
281
 
282
282
  for (const channel of channels) {
283
- if (bytes === null || this.own(channel)) channel.writeAndFlushSync(packet)
283
+ if (bytes === null || this.own(channel)) channel.writeAndFlush(packet)
284
284
  else shared.push(channel)
285
285
  }
286
286
 
@@ -320,13 +320,13 @@ export class Fanout<S = unknown> {
320
320
  const guest = this.tunnel.hostOf(channel)
321
321
  const reach = this.portable.reach(channel)
322
322
  if (reach === 'packet' || guest?.reach === 'packet') {
323
- channel.writeAndFlushSync(packet)
323
+ channel.writeAndFlush(packet)
324
324
  continue
325
325
  }
326
326
 
327
327
  if (guest === null) {
328
- if (reach === 'socket') channel.writeAndFlushEncodedSync(bytes)
329
- else channel.writeAndFlushSync(ByteBuf.wrap(bytes))
328
+ if (reach === 'socket') channel.send(bytes)
329
+ else channel.writeAndFlush(ByteBuf.wrap(bytes))
330
330
 
331
331
  continue
332
332
  }
@@ -437,9 +437,9 @@ export class Fanout<S = unknown> {
437
437
  const guest = this.tunnel.hostOf(channel)
438
438
  if (guest === null) {
439
439
  if (this.portable.reach(channel) === 'socket') {
440
- channel.writeAndFlushEncodedSync(bytes)
440
+ channel.send(bytes)
441
441
  } else {
442
- channel.writeAndFlushSync(ByteBuf.wrap(bytes))
442
+ channel.writeAndFlush(ByteBuf.wrap(bytes))
443
443
  }
444
444
 
445
445
  continue
@@ -21,12 +21,11 @@ export { Fanout } from './fanout/index.ts'
21
21
  export type { FanoutOptions } from './fanout/index.ts'
22
22
  export { Handoff } from './handoff/index.ts'
23
23
  export type { HandoffReturn } from './handoff/index.ts'
24
- export { HostedChannel } from './hosted/index.ts'
25
24
  export { Layout } from './layout/index.ts'
26
25
  export { Mesh } from './mesh/index.ts'
27
26
  export type { MeshOptions } from './mesh/index.ts'
28
- export { TunnelOutlet } from './outlet/index.ts'
29
- export type { OutletSink } from './outlet/index.ts'
27
+ export { TunnelOutbound } from './outbound/index.ts'
28
+ export type { TunnelSink } from './outbound/index.ts'
30
29
  export { Placement } from './placement/index.ts'
31
30
  export type { PlacementRule } from './placement/index.ts'
32
31
  export type { Portable, Reach } from './portable/index.ts'
@@ -36,6 +35,5 @@ export { SinkTransport } from './sink/index.ts'
36
35
  export { Switchboard } from './switchboard/index.ts'
37
36
  export type { SwitchboardLocal } from './switchboard/index.ts'
38
37
  export { TunnelTransport } from './transport/index.ts'
39
- export type { TransportEvents } from './transport/index.ts'
40
38
  export { Tunnel } from './tunnel/index.ts'
41
39
  export type { Hosted, TunnelOptions } from './tunnel/index.ts'
@@ -0,0 +1,192 @@
1
+ import { ByteBuf, type Outbound, type OutboundFactory } from '@netifyjs/netify'
2
+
3
+ /**
4
+ * Takes each frame a moved session writes
5
+ * @param source holds the frame, borrowed for the call
6
+ * @param start where it begins
7
+ * @param size how many bytes it takes
8
+ */
9
+ export type TunnelSink = (
10
+ source: Uint8Array,
11
+ start: number,
12
+ size: number,
13
+ ) => void
14
+
15
+ /**
16
+ * Outgoing side of a moved session, handing each frame to the shard it came from
17
+ * @class
18
+ */
19
+ export class TunnelOutbound implements Outbound {
20
+ /**
21
+ * Room encoders write into, shared by every moved session
22
+ */
23
+ private static scratch: Uint8Array = new Uint8Array(4096)
24
+
25
+ /**
26
+ * Where frames go
27
+ */
28
+ private readonly sink: TunnelSink
29
+
30
+ /**
31
+ * Bytes the last reserve asked for
32
+ */
33
+ private reserved: number
34
+
35
+ /**
36
+ * Bytes handed on so far
37
+ */
38
+ private written: number
39
+
40
+ /**
41
+ * Error it stopped with, or null
42
+ */
43
+ private failure: Error | null
44
+
45
+ /**
46
+ * @param sink where frames go
47
+ */
48
+ public constructor(sink: TunnelSink) {
49
+ this.sink = sink
50
+ this.reserved = 0
51
+ this.written = 0
52
+ this.failure = null
53
+ }
54
+
55
+ /**
56
+ * Builds the outbound side for one moved session
57
+ * @param sink where its frames go
58
+ */
59
+ public static factory(sink: TunnelSink): OutboundFactory {
60
+ return (): Outbound => new TunnelOutbound(sink)
61
+ }
62
+
63
+ /**
64
+ * Always writable, the shard batch takes everything
65
+ */
66
+ public readonly isWritable: boolean = true
67
+
68
+ /**
69
+ * Nothing ever waits here
70
+ */
71
+ public readonly pendingBytes: number = 0
72
+
73
+ /**
74
+ * Bytes handed on so far
75
+ */
76
+ public get writtenBytes(): number {
77
+ return this.written
78
+ }
79
+
80
+ /**
81
+ * Array that reserved room points into
82
+ */
83
+ public get stagingBuffer(): Uint8Array {
84
+ return TunnelOutbound.scratch
85
+ }
86
+
87
+ /**
88
+ * Hands bytes or a buffer's readable part on
89
+ * @param message what the head was given
90
+ */
91
+ public write(message: unknown): void {
92
+ if (this.failure !== null) return
93
+
94
+ if (message instanceof ByteBuf) {
95
+ const size = message.readableBytes
96
+ this.pass(message.array, message.arrayOffset + message.readerIndex, size)
97
+ return
98
+ }
99
+
100
+ if (!(message instanceof Uint8Array)) {
101
+ throw new TypeError('Message type is not supported')
102
+ }
103
+
104
+ this.pass(message, 0, message.length)
105
+ }
106
+
107
+ /**
108
+ * Makes room in the shared scratch and says where it starts, or -1
109
+ * @param size bytes wanted
110
+ */
111
+ public reserve(size: number): number {
112
+ if (this.failure !== null) return -1
113
+
114
+ if (TunnelOutbound.scratch.length < size) {
115
+ TunnelOutbound.scratch = new Uint8Array(
116
+ Math.max(size, TunnelOutbound.scratch.length * 2),
117
+ )
118
+ }
119
+
120
+ this.reserved = size
121
+ return 0
122
+ }
123
+
124
+ /**
125
+ * Hands on what was written into reserved room
126
+ * @param unused bytes of the room left over
127
+ */
128
+ public commit(unused = 0): void {
129
+ const size = this.reserved - unused
130
+
131
+ this.reserved = 0
132
+ if (size > 0) this.pass(TunnelOutbound.scratch, 0, size)
133
+ }
134
+
135
+ /**
136
+ * Nothing to do, the shard batch goes out at the end of the turn
137
+ */
138
+ public flush(): void {
139
+ return
140
+ }
141
+
142
+ /**
143
+ * Nothing to do, frames are handed on as they come
144
+ */
145
+ public send(): void {
146
+ return
147
+ }
148
+
149
+ /**
150
+ * Nothing to do, there's no socket to drain
151
+ */
152
+ public drain(): void {
153
+ return
154
+ }
155
+
156
+ /**
157
+ * Settles straight away, frames are handed on as they come
158
+ */
159
+ public flushed(): Promise<void> {
160
+ return this.failure === null
161
+ ? Promise.resolve()
162
+ : Promise.reject(this.failure)
163
+ }
164
+
165
+ /**
166
+ * Nothing to do, frames leaving don't touch reads
167
+ */
168
+ public reading(): void {
169
+ return
170
+ }
171
+
172
+ /**
173
+ * Refuses later writes
174
+ * @param error why it stopped
175
+ */
176
+ public fail(error: Error): void {
177
+ this.failure ??= error
178
+ }
179
+
180
+ /**
181
+ * Gives a frame to the sink and counts it
182
+ * @param source array the frame lives in
183
+ * @param start where it begins
184
+ * @param size how many bytes it takes
185
+ */
186
+ private pass(source: Uint8Array, start: number, size: number): void {
187
+ if (size === 0) return
188
+
189
+ this.written += size
190
+ this.sink(source, start, size)
191
+ }
192
+ }
@@ -72,24 +72,15 @@ export class TunnelRelay extends InboundHandlerAdapter {
72
72
  const context = this.context
73
73
  if (context === null) return
74
74
 
75
- const channel = context.channel
76
- const offset = context.writesToSocket ? channel.reserve(size) : -1
77
- if (offset < 0) {
78
- context.writeAndFlushSync(
79
- new ByteBuf(
80
- source,
81
- size,
82
- ByteBuf.defaultMaxCapacity,
83
- start,
84
- start + size,
85
- ),
86
- )
87
- return
88
- }
89
-
90
- ByteBuf.copyBytes(source, start, channel.stagingBuffer, offset, size)
91
- channel.commitStaged()
92
- channel.flushEncoded()
75
+ const frame = new ByteBuf(
76
+ source,
77
+ size,
78
+ ByteBuf.defaultMaxCapacity,
79
+ start,
80
+ start + size,
81
+ )
82
+ if (context.writesToSocket) context.channel.send(frame)
83
+ else context.writeAndFlush(frame)
93
84
  }
94
85
 
95
86
  /**
@@ -97,11 +88,11 @@ export class TunnelRelay extends InboundHandlerAdapter {
97
88
  * @param context the connection
98
89
  * @param message bytes, or anything else
99
90
  */
100
- public override channelRead(
101
- context: HandlerContext,
102
- message: unknown,
103
- ): void | Promise<void> {
104
- if (!(message instanceof ByteBuf)) return context.fireChannelRead(message)
91
+ public override channelRead(context: HandlerContext, message: unknown): void {
92
+ if (!(message instanceof ByteBuf)) {
93
+ context.fireChannelRead(message)
94
+ return
95
+ }
105
96
 
106
97
  const size = message.readableBytes
107
98
 
@@ -121,10 +112,10 @@ export class TunnelRelay extends InboundHandlerAdapter {
121
112
  public override userEventTriggered(
122
113
  context: HandlerContext,
123
114
  event: unknown,
124
- ): Promise<void> {
115
+ ): void {
125
116
  if (event instanceof ShutdownEvent) this.events.stopping(event.deadline)
126
117
 
127
- return context.fireUserEventTriggered(event)
118
+ context.fireUserEventTriggered(event)
128
119
  }
129
120
 
130
121
  /**
@@ -135,21 +126,19 @@ export class TunnelRelay extends InboundHandlerAdapter {
135
126
  public override exceptionCaught(
136
127
  context: HandlerContext,
137
128
  error: unknown,
138
- ): Promise<void> {
139
- const fired = context.fireExceptionCaught(error)
140
-
129
+ ): void {
130
+ context.fireExceptionCaught(error)
141
131
  void context.close()
142
- return fired
143
132
  }
144
133
 
145
134
  /**
146
135
  * Tells the session's shard the client went away
147
136
  * @param context the connection
148
137
  */
149
- public override channelInactive(context: HandlerContext): Promise<void> {
138
+ public override channelInactive(context: HandlerContext): void {
150
139
  this.context = null
151
140
  this.events.closed()
152
141
 
153
- return context.fireChannelInactive()
142
+ context.fireChannelInactive()
154
143
  }
155
144
  }
@@ -1,15 +1,10 @@
1
1
  import type { Transport } from '@netifyjs/netify'
2
2
 
3
3
  /**
4
- * A socket that throws away everything written to it
4
+ * A socket with nobody on the other end, for channels that only hold subscriptions
5
5
  * @class
6
6
  */
7
7
  export class SinkTransport implements Transport {
8
- /**
9
- * Never a datagram socket
10
- */
11
- public readonly datagram: boolean = false
12
-
13
8
  /**
14
9
  * No peer
15
10
  */
@@ -21,7 +16,7 @@ export class SinkTransport implements Transport {
21
16
  public readonly remotePort: number = 0
22
17
 
23
18
  /**
24
- * Takes the bytes and drops them
19
+ * Takes everything and drops it
25
20
  * @param bytes what was written
26
21
  */
27
22
  public write(bytes: Uint8Array): number {
@@ -29,17 +24,17 @@ export class SinkTransport implements Transport {
29
24
  }
30
25
 
31
26
  /**
32
- * Nothing to shut down
27
+ * Nothing to end
33
28
  */
34
29
  public end(): void {
35
30
  return
36
31
  }
37
32
 
38
33
  /**
39
- * Nothing to pause
34
+ * Nothing ever arrives
40
35
  */
41
- public pause(): void {
42
- return
36
+ public pause(): boolean {
37
+ return true
43
38
  }
44
39
 
45
40
  /**