@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
@@ -13,14 +13,14 @@ export class SfsObject {
13
13
  private static readonly scanned: number = 12
14
14
 
15
15
  /**
16
- * Keys in the order they were first set
16
+ * Places each entry takes up, key then type then value
17
17
  */
18
- private readonly names: string[]
18
+ private static readonly stride: number = 3
19
19
 
20
20
  /**
21
- * The value of each key, in step with the keys
21
+ * Every entry in the order it was first set, laid out flat
22
22
  */
23
- private readonly held: SfsEntry[]
23
+ private readonly slots: SfsData[]
24
24
 
25
25
  /**
26
26
  * Where each key sits once there are many, or null
@@ -31,8 +31,7 @@ export class SfsObject {
31
31
  * Starts empty
32
32
  */
33
33
  private constructor() {
34
- this.names = []
35
- this.held = []
34
+ this.slots = []
36
35
  this.index = null
37
36
  }
38
37
 
@@ -47,7 +46,7 @@ export class SfsObject {
47
46
  * How many keys it holds
48
47
  */
49
48
  public get size(): number {
50
- return this.names.length
49
+ return this.slots.length / SfsObject.stride
51
50
  }
52
51
 
53
52
  /**
@@ -63,9 +62,9 @@ export class SfsObject {
63
62
  * @param key name to look for
64
63
  */
65
64
  public isNull(key: string): boolean {
66
- const entry = this.lookup(key)
65
+ const at = this.find(key)
67
66
 
68
- return entry === undefined || entry.type === DataType.NULL
67
+ return at < 0 || this.typeOf(at) === DataType.NULL
69
68
  }
70
69
 
71
70
  /**
@@ -76,8 +75,7 @@ export class SfsObject {
76
75
  const at = this.find(key)
77
76
  if (at < 0) return false
78
77
 
79
- this.names.splice(at, 1)
80
- this.held.splice(at, 1)
78
+ this.slots.splice(at * SfsObject.stride, SfsObject.stride)
81
79
  this.index = null
82
80
 
83
81
  return true
@@ -87,16 +85,18 @@ export class SfsObject {
87
85
  * Every key that's set
88
86
  */
89
87
  public keys(): string[] {
90
- return [...this.names]
88
+ const keys: string[] = []
89
+ for (let at = 0; at < this.size; at++) keys.push(this.keyOf(at))
90
+
91
+ return keys
91
92
  }
92
93
 
93
94
  /**
94
95
  * Every key with its typed value
95
96
  */
96
97
  public *entries(): IterableIterator<[string, SfsEntry]> {
97
- for (const [at, key] of this.names.entries()) {
98
- const entry = this.held[at]
99
- if (entry !== undefined) yield [key, entry]
98
+ for (let at = 0; at < this.size; at++) {
99
+ yield [this.keyOf(at), { type: this.typeOf(at), value: this.valueOf(at) }]
100
100
  }
101
101
  }
102
102
 
@@ -105,7 +105,23 @@ export class SfsObject {
105
105
  * @param position how many keys were set before it
106
106
  */
107
107
  public keyAt(position: number): string | null {
108
- return this.names[position] ?? null
108
+ return this.within(position) ? this.keyOf(position) : null
109
+ }
110
+
111
+ /**
112
+ * The wire type at a position, or null
113
+ * @param position how many keys were set before it
114
+ */
115
+ public typeAt(position: number): DataTypeId | null {
116
+ return this.within(position) ? this.typeOf(position) : null
117
+ }
118
+
119
+ /**
120
+ * The value at a position, or null
121
+ * @param position how many keys were set before it
122
+ */
123
+ public valueAt(position: number): SfsData {
124
+ return this.within(position) ? this.valueOf(position) : null
109
125
  }
110
126
 
111
127
  /**
@@ -113,7 +129,9 @@ export class SfsObject {
113
129
  * @param position how many keys were set before it
114
130
  */
115
131
  public entryAt(position: number): SfsEntry | null {
116
- return this.held[position] ?? null
132
+ if (!this.within(position)) return null
133
+
134
+ return { type: this.typeOf(position), value: this.valueOf(position) }
117
135
  }
118
136
 
119
137
  /**
@@ -121,7 +139,7 @@ export class SfsObject {
121
139
  * @param key name to look for
122
140
  */
123
141
  public entry(key: string): SfsEntry | null {
124
- return this.lookup(key) ?? null
142
+ return this.entryAt(this.find(key))
125
143
  }
126
144
 
127
145
  /**
@@ -131,15 +149,16 @@ export class SfsObject {
131
149
  * @param value what to store
132
150
  */
133
151
  public put(key: string, type: DataTypeId, value: SfsData): this {
152
+ const slots = this.slots
134
153
  const at = this.find(key)
135
154
  if (at >= 0) {
136
- this.held[at] = { type, value }
155
+ slots[at * SfsObject.stride + 1] = type
156
+ slots[at * SfsObject.stride + 2] = value
137
157
  return this
138
158
  }
139
159
 
140
- this.index?.set(key, this.names.length)
141
- this.names.push(key)
142
- this.held.push({ type, value })
160
+ this.index?.set(key, this.size)
161
+ slots.push(key, type, value)
143
162
 
144
163
  return this
145
164
  }
@@ -337,7 +356,7 @@ export class SfsObject {
337
356
  * @param key name to look for
338
357
  */
339
358
  public getBool(key: string): boolean | null {
340
- const value = this.lookup(key)?.value
359
+ const value = this.value(key)
341
360
 
342
361
  return typeof value === 'boolean' ? value : null
343
362
  }
@@ -347,7 +366,7 @@ export class SfsObject {
347
366
  * @param key name to look for
348
367
  */
349
368
  public getNumber(key: string): number | null {
350
- const value = this.lookup(key)?.value
369
+ const value = this.value(key)
351
370
  if (typeof value === 'number') return value
352
371
 
353
372
  return typeof value === 'bigint' ? Number(value) : null
@@ -392,7 +411,7 @@ export class SfsObject {
392
411
  * @param key name to look for
393
412
  */
394
413
  public getLong(key: string): bigint | null {
395
- const value = this.lookup(key)?.value
414
+ const value = this.value(key)
396
415
  if (typeof value === 'bigint') return value
397
416
 
398
417
  return typeof value === 'number' && Number.isInteger(value)
@@ -421,7 +440,7 @@ export class SfsObject {
421
440
  * @param key name to look for
422
441
  */
423
442
  public getUtfString(key: string): string | null {
424
- const value = this.lookup(key)?.value
443
+ const value = this.value(key)
425
444
 
426
445
  return typeof value === 'string' ? value : null
427
446
  }
@@ -439,11 +458,10 @@ export class SfsObject {
439
458
  * @param key name to look for
440
459
  */
441
460
  public getBoolArray(key: string): readonly boolean[] | null {
442
- const entry = this.lookup(key)
443
-
444
- return entry?.type === DataType.BOOL_ARRAY
445
- ? (entry.value as readonly boolean[])
446
- : null
461
+ return (
462
+ (this.typed(key, DataType.BOOL_ARRAY) as
463
+ readonly boolean[] | undefined) ?? null
464
+ )
447
465
  }
448
466
 
449
467
  /**
@@ -451,11 +469,9 @@ export class SfsObject {
451
469
  * @param key name to look for
452
470
  */
453
471
  public getByteArray(key: string): Uint8Array | null {
454
- const entry = this.lookup(key)
455
-
456
- return entry?.type === DataType.BYTE_ARRAY
457
- ? (entry.value as Uint8Array)
458
- : null
472
+ return (
473
+ (this.typed(key, DataType.BYTE_ARRAY) as Uint8Array | undefined) ?? null
474
+ )
459
475
  }
460
476
 
461
477
  /**
@@ -463,11 +479,10 @@ export class SfsObject {
463
479
  * @param key name to look for
464
480
  */
465
481
  public getShortArray(key: string): readonly number[] | null {
466
- const entry = this.lookup(key)
467
-
468
- return entry?.type === DataType.SHORT_ARRAY
469
- ? (entry.value as readonly number[])
470
- : null
482
+ return (
483
+ (this.typed(key, DataType.SHORT_ARRAY) as
484
+ readonly number[] | undefined) ?? null
485
+ )
471
486
  }
472
487
 
473
488
  /**
@@ -475,11 +490,10 @@ export class SfsObject {
475
490
  * @param key name to look for
476
491
  */
477
492
  public getIntArray(key: string): readonly number[] | null {
478
- const entry = this.lookup(key)
479
-
480
- return entry?.type === DataType.INT_ARRAY
481
- ? (entry.value as readonly number[])
482
- : null
493
+ return (
494
+ (this.typed(key, DataType.INT_ARRAY) as readonly number[] | undefined) ??
495
+ null
496
+ )
483
497
  }
484
498
 
485
499
  /**
@@ -487,11 +501,10 @@ export class SfsObject {
487
501
  * @param key name to look for
488
502
  */
489
503
  public getLongArray(key: string): readonly bigint[] | null {
490
- const entry = this.lookup(key)
491
-
492
- return entry?.type === DataType.LONG_ARRAY
493
- ? (entry.value as readonly bigint[])
494
- : null
504
+ return (
505
+ (this.typed(key, DataType.LONG_ARRAY) as readonly bigint[] | undefined) ??
506
+ null
507
+ )
495
508
  }
496
509
 
497
510
  /**
@@ -499,11 +512,10 @@ export class SfsObject {
499
512
  * @param key name to look for
500
513
  */
501
514
  public getFloatArray(key: string): readonly number[] | null {
502
- const entry = this.lookup(key)
503
-
504
- return entry?.type === DataType.FLOAT_ARRAY
505
- ? (entry.value as readonly number[])
506
- : null
515
+ return (
516
+ (this.typed(key, DataType.FLOAT_ARRAY) as
517
+ readonly number[] | undefined) ?? null
518
+ )
507
519
  }
508
520
 
509
521
  /**
@@ -511,11 +523,10 @@ export class SfsObject {
511
523
  * @param key name to look for
512
524
  */
513
525
  public getDoubleArray(key: string): readonly number[] | null {
514
- const entry = this.lookup(key)
515
-
516
- return entry?.type === DataType.DOUBLE_ARRAY
517
- ? (entry.value as readonly number[])
518
- : null
526
+ return (
527
+ (this.typed(key, DataType.DOUBLE_ARRAY) as
528
+ readonly number[] | undefined) ?? null
529
+ )
519
530
  }
520
531
 
521
532
  /**
@@ -523,11 +534,10 @@ export class SfsObject {
523
534
  * @param key name to look for
524
535
  */
525
536
  public getUtfStringArray(key: string): readonly string[] | null {
526
- const entry = this.lookup(key)
527
-
528
- return entry?.type === DataType.UTF_STRING_ARRAY
529
- ? (entry.value as readonly string[])
530
- : null
537
+ return (
538
+ (this.typed(key, DataType.UTF_STRING_ARRAY) as
539
+ readonly string[] | undefined) ?? null
540
+ )
531
541
  }
532
542
 
533
543
  /**
@@ -535,9 +545,7 @@ export class SfsObject {
535
545
  * @param key name to look for
536
546
  */
537
547
  public getSfsArray(key: string): SfsArray | null {
538
- const entry = this.lookup(key)
539
-
540
- return entry?.type === DataType.SFS_ARRAY ? (entry.value as SfsArray) : null
548
+ return (this.typed(key, DataType.SFS_ARRAY) as SfsArray | undefined) ?? null
541
549
  }
542
550
 
543
551
  /**
@@ -545,11 +553,12 @@ export class SfsObject {
545
553
  * @param key name to look for
546
554
  */
547
555
  public getSfsObject(key: string): SfsObject | null {
548
- const entry = this.lookup(key)
549
- if (entry === undefined) return null
556
+ const at = this.find(key)
557
+ if (at < 0) return null
550
558
 
551
- return entry.type === DataType.SFS_OBJECT || entry.type === DataType.CLASS
552
- ? (entry.value as SfsObject)
559
+ const type = this.typeOf(at)
560
+ return type === DataType.SFS_OBJECT || type === DataType.CLASS
561
+ ? (this.valueOf(at) as SfsObject)
553
562
  : null
554
563
  }
555
564
 
@@ -562,11 +571,60 @@ export class SfsObject {
562
571
  }
563
572
 
564
573
  /**
565
- * The typed value under a key, or nothing
574
+ * The value under a key, or nothing
575
+ * @param key name to look for
576
+ */
577
+ private value(key: string): SfsData | undefined {
578
+ const at = this.find(key)
579
+
580
+ return at < 0 ? undefined : this.valueOf(at)
581
+ }
582
+
583
+ /**
584
+ * The value under a key if it has the wire type asked for, or nothing
566
585
  * @param key name to look for
586
+ * @param type wire type it has to be
587
+ */
588
+ private typed(key: string, type: DataTypeId): SfsData | undefined {
589
+ const at = this.find(key)
590
+
591
+ return at >= 0 && this.typeOf(at) === type ? this.valueOf(at) : undefined
592
+ }
593
+
594
+ /**
595
+ * Whether a position has an entry
596
+ * @param position how many keys were set before it
597
+ */
598
+ private within(position: number): boolean {
599
+ return (
600
+ (position | 0) === position &&
601
+ position >= 0 &&
602
+ position * SfsObject.stride < this.slots.length
603
+ )
604
+ }
605
+
606
+ /**
607
+ * Key at a position in range
608
+ * @param position how many keys were set before it
609
+ */
610
+ private keyOf(position: number): string {
611
+ return this.slots[position * SfsObject.stride] as string
612
+ }
613
+
614
+ /**
615
+ * Wire type at a position in range
616
+ * @param position how many keys were set before it
617
+ */
618
+ private typeOf(position: number): DataTypeId {
619
+ return this.slots[position * SfsObject.stride + 1] as DataTypeId
620
+ }
621
+
622
+ /**
623
+ * Value at a position in range
624
+ * @param position how many keys were set before it
567
625
  */
568
- private lookup(key: string): SfsEntry | undefined {
569
- return this.held[this.find(key)]
626
+ private valueOf(position: number): SfsData {
627
+ return this.slots[position * SfsObject.stride + 2] ?? null
570
628
  }
571
629
 
572
630
  /**
@@ -574,10 +632,16 @@ export class SfsObject {
574
632
  * @param key name to look for
575
633
  */
576
634
  private find(key: string): number {
577
- const names = this.names
578
- if (names.length <= SfsObject.scanned) return names.indexOf(key)
635
+ const slots = this.slots
636
+ if (slots.length > SfsObject.scanned * SfsObject.stride) {
637
+ return this.indexed().get(key) ?? -1
638
+ }
639
+
640
+ for (let at = 0; at < slots.length; at += SfsObject.stride) {
641
+ if (slots[at] === key) return at / SfsObject.stride
642
+ }
579
643
 
580
- return this.indexed().get(key) ?? -1
644
+ return -1
581
645
  }
582
646
 
583
647
  /**
@@ -587,9 +651,7 @@ export class SfsObject {
587
651
  if (this.index !== null) return this.index
588
652
 
589
653
  const index = new Map<string, number>()
590
- for (const [at, key] of this.names.entries()) {
591
- index.set(key, at)
592
- }
654
+ for (let at = 0; at < this.size; at++) index.set(this.keyOf(at), at)
593
655
 
594
656
  this.index = index
595
657
  return index
@@ -2,7 +2,7 @@ import { ByteBuf, DecoderError, EncoderError } from '@netifyjs/netify'
2
2
 
3
3
  import { NameTable } from '../../../../names/index.ts'
4
4
  import { SfsArray } from '../array/index.ts'
5
- import type { SfsData, SfsEntry } from '../entry/index.ts'
5
+ import type { SfsData } from '../entry/index.ts'
6
6
  import { SfsObject } from '../object/index.ts'
7
7
  import { DataType, type DataTypeId } from '../type/index.ts'
8
8
 
@@ -26,16 +26,27 @@ export class SfsSerializer {
26
26
  */
27
27
  private static readonly lastType: number = DataType.TEXT
28
28
 
29
+ /**
30
+ * Most objects and arrays read inside each other unless told otherwise
31
+ */
32
+ public static readonly defaultMaxDepth: number = 32
33
+
29
34
  /**
30
35
  * Short strings read before
31
36
  */
32
37
  private readonly names: NameTable
33
38
 
34
39
  /**
35
- * Starts with no strings kept
40
+ * Most objects and arrays read inside each other
41
+ */
42
+ private readonly maxDepth: number
43
+
44
+ /**
45
+ * @param maxDepth most objects and arrays read inside each other
36
46
  */
37
- public constructor() {
47
+ public constructor(maxDepth: number = SfsSerializer.defaultMaxDepth) {
38
48
  this.names = new NameTable()
49
+ this.maxDepth = maxDepth
39
50
  }
40
51
 
41
52
  /**
@@ -62,7 +73,7 @@ export class SfsSerializer {
62
73
  throw new DecoderError('Object is expected')
63
74
  }
64
75
 
65
- return this.readObject(buffer)
76
+ return this.readObject(buffer, 1)
66
77
  }
67
78
 
68
79
  /**
@@ -78,11 +89,11 @@ export class SfsSerializer {
78
89
  buffer.writeUint8(DataType.SFS_OBJECT).writeInt16(object.size)
79
90
  for (let at = 0; at < object.size; at++) {
80
91
  const key = object.keyAt(at)
81
- const entry = object.entryAt(at)
82
- if (key === null || entry === null) continue
92
+ const type = object.typeAt(at)
93
+ if (key === null || type === null) continue
83
94
 
84
95
  this.writeString(key, buffer, SfsSerializer.keyLimit)
85
- this.writeEntry(entry, buffer)
96
+ this.writeEntry(type, object.valueAt(at), buffer)
86
97
  }
87
98
  }
88
99
 
@@ -97,20 +108,20 @@ export class SfsSerializer {
97
108
  }
98
109
 
99
110
  buffer.writeUint8(DataType.SFS_ARRAY).writeInt16(array.size)
100
- for (const entry of array.entries()) {
101
- this.writeEntry(entry, buffer)
111
+ for (let at = 0; at < array.size; at++) {
112
+ const type = array.typeAt(at)
113
+ if (type !== null) this.writeEntry(type, array.valueAt(at), buffer)
102
114
  }
103
115
  }
104
116
 
105
117
  /**
106
118
  * Writes one value with its type byte
107
- * @param entry the value and its wire type
119
+ * @param type its wire id
120
+ * @param value what to write
108
121
  * @param buffer where it goes
109
122
  */
110
- private writeEntry(entry: SfsEntry, buffer: ByteBuf): void {
111
- const value = entry.value
112
-
113
- switch (entry.type) {
123
+ private writeEntry(type: DataTypeId, value: SfsData, buffer: ByteBuf): void {
124
+ switch (type) {
114
125
  case DataType.SFS_OBJECT:
115
126
  case DataType.CLASS:
116
127
  this.writeObject(value as SfsObject, buffer)
@@ -119,8 +130,8 @@ export class SfsSerializer {
119
130
  this.writeArray(value as SfsArray, buffer)
120
131
  return
121
132
  default:
122
- buffer.writeUint8(entry.type)
123
- this.writePlain(entry.type, value, buffer)
133
+ buffer.writeUint8(type)
134
+ this.writePlain(type, value, buffer)
124
135
  }
125
136
  }
126
137
 
@@ -243,8 +254,11 @@ export class SfsSerializer {
243
254
  /**
244
255
  * Reads an object after its type byte
245
256
  * @param buffer where it comes from
257
+ * @param depth how deep it sits, 1 for the outermost
246
258
  */
247
- private readObject(buffer: ByteBuf): SfsObject {
259
+ private readObject(buffer: ByteBuf, depth: number): SfsObject {
260
+ if (depth > this.maxDepth) throw new DecoderError('Data is nested too deep')
261
+
248
262
  const object = SfsObject.of()
249
263
 
250
264
  const count = this.count(buffer.readInt16())
@@ -256,7 +270,7 @@ export class SfsSerializer {
256
270
 
257
271
  const key = this.names.read(size, buffer)
258
272
  const type = this.type(buffer.readUint8())
259
- object.put(key, type, this.readValue(type, buffer))
273
+ object.put(key, type, this.readValue(type, buffer, depth))
260
274
  }
261
275
 
262
276
  return object
@@ -265,14 +279,17 @@ export class SfsSerializer {
265
279
  /**
266
280
  * Reads an array after its type byte
267
281
  * @param buffer where it comes from
282
+ * @param depth how deep it sits
268
283
  */
269
- private readArray(buffer: ByteBuf): SfsArray {
284
+ private readArray(buffer: ByteBuf, depth: number): SfsArray {
285
+ if (depth > this.maxDepth) throw new DecoderError('Data is nested too deep')
286
+
270
287
  const array = SfsArray.of()
271
288
 
272
289
  const count = this.count(buffer.readInt16())
273
290
  for (let index = 0; index < count; index++) {
274
291
  const type = this.type(buffer.readUint8())
275
- array.add(type, this.readValue(type, buffer))
292
+ array.add(type, this.readValue(type, buffer, depth))
276
293
  }
277
294
 
278
295
  return array
@@ -282,8 +299,9 @@ export class SfsSerializer {
282
299
  * Reads one value after its type byte
283
300
  * @param type its wire id
284
301
  * @param buffer where it comes from
302
+ * @param depth how deep the object or array holding it sits
285
303
  */
286
- private readValue(type: DataTypeId, buffer: ByteBuf): SfsData {
304
+ private readValue(type: DataTypeId, buffer: ByteBuf, depth: number): SfsData {
287
305
  switch (type) {
288
306
  case DataType.NULL:
289
307
  return null
@@ -308,10 +326,10 @@ export class SfsSerializer {
308
326
  case DataType.BYTE_ARRAY:
309
327
  return buffer.readBytes(this.count(buffer.readInt32()))
310
328
  case DataType.SFS_ARRAY:
311
- return this.readArray(buffer)
329
+ return this.readArray(buffer, depth + 1)
312
330
  case DataType.SFS_OBJECT:
313
331
  case DataType.CLASS:
314
- return this.readObject(buffer)
332
+ return this.readObject(buffer, depth + 1)
315
333
  default:
316
334
  return this.readList(type, buffer)
317
335
  }
@@ -1,4 +1,8 @@
1
- import { InboundHandlerAdapter, type HandlerContext } from '@netifyjs/netify'
1
+ import {
2
+ InboundHandlerAdapter,
3
+ type HandlerContext,
4
+ type Outcome,
5
+ } from '@netifyjs/netify'
2
6
 
3
7
  import { Session } from '../../../../dispatch/session/index.ts'
4
8
  import type { Member } from '../../member/index.ts'
@@ -33,24 +37,22 @@ export class FloodGate extends InboundHandlerAdapter {
33
37
  public override channelRead(
34
38
  context: HandlerContext,
35
39
  message: unknown,
36
- ): void | Promise<void> {
40
+ ): Outcome {
37
41
  const member = Session.of<Member>(context.channel).user
38
42
  const zone = member === null ? null : this.zones.zoneOf(member)
39
43
  const filter = zone?.shared ?? null
40
- if (member === null || zone === null || filter === null) {
41
- return context.fireChannelRead(message)
42
- }
43
44
 
44
- const read = context.fireChannelRead(message)
45
+ context.fireChannelRead(message)
46
+ if (member === null || zone === null || filter === null) return
47
+
45
48
  const request = filter.take()
49
+ if (request === null) return
46
50
 
47
- return request === null
48
- ? read
49
- : this.count(context, message, member, zone, filter, request)
51
+ return this.count(context, message, member, zone, filter, request)
50
52
  }
51
53
 
52
54
  /**
53
- * Counts a request, then hands its packet to the handler again
55
+ * Counts a request with later reads held back, then hands its packet to the handler again
54
56
  * @param context the connection
55
57
  * @param message the packet that was held back
56
58
  * @param member who sent it
@@ -66,13 +68,17 @@ export class FloodGate extends InboundHandlerAdapter {
66
68
  filter: SharedFloodFilter,
67
69
  request: string,
68
70
  ): Promise<void> {
69
- const flooding = await zone.crowded(member, request)
71
+ const channel = context.channel
72
+ channel.pauseReads()
70
73
 
71
- filter.admit(member.counters, request, flooding)
72
74
  try {
73
- await context.fireChannelRead(message)
75
+ const flooding = await zone.crowded(member, request)
76
+
77
+ filter.admit(member.counters, request, flooding)
78
+ context.fireChannelRead(message)
74
79
  } finally {
75
80
  filter.revoke()
81
+ channel.resumeReads()
76
82
  }
77
83
  }
78
84
  }
@@ -68,9 +68,10 @@ export class InitBuddyListResponseFormat implements X2Format<InitBuddyListRespon
68
68
  const buddies: Buddy[] = []
69
69
  if (list === null) return buddies
70
70
 
71
- for (const entry of list.entries()) {
72
- if (entry.value instanceof SfsArray) {
73
- buddies.push(Buddy.fromArray(entry.value))
71
+ for (let at = 0; at < list.size; at++) {
72
+ const value = list.valueAt(at)
73
+ if (value instanceof SfsArray) {
74
+ buddies.push(Buddy.fromArray(value))
74
75
  }
75
76
  }
76
77
 
@@ -50,9 +50,10 @@ export class FindRoomsResponseFormat implements X2Format<FindRoomsResponse> {
50
50
  const rooms: Room[] = []
51
51
  if (list === null) return rooms
52
52
 
53
- for (const entry of list.entries()) {
54
- if (entry.value instanceof SfsArray) {
55
- rooms.push(Room.fromArray(entry.value))
53
+ for (let at = 0; at < list.size; at++) {
54
+ const value = list.valueAt(at)
55
+ if (value instanceof SfsArray) {
56
+ rooms.push(Room.fromArray(value))
56
57
  }
57
58
  }
58
59