@pluv/io 0.4.1 → 0.5.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 +12 -0
- package/dist/index.d.ts +64 -55
- package/dist/index.js +55 -38
- package/dist/index.mjs +55 -38
- package/package.json +1 -1
- package/src/AbstractPlatform.ts +19 -2
- package/src/IORoom.ts +66 -27
- package/src/PluvIO.ts +57 -33
- package/src/createIO.ts +1 -1
- package/src/index.ts +11 -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.5.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
|
-
[32mCJS[39m [1mdist/index.js [22m[32m30.
|
|
12
|
-
[32mCJS[39m ⚡️ Build success in
|
|
13
|
-
[32mESM[39m [1mdist/index.mjs [22m[
|
|
14
|
-
[32mESM[39m ⚡️ Build success in
|
|
11
|
+
[32mCJS[39m [1mdist/index.js [22m[32m30.95 KB[39m
|
|
12
|
+
[32mCJS[39m ⚡️ Build success in 112ms
|
|
13
|
+
[32mESM[39m [1mdist/index.mjs [22m[32m28.88 KB[39m
|
|
14
|
+
[32mESM[39m ⚡️ Build success in 129ms
|
|
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 3722ms
|
|
17
|
+
[32mDTS[39m [1mdist/index.d.ts [22m[32m16.78 KB[39m
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @pluv/io
|
|
2
2
|
|
|
3
|
+
## 0.5.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- ae4e1f1: added platform-specific contexts to expose env in cloudflare and req in node.js
|
|
8
|
+
|
|
9
|
+
## 0.4.2
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- 8917309: fixed PluvIO listeners onRoomDeleted and onStorageUpdated not working when additional events were added
|
|
14
|
+
|
|
3
15
|
## 0.4.1
|
|
4
16
|
|
|
5
17
|
### 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,68 @@ 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
|
-
interface PluvIOListeners {
|
|
233
|
-
onRoomDeleted: (
|
|
234
|
-
onStorageUpdated: (
|
|
241
|
+
interface PluvIOListeners<TPlatform extends AbstractPlatform> {
|
|
242
|
+
onRoomDeleted: (event: IORoomListenerEvent<TPlatform>) => void;
|
|
243
|
+
onStorageUpdated: (event: IORoomListenerEvent<TPlatform>) => void;
|
|
235
244
|
}
|
|
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
|
|
245
|
+
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
246
|
authorize?: TAuthorize;
|
|
238
247
|
context?: TContext;
|
|
239
248
|
debug?: boolean;
|
|
240
|
-
events?: InferEventConfig<TContext, TInput, TOutputBroadcast, TOutputSelf, TOutputSync>;
|
|
249
|
+
events?: InferEventConfig<TPlatform, TContext, TInput, TOutputBroadcast, TOutputSelf, TOutputSync>;
|
|
241
250
|
initialStorage?: (room: string) => MaybePromise<Maybe<string>>;
|
|
242
251
|
platform: TPlatform;
|
|
243
252
|
};
|
|
244
|
-
|
|
253
|
+
type GetRoomOptions<TPlatform extends AbstractPlatform> = {
|
|
245
254
|
debug?: boolean;
|
|
246
|
-
}
|
|
255
|
+
} & InferPlatformRoomContextType<TPlatform>;
|
|
247
256
|
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
257
|
private _context;
|
|
249
258
|
private _initialStorage;
|
|
250
|
-
private _listeners;
|
|
251
259
|
private _rooms;
|
|
252
260
|
readonly _authorize: TAuthorize | null;
|
|
253
261
|
readonly _debug: boolean;
|
|
254
|
-
readonly _events: InferEventConfig<TContext, TInput, TOutputBroadcast, TOutputSelf, TOutputSync>;
|
|
262
|
+
readonly _events: InferEventConfig<TPlatform, TContext, TInput, TOutputBroadcast, TOutputSelf, TOutputSync>;
|
|
263
|
+
readonly _listeners: PluvIOListeners<TPlatform>;
|
|
255
264
|
readonly _platform: TPlatform;
|
|
256
265
|
constructor(options: PluvIOConfig<TPlatform, TAuthorize, TContext, TInput, TOutputBroadcast, TOutputSelf, TOutputSync>);
|
|
257
266
|
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
|
|
267
|
+
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]>>;
|
|
268
|
+
getRoom(room: string, options: GetRoomOptions<TPlatform>): IORoom<TPlatform, TAuthorize, TContext, TInput, TOutputBroadcast, TOutputSelf, TOutputSync>;
|
|
260
269
|
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
270
|
private _logDebug;
|
|
262
271
|
private _purgeEmptyRooms;
|
|
263
272
|
}
|
|
264
273
|
|
|
265
|
-
type CreateIOParams<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TContext extends JsonObject = {}, TAuthorizeUser extends BaseUser = BaseUser, TAuthorizeRequired extends boolean = false> = Partial<PluvIOListeners
|
|
274
|
+
type CreateIOParams<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TContext extends JsonObject = {}, TAuthorizeUser extends BaseUser = BaseUser, TAuthorizeRequired extends boolean = false> = Partial<PluvIOListeners<TPlatform>> & {
|
|
266
275
|
authorize?: IOAuthorize<TAuthorizeUser, TAuthorizeRequired>;
|
|
267
276
|
context?: TContext;
|
|
268
277
|
debug?: boolean;
|
|
269
278
|
initialStorage?: (room: string) => MaybePromise<Maybe<string>>;
|
|
270
279
|
platform: TPlatform;
|
|
271
280
|
};
|
|
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>>, {}, {}>;
|
|
281
|
+
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
282
|
|
|
274
|
-
export { AbstractCloseEvent, AbstractErrorEvent, AbstractEvent, AbstractEventMap, AbstractListener, AbstractMessageEvent, AbstractPersistance, AbstractPlatform, AbstractPubSub, AbstractWebSocket, AbstractWebSocketConfig, AbstractWebSocketHandleErrorParams, AuthorizeModule, AuthorizeParams, ConvertWebSocketConfig, CreateIOParams, GetRoomOptions, IOPubSubEventMessage, IORoom, InferIORoom,
|
|
283
|
+
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, 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]) => {
|
|
@@ -759,7 +766,7 @@ 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 = [
|
|
@@ -767,12 +774,15 @@ var PluvIO = class {
|
|
|
767
774
|
update
|
|
768
775
|
];
|
|
769
776
|
if (updates.some((_update) => !!_update)) {
|
|
770
|
-
const
|
|
777
|
+
const encodedState = updates.reduce((_doc, _up) => _doc.applyUpdate(_up), doc2).encodeStateAsUpdate();
|
|
771
778
|
yield this._platform.persistance.setStorageState(
|
|
772
779
|
room,
|
|
773
|
-
|
|
780
|
+
encodedState
|
|
774
781
|
);
|
|
775
|
-
this._listeners.onStorageUpdated(
|
|
782
|
+
this._listeners.onStorageUpdated(__spreadProps(__spreadValues({}, context), {
|
|
783
|
+
encodedState,
|
|
784
|
+
room
|
|
785
|
+
}));
|
|
776
786
|
}
|
|
777
787
|
const state = (_b = yield this._platform.persistance.getStorageState(
|
|
778
788
|
room
|
|
@@ -805,16 +815,19 @@ var PluvIO = class {
|
|
|
805
815
|
}
|
|
806
816
|
},
|
|
807
817
|
$UPDATE_STORAGE: {
|
|
808
|
-
resolver: ({ origin, update }, { doc: doc2, room }) => {
|
|
818
|
+
resolver: ({ origin, update }, { context, doc: doc2, room }) => {
|
|
809
819
|
if (origin === "$STORAGE_RECEIVED" && Object.keys(doc2.toJSON()).length) {
|
|
810
820
|
return;
|
|
811
821
|
}
|
|
812
822
|
const updated = doc2.applyUpdate(update);
|
|
813
|
-
const
|
|
814
|
-
this._platform.persistance.setStorageState(room,
|
|
815
|
-
this._listeners.onStorageUpdated(
|
|
823
|
+
const encodedState = updated.encodeStateAsUpdate();
|
|
824
|
+
this._platform.persistance.setStorageState(room, encodedState).then(() => {
|
|
825
|
+
this._listeners.onStorageUpdated(__spreadProps(__spreadValues({}, context), {
|
|
826
|
+
encodedState,
|
|
827
|
+
room
|
|
828
|
+
}));
|
|
816
829
|
});
|
|
817
|
-
return { $STORAGE_UPDATED: { state } };
|
|
830
|
+
return { $STORAGE_UPDATED: { state: encodedState } };
|
|
818
831
|
}
|
|
819
832
|
}
|
|
820
833
|
};
|
|
@@ -839,12 +852,8 @@ var PluvIO = class {
|
|
|
839
852
|
if (initialStorage)
|
|
840
853
|
this._initialStorage = initialStorage;
|
|
841
854
|
this._listeners = {
|
|
842
|
-
onRoomDeleted: (
|
|
843
|
-
|
|
844
|
-
},
|
|
845
|
-
onStorageUpdated: (room, encodedState) => {
|
|
846
|
-
return onStorageUpdated == null ? void 0 : onStorageUpdated(room, encodedState);
|
|
847
|
-
}
|
|
855
|
+
onRoomDeleted: (event) => onRoomDeleted == null ? void 0 : onRoomDeleted(event),
|
|
856
|
+
onStorageUpdated: (event) => onStorageUpdated == null ? void 0 : onStorageUpdated(event)
|
|
848
857
|
};
|
|
849
858
|
}
|
|
850
859
|
createToken(params) {
|
|
@@ -875,9 +884,9 @@ var PluvIO = class {
|
|
|
875
884
|
})
|
|
876
885
|
);
|
|
877
886
|
}
|
|
878
|
-
getRoom(room, options
|
|
879
|
-
var
|
|
880
|
-
const { debug } =
|
|
887
|
+
getRoom(room, options) {
|
|
888
|
+
var _b;
|
|
889
|
+
const _a = options, { debug } = _a, platformRoomContext = __objRest(_a, ["debug"]);
|
|
881
890
|
this._purgeEmptyRooms();
|
|
882
891
|
if (!/^[a-z0-9]+[a-z0-9\-_]+[a-z0-9]+$/i.test(room)) {
|
|
883
892
|
throw new Error("Unsupported room name");
|
|
@@ -886,20 +895,25 @@ var PluvIO = class {
|
|
|
886
895
|
const oldRoom = this._rooms.get(room);
|
|
887
896
|
if (oldRoom)
|
|
888
897
|
return oldRoom;
|
|
898
|
+
const roomContext = __spreadValues(__spreadValues({}, this._context), platformRoomContext);
|
|
889
899
|
const newRoom = new IORoom(room, {
|
|
890
|
-
authorize: (
|
|
891
|
-
context:
|
|
900
|
+
authorize: (_b = this._authorize) != null ? _b : void 0,
|
|
901
|
+
context: roomContext,
|
|
892
902
|
debug: debug != null ? debug : this._debug,
|
|
893
903
|
events: this._events,
|
|
894
|
-
onDestroy: (encodedState) => {
|
|
904
|
+
onDestroy: ({ encodedState, room: room2 }) => {
|
|
895
905
|
this._logDebug(
|
|
896
|
-
`${import_kleur2.default.blue("Deleting empty room:")} ${
|
|
906
|
+
`${import_kleur2.default.blue("Deleting empty room:")} ${room2}`
|
|
897
907
|
);
|
|
898
|
-
|
|
899
|
-
|
|
908
|
+
const roomContext2 = __spreadValues({
|
|
909
|
+
room: room2,
|
|
910
|
+
encodedState
|
|
911
|
+
}, platformRoomContext);
|
|
912
|
+
this._rooms.delete(room2);
|
|
913
|
+
this._listeners.onRoomDeleted(roomContext2);
|
|
900
914
|
if (this._debug) {
|
|
901
915
|
const rooms = Array.from(this._rooms.keys());
|
|
902
|
-
this._logDebug(`${import_kleur2.default.blue("Deleted room:")} ${
|
|
916
|
+
this._logDebug(`${import_kleur2.default.blue("Deleted room:")} ${room2}`);
|
|
903
917
|
this._logDebug(
|
|
904
918
|
`${import_kleur2.default.blue("Rooms available:")} ${rooms.join(", ")}`
|
|
905
919
|
);
|
|
@@ -924,11 +938,14 @@ var PluvIO = class {
|
|
|
924
938
|
const platform = first._platform;
|
|
925
939
|
const events1 = first._events;
|
|
926
940
|
const events2 = second._events;
|
|
941
|
+
const { onRoomDeleted, onStorageUpdated } = first._listeners;
|
|
927
942
|
const events = Object.assign(/* @__PURE__ */ Object.create(null), events1, events2);
|
|
928
943
|
return new PluvIO({
|
|
929
944
|
authorize: authorize2,
|
|
930
945
|
debug,
|
|
931
946
|
events,
|
|
947
|
+
onRoomDeleted,
|
|
948
|
+
onStorageUpdated,
|
|
932
949
|
platform
|
|
933
950
|
});
|
|
934
951
|
}
|