@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.
@@ -4,7 +4,7 @@ import type { EventResolver, EventResolverKind, MergeEventRecords } from "./type
4
4
 
5
5
  export interface PluvProcedureConfig<
6
6
  TPlatform extends AbstractPlatform<any>,
7
- TAuthorize extends IOAuthorize<any, any, InferInitContextType<TPlatform>>,
7
+ TAuthorize extends IOAuthorize<any, InferInitContextType<TPlatform>> | null,
8
8
  TContext extends Record<string, any>,
9
9
  TInput extends JsonObject = {},
10
10
  TOutput extends EventRecord<string, any> = {},
@@ -17,7 +17,7 @@ export interface PluvProcedureConfig<
17
17
 
18
18
  export class PluvProcedure<
19
19
  TPlatform extends AbstractPlatform<any>,
20
- TAuthorize extends IOAuthorize<any, any, InferInitContextType<TPlatform>>,
20
+ TAuthorize extends IOAuthorize<any, InferInitContextType<TPlatform>> | null,
21
21
  TContext extends Record<string, any>,
22
22
  TInput extends JsonObject = {},
23
23
  TOutput extends EventRecord<string, any> = {},
package/src/PluvRouter.ts CHANGED
@@ -5,14 +5,14 @@ import type { PluvIOAuthorize } from "./types";
5
5
 
6
6
  export type PluvRouterEventConfig<
7
7
  TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>,
8
- TAuthorize extends PluvIOAuthorize<TPlatform, any, any, InferInitContextType<TPlatform>> = any,
8
+ TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null = any,
9
9
  TContext extends Record<string, any> = {},
10
10
  > = { [P: string]: Pick<PluvProcedure<TPlatform, TAuthorize, TContext, any, any>, "config"> };
11
11
 
12
12
  export type MergedRouter<
13
13
  TRouters extends PluvRouter<TPlatform, TAuthorize, TContext, any>[] = [],
14
14
  TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>,
15
- TAuthorize extends PluvIOAuthorize<TPlatform, any, any, InferInitContextType<TPlatform>> = any,
15
+ TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> = any,
16
16
  TContext extends Record<string, any> = {},
17
17
  TRoot extends TRouters[0]["_defs"]["events"] = {},
