@pluv/io 0.34.1 → 0.35.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.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { JsonObject, MaybePromise, IOAuthorize, IOEventMessage, EventRecord, InputZodLike, ProcedureLike, IORouterLike, BaseIOAuthorize, InferIOAuthorizeUser, Id, Maybe, BaseUser, UndefinedProps, InferEventMessage, InferEventsOutput, IOLike, InferIOOutput, InferIOInput, InferIOAuthorize } from '@pluv/types';
1
+ import { JsonObject, MaybePromise, IOAuthorize, IOEventMessage, EventRecord, InputZodLike, ProcedureLike, IORouterLike, InferIOAuthorizeUser, Id, Maybe, BaseUser, UndefinedProps, InferEventMessage, InferEventsOutput, IOLike, InferIOOutput, InferIOInput, InferIOAuthorize } from '@pluv/types';
2
2
  export { BaseUser, EventMessage, EventRecord, IOEventMessage } from '@pluv/types';
3
3
  import { AbstractCrdtDocFactory, AbstractCrdtDoc } from '@pluv/crdt';
4
4
 
@@ -77,18 +77,18 @@ declare abstract class AbstractWebSocket<TWebSocket = any> {
77
77
  abstract close(code?: number | undefined, reason?: string | undefined): void;
78
78
  abstract send(message: ArrayBuffer | ArrayBufferView | string): void;
79
79
  abstract terminate(): void;
80
- getSession<TAuthorize extends IOAuthorize<any, any, any>>(): Promise<WebSocketSession<TAuthorize>>;
80
+ getSession<TAuthorize extends IOAuthorize<any, any> | null>(): Promise<WebSocketSession<TAuthorize>>;
81
81
  handleError(params: AbstractWebSocketHandleErrorParams): void;
82
82
  sendMessage<TMessage extends IOEventMessage<any> = IOEventMessage<any>>(data: TMessage): void;
83
83
  }
84
84
 
