@pluv/io 0.4.2 → 0.6.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 +7 -7
- package/CHANGELOG.md +38 -0
- package/dist/index.d.ts +71 -58
- package/dist/index.js +61 -45
- package/dist/index.mjs +61 -45
- package/package.json +1 -1
- package/src/AbstractPlatform.ts +19 -2
- package/src/IORoom.ts +66 -27
- package/src/PluvIO.ts +78 -49
- package/src/createIO.ts +5 -5
- package/src/index.ts +16 -9
- package/src/types.ts +48 -9
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
> @pluv/io@0.
|
|
2
|
+
> @pluv/io@0.6.0 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
|
|
@@ -8,10 +8,10 @@
|
|
|
8
8
|
[34mCLI[39m Target: es6
|
|
9
9
|
[34mESM[39m Build start
|
|
10
10
|
[34mCJS[39m Build start
|
|
11
|
-
[
|
|
12
|
-
[
|
|
13
|
-
[
|
|
14
|
-
[
|
|
11
|
+
[32mESM[39m [1mdist/index.mjs [22m[32m28.98 KB[39m
|
|
12
|
+
[32mESM[39m ⚡️ Build success in 93ms
|
|
13
|
+
[32mCJS[39m [1mdist/index.js [22m[32m31.04 KB[39m
|
|
14
|
+
[32mCJS[39m ⚡️ Build success in 93ms
|
|
15
15
|
[34mDTS[39m Build start
|
|
16
|
-
[32mDTS[39m ⚡️ Build success in
|
|
17
|
-
[32mDTS[39m [1mdist/index.d.ts [22m[
|
|
16
|
+
[32mDTS[39m ⚡️ Build success in 3311ms
|
|
17
|
+
[32mDTS[39m [1mdist/index.d.ts [22m[32m17.03 KB[39m
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,43 @@
|
|
|
1
1
|
# @pluv/io
|
|
2
2
|
|
|
3
|
+
## 0.6.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 2e7cbfa: Updated createIO config option `initialStorage` to be renamed to `getInitialStorage` and updated the params to use an object that includes platform parameters.
|
|
8
|
+
|
|
9
|
+
Previously:
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
createIO({
|
|
13
|
+
initialStorage(room: string): Promise<string> => {
|
|
14
|
+
// ...
|
|
15
|
+
},
|
|
16
|
+
});
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Now:
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
createIO({
|
|
23
|
+
platform: platformCloudflare<Env>(),
|
|
24
|
+
getInitialStorage({
|
|
25
|
+
// If you're using Cloudflare, this is the Cloudflare env parameter
|
|
26
|
+
env,
|
|
27
|
+
// The name of the room
|
|
28
|
+
room,
|
|
29
|
+
}): Promise<string> => {
|
|
30
|
+
// ...
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## 0.5.0
|
|
36
|
+
|
|
37
|
+
### Minor Changes
|
|
38
|
+
|
|
39
|
+
- ae4e1f1: added platform-specific contexts to expose env in cloudflare and req in node.js
|
|
40
|
+
|
|
3
41
|
## 0.4.2
|
|
4
42
|
|
|
5
43
|
### Patch Changes
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { JsonObject, MaybePromise, IOEventMessage, EventRecord, InputZodLike, Id, IOLike, InferIOOutput,
|
|
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
2
|
export { BaseUser, EventMessage, EventRecord, IOEventMessage } from '@pluv/types';
|
|
3
3
|
import { YjsDoc } from '@pluv/crdt-yjs';
|
|
4
4
|
|
|
@@ -79,12 +79,13 @@ declare global {
|
|
|
79
79
|
log: (...data: any[]) => void;
|
|
80
80
|
};
|
|
81
81
|
}
|
|
82
|
-
interface EventConfig<TContext extends
|
|
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
83
|
input?: InputZodLike<TData>;
|
|
84
|
-
resolver: EventResolver<TContext
|
|
84
|
+
resolver: EventResolver<TContext & InferPlatformRoomContextType<TPlatform> & InferPlatformEventContextType<TPlatform>, TData, TResultBroadcast> | EventResolverObject<TPlatform, TContext, TData, TResultBroadcast, TResultSelf, TResultSync>;
|
|
85
85
|
}
|
|
86
|
-
type
|
|
87
|
-
|
|
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> = {}> {
|
|
88
89
|
context: TContext;
|
|
89
90
|
doc: YjsDoc<any>;
|
|
90
91
|
room: string;
|
|
@@ -103,13 +104,13 @@ interface EventResolverContext<TContext extends JsonObject = {}> {
|
|
|
103
104
|
session: WebSocketSession | null;
|
|
104
105
|
sessions: Map<string, WebSocketSession>;
|
|
105
106
|
}
|
|
106
|
-
type EventResolverObject<TContext extends
|
|
107
|
-
broadcast?: EventResolver<TContext
|
|
108
|
-
self?: EventResolver<TContext
|
|
109
|
-
sync?:
|
|
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>;
|
|
110
111
|
};
|
|
111
|
-
type InferEventConfig<TContext extends
|
|
112
|
-
[P in keyof TInput]: P extends string ? Id<EventConfig<TContext, TInput[P], TOutputBroadcast, TOutputSelf, TOutputSync>> : never;
|
|
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;
|
|
113
114
|
};
|
|
114
115
|
type SendMessageOptions = {
|
|
115
116
|
type?: "broadcast";
|
|
@@ -141,7 +142,9 @@ declare abstract class AbstractPubSub {
|
|
|
141
142
|
abstract unsubscribe(subscriptionId: number): void;
|
|
142
143
|
}
|
|
143
144
|
|
|
144
|
-
type
|
|
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;
|
|
145
148
|
type AbstractPlatformConfig = {
|
|
146
149
|
persistance?: undefined;
|
|
147
150
|
pubSub?: undefined;
|
|
@@ -152,7 +155,9 @@ type AbstractPlatformConfig = {
|
|
|
152
155
|
interface ConvertWebSocketConfig {
|
|
153
156
|
room: string;
|
|
154
157
|
}
|
|
155
|
-
declare abstract class AbstractPlatform<TWebSocket = any> {
|
|
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;
|
|
156
161
|
persistance: AbstractPersistance;
|
|
157
162
|
pubSub: AbstractPubSub;
|
|
158
163
|
constructor(config?: AbstractPlatformConfig);
|
|
@@ -161,39 +166,24 @@ declare abstract class AbstractPlatform<TWebSocket = any> {
|
|
|
161
166
|
abstract randomUUID(): string;
|
|
162
167
|
}
|
|
163
168
|
|
|
164
|
-
|
|
169
|
+
type IORoomListenerEvent<TPlatform extends AbstractPlatform> = {
|
|
165
170
|
room: string;
|
|
166
|
-
|
|
167
|
-
|
|
171
|
+
encodedState: string;
|
|
172
|
+
} & InferPlatformRoomContextType<TPlatform>;
|
|
173
|
+
interface IORoomListeners<TPlatform extends AbstractPlatform> {
|
|
174
|
+
onDestroy: (event: IORoomListenerEvent<TPlatform>) => void;
|
|
168
175
|
}
|
|
169
|
-
|
|
170
|
-
maxAge?: number;
|
|
171
|
-
room: string;
|
|
172
|
-
user: TUser;
|
|
173
|
-
}
|
|
174
|
-
interface AuthorizeParams {
|
|
175
|
-
platform: AbstractPlatform;
|
|
176
|
-
secret: string;
|
|
177
|
-
}
|
|
178
|
-
interface AuthorizeModule {
|
|
179
|
-
decode: <TUser extends BaseUser>(jwt: string) => Promise<TUser | null>;
|
|
180
|
-
encode: <TUser extends BaseUser>(params: JWTEncodeParams<TUser>) => Promise<string>;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
interface IORoomListeners {
|
|
184
|
-
onDestroy: (encodedState: string) => void;
|
|
185
|
-
}
|
|
186
|
-
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> & {
|
|
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>> & {
|
|
187
177
|
authorize?: TAuthorize;
|
|
188
|
-
context: TContext
|
|
178
|
+
context: TContext & InferPlatformRoomContextType<TPlatform>;
|
|
189
179
|
debug: boolean;
|
|
190
|
-
events: InferEventConfig<TContext, TInput, TOutputBroadcast, TOutputSelf, TOutputSync>;
|
|
180
|
+
events: InferEventConfig<TPlatform, TContext, TInput, TOutputBroadcast, TOutputSelf, TOutputSync>;
|
|
191
181
|
platform: TPlatform;
|
|
192
182
|
};
|
|
193
|
-
|
|
183
|
+
type WebsocketRegisterOptions<TPlatform extends AbstractPlatform> = {
|
|
194
184
|
token?: string | null;
|
|
195
|
-
}
|
|
196
|
-
declare class IORoom<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends IOAuthorize<any, any> = BaseIOAuthorize, TContext extends
|
|
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> {
|
|
197
187
|
private readonly _context;
|
|
198
188
|
private readonly _debug;
|
|
199
189
|
private readonly _platform;
|
|
@@ -202,12 +192,12 @@ declare class IORoom<TPlatform extends AbstractPlatform<any> = AbstractPlatform<
|
|
|
202
192
|
private _sessions;
|
|
203
193
|
private _uninitialize;
|
|
204
194
|
readonly _authorize: TAuthorize | null;
|
|
205
|
-
readonly _events: InferEventConfig<TContext, TInput, TOutputBroadcast, TOutputSelf, TOutputSync>;
|
|
195
|
+
readonly _events: InferEventConfig<TPlatform, TContext, TInput, TOutputBroadcast, TOutputSelf, TOutputSync>;
|
|
206
196
|
readonly id: string;
|
|
207
197
|
constructor(id: string, config: IORoomConfig<TPlatform, TAuthorize, TContext, TInput, TOutputBroadcast, TOutputSelf, TOutputSync>);
|
|
208
198
|
broadcast<TEvent extends keyof InferIOInput<this>>(event: TEvent, data: Id<InferIOInput<this>[TEvent]>): Promise<void>;
|
|
209
199
|
getSize(): number;
|
|
210
|
-
register(webSocket:
|
|
200
|
+
register(webSocket: InferPlatformWebSocketType<TPlatform>, options: WebsocketRegisterOptions<TPlatform>): Promise<void>;
|
|
211
201
|
private _broadcast;
|
|
212
202
|
private _emitQuitters;
|
|
213
203
|
private _emitRegistered;
|
|
@@ -226,49 +216,72 @@ declare class IORoom<TPlatform extends AbstractPlatform<any> = AbstractPlatform<
|
|
|
226
216
|
private _sendSyncMessage;
|
|
227
217
|
}
|
|
228
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
|
+
|
|
229
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;
|
|
230
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;
|
|
231
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;
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
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;
|
|
235
248
|
}
|
|
236
|
-
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
|
|
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>> & {
|
|
237
250
|
authorize?: TAuthorize;
|
|
238
251
|
context?: TContext;
|
|
239
252
|
debug?: boolean;
|
|
240
|
-
events?: InferEventConfig<TContext, TInput, TOutputBroadcast, TOutputSelf, TOutputSync>;
|
|
241
|
-
|
|
253
|
+
events?: InferEventConfig<TPlatform, TContext, TInput, TOutputBroadcast, TOutputSelf, TOutputSync>;
|
|
254
|
+
getInitialStorage?: GetInitialStorageFn<TPlatform>;
|
|
242
255
|
platform: TPlatform;
|
|
243
256
|
};
|
|
244
|
-
|
|
257
|
+
type GetRoomOptions<TPlatform extends AbstractPlatform> = {
|
|
245
258
|
debug?: boolean;
|
|
246
|
-
}
|
|
259
|
+
} & InferPlatformRoomContextType<TPlatform>;
|
|
247
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> {
|
|
248
261
|
private _context;
|
|
249
|
-
private
|
|
262
|
+
private _getInitialStorage;
|
|
250
263
|
private _rooms;
|
|
251
264
|
readonly _authorize: TAuthorize | null;
|
|
252
265
|
readonly _debug: boolean;
|
|
253
|
-
readonly _events: InferEventConfig<TContext, TInput, TOutputBroadcast, TOutputSelf, TOutputSync>;
|
|
254
|
-
readonly _listeners: PluvIOListeners
|
|
266
|
+
readonly _events: InferEventConfig<TPlatform, TContext, TInput, TOutputBroadcast, TOutputSelf, TOutputSync>;
|
|
267
|
+
readonly _listeners: PluvIOListeners<TPlatform>;
|
|
255
268
|
readonly _platform: TPlatform;
|
|
256
269
|
constructor(options: PluvIOConfig<TPlatform, TAuthorize, TContext, TInput, TOutputBroadcast, TOutputSelf, TOutputSync>);
|
|
257
270
|
createToken(params: JWTEncodeParams<InferIOAuthorizeUser<InferIOAuthorize<this>>>): Promise<string>;
|
|
258
|
-
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<TContext, TData, TResultBroadcast, TResultSelf, TResultSync>): PluvIO<TPlatform, TAuthorize, TContext, Spread<[TInput, EventRecord<TEvent, TData>]>, Spread<[TOutputBroadcast, TResultBroadcast]>, Spread<[TOutputSelf, TResultSelf]>, Spread<[TOutputSync, TResultSync]>>;
|
|
259
|
-
getRoom(room: string, options
|
|
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>;
|
|
260
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>]>>;
|
|
261
274
|
private _logDebug;
|
|
262
275
|
private _purgeEmptyRooms;
|
|
263
276
|
}
|
|
264
277
|
|
|
265
|
-
type CreateIOParams<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TContext extends JsonObject = {}, TAuthorizeUser extends BaseUser = BaseUser, TAuthorizeRequired extends boolean = false> = Partial<PluvIOListeners
|
|
278
|
+
type CreateIOParams<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TContext extends JsonObject = {}, TAuthorizeUser extends BaseUser = BaseUser, TAuthorizeRequired extends boolean = false> = Partial<PluvIOListeners<TPlatform>> & {
|
|
266
279
|
authorize?: IOAuthorize<TAuthorizeUser, TAuthorizeRequired>;
|
|
267
280
|
context?: TContext;
|
|
268
281
|
debug?: boolean;
|
|
269
|
-
|
|
282
|
+
getInitialStorage?: GetInitialStorageFn<TPlatform>;
|
|
270
283
|
platform: TPlatform;
|
|
271
284
|
};
|
|
272
|
-
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>>, {}, {}>;
|
|
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>>, {}, {}>;
|
|
273
286
|
|
|
274
|
-
export { AbstractCloseEvent, AbstractErrorEvent, AbstractEvent, AbstractEventMap, AbstractListener, AbstractMessageEvent, AbstractPersistance, AbstractPlatform, AbstractPubSub, AbstractWebSocket, AbstractWebSocketConfig, AbstractWebSocketHandleErrorParams, AuthorizeModule, AuthorizeParams, ConvertWebSocketConfig, CreateIOParams, GetRoomOptions, IOPubSubEventMessage, IORoom, InferIORoom,
|
|
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
|
@@ -316,9 +316,7 @@ var IORoom = class {
|
|
|
316
316
|
this._platform = platform;
|
|
317
317
|
this.id = id;
|
|
318
318
|
this._listeners = {
|
|
319
|
-
onDestroy: (
|
|
320
|
-
onDestroy == null ? void 0 : onDestroy(encodedState);
|
|
321
|
-
}
|
|
319
|
+
onDestroy: (event) => onDestroy == null ? void 0 : onDestroy(event)
|
|
322
320
|
};
|
|
323
321
|
if (authorize2)
|
|
324
322
|
this._authorize = authorize2;
|
|
@@ -337,10 +335,11 @@ var IORoom = class {
|
|
|
337
335
|
}
|
|
338
336
|
register(webSocket, options) {
|
|
339
337
|
return __async(this, null, function* () {
|
|
340
|
-
var
|
|
338
|
+
var _b;
|
|
339
|
+
const _a = options, { token } = _a, _platformEventContext = __objRest(_a, ["token"]);
|
|
340
|
+
const platformEventContext = _platformEventContext;
|
|
341
341
|
if (!this._uninitialize)
|
|
342
342
|
yield this._initialize();
|
|
343
|
-
const token = options == null ? void 0 : options.token;
|
|
344
343
|
const sessionId = this._platform.randomUUID();
|
|
345
344
|
const pluvWs = this._platform.convertWebSocket(webSocket, {
|
|
346
345
|
room: this.id
|
|
@@ -352,7 +351,7 @@ var IORoom = class {
|
|
|
352
351
|
);
|
|
353
352
|
const uninitializeWs = yield pluvWs.initialize();
|
|
354
353
|
const user = yield this._getAuthorizedUser(token);
|
|
355
|
-
const isUnauthorized = !!((
|
|
354
|
+
const isUnauthorized = !!((_b = this._authorize) == null ? void 0 : _b.required) && !user;
|
|
356
355
|
const isTokenInUse = !!user && Array.from(this._sessions.values()).some(
|
|
357
356
|
(session2) => {
|
|
358
357
|
var _a2;
|
|
@@ -388,7 +387,9 @@ var IORoom = class {
|
|
|
388
387
|
user != null ? user : {}
|
|
389
388
|
);
|
|
390
389
|
const onClose = this._onClose(session, uninitializeWs).bind(this);
|
|
391
|
-
const onMessage = this._onMessage(session).bind(
|
|
390
|
+
const onMessage = this._onMessage(session, platformEventContext).bind(
|
|
391
|
+
this
|
|
392
|
+
);
|
|
392
393
|
pluvWs.addEventListener("close", onClose);
|
|
393
394
|
pluvWs.addEventListener("error", onClose);
|
|
394
395
|
pluvWs.addEventListener("message", onMessage);
|
|
@@ -514,7 +515,10 @@ var IORoom = class {
|
|
|
514
515
|
const encodedState = this._doc.encodeStateAsUpdate();
|
|
515
516
|
this._doc.destroy();
|
|
516
517
|
this._doc = (0, import_crdt_yjs.doc)();
|
|
517
|
-
this._listeners.onDestroy(
|
|
518
|
+
this._listeners.onDestroy(__spreadProps(__spreadValues({}, this._context), {
|
|
519
|
+
encodedState,
|
|
520
|
+
room: this.id
|
|
521
|
+
}));
|
|
518
522
|
this._uninitialize = null;
|
|
519
523
|
});
|
|
520
524
|
});
|
|
@@ -558,10 +562,10 @@ var IORoom = class {
|
|
|
558
562
|
});
|
|
559
563
|
};
|
|
560
564
|
}
|
|
561
|
-
_onMessage(session) {
|
|
565
|
+
_onMessage(session, platformEventContext) {
|
|
562
566
|
return (event) => {
|
|
563
567
|
var _a, _b, _c;
|
|
564
|
-
const
|
|
568
|
+
const baseContext = {
|
|
565
569
|
context: this._context,
|
|
566
570
|
doc: this._doc,
|
|
567
571
|
room: this.id,
|
|
@@ -589,10 +593,13 @@ var IORoom = class {
|
|
|
589
593
|
});
|
|
590
594
|
return;
|
|
591
595
|
}
|
|
596
|
+
const extendedContext = __spreadProps(__spreadValues({}, baseContext), {
|
|
597
|
+
context: __spreadValues(__spreadValues({}, this._context), platformEventContext)
|
|
598
|
+
});
|
|
592
599
|
Promise.all([
|
|
593
|
-
(_a = resolver.broadcast) == null ? void 0 : _a.call(resolver, inputs,
|
|
594
|
-
(_b = resolver.self) == null ? void 0 : _b.call(resolver, inputs,
|
|
595
|
-
(_c = resolver.sync) == null ? void 0 : _c.call(resolver, inputs,
|
|
600
|
+
(_a = resolver.broadcast) == null ? void 0 : _a.call(resolver, inputs, extendedContext),
|
|
601
|
+
(_b = resolver.self) == null ? void 0 : _b.call(resolver, inputs, extendedContext),
|
|
602
|
+
(_c = resolver.sync) == null ? void 0 : _c.call(resolver, inputs, baseContext)
|
|
596
603
|
]).then(([broadcast, self, sync]) => {
|
|
597
604
|
if (broadcast) {
|
|
598
605
|
Object.entries(broadcast).forEach(([type, data]) => {
|
|
@@ -723,7 +730,7 @@ var IORoom = class {
|
|
|
723
730
|
var PluvIO = class {
|
|
724
731
|
constructor(options) {
|
|
725
732
|
this._context = {};
|
|
726
|
-
this.
|
|
733
|
+
this._getInitialStorage = null;
|
|
727
734
|
this._rooms = /* @__PURE__ */ new Map();
|
|
728
735
|
this._authorize = null;
|
|
729
736
|
this._events = {
|
|
@@ -759,20 +766,25 @@ var PluvIO = class {
|
|
|
759
766
|
}
|
|
760
767
|
};
|
|
761
768
|
},
|
|
762
|
-
self: (_0, _1) => __async(this, [_0, _1], function* ({ update }, { doc: doc2, room }) {
|
|
769
|
+
self: (_0, _1) => __async(this, [_0, _1], function* ({ update }, { context, doc: doc2, room }) {
|
|
763
770
|
var _a, _b;
|
|
764
771
|
const oldState = yield this._platform.persistance.getStorageState(room);
|
|
765
772
|
const updates = [
|
|
766
|
-
oldState ? null : yield (_a = this.
|
|
773
|
+
oldState ? null : yield (_a = this._getInitialStorage) == null ? void 0 : _a.call(this, __spreadProps(__spreadValues({}, context), {
|
|
774
|
+
room
|
|
775
|
+
})),
|
|
767
776
|
update
|
|
768
777
|
];
|
|
769
778
|
if (updates.some((_update) => !!_update)) {
|
|
770
|
-
const
|
|
779
|
+
const encodedState = updates.reduce((_doc, _up) => _doc.applyUpdate(_up), doc2).encodeStateAsUpdate();
|
|
771
780
|
yield this._platform.persistance.setStorageState(
|
|
772
781
|
room,
|
|
773
|
-
|
|
782
|
+
encodedState
|
|
774
783
|
);
|
|
775
|
-
this._listeners.onStorageUpdated(
|
|
784
|
+
this._listeners.onStorageUpdated(__spreadProps(__spreadValues({}, context), {
|
|
785
|
+
encodedState,
|
|
786
|
+
room
|
|
787
|
+
}));
|
|
776
788
|
}
|
|
777
789
|
const state = (_b = yield this._platform.persistance.getStorageState(
|
|
778
790
|
room
|
|
@@ -805,16 +817,19 @@ var PluvIO = class {
|
|
|
805
817
|
}
|
|
806
818
|
},
|
|
807
819
|
$UPDATE_STORAGE: {
|
|
808
|
-
resolver: ({ origin, update }, { doc: doc2, room }) => {
|
|
820
|
+
resolver: ({ origin, update }, { context, doc: doc2, room }) => {
|
|
809
821
|
if (origin === "$STORAGE_RECEIVED" && Object.keys(doc2.toJSON()).length) {
|
|
810
822
|
return;
|
|
811
823
|
}
|
|
812
824
|
const updated = doc2.applyUpdate(update);
|
|
813
|
-
const
|
|
814
|
-
this._platform.persistance.setStorageState(room,
|
|
815
|
-
this._listeners.onStorageUpdated(
|
|
825
|
+
const encodedState = updated.encodeStateAsUpdate();
|
|
826
|
+
this._platform.persistance.setStorageState(room, encodedState).then(() => {
|
|
827
|
+
this._listeners.onStorageUpdated(__spreadProps(__spreadValues({}, context), {
|
|
828
|
+
encodedState,
|
|
829
|
+
room
|
|
830
|
+
}));
|
|
816
831
|
});
|
|
817
|
-
return { $STORAGE_UPDATED: { state } };
|
|
832
|
+
return { $STORAGE_UPDATED: { state: encodedState } };
|
|
818
833
|
}
|
|
819
834
|
}
|
|
820
835
|
};
|
|
@@ -823,7 +838,7 @@ var PluvIO = class {
|
|
|
823
838
|
context,
|
|
824
839
|
debug = false,
|
|
825
840
|
events,
|
|
826
|
-
|
|
841
|
+
getInitialStorage,
|
|
827
842
|
onRoomDeleted,
|
|
828
843
|
onStorageUpdated,
|
|
829
844
|
platform
|
|
@@ -836,15 +851,11 @@ var PluvIO = class {
|
|
|
836
851
|
this._context = context;
|
|
837
852
|
if (events)
|
|
838
853
|
this._events = events;
|
|
839
|
-
if (
|
|
840
|
-
this.
|
|
854
|
+
if (getInitialStorage)
|
|
855
|
+
this._getInitialStorage = getInitialStorage;
|
|
841
856
|
this._listeners = {
|
|
842
|
-
onRoomDeleted: (
|
|
843
|
-
|
|
844
|
-
},
|
|
845
|
-
onStorageUpdated: (room, encodedState) => {
|
|
846
|
-
return onStorageUpdated == null ? void 0 : onStorageUpdated(room, encodedState);
|
|
847
|
-
}
|
|
857
|
+
onRoomDeleted: (event) => onRoomDeleted == null ? void 0 : onRoomDeleted(event),
|
|
858
|
+
onStorageUpdated: (event) => onStorageUpdated == null ? void 0 : onStorageUpdated(event)
|
|
848
859
|
};
|
|
849
860
|
}
|
|
850
861
|
createToken(params) {
|
|
@@ -875,9 +886,9 @@ var PluvIO = class {
|
|
|
875
886
|
})
|
|
876
887
|
);
|
|
877
888
|
}
|
|
878
|
-
getRoom(room, options
|
|
879
|
-
var
|
|
880
|
-
const { debug } =
|
|
889
|
+
getRoom(room, options) {
|
|
890
|
+
var _b;
|
|
891
|
+
const _a = options, { debug } = _a, platformRoomContext = __objRest(_a, ["debug"]);
|
|
881
892
|
this._purgeEmptyRooms();
|
|
882
893
|
if (!/^[a-z0-9]+[a-z0-9\-_]+[a-z0-9]+$/i.test(room)) {
|
|
883
894
|
throw new Error("Unsupported room name");
|
|
@@ -886,20 +897,25 @@ var PluvIO = class {
|
|
|
886
897
|
const oldRoom = this._rooms.get(room);
|
|
887
898
|
if (oldRoom)
|
|
888
899
|
return oldRoom;
|
|
900
|
+
const roomContext = __spreadValues(__spreadValues({}, this._context), platformRoomContext);
|
|
889
901
|
const newRoom = new IORoom(room, {
|
|
890
|
-
authorize: (
|
|
891
|
-
context:
|
|
902
|
+
authorize: (_b = this._authorize) != null ? _b : void 0,
|
|
903
|
+
context: roomContext,
|
|
892
904
|
debug: debug != null ? debug : this._debug,
|
|
893
905
|
events: this._events,
|
|
894
|
-
onDestroy: (encodedState) => {
|
|
906
|
+
onDestroy: ({ encodedState, room: room2 }) => {
|
|
895
907
|
this._logDebug(
|
|
896
|
-
`${import_kleur2.default.blue("Deleting empty room:")} ${
|
|
908
|
+
`${import_kleur2.default.blue("Deleting empty room:")} ${room2}`
|
|
897
909
|
);
|
|
898
|
-
|
|
899
|
-
|
|
910
|
+
const roomContext2 = __spreadValues({
|
|
911
|
+
room: room2,
|
|
912
|
+
encodedState
|
|
913
|
+
}, platformRoomContext);
|
|
914
|
+
this._rooms.delete(room2);
|
|
915
|
+
this._listeners.onRoomDeleted(roomContext2);
|
|
900
916
|
if (this._debug) {
|
|
901
917
|
const rooms = Array.from(this._rooms.keys());
|
|
902
|
-
this._logDebug(`${import_kleur2.default.blue("Deleted room:")} ${
|
|
918
|
+
this._logDebug(`${import_kleur2.default.blue("Deleted room:")} ${room2}`);
|
|
903
919
|
this._logDebug(
|
|
904
920
|
`${import_kleur2.default.blue("Rooms available:")} ${rooms.join(", ")}`
|
|
905
921
|
);
|
|
@@ -954,7 +970,7 @@ var createIO = (params) => {
|
|
|
954
970
|
authorize: authorize2,
|
|
955
971
|
context,
|
|
956
972
|
debug,
|
|
957
|
-
|
|
973
|
+
getInitialStorage,
|
|
958
974
|
onRoomDeleted,
|
|
959
975
|
onStorageUpdated,
|
|
960
976
|
platform
|
|
@@ -963,7 +979,7 @@ var createIO = (params) => {
|
|
|
963
979
|
authorize: authorize2,
|
|
964
980
|
context,
|
|
965
981
|
debug,
|
|
966
|
-
|
|
982
|
+
getInitialStorage,
|
|
967
983
|
onRoomDeleted,
|
|
968
984
|
onStorageUpdated,
|
|
969
985
|
platform
|