@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
@@ -0,0 +1,263 @@
1
+ /**
2
+ * One bracketed part of a filter while it's being read
3
+ */
4
+ type Scope = {
5
+ /**
6
+ * Whether a SELECT starts in it
7
+ */
8
+ query: boolean
9
+
10
+ /**
11
+ * Tables and other row sources its FROM names
12
+ */
13
+ sources: number
14
+
15
+ /**
16
+ * Whether a FROM list is being read
17
+ */
18
+ listing: boolean
19
+
20
+ /**
21
+ * Rows the subqueries inside it visit for each of its own rows
22
+ */
23
+ inner: number
24
+ }
25
+
26
+ /**
27
+ * How much work a SQL lobby filter can cause, read without running it
28
+ * @class
29
+ */
30
+ export class FilterCost {
31
+ /**
32
+ * Words that end a FROM list
33
+ */
34
+ private static readonly CLAUSES: ReadonlySet<string> = new Set<string>([
35
+ 'WHERE',
36
+ 'GROUP',
37
+ 'HAVING',
38
+ 'ORDER',
39
+ 'LIMIT',
40
+ 'WINDOW',
41
+ 'UNION',
42
+ 'EXCEPT',
43
+ 'INTERSECT',
44
+ 'ON',
45
+ 'USING',
46
+ ])
47
+
48
+ /**
49
+ * Deepest bracket nesting
50
+ */
51
+ public readonly depth: number
52
+
53
+ /**
54
+ * Whether it asks for a recursive query
55
+ */
56
+ public readonly recursive: boolean
57
+
58
+ /**
59
+ * Row visits as powers of the game count, with their multipliers
60
+ */
61
+ private readonly terms: readonly (readonly [number, number])[]
62
+
63
+ /**
64
+ * @param depth deepest bracket nesting
65
+ * @param recursive whether it asks for a recursive query
66
+ * @param terms row visits as powers of the game count
67
+ */
68
+ private constructor(
69
+ depth: number,
70
+ recursive: boolean,
71
+ terms: readonly (readonly [number, number])[],
72
+ ) {
73
+ this.depth = depth
74
+ this.recursive = recursive
75
+ this.terms = terms
76
+ }
77
+
78
+ /**
79
+ * Reads a filter, skipping text, quoted names and comments
80
+ * @param filter what follows SELECT Id FROM Game WHERE
81
+ */
82
+ public static of(filter: string): FilterCost {
83
+ const scopes: Scope[] = [FilterCost.scope(true, 1)]
84
+ const finished: Scope[][] = [[]]
85
+ let depth = 0
86
+ let deepest = 0
87
+ let recursive = false
88
+ let index = 0
89
+
90
+ while (index < filter.length) {
91
+ const char = filter[index] ?? ''
92
+ const end = FilterCost.skip(filter, index)
93
+ if (end > index) {
94
+ index = end
95
+ continue
96
+ }
97
+
98
+ const scope = scopes.at(-1)
99
+ if (/[A-Za-z_]/.test(char)) {
100
+ const word = /^[A-Za-z_][A-Za-z0-9_$]*/.exec(filter.slice(index))
101
+ const text = (word?.[0] ?? char).toUpperCase()
102
+ index += text.length
103
+ if (text === 'RECURSIVE') recursive = true
104
+ if (scope !== undefined) FilterCost.note(scope, text)
105
+ continue
106
+ }
107
+
108
+ index += 1
109
+ if (char === ',' && scope?.listing === true) scope.sources += 1
110
+ if (char === '(') {
111
+ depth += 1
112
+ deepest = Math.max(deepest, depth)
113
+ scopes.push(FilterCost.scope(false, 0))
114
+ finished.push([])
115
+ } else if (char === ')' && depth > 0) {
116
+ depth -= 1
117
+ FilterCost.close(scopes, finished)
118
+ }
119
+ }
120
+
121
+ while (scopes.length > 1) FilterCost.close(scopes, finished)
122
+
123
+ const root = scopes[0] ?? FilterCost.scope(true, 1)
124
+ return new FilterCost(deepest, recursive, FilterCost.expand(root, finished))
125
+ }
126
+
127
+ /**
128
+ * Most rows the filter may visit over some games
129
+ * @param games how many games the lobby holds
130
+ */
131
+ public rows(games: number): number {
132
+ let total = 0
133
+ for (const [power, times] of this.terms) {
134
+ total += times * games ** power
135
+ }
136
+
137
+ return total
138
+ }
139
+
140
+ /**
141
+ * A bracket that's just been opened
142
+ * @param query whether a SELECT starts in it
143
+ * @param sources row sources it starts with
144
+ */
145
+ private static scope(query: boolean, sources: number): Scope {
146
+ return { query, sources, listing: false, inner: 0 }
147
+ }
148
+
149
+ /**
150
+ * Where the text, quoted name or comment at a position ends, or the position itself
151
+ * @param filter the whole filter
152
+ * @param index where to look
153
+ */
154
+ private static skip(filter: string, index: number): number {
155
+ const char = filter[index]
156
+ if (filter.startsWith('--', index)) {
157
+ const end = filter.indexOf('\n', index)
158
+ return end < 0 ? filter.length : end + 1
159
+ }
160
+
161
+ if (filter.startsWith('/*', index)) {
162
+ const end = filter.indexOf('*/', index + 2)
163
+ return end < 0 ? filter.length : end + 2
164
+ }
165
+
166
+ const close =
167
+ char === "'" || char === '"' || char === '`'
168
+ ? char
169
+ : char === '['
170
+ ? ']'
171
+ : null
172
+ if (close === null) return index
173
+
174
+ let at = index + 1
175
+ while (at < filter.length) {
176
+ if (filter[at] !== close) {
177
+ at += 1
178
+ continue
179
+ }
180
+
181
+ if (close !== ']' && filter[at + 1] === close) {
182
+ at += 2
183
+ continue
184
+ }
185
+
186
+ return at + 1
187
+ }
188
+
189
+ return filter.length
190
+ }
191
+
192
+ /**
193
+ * Notes what a keyword means for the bracket it's in
194
+ * @param scope the bracket
195
+ * @param word the keyword in upper case
196
+ */
197
+ private static note(scope: Scope, word: string): void {
198
+ if (word === 'SELECT') {
199
+ scope.query = true
200
+ } else if (word === 'FROM') {
201
+ scope.sources += 1
202
+ scope.listing = true
203
+ } else if (word === 'JOIN') {
204
+ scope.sources += 1
205
+ } else if (FilterCost.CLAUSES.has(word)) {
206
+ scope.listing = false
207
+ }
208
+ }
209
+
210
+ /**
211
+ * Closes the innermost bracket and hands what it found to the one around it
212
+ * @param scopes open brackets, innermost last
213
+ * @param finished closed brackets under each open one
214
+ */
215
+ private static close(scopes: Scope[], finished: Scope[][]): void {
216
+ const scope = scopes.pop()
217
+ const children = finished.pop() ?? []
218
+ const parent = scopes.at(-1)
219
+ const siblings = finished.at(-1)
220
+ if (scope === undefined || parent === undefined || siblings === undefined) {
221
+ return
222
+ }
223
+
224
+ if (scope.query) {
225
+ scope.inner = FilterCost.weigh(children)
226
+ siblings.push(scope)
227
+ return
228
+ }
229
+
230
+ siblings.push(...children)
231
+ }
232
+
233
+ /**
234
+ * Powers of the game count the closed subqueries of a bracket add up to
235
+ * @param children subqueries directly inside it
236
+ */
237
+ private static weigh(children: readonly Scope[]): number {
238
+ let power = 0
239
+ for (const child of children) {
240
+ power = Math.max(power, child.sources + child.inner)
241
+ }
242
+
243
+ return power
244
+ }
245
+
246
+ /**
247
+ * The filter's row visits as powers of the game count
248
+ * @param root the filter itself
249
+ * @param finished closed brackets under it
250
+ */
251
+ private static expand(
252
+ root: Scope,
253
+ finished: readonly Scope[][],
254
+ ): readonly (readonly [number, number])[] {
255
+ const children = finished[0] ?? []
256
+ const terms: [number, number][] = [[root.sources, 1]]
257
+ for (const child of children) {
258
+ terms.push([root.sources + child.sources + child.inner, 1])
259
+ }
260
+
261
+ return terms
262
+ }
263
+ }
@@ -1,3 +1,4 @@
1
+ export * from './cost/index.ts'
1
2
  export * from './error/index.ts'