85
- interface PluvProcedureConfig<TPlatform extends AbstractPlatform<any>, TAuthorize extends IOAuthorize<any, any, InferInitContextType<TPlatform>>, TContext extends Record<string, any>, TInput extends JsonObject = {}, TOutput extends EventRecord<string, any> = {}> {
85
+ interface PluvProcedureConfig<TPlatform extends AbstractPlatform<any>, TAuthorize extends IOAuthorize<any, InferInitContextType<TPlatform>> | null, TContext extends Record<string, any>, TInput extends JsonObject = {}, TOutput extends EventRecord<string, any> = {}> {
86
86
  broadcast?: EventResolver<"broadcast", TPlatform, TAuthorize, TContext, TInput, Partial<TOutput>> | null;
87
87
  input?: InputZodLike<TInput>;
88
88
  self?: EventResolver<"self", TPlatform, TAuthorize, TContext, TInput, Partial<TOutput>> | null;
89
89
  sync?: EventResolver<"sync", TPlatform, TAuthorize, TContext, TInput, Partial<TOutput>> | null;
90
90
  }
91
- declare class PluvProcedure<TPlatform extends AbstractPlatform<any>, TAuthorize extends IOAuthorize<any, any, InferInitContextType<TPlatform>>, TContext extends Record<string, any>, TInput extends JsonObject = {}, TOutput extends EventRecord<string, any> = {}, TFilled extends "input" | "broadcast" | "self" | "sync" | "" = ""> {
91
+ declare class PluvProcedure<TPlatform extends AbstractPlatform<any>, TAuthorize extends IOAuthorize<any, InferInitContextType<TPlatform>> | null, TContext extends Record<string, any>, TInput extends JsonObject = {}, TOutput extends EventRecord<string, any> = {}, TFilled extends "input" | "broadcast" | "self" | "sync" | "" = ""> {
92
92
  private _broadcast;
93
93
  private _input;
94
94
  private _self;
@@ -102,14 +102,14 @@ declare class PluvProcedure<TPlatform extends AbstractPlatform<any>, TAuthorize
102
102
  private _resolver;
103
103
  }
104
104
 
105
- type PluvRouterEventConfig<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, any, InferInitContextType<TPlatform>> = any, TContext extends Record<string, any> = {}> = {
105
+ type PluvRouterEventConfig<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null = any, TContext extends Record<string, any> = {}> = {
106
106
  [P: string]: Pick<PluvProcedure<TPlatform, TAuthorize, TContext, any, any>, "config">;
107
107
  };
108
- type MergedRouter<TRouters extends PluvRouter<TPlatform, TAuthorize, TContext, any>[] = [], TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, any, InferInitContextType<TPlatform>> = any, TContext extends Record<string, any> = {}, TRoot extends TRouters[0]["_defs"]["events"] = {}> = TRouters extends [
108
+ type MergedRouter<TRouters extends PluvRouter<TPlatform, TAuthorize, TContext, any>[] = [], TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> = any, TContext extends Record<string, any> = {}, TRoot extends TRouters[0]["_defs"]["events"] = {}> = TRouters extends [
109
109
  infer IHead extends PluvRouter<TPlatform, TAuthorize, TContext, any>,
110
110
  ...infer ITail extends PluvRouter<TPlatform, TAuthorize, TContext, any>[]
111
111
  ] ? MergedRouter<ITail, TPlatform, TAuthorize, TContext, TRoot & IHead["_defs"]["events"]> : PluvRouter<TPlatform, TAuthorize, TContext, TRoot>;
112
- declare class PluvRouter<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, any, InferInitContextType<TPlatform>> = any, TContext extends Record<string, any> = {}, TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {}> implements IORouterLike<TEvents> {
112
+ declare class PluvRouter<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null = any, TContext extends Record<string, any> = {}, TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {}> implements IORouterLike<TEvents> {
113
113
  readonly _defs: {
114
114
  events: TEvents;
115
115
  };
@@ -128,8 +128,8 @@ interface CrdtLibraryType {
128
128
  kind: "loro" | "yjs";
129
129
  }
130
130
  type EventResolverKind = "broadcast" | "self" | "sync";
131
- type EventResolver<TKind extends EventResolverKind = EventResolverKind, TPlatform extends AbstractPlatform = AbstractPlatform, TAuthorize extends IOAuthorize<any, any, InferInitContextType<TPlatform>> = BaseIOAuthorize, TContext extends Record<string, any> = {}, TInput extends JsonObject = {}, TOutput extends EventRecord<string, any> = {}> = (data: TInput, context: EventResolverContext<TKind, TPlatform, TAuthorize, TContext>) => MaybePromise<TOutput | void>;
132
- interface EventResolverContext<TKind extends EventResolverKind = EventResolverKind, TPlatform extends AbstractPlatform = AbstractPlatform, TAuthorize extends IOAuthorize<any, any, InferInitContextType<TPlatform>> = BaseIOAuthorize, TContext extends Record<string, any> = {}> {
131
+ type EventResolver<TKind extends EventResolverKind = EventResolverKind, TPlatform extends AbstractPlatform = AbstractPlatform, TAuthorize extends IOAuthorize<any, InferInitContextType<TPlatform>> | null = null, TContext extends Record<string, any> = {}, TInput extends JsonObject = {}, TOutput extends EventRecord<string, any> = {}> = (data: TInput, context: EventResolverContext<TKind, TPlatform, TAuthorize, TContext>) => MaybePromise<TOutput | void>;
132
+ interface EventResolverContext<TKind extends EventResolverKind = EventResolverKind, TPlatform extends AbstractPlatform = AbstractPlatform, TAuthorize extends IOAuthorize<any, InferInitContextType<TPlatform>> | null = null, TContext extends Record<string, any> = {}> {
133
133
  context: TContext;
134
134
  doc: AbstractCrdtDoc<any>;
135
135
  room: string;
@@ -153,7 +153,7 @@ interface WebSocketSerializedState {
153
153
  room: string;
154
154
  timers: WebSocketSessionTimers;
155
155
  }
156
- type WebSocketSession<TAuthorize extends IOAuthorize<any, any, any> = BaseIOAuthorize> = WebSocketSerializedState & {
156
+ type WebSocketSession<TAuthorize extends IOAuthorize<any, any> | null = null> = WebSocketSerializedState & {
157
157
  id: string;
158
158
  user: InferIOAuthorizeUser<TAuthorize>;
159
159
  webSocket: AbstractWebSocket;
@@ -173,7 +173,6 @@ type HandleMode = "io" | "fetch";
173
173
  type WebSocketRegistrationMode = "attached" | "detached";
174
174
  interface PlatformConfig {
175
175
  authorize: {
176
- required?: boolean;
177
176
  secret?: boolean;
178
177
  };
179
178
  requireAuth?: boolean;
@@ -187,21 +186,20 @@ interface PlatformConfig {
187
186
  onUserDisconnected?: boolean;
188
187
  };
189
188
  }
190
- type ResolvedPluvIOAuthorize<TPlatform extends AbstractPlatform<any, any, any, any>, TUser extends BaseUser = any, TRequired extends boolean = false> = {
189
+ type ResolvedPluvIOAuthorize<TPlatform extends AbstractPlatform<any, any, any, any>, TUser extends BaseUser = any> = {
191
190
  user: InputZodLike<TUser>;
192
- required?: TRequired;
193
191
  } & UndefinedProps<{
194
192
  secret?: string;
195
193
  }, Exclude<"secret", InferPlatformAuthorizeProperties<TPlatform>>>;
196
- type PluvIOAuthorize<TPlatform extends AbstractPlatform<any, any, any, any>, TUser extends BaseUser = any, TRequired extends boolean = false, TContext extends Record<string, unknown> = {}> = ResolvedPluvIOAuthorize<TPlatform, TUser, TRequired> | ((context: TContext) => ResolvedPluvIOAuthorize<TPlatform, TUser, TRequired>);
197
- type BasePluvIOListeners<TPlatform extends AbstractPlatform<any, any, any, any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, any, InferInitContextType<TPlatform>>, TContext extends Record<string, any>, TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext>> = {
194
+ type PluvIOAuthorize<TPlatform extends AbstractPlatform<any, any, any, any>, TUser extends BaseUser = any, TContext extends Record<string, unknown> = {}> = ResolvedPluvIOAuthorize<TPlatform, TUser> | ((context: TContext) => ResolvedPluvIOAuthorize<TPlatform, TUser>);
195
+ type BasePluvIOListeners<TPlatform extends AbstractPlatform<any, any, any, any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null, TContext extends Record<string, any>, TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext>> = {
198
196
  onRoomDeleted: (event: IORoomListenerEvent<TContext>) => void;
199
197
  onRoomMessage: (event: IORoomMessageEvent<TPlatform, TAuthorize, TContext, TEvents>) => void;
200
198
  onStorageUpdated: (event: IOStorageUpdatedEvent<TPlatform, TAuthorize, TContext>) => void;
201
199
  onUserConnected: (event: IOUserConnectedEvent<TPlatform, TAuthorize, TContext>) => void;
202
200
  onUserDisconnected: (event: IOUserDisconnectedEvent<TPlatform, TAuthorize, TContext>) => void;
203
201
  };
204
- type PluvIOListeners<TPlatform extends AbstractPlatform<any, any, any, any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, any, InferInitContextType<TPlatform>>, TContext extends Record<string, any>, TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext>> = UndefinedProps<BasePluvIOListeners<TPlatform, TAuthorize, TContext, TEvents>, Exclude<keyof BasePluvIOListeners<TPlatform, TAuthorize, TContext, TEvents>, InferPlatformListeners<TPlatform>>>;
202
+ type PluvIOListeners<TPlatform extends AbstractPlatform<any, any, any, any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null, TContext extends Record<string, any>, TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext>> = UndefinedProps<BasePluvIOListeners<TPlatform, TAuthorize, TContext, TEvents>, Exclude<keyof BasePluvIOListeners<TPlatform, TAuthorize, TContext, TEvents>, InferPlatformListeners<TPlatform>>>;
205
203
  type InferPlatformConfig<TPlatform extends AbstractPlatform<any, any, any, any>> = TPlatform extends AbstractPlatform<any, any, any, infer IConfig> ? IConfig : never;
206
204
  type InferPlatformAuthorizeProperties<TPlatform extends AbstractPlatform<any, any, any, any>> = keyof {
207
205
  [P in keyof PlatformConfig["authorize"] as InferPlatformConfig<TPlatform>["authorize"][P] extends true | undefined ? P : never]: true;
@@ -214,20 +212,20 @@ type IORoomListenerEvent<TContext extends Record<string, any>> = {
214
212
  encodedState: string | null;
215
213
  room: string;
216
214
  };
217
- type IORoomMessageEvent<TPlatform extends AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, any, InferInitContextType<TPlatform>>, TContext extends Record<string, any>, TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext>> = IORoomListenerEvent<TContext> & {
215
+ type IORoomMessageEvent<TPlatform extends AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null, TContext extends Record<string, any>, TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext>> = IORoomListenerEvent<TContext> & {
218
216
  message: InferEventMessage<InferEventsOutput<TEvents>, keyof InferEventsOutput<TEvents>>;
219
217
  user?: InferIOAuthorizeUser<TAuthorize>;
220
218
  webSocket?: InferPlatformWebSocketSource<TPlatform>;
221
219
  };
222
- type IOStorageUpdatedEvent<TPlatform extends AbstractPlatform<any>, TAuthorize extends IOAuthorize<any, any, InferInitContextType<TPlatform>>, TContext extends Record<string, any>> = IORoomListenerEvent<TContext> & {
220
+ type IOStorageUpdatedEvent<TPlatform extends AbstractPlatform<any>, TAuthorize extends IOAuthorize<any, InferInitContextType<TPlatform>> | null, TContext extends Record<string, any>> = IORoomListenerEvent<TContext> & {
223
221
  user?: InferIOAuthorizeUser<TAuthorize>;
224
222
  webSocket?: InferPlatformWebSocketSource<TPlatform>;
225
223
  };
226
- type IOUserConnectedEvent<TPlatform extends AbstractPlatform<any>, TAuthorize extends IOAuthorize<any, any, InferInitContextType<TPlatform>>, TContext extends Record<string, any>> = IORoomListenerEvent<TContext> & {
224
+ type IOUserConnectedEvent<TPlatform extends AbstractPlatform<any>, TAuthorize extends IOAuthorize<any, InferInitContextType<TPlatform>> | null, TContext extends Record<string, any>> = IORoomListenerEvent<TContext> & {
227
225
  user?: InferIOAuthorizeUser<TAuthorize>;
228
226
  webSocket?: InferPlatformWebSocketSource<TPlatform>;
229
227
  };
230
- type IOUserDisconnectedEvent<TPlatform extends AbstractPlatform<any>, TAuthorize extends IOAuthorize<any, any, InferInitContextType<TPlatform>>, TContext extends Record<string, any>> = IORoomListenerEvent<TContext> & {
228
+ type IOUserDisconnectedEvent<TPlatform extends AbstractPlatform<any>, TAuthorize extends IOAuthorize<any, InferInitContextType<TPlatform>> | null, TContext extends Record<string, any>> = IORoomListenerEvent<TContext> & {
231
229
  user?: InferIOAuthorizeUser<TAuthorize>;
232
230
  };
233
231
  type WebSocketType<TPlatform extends AbstractPlatform> = InferPlatformWebSocketType<TPlatform> | InferPlatformWebSocketSource<TPlatform>;
@@ -246,10 +244,10 @@ interface JWT<TUser extends BaseUser> {
246
244
  sub: string;
247
245
  user: TUser;
248
246
  }
249
- type JWTEncodeParams<TUser extends BaseUser, TPlatform extends AbstractPlatform> = {
247
+ type JWTEncodeParams<TUser extends BaseUser | null, TPlatform extends AbstractPlatform> = {
250
248
  maxAge?: number;
251
249
  room: string;
252
- user: TUser;
250
+ user: TUser extends BaseUser ? TUser : "Error: Cannot create token without authorization!";
253
251
  } & InferInitContextType<TPlatform>;
254
252
  interface AuthorizeParams {
255
253
  platform: AbstractPlatform;
@@ -301,7 +299,7 @@ declare abstract class AbstractPlatform<TWebSocket extends AbstractWebSocket = A
301
299
  protected _initialize(): this;
302
300
  }
303
301
 
304
- interface IORoomListeners<TPlatform extends AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, any, InferInitContextType<TPlatform>>, TContext extends Record<string, any>, TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext>> {
302
+ interface IORoomListeners<TPlatform extends AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null, TContext extends Record<string, any>, TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext>> {
305
303
  onDestroy: (event: IORoomListenerEvent<TContext>) => void;
306
304
  onMessage: (event: IORoomMessageEvent<TPlatform, TAuthorize, TContext, TEvents>) => void;
307
305
  onUserConnected: (event: IOUserConnectedEvent<TPlatform, TAuthorize, TContext>) => void;
@@ -310,7 +308,7 @@ interface IORoomListeners<TPlatform extends AbstractPlatform<any>, TAuthorize ex
310
308
  type BroadcastProxy<TIO extends IORoom<any, any, any, any>> = (<TEvent extends keyof InferIOInput<TIO>>(event: TEvent, data: Id<InferIOInput<TIO>[TEvent]>) => Promise<void>) & {
311
309
  [event in keyof InferIOInput<TIO>]: (data: Id<InferIOInput<TIO>>[event]) => Promise<void>;
312
310
  };
313
- type IORoomConfig<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, any, InferInitContextType<TPlatform>> = any, TContext extends Record<string, any> = {}, TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {}> = Partial<IORoomListeners<TPlatform, TAuthorize, TContext, TEvents>> & {
311
+ type IORoomConfig<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null = any, TContext extends Record<string, any> = {}, TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {}> = Partial<IORoomListeners<TPlatform, TAuthorize, TContext, TEvents>> & {
314
312
  authorize?: TAuthorize;
315
313
  context: TContext;
316
314
  crdt?: {
@@ -324,7 +322,7 @@ type IORoomConfig<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any
324
322
  type WebsocketRegisterConfig<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>> = {
325
323
  token?: string | null;
326
324
  } & InferInitContextType<TPlatform>;
327
- declare class IORoom<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, any, InferInitContextType<TPlatform>> = any, TContext extends Record<string, any> = {}, TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {}> implements IOLike<TAuthorize, TEvents> {
325
+ declare class IORoom<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null = null, TContext extends Record<string, any> = {}, TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {}> implements IOLike<TAuthorize, TEvents> {
328
326
  readonly id: string;
329
327
  private _doc;
330
328
  private _uninitialize;
@@ -342,7 +340,7 @@ declare class IORoom<TPlatform extends AbstractPlatform<any> = AbstractPlatform<
342
340
  * @deprecated Internal use only. Changes to this will never be marked as breaking.
343
341
  */
344
342
  get _defs(): {
345
- authorize: TAuthorize | null;
343
+ authorize: TAuthorize;
346
344
  context: TContext;
347
345
  events: TEvents;
348
346
  platform: TPlatform;
@@ -392,7 +390,7 @@ declare class IORoom<TPlatform extends AbstractPlatform<any> = AbstractPlatform<
392
390
  }
393
391
 
394
392
  type InferIORoom<TServer extends PluvServer<any, any, any, any>> = TServer extends PluvServer<infer IPlatform, infer IAuthorize, infer IContext, infer IEvents> ? IORoom<IPlatform, IAuthorize, IContext, IEvents> : never;
395
- type PluvServerConfig<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, any, InferInitContextType<TPlatform>> = any, TContext extends Record<string, any> = {}, TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {}> = Partial<PluvIOListeners<TPlatform, TAuthorize, TContext, TEvents>> & {
393
+ type PluvServerConfig<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null = any, TContext extends Record<string, any> = {}, TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {}> = Partial<PluvIOListeners<TPlatform, TAuthorize, TContext, TEvents>> & {
396
394
  authorize?: TAuthorize;
397
395
  context?: PluvContext<TPlatform, TContext>;
398
396
  crdt?: {
@@ -403,11 +401,11 @@ type PluvServerConfig<TPlatform extends AbstractPlatform<any> = AbstractPlatform
403
401
  platform: TPlatform;
404
402
  router?: PluvRouter<TPlatform, TAuthorize, TContext, TEvents>;
405
403
  };
406
- type BaseCreateRoomOptions<TPlatform extends AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, any, InferInitContextType<TPlatform>>, TContext extends Record<string, any>, TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext>> = Partial<IORoomListeners<TPlatform, TAuthorize, TContext, TEvents>> & {
404
+ type BaseCreateRoomOptions<TPlatform extends AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null, TContext extends Record<string, any>, TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext>> = Partial<IORoomListeners<TPlatform, TAuthorize, TContext, TEvents>> & {
407
405
  debug?: boolean;
408
406
  };
409
- type CreateRoomOptions<TPlatform extends AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, any, InferInitContextType<TPlatform>>, TContext extends Record<string, any>, TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext>> = keyof InferRoomContextType<TPlatform> extends never ? [BaseCreateRoomOptions<TPlatform, TAuthorize, TContext, TEvents>] | [] : [Id<BaseCreateRoomOptions<TPlatform, TAuthorize, TContext, TEvents> & InferRoomContextType<TPlatform>>];
410
- declare class PluvServer<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, any, InferInitContextType<TPlatform>> = any, TContext extends Record<string, any> = {}, TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {}> implements IOLike<TAuthorize, TEvents> {
407
+ type CreateRoomOptions<TPlatform extends AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null, TContext extends Record<string, any>, TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext>> = keyof InferRoomContextType<TPlatform> extends never ? [BaseCreateRoomOptions<TPlatform, TAuthorize, TContext, TEvents>] | [] : [Id<BaseCreateRoomOptions<TPlatform, TAuthorize, TContext, TEvents> & InferRoomContextType<TPlatform>>];
408
+ declare class PluvServer<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null = any, TContext extends Record<string, any> = {}, TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {}> implements IOLike<TAuthorize, TEvents> {
411
409
  readonly version: string;
412
410
  private readonly _authorize;
413
411
  private readonly _baseRouter;
@@ -424,8 +422,8 @@ declare class PluvServer<TPlatform extends AbstractPlatform<any> = AbstractPlatf
424
422
  * @deprecated Internal use only. Changes to this will never be marked as breaking.
425
423
  */
426
424
  get _defs(): {
427
- authorize: TAuthorize | null;
428
- context: PluvContext<TPlatform, TContext>;
425
+ authorize: TAuthorize;
426
+ context: TContext;
429
427
  events: TEvents;
430
428
  platform: TPlatform;
431
429
  };
@@ -438,21 +436,18 @@ declare class PluvServer<TPlatform extends AbstractPlatform<any> = AbstractPlatf
438
436
  private _logDebug;
439
437
  }
440
438
 
441
- type PluvIOConfig<TPlatform extends AbstractPlatform<any>, TAuthorize extends IOAuthorize<any, any, InferInitContextType<TPlatform>>, TContext extends Record<string, any>> = {
439
+ type PluvIOConfig<TPlatform extends AbstractPlatform<any>, TAuthorize extends IOAuthorize<any, InferInitContextType<TPlatform>> | null, TContext extends Record<string, any>> = {
440
+ authorize?: TAuthorize;
442
441
  context?: PluvContext<TPlatform, TContext>;
443
442
  crdt?: CrdtLibraryType;
444
443
  debug?: boolean;
445
444
  platform: TPlatform;
446
- } & (InferPlatformConfig<TPlatform>["requireAuth"] extends true ? {
447
- authorize: TAuthorize;
448
- } : {
449
- authorize?: TAuthorize;
450
- });
451
- type ServerConfig<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, any, InferInitContextType<TPlatform>> = any, TContext extends Record<string, any> = {}, TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {}> = Partial<PluvIOListeners<TPlatform, TAuthorize, TContext, TEvents>> & {
445
+ };
446
+ type ServerConfig<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null = any, TContext extends Record<string, any> = {}, TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {}> = Partial<PluvIOListeners<TPlatform, TAuthorize, TContext, TEvents>> & {
452
447
  getInitialStorage?: GetInitialStorageFn<TContext>;
453
448
  router?: PluvRouter<TPlatform, TAuthorize, TContext, TEvents>;
454
449
  };
455
- declare class PluvIO<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, any, InferInitContextType<TPlatform>> = any, TContext extends Record<string, any> = {}> {
450
+ declare class PluvIO<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null = any, TContext extends Record<string, any> = {}> {
456
451
  readonly version: string;
457
452
  private readonly _authorize;
458
453
  private readonly _context;
@@ -468,13 +463,13 @@ declare class PluvIO<TPlatform extends AbstractPlatform<any> = AbstractPlatform<
468
463
  private _getIOAuthorize;
469
464
  }
470
465
 
471
- type CreateIOParams<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TContext extends Record<string, any> = {}, TAuthorizeUser extends BaseUser = BaseUser, TAuthorizeRequired extends boolean = false> = {
472
- authorize?: PluvIOAuthorize<TPlatform, TAuthorizeUser, TAuthorizeRequired, InferInitContextType<TPlatform>>;
466
+ type CreateIOParams<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TContext extends Record<string, any> = {}, TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null = null> = {
467
+ authorize?: TAuthorize;
473
468
  context?: PluvContext<TPlatform, TContext>;
474
469
  crdt?: CrdtLibraryType;
475
470
  debug?: boolean;
476
471
  platform: TPlatform;
477
472
  };
478
- declare const createIO: <TPlatform extends AbstractPlatform<any> = AbstractPlatform<any, {}, {}, PlatformConfig>, TContext extends Record<string, any> = {}, TAuthorizeUser extends BaseUser = BaseUser, TAuthorizeRequired extends boolean = false>(params: CreateIOParams<TPlatform, TContext, TAuthorizeUser, TAuthorizeRequired>) => PluvIO<TPlatform, PluvIOAuthorize<TPlatform, TAuthorizeUser, TAuthorizeRequired, InferInitContextType<TPlatform>>, TContext>;
473
+ declare const createIO: <TPlatform extends AbstractPlatform<any> = AbstractPlatform<any, {}, {}, PlatformConfig>, TContext extends Record<string, any> = {}, TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null = null>(params: CreateIOParams<TPlatform, TContext, TAuthorize>) => PluvIO<TPlatform, TAuthorize, TContext>;
479
474
 
480
475
  export { type AbstractCloseEvent, type AbstractErrorEvent, type AbstractEvent, type AbstractEventMap, type AbstractListener, type AbstractMessageEvent, AbstractPersistence, AbstractPlatform, type AbstractPlatformConfig, AbstractPubSub, AbstractWebSocket, type AbstractWebSocketConfig, type AbstractWebSocketHandleErrorParams, type AuthorizeModule, type AuthorizeParams, type ConvertWebSocketConfig, type CrdtLibraryType, type CreateIOParams, type CreateRoomOptions, type GetInitialStorageFn, type HandleMode, type IOPubSubEventMessage, IORoom, type IORoomListenerEvent, type IORoomMessageEvent, type IOStorageUpdatedEvent, type IOUserConnectedEvent, type IOUserDisconnectedEvent, type InferIORoom, type InferInitContextType, type InferPlatformWebSocketType, type InferRoomContextType, type JWT, type JWTEncodeParams, type MergedRouter, type PlatformConfig, PluvIO, type PluvIOConfig, type PluvIOListeners, PluvProcedure, type PluvProcedureConfig, PluvRouter, type PluvRouterEventConfig, PluvServer, type PluvServerConfig, type ServerConfig, type WebSocketRegistrationMode, type WebSocketSerializedState, type WebSocketSession, authorize, createIO };
package/dist/index.js CHANGED
@@ -270,8 +270,9 @@ var AbstractWebSocket = class {
270
270
  const { error, message: _message, room, session } = params;
271
271
  const message = error instanceof Error ? error.message : "Unexpected error";
272
272
  const stack = error instanceof Error ? (_a = error.stack) != null ? _a : null : null;
273
+ const connectionId = (_b = session == null ? void 0 : session.id) != null ? _b : null;
273
274
  return this.sendMessage({
274
- connectionId: (_b = session == null ? void 0 : session.id) != null ? _b : null,
275
+ connectionId,
275
276
  data: { message, stack },
276
277
  room,
277
278
  type: "$ERROR",
@@ -539,7 +540,7 @@ var IORoom = class {
539
540
  }
540
541
  const user = yield this._getAuthorizedUser(token, _options);
541
542
  const ioAuthorize = this._getIOAuthorize(_options);
542
- const isUnauthorized = !!(ioAuthorize == null ? void 0 : ioAuthorize.required) && !user;
543
+ const isUnauthorized = !!ioAuthorize && !user;
543
544
  const isTokenInUse = !!user && (yield this._getSessions().then((sessions) => sessions.some((session) => {
544
545
  var _a2;
545
546
  return ((_a2 = session.user) == null ? void 0 : _a2.id) === user.id;
@@ -575,6 +576,7 @@ var IORoom = class {
575
576
  const sender = senderId ? (_a = this._sessions.get(senderId)) != null ? _a : null : null;
576
577
  const session = (_b = yield sender == null ? void 0 : sender.getSession()) != null ? _b : null;
577
578
  const user = (_c = session == null ? void 0 : session.user) != null ? _c : null;
579
+ if (typeof senderId !== "string") return;
578
580
  this._platform.pubSub.publish(this.id, __spreadValues({
579
581
  connectionId: senderId != null ? senderId : null,
580
582
  room: this.id,
@@ -923,6 +925,7 @@ var IORoom = class {
923
925
  const { data, type } = message;
924
926
  const connectionId = (_a = sender == null ? void 0 : sender.sessionId) != null ? _a : null;
925
927
  const room = this.id;
928
+ if (typeof connectionId !== "string") return;
926
929
  const webSockets = (_b = sessionIds == null ? void 0 : sessionIds.reduce((dict, id) => {
927
930
  const pluvWs = this._sessions.get(id);
928
931
  return pluvWs ? dict.set(id, pluvWs) : dict;
@@ -957,6 +960,8 @@ var IORoom = class {
957
960
  return __async(this, null, function* () {
958
961
  var _a;
959
962
  if (!sender) return;
963
+ const connectionId = sender.sessionId;
964
+ if (typeof connectionId !== "string") return;
960
965
  const doc = yield this._doc;
961
966
  const context = {
962
967
  context: this._context,
@@ -979,7 +984,7 @@ var IORoom = class {
979
984
  var _a2, _b;
980
985
  const data = (_a2 = output[type]) != null ? _a2 : {};
981
986
  this._platform.pubSub.publish(this.id, {
982
- connectionId: sender.sessionId,
987
+ connectionId,
983
988
  data,
984
989
  options: { type: "self" },
985
990
  room: this.id,
@@ -993,7 +998,7 @@ var IORoom = class {
993
998
  };
994
999
 
995
1000
  // src/version.generated.ts
996
- var version = "0.34.1";
1001
+ var version = "0.35.1";
997
1002
 
998
1003
  // src/PluvServer.ts
999
1004
  var PluvServer = class {
package/dist/index.mjs CHANGED
@@ -228,8 +228,9 @@ var AbstractWebSocket = class {
228
228
  const { error, message: _message, room, session } = params;
229
229
  const message = error instanceof Error ? error.message : "Unexpected error";
230
230
  const stack = error instanceof Error ? (_a = error.stack) != null ? _a : null : null;
231
+ const connectionId = (_b = session == null ? void 0 : session.id) != null ? _b : null;
231
232
  return this.sendMessage({
232
- connectionId: (_b = session == null ? void 0 : session.id) != null ? _b : null,
233
+ connectionId,
233
234
  data: { message, stack },
234
235
  room,
235
236
  type: "$ERROR",
@@ -497,7 +498,7 @@ var IORoom = class {
497
498
  }
498
499
  const user = yield this._getAuthorizedUser(token, _options);
499
500
  const ioAuthorize = this._getIOAuthorize(_options);
500
- const isUnauthorized = !!(ioAuthorize == null ? void 0 : ioAuthorize.required) && !user;
501
+ const isUnauthorized = !!ioAuthorize && !user;
501
502
  const isTokenInUse = !!user && (yield this._getSessions().then((sessions) => sessions.some((session) => {
502
503
  var _a2;
503
504
  return ((_a2 = session.user) == null ? void 0 : _a2.id) === user.id;
@@ -533,6 +534,7 @@ var IORoom = class {
533
534
  const sender = senderId ? (_a = this._sessions.get(senderId)) != null ? _a : null : null;
534
535
  const session = (_b = yield sender == null ? void 0 : sender.getSession()) != null ? _b : null;
535
536
  const user = (_c = session == null ? void 0 : session.user) != null ? _c : null;
537
+ if (typeof senderId !== "string") return;
536
538
  this._platform.pubSub.publish(this.id, __spreadValues({
537
539
  connectionId: senderId != null ? senderId : null,
538
540
  room: this.id,
@@ -881,6 +883,7 @@ var IORoom = class {
881
883
  const { data, type } = message;
882
884
  const connectionId = (_a = sender == null ? void 0 : sender.sessionId) != null ? _a : null;
883
885
  const room = this.id;
886
+ if (typeof connectionId !== "string") return;
884
887
  const webSockets = (_b = sessionIds == null ? void 0 : sessionIds.reduce((dict, id) => {
885
888
  const pluvWs = this._sessions.get(id);
886
889
  return pluvWs ? dict.set(id, pluvWs) : dict;
@@ -915,6 +918,8 @@ var IORoom = class {
915
918
  return __async(this, null, function* () {
916
919
  var _a;
917
920
  if (!sender) return;
921
+ const connectionId = sender.sessionId;
922
+ if (typeof connectionId !== "string") return;
918
923
  const doc = yield this._doc;
919
924
  const context = {
920
925
  context: this._context,
@@ -937,7 +942,7 @@ var IORoom = class {
937
942
  var _a2, _b;
938
943
  const data = (_a2 = output[type]) != null ? _a2 : {};
939
944
  this._platform.pubSub.publish(this.id, {
940
- connectionId: sender.sessionId,
945
+ connectionId,
941
946
  data,
942
947
  options: { type: "self" },
943
948
  room: this.id,
@@ -951,7 +956,7 @@ var IORoom = class {
951
956
  };
952
957
 
953
958
  // src/version.generated.ts
954
- var version = "0.34.1";
959
+ var version = "0.35.1";
955
960
 
956
961
  // src/PluvServer.ts
957
962
  var PluvServer = class {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pluv/io",
3
- "version": "0.34.1",
3
+ "version": "0.35.1",
4
4
  "description": "@pluv/io server-side websocket manager",
5
5
  "author": "leedavidcs",
6
6
  "license": "MIT",
@@ -21,16 +21,16 @@
21
21
  "jose": "^5.9.6",
22
22
  "kleur": "^4.1.5",
23
23
  "wonka": "^6.3.4",
24
- "@pluv/crdt": "^0.34.1",
25
- "@pluv/types": "^0.34.1"
24
+ "@pluv/crdt": "^0.35.1",
25
+ "@pluv/types": "^0.35.1"
26
26
  },
27
27
  "devDependencies": {
28
28
  "eslint": "^8.57.1",
29
29
  "genversion": "^3.2.0",
30
30
  "tsup": "^8.3.5",
31
- "typescript": "^5.7.2",
32
- "@pluv/tsconfig": "^0.34.1",
33
- "eslint-config-pluv": "^0.34.1"
31
+ "typescript": "^5.7.3",
32
+ "@pluv/tsconfig": "^0.35.1",
33
+ "eslint-config-pluv": "^0.35.1"
34
34
  },
35
35
  "scripts": {
36
36
  "build": "pnpm run version && tsup src/index.ts --format esm,cjs --dts",
@@ -100,7 +100,7 @@ export abstract class AbstractWebSocket<TWebSocket = any> {
100
100
 
101
101
  public abstract terminate(): void;
102
102
 
103
- public async getSession<TAuthorize extends IOAuthorize<any, any, any>>(): Promise<WebSocketSession<TAuthorize>> {
103
+ public async getSession<TAuthorize extends IOAuthorize<any, any> | null>(): Promise<WebSocketSession<TAuthorize>> {
104
104
  const sessionId = this.sessionId;
105
105
  const state = this.state;
106
106
  const room = state.room;
@@ -122,12 +122,13 @@ export abstract class AbstractWebSocket<TWebSocket = any> {
122
122
 
123
123
  const message = error instanceof Error ? error.message : "Unexpected error";
124
124
  const stack: string | null = error instanceof Error ? (error.stack ?? null) : null;
125
+ const connectionId = (session?.id ?? null) as string;
125
126
 
126
127
  return this.sendMessage({
127
- connectionId: session?.id ?? null,
128
+ connectionId,
128
129
  data: { message, stack },
129
130
  room,
130
- type: "$ERROR",
131
+ type: "$ERROR" as const,
131
132
  user: session?.user ?? null,
132
133
  });
133
134
  }
package/src/IORoom.ts CHANGED
@@ -50,7 +50,7 @@ interface BroadcastParams<TIO extends IORoom<any, any, any, any>> {
50
50
 
51
51
  export interface IORoomListeners<
52
52
  TPlatform extends AbstractPlatform<any>,
53
- TAuthorize extends PluvIOAuthorize<TPlatform, any, any, InferInitContextType<TPlatform>>,
53
+ TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null,
54
54
  TContext extends Record<string, any>,
55
55
  TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext>,
56
56
  > {
@@ -69,7 +69,7 @@ export type BroadcastProxy<TIO extends IORoom<any, any, any, any>> = (<TEvent ex
69
69
 
70
70
  export type IORoomConfig<
71
71
  TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>,
72
- TAuthorize extends PluvIOAuthorize<TPlatform, any, any, InferInitContextType<TPlatform>> = any,
72
+ TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null = any,
73
73
  TContext extends Record<string, any> = {},
74
74
  TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {},
75
75
  > = Partial<IORoomListeners<TPlatform, TAuthorize, TContext, TEvents>> & {
@@ -93,7 +93,7 @@ export type WebsocketRegisterConfig<TPlatform extends AbstractPlatform<any> = Ab
93
93
 
94
94
  export class IORoom<
95
95
  TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>,
96
- TAuthorize extends PluvIOAuthorize<TPlatform, any, any, InferInitContextType<TPlatform>> = any,
96
+ TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null = null,
97
97
  TContext extends Record<string, any> = {},
98
98
  TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {},
99
99
  > implements IOLike<TAuthorize, TEvents>
@@ -103,7 +103,7 @@ export class IORoom<
103
103
  private _doc: Promise<AbstractCrdtDoc<any>>;
104
104
  private _uninitialize: Promise<() => Promise<void>> | null = null;
105
105
 
106
- private readonly _authorize: TAuthorize | null = null;
106
+ private readonly _authorize: TAuthorize = null as TAuthorize;
107
107
  private readonly _context: TContext;
108
108
  private readonly _debug: boolean;
109
109
  private readonly _docFactory: AbstractCrdtDocFactory<any>;
@@ -123,6 +123,11 @@ export class IORoom<
123
123
  context: this._context,
124
124
  events: this._router._defs.events,
125
125
  platform: this._platform,
126
+ } as {
127
+ authorize: TAuthorize;
128
+ context: TContext;
129
+ events: TEvents;
130
+ platform: TPlatform;
126
131
  };
127
132
  }
128
133
 
@@ -295,7 +300,7 @@ export class IORoom<
295
300
 
296
301
  const user = await this._getAuthorizedUser(token, _options);
297
302
  const ioAuthorize = this._getIOAuthorize(_options);
298
- const isUnauthorized = !!ioAuthorize?.required && !user;
303
+ const isUnauthorized = !!ioAuthorize && !user;
299
304
 
300
305
  // There is an attempt for multiple of the same identities. Probably malicious.
301
306
  const isTokenInUse: boolean =
@@ -346,6 +351,8 @@ export class IORoom<
346
351
  const session = (await sender?.getSession()) ?? null;
347
352
  const user = session?.user ?? null;
348
353
 
354
+ if (typeof senderId !== "string") return;
355
+
349
356
  this._platform.pubSub.publish(this.id, {
350
357
  connectionId: senderId ?? null,
351
358
  room: this.id,
@@ -491,14 +498,12 @@ export class IORoom<
491
498
  return doc;
492
499
  }
493
500
 
494
- private _getIOAuthorize(
495
- options: WebsocketRegisterConfig<TPlatform>,
496
- ): ResolvedPluvIOAuthorize<any, any, any> | null {
501
+ private _getIOAuthorize(options: WebsocketRegisterConfig<TPlatform>): ResolvedPluvIOAuthorize<any, any> | null {
497
502
  if (typeof this._authorize === "function") {
498
503
  return this._authorize(options);
499
504
  }
500
505
 
501
- return this._authorize as ResolvedPluvIOAuthorize<any, any, any> | null;
506
+ return this._authorize as ResolvedPluvIOAuthorize<any, any> | null;
502
507
  }
503
508
 
504
509
  private _getProcedure(
@@ -782,6 +787,8 @@ export class IORoom<
782
787
  const connectionId = sender?.sessionId ?? null;
783
788
  const room = this.id;
784
789
 
790
+ if (typeof connectionId !== "string") return;
791
+
785
792
  const webSockets =
786
793
  sessionIds?.reduce((dict, id) => {
787
794
  const pluvWs = this._sessions.get(id);
@@ -829,6 +836,10 @@ export class IORoom<
829
836
  ): Promise<void> {
830
837
  if (!sender) return;
831
838
 
839
+ const connectionId = sender.sessionId;
840
+
841
+ if (typeof connectionId !== "string") return;
842
+
832
843
  const doc = await this._doc;
833
844
  const context: EventResolverContext<"sync", TPlatform, TAuthorize, TContext> = {
834
845
  context: this._context,
@@ -857,7 +868,7 @@ export class IORoom<
857
868
  const data = output[type] ?? {};
858
869
 
859
870
  this._platform.pubSub.publish(this.id, {
860
- connectionId: sender.sessionId,
871
+ connectionId,
861
872
  data,
862
873
  options: { type: "self" },
863
874
  room: this.id,
package/src/PluvIO.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  import type { AbstractCrdtDocFactory } from "@pluv/crdt";
2
2
  import { noop } from "@pluv/crdt";
3
- import type { IOAuthorize, InferIOAuthorizeUser, InputZodLike } from "@pluv/types";
3
+ import type { IOAuthorize, InferIOAuthorizeUser } from "@pluv/types";
4
4
  import type { AbstractPlatform, InferInitContextType } from "./AbstractPlatform";
5
+ import { WebsocketRegisterConfig } from "./IORoom";
5
6
  import { PluvProcedure } from "./PluvProcedure";
6
7
  import type { MergedRouter, PluvRouterEventConfig } from "./PluvRouter";
7
8
  import { PluvRouter } from "./PluvRouter";
@@ -11,31 +12,28 @@ import { authorize } from "./authorize";
11
12
  import type {
12
13
  CrdtLibraryType,
13
14
  GetInitialStorageFn,
14
- InferPlatformConfig,
15
15
  PluvContext,
16
16
  PluvIOAuthorize,
17
17
  PluvIOListeners,
18
18
  ResolvedPluvIOAuthorize,
19
19
  } from "./types";
20
20
  import { __PLUV_VERSION } from "./version";
21
- import { WebsocketRegisterConfig } from "./IORoom";
22
21
 
23
22
  export type PluvIOConfig<
24
23
  TPlatform extends AbstractPlatform<any>,
25
- TAuthorize extends IOAuthorize<any, any, InferInitContextType<TPlatform>>,
24
+ TAuthorize extends IOAuthorize<any, InferInitContextType<TPlatform>> | null,
26
25
  TContext extends Record<string, any>,
27
26
  > = {
27
+ authorize?: TAuthorize;
28
28
  context?: PluvContext<TPlatform, TContext>;
29
29
  crdt?: CrdtLibraryType;
30
30
  debug?: boolean;
31
31
  platform: TPlatform;
32
- } & (InferPlatformConfig<TPlatform>["requireAuth"] extends true
33
- ? { authorize: TAuthorize }
34
- : { authorize?: TAuthorize });
32
+ };
35
33
 
36
34
  export type ServerConfig<
37
35
  TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>,
38
- TAuthorize extends PluvIOAuthorize<TPlatform, any, any, InferInitContextType<TPlatform>> = any,
36
+ TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null = any,
39
37
  TContext extends Record<string, any> = {},
40
38
  TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {},
41
39
  > = Partial<PluvIOListeners<TPlatform, TAuthorize, TContext, TEvents>> & {
@@ -45,12 +43,12 @@ export type ServerConfig<
45
43
 
46
44
  export class PluvIO<
47
45
  TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>,
48
- TAuthorize extends PluvIOAuthorize<TPlatform, any, any, InferInitContextType<TPlatform>> = any,
46
+ TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null = any,
49
47
  TContext extends Record<string, any> = {},
50
48
  > {
51
49
  public readonly version: string = __PLUV_VERSION as any;
52
50
 
53
- private readonly _authorize: TAuthorize | null = null;
51
+ private readonly _authorize: TAuthorize = null as TAuthorize;
54
52
  private readonly _context: PluvContext<TPlatform, TContext> = {} as PluvContext<TPlatform, TContext>;
55
53
  private readonly _crdt: { doc: (value: any) => AbstractCrdtDocFactory<any> };
56
54
  private readonly _debug: boolean;
@@ -79,7 +77,7 @@ export class PluvIO<
79
77
 
80
78
  if (!secret) throw new Error("`authorize` was specified without a valid secret");
81
79
 
82
- return await authorize({ platform: this._platform, secret }).encode(params);
80
+ return await authorize({ platform: this._platform, secret }).encode(params as JWTEncodeParams<any, TPlatform>);
83
81
  }
84
82
 
85
83
  public mergeRouters<TRouters extends PluvRouter<TPlatform, TAuthorize, TContext, any>[]>(
@@ -107,13 +105,11 @@ export class PluvIO<
107
105
  });
108
106
  }
109
107
 
110
- private _getIOAuthorize(
111
- options: WebsocketRegisterConfig<TPlatform>,
112
- ): ResolvedPluvIOAuthorize<any, any, any> | null {
108
+ private _getIOAuthorize(options: WebsocketRegisterConfig<TPlatform>): ResolvedPluvIOAuthorize<any, any> | null {
113
109
  if (typeof this._authorize === "function") {
114
110
  return this._authorize(options);
115
111
  }
116
112
 
117
- return this._authorize as ResolvedPluvIOAuthorize<any, any, any> | null;
113
+ return this._authorize as ResolvedPluvIOAuthorize<any, any> | null;
118
114
  }
119
115
  }