@pluv/io 0.2.5 → 0.3.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.
@@ -1,17 +1,17 @@
1
1
 
2
- > @pluv/io@0.2.5 build /home/runner/work/pluv/pluv/packages/io
2
+ > @pluv/io@0.3.0 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
6
6
  CLI Using tsconfig: tsconfig.json
7
- CLI tsup v6.6.3
7
+ CLI tsup v6.7.0
8
8
  CLI Target: es6
9
9
  ESM Build start
10
10
  CJS Build start
11
- CJS dist/index.js 29.22 KB
12
- CJS ⚡️ Build success in 119ms
13
- ESM dist/index.mjs 27.14 KB
14
- ESM ⚡️ Build success in 120ms
11
+ ESM dist/index.mjs 27.13 KB
12
+ ESM ⚡️ Build success in 83ms
13
+ CJS dist/index.js 29.21 KB
14
+ CJS ⚡️ Build success in 85ms
15
15
  DTS Build start
16
- DTS ⚡️ Build success in 2926ms
17
- DTS dist/index.d.ts 12.86 KB
16
+ DTS ⚡️ Build success in 2501ms
17
+ DTS dist/index.d.ts 12.73 KB
package/CHANGELOG.md CHANGED
@@ -1,5 +1,32 @@
1
1
  # @pluv/io
2
2
 
3
+ ## 0.3.0
4
+
5
+ ### Minor Changes
6
+
7
+ - c5567f1: renamed IORoom.room to IORoom.id
8
+ - c5567f1: updated IORoom.broadcast to have the same function signature as PluvRoom.broadcast
9
+
10
+ ### Patch Changes
11
+
12
+ - @pluv/crdt-yjs@0.3.2
13
+ - @pluv/types@0.1.4
14
+
15
+ ## 0.2.6
16
+
17
+ ### Patch Changes
18
+
19
+ - 9516a4e: updated typescript to 5.0
20
+ - 7b6da1c: bumped dependencies
21
+ - e9c1514: bumped dependencies
22
+ - 9d1829c: chore: bumped dependencies
23
+ - Updated dependencies [9516a4e]
24
+ - Updated dependencies [7b6da1c]
25
+ - Updated dependencies [e9c1514]
26
+ - Updated dependencies [9d1829c]
27
+ - @pluv/crdt-yjs@0.3.2
28
+ - @pluv/types@0.1.4
29
+
3
30
  ## 0.2.5
4
31
 
