@pluv/io 0.3.6 → 0.3.8

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.
@@ -1,5 +1,5 @@
1
1
 
2
- > @pluv/io@0.3.6 build /home/runner/work/pluv/pluv/packages/io
2
+ > @pluv/io@0.3.8 build /home/runner/work/pluv/pluv/packages/io
3
3
  > tsup src/index.ts --format esm,cjs --dts
4
4
 
5
5
  CLI Building entry: src/index.ts
@@ -8,10 +8,10 @@
8
8
  CLI Target: es6
9
9
  ESM Build start
10
10
  CJS Build start
11
- ESM dist/index.mjs 27.19 KB
12
- ESM ⚡️ Build success in 112ms
13
- CJS dist/index.js 29.25 KB
14
- CJS ⚡️ Build success in 113ms
11
+ CJS dist/index.js 29.51 KB
12
+ CJS ⚡️ Build success in 109ms
13
+ ESM dist/index.mjs 27.45 KB
14
+ ESM ⚡️ Build success in 118ms
15
15
  DTS Build start
16
- DTS ⚡️ Build success in 2971ms
17
- DTS dist/index.d.ts 12.73 KB
16
+ DTS ⚡️ Build success in 2905ms
17
+ DTS dist/index.d.ts 12.84 KB
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @pluv/io
2
2
 
3
+ ## 0.3.8
4
+
5
+ ### Patch Changes
6
+
7
+ - bcf1c3e: purge empty rooms before joining a new room
8
+
9
+ ## 0.3.7
10
+
11
+ ### Patch Changes
12
+
13
+ - ecc4040: added ability to set debug when room is created
14
+
3
15
  ## 0.3.6
4
16
 
5
17
  ### Patch Changes
package/dist/index.d.ts CHANGED
@@ -204,6 +204,7 @@ declare class IORoom<TPlatform extends AbstractPlatform<any> = AbstractPlatform<
204
204
  readonly id: string;
205
205
  constructor(id: string, config: IORoomConfig<TPlatform, TAuthorize, TContext, TInput, TOutput>);
206
206
  broadcast<TEvent extends keyof InferIOInput<this>>(event: TEvent, data: Id<InferIOInput<this>[TEvent]>): Promise<void>;
207
+ getSize(): number;
207
208
  register(webSocket: InferWebSocketType<TPlatform>, options?: WebsocketRegisterOptions): Promise<void>;
208
209
  private _broadcast;
209
210
  private _emitQuitters;
@@ -211,7 +212,6 @@ declare class IORoom<TPlatform extends AbstractPlatform<any> = AbstractPlatform<
211
212
  private _emitStorageReceived;
212
213
  private _emitUserJoined;
213
214
  private _getAuthorizedUser;
214
- private _getSessionsSize;
215
215
  private _getEventConfig;
216
216
  private _initialize;
217
217
  private _logDebug;
@@ -238,6 +238,9 @@ type PluvIOConfig<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any
238
238
  initialStorage?: (room: string) => MaybePromise<Maybe<string>>;
239
239
  platform: TPlatform;
240
240
  };
241
+ interface GetRoomOptions {
242
+ debug?: boolean;
243
+ }
241
244
  declare class PluvIO<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends IOAuthorize<any, any> = BaseIOAuthorize, TContext extends JsonObject = {}, TInput extends EventRecord<string, any> = {}, TOutput extends EventRecord<string, any> = {}> implements IOLike<TAuthorize, TInput, TOutput> {
242
245
  private _context;
243
246
  private _initialStorage;
@@ -250,9 +253,10 @@ declare class PluvIO<TPlatform extends AbstractPlatform<any> = AbstractPlatform<
250
253
  constructor(options: PluvIOConfig<TPlatform, TAuthorize, TContext, TInput, TOutput>);
251
254
  createToken(params: JWTEncodeParams<InferIOAuthorizeUser<InferIOAuthorize<this>>>): Promise<string>;
252
255
  event<TEvent extends string, TData extends JsonObject = {}, TResult extends EventRecord<string, any> = {}>(event: TEvent, config: EventConfig<TContext, TData, TResult>): PluvIO<TPlatform, TAuthorize, TContext, Spread<[TInput, EventRecord<TEvent, TData>]>, Spread<[TOutput, TResult]>>;
253
- getRoom(room: string): IORoom<TPlatform, TAuthorize, TContext, TInput, TOutput>;
256
+ getRoom(room: string, options?: GetRoomOptions): IORoom<TPlatform, TAuthorize, TContext, TInput, TOutput>;
254
257
  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<[InferIOOutput<TIO1>, InferIOOutput<TIO2>]>>;
255
258
  private _logDebug;
259
+ private _purgeEmptyRooms;
256
260
  }
257
261
 
258
262
  type CreateIOParams<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TContext extends JsonObject = {}, TAuthorizeUser extends BaseUser = BaseUser, TAuthorizeRequired extends boolean = false> = Partial<PluvIOListeners> & {
@@ -264,4 +268,4 @@ type CreateIOParams<TPlatform extends AbstractPlatform<any> = AbstractPlatform<a
264
268
  };
265
269
  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>>>;
266
270
 
267
- export { AbstractCloseEvent, AbstractErrorEvent, AbstractEvent, AbstractEventMap, AbstractListener, AbstractMessageEvent, AbstractPersistance, AbstractPlatform, AbstractPubSub, AbstractWebSocket, AbstractWebSocketConfig, AbstractWebSocketHandleErrorParams, AuthorizeModule, AuthorizeParams, ConvertWebSocketConfig, CreateIOParams, IOPubSubEventMessage, IORoom, InferIORoom, InferWebSocketType, JWT, JWTEncodeParams, PluvIO, createIO };
271
+ export { AbstractCloseEvent, AbstractErrorEvent, AbstractEvent, AbstractEventMap, AbstractListener, AbstractMessageEvent, AbstractPersistance, AbstractPlatform, AbstractPubSub, AbstractWebSocket, AbstractWebSocketConfig, AbstractWebSocketHandleErrorParams, AuthorizeModule, AuthorizeParams, ConvertWebSocketConfig, CreateIOParams, GetRoomOptions, IOPubSubEventMessage, IORoom, InferIORoom, InferWebSocketType, JWT, JWTEncodeParams, PluvIO, createIO };
package/dist/index.js CHANGED
@@ -334,6 +334,14 @@ var IORoom = class {
334
334
  const message = { type: event, data };
335
335
  return Promise.resolve(this._broadcast({ message }));
336
336
  }
337
+ getSize() {
338
+ const currentTime = (/* @__PURE__ */ new Date()).getTime();
339
+ return Array.from(this._sessions.values()).reduce((count, session) => {
340
+ if (session.quit)
341
+ return count;
342
+ return currentTime - session.timers.ping > PING_TIMEOUT_MS ? count : count + 1;
343
+ }, 0);
344
+ }
337
345
  register(webSocket, options) {
338
346
  return __async(this, null, function* () {
339
347
  var _a;
@@ -465,7 +473,7 @@ var IORoom = class {
465
473
  senderId: session.id
466
474
  });
467
475
  uninitializeWs();
468
- const size2 = this._getSessionsSize();
476
+ const size2 = this.getSize();
469
477
  this._logDebug(
470
478
  `${import_kleur.default.blue(
471
479
  `Unregistered connection for room ${this.id}:`
@@ -486,7 +494,7 @@ var IORoom = class {
486
494
  `Registered connection for room ${this.id}:`
487
495
  )} ${sessionId}`
488
496
  );
489
- const size = this._getSessionsSize();
497
+ const size = this.getSize();
490
498
  this._logDebug(`${import_kleur.default.blue(`Room ${this.id} size:`)} ${size}`);
491
499
  });
492
500
  }
@@ -590,14 +598,6 @@ var IORoom = class {
590
598
  }
591
599
  });
592
600
  }
593
- _getSessionsSize() {
594
- const currentTime = (/* @__PURE__ */ new Date()).getTime();
595
- return Array.from(this._sessions.values()).reduce((count, session) => {
596
- if (session.quit)
597
- return count;
598
- return currentTime - session.timers.ping > PING_TIMEOUT_MS ? count : count + 1;
599
- }, 0);
600
- }
601
601
  _getEventConfig(message) {
602
602
  var _a;
603
603
  return (_a = this._events[message.type]) != null ? _a : null;
@@ -861,8 +861,10 @@ var PluvIO = class {
861
861
  })
862
862
  );
863
863
  }
864
- getRoom(room) {
864
+ getRoom(room, options = {}) {
865
865
  var _a;
866
+ const { debug } = options;
867
+ this._purgeEmptyRooms();
866
868
  if (!/^[a-z0-9]+[a-z0-9\-_]+[a-z0-9]+$/i.test(room)) {
867
869
  throw new Error("Unsupported room name");
868
870
  }
@@ -873,7 +875,7 @@ var PluvIO = class {
873
875
  const newRoom = new IORoom(room, {
874
876
  authorize: (_a = this._authorize) != null ? _a : void 0,
875
877
  context: this._context,
876
- debug: this._debug,
878
+ debug: debug != null ? debug : this._debug,
877
879
  events: this._events,
878
880
  initialStorage: () => {
879
881
  var _a2;
@@ -927,6 +929,14 @@ var PluvIO = class {
927
929
  _logDebug(...data) {
928
930
  this._debug && console.log(...data);
929
931
  }
932
+ _purgeEmptyRooms() {
933
+ const rooms = Array.from(this._rooms.entries());
934
+ rooms.forEach(([id, room]) => {
935
+ if (room.getSize())
936
+ return;
937
+ this._rooms.delete(id);
938
+ });
939
+ }
930
940
  };
931
941
 
932
942
  // src/createIO.ts
package/dist/index.mjs CHANGED
@@ -297,6 +297,14 @@ var IORoom = class {
297
297
  const message = { type: event, data };
298
298
  return Promise.resolve(this._broadcast({ message }));
299
299
  }
300
+ getSize() {
301
+ const currentTime = (/* @__PURE__ */ new Date()).getTime();
302
+ return Array.from(this._sessions.values()).reduce((count, session) => {
303
+ if (session.quit)
304
+ return count;
305
+ return currentTime - session.timers.ping > PING_TIMEOUT_MS ? count : count + 1;
306
+ }, 0);
307
+ }
300
308
  register(webSocket, options) {
301
309
  return __async(this, null, function* () {
302
310
  var _a;
@@ -428,7 +436,7 @@ var IORoom = class {
428
436
  senderId: session.id
429
437
  });
430
438
  uninitializeWs();
431
- const size2 = this._getSessionsSize();
439
+ const size2 = this.getSize();
432
440
  this._logDebug(
433
441
  `${colors.blue(
434
442
  `Unregistered connection for room ${this.id}:`
@@ -449,7 +457,7 @@ var IORoom = class {
449
457
  `Registered connection for room ${this.id}:`
450
458
  )} ${sessionId}`
451
459
  );
452
- const size = this._getSessionsSize();
460
+ const size = this.getSize();
453
461
  this._logDebug(`${colors.blue(`Room ${this.id} size:`)} ${size}`);
454
462
  });
455
463
  }
@@ -553,14 +561,6 @@ var IORoom = class {
553
561
  }
554
562
  });
555
563
  }
556
- _getSessionsSize() {
557
- const currentTime = (/* @__PURE__ */ new Date()).getTime();
558
- return Array.from(this._sessions.values()).reduce((count, session) => {
559
- if (session.quit)
560
- return count;
561
- return currentTime - session.timers.ping > PING_TIMEOUT_MS ? count : count + 1;
562
- }, 0);
563
- }
564
564
  _getEventConfig(message) {
565
565
  var _a;
566
566
  return (_a = this._events[message.type]) != null ? _a : null;
@@ -824,8 +824,10 @@ var PluvIO = class {
824
824
  })
825
825
  );
826
826
  }
827
- getRoom(room) {
827
+ getRoom(room, options = {}) {
828
828
  var _a;
829
+ const { debug } = options;
830
+ this._purgeEmptyRooms();
829
831
  if (!/^[a-z0-9]+[a-z0-9\-_]+[a-z0-9]+$/i.test(room)) {
830
832
  throw new Error("Unsupported room name");
831
833
  }
@@ -836,7 +838,7 @@ var PluvIO = class {
836
838
  const newRoom = new IORoom(room, {
837
839
  authorize: (_a = this._authorize) != null ? _a : void 0,
838
840
  context: this._context,
839
- debug: this._debug,
841
+ debug: debug != null ? debug : this._debug,
840
842
  events: this._events,
841
843
  initialStorage: () => {
842
844
  var _a2;
@@ -890,6 +892,14 @@ var PluvIO = class {
890
892
  _logDebug(...data) {
891
893
  this._debug && console.log(...data);
892
894
  }
895
+ _purgeEmptyRooms() {
896
+ const rooms = Array.from(this._rooms.entries());
897
+ rooms.forEach(([id, room]) => {
898
+ if (room.getSize())
899
+ return;
900
+ this._rooms.delete(id);
901
+ });
902
+ }
893
903
  };
894
904
 
895
905
  // src/createIO.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pluv/io",
3
- "version": "0.3.6",
3
+ "version": "0.3.8",
4
4
  "description": "@pluv/io server-side websocket manager",
5
5
  "author": "leedavidcs",
6
6
  "license": "MIT",
package/src/IORoom.ts CHANGED
@@ -126,6 +126,24 @@ export class IORoom<
126
126
  return Promise.resolve(this._broadcast({ message }));
127
127
  }
128
128
 
129
+ public getSize(): number {
130
+ const currentTime = new Date().getTime();
131
+
132
+ /**
133
+ * @description Doing this instead of .size because some sessions
134
+ * in the map can be considered as "omitted".
135
+ * @author leedavidcs
136
+ * @date December 21, 2022
137
+ */
138
+ return Array.from(this._sessions.values()).reduce((count, session) => {
139
+ if (session.quit) return count;
140
+
141
+ return currentTime - session.timers.ping > PING_TIMEOUT_MS
142
+ ? count
143
+ : count + 1;
144
+ }, 0);
145
+ }
146
+
129
147
  public async register(
130
148
  webSocket: InferWebSocketType<TPlatform>,
131
149
  options?: WebsocketRegisterOptions
@@ -285,7 +303,7 @@ export class IORoom<
285
303
 
286
304
  uninitializeWs();
287
305
 
288
- const size = this._getSessionsSize();
306
+ const size = this.getSize();
289
307
 
290
308
  this._logDebug(
291
309
  `${colors.blue(
@@ -311,7 +329,7 @@ export class IORoom<
311
329
  )} ${sessionId}`
312
330
  );
313
331
 
314
- const size = this._getSessionsSize();
332
+ const size = this.getSize();
315
333
 
316
334
  this._logDebug(`${colors.blue(`Room ${this.id} size:`)} ${size}`);
317
335
  }
@@ -439,24 +457,6 @@ export class IORoom<
439
457
  }
440
458
  }
441
459
 
442
- private _getSessionsSize(): number {
443
- const currentTime = new Date().getTime();
444
-
445
- /**
446
- * @description Doing this instead of .size because some sessions
447
- * in the map can be considered as "omitted".
448
- * @author leedavidcs
449
- * @date December 21, 2022
450
- */
451
- return Array.from(this._sessions.values()).reduce((count, session) => {
452
- if (session.quit) return count;
453
-
454
- return currentTime - session.timers.ping > PING_TIMEOUT_MS
455
- ? count
456
- : count + 1;
457
- }, 0);
458
- }
459
-
460
460
  private _getEventConfig(
461
461
  message: EventMessage<string, any>
462
462
  ): InferEventConfig<TContext, TInput, TOutput>[keyof TInput] | null {
package/src/PluvIO.ts CHANGED
@@ -60,6 +60,10 @@ export type PluvIOConfig<
60
60
  platform: TPlatform;
61
61
  };
62
62
 
63
+ export interface GetRoomOptions {
64
+ debug?: boolean;
65
+ }
66
+
63
67
  export class PluvIO<
64
68
  TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>,
65
69
  TAuthorize extends IOAuthorize<any, any> = BaseIOAuthorize,
@@ -253,8 +257,13 @@ export class PluvIO<
253
257
  }
254
258
 
255
259
  public getRoom(
256
- room: string
260
+ room: string,
261
+ options: GetRoomOptions = {}
257
262
  ): IORoom<TPlatform, TAuthorize, TContext, TInput, TOutput> {
263
+ const { debug } = options;
264
+
265
+ this._purgeEmptyRooms();
266
+
258
267
  if (!/^[a-z0-9]+[a-z0-9\-_]+[a-z0-9]+$/i.test(room)) {
259
268
  throw new Error("Unsupported room name");
260
269
  }
@@ -273,7 +282,7 @@ export class PluvIO<
273
282
  >(room, {
274
283
  authorize: this._authorize ?? undefined,
275
284
  context: this._context,
276
- debug: this._debug,
285
+ debug: debug ?? this._debug,
277
286
  events: this._events,
278
287
  initialStorage: () => this._initialStorage?.(room),
279
288
  onDestroy: (encodedState) => {
@@ -346,4 +355,14 @@ export class PluvIO<
346
355
  private _logDebug(...data: any[]): void {
347
356
  this._debug && console.log(...data);
348
357
  }
358
+
359
+ private _purgeEmptyRooms(): void {
360
+ const rooms = Array.from(this._rooms.entries());
361
+
362
+ rooms.forEach(([id, room]) => {
363
+ if (room.getSize()) return;
364
+
365
+ this._rooms.delete(id);
366
+ });
367
+ }
349
368
  }
package/src/index.ts CHANGED
@@ -32,4 +32,4 @@ export type {
32
32
  export type { CreateIOParams } from "./createIO";
33
33
  export { createIO } from "./createIO";
34
34
  export type { IORoom } from "./IORoom";
35
- export type { InferIORoom, PluvIO } from "./PluvIO";
35
+ export type { GetRoomOptions, InferIORoom, PluvIO } from "./PluvIO";