@pluv/io 0.34.0 → 0.35.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +9 -9
- package/CHANGELOG.md +56 -0
- package/dist/index.d.mts +46 -62
- package/dist/index.d.ts +46 -62
- package/dist/index.js +9 -4
- package/dist/index.mjs +9 -4
- package/package.json +5 -5
- package/src/AbstractWebSocket.ts +4 -3
- package/src/IORoom.ts +26 -16
- package/src/PluvIO.ts +11 -15
- package/src/PluvProcedure.ts +14 -13
- package/src/PluvRouter.ts +3 -3
- package/src/PluvServer.ts +14 -11
- package/src/authorize.ts +2 -2
- package/src/createIO.ts +7 -23
- package/src/types.ts +24 -37
- package/src/version.generated.ts +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { JsonObject, MaybePromise, IOAuthorize, IOEventMessage, EventRecord, InputZodLike, ProcedureLike, IORouterLike,
|
|
1
|
+
import { JsonObject, MaybePromise, IOAuthorize, IOEventMessage, EventRecord, InputZodLike, ProcedureLike, IORouterLike, InferIOAuthorizeUser, Id, Maybe, BaseUser, UndefinedProps, InferEventMessage, InferEventsOutput, IOLike, InferIOOutput, InferIOInput, InferIOAuthorize } from '@pluv/types';
|
|
2
2
|
export { BaseUser, EventMessage, EventRecord, IOEventMessage } from '@pluv/types';
|
|
3
3
|
import { AbstractCrdtDocFactory, AbstractCrdtDoc } from '@pluv/crdt';
|
|
4
4
|
|
|
@@ -77,39 +77,39 @@ declare abstract class AbstractWebSocket<TWebSocket = any> {
|
|
|
77
77
|
abstract close(code?: number | undefined, reason?: string | undefined): void;
|
|
78
78
|
abstract send(message: ArrayBuffer | ArrayBufferView | string): void;
|
|
79
79
|
abstract terminate(): void;
|
|
80
|
-
getSession<TAuthorize extends IOAuthorize<any, any
|
|
80
|
+
getSession<TAuthorize extends IOAuthorize<any, any> | null>(): Promise<WebSocketSession<TAuthorize>>;
|
|
81
81
|
handleError(params: AbstractWebSocketHandleErrorParams): void;
|
|
82
82
|
sendMessage<TMessage extends IOEventMessage<any> = IOEventMessage<any>>(data: TMessage): void;
|
|
83
83
|
}
|
|
84
84
|
|
|
85
|
-
interface PluvProcedureConfig<TPlatform extends AbstractPlatform<any>, TAuthorize extends IOAuthorize<any,
|
|
86
|
-
broadcast?: EventResolver<TPlatform, TAuthorize, TContext, TInput, Partial<TOutput>> | null;
|
|
85
|
+
interface PluvProcedureConfig<TPlatform extends AbstractPlatform<any>, TAuthorize extends IOAuthorize<any, InferInitContextType<TPlatform>> | null, TContext extends Record<string, any>, TInput extends JsonObject = {}, TOutput extends EventRecord<string, any> = {}> {
|
|
86
|
+
broadcast?: EventResolver<"broadcast", TPlatform, TAuthorize, TContext, TInput, Partial<TOutput>> | null;
|
|
87
87
|
input?: InputZodLike<TInput>;
|
|
88
|
-
self?: EventResolver<TPlatform, TAuthorize, TContext, TInput, Partial<TOutput>> | null;
|
|
89
|
-
sync?: EventResolver<TPlatform, TAuthorize, TContext, TInput, Partial<TOutput>> | null;
|
|
88
|
+
self?: EventResolver<"self", TPlatform, TAuthorize, TContext, TInput, Partial<TOutput>> | null;
|
|
89
|
+
sync?: EventResolver<"sync", TPlatform, TAuthorize, TContext, TInput, Partial<TOutput>> | null;
|
|
90
90
|
}
|
|
91
|
-
declare class PluvProcedure<TPlatform extends AbstractPlatform<any>, TAuthorize extends IOAuthorize<any,
|
|
91
|
+
declare class PluvProcedure<TPlatform extends AbstractPlatform<any>, TAuthorize extends IOAuthorize<any, InferInitContextType<TPlatform>> | null, TContext extends Record<string, any>, TInput extends JsonObject = {}, TOutput extends EventRecord<string, any> = {}, TFilled extends "input" | "broadcast" | "self" | "sync" | "" = ""> {
|
|
92
92
|
private _broadcast;
|
|
93
93
|
private _input;
|
|
94
94
|
private _self;
|
|
95
95
|
private _sync;
|
|
96
96
|
get config(): ProcedureLike<TInput, TOutput>["config"];
|
|
97
97
|
constructor(config?: PluvProcedureConfig<TPlatform, TAuthorize, TContext, TInput, TOutput>);
|
|
98
|
-
broadcast<TResult extends EventRecord<string, any> = {}>(resolver: EventResolver<TPlatform, TAuthorize, TContext, TInput, TResult>): Omit<PluvProcedure<TPlatform, TAuthorize, TContext, TInput, MergeEventRecords<[TOutput, TResult]>, TFilled | "input" | "broadcast">, TFilled | "input" | "broadcast">;
|
|
98
|
+
broadcast<TResult extends EventRecord<string, any> = {}>(resolver: EventResolver<"broadcast", TPlatform, TAuthorize, TContext, TInput, TResult>): Omit<PluvProcedure<TPlatform, TAuthorize, TContext, TInput, MergeEventRecords<[TOutput, TResult]>, TFilled | "input" | "broadcast">, TFilled | "input" | "broadcast">;
|
|
99
99
|
input<TData extends JsonObject>(input: InputZodLike<TData>): Omit<PluvProcedure<TPlatform, TAuthorize, TContext, TData, {}, TFilled | "input">, TFilled | "input">;
|
|
100
|
-
self<TResult extends EventRecord<string, any> = {}>(resolver: EventResolver<TPlatform, TAuthorize, TContext, TInput, TResult>): Omit<PluvProcedure<TPlatform, TAuthorize, TContext, TInput, MergeEventRecords<[TOutput, TResult]>, TFilled | "input" | "self">, TFilled | "input" | "self">;
|
|
101
|
-
sync<TResult extends EventRecord<string, any> = {}>(resolver: EventResolver<TPlatform, TAuthorize, TContext, TInput, TResult>): Omit<PluvProcedure<TPlatform, TAuthorize, TContext, TInput, MergeEventRecords<[TOutput, TResult]>, TFilled | "input" | "sync">, TFilled | "input" | "sync">;
|
|
100
|
+
self<TResult extends EventRecord<string, any> = {}>(resolver: EventResolver<"self", TPlatform, TAuthorize, TContext, TInput, TResult>): Omit<PluvProcedure<TPlatform, TAuthorize, TContext, TInput, MergeEventRecords<[TOutput, TResult]>, TFilled | "input" | "self">, TFilled | "input" | "self">;
|
|
101
|
+
sync<TResult extends EventRecord<string, any> = {}>(resolver: EventResolver<"sync", TPlatform, TAuthorize, TContext, TInput, TResult>): Omit<PluvProcedure<TPlatform, TAuthorize, TContext, TInput, MergeEventRecords<[TOutput, TResult]>, TFilled | "input" | "sync">, TFilled | "input" | "sync">;
|
|
102
102
|
private _resolver;
|
|
103
103
|
}
|
|
104
104
|
|
|
105
|
-
type PluvRouterEventConfig<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any,
|
|
105
|
+
type PluvRouterEventConfig<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null = any, TContext extends Record<string, any> = {}> = {
|
|
106
106
|
[P: string]: Pick<PluvProcedure<TPlatform, TAuthorize, TContext, any, any>, "config">;
|
|
107
107
|
};
|
|
108
|
-
type MergedRouter<TRouters extends PluvRouter<TPlatform, TAuthorize, TContext, any>[] = [], TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any,
|
|
108
|
+
type MergedRouter<TRouters extends PluvRouter<TPlatform, TAuthorize, TContext, any>[] = [], TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> = any, TContext extends Record<string, any> = {}, TRoot extends TRouters[0]["_defs"]["events"] = {}> = TRouters extends [
|
|
109
109
|
infer IHead extends PluvRouter<TPlatform, TAuthorize, TContext, any>,
|
|
110
110
|
...infer ITail extends PluvRouter<TPlatform, TAuthorize, TContext, any>[]
|
|
111
111
|
] ? MergedRouter<ITail, TPlatform, TAuthorize, TContext, TRoot & IHead["_defs"]["events"]> : PluvRouter<TPlatform, TAuthorize, TContext, TRoot>;
|
|
112
|
-
declare class PluvRouter<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any,
|
|
112
|
+
declare class PluvRouter<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null = any, TContext extends Record<string, any> = {}, TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {}> implements IORouterLike<TEvents> {
|
|
113
113
|
readonly _defs: {
|
|
114
114
|
events: TEvents;
|
|
115
115
|
};
|
|
@@ -127,24 +127,13 @@ interface CrdtLibraryType {
|
|
|
127
127
|
doc: (value: any) => AbstractCrdtDocFactory<any>;
|
|
128
128
|
kind: "loro" | "yjs";
|
|
129
129
|
}
|
|
130
|
-
type
|
|
131
|
-
|
|
130
|
+
type EventResolverKind = "broadcast" | "self" | "sync";
|
|
131
|
+
type EventResolver<TKind extends EventResolverKind = EventResolverKind, TPlatform extends AbstractPlatform = AbstractPlatform, TAuthorize extends IOAuthorize<any, InferInitContextType<TPlatform>> | null = null, TContext extends Record<string, any> = {}, TInput extends JsonObject = {}, TOutput extends EventRecord<string, any> = {}> = (data: TInput, context: EventResolverContext<TKind, TPlatform, TAuthorize, TContext>) => MaybePromise<TOutput | void>;
|
|
132
|
+
interface EventResolverContext<TKind extends EventResolverKind = EventResolverKind, TPlatform extends AbstractPlatform = AbstractPlatform, TAuthorize extends IOAuthorize<any, InferInitContextType<TPlatform>> | null = null, TContext extends Record<string, any> = {}> {
|
|
132
133
|
context: TContext;
|
|
133
134
|
doc: AbstractCrdtDoc<any>;
|
|
134
135
|
room: string;
|
|
135
|
-
|
|
136
|
-
* !HACK
|
|
137
|
-
* @description Session should only not exist when invoking the event as a
|
|
138
|
-
* "sync" event on another machine where the session doesn't exist. There must
|
|
139
|
-
* be a better way to handle this, but going to make this nullable for ease
|
|
140
|
-
* for now.
|
|
141
|
-
*
|
|
142
|
-
* `session` should not be referenced most of the time anyhow (outside of
|
|
143
|
-
* internal to this library).
|
|
144
|
-
* @author leedavidcs
|
|
145
|
-
* @date December 25, 2022
|
|
146
|
-
*/
|
|
147
|
-
session: WebSocketSession<TAuthorize> | null;
|
|
136
|
+
session: TKind extends "sync" ? WebSocketSession<TAuthorize> | null : WebSocketSession<TAuthorize>;
|
|
148
137
|
sessions: readonly WebSocketSession<TAuthorize>[];
|
|
149
138
|
}
|
|
150
139
|
type SendMessageOptions = {
|
|
@@ -164,7 +153,7 @@ interface WebSocketSerializedState {
|
|
|
164
153
|
room: string;
|
|
165
154
|
timers: WebSocketSessionTimers;
|
|
166
155
|
}
|
|
167
|
-
type WebSocketSession<TAuthorize extends IOAuthorize<any, any
|
|
156
|
+
type WebSocketSession<TAuthorize extends IOAuthorize<any, any> | null = null> = WebSocketSerializedState & {
|
|
168
157
|
id: string;
|
|
169
158
|
user: InferIOAuthorizeUser<TAuthorize>;
|
|
170
159
|
webSocket: AbstractWebSocket;
|
|
@@ -184,7 +173,6 @@ type HandleMode = "io" | "fetch";
|
|
|
184
173
|
type WebSocketRegistrationMode = "attached" | "detached";
|
|
185
174
|
interface PlatformConfig {
|
|
186
175
|
authorize: {
|
|
187
|
-
required?: boolean;
|
|
188
176
|
secret?: boolean;
|
|
189
177
|
};
|
|
190
178
|
requireAuth?: boolean;
|
|
@@ -198,21 +186,20 @@ interface PlatformConfig {
|
|
|
198
186
|
onUserDisconnected?: boolean;
|
|
199
187
|
};
|
|
200
188
|
}
|
|
201
|
-
type ResolvedPluvIOAuthorize<TPlatform extends AbstractPlatform<any, any, any, any>, TUser extends BaseUser = any
|
|
189
|
+
type ResolvedPluvIOAuthorize<TPlatform extends AbstractPlatform<any, any, any, any>, TUser extends BaseUser = any> = {
|
|
202
190
|
user: InputZodLike<TUser>;
|
|
203
|
-
required?: TRequired;
|
|
204
191
|
} & UndefinedProps<{
|
|
205
192
|
secret?: string;
|
|
206
193
|
}, Exclude<"secret", InferPlatformAuthorizeProperties<TPlatform>>>;
|
|
207
|
-
type PluvIOAuthorize<TPlatform extends AbstractPlatform<any, any, any, any>, TUser extends BaseUser = any,
|
|
208
|
-
type BasePluvIOListeners<TPlatform extends AbstractPlatform<any, any, any, any>, TAuthorize extends PluvIOAuthorize<TPlatform, any,
|
|
194
|
+
type PluvIOAuthorize<TPlatform extends AbstractPlatform<any, any, any, any>, TUser extends BaseUser = any, TContext extends Record<string, unknown> = {}> = ResolvedPluvIOAuthorize<TPlatform, TUser> | ((context: TContext) => ResolvedPluvIOAuthorize<TPlatform, TUser>);
|
|
195
|
+
type BasePluvIOListeners<TPlatform extends AbstractPlatform<any, any, any, any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null, TContext extends Record<string, any>, TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext>> = {
|
|
209
196
|
onRoomDeleted: (event: IORoomListenerEvent<TContext>) => void;
|
|
210
197
|
onRoomMessage: (event: IORoomMessageEvent<TPlatform, TAuthorize, TContext, TEvents>) => void;
|
|
211
198
|
onStorageUpdated: (event: IOStorageUpdatedEvent<TPlatform, TAuthorize, TContext>) => void;
|
|
212
199
|
onUserConnected: (event: IOUserConnectedEvent<TPlatform, TAuthorize, TContext>) => void;
|
|
213
200
|
onUserDisconnected: (event: IOUserDisconnectedEvent<TPlatform, TAuthorize, TContext>) => void;
|
|
214
201
|
};
|
|
215
|
-
type PluvIOListeners<TPlatform extends AbstractPlatform<any, any, any, any>, TAuthorize extends PluvIOAuthorize<TPlatform, any,
|
|
202
|
+
type PluvIOListeners<TPlatform extends AbstractPlatform<any, any, any, any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null, TContext extends Record<string, any>, TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext>> = UndefinedProps<BasePluvIOListeners<TPlatform, TAuthorize, TContext, TEvents>, Exclude<keyof BasePluvIOListeners<TPlatform, TAuthorize, TContext, TEvents>, InferPlatformListeners<TPlatform>>>;
|
|
216
203
|
type InferPlatformConfig<TPlatform extends AbstractPlatform<any, any, any, any>> = TPlatform extends AbstractPlatform<any, any, any, infer IConfig> ? IConfig : never;
|
|
217
204
|
type InferPlatformAuthorizeProperties<TPlatform extends AbstractPlatform<any, any, any, any>> = keyof {
|
|
218
205
|
[P in keyof PlatformConfig["authorize"] as InferPlatformConfig<TPlatform>["authorize"][P] extends true | undefined ? P : never]: true;
|
|
@@ -225,20 +212,20 @@ type IORoomListenerEvent<TContext extends Record<string, any>> = {
|
|
|
225
212
|
encodedState: string | null;
|
|
226
213
|
room: string;
|
|
227
214
|
};
|
|
228
|
-
type IORoomMessageEvent<TPlatform extends AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any,
|
|
215
|
+
type IORoomMessageEvent<TPlatform extends AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null, TContext extends Record<string, any>, TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext>> = IORoomListenerEvent<TContext> & {
|
|
229
216
|
message: InferEventMessage<InferEventsOutput<TEvents>, keyof InferEventsOutput<TEvents>>;
|
|
230
217
|
user?: InferIOAuthorizeUser<TAuthorize>;
|
|
231
218
|
webSocket?: InferPlatformWebSocketSource<TPlatform>;
|
|
232
219
|
};
|
|
233
|
-
type IOStorageUpdatedEvent<TPlatform extends AbstractPlatform<any>, TAuthorize extends IOAuthorize<any,
|
|
220
|
+
type IOStorageUpdatedEvent<TPlatform extends AbstractPlatform<any>, TAuthorize extends IOAuthorize<any, InferInitContextType<TPlatform>> | null, TContext extends Record<string, any>> = IORoomListenerEvent<TContext> & {
|
|
234
221
|
user?: InferIOAuthorizeUser<TAuthorize>;
|
|
235
222
|
webSocket?: InferPlatformWebSocketSource<TPlatform>;
|
|
236
223
|
};
|
|
237
|
-
type IOUserConnectedEvent<TPlatform extends AbstractPlatform<any>, TAuthorize extends IOAuthorize<any,
|
|
224
|
+
type IOUserConnectedEvent<TPlatform extends AbstractPlatform<any>, TAuthorize extends IOAuthorize<any, InferInitContextType<TPlatform>> | null, TContext extends Record<string, any>> = IORoomListenerEvent<TContext> & {
|
|
238
225
|
user?: InferIOAuthorizeUser<TAuthorize>;
|
|
239
226
|
webSocket?: InferPlatformWebSocketSource<TPlatform>;
|
|
240
227
|
};
|
|
241
|
-
type IOUserDisconnectedEvent<TPlatform extends AbstractPlatform<any>, TAuthorize extends IOAuthorize<any,
|
|
228
|
+
type IOUserDisconnectedEvent<TPlatform extends AbstractPlatform<any>, TAuthorize extends IOAuthorize<any, InferInitContextType<TPlatform>> | null, TContext extends Record<string, any>> = IORoomListenerEvent<TContext> & {
|
|
242
229
|
user?: InferIOAuthorizeUser<TAuthorize>;
|
|
243
230
|
};
|
|
244
231
|
type WebSocketType<TPlatform extends AbstractPlatform> = InferPlatformWebSocketType<TPlatform> | InferPlatformWebSocketSource<TPlatform>;
|
|
@@ -257,10 +244,10 @@ interface JWT<TUser extends BaseUser> {
|
|
|
257
244
|
sub: string;
|
|
258
245
|
user: TUser;
|
|
259
246
|
}
|
|
260
|
-
type JWTEncodeParams<TUser extends BaseUser, TPlatform extends AbstractPlatform> = {
|
|
247
|
+
type JWTEncodeParams<TUser extends BaseUser | null, TPlatform extends AbstractPlatform> = {
|
|
261
248
|
maxAge?: number;
|
|
262
249
|
room: string;
|
|
263
|
-
user: TUser;
|
|
250
|
+
user: TUser extends BaseUser ? TUser : "Error: Cannot create token without authorization!";
|
|
264
251
|
} & InferInitContextType<TPlatform>;
|
|
265
252
|
interface AuthorizeParams {
|
|
266
253
|
platform: AbstractPlatform;
|
|
@@ -312,7 +299,7 @@ declare abstract class AbstractPlatform<TWebSocket extends AbstractWebSocket = A
|
|
|
312
299
|
protected _initialize(): this;
|
|
313
300
|
}
|
|
314
301
|
|
|
315
|
-
interface IORoomListeners<TPlatform extends AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any,
|
|
302
|
+
interface IORoomListeners<TPlatform extends AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null, TContext extends Record<string, any>, TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext>> {
|
|
316
303
|
onDestroy: (event: IORoomListenerEvent<TContext>) => void;
|
|
317
304
|
onMessage: (event: IORoomMessageEvent<TPlatform, TAuthorize, TContext, TEvents>) => void;
|
|
318
305
|
onUserConnected: (event: IOUserConnectedEvent<TPlatform, TAuthorize, TContext>) => void;
|
|
@@ -321,7 +308,7 @@ interface IORoomListeners<TPlatform extends AbstractPlatform<any>, TAuthorize ex
|
|
|
321
308
|
type BroadcastProxy<TIO extends IORoom<any, any, any, any>> = (<TEvent extends keyof InferIOInput<TIO>>(event: TEvent, data: Id<InferIOInput<TIO>[TEvent]>) => Promise<void>) & {
|
|
322
309
|
[event in keyof InferIOInput<TIO>]: (data: Id<InferIOInput<TIO>>[event]) => Promise<void>;
|
|
323
310
|
};
|
|
324
|
-
type IORoomConfig<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any,
|
|
311
|
+
type IORoomConfig<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null = any, TContext extends Record<string, any> = {}, TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {}> = Partial<IORoomListeners<TPlatform, TAuthorize, TContext, TEvents>> & {
|
|
325
312
|
authorize?: TAuthorize;
|
|
326
313
|
context: TContext;
|
|
327
314
|
crdt?: {
|
|
@@ -335,7 +322,7 @@ type IORoomConfig<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any
|
|
|
335
322
|
type WebsocketRegisterConfig<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>> = {
|
|
336
323
|
token?: string | null;
|
|
337
324
|
} & InferInitContextType<TPlatform>;
|
|
338
|
-
declare class IORoom<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any,
|
|
325
|
+
declare class IORoom<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null = null, TContext extends Record<string, any> = {}, TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {}> implements IOLike<TAuthorize, TEvents> {
|
|
339
326
|
readonly id: string;
|
|
340
327
|
private _doc;
|
|
341
328
|
private _uninitialize;
|
|
@@ -353,7 +340,7 @@ declare class IORoom<TPlatform extends AbstractPlatform<any> = AbstractPlatform<
|
|
|
353
340
|
* @deprecated Internal use only. Changes to this will never be marked as breaking.
|
|
354
341
|
*/
|
|
355
342
|
get _defs(): {
|
|
356
|
-
authorize: TAuthorize
|
|
343
|
+
authorize: TAuthorize;
|
|
357
344
|
context: TContext;
|
|
358
345
|
events: TEvents;
|
|
359
346
|
platform: TPlatform;
|
|
@@ -403,7 +390,7 @@ declare class IORoom<TPlatform extends AbstractPlatform<any> = AbstractPlatform<
|
|
|
403
390
|
}
|
|
404
391
|
|
|
405
392
|
type InferIORoom<TServer extends PluvServer<any, any, any, any>> = TServer extends PluvServer<infer IPlatform, infer IAuthorize, infer IContext, infer IEvents> ? IORoom<IPlatform, IAuthorize, IContext, IEvents> : never;
|
|
406
|
-
type PluvServerConfig<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any,
|
|
393
|
+
type PluvServerConfig<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null = any, TContext extends Record<string, any> = {}, TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {}> = Partial<PluvIOListeners<TPlatform, TAuthorize, TContext, TEvents>> & {
|
|
407
394
|
authorize?: TAuthorize;
|
|
408
395
|
context?: PluvContext<TPlatform, TContext>;
|
|
409
396
|
crdt?: {
|
|
@@ -414,11 +401,11 @@ type PluvServerConfig<TPlatform extends AbstractPlatform<any> = AbstractPlatform
|
|
|
414
401
|
platform: TPlatform;
|
|
415
402
|
router?: PluvRouter<TPlatform, TAuthorize, TContext, TEvents>;
|
|
416
403
|
};
|
|
417
|
-
type BaseCreateRoomOptions<TPlatform extends AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any,
|
|
404
|
+
type BaseCreateRoomOptions<TPlatform extends AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null, TContext extends Record<string, any>, TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext>> = Partial<IORoomListeners<TPlatform, TAuthorize, TContext, TEvents>> & {
|
|
418
405
|
debug?: boolean;
|
|
419
406
|
};
|
|
420
|
-
type CreateRoomOptions<TPlatform extends AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any,
|
|
421
|
-
declare class PluvServer<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any,
|
|
407
|
+
type CreateRoomOptions<TPlatform extends AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null, TContext extends Record<string, any>, TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext>> = keyof InferRoomContextType<TPlatform> extends never ? [BaseCreateRoomOptions<TPlatform, TAuthorize, TContext, TEvents>] | [] : [Id<BaseCreateRoomOptions<TPlatform, TAuthorize, TContext, TEvents> & InferRoomContextType<TPlatform>>];
|
|
408
|
+
declare class PluvServer<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null = any, TContext extends Record<string, any> = {}, TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {}> implements IOLike<TAuthorize, TEvents> {
|
|
422
409
|
readonly version: string;
|
|
423
410
|
private readonly _authorize;
|
|
424
411
|
private readonly _baseRouter;
|
|
@@ -435,8 +422,8 @@ declare class PluvServer<TPlatform extends AbstractPlatform<any> = AbstractPlatf
|
|
|
435
422
|
* @deprecated Internal use only. Changes to this will never be marked as breaking.
|
|
436
423
|
*/
|
|
437
424
|
get _defs(): {
|
|
438
|
-
authorize: TAuthorize
|
|
439
|
-
context:
|
|
425
|
+
authorize: TAuthorize;
|
|
426
|
+
context: TContext;
|
|
440
427
|
events: TEvents;
|
|
441
428
|
platform: TPlatform;
|
|
442
429
|
};
|
|
@@ -449,21 +436,18 @@ declare class PluvServer<TPlatform extends AbstractPlatform<any> = AbstractPlatf
|
|
|
449
436
|
private _logDebug;
|
|
450
437
|
}
|
|
451
438
|
|
|
452
|
-
type PluvIOConfig<TPlatform extends AbstractPlatform<any>, TAuthorize extends IOAuthorize<any,
|
|
439
|
+
type PluvIOConfig<TPlatform extends AbstractPlatform<any>, TAuthorize extends IOAuthorize<any, InferInitContextType<TPlatform>> | null, TContext extends Record<string, any>> = {
|
|
440
|
+
authorize?: TAuthorize;
|
|
453
441
|
context?: PluvContext<TPlatform, TContext>;
|
|
454
442
|
crdt?: CrdtLibraryType;
|
|
455
443
|
debug?: boolean;
|
|
456
444
|
platform: TPlatform;
|
|
457
|
-
}
|
|
458
|
-
|
|
459
|
-
} : {
|
|
460
|
-
authorize?: TAuthorize;
|
|
461
|
-
});
|
|
462
|
-
type ServerConfig<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, any, InferInitContextType<TPlatform>> = any, TContext extends Record<string, any> = {}, TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {}> = Partial<PluvIOListeners<TPlatform, TAuthorize, TContext, TEvents>> & {
|
|
445
|
+
};
|
|
446
|
+
type ServerConfig<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null = any, TContext extends Record<string, any> = {}, TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {}> = Partial<PluvIOListeners<TPlatform, TAuthorize, TContext, TEvents>> & {
|
|
463
447
|
getInitialStorage?: GetInitialStorageFn<TContext>;
|
|
464
448
|
router?: PluvRouter<TPlatform, TAuthorize, TContext, TEvents>;
|
|
465
449
|
};
|
|
466
|
-
declare class PluvIO<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any,
|
|
450
|
+
declare class PluvIO<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null = any, TContext extends Record<string, any> = {}> {
|
|
467
451
|
readonly version: string;
|
|
468
452
|
private readonly _authorize;
|
|
469
453
|
private readonly _context;
|
|
@@ -479,13 +463,13 @@ declare class PluvIO<TPlatform extends AbstractPlatform<any> = AbstractPlatform<
|
|
|
479
463
|
private _getIOAuthorize;
|
|
480
464
|
}
|
|
481
465
|
|
|
482
|
-
type CreateIOParams<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TContext extends Record<string, any> = {},
|
|
483
|
-
authorize?:
|
|
466
|
+
type CreateIOParams<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TContext extends Record<string, any> = {}, TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null = null> = {
|
|
467
|
+
authorize?: TAuthorize;
|
|
484
468
|
context?: PluvContext<TPlatform, TContext>;
|
|
485
469
|
crdt?: CrdtLibraryType;
|
|
486
470
|
debug?: boolean;
|
|
487
471
|
platform: TPlatform;
|
|
488
472
|
};
|
|
489
|
-
declare const createIO: <TPlatform extends AbstractPlatform<any> = AbstractPlatform<any, {}, {}, PlatformConfig>, TContext extends Record<string, any> = {},
|
|
473
|
+
declare const createIO: <TPlatform extends AbstractPlatform<any> = AbstractPlatform<any, {}, {}, PlatformConfig>, TContext extends Record<string, any> = {}, TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null = null>(params: CreateIOParams<TPlatform, TContext, TAuthorize>) => PluvIO<TPlatform, TAuthorize, TContext>;
|
|
490
474
|
|
|
491
475
|
export { type AbstractCloseEvent, type AbstractErrorEvent, type AbstractEvent, type AbstractEventMap, type AbstractListener, type AbstractMessageEvent, AbstractPersistence, AbstractPlatform, type AbstractPlatformConfig, AbstractPubSub, AbstractWebSocket, type AbstractWebSocketConfig, type AbstractWebSocketHandleErrorParams, type AuthorizeModule, type AuthorizeParams, type ConvertWebSocketConfig, type CrdtLibraryType, type CreateIOParams, type CreateRoomOptions, type GetInitialStorageFn, type HandleMode, type IOPubSubEventMessage, IORoom, type IORoomListenerEvent, type IORoomMessageEvent, type IOStorageUpdatedEvent, type IOUserConnectedEvent, type IOUserDisconnectedEvent, type InferIORoom, type InferInitContextType, type InferPlatformWebSocketType, type InferRoomContextType, type JWT, type JWTEncodeParams, type MergedRouter, type PlatformConfig, PluvIO, type PluvIOConfig, type PluvIOListeners, PluvProcedure, type PluvProcedureConfig, PluvRouter, type PluvRouterEventConfig, PluvServer, type PluvServerConfig, type ServerConfig, type WebSocketRegistrationMode, type WebSocketSerializedState, type WebSocketSession, authorize, createIO };
|
package/dist/index.js
CHANGED
|
@@ -270,8 +270,9 @@ var AbstractWebSocket = class {
|
|
|
270
270
|
const { error, message: _message, room, session } = params;
|
|
271
271
|
const message = error instanceof Error ? error.message : "Unexpected error";
|
|
272
272
|
const stack = error instanceof Error ? (_a = error.stack) != null ? _a : null : null;
|
|
273
|
+
const connectionId = (_b = session == null ? void 0 : session.id) != null ? _b : null;
|
|
273
274
|
return this.sendMessage({
|
|
274
|
-
connectionId
|
|
275
|
+
connectionId,
|
|
275
276
|
data: { message, stack },
|
|
276
277
|
room,
|
|
277
278
|
type: "$ERROR",
|
|
@@ -539,7 +540,7 @@ var IORoom = class {
|
|
|
539
540
|
}
|
|
540
541
|
const user = yield this._getAuthorizedUser(token, _options);
|
|
541
542
|
const ioAuthorize = this._getIOAuthorize(_options);
|
|
542
|
-
const isUnauthorized = !!
|
|
543
|
+
const isUnauthorized = !!ioAuthorize && !user;
|
|
543
544
|
const isTokenInUse = !!user && (yield this._getSessions().then((sessions) => sessions.some((session) => {
|
|
544
545
|
var _a2;
|
|
545
546
|
return ((_a2 = session.user) == null ? void 0 : _a2.id) === user.id;
|
|
@@ -575,6 +576,7 @@ var IORoom = class {
|
|
|
575
576
|
const sender = senderId ? (_a = this._sessions.get(senderId)) != null ? _a : null : null;
|
|
576
577
|
const session = (_b = yield sender == null ? void 0 : sender.getSession()) != null ? _b : null;
|
|
577
578
|
const user = (_c = session == null ? void 0 : session.user) != null ? _c : null;
|
|
579
|
+
if (typeof senderId !== "string") return;
|
|
578
580
|
this._platform.pubSub.publish(this.id, __spreadValues({
|
|
579
581
|
connectionId: senderId != null ? senderId : null,
|
|
580
582
|
room: this.id,
|
|
@@ -923,6 +925,7 @@ var IORoom = class {
|
|
|
923
925
|
const { data, type } = message;
|
|
924
926
|
const connectionId = (_a = sender == null ? void 0 : sender.sessionId) != null ? _a : null;
|
|
925
927
|
const room = this.id;
|
|
928
|
+
if (typeof connectionId !== "string") return;
|
|
926
929
|
const webSockets = (_b = sessionIds == null ? void 0 : sessionIds.reduce((dict, id) => {
|
|
927
930
|
const pluvWs = this._sessions.get(id);
|
|
928
931
|
return pluvWs ? dict.set(id, pluvWs) : dict;
|
|
@@ -957,6 +960,8 @@ var IORoom = class {
|
|
|
957
960
|
return __async(this, null, function* () {
|
|
958
961
|
var _a;
|
|
959
962
|
if (!sender) return;
|
|
963
|
+
const connectionId = sender.sessionId;
|
|
964
|
+
if (typeof connectionId !== "string") return;
|
|
960
965
|
const doc = yield this._doc;
|
|
961
966
|
const context = {
|
|
962
967
|
context: this._context,
|
|
@@ -979,7 +984,7 @@ var IORoom = class {
|
|
|
979
984
|
var _a2, _b;
|
|
980
985
|
const data = (_a2 = output[type]) != null ? _a2 : {};
|
|
981
986
|
this._platform.pubSub.publish(this.id, {
|
|
982
|
-
connectionId
|
|
987
|
+
connectionId,
|
|
983
988
|
data,
|
|
984
989
|
options: { type: "self" },
|
|
985
990
|
room: this.id,
|
|
@@ -993,7 +998,7 @@ var IORoom = class {
|
|
|
993
998
|
};
|
|
994
999
|
|
|
995
1000
|
// src/version.generated.ts
|
|
996
|
-
var version = "0.
|
|
1001
|
+
var version = "0.35.0";
|
|
997
1002
|
|
|
998
1003
|
// src/PluvServer.ts
|
|
999
1004
|
var PluvServer = class {
|
package/dist/index.mjs
CHANGED
|
@@ -228,8 +228,9 @@ var AbstractWebSocket = class {
|
|
|
228
228
|
const { error, message: _message, room, session } = params;
|
|
229
229
|
const message = error instanceof Error ? error.message : "Unexpected error";
|
|
230
230
|
const stack = error instanceof Error ? (_a = error.stack) != null ? _a : null : null;
|
|
231
|
+
const connectionId = (_b = session == null ? void 0 : session.id) != null ? _b : null;
|
|
231
232
|
return this.sendMessage({
|
|
232
|
-
connectionId
|
|
233
|
+
connectionId,
|
|
233
234
|
data: { message, stack },
|
|
234
235
|
room,
|
|
235
236
|
type: "$ERROR",
|
|
@@ -497,7 +498,7 @@ var IORoom = class {
|
|
|
497
498
|
}
|
|
498
499
|
const user = yield this._getAuthorizedUser(token, _options);
|
|
499
500
|
const ioAuthorize = this._getIOAuthorize(_options);
|
|
500
|
-
const isUnauthorized = !!
|
|
501
|
+
const isUnauthorized = !!ioAuthorize && !user;
|
|
501
502
|
const isTokenInUse = !!user && (yield this._getSessions().then((sessions) => sessions.some((session) => {
|
|
502
503
|
var _a2;
|
|
503
504
|
return ((_a2 = session.user) == null ? void 0 : _a2.id) === user.id;
|
|
@@ -533,6 +534,7 @@ var IORoom = class {
|
|
|
533
534
|
const sender = senderId ? (_a = this._sessions.get(senderId)) != null ? _a : null : null;
|
|
534
535
|
const session = (_b = yield sender == null ? void 0 : sender.getSession()) != null ? _b : null;
|
|
535
536
|
const user = (_c = session == null ? void 0 : session.user) != null ? _c : null;
|
|
537
|
+
if (typeof senderId !== "string") return;
|
|
536
538
|
this._platform.pubSub.publish(this.id, __spreadValues({
|
|
537
539
|
connectionId: senderId != null ? senderId : null,
|
|
538
540
|
room: this.id,
|
|
@@ -881,6 +883,7 @@ var IORoom = class {
|
|
|
881
883
|
const { data, type } = message;
|
|
882
884
|
const connectionId = (_a = sender == null ? void 0 : sender.sessionId) != null ? _a : null;
|
|
883
885
|
const room = this.id;
|
|
886
|
+
if (typeof connectionId !== "string") return;
|
|
884
887
|
const webSockets = (_b = sessionIds == null ? void 0 : sessionIds.reduce((dict, id) => {
|
|
885
888
|
const pluvWs = this._sessions.get(id);
|
|
886
889
|
return pluvWs ? dict.set(id, pluvWs) : dict;
|
|
@@ -915,6 +918,8 @@ var IORoom = class {
|
|
|
915
918
|
return __async(this, null, function* () {
|
|
916
919
|
var _a;
|
|
917
920
|
if (!sender) return;
|
|
921
|
+
const connectionId = sender.sessionId;
|
|
922
|
+
if (typeof connectionId !== "string") return;
|
|
918
923
|
const doc = yield this._doc;
|
|
919
924
|
const context = {
|
|
920
925
|
context: this._context,
|
|
@@ -937,7 +942,7 @@ var IORoom = class {
|
|
|
937
942
|
var _a2, _b;
|
|
938
943
|
const data = (_a2 = output[type]) != null ? _a2 : {};
|
|
939
944
|
this._platform.pubSub.publish(this.id, {
|
|
940
|
-
connectionId
|
|
945
|
+
connectionId,
|
|
941
946
|
data,
|
|
942
947
|
options: { type: "self" },
|
|
943
948
|
room: this.id,
|
|
@@ -951,7 +956,7 @@ var IORoom = class {
|
|
|
951
956
|
};
|
|
952
957
|
|
|
953
958
|
// src/version.generated.ts
|
|
954
|
-
var version = "0.
|
|
959
|
+
var version = "0.35.0";
|
|
955
960
|
|
|
956
961
|
// src/PluvServer.ts
|
|
957
962
|
var PluvServer = class {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pluv/io",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.35.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.9.6",
|
|
22
22
|
"kleur": "^4.1.5",
|
|
23
23
|
"wonka": "^6.3.4",
|
|
24
|
-
"@pluv/crdt": "^0.
|
|
25
|
-
"@pluv/types": "^0.
|
|
24
|
+
"@pluv/crdt": "^0.35.0",
|
|
25
|
+
"@pluv/types": "^0.35.0"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
28
|
"eslint": "^8.57.1",
|
|
29
29
|
"genversion": "^3.2.0",
|
|
30
30
|
"tsup": "^8.3.5",
|
|
31
31
|
"typescript": "^5.7.2",
|
|
32
|
-
"@pluv/tsconfig": "^0.
|
|
33
|
-
"eslint-config-pluv": "^0.
|
|
32
|
+
"@pluv/tsconfig": "^0.35.0",
|
|
33
|
+
"eslint-config-pluv": "^0.35.0"
|
|
34
34
|
},
|
|
35
35
|
"scripts": {
|
|
36
36
|
"build": "pnpm run version && tsup src/index.ts --format esm,cjs --dts",
|
package/src/AbstractWebSocket.ts
CHANGED
|
@@ -100,7 +100,7 @@ export abstract class AbstractWebSocket<TWebSocket = any> {
|
|
|
100
100
|
|
|
101
101
|
public abstract terminate(): void;
|
|
102
102
|
|
|
103
|
-
public async getSession<TAuthorize extends IOAuthorize<any, any
|
|
103
|
+
public async getSession<TAuthorize extends IOAuthorize<any, any> | null>(): Promise<WebSocketSession<TAuthorize>> {
|
|
104
104
|
const sessionId = this.sessionId;
|
|
105
105
|
const state = this.state;
|
|
106
106
|
const room = state.room;
|
|
@@ -122,12 +122,13 @@ export abstract class AbstractWebSocket<TWebSocket = any> {
|
|
|
122
122
|
|
|
123
123
|
const message = error instanceof Error ? error.message : "Unexpected error";
|
|
124
124
|
const stack: string | null = error instanceof Error ? (error.stack ?? null) : null;
|
|
125
|
+
const connectionId = (session?.id ?? null) as string;
|
|
125
126
|
|
|
126
127
|
return this.sendMessage({
|
|
127
|
-
connectionId
|
|
128
|
+
connectionId,
|
|
128
129
|
data: { message, stack },
|
|
129
130
|
room,
|
|
130
|
-
type: "$ERROR",
|
|
131
|
+
type: "$ERROR" as const,
|
|
131
132
|
user: session?.user ?? null,
|
|
132
133
|
});
|
|
133
134
|
}
|
package/src/IORoom.ts
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
import type { AbstractCrdtDoc, AbstractCrdtDocFactory } from "@pluv/crdt";
|
|
2
2
|
import { noop } from "@pluv/crdt";
|
|
3
3
|
import type {
|
|
4
|
-
BaseIOAuthorize,
|
|
5
4
|
BaseIOEventRecord,
|
|
6
5
|
EventMessage,
|
|
7
|
-
IOAuthorize,
|
|
8
6
|
IOEventMessage,
|
|
9
7
|
IOLike,
|
|
10
8
|
Id,
|
|
@@ -13,7 +11,6 @@ import type {
|
|
|
13
11
|
InferIOAuthorize,
|
|
14
12
|
InferIOAuthorizeUser,
|
|
15
13
|
InferIOInput,
|
|
16
|
-
InputZodLike,
|
|
17
14
|
JsonObject,
|
|
18
15
|
Maybe,
|
|
19
16
|
} from "@pluv/types";
|
|
@@ -24,12 +21,14 @@ import type {
|
|
|
24
21
|
InferPlatformWebSocketSource,
|
|
25
22
|
InferRoomContextType,
|
|
26
23
|
} from "./AbstractPlatform";
|
|
27
|
-
import { AbstractCloseEvent, AbstractErrorEvent, AbstractMessageEvent
|
|
24
|
+
import type { AbstractCloseEvent, AbstractErrorEvent, AbstractMessageEvent } from "./AbstractWebSocket";
|
|
25
|
+
import { AbstractWebSocket } from "./AbstractWebSocket";
|
|
28
26
|
import type { PluvRouter, PluvRouterEventConfig } from "./PluvRouter";
|
|
29
27
|
import { authorize } from "./authorize";
|
|
30
28
|
import { PING_TIMEOUT_MS } from "./constants";
|
|
31
29
|
import type {
|
|
32
30
|
EventResolverContext,
|
|
31
|
+
EventResolverKind,
|
|
33
32
|
IORoomListenerEvent,
|
|
34
33
|
IORoomMessageEvent,
|
|
35
34
|
IOUserConnectedEvent,
|
|
@@ -51,7 +50,7 @@ interface BroadcastParams<TIO extends IORoom<any, any, any, any>> {
|
|
|
51
50
|
|
|
52
51
|
export interface IORoomListeners<
|
|
53
52
|
TPlatform extends AbstractPlatform<any>,
|
|
54
|
-
TAuthorize extends PluvIOAuthorize<TPlatform, any,
|
|
53
|
+
TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null,
|
|
55
54
|
TContext extends Record<string, any>,
|
|
56
55
|
TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext>,
|
|
57
56
|
> {
|
|
@@ -70,7 +69,7 @@ export type BroadcastProxy<TIO extends IORoom<any, any, any, any>> = (<TEvent ex
|
|
|
70
69
|
|
|
71
70
|
export type IORoomConfig<
|
|
72
71
|
TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>,
|
|
73
|
-
TAuthorize extends PluvIOAuthorize<TPlatform, any,
|
|
72
|
+
TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null = any,
|
|
74
73
|
TContext extends Record<string, any> = {},
|
|
75
74
|
TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {},
|
|
76
75
|
> = Partial<IORoomListeners<TPlatform, TAuthorize, TContext, TEvents>> & {
|
|
@@ -94,7 +93,7 @@ export type WebsocketRegisterConfig<TPlatform extends AbstractPlatform<any> = Ab
|
|
|
94
93
|
|
|
95
94
|
export class IORoom<
|
|
96
95
|
TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>,
|
|
97
|
-
TAuthorize extends PluvIOAuthorize<TPlatform, any,
|
|
96
|
+
TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null = null,
|
|
98
97
|
TContext extends Record<string, any> = {},
|
|
99
98
|
TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {},
|
|
100
99
|
> implements IOLike<TAuthorize, TEvents>
|
|
@@ -104,7 +103,7 @@ export class IORoom<
|
|
|
104
103
|
private _doc: Promise<AbstractCrdtDoc<any>>;
|
|
105
104
|
private _uninitialize: Promise<() => Promise<void>> | null = null;
|
|
106
105
|
|
|
107
|
-
private readonly _authorize: TAuthorize
|
|
106
|
+
private readonly _authorize: TAuthorize = null as TAuthorize;
|
|
108
107
|
private readonly _context: TContext;
|
|
109
108
|
private readonly _debug: boolean;
|
|
110
109
|
private readonly _docFactory: AbstractCrdtDocFactory<any>;
|
|
@@ -124,6 +123,11 @@ export class IORoom<
|
|
|
124
123
|
context: this._context,
|
|
125
124
|
events: this._router._defs.events,
|
|
126
125
|
platform: this._platform,
|
|
126
|
+
} as {
|
|
127
|
+
authorize: TAuthorize;
|
|
128
|
+
context: TContext;
|
|
129
|
+
events: TEvents;
|
|
130
|
+
platform: TPlatform;
|
|
127
131
|
};
|
|
128
132
|
}
|
|
129
133
|
|
|
@@ -296,7 +300,7 @@ export class IORoom<
|
|
|
296
300
|
|
|
297
301
|
const user = await this._getAuthorizedUser(token, _options);
|
|
298
302
|
const ioAuthorize = this._getIOAuthorize(_options);
|
|
299
|
-
const isUnauthorized = !!ioAuthorize
|
|
303
|
+
const isUnauthorized = !!ioAuthorize && !user;
|
|
300
304
|
|
|
301
305
|
// There is an attempt for multiple of the same identities. Probably malicious.
|
|
302
306
|
const isTokenInUse: boolean =
|
|
@@ -347,6 +351,8 @@ export class IORoom<
|
|
|
347
351
|
const session = (await sender?.getSession()) ?? null;
|
|
348
352
|
const user = session?.user ?? null;
|
|
349
353
|
|
|
354
|
+
if (typeof senderId !== "string") return;
|
|
355
|
+
|
|
350
356
|
this._platform.pubSub.publish(this.id, {
|
|
351
357
|
connectionId: senderId ?? null,
|
|
352
358
|
room: this.id,
|
|
@@ -492,14 +498,12 @@ export class IORoom<
|
|
|
492
498
|
return doc;
|
|
493
499
|
}
|
|
494
500
|
|
|
495
|
-
private _getIOAuthorize(
|
|
496
|
-
options: WebsocketRegisterConfig<TPlatform>,
|
|
497
|
-
): ResolvedPluvIOAuthorize<any, any, any> | null {
|
|
501
|
+
private _getIOAuthorize(options: WebsocketRegisterConfig<TPlatform>): ResolvedPluvIOAuthorize<any, any> | null {
|
|
498
502
|
if (typeof this._authorize === "function") {
|
|
499
503
|
return this._authorize(options);
|
|
500
504
|
}
|
|
501
505
|
|
|
502
|
-
return this._authorize as ResolvedPluvIOAuthorize<any, any
|
|
506
|
+
return this._authorize as ResolvedPluvIOAuthorize<any, any> | null;
|
|
503
507
|
}
|
|
504
508
|
|
|
505
509
|
private _getProcedure(
|
|
@@ -646,7 +650,7 @@ export class IORoom<
|
|
|
646
650
|
]);
|
|
647
651
|
|
|
648
652
|
const doc = await this._doc;
|
|
649
|
-
const eventContext: EventResolverContext<TPlatform, TAuthorize, TContext> = {
|
|
653
|
+
const eventContext: EventResolverContext<EventResolverKind, TPlatform, TAuthorize, TContext> = {
|
|
650
654
|
context: this._context,
|
|
651
655
|
doc,
|
|
652
656
|
room: this.id,
|
|
@@ -783,6 +787,8 @@ export class IORoom<
|
|
|
783
787
|
const connectionId = sender?.sessionId ?? null;
|
|
784
788
|
const room = this.id;
|
|
785
789
|
|
|
790
|
+
if (typeof connectionId !== "string") return;
|
|
791
|
+
|
|
786
792
|
const webSockets =
|
|
787
793
|
sessionIds?.reduce((dict, id) => {
|
|
788
794
|
const pluvWs = this._sessions.get(id);
|
|
@@ -830,8 +836,12 @@ export class IORoom<
|
|
|
830
836
|
): Promise<void> {
|
|
831
837
|
if (!sender) return;
|
|
832
838
|
|
|
839
|
+
const connectionId = sender.sessionId;
|
|
840
|
+
|
|
841
|
+
if (typeof connectionId !== "string") return;
|
|
842
|
+
|
|
833
843
|
const doc = await this._doc;
|
|
834
|
-
const context: EventResolverContext<TPlatform, TAuthorize, TContext> = {
|
|
844
|
+
const context: EventResolverContext<"sync", TPlatform, TAuthorize, TContext> = {
|
|
835
845
|
context: this._context,
|
|
836
846
|
doc,
|
|
837
847
|
room: this.id,
|
|
@@ -858,7 +868,7 @@ export class IORoom<
|
|
|
858
868
|
const data = output[type] ?? {};
|
|
859
869
|
|
|
860
870
|
this._platform.pubSub.publish(this.id, {
|
|
861
|
-
connectionId
|
|
871
|
+
connectionId,
|
|
862
872
|
data,
|
|
863
873
|
options: { type: "self" },
|
|
864
874
|
room: this.id,
|