@pluv/io 0.19.0 → 0.21.0

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/src/PluvIO.ts CHANGED
@@ -9,19 +9,20 @@ import type {
9
9
  JsonObject,
10
10
  Maybe,
11
11
  } from "@pluv/types";
12
- import type { AbstractPlatform, InferPlatformRoomContextType } from "./AbstractPlatform";
12
+ import type { AbstractPlatform, InferPlatformContextType, WebSocketRegistrationMode } from "./AbstractPlatform";
13
13
  import { PluvProcedure } from "./PluvProcedure";
14
14
  import type { MergedRouter, PluvRouterEventConfig } from "./PluvRouter";
15
15
  import { PluvRouter } from "./PluvRouter";
16
16
  import { PluvServer } from "./PluvServer";
17
17
  import type { JWTEncodeParams } from "./authorize";
18
18
  import { authorize } from "./authorize";
19
+ import { PING_TIMEOUT_MS } from "./constants";
19
20
  import type { GetInitialStorageFn, PluvIOListeners } from "./types";
20
21
  import { __PLUV_VERSION } from "./version";
21
22
 
22
23
  export type PluvIOConfig<
23
24
  TPlatform extends AbstractPlatform<any>,
24
- TAuthorize extends IOAuthorize<any, any, InferPlatformRoomContextType<TPlatform>>,
25
+ TAuthorize extends IOAuthorize<any, any, InferPlatformContextType<TPlatform>>,
25
26
  TContext extends JsonObject,