18
18
  > = TRouters extends [
@@ -24,7 +24,7 @@ export type MergedRouter<
24
24
 
25
25
  export class PluvRouter<
26
26
  TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>,
27
- TAuthorize extends PluvIOAuthorize<TPlatform, any, any, InferInitContextType<TPlatform>> = any,
27
+ TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null = any,
28
28
  TContext extends Record<string, any> = {},
29
29
  TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {},
30
30
  > implements IORouterLike<TEvents>
package/src/PluvServer.ts CHANGED
@@ -28,7 +28,7 @@ export type InferIORoom<TServer extends PluvServer<any, any, any, any>> =
28
28
 
29
29
  export type PluvServerConfig<
30
30
  TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>,
31
- TAuthorize extends PluvIOAuthorize<TPlatform, any, any, InferInitContextType<TPlatform>> = any,
31
+ TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null = any,
32
32
  TContext extends Record<string, any> = {},
33
33
  TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {},
34
34
  > = Partial<PluvIOListeners<TPlatform, TAuthorize, TContext, TEvents>> & {
@@ -43,7 +43,7 @@ export type PluvServerConfig<
43
43
 
44
44
  type BaseCreateRoomOptions<
45
45
  TPlatform extends AbstractPlatform<any>,
46
- TAuthorize extends PluvIOAuthorize<TPlatform, any, any, InferInitContextType<TPlatform>>,
46
+ TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null,
47
47
  TContext extends Record<string, any>,
48
48
  TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext>,
49
49
  > = Partial<IORoomListeners<TPlatform, TAuthorize, TContext, TEvents>> & {
@@ -52,7 +52,7 @@ type BaseCreateRoomOptions<
52
52
 
53
53
  export type CreateRoomOptions<
54
54
  TPlatform extends AbstractPlatform<any>,
55
- TAuthorize extends PluvIOAuthorize<TPlatform, any, any, InferInitContextType<TPlatform>>,
55
+ TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null,
56
56
  TContext extends Record<string, any>,
57
57
  TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext>,
58
58
  > = keyof InferRoomContextType<TPlatform> extends never
@@ -61,14 +61,14 @@ export type CreateRoomOptions<
61
61
 
62
62
  export class PluvServer<
63
63
  TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>,
64
- TAuthorize extends PluvIOAuthorize<TPlatform, any, any, InferInitContextType<TPlatform>> = any,
64
+ TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null = any,
65
65
  TContext extends Record<string, any> = {},
66
66
  TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {},
67
67
  > implements IOLike<TAuthorize, TEvents>
68
68
  {
69
69
  public readonly version: string = __PLUV_VERSION as any;
70
70
 
71
- private readonly _authorize: TAuthorize | null = null;
71
+ private readonly _authorize: TAuthorize = null as TAuthorize;
72
72
  private readonly _baseRouter: PluvRouter<TPlatform, TAuthorize, TContext, {}> = new PluvRouter({
73
73
  $GET_OTHERS: this._procedure.sync((data, { room, session, sessions }) => {
74
74
  const currentTime = new Date().getTime();
@@ -85,7 +85,7 @@ export class PluvServer<
85
85
  [connectionId: string]: {
86
86
  connectionId: string;
87
87
  room: string | null;
88
- user: JsonObject;
88
+ user: JsonObject | null;
89
89
  };
90
90
  }>(
91
91
  (acc, { id, presence, user }) => ({
@@ -216,6 +216,11 @@ export class PluvServer<
216
216
  context: this._context,
217
217
  events: this._router._defs.events,
218
218
  platform: this._platform,
219
+ } as {
220
+ authorize: TAuthorize;
221
+ context: TContext;
222
+ events: TEvents;
223
+ platform: TPlatform;
219
224
  };
220
225
  }
221
226
 
@@ -338,17 +343,15 @@ export class PluvServer<
338
343
 
339
344
  if (!secret) throw new Error("`authorize` was specified without a valid secret");
340
345
 
341
- return await authorize({ platform: this._platform, secret }).encode(params);
346
+ return await authorize({ platform: this._platform, secret }).encode(params as JWTEncodeParams<any, TPlatform>);
342
347
  }
343
348
 
344
- private _getIOAuthorize(
345
- options: WebsocketRegisterConfig<TPlatform>,
346
- ): ResolvedPluvIOAuthorize<any, any, any> | null {
349
+ private _getIOAuthorize(options: WebsocketRegisterConfig<TPlatform>): ResolvedPluvIOAuthorize<any, any> | null {
347
350
  if (typeof this._authorize === "function") {
348
351
  return this._authorize(options);
349
352
  }
350
353
 
351
- return this._authorize as ResolvedPluvIOAuthorize<any, any, any> | null;
354
+ return this._authorize as ResolvedPluvIOAuthorize<any, any> | null;
352
355
  }
353
356
 
354
357
  private _getListeners(): BasePluvIOListeners<TPlatform, TAuthorize, TContext, TEvents> {
package/src/authorize.ts CHANGED
@@ -15,10 +15,10 @@ export interface JWT<TUser extends BaseUser> {
15
15
  user: TUser;
16
16
  }
17
17
 
18
- export type JWTEncodeParams<TUser extends BaseUser, TPlatform extends AbstractPlatform> = {
18
+ export type JWTEncodeParams<TUser extends BaseUser | null, TPlatform extends AbstractPlatform> = {
19
19
  maxAge?: number;
20
20
  room: string;
21
- user: TUser;
21
+ user: TUser extends BaseUser ? TUser : "Error: Cannot create token without authorization!";
22
22
  } & InferInitContextType<TPlatform>;
23
23
 
24
24
  export interface AuthorizeParams {
package/src/createIO.ts CHANGED
@@ -1,4 +1,3 @@
1
- import type { BaseUser } from "@pluv/types";
2
1
  import type { AbstractPlatform, InferInitContextType } from "./AbstractPlatform";
3
2
  import { PluvIO } from "./PluvIO";
4
3
  import type { CrdtLibraryType, PluvContext, PluvIOAuthorize } from "./types";
@@ -6,10 +5,9 @@ import type { CrdtLibraryType, PluvContext, PluvIOAuthorize } from "./types";
6
5
  export type CreateIOParams<
7
6
  TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>,
8
7
  TContext extends Record<string, any> = {},
9
- TAuthorizeUser extends BaseUser = BaseUser,
10
- TAuthorizeRequired extends boolean = false,
8
+ TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null = null,
11
9
  > = {
12
- authorize?: PluvIOAuthorize<TPlatform, TAuthorizeUser, TAuthorizeRequired, InferInitContextType<TPlatform>>;
10
+ authorize?: TAuthorize;
13
11
  context?: PluvContext<TPlatform, TContext>;
14
12
  crdt?: CrdtLibraryType;
15
13
  debug?: boolean;
@@ -19,28 +17,14 @@ export type CreateIOParams<
19
17
  export const createIO = <
20
18
  TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>,
21
19
  TContext extends Record<string, any> = {},
22
- TAuthorizeUser extends BaseUser = BaseUser,
23
- TAuthorizeRequired extends boolean = false,
20
+ TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null = null,
24
21
  >(
25
- params: CreateIOParams<TPlatform, TContext, TAuthorizeUser, TAuthorizeRequired>,
26
- ): PluvIO<
27
- TPlatform,
28
- PluvIOAuthorize<TPlatform, TAuthorizeUser, TAuthorizeRequired, InferInitContextType<TPlatform>>,
29
- TContext
30
- > => {
22
+ params: CreateIOParams<TPlatform, TContext, TAuthorize>,
23
+ ): PluvIO<TPlatform, TAuthorize, TContext> => {
31
24
  const { authorize, context, crdt, debug, platform } = params;
32
25
 
33
- return new PluvIO<
34
- TPlatform,
35
- PluvIOAuthorize<TPlatform, TAuthorizeUser, TAuthorizeRequired, InferInitContextType<TPlatform>>,
36
- TContext
37
- >({
38
- authorize: authorize as PluvIOAuthorize<
39
- TPlatform,
40
- TAuthorizeUser,
41
- TAuthorizeRequired,
42
- InferInitContextType<TPlatform>
43
- >,
26
+ return new PluvIO<TPlatform, TAuthorize, TContext>({
27
+ authorize,
44
28
  context,
45
29
  crdt,
46
30
  debug,
package/src/types.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import type { AbstractCrdtDoc, AbstractCrdtDocFactory } from "@pluv/crdt";
2
2
  import type {
3
- BaseIOAuthorize,
4
3
  BaseUser,
5
4
  EventRecord,
6
5
  IOAuthorize,
@@ -12,7 +11,6 @@ import type {
12
11
  JsonObject,
13
12
  Maybe,
14
13
  MaybePromise,
15
- Spread,
16
14
  UndefinedProps,
17
15
  } from "@pluv/types";
18
16
  import type {
@@ -45,7 +43,7 @@ export type EventResolverKind = "broadcast" | "self" | "sync";
45
43
  export type EventResolver<
46
44
  TKind extends EventResolverKind = EventResolverKind,
47
45
  TPlatform extends AbstractPlatform = AbstractPlatform,
48
- TAuthorize extends IOAuthorize<any, any, InferInitContextType<TPlatform>> = BaseIOAuthorize,
46
+ TAuthorize extends IOAuthorize<any, InferInitContextType<TPlatform>> | null = null,
49
47
  TContext extends Record<string, any> = {},
50
48
  TInput extends JsonObject = {},
51
49
  TOutput extends EventRecord<string, any> = {},
@@ -57,7 +55,7 @@ export type EventResolver<
57
55
  export interface EventResolverContext<
58
56
  TKind extends EventResolverKind = EventResolverKind,
59
57
  TPlatform extends AbstractPlatform = AbstractPlatform,
60
- TAuthorize extends IOAuthorize<any, any, InferInitContextType<TPlatform>> = BaseIOAuthorize,
58
+ TAuthorize extends IOAuthorize<any, InferInitContextType<TPlatform>> | null = null,
61
59
  TContext extends Record<string, any> = {},
62
60
  > {
63
61
  context: TContext;
@@ -83,12 +81,11 @@ export interface WebSocketSerializedState {
83
81
  timers: WebSocketSessionTimers;
84
82
  }
85
83
 
86
- export type WebSocketSession<TAuthorize extends IOAuthorize<any, any, any> = BaseIOAuthorize> =
87
- WebSocketSerializedState & {
88
- id: string;
89
- user: InferIOAuthorizeUser<TAuthorize>;
90
- webSocket: AbstractWebSocket;
91
- };
84
+ export type WebSocketSession<TAuthorize extends IOAuthorize<any, any> | null = null> = WebSocketSerializedState & {
85
+ id: string;
86
+ user: InferIOAuthorizeUser<TAuthorize>;
87
+ webSocket: AbstractWebSocket;
88
+ };
92
89
 
93
90
  export type MergeEventRecords<
94
91
  TEventRecords extends EventRecord<string, any>[],
@@ -119,7 +116,6 @@ export type WebSocketRegistrationMode = "attached" | "detached";
119
116
 
120
117
  export interface PlatformConfig {
121
118
  authorize: {
122
- required?: boolean;
123
119
  secret?: boolean;
124
120
  };
125
121
  requireAuth?: boolean;
@@ -137,8 +133,7 @@ export interface PlatformConfig {
137
133
  export type ResolvedPluvIOAuthorize<
138
134
  TPlatform extends AbstractPlatform<any, any, any, any>,
139
135
  TUser extends BaseUser = any,
140
- TRequired extends boolean = false,
141
- > = { user: InputZodLike<TUser>; required?: TRequired } & UndefinedProps<
136
+ > = { user: InputZodLike<TUser> } & UndefinedProps<
142
137
  { secret?: string },
143
138
  Exclude<"secret", InferPlatformAuthorizeProperties<TPlatform>>
144
139
  >;
@@ -146,15 +141,12 @@ export type ResolvedPluvIOAuthorize<
146
141
  export type PluvIOAuthorize<
147
142
  TPlatform extends AbstractPlatform<any, any, any, any>,
148
143
  TUser extends BaseUser = any,
149
- TRequired extends boolean = false,
150
144
  TContext extends Record<string, unknown> = {},
151
- > =
152
- | ResolvedPluvIOAuthorize<TPlatform, TUser, TRequired>
153
- | ((context: TContext) => ResolvedPluvIOAuthorize<TPlatform, TUser, TRequired>);
145
+ > = ResolvedPluvIOAuthorize<TPlatform, TUser> | ((context: TContext) => ResolvedPluvIOAuthorize<TPlatform, TUser>);
154
146
 
155
147
  export type BasePluvIOListeners<
156
148
  TPlatform extends AbstractPlatform<any, any, any, any>,
157
- TAuthorize extends PluvIOAuthorize<TPlatform, any, any, InferInitContextType<TPlatform>>,
149
+ TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null,
158
150
  TContext extends Record<string, any>,
159
151
  TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext>,
160
152
  > = {
@@ -167,7 +159,7 @@ export type BasePluvIOListeners<
167
159
 
168
160
  export type PluvIOListeners<
169
161
  TPlatform extends AbstractPlatform<any, any, any, any>,
170
- TAuthorize extends PluvIOAuthorize<TPlatform, any, any, InferInitContextType<TPlatform>>,
162
+ TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null,
171
163
  TContext extends Record<string, any>,
172
164
  TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext>,
173
165
  > = UndefinedProps<
@@ -198,7 +190,7 @@ export type IORoomListenerEvent<TContext extends Record<string, any>> = {
198
190
 
199
191
  export type IORoomMessageEvent<
200
192
  TPlatform extends AbstractPlatform<any>,
201
- TAuthorize extends PluvIOAuthorize<TPlatform, any, any, InferInitContextType<TPlatform>>,
193
+ TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null,
202
194
  TContext extends Record<string, any>,
203
195
  TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext>,
204
196
  > = IORoomListenerEvent<TContext> & {
@@ -209,7 +201,7 @@ export type IORoomMessageEvent<
209
201
 
210
202
  export type IOStorageUpdatedEvent<
211
203
  TPlatform extends AbstractPlatform<any>,
212
- TAuthorize extends IOAuthorize<any, any, InferInitContextType<TPlatform>>,
204
+ TAuthorize extends IOAuthorize<any, InferInitContextType<TPlatform>> | null,
213
205
  TContext extends Record<string, any>,
214
206
  > = IORoomListenerEvent<TContext> & {
215
207
  user?: InferIOAuthorizeUser<TAuthorize>;
@@ -218,7 +210,7 @@ export type IOStorageUpdatedEvent<
218
210
 
219
211
  export type IOUserConnectedEvent<
220
212
  TPlatform extends AbstractPlatform<any>,
221
- TAuthorize extends IOAuthorize<any, any, InferInitContextType<TPlatform>>,
213
+ TAuthorize extends IOAuthorize<any, InferInitContextType<TPlatform>> | null,
222
214
  TContext extends Record<string, any>,
223
215
  > = IORoomListenerEvent<TContext> & {
224
216
  user?: InferIOAuthorizeUser<TAuthorize>;
@@ -227,7 +219,7 @@ export type IOUserConnectedEvent<
227
219
 
228
220
  export type IOUserDisconnectedEvent<
229
221
  TPlatform extends AbstractPlatform<any>,
230
- TAuthorize extends IOAuthorize<any, any, InferInitContextType<TPlatform>>,
222
+ TAuthorize extends IOAuthorize<any, InferInitContextType<TPlatform>> | null,
231
223
  TContext extends Record<string, any>,
232
224
  > = IORoomListenerEvent<TContext> & {
233
225
  user?: InferIOAuthorizeUser<TAuthorize>;
@@ -1,2 +1,2 @@
1
1
  // Generated by genversion.
2
- export const version = '0.34.1'
2
+ export const version = '0.35.1'