@pluv/io 0.33.0 → 0.34.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 +12 -0
- package/dist/index.d.mts +89 -47
- package/dist/index.d.ts +89 -47
- package/dist/index.js +43 -37
- package/dist/index.mjs +40 -34
- package/package.json +5 -5
- package/src/AbstractPlatform.ts +9 -4
- package/src/IORoom.ts +15 -25
- package/src/PluvIO.ts +30 -13
- package/src/PluvRouter.ts +5 -4
- package/src/PluvServer.ts +54 -30
- package/src/createIO.ts +15 -6
- package/src/index.ts +3 -1
- package/src/types.ts +73 -6
- package/src/version.generated.ts +1 -1
package/src/PluvServer.ts
CHANGED
|
@@ -1,18 +1,9 @@
|
|
|
1
1
|
import type { AbstractCrdtDocFactory } from "@pluv/crdt";
|
|
2
2
|
import { noop } from "@pluv/crdt";
|
|
3
|
-
import type {
|
|
4
|
-
BaseIOAuthorize,
|
|
5
|
-
IOAuthorize,
|
|
6
|
-
IOLike,
|
|
7
|
-
Id,
|
|
8
|
-
InferIOAuthorize,
|
|
9
|
-
InferIOAuthorizeUser,
|
|
10
|
-
JsonObject,
|
|
11
|
-
Maybe,
|
|
12
|
-
} from "@pluv/types";
|
|
3
|
+
import type { IOLike, Id, InferIOAuthorize, InferIOAuthorizeUser, JsonObject, Maybe } from "@pluv/types";
|
|
13
4
|
import colors from "kleur";
|
|
14
5
|
import type { AbstractPlatform, InferInitContextType, InferRoomContextType } from "./AbstractPlatform";
|
|
15
|
-
import type { IORoomListeners } from "./IORoom";
|
|
6
|
+
import type { IORoomListeners, WebsocketRegisterConfig } from "./IORoom";
|
|
16
7
|
import { IORoom } from "./IORoom";
|
|
17
8
|
import { PluvProcedure } from "./PluvProcedure";
|
|
18
9
|
import type { PluvRouterEventConfig } from "./PluvRouter";
|
|
@@ -20,7 +11,14 @@ import { PluvRouter } from "./PluvRouter";
|
|
|
20
11
|
import type { JWTEncodeParams } from "./authorize";
|
|
21
12
|
import { authorize } from "./authorize";
|
|
22
13
|
import { PING_TIMEOUT_MS } from "./constants";
|
|
23
|
-
import type {
|
|
14
|
+
import type {
|
|
15
|
+
BasePluvIOListeners,
|
|
16
|
+
GetInitialStorageFn,
|
|
17
|
+
PluvContext,
|
|
18
|
+
PluvIOAuthorize,
|
|
19
|
+
PluvIOListeners,
|
|
20
|
+
ResolvedPluvIOAuthorize,
|
|
21
|
+
} from "./types";
|
|
24
22
|
import { __PLUV_VERSION } from "./version";
|
|
25
23
|
|
|
26
24
|
export type InferIORoom<TServer extends PluvServer<any, any, any, any>> =
|
|
@@ -30,7 +28,7 @@ export type InferIORoom<TServer extends PluvServer<any, any, any, any>> =
|
|
|
30
28
|
|
|
31
29
|
export type PluvServerConfig<
|
|
32
30
|
TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>,
|
|
33
|
-
TAuthorize extends
|
|
31
|
+
TAuthorize extends PluvIOAuthorize<TPlatform, any, any, InferInitContextType<TPlatform>> = any,
|
|
34
32
|
TContext extends Record<string, any> = {},
|
|
35
33
|
TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {},
|
|
36
34
|
> = Partial<PluvIOListeners<TPlatform, TAuthorize, TContext, TEvents>> & {
|
|
@@ -45,7 +43,7 @@ export type PluvServerConfig<
|
|
|
45
43
|
|
|
46
44
|
type BaseCreateRoomOptions<
|
|
47
45
|
TPlatform extends AbstractPlatform<any>,
|
|
48
|
-
TAuthorize extends
|
|
46
|
+
TAuthorize extends PluvIOAuthorize<TPlatform, any, any, InferInitContextType<TPlatform>>,
|
|
49
47
|
TContext extends Record<string, any>,
|
|
50
48
|
TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext>,
|
|
51
49
|
> = Partial<IORoomListeners<TPlatform, TAuthorize, TContext, TEvents>> & {
|
|
@@ -54,7 +52,7 @@ type BaseCreateRoomOptions<
|
|
|
54
52
|
|
|
55
53
|
export type CreateRoomOptions<
|
|
56
54
|
TPlatform extends AbstractPlatform<any>,
|
|
57
|
-
TAuthorize extends
|
|
55
|
+
TAuthorize extends PluvIOAuthorize<TPlatform, any, any, InferInitContextType<TPlatform>>,
|
|
58
56
|
TContext extends Record<string, any>,
|
|
59
57
|
TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext>,
|
|
60
58
|
> = keyof InferRoomContextType<TPlatform> extends never
|
|
@@ -63,7 +61,7 @@ export type CreateRoomOptions<
|
|
|
63
61
|
|
|
64
62
|
export class PluvServer<
|
|
65
63
|
TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>,
|
|
66
|
-
TAuthorize extends
|
|
64
|
+
TAuthorize extends PluvIOAuthorize<TPlatform, any, any, InferInitContextType<TPlatform>> = any,
|
|
67
65
|
TContext extends Record<string, any> = {},
|
|
68
66
|
TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {},
|
|
69
67
|
> implements IOLike<TAuthorize, TEvents>
|
|
@@ -201,6 +199,12 @@ export class PluvServer<
|
|
|
201
199
|
private readonly _platform: TPlatform;
|
|
202
200
|
private readonly _router: PluvRouter<TPlatform, TAuthorize, TContext, TEvents>;
|
|
203
201
|
|
|
202
|
+
public get fetch() {
|
|
203
|
+
if (!this._platform._fetch) throw new Error(`\`${this._platform._name}\` does not support \`fetch\``);
|
|
204
|
+
|
|
205
|
+
return this._platform._fetch;
|
|
206
|
+
}
|
|
207
|
+
|
|
204
208
|
/**
|
|
205
209
|
* @ignore
|
|
206
210
|
* @readonly
|
|
@@ -226,15 +230,16 @@ export class PluvServer<
|
|
|
226
230
|
crdt = noop,
|
|
227
231
|
debug = false,
|
|
228
232
|
getInitialStorage,
|
|
229
|
-
|
|
230
|
-
onRoomMessage,
|
|
231
|
-
onStorageUpdated,
|
|
232
|
-
onUserConnected,
|
|
233
|
-
onUserDisconnected,
|
|
233
|
+
|
|
234
234
|
platform,
|
|
235
235
|
router = new PluvRouter<TPlatform, TAuthorize, TContext, TEvents>({} as TEvents),
|
|
236
236
|
} = options;
|
|
237
237
|
|
|
238
|
+
const { onRoomDeleted, onRoomMessage, onStorageUpdated, onUserConnected, onUserDisconnected } =
|
|
239
|
+
options as Partial<BasePluvIOListeners<TPlatform, TAuthorize, TContext, TEvents>>;
|
|
240
|
+
|
|
241
|
+
platform.validateConfig(options);
|
|
242
|
+
|
|
238
243
|
this._crdt = crdt;
|
|
239
244
|
this._debug = debug;
|
|
240
245
|
this._platform = platform;
|
|
@@ -255,7 +260,7 @@ export class PluvServer<
|
|
|
255
260
|
onStorageUpdated: (event) => onStorageUpdated?.(event),
|
|
256
261
|
onUserConnected: (event) => onUserConnected?.(event),
|
|
257
262
|
onUserDisconnected: (event) => onUserDisconnected?.(event),
|
|
258
|
-
} as
|
|
263
|
+
} as BasePluvIOListeners<TPlatform, TAuthorize, TContext, TEvents>;
|
|
259
264
|
}
|
|
260
265
|
|
|
261
266
|
public createRoom(
|
|
@@ -269,6 +274,10 @@ export class PluvServer<
|
|
|
269
274
|
TEvents
|
|
270
275
|
>[0] & { _meta?: any };
|
|
271
276
|
|
|
277
|
+
if (this._platform._config.handleMode !== "io") {
|
|
278
|
+
throw new Error(`\`createRoom\' is unsupported for \`${this._platform._name}\``);
|
|
279
|
+
}
|
|
280
|
+
|
|
272
281
|
if (!/^[a-z0-9]+[a-z0-9\-_]+[a-z0-9]+$/i.test(room)) throw new Error("Unsupported room name");
|
|
273
282
|
|
|
274
283
|
const roomContext = platformRoomContext as InferRoomContextType<TPlatform>;
|
|
@@ -316,18 +325,33 @@ export class PluvServer<
|
|
|
316
325
|
throw new Error("IO does not specify authorize during initialization.");
|
|
317
326
|
}
|
|
318
327
|
|
|
319
|
-
|
|
320
|
-
|
|
328
|
+
/**
|
|
329
|
+
* !HACK
|
|
330
|
+
* @description Allow the platform to overwrite this behavior as needed. This is introduced
|
|
331
|
+
* to support platformPluv
|
|
332
|
+
* @date December 15, 2024
|
|
333
|
+
*/
|
|
334
|
+
if (this._platform._createToken) return await this._platform._createToken(params);
|
|
321
335
|
|
|
322
|
-
const
|
|
336
|
+
const ioAuthorize = this._getIOAuthorize(params);
|
|
337
|
+
const secret = ioAuthorize?.secret ?? null;
|
|
323
338
|
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
339
|
+
if (!secret) throw new Error("`authorize` was specified without a valid secret");
|
|
340
|
+
|
|
341
|
+
return await authorize({ platform: this._platform, secret }).encode(params);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
private _getIOAuthorize(
|
|
345
|
+
options: WebsocketRegisterConfig<TPlatform>,
|
|
346
|
+
): ResolvedPluvIOAuthorize<any, any, any> | null {
|
|
347
|
+
if (typeof this._authorize === "function") {
|
|
348
|
+
return this._authorize(options);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
return this._authorize as ResolvedPluvIOAuthorize<any, any, any> | null;
|
|
328
352
|
}
|
|
329
353
|
|
|
330
|
-
private _getListeners():
|
|
354
|
+
private _getListeners(): BasePluvIOListeners<TPlatform, TAuthorize, TContext, TEvents> {
|
|
331
355
|
return (this as any)._listeners;
|
|
332
356
|
}
|
|
333
357
|
|
package/src/createIO.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type { BaseUser
|
|
1
|
+
import type { BaseUser } from "@pluv/types";
|
|
2
2
|
import type { AbstractPlatform, InferInitContextType } from "./AbstractPlatform";
|
|
3
3
|
import { PluvIO } from "./PluvIO";
|
|
4
|
-
import type { CrdtLibraryType, PluvContext } from "./types";
|
|
4
|
+
import type { CrdtLibraryType, PluvContext, PluvIOAuthorize } from "./types";
|
|
5
5
|
|
|
6
6
|
export type CreateIOParams<
|
|
7
7
|
TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>,
|
|
@@ -9,7 +9,7 @@ export type CreateIOParams<
|
|
|
9
9
|
TAuthorizeUser extends BaseUser = BaseUser,
|
|
10
10
|
TAuthorizeRequired extends boolean = false,
|
|
11
11
|
> = {
|
|
12
|
-
authorize?:
|
|
12
|
+
authorize?: PluvIOAuthorize<TPlatform, TAuthorizeUser, TAuthorizeRequired, InferInitContextType<TPlatform>>;
|
|
13
13
|
context?: PluvContext<TPlatform, TContext>;
|
|
14
14
|
crdt?: CrdtLibraryType;
|
|
15
15
|
debug?: boolean;
|
|
@@ -23,15 +23,24 @@ export const createIO = <
|
|
|
23
23
|
TAuthorizeRequired extends boolean = false,
|
|
24
24
|
>(
|
|
25
25
|
params: CreateIOParams<TPlatform, TContext, TAuthorizeUser, TAuthorizeRequired>,
|
|
26
|
-
): PluvIO<
|
|
26
|
+
): PluvIO<
|
|
27
|
+
TPlatform,
|
|
28
|
+
PluvIOAuthorize<TPlatform, TAuthorizeUser, TAuthorizeRequired, InferInitContextType<TPlatform>>,
|
|
29
|
+
TContext
|
|
30
|
+
> => {
|
|
27
31
|
const { authorize, context, crdt, debug, platform } = params;
|
|
28
32
|
|
|
29
33
|
return new PluvIO<
|
|
30
34
|
TPlatform,
|
|
31
|
-
|
|
35
|
+
PluvIOAuthorize<TPlatform, TAuthorizeUser, TAuthorizeRequired, InferInitContextType<TPlatform>>,
|
|
32
36
|
TContext
|
|
33
37
|
>({
|
|
34
|
-
authorize
|
|
38
|
+
authorize: authorize as PluvIOAuthorize<
|
|
39
|
+
TPlatform,
|
|
40
|
+
TAuthorizeUser,
|
|
41
|
+
TAuthorizeRequired,
|
|
42
|
+
InferInitContextType<TPlatform>
|
|
43
|
+
>,
|
|
35
44
|
context,
|
|
36
45
|
crdt,
|
|
37
46
|
debug,
|
package/src/index.ts
CHANGED
|
@@ -7,7 +7,6 @@ export type {
|
|
|
7
7
|
InferInitContextType,
|
|
8
8
|
InferPlatformWebSocketType,
|
|
9
9
|
InferRoomContextType,
|
|
10
|
-
WebSocketRegistrationMode,
|
|
11
10
|
} from "./AbstractPlatform";
|
|
12
11
|
export { AbstractPubSub } from "./AbstractPubSub";
|
|
13
12
|
export type { IOPubSubEventMessage } from "./AbstractPubSub";
|
|
@@ -38,12 +37,15 @@ export type { CreateRoomOptions, InferIORoom, PluvServerConfig } from "./PluvSer
|
|
|
38
37
|
export type {
|
|
39
38
|
CrdtLibraryType,
|
|
40
39
|
GetInitialStorageFn,
|
|
40
|
+
HandleMode,
|
|
41
41
|
IORoomListenerEvent,
|
|
42
42
|
IORoomMessageEvent,
|
|
43
43
|
IOStorageUpdatedEvent,
|
|
44
44
|
IOUserConnectedEvent,
|
|
45
45
|
IOUserDisconnectedEvent,
|
|
46
|
+
PlatformConfig,
|
|
46
47
|
PluvIOListeners,
|
|
48
|
+
WebSocketRegistrationMode,
|
|
47
49
|
WebSocketSerializedState,
|
|
48
50
|
WebSocketSession,
|
|
49
51
|
} from "./types";
|
package/src/types.ts
CHANGED
|
@@ -1,15 +1,19 @@
|
|
|
1
1
|
import type { AbstractCrdtDoc, AbstractCrdtDocFactory } from "@pluv/crdt";
|
|
2
2
|
import type {
|
|
3
3
|
BaseIOAuthorize,
|
|
4
|
+
BaseUser,
|
|
4
5
|
EventRecord,
|
|
5
6
|
IOAuthorize,
|
|
6
7
|
Id,
|
|
7
8
|
InferEventMessage,
|
|
8
9
|
InferEventsOutput,
|
|
9
10
|
InferIOAuthorizeUser,
|
|
11
|
+
InputZodLike,
|
|
10
12
|
JsonObject,
|
|
11
13
|
Maybe,
|
|
12
14
|
MaybePromise,
|
|
15
|
+
Spread,
|
|
16
|
+
UndefinedProps,
|
|
13
17
|
} from "@pluv/types";
|
|
14
18
|
import type {
|
|
15
19
|
AbstractPlatform,
|
|
@@ -115,18 +119,81 @@ export type GetInitialStorageFn<TContext extends Record<string, any>> = (
|
|
|
115
119
|
event: GetInitialStorageEvent<TContext>,
|
|
116
120
|
) => MaybePromise<Maybe<string>>;
|
|
117
121
|
|
|
118
|
-
export
|
|
119
|
-
|
|
120
|
-
|
|
122
|
+
export type HandleMode = "io" | "fetch";
|
|
123
|
+
export type WebSocketRegistrationMode = "attached" | "detached";
|
|
124
|
+
|
|
125
|
+
export interface PlatformConfig {
|
|
126
|
+
authorize: {
|
|
127
|
+
required?: boolean;
|
|
128
|
+
secret?: boolean;
|
|
129
|
+
};
|
|
130
|
+
requireAuth?: boolean;
|
|
131
|
+
handleMode: HandleMode;
|
|
132
|
+
registrationMode: WebSocketRegistrationMode;
|
|
133
|
+
listeners: {
|
|
134
|
+
onRoomDeleted?: boolean;
|
|
135
|
+
onRoomMessage?: boolean;
|
|
136
|
+
onStorageUpdated?: boolean;
|
|
137
|
+
onUserConnected?: boolean;
|
|
138
|
+
onUserDisconnected?: boolean;
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export type ResolvedPluvIOAuthorize<
|
|
143
|
+
TPlatform extends AbstractPlatform<any, any, any, any>,
|
|
144
|
+
TUser extends BaseUser = any,
|
|
145
|
+
TRequired extends boolean = false,
|
|
146
|
+
> = { user: InputZodLike<TUser>; required?: TRequired } & UndefinedProps<
|
|
147
|
+
{ secret?: string },
|
|
148
|
+
Exclude<"secret", InferPlatformAuthorizeProperties<TPlatform>>
|
|
149
|
+
>;
|
|
150
|
+
|
|
151
|
+
export type PluvIOAuthorize<
|
|
152
|
+
TPlatform extends AbstractPlatform<any, any, any, any>,
|
|
153
|
+
TUser extends BaseUser = any,
|
|
154
|
+
TRequired extends boolean = false,
|
|
155
|
+
TContext extends Record<string, unknown> = {},
|
|
156
|
+
> =
|
|
157
|
+
| ResolvedPluvIOAuthorize<TPlatform, TUser, TRequired>
|
|
158
|
+
| ((context: TContext) => ResolvedPluvIOAuthorize<TPlatform, TUser, TRequired>);
|
|
159
|
+
|
|
160
|
+
export type BasePluvIOListeners<
|
|
161
|
+
TPlatform extends AbstractPlatform<any, any, any, any>,
|
|
162
|
+
TAuthorize extends PluvIOAuthorize<TPlatform, any, any, InferInitContextType<TPlatform>>,
|
|
121
163
|
TContext extends Record<string, any>,
|
|
122
164
|
TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext>,
|
|
123
|
-
> {
|
|
165
|
+
> = {
|
|
124
166
|
onRoomDeleted: (event: IORoomListenerEvent<TContext>) => void;
|
|
125
167
|
onRoomMessage: (event: IORoomMessageEvent<TPlatform, TAuthorize, TContext, TEvents>) => void;
|
|
126
168
|
onStorageUpdated: (event: IOStorageUpdatedEvent<TPlatform, TAuthorize, TContext>) => void;
|
|
127
169
|
onUserConnected: (event: IOUserConnectedEvent<TPlatform, TAuthorize, TContext>) => void;
|
|
128
170
|
onUserDisconnected: (event: IOUserDisconnectedEvent<TPlatform, TAuthorize, TContext>) => void;
|
|
129
|
-
}
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
export type PluvIOListeners<
|
|
174
|
+
TPlatform extends AbstractPlatform<any, any, any, any>,
|
|
175
|
+
TAuthorize extends PluvIOAuthorize<TPlatform, any, any, InferInitContextType<TPlatform>>,
|
|
176
|
+
TContext extends Record<string, any>,
|
|
177
|
+
TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext>,
|
|
178
|
+
> = UndefinedProps<
|
|
179
|
+
BasePluvIOListeners<TPlatform, TAuthorize, TContext, TEvents>,
|
|
180
|
+
Exclude<keyof BasePluvIOListeners<TPlatform, TAuthorize, TContext, TEvents>, InferPlatformListeners<TPlatform>>
|
|
181
|
+
>;
|
|
182
|
+
|
|
183
|
+
export type InferPlatformConfig<TPlatform extends AbstractPlatform<any, any, any, any>> =
|
|
184
|
+
TPlatform extends AbstractPlatform<any, any, any, infer IConfig> ? IConfig : never;
|
|
185
|
+
|
|
186
|
+
export type InferPlatformAuthorizeProperties<TPlatform extends AbstractPlatform<any, any, any, any>> = keyof {
|
|
187
|
+
[P in keyof PlatformConfig["authorize"] as InferPlatformConfig<TPlatform>["authorize"][P] extends true | undefined
|
|
188
|
+
? P
|
|
189
|
+
: never]: true;
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
export type InferPlatformListeners<TPlatform extends AbstractPlatform<any, any, any, any>> = keyof {
|
|
193
|
+
[P in keyof PlatformConfig["listeners"] as InferPlatformConfig<TPlatform>["listeners"][P] extends true | undefined
|
|
194
|
+
? P
|
|
195
|
+
: never]: true;
|
|
196
|
+
};
|
|
130
197
|
|
|
131
198
|
export type IORoomListenerEvent<TContext extends Record<string, any>> = {
|
|
132
199
|
context: TContext;
|
|
@@ -136,7 +203,7 @@ export type IORoomListenerEvent<TContext extends Record<string, any>> = {
|
|
|
136
203
|
|
|
137
204
|
export type IORoomMessageEvent<
|
|
138
205
|
TPlatform extends AbstractPlatform<any>,
|
|
139
|
-
TAuthorize extends
|
|
206
|
+
TAuthorize extends PluvIOAuthorize<TPlatform, any, any, InferInitContextType<TPlatform>>,
|
|
140
207
|
TContext extends Record<string, any>,
|
|
141
208
|
TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext>,
|
|
142
209
|
> = IORoomListenerEvent<TContext> & {
|
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.34.0'
|