@pluv/io 0.16.2 → 0.17.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 +64 -0
- package/dist/index.d.mts +13 -13
- package/dist/index.d.ts +13 -13
- package/dist/index.js +22 -13
- package/dist/index.mjs +22 -12
- package/package.json +6 -6
- package/src/IORoom.ts +32 -7
- package/src/PluvIO.ts +25 -4
- package/src/authorize.ts +16 -7
- package/src/createIO.ts +19 -4
- package/src/index.ts +0 -1
- package/src/version.generated.ts +1 -1
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
|
|
2
|
-
> @pluv/io@0.
|
|
2
|
+
> @pluv/io@0.17.0 build /home/runner/work/pluv/pluv/packages/io
|
|
3
3
|
> pnpm run version && tsup src/index.ts --format esm,cjs --dts
|
|
4
4
|
|
|
5
5
|
|
|
6
|
-
> @pluv/io@0.
|
|
6
|
+
> @pluv/io@0.17.0 version /home/runner/work/pluv/pluv/packages/io
|
|
7
7
|
> genversion ./src/version.generated.ts --esm
|
|
8
8
|
|
|
9
9
|
[34mCLI[39m Building entry: src/index.ts
|
|
@@ -12,11 +12,11 @@
|
|
|
12
12
|
[34mCLI[39m Target: es6
|
|
13
13
|
[34mESM[39m Build start
|
|
14
14
|
[34mCJS[39m Build start
|
|
15
|
-
[
|
|
16
|
-
[
|
|
17
|
-
[
|
|
18
|
-
[
|
|
15
|
+
[32mESM[39m [1mdist/index.mjs [22m[32m30.06 KB[39m
|
|
16
|
+
[32mESM[39m ⚡️ Build success in 124ms
|
|
17
|
+
[32mCJS[39m [1mdist/index.js [22m[32m32.12 KB[39m
|
|
18
|
+
[32mCJS[39m ⚡️ Build success in 124ms
|
|
19
19
|
[34mDTS[39m Build start
|
|
20
|
-
[32mDTS[39m ⚡️ Build success in
|
|
21
|
-
[32mDTS[39m [1mdist/index.d.mts [22m[
|
|
22
|
-
[32mDTS[39m [1mdist/index.d.ts [22m[
|
|
20
|
+
[32mDTS[39m ⚡️ Build success in 2909ms
|
|
21
|
+
[32mDTS[39m [1mdist/index.d.mts [22m[32m18.04 KB[39m
|
|
22
|
+
[32mDTS[39m [1mdist/index.d.ts [22m[32m18.04 KB[39m
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,69 @@
|
|
|
1
1
|
# @pluv/io
|
|
2
2
|
|
|
3
|
+
## 0.17.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 507bc00: _BREAKING_: The `authorize` config when calling `createIO` can now also be a function that exposes the platform context.
|
|
8
|
+
This allows accessing the `env` in Cloudflare workers.
|
|
9
|
+
|
|
10
|
+
```ts
|
|
11
|
+
import { createIO } from "@pluv/io";
|
|
12
|
+
import { platformCloudflare } from "@pluv/platform-cloudflare";
|
|
13
|
+
import { z } from "zod";
|
|
14
|
+
|
|
15
|
+
const io = createIO({
|
|
16
|
+
authorize: ({ env }) => ({
|
|
17
|
+
required: true,
|
|
18
|
+
secret: env.PLUV_AUTHORIZE_SECRET,
|
|
19
|
+
user: z.object({
|
|
20
|
+
id: z.string(),
|
|
21
|
+
name: z.string(),
|
|
22
|
+
}),
|
|
23
|
+
}),
|
|
24
|
+
platform: platformCloudflare<{ PLUV_AUTHORIZE_SECRET: string }>(),
|
|
25
|
+
// ...
|
|
26
|
+
});
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
This also requires that the platform contexts are passed to `io.createToken`.
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
// If using `platformNode`
|
|
33
|
+
await io.createToken({
|
|
34
|
+
req, // This `IncomingMessage` is now required
|
|
35
|
+
room,
|
|
36
|
+
user: {
|
|
37
|
+
id: "user_123",
|
|
38
|
+
name: "john doe",
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
// If using `platformCloudflare`
|
|
43
|
+
await io.createToken({
|
|
44
|
+
env, // This env is now required from the handler's fetch function
|
|
45
|
+
room,
|
|
46
|
+
user: {
|
|
47
|
+
id: "user_123",
|
|
48
|
+
name: "john doe",
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### Patch Changes
|
|
54
|
+
|
|
55
|
+
- Updated dependencies [507bc00]
|
|
56
|
+
- @pluv/types@0.17.0
|
|
57
|
+
- @pluv/crdt@0.17.0
|
|
58
|
+
|
|
59
|
+
## 0.16.3
|
|
60
|
+
|
|
61
|
+
### Patch Changes
|
|
62
|
+
|
|
63
|
+
- 0bf0934: Moved exposed `@pluv/io` version on the `PluvIO` instance as a property.
|
|
64
|
+
- @pluv/crdt@0.16.3
|
|
65
|
+
- @pluv/types@0.16.3
|
|
66
|
+
|
|
3
67
|
## 0.16.2
|
|
4
68
|
|
|
5
69
|
### Patch Changes
|
package/dist/index.d.mts
CHANGED
|
@@ -172,7 +172,7 @@ type IORoomListenerEvent<TPlatform extends AbstractPlatform> = {
|
|
|
172
172
|
interface IORoomListeners<TPlatform extends AbstractPlatform> {
|
|
173
173
|
onDestroy: (event: IORoomListenerEvent<TPlatform>) => void;
|
|
174
174
|
}
|
|
175
|
-
type IORoomConfig<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends IOAuthorize<any, any
|
|
175
|
+
type IORoomConfig<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends IOAuthorize<any, any, InferPlatformRoomContextType<TPlatform>> = BaseIOAuthorize, TContext extends JsonObject = {}, TInput extends EventRecord<string, any> = {}, TOutputBroadcast extends EventRecord<string, any> = {}, TOutputSelf extends EventRecord<string, any> = {}, TOutputSync extends EventRecord<string, any> = {}> = Partial<IORoomListeners<TPlatform>> & {
|
|
176
176
|
authorize?: TAuthorize;
|
|
177
177
|
context: TContext & InferPlatformRoomContextType<TPlatform>;
|
|
178
178
|
crdt?: {
|
|
@@ -185,7 +185,7 @@ type IORoomConfig<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any
|
|
|
185
185
|
type WebsocketRegisterOptions<TPlatform extends AbstractPlatform> = {
|
|
186
186
|
token?: string | null;
|
|
187
187
|
} & InferPlatformRoomContextType<TPlatform> & InferPlatformEventContextType<TPlatform>;
|
|
188
|
-
declare class IORoom<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends IOAuthorize<any, any
|
|
188
|
+
declare class IORoom<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends IOAuthorize<any, any, InferPlatformRoomContextType<TPlatform>> = BaseIOAuthorize, TContext extends Record<string, any> = {}, TInput extends EventRecord<string, any> = {}, TOutputBroadcast extends EventRecord<string, any> = {}, TOutputSelf extends EventRecord<string, any> = {}, TOutputSync extends EventRecord<string, any> = {}> implements IOLike<TAuthorize, TInput, TOutputBroadcast, TOutputSelf, TOutputSync> {
|
|
189
189
|
private readonly _context;
|
|
190
190
|
private readonly _debug;
|
|
191
191
|
private readonly _docFactory;
|
|
@@ -204,6 +204,7 @@ declare class IORoom<TPlatform extends AbstractPlatform<any> = AbstractPlatform<
|
|
|
204
204
|
private _broadcast;
|
|
205
205
|
private _emitQuitters;
|
|
206
206
|
private _emitRegistered;
|
|
207
|
+
private _getIOAuthorize;
|
|
207
208
|
private _getAuthorizedUser;
|
|
208
209
|
private _getEventConfig;
|
|
209
210
|
private _getEventInputs;
|
|
@@ -224,18 +225,18 @@ interface JWT<TUser extends BaseUser> {
|
|
|
224
225
|
sub: string;
|
|
225
226
|
user: TUser;
|
|
226
227
|
}
|
|
227
|
-
|
|
228
|
+
type JWTEncodeParams<TUser extends BaseUser, TPlatform extends AbstractPlatform> = {
|
|
228
229
|
maxAge?: number;
|
|
229
230
|
room: string;
|
|
230
231
|
user: TUser;
|
|
231
|
-
}
|
|
232
|
+
} & InferPlatformRoomContextType<TPlatform>;
|
|
232
233
|
interface AuthorizeParams {
|
|
233
234
|
platform: AbstractPlatform;
|
|
234
235
|
secret: string;
|
|
235
236
|
}
|
|
236
237
|
interface AuthorizeModule {
|
|
237
238
|
decode: <TUser extends BaseUser>(jwt: string) => Promise<TUser | null>;
|
|
238
|
-
encode: <TUser extends BaseUser>(params: JWTEncodeParams<TUser>) => Promise<string>;
|
|
239
|
+
encode: <TUser extends BaseUser, TPlatform extends AbstractPlatform>(params: JWTEncodeParams<TUser, TPlatform>) => Promise<string>;
|
|
239
240
|
}
|
|
240
241
|
|
|
241
242
|
type InferIOContext<TIO extends PluvIO<any, any, any, any, any, any, any>> = TIO extends PluvIO<any, any, infer IContext, any, any, any, any> ? IContext : never;
|
|
@@ -249,7 +250,7 @@ interface PluvIOListeners<TPlatform extends AbstractPlatform> {
|
|
|
249
250
|
onRoomDeleted: (event: IORoomListenerEvent<TPlatform>) => void;
|
|
250
251
|
onStorageUpdated: (event: IORoomListenerEvent<TPlatform>) => void;
|
|
251
252
|
}
|
|
252
|
-
type PluvIOConfig<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends IOAuthorize<any, any
|
|
253
|
+
type PluvIOConfig<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends IOAuthorize<any, any, InferPlatformRoomContextType<TPlatform>> | null = null, TContext extends JsonObject = {}, TInput extends EventRecord<string, any> = {}, TOutputBroadcast extends EventRecord<string, any> = {}, TOutputSelf extends EventRecord<string, any> = {}, TOutputSync extends EventRecord<string, any> = {}> = Partial<PluvIOListeners<TPlatform>> & {
|
|
253
254
|
authorize?: TAuthorize;
|
|
254
255
|
context?: TContext;
|
|
255
256
|
crdt?: {
|
|
@@ -263,7 +264,7 @@ type PluvIOConfig<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any
|
|
|
263
264
|
type GetRoomOptions<TPlatform extends AbstractPlatform> = {
|
|
264
265
|
debug?: boolean;
|
|
265
266
|
} & InferPlatformRoomContextType<TPlatform>;
|
|
266
|
-
declare class PluvIO<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends IOAuthorize<any, any
|
|
267
|
+
declare class PluvIO<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends IOAuthorize<any, any, InferPlatformRoomContextType<TPlatform>> = BaseIOAuthorize, TContext extends JsonObject = {}, TInput extends EventRecord<string, any> = {}, TOutputBroadcast extends EventRecord<string, any> = {}, TOutputSelf extends EventRecord<string, any> = {}, TOutputSync extends EventRecord<string, any> = {}> implements IOLike<TAuthorize, TInput, TOutputBroadcast, TOutputSelf, TOutputSync> {
|
|
267
268
|
private _rooms;
|
|
268
269
|
readonly _authorize: TAuthorize | null;
|
|
269
270
|
readonly _context: TContext;
|
|
@@ -275,8 +276,9 @@ declare class PluvIO<TPlatform extends AbstractPlatform<any> = AbstractPlatform<
|
|
|
275
276
|
readonly _getInitialStorage: GetInitialStorageFn<TPlatform> | null;
|
|
276
277
|
readonly _listeners: PluvIOListeners<TPlatform>;
|
|
277
278
|
readonly _platform: TPlatform;
|
|
279
|
+
readonly _version: string;
|
|
278
280
|
constructor(options: PluvIOConfig<TPlatform, TAuthorize, TContext, TInput, TOutputBroadcast, TOutputSelf, TOutputSync>);
|
|
279
|
-
createToken(params: JWTEncodeParams<InferIOAuthorizeUser<InferIOAuthorize<this
|
|
281
|
+
createToken(params: JWTEncodeParams<InferIOAuthorizeUser<InferIOAuthorize<this>>, TPlatform>): Promise<string>;
|
|
280
282
|
event<TEvent extends string, TData extends JsonObject = {}, TResultBroadcast extends EventRecord<string, any> = {}, TResultSelf extends EventRecord<string, any> = {}, TResultSync extends EventRecord<string, any> = {}>(event: TEvent, config: EventConfig<TPlatform, TContext, TData, TResultBroadcast, TResultSelf, TResultSync>): PluvIO<TPlatform, TAuthorize, TContext, Spread<[TInput, EventRecord<TEvent, TData>]>, Spread<[TOutputBroadcast, TResultBroadcast]>, Spread<[TOutputSelf, TResultSelf]>, Spread<[TOutputSync, TResultSync]>>;
|
|
281
283
|
getRoom(room: string, options: GetRoomOptions<TPlatform>): IORoom<TPlatform, TAuthorize, TContext, TInput, TOutputBroadcast, TOutputSelf, TOutputSync>;
|
|
282
284
|
static merge<TIO1 extends PluvIO, TIO2 extends PluvIO>(first: TIO1, second: TIO2): PluvIO<InferIOPlatform<TIO1>, InferIOAuthorize<TIO1>, InferIOContext<TIO1>, Spread<[InferIOInput<TIO1>, InferIOInput<TIO2>]>, Spread<[InferIOOutputBroadcast<TIO1>, InferIOOutputBroadcast<TIO2>]>, Spread<[InferIOOutputSelf<TIO1>, InferIOOutputSelf<TIO2>]>, Spread<[InferIOOutputSync<TIO1>, InferIOOutputSync<TIO2>]>>;
|
|
@@ -285,7 +287,7 @@ declare class PluvIO<TPlatform extends AbstractPlatform<any> = AbstractPlatform<
|
|
|
285
287
|
}
|
|
286
288
|
|
|
287
289
|
type CreateIOParams<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TContext extends JsonObject = {}, TAuthorizeUser extends BaseUser = BaseUser, TAuthorizeRequired extends boolean = false> = Partial<PluvIOListeners<TPlatform>> & {
|
|
288
|
-
authorize?: IOAuthorize<TAuthorizeUser, TAuthorizeRequired
|
|
290
|
+
authorize?: IOAuthorize<TAuthorizeUser, TAuthorizeRequired, InferPlatformRoomContextType<TPlatform>>;
|
|
289
291
|
context?: TContext;
|
|
290
292
|
crdt?: {
|
|
291
293
|
doc: (value: any) => AbstractCrdtDocFactory<any>;
|
|
@@ -294,8 +296,6 @@ type CreateIOParams<TPlatform extends AbstractPlatform<any> = AbstractPlatform<a
|
|
|
294
296
|
getInitialStorage?: GetInitialStorageFn<TPlatform>;
|
|
295
297
|
platform: TPlatform;
|
|
296
298
|
};
|
|
297
|
-
declare const createIO: <TPlatform extends AbstractPlatform<any, {}, {}> = AbstractPlatform<any, {}, {}>, TContext extends JsonObject = {}, TAuthorizeUser extends BaseUser = BaseUser, TAuthorizeRequired extends boolean = false>(params: CreateIOParams<TPlatform, TContext, TAuthorizeUser, TAuthorizeRequired>) => PluvIO<TPlatform, IOAuthorize<TAuthorizeUser, TAuthorizeRequired
|
|
298
|
-
|
|
299
|
-
declare const version = "0.16.2";
|
|
299
|
+
declare const createIO: <TPlatform extends AbstractPlatform<any, {}, {}> = AbstractPlatform<any, {}, {}>, TContext extends JsonObject = {}, TAuthorizeUser extends BaseUser = BaseUser, TAuthorizeRequired extends boolean = false>(params: CreateIOParams<TPlatform, TContext, TAuthorizeUser, TAuthorizeRequired>) => PluvIO<TPlatform, IOAuthorize<TAuthorizeUser, TAuthorizeRequired, InferPlatformRoomContextType<TPlatform>>, TContext, BaseClientEventRecord, BaseIOEventRecord<IOAuthorize<TAuthorizeUser, TAuthorizeRequired>>, {}, {}>;
|
|
300
300
|
|
|
301
|
-
export { type AbstractCloseEvent, type AbstractErrorEvent, type AbstractEvent, type AbstractEventMap, type AbstractListener, type AbstractMessageEvent, AbstractPersistance, AbstractPlatform, type AbstractPlatformConfig, AbstractPubSub, AbstractWebSocket, type AbstractWebSocketConfig, type AbstractWebSocketHandleErrorParams, type AuthorizeModule, type AuthorizeParams, type ConvertWebSocketConfig, type CreateIOParams, type GetRoomOptions, type IOPubSubEventMessage, IORoom, type InferIORoom, type InferPlatformEventContextType, type InferPlatformRoomContextType, type InferPlatformWebSocketType, type JWT, type JWTEncodeParams, PluvIO, type PluvIOConfig,
|
|
301
|
+
export { type AbstractCloseEvent, type AbstractErrorEvent, type AbstractEvent, type AbstractEventMap, type AbstractListener, type AbstractMessageEvent, AbstractPersistance, AbstractPlatform, type AbstractPlatformConfig, AbstractPubSub, AbstractWebSocket, type AbstractWebSocketConfig, type AbstractWebSocketHandleErrorParams, type AuthorizeModule, type AuthorizeParams, type ConvertWebSocketConfig, type CreateIOParams, type GetRoomOptions, type IOPubSubEventMessage, IORoom, type InferIORoom, type InferPlatformEventContextType, type InferPlatformRoomContextType, type InferPlatformWebSocketType, type JWT, type JWTEncodeParams, PluvIO, type PluvIOConfig, createIO };
|
package/dist/index.d.ts
CHANGED
|
@@ -172,7 +172,7 @@ type IORoomListenerEvent<TPlatform extends AbstractPlatform> = {
|
|
|
172
172
|
interface IORoomListeners<TPlatform extends AbstractPlatform> {
|
|
173
173
|
onDestroy: (event: IORoomListenerEvent<TPlatform>) => void;
|
|
174
174
|
}
|
|
175
|
-
type IORoomConfig<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends IOAuthorize<any, any
|
|
175
|
+
type IORoomConfig<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends IOAuthorize<any, any, InferPlatformRoomContextType<TPlatform>> = BaseIOAuthorize, TContext extends JsonObject = {}, TInput extends EventRecord<string, any> = {}, TOutputBroadcast extends EventRecord<string, any> = {}, TOutputSelf extends EventRecord<string, any> = {}, TOutputSync extends EventRecord<string, any> = {}> = Partial<IORoomListeners<TPlatform>> & {
|
|
176
176
|
authorize?: TAuthorize;
|
|
177
177
|
context: TContext & InferPlatformRoomContextType<TPlatform>;
|
|
178
178
|
crdt?: {
|
|
@@ -185,7 +185,7 @@ type IORoomConfig<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any
|
|
|
185
185
|
type WebsocketRegisterOptions<TPlatform extends AbstractPlatform> = {
|
|
186
186
|
token?: string | null;
|
|
187
187
|
} & InferPlatformRoomContextType<TPlatform> & InferPlatformEventContextType<TPlatform>;
|
|
188
|
-
declare class IORoom<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends IOAuthorize<any, any
|
|
188
|
+
declare class IORoom<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends IOAuthorize<any, any, InferPlatformRoomContextType<TPlatform>> = BaseIOAuthorize, TContext extends Record<string, any> = {}, TInput extends EventRecord<string, any> = {}, TOutputBroadcast extends EventRecord<string, any> = {}, TOutputSelf extends EventRecord<string, any> = {}, TOutputSync extends EventRecord<string, any> = {}> implements IOLike<TAuthorize, TInput, TOutputBroadcast, TOutputSelf, TOutputSync> {
|
|
189
189
|
private readonly _context;
|
|
190
190
|
private readonly _debug;
|
|
191
191
|
private readonly _docFactory;
|
|
@@ -204,6 +204,7 @@ declare class IORoom<TPlatform extends AbstractPlatform<any> = AbstractPlatform<
|
|
|
204
204
|
private _broadcast;
|
|
205
205
|
private _emitQuitters;
|
|
206
206
|
private _emitRegistered;
|
|
207
|
+
private _getIOAuthorize;
|
|
207
208
|
private _getAuthorizedUser;
|
|
208
209
|
private _getEventConfig;
|
|
209
210
|
private _getEventInputs;
|
|
@@ -224,18 +225,18 @@ interface JWT<TUser extends BaseUser> {
|
|
|
224
225
|
sub: string;
|
|
225
226
|
user: TUser;
|
|
226
227
|
}
|
|
227
|
-
|
|
228
|
+
type JWTEncodeParams<TUser extends BaseUser, TPlatform extends AbstractPlatform> = {
|
|
228
229
|
maxAge?: number;
|
|
229
230
|
room: string;
|
|
230
231
|
user: TUser;
|
|
231
|
-
}
|
|
232
|
+
} & InferPlatformRoomContextType<TPlatform>;
|
|
232
233
|
interface AuthorizeParams {
|
|
233
234
|
platform: AbstractPlatform;
|
|
234
235
|
secret: string;
|
|
235
236
|
}
|
|
236
237
|
interface AuthorizeModule {
|
|
237
238
|
decode: <TUser extends BaseUser>(jwt: string) => Promise<TUser | null>;
|
|
238
|
-
encode: <TUser extends BaseUser>(params: JWTEncodeParams<TUser>) => Promise<string>;
|
|
239
|
+
encode: <TUser extends BaseUser, TPlatform extends AbstractPlatform>(params: JWTEncodeParams<TUser, TPlatform>) => Promise<string>;
|
|
239
240
|
}
|
|
240
241
|
|
|
241
242
|
type InferIOContext<TIO extends PluvIO<any, any, any, any, any, any, any>> = TIO extends PluvIO<any, any, infer IContext, any, any, any, any> ? IContext : never;
|
|
@@ -249,7 +250,7 @@ interface PluvIOListeners<TPlatform extends AbstractPlatform> {
|
|
|
249
250
|
onRoomDeleted: (event: IORoomListenerEvent<TPlatform>) => void;
|
|
250
251
|
onStorageUpdated: (event: IORoomListenerEvent<TPlatform>) => void;
|
|
251
252
|
}
|
|
252
|
-
type PluvIOConfig<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends IOAuthorize<any, any
|
|
253
|
+
type PluvIOConfig<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends IOAuthorize<any, any, InferPlatformRoomContextType<TPlatform>> | null = null, TContext extends JsonObject = {}, TInput extends EventRecord<string, any> = {}, TOutputBroadcast extends EventRecord<string, any> = {}, TOutputSelf extends EventRecord<string, any> = {}, TOutputSync extends EventRecord<string, any> = {}> = Partial<PluvIOListeners<TPlatform>> & {
|
|
253
254
|
authorize?: TAuthorize;
|
|
254
255
|
context?: TContext;
|
|
255
256
|
crdt?: {
|
|
@@ -263,7 +264,7 @@ type PluvIOConfig<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any
|
|
|
263
264
|
type GetRoomOptions<TPlatform extends AbstractPlatform> = {
|
|
264
265
|
debug?: boolean;
|
|
265
266
|
} & InferPlatformRoomContextType<TPlatform>;
|
|
266
|
-
declare class PluvIO<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends IOAuthorize<any, any
|
|
267
|
+
declare class PluvIO<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends IOAuthorize<any, any, InferPlatformRoomContextType<TPlatform>> = BaseIOAuthorize, TContext extends JsonObject = {}, TInput extends EventRecord<string, any> = {}, TOutputBroadcast extends EventRecord<string, any> = {}, TOutputSelf extends EventRecord<string, any> = {}, TOutputSync extends EventRecord<string, any> = {}> implements IOLike<TAuthorize, TInput, TOutputBroadcast, TOutputSelf, TOutputSync> {
|
|
267
268
|
private _rooms;
|
|
268
269
|
readonly _authorize: TAuthorize | null;
|
|
269
270
|
readonly _context: TContext;
|
|
@@ -275,8 +276,9 @@ declare class PluvIO<TPlatform extends AbstractPlatform<any> = AbstractPlatform<
|
|
|
275
276
|
readonly _getInitialStorage: GetInitialStorageFn<TPlatform> | null;
|
|
276
277
|
readonly _listeners: PluvIOListeners<TPlatform>;
|
|
277
278
|
readonly _platform: TPlatform;
|
|
279
|
+
readonly _version: string;
|
|
278
280
|
constructor(options: PluvIOConfig<TPlatform, TAuthorize, TContext, TInput, TOutputBroadcast, TOutputSelf, TOutputSync>);
|
|
279
|
-
createToken(params: JWTEncodeParams<InferIOAuthorizeUser<InferIOAuthorize<this
|
|
281
|
+
createToken(params: JWTEncodeParams<InferIOAuthorizeUser<InferIOAuthorize<this>>, TPlatform>): Promise<string>;
|
|
280
282
|
event<TEvent extends string, TData extends JsonObject = {}, TResultBroadcast extends EventRecord<string, any> = {}, TResultSelf extends EventRecord<string, any> = {}, TResultSync extends EventRecord<string, any> = {}>(event: TEvent, config: EventConfig<TPlatform, TContext, TData, TResultBroadcast, TResultSelf, TResultSync>): PluvIO<TPlatform, TAuthorize, TContext, Spread<[TInput, EventRecord<TEvent, TData>]>, Spread<[TOutputBroadcast, TResultBroadcast]>, Spread<[TOutputSelf, TResultSelf]>, Spread<[TOutputSync, TResultSync]>>;
|
|
281
283
|
getRoom(room: string, options: GetRoomOptions<TPlatform>): IORoom<TPlatform, TAuthorize, TContext, TInput, TOutputBroadcast, TOutputSelf, TOutputSync>;
|
|
282
284
|
static merge<TIO1 extends PluvIO, TIO2 extends PluvIO>(first: TIO1, second: TIO2): PluvIO<InferIOPlatform<TIO1>, InferIOAuthorize<TIO1>, InferIOContext<TIO1>, Spread<[InferIOInput<TIO1>, InferIOInput<TIO2>]>, Spread<[InferIOOutputBroadcast<TIO1>, InferIOOutputBroadcast<TIO2>]>, Spread<[InferIOOutputSelf<TIO1>, InferIOOutputSelf<TIO2>]>, Spread<[InferIOOutputSync<TIO1>, InferIOOutputSync<TIO2>]>>;
|
|
@@ -285,7 +287,7 @@ declare class PluvIO<TPlatform extends AbstractPlatform<any> = AbstractPlatform<
|
|
|
285
287
|
}
|
|
286
288
|
|
|
287
289
|
type CreateIOParams<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TContext extends JsonObject = {}, TAuthorizeUser extends BaseUser = BaseUser, TAuthorizeRequired extends boolean = false> = Partial<PluvIOListeners<TPlatform>> & {
|
|
288
|
-
authorize?: IOAuthorize<TAuthorizeUser, TAuthorizeRequired
|
|
290
|
+
authorize?: IOAuthorize<TAuthorizeUser, TAuthorizeRequired, InferPlatformRoomContextType<TPlatform>>;
|
|
289
291
|
context?: TContext;
|
|
290
292
|
crdt?: {
|
|
291
293
|
doc: (value: any) => AbstractCrdtDocFactory<any>;
|
|
@@ -294,8 +296,6 @@ type CreateIOParams<TPlatform extends AbstractPlatform<any> = AbstractPlatform<a
|
|
|
294
296
|
getInitialStorage?: GetInitialStorageFn<TPlatform>;
|
|
295
297
|
platform: TPlatform;
|
|
296
298
|
};
|
|
297
|
-
declare const createIO: <TPlatform extends AbstractPlatform<any, {}, {}> = AbstractPlatform<any, {}, {}>, TContext extends JsonObject = {}, TAuthorizeUser extends BaseUser = BaseUser, TAuthorizeRequired extends boolean = false>(params: CreateIOParams<TPlatform, TContext, TAuthorizeUser, TAuthorizeRequired>) => PluvIO<TPlatform, IOAuthorize<TAuthorizeUser, TAuthorizeRequired
|
|
298
|
-
|
|
299
|
-
declare const version = "0.16.2";
|
|
299
|
+
declare const createIO: <TPlatform extends AbstractPlatform<any, {}, {}> = AbstractPlatform<any, {}, {}>, TContext extends JsonObject = {}, TAuthorizeUser extends BaseUser = BaseUser, TAuthorizeRequired extends boolean = false>(params: CreateIOParams<TPlatform, TContext, TAuthorizeUser, TAuthorizeRequired>) => PluvIO<TPlatform, IOAuthorize<TAuthorizeUser, TAuthorizeRequired, InferPlatformRoomContextType<TPlatform>>, TContext, BaseClientEventRecord, BaseIOEventRecord<IOAuthorize<TAuthorizeUser, TAuthorizeRequired>>, {}, {}>;
|
|
300
300
|
|
|
301
|
-
export { type AbstractCloseEvent, type AbstractErrorEvent, type AbstractEvent, type AbstractEventMap, type AbstractListener, type AbstractMessageEvent, AbstractPersistance, AbstractPlatform, type AbstractPlatformConfig, AbstractPubSub, AbstractWebSocket, type AbstractWebSocketConfig, type AbstractWebSocketHandleErrorParams, type AuthorizeModule, type AuthorizeParams, type ConvertWebSocketConfig, type CreateIOParams, type GetRoomOptions, type IOPubSubEventMessage, IORoom, type InferIORoom, type InferPlatformEventContextType, type InferPlatformRoomContextType, type InferPlatformWebSocketType, type JWT, type JWTEncodeParams, PluvIO, type PluvIOConfig,
|
|
301
|
+
export { type AbstractCloseEvent, type AbstractErrorEvent, type AbstractEvent, type AbstractEventMap, type AbstractListener, type AbstractMessageEvent, AbstractPersistance, AbstractPlatform, type AbstractPlatformConfig, AbstractPubSub, AbstractWebSocket, type AbstractWebSocketConfig, type AbstractWebSocketHandleErrorParams, type AuthorizeModule, type AuthorizeParams, type ConvertWebSocketConfig, type CreateIOParams, type GetRoomOptions, type IOPubSubEventMessage, IORoom, type InferIORoom, type InferPlatformEventContextType, type InferPlatformRoomContextType, type InferPlatformWebSocketType, type JWT, type JWTEncodeParams, PluvIO, type PluvIOConfig, createIO };
|
package/dist/index.js
CHANGED
|
@@ -84,7 +84,6 @@ __export(src_exports, {
|
|
|
84
84
|
AbstractPubSub: () => AbstractPubSub,
|
|
85
85
|
AbstractWebSocket: () => AbstractWebSocket,
|
|
86
86
|
PluvIO: () => PluvIO,
|
|
87
|
-
__PLUV_VERSION: () => version,
|
|
88
87
|
createIO: () => createIO
|
|
89
88
|
});
|
|
90
89
|
module.exports = __toCommonJS(src_exports);
|
|
@@ -348,12 +347,12 @@ var IORoom = class {
|
|
|
348
347
|
}
|
|
349
348
|
register(webSocket, options) {
|
|
350
349
|
return __async(this, null, function* () {
|
|
351
|
-
var _b
|
|
350
|
+
var _b;
|
|
352
351
|
const _a = options, { token } = _a, _platformEventContext = __objRest(_a, ["token"]);
|
|
353
352
|
const platformEventContext = _platformEventContext;
|
|
354
353
|
if (!this._uninitialize)
|
|
355
354
|
yield this._initialize();
|
|
356
|
-
const user = yield this._getAuthorizedUser(token);
|
|
355
|
+
const user = yield this._getAuthorizedUser(token, options);
|
|
357
356
|
const sessionId = this._platform.randomUUID();
|
|
358
357
|
const pluvWs = this._platform.convertWebSocket(webSocket, {
|
|
359
358
|
room: this.id,
|
|
@@ -366,7 +365,8 @@ var IORoom = class {
|
|
|
366
365
|
)} ${sessionId}`
|
|
367
366
|
);
|
|
368
367
|
const uninitializeWs = yield pluvWs.initialize();
|
|
369
|
-
const
|
|
368
|
+
const ioAuthorize = this._getIOAuthorize(options);
|
|
369
|
+
const isUnauthorized = !!(ioAuthorize == null ? void 0 : ioAuthorize.required) && !user;
|
|
370
370
|
const isTokenInUse = !!user && Array.from(this._sessions.values()).some(
|
|
371
371
|
(session2) => {
|
|
372
372
|
var _a2;
|
|
@@ -456,16 +456,23 @@ var IORoom = class {
|
|
|
456
456
|
session
|
|
457
457
|
);
|
|
458
458
|
}
|
|
459
|
-
|
|
459
|
+
_getIOAuthorize(context) {
|
|
460
|
+
if (typeof this._authorize === "function") {
|
|
461
|
+
return this._authorize(context);
|
|
462
|
+
}
|
|
463
|
+
return this._authorize;
|
|
464
|
+
}
|
|
465
|
+
_getAuthorizedUser(token, context) {
|
|
460
466
|
return __async(this, null, function* () {
|
|
461
467
|
var _a;
|
|
462
|
-
|
|
468
|
+
const ioAuthorize = this._getIOAuthorize(context);
|
|
469
|
+
if (!ioAuthorize)
|
|
463
470
|
return null;
|
|
464
471
|
if (!token)
|
|
465
472
|
return null;
|
|
466
473
|
const payload = yield authorize({
|
|
467
474
|
platform: this._platform,
|
|
468
|
-
secret:
|
|
475
|
+
secret: ioAuthorize.secret
|
|
469
476
|
}).decode(token);
|
|
470
477
|
if (!payload) {
|
|
471
478
|
this._logDebug(import_kleur.default.blue("Could not decode token:"));
|
|
@@ -481,7 +488,7 @@ var IORoom = class {
|
|
|
481
488
|
return null;
|
|
482
489
|
}
|
|
483
490
|
try {
|
|
484
|
-
return (_a =
|
|
491
|
+
return (_a = ioAuthorize.user.parse(payload.user)) != null ? _a : null;
|
|
485
492
|
} catch (e) {
|
|
486
493
|
this._logDebug(
|
|
487
494
|
`${import_kleur.default.blue("Token fails validation:")} ${token}`
|
|
@@ -741,6 +748,9 @@ var IORoom = class {
|
|
|
741
748
|
}
|
|
742
749
|
};
|
|
743
750
|
|
|
751
|
+
// src/version.generated.ts
|
|
752
|
+
var version = "0.17.0";
|
|
753
|
+
|
|
744
754
|
// src/PluvIO.ts
|
|
745
755
|
var PluvIO = class _PluvIO {
|
|
746
756
|
constructor(options) {
|
|
@@ -848,6 +858,7 @@ var PluvIO = class _PluvIO {
|
|
|
848
858
|
}
|
|
849
859
|
};
|
|
850
860
|
this._getInitialStorage = null;
|
|
861
|
+
this._version = version;
|
|
851
862
|
const {
|
|
852
863
|
authorize: authorize2,
|
|
853
864
|
context,
|
|
@@ -882,9 +893,11 @@ var PluvIO = class _PluvIO {
|
|
|
882
893
|
"IO does not specify authorize during initialization."
|
|
883
894
|
);
|
|
884
895
|
}
|
|
896
|
+
const ioAuthorize = typeof this._authorize === "function" ? this._authorize(params) : this._authorize;
|
|
897
|
+
const secret = ioAuthorize.secret;
|
|
885
898
|
return yield authorize({
|
|
886
899
|
platform: this._platform,
|
|
887
|
-
secret
|
|
900
|
+
secret
|
|
888
901
|
}).encode(params);
|
|
889
902
|
});
|
|
890
903
|
}
|
|
@@ -1014,9 +1027,6 @@ var createIO = (params) => {
|
|
|
1014
1027
|
platform
|
|
1015
1028
|
});
|
|
1016
1029
|
};
|
|
1017
|
-
|
|
1018
|
-
// src/version.generated.ts
|
|
1019
|
-
var version = "0.16.2";
|
|
1020
1030
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1021
1031
|
0 && (module.exports = {
|
|
1022
1032
|
AbstractPersistance,
|
|
@@ -1024,6 +1034,5 @@ var version = "0.16.2";
|
|
|
1024
1034
|
AbstractPubSub,
|
|
1025
1035
|
AbstractWebSocket,
|
|
1026
1036
|
PluvIO,
|
|
1027
|
-
__PLUV_VERSION,
|
|
1028
1037
|
createIO
|
|
1029
1038
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -309,12 +309,12 @@ var IORoom = class {
|
|
|
309
309
|
}
|
|
310
310
|
register(webSocket, options) {
|
|
311
311
|
return __async(this, null, function* () {
|
|
312
|
-
var _b
|
|
312
|
+
var _b;
|
|
313
313
|
const _a = options, { token } = _a, _platformEventContext = __objRest(_a, ["token"]);
|
|
314
314
|
const platformEventContext = _platformEventContext;
|
|
315
315
|
if (!this._uninitialize)
|
|
316
316
|
yield this._initialize();
|
|
317
|
-
const user = yield this._getAuthorizedUser(token);
|
|
317
|
+
const user = yield this._getAuthorizedUser(token, options);
|
|
318
318
|
const sessionId = this._platform.randomUUID();
|
|
319
319
|
const pluvWs = this._platform.convertWebSocket(webSocket, {
|
|
320
320
|
room: this.id,
|
|
@@ -327,7 +327,8 @@ var IORoom = class {
|
|
|
327
327
|
)} ${sessionId}`
|
|
328
328
|
);
|
|
329
329
|
const uninitializeWs = yield pluvWs.initialize();
|
|
330
|
-
const
|
|
330
|
+
const ioAuthorize = this._getIOAuthorize(options);
|
|
331
|
+
const isUnauthorized = !!(ioAuthorize == null ? void 0 : ioAuthorize.required) && !user;
|
|
331
332
|
const isTokenInUse = !!user && Array.from(this._sessions.values()).some(
|
|
332
333
|
(session2) => {
|
|
333
334
|
var _a2;
|
|
@@ -417,16 +418,23 @@ var IORoom = class {
|
|
|
417
418
|
session
|
|
418
419
|
);
|
|
419
420
|
}
|
|
420
|
-
|
|
421
|
+
_getIOAuthorize(context) {
|
|
422
|
+
if (typeof this._authorize === "function") {
|
|
423
|
+
return this._authorize(context);
|
|
424
|
+
}
|
|
425
|
+
return this._authorize;
|
|
426
|
+
}
|
|
427
|
+
_getAuthorizedUser(token, context) {
|
|
421
428
|
return __async(this, null, function* () {
|
|
422
429
|
var _a;
|
|
423
|
-
|
|
430
|
+
const ioAuthorize = this._getIOAuthorize(context);
|
|
431
|
+
if (!ioAuthorize)
|
|
424
432
|
return null;
|
|
425
433
|
if (!token)
|
|
426
434
|
return null;
|
|
427
435
|
const payload = yield authorize({
|
|
428
436
|
platform: this._platform,
|
|
429
|
-
secret:
|
|
437
|
+
secret: ioAuthorize.secret
|
|
430
438
|
}).decode(token);
|
|
431
439
|
if (!payload) {
|
|
432
440
|
this._logDebug(colors.blue("Could not decode token:"));
|
|
@@ -442,7 +450,7 @@ var IORoom = class {
|
|
|
442
450
|
return null;
|
|
443
451
|
}
|
|
444
452
|
try {
|
|
445
|
-
return (_a =
|
|
453
|
+
return (_a = ioAuthorize.user.parse(payload.user)) != null ? _a : null;
|
|
446
454
|
} catch (e) {
|
|
447
455
|
this._logDebug(
|
|
448
456
|
`${colors.blue("Token fails validation:")} ${token}`
|
|
@@ -702,6 +710,9 @@ var IORoom = class {
|
|
|
702
710
|
}
|
|
703
711
|
};
|
|
704
712
|
|
|
713
|
+
// src/version.generated.ts
|
|
714
|
+
var version = "0.17.0";
|
|
715
|
+
|
|
705
716
|
// src/PluvIO.ts
|
|
706
717
|
var PluvIO = class _PluvIO {
|
|
707
718
|
constructor(options) {
|
|
@@ -809,6 +820,7 @@ var PluvIO = class _PluvIO {
|
|
|
809
820
|
}
|
|
810
821
|
};
|
|
811
822
|
this._getInitialStorage = null;
|
|
823
|
+
this._version = version;
|
|
812
824
|
const {
|
|
813
825
|
authorize: authorize2,
|
|
814
826
|
context,
|
|
@@ -843,9 +855,11 @@ var PluvIO = class _PluvIO {
|
|
|
843
855
|
"IO does not specify authorize during initialization."
|
|
844
856
|
);
|
|
845
857
|
}
|
|
858
|
+
const ioAuthorize = typeof this._authorize === "function" ? this._authorize(params) : this._authorize;
|
|
859
|
+
const secret = ioAuthorize.secret;
|
|
846
860
|
return yield authorize({
|
|
847
861
|
platform: this._platform,
|
|
848
|
-
secret
|
|
862
|
+
secret
|
|
849
863
|
}).encode(params);
|
|
850
864
|
});
|
|
851
865
|
}
|
|
@@ -975,15 +989,11 @@ var createIO = (params) => {
|
|
|
975
989
|
platform
|
|
976
990
|
});
|
|
977
991
|
};
|
|
978
|
-
|
|
979
|
-
// src/version.generated.ts
|
|
980
|
-
var version = "0.16.2";
|
|
981
992
|
export {
|
|
982
993
|
AbstractPersistance,
|
|
983
994
|
AbstractPlatform,
|
|
984
995
|
AbstractPubSub,
|
|
985
996
|
AbstractWebSocket,
|
|
986
997
|
PluvIO,
|
|
987
|
-
version as __PLUV_VERSION,
|
|
988
998
|
createIO
|
|
989
999
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pluv/io",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.17.0",
|
|
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.2.2",
|
|
22
22
|
"kleur": "^4.1.5",
|
|
23
23
|
"wonka": "^6.3.4",
|
|
24
|
-
"@pluv/crdt": "^0.
|
|
25
|
-
"@pluv/types": "^0.
|
|
24
|
+
"@pluv/crdt": "^0.17.0",
|
|
25
|
+
"@pluv/types": "^0.17.0"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
|
-
"eslint": "^8.
|
|
28
|
+
"eslint": "^8.57.0",
|
|
29
29
|
"genversion": "^3.2.0",
|
|
30
30
|
"tsup": "^8.0.2",
|
|
31
31
|
"typescript": "^5.3.3",
|
|
32
|
-
"@pluv/tsconfig": "^0.
|
|
33
|
-
"eslint-config-pluv": "^0.
|
|
32
|
+
"@pluv/tsconfig": "^0.17.0",
|
|
33
|
+
"eslint-config-pluv": "^0.17.0"
|
|
34
34
|
},
|
|
35
35
|
"scripts": {
|
|
36
36
|
"build": "pnpm run version && tsup src/index.ts --format esm,cjs --dts",
|
package/src/IORoom.ts
CHANGED
|
@@ -12,6 +12,7 @@ import type {
|
|
|
12
12
|
InferIOAuthorize,
|
|
13
13
|
InferIOAuthorizeUser,
|
|
14
14
|
InferIOInput,
|
|
15
|
+
InputZodLike,
|
|
15
16
|
JsonObject,
|
|
16
17
|
Maybe,
|
|
17
18
|
} from "@pluv/types";
|
|
@@ -55,7 +56,11 @@ interface IORoomListeners<TPlatform extends AbstractPlatform> {
|
|
|
55
56
|
|
|
56
57
|
export type IORoomConfig<
|
|
57
58
|
TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>,
|
|
58
|
-
TAuthorize extends IOAuthorize<
|
|
59
|
+
TAuthorize extends IOAuthorize<
|
|
60
|
+
any,
|
|
61
|
+
any,
|
|
62
|
+
InferPlatformRoomContextType<TPlatform>
|
|
63
|
+
> = BaseIOAuthorize,
|
|
59
64
|
TContext extends JsonObject = {},
|
|
60
65
|
TInput extends EventRecord<string, any> = {},
|
|
61
66
|
TOutputBroadcast extends EventRecord<string, any> = {},
|
|
@@ -89,7 +94,11 @@ export type WebsocketRegisterOptions<TPlatform extends AbstractPlatform> = {
|
|
|
89
94
|
|
|
90
95
|
export class IORoom<
|
|
91
96
|
TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>,
|
|
92
|
-
TAuthorize extends IOAuthorize<
|
|
97
|
+
TAuthorize extends IOAuthorize<
|
|
98
|
+
any,
|
|
99
|
+
any,
|
|
100
|
+
InferPlatformRoomContextType<TPlatform>
|
|
101
|
+
> = BaseIOAuthorize,
|
|
93
102
|
TContext extends Record<string, any> = {},
|
|
94
103
|
TInput extends EventRecord<string, any> = {},
|
|
95
104
|
TOutputBroadcast extends EventRecord<string, any> = {},
|
|
@@ -197,7 +206,7 @@ export class IORoom<
|
|
|
197
206
|
|
|
198
207
|
if (!this._uninitialize) await this._initialize();
|
|
199
208
|
|
|
200
|
-
const user = await this._getAuthorizedUser(token);
|
|
209
|
+
const user = await this._getAuthorizedUser(token, options);
|
|
201
210
|
|
|
202
211
|
const sessionId = this._platform.randomUUID();
|
|
203
212
|
const pluvWs = this._platform.convertWebSocket(webSocket, {
|
|
@@ -213,8 +222,9 @@ export class IORoom<
|
|
|
213
222
|
);
|
|
214
223
|
|
|
215
224
|
const uninitializeWs = await pluvWs.initialize();
|
|
225
|
+
const ioAuthorize = this._getIOAuthorize(options);
|
|
216
226
|
|
|
217
|
-
const isUnauthorized = !!
|
|
227
|
+
const isUnauthorized = !!ioAuthorize?.required && !user;
|
|
218
228
|
// There is an attempt for multiple of the same identities. Probably malicious.
|
|
219
229
|
const isTokenInUse =
|
|
220
230
|
!!user &&
|
|
@@ -326,15 +336,30 @@ export class IORoom<
|
|
|
326
336
|
);
|
|
327
337
|
}
|
|
328
338
|
|
|
339
|
+
private _getIOAuthorize(context: InferPlatformRoomContextType<TPlatform>) {
|
|
340
|
+
if (typeof this._authorize === "function") {
|
|
341
|
+
return this._authorize(context);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
return this._authorize as {
|
|
345
|
+
required: any;
|
|
346
|
+
secret: string;
|
|
347
|
+
user: InputZodLike<any>;
|
|
348
|
+
} | null;
|
|
349
|
+
}
|
|
350
|
+
|
|
329
351
|
private async _getAuthorizedUser(
|
|
330
352
|
token: Maybe<string>,
|
|
353
|
+
context: InferPlatformRoomContextType<TPlatform>,
|
|
331
354
|
): Promise<InferIOAuthorizeUser<InferIOAuthorize<this>> | null> {
|
|
332
|
-
|
|
355
|
+
const ioAuthorize = this._getIOAuthorize(context);
|
|
356
|
+
|
|
357
|
+
if (!ioAuthorize) return null;
|
|
333
358
|
if (!token) return null;
|
|
334
359
|
|
|
335
360
|
const payload = await authorize({
|
|
336
361
|
platform: this._platform,
|
|
337
|
-
secret:
|
|
362
|
+
secret: ioAuthorize.secret,
|
|
338
363
|
}).decode(token);
|
|
339
364
|
|
|
340
365
|
if (!payload) {
|
|
@@ -355,7 +380,7 @@ export class IORoom<
|
|
|
355
380
|
}
|
|
356
381
|
|
|
357
382
|
try {
|
|
358
|
-
return
|
|
383
|
+
return ioAuthorize.user.parse(payload.user) ?? null;
|
|
359
384
|
} catch {
|
|
360
385
|
this._logDebug(
|
|
361
386
|
`${colors.blue("Token fails validation:")} ${token}`,
|
package/src/PluvIO.ts
CHANGED
|
@@ -19,6 +19,7 @@ import type {
|
|
|
19
19
|
import colors from "kleur";
|
|
20
20
|
import type {
|
|
21
21
|
AbstractPlatform,
|
|
22
|
+
InferPlatformEventContextType,
|
|
22
23
|
InferPlatformRoomContextType,
|
|
23
24
|
} from "./AbstractPlatform";
|
|
24
25
|
import type { IORoomListenerEvent } from "./IORoom";
|
|
@@ -26,6 +27,7 @@ import { IORoom } from "./IORoom";
|
|
|
26
27
|
import type { JWTEncodeParams } from "./authorize";
|
|
27
28
|
import { authorize } from "./authorize";
|
|
28
29
|
import type { EventConfig, InferEventConfig } from "./types";
|
|
30
|
+
import { __PLUV_VERSION } from "./version";
|
|
29
31
|
|
|
30
32
|
type InferIOContext<TIO extends PluvIO<any, any, any, any, any, any, any>> =
|
|
31
33
|
TIO extends PluvIO<any, any, infer IContext, any, any, any, any>
|
|
@@ -73,7 +75,11 @@ export interface PluvIOListeners<TPlatform extends AbstractPlatform> {
|
|
|
73
75
|
|
|
74
76
|
export type PluvIOConfig<
|
|
75
77
|
TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>,
|
|
76
|
-
TAuthorize extends IOAuthorize<
|
|
78
|
+
TAuthorize extends IOAuthorize<
|
|
79
|
+
any,
|
|
80
|
+
any,
|
|
81
|
+
InferPlatformRoomContextType<TPlatform>
|
|
82
|
+
> | null = null,
|
|
77
83
|
TContext extends JsonObject = {},
|
|
78
84
|
TInput extends EventRecord<string, any> = {},
|
|
79
85
|
TOutputBroadcast extends EventRecord<string, any> = {},
|
|
@@ -102,7 +108,11 @@ export type GetRoomOptions<TPlatform extends AbstractPlatform> = {
|
|
|
102
108
|
|
|
103
109
|
export class PluvIO<
|
|
104
110
|
TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>,
|
|
105
|
-
TAuthorize extends IOAuthorize<
|
|
111
|
+
TAuthorize extends IOAuthorize<
|
|
112
|
+
any,
|
|
113
|
+
any,
|
|
114
|
+
InferPlatformRoomContextType<TPlatform>
|
|
115
|
+
> = BaseIOAuthorize,
|
|
106
116
|
TContext extends JsonObject = {},
|
|
107
117
|
TInput extends EventRecord<string, any> = {},
|
|
108
118
|
TOutputBroadcast extends EventRecord<string, any> = {},
|
|
@@ -281,6 +291,7 @@ export class PluvIO<
|
|
|
281
291
|
readonly _getInitialStorage: GetInitialStorageFn<TPlatform> | null = null;
|
|
282
292
|
readonly _listeners: PluvIOListeners<TPlatform>;
|
|
283
293
|
readonly _platform: TPlatform;
|
|
294
|
+
readonly _version: string = __PLUV_VERSION as any;
|
|
284
295
|
|
|
285
296
|
constructor(
|
|
286
297
|
options: PluvIOConfig<
|
|
@@ -321,7 +332,10 @@ export class PluvIO<
|
|
|
321
332
|
}
|
|
322
333
|
|
|
323
334
|
public async createToken(
|
|
324
|
-
params: JWTEncodeParams<
|
|
335
|
+
params: JWTEncodeParams<
|
|
336
|
+
InferIOAuthorizeUser<InferIOAuthorize<this>>,
|
|
337
|
+
TPlatform
|
|
338
|
+
>,
|
|
325
339
|
): Promise<string> {
|
|
326
340
|
if (!this._authorize) {
|
|
327
341
|
throw new Error(
|
|
@@ -329,9 +343,16 @@ export class PluvIO<
|
|
|
329
343
|
);
|
|
330
344
|
}
|
|
331
345
|
|
|
346
|
+
const ioAuthorize =
|
|
347
|
+
typeof this._authorize === "function"
|
|
348
|
+
? this._authorize(params)
|
|
349
|
+
: (this._authorize as { secret: string });
|
|
350
|
+
|
|
351
|
+
const secret = ioAuthorize.secret;
|
|
352
|
+
|
|
332
353
|
return await authorize({
|
|
333
354
|
platform: this._platform,
|
|
334
|
-
secret
|
|
355
|
+
secret,
|
|
335
356
|
}).encode(params);
|
|
336
357
|
}
|
|
337
358
|
|
package/src/authorize.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import hkdf from "@panva/hkdf";
|
|
2
2
|
import type { BaseUser } from "@pluv/types";
|
|
3
3
|
import { EncryptJWT, jwtDecrypt } from "jose";
|
|
4
|
-
import type {
|
|
4
|
+
import type {
|
|
5
|
+
AbstractPlatform,
|
|
6
|
+
InferPlatformRoomContextType,
|
|
7
|
+
} from "./AbstractPlatform";
|
|
5
8
|
|
|
6
9
|
const DEFAULT_MAX_AGE_MS = 60_000;
|
|
7
10
|
|
|
@@ -21,11 +24,14 @@ export interface JWT<TUser extends BaseUser> {
|
|
|
21
24
|
user: TUser;
|
|
22
25
|
}
|
|
23
26
|
|
|
24
|
-
export
|
|
27
|
+
export type JWTEncodeParams<
|
|
28
|
+
TUser extends BaseUser,
|
|
29
|
+
TPlatform extends AbstractPlatform,
|
|
30
|
+
> = {
|
|
25
31
|
maxAge?: number;
|
|
26
32
|
room: string;
|
|
27
33
|
user: TUser;
|
|
28
|
-
}
|
|
34
|
+
} & InferPlatformRoomContextType<TPlatform>;
|
|
29
35
|
|
|
30
36
|
export interface AuthorizeParams {
|
|
31
37
|
platform: AbstractPlatform;
|
|
@@ -34,8 +40,8 @@ export interface AuthorizeParams {
|
|
|
34
40
|
|
|
35
41
|
export interface AuthorizeModule {
|
|
36
42
|
decode: <TUser extends BaseUser>(jwt: string) => Promise<TUser | null>;
|
|
37
|
-
encode: <TUser extends BaseUser>(
|
|
38
|
-
params: JWTEncodeParams<TUser>,
|
|
43
|
+
encode: <TUser extends BaseUser, TPlatform extends AbstractPlatform>(
|
|
44
|
+
params: JWTEncodeParams<TUser, TPlatform>,
|
|
39
45
|
) => Promise<string>;
|
|
40
46
|
}
|
|
41
47
|
|
|
@@ -44,8 +50,11 @@ const now = () => (Date.now() / 1_000) | 0;
|
|
|
44
50
|
export const authorize = (params: AuthorizeParams) => {
|
|
45
51
|
const { platform, secret } = params;
|
|
46
52
|
|
|
47
|
-
const encode = async <
|
|
48
|
-
|
|
53
|
+
const encode = async <
|
|
54
|
+
TUser extends BaseUser,
|
|
55
|
+
TPlatform extends AbstractPlatform,
|
|
56
|
+
>(
|
|
57
|
+
encodeParams: JWTEncodeParams<TUser, TPlatform>,
|
|
49
58
|
): Promise<string> => {
|
|
50
59
|
const { maxAge = DEFAULT_MAX_AGE_MS, room, user } = encodeParams;
|
|
51
60
|
|
package/src/createIO.ts
CHANGED
|
@@ -6,7 +6,10 @@ import type {
|
|
|
6
6
|
IOAuthorize,
|
|
7
7
|
JsonObject,
|
|
8
8
|
} from "@pluv/types";
|
|
9
|
-
import type {
|
|
9
|
+
import type {
|
|
10
|
+
AbstractPlatform,
|
|
11
|
+
InferPlatformRoomContextType,
|
|
12
|
+
} from "./AbstractPlatform";
|
|
10
13
|
import type { GetInitialStorageFn, PluvIOListeners } from "./PluvIO";
|
|
11
14
|
import { PluvIO } from "./PluvIO";
|
|
12
15
|
|
|
@@ -16,7 +19,11 @@ export type CreateIOParams<
|
|
|
16
19
|
TAuthorizeUser extends BaseUser = BaseUser,
|
|
17
20
|
TAuthorizeRequired extends boolean = false,
|
|
18
21
|
> = Partial<PluvIOListeners<TPlatform>> & {
|
|
19
|
-
authorize?: IOAuthorize<
|
|
22
|
+
authorize?: IOAuthorize<
|
|
23
|
+
TAuthorizeUser,
|
|
24
|
+
TAuthorizeRequired,
|
|
25
|
+
InferPlatformRoomContextType<TPlatform>
|
|
26
|
+
>;
|
|
20
27
|
context?: TContext;
|
|
21
28
|
crdt?: { doc: (value: any) => AbstractCrdtDocFactory<any> };
|
|
22
29
|
debug?: boolean;
|
|
@@ -38,7 +45,11 @@ export const createIO = <
|
|
|
38
45
|
>,
|
|
39
46
|
): PluvIO<
|
|
40
47
|
TPlatform,
|
|
41
|
-
IOAuthorize<
|
|
48
|
+
IOAuthorize<
|
|
49
|
+
TAuthorizeUser,
|
|
50
|
+
TAuthorizeRequired,
|
|
51
|
+
InferPlatformRoomContextType<TPlatform>
|
|
52
|
+
>,
|
|
42
53
|
TContext,
|
|
43
54
|
BaseClientEventRecord,
|
|
44
55
|
BaseIOEventRecord<IOAuthorize<TAuthorizeUser, TAuthorizeRequired>>
|
|
@@ -56,7 +67,11 @@ export const createIO = <
|
|
|
56
67
|
|
|
57
68
|
return new PluvIO<
|
|
58
69
|
TPlatform,
|
|
59
|
-
IOAuthorize<
|
|
70
|
+
IOAuthorize<
|
|
71
|
+
TAuthorizeUser,
|
|
72
|
+
TAuthorizeRequired,
|
|
73
|
+
InferPlatformRoomContextType<TPlatform>
|
|
74
|
+
>,
|
|
60
75
|
TContext,
|
|
61
76
|
BaseClientEventRecord,
|
|
62
77
|
BaseIOEventRecord<IOAuthorize<TAuthorizeUser, TAuthorizeRequired>>
|
package/src/index.ts
CHANGED
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.17.0'
|