@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,220 @@
1
+ import { Offender } from '../../common/counters/offender/index.ts'
2
+ import type { CounterStore } from '../../common/counters/store/index.ts'
3
+ import { Bans } from '../bans/list/index.ts'
4
+ import { MemoryBanStore } from '../bans/memory/index.ts'
5
+ import type { Escalation } from '../escalation/index.ts'
6
+ import { WordFilter } from '../filter/index.ts'
7
+ import { FloodGuard } from '../flood/index.ts'
8
+ import type { ZoneOptions } from '../options/index.ts'
9
+ import { AdminMessage } from '../packet/chat/admin/index.ts'
10
+ import type { Penalty } from '../penalty/index.ts'
11
+ import type { User } from '../user/index.ts'
12
+ import type { Zone } from '../zone/index.ts'
13
+
14
+ /**
15
+ * Keeps a zone's chat clean and puts out whoever breaks the rules
16
+ * @class
17
+ */
18
+ export class Moderation {
19
+ /**
20
+ * Names and addresses that can't log in
21
+ */
22
+ public readonly bans: Bans
23
+
24
+ /**
25
+ * Hides unwanted words in chat, or null
26
+ */
27
+ public readonly filter: WordFilter | null
28
+
29
+ /**
30
+ * Stops users from spamming chat, or null
31
+ */
32
+ public readonly flood: FloodGuard | null
33
+
34
+ /**
35
+ * The zone whose users are watched
36
+ */
37
+ private readonly zone: Zone
38
+
39
+ /**
40
+ * How long a kicked user gets to read why, in ms
41
+ */
42
+ private readonly kickDelay: number
43
+
44
+ /**
45
+ * Whether flooding and swearing ban the address too
46
+ */
47
+ private readonly banAddresses: boolean
48
+
49
+ /**
50
+ * Where warnings and kicks are counted, or null
51
+ */
52
+ private readonly counters: CounterStore | null
53
+
54
+ /**
55
+ * @param zone the zone whose users are watched
56
+ * @param options how the zone is set up
57
+ */
58
+ public constructor(zone: Zone, options: ZoneOptions) {
59
+ this.zone = zone
60
+ this.bans = new Bans(
61
+ options.bans ?? new MemoryBanStore(),
62
+ zone.logger,
63
+ options.banOutage === 'refuse',
64
+ )
65
+ this.filter =
66
+ options.filter === undefined ? null : new WordFilter(options.filter)
67
+ this.flood =
68
+ options.flood === undefined ? null : new FloodGuard(options.flood)
69
+ this.kickDelay = options.kickDelay ?? 1000
70
+ this.banAddresses = options.banMode !== 'name'
71
+ this.counters = options.counters ?? null
72
+ if (this.flood?.shared === true && this.counters === null) {
73
+ throw new RangeError('Counter store is missing')
74
+ }
75
+ }
76
+
77
+ /**
78
+ * Tells a user why, then hangs up on them
79
+ * @param user who to put out
80
+ * @param text what they read first
81
+ */
82
+ public kick(user: User, text: string): void {
83
+ const session = this.zone.session(user.id)
84
+ if (session === null) return
85
+
86
+ session.send(AdminMessage.of(-1, 0, text))
87
+ setTimeout((): void => {
88
+ void session.close()
89
+ }, this.kickDelay)
90
+ }
91
+
92
+ /**
93
+ * Kicks a user and keeps their name and address out
94
+ * @param user who to ban
95
+ * @param text what they read first
96
+ * @param duration how long it lasts in ms, null for good
97
+ */
98
+ public async ban(
99
+ user: User,
100
+ text: string,
101
+ duration: number | null = null,
102
+ ): Promise<void> {
103
+ const session = this.zone.session(user.id)
104
+
105
+ this.kick(user, text)
106
+ await this.bans.add(
107
+ user.name,
108
+ session?.channel.remoteAddress ?? '',
109
+ duration,
110
+ )
111
+ }
112
+
113
+ /**
114
+ * Warns, kicks or bans a user for breaking a rule
115
+ * @param user who broke it
116
+ * @param escalation what that rule earns
117
+ */
118
+ public discipline(user: User, escalation: Escalation): Promise<void> {
119
+ return this.counters === null
120
+ ? this.punish(user, escalation.strike(user.id, user.name))
121
+ : this.counted(user, escalation, this.counters)
122
+ }
123
+
124
+ /**
125
+ * Counts a chat line in the counter store and says whether it's flooding
126
+ * @param user who wrote it
127
+ * @param text what they wrote
128
+ */
129
+ public async floods(user: User, text: string): Promise<boolean> {
130
+ const flood = this.flood
131
+ const counters = this.counters
132
+ if (flood?.shared !== true || counters === null) return false
133
+
134
+ try {
135
+ return await flood.counts(counters, this.offender(user), text)
136
+ } catch (err) {
137
+ this.zone.logger.warn({ err }, 'Counter store failed')
138
+
139
+ return false
140
+ }
141
+ }
142
+
143
+ /**
144
+ * Whether a name is too long or has a listed word
145
+ * @param name a user or room name
146
+ */
147
+ public unfit(name: string): boolean {
148
+ if (name.length > this.zone.limits.nameLength) return true
149
+
150
+ return this.filter !== null && this.filter.names && this.filter.has(name)
151
+ }
152
+
153
+ /**
154
+ * Drops the warnings and chat counts of a user who left
155
+ * @param user who left
156
+ */
157
+ public forget(user: User): void {
158
+ this.flood?.forget(user.id)
159
+ this.filter?.escalation?.forget(user.id)
160
+ }
161
+
162
+ /**
163
+ * Carries out one step up the ladder
164
+ * @param user who broke a rule
165
+ * @param penalty what they earned
166
+ */
167
+ private async punish(user: User, penalty: Penalty): Promise<void> {
168
+ switch (penalty.action) {
169
+ case 'warn':
170
+ this.zone.send(user.id, AdminMessage.of(-1, 0, penalty.text))
171
+ break
172
+ case 'kick':
173
+ this.kick(user, penalty.text)
174
+ break
175
+ case 'ban':
176
+ if (this.banAddresses) {
177
+ await this.ban(user, penalty.text, penalty.duration)
178
+ break
179
+ }
180
+
181
+ this.kick(user, penalty.text)
182
+ await this.bans.add(user.name, '', penalty.duration)
183
+ break
184
+ }
185
+ }
186
+
187
+ /**
188
+ * Punishes by the counts in a store
189
+ * @param user who broke a rule
190
+ * @param escalation what that rule earns
191
+ * @param counters where warnings and kicks are counted
192
+ */
193
+ private async counted(
194
+ user: User,
195
+ escalation: Escalation,
196
+ counters: CounterStore,
197
+ ): Promise<void> {
198
+ let penalty: Penalty
199
+ try {
200
+ penalty = await escalation.count(counters, this.offender(user))
201
+ } catch (err) {
202
+ this.zone.logger.warn({ err }, 'Counter store failed')
203
+ penalty = escalation.strike(user.id, user.name)
204
+ }
205
+
206
+ await this.punish(user, penalty)
207
+ }
208
+
209
+ /**
210
+ * Who a user's offences are counted against
211
+ * @param user who broke a rule
212
+ */
213
+ private offender(user: User): Offender {
214
+ const address = this.zone.session(user.id)?.channel.remoteAddress ?? ''
215
+
216
+ return this.zone.gate.guested(user) && address !== ''
217
+ ? Offender.addressed(this.zone.name, address)
218
+ : Offender.named(this.zone.name, user.name.toLowerCase())
219
+ }
220
+ }
@@ -2,7 +2,7 @@ import type { Logger } from '@netifyjs/netify'
2
2
 
