@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
@@ -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'
@@ -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
  }
@@ -4,7 +4,6 @@ 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'
9
8
  import { TcpCodec } from '../../tcp/codec/index.ts'
10
9
  import type { MessageFormat } from '../format/index.ts'
@@ -37,61 +36,22 @@ export abstract class MessageHandlerAdapter extends DuplexHandlerAdapter {
37
36
  this.format = format
38
37
  }
39
38
 
40
- /**
41
- * Hangs up on a client too slow to take its frames
42
- * @param context the connection
43
- * @param error what went wrong
44
- */
45
- public override exceptionCaught(
46
- context: HandlerContext,
47
- error: unknown,
48
- ): Promise<void> {
49
- if (PacketCodec.overflowed(error)) void context.close()
50
-
51
- return context.fireExceptionCaught(error)
52
- }
53
-
54
39
  /**
55
40
  * Sends envelopes and messages in the connection's format
56
41
  * @param context where the write goes
57
42
  * @param message anything heading out
58
43
  */
59
- public override write(
60
- context: HandlerContext,
61
- message: unknown,
62
- ): Promise<void> {
44
+ public override write(context: HandlerContext, message: unknown): void {
63
45
  const wire = MessageHandlerAdapter.wire(message)
64
- if (wire === null) return context.write(message)
65
-
66
- let unit: unknown
67
- try {
68
- unit = this.unit(wire)
69
- } catch (cause) {
70
- return Promise.reject(new EncoderError('Encoding failed', { cause }))
46
+ if (wire === null) {
47
+ context.write(message)
48
+ return
71
49
  }
72
50
 
73
- return unit === null ? Promise.resolve() : context.write(unit)
74
- }
75
-
76
- /**
77
- * Sends without a promise, false for other messages
78
- * @param context where the write goes
79
- * @param message anything heading out
80
- */
81
- public writeSync(context: HandlerContext, message: unknown): boolean {
82
- const wire = MessageHandlerAdapter.wire(message)
83
- if (wire === null) return false
84
- if (this.staged(context, wire)) return true
85
-
86
- let unit: unknown
87
- try {
88
- unit = this.unit(wire)
89
- } catch (cause) {
90
- throw new EncoderError('Encoding failed', { cause })
91
- }
51
+ if (this.staged(context, wire)) return
92
52
 
93
- if (unit !== null) context.writeSync(unit)
94
- return true
53
+ const unit = this.unit(wire)
54
+ if (unit !== null) context.write(unit)
95
55
  }
96
56
 
97
57
  /**
@@ -99,15 +59,9 @@ export abstract class MessageHandlerAdapter extends DuplexHandlerAdapter {
99
59
  * @param context the connection
100
60
  * @param message what to send
101
61
  */
102
- protected send(context: HandlerContext, message: WireMessage): Promise<void> {
103
- let unit: unknown
104
- try {
105
- unit = this.unit(message)
106
- } catch (cause) {
107
- return Promise.reject(new EncoderError('Encoding failed', { cause }))
108
- }
109
-
110
- 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)
111
65
  }
112
66
 
113
67
  /**
@@ -138,12 +92,10 @@ export abstract class MessageHandlerAdapter extends DuplexHandlerAdapter {
138
92
  * @param context the connection
139
93
  * @param message a request, response, event or disconnect message
140
94
  */
141
- protected deliver(
142
- context: HandlerContext,
143
- message: WireMessage,
144
- ): void | Promise<void> {
95
+ protected deliver(context: HandlerContext, message: WireMessage): void {
145
96
  if (message.body instanceof Uint8Array && this.peer.forwards) {
146
- return context.fireChannelRead(message)
97
+ context.fireChannelRead(message)
98
+ return
147
99
  }
148
100
 
149
101
  const envelope = message.envelope()
@@ -152,7 +104,7 @@ export abstract class MessageHandlerAdapter extends DuplexHandlerAdapter {
152
104
  return
153
105
  }
154
106
 
155
- return context.fireChannelRead(envelope)
107
+ context.fireChannelRead(envelope)
156
108
  }
157
109
 
158
110
  /**
@@ -196,8 +148,14 @@ export abstract class MessageHandlerAdapter extends DuplexHandlerAdapter {
196
148
  */
197
149
  private unit(message: WireMessage): unknown {
198
150
  const reason = this.refusal(message)
199
- if (reason !== null) throw new Error(reason)
151
+ if (reason !== null) {
152
+ throw new EncoderError('Encoding failed', { cause: new Error(reason) })
153
+ }
200
154
 
201
- 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
+ }
202
160
  }
203
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