@pluv/io 2.2.0 → 2.2.2

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/src/PluvServer.ts CHANGED
@@ -7,6 +7,7 @@ import type {
7
7
  InferIOAuthorizeUser,
8
8
  JsonObject,
9
9
  Maybe,
10
+ NonNilProps,
10
11
  } from "@pluv/types";
11
12
  import colors from "kleur";
12
13
  import type {
@@ -95,21 +96,12 @@ export class PluvServer<
95
96
  {
96
97
  public readonly version: string = __PLUV_VERSION as any;
97
98
 
98
- private readonly _authorize: TAuthorize = null as TAuthorize;
99
- private readonly _context: PluvContext<TPlatform, TContext> = {} as PluvContext<
100
- TPlatform,
101
- TContext
99
+ private readonly _config: NonNilProps<
100
+ PluvServerConfig<TPlatform, TAuthorize, TContext, TEvents>
102
101
  >;
103
- private readonly _crdt: { doc: (value: any) => AbstractCrdtDocFactory<any, any> };
104
- private readonly _debug: boolean;
105
- private readonly _getInitialStorage: GetInitialStorageFn<TContext> | null = null;
106
- private readonly _io: PluvIO<TPlatform, TAuthorize, TContext>;
107
- private readonly _limits: PluvIOLimits;
108
- private readonly _platform: () => TPlatform;
109
- private readonly _inputRouter: PluvRouter<TPlatform, TAuthorize, TContext, TEvents>;
110
102
 
111
103
  public get fetch(): (...args: any[]) => Promise<any> {
112
- const platform = this._platform();
104
+ const platform = this._config.platform();
113
105
 
114
106
  return ((...args: any[]): Promise<any> => {
115
107
  if (!platform._fetch)
@@ -126,10 +118,10 @@ export class PluvServer<
126
118
  */
127
119
  public get _defs() {
128
120
  return {
129
- authorize: this._authorize,
130
- context: this._context,
121
+ authorize: this._config.authorize,
122
+ context: this._config.context,
131
123
  events: this._router._defs.events,
132
- platform: this._platform(),
124
+ platform: this._config.platform(),
133
125
  } as {
134
126
  authorize: TAuthorize;
135
127
  context: TContext;
@@ -139,6 +131,8 @@ export class PluvServer<
139
131
  }
140
132
 
141
133
  private get _baseRouter(): PluvRouter<TPlatform, TAuthorize, TContext, {}> {
134
+ const listeners = this._getListeners();
135
+
142
136
  return new PluvRouter({
143
137
  $getOthers: this._procedure.sync((data, { room, session, sessions }) => {
144
138
  const currentTime = Date.now();
@@ -202,7 +196,10 @@ export class PluvServer<
202
196
  const update = (data as any)?.update as Maybe<string>;
203
197
 
204
198
  if (!oldState) {
205
- const loadedState = await this._getInitialStorage?.({ context, room });
199
+ const loadedState = await this._config.getInitialStorage?.({
200
+ context,
201
+ room,
202
+ });
206
203
 
207
204
  if (!!loadedState) {
208
205
  doc.applyEncodedState({ update: loadedState }).getEncodedState();
@@ -234,8 +231,8 @@ export class PluvServer<
234
231
  const storageSize = new TextEncoder().encode(encodedState).length;
235
232
 
236
233
  if (
237
- !!this._limits.storageMaxSize &&
238
- storageSize > this._limits.storageMaxSize
234
+ !!this._config.limits.storageMaxSize &&
235
+ storageSize > this._config.limits.storageMaxSize
239
236
  ) {
240
237
  throw new Error("Storage has exceeded the size limit");
241
238
  }
@@ -246,7 +243,7 @@ export class PluvServer<
246
243
  this._logDebug(error);
247
244
  });
248
245
 
249
- this._getListeners().onStorageUpdated({
246
+ listeners.onStorageUpdated({
250
247
  context,
251
248
  encodedState,
252
249
  platform,
@@ -293,7 +290,10 @@ export class PluvServer<
293
290
  const updated = Object.assign(Object.create(null), session.presence, cleanedPatch);
294
291
  const bytes = new TextEncoder().encode(JSON.stringify(updated)).length;
295
292
 
296
- if (!!this._limits.presenceMaxSize && bytes > this._limits.presenceMaxSize) {
293
+ if (
294
+ !!this._config.limits.presenceMaxSize &&
295
+ bytes > this._config.limits.presenceMaxSize
296
+ ) {
297
297
  throw new Error(
298
298
  `Large presence. Presence must be at most 512 bytes. Current size: ${bytes.toLocaleString()}`,
299
299
  );
@@ -318,12 +318,15 @@ export class PluvServer<
318
318
  const encodedState = updated.getEncodedState();
319
319
  const storageSize = new TextEncoder().encode(encodedState).length;
320
320
 
321
- if (!!this._limits.storageMaxSize && storageSize > this._limits.storageMaxSize) {
321
+ if (
322
+ !!this._config.limits.storageMaxSize &&
323
+ storageSize > this._config.limits.storageMaxSize
324
+ ) {
322
325
  throw new Error("Storage has exceeded the size limit");
323
326
  }
324
327
 
325
328
  platform.persistence.setStorageState(room, encodedState).then(() => {
326
- this._getListeners().onStorageUpdated({
329
+ listeners.onStorageUpdated({
327
330
  context,
328
331
  encodedState,
329
332
  platform,
@@ -342,24 +345,19 @@ export class PluvServer<
342
345
 
343
346
  private get _router(): PluvRouter<TPlatform, TAuthorize, TContext, TEvents> {
344
347
  return (
345
- this._inputRouter
346
- ? PluvRouter.merge(this._baseRouter, this._inputRouter)
348
+ this._config.router
349
+ ? PluvRouter.merge(this._baseRouter, this._config.router)
347
350
  : this._baseRouter
348
351
  ) as PluvRouter<TPlatform, TAuthorize, TContext, TEvents>;
349
352
  }
350
353
 
351
354
  constructor(options: PluvServerConfig<TPlatform, TAuthorize, TContext, TEvents>) {
352
- const {
353
- authorize,
354
- context,
355
- crdt = noop,
356
- debug = false,
357
- getInitialStorage,
358
- io,
359
- limits,
360
- platform,
361
- router = new PluvRouter<TPlatform, TAuthorize, TContext, TEvents>({} as TEvents),
362
- } = options;
355
+ this._config = {
356
+ crdt: noop,
357
+ debug: false,
358
+ router: new PluvRouter<TPlatform, TAuthorize, TContext, TEvents>({} as TEvents),
359
+ ...options,
360
+ } as NonNilProps<PluvServerConfig<TPlatform, TAuthorize, TContext, TEvents>>;
363
361
 
364
362
  const {
365
363
  onRoomDeleted,
@@ -369,19 +367,6 @@ export class PluvServer<
369
367
  onUserDisconnected,
370
368
  } = options as Partial<BasePluvIOListeners<TPlatform, TAuthorize, TContext, TEvents>>;
371
369
 
372
- platform().validateConfig(options);
373
-
374
- this._crdt = crdt;
375
- this._debug = debug;
376
- this._inputRouter = router;
377
- this._io = io;
378
- this._limits = limits;
379
- this._platform = platform;
380
-
381
- if (authorize) this._authorize = authorize;
382
- if (context) this._context = context;
383
- if (getInitialStorage) this._getInitialStorage = getInitialStorage;
384
-
385
370
  (this as any)._listeners = {
386
371
  onRoomDeleted: (event) => onRoomDeleted?.(event),
387
372
  onRoomMessage: (event) => onRoomMessage?.(event),
@@ -398,7 +383,9 @@ export class PluvServer<
398
383
  const { _meta, debug, onDestroy, onMessage, ...platformRoomContext } = (options[0] ??
399
384
  {}) as CreateRoomOptions<TPlatform, TAuthorize, TContext, TEvents>[0] & { _meta?: any };
400
385
 
401
- const platform = this._platform();
386
+ const platform = this._config.platform();
387
+
388
+ platform.validateConfig(this._config);
402
389
 
403
390
  if (platform._config.handleMode !== "io") {
404
391
  throw new Error(`\`createRoom\` is unsupported for \`${platform._name}\``);
@@ -409,16 +396,18 @@ export class PluvServer<
409
396
 
410
397
  const roomContext = platformRoomContext as InferRoomContextType<TPlatform>;
411
398
  const context: TContext =
412
- typeof this._context === "function" ? this._context(roomContext) : this._context;
399
+ typeof this._config.context === "function"
400
+ ? this._config.context(roomContext)
401
+ : this._config.context;
413
402
  const listeners = this._getListeners();
414
403
  const logDebug = this._logDebug.bind(this);
415
404
 
416
405
  const newRoom = new IORoom<TPlatform, TAuthorize, TContext, TEvents>(room, {
417
406
  ...(!!_meta ? { _meta } : {}),
418
- authorize: this._authorize ?? undefined,
407
+ authorize: this._config.authorize ?? undefined,
419
408
  context,
420
- crdt: this._crdt,
421
- debug: debug ?? this._debug,
409
+ crdt: this._config.crdt,
410
+ debug: debug ?? this._config.debug,
422
411
  async onDestroy(event) {
423
412
  logDebug(`${colors.blue("Deleting empty room:")} ${room}`);
424
413
 
@@ -451,7 +440,7 @@ export class PluvServer<
451
440
  public async createToken(
452
441
  params: JWTEncodeParams<InferIOAuthorizeUser<InferIOAuthorize<this>>, TPlatform>,
453
442
  ): Promise<string> {
454
- return await this._io.createToken(params);
443
+ return await this._config.io.createToken(params);
455
444
  }
456
445
 
457
446
  private _getListeners(): BasePluvIOListeners<TPlatform, TAuthorize, TContext, TEvents> {
@@ -459,6 +448,6 @@ export class PluvServer<
459
448
  }
460
449
 
461
450
  private _logDebug(...data: any[]): void {
462
- if (this._debug) console.log(...data);
451
+ if (this._config.debug) console.log(...data);
463
452
  }
464
453
  }
@@ -1,2 +1,2 @@
1
1
  // Generated by genversion.
2
- export const version = '2.2.0'
2
+ export const version = '2.2.2'