5
32
  ### Patch Changes
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { JsonObject, MaybePromise, IOEventMessage, EventRecord, InputZodLike, Id, IOLike, InferIOOutput, BaseUser, IOAuthorize, BaseIOAuthorize, InferEventMessage, InferIOInput, BaseIOEventRecord, InferIOAuthorize, Maybe, InferIOAuthorizeUser, Spread, BaseClientEventRecord } from '@pluv/types';
1
+ import { JsonObject, MaybePromise, IOEventMessage, EventRecord, InputZodLike, Id, IOLike, InferIOOutput, BaseUser, IOAuthorize, BaseIOAuthorize, InferIOInput, Maybe, InferIOAuthorizeUser, InferIOAuthorize, Spread, 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
 
@@ -176,7 +176,6 @@ interface AuthorizeModule {
176
176
  encode: <TUser extends BaseUser>(params: JWTEncodeParams<TUser>) => Promise<string>;
177
177
  }
178
178
 
179
- type BroadcastMessage<TIO extends IORoom<any, any, any, any, any>> = InferEventMessage<InferIOInput<TIO>> | InferEventMessage<BaseIOEventRecord<InferIOAuthorize<TIO>>>;
180
179
  interface IORoomListeners {
181
180
  onDestroy: (encodedState: string) => void;
182
181
  }
@@ -202,9 +201,9 @@ declare class IORoom<TPlatform extends AbstractPlatform<any> = AbstractPlatform<
202
201
  private _uninitialize;
203
202
  readonly _authorize: TAuthorize | null;
204
203
  readonly _events: InferEventConfig<TContext, TInput, TOutput>;
205
- readonly room: string;
206
- constructor(room: string, config: IORoomConfig<TPlatform, TAuthorize, TContext, TInput, TOutput>);
207
- broadcast(message: BroadcastMessage<this>): Promise<void>;
204
+ readonly id: string;
205
+ constructor(id: string, config: IORoomConfig<TPlatform, TAuthorize, TContext, TInput, TOutput>);
206
+ broadcast<TEvent extends keyof InferIOInput<this>>(event: TEvent, data: Id<InferIOInput<this>[TEvent]>): Promise<void>;
208
207
  register(webSocket: InferWebSocketType<TPlatform>, options?: WebsocketRegisterOptions): Promise<void>;
209
208
  private _broadcast;
210
209
  private _emitQuitters;
package/dist/index.js CHANGED
@@ -302,7 +302,7 @@ var import_crdt_yjs = require("@pluv/crdt-yjs");
302
302
  var import_chalk = __toESM(require("chalk"));
303
303
  var PING_TIMEOUT_MS = 3e4;
304
304
  var IORoom = class {
305
- constructor(room, config) {
305
+ constructor(id, config) {
306
306
  this._doc = (0, import_crdt_yjs.doc)();
307
307
  this._initialStorage = null;
308
308
  this._sessions = /* @__PURE__ */ new Map();
@@ -321,7 +321,7 @@ var IORoom = class {
321
321
  this._debug = debug;
322
322
  this._events = events;
323
323
  this._platform = platform;
324
- this.room = room;
324
+ this.id = id;
325
325
  this._listeners = {
326
326
  onDestroy: (encodedState) => onDestroy == null ? void 0 : onDestroy(encodedState)
327
327
  };
@@ -330,7 +330,8 @@ var IORoom = class {
330
330
  if (initialStorage)
331
331
  this._initialStorage = initialStorage;
332
332
  }
333
- broadcast(message) {
333
+ broadcast(event, data) {
334
+ const message = { type: event, data };
334
335
  return Promise.resolve(this._broadcast({ message }));
335
336
  }
336
337
  register(webSocket, options) {
@@ -341,11 +342,11 @@ var IORoom = class {
341
342
  const token = options == null ? void 0 : options.token;
342
343
  const sessionId = this._platform.randomUUID();
343
344
  const pluvWs = this._platform.convertWebSocket(webSocket, {
344
- room: this.room
345
+ room: this.id
345
346
  });
346
347
  this._logDebug(
347
348
  `${import_chalk.default.blue(
348
- `Registering connection for room ${this.room}:`
349
+ `Registering connection for room ${this.id}:`
349
350
  )} ${sessionId}`
350
351
  );
351
352
  const uninitializeWs = yield pluvWs.initialize();
@@ -372,7 +373,7 @@ var IORoom = class {
372
373
  id: sessionId,
373
374
  presence: null,
374
375
  quit: false,
375
- room: this.room,
376
+ room: this.id,
376
377
  timers: {
377
378
  ping: currentTime
378
379
  },
@@ -381,7 +382,7 @@ var IORoom = class {
381
382
  };
382
383
  this._sessions.set(session.id, session);
383
384
  yield this._platform.persistance.addUser(
384
- this.room,
385
+ this.id,
385
386
  sessionId,
386
387
  user != null ? user : {}
387
388
  );
@@ -391,7 +392,7 @@ var IORoom = class {
391
392
  const context = {
392
393
  context: this._context,
393
394
  doc: this._doc,
394
- room: this.room,
395
+ room: this.id,
395
396
  session,
396
397
  sessions: this._sessions
397
398
  };
@@ -421,10 +422,10 @@ var IORoom = class {
421
422
  return;
422
423
  }
423
424
  case "sync": {
424
- this._platform.pubSub.publish(this.room, __spreadValues({
425
+ this._platform.pubSub.publish(this.id, __spreadValues({
425
426
  connectionId: session.id,
426
427
  options: eventConfig.options,
427
- room: this.room,
428
+ room: this.id,
428
429
  user: session.user
429
430
  }, message));
430
431
  Object.entries(output).forEach(([type, data]) => {
@@ -450,11 +451,11 @@ var IORoom = class {
450
451
  session.quit = true;
451
452
  this._logDebug(
452
453
  `${import_chalk.default.blue(
453
- `(Unregistering connection for room ${this.room}:`
454
+ `(Unregistering connection for room ${this.id}:`
454
455
  )} ${sessionId}`
455
456
  );
456
457
  this._sessions.delete(session.id);
457
- this._platform.persistance.deleteUser(this.room, session.id).finally(() => {
458
+ this._platform.persistance.deleteUser(this.id, session.id).finally(() => {
458
459
  var _a2;
459
460
  this._broadcast({
460
461
  message: {
@@ -467,11 +468,11 @@ var IORoom = class {
467
468
  const size2 = this._getSessionsSize();
468
469
  this._logDebug(
469
470
  `${import_chalk.default.blue(
470
- `Unregistered connection for room ${this.room}:`
471
+ `Unregistered connection for room ${this.id}:`
471
472
  )} ${sessionId}`
472
473
  );
473
474
  this._logDebug(
474
- `${import_chalk.default.blue(`Room ${this.room} size:`)} ${size2}`
475
+ `${import_chalk.default.blue(`Room ${this.id} size:`)} ${size2}`
475
476
  );
476
477
  if (size2)
477
478
  return;
@@ -482,11 +483,11 @@ var IORoom = class {
482
483
  pluvWs.addEventListener("error", closeHandler);
483
484
  this._logDebug(
484
485
  `${import_chalk.default.blue(
485
- `Registered connection for room ${this.room}:`
486
+ `Registered connection for room ${this.id}:`
486
487
  )} ${sessionId}`
487
488
  );
488
489
  const size = this._getSessionsSize();
489
- this._logDebug(`${import_chalk.default.blue(`Room ${this.room} size:`)} ${size}`);
490
+ this._logDebug(`${import_chalk.default.blue(`Room ${this.id} size:`)} ${size}`);
490
491
  });
491
492
  }
492
493
  _broadcast(params) {
@@ -501,9 +502,9 @@ var IORoom = class {
501
502
  this._sessions.delete(session.id);
502
503
  });
503
504
  this._emitQuitters(quitters);
504
- this._platform.pubSub.publish(this.room, __spreadValues({
505
+ this._platform.pubSub.publish(this.id, __spreadValues({
505
506
  connectionId: senderId != null ? senderId : null,
506
- room: this.room,
507
+ room: this.id,
507
508
  user: (_a = sender == null ? void 0 : sender.user) != null ? _a : null
508
509
  }, message));
509
510
  }
@@ -528,7 +529,7 @@ var IORoom = class {
528
529
  );
529
530
  }
530
531
  _emitStorageReceived(session) {
531
- this._platform.persistance.getStorageState(this.room).then((_state) => __async(this, null, function* () {
532
+ this._platform.persistance.getStorageState(this.id).then((_state) => __async(this, null, function* () {
532
533
  var _a, _b;
533
534
  const state = (_b = _state != null ? _state : yield (_a = this._initialStorage) == null ? void 0 : _a.call(this)) != null ? _b : this._doc.encodeStateAsUpdate();
534
535
  this._sendSelfMessage(
@@ -571,9 +572,9 @@ var IORoom = class {
571
572
  this._logDebug(token);
572
573
  return null;
573
574
  }
574
- if (payload.room !== this.room) {
575
+ if (payload.room !== this.id) {
575
576
  this._logDebug(
576
- import_chalk.default.blue(`Token is not authorized for room ${this.room}:`)
577
+ import_chalk.default.blue(`Token is not authorized for room ${this.id}:`)
577
578
  );
578
579
  this._logDebug(import_chalk.default.blue("Received:"), payload.room);
579
580
  this._logDebug(token);
@@ -601,13 +602,13 @@ var IORoom = class {
601
602
  }
602
603
  _initialize() {
603
604
  return __async(this, null, function* () {
604
- this._logDebug(`${import_chalk.default.blue(`Initializing room ${this.room}:`)}`);
605
+ this._logDebug(`${import_chalk.default.blue(`Initializing room ${this.id}:`)}`);
605
606
  if (!!this._uninitialize) {
606
607
  yield this._uninitialize();
607
608
  this._uninitialize = null;
608
609
  }
609
610
  const pubSubId = yield this._platform.pubSub.subscribe(
610
- this.room,
611
+ this.id,
611
612
  (_a) => {
612
613
  var _b = _a, { options = {} } = _b, message = __objRest(_b, ["options"]);
613
614
  const session = {
@@ -693,7 +694,7 @@ var IORoom = class {
693
694
  Array.from(sessions.values()).forEach((_session) => {
694
695
  _session.webSocket.sendMessage(__spreadValues({
695
696
  connectionId: senderId,
696
- room: this.room,
697
+ room: this.id,
697
698
  user: _session.user
698
699
  }, message));
699
700
  });
@@ -707,7 +708,7 @@ var IORoom = class {
707
708
  return;
708
709
  session.webSocket.sendMessage(__spreadValues({
709
710
  connectionId: sender.id,
710
- room: this.room,
711
+ room: this.id,
711
712
  user: sender.user
712
713
  }, message));
713
714
  }
@@ -718,7 +719,7 @@ var IORoom = class {
718
719
  this._resolveMessage(message, {
719
720
  context: this._context,
720
721
  doc: this._doc,
721
- room: this.room,
722
+ room: this.id,
722
723
  session: null,
723
724
  sessions: this._sessions
724
725
  }).then((output) => {
@@ -727,11 +728,11 @@ var IORoom = class {
727
728
  Object.keys(output).forEach((type) => {
728
729
  var _a, _b;
729
730
  const data = (_a = output[type]) != null ? _a : {};
730
- this._platform.pubSub.publish(this.room, {
731
+ this._platform.pubSub.publish(this.id, {
731
732
  connectionId: senderId,
732
733
  data,
733
734
  options: { type: "self" },
734
- room: this.room,
735
+ room: this.id,
735
736
  type,
736
737
  user: (_b = sender.user) != null ? _b : null
737
738
  });
package/dist/index.mjs CHANGED
@@ -265,7 +265,7 @@ import { doc } from "@pluv/crdt-yjs";
265
265
  import chalk from "chalk";
266
266
  var PING_TIMEOUT_MS = 3e4;
267
267
  var IORoom = class {
268
- constructor(room, config) {
268
+ constructor(id, config) {
269
269
  this._doc = doc();
270
270
  this._initialStorage = null;
271
271
  this._sessions = /* @__PURE__ */ new Map();
@@ -284,7 +284,7 @@ var IORoom = class {
284
284
  this._debug = debug;
285
285
  this._events = events;
286
286
  this._platform = platform;
287
- this.room = room;
287
+ this.id = id;
288
288
  this._listeners = {
289
289
  onDestroy: (encodedState) => onDestroy == null ? void 0 : onDestroy(encodedState)
290
290
  };
@@ -293,7 +293,8 @@ var IORoom = class {
293
293
  if (initialStorage)
294
294
  this._initialStorage = initialStorage;
295
295
  }
296
- broadcast(message) {
296
+ broadcast(event, data) {
297
+ const message = { type: event, data };
297
298
  return Promise.resolve(this._broadcast({ message }));
298
299
  }
299
300
  register(webSocket, options) {
@@ -304,11 +305,11 @@ var IORoom = class {
304
305
  const token = options == null ? void 0 : options.token;
305
306
  const sessionId = this._platform.randomUUID();
306
307
  const pluvWs = this._platform.convertWebSocket(webSocket, {
307
- room: this.room
308
+ room: this.id
308
309
  });
309
310
  this._logDebug(
310
311
  `${chalk.blue(
311
- `Registering connection for room ${this.room}:`
312
+ `Registering connection for room ${this.id}:`
312
313
  )} ${sessionId}`
313
314
  );
314
315
  const uninitializeWs = yield pluvWs.initialize();
@@ -335,7 +336,7 @@ var IORoom = class {
335
336
  id: sessionId,
336
337
  presence: null,
337
338
  quit: false,
338
- room: this.room,
339
+ room: this.id,
339
340
  timers: {
340
341
  ping: currentTime
341
342
  },
@@ -344,7 +345,7 @@ var IORoom = class {
344
345
  };
345
346
  this._sessions.set(session.id, session);
346
347
  yield this._platform.persistance.addUser(
347
- this.room,
348
+ this.id,
348
349
  sessionId,
349
350
  user != null ? user : {}
350
351
  );
@@ -354,7 +355,7 @@ var IORoom = class {
354
355
  const context = {
355
356
  context: this._context,
356
357
  doc: this._doc,
357
- room: this.room,
358
+ room: this.id,
358
359
  session,
359
360
  sessions: this._sessions
360
361
  };
@@ -384,10 +385,10 @@ var IORoom = class {
384
385
  return;
385
386
  }
386
387
  case "sync": {
387
- this._platform.pubSub.publish(this.room, __spreadValues({
388
+ this._platform.pubSub.publish(this.id, __spreadValues({
388
389
  connectionId: session.id,
389
390
  options: eventConfig.options,
390
- room: this.room,
391
+ room: this.id,
391
392
  user: session.user
392
393
  }, message));
393
394
  Object.entries(output).forEach(([type, data]) => {
@@ -413,11 +414,11 @@ var IORoom = class {
413
414
  session.quit = true;
414
415
  this._logDebug(
415
416
  `${chalk.blue(
416
- `(Unregistering connection for room ${this.room}:`
417
+ `(Unregistering connection for room ${this.id}:`
417
418
  )} ${sessionId}`
418
419
  );
419
420
  this._sessions.delete(session.id);
420
- this._platform.persistance.deleteUser(this.room, session.id).finally(() => {
421
+ this._platform.persistance.deleteUser(this.id, session.id).finally(() => {
421
422
  var _a2;
422
423
  this._broadcast({
423
424
  message: {
@@ -430,11 +431,11 @@ var IORoom = class {
430
431
  const size2 = this._getSessionsSize();
431
432
  this._logDebug(
432
433
  `${chalk.blue(
433
- `Unregistered connection for room ${this.room}:`
434
+ `Unregistered connection for room ${this.id}:`
434
435
  )} ${sessionId}`
435
436
  );
436
437
  this._logDebug(
437
- `${chalk.blue(`Room ${this.room} size:`)} ${size2}`
438
+ `${chalk.blue(`Room ${this.id} size:`)} ${size2}`
438
439
  );
439
440
  if (size2)
440
441
  return;
@@ -445,11 +446,11 @@ var IORoom = class {
445
446
  pluvWs.addEventListener("error", closeHandler);
446
447
  this._logDebug(
447
448
  `${chalk.blue(
448
- `Registered connection for room ${this.room}:`
449
+ `Registered connection for room ${this.id}:`
449
450
  )} ${sessionId}`
450
451
  );
451
452
  const size = this._getSessionsSize();
452
- this._logDebug(`${chalk.blue(`Room ${this.room} size:`)} ${size}`);
453
+ this._logDebug(`${chalk.blue(`Room ${this.id} size:`)} ${size}`);
453
454
  });
454
455
  }
455
456
  _broadcast(params) {
@@ -464,9 +465,9 @@ var IORoom = class {
464
465
  this._sessions.delete(session.id);
465
466
  });
466
467
  this._emitQuitters(quitters);
467
- this._platform.pubSub.publish(this.room, __spreadValues({
468
+ this._platform.pubSub.publish(this.id, __spreadValues({
468
469
  connectionId: senderId != null ? senderId : null,
469
- room: this.room,
470
+ room: this.id,
470
471
  user: (_a = sender == null ? void 0 : sender.user) != null ? _a : null
471
472
  }, message));
472
473
  }
@@ -491,7 +492,7 @@ var IORoom = class {
491
492
  );
492
493
  }
493
494
  _emitStorageReceived(session) {
494
- this._platform.persistance.getStorageState(this.room).then((_state) => __async(this, null, function* () {
495
+ this._platform.persistance.getStorageState(this.id).then((_state) => __async(this, null, function* () {
495
496
  var _a, _b;
496
497
  const state = (_b = _state != null ? _state : yield (_a = this._initialStorage) == null ? void 0 : _a.call(this)) != null ? _b : this._doc.encodeStateAsUpdate();
497
498
  this._sendSelfMessage(
@@ -534,9 +535,9 @@ var IORoom = class {
534
535
  this._logDebug(token);
535
536
  return null;
536
537
  }
537
- if (payload.room !== this.room) {
538
+ if (payload.room !== this.id) {
538
539
  this._logDebug(
539
- chalk.blue(`Token is not authorized for room ${this.room}:`)
540
+ chalk.blue(`Token is not authorized for room ${this.id}:`)
540
541
  );
541
542
  this._logDebug(chalk.blue("Received:"), payload.room);
542
543
  this._logDebug(token);
@@ -564,13 +565,13 @@ var IORoom = class {
564
565
  }
565
566
  _initialize() {
566
567
  return __async(this, null, function* () {
567
- this._logDebug(`${chalk.blue(`Initializing room ${this.room}:`)}`);
568
+ this._logDebug(`${chalk.blue(`Initializing room ${this.id}:`)}`);
568
569
  if (!!this._uninitialize) {
569
570
  yield this._uninitialize();
570
571
  this._uninitialize = null;
571
572
  }
572
573
  const pubSubId = yield this._platform.pubSub.subscribe(
573
- this.room,
574
+ this.id,
574
575
  (_a) => {
575
576
  var _b = _a, { options = {} } = _b, message = __objRest(_b, ["options"]);
576
577
  const session = {
@@ -656,7 +657,7 @@ var IORoom = class {
656
657
  Array.from(sessions.values()).forEach((_session) => {
657
658
  _session.webSocket.sendMessage(__spreadValues({
658
659
  connectionId: senderId,
659
- room: this.room,
660
+ room: this.id,
660
661
  user: _session.user
661
662
  }, message));
662
663
  });
@@ -670,7 +671,7 @@ var IORoom = class {
670
671
  return;
671
672
  session.webSocket.sendMessage(__spreadValues({
672
673
  connectionId: sender.id,
673
- room: this.room,
674
+ room: this.id,
674
675
  user: sender.user
675
676
  }, message));
676
677
  }
@@ -681,7 +682,7 @@ var IORoom = class {
681
682
  this._resolveMessage(message, {
682
683
  context: this._context,
683
684
  doc: this._doc,
684
- room: this.room,
685
+ room: this.id,
685
686
  session: null,
686
687
  sessions: this._sessions
687
688
  }).then((output) => {
@@ -690,11 +691,11 @@ var IORoom = class {
690
691
  Object.keys(output).forEach((type) => {
691
692
  var _a, _b;
692
693
  const data = (_a = output[type]) != null ? _a : {};
693
- this._platform.pubSub.publish(this.room, {
694
+ this._platform.pubSub.publish(this.id, {
694
695
  connectionId: senderId,
695
696
  data,
696
697
  options: { type: "self" },
697
- room: this.room,
698
+ room: this.id,
698
699
  type,
699
700
  user: (_b = sender.user) != null ? _b : null
700
701
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pluv/io",
3
- "version": "0.2.5",
3
+ "version": "0.3.0",
4
4
  "description": "@pluv/io server-side websocket manager",
5
5
  "author": "leedavidcs",
6
6
  "license": "MIT",
@@ -20,19 +20,19 @@
20
20
  "@panva/hkdf": "^1.0.4",
21
21
  "chalk": "^4.1.2",
22
22
  "jose": "^4.13.1",
23
- "wonka": "^6.2.4",
24
- "@pluv/crdt-yjs": "^0.3.1",
25
- "@pluv/types": "^0.1.3"
23
+ "wonka": "^6.2.5",
24
+ "@pluv/crdt-yjs": "^0.3.2",
25
+ "@pluv/types": "^0.1.4"
26
26
  },
27
27
  "peerDependencies": {
28
28
  "yjs": "^13.0.0"
29
29
  },
30
30
  "devDependencies": {
31
- "tsup": "^6.6.3",
32
- "typescript": "^4.9.5",
33
- "yjs": "^13.5.50",
34
- "@pluv/tsconfig": "^0.1.3",
35
- "eslint-config-pluv": "^0.1.5"
31
+ "tsup": "^6.7.0",
32
+ "typescript": "^5.0.2",
33
+ "yjs": "^13.5.51",
34
+ "@pluv/tsconfig": "^0.1.4",
35
+ "eslint-config-pluv": "^0.1.7"
36
36
  },
37
37
  "scripts": {
38
38
  "build": "tsup src/index.ts --format esm,cjs --dts",
package/src/IORoom.ts CHANGED
@@ -5,6 +5,7 @@ import type {
5
5
  BaseIOEventRecord,
6
6
  EventMessage,
7
7
  EventRecord,
8
+ Id,
8
9
  InferEventMessage,
9
10
  InferIOAuthorize,
10
11
  InferIOAuthorizeUser,
@@ -85,10 +86,10 @@ export class IORoom<
85
86
  readonly _authorize: TAuthorize | null = null;
86
87
  readonly _events: InferEventConfig<TContext, TInput, TOutput>;
87
88
 
88
- readonly room: string;
89
+ readonly id: string;
89
90
 
90
91
  constructor(
91
- room: string,
92
+ id: string,
92
93
  config: IORoomConfig<TPlatform, TAuthorize, TContext, TInput, TOutput>
93
94
  ) {
94
95
  const {
@@ -106,7 +107,7 @@ export class IORoom<
106
107
  this._events = events;
107
108
  this._platform = platform;
108
109
 
109
- this.room = room;
110
+ this.id = id;
110
111
 
111
112
  this._listeners = {
112
113
  onDestroy: (encodedState) => onDestroy?.(encodedState),
@@ -116,7 +117,12 @@ export class IORoom<
116
117
  if (initialStorage) this._initialStorage = initialStorage;
117
118
  }
118
119
 
119
- public broadcast(message: BroadcastMessage<this>): Promise<void> {
120
+ public broadcast<TEvent extends keyof InferIOInput<this>>(
121
+ event: TEvent,
122
+ data: Id<InferIOInput<this>[TEvent]>
123
+ ): Promise<void> {
124
+ const message = { type: event, data } as BroadcastMessage<this>;
125
+
120
126
  return Promise.resolve(this._broadcast({ message }));
121
127
  }
122
128
 
@@ -129,12 +135,12 @@ export class IORoom<
129
135
  const token = options?.token;
130
136
  const sessionId = this._platform.randomUUID();
131
137
  const pluvWs = this._platform.convertWebSocket(webSocket, {
132
- room: this.room,
138
+ room: this.id,
133
139
  });
134
140
 
135
141
  this._logDebug(
136
142
  `${chalk.blue(
137
- `Registering connection for room ${this.room}:`
143
+ `Registering connection for room ${this.id}:`
138
144
  )} ${sessionId}`
139
145
  );
140
146
 
@@ -168,7 +174,7 @@ export class IORoom<
168
174
  id: sessionId,
169
175
  presence: null,
170
176
  quit: false,
171
- room: this.room,
177
+ room: this.id,
172
178
  timers: {
173
179
  ping: currentTime,
174
180
  },
@@ -179,7 +185,7 @@ export class IORoom<
179
185
  this._sessions.set(session.id, session);
180
186
 
181
187
  await this._platform.persistance.addUser(
182
- this.room,
188
+ this.id,
183
189
  sessionId,
184
190
  user ?? {}
185
191
  );
@@ -191,7 +197,7 @@ export class IORoom<
191
197
  const context: EventResolverContext<TContext> = {
192
198
  context: this._context,
193
199
  doc: this._doc,
194
- room: this.room,
200
+ room: this.id,
195
201
  session,
196
202
  sessions: this._sessions,
197
203
  };
@@ -227,10 +233,10 @@ export class IORoom<
227
233
  return;
228
234
  }
229
235
  case "sync": {
230
- this._platform.pubSub.publish(this.room, {
236
+ this._platform.pubSub.publish(this.id, {
231
237
  connectionId: session.id,
232
238
  options: eventConfig.options,
233
- room: this.room,
239
+ room: this.id,
234
240
  user: session.user,
235
241
  ...message,
236
242
  });
@@ -261,13 +267,13 @@ export class IORoom<
261
267
 
262
268
  this._logDebug(
263
269
  `${chalk.blue(
264
- `(Unregistering connection for room ${this.room}:`
270
+ `(Unregistering connection for room ${this.id}:`
265
271
  )} ${sessionId}`
266
272
  );
267
273
  this._sessions.delete(session.id);
268
274
 
269
275
  this._platform.persistance
270
- .deleteUser(this.room, session.id)
276
+ .deleteUser(this.id, session.id)
271
277
  .finally(() => {
272
278
  this._broadcast({
273
279
  message: {
@@ -283,11 +289,11 @@ export class IORoom<
283
289
 
284
290
  this._logDebug(
285
291
  `${chalk.blue(
286
- `Unregistered connection for room ${this.room}:`
292
+ `Unregistered connection for room ${this.id}:`
287
293
  )} ${sessionId}`
288
294
  );
289
295
  this._logDebug(
290
- `${chalk.blue(`Room ${this.room} size:`)} ${size}`
296
+ `${chalk.blue(`Room ${this.id} size:`)} ${size}`
291
297
  );
292
298
 
293
299
  if (size) return;
@@ -301,13 +307,13 @@ export class IORoom<
301
307
 
302
308
  this._logDebug(
303
309
  `${chalk.blue(
304
- `Registered connection for room ${this.room}:`
310
+ `Registered connection for room ${this.id}:`
305
311
  )} ${sessionId}`
306
312
  );
307
313
 
308
314
  const size = this._getSessionsSize();
309
315
 
310
- this._logDebug(`${chalk.blue(`Room ${this.room} size:`)} ${size}`);
316
+ this._logDebug(`${chalk.blue(`Room ${this.id} size:`)} ${size}`);
311
317
  }
312
318
 
313
319
  private _broadcast(params: BroadcastParams<this>): void {
@@ -328,9 +334,9 @@ export class IORoom<
328
334
 
329
335
  this._emitQuitters(quitters);
330
336
 
331
- this._platform.pubSub.publish(this.room, {
337
+ this._platform.pubSub.publish(this.id, {
332
338
  connectionId: senderId ?? null,
333
- room: this.room,
339
+ room: this.id,
334
340
  user: sender?.user ?? null,
335
341
  ...message,
336
342
  });
@@ -360,7 +366,7 @@ export class IORoom<
360
366
 
361
367
  private _emitStorageReceived(session: WebSocketSession): void {
362
368
  this._platform.persistance
363
- .getStorageState(this.room)
369
+ .getStorageState(this.id)
364
370
  .then(async (_state) => {
365
371
  const state =
366
372
  _state ??
@@ -412,9 +418,9 @@ export class IORoom<
412
418
  return null;
413
419
  }
414
420
 
415
- if (payload.room !== this.room) {
421
+ if (payload.room !== this.id) {
416
422
  this._logDebug(
417
- chalk.blue(`Token is not authorized for room ${this.room}:`)
423
+ chalk.blue(`Token is not authorized for room ${this.id}:`)
418
424
  );
419
425
  this._logDebug(chalk.blue("Received:"), payload.room);
420
426
  this._logDebug(token);
@@ -456,7 +462,7 @@ export class IORoom<
456
462
  }
457
463
 
458
464
  private async _initialize(): Promise<void> {
459
- this._logDebug(`${chalk.blue(`Initializing room ${this.room}:`)}`);
465
+ this._logDebug(`${chalk.blue(`Initializing room ${this.id}:`)}`);
460
466
 
461
467
  if (!!this._uninitialize) {
462
468
  await this._uninitialize();
@@ -465,7 +471,7 @@ export class IORoom<
465
471
  }
466
472
 
467
473
  const pubSubId = await this._platform.pubSub.subscribe(
468
- this.room,
474
+ this.id,
469
475
  ({ options = {}, ...message }) => {
470
476
  const session = {
471
477
  id: message.connectionId,
@@ -591,7 +597,7 @@ export class IORoom<
591
597
  Array.from(sessions.values()).forEach((_session) => {
592
598
  _session.webSocket.sendMessage({
593
599
  connectionId: senderId,
594
- room: this.room,
600
+ room: this.id,
595
601
  user: _session.user,
596
602
  ...message,
597
603
  });
@@ -612,7 +618,7 @@ export class IORoom<
612
618
 
613
619
  session.webSocket.sendMessage({
614
620
  connectionId: sender.id,
615
- room: this.room,
621
+ room: this.id,
616
622
  user: sender.user,
617
623
  ...message,
618
624
  });
@@ -629,7 +635,7 @@ export class IORoom<
629
635
  this._resolveMessage(message, {
630
636
  context: this._context,
631
637
  doc: this._doc,
632
- room: this.room,
638
+ room: this.id,
633
639
  session: null,
634
640
  sessions: this._sessions,
635
641
  }).then((output) => {
@@ -638,11 +644,11 @@ export class IORoom<
638
644
  Object.keys(output).forEach((type: string) => {
639
645
  const data = output[type] ?? {};
640
646
 
641
- this._platform.pubSub.publish(this.room, {
647
+ this._platform.pubSub.publish(this.id, {
642
648
  connectionId: senderId,
643
649
  data,
644
650
  options: { type: "self" },
645
- room: this.room,
651
+ room: this.id,
646
652
  type,
647
653
  user: sender.user ?? null,
648
654
  });