@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
@@ -1,4 +1,5 @@
1
- import type { CounterStore } from '../../pro/counters/store/index.ts'
1
+ import type { CounterStore } from '../../common/counters/store/index.ts'
2
+ import { type Sentence, Strikes } from '../../common/strikes/index.ts'
2
3
  import type { SfsObject } from '../data/object/index.ts'
3
4
  import type { Hall } from '../hall/index.ts'
4
5
  import type { Member } from '../member/index.ts'
@@ -25,11 +26,6 @@ type Kicks = {
25
26
  readonly until: number
26
27
  }
27
28
 
28
- /**
29
- * What the words filter does to someone it caught
30
- */
31
- type Sentence = 'warn' | 'kick' | 'ban'
32
-
33
29
  /**
34
30
  * Messages between a zone's users
35
31
  * @class
@@ -327,21 +323,15 @@ export class Chat {
327
323
  words: WordsFilter,
328
324
  counters: CounterStore,
329
325
  ): Promise<Sentence> {
330
- const offender = this.zone.offender(member)
331
326
  const window = (words.kicksBeforeBanMinutes ?? words.banMinutes) * 60_000
327
+ const strikes = new Strikes(
328
+ 'swearing',
329
+ words.warningsBeforeKick,
330
+ words.kicksBeforeBan,
331
+ window,
332
+ )
332
333
 
333
- const warnings = offender.counter('swearing:warnings')
334
- const warned = await counters.hit(warnings, window)
335
- if (warned <= words.warningsBeforeKick) return 'warn'
336
-
337
- await counters.clear(warnings)
338
-
339
- const kicks = offender.counter('swearing:kicks')
340
- const kicked = await counters.hit(kicks, window)
341
- if (kicked <= words.kicksBeforeBan) return 'kick'
342
-
343
- await counters.clear(kicks)
344
- return 'ban'
334
+ return strikes.count(counters, this.zone.offender(member))
345
335
  }
346
336
 
347
337
  /**
@@ -32,7 +32,7 @@ export class SmartFox2XCodec extends PacketCodec<object> {
32
32
  /**
33
33
  * What the codec speaks
34
34
  */
35
- private readonly protocol: SmartFox2X
35
+ protected override readonly protocol: SmartFox2X
36
36
 
37
37
  /**
38
38
  * Bytes of an oversized message still to throw away
@@ -71,53 +71,38 @@ export class SmartFox2XCodec extends PacketCodec<object> {
71
71
  * @param context the connection
72
72
  * @param message a packet
73
73
  */
