@netifyjs/protocol 1.0.0-alpha.2 → 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 (183) hide show
  1. package/package.json +2 -2
  2. package/src/codec/index.ts +82 -157
  3. package/src/dispatch/dispatcher/index.ts +116 -33
  4. package/src/dispatch/session/index.ts +9 -3
  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/binary/v18/index.ts +26 -1
  34. package/src/photon/binary/value/index.ts +52 -3
  35. package/src/photon/client/connection/index.ts +6 -6
  36. package/src/photon/cluster/back/index.ts +32 -45
  37. package/src/photon/cluster/dresser/index.ts +7 -8
  38. package/src/photon/cluster/front/index.ts +25 -49
  39. package/src/photon/cluster/grid/index.ts +1 -1
  40. package/src/photon/cluster/mover/index.ts +12 -12
  41. package/src/photon/cluster/press/index.ts +2 -18
  42. package/src/photon/cluster/route/game/index.ts +9 -11
  43. package/src/photon/cluster/route/master/index.ts +15 -34
  44. package/src/photon/cluster/route/name/index.ts +4 -21
  45. package/src/photon/cluster/seat/game/index.ts +6 -26
  46. package/src/photon/cluster/seat/master/index.ts +1 -1
  47. package/src/photon/cluster/sessions/index.ts +5 -6
  48. package/src/photon/cluster/spread/index.ts +8 -147
  49. package/src/photon/enet/client/index.ts +1 -8
  50. package/src/photon/enet/gateway/index.ts +5 -15
  51. package/src/photon/enet/index.ts +1 -1
  52. package/src/photon/enet/link/index.ts +34 -15
  53. package/src/photon/enet/outbound/index.ts +279 -0
  54. package/src/photon/enet/peer/index.ts +59 -0
  55. package/src/photon/enet/transport/index.ts +8 -24
  56. package/src/photon/game/admission/index.ts +353 -0
  57. package/src/photon/game/bag/index.ts +13 -0
  58. package/src/photon/game/broadcast/index.ts +79 -0
  59. package/src/photon/game/editor/index.ts +446 -0
  60. package/src/photon/game/gate/index.ts +9 -8
  61. package/src/photon/game/handler/index.ts +30 -3
  62. package/src/photon/game/history/index.ts +234 -0
  63. package/src/photon/game/hooked/index.ts +16 -15
  64. package/src/photon/game/index.ts +12 -1
  65. package/src/photon/game/lifecycle/index.ts +193 -0
  66. package/src/photon/game/peer/index.ts +1 -10
  67. package/src/photon/game/properties/index.ts +342 -0
  68. package/src/photon/game/room/index.ts +435 -1793
  69. package/src/photon/game/rooms/index.ts +2 -1
  70. package/src/photon/game/seats/index.ts +237 -0
  71. package/src/photon/game/server/index.ts +12 -2
  72. package/src/photon/game/services/index.ts +41 -0
  73. package/src/photon/game/settings/index.ts +22 -0
  74. package/src/photon/game/snapshot/index.ts +133 -0
  75. package/src/photon/game/throttle/index.ts +60 -0
  76. package/src/photon/game/touch/index.ts +2 -5
  77. package/src/photon/game/venue/index.ts +31 -0
  78. package/src/photon/master/application/index.ts +7 -0
  79. package/src/photon/master/gate/index.ts +12 -8
  80. package/src/photon/master/handler/index.ts +36 -748
  81. package/src/photon/master/index.ts +5 -0
  82. package/src/photon/master/list/sql/index.ts +37 -3
  83. package/src/photon/master/listing/index.ts +133 -0
  84. package/src/photon/master/lobby/index.ts +9 -0
  85. package/src/photon/master/login/index.ts +424 -0
  86. package/src/photon/master/matchmaking/index.ts +282 -0
  87. package/src/photon/master/query/index.ts +45 -0
  88. package/src/photon/master/redirect/index.ts +8 -7
  89. package/src/photon/master/services/index.ts +35 -0
  90. package/src/photon/master/settings/index.ts +44 -0
  91. package/src/photon/master/sql/cost/index.ts +263 -0
  92. package/src/photon/master/sql/index.ts +1 -0
  93. package/src/photon/message/binary/index.ts +35 -1
  94. package/src/photon/message/client/index.ts +14 -11
  95. package/src/photon/message/format/index.ts +9 -0
  96. package/src/photon/message/handler/index.ts +48 -63
  97. package/src/photon/message/pipeline/index.ts +12 -12
  98. package/src/photon/message/server/index.ts +87 -43
  99. package/src/photon/message/watch/index.ts +127 -15
  100. package/src/photon/server/config/index.ts +5 -0
  101. package/src/photon/server/deferral/index.ts +57 -0
  102. package/src/photon/server/host/index.ts +36 -10
  103. package/src/photon/server/index.ts +1 -0
  104. package/src/photon/server/listeners/index.ts +11 -0
  105. package/src/photon/tcp/client/index.ts +1 -1
  106. package/src/photon/tcp/codec/index.ts +41 -38
  107. package/src/photon/tcp/framing/index.ts +24 -4
  108. package/src/photon/tcp/server/index.ts +4 -4
  109. package/src/photon/websocket/bridge/index.ts +16 -20
  110. package/src/photon/websocket/message/index.ts +27 -39
  111. package/src/photon/websocket/path/index.ts +2 -5
  112. package/src/scale/batch/index.ts +14 -6
  113. package/src/scale/fanout/index.ts +6 -6
  114. package/src/scale/index.ts +2 -4
  115. package/src/scale/outbound/index.ts +192 -0
  116. package/src/scale/relay/index.ts +24 -16
  117. package/src/scale/sink/index.ts +6 -11
  118. package/src/scale/transport/index.ts +27 -44
  119. package/src/scale/tunnel/index.ts +56 -86
  120. package/src/smartfox/common/bluebox/gateway/index.ts +401 -0
  121. package/src/smartfox/common/bluebox/index.ts +4 -0
  122. package/src/smartfox/{x2 → common}/bluebox/transport/index.ts +36 -23
  123. package/src/smartfox/common/index.ts +5 -0
  124. package/src/smartfox/common/strikes/index.ts +74 -0
  125. package/src/smartfox/pro/bluebox/index.ts +176 -4
  126. package/src/smartfox/pro/codec/index.ts +8 -7
  127. package/src/smartfox/pro/escalation/index.ts +28 -20
  128. package/src/smartfox/pro/flood/index.ts +2 -2
  129. package/src/smartfox/pro/format/extension/raw/index.ts +12 -2
  130. package/src/smartfox/pro/handler/chat/index.ts +11 -8
  131. package/src/smartfox/pro/handler/login/index.ts +2 -25
  132. package/src/smartfox/pro/handler/shared/index.ts +4 -3
  133. package/src/smartfox/pro/index.ts +11 -5
  134. package/src/smartfox/pro/login/gate/index.ts +166 -0
  135. package/src/smartfox/pro/login/index.ts +1 -0
  136. package/src/smartfox/pro/moderation/index.ts +220 -0
  137. package/src/smartfox/pro/options/index.ts +5 -5
  138. package/src/smartfox/pro/protocol/index.ts +62 -8
  139. package/src/smartfox/pro/web/index.ts +33 -26
  140. package/src/smartfox/pro/zone/index.ts +59 -282
  141. package/src/smartfox/pro/zones/index.ts +3 -3
  142. package/src/smartfox/x2/bluebox/index.ts +149 -4
  143. package/src/smartfox/x2/chat/index.ts +9 -19
  144. package/src/smartfox/x2/codec/index.ts +34 -40
  145. package/src/smartfox/x2/config/index.ts +6 -1
  146. package/src/smartfox/x2/data/array/index.ts +96 -60
  147. package/src/smartfox/x2/data/object/index.ts +144 -82
  148. package/src/smartfox/x2/data/serializer/index.ts +41 -23
  149. package/src/smartfox/x2/flood/gate/index.ts +19 -13
  150. package/src/smartfox/x2/format/buddy/init/response/index.ts +4 -3
  151. package/src/smartfox/x2/format/find/rooms/response/index.ts +4 -3
  152. package/src/smartfox/x2/format/find/users/response/index.ts +4 -3
  153. package/src/smartfox/x2/format/group/subscribe/response/index.ts +4 -3
  154. package/src/smartfox/x2/format/login/response/index.ts +4 -3
  155. package/src/smartfox/x2/format/mmo/position/response/index.ts +8 -6
  156. package/src/smartfox/x2/format/room/join/response/index.ts +4 -3
  157. package/src/smartfox/x2/games/index.ts +35 -8
  158. package/src/smartfox/x2/hall/index.ts +0 -6
  159. package/src/smartfox/x2/handler/session/index.ts +1 -1
  160. package/src/smartfox/x2/index.ts +8 -3
  161. package/src/smartfox/x2/member/index.ts +10 -3
  162. package/src/smartfox/x2/packet/login/response/index.ts +12 -0
  163. package/src/smartfox/x2/protocol/index.ts +11 -1
  164. package/src/smartfox/x2/rooms/index.ts +1 -2
  165. package/src/smartfox/x2/sessions/index.ts +6 -0
  166. package/src/smartfox/x2/udp/index.ts +61 -24
  167. package/src/smartfox/x2/web/index.ts +35 -17
  168. package/src/smartfox/x2/websocket/index.ts +18 -36
  169. package/src/smartfox/x2/zone/index.ts +77 -28
  170. package/src/smartfox/x2/zones/index.ts +17 -5
  171. package/src/photon/enet/handler/index.ts +0 -65
  172. package/src/scale/hosted/index.ts +0 -68
  173. package/src/scale/outlet/index.ts +0 -121
  174. package/src/smartfox/pro/bluebox/gateway/index.ts +0 -310
  175. package/src/smartfox/pro/bluebox/session/index.ts +0 -63
  176. package/src/smartfox/pro/bluebox/transport/index.ts +0 -104
  177. package/src/smartfox/x2/bluebox/gateway/index.ts +0 -315
  178. /package/src/smartfox/{x2 → common}/bluebox/session/index.ts +0 -0
  179. /package/src/smartfox/{pro → common}/counters/index.ts +0 -0
  180. /package/src/smartfox/{pro → common}/counters/memory/index.ts +0 -0
  181. /package/src/smartfox/{pro → common}/counters/offender/index.ts +0 -0
  182. /package/src/smartfox/{pro → common}/counters/store/index.ts +0 -0
  183. /package/src/smartfox/{pro → common}/sequence/index.ts +0 -0
