@pluv/io 0.3.7 → 0.3.9

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.7 build /home/runner/work/pluv/pluv/packages/io
2
+ > @pluv/io@0.3.9 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
- CJS dist/index.js 29.32 KB
12
- CJS ⚡️ Build success in 63ms
13
- ESM dist/index.mjs 27.26 KB
14
- ESM ⚡️ Build success in 79ms
11
+ CJS dist/index.js 29.45 KB
12
+ CJS ⚡️ Build success in 105ms
13
+ ESM dist/index.mjs 27.39 KB
14
+ ESM ⚡️ Build success in 107ms
15
15
  DTS Build start
16
- DTS ⚡️ Build success in 2692ms
17
- DTS dist/index.d.ts 12.82 KB
16
+ DTS ⚡️ Build success in 2633ms
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.9
4
+
5
+ ### Patch Changes
6
+
7
+ - abb3622: fixed @pluv/io yjs doc becoming unresponsive when the last user leaves on a cloudflare worker
8
+
9
+ ## 0.3.8
10
+
11
+ ### Patch Changes
12
+
13
+ - bcf1c3e: purge empty rooms before joining a new room
14
+
3
15
  ## 0.3.7
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;
@@ -256,6 +256,7 @@ declare class PluvIO<TPlatform extends AbstractPlatform<any> = AbstractPlatform<
256
256
  getRoom(room: string, options?: GetRoomOptions): IORoom<TPlatform, TAuthorize, TContext, TInput, TOutput>;
257
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>]>>;
258
258
  private _logDebug;
259
+ private _purgeEmptyRooms;
259
260
  }
260
261
 
261
262
  type CreateIOParams<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TContext extends JsonObject = {}, TAuthorizeUser extends BaseUser = BaseUser, TAuthorizeRequired extends boolean = false> = Partial<PluvIOListeners> & {
package/dist/index.js CHANGED
@@ -323,7 +323,9 @@ var IORoom = class {
323
323
  this._platform = platform;
324
324
  this.id = id;
325
325
  this._listeners = {
326
- onDestroy: (encodedState) => onDestroy == null ? void 0 : onDestroy(encodedState)
326
+ onDestroy: (encodedState) => {
327
+ onDestroy == null ? void 0 : onDestroy(encodedState);
328
+ }
327
329
  };
328
330
  if (authorize2)
329
331
  this._authorize = authorize2;
@@ -334,6 +336,14 @@ var IORoom = class {
334
336
  const message = { type: event, data };
335
337
  return Promise.resolve(this._broadcast({ message }));
336
338
  }
339
+ getSize() {
340
+ const currentTime = (/* @__PURE__ */ new Date()).getTime();
341
+ return Array.from(this._sessions.values()).reduce((count, session) => {
342
+ if (session.quit)
343
+ return count;
344
+ return currentTime - session.timers.ping > PING_TIMEOUT_MS ? count : count + 1;
345
+ }, 0);
346
+ }
337
347
  register(webSocket, options) {
338
348
  return __async(this, null, function* () {
339
349
  var _a;
@@ -465,7 +475,7 @@ var IORoom = class {
465
475
  senderId: session.id
466
476
  });
467
477
  uninitializeWs();
468
- const size2 = this._getSessionsSize();
478
+ const size2 = this.getSize();
469
479
  this._logDebug(
470
480
  `${import_kleur.default.blue(
471
481
  `Unregistered connection for room ${this.id}:`
@@ -486,7 +496,7 @@ var IORoom = class {
486
496
  `Registered connection for room ${this.id}:`
487
497
  )} ${sessionId}`
488
498
  );
489
- const size = this._getSessionsSize();
499
+ const size = this.getSize();
490
500
  this._logDebug(`${import_kleur.default.blue(`Room ${this.id} size:`)} ${size}`);
491
501
  });
492
502
  }
@@ -590,14 +600,6 @@ var IORoom = class {
590
600
  }
591
601
  });
592
602
  }
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
603
  _getEventConfig(message) {
602
604
  var _a;
603
605
  return (_a = this._events[message.type]) != null ? _a : null;
@@ -626,6 +628,7 @@ var IORoom = class {
626
628
  this._doc.destroy();
627
629
  this._doc = (0, import_crdt_yjs.doc)();
628
630
  this._listeners.onDestroy(encodedState);
631
+ this._uninitialize = null;
629
632
  });
630
633
  });
631
634
  }
