@pluv/io 0.34.0 → 0.35.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/.turbo/turbo-build.log +9 -9
- package/CHANGELOG.md +56 -0
- package/dist/index.d.mts +46 -62
- package/dist/index.d.ts +46 -62
- package/dist/index.js +9 -4
- package/dist/index.mjs +9 -4
- package/package.json +5 -5
- package/src/AbstractWebSocket.ts +4 -3
- package/src/IORoom.ts +26 -16
- package/src/PluvIO.ts +11 -15
- package/src/PluvProcedure.ts +14 -13
- package/src/PluvRouter.ts +3 -3
- package/src/PluvServer.ts +14 -11
- package/src/authorize.ts +2 -2
- package/src/createIO.ts +7 -23
- package/src/types.ts +24 -37
- package/src/version.generated.ts +1 -1
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
|
|
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,
|
|
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
|
-
}
|
|
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,
|
|
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,
|
|
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
|
|
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
|
|
113
|
+
return this._authorize as ResolvedPluvIOAuthorize<any, any> | null;
|
|
118
114
|
}
|
|
119
115
|
}
|
package/src/PluvProcedure.ts
CHANGED
|
@@ -1,32 +1,33 @@
|
|
|
1
1
|
import type { EventRecord, IOAuthorize, InputZodLike, JsonObject, ProcedureLike } from "@pluv/types";
|
|
2
2
|
import type { AbstractPlatform, InferInitContextType } from "./AbstractPlatform";
|
|
3
|
-
import type { EventResolver, MergeEventRecords } from "./types";
|
|
3
|
+
import type { EventResolver, EventResolverKind, MergeEventRecords } from "./types";
|
|
4
4
|
|
|
5
5
|
export interface PluvProcedureConfig<
|
|
6
6
|
TPlatform extends AbstractPlatform<any>,
|
|
7
|
-
TAuthorize extends IOAuthorize<any,
|
|
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> = {},
|
|
11
11
|
> {
|
|
12
|
-
broadcast?: EventResolver<TPlatform, TAuthorize, TContext, TInput, Partial<TOutput>> | null;
|
|
12
|
+
broadcast?: EventResolver<"broadcast", TPlatform, TAuthorize, TContext, TInput, Partial<TOutput>> | null;
|
|
13
13
|
input?: InputZodLike<TInput>;
|
|
14
|
-
self?: EventResolver<TPlatform, TAuthorize, TContext, TInput, Partial<TOutput>> | null;
|
|
15
|
-
sync?: EventResolver<TPlatform, TAuthorize, TContext, TInput, Partial<TOutput>> | null;
|
|
14
|
+
self?: EventResolver<"self", TPlatform, TAuthorize, TContext, TInput, Partial<TOutput>> | null;
|
|
15
|
+
sync?: EventResolver<"sync", TPlatform, TAuthorize, TContext, TInput, Partial<TOutput>> | null;
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
export class PluvProcedure<
|
|
19
19
|
TPlatform extends AbstractPlatform<any>,
|
|
20
|
-
TAuthorize extends IOAuthorize<any,
|
|
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> = {},
|
|
24
24
|
TFilled extends "input" | "broadcast" | "self" | "sync" | "" = "",
|
|
25
25
|
> {
|
|
26
|
-
private _broadcast: EventResolver<TPlatform, TAuthorize, TContext, TInput, Partial<TOutput>> | null =
|
|
26
|
+
private _broadcast: EventResolver<"broadcast", TPlatform, TAuthorize, TContext, TInput, Partial<TOutput>> | null =
|
|
27
|
+
null;
|
|
27
28
|
private _input: InputZodLike<TInput> | null = null;
|
|
28
|
-
private _self: EventResolver<TPlatform, TAuthorize, TContext, TInput, Partial<TOutput>> | null = null;
|
|
29
|
-
private _sync: EventResolver<TPlatform, TAuthorize, TContext, TInput, Partial<TOutput>> | null = null;
|
|
29
|
+
private _self: EventResolver<"self", TPlatform, TAuthorize, TContext, TInput, Partial<TOutput>> | null = null;
|
|
30
|
+
private _sync: EventResolver<"sync", TPlatform, TAuthorize, TContext, TInput, Partial<TOutput>> | null = null;
|
|
30
31
|
|
|
31
32
|
public get config(): ProcedureLike<TInput, TOutput>["config"] {
|
|
32
33
|
return {
|
|
@@ -48,7 +49,7 @@ export class PluvProcedure<
|
|
|
48
49
|
}
|
|
49
50
|
|
|
50
51
|
public broadcast<TResult extends EventRecord<string, any> = {}>(
|
|
51
|
-
resolver: EventResolver<TPlatform, TAuthorize, TContext, TInput, TResult>,
|
|
52
|
+
resolver: EventResolver<"broadcast", TPlatform, TAuthorize, TContext, TInput, TResult>,
|
|
52
53
|
): Omit<
|
|
53
54
|
PluvProcedure<
|
|
54
55
|
TPlatform,
|
|
@@ -84,7 +85,7 @@ export class PluvProcedure<
|
|
|
84
85
|
}
|
|
85
86
|
|
|
86
87
|
public self<TResult extends EventRecord<string, any> = {}>(
|
|
87
|
-
resolver: EventResolver<TPlatform, TAuthorize, TContext, TInput, TResult>,
|
|
88
|
+
resolver: EventResolver<"self", TPlatform, TAuthorize, TContext, TInput, TResult>,
|
|
88
89
|
): Omit<
|
|
89
90
|
PluvProcedure<
|
|
90
91
|
TPlatform,
|
|
@@ -114,7 +115,7 @@ export class PluvProcedure<
|
|
|
114
115
|
}
|
|
115
116
|
|
|
116
117
|
public sync<TResult extends EventRecord<string, any> = {}>(
|
|
117
|
-
resolver: EventResolver<TPlatform, TAuthorize, TContext, TInput, TResult>,
|
|
118
|
+
resolver: EventResolver<"sync", TPlatform, TAuthorize, TContext, TInput, TResult>,
|
|
118
119
|
): Omit<
|
|
119
120
|
PluvProcedure<
|
|
120
121
|
TPlatform,
|
|
@@ -143,7 +144,7 @@ export class PluvProcedure<
|
|
|
143
144
|
});
|
|
144
145
|
}
|
|
145
146
|
|
|
146
|
-
private _resolver(): EventResolver<TPlatform, TAuthorize, TContext, TInput, TOutput> {
|
|
147
|
+
private _resolver(): EventResolver<EventResolverKind, TPlatform, TAuthorize, TContext, TInput, TOutput> {
|
|
147
148
|
return (data, context) => {
|
|
148
149
|
return {
|
|
149
150
|
...this._broadcast?.(data, context),
|
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,
|
|
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,
|
|
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,
|
|
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,
|
|
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,
|
|
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,
|
|
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,
|
|
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
|
|
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
|
|
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
|
-
|
|
10
|
-
TAuthorizeRequired extends boolean = false,
|
|
8
|
+
TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null = null,
|
|
11
9
|
> = {
|
|
12
|
-
authorize?:
|
|
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
|
-
|
|
23
|
-
TAuthorizeRequired extends boolean = false,
|
|
20
|
+
TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null = null,
|
|
24
21
|
>(
|
|
25
|
-
params: CreateIOParams<TPlatform, TContext,
|
|
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
|
-
|
|
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 {
|
|
@@ -40,35 +38,30 @@ export interface CrdtLibraryType {
|
|
|
40
38
|
kind: "loro" | "yjs";
|
|
41
39
|
}
|
|
42
40
|
|
|
41
|
+
export type EventResolverKind = "broadcast" | "self" | "sync";
|
|
42
|
+
|
|
43
43
|
export type EventResolver<
|
|
44
|
+
TKind extends EventResolverKind = EventResolverKind,
|
|
44
45
|
TPlatform extends AbstractPlatform = AbstractPlatform,
|
|
45
|
-
TAuthorize extends IOAuthorize<any,
|
|
46
|
+
TAuthorize extends IOAuthorize<any, InferInitContextType<TPlatform>> | null = null,
|
|
46
47
|
TContext extends Record<string, any> = {},
|
|
47
48
|
TInput extends JsonObject = {},
|
|
48
49
|
TOutput extends EventRecord<string, any> = {},
|
|
49
|
-
> = (
|
|
50
|
+
> = (
|
|
51
|
+
data: TInput,
|
|
52
|
+
context: EventResolverContext<TKind, TPlatform, TAuthorize, TContext>,
|
|
53
|
+
) => MaybePromise<TOutput | void>;
|
|
50
54
|
|
|
51
55
|
export interface EventResolverContext<
|
|
56
|
+
TKind extends EventResolverKind = EventResolverKind,
|
|
52
57
|
TPlatform extends AbstractPlatform = AbstractPlatform,
|
|
53
|
-
TAuthorize extends IOAuthorize<any,
|
|
58
|
+
TAuthorize extends IOAuthorize<any, InferInitContextType<TPlatform>> | null = null,
|
|
54
59
|
TContext extends Record<string, any> = {},
|
|
55
60
|
> {
|
|
56
61
|
context: TContext;
|
|
57
62
|
doc: AbstractCrdtDoc<any>;
|
|
58
63
|
room: string;
|
|
59
|
-
|
|
60
|
-
* !HACK
|
|
61
|
-
* @description Session should only not exist when invoking the event as a
|
|
62
|
-
* "sync" event on another machine where the session doesn't exist. There must
|
|
63
|
-
* be a better way to handle this, but going to make this nullable for ease
|
|
64
|
-
* for now.
|
|
65
|
-
*
|
|
66
|
-
* `session` should not be referenced most of the time anyhow (outside of
|
|
67
|
-
* internal to this library).
|
|
68
|
-
* @author leedavidcs
|
|
69
|
-
* @date December 25, 2022
|
|
70
|
-
*/
|
|
71
|
-
session: WebSocketSession<TAuthorize> | null;
|
|
64
|
+
session: TKind extends "sync" ? WebSocketSession<TAuthorize> | null : WebSocketSession<TAuthorize>;
|
|
72
65
|
sessions: readonly WebSocketSession<TAuthorize>[];
|
|
73
66
|
}
|
|
74
67
|
|
|
@@ -88,12 +81,11 @@ export interface WebSocketSerializedState {
|
|
|
88
81
|
timers: WebSocketSessionTimers;
|
|
89
82
|
}
|
|
90
83
|
|
|
91
|
-
export type WebSocketSession<TAuthorize extends IOAuthorize<any, any
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
};
|
|
84
|
+
export type WebSocketSession<TAuthorize extends IOAuthorize<any, any> | null = null> = WebSocketSerializedState & {
|
|
85
|
+
id: string;
|
|
86
|
+
user: InferIOAuthorizeUser<TAuthorize>;
|
|
87
|
+
webSocket: AbstractWebSocket;
|
|
88
|
+
};
|
|
97
89
|
|
|
98
90
|
export type MergeEventRecords<
|
|
99
91
|
TEventRecords extends EventRecord<string, any>[],
|
|
@@ -124,7 +116,6 @@ export type WebSocketRegistrationMode = "attached" | "detached";
|
|
|
124
116
|
|
|
125
117
|
export interface PlatformConfig {
|
|
126
118
|
authorize: {
|
|
127
|
-
required?: boolean;
|
|
128
119
|
secret?: boolean;
|
|
129
120
|
};
|
|
130
121
|
requireAuth?: boolean;
|
|
@@ -142,8 +133,7 @@ export interface PlatformConfig {
|
|
|
142
133
|
export type ResolvedPluvIOAuthorize<
|
|
143
134
|
TPlatform extends AbstractPlatform<any, any, any, any>,
|
|
144
135
|
TUser extends BaseUser = any,
|
|
145
|
-
|
|
146
|
-
> = { user: InputZodLike<TUser>; required?: TRequired } & UndefinedProps<
|
|
136
|
+
> = { user: InputZodLike<TUser> } & UndefinedProps<
|
|
147
137
|
{ secret?: string },
|
|
148
138
|
Exclude<"secret", InferPlatformAuthorizeProperties<TPlatform>>
|
|
149
139
|
>;
|
|
@@ -151,15 +141,12 @@ export type ResolvedPluvIOAuthorize<
|
|
|
151
141
|
export type PluvIOAuthorize<
|
|
152
142
|
TPlatform extends AbstractPlatform<any, any, any, any>,
|
|
153
143
|
TUser extends BaseUser = any,
|
|
154
|
-
TRequired extends boolean = false,
|
|
155
144
|
TContext extends Record<string, unknown> = {},
|
|
156
|
-
> =
|
|
157
|
-
| ResolvedPluvIOAuthorize<TPlatform, TUser, TRequired>
|
|
158
|
-
| ((context: TContext) => ResolvedPluvIOAuthorize<TPlatform, TUser, TRequired>);
|
|
145
|
+
> = ResolvedPluvIOAuthorize<TPlatform, TUser> | ((context: TContext) => ResolvedPluvIOAuthorize<TPlatform, TUser>);
|
|
159
146
|
|
|
160
147
|
export type BasePluvIOListeners<
|
|
161
148
|
TPlatform extends AbstractPlatform<any, any, any, any>,
|
|
162
|
-
TAuthorize extends PluvIOAuthorize<TPlatform, any,
|
|
149
|
+
TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null,
|
|
163
150
|
TContext extends Record<string, any>,
|
|
164
151
|
TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext>,
|
|
165
152
|
> = {
|
|
@@ -172,7 +159,7 @@ export type BasePluvIOListeners<
|
|
|
172
159
|
|
|
173
160
|
export type PluvIOListeners<
|
|
174
161
|
TPlatform extends AbstractPlatform<any, any, any, any>,
|
|
175
|
-
TAuthorize extends PluvIOAuthorize<TPlatform, any,
|
|
162
|
+
TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null,
|
|
176
163
|
TContext extends Record<string, any>,
|
|
177
164
|
TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext>,
|
|
178
165
|
> = UndefinedProps<
|
|
@@ -203,7 +190,7 @@ export type IORoomListenerEvent<TContext extends Record<string, any>> = {
|
|
|
203
190
|
|
|
204
191
|
export type IORoomMessageEvent<
|
|
205
192
|
TPlatform extends AbstractPlatform<any>,
|
|
206
|
-
TAuthorize extends PluvIOAuthorize<TPlatform, any,
|
|
193
|
+
TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null,
|
|
207
194
|
TContext extends Record<string, any>,
|
|
208
195
|
TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext>,
|
|
209
196
|
> = IORoomListenerEvent<TContext> & {
|
|
@@ -214,7 +201,7 @@ export type IORoomMessageEvent<
|
|
|
214
201
|
|
|
215
202
|
export type IOStorageUpdatedEvent<
|
|
216
203
|
TPlatform extends AbstractPlatform<any>,
|
|
217
|
-
TAuthorize extends IOAuthorize<any,
|
|
204
|
+
TAuthorize extends IOAuthorize<any, InferInitContextType<TPlatform>> | null,
|
|
218
205
|
TContext extends Record<string, any>,
|
|
219
206
|
> = IORoomListenerEvent<TContext> & {
|
|
220
207
|
user?: InferIOAuthorizeUser<TAuthorize>;
|
|
@@ -223,7 +210,7 @@ export type IOStorageUpdatedEvent<
|
|
|
223
210
|
|
|
224
211
|
export type IOUserConnectedEvent<
|
|
225
212
|
TPlatform extends AbstractPlatform<any>,
|
|
226
|
-
TAuthorize extends IOAuthorize<any,
|
|
213
|
+
TAuthorize extends IOAuthorize<any, InferInitContextType<TPlatform>> | null,
|
|
227
214
|
TContext extends Record<string, any>,
|
|
228
215
|
> = IORoomListenerEvent<TContext> & {
|
|
229
216
|
user?: InferIOAuthorizeUser<TAuthorize>;
|
|
@@ -232,7 +219,7 @@ export type IOUserConnectedEvent<
|
|
|
232
219
|
|
|
233
220
|
export type IOUserDisconnectedEvent<
|
|
234
221
|
TPlatform extends AbstractPlatform<any>,
|
|
235
|
-
TAuthorize extends IOAuthorize<any,
|
|
222
|
+
TAuthorize extends IOAuthorize<any, InferInitContextType<TPlatform>> | null,
|
|
236
223
|
TContext extends Record<string, any>,
|
|
237
224
|
> = IORoomListenerEvent<TContext> & {
|
|
238
225
|
user?: InferIOAuthorizeUser<TAuthorize>;
|
package/src/version.generated.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// Generated by genversion.
|
|
2
|
-
export const version = '0.
|
|
2
|
+
export const version = '0.35.0'
|