@@ -94,16 +94,36 @@ export class TcpFraming {
94
94
  * @param offset where it starts in the target
95
95
  */
96
96
  public write(frame: Frame, target: Uint8Array, offset: number): void {
97
- const size = TcpFraming.HEADER + frame.payload.length
97
+ const payload = frame.payload
98
+
99
+ this.head(frame.channel, frame.delivery, payload.length, target, offset)
100
+ target.set(payload, offset + TcpFraming.HEADER)
101
+ }
102
+
103
+ /**
104
+ * Writes a header for a payload that follows it
105
+ * @param channel the message's channel
106
+ * @param delivery how it's delivered
107
+ * @param length bytes of payload
108
+ * @param target where it goes
109
+ * @param offset where the header starts
110
+ */
111
+ public head(
112
+ channel: number,
113
+ delivery: DeliveryMode,
114
+ length: number,
115
+ target: Uint8Array,
116
+ offset: number,
117
+ ): void {
118
+ const size = TcpFraming.HEADER + length
98
119
 
99
120
  target[offset] = TcpFraming.MAGIC
100
121
  target[offset + 1] = size >>> 24
101
122
  target[offset + 2] = size >>> 16
102
123
  target[offset + 3] = size >>> 8
103
124
  target[offset + 4] = size
104
- target[offset + 5] = frame.channel
105
- target[offset + 6] = frame.delivery
106
- target.set(frame.payload, offset + TcpFraming.HEADER)
125
+ target[offset + 5] = channel
126
+ target[offset + 6] = delivery
107
127
  }
108
128
 
109
129
  /**
@@ -1,4 +1,4 @@
1
- import { ByteBuf, type HandlerContext } from '@netifyjs/netify'
1
+ import type { ByteBuf, HandlerContext } from '@netifyjs/netify'
2
2
 
3
3
  import { Frame } from '../../frame/index.ts'
4
4
  import { TcpCodec, type TcpOptions } from '../codec/index.ts'
@@ -49,14 +49,14 @@ export class TcpServerCodec extends TcpCodec<Frame> {
49
49
  }
50
50
 
51
51
  if (first !== TcpFraming.MAGIC) {
52
- this.fail(context, input, 'Frame is malformed')
52
+ this.fail(input, 'Frame is malformed')
53
53
  }
54
54
 
55
55
  if (input.readableBytes < 5) return
56
56
 
57
57
  const size = input.getUint32(start + 1)
58
58
  const reason = this.framing.refusal(size)
59
- if (reason !== null) this.fail(context, input, reason)
59
+ if (reason !== null) this.fail(input, reason)
60
60
  if (input.readableBytes < size) return
61
61
 
62
62
  input.skipBytes(5)
@@ -76,6 +76,6 @@ export class TcpServerCodec extends TcpCodec<Frame> {
76
76
  private answer(context: HandlerContext, ping: TcpPing): void {
77
77
  const pong = TcpPong.of(this.clock.now(), ping.clientTime)
78
78
 
79
- context.writeAndFlushSync(ByteBuf.wrap(pong.encode()))
79
+ context.writeAndFlush(pong.encode())
80
80
  }
81
81
  }
@@ -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
  }
@@ -3,10 +3,18 @@ import { ByteBuf } from '@netifyjs/netify'
3
3
  /**
4
4
  * Hears one session's share of a batch
5
5
  * @param id the session on the client's shard
6
- * @param bytes its frames, a view into the batch
6
+ * @param bytes the whole batch, borrowed for the call
7
+ * @param start where the session's frames begin
8
+ * @param size how many bytes they take
7
9
  * @param from the shard that sent the batch
8
10
  */
9
- export type BatchVisitor = (id: number, bytes: Uint8Array, from: number) => void
11
+ export type BatchVisitor = (
12
+ id: number,
13
+ bytes: Uint8Array,
14
+ start: number,
15
+ size: number,
16
+ from: number,
17
+ ) => void
10
18
 
11
19
  /**
12
20
  * Everything a batch held as it was taken
@@ -185,11 +193,11 @@ export class Batch {
185
193
  while (at + Batch.HEAD <= bytes.length) {
186
194
  const id = Batch.word(bytes, at)
187
195
  const start = at + Batch.HEAD
188
- const end = start + Batch.word(bytes, at + 4)
189
- if (end > bytes.length) throw new Error('Batch is cut short')
196
+ const size = Batch.word(bytes, at + 4)
197
+ if (start + size > bytes.length) throw new Error('Batch is cut short')
190
198
 
191
- visit(id, bytes.subarray(start, end), from)
192
- at = end
199
+ visit(id, bytes, start, size, from)
200
+ at = start + size
193
201
  }
194
202
  }
195
203
 
@@ -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
+ }
@@ -64,13 +64,23 @@ export class TunnelRelay extends InboundHandlerAdapter {
64
64
 
65
65
  /**
66
66
  * Writes bytes from the session's shard to the client
67
- * @param bytes whole frames
67
+ * @param source holds whole frames and is never written again
68
+ * @param start where they begin
69
+ * @param size how many bytes they take
68
70
  */
69
- public deliver(bytes: Uint8Array): void {
71
+ public deliver(source: Uint8Array, start: number, size: number): void {
70
72
  const context = this.context
71
73
  if (context === null) return
72
74
 
73
- context.writeAndFlushSync(ByteBuf.wrap(bytes))
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)
74
84
  }
75
85
 
76
86
  /**
@@ -78,11 +88,11 @@ export class TunnelRelay extends InboundHandlerAdapter {
78
88
  * @param context the connection
79
89
  * @param message bytes, or anything else
80
90
  */
81
- public override channelRead(
82
- context: HandlerContext,
83
- message: unknown,
84
- ): void | Promise<void> {
85
- 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
+ }
86
96
 
87
97
  const size = message.readableBytes
88
98
 
@@ -102,10 +112,10 @@ export class TunnelRelay extends InboundHandlerAdapter {
102
112
  public override userEventTriggered(
103
113
  context: HandlerContext,
104
114
  event: unknown,
105
- ): Promise<void> {
115
+ ): void {
106
116
  if (event instanceof ShutdownEvent) this.events.stopping(event.deadline)
107
117
 
108
- return context.fireUserEventTriggered(event)
118
+ context.fireUserEventTriggered(event)
109
119
  }
110
120
 
111
121
  /**
@@ -116,21 +126,19 @@ export class TunnelRelay extends InboundHandlerAdapter {
116
126
  public override exceptionCaught(
117
127
  context: HandlerContext,
118
128
  error: unknown,
119
- ): Promise<void> {
120
- const fired = context.fireExceptionCaught(error)
121
-
129
+ ): void {
130
+ context.fireExceptionCaught(error)
122
131
  void context.close()
123
- return fired
124
132
  }
125
133
 
126
134
  /**
127
135
  * Tells the session's shard the client went away
128
136
  * @param context the connection
129
137
  */
130
- public override channelInactive(context: HandlerContext): Promise<void> {
138
+ public override channelInactive(context: HandlerContext): void {
131
139
  this.context = null
132
140
  this.events.closed()
133
141
 
134
- return context.fireChannelInactive()
142
+ context.fireChannelInactive()
135
143
  }
136
144
  }