@pluv/io 0.16.3 → 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 +56 -0
- package/dist/index.d.mts +11 -10
- package/dist/index.d.ts +11 -10
- package/dist/index.js +19 -9
- package/dist/index.mjs +19 -9
- package/package.json +6 -6
- package/src/IORoom.ts +32 -7
- package/src/PluvIO.ts +23 -4
- package/src/authorize.ts +16 -7
- package/src/createIO.ts +19 -4
- 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
|
-
[32mESM[39m [1mdist/index.mjs [22m[
|
|
16
|
-
[32mESM[39m ⚡️ Build success in
|
|
17
|
-
[32mCJS[39m [1mdist/index.js [22m[
|
|
18
|
-
[32mCJS[39m ⚡️ Build success in
|
|
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,61 @@
|
|
|
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
|
+
|
|
3
59
|
## 0.16.3
|
|
4
60
|
|
|
5
61
|
### 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;
|
|
@@ -277,7 +278,7 @@ declare class PluvIO<TPlatform extends AbstractPlatform<any> = AbstractPlatform<
|
|
|
277
278
|
readonly _platform: TPlatform;
|
|
278
279
|
readonly _version: string;
|
|
279
280
|
constructor(options: PluvIOConfig<TPlatform, TAuthorize, TContext, TInput, TOutputBroadcast, TOutputSelf, TOutputSync>);
|
|
280
|
-
createToken(params: JWTEncodeParams<InferIOAuthorizeUser<InferIOAuthorize<this
|
|
281
|
+
createToken(params: JWTEncodeParams<InferIOAuthorizeUser<InferIOAuthorize<this>>, TPlatform>): Promise<string>;
|
|
281
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]>>;
|
|
282
283
|
getRoom(room: string, options: GetRoomOptions<TPlatform>): IORoom<TPlatform, TAuthorize, TContext, TInput, TOutputBroadcast, TOutputSelf, TOutputSync>;
|
|
283
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>]>>;
|
|
@@ -286,7 +287,7 @@ declare class PluvIO<TPlatform extends AbstractPlatform<any> = AbstractPlatform<
|
|
|
286
287
|
}
|
|
287
288
|
|
|
288
289
|
type CreateIOParams<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TContext extends JsonObject = {}, TAuthorizeUser extends BaseUser = BaseUser, TAuthorizeRequired extends boolean = false> = Partial<PluvIOListeners<TPlatform>> & {
|
|
289
|
-
authorize?: IOAuthorize<TAuthorizeUser, TAuthorizeRequired
|
|
290
|
+
authorize?: IOAuthorize<TAuthorizeUser, TAuthorizeRequired, InferPlatformRoomContextType<TPlatform>>;
|
|
290
291
|
context?: TContext;
|
|
291
292
|
crdt?: {
|
|
292
293
|
doc: (value: any) => AbstractCrdtDocFactory<any>;
|
|
@@ -295,6 +296,6 @@ type CreateIOParams<TPlatform extends AbstractPlatform<any> = AbstractPlatform<a
|
|
|
295
296
|
getInitialStorage?: GetInitialStorageFn<TPlatform>;
|
|
296
297
|
platform: TPlatform;
|
|
297
298
|
};
|
|
298
|
-
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
|
|
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>>, {}, {}>;
|
|
299
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, 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;
|
|
@@ -277,7 +278,7 @@ declare class PluvIO<TPlatform extends AbstractPlatform<any> = AbstractPlatform<
|
|
|
277
278
|
readonly _platform: TPlatform;
|
|
278
279
|
readonly _version: string;
|
|
279
280
|
constructor(options: PluvIOConfig<TPlatform, TAuthorize, TContext, TInput, TOutputBroadcast, TOutputSelf, TOutputSync>);
|
|
280
|
-
createToken(params: JWTEncodeParams<InferIOAuthorizeUser<InferIOAuthorize<this
|
|
281
|
+
createToken(params: JWTEncodeParams<InferIOAuthorizeUser<InferIOAuthorize<this>>, TPlatform>): Promise<string>;
|
|
281
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]>>;
|
|
282
283
|
getRoom(room: string, options: GetRoomOptions<TPlatform>): IORoom<TPlatform, TAuthorize, TContext, TInput, TOutputBroadcast, TOutputSelf, TOutputSync>;
|
|
283
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>]>>;
|
|
@@ -286,7 +287,7 @@ declare class PluvIO<TPlatform extends AbstractPlatform<any> = AbstractPlatform<
|
|
|
286
287
|
}
|
|
287
288
|
|
|
288
289
|
type CreateIOParams<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TContext extends JsonObject = {}, TAuthorizeUser extends BaseUser = BaseUser, TAuthorizeRequired extends boolean = false> = Partial<PluvIOListeners<TPlatform>> & {
|
|
289
|
-
authorize?: IOAuthorize<TAuthorizeUser, TAuthorizeRequired
|
|
290
|
+
authorize?: IOAuthorize<TAuthorizeUser, TAuthorizeRequired, InferPlatformRoomContextType<TPlatform>>;
|
|
290
291
|
context?: TContext;
|
|
291
292
|
crdt?: {
|
|
292
293
|
doc: (value: any) => AbstractCrdtDocFactory<any>;
|
|
@@ -295,6 +296,6 @@ type CreateIOParams<TPlatform extends AbstractPlatform<any> = AbstractPlatform<a
|
|
|
295
296
|
getInitialStorage?: GetInitialStorageFn<TPlatform>;
|
|
296
297
|
platform: TPlatform;
|
|
297
298
|
};
|
|
298
|
-
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
|
|
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>>, {}, {}>;
|
|
299
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, createIO };
|
package/dist/index.js
CHANGED
|
@@ -347,12 +347,12 @@ var IORoom = class {
|
|
|
347
347
|
}
|
|
348
348
|
register(webSocket, options) {
|
|
349
349
|
return __async(this, null, function* () {
|
|
350
|
-
var _b
|
|
350
|
+
var _b;
|
|
351
351
|
const _a = options, { token } = _a, _platformEventContext = __objRest(_a, ["token"]);
|
|
352
352
|
const platformEventContext = _platformEventContext;
|
|
353
353
|
if (!this._uninitialize)
|
|
354
354
|
yield this._initialize();
|
|
355
|
-
const user = yield this._getAuthorizedUser(token);
|
|
355
|
+
const user = yield this._getAuthorizedUser(token, options);
|
|
356
356
|
const sessionId = this._platform.randomUUID();
|
|
357
357
|
const pluvWs = this._platform.convertWebSocket(webSocket, {
|
|
358
358
|
room: this.id,
|
|
@@ -365,7 +365,8 @@ var IORoom = class {
|
|
|
365
365
|
)} ${sessionId}`
|
|
366
366
|
);
|
|
367
367
|
const uninitializeWs = yield pluvWs.initialize();
|
|
368
|
-
const
|
|
368
|
+
const ioAuthorize = this._getIOAuthorize(options);
|
|
369
|
+
const isUnauthorized = !!(ioAuthorize == null ? void 0 : ioAuthorize.required) && !user;
|
|
369
370
|
const isTokenInUse = !!user && Array.from(this._sessions.values()).some(
|
|
370
371
|
(session2) => {
|
|
371
372
|
var _a2;
|
|
@@ -455,16 +456,23 @@ var IORoom = class {
|
|
|
455
456
|
session
|
|
456
457
|
);
|
|
457
458
|
}
|
|
458
|
-
|
|
459
|
+
_getIOAuthorize(context) {
|
|
460
|
+
if (typeof this._authorize === "function") {
|
|
461
|
+
return this._authorize(context);
|
|
462
|
+
}
|
|
463
|
+
return this._authorize;
|
|
464
|
+
}
|
|
465
|
+
_getAuthorizedUser(token, context) {
|
|
459
466
|
return __async(this, null, function* () {
|
|
460
467
|
var _a;
|
|
461
|
-
|
|
468
|
+
const ioAuthorize = this._getIOAuthorize(context);
|
|
469
|
+
if (!ioAuthorize)
|
|
462
470
|
return null;
|
|
463
471
|
if (!token)
|
|
464
472
|
return null;
|
|
465
473
|
const payload = yield authorize({
|
|
466
474
|
platform: this._platform,
|
|
467
|
-
secret:
|
|
475
|
+
secret: ioAuthorize.secret
|
|
468
476
|
}).decode(token);
|
|
469
477
|
if (!payload) {
|
|
470
478
|
this._logDebug(import_kleur.default.blue("Could not decode token:"));
|
|
@@ -480,7 +488,7 @@ var IORoom = class {
|
|
|
480
488
|
return null;
|
|
481
489
|
}
|
|
482
490
|
try {
|
|
483
|
-
return (_a =
|
|
491
|
+
return (_a = ioAuthorize.user.parse(payload.user)) != null ? _a : null;
|
|
484
492
|
} catch (e) {
|
|
485
493
|
this._logDebug(
|
|
486
494
|
`${import_kleur.default.blue("Token fails validation:")} ${token}`
|
|
@@ -741,7 +749,7 @@ var IORoom = class {
|
|
|
741
749
|
};
|
|
742
750
|
|
|
743
751
|
// src/version.generated.ts
|
|
744
|
-
var version = "0.
|
|
752
|
+
var version = "0.17.0";
|
|
745
753
|
|
|
746
754
|
// src/PluvIO.ts
|
|
747
755
|
var PluvIO = class _PluvIO {
|
|
@@ -885,9 +893,11 @@ var PluvIO = class _PluvIO {
|
|
|
885
893
|
"IO does not specify authorize during initialization."
|
|
886
894
|
);
|
|
887
895
|
}
|
|
896
|
+
const ioAuthorize = typeof this._authorize === "function" ? this._authorize(params) : this._authorize;
|
|
897
|
+
const secret = ioAuthorize.secret;
|
|
888
898
|
return yield authorize({
|
|
889
899
|
platform: this._platform,
|
|
890
|
-
secret
|
|
900
|
+
secret
|
|
891
901
|
}).encode(params);
|
|
892
902
|
});
|
|
893
903
|
}
|
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}`
|
|
@@ -703,7 +711,7 @@ var IORoom = class {
|
|
|
703
711
|
};
|
|
704
712
|
|
|
705
713
|
// src/version.generated.ts
|
|
706
|
-
var version = "0.
|
|
714
|
+
var version = "0.17.0";
|
|
707
715
|
|
|
708
716
|
// src/PluvIO.ts
|
|
709
717
|
var PluvIO = class _PluvIO {
|
|
@@ -847,9 +855,11 @@ var PluvIO = class _PluvIO {
|
|
|
847
855
|
"IO does not specify authorize during initialization."
|
|
848
856
|
);
|
|
849
857
|
}
|
|
858
|
+
const ioAuthorize = typeof this._authorize === "function" ? this._authorize(params) : this._authorize;
|
|
859
|
+
const secret = ioAuthorize.secret;
|
|
850
860
|
return yield authorize({
|
|
851
861
|
platform: this._platform,
|
|
852
|
-
secret
|
|
862
|
+
secret
|
|
853
863
|
}).encode(params);
|
|
854
864
|
});
|
|
855
865
|
}
|
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";
|
|
@@ -74,7 +75,11 @@ export interface PluvIOListeners<TPlatform extends AbstractPlatform> {
|
|
|
74
75
|
|
|
75
76
|
export type PluvIOConfig<
|
|
76
77
|
TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>,
|
|
77
|
-
TAuthorize extends IOAuthorize<
|
|
78
|
+
TAuthorize extends IOAuthorize<
|
|
79
|
+
any,
|
|
80
|
+
any,
|
|
81
|
+
InferPlatformRoomContextType<TPlatform>
|
|
82
|
+
> | null = null,
|
|
78
83
|
TContext extends JsonObject = {},
|
|
79
84
|
TInput extends EventRecord<string, any> = {},
|
|
80
85
|
TOutputBroadcast extends EventRecord<string, any> = {},
|
|
@@ -103,7 +108,11 @@ export type GetRoomOptions<TPlatform extends AbstractPlatform> = {
|
|
|
103
108
|
|
|
104
109
|
export class PluvIO<
|
|
105
110
|
TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>,
|
|
106
|
-
TAuthorize extends IOAuthorize<
|
|
111
|
+
TAuthorize extends IOAuthorize<
|
|
112
|
+
any,
|
|
113
|
+
any,
|
|
114
|
+
InferPlatformRoomContextType<TPlatform>
|
|
115
|
+
> = BaseIOAuthorize,
|
|
107
116
|
TContext extends JsonObject = {},
|
|
108
117
|
TInput extends EventRecord<string, any> = {},
|
|
109
118
|
TOutputBroadcast extends EventRecord<string, any> = {},
|
|
@@ -323,7 +332,10 @@ export class PluvIO<
|
|
|
323
332
|
}
|
|
324
333
|
|
|
325
334
|
public async createToken(
|
|
326
|
-
params: JWTEncodeParams<
|
|
335
|
+
params: JWTEncodeParams<
|
|
336
|
+
InferIOAuthorizeUser<InferIOAuthorize<this>>,
|
|
337
|
+
TPlatform
|
|
338
|
+
>,
|
|
327
339
|
): Promise<string> {
|
|
328
340
|
if (!this._authorize) {
|
|
329
341
|
throw new Error(
|
|
@@ -331,9 +343,16 @@ export class PluvIO<
|
|
|
331
343
|
);
|
|
332
344
|
}
|
|
333
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
|
+
|
|
334
353
|
return await authorize({
|
|
335
354
|
platform: this._platform,
|
|
336
|
-
secret
|
|
355
|
+
secret,
|
|
337
356
|
}).encode(params);
|
|
338
357
|
}
|
|
339
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/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'
|