@pluv/io 0.22.0 → 0.23.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/dist/index.js CHANGED
@@ -394,10 +394,10 @@ var PING_TIMEOUT_MS = 3e4;
394
394
  // src/IORoom.ts
395
395
  var IORoom = class {
396
396
  constructor(id, config) {
397
- this._sessions = /* @__PURE__ */ new Map();
398
- this._uninitialize = null;
399
397
  this._authorize = null;
400
- const { authorize: authorize2, context, crdt = import_crdt.noop, debug, onDestroy, platform, router } = config;
398
+ this._uninitialize = null;
399
+ this._sessions = /* @__PURE__ */ new Map();
400
+ const { authorize: authorize2, context, crdt = import_crdt.noop, debug, onDestroy, onMessage, platform, router } = config;
401
401
  this._context = context;
402
402
  this._debug = debug;
403
403
  this._docFactory = crdt.doc(() => ({}));
@@ -406,7 +406,8 @@ var IORoom = class {
406
406
  this._platform = platform.initialize({ context });
407
407
  this.id = id;
408
408
  this._listeners = {
409
- onDestroy: (event) => onDestroy == null ? void 0 : onDestroy(event)
409
+ onDestroy: (event) => onDestroy == null ? void 0 : onDestroy(event),
410
+ onMessage: (event) => onMessage == null ? void 0 : onMessage(event)
410
411
  };
411
412
  if (authorize2) this._authorize = authorize2;
412
413
  const webSockets = this._platform.getWebSockets();
@@ -623,9 +624,25 @@ var IORoom = class {
623
624
  this.id,
624
625
  (_a) => __async(this, null, function* () {
625
626
  var _b = _a, { options = {} } = _b, message = __objRest(_b, ["options"]);
626
- const sessionId = message.connectionId;
627
- const user = message.user;
628
- yield this._sendMessage(message, { sessionId, user }, options);
627
+ const sender = {
628
+ sessionId: message.connectionId,
629
+ user: message.user
630
+ };
631
+ switch (options.type) {
632
+ case "self": {
633
+ yield this._sendSelfMessage(message, sender);
634
+ return;
635
+ }
636
+ case "sync": {
637
+ yield this._sendSyncMessage(message, sender);
638
+ return;
639
+ }
640
+ case "broadcast":
641
+ default: {
642
+ const sessionIds = options.sessionIds;
643
+ yield this._sendBroadcastMessage(message, sender, sessionIds);
644
+ }
645
+ }
629
646
  })
630
647
  );
631
648
  this._uninitialize = () => __async(this, null, function* () {
@@ -633,10 +650,11 @@ var IORoom = class {
633
650
  const encodedState = this._doc.getEncodedState();
634
651
  this._doc.destroy();
635
652
  this._doc = this._docFactory.getEmpty();
636
- this._listeners.onDestroy(__spreadProps(__spreadValues({}, this._context), {
653
+ this._listeners.onDestroy({
654
+ context: this._context,
637
655
  encodedState,
638
656
  room: this.id
639
- }));
657
+ });
640
658
  this._uninitialize = null;
641
659
  });
642
660
  });
@@ -754,23 +772,17 @@ var IORoom = class {
754
772
  return null;
755
773
  }
756
774
  }
757
- _sendMessage(_0, _1) {
758
- return __async(this, arguments, function* (message, sender, options = {}) {
759
- switch (options.type) {
760
- case "self": {
761
- yield this._sendSelfMessage(message, sender);
762
- return;
763
- }
764
- case "sync": {
765
- yield this._sendSyncMessage(message, sender);
766
- return;
767
- }
768
- case "broadcast":
769
- default: {
770
- const sessionIds = options.sessionIds;
771
- yield this._sendBroadcastMessage(message, sender, sessionIds);
772
- }
773
- }
775
+ _sendMessage(pluvWs, message) {
776
+ return __async(this, null, function* () {
777
+ const { data, type } = message;
778
+ this._listeners.onMessage({
779
+ context: this._context,
780
+ encodedState: this._doc.getEncodedState(),
781
+ message: { data, type },
782
+ room: this.id,
783
+ user: message.user
784
+ });
785
+ yield Promise.resolve(pluvWs.sendMessage(message));
774
786
  });
775
787
  }
776
788
  _sendBroadcastMessage(message, sender, sessionIds) {
@@ -787,7 +799,7 @@ var IORoom = class {
787
799
  var _a2;
788
800
  const session = yield pluvWs.getSession();
789
801
  const user = (_a2 = session.user) != null ? _a2 : null;
790
- pluvWs.sendMessage({ connectionId, data, room, type, user });
802
+ this._sendMessage(pluvWs, { connectionId, data, room, type, user });
791
803
  }));
792
804
  yield Promise.all(promises);
793
805
  });
@@ -802,7 +814,7 @@ var IORoom = class {
802
814
  if (message.type === "$PONG") yield this._emitQuitters();
803
815
  const session = yield pluvWs.getSession();
804
816
  const user = (_b = session.user) != null ? _b : null;
805
- pluvWs.sendMessage(__spreadValues({
817
+ this._sendMessage(pluvWs, __spreadValues({
806
818
  connectionId: senderId,
807
819
  room: this.id,
808
820
  user
@@ -848,114 +860,15 @@ var IORoom = class {
848
860
  };
849
861
 
850
862
  // src/version.generated.ts
851
- var version = "0.22.0";
863
+ var version = "0.23.0";
852
864
 
853
865
  // src/PluvServer.ts
854
866
  var PluvServer = class {
855
867
  constructor(options) {
856
- this._rooms = /* @__PURE__ */ new Map();
868
+ this.version = version;
857
869
  this._authorize = null;
858
- this._context = {};
859
- this._version = version;
860
- const {
861
- authorize: authorize2,
862
- context,
863
- crdt = import_crdt2.noop,
864
- debug = false,
865
- onRoomDeleted,
866
- onStorageUpdated,
867
- platform,
868
- router = new PluvRouter({})
869
- } = options;
870
- this._crdt = crdt;
871
- this._debug = debug;
872
- this._platform = platform;
873
- this._router = router != null ? router : null;
874
- if (authorize2) this._authorize = authorize2;
875
- if (context) this._context = context;
876
- this._listeners = {
877
- onRoomDeleted: (event) => onRoomDeleted == null ? void 0 : onRoomDeleted(event),
878
- onStorageUpdated: (event) => onStorageUpdated == null ? void 0 : onStorageUpdated(event)
879
- };
880
- }
881
- get _events() {
882
- return this._router._events;
883
- }
884
- get _registrationMode() {
885
- return this._platform._registrationMode;
886
- }
887
- createToken(params) {
888
- return __async(this, null, function* () {
889
- if (!this._authorize) {
890
- throw new Error("IO does not specify authorize during initialization.");
891
- }
892
- const ioAuthorize = typeof this._authorize === "function" ? this._authorize(params) : this._authorize;
893
- const secret = ioAuthorize.secret;
894
- return yield authorize({
895
- platform: this._platform,
896
- secret
897
- }).encode(params);
898
- });
899
- }
900
- getRoom(room, ...options) {
901
- var _a, _c;
902
- const _b = (_a = options[0]) != null ? _a : {}, { debug } = _b, platformRoomContext = __objRest(_b, ["debug"]);
903
- this._purgeEmptyRooms();
904
- if (!/^[a-z0-9]+[a-z0-9\-_]+[a-z0-9]+$/i.test(room)) {
905
- throw new Error("Unsupported room name");
906
- }
907
- const platform = this._platform;
908
- const oldRoom = this._rooms.get(room);
909
- if (oldRoom) return oldRoom;
910
- const roomContext = __spreadValues(__spreadValues({}, this._context), platformRoomContext);
911
- const newRoom = new IORoom(room, {
912
- authorize: (_c = this._authorize) != null ? _c : void 0,
913
- context: roomContext,
914
- crdt: this._crdt,
915
- debug: debug != null ? debug : this._debug,
916
- onDestroy: (_0) => __async(this, [_0], function* ({ encodedState, room: room2 }) {
917
- this._logDebug(`${import_kleur2.default.blue("Deleting empty room:")} ${room2}`);
918
- const roomContext2 = __spreadValues({
919
- room: room2,
920
- encodedState
921
- }, platformRoomContext);
922
- this._rooms.delete(room2);
923
- yield Promise.resolve(this._listeners.onRoomDeleted(roomContext2));
924
- yield this._platform.persistance.deleteStorageState(room2);
925
- if (this._debug) {
926
- const rooms = Array.from(this._rooms.keys());
927
- this._logDebug(`${import_kleur2.default.blue("Deleted room:")} ${room2}`);
928
- this._logDebug(`${import_kleur2.default.blue("Rooms available:")} ${rooms.join(", ")}`);
929
- }
930
- }),
931
- platform,
932
- router: this._router
933
- });
934
- this._rooms.set(room, newRoom);
935
- if (this._debug) {
936
- const rooms = Array.from(this._rooms.keys());
937
- this._logDebug(`${import_kleur2.default.blue("Created room:")} ${room}`);
938
- this._logDebug(`${import_kleur2.default.blue("Rooms available:")} ${rooms.join(", ")}`);
939
- }
940
- return newRoom;
941
- }
942
- _logDebug(...data) {
943
- this._debug && console.log(...data);
944
- }
945
- _purgeEmptyRooms() {
946
- const rooms = Array.from(this._rooms.entries());
947
- rooms.forEach(([id, room]) => {
948
- if (!!(room == null ? void 0 : room.getSize())) return;
949
- this._rooms.delete(id);
950
- });
951
- }
952
- };
953
-
954
- // src/PluvIO.ts
955
- var PluvIO = class {
956
- constructor(options) {
957
- this._router = new PluvRouter({
958
- $GET_OTHERS: this.procedure.sync((data, { room, session, sessions }) => {
870
+ this._baseRouter = new PluvRouter({
871
+ $GET_OTHERS: this._procedure.sync((data, { room, session, sessions }) => {
959
872
  const currentTime = (/* @__PURE__ */ new Date()).getTime();
960
873
  const others = sessions.filter((wsSession) => {
961
874
  if (wsSession.id === (session == null ? void 0 : session.id)) return false;
@@ -975,7 +888,7 @@ var PluvIO = class {
975
888
  );
976
889
  return { $OTHERS_RECEIVED: { others } };
977
890
  }),
978
- $INITIALIZE_SESSION: this.procedure.broadcast((data, { session }) => {
891
+ $INITIALIZE_SESSION: this._procedure.broadcast((data, { session }) => {
979
892
  const presence = data == null ? void 0 : data.presence;
980
893
  if (!session) return {};
981
894
  session.presence = presence;
@@ -999,12 +912,12 @@ var PluvIO = class {
999
912
  if (updates.some((_update) => !!_update)) {
1000
913
  const encodedState = doc.batchApplyEncodedState(updates).getEncodedState();
1001
914
  yield this._platform.persistance.setStorageState(room, encodedState);
1002
- this._listeners.onStorageUpdated(__spreadProps(__spreadValues({}, context), { encodedState, room }));
915
+ this._listeners.onStorageUpdated({ context, encodedState, room });
1003
916
  }
1004
917
  const state = (_b = yield this._platform.persistance.getStorageState(room)) != null ? _b : doc.getEncodedState();
1005
918
  return { $STORAGE_RECEIVED: { state } };
1006
919
  })),
1007
- $PING: this.procedure.self((data, { session }) => {
920
+ $PING: this._procedure.self((data, { session }) => {
1008
921
  if (!session) return {};
1009
922
  const currentTime = (/* @__PURE__ */ new Date()).getTime();
1010
923
  const prevState = session.webSocket.state;
@@ -1015,14 +928,14 @@ var PluvIO = class {
1015
928
  });
1016
929
  return { $PONG: {} };
1017
930
  }),
1018
- $UPDATE_PRESENCE: this.procedure.broadcast((data, { session }) => {
931
+ $UPDATE_PRESENCE: this._procedure.broadcast((data, { session }) => {
1019
932
  const presence = data == null ? void 0 : data.presence;
1020
933
  if (!session) return {};
1021
934
  const updated = Object.assign(/* @__PURE__ */ Object.create(null), session.presence, presence);
1022
935
  session.webSocket.presence = updated;
1023
936
  return { $PRESENCE_UPDATED: { presence: updated } };
1024
937
  }),
1025
- $UPDATE_STORAGE: this.procedure.broadcast((data, { context, doc, room }) => {
938
+ $UPDATE_STORAGE: this._procedure.broadcast((data, { context, doc, room }) => {
1026
939
  var _a;
1027
940
  const origin = data == null ? void 0 : data.origin;
1028
941
  const update = (_a = data == null ? void 0 : data.update) != null ? _a : null;
@@ -1032,36 +945,40 @@ var PluvIO = class {
1032
945
  const updated = update === null ? doc : doc.applyEncodedState({ update });
1033
946
  const encodedState = updated.getEncodedState();
1034
947
  this._platform.persistance.setStorageState(room, encodedState).then(() => {
1035
- this._listeners.onStorageUpdated(__spreadProps(__spreadValues({}, context), {
948
+ this._listeners.onStorageUpdated({
949
+ context,
1036
950
  encodedState,
1037
951
  room
1038
- }));
952
+ });
1039
953
  });
1040
954
  return { $STORAGE_UPDATED: { state: encodedState } };
1041
955
  })
1042
956
  });
1043
- this._authorize = null;
1044
957
  this._context = {};
1045
958
  this._getInitialStorage = null;
1046
- this._version = version;
959
+ this._rooms = /* @__PURE__ */ new Map();
1047
960
  const {
1048
961
  authorize: authorize2,
1049
962
  context,
1050
- crdt = import_crdt3.noop,
963
+ crdt = import_crdt2.noop,
1051
964
  debug = false,
1052
965
  getInitialStorage,
1053
966
  onRoomDeleted,
967
+ onRoomMessage,
1054
968
  onStorageUpdated,
1055
- platform
969
+ platform,
970
+ router = new PluvRouter({})
1056
971
  } = options;
1057
972
  this._crdt = crdt;
1058
973
  this._debug = debug;
1059
974
  this._platform = platform;
975
+ this._router = router ? this._baseRouter : PluvRouter.merge(this._baseRouter, router);
1060
976
  if (authorize2) this._authorize = authorize2;
1061
977
  if (context) this._context = context;
1062
978
  if (getInitialStorage) this._getInitialStorage = getInitialStorage;
1063
979
  this._listeners = {
1064
980
  onRoomDeleted: (event) => onRoomDeleted == null ? void 0 : onRoomDeleted(event),
981
+ onRoomMessage: (event) => onRoomMessage == null ? void 0 : onRoomMessage(event),
1065
982
  onStorageUpdated: (event) => onStorageUpdated == null ? void 0 : onStorageUpdated(event)
1066
983
  };
1067
984
  }
@@ -1071,6 +988,88 @@ var PluvIO = class {
1071
988
  get _registrationMode() {
1072
989
  return this._platform._registrationMode;
1073
990
  }
991
+ get _procedure() {
992
+ return new PluvProcedure();
993
+ }
994
+ createToken(params) {
995
+ return __async(this, null, function* () {
996
+ if (!this._authorize) {
997
+ throw new Error("IO does not specify authorize during initialization.");
998
+ }
999
+ const ioAuthorize = typeof this._authorize === "function" ? this._authorize(params) : this._authorize;
1000
+ const secret = ioAuthorize.secret;
1001
+ return yield authorize({
1002
+ platform: this._platform,
1003
+ secret
1004
+ }).encode(params);
1005
+ });
1006
+ }
1007
+ getRoom(room, ...options) {
1008
+ var _a, _c;
1009
+ const _b = (_a = options[0]) != null ? _a : {}, { debug } = _b, platformRoomContext = __objRest(_b, ["debug"]);
1010
+ this._purgeEmptyRooms();
1011
+ if (!/^[a-z0-9]+[a-z0-9\-_]+[a-z0-9]+$/i.test(room)) {
1012
+ throw new Error("Unsupported room name");
1013
+ }
1014
+ const platform = this._platform;
1015
+ const oldRoom = this._rooms.get(room);
1016
+ if (oldRoom) return oldRoom;
1017
+ const roomContext = __spreadValues(__spreadValues({}, this._context), platformRoomContext);
1018
+ const newRoom = new IORoom(room, {
1019
+ authorize: (_c = this._authorize) != null ? _c : void 0,
1020
+ context: roomContext,
1021
+ crdt: this._crdt,
1022
+ debug: debug != null ? debug : this._debug,
1023
+ onDestroy: (event) => __async(this, null, function* () {
1024
+ this._logDebug(`${import_kleur2.default.blue("Deleting empty room:")} ${room}`);
1025
+ this._rooms.delete(room);
1026
+ yield Promise.resolve(this._listeners.onRoomDeleted(event));
1027
+ yield this._platform.persistance.deleteStorageState(room);
1028
+ if (this._debug) {
1029
+ const rooms = Array.from(this._rooms.keys());
1030
+ this._logDebug(`${import_kleur2.default.blue("Deleted room:")} ${room}`);
1031
+ this._logDebug(`${import_kleur2.default.blue("Rooms available:")} ${rooms.join(", ")}`);
1032
+ }
1033
+ }),
1034
+ onMessage: (event) => __async(this, null, function* () {
1035
+ yield Promise.resolve(this._listeners.onRoomMessage(event));
1036
+ }),
1037
+ platform,
1038
+ router: this._router
1039
+ });
1040
+ this._rooms.set(room, newRoom);
1041
+ if (this._debug) {
1042
+ const rooms = Array.from(this._rooms.keys());
1043
+ this._logDebug(`${import_kleur2.default.blue("Created room:")} ${room}`);
1044
+ this._logDebug(`${import_kleur2.default.blue("Rooms available:")} ${rooms.join(", ")}`);
1045
+ }
1046
+ return newRoom;
1047
+ }
1048
+ _logDebug(...data) {
1049
+ this._debug && console.log(...data);
1050
+ }
1051
+ _purgeEmptyRooms() {
1052
+ const rooms = Array.from(this._rooms.entries());
1053
+ rooms.forEach(([id, room]) => {
1054
+ if (!!(room == null ? void 0 : room.getSize())) return;
1055
+ this._rooms.delete(id);
1056
+ });
1057
+ }
1058
+ };
1059
+
1060
+ // src/PluvIO.ts
1061
+ var PluvIO = class {
1062
+ constructor(options) {
1063
+ this.version = version;
1064
+ this._authorize = null;
1065
+ this._context = {};
1066
+ const { authorize: authorize2, context, crdt = import_crdt3.noop, debug = false, platform } = options;
1067
+ this._crdt = crdt;
1068
+ this._debug = debug;
1069
+ this._platform = platform;
1070
+ if (authorize2) this._authorize = authorize2;
1071
+ if (context) this._context = context;
1072
+ }
1074
1073
  get procedure() {
1075
1074
  return new PluvProcedure();
1076
1075
  }
@@ -1093,29 +1092,24 @@ var PluvIO = class {
1093
1092
  }
1094
1093
  server(config = {}) {
1095
1094
  var _a;
1096
- const router = config.router ? this.mergeRouters(this._router, config.router) : this._router;
1097
- return new PluvServer(__spreadProps(__spreadValues({}, this._listeners), {
1095
+ return new PluvServer(__spreadProps(__spreadValues({}, config), {
1098
1096
  authorize: (_a = this._authorize) != null ? _a : void 0,
1099
1097
  context: this._context,
1100
1098
  crdt: this._crdt,
1101
1099
  debug: this._debug,
1102
- platform: this._platform,
1103
- router
1100
+ platform: this._platform
1104
1101
  }));
1105
1102
  }
1106
1103
  };
1107
1104
 
1108
1105
  // src/createIO.ts
1109
1106
  var createIO = (params) => {
1110
- const { authorize: authorize2, context, crdt, debug, getInitialStorage, onRoomDeleted, onStorageUpdated, platform } = params;
1107
+ const { authorize: authorize2, context, crdt, debug, platform } = params;
1111
1108
  return new PluvIO({
1112
1109
  authorize: authorize2,
1113
1110
  context,
1114
1111
  crdt,
1115
1112
  debug,
1116
- getInitialStorage,
1117
- onRoomDeleted,
1118
- onStorageUpdated,
1119
1113
  platform
1120
1114
  });
1121
1115
  };