26
27
  > = Partial<PluvIOListeners<TPlatform>> & {
27
28
  authorize?: TAuthorize;
@@ -34,7 +35,7 @@ export type PluvIOConfig<
34
35
 
35
36
  export interface ServerConfig<
36
37
  TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>,
37
- TAuthorize extends IOAuthorize<any, any, InferPlatformRoomContextType<TPlatform>> = BaseIOAuthorize,
38
+ TAuthorize extends IOAuthorize<any, any, InferPlatformContextType<TPlatform>> = BaseIOAuthorize,
38
39
  TContext extends JsonObject = {},
39
40
  TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {},
40
41
  > {
@@ -43,14 +44,22 @@ export interface ServerConfig<
43
44
 
44
45
  export class PluvIO<
45
46
  TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>,
46
- TAuthorize extends IOAuthorize<any, any, InferPlatformRoomContextType<TPlatform>> = BaseIOAuthorize,
47
+ TAuthorize extends IOAuthorize<any, any, InferPlatformContextType<TPlatform>> = BaseIOAuthorize,
47
48
  TContext extends JsonObject = {},
48
49
  > implements IOLike<TAuthorize, {}>
49
50
  {
50
51
  private _router: PluvRouter<TPlatform, TAuthorize, TContext, {}> = new PluvRouter({
51
52
  $GET_OTHERS: this.procedure.sync((data, { room, session, sessions }) => {
52
- const others = Array.from(sessions.entries())
53
- .filter(([, wsSession]) => wsSession.id !== session?.id)
53
+ const currentTime = new Date().getTime();
54
+
55
+ const others = sessions
56
+ .filter((wsSession) => {
57
+ if (wsSession.id === session?.id) return false;
58
+ if (wsSession.quit) return false;
59
+ if (currentTime - wsSession.timers.ping > PING_TIMEOUT_MS) return false;
60
+
61
+ return true;
62
+ })
54
63
  .reduce<{
55
64
  [connectionId: string]: {
56
65
  connectionId: string;
@@ -58,10 +67,10 @@ export class PluvIO<
58
67
  user: JsonObject;
59
68
  };
60
69
  }>(
61
- (acc, [connectionId, { presence, user }]) => ({
70
+ (acc, { id, presence, user }) => ({
62
71
  ...acc,
63
- [connectionId]: {
64
- connectionId,
72
+ [id]: {
73
+ connectionId: id,
65
74
  presence,
66
75
  room,
67
76
  user,
@@ -128,9 +137,11 @@ export class PluvIO<
128
137
 
129
138
  if (!session) return {};
130
139
 
131
- session.presence = Object.assign(Object.create(null), session.presence, presence);
140
+ const updated = Object.assign(Object.create(null), session.presence, presence);
132
141
 
133
- return { $PRESENCE_UPDATED: { presence } };
142
+ session.webSocket.presence = updated;
143
+
144
+ return { $PRESENCE_UPDATED: { presence: updated } };
134
145
  }),
135
146
  $UPDATE_STORAGE: this.procedure.broadcast((data, { context, doc, room }) => {
136
147
  const origin = (data as any)?.origin as Maybe<string>;
@@ -168,6 +179,10 @@ export class PluvIO<
168
179
  return this._router._events;
169
180
  }
170
181
 
182
+ public get _registrationMode(): WebSocketRegistrationMode {
183
+ return this._platform._registrationMode;
184
+ }
185
+
171
186
  public get procedure(): PluvProcedure<TPlatform, TAuthorize, TContext, {}, {}> {
172
187
  return new PluvProcedure();
173
188
  }
@@ -201,9 +216,7 @@ export class PluvIO<
201
216
  public async createToken(
202
217
  params: JWTEncodeParams<InferIOAuthorizeUser<InferIOAuthorize<this>>, TPlatform>,
203
218
  ): Promise<string> {
204
- if (!this._authorize) {
205
- throw new Error("IO does not specify authorize during initialization.");
206
- }
219
+ if (!this._authorize) throw new Error("IO does not specify authorize during initialization.");
207
220
 
208
221
  const ioAuthorize =
209
222
  typeof this._authorize === "function" ? this._authorize(params) : (this._authorize as { secret: string });
@@ -1,10 +1,10 @@
1
1
  import type { EventRecord, IOAuthorize, InputZodLike, JsonObject } from "@pluv/types";
2
- import type { AbstractPlatform, InferPlatformEventContextType, InferPlatformRoomContextType } from "./AbstractPlatform";
2
+ import type { AbstractPlatform, InferRoomContextType } from "./AbstractPlatform";
3
3
  import type { EventResolver, MergeEventRecords } from "./types";
4
4
 
5
5
  export interface PluvProcedureConfig<
6
6
  TPlatform extends AbstractPlatform<any>,
7
- TAuthorize extends IOAuthorize<any, any, InferPlatformRoomContextType<TPlatform>>,
7
+ TAuthorize extends IOAuthorize<any, any, InferRoomContextType<TPlatform>>,
8
8
  TContext extends JsonObject,
9
9
  TInput extends JsonObject = {},
10
10
  TOutput extends EventRecord<string, any> = {},
@@ -12,7 +12,7 @@ export interface PluvProcedureConfig<
12
12
  broadcast?: EventResolver<
13
13
  TPlatform,
14
14
  TAuthorize,
15
- TContext & InferPlatformRoomContextType<TPlatform> & InferPlatformEventContextType<TPlatform>,
15
+ TContext & InferRoomContextType<TPlatform>,
16
16
  TInput,
17
17
  Partial<TOutput>
18
18
  > | null;
@@ -20,7 +20,7 @@ export interface PluvProcedureConfig<
20
20
  self?: EventResolver<
21
21
  TPlatform,
22
22
  TAuthorize,
23
- TContext & InferPlatformRoomContextType<TPlatform> & InferPlatformEventContextType<TPlatform>,
23
+ TContext & InferRoomContextType<TPlatform>,
24
24
  TInput,
25
25
  Partial<TOutput>
26
26
  > | null;
@@ -29,7 +29,7 @@ export interface PluvProcedureConfig<
29
29
 
30
30
  export class PluvProcedure<
31
31
  TPlatform extends AbstractPlatform<any>,
32
- TAuthorize extends IOAuthorize<any, any, InferPlatformRoomContextType<TPlatform>>,
32
+ TAuthorize extends IOAuthorize<any, any, InferRoomContextType<TPlatform>>,
33
33
  TContext extends JsonObject,
34
34
  TInput extends JsonObject = {},
35
35
  TOutput extends EventRecord<string, any> = {},
@@ -38,7 +38,7 @@ export class PluvProcedure<
38
38
  private _broadcast: EventResolver<
39
39
  TPlatform,
40
40
  TAuthorize,
41
- TContext & InferPlatformRoomContextType<TPlatform> & InferPlatformEventContextType<TPlatform>,
41
+ TContext & InferRoomContextType<TPlatform>,
42
42
  TInput,
43
43
  Partial<TOutput>
44
44
  > | null = null;
@@ -46,7 +46,7 @@ export class PluvProcedure<
46
46
  private _self: EventResolver<
47
47
  TPlatform,
48
48
  TAuthorize,
49
- TContext & InferPlatformRoomContextType<TPlatform> & InferPlatformEventContextType<TPlatform>,
49
+ TContext & InferRoomContextType<TPlatform>,
50
50
  TInput,
51
51
  Partial<TOutput>
52
52
  > | null = null;
@@ -72,13 +72,7 @@ export class PluvProcedure<
72
72
  }
73
73
 
74
74
  public broadcast<TResult extends EventRecord<string, any> = {}>(
75
- resolver: EventResolver<
76
- TPlatform,
77
- TAuthorize,
78
- TContext & InferPlatformRoomContextType<TPlatform> & InferPlatformEventContextType<TPlatform>,
79
- TInput,
80
- TResult
81
- >,
75
+ resolver: EventResolver<TPlatform, TAuthorize, TContext & InferRoomContextType<TPlatform>, TInput, TResult>,
82
76
  ): Omit<
83
77
  PluvProcedure<
84
78
  TPlatform,
@@ -114,13 +108,7 @@ export class PluvProcedure<
114
108
  }
115
109
 
116
110
  public self<TResult extends EventRecord<string, any> = {}>(
117
- resolver: EventResolver<
118
- TPlatform,
119
- TAuthorize,
120
- TContext & InferPlatformRoomContextType<TPlatform> & InferPlatformEventContextType<TPlatform>,
121
- TInput,
122
- TResult
123
- >,
111
+ resolver: EventResolver<TPlatform, TAuthorize, TContext & InferRoomContextType<TPlatform>, TInput, TResult>,
124
112
  ): Omit<
125
113
  PluvProcedure<
126
114
  TPlatform,
@@ -150,13 +138,7 @@ export class PluvProcedure<
150
138
  }
151
139
 
152
140
  public sync<TResult extends EventRecord<string, any> = {}>(
153
- resolver: EventResolver<
154
- TPlatform,
155
- TAuthorize,
156
- TContext & InferPlatformRoomContextType<TPlatform> & InferPlatformEventContextType<TPlatform>,
157
- TInput,
158
- TResult
159
- >,
141
+ resolver: EventResolver<TPlatform, TAuthorize, TContext & InferRoomContextType<TPlatform>, TInput, TResult>,
160
142
  ): Omit<
161
143
  PluvProcedure<
162
144
  TPlatform,
@@ -188,7 +170,7 @@ export class PluvProcedure<
188
170
  private _resolver(): EventResolver<
189
171
  TPlatform,
190
172
  TAuthorize,
191
- TContext & InferPlatformRoomContextType<TPlatform> & InferPlatformEventContextType<TPlatform>,
173
+ TContext & InferRoomContextType<TPlatform>,
192
174
  TInput,
193
175
  TOutput
194
176
  > {
package/src/PluvRouter.ts CHANGED
@@ -1,10 +1,10 @@
1
1
  import type { BaseIOAuthorize, IOAuthorize, IORouterLike, JsonObject } from "@pluv/types";
2
- import type { AbstractPlatform, InferPlatformRoomContextType } from "./AbstractPlatform";
2
+ import type { AbstractPlatform, InferRoomContextType } from "./AbstractPlatform";
3
3
  import type { PluvProcedure } from "./PluvProcedure";
4
4
 
5
5
  export type PluvRouterEventConfig<
6
6
  TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>,
7
- TAuthorize extends IOAuthorize<any, any, InferPlatformRoomContextType<TPlatform>> = BaseIOAuthorize,
7
+ TAuthorize extends IOAuthorize<any, any, InferRoomContextType<TPlatform>> = BaseIOAuthorize,
8
8
  TContext extends JsonObject = {},
9
9
  > = {
10
10
  [P: string]: Pick<PluvProcedure<TPlatform, TAuthorize, TContext, any, any>, "config">;
@@ -13,7 +13,7 @@ export type PluvRouterEventConfig<
13
13
  export type MergedRouter<
14
14
  TRouters extends PluvRouter<TPlatform, TAuthorize, TContext, any>[] = [],
15
15
  TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>,
16
- TAuthorize extends IOAuthorize<any, any, InferPlatformRoomContextType<TPlatform>> = BaseIOAuthorize,
16
+ TAuthorize extends IOAuthorize<any, any, InferRoomContextType<TPlatform>> = BaseIOAuthorize,
17
17
  TContext extends JsonObject = {},
18
18
  TRoot extends TRouters[0]["_events"] = {},
19
19
  > = TRouters extends [
@@ -25,7 +25,7 @@ export type MergedRouter<
25
25
 
26
26
  export class PluvRouter<
27
27
  TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>,
28
- TAuthorize extends IOAuthorize<any, any, InferPlatformRoomContextType<TPlatform>> = BaseIOAuthorize,
28
+ TAuthorize extends IOAuthorize<any, any, InferRoomContextType<TPlatform>> = BaseIOAuthorize,
29
29
  TContext extends JsonObject = {},
30
30
  TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {},
31
31
  > implements IORouterLike<TEvents>
package/src/PluvServer.ts CHANGED
@@ -4,12 +4,18 @@ import type {
4
4
  BaseIOAuthorize,
5
5
  IOAuthorize,
6
6
  IORouterLike,
7
+ Id,
7
8
  InferIOAuthorize,
8
9
  InferIOAuthorizeUser,
9
10
  JsonObject,
10
11
  } from "@pluv/types";
11
12
  import colors from "kleur";
12
- import type { AbstractPlatform, InferPlatformRoomContextType } from "./AbstractPlatform";
13
+ import type {
14
+ AbstractPlatform,
15
+ InferPlatformContextType,
16
+ InferRoomContextType,
17
+ WebSocketRegistrationMode,
18
+ } from "./AbstractPlatform";
13
19
  import { IORoom } from "./IORoom";
14
20
  import type { PluvRouterEventConfig } from "./PluvRouter";
15
21
  import { PluvRouter } from "./PluvRouter";
@@ -25,7 +31,7 @@ export type InferIORoom<TServer extends PluvServer<any, any, any, any>> =
25
31
 
26
32
  export type PluvServerConfig<
27
33
  TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>,
28
- TAuthorize extends IOAuthorize<any, any, InferPlatformRoomContextType<TPlatform>> = BaseIOAuthorize,
34
+ TAuthorize extends IOAuthorize<any, any, InferPlatformContextType<TPlatform>> = BaseIOAuthorize,
29
35
  TContext extends JsonObject = {},
30
36
  TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {},
31
37
  > = Partial<PluvIOListeners<TPlatform>> & {
@@ -37,13 +43,13 @@ export type PluvServerConfig<
37
43
  router?: PluvRouter<TPlatform, TAuthorize, TContext, TEvents>;
38
44
  };
39
45
 
40
- export type GetRoomOptions<TPlatform extends AbstractPlatform> = {
41
- debug?: boolean;
42
- } & InferPlatformRoomContextType<TPlatform>;
46
+ export type GetRoomOptions<TPlatform extends AbstractPlatform> = keyof InferRoomContextType<TPlatform> extends never
47
+ ? [{ debug?: boolean }] | []
48
+ : [Id<{ debug?: boolean } & InferRoomContextType<TPlatform>>];
43
49
 
44
50
  export class PluvServer<
45
51
  TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>,
46
- TAuthorize extends IOAuthorize<any, any, InferPlatformRoomContextType<TPlatform>> = BaseIOAuthorize,
52
+ TAuthorize extends IOAuthorize<any, any, InferPlatformContextType<TPlatform>> = BaseIOAuthorize,
47
53
  TContext extends JsonObject = {},
48
54
  TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {},
49
55
  > implements IORouterLike<TEvents>
@@ -63,6 +69,10 @@ export class PluvServer<
63
69
  return this._router._events;
64
70
  }
65
71
 
72
+ public get _registrationMode(): WebSocketRegistrationMode {
73
+ return this._platform._registrationMode;
74
+ }
75
+
66
76
  constructor(options: PluvServerConfig<TPlatform, TAuthorize, TContext, TEvents>) {
67
77
  const {
68
78
  authorize,
@@ -107,8 +117,11 @@ export class PluvServer<
107
117
  }).encode(params);
108
118
  }
109
119
 
110
- public getRoom(room: string, options: GetRoomOptions<TPlatform>): IORoom<TPlatform, TAuthorize, TContext, TEvents> {
111
- const { debug, ...platformRoomContext } = options;
120
+ public getRoom(
121
+ room: string,
122
+ ...options: GetRoomOptions<TPlatform>
123
+ ): IORoom<TPlatform, TAuthorize, TContext, TEvents> {
124
+ const { debug, ...platformRoomContext } = options[0] ?? {};
112
125
 
113
126
  this._purgeEmptyRooms();
114
127
 
@@ -124,24 +137,26 @@ export class PluvServer<
124
137
  const roomContext = {
125
138
  ...this._context,
126
139
  ...platformRoomContext,
127
- } as TContext & InferPlatformRoomContextType<TPlatform>;
140
+ } as TContext & InferRoomContextType<TPlatform>;
128
141
 
129
142
  const newRoom = new IORoom<TPlatform, TAuthorize, TContext, TEvents>(room, {
130
143
  authorize: this._authorize ?? undefined,
131
144
  context: roomContext,
132
145
  crdt: this._crdt,
133
146
  debug: debug ?? this._debug,
134
- onDestroy: ({ encodedState, room }) => {
147
+ onDestroy: async ({ encodedState, room }) => {
135
148
  this._logDebug(`${colors.blue("Deleting empty room:")} ${room}`);
136
149
 
137
150
  const roomContext = {
138
151
  room,
139
152
  encodedState,
140
153
  ...platformRoomContext,
141
- } as IORoomListenerEvent<TPlatform> & InferPlatformRoomContextType<TPlatform>;
154
+ } as IORoomListenerEvent<TPlatform> & InferRoomContextType<TPlatform>;
142
155
 
143
156
  this._rooms.delete(room);
144
- this._listeners.onRoomDeleted(roomContext);
157
+
158
+ await Promise.resolve(this._listeners.onRoomDeleted(roomContext));
159
+ await this._platform.persistance.deleteStorageState(room);
145
160
 
146
161
  if (this._debug) {
147
162
  const rooms = Array.from(this._rooms.keys());
package/src/authorize.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import hkdf from "@panva/hkdf";
2
2
  import type { BaseUser } from "@pluv/types";
3
3
  import { EncryptJWT, jwtDecrypt } from "jose";
4
- import type { AbstractPlatform, InferPlatformRoomContextType } from "./AbstractPlatform";
4
+ import type { AbstractPlatform, InferPlatformContextType } from "./AbstractPlatform";
5
5
 
6
6
  const DEFAULT_MAX_AGE_MS = 60_000;
7
7
 
@@ -19,7 +19,7 @@ export type JWTEncodeParams<TUser extends BaseUser, TPlatform extends AbstractPl
19
19
  maxAge?: number;
20
20
  room: string;
21
21
  user: TUser;
22
- } & InferPlatformRoomContextType<TPlatform>;
22
+ } & InferPlatformContextType<TPlatform>;
23
23
 
24
24
  export interface AuthorizeParams {
25
25
  platform: AbstractPlatform;
@@ -0,0 +1 @@
1
+ export const PING_TIMEOUT_MS = 30_000;
package/src/createIO.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { AbstractCrdtDocFactory } from "@pluv/crdt";
2
2
  import type { BaseUser, IOAuthorize, JsonObject } from "@pluv/types";
3
- import type { AbstractPlatform, InferPlatformRoomContextType } from "./AbstractPlatform";
3
+ import type { AbstractPlatform, InferPlatformContextType } from "./AbstractPlatform";
4
4
  import { PluvIO } from "./PluvIO";
5
5
  import type { GetInitialStorageFn, PluvIOListeners } from "./types";
6
6
 
@@ -10,7 +10,7 @@ export type CreateIOParams<
10
10
  TAuthorizeUser extends BaseUser = BaseUser,
11
11
  TAuthorizeRequired extends boolean = false,
12
12
  > = Partial<PluvIOListeners<TPlatform>> & {
13
- authorize?: IOAuthorize<TAuthorizeUser, TAuthorizeRequired, InferPlatformRoomContextType<TPlatform>>;
13
+ authorize?: IOAuthorize<TAuthorizeUser, TAuthorizeRequired, InferPlatformContextType<TPlatform>>;
14
14
  context?: TContext;
15
15
  crdt?: { doc: (value: any) => AbstractCrdtDocFactory<any> };
16
16
  debug?: boolean;
@@ -27,14 +27,14 @@ export const createIO = <
27
27
  params: CreateIOParams<TPlatform, TContext, TAuthorizeUser, TAuthorizeRequired>,
28
28
  ): PluvIO<
29
29
  TPlatform,
30
- IOAuthorize<TAuthorizeUser, TAuthorizeRequired, InferPlatformRoomContextType<TPlatform>>,
30
+ IOAuthorize<TAuthorizeUser, TAuthorizeRequired, InferPlatformContextType<TPlatform>>,
31
31
  TContext
32
32
  > => {
33
33
  const { authorize, context, crdt, debug, getInitialStorage, onRoomDeleted, onStorageUpdated, platform } = params;
34
34
 
35
35
  return new PluvIO<
36
36
  TPlatform,
37
- IOAuthorize<TAuthorizeUser, TAuthorizeRequired, InferPlatformRoomContextType<TPlatform>>,
37
+ IOAuthorize<TAuthorizeUser, TAuthorizeRequired, InferPlatformContextType<TPlatform>>,
38
38
  TContext
39
39
  >({
40
40
  authorize,
package/src/index.ts CHANGED
@@ -4,9 +4,10 @@ export { AbstractPlatform } from "./AbstractPlatform";
4
4
  export type {
5
5
  AbstractPlatformConfig,
6
6
  ConvertWebSocketConfig,
7
- InferPlatformEventContextType,
8
- InferPlatformRoomContextType,
7
+ InferPlatformContextType,
9
8
  InferPlatformWebSocketType,
9
+ InferRoomContextType,
10
+ WebSocketRegistrationMode,
10
11
  } from "./AbstractPlatform";
11
12
  export { AbstractPubSub } from "./AbstractPubSub";
12
13
  export type { IOPubSubEventMessage } from "./AbstractPubSub";
@@ -21,6 +22,9 @@ export type {
21
22
  AbstractWebSocketConfig,
22
23
  AbstractWebSocketHandleErrorParams,
23
24
  } from "./AbstractWebSocket";
25
+ export type { AuthorizeModule, AuthorizeParams, JWT, JWTEncodeParams } from "./authorize";
26
+ export { createIO } from "./createIO";
27
+ export type { CreateIOParams } from "./createIO";
24
28
  export type { IORoom } from "./IORoom";
25
29
  export { PluvIO } from "./PluvIO";
26
30
  export type { PluvIOConfig, ServerConfig } from "./PluvIO";
@@ -30,6 +34,4 @@ export { PluvRouter } from "./PluvRouter";
30
34
  export type { MergedRouter, PluvRouterEventConfig } from "./PluvRouter";
31
35
  export { PluvServer } from "./PluvServer";
32
36
  export type { GetRoomOptions, InferIORoom, PluvServerConfig } from "./PluvServer";
33
- export type { AuthorizeModule, AuthorizeParams, JWT, JWTEncodeParams } from "./authorize";
34
- export { createIO } from "./createIO";
35
- export type { CreateIOParams } from "./createIO";
37
+ export type { WebSocketSerializedState, WebSocketSession } from "./types";
package/src/types.ts CHANGED
@@ -9,7 +9,12 @@ import type {
9
9
  Maybe,
10
10
  MaybePromise,
11
11
  } from "@pluv/types";
12
- import type { AbstractPlatform, InferPlatformRoomContextType } from "./AbstractPlatform";
12
+ import type {
13
+ AbstractPlatform,
14
+ InferPlatformWebSocketSource,
15
+ InferPlatformWebSocketType,
16
+ InferRoomContextType,
17
+ } from "./AbstractPlatform";
13
18
  import type { AbstractWebSocket } from "./AbstractWebSocket";
14
19
 
15
20
  declare global {
@@ -20,7 +25,7 @@ declare global {
20
25
 
21
26
  export type EventResolver<
22
27
  TPlatform extends AbstractPlatform = AbstractPlatform,
23
- TAuthorize extends IOAuthorize<any, any, InferPlatformRoomContextType<TPlatform>> = BaseIOAuthorize,
28
+ TAuthorize extends IOAuthorize<any, any, InferRoomContextType<TPlatform>> = BaseIOAuthorize,
24
29
  TContext extends Record<string, any> = {},
25
30
  TInput extends JsonObject = {},
26
31
  TOutput extends EventRecord<string, any> = {},
@@ -28,7 +33,7 @@ export type EventResolver<
28
33
 
29
34
  export interface EventResolverContext<
30
35
  TPlatform extends AbstractPlatform = AbstractPlatform,
31
- TAuthorize extends IOAuthorize<any, any, InferPlatformRoomContextType<TPlatform>> = BaseIOAuthorize,
36
+ TAuthorize extends IOAuthorize<any, any, InferRoomContextType<TPlatform>> = BaseIOAuthorize,
32
37
  TContext extends Record<string, any> = {},
33
38
  > {
34
39
  context: TContext;
@@ -47,7 +52,7 @@ export interface EventResolverContext<
47
52
  * @date December 25, 2022
48
53
  */
49
54
  session: WebSocketSession<TAuthorize> | null;
50
- sessions: Map<string, WebSocketSession<TAuthorize>>;
55
+ sessions: readonly WebSocketSession<TAuthorize>[];
51
56
  }
52
57
 
53
58
  export type SendMessageOptions =
@@ -59,16 +64,20 @@ export interface WebSocketSessionTimers {
59
64
  ping: number;
60
65
  }
61
66
 
62
- export interface WebSocketSession<TAuthorize extends IOAuthorize<any, any, any> = BaseIOAuthorize> {
63
- id: string;
67
+ export interface WebSocketSerializedState {
64
68
  presence: JsonObject | null;
65
69
  quit: boolean;
66
70
  room: string;
67
71
  timers: WebSocketSessionTimers;
68
- webSocket: AbstractWebSocket;
69
- user: InferIOAuthorizeUser<TAuthorize>;
70
72
  }
71
73
 
74
+ export type WebSocketSession<TAuthorize extends IOAuthorize<any, any, any> = BaseIOAuthorize> =
75
+ WebSocketSerializedState & {
76
+ id: string;
77
+ user: InferIOAuthorizeUser<TAuthorize>;
78
+ webSocket: AbstractWebSocket;
79
+ };
80
+
72
81
  export type MergeEventRecords<
73
82
  TEventRecords extends EventRecord<string, any>[],
74
83
  TRoot extends EventRecord<string, any> = {},
@@ -86,7 +95,7 @@ export type MergeEventRecords<
86
95
 
87
96
  type GetInitialStorageEvent<TPlatform extends AbstractPlatform> = {
88
97
  room: string;
89
- } & InferPlatformRoomContextType<TPlatform>;
98
+ } & InferRoomContextType<TPlatform>;
90
99
 
91
100
  export type GetInitialStorageFn<TPlatform extends AbstractPlatform> = (
92
101
  event: GetInitialStorageEvent<TPlatform>,
@@ -99,5 +108,9 @@ export interface PluvIOListeners<TPlatform extends AbstractPlatform> {
99
108
 
100
109
  export type IORoomListenerEvent<TPlatform extends AbstractPlatform> = {
101
110
  room: string;
102
- encodedState: string;
103
- } & InferPlatformRoomContextType<TPlatform>;
111
+ encodedState: string | null;
112
+ } & InferRoomContextType<TPlatform>;
113
+
114
+ export type WebSocketType<TPlatform extends AbstractPlatform> =
115
+ | InferPlatformWebSocketType<TPlatform>
116
+ | InferPlatformWebSocketSource<TPlatform>;
@@ -1,2 +1,2 @@
1
1
  // Generated by genversion.
2
- export const version = '0.19.0'
2
+ export const version = '0.21.0'