2
3
  export * from './host/index.ts'
3
4
  export * from './relay/index.ts'
@@ -146,6 +146,24 @@ export class BinaryFormat implements MessageFormat {
146
146
  )
147
147
  }
148
148
 
149
+ /**
150
+ * A clear message serialised into the shared scratch, or null
151
+ * @param message what to send
152
+ * @param peer the connection it leaves on
153
+ */
154
+ public scratch(message: WireMessage, peer: PhotonPeer): ByteBuf | null {
155
+ if (
156
+ message.type === MessageType.INIT ||
157
+ message.type === MessageType.INIT_RESPONSE ||
158
+ message.body instanceof Uint8Array ||
159
+ (message.encrypted && peer.cipher !== null)
160
+ ) {
161
+ return null
162
+ }
163
+
164
+ return BinaryFormat.fill(message, peer, true)
165
+ }
166
+
149
167
  /**
150
168
  * The frame for a message, or null
151
169
  * @param message what to send
@@ -266,6 +284,22 @@ export class BinaryFormat implements MessageFormat {
266
284
  const body = message.body
267
285
  if (body instanceof Uint8Array) return body
268
286
 
287
+ const buffer = BinaryFormat.fill(message, peer, headed)
288
+ return buffer.readBytes(buffer.readableBytes)
289
+ }
290
+
291
+ /**
292
+ * Serialises a message into the shared scratch
293
+ * @param message what to send, with a body to serialise
294
+ * @param peer the connection it leaves on
295
+ * @param headed whether the magic byte and type go first
296
+ */
297
+ private static fill(
298
+ message: WireMessage,
299
+ peer: PhotonPeer,
300
+ headed: boolean,
301
+ ): ByteBuf {
302
+ const body = message.body
269
303
  const serialiser = BinaryFormat.serialiser(peer)
270
304
  const buffer = BinaryFormat.scratch.clear()
271
305
  if (headed) buffer.writeUint8(BinaryFormat.MAGIC).writeUint8(message.type)
@@ -284,6 +318,6 @@ export class BinaryFormat implements MessageFormat {
284
318
  throw new Error('Message has no body')
285
319
  }
286
320
 
287
- return buffer.readBytes(buffer.readableBytes)
321
+ return buffer
288
322
  }
