@netifyjs/protocol 1.0.0-alpha.1 → 1.0.0-alpha.3

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.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@netifyjs/protocol",
3
3
  "description": "Ready-made game server protocols for Netify, starting with SmartFoxServer Pro and 2X",
4
- "version": "1.0.0-alpha.1",
4
+ "version": "1.0.0-alpha.3",
5
5
  "type": "module",
6
6
  "exports": {
7
7
  ".": "./src/index.ts",
@@ -37,7 +37,7 @@
37
37
  "url": "https://github.com/netifyjs/protocol/issues"
38
38
  },
39
39
  "peerDependencies": {
40
- "@netifyjs/netify": ">=1.0.0-alpha.10"
40
+ "@netifyjs/netify": ">=1.0.0-alpha.12"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@eslint/js": "^10.0.1",
@@ -1,18 +1,70 @@
1
1
  import {
2
+ AttributeKey,
2
3
  ByteBuf,
3
4
  ByteToMessageDecoder,
4
5
  EncoderError,
6
+ QueueFullError,
7
+ type Channel,
5
8
  type DecoderOptions,
6
9
  type HandlerContext,
7
10
  } from '@netifyjs/netify'
8
11
 
9
12
  import type { DirectWriter, Protocol } from '../protocol/index.ts'
13
+ import { TunnelOutlet } from '../scale/outlet/index.ts'
14
+
15
+ /**
16
+ * Takes encoded bytes written straight into its room
17
+ */
18
+ export type Staging = {
19
+ /**
20
+ * Where reserved bytes get written
21
+ */
22
+ readonly stagingBuffer: Uint8Array
23
+
24
+ /**
25
+ * Makes room and gives where it starts, or -1
26
+ * @param size bytes to make room for
27
+ */
28
+ reserve(size: number): number
29
+
30
+ /**
31
+ * Sends what was written into the reserved room
32
+ * @param unused bytes of the room left over
33
+ */
34
+ commitStaged(unused?: number): void
35
+ }
10
36
 
11
37
  /**
12
38
  * Base for a protocol's pipeline handler, with the outbound half done
13
39
  * @class
14
40
  */
15
41
  export abstract class PacketCodec<O = unknown> extends ByteToMessageDecoder<O> {
42
+ /**
43
+ * Causes looked through before giving up
44
+ */
45
+ private static readonly causes: number = 8
46
+
47
+ /**
48
+ * Where a channel keeps its codec
49
+ */
50
+ private static readonly key: AttributeKey<PacketCodec> =
51
+ new AttributeKey<PacketCodec>('netify:protocol:codec')
52
+
53
+ /**
54
+ * Its place in the pipeline, or null
55
+ */
56
+ private context: HandlerContext | null
57
+
58
+ /**
59
+ * Pipeline version the last check of what follows belongs to
60
+ */
61
+ private checked: number
62
+
63
+ /**
64
+ * Whether no outbound handler follows it
65
+ */
66
+ private last: boolean
67
+
16
68
  /**
17
69
  * The protocol that knows the wire format
18
70
  */
@@ -44,6 +96,78 @@ export abstract class PacketCodec<O = unknown> extends ByteToMessageDecoder<O> {
44
96
  this.straight = protocol.straight()
45
97
  this.starts = []
46
98
  this.produced = []
99
+ this.context = null
100
+ this.checked = -1
101
+ this.last = false
102
+ }
103
+
104
+ /**
105
+ * The codec a channel was set up with, or null
106
+ * @param channel the connection
107
+ */
108
+ public static on(channel: Channel): PacketCodec | null {
109
+ return channel.attributes.get<PacketCodec>(PacketCodec.key)
110
+ }
111
+
112
+ /**
113
+ * Whether it still sits in a pipeline
114
+ */
115
+ public get attached(): boolean {
116
+ return this.context !== null
117
+ }
118
+
119
+ /**
120
+ * Remembers where it sits and puts itself on the channel
121
+ * @param context its place in the pipeline
122
+ */
123
+ public handlerAdded(context: HandlerContext): void {
124
+ this.context = context
125
+ this.checked = -1
126
+ context.channel.attributes.set<PacketCodec>(PacketCodec.key, this)
127
+ }
128
+
129
+ /**
130
+ * Takes itself off the channel and hands on leftover bytes
131
+ * @param context its place in the pipeline
132
+ */
133
+ public override handlerRemoved(context: HandlerContext): Promise<void> {
134
+ this.context = null
135
+
136
+ const attributes = context.channel.attributes
137
+ if (attributes.get<PacketCodec>(PacketCodec.key) === this) {
138
+ attributes.delete<PacketCodec>(PacketCodec.key)
139
+ }
140
+
141
+ return super.handlerRemoved(context)
142
+ }
143
+
144
+ /**
145
+ * Encodes a packet without walking the pipeline, false if something else has to see it
146
+ * @param packet anything heading out
147
+ */
148
+ public send(packet: unknown): boolean {
149
+ const context = this.context
150
+ if (context === null) return false
151
+
152
+ const version = context.pipeline.version
153
+ if (this.checked !== version) {
154
+ this.checked = version
155
+ this.last = PacketCodec.alone(context)
156
+ }
157
+
158
+ return this.last && this.writeSync(context, packet)
159
+ }
160
+
161
+ /**
162
+ * Whether no outbound handler sits between it and the tail
163
+ * @param context its place in the pipeline
164
+ */
165
+ private static alone(context: HandlerContext): boolean {
166
+ for (let next = context.next; next !== null; next = next.next) {
167
+ if ('write' in next.handler) return false
168
+ }
169
+
170
+ return true
47
171
  }
48
172
 
49
173
  /**
@@ -73,6 +197,20 @@ export abstract class PacketCodec<O = unknown> extends ByteToMessageDecoder<O> {
73
197
  return true
74
198
  }
75
199
 
200
+ /**
201
+ * Hangs up on a client too slow to take its frames
202
+ * @param context the connection
203
+ * @param error what went wrong
204
+ */
205
+ public override exceptionCaught(
206
+ context: HandlerContext,
207
+ error: unknown,
208
+ ): Promise<void> {
209
+ if (PacketCodec.overflowed(error)) void context.close()
210
+
211
+ return context.fireExceptionCaught(error)
212
+ }
213
+
76
214
  /**
77
215
  * Encodes packets and lets everything else through
78
216
  * @param context where the bytes go
@@ -119,34 +257,57 @@ export abstract class PacketCodec<O = unknown> extends ByteToMessageDecoder<O> {
119
257
  private direct(context: HandlerContext, message: unknown): boolean {
120
258
  const writer = this.straight
121
259
  const room = writer === null ? -1 : writer.measure(message)
122
- if (writer === null || room < 0 || !context.writesToSocket) return false
260
+ const staging = room < 0 ? null : PacketCodec.staging(context)
261
+ if (writer === null || staging === null) return false
123
262
 
124
- const channel = context.channel
125
- const offset = channel.reserve(room)
263
+ const offset = staging.reserve(room)
126
264
  if (offset < 0) return false
127
265
 
128
266
  let taken = -1
129
267
  try {
130
- taken = writer.write(message, channel.stagingBuffer, offset)
268
+ taken = writer.write(message, staging.stagingBuffer, offset)
131
269
  } finally {
132
- channel.commitStaged(taken < 0 ? room : room - taken)
270
+ staging.commitStaged(taken < 0 ? room : room - taken)
133
271
  }
134
272
 
135
273
  return taken >= 0
136
274
  }
137
275
 
276
+ /**
277
+ * Whether an error comes from a full outbox
278
+ * @param error what the pipeline caught
279
+ */
280
+ public static overflowed(error: unknown): boolean {
281
+ let current: unknown = error
282
+
283
+ for (let step = 0; step < PacketCodec.causes; step++) {
284
+ if (current instanceof QueueFullError) return true
285
+ if (!(current instanceof Error)) return false
286
+
287
+ current = current.cause
288
+ }
289
+
290
+ return false
291
+ }
292
+
293
+ /**
294
+ * Where the next write can be staged directly, or null
295
+ * @param context the codec's place in the pipeline
296
+ */
297
+ public static staging(context: HandlerContext): Staging | null {
298
+ if (context.writesToSocket) return context.channel
299
+
300
+ const outlet = context.previous?.handler
301
+ return outlet instanceof TunnelOutlet ? outlet : null
302
+ }
303
+
138
304
  /**
139
305
  * Writes encoded bytes without a promise
140
306
  * @param context where the bytes go
141
307
  * @param bytes one or more whole frames
142
308
  */
143
309
  protected static put(context: HandlerContext, bytes: Uint8Array): void {
144
- if (
145
- context.writesToSocket &&
146
- PacketCodec.stage(context, bytes, 0, bytes.length)
147
- ) {
148
- return
149
- }
310
+ if (PacketCodec.stage(context, bytes, 0, bytes.length)) return
150
311
 
151
312
  context.writeSync(ByteBuf.wrap(bytes))
152
313
  }
@@ -157,19 +318,16 @@ export abstract class PacketCodec<O = unknown> extends ByteToMessageDecoder<O> {
157
318
  * @param frame one or more whole frames
158
319
  */
159
320
  protected static pass(context: HandlerContext, frame: ByteBuf): boolean {
160
- return (
161
- context.writesToSocket &&
162
- PacketCodec.stage(
163
- context,
164
- frame.array,
165
- frame.arrayOffset + frame.readerIndex,
166
- frame.readableBytes,
167
- )
321
+ return PacketCodec.stage(
322
+ context,
323
+ frame.array,
324
+ frame.arrayOffset + frame.readerIndex,
325
+ frame.readableBytes,
168
326
  )
169
327
  }
170
328
 
171
329
  /**
172
- * Copies bytes into the connection's outbound buffer, false if they don't fit
330
+ * Copies bytes into the outbound room, false if there's none
173
331
  * @param context where the bytes go
174
332
  * @param source array the bytes live in
175
333
  * @param start where they begin
@@ -181,12 +339,12 @@ export abstract class PacketCodec<O = unknown> extends ByteToMessageDecoder<O> {
181
339
  start: number,
182
340
  size: number,
183
341
  ): boolean {
184
- const channel = context.channel
185
- const offset = channel.reserve(size)
186
- if (offset < 0) return false
342
+ const staging = PacketCodec.staging(context)
343
+ const offset = staging === null ? -1 : staging.reserve(size)
344
+ if (staging === null || offset < 0) return false
187
345
 
188
- ByteBuf.copyBytes(source, start, channel.stagingBuffer, offset, size)
189
- channel.commitStaged()
346
+ ByteBuf.copyBytes(source, start, staging.stagingBuffer, offset, size)
347
+ staging.commitStaged()
190
348
 
191
349
  return true
192
350
  }
@@ -1,4 +1,8 @@
1
- import { InboundHandlerAdapter, type HandlerContext } from '@netifyjs/netify'
1
+ import {
2
+ InboundHandlerAdapter,
3
+ type Channel,
4
+ type HandlerContext,
5
+ } from '@netifyjs/netify'
2
6
 
3
7
  import { Tunnel } from '../../scale/tunnel/index.ts'
4
8
  import { Closed, Opened } from '../event/index.ts'
@@ -45,6 +49,11 @@ export class PacketDispatcher<U = unknown> extends InboundHandlerAdapter {
45
49
  */
46
50
  private lastTyped: Bound | null
47
51
 
52
+ /**
53
+ * Session of the last connection read, or null
54
+ */
55
+ private lastSession: Session<U> | null
56
+
48
57
  /**
49
58
  * Starts with no handlers
50
59
  */
@@ -54,6 +63,7 @@ export class PacketDispatcher<U = unknown> extends InboundHandlerAdapter {
54
63
  this.lastType = null
55
64
  this.lastKeys = null
56
65
  this.lastTyped = null
66
+ this.lastSession = null
57
67
 
58
68
  this.events = new Map<unknown, Bound[]>()
59
69
  this.typed = new Map<unknown, Bound>()
@@ -113,7 +123,23 @@ export class PacketDispatcher<U = unknown> extends InboundHandlerAdapter {
113
123
  const bound = this.lastKeys?.get((message as Keyed).route) ?? this.lastTyped
114
124
  if (bound === null) return context.fireChannelRead(message)
115
125
 
116
- return bound(Session.of<U>(context.channel), message)
126
+ const session = this.session(context.channel)
127
+ session.reading = true
128
+ return bound(session, message)
129
+ }
130
+
131
+ /**
132
+ * Sends everything the read's handlers wrote in one flush
133
+ * @param context the connection
134
+ */
135
+ public override channelReadComplete(context: HandlerContext): Promise<void> {
136
+ const session = this.session(context.channel)
137
+ if (session.reading) {
138
+ session.reading = false
139
+ void context.channel.flush()
140
+ }
141
+
142
+ return context.fireChannelReadComplete()
117
143
  }
118
144
 
119
145
  /**
@@ -135,6 +161,8 @@ export class PacketDispatcher<U = unknown> extends InboundHandlerAdapter {
135
161
  public override async channelInactive(
136
162
  context: HandlerContext,
137
163
  ): Promise<void> {
164
+ if (this.lastSession?.channel === context.channel) this.lastSession = null
165
+
138
166
  await this.emit(context, Closed.now())
139
167
  await context.fireChannelInactive()
140
168
  }
@@ -169,6 +197,19 @@ export class PacketDispatcher<U = unknown> extends InboundHandlerAdapter {
169
197
  }
170
198
  }
171
199
 
200
+ /**
201
+ * A connection's session, looked up once for a run of reads
202
+ * @param channel the connection
203
+ */
204
+ private session(channel: Channel): Session<U> {
205
+ const last = this.lastSession
206
+ if (last !== null && last.channel === channel) return last
207
+
208
+ const session = Session.of<U>(channel)
209
+ this.lastSession = session
210
+ return session
211
+ }
212
+
172
213
  /**
173
214
  * Files one method under its packet class and key
174
215
  * @param handler the instance it runs on
@@ -1,5 +1,7 @@
1
1
  import { AttributeKey, type Channel } from '@netifyjs/netify'
2
2
 
3
+ import { PacketCodec } from '../../codec/index.ts'
4
+
3
5
  /**
4
6
  * One connection and whoever is logged in on it
5
7
  * @class
@@ -21,6 +23,16 @@ export class Session<U = unknown> {
21
23
  */
22
24
  public user: U | null
23
25
 
26
+ /**
27
+ * Whether a read is under way, and its sends wait for one flush at the end
28
+ */
29
+ public reading: boolean
30
+
31
+ /**
32
+ * Codec sends go straight to, or null
33
+ */
34
+ private codec: PacketCodec | null
35
+
24
36
  /**
25
37
  * Starts with nobody logged in
26
38
  * @param channel the connection underneath
@@ -28,6 +40,8 @@ export class Session<U = unknown> {
28
40
  private constructor(channel: Channel) {
29
41
  this.channel = channel
30
42
  this.user = null
43
+ this.reading = false
44
+ this.codec = null
31
45
  }
32
46
 
33
47
  /**
@@ -48,7 +62,17 @@ export class Session<U = unknown> {
48
62
  * @param packet anything the codec knows how to encode
49
63
  */
50
64
  public send(packet: unknown): void {
51
- this.channel.writeAndFlushSync(packet)
65
+ if (this.codec?.attached !== true) {
66
+ this.codec = PacketCodec.on(this.channel)
67
+ }
68
+
69
+ if (this.codec?.send(packet) === true) {
70
+ if (!this.reading) void this.channel.flush()
71
+ return
72
+ }
73
+
74
+ if (this.reading) this.channel.writeSync(packet)
75
+ else this.channel.writeAndFlushSync(packet)
52
76
  }
53
77
 
54
78
  /**
package/src/index.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export { PacketCodec } from './codec/index.ts'
2
+ export type { Staging } from './codec/index.ts'
2
3
  export {
3
4
  Closed,
4
5
  Opened,
@@ -22,12 +23,14 @@ export { NameTable } from './names/index.ts'
22
23
  export { type DirectWriter, Protocol } from './protocol/index.ts'
23
24
  export type { Role } from './protocol/index.ts'
24
25
  export {
26
+ Batch,
25
27
  Courier,
26
28
  DatagramSwitch,
27
29
  Fanout,
28
30
  Handoff,
29
31
  Layout,
30
32
  Mesh,
33
+ Pending,
31
34
  Placement,
32
35
  SinkTransport,
33
36
  Switchboard,
@@ -37,7 +40,10 @@ export {
37
40
  TunnelTransport,
38
41
  } from './scale/index.ts'
39
42
  export type {
43
+ BatchTaken,
44
+ BatchVisitor,
40
45
  CourierBytesListener,
46
+ CourierFlusher,
41
47
  CourierListener,
42
48
  CourierResponder,
43
49
  DatagramReceiver,
@@ -49,6 +55,7 @@ export type {
49
55
  Hosted,
50
56
  MeshOptions,
51
57
  OutletSink,
58
+ PendingLoss,
52
59
  PlacementRule,
53
60
  Portable,
54
61
  Reach,
@@ -64,7 +64,9 @@ export class ParameterTable {
64
64
  * @param code parameter code
65
65
  */
66
66
  public get(code: number): PhotonValue | null {
67
- return this.held[this.codes.indexOf(code)] ?? null
67
+ const at = this.codes.indexOf(code)
68
+
69
+ return at < 0 ? null : (this.held[at] ?? null)
68
70
  }
69
71
 
70
72
  /**
@@ -261,6 +261,11 @@ const Code = {
261
261
  * @class
262
262
  */
263
263
  export class Protocol18 extends BinaryProtocolAdapter {
264
+ /**
265
+ * Longest text read straight away through the name cache
266
+ */
267
+ private static readonly named: number = 24
268
+
264
269
  /**
265
270
  * Custom type codes below this go out in the slim form
266
271
  */
@@ -376,7 +381,7 @@ export class Protocol18 extends BinaryProtocolAdapter {
376
381
  case Code.DOUBLE_ZERO:
377
382
  return PhotonValue.double(0)
378
383
  case Code.STRING:
379
- return PhotonValue.string(this.decodeString(buffer))
384
+ return this.decodeText(buffer)
380
385
  case Code.BYTE_ARRAY:
381
386
  return PhotonValue.bytes(this.decodeBytes(buffer))
382
387
  case Code.CUSTOM:
@@ -392,6 +397,13 @@ export class Protocol18 extends BinaryProtocolAdapter {
392
397
  * @param buffer where it goes
393
398
  */
394
399
  protected encodeValue(value: PhotonValue, buffer: ByteBuf): void {
400
+ const spelt = value.spelling
401
+ if (spelt !== null) {
402
+ buffer.writeUint8(Code.STRING)
403
+ this.encodeBytes(spelt, buffer)
404
+ return
405
+ }
406
+
395
407
  const data = value.data
396
408
 
397
409
  switch (value.shape.kind) {
@@ -606,6 +618,19 @@ export class Protocol18 extends BinaryProtocolAdapter {
606
618
  return BigInt.asIntN(64, (raw >> 1n) ^ -(raw & 1n))
607
619
  }
608
620
 
621
+ /**
622
+ * Reads a string value, keeping a long one as bytes until it's read
623
+ * @param buffer bytes to read from
624
+ */
625
+ private decodeText(buffer: ByteBuf): PhotonValue {
626
+ const size = this.size(this.decodeCount(buffer), buffer)
627
+ if (size <= Protocol18.named) {
628
+ return PhotonValue.string(this.names.read(size, buffer))
629
+ }
630
+
631
+ return PhotonValue.text(buffer.readBytes(size))
632
+ }
633
+
609
634
  /**
610
635
  * Reads a string with its length
611
636
  * @param buffer bytes to read from
@@ -37,15 +37,48 @@ export class PhotonValue {
37
37
  null,
38
38
  )
39
39
 
40
+ /**
41
+ * Lowest whole number with a kept label
42
+ */
43
+ private static readonly lowest: number = -128
44
+
45
+ /**
46
+ * Highest whole number with a kept label
47
+ */
48
+ private static readonly highest: number = 255
49
+
50
+ /**
51
+ * Labels of small whole numbers, by kind and then value
52
+ */
53
+ private static readonly labels: Map<KindId, (string | undefined)[]> = new Map<
54
+ KindId,
55
+ (string | undefined)[]
56
+ >()
57
+
58
+ /**
59
+ * Turns kept UTF-8 bytes into text
60
+ */
61
+ private static readonly utf8: TextDecoder = new TextDecoder()
62
+
40
63
  /**
41
64
  * Its exact type
42
65
  */
43
66
  public readonly shape: Shape
44
67
 
45
68
  /**
46
- * What it holds
69
+ * What it holds, once it's been read
70
+ */
71
+ private held: PhotonData
72
+
73
+ /**
74
+ * UTF-8 bytes of a text that came off the wire, or null
47
75
  */
48
- public readonly data: PhotonData
76
+ private spelt: Uint8Array | null
77
+
78
+ /**
79
+ * Whether the text still has to be read out of its bytes
80
+ */
81
+ private waiting: boolean
49
82
 
50
83
  /**
51
84
  * What tables file it under once it's been worked out, or null
@@ -63,11 +96,33 @@ export class PhotonValue {
63
96
  */
64
97
  private constructor(shape: Shape, data: PhotonData) {
65
98
  this.shape = shape
66
- this.data = data
99
+ this.held = data
100
+ this.spelt = null
101
+ this.waiting = false
67
102
  this.label = null
68
103
  this.labelled = false
69
104
  }
70
105
 
106
+ /**
107
+ * What it holds
108
+ */
109
+ public get data(): PhotonData {
110
+ const spelt = this.spelt
111
+ if (this.waiting && spelt !== null) {
112
+ this.held = PhotonValue.utf8.decode(spelt)
113
+ this.waiting = false
114
+ }
115
+
116
+ return this.held
117
+ }
118
+
119
+ /**
120
+ * UTF-8 bytes of a text read off the wire, or null
121
+ */
122
+ public get spelling(): Uint8Array | null {
123
+ return this.spelt
124
+ }
125
+
71
126
  /**
72
127
  * A value of any shape
73
128
  * @param shape its exact type
@@ -159,6 +214,18 @@ export class PhotonValue {
159
214
  return new PhotonValue(Shape.string(), value)
160
215
  }
161
216
 
217
+ /**
218
+ * A text kept as its UTF-8 bytes until something reads it
219
+ * @param bytes the text as UTF-8, owned by the value from now on
220
+ */
221
+ public static text(bytes: Uint8Array): PhotonValue {
222
+ const value = new PhotonValue(Shape.string(), '')
223
+ value.spelt = bytes
224
+ value.waiting = true
225
+
226
+ return value
227
+ }
228
+
162
229
  /**
163
230
  * Raw bytes
164
231
  * @param value what to hold
@@ -271,14 +338,48 @@ export class PhotonValue {
271
338
  if (this.labelled) return this.label
272
339
 
273
340
  this.labelled = true
274
- this.label =
275
- typeof data === 'object'
276
- ? null
277
- : `\0${String(this.shape.kind)}:${String(data)}`
341
+ if (typeof data === 'number') {
342
+ this.label = PhotonValue.numbered(this.shape.kind, data)
343
+ } else {
344
+ this.label =
345
+ typeof data === 'object'
346
+ ? null
347
+ : `\0${String(this.shape.kind)}:${String(data)}`
348
+ }
278
349
 
279
350
  return this.label
280
351
  }
281
352
 
353
+ /**
354
+ * The label of a number, kept for small whole ones
355
+ * @param kind its type
356
+ * @param data the number
357
+ */
358
+ private static numbered(kind: KindId, data: number): string {
359
+ const small =
360
+ Number.isInteger(data) &&
361
+ data >= PhotonValue.lowest &&
362
+ data <= PhotonValue.highest
363
+ if (!small) return `\0${String(kind)}:${String(data)}`
364
+
365
+ let labels = PhotonValue.labels.get(kind)
366
+ if (labels === undefined) {
367
+ labels = new Array<string | undefined>(
368
+ PhotonValue.highest - PhotonValue.lowest + 1,
369
+ ).fill(undefined)
370
+ PhotonValue.labels.set(kind, labels)
371
+ }
372
+
373
+ const at = data - PhotonValue.lowest
374
+ const known = labels[at]
375
+ if (known !== undefined) return known
376
+
377
+ const label = `\0${String(kind)}:${String(data)}`
378
+ labels[at] = label
379
+
380
+ return label
381
+ }
382
+
282
383
  /**
283
384
  * A byte, short, int or long as a number, or null
284
385
  */
@@ -424,6 +424,8 @@ export class GameHandler {
424
424
  * @param session the connection
425
425
  */
426
426
  private peer(session: Session<GamePeer>): GamePeer {
427
+ if (session.user !== null) return session.user
428
+
427
429
  const peer = GamePeer.of(session.channel, this.services.clock.now())
428
430
  session.user = peer
429
431