74
- public override write(
75
- context: HandlerContext,
76
- message: unknown,
77
- ): Promise<void> {
78
- let frame: ByteBuf | null
79
- try {
80
- frame = this.frame(context, message)
81
- } catch (cause) {
82
- return Promise.reject(new EncoderError('Encoding failed', { cause }))
74
+ public override write(context: HandlerContext, message: unknown): void {
75
+ if (message instanceof ByteBuf || message instanceof Uint8Array) {
76
+ context.write(message)
77
+ return
83
78
  }
84
79
 
85
- return context.write(frame ?? message)
86
- }
87
-
88
- /**
89
- * Frames a packet with the connection's key, without a promise
90
- * @param context the connection
91
- * @param message a packet
92
- */
93
- public override writeSync(
94
- context: HandlerContext,
95
- message: unknown,
96
- ): boolean {
97
- if (message instanceof ByteBuf) return PacketCodec.pass(context, message)
98
-
80
+ const direct = context.writesToSocket
99
81
  let frame: ByteBuf | null
100
82
  try {
101
83
  frame = this.protocol.pack(
102
84
  message,
103
85
  SmartFox2XCodec.cipherOf(context.channel),
104
- true,
86
+ direct,
105
87
  )
106
88
  } catch (cause) {
107
89
  throw new EncoderError('Encoding failed', { cause })
108
90
  }
109
91
 
110
- if (frame === null) return false
111
- if (!PacketCodec.pass(context, frame)) context.writeSync(frame.copy())
112
-
113
- return true
92
+ if (frame === null) context.write(message)
93
+ else if (!direct) context.write(frame)
94
+ else if (!this.place(context, frame)) {
95
+ context.write(frame.readBytes(frame.readableBytes))
96
+ }
114
97
  }
115
98
 
116
99
  /**
117
100
  * Puts the protocol's gate behind this codec
118
101
  * @param context the connection
119
102
  */
120
- public handlerAdded(context: HandlerContext): void {
103
+ public override handlerAdded(context: HandlerContext): void {
104
+ super.handlerAdded(context)
105
+
121
106
  const gate = this.protocol.gate
122
107
  if (gate === null) return
123
108
 
@@ -163,9 +148,7 @@ export class SmartFox2XCodec extends PacketCodec<object> {
163
148
 
164
149
  input.skipBytes(prefix)
165
150
  this.dropping = size
166
- void context.fireExceptionCaught(
167
- new DecoderError('Message is too large'),
168
- )
151
+ context.fireExceptionCaught(new DecoderError('Message is too large'))
169
152
  continue
170
153
  }
171
154
 
@@ -188,14 +171,25 @@ export class SmartFox2XCodec extends PacketCodec<object> {
188
171
  }
189
172
 
190
173
  /**
191
- * A packet as a frame for this connection, or null
192
- * @param context the connection
193
- * @param message a packet
174
+ * Copies a frame into the channel's staging slab, false if it won't fit
175
+ * @param context next to the socket
176
+ * @param frame a whole frame
194
177
  */
195
- private frame(context: HandlerContext, message: unknown): ByteBuf | null {
196
- return this.protocol.pack(
197
- message,
198
- SmartFox2XCodec.cipherOf(context.channel),
178
+ private place(context: HandlerContext, frame: ByteBuf): boolean {
179
+ const channel = context.channel
180
+ const size = frame.readableBytes
181
+ const offset = channel.reserve(size)
182
+ if (offset < 0) return false
183
+
184
+ ByteBuf.copyBytes(
185
+ frame.array,
186
+ frame.arrayOffset + frame.readerIndex,
187
+ channel.stagingBuffer,
188
+ offset,
189
+ size,
199
190
  )
191
+ channel.commit()
192
+
193
+ return true
200
194
  }
201
195
  }
@@ -1,6 +1,6 @@
1
1
  import type { Logger } from '@netifyjs/netify'
2
2
 
3
- import type { CounterStore } from '../../pro/counters/store/index.ts'
3
+ import type { CounterStore } from '../../common/counters/store/index.ts'
4
4
  import type { BanStore } from '../ban/store/index.ts'
5
5
  import type { PrivilegeId } from '../entity/privilege/index.ts'
6
6
  import type {
@@ -311,6 +311,11 @@ export type ZoneConfig = {
311
311
  */
312
312
  readonly maxJoinedRooms?: number
313
313
 
314
+ /**
315
+ * Invitations one user may have waiting for an answer, 20 by default
316
+ */
317
+ readonly maxInvitations?: number
318
+
314
319
  /**
315
320
  * Every group clients may subscribe to
316
321
  */
@@ -8,14 +8,20 @@ import { DataType, type DataTypeId } from '../type/index.ts'
8
8
  */
9
9
  export class SfsArray {
10
10
  /**
11
- * Values by position
11
+ * Wire type of each value
12
12
  */
13
- private readonly values: SfsEntry[]
13
+ private readonly types: DataTypeId[]
14
+
15
+ /**
16
+ * Values by position, in step with the types
17
+ */
18
+ private readonly values: SfsData[]
14
19
 
15
20
  /**
16
21
  * Starts empty
17
22
  */
18
23
  private constructor() {
24
+ this.types = []
19
25
  this.values = []
20
26
  }
21
27
 
@@ -30,7 +36,7 @@ export class SfsArray {
30
36
  * How many values it holds
31
37
  */
32
38
  public get size(): number {
33
- return this.values.length
39
+ return this.types.length
34
40
  }
35
41
 
36
42
  /**
@@ -38,16 +44,35 @@ export class SfsArray {
38
44
  * @param index position to look at
39
45
  */
40
46
  public isNull(index: number): boolean {
41
- const entry = this.values[index]
47
+ const type = this.types[index]
42
48
 
43
- return entry === undefined || entry.type === DataType.NULL
49
+ return type === undefined || type === DataType.NULL
44
50
  }
45
51
 
46
52
  /**
47
53
  * Every value with its wire type, in order
48
54
  */
49
- public entries(): IterableIterator<SfsEntry> {
50
- return this.values.values()
55
+ public *entries(): IterableIterator<SfsEntry> {
56
+ const types = this.types
57
+ for (let at = 0; at < types.length; at++) {
58
+ yield { type: types[at] ?? DataType.NULL, value: this.values[at] ?? null }
59
+ }
60
+ }
61
+
62
+ /**
63
+ * The wire type at a position, or null
64
+ * @param index position to look at
65
+ */
66
+ public typeAt(index: number): DataTypeId | null {
67
+ return this.types[index] ?? null
68
+ }
69
+
70
+ /**
71
+ * The value at a position, or null
72
+ * @param index position to look at
73
+ */
74
+ public valueAt(index: number): SfsData {
75
+ return this.values[index] ?? null
51
76
  }
52
77
 
53
78
  /**
@@ -55,7 +80,10 @@ export class SfsArray {
55
80
  * @param index position to look at
56
81
  */
57
82
  public entry(index: number): SfsEntry | null {
58
- return this.values[index] ?? null
83
+ const type = this.types[index]
84
+ if (type === undefined) return null
85
+
86
+ return { type, value: this.values[index] ?? null }
59
87
  }
60
88
 
61
89
  /**
@@ -64,7 +92,8 @@ export class SfsArray {
64
92
  * @param value what to store
65
93
  */
66
94
  public add(type: DataTypeId, value: SfsData): this {
67
- this.values.push({ type, value })
95
+ this.types.push(type)
96
+ this.values.push(value)
68
97
 
69
98
  return this
70
99
  }
@@ -241,7 +270,7 @@ export class SfsArray {
241
270
  * @param index position to look at
242
271
  */
243
272
  public getBool(index: number): boolean | null {
244
- const value = this.values[index]?.value
273
+ const value = this.value(index)
245
274
 
246
275
  return typeof value === 'boolean' ? value : null
247
276
  }
@@ -251,7 +280,7 @@ export class SfsArray {
251
280
  * @param index position to look at
252
281
  */
253
282
  public getNumber(index: number): number | null {
254
- const value = this.values[index]?.value
283
+ const value = this.value(index)
255
284
  if (typeof value === 'number') return value
256
285
 
257
286
  return typeof value === 'bigint' ? Number(value) : null
@@ -296,7 +325,7 @@ export class SfsArray {
296
325
  * @param index position to look at
297
326
  */
298
327
  public getLong(index: number): bigint | null {
299
- const value = this.values[index]?.value
328
+ const value = this.value(index)
300
329
  if (typeof value === 'bigint') return value
301
330
 
302
331
  return typeof value === 'number' && Number.isInteger(value)
@@ -325,7 +354,7 @@ export class SfsArray {
325
354
  * @param index position to look at
326
355
  */
327
356
  public getUtfString(index: number): string | null {
328
- const value = this.values[index]?.value
357
+ const value = this.value(index)
329
358
 
330
359
  return typeof value === 'string' ? value : null
331
360
  }
@@ -343,11 +372,10 @@ export class SfsArray {
343
372
  * @param index position to look at
344
373
  */
345
374
  public getBoolArray(index: number): readonly boolean[] | null {
346
- const entry = this.values[index]
347
-
348
- return entry?.type === DataType.BOOL_ARRAY
349
- ? (entry.value as readonly boolean[])
350
- : null
375
+ return (
376
+ (this.typed(index, DataType.BOOL_ARRAY) as
377
+ readonly boolean[] | undefined) ?? null
378
+ )
351
379
  }
352
380
 
353
381
  /**
@@ -355,11 +383,9 @@ export class SfsArray {
355
383
  * @param index position to look at
356
384
  */
357
385
  public getByteArray(index: number): Uint8Array | null {
358
- const entry = this.values[index]
359
-
360
- return entry?.type === DataType.BYTE_ARRAY
361
- ? (entry.value as Uint8Array)
362
- : null
386
+ return (
387
+ (this.typed(index, DataType.BYTE_ARRAY) as Uint8Array | undefined) ?? null
388
+ )
363
389
  }
364
390
 
365
391
  /**
@@ -367,11 +393,10 @@ export class SfsArray {
367
393
  * @param index position to look at
368
394
  */
369
395
  public getShortArray(index: number): readonly number[] | null {
370
- const entry = this.values[index]
371
-
372
- return entry?.type === DataType.SHORT_ARRAY
373
- ? (entry.value as readonly number[])
374
- : null
396
+ return (
397
+ (this.typed(index, DataType.SHORT_ARRAY) as
398
+ readonly number[] | undefined) ?? null
399
+ )
375
400
  }
376
401
 
377
402
  /**
@@ -379,11 +404,10 @@ export class SfsArray {
379
404
  * @param index position to look at
380
405
  */
381
406
  public getIntArray(index: number): readonly number[] | null {
382
- const entry = this.values[index]
383
-
384
- return entry?.type === DataType.INT_ARRAY
385
- ? (entry.value as readonly number[])
386
- : null
407
+ return (
408
+ (this.typed(index, DataType.INT_ARRAY) as
409
+ readonly number[] | undefined) ?? null
410
+ )
387
411
  }
388
412
 
389
413
  /**
@@ -391,11 +415,10 @@ export class SfsArray {
391
415
  * @param index position to look at
392
416
  */
393
417
  public getLongArray(index: number): readonly bigint[] | null {
394
- const entry = this.values[index]
395
-
396
- return entry?.type === DataType.LONG_ARRAY
397
- ? (entry.value as readonly bigint[])
398
- : null
418
+ return (
419
+ (this.typed(index, DataType.LONG_ARRAY) as
420
+ readonly bigint[] | undefined) ?? null
421
+ )
399
422
  }
400
423
 
401
424
  /**
@@ -403,11 +426,10 @@ export class SfsArray {
403
426
  * @param index position to look at
404
427
  */
405
428
  public getFloatArray(index: number): readonly number[] | null {
406
- const entry = this.values[index]
407
-
408
- return entry?.type === DataType.FLOAT_ARRAY
409
- ? (entry.value as readonly number[])
410
- : null
429
+ return (
430
+ (this.typed(index, DataType.FLOAT_ARRAY) as
431
+ readonly number[] | undefined) ?? null
432
+ )
411
433
  }
412
434
 
413
435
  /**
@@ -415,11 +437,10 @@ export class SfsArray {
415
437
  * @param index position to look at
416
438
  */
417
439
  public getDoubleArray(index: number): readonly number[] | null {
418
- const entry = this.values[index]
419
-
420
- return entry?.type === DataType.DOUBLE_ARRAY
421
- ? (entry.value as readonly number[])
422
- : null
440
+ return (
441
+ (this.typed(index, DataType.DOUBLE_ARRAY) as
442
+ readonly number[] | undefined) ?? null
443
+ )
423
444
  }
424
445
 
425
446
  /**
@@ -427,11 +448,10 @@ export class SfsArray {
427
448
  * @param index position to look at
428
449
  */
429
450
  public getUtfStringArray(index: number): readonly string[] | null {
430
- const entry = this.values[index]
431
-
432
- return entry?.type === DataType.UTF_STRING_ARRAY
433
- ? (entry.value as readonly string[])
434
- : null
451
+ return (
452
+ (this.typed(index, DataType.UTF_STRING_ARRAY) as
453
+ readonly string[] | undefined) ?? null
454
+ )
435
455
  }
436
456
 
437
457
  /**
@@ -439,9 +459,9 @@ export class SfsArray {
439
459
  * @param index position to look at
440
460
  */
441
461
  public getSfsArray(index: number): SfsArray | null {
442
- const entry = this.values[index]
443
-
444
- return entry?.type === DataType.SFS_ARRAY ? (entry.value as SfsArray) : null
462
+ return (
463
+ (this.typed(index, DataType.SFS_ARRAY) as SfsArray | undefined) ?? null
464
+ )
445
465
  }
446
466
 
447
467
  /**
@@ -449,11 +469,10 @@ export class SfsArray {
449
469
  * @param index position to look at
450
470
  */
451
471
  public getSfsObject(index: number): SfsObject | null {
452
- const entry = this.values[index]
453
- if (entry === undefined) return null
472
+ const type = this.types[index]
454
473
 
455
- return entry.type === DataType.SFS_OBJECT || entry.type === DataType.CLASS
456
- ? (entry.value as SfsObject)
474
+ return type === DataType.SFS_OBJECT || type === DataType.CLASS
475
+ ? (this.values[index] as SfsObject)
457
476
  : null
458
477
  }
459
478
 
@@ -464,4 +483,21 @@ export class SfsArray {
464
483
  public getClass(index: number): SfsObject | null {
465
484
  return this.getSfsObject(index)
466
485
  }
486
+
487
+ /**
488
+ * The value at a position, or nothing
489
+ * @param index position to look at
490
+ */
491
+ private value(index: number): SfsData | undefined {
492
+ return this.values[index]
493
+ }
494
+
495
+ /**
496
+ * The value at a position if it has the wire type asked for, or nothing
497
+ * @param index position to look at
498
+ * @param type wire type it has to be
499
+ */
500
+ private typed(index: number, type: DataTypeId): SfsData | undefined {
501
+ return this.types[index] === type ? this.values[index] : undefined
502
+ }
467
503
  }