289
323
  }
@@ -37,11 +37,11 @@ export class MessageClientHandler extends MessageHandlerAdapter {
37
37
  * Sends the init request a transport needs
38
38
  * @param context the connection
39
39
  */
40
- public override async channelActive(context: HandlerContext): Promise<void> {
40
+ public override channelActive(context: HandlerContext): void {
41
41
  this.peer.initialise(this.init)
42
42
 
43
- await this.send(context, WireMessage.init(this.init))
44
- await context.fireChannelActive()
43
+ this.send(context, WireMessage.init(this.init))
44
+ context.fireChannelActive()
45
45
  }
46
46
 
47
47
  /**
@@ -49,10 +49,7 @@ export class MessageClientHandler extends MessageHandlerAdapter {
49
49
  * @param context the connection
50
50
  * @param unit a frame, JSON text, or anything else
51
51
  */
52
- public override channelRead(
53
- context: HandlerContext,
54
- unit: unknown,
55
- ): void | Promise<void> {
52
+ public override channelRead(context: HandlerContext, unit: unknown): void {
56
53
  let message: WireMessage | null
57
54
  try {
58
55
  message = this.format.decode(unit, this.peer)
@@ -61,20 +58,26 @@ export class MessageClientHandler extends MessageHandlerAdapter {
61
58
  return
62
59
  }
63
60
 
64
- if (message === null) return context.fireChannelRead(unit)
61
+ if (message === null) {
62
+ context.fireChannelRead(unit)
63
+ return
64
+ }
65
65
 
66
66
  switch (message.type) {
67
67
  case MessageType.OPERATION_RESPONSE:
68
68
  case MessageType.EVENT:
69
69
  case MessageType.DISCONNECT:
70
- return this.deliver(context, message)
70
+ this.deliver(context, message)
71
+ return
71
72
  case MessageType.INTERNAL_RESPONSE:
72
73
  this.settle(context, message)
73
- return context.fireChannelRead(message)
74
+ context.fireChannelRead(message)
75
+ return
74
76
  case MessageType.INIT_RESPONSE:
75
77
  case MessageType.MESSAGE:
76
78
  case MessageType.RAW_MESSAGE:
77
- return context.fireChannelRead(message)
79
+ context.fireChannelRead(message)
80
+ return
78
81
  default:
79
82
  this.drop(context, 'Message type is unexpected')
80
83
  }
@@ -1,3 +1,5 @@
1
+ import type { ByteBuf } from '@netifyjs/netify'
2
+
1
3
  import type { PhotonPeer } from '../peer/index.ts'
2
4
  import type { WireMessage } from '../wire/index.ts'
3
5
 
@@ -18,4 +20,11 @@ export type MessageFormat = {
18
20
  * @param peer the connection it leaves on
19
21
  */
20
22
  encode(message: WireMessage, peer: PhotonPeer): unknown
23
+
24
+ /**
25
+ * Encodes a message into room shared by every connection, or null
26
+ * @param message what to send
27
+ * @param peer the connection it leaves on
28
+ */
29
+ scratch?(message: WireMessage, peer: PhotonPeer): ByteBuf | null
21
30
  }
@@ -4,8 +4,8 @@ import {
4
4
  type HandlerContext,
5
5
  } from '@netifyjs/netify'
6
6
 
7
- import { PacketCodec } from '../../../codec/index.ts'
8
7
  import { Envelope } from '../../envelope/index.ts'
8
+ import { TcpCodec } from '../../tcp/codec/index.ts'
9
9
  import type { MessageFormat } from '../format/index.ts'
10
10
  import type { PhotonPeer } from '../peer/index.ts'
11
11
  import { WireMessage } from '../wire/index.ts'
@@ -36,60 +36,22 @@ export abstract class MessageHandlerAdapter extends DuplexHandlerAdapter {
36
36
  this.format = format
37
37
  }
38
38
 
39
- /**
40
- * Hangs up on a client too slow to take its frames
41
- * @param context the connection
42
- * @param error what went wrong
43
- */
44
- public override exceptionCaught(
45
- context: HandlerContext,
46
- error: unknown,
47
- ): Promise<void> {
48
- if (PacketCodec.overflowed(error)) void context.close()
49
-
50
- return context.fireExceptionCaught(error)
51
- }
52
-
53
39
  /**
54
40
  * Sends envelopes and messages in the connection's format
55
41
  * @param context where the write goes
56
42
  * @param message anything heading out
57
43
  */
58
- public override write(
59
- context: HandlerContext,
60
- message: unknown,
61
- ): Promise<void> {
44
+ public override write(context: HandlerContext, message: unknown): void {
62
45
  const wire = MessageHandlerAdapter.wire(message)
63
- if (wire === null) return context.write(message)
64
-
65
- let unit: unknown
66
- try {
67
- unit = this.unit(wire)
68
- } catch (cause) {
69
- return Promise.reject(new EncoderError('Encoding failed', { cause }))
46
+ if (wire === null) {
47
+ context.write(message)
48
+ return
70
49
  }
71
50
 
72
- return unit === null ? Promise.resolve() : context.write(unit)
73
- }
51
+ if (this.staged(context, wire)) return
74
52
 
75
- /**
76
- * Sends without a promise, false for other messages
77
- * @param context where the write goes
78
- * @param message anything heading out
79
- */
80
- public writeSync(context: HandlerContext, message: unknown): boolean {
81
- const wire = MessageHandlerAdapter.wire(message)
82
- if (wire === null) return false
83
-
84
- let unit: unknown
85
- try {
86
- unit = this.unit(wire)
87
- } catch (cause) {
88
- throw new EncoderError('Encoding failed', { cause })
89
- }
90
-
91
- if (unit !== null) context.writeSync(unit)
92
- return true
53
+ const unit = this.unit(wire)
54
+ if (unit !== null) context.write(unit)
93
55
  }
94
56
 
95
57
  /**
@@ -97,15 +59,9 @@ export abstract class MessageHandlerAdapter extends DuplexHandlerAdapter {
97
59
  * @param context the connection
98
60
  * @param message what to send
99
61
  */
100
- protected send(context: HandlerContext, message: WireMessage): Promise<void> {
101
- let unit: unknown
102
- try {
103
- unit = this.unit(message)
104
- } catch (cause) {
105
- return Promise.reject(new EncoderError('Encoding failed', { cause }))
106
- }
107
-
108
- return unit === null ? Promise.resolve() : context.writeAndFlush(unit)
62
+ protected send(context: HandlerContext, message: WireMessage): void {
63
+ const unit = this.unit(message)
64
+ if (unit !== null) context.writeAndFlush(unit)
109
65
  }
110
66
 
111
67
  /**
@@ -136,12 +92,10 @@ export abstract class MessageHandlerAdapter extends DuplexHandlerAdapter {
136
92
  * @param context the connection
137
93
  * @param message a request, response, event or disconnect message
138
94
  */
139
- protected deliver(
140
- context: HandlerContext,
141
- message: WireMessage,
142
- ): void | Promise<void> {
95
+ protected deliver(context: HandlerContext, message: WireMessage): void {
143
96
  if (message.body instanceof Uint8Array && this.peer.forwards) {
144
- return context.fireChannelRead(message)
97
+ context.fireChannelRead(message)
98
+ return
145
99
  }
146
100
 
147
101
  const envelope = message.envelope()
@@ -150,7 +104,7 @@ export abstract class MessageHandlerAdapter extends DuplexHandlerAdapter {
150
104
  return
151
105
  }
152
106
 
153
- return context.fireChannelRead(envelope)
107
+ context.fireChannelRead(envelope)
154
108
  }
155
109
 
156
110
  /**
@@ -163,14 +117,45 @@ export abstract class MessageHandlerAdapter extends DuplexHandlerAdapter {
163
117
  : null
164
118
  }
165
119
 
120
+ /**
121
+ * Frames a clear message straight into the socket's outbound bytes, or false
122
+ * @param context the handler's place in the pipeline
123
+ * @param message what to send
124
+ */
125
+ private staged(context: HandlerContext, message: WireMessage): boolean {
126
+ const codec = context.previous
127
+ const handler = codec?.handler
128
+ if (codec === null || !(handler instanceof TcpCodec)) return false
129
+ if (this.refusal(message) !== null) return false
130
+
131
+ const bytes = this.format.scratch?.(message, this.peer) ?? null
132
+ return (
133
+ bytes !== null &&
134
+ handler.stage(
135
+ codec,
136
+ message.channel,
137
+ message.delivery,
138
+ bytes.array,
139
+ bytes.arrayOffset + bytes.readerIndex,
140
+ bytes.readableBytes,
141
+ )
142
+ )
143
+ }
144
+
166
145
  /**
167
146
  * What the transport sends for a message, or null
168
147
  * @param message what the application wrote
169
148
  */
170
149
  private unit(message: WireMessage): unknown {
171
150
  const reason = this.refusal(message)
172
- if (reason !== null) throw new Error(reason)
151
+ if (reason !== null) {
152
+ throw new EncoderError('Encoding failed', { cause: new Error(reason) })
153
+ }
173
154
 
174
- return this.format.encode(message, this.peer)
155
+ try {
156
+ return this.format.encode(message, this.peer)
157
+ } catch (cause) {
158
+ throw new EncoderError('Encoding failed', { cause })
159
+ }
175
160
  }
176
161
  }
@@ -92,14 +92,14 @@ export class MessagePipeline {
92
92
  setup: ChannelSetup,
93
93
  options: MessageServerOptions = {},
94
94
  ): ChannelSetup {
95
- return async (channel: Channel): Promise<void> => {
95
+ return (channel: Channel): void => {
96
96
  channel.pipeline.addLast<TcpServerCodec>(
97
97
  MessagePipeline.transport,
98
98
  new TcpServerCodec(MessagePipeline.framing(options)),
99
99
  )
100
100
  MessagePipeline.serve(channel, ConnectionProtocol.TCP, options)
101
101
 
102
- await setup(channel)
102
+ setup(channel)
103
103
  }
104
104
  }
105
105
 
@@ -112,14 +112,14 @@ export class MessagePipeline {
112
112
  setup: ChannelSetup,
113
113
  options: MessageServerOptions = {},
114
114
  ): ChannelSetup {
115
- return WebSocketBridge.listener(async (channel: Channel): Promise<void> => {
115
+ return WebSocketBridge.listener((channel: Channel): void => {
116
116
  MessagePipeline.serve(
117
117
  channel,
118
118
  MessagePipeline.socket(options.secure),
119
119
  options,
120
120
  )
121
121
 
122
- await setup(channel)
122
+ setup(channel)
123
123
  }, MessagePipeline.bridging(options))
124
124
  }
125
125
 
@@ -132,10 +132,10 @@ export class MessagePipeline {
132
132
  setup: ChannelSetup,
133
133
  options: MessageServerOptions = {},
134
134
  ): ChannelSetup {
135
- return async (channel: Channel): Promise<void> => {
135
+ return (channel: Channel): void => {
136
136
  MessagePipeline.serve(channel, ConnectionProtocol.UDP, options)
137
137
 
138
- await setup(channel)
138
+ setup(channel)
139
139
  }
140
140
  }
141
141
 
@@ -150,7 +150,7 @@ export class MessagePipeline {
150
150
  setup: ChannelSetup,
151
151
  options: MessageClientOptions = {},
152
152
  ): ChannelSetup {
153
- return async (channel: Channel): Promise<void> => {
153
+ return (channel: Channel): void => {
154
154
  channel.pipeline.addLast<TcpClientCodec>(
155
155
  MessagePipeline.transport,
156
156
  new TcpClientCodec(MessagePipeline.framing(options)),
@@ -161,7 +161,7 @@ export class MessagePipeline {
161
161
  MessagePipeline.framed(init),
162
162
  )
163
163
 
164
- await setup(channel)
164
+ setup(channel)
165
165
  }
166
166
  }
167
167
 
@@ -183,14 +183,14 @@ export class MessagePipeline {
183
183
  return WebSocketBridge.connector(
184
184
  `${address}${MessagePipeline.target(request)}`,
185
185
  request.protocol,
186
- async (channel: Channel): Promise<void> => {
186
+ (channel: Channel): void => {
187
187
  MessagePipeline.dial(
188
188
  channel,
189
189
  MessagePipeline.socket(options.secure),
190
190
  request,
191
191
  )
192
192
 
193
- await setup(channel)
193
+ setup(channel)
194
194
  },
195
195
  MessagePipeline.bridging(options),
196
196
  )
@@ -205,14 +205,14 @@ export class MessagePipeline {
205
205
  init: InitRequest | InitFields,
206
206
  setup: ChannelSetup,
207
207
  ): ChannelSetup {
208
- return async (channel: Channel): Promise<void> => {
208
+ return (channel: Channel): void => {
209
209
  MessagePipeline.dial(
210
210
  channel,
211
211
  ConnectionProtocol.UDP,
212
212
  MessagePipeline.framed(init),
213
213
  )
214
214
 
215
- await setup(channel)
215
+ setup(channel)
216
216
  }
217
217
  }
218
218