3
3
  import type { BanStore } from '../bans/store/index.ts'
4
4
  import type { BuddyStore } from '../buddies/store/index.ts'
5
- import type { CounterStore } from '../counters/store/index.ts'
5
+ import type { CounterStore } from '../../common/counters/store/index.ts'
6
6
  import type { FilterOptions } from '../filter/index.ts'
7
7
  import type { FloodOptions } from '../flood/index.ts'
8
8
  import type { Authenticator } from '../login/authenticator/index.ts'
@@ -58,17 +58,17 @@ export type ZoneLimits = {
58
58
  readonly nameLength?: number
59
59
 
60
60
  /**
61
- * Most rooms one user can have open
61
+ * Most rooms one user can have open, 5 by default
62
62
  */
63
63
  readonly roomsPerUser?: number
64
64
 
65
65
  /**
66
- * Most variables on one room
66
+ * Most variables on one room, 100 by default
67
67
  */
68
68
  readonly roomVariables?: number
69
69
 
70
70
  /**
71
- * Most variables on one user
71
+ * Most variables on one user, 100 by default
72
72
  */
73
73
  readonly userVariables?: number
74
74
  }
@@ -103,7 +103,7 @@ export type ZoneOptions = {
103
103
  readonly maxUsers?: number
104
104
 
105
105
  /**
106
- * Most rooms open at once
106
+ * Most rooms open at once, 10000 by default
107
107
  */
108
108
  readonly maxRooms?: number
109
109
 
@@ -110,6 +110,16 @@ export class SmartFoxPro extends Protocol {
110
110
  */
111
111
  private readonly encoder: TextEncoder
112
112
 
113
+ /**
114
+ * JSON reply measured last, or null
115
+ */
116
+ private measured: JsonReply | null
117
+
118
+ /**
119
+ * Text of the JSON reply measured last, or null
120
+ */
121
+ private measuredText: string | null
122
+
113
123
  /**
114
124
  * First character of a raw message
115
125
  */
@@ -151,6 +161,8 @@ export class SmartFoxPro extends Protocol {
151
161
  this.object = new ObjectFormat()
152
162
  this.writer = new XmlWriter()
153
163
  this.encoder = new TextEncoder()
164
+ this.measured = null
165
+ this.measuredText = null
154
166
  this.separatorCode = separator.charCodeAt(0)
155
167
  this.policy =
156
168
  options.policy === undefined ? SmartFoxPro.defaultPolicy : options.policy
@@ -232,7 +244,9 @@ export class SmartFoxPro extends Protocol {
232
244
  const size = this.measure(packet)
233
245
  if (size >= 0) {
234
246
  const plain = new Uint8Array(size)
235
- if (this.write(packet, plain, 0) === size) return plain
247
+ const taken = this.write(packet, plain, 0)
248
+ if (taken === size) return plain
249
+ if (taken >= 0) return plain.slice(0, taken)
236
250
  }
237
251
 
238
252
  const text = this.text(packet)
@@ -241,30 +255,70 @@ export class SmartFoxPro extends Protocol {
241
255
  }
242
256
 
243
257
  /**
244
- * Writes raw replies straight into a socket's outbound bytes
258
+ * Writes raw and JSON replies straight into a socket's outbound bytes
245
259
  */
246
260
  public override straight(): DirectWriter {
247
261
  return this
248
262
  }
249
263
 
250
264
  /**
251
- * Bytes a raw reply takes when it's written straight in, or -1
265
+ * Most bytes a reply could take in staged room, or -1
252
266
  * @param packet what's being sent
253
267
  */
254
268
  public measure(packet: unknown): number {
255
- return packet instanceof RawReply ? this.raw.replySize(packet) : -1
269
+ if (packet instanceof RawReply) return this.raw.replySize(packet)
270
+ if (!(packet instanceof JsonReply)) return -1
271
+
272
+ const text = this.json.writeReply(packet)
273
+ this.measured = packet
274
+ this.measuredText = text
275
+ return text.length * 3 + 1
256
276
  }
257
277
 
258
278
  /**
259
- * Writes a raw reply and says how many bytes it took, or -1
279
+ * Writes a measured reply and says how many bytes it took, or -1
260
280
  * @param packet what's being sent
261
281
  * @param target where it goes
262
282
  * @param offset where it starts in the target
263
283
  */
264
284
  public write(packet: unknown, target: Uint8Array, offset: number): number {
265
- return packet instanceof RawReply
266
- ? this.raw.writeBytes(packet, target, offset)
267
- : -1
285
+ if (packet instanceof RawReply) {
286
+ return this.raw.writeBytes(packet, target, offset)
287
+ }
288
+
289
+ const text = packet === this.measured ? this.measuredText : null
290
+ this.measured = null
291
+ this.measuredText = null
292
+ if (text === null) return -1
293
+
294
+ return this.writeText(text, target, offset)
295
+ }
296
+
297
+ /**
298
+ * Writes message text with its zero byte and says how many bytes it took
299
+ * @param text the whole message
300
+ * @param target where it goes, with room for three bytes a character
301
+ * @param offset where it starts in the target
302
+ */
303
+ private writeText(text: string, target: Uint8Array, offset: number): number {
304
+ const length = text.length
305
+
306
+ for (let index = 0; index < length; index++) {
307
+ const code = text.charCodeAt(index)
308
+ if (code > 127) {
309
+ const written = this.encoder.encodeInto(
310
+ text.substring(index),
311
+ target.subarray(offset + index),
312
+ ).written
313
+ target[offset + index + written] = 0
314
+ return index + written + 1
315
+ }
316
+
317
+ target[offset + index] = code
318
+ }
319
+
320
+ target[offset + length] = 0
321
+ return length + 1
268
322
  }
269
323
 
270
324
  /**
@@ -7,7 +7,7 @@ import {
7
7
  type HandlerContext,
8
8
  } from '@netifyjs/netify'
9
9
 
10
- import { BlueBox } from '../bluebox/gateway/index.ts'
10
+ import { BlueBox } from '../bluebox/index.ts'
11
11
 
12
12
  /**
13
13
  * What the HTTP side of a server needs
@@ -48,12 +48,9 @@ export class WebHandler extends InboundHandlerAdapter {
48
48
  * @param context the HTTP connection
49
49
  * @param message an aggregated request
50
50
  */
51
- public override async channelRead(
52
- context: HandlerContext,
53
- message: unknown,
54
- ): Promise<void> {
51
+ public override channelRead(context: HandlerContext, message: unknown): void {
55
52
  if (!(message instanceof FullHttpRequest)) {
56
- await context.fireChannelRead(message)
53
+ context.fireChannelRead(message)
57
54
  return
58
55
  }
59
56
 
@@ -61,17 +58,17 @@ export class WebHandler extends InboundHandlerAdapter {
61
58
  const path = query === -1 ? message.uri : message.uri.slice(0, query)
62
59
 
63
60
  if (path === BlueBox.path && message.method === HttpMethod.POST) {
64
- await this.tunnel(context, message)
61
+ this.tunnel(context, message)
65
62
  return
66
63
  }
67
64
 
68
65
  const policy = this.bluebox.policy
69
66
  if (path === '/crossdomain.xml' && policy !== null) {
70
- await this.reply(context, message, policy, 'text/xml')
67
+ this.reply(context, message, policy, 'text/xml')
71
68
  return
72
69
  }
73
70
 
74
- await this.missing(context, message)
71
+ this.missing(context, message)
75
72
  }
76
73
 
77
74
  /**
@@ -79,16 +76,13 @@ export class WebHandler extends InboundHandlerAdapter {
79
76
  * @param context the HTTP connection
80
77
  * @param request what's being answered
81
78
  */
82
- private async missing(
83
- context: HandlerContext,
84
- request: FullHttpRequest,
85
- ): Promise<void> {
79
+ private missing(context: HandlerContext, request: FullHttpRequest): void {
86
80
  const response = new FullHttpResponse(HttpResponseStatus.NOT_FOUND, '')
87
81
  response.setContentLength(0)
88
82
  response.setKeepAlive(request.isKeepAlive())
89
83
 
90
- await context.writeAndFlush(response)
91
- if (!request.isKeepAlive()) await context.close()
84
+ context.writeAndFlush(response)
85
+ if (!request.isKeepAlive()) void context.close()
92
86
  }
93
87
 
94
88
  /**
@@ -96,26 +90,39 @@ export class WebHandler extends InboundHandlerAdapter {
96
90
  * @param context the HTTP connection
97
91
  * @param request the client's post
98
92
  */
99
- private async tunnel(
100
- context: HandlerContext,
101
- request: FullHttpRequest,
102
- ): Promise<void> {
93
+ private tunnel(context: HandlerContext, request: FullHttpRequest): void {
103
94
  const value = WebHandler.field(
104
95
  request.content.readString(request.content.readableBytes),
105
96
  )
106
97
  if (value === null) {
107
98
  const refused = new FullHttpResponse(HttpResponseStatus.BAD_REQUEST)
108
99
  refused.setContentLength(0)
109
- await context.writeAndFlush(refused)
100
+ context.writeAndFlush(refused)
110
101
  return
111
102
  }
112
103
 
113
- const answer = await this.bluebox.handle(
104
+ const answer = this.bluebox.handle(
114
105
  value,
115
106
  context.channel.remoteAddress,
116
107
  context.channel.remotePort,
117
108
  )
118
- await this.reply(context, request, answer, 'text/plain')
109
+ if (typeof answer === 'string') {
110
+ this.reply(context, request, answer, 'text/plain')
111
+ return
112
+ }
113
+
114
+ const channel = context.channel
115
+ channel.pauseReads()
116
+ void answer.then(
117
+ (text: string): void => {
118
+ this.reply(context, request, text, 'text/plain')
119
+ channel.resumeReads()
120
+ },
121
+ (error: unknown): void => {
122
+ context.fireExceptionCaught(error)
123
+ channel.resumeReads()
124
+ },
125
+ )
119
126
  }
120
127
 
121
128
  /**
@@ -138,18 +145,18 @@ export class WebHandler extends InboundHandlerAdapter {
138
145
  * @param text the response body
139
146
  * @param type its content type
140
147
  */
141
- private async reply(
148
+ private reply(
142
149
  context: HandlerContext,
143
150
  request: FullHttpRequest,
144
151
  text: string,
145
152
  type: string,
146
- ): Promise<void> {
153
+ ): void {
147
154
  const response = new FullHttpResponse(HttpResponseStatus.OK, text)
148
155
  response.headers.set('content-type', `${type}; charset=utf-8`)
149
156
  response.setContentLength(response.content.readableBytes)
150
157
  response.setKeepAlive(request.isKeepAlive())
151
158
 
152
- await context.writeAndFlush(response)
153
- if (!request.isKeepAlive()) await context.close()
159
+ context.writeAndFlush(response)
160
+ if (!request.isKeepAlive()) void context.close()
154
161
  }
155
162
  }