@signe/room 2.8.2 → 2.9.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.d.ts CHANGED
@@ -175,6 +175,11 @@ type ServerOptions = {
175
175
  type GuardFn = (sender: Connection, value: any | Request$1, room: Room$1) => boolean | Promise<boolean | Response>;
176
176
  type RoomGuardFn = (conn: Connection, ctx: ConnectionContext, room: Room$1) => boolean | Promise<boolean | Response>;
177
177
  declare function Action(name: string, bodyValidation?: z.ZodSchema): (target: any, propertyKey: string) => void;
178
+ /**
179
+ * Fallback decorator for handling websocket messages whose action
180
+ * does not match any registered @Action decorator.
181
+ */
182
+ declare function UnhandledAction(): (target: any, propertyKey: string) => void;
178
183
  /**
179
184
  * Request decorator for handling HTTP requests with path and method routing
180
185
  * @param options Configuration for the HTTP request handler
@@ -922,4 +927,4 @@ declare function createRequireSessionGuard(storage: Storage$1): (sender: Connect
922
927
  */
923
928
  declare const requireSession: (sender: Connection, value: any, room: Room$1) => Promise<boolean>;
924
929
 
925
- export { Action, ClientIo, Guard, MockConnection, Request, type RequestOptions, Room, RoomGuard, type RoomInterceptorPacket, type RoomMethods, type RoomOnJoin, type RoomOnLeave, type RoomOptions, Server, ServerIo, ServerResponse, Shard, type ShardOptions, WorldRoom, createRequireSessionGuard, request, requireSession, testRoom, tick };
930
+ export { Action, ClientIo, Guard, MockConnection, Request, type RequestOptions, Room, RoomGuard, type RoomInterceptorPacket, type RoomMethods, type RoomOnJoin, type RoomOnLeave, type RoomOptions, Server, ServerIo, ServerResponse, Shard, type ShardOptions, UnhandledAction, WorldRoom, createRequireSessionGuard, request, requireSession, testRoom, tick };
package/dist/index.js CHANGED
@@ -14,6 +14,14 @@ function Action(name, bodyValidation) {
14
14
  };
15
15
  }
16
16
  __name(Action, "Action");
17
+ function UnhandledAction() {
18
+ return function(target, propertyKey) {
19
+ target.constructor._unhandledActionMetadata = {
20
+ key: propertyKey
21
+ };
22
+ };
23
+ }
24
+ __name(UnhandledAction, "UnhandledAction");
17
25
  function Request2(options, bodyValidation) {
18
26
  return function(target, propertyKey) {
19
27
  if (!target.constructor._requestMetadata) {
@@ -1050,27 +1058,37 @@ var Server = class {
1050
1058
  }
1051
1059
  }
1052
1060
  const actions = subRoom.constructor["_actionMetadata"];
1053
- if (actions) {
1054
- const signal2 = this.getUsersProperty(subRoom);
1055
- const { publicId } = sender.state;
1056
- const user = signal2?.()[publicId];
1057
- const actionName = actions.get(result.data.action);
1058
- if (actionName) {
1059
- const guards = subRoom.constructor["_actionGuards"]?.get(actionName.key) || [];
1060
- for (const guard of guards) {
1061
- const isAuthorized = await guard(sender, result.data.value);
1062
- if (!isAuthorized) {
1063
- return;
1064
- }
1061
+ const signal2 = this.getUsersProperty(subRoom);
1062
+ const { publicId } = sender.state;
1063
+ const user = signal2?.()[publicId];
1064
+ const actionName = actions?.get(result.data.action);
1065
+ if (actionName) {
1066
+ const guards = subRoom.constructor["_actionGuards"]?.get(actionName.key) || [];
1067
+ for (const guard of guards) {
1068
+ const isAuthorized = await guard(sender, result.data.value, this.room);
1069
+ if (!isAuthorized) {
1070
+ return;
1065
1071
  }
1066
- if (actionName.bodyValidation) {
1067
- const bodyResult = actionName.bodyValidation.safeParse(result.data.value);
1068
- if (!bodyResult.success) {
1069
- return;
1070
- }
1072
+ }
1073
+ if (actionName.bodyValidation) {
1074
+ const bodyResult = actionName.bodyValidation.safeParse(result.data.value);
1075
+ if (!bodyResult.success) {
1076
+ return;
1071
1077
  }
1072
- await awaitReturn(subRoom[actionName.key](user, result.data.value, sender));
1073
1078
  }
1079
+ await awaitReturn(subRoom[actionName.key](user, result.data.value, sender));
1080
+ return;
1081
+ }
1082
+ const unhandledAction = subRoom.constructor["_unhandledActionMetadata"];
1083
+ if (unhandledAction) {
1084
+ const guards = subRoom.constructor["_actionGuards"]?.get(unhandledAction.key) || [];
1085
+ for (const guard of guards) {
1086
+ const isAuthorized = await guard(sender, result.data, this.room);
1087
+ if (!isAuthorized) {
1088
+ return;
1089
+ }
1090
+ }
1091
+ await awaitReturn(subRoom[unhandledAction.key](user, result.data, sender));
1074
1092
  }
1075
1093
  }
1076
1094
  /**
@@ -1350,9 +1368,9 @@ var Server = class {
1350
1368
  sessionState,
1351
1369
  room: this.room
1352
1370
  })) ?? userSnapshot;
1353
- load(user, hydratedSnapshot, true);
1354
1371
  signal2()[publicId] = user;
1355
- await this.room.storage.put(`${usersPropName}.${publicId}`, hydratedSnapshot);
1372
+ load(user, hydratedSnapshot, true);
1373
+ await this.room.storage.put(`${usersPropName}.${publicId}`, userSnapshot);
1356
1374
  }
1357
1375
  }
1358
1376
  const transferToken = generateShortUUID2();
@@ -2732,6 +2750,7 @@ export {
2732
2750
  ServerIo,
2733
2751
  ServerResponse,
2734
2752
  Shard,
2753
+ UnhandledAction,
2735
2754
  WorldRoom,
2736
2755
  createRequireSessionGuard,
2737
2756
  request,