@pluv/io 0.6.0 → 0.7.1
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/.eslintrc.js +2 -2
- package/.turbo/turbo-build.log +9 -8
- package/CHANGELOG.md +61 -0
- package/dist/index.d.mts +287 -0
- package/dist/index.js +8 -6
- package/dist/index.mjs +8 -6
- package/package.json +9 -8
- package/src/AbstractPersistance.ts +4 -4
- package/src/AbstractPlatform.ts +2 -2
- package/src/AbstractPubSub.ts +3 -3
- package/src/AbstractWebSocket.ts +4 -4
- package/src/IORoom.ts +34 -34
- package/src/Persistance.ts +2 -2
- package/src/PluvIO.ts +21 -19
- package/src/PubSub.ts +3 -3
- package/src/authorize.ts +4 -4
- package/src/createIO.ts +3 -3
- package/src/types.ts +8 -8
package/.eslintrc.js
CHANGED
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
|
|
2
|
-
> @pluv/io@0.
|
|
2
|
+
> @pluv/io@0.7.1 build /home/runner/work/pluv/pluv/packages/io
|
|
3
3
|
> tsup src/index.ts --format esm,cjs --dts
|
|
4
4
|
|
|
5
5
|
[34mCLI[39m Building entry: src/index.ts
|
|
6
6
|
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
7
|
-
[34mCLI[39m tsup
|
|
7
|
+
[34mCLI[39m tsup v7.1.0
|
|
8
8
|
[34mCLI[39m Target: es6
|
|
9
9
|
[34mESM[39m Build start
|
|
10
10
|
[34mCJS[39m Build start
|
|
11
|
-
[
|
|
12
|
-
[
|
|
13
|
-
[
|
|
14
|
-
[
|
|
11
|
+
[32mCJS[39m [1mdist/index.js [22m[32m31.08 KB[39m
|
|
12
|
+
[32mCJS[39m ⚡️ Build success in 133ms
|
|
13
|
+
[32mESM[39m [1mdist/index.mjs [22m[32m29.02 KB[39m
|
|
14
|
+
[32mESM[39m ⚡️ Build success in 134ms
|
|
15
15
|
[34mDTS[39m Build start
|
|
16
|
-
[32mDTS[39m ⚡️ Build success in
|
|
17
|
-
[32mDTS[39m [1mdist/index.d.
|
|
16
|
+
[32mDTS[39m ⚡️ Build success in 4386ms
|
|
17
|
+
[32mDTS[39m [1mdist/index.d.mts [22m[32m17.03 KB[39m
|
|
18
|
+
[32mDTS[39m [1mdist/index.d.ts [22m[32m17.03 KB[39m
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,66 @@
|
|
|
1
1
|
# @pluv/io
|
|
2
2
|
|
|
3
|
+
## 0.7.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 8fba48b: fix including incorrect dependency
|
|
8
|
+
- 8997c65: bumped dependencies
|
|
9
|
+
- Updated dependencies [8fba48b]
|
|
10
|
+
- Updated dependencies [8997c65]
|
|
11
|
+
- @pluv/crdt-yjs@0.4.1
|
|
12
|
+
- @pluv/types@0.2.2
|
|
13
|
+
|
|
14
|
+
## 0.7.0
|
|
15
|
+
|
|
16
|
+
### Minor Changes
|
|
17
|
+
|
|
18
|
+
- 829d31b: Added support for defining persistant frontend storage for rooms via a new `addons` option on rooms.
|
|
19
|
+
|
|
20
|
+
This also introduces the first new addon `@pluv/addon-indexeddb`, which is more-or-less the equivalent to `y-indexeddb` which you can install like so:
|
|
21
|
+
|
|
22
|
+
```
|
|
23
|
+
npm install @pluv/addon-indexeddb
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
To use this new addon, simply pass it to options when creating a room:
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
import { addonIndexedDB } from "@pluv/addon-indexeddb";
|
|
30
|
+
import { createClient } from "@pluv/client";
|
|
31
|
+
|
|
32
|
+
const client = createClient({
|
|
33
|
+
// ...
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const room = client.createRoom("my-new-room", {
|
|
37
|
+
addons: [
|
|
38
|
+
// Define your addons in an array like so
|
|
39
|
+
addonIndexedDB(),
|
|
40
|
+
],
|
|
41
|
+
});
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Or when using `@pluv/react`:
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
const PluvRoom = createRoomBundle({
|
|
48
|
+
// ...
|
|
49
|
+
addons: [
|
|
50
|
+
// Define your addons in an array like so
|
|
51
|
+
addonIndexedDB(),
|
|
52
|
+
],
|
|
53
|
+
});
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Patch Changes
|
|
57
|
+
|
|
58
|
+
- 8d11672: bumped dependencies to latest
|
|
59
|
+
- Updated dependencies [8d11672]
|
|
60
|
+
- Updated dependencies [829d31b]
|
|
61
|
+
- @pluv/crdt-yjs@0.4.0
|
|
62
|
+
- @pluv/types@0.2.1
|
|
63
|
+
|
|
3
64
|
## 0.6.0
|
|
4
65
|
|
|
5
66
|
### Minor Changes
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
import { JsonObject, MaybePromise, IOEventMessage, EventRecord, InputZodLike, Id, IOLike, InferIOOutput, IOAuthorize, BaseIOAuthorize, InferIOInput, BaseUser, InferIOAuthorizeUser, InferIOAuthorize, Spread, InferIOOutputBroadcast, InferIOOutputSelf, InferIOOutputSync, Maybe, BaseClientEventRecord, BaseIOEventRecord } from '@pluv/types';
|
|
2
|
+
export { BaseUser, EventMessage, EventRecord, IOEventMessage } from '@pluv/types';
|
|
3
|
+
import { YjsDoc } from '@pluv/crdt-yjs';
|
|
4
|
+
|
|
5
|
+
declare abstract class AbstractPersistance {
|
|
6
|
+
abstract addUser(room: string, connectionId: string, user: JsonObject | null): Promise<void>;
|
|
7
|
+
abstract deleteStorageState(room: string): Promise<void>;
|
|
8
|
+
abstract deleteUser(room: string, connectionId: string): Promise<void>;
|
|
9
|
+
abstract deleteUsers(room: string): Promise<void>;
|
|
10
|
+
abstract getSize(room: string): Promise<number>;
|
|
11
|
+
abstract getStorageState(room: string): Promise<string | null>;
|
|
12
|
+
abstract getUser(room: string, connectionId: string): Promise<JsonObject | null>;
|
|
13
|
+
abstract getUsers(room: string): Promise<readonly (JsonObject | null)[]>;
|
|
14
|
+
abstract setStorageState(room: string, state: string): Promise<void>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
type AbstractEvent = {};
|
|
18
|
+
type AbstractCloseEvent = AbstractEvent & {
|
|
19
|
+
readonly code: number;
|
|
20
|
+
readonly reason: string;
|
|
21
|
+
};
|
|
22
|
+
type AbstractMessageEvent<T = any> = AbstractEvent & {
|
|
23
|
+
readonly data: T;
|
|
24
|
+
};
|
|
25
|
+
type AbstractErrorEvent = AbstractEvent & {
|
|
26
|
+
error: unknown;
|
|
27
|
+
message: string;
|
|
28
|
+
};
|
|
29
|
+
interface AbstractEventMap {
|
|
30
|
+
close: AbstractCloseEvent;
|
|
31
|
+
message: AbstractMessageEvent;
|
|
32
|
+
error: AbstractErrorEvent;
|
|
33
|
+
}
|
|
34
|
+
type EventType = keyof AbstractEventMap;
|
|
35
|
+
interface AbstractWebSocketHandleErrorParams {
|
|
36
|
+
error: unknown;
|
|
37
|
+
message?: string;
|
|
38
|
+
session?: WebSocketSession;
|
|
39
|
+
}
|
|
40
|
+
type AbstractListener<TType extends keyof AbstractEventMap> = (event: AbstractEventMap[TType]) => MaybePromise<void>;
|
|
41
|
+
interface AbstractWebSocketConfig {
|
|
42
|
+
pubSub?: AbstractPubSub;
|
|
43
|
+
room: string;
|
|
44
|
+
}
|
|
45
|
+
declare abstract class AbstractWebSocket {
|
|
46
|
+
/** The connection is not yet open. */
|
|
47
|
+
readonly CONNECTING = 0;
|
|
48
|
+
/** The connection is open and ready to communicate. */
|
|
49
|
+
readonly OPEN = 1;
|
|
50
|
+
/** The connection is in the process of closing. */
|
|
51
|
+
readonly CLOSING = 2;
|
|
52
|
+
/** The connection is closed. */
|
|
53
|
+
readonly CLOSED = 3;
|
|
54
|
+
pubSub?: AbstractPubSub;
|
|
55
|
+
room: string;
|
|
56
|
+
abstract get readyState(): 0 | 1 | 2 | 3;
|
|
57
|
+
constructor(config: AbstractWebSocketConfig);
|
|
58
|
+
/**
|
|
59
|
+
* TODO
|
|
60
|
+
* @description Return an unsubscribe method to remove the event-listener.
|
|
61
|
+
* This is preferred over adding an explicit abstract removeEventListener
|
|
62
|
+
* method, because the implementing class might have to transform the
|
|
63
|
+
* listener, and removing the same listener might be complicated as a
|
|
64
|
+
* result.
|
|
65
|
+
* @author leedavidcs
|
|
66
|
+
* @date August 28, 2022
|
|
67
|
+
*/
|
|
68
|
+
abstract addEventListener<TType extends EventType>(type: TType, handler: AbstractListener<TType>): void;
|
|
69
|
+
abstract close(code?: number | undefined, reason?: string | undefined): void;
|
|
70
|
+
abstract initialize(): Promise<() => void>;
|
|
71
|
+
abstract send(message: ArrayBuffer | ArrayBufferView | string): void;
|
|
72
|
+
abstract terminate(): void;
|
|
73
|
+
handleError(params: AbstractWebSocketHandleErrorParams): void;
|
|
74
|
+
sendMessage<TMessage extends IOEventMessage<any> = IOEventMessage<any>>(data: TMessage): void;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
declare global {
|
|
78
|
+
var console: {
|
|
79
|
+
log: (...data: any[]) => void;
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
interface EventConfig<TPlatform extends AbstractPlatform = AbstractPlatform, TContext extends Record<string, any> = {}, TData extends JsonObject = {}, TResultBroadcast extends EventRecord<string, any> = {}, TResultSelf extends EventRecord<string, any> = {}, TResultSync extends EventRecord<string, any> = {}> {
|
|
83
|
+
input?: InputZodLike<TData>;
|
|
84
|
+
resolver: EventResolver<TContext & InferPlatformRoomContextType<TPlatform> & InferPlatformEventContextType<TPlatform>, TData, TResultBroadcast> | EventResolverObject<TPlatform, TContext, TData, TResultBroadcast, TResultSelf, TResultSync>;
|
|
85
|
+
}
|
|
86
|
+
type SyncEventResolver<TContext extends Record<string, any> = {}, TData extends JsonObject = {}, TResultBroadcast extends EventRecord<string, any> = {}> = (data: TData, context: EventResolverContext<TContext>) => MaybePromise<TResultBroadcast | void>;
|
|
87
|
+
type EventResolver<TContext extends Record<string, any> = {}, TData extends JsonObject = {}, TResultBroadcast extends EventRecord<string, any> = {}> = (data: TData, context: EventResolverContext<TContext>) => MaybePromise<TResultBroadcast | void>;
|
|
88
|
+
interface EventResolverContext<TContext extends Record<string, any> = {}> {
|
|
89
|
+
context: TContext;
|
|
90
|
+
doc: YjsDoc<any>;
|
|
91
|
+
room: string;
|
|
92
|
+
/**
|
|
93
|
+
* !HACK
|
|
94
|
+
* @description Session should only not exist when invoking the event as a
|
|
95
|
+
* "sync" event on another machine where the session doesn't exist. There must
|
|
96
|
+
* be a better way to handle this, but going to make this nullable for ease
|
|
97
|
+
* for now.
|
|
98
|
+
*
|
|
99
|
+
* `session` should not be referenced most of the time anyhow (outside of
|
|
100
|
+
* internal to this library).
|
|
101
|
+
* @author leedavidcs
|
|
102
|
+
* @date December 25, 2022
|
|
103
|
+
*/
|
|
104
|
+
session: WebSocketSession | null;
|
|
105
|
+
sessions: Map<string, WebSocketSession>;
|
|
106
|
+
}
|
|
107
|
+
type EventResolverObject<TPlatform extends AbstractPlatform = AbstractPlatform, TContext extends Record<string, any> = {}, TData extends JsonObject = {}, TResultBroadcast extends EventRecord<string, any> = {}, TResultSelf extends EventRecord<string, any> = {}, TResultSync extends EventRecord<string, any> = {}> = {
|
|
108
|
+
broadcast?: EventResolver<TContext & InferPlatformRoomContextType<TPlatform> & InferPlatformEventContextType<TPlatform>, TData, TResultBroadcast>;
|
|
109
|
+
self?: EventResolver<TContext & InferPlatformRoomContextType<TPlatform> & InferPlatformEventContextType<TPlatform>, TData, TResultSelf>;
|
|
110
|
+
sync?: SyncEventResolver<TContext, TData, TResultSync>;
|
|
111
|
+
};
|
|
112
|
+
type InferEventConfig<TPlatform extends AbstractPlatform = AbstractPlatform, 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> = {}> = {
|
|
113
|
+
[P in keyof TInput]: P extends string ? Id<EventConfig<TPlatform, TContext, TInput[P], TOutputBroadcast, TOutputSelf, TOutputSync>> : never;
|
|
114
|
+
};
|
|
115
|
+
type SendMessageOptions = {
|
|
116
|
+
type?: "broadcast";
|
|
117
|
+
sessionIds?: readonly string[];
|
|
118
|
+
} | {
|
|
119
|
+
type: "self";
|
|
120
|
+
} | {
|
|
121
|
+
type: "sync";
|
|
122
|
+
};
|
|
123
|
+
interface WebSocketSessionTimers {
|
|
124
|
+
ping: number;
|
|
125
|
+
}
|
|
126
|
+
interface WebSocketSession {
|
|
127
|
+
id: string;
|
|
128
|
+
presence: JsonObject | null;
|
|
129
|
+
quit: boolean;
|
|
130
|
+
room: string;
|
|
131
|
+
timers: WebSocketSessionTimers;
|
|
132
|
+
webSocket: AbstractWebSocket;
|
|
133
|
+
user: JsonObject | null;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
type IOPubSubEventMessage<TIO extends IOLike, TEvent extends keyof InferIOOutput<TIO> = keyof InferIOOutput<TIO>> = IOEventMessage<TIO, TEvent> & {
|
|
137
|
+
options?: SendMessageOptions;
|
|
138
|
+
};
|
|
139
|
+
declare abstract class AbstractPubSub {
|
|
140
|
+
abstract publish(roomName: string, payload: IOPubSubEventMessage<any>): Promise<void>;
|
|
141
|
+
abstract subscribe(roomName: string, onMessage: (message: IOPubSubEventMessage<any>) => void): Promise<number>;
|
|
142
|
+
abstract unsubscribe(subscriptionId: number): void;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
type InferPlatformWebSocketType<TPlatform extends AbstractPlatform> = TPlatform extends AbstractPlatform<infer IWebSocket> ? IWebSocket : never;
|
|
146
|
+
type InferPlatformRoomContextType<TPlatform extends AbstractPlatform> = TPlatform extends AbstractPlatform<any, infer IIOContext> ? IIOContext : never;
|
|
147
|
+
type InferPlatformEventContextType<TPlatform extends AbstractPlatform> = TPlatform extends AbstractPlatform<any, any, infer IRoomContext> ? IRoomContext : never;
|
|
148
|
+
type AbstractPlatformConfig = {
|
|
149
|
+
persistance?: undefined;
|
|
150
|
+
pubSub?: undefined;
|
|
151
|
+
} | {
|
|
152
|
+
persistance: AbstractPersistance;
|
|
153
|
+
pubSub: AbstractPubSub;
|
|
154
|
+
};
|
|
155
|
+
interface ConvertWebSocketConfig {
|
|
156
|
+
room: string;
|
|
157
|
+
}
|
|
158
|
+
declare abstract class AbstractPlatform<TWebSocket = any, TIOContext extends Record<string, any> = {}, TRoomContext extends Record<string, any> = {}> {
|
|
159
|
+
readonly _ioContext: TIOContext | undefined;
|
|
160
|
+
readonly _roomContext: TRoomContext | undefined;
|
|
161
|
+
persistance: AbstractPersistance;
|
|
162
|
+
pubSub: AbstractPubSub;
|
|
163
|
+
constructor(config?: AbstractPlatformConfig);
|
|
164
|
+
abstract convertWebSocket(webSocket: TWebSocket, config: ConvertWebSocketConfig): AbstractWebSocket;
|
|
165
|
+
abstract parseData(data: string | ArrayBuffer): Record<string, any>;
|
|
166
|
+
abstract randomUUID(): string;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
type IORoomListenerEvent<TPlatform extends AbstractPlatform> = {
|
|
170
|
+
room: string;
|
|
171
|
+
encodedState: string;
|
|
172
|
+
} & InferPlatformRoomContextType<TPlatform>;
|
|
173
|
+
interface IORoomListeners<TPlatform extends AbstractPlatform> {
|
|
174
|
+
onDestroy: (event: IORoomListenerEvent<TPlatform>) => void;
|
|
175
|
+
}
|
|
176
|
+
type IORoomConfig<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends IOAuthorize<any, any> = 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>> & {
|
|
177
|
+
authorize?: TAuthorize;
|
|
178
|
+
context: TContext & InferPlatformRoomContextType<TPlatform>;
|
|
179
|
+
debug: boolean;
|
|
180
|
+
events: InferEventConfig<TPlatform, TContext, TInput, TOutputBroadcast, TOutputSelf, TOutputSync>;
|
|
181
|
+
platform: TPlatform;
|
|
182
|
+
};
|
|
183
|
+
type WebsocketRegisterOptions<TPlatform extends AbstractPlatform> = {
|
|
184
|
+
token?: string | null;
|
|
185
|
+
} & InferPlatformRoomContextType<TPlatform> & InferPlatformEventContextType<TPlatform>;
|
|
186
|
+
declare class IORoom<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends IOAuthorize<any, any> = 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> {
|
|
187
|
+
private readonly _context;
|
|
188
|
+
private readonly _debug;
|
|
189
|
+
private readonly _platform;
|
|
190
|
+
private _doc;
|
|
191
|
+
private _listeners;
|
|
192
|
+
private _sessions;
|
|
193
|
+
private _uninitialize;
|
|
194
|
+
readonly _authorize: TAuthorize | null;
|
|
195
|
+
readonly _events: InferEventConfig<TPlatform, TContext, TInput, TOutputBroadcast, TOutputSelf, TOutputSync>;
|
|
196
|
+
readonly id: string;
|
|
197
|
+
constructor(id: string, config: IORoomConfig<TPlatform, TAuthorize, TContext, TInput, TOutputBroadcast, TOutputSelf, TOutputSync>);
|
|
198
|
+
broadcast<TEvent extends keyof InferIOInput<this>>(event: TEvent, data: Id<InferIOInput<this>[TEvent]>): Promise<void>;
|
|
199
|
+
getSize(): number;
|
|
200
|
+
register(webSocket: InferPlatformWebSocketType<TPlatform>, options: WebsocketRegisterOptions<TPlatform>): Promise<void>;
|
|
201
|
+
private _broadcast;
|
|
202
|
+
private _emitQuitters;
|
|
203
|
+
private _emitRegistered;
|
|
204
|
+
private _getAuthorizedUser;
|
|
205
|
+
private _getEventConfig;
|
|
206
|
+
private _getEventInputs;
|
|
207
|
+
private _getEventResolverObject;
|
|
208
|
+
private _initialize;
|
|
209
|
+
private _logDebug;
|
|
210
|
+
private _onClose;
|
|
211
|
+
private _onMessage;
|
|
212
|
+
private _parseMessage;
|
|
213
|
+
private _sendMessage;
|
|
214
|
+
private _sendBroadcastMessage;
|
|
215
|
+
private _sendSelfMessage;
|
|
216
|
+
private _sendSyncMessage;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
interface JWT<TUser extends BaseUser> {
|
|
220
|
+
room: string;
|
|
221
|
+
sub: string;
|
|
222
|
+
user: TUser;
|
|
223
|
+
}
|
|
224
|
+
interface JWTEncodeParams<TUser extends BaseUser> {
|
|
225
|
+
maxAge?: number;
|
|
226
|
+
room: string;
|
|
227
|
+
user: TUser;
|
|
228
|
+
}
|
|
229
|
+
interface AuthorizeParams {
|
|
230
|
+
platform: AbstractPlatform;
|
|
231
|
+
secret: string;
|
|
232
|
+
}
|
|
233
|
+
interface AuthorizeModule {
|
|
234
|
+
decode: <TUser extends BaseUser>(jwt: string) => Promise<TUser | null>;
|
|
235
|
+
encode: <TUser extends BaseUser>(params: JWTEncodeParams<TUser>) => Promise<string>;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
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;
|
|
239
|
+
type InferIOPlatform<TIO extends PluvIO<any, any, any, any, any, any, any>> = TIO extends PluvIO<infer IPlatform, any, any, any, any, any, any> ? IPlatform : never;
|
|
240
|
+
type InferIORoom<TIO extends PluvIO<any, any, any, any, any, any, any>> = TIO extends PluvIO<infer IPlatform, infer IAuthorize, infer IContext, infer IInput, infer IOutputBroadcast, infer IOutputSelf, infer IOutputSync> ? IORoom<IPlatform, IAuthorize, IContext, IInput, IOutputBroadcast, IOutputSelf, IOutputSync> : never;
|
|
241
|
+
type GetInitialStorageEvent<TPlatform extends AbstractPlatform> = {
|
|
242
|
+
room: string;
|
|
243
|
+
} & InferPlatformRoomContextType<TPlatform>;
|
|
244
|
+
type GetInitialStorageFn<TPlatform extends AbstractPlatform> = (event: GetInitialStorageEvent<TPlatform>) => MaybePromise<Maybe<string>>;
|
|
245
|
+
interface PluvIOListeners<TPlatform extends AbstractPlatform> {
|
|
246
|
+
onRoomDeleted: (event: IORoomListenerEvent<TPlatform>) => void;
|
|
247
|
+
onStorageUpdated: (event: IORoomListenerEvent<TPlatform>) => void;
|
|
248
|
+
}
|
|
249
|
+
type PluvIOConfig<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends IOAuthorize<any, any> | 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>> & {
|
|
250
|
+
authorize?: TAuthorize;
|
|
251
|
+
context?: TContext;
|
|
252
|
+
debug?: boolean;
|
|
253
|
+
events?: InferEventConfig<TPlatform, TContext, TInput, TOutputBroadcast, TOutputSelf, TOutputSync>;
|
|
254
|
+
getInitialStorage?: GetInitialStorageFn<TPlatform>;
|
|
255
|
+
platform: TPlatform;
|
|
256
|
+
};
|
|
257
|
+
type GetRoomOptions<TPlatform extends AbstractPlatform> = {
|
|
258
|
+
debug?: boolean;
|
|
259
|
+
} & InferPlatformRoomContextType<TPlatform>;
|
|
260
|
+
declare class PluvIO<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends IOAuthorize<any, any> = 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> {
|
|
261
|
+
private _context;
|
|
262
|
+
private _getInitialStorage;
|
|
263
|
+
private _rooms;
|
|
264
|
+
readonly _authorize: TAuthorize | null;
|
|
265
|
+
readonly _debug: boolean;
|
|
266
|
+
readonly _events: InferEventConfig<TPlatform, TContext, TInput, TOutputBroadcast, TOutputSelf, TOutputSync>;
|
|
267
|
+
readonly _listeners: PluvIOListeners<TPlatform>;
|
|
268
|
+
readonly _platform: TPlatform;
|
|
269
|
+
constructor(options: PluvIOConfig<TPlatform, TAuthorize, TContext, TInput, TOutputBroadcast, TOutputSelf, TOutputSync>);
|
|
270
|
+
createToken(params: JWTEncodeParams<InferIOAuthorizeUser<InferIOAuthorize<this>>>): Promise<string>;
|
|
271
|
+
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]>>;
|
|
272
|
+
getRoom(room: string, options: GetRoomOptions<TPlatform>): IORoom<TPlatform, TAuthorize, TContext, TInput, TOutputBroadcast, TOutputSelf, TOutputSync>;
|
|
273
|
+
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>]>>;
|
|
274
|
+
private _logDebug;
|
|
275
|
+
private _purgeEmptyRooms;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
type CreateIOParams<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TContext extends JsonObject = {}, TAuthorizeUser extends BaseUser = BaseUser, TAuthorizeRequired extends boolean = false> = Partial<PluvIOListeners<TPlatform>> & {
|
|
279
|
+
authorize?: IOAuthorize<TAuthorizeUser, TAuthorizeRequired>;
|
|
280
|
+
context?: TContext;
|
|
281
|
+
debug?: boolean;
|
|
282
|
+
getInitialStorage?: GetInitialStorageFn<TPlatform>;
|
|
283
|
+
platform: TPlatform;
|
|
284
|
+
};
|
|
285
|
+
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>, TContext, BaseClientEventRecord, BaseIOEventRecord<IOAuthorize<TAuthorizeUser, TAuthorizeRequired>>, {}, {}>;
|
|
286
|
+
|
|
287
|
+
export { AbstractCloseEvent, AbstractErrorEvent, AbstractEvent, AbstractEventMap, AbstractListener, AbstractMessageEvent, AbstractPersistance, AbstractPlatform, AbstractPubSub, AbstractWebSocket, AbstractWebSocketConfig, AbstractWebSocketHandleErrorParams, AuthorizeModule, AuthorizeParams, ConvertWebSocketConfig, CreateIOParams, GetRoomOptions, IOPubSubEventMessage, IORoom, InferIORoom, InferPlatformEventContextType, InferPlatformRoomContextType, InferPlatformWebSocketType, JWT, JWTEncodeParams, PluvIO, PluvIOConfig, createIO };
|
package/dist/index.js
CHANGED
|
@@ -727,7 +727,7 @@ var IORoom = class {
|
|
|
727
727
|
};
|
|
728
728
|
|
|
729
729
|
// src/PluvIO.ts
|
|
730
|
-
var PluvIO = class {
|
|
730
|
+
var PluvIO = class _PluvIO {
|
|
731
731
|
constructor(options) {
|
|
732
732
|
this._context = {};
|
|
733
733
|
this._getInitialStorage = null;
|
|
@@ -818,7 +818,7 @@ var PluvIO = class {
|
|
|
818
818
|
},
|
|
819
819
|
$UPDATE_STORAGE: {
|
|
820
820
|
resolver: ({ origin, update }, { context, doc: doc2, room }) => {
|
|
821
|
-
if (origin === "$
|
|
821
|
+
if (origin === "$INITIALIZED" && Object.keys(doc2.toJSON()).length) {
|
|
822
822
|
return;
|
|
823
823
|
}
|
|
824
824
|
const updated = doc2.applyUpdate(update);
|
|
@@ -878,9 +878,9 @@ var PluvIO = class {
|
|
|
878
878
|
const newEvent = {
|
|
879
879
|
[event]: config
|
|
880
880
|
};
|
|
881
|
-
return
|
|
881
|
+
return _PluvIO.merge(
|
|
882
882
|
this,
|
|
883
|
-
new
|
|
883
|
+
new _PluvIO({
|
|
884
884
|
events: newEvent,
|
|
885
885
|
platform: this._platform
|
|
886
886
|
})
|
|
@@ -917,7 +917,9 @@ var PluvIO = class {
|
|
|
917
917
|
const rooms = Array.from(this._rooms.keys());
|
|
918
918
|
this._logDebug(`${import_kleur2.default.blue("Deleted room:")} ${room2}`);
|
|
919
919
|
this._logDebug(
|
|
920
|
-
`${import_kleur2.default.blue("Rooms available:")} ${rooms.join(
|
|
920
|
+
`${import_kleur2.default.blue("Rooms available:")} ${rooms.join(
|
|
921
|
+
", "
|
|
922
|
+
)}`
|
|
921
923
|
);
|
|
922
924
|
}
|
|
923
925
|
},
|
|
@@ -942,7 +944,7 @@ var PluvIO = class {
|
|
|
942
944
|
const events2 = second._events;
|
|
943
945
|
const { onRoomDeleted, onStorageUpdated } = first._listeners;
|
|
944
946
|
const events = Object.assign(/* @__PURE__ */ Object.create(null), events1, events2);
|
|
945
|
-
return new
|
|
947
|
+
return new _PluvIO({
|
|
946
948
|
authorize: authorize2,
|
|
947
949
|
debug,
|
|
948
950
|
events,
|
package/dist/index.mjs
CHANGED
|
@@ -690,7 +690,7 @@ var IORoom = class {
|
|
|
690
690
|
};
|
|
691
691
|
|
|
692
692
|
// src/PluvIO.ts
|
|
693
|
-
var PluvIO = class {
|
|
693
|
+
var PluvIO = class _PluvIO {
|
|
694
694
|
constructor(options) {
|
|
695
695
|
this._context = {};
|
|
696
696
|
this._getInitialStorage = null;
|
|
@@ -781,7 +781,7 @@ var PluvIO = class {
|
|
|
781
781
|
},
|
|
782
782
|
$UPDATE_STORAGE: {
|
|
783
783
|
resolver: ({ origin, update }, { context, doc: doc2, room }) => {
|
|
784
|
-
if (origin === "$
|
|
784
|
+
if (origin === "$INITIALIZED" && Object.keys(doc2.toJSON()).length) {
|
|
785
785
|
return;
|
|
786
786
|
}
|
|
787
787
|
const updated = doc2.applyUpdate(update);
|
|
@@ -841,9 +841,9 @@ var PluvIO = class {
|
|
|
841
841
|
const newEvent = {
|
|
842
842
|
[event]: config
|
|
843
843
|
};
|
|
844
|
-
return
|
|
844
|
+
return _PluvIO.merge(
|
|
845
845
|
this,
|
|
846
|
-
new
|
|
846
|
+
new _PluvIO({
|
|
847
847
|
events: newEvent,
|
|
848
848
|
platform: this._platform
|
|
849
849
|
})
|
|
@@ -880,7 +880,9 @@ var PluvIO = class {
|
|
|
880
880
|
const rooms = Array.from(this._rooms.keys());
|
|
881
881
|
this._logDebug(`${colors2.blue("Deleted room:")} ${room2}`);
|
|
882
882
|
this._logDebug(
|
|
883
|
-
`${colors2.blue("Rooms available:")} ${rooms.join(
|
|
883
|
+
`${colors2.blue("Rooms available:")} ${rooms.join(
|
|
884
|
+
", "
|
|
885
|
+
)}`
|
|
884
886
|
);
|
|
885
887
|
}
|
|
886
888
|
},
|
|
@@ -905,7 +907,7 @@ var PluvIO = class {
|
|
|
905
907
|
const events2 = second._events;
|
|
906
908
|
const { onRoomDeleted, onStorageUpdated } = first._listeners;
|
|
907
909
|
const events = Object.assign(/* @__PURE__ */ Object.create(null), events1, events2);
|
|
908
|
-
return new
|
|
910
|
+
return new _PluvIO({
|
|
909
911
|
authorize: authorize2,
|
|
910
912
|
debug,
|
|
911
913
|
events,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pluv/io",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.1",
|
|
4
4
|
"description": "@pluv/io server-side websocket manager",
|
|
5
5
|
"author": "leedavidcs",
|
|
6
6
|
"license": "MIT",
|
|
@@ -18,21 +18,22 @@
|
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
20
|
"@panva/hkdf": "^1.1.1",
|
|
21
|
+
"@types/react": "^18.2.15",
|
|
21
22
|
"jose": "^4.14.4",
|
|
22
23
|
"kleur": "^4.1.5",
|
|
23
24
|
"wonka": "^6.3.2",
|
|
24
|
-
"@pluv/crdt-yjs": "^0.
|
|
25
|
-
"@pluv/types": "^0.2.
|
|
25
|
+
"@pluv/crdt-yjs": "^0.4.1",
|
|
26
|
+
"@pluv/types": "^0.2.2"
|
|
26
27
|
},
|
|
27
28
|
"peerDependencies": {
|
|
28
29
|
"yjs": "^13.0.0"
|
|
29
30
|
},
|
|
30
31
|
"devDependencies": {
|
|
31
|
-
"tsup": "^
|
|
32
|
-
"typescript": "^5.
|
|
33
|
-
"yjs": "^13.6.
|
|
34
|
-
"@pluv/tsconfig": "^0.1.
|
|
35
|
-
"eslint-config-pluv": "^0.1.
|
|
32
|
+
"tsup": "^7.1.0",
|
|
33
|
+
"typescript": "^5.1.6",
|
|
34
|
+
"yjs": "^13.6.6",
|
|
35
|
+
"@pluv/tsconfig": "^0.1.8",
|
|
36
|
+
"eslint-config-pluv": "^0.1.14"
|
|
36
37
|
},
|
|
37
38
|
"scripts": {
|
|
38
39
|
"build": "tsup src/index.ts --format esm,cjs --dts",
|
|
@@ -4,14 +4,14 @@ export abstract class AbstractPersistance {
|
|
|
4
4
|
public abstract addUser(
|
|
5
5
|
room: string,
|
|
6
6
|
connectionId: string,
|
|
7
|
-
user: JsonObject | null
|
|
7
|
+
user: JsonObject | null,
|
|
8
8
|
): Promise<void>;
|
|
9
9
|
|
|
10
10
|
public abstract deleteStorageState(room: string): Promise<void>;
|
|
11
11
|
|
|
12
12
|
public abstract deleteUser(
|
|
13
13
|
room: string,
|
|
14
|
-
connectionId: string
|
|
14
|
+
connectionId: string,
|
|
15
15
|
): Promise<void>;
|
|
16
16
|
|
|
17
17
|
public abstract deleteUsers(room: string): Promise<void>;
|
|
@@ -22,11 +22,11 @@ export abstract class AbstractPersistance {
|
|
|
22
22
|
|
|
23
23
|
public abstract getUser(
|
|
24
24
|
room: string,
|
|
25
|
-
connectionId: string
|
|
25
|
+
connectionId: string,
|
|
26
26
|
): Promise<JsonObject | null>;
|
|
27
27
|
|
|
28
28
|
public abstract getUsers(
|
|
29
|
-
room: string
|
|
29
|
+
room: string,
|
|
30
30
|
): Promise<readonly (JsonObject | null)[]>;
|
|
31
31
|
|
|
32
32
|
public abstract setStorageState(room: string, state: string): Promise<void>;
|
package/src/AbstractPlatform.ts
CHANGED
|
@@ -31,7 +31,7 @@ export interface ConvertWebSocketConfig {
|
|
|
31
31
|
export abstract class AbstractPlatform<
|
|
32
32
|
TWebSocket = any,
|
|
33
33
|
TIOContext extends Record<string, any> = {},
|
|
34
|
-
TRoomContext extends Record<string, any> = {}
|
|
34
|
+
TRoomContext extends Record<string, any> = {},
|
|
35
35
|
> {
|
|
36
36
|
readonly _ioContext: TIOContext | undefined;
|
|
37
37
|
readonly _roomContext: TRoomContext | undefined;
|
|
@@ -48,7 +48,7 @@ export abstract class AbstractPlatform<
|
|
|
48
48
|
|
|
49
49
|
public abstract convertWebSocket(
|
|
50
50
|
webSocket: TWebSocket,
|
|
51
|
-
config: ConvertWebSocketConfig
|
|
51
|
+
config: ConvertWebSocketConfig,
|
|
52
52
|
): AbstractWebSocket;
|
|
53
53
|
|
|
54
54
|
public abstract parseData(data: string | ArrayBuffer): Record<string, any>;
|
package/src/AbstractPubSub.ts
CHANGED
|
@@ -3,7 +3,7 @@ import type { SendMessageOptions } from "./types";
|
|
|
3
3
|
|
|
4
4
|
export type IOPubSubEventMessage<
|
|
5
5
|
TIO extends IOLike,
|
|
6
|
-
TEvent extends keyof InferIOOutput<TIO> = keyof InferIOOutput<TIO
|
|
6
|
+
TEvent extends keyof InferIOOutput<TIO> = keyof InferIOOutput<TIO>,
|
|
7
7
|
> = IOEventMessage<TIO, TEvent> & {
|
|
8
8
|
options?: SendMessageOptions;
|
|
9
9
|
};
|
|
@@ -11,12 +11,12 @@ export type IOPubSubEventMessage<
|
|
|
11
11
|
export abstract class AbstractPubSub {
|
|
12
12
|
public abstract publish(
|
|
13
13
|
roomName: string,
|
|
14
|
-
payload: IOPubSubEventMessage<any
|
|
14
|
+
payload: IOPubSubEventMessage<any>,
|
|
15
15
|
): Promise<void>;
|
|
16
16
|
|
|
17
17
|
public abstract subscribe(
|
|
18
18
|
roomName: string,
|
|
19
|
-
onMessage: (message: IOPubSubEventMessage<any>) => void
|
|
19
|
+
onMessage: (message: IOPubSubEventMessage<any>) => void,
|
|
20
20
|
): Promise<number>;
|
|
21
21
|
|
|
22
22
|
public abstract unsubscribe(subscriptionId: number): void;
|
package/src/AbstractWebSocket.ts
CHANGED
|
@@ -33,7 +33,7 @@ export interface AbstractWebSocketHandleErrorParams {
|
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
export type AbstractListener<TType extends keyof AbstractEventMap> = (
|
|
36
|
-
event: AbstractEventMap[TType]
|
|
36
|
+
event: AbstractEventMap[TType],
|
|
37
37
|
) => MaybePromise<void>;
|
|
38
38
|
|
|
39
39
|
export interface AbstractWebSocketConfig {
|
|
@@ -74,12 +74,12 @@ export abstract class AbstractWebSocket {
|
|
|
74
74
|
*/
|
|
75
75
|
public abstract addEventListener<TType extends EventType>(
|
|
76
76
|
type: TType,
|
|
77
|
-
handler: AbstractListener<TType
|
|
77
|
+
handler: AbstractListener<TType>,
|
|
78
78
|
): void;
|
|
79
79
|
|
|
80
80
|
public abstract close(
|
|
81
81
|
code?: number | undefined,
|
|
82
|
-
reason?: string | undefined
|
|
82
|
+
reason?: string | undefined,
|
|
83
83
|
): void;
|
|
84
84
|
|
|
85
85
|
public abstract initialize(): Promise<() => void>;
|
|
@@ -110,7 +110,7 @@ export abstract class AbstractWebSocket {
|
|
|
110
110
|
}
|
|
111
111
|
|
|
112
112
|
public sendMessage<
|
|
113
|
-
TMessage extends IOEventMessage<any> = IOEventMessage<any
|
|
113
|
+
TMessage extends IOEventMessage<any> = IOEventMessage<any>,
|
|
114
114
|
>(data: TMessage): void {
|
|
115
115
|
return this.send(JSON.stringify(data));
|
|
116
116
|
}
|
package/src/IORoom.ts
CHANGED
|
@@ -60,7 +60,7 @@ export type IORoomConfig<
|
|
|
60
60
|
TInput extends EventRecord<string, any> = {},
|
|
61
61
|
TOutputBroadcast extends EventRecord<string, any> = {},
|
|
62
62
|
TOutputSelf extends EventRecord<string, any> = {},
|
|
63
|
-
TOutputSync extends EventRecord<string, any> = {}
|
|
63
|
+
TOutputSync extends EventRecord<string, any> = {},
|
|
64
64
|
> = Partial<IORoomListeners<TPlatform>> & {
|
|
65
65
|
authorize?: TAuthorize;
|
|
66
66
|
context: TContext & InferPlatformRoomContextType<TPlatform>;
|
|
@@ -93,7 +93,7 @@ export class IORoom<
|
|
|
93
93
|
TInput extends EventRecord<string, any> = {},
|
|
94
94
|
TOutputBroadcast extends EventRecord<string, any> = {},
|
|
95
95
|
TOutputSelf extends EventRecord<string, any> = {},
|
|
96
|
-
TOutputSync extends EventRecord<string, any> = {}
|
|
96
|
+
TOutputSync extends EventRecord<string, any> = {},
|
|
97
97
|
> implements
|
|
98
98
|
IOLike<TAuthorize, TInput, TOutputBroadcast, TOutputSelf, TOutputSync>
|
|
99
99
|
{
|
|
@@ -129,7 +129,7 @@ export class IORoom<
|
|
|
129
129
|
TOutputBroadcast,
|
|
130
130
|
TOutputSelf,
|
|
131
131
|
TOutputSync
|
|
132
|
-
|
|
132
|
+
>,
|
|
133
133
|
) {
|
|
134
134
|
const { authorize, context, debug, events, onDestroy, platform } =
|
|
135
135
|
config;
|
|
@@ -150,7 +150,7 @@ export class IORoom<
|
|
|
150
150
|
|
|
151
151
|
public broadcast<TEvent extends keyof InferIOInput<this>>(
|
|
152
152
|
event: TEvent,
|
|
153
|
-
data: Id<InferIOInput<this>[TEvent]
|
|
153
|
+
data: Id<InferIOInput<this>[TEvent]>,
|
|
154
154
|
): Promise<void> {
|
|
155
155
|
const message = { type: event, data } as BroadcastMessage<this>;
|
|
156
156
|
|
|
@@ -177,7 +177,7 @@ export class IORoom<
|
|
|
177
177
|
|
|
178
178
|
public async register(
|
|
179
179
|
webSocket: InferPlatformWebSocketType<TPlatform>,
|
|
180
|
-
options: WebsocketRegisterOptions<TPlatform
|
|
180
|
+
options: WebsocketRegisterOptions<TPlatform>,
|
|
181
181
|
): Promise<void> {
|
|
182
182
|
const { token, ..._platformEventContext } = options;
|
|
183
183
|
const platformEventContext =
|
|
@@ -193,8 +193,8 @@ export class IORoom<
|
|
|
193
193
|
|
|
194
194
|
this._logDebug(
|
|
195
195
|
`${colors.blue(
|
|
196
|
-
`Registering connection for room ${this.id}
|
|
197
|
-
)} ${sessionId}
|
|
196
|
+
`Registering connection for room ${this.id}:`,
|
|
197
|
+
)} ${sessionId}`,
|
|
198
198
|
);
|
|
199
199
|
|
|
200
200
|
const uninitializeWs = await pluvWs.initialize();
|
|
@@ -206,14 +206,14 @@ export class IORoom<
|
|
|
206
206
|
const isTokenInUse =
|
|
207
207
|
!!user &&
|
|
208
208
|
Array.from(this._sessions.values()).some(
|
|
209
|
-
(session) => session.user?.id === user.id
|
|
209
|
+
(session) => session.user?.id === user.id,
|
|
210
210
|
);
|
|
211
211
|
|
|
212
212
|
if (isUnauthorized || isTokenInUse) {
|
|
213
213
|
this._logDebug(
|
|
214
214
|
`${colors.blue(
|
|
215
|
-
"Authorization failed for connection:"
|
|
216
|
-
)} ${sessionId}
|
|
215
|
+
"Authorization failed for connection:",
|
|
216
|
+
)} ${sessionId}`,
|
|
217
217
|
);
|
|
218
218
|
|
|
219
219
|
pluvWs.handleError({ error: new Error("Not authorized") });
|
|
@@ -240,12 +240,12 @@ export class IORoom<
|
|
|
240
240
|
await this._platform.persistance.addUser(
|
|
241
241
|
this.id,
|
|
242
242
|
sessionId,
|
|
243
|
-
user ?? {}
|
|
243
|
+
user ?? {},
|
|
244
244
|
);
|
|
245
245
|
|
|
246
246
|
const onClose = this._onClose(session, uninitializeWs).bind(this);
|
|
247
247
|
const onMessage = this._onMessage(session, platformEventContext).bind(
|
|
248
|
-
this
|
|
248
|
+
this,
|
|
249
249
|
);
|
|
250
250
|
|
|
251
251
|
pluvWs.addEventListener("close", onClose);
|
|
@@ -256,8 +256,8 @@ export class IORoom<
|
|
|
256
256
|
|
|
257
257
|
this._logDebug(
|
|
258
258
|
`${colors.blue(
|
|
259
|
-
`Registered connection for room ${this.id}
|
|
260
|
-
)} ${sessionId}
|
|
259
|
+
`Registered connection for room ${this.id}:`,
|
|
260
|
+
)} ${sessionId}`,
|
|
261
261
|
);
|
|
262
262
|
|
|
263
263
|
const size = this.getSize();
|
|
@@ -274,7 +274,7 @@ export class IORoom<
|
|
|
274
274
|
const quitters = Array.from(this._sessions.values()).filter(
|
|
275
275
|
(session) =>
|
|
276
276
|
session.quit ||
|
|
277
|
-
currentTime - session.timers.ping > PING_TIMEOUT_MS
|
|
277
|
+
currentTime - session.timers.ping > PING_TIMEOUT_MS,
|
|
278
278
|
);
|
|
279
279
|
|
|
280
280
|
quitters.forEach((session) => {
|
|
@@ -309,12 +309,12 @@ export class IORoom<
|
|
|
309
309
|
type: "$REGISTERED",
|
|
310
310
|
data: { sessionId: session.id },
|
|
311
311
|
},
|
|
312
|
-
session
|
|
312
|
+
session,
|
|
313
313
|
);
|
|
314
314
|
}
|
|
315
315
|
|
|
316
316
|
private async _getAuthorizedUser(
|
|
317
|
-
token: Maybe<string
|
|
317
|
+
token: Maybe<string>,
|
|
318
318
|
): Promise<InferIOAuthorizeUser<InferIOAuthorize<this>> | null> {
|
|
319
319
|
if (!this._authorize) return null;
|
|
320
320
|
if (!token) return null;
|
|
@@ -333,7 +333,7 @@ export class IORoom<
|
|
|
333
333
|
|
|
334
334
|
if (payload.room !== this.id) {
|
|
335
335
|
this._logDebug(
|
|
336
|
-
colors.blue(`Token is not authorized for room ${this.id}:`)
|
|
336
|
+
colors.blue(`Token is not authorized for room ${this.id}:`),
|
|
337
337
|
);
|
|
338
338
|
this._logDebug(colors.blue("Received:"), payload.room);
|
|
339
339
|
this._logDebug(token);
|
|
@@ -345,7 +345,7 @@ export class IORoom<
|
|
|
345
345
|
return this._authorize.user.parse(payload.user) ?? null;
|
|
346
346
|
} catch {
|
|
347
347
|
this._logDebug(
|
|
348
|
-
`${colors.blue("Token fails validation:")} ${token}
|
|
348
|
+
`${colors.blue("Token fails validation:")} ${token}`,
|
|
349
349
|
);
|
|
350
350
|
|
|
351
351
|
return null;
|
|
@@ -353,7 +353,7 @@ export class IORoom<
|
|
|
353
353
|
}
|
|
354
354
|
|
|
355
355
|
private _getEventConfig(
|
|
356
|
-
message: EventMessage<string, any
|
|
356
|
+
message: EventMessage<string, any>,
|
|
357
357
|
):
|
|
358
358
|
| InferEventConfig<
|
|
359
359
|
TPlatform,
|
|
@@ -368,7 +368,7 @@ export class IORoom<
|
|
|
368
368
|
}
|
|
369
369
|
|
|
370
370
|
private _getEventInputs(
|
|
371
|
-
message: EventMessage<string, any
|
|
371
|
+
message: EventMessage<string, any>,
|
|
372
372
|
): TInput[string] {
|
|
373
373
|
const eventConfig = this._getEventConfig(message);
|
|
374
374
|
|
|
@@ -388,7 +388,7 @@ export class IORoom<
|
|
|
388
388
|
}
|
|
389
389
|
|
|
390
390
|
private _getEventResolverObject(
|
|
391
|
-
message: EventMessage<string, any
|
|
391
|
+
message: EventMessage<string, any>,
|
|
392
392
|
): EventResolverObject<
|
|
393
393
|
TPlatform,
|
|
394
394
|
TContext,
|
|
@@ -441,7 +441,7 @@ export class IORoom<
|
|
|
441
441
|
};
|
|
442
442
|
|
|
443
443
|
this._sendMessage(message, session, options);
|
|
444
|
-
}
|
|
444
|
+
},
|
|
445
445
|
);
|
|
446
446
|
|
|
447
447
|
this._uninitialize = async () => {
|
|
@@ -468,7 +468,7 @@ export class IORoom<
|
|
|
468
468
|
|
|
469
469
|
private _onClose(
|
|
470
470
|
session: WebSocketSession,
|
|
471
|
-
callback?: () => void
|
|
471
|
+
callback?: () => void,
|
|
472
472
|
): () => void {
|
|
473
473
|
return (): void => {
|
|
474
474
|
if (!this._uninitialize) return;
|
|
@@ -477,8 +477,8 @@ export class IORoom<
|
|
|
477
477
|
|
|
478
478
|
this._logDebug(
|
|
479
479
|
`${colors.blue(
|
|
480
|
-
`(Unregistering connection for room ${this.id}
|
|
481
|
-
)} ${session.id}
|
|
480
|
+
`(Unregistering connection for room ${this.id}:`,
|
|
481
|
+
)} ${session.id}`,
|
|
482
482
|
);
|
|
483
483
|
this._sessions.delete(session.id);
|
|
484
484
|
|
|
@@ -499,11 +499,11 @@ export class IORoom<
|
|
|
499
499
|
|
|
500
500
|
this._logDebug(
|
|
501
501
|
`${colors.blue(
|
|
502
|
-
`Unregistered connection for room ${this.id}
|
|
503
|
-
)} ${session.id}
|
|
502
|
+
`Unregistered connection for room ${this.id}:`,
|
|
503
|
+
)} ${session.id}`,
|
|
504
504
|
);
|
|
505
505
|
this._logDebug(
|
|
506
|
-
`${colors.blue(`Room ${this.id} size:`)} ${size}
|
|
506
|
+
`${colors.blue(`Room ${this.id} size:`)} ${size}`,
|
|
507
507
|
);
|
|
508
508
|
|
|
509
509
|
if (size) return;
|
|
@@ -515,7 +515,7 @@ export class IORoom<
|
|
|
515
515
|
|
|
516
516
|
private _onMessage(
|
|
517
517
|
session: WebSocketSession,
|
|
518
|
-
platformEventContext: InferPlatformEventContextType<TPlatform
|
|
518
|
+
platformEventContext: InferPlatformEventContextType<TPlatform>,
|
|
519
519
|
): (event: AbstractMessageEvent) => void {
|
|
520
520
|
return (event: AbstractMessageEvent): void => {
|
|
521
521
|
const baseContext: EventResolverContext<TContext> = {
|
|
@@ -619,7 +619,7 @@ export class IORoom<
|
|
|
619
619
|
private _sendMessage(
|
|
620
620
|
message: EventMessage<string, any>,
|
|
621
621
|
sender: SendMessageSender | null,
|
|
622
|
-
options: SendMessageOptions = {}
|
|
622
|
+
options: SendMessageOptions = {},
|
|
623
623
|
): void {
|
|
624
624
|
switch (options.type) {
|
|
625
625
|
case "self": {
|
|
@@ -644,7 +644,7 @@ export class IORoom<
|
|
|
644
644
|
private _sendBroadcastMessage(
|
|
645
645
|
message: EventMessage<string, any>,
|
|
646
646
|
sender: SendMessageSender | null,
|
|
647
|
-
sessionIds?: readonly string[]
|
|
647
|
+
sessionIds?: readonly string[],
|
|
648
648
|
): void {
|
|
649
649
|
const senderId = sender?.id ?? null;
|
|
650
650
|
|
|
@@ -667,7 +667,7 @@ export class IORoom<
|
|
|
667
667
|
|
|
668
668
|
private _sendSelfMessage(
|
|
669
669
|
message: EventMessage<string, any>,
|
|
670
|
-
sender: SendMessageSender | null
|
|
670
|
+
sender: SendMessageSender | null,
|
|
671
671
|
): void {
|
|
672
672
|
const senderId = sender?.id;
|
|
673
673
|
|
|
@@ -687,7 +687,7 @@ export class IORoom<
|
|
|
687
687
|
|
|
688
688
|
private _sendSyncMessage(
|
|
689
689
|
message: EventMessage<string, any>,
|
|
690
|
-
sender: SendMessageSender | null
|
|
690
|
+
sender: SendMessageSender | null,
|
|
691
691
|
): void {
|
|
692
692
|
const senderId = sender?.id;
|
|
693
693
|
|
package/src/Persistance.ts
CHANGED
|
@@ -8,7 +8,7 @@ export class Persistance extends AbstractPersistance {
|
|
|
8
8
|
public addUser(
|
|
9
9
|
room: string,
|
|
10
10
|
connectionId: string,
|
|
11
|
-
user: JsonObject | null
|
|
11
|
+
user: JsonObject | null,
|
|
12
12
|
): Promise<void> {
|
|
13
13
|
const users =
|
|
14
14
|
this._users.get(room) ?? new Map<string, JsonObject | null>();
|
|
@@ -58,7 +58,7 @@ export class Persistance extends AbstractPersistance {
|
|
|
58
58
|
|
|
59
59
|
public getUser(
|
|
60
60
|
room: string,
|
|
61
|
-
connectionId: string
|
|
61
|
+
connectionId: string,
|
|
62
62
|
): Promise<JsonObject | null> {
|
|
63
63
|
const user = this._users.get(room)?.get(connectionId);
|
|
64
64
|
|
package/src/PluvIO.ts
CHANGED
|
@@ -60,7 +60,7 @@ type GetInitialStorageEvent<TPlatform extends AbstractPlatform> = {
|
|
|
60
60
|
} & InferPlatformRoomContextType<TPlatform>;
|
|
61
61
|
|
|
62
62
|
export type GetInitialStorageFn<TPlatform extends AbstractPlatform> = (
|
|
63
|
-
event: GetInitialStorageEvent<TPlatform
|
|
63
|
+
event: GetInitialStorageEvent<TPlatform>,
|
|
64
64
|
) => MaybePromise<Maybe<string>>;
|
|
65
65
|
|
|
66
66
|
export interface PluvIOListeners<TPlatform extends AbstractPlatform> {
|
|
@@ -75,7 +75,7 @@ export type PluvIOConfig<
|
|
|
75
75
|
TInput extends EventRecord<string, any> = {},
|
|
76
76
|
TOutputBroadcast extends EventRecord<string, any> = {},
|
|
77
77
|
TOutputSelf extends EventRecord<string, any> = {},
|
|
78
|
-
TOutputSync extends EventRecord<string, any> = {}
|
|
78
|
+
TOutputSync extends EventRecord<string, any> = {},
|
|
79
79
|
> = Partial<PluvIOListeners<TPlatform>> & {
|
|
80
80
|
authorize?: TAuthorize;
|
|
81
81
|
context?: TContext;
|
|
@@ -103,7 +103,7 @@ export class PluvIO<
|
|
|
103
103
|
TInput extends EventRecord<string, any> = {},
|
|
104
104
|
TOutputBroadcast extends EventRecord<string, any> = {},
|
|
105
105
|
TOutputSelf extends EventRecord<string, any> = {},
|
|
106
|
-
TOutputSync extends EventRecord<string, any> = {}
|
|
106
|
+
TOutputSync extends EventRecord<string, any> = {},
|
|
107
107
|
> implements
|
|
108
108
|
IOLike<TAuthorize, TInput, TOutputBroadcast, TOutputSelf, TOutputSync>
|
|
109
109
|
{
|
|
@@ -153,7 +153,7 @@ export class PluvIO<
|
|
|
153
153
|
user: user as InferIOAuthorizeUser<TAuthorize>,
|
|
154
154
|
},
|
|
155
155
|
}),
|
|
156
|
-
{}
|
|
156
|
+
{},
|
|
157
157
|
);
|
|
158
158
|
|
|
159
159
|
return { $OTHERS_RECEIVED: { others } };
|
|
@@ -196,7 +196,7 @@ export class PluvIO<
|
|
|
196
196
|
|
|
197
197
|
await this._platform.persistance.setStorageState(
|
|
198
198
|
room,
|
|
199
|
-
encodedState
|
|
199
|
+
encodedState,
|
|
200
200
|
);
|
|
201
201
|
|
|
202
202
|
this._listeners.onStorageUpdated({
|
|
@@ -208,7 +208,7 @@ export class PluvIO<
|
|
|
208
208
|
|
|
209
209
|
const state =
|
|
210
210
|
(await this._platform.persistance.getStorageState(
|
|
211
|
-
room
|
|
211
|
+
room,
|
|
212
212
|
)) ?? doc.encodeStateAsUpdate();
|
|
213
213
|
|
|
214
214
|
return { $STORAGE_RECEIVED: { state } };
|
|
@@ -235,7 +235,7 @@ export class PluvIO<
|
|
|
235
235
|
session.presence = Object.assign(
|
|
236
236
|
Object.create(null),
|
|
237
237
|
session.presence,
|
|
238
|
-
presence
|
|
238
|
+
presence,
|
|
239
239
|
);
|
|
240
240
|
|
|
241
241
|
return { $PRESENCE_UPDATED: { presence } };
|
|
@@ -244,7 +244,7 @@ export class PluvIO<
|
|
|
244
244
|
$UPDATE_STORAGE: {
|
|
245
245
|
resolver: ({ origin, update }, { context, doc, room }) => {
|
|
246
246
|
if (
|
|
247
|
-
origin === "$
|
|
247
|
+
origin === "$INITIALIZED" &&
|
|
248
248
|
Object.keys(doc.toJSON()).length
|
|
249
249
|
) {
|
|
250
250
|
return;
|
|
@@ -286,7 +286,7 @@ export class PluvIO<
|
|
|
286
286
|
TOutputBroadcast,
|
|
287
287
|
TOutputSelf,
|
|
288
288
|
TOutputSync
|
|
289
|
-
|
|
289
|
+
>,
|
|
290
290
|
) {
|
|
291
291
|
const {
|
|
292
292
|
authorize,
|
|
@@ -314,11 +314,11 @@ export class PluvIO<
|
|
|
314
314
|
}
|
|
315
315
|
|
|
316
316
|
public async createToken(
|
|
317
|
-
params: JWTEncodeParams<InferIOAuthorizeUser<InferIOAuthorize<this
|
|
317
|
+
params: JWTEncodeParams<InferIOAuthorizeUser<InferIOAuthorize<this>>>,
|
|
318
318
|
): Promise<string> {
|
|
319
319
|
if (!this._authorize) {
|
|
320
320
|
throw new Error(
|
|
321
|
-
"IO does not specify authorize during initialization."
|
|
321
|
+
"IO does not specify authorize during initialization.",
|
|
322
322
|
);
|
|
323
323
|
}
|
|
324
324
|
|
|
@@ -333,7 +333,7 @@ export class PluvIO<
|
|
|
333
333
|
TData extends JsonObject = {},
|
|
334
334
|
TResultBroadcast extends EventRecord<string, any> = {},
|
|
335
335
|
TResultSelf extends EventRecord<string, any> = {},
|
|
336
|
-
TResultSync extends EventRecord<string, any> = {}
|
|
336
|
+
TResultSync extends EventRecord<string, any> = {},
|
|
337
337
|
>(
|
|
338
338
|
event: TEvent,
|
|
339
339
|
config: EventConfig<
|
|
@@ -343,7 +343,7 @@ export class PluvIO<
|
|
|
343
343
|
TResultBroadcast,
|
|
344
344
|
TResultSelf,
|
|
345
345
|
TResultSync
|
|
346
|
-
|
|
346
|
+
>,
|
|
347
347
|
): PluvIO<
|
|
348
348
|
TPlatform,
|
|
349
349
|
TAuthorize,
|
|
@@ -388,13 +388,13 @@ export class PluvIO<
|
|
|
388
388
|
new PluvIO({
|
|
389
389
|
events: newEvent as any,
|
|
390
390
|
platform: this._platform,
|
|
391
|
-
})
|
|
391
|
+
}),
|
|
392
392
|
) as any;
|
|
393
393
|
}
|
|
394
394
|
|
|
395
395
|
public getRoom(
|
|
396
396
|
room: string,
|
|
397
|
-
options: GetRoomOptions<TPlatform
|
|
397
|
+
options: GetRoomOptions<TPlatform>,
|
|
398
398
|
): IORoom<
|
|
399
399
|
TPlatform,
|
|
400
400
|
TAuthorize,
|
|
@@ -437,7 +437,7 @@ export class PluvIO<
|
|
|
437
437
|
events: this._events,
|
|
438
438
|
onDestroy: ({ encodedState, room }) => {
|
|
439
439
|
this._logDebug(
|
|
440
|
-
`${colors.blue("Deleting empty room:")} ${room}
|
|
440
|
+
`${colors.blue("Deleting empty room:")} ${room}`,
|
|
441
441
|
);
|
|
442
442
|
|
|
443
443
|
const roomContext = {
|
|
@@ -455,7 +455,9 @@ export class PluvIO<
|
|
|
455
455
|
|
|
456
456
|
this._logDebug(`${colors.blue("Deleted room:")} ${room}`);
|
|
457
457
|
this._logDebug(
|
|
458
|
-
`${colors.blue("Rooms available:")} ${rooms.join(
|
|
458
|
+
`${colors.blue("Rooms available:")} ${rooms.join(
|
|
459
|
+
", ",
|
|
460
|
+
)}`,
|
|
459
461
|
);
|
|
460
462
|
}
|
|
461
463
|
},
|
|
@@ -469,7 +471,7 @@ export class PluvIO<
|
|
|
469
471
|
|
|
470
472
|
this._logDebug(`${colors.blue("Created room:")} ${room}`);
|
|
471
473
|
this._logDebug(
|
|
472
|
-
`${colors.blue("Rooms available:")} ${rooms.join(", ")}
|
|
474
|
+
`${colors.blue("Rooms available:")} ${rooms.join(", ")}`,
|
|
473
475
|
);
|
|
474
476
|
}
|
|
475
477
|
|
|
@@ -478,7 +480,7 @@ export class PluvIO<
|
|
|
478
480
|
|
|
479
481
|
public static merge<TIO1 extends PluvIO, TIO2 extends PluvIO>(
|
|
480
482
|
first: TIO1,
|
|
481
|
-
second: TIO2
|
|
483
|
+
second: TIO2,
|
|
482
484
|
): PluvIO<
|
|
483
485
|
InferIOPlatform<TIO1>,
|
|
484
486
|
InferIOAuthorize<TIO1>,
|
package/src/PubSub.ts
CHANGED
|
@@ -17,7 +17,7 @@ export class PubSub extends AbstractPubSub {
|
|
|
17
17
|
|
|
18
18
|
public async publish(
|
|
19
19
|
roomName: string,
|
|
20
|
-
payload: IOPubSubEventMessage<any
|
|
20
|
+
payload: IOPubSubEventMessage<any>,
|
|
21
21
|
): Promise<void> {
|
|
22
22
|
const subject = this._subjects.get(roomName);
|
|
23
23
|
|
|
@@ -28,7 +28,7 @@ export class PubSub extends AbstractPubSub {
|
|
|
28
28
|
|
|
29
29
|
public subscribe(
|
|
30
30
|
roomName: string,
|
|
31
|
-
onMessage: (message: IOPubSubEventMessage<any>) => void
|
|
31
|
+
onMessage: (message: IOPubSubEventMessage<any>) => void,
|
|
32
32
|
): Promise<number> {
|
|
33
33
|
const id = this._currentSubscriptionId++;
|
|
34
34
|
const refs = this._subscriptionsRefs.get(roomName) ?? [];
|
|
@@ -69,7 +69,7 @@ export class PubSub extends AbstractPubSub {
|
|
|
69
69
|
roomName,
|
|
70
70
|
index === -1
|
|
71
71
|
? refs
|
|
72
|
-
: [...refs.slice(0, index), ...refs.slice(index + 1)]
|
|
72
|
+
: [...refs.slice(0, index), ...refs.slice(index + 1)],
|
|
73
73
|
);
|
|
74
74
|
|
|
75
75
|
unsubscribe();
|
package/src/authorize.ts
CHANGED
|
@@ -11,7 +11,7 @@ export const getEncryptionKey = async (secret: string): Promise<Uint8Array> => {
|
|
|
11
11
|
secret,
|
|
12
12
|
"",
|
|
13
13
|
"Pluv.io Generated Encryption Key",
|
|
14
|
-
32
|
|
14
|
+
32,
|
|
15
15
|
);
|
|
16
16
|
};
|
|
17
17
|
|
|
@@ -35,7 +35,7 @@ export interface AuthorizeParams {
|
|
|
35
35
|
export interface AuthorizeModule {
|
|
36
36
|
decode: <TUser extends BaseUser>(jwt: string) => Promise<TUser | null>;
|
|
37
37
|
encode: <TUser extends BaseUser>(
|
|
38
|
-
params: JWTEncodeParams<TUser
|
|
38
|
+
params: JWTEncodeParams<TUser>,
|
|
39
39
|
) => Promise<string>;
|
|
40
40
|
}
|
|
41
41
|
|
|
@@ -45,7 +45,7 @@ export const authorize = (params: AuthorizeParams) => {
|
|
|
45
45
|
const { platform, secret } = params;
|
|
46
46
|
|
|
47
47
|
const encode = async <TUser extends BaseUser>(
|
|
48
|
-
encodeParams: JWTEncodeParams<TUser
|
|
48
|
+
encodeParams: JWTEncodeParams<TUser>,
|
|
49
49
|
): Promise<string> => {
|
|
50
50
|
const { maxAge = DEFAULT_MAX_AGE_MS, room, user } = encodeParams;
|
|
51
51
|
|
|
@@ -66,7 +66,7 @@ export const authorize = (params: AuthorizeParams) => {
|
|
|
66
66
|
};
|
|
67
67
|
|
|
68
68
|
const decode = async <TUser extends BaseUser>(
|
|
69
|
-
jwt: string
|
|
69
|
+
jwt: string,
|
|
70
70
|
): Promise<JWT<TUser> | null> => {
|
|
71
71
|
const encryptionSecret = await getEncryptionKey(secret);
|
|
72
72
|
|
package/src/createIO.ts
CHANGED
|
@@ -15,7 +15,7 @@ export type CreateIOParams<
|
|
|
15
15
|
TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>,
|
|
16
16
|
TContext extends JsonObject = {},
|
|
17
17
|
TAuthorizeUser extends BaseUser = BaseUser,
|
|
18
|
-
TAuthorizeRequired extends boolean = false
|
|
18
|
+
TAuthorizeRequired extends boolean = false,
|
|
19
19
|
> = Partial<PluvIOListeners<TPlatform>> & {
|
|
20
20
|
authorize?: IOAuthorize<TAuthorizeUser, TAuthorizeRequired>;
|
|
21
21
|
context?: TContext;
|
|
@@ -28,14 +28,14 @@ export const createIO = <
|
|
|
28
28
|
TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>,
|
|
29
29
|
TContext extends JsonObject = {},
|
|
30
30
|
TAuthorizeUser extends BaseUser = BaseUser,
|
|
31
|
-
TAuthorizeRequired extends boolean = false
|
|
31
|
+
TAuthorizeRequired extends boolean = false,
|
|
32
32
|
>(
|
|
33
33
|
params: CreateIOParams<
|
|
34
34
|
TPlatform,
|
|
35
35
|
TContext,
|
|
36
36
|
TAuthorizeUser,
|
|
37
37
|
TAuthorizeRequired
|
|
38
|
-
|
|
38
|
+
>,
|
|
39
39
|
): PluvIO<
|
|
40
40
|
TPlatform,
|
|
41
41
|
IOAuthorize<TAuthorizeUser, TAuthorizeRequired>,
|
package/src/types.ts
CHANGED
|
@@ -33,7 +33,7 @@ export interface EventConfig<
|
|
|
33
33
|
TData extends JsonObject = {},
|
|
34
34
|
TResultBroadcast extends EventRecord<string, any> = {},
|
|
35
35
|
TResultSelf extends EventRecord<string, any> = {},
|
|
36
|
-
TResultSync extends EventRecord<string, any> = {}
|
|
36
|
+
TResultSync extends EventRecord<string, any> = {},
|
|
37
37
|
> {
|
|
38
38
|
input?: InputZodLike<TData>;
|
|
39
39
|
resolver:
|
|
@@ -59,23 +59,23 @@ export type EventConfigType = "broadcast" | "self" | "sync";
|
|
|
59
59
|
export type SyncEventResolver<
|
|
60
60
|
TContext extends Record<string, any> = {},
|
|
61
61
|
TData extends JsonObject = {},
|
|
62
|
-
TResultBroadcast extends EventRecord<string, any> = {}
|
|
62
|
+
TResultBroadcast extends EventRecord<string, any> = {},
|
|
63
63
|
> = (
|
|
64
64
|
data: TData,
|
|
65
|
-
context: EventResolverContext<TContext
|
|
65
|
+
context: EventResolverContext<TContext>,
|
|
66
66
|
) => MaybePromise<TResultBroadcast | void>;
|
|
67
67
|
|
|
68
68
|
export type EventResolver<
|
|
69
69
|
TContext extends Record<string, any> = {},
|
|
70
70
|
TData extends JsonObject = {},
|
|
71
|
-
TResultBroadcast extends EventRecord<string, any> = {}
|
|
71
|
+
TResultBroadcast extends EventRecord<string, any> = {},
|
|
72
72
|
> = (
|
|
73
73
|
data: TData,
|
|
74
|
-
context: EventResolverContext<TContext
|
|
74
|
+
context: EventResolverContext<TContext>,
|
|
75
75
|
) => MaybePromise<TResultBroadcast | void>;
|
|
76
76
|
|
|
77
77
|
export interface EventResolverContext<
|
|
78
|
-
TContext extends Record<string, any> = {}
|
|
78
|
+
TContext extends Record<string, any> = {},
|
|
79
79
|
> {
|
|
80
80
|
context: TContext;
|
|
81
81
|
doc: YjsDoc<any>;
|
|
@@ -102,7 +102,7 @@ export type EventResolverObject<
|
|
|
102
102
|
TData extends JsonObject = {},
|
|
103
103
|
TResultBroadcast extends EventRecord<string, any> = {},
|
|
104
104
|
TResultSelf extends EventRecord<string, any> = {},
|
|
105
|
-
TResultSync extends EventRecord<string, any> = {}
|
|
105
|
+
TResultSync extends EventRecord<string, any> = {},
|
|
106
106
|
> = {
|
|
107
107
|
broadcast?: EventResolver<
|
|
108
108
|
TContext &
|
|
@@ -127,7 +127,7 @@ export type InferEventConfig<
|
|
|
127
127
|
TInput extends EventRecord<string, any> = {},
|
|
128
128
|
TOutputBroadcast extends EventRecord<string, any> = {},
|
|
129
129
|
TOutputSelf extends EventRecord<string, any> = {},
|
|
130
|
-
TOutputSync extends EventRecord<string, any> = {}
|
|
130
|
+
TOutputSync extends EventRecord<string, any> = {},
|
|
131
131
|
> = {
|
|
132
132
|
[P in keyof TInput]: P extends string
|
|
133
133
|
? Id<
|