@@ -864,6 +867,7 @@ var PluvIO = class {
864
867
  getRoom(room, options = {}) {
865
868
  var _a;
866
869
  const { debug } = options;
870
+ this._purgeEmptyRooms();
867
871
  if (!/^[a-z0-9]+[a-z0-9\-_]+[a-z0-9]+$/i.test(room)) {
868
872
  throw new Error("Unsupported room name");
869
873
  }
@@ -885,6 +889,7 @@ var PluvIO = class {
885
889
  `${import_kleur2.default.blue("Deleting empty room:")} ${room}`
886
890
  );
887
891
  this._rooms.delete(room);
892
+ this._listeners.onRoomDeleted(room, encodedState);
888
893
  if (this._debug) {
889
894
  const rooms = Array.from(this._rooms.keys());
890
895
  this._logDebug(`${import_kleur2.default.blue("Deleted room:")} ${room}`);
@@ -892,11 +897,6 @@ var PluvIO = class {
892
897
  `${import_kleur2.default.blue("Rooms available:")} ${rooms.join(", ")}`
893
898
  );
894
899
  }
895
- this._platform.persistance.getSize(room).then((size) => {
896
- if (size)
897
- return;
898
- this._listeners.onRoomDeleted(room, encodedState);
899
- });
900
900
  },
901
901
  platform
902
902
  });
@@ -928,6 +928,14 @@ var PluvIO = class {
928
928
  _logDebug(...data) {
929
929
  this._debug && console.log(...data);
930
930
  }
931
+ _purgeEmptyRooms() {
932
+ const rooms = Array.from(this._rooms.entries());
933
+ rooms.forEach(([id, room]) => {
934
+ if (room.getSize())
935
+ return;
936
+ this._rooms.delete(id);
937
+ });
938
+ }
931
939
  };
932
940
 
933
941
  // src/createIO.ts
package/dist/index.mjs CHANGED
@@ -286,7 +286,9 @@ var IORoom = class {
286
286
  this._platform = platform;
287
287
  this.id = id;
288
288
  this._listeners = {
289
- onDestroy: (encodedState) => onDestroy == null ? void 0 : onDestroy(encodedState)
289
+ onDestroy: (encodedState) => {
290
+ onDestroy == null ? void 0 : onDestroy(encodedState);
291
+ }
290
292
  };
291
293
  if (authorize2)
292
294
  this._authorize = authorize2;
@@ -297,6 +299,14 @@ var IORoom = class {
297
299
  const message = { type: event, data };
298
300
  return Promise.resolve(this._broadcast({ message }));
299
301
  }
302
+ getSize() {
303
+ const currentTime = (/* @__PURE__ */ new Date()).getTime();
304
+ return Array.from(this._sessions.values()).reduce((count, session) => {
305
+ if (session.quit)
306
+ return count;
307
+ return currentTime - session.timers.ping > PING_TIMEOUT_MS ? count : count + 1;
308
+ }, 0);
309
+ }
300
310
  register(webSocket, options) {
301
311
  return __async(this, null, function* () {
302
312
  var _a;
@@ -428,7 +438,7 @@ var IORoom = class {
428
438
  senderId: session.id
429
439
  });
430
440
  uninitializeWs();
431
- const size2 = this._getSessionsSize();
441
+ const size2 = this.getSize();
432
442
  this._logDebug(
433
443
  `${colors.blue(
434
444
  `Unregistered connection for room ${this.id}:`
@@ -449,7 +459,7 @@ var IORoom = class {
449
459
  `Registered connection for room ${this.id}:`
450
460
  )} ${sessionId}`
451
461
  );
452
- const size = this._getSessionsSize();
462
+ const size = this.getSize();
453
463
  this._logDebug(`${colors.blue(`Room ${this.id} size:`)} ${size}`);
454
464
  });
455
465
  }
@@ -553,14 +563,6 @@ var IORoom = class {
553
563
  }
554
564
  });
555
565
  }
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
566
  _getEventConfig(message) {
565
567
  var _a;
566
568
  return (_a = this._events[message.type]) != null ? _a : null;
@@ -589,6 +591,7 @@ var IORoom = class {
589
591
  this._doc.destroy();
590
592
  this._doc = doc();
591
593
  this._listeners.onDestroy(encodedState);
594
+ this._uninitialize = null;
592
595
  });
593
596
  });
594
597
  }
@@ -827,6 +830,7 @@ var PluvIO = class {
827
830
  getRoom(room, options = {}) {
828
831
  var _a;
829
832
  const { debug } = options;
833
+ this._purgeEmptyRooms();
830
834
  if (!/^[a-z0-9]+[a-z0-9\-_]+[a-z0-9]+$/i.test(room)) {
831
835
  throw new Error("Unsupported room name");
832
836
  }
@@ -848,6 +852,7 @@ var PluvIO = class {
848
852
  `${colors2.blue("Deleting empty room:")} ${room}`
849
853
  );
850
854
  this._rooms.delete(room);
855
+ this._listeners.onRoomDeleted(room, encodedState);
851
856
  if (this._debug) {
852
857
  const rooms = Array.from(this._rooms.keys());
853
858
  this._logDebug(`${colors2.blue("Deleted room:")} ${room}`);
@@ -855,11 +860,6 @@ var PluvIO = class {
855
860
  `${colors2.blue("Rooms available:")} ${rooms.join(", ")}`
856
861
  );
857
862
  }
858
- this._platform.persistance.getSize(room).then((size) => {
859
- if (size)
860
- return;
861
- this._listeners.onRoomDeleted(room, encodedState);
862
- });
863
863
  },
864
864
  platform
865
865
  });
@@ -891,6 +891,14 @@ var PluvIO = class {
891
891
  _logDebug(...data) {
892
892
  this._debug && console.log(...data);
893
893
  }
894
+ _purgeEmptyRooms() {
895
+ const rooms = Array.from(this._rooms.entries());
896
+ rooms.forEach(([id, room]) => {
897
+ if (room.getSize())
898
+ return;
899
+ this._rooms.delete(id);
900
+ });
901
+ }
894
902
  };
895
903
 
896
904
  // src/createIO.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pluv/io",
3
- "version": "0.3.7",
3
+ "version": "0.3.9",
4
4
  "description": "@pluv/io server-side websocket manager",
5
5
  "author": "leedavidcs",
6
6
  "license": "MIT",
package/src/IORoom.ts CHANGED
@@ -110,7 +110,9 @@ export class IORoom<
110
110
  this.id = id;
111
111
 
112
112
  this._listeners = {
113
- onDestroy: (encodedState) => onDestroy?.(encodedState),
113
+ onDestroy: (encodedState) => {
114
+ onDestroy?.(encodedState);
115
+ },
114
116
  };
115
117
 
116
118
  if (authorize) this._authorize = authorize;
@@ -126,6 +128,24 @@ export class IORoom<
126
128
  return Promise.resolve(this._broadcast({ message }));
127
129
  }
128
130
 
131
+ public getSize(): number {
132
+ const currentTime = new Date().getTime();
133
+
134
+ /**
135
+ * @description Doing this instead of .size because some sessions
136
+ * in the map can be considered as "omitted".
137
+ * @author leedavidcs
138
+ * @date December 21, 2022
139
+ */
140
+ return Array.from(this._sessions.values()).reduce((count, session) => {
141
+ if (session.quit) return count;
142
+
143
+ return currentTime - session.timers.ping > PING_TIMEOUT_MS
144
+ ? count
145
+ : count + 1;
146
+ }, 0);
147
+ }
148
+
129
149
  public async register(
130
150
  webSocket: InferWebSocketType<TPlatform>,
131
151
  options?: WebsocketRegisterOptions
@@ -285,7 +305,7 @@ export class IORoom<
285
305
 
286
306
  uninitializeWs();
287
307
 
288
- const size = this._getSessionsSize();
308
+ const size = this.getSize();
289
309
 
290
310
  this._logDebug(
291
311
  `${colors.blue(
@@ -311,7 +331,7 @@ export class IORoom<
311
331
  )} ${sessionId}`
312
332
  );
313
333
 
314
- const size = this._getSessionsSize();
334
+ const size = this.getSize();
315
335
 
316
336
  this._logDebug(`${colors.blue(`Room ${this.id} size:`)} ${size}`);
317
337
  }
@@ -439,24 +459,6 @@ export class IORoom<
439
459
  }
440
460
  }
441
461
 
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
462
  private _getEventConfig(
461
463
  message: EventMessage<string, any>
462
464
  ): InferEventConfig<TContext, TInput, TOutput>[keyof TInput] | null {
@@ -493,6 +495,8 @@ export class IORoom<
493
495
  this._doc = doc();
494
496
 
495
497
  this._listeners.onDestroy(encodedState);
498
+
499
+ this._uninitialize = null;
496
500
  };
497
501
  }
498
502
 
package/src/PluvIO.ts CHANGED
@@ -262,6 +262,8 @@ export class PluvIO<
262
262
  ): IORoom<TPlatform, TAuthorize, TContext, TInput, TOutput> {
263
263
  const { debug } = options;
264
264
 
265
+ this._purgeEmptyRooms();
266
+
265
267
  if (!/^[a-z0-9]+[a-z0-9\-_]+[a-z0-9]+$/i.test(room)) {
266
268
  throw new Error("Unsupported room name");
267
269
  }
@@ -289,6 +291,7 @@ export class PluvIO<
289
291
  );
290
292
 
291
293
  this._rooms.delete(room);
294
+ this._listeners.onRoomDeleted(room, encodedState);
292
295
 
293
296
  if (this._debug) {
294
297
  const rooms = Array.from(this._rooms.keys());
@@ -298,12 +301,6 @@ export class PluvIO<
298
301
  `${colors.blue("Rooms available:")} ${rooms.join(", ")}`
299
302
  );
300
303
  }
301
-
302
- this._platform.persistance.getSize(room).then((size) => {
303
- if (size) return;
304
-
305
- this._listeners.onRoomDeleted(room, encodedState);
306
- });
307
304
  },
308
305
  platform,
309
306
  });
@@ -353,4 +350,14 @@ export class PluvIO<
353
350
  private _logDebug(...data: any[]): void {
354
351
  this._debug && console.log(...data);
355
352
  }
353
+
354
+ private _purgeEmptyRooms(): void {
355
+ const rooms = Array.from(this._rooms.entries());
356
+
357
+ rooms.forEach(([id, room]) => {
358
+ if (room.getSize()) return;
359
+
360
+ this._rooms.delete(id);
361
+ });
362
+ }
356
363
  }