@colyseus/shared-types 0.17.1 → 0.17.2

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.
@@ -58,7 +58,7 @@ var CloseCode = {
58
58
  CONSENTED: 4e3,
59
59
  SERVER_SHUTDOWN: 4001,
60
60
  WITH_ERROR: 4002,
61
- DEVMODE_RESTART: 4010
61
+ MAY_TRY_RECONNECT: 4010
62
62
  };
63
63
  // Annotate the CommonJS export names for ESM import in node:
64
64
  0 && (module.exports = {
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/Protocol.ts"],
4
- "sourcesContent": ["/**\n * Colyseus protocol codes range between 0~100\n * Use codes between 0~127 for lesser throughput (1 byte)\n */\nexport const Protocol = {\n // Room-related (10~19)\n JOIN_ROOM: 10,\n ERROR: 11,\n LEAVE_ROOM: 12,\n ROOM_DATA: 13,\n ROOM_STATE: 14,\n ROOM_STATE_PATCH: 15,\n ROOM_DATA_SCHEMA: 16, // DEPRECATED: used to send schema instances via room.send()\n ROOM_DATA_BYTES: 17,\n PING: 18,\n} as const;\nexport type Protocol = typeof Protocol[keyof typeof Protocol];\n\n/**\n * HTTP MatchMaking Error Codes\n */\nexport const ErrorCode = {\n MATCHMAKE_NO_HANDLER: 520,\n MATCHMAKE_INVALID_CRITERIA: 521,\n MATCHMAKE_INVALID_ROOM_ID: 522,\n MATCHMAKE_UNHANDLED: 523, // generic exception during onCreate/onJoin\n MATCHMAKE_EXPIRED: 524, // generic exception during onCreate/onJoin\n AUTH_FAILED: 525,\n APPLICATION_ERROR: 526,\n\n INVALID_PAYLOAD: 4217,\n} as const;\nexport type ErrorCode = typeof ErrorCode[keyof typeof ErrorCode];\n\n/**\n * WebSocket close codes\n * (See https://github.com/Luka967/websocket-close-codes)\n */\nexport const CloseCode = {\n NORMAL_CLOSURE: 1000,\n GOING_AWAY: 1001,\n NO_STATUS_RECEIVED: 1005,\n ABNORMAL_CLOSURE: 1006,\n\n CONSENTED: 4000,\n SERVER_SHUTDOWN: 4001,\n WITH_ERROR: 4002,\n DEVMODE_RESTART: 4010\n} as const;\nexport type CloseCode = typeof CloseCode[keyof typeof CloseCode];\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAIO,IAAM,WAAW;AAAA;AAAA,EAEtB,WAAW;AAAA,EACX,OAAO;AAAA,EACP,YAAY;AAAA,EACZ,WAAW;AAAA,EACX,YAAY;AAAA,EACZ,kBAAkB;AAAA,EAClB,kBAAkB;AAAA;AAAA,EAClB,iBAAiB;AAAA,EACjB,MAAM;AACR;AAMO,IAAM,YAAY;AAAA,EACvB,sBAAsB;AAAA,EACtB,4BAA4B;AAAA,EAC5B,2BAA2B;AAAA,EAC3B,qBAAqB;AAAA;AAAA,EACrB,mBAAmB;AAAA;AAAA,EACnB,aAAa;AAAA,EACb,mBAAmB;AAAA,EAEnB,iBAAiB;AACnB;AAOO,IAAM,YAAY;AAAA,EACvB,gBAAgB;AAAA,EAChB,YAAY;AAAA,EACZ,oBAAoB;AAAA,EACpB,kBAAkB;AAAA,EAElB,WAAW;AAAA,EACX,iBAAiB;AAAA,EACjB,YAAY;AAAA,EACZ,iBAAiB;AACnB;",
4
+ "sourcesContent": ["/**\n * Colyseus protocol codes range between 0~100\n * Use codes between 0~127 for lesser throughput (1 byte)\n */\nexport const Protocol = {\n // Room-related (10~19)\n JOIN_ROOM: 10,\n ERROR: 11,\n LEAVE_ROOM: 12,\n ROOM_DATA: 13,\n ROOM_STATE: 14,\n ROOM_STATE_PATCH: 15,\n ROOM_DATA_SCHEMA: 16, // DEPRECATED: used to send schema instances via room.send()\n ROOM_DATA_BYTES: 17,\n PING: 18,\n} as const;\nexport type Protocol = typeof Protocol[keyof typeof Protocol];\n\n/**\n * HTTP MatchMaking Error Codes\n */\nexport const ErrorCode = {\n MATCHMAKE_NO_HANDLER: 520,\n MATCHMAKE_INVALID_CRITERIA: 521,\n MATCHMAKE_INVALID_ROOM_ID: 522,\n MATCHMAKE_UNHANDLED: 523, // generic exception during onCreate/onJoin\n MATCHMAKE_EXPIRED: 524, // generic exception during onCreate/onJoin\n AUTH_FAILED: 525,\n APPLICATION_ERROR: 526,\n\n INVALID_PAYLOAD: 4217,\n} as const;\nexport type ErrorCode = typeof ErrorCode[keyof typeof ErrorCode];\n\n/**\n * WebSocket close codes\n * (See https://github.com/Luka967/websocket-close-codes)\n */\nexport const CloseCode = {\n NORMAL_CLOSURE: 1000,\n GOING_AWAY: 1001,\n NO_STATUS_RECEIVED: 1005,\n ABNORMAL_CLOSURE: 1006,\n\n CONSENTED: 4000,\n SERVER_SHUTDOWN: 4001,\n WITH_ERROR: 4002,\n MAY_TRY_RECONNECT: 4010\n} as const;\nexport type CloseCode = typeof CloseCode[keyof typeof CloseCode];\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAIO,IAAM,WAAW;AAAA;AAAA,EAEtB,WAAW;AAAA,EACX,OAAO;AAAA,EACP,YAAY;AAAA,EACZ,WAAW;AAAA,EACX,YAAY;AAAA,EACZ,kBAAkB;AAAA,EAClB,kBAAkB;AAAA;AAAA,EAClB,iBAAiB;AAAA,EACjB,MAAM;AACR;AAMO,IAAM,YAAY;AAAA,EACvB,sBAAsB;AAAA,EACtB,4BAA4B;AAAA,EAC5B,2BAA2B;AAAA,EAC3B,qBAAqB;AAAA;AAAA,EACrB,mBAAmB;AAAA;AAAA,EACnB,aAAa;AAAA,EACb,mBAAmB;AAAA,EAEnB,iBAAiB;AACnB;AAOO,IAAM,YAAY;AAAA,EACvB,gBAAgB;AAAA,EAChB,YAAY;AAAA,EACZ,oBAAoB;AAAA,EACpB,kBAAkB;AAAA,EAElB,WAAW;AAAA,EACX,iBAAiB;AAAA,EACjB,YAAY;AAAA,EACZ,mBAAmB;AACrB;",
6
6
  "names": []
7
7
  }
@@ -40,6 +40,6 @@ export declare const CloseCode: {
40
40
  readonly CONSENTED: 4000;
41
41
  readonly SERVER_SHUTDOWN: 4001;
42
42
  readonly WITH_ERROR: 4002;
43
- readonly DEVMODE_RESTART: 4010;
43
+ readonly MAY_TRY_RECONNECT: 4010;
44
44
  };
45
45
  export type CloseCode = typeof CloseCode[keyof typeof CloseCode];
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Colyseus protocol codes range between 0~100
3
+ * Use codes between 0~127 for lesser throughput (1 byte)
4
+ */
5
+ export const Protocol = {
6
+ // Room-related (10~19)
7
+ JOIN_ROOM: 10,
8
+ ERROR: 11,
9
+ LEAVE_ROOM: 12,
10
+ ROOM_DATA: 13,
11
+ ROOM_STATE: 14,
12
+ ROOM_STATE_PATCH: 15,
13
+ ROOM_DATA_SCHEMA: 16, // DEPRECATED: used to send schema instances via room.send()
14
+ ROOM_DATA_BYTES: 17,
15
+ PING: 18,
16
+ };
17
+ /**
18
+ * HTTP MatchMaking Error Codes
19
+ */
20
+ export const ErrorCode = {
21
+ MATCHMAKE_NO_HANDLER: 520,
22
+ MATCHMAKE_INVALID_CRITERIA: 521,
23
+ MATCHMAKE_INVALID_ROOM_ID: 522,
24
+ MATCHMAKE_UNHANDLED: 523, // generic exception during onCreate/onJoin
25
+ MATCHMAKE_EXPIRED: 524, // generic exception during onCreate/onJoin
26
+ AUTH_FAILED: 525,
27
+ APPLICATION_ERROR: 526,
28
+ INVALID_PAYLOAD: 4217,
29
+ };
30
+ /**
31
+ * WebSocket close codes
32
+ * (See https://github.com/Luka967/websocket-close-codes)
33
+ */
34
+ export const CloseCode = {
35
+ NORMAL_CLOSURE: 1000,
36
+ GOING_AWAY: 1001,
37
+ NO_STATUS_RECEIVED: 1005,
38
+ ABNORMAL_CLOSURE: 1006,
39
+ CONSENTED: 4000,
40
+ SERVER_SHUTDOWN: 4001,
41
+ WITH_ERROR: 4002,
42
+ MAY_TRY_RECONNECT: 4010
43
+ };
44
+ //# sourceMappingURL=Protocol.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Protocol.js","sourceRoot":"","sources":["../src/Protocol.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAG;IACtB,uBAAuB;IACvB,SAAS,EAAE,EAAE;IACb,KAAK,EAAE,EAAE;IACT,UAAU,EAAE,EAAE;IACd,SAAS,EAAE,EAAE;IACb,UAAU,EAAE,EAAE;IACd,gBAAgB,EAAE,EAAE;IACpB,gBAAgB,EAAE,EAAE,EAAE,4DAA4D;IAClF,eAAe,EAAE,EAAE;IACnB,IAAI,EAAE,EAAE;CACA,CAAC;AAGX;;GAEG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG;IACvB,oBAAoB,EAAE,GAAG;IACzB,0BAA0B,EAAE,GAAG;IAC/B,yBAAyB,EAAE,GAAG;IAC9B,mBAAmB,EAAE,GAAG,EAAE,2CAA2C;IACrE,iBAAiB,EAAE,GAAG,EAAE,2CAA2C;IACnE,WAAW,EAAE,GAAG;IAChB,iBAAiB,EAAE,GAAG;IAEtB,eAAe,EAAE,IAAI;CACb,CAAC;AAGX;;;GAGG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG;IACvB,cAAc,EAAE,IAAI;IACpB,UAAU,EAAE,IAAI;IAChB,kBAAkB,EAAE,IAAI;IACxB,gBAAgB,EAAE,IAAI;IAEtB,SAAS,EAAE,IAAI;IACf,eAAe,EAAE,IAAI;IACrB,UAAU,EAAE,IAAI;IAChB,iBAAiB,EAAE,IAAI;CACf,CAAC","sourcesContent":["/**\n * Colyseus protocol codes range between 0~100\n * Use codes between 0~127 for lesser throughput (1 byte)\n */\nexport const Protocol = {\n // Room-related (10~19)\n JOIN_ROOM: 10,\n ERROR: 11,\n LEAVE_ROOM: 12,\n ROOM_DATA: 13,\n ROOM_STATE: 14,\n ROOM_STATE_PATCH: 15,\n ROOM_DATA_SCHEMA: 16, // DEPRECATED: used to send schema instances via room.send()\n ROOM_DATA_BYTES: 17,\n PING: 18,\n} as const;\nexport type Protocol = typeof Protocol[keyof typeof Protocol];\n\n/**\n * HTTP MatchMaking Error Codes\n */\nexport const ErrorCode = {\n MATCHMAKE_NO_HANDLER: 520,\n MATCHMAKE_INVALID_CRITERIA: 521,\n MATCHMAKE_INVALID_ROOM_ID: 522,\n MATCHMAKE_UNHANDLED: 523, // generic exception during onCreate/onJoin\n MATCHMAKE_EXPIRED: 524, // generic exception during onCreate/onJoin\n AUTH_FAILED: 525,\n APPLICATION_ERROR: 526,\n\n INVALID_PAYLOAD: 4217,\n} as const;\nexport type ErrorCode = typeof ErrorCode[keyof typeof ErrorCode];\n\n/**\n * WebSocket close codes\n * (See https://github.com/Luka967/websocket-close-codes)\n */\nexport const CloseCode = {\n NORMAL_CLOSURE: 1000,\n GOING_AWAY: 1001,\n NO_STATUS_RECEIVED: 1005,\n ABNORMAL_CLOSURE: 1006,\n\n CONSENTED: 4000,\n SERVER_SHUTDOWN: 4001,\n WITH_ERROR: 4002,\n MAY_TRY_RECONNECT: 4010\n} as const;\nexport type CloseCode = typeof CloseCode[keyof typeof CloseCode];\n"]}
@@ -32,7 +32,7 @@ var CloseCode = {
32
32
  CONSENTED: 4e3,
33
33
  SERVER_SHUTDOWN: 4001,
34
34
  WITH_ERROR: 4002,
35
- DEVMODE_RESTART: 4010
35
+ MAY_TRY_RECONNECT: 4010
36
36
  };
37
37
  export {
38
38
  CloseCode,
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/Protocol.ts"],
4
- "sourcesContent": ["/**\n * Colyseus protocol codes range between 0~100\n * Use codes between 0~127 for lesser throughput (1 byte)\n */\nexport const Protocol = {\n // Room-related (10~19)\n JOIN_ROOM: 10,\n ERROR: 11,\n LEAVE_ROOM: 12,\n ROOM_DATA: 13,\n ROOM_STATE: 14,\n ROOM_STATE_PATCH: 15,\n ROOM_DATA_SCHEMA: 16, // DEPRECATED: used to send schema instances via room.send()\n ROOM_DATA_BYTES: 17,\n PING: 18,\n} as const;\nexport type Protocol = typeof Protocol[keyof typeof Protocol];\n\n/**\n * HTTP MatchMaking Error Codes\n */\nexport const ErrorCode = {\n MATCHMAKE_NO_HANDLER: 520,\n MATCHMAKE_INVALID_CRITERIA: 521,\n MATCHMAKE_INVALID_ROOM_ID: 522,\n MATCHMAKE_UNHANDLED: 523, // generic exception during onCreate/onJoin\n MATCHMAKE_EXPIRED: 524, // generic exception during onCreate/onJoin\n AUTH_FAILED: 525,\n APPLICATION_ERROR: 526,\n\n INVALID_PAYLOAD: 4217,\n} as const;\nexport type ErrorCode = typeof ErrorCode[keyof typeof ErrorCode];\n\n/**\n * WebSocket close codes\n * (See https://github.com/Luka967/websocket-close-codes)\n */\nexport const CloseCode = {\n NORMAL_CLOSURE: 1000,\n GOING_AWAY: 1001,\n NO_STATUS_RECEIVED: 1005,\n ABNORMAL_CLOSURE: 1006,\n\n CONSENTED: 4000,\n SERVER_SHUTDOWN: 4001,\n WITH_ERROR: 4002,\n DEVMODE_RESTART: 4010\n} as const;\nexport type CloseCode = typeof CloseCode[keyof typeof CloseCode];\n"],
5
- "mappings": ";AAIO,IAAM,WAAW;AAAA;AAAA,EAEtB,WAAW;AAAA,EACX,OAAO;AAAA,EACP,YAAY;AAAA,EACZ,WAAW;AAAA,EACX,YAAY;AAAA,EACZ,kBAAkB;AAAA,EAClB,kBAAkB;AAAA;AAAA,EAClB,iBAAiB;AAAA,EACjB,MAAM;AACR;AAMO,IAAM,YAAY;AAAA,EACvB,sBAAsB;AAAA,EACtB,4BAA4B;AAAA,EAC5B,2BAA2B;AAAA,EAC3B,qBAAqB;AAAA;AAAA,EACrB,mBAAmB;AAAA;AAAA,EACnB,aAAa;AAAA,EACb,mBAAmB;AAAA,EAEnB,iBAAiB;AACnB;AAOO,IAAM,YAAY;AAAA,EACvB,gBAAgB;AAAA,EAChB,YAAY;AAAA,EACZ,oBAAoB;AAAA,EACpB,kBAAkB;AAAA,EAElB,WAAW;AAAA,EACX,iBAAiB;AAAA,EACjB,YAAY;AAAA,EACZ,iBAAiB;AACnB;",
4
+ "sourcesContent": ["/**\n * Colyseus protocol codes range between 0~100\n * Use codes between 0~127 for lesser throughput (1 byte)\n */\nexport const Protocol = {\n // Room-related (10~19)\n JOIN_ROOM: 10,\n ERROR: 11,\n LEAVE_ROOM: 12,\n ROOM_DATA: 13,\n ROOM_STATE: 14,\n ROOM_STATE_PATCH: 15,\n ROOM_DATA_SCHEMA: 16, // DEPRECATED: used to send schema instances via room.send()\n ROOM_DATA_BYTES: 17,\n PING: 18,\n} as const;\nexport type Protocol = typeof Protocol[keyof typeof Protocol];\n\n/**\n * HTTP MatchMaking Error Codes\n */\nexport const ErrorCode = {\n MATCHMAKE_NO_HANDLER: 520,\n MATCHMAKE_INVALID_CRITERIA: 521,\n MATCHMAKE_INVALID_ROOM_ID: 522,\n MATCHMAKE_UNHANDLED: 523, // generic exception during onCreate/onJoin\n MATCHMAKE_EXPIRED: 524, // generic exception during onCreate/onJoin\n AUTH_FAILED: 525,\n APPLICATION_ERROR: 526,\n\n INVALID_PAYLOAD: 4217,\n} as const;\nexport type ErrorCode = typeof ErrorCode[keyof typeof ErrorCode];\n\n/**\n * WebSocket close codes\n * (See https://github.com/Luka967/websocket-close-codes)\n */\nexport const CloseCode = {\n NORMAL_CLOSURE: 1000,\n GOING_AWAY: 1001,\n NO_STATUS_RECEIVED: 1005,\n ABNORMAL_CLOSURE: 1006,\n\n CONSENTED: 4000,\n SERVER_SHUTDOWN: 4001,\n WITH_ERROR: 4002,\n MAY_TRY_RECONNECT: 4010\n} as const;\nexport type CloseCode = typeof CloseCode[keyof typeof CloseCode];\n"],
5
+ "mappings": ";AAIO,IAAM,WAAW;AAAA;AAAA,EAEtB,WAAW;AAAA,EACX,OAAO;AAAA,EACP,YAAY;AAAA,EACZ,WAAW;AAAA,EACX,YAAY;AAAA,EACZ,kBAAkB;AAAA,EAClB,kBAAkB;AAAA;AAAA,EAClB,iBAAiB;AAAA,EACjB,MAAM;AACR;AAMO,IAAM,YAAY;AAAA,EACvB,sBAAsB;AAAA,EACtB,4BAA4B;AAAA,EAC5B,2BAA2B;AAAA,EAC3B,qBAAqB;AAAA;AAAA,EACrB,mBAAmB;AAAA;AAAA,EACnB,aAAa;AAAA,EACb,mBAAmB;AAAA,EAEnB,iBAAiB;AACnB;AAOO,IAAM,YAAY;AAAA,EACvB,gBAAgB;AAAA,EAChB,YAAY;AAAA,EACZ,oBAAoB;AAAA,EACpB,kBAAkB;AAAA,EAElB,WAAW;AAAA,EACX,iBAAiB;AAAA,EACjB,YAAY;AAAA,EACZ,mBAAmB;AACrB;",
6
6
  "names": []
7
7
  }
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/index.ts"],
4
- "sourcesContent": ["import type { StandardSchemaV1 } from '@standard-schema/spec';\n\n// Re-export StandardSchemaV1 for convenience\nexport type { StandardSchemaV1 };\n\n// Re-export Protocol types\nexport { Protocol, ErrorCode, CloseCode } from './Protocol.js';\n\n/**\n * Minimal Room-like interface for SDK type inference.\n * Allows typing SDK methods without depending on @colyseus/core.\n */\nexport interface ServerRoomLike<State = any, Options = any> {\n state: State;\n onJoin: (client: any, options?: Options, auth?: any) => any;\n messages?: Record<string, any>;\n '~client'?: { '~messages'?: Record<string, any> };\n}\n\n/**\n * Seat reservation returned by matchmaking operations.\n */\nexport interface ISeatReservation {\n name: string;\n sessionId: string;\n roomId: string;\n publicAddress?: string;\n processId?: string;\n reconnectionToken?: string;\n devMode?: boolean;\n}\n\n/**\n * Helper types for flexible Room generics on the client SDK.\n * Allows: Room<State>, Room<ServerRoom>, or Room<ServerRoom, State>\n */\nexport type InferState<T, S> = [S] extends [never]\n ? (T extends abstract new (...args: any) => { state: infer ST }\n ? ST // Constructor type (typeof MyRoom): extract state from instance\n : T extends { state: infer ST }\n ? ST // Instance type (MyRoom): extract state directly\n : T) // State type or other: return as-is\n : S;\n\n/**\n * Extract room messages type from a Room constructor or instance type.\n * Supports both constructor types (typeof MyRoom) and instance types (MyRoom)\n */\nexport type ExtractRoomMessages<T> = T extends abstract new (...args: any) => { messages: infer M }\n ? M\n : T extends { messages: infer M }\n ? M\n : {};\n\n/**\n * Extract client-side messages type from a Room constructor or instance type.\n * These are messages that the server can send to the client.\n */\nexport type ExtractRoomClientMessages<T> = T extends abstract new (...args: any) => { '~client': { '~messages': infer M } }\n ? M\n : T extends { '~client': { '~messages': infer M } }\n ? M\n : {};\n\n/**\n * Message handler with automatic type inference from format schema.\n * When a format is provided, the message type is automatically inferred from the schema.\n *\n * @template T - The StandardSchema type for message validation\n * @template Client - The client type (from @colyseus/core Transport)\n * @template This - The Room class context\n */\nexport type MessageHandlerWithFormat<T extends StandardSchemaV1 = any, Client = any, This = any> = {\n format: T;\n handler: (this: This, client: Client, message: StandardSchemaV1.InferOutput<T>) => void;\n};\n\n/**\n * Message handler type that can be either a function or a format handler with validation.\n *\n * @template Client - The client type (from @colyseus/core Transport)\n * @template This - The Room class context\n */\nexport type MessageHandler<Client = any, This = any> =\n | ((this: This, client: Client, message: any) => void)\n | MessageHandlerWithFormat<any, Client, This>;\n\n/**\n * Extract the message payload type from a message handler.\n * Works with both function handlers and format handlers.\n */\nexport type ExtractMessageType<T> =\n T extends { format: infer Format extends StandardSchemaV1; handler: any }\n ? StandardSchemaV1.InferOutput<Format>\n : T extends (this: any, client: any, message: infer Message) => void\n ? Message\n : any;\n\n/**\n * A map of message types to message handlers.\n *\n * @template Room - The Room class type\n * @template Client - The client type\n */\nexport type Messages<Room = any, Client = any> = Record<string, MessageHandler<Client, Room>>;\n\n/**\n * Exposed types for the client-side SDK.\n * Used by defineServer() to expose room and route types to the client.\n *\n * @template RoomTypes - Record of room names to their RegisteredHandler types\n * @template Routes - Router type from @colyseus/better-call\n */\nexport interface SDKTypes<\n RoomTypes extends Record<string, any> = any,\n Routes = any\n> {\n '~rooms': RoomTypes;\n '~routes': Routes;\n}\n"],
4
+ "sourcesContent": ["import type { StandardSchemaV1 } from '@standard-schema/spec';\n\n// Re-export StandardSchemaV1 for convenience\nexport type { StandardSchemaV1 };\n\n// Re-export Protocol types\nexport { Protocol, ErrorCode, CloseCode } from './Protocol.js';\n\n/**\n * Minimal Room-like interface for SDK type inference.\n * Allows typing SDK methods without depending on @colyseus/core.\n */\nexport interface ServerRoomLike<State = any, Options = any> {\n state: State;\n onJoin: (client: any, options?: Options, auth?: any) => any;\n messages?: Record<string, any>;\n '~client'?: { '~messages'?: Record<string, any> };\n}\n\n/**\n * Seat reservation returned by matchmaking operations.\n */\nexport interface ISeatReservation {\n name: string;\n sessionId: string;\n roomId: string;\n publicAddress?: string;\n processId?: string;\n reconnectionToken?: string;\n devMode?: boolean;\n}\n\n/**\n * Helper types for flexible Room generics on the client SDK.\n * Allows: Room<State>, Room<ServerRoom>, or Room<ServerRoom, State>\n *\n * Uses `~state` phantom property to distinguish Room types from plain state types.\n * This prevents incorrectly extracting `state` from a state type that happens to have a `state` property.\n */\nexport type InferState<T, S> = [S] extends [never]\n ? (T extends abstract new (...args: any) => { '~state': infer ST }\n ? ST // Constructor type (typeof MyRoom): extract state via ~state phantom\n : T extends { '~state': infer ST }\n ? ST // Instance type (MyRoom): extract state via ~state phantom\n : T) // State type or other: return as-is\n : S;\n\n/**\n * Normalizes T for message extraction: returns T if it has ~state (Room type),\n * otherwise returns any (plain state type). This ensures Room<State> is equivalent\n * to Room<any, State> when State doesn't have ~state.\n */\nexport type NormalizeRoomType<T> = T extends abstract new (...args: any) => { '~state': any }\n ? T // Constructor type with ~state: keep as-is\n : T extends { '~state': any }\n ? T // Instance type with ~state: keep as-is\n : any; // Plain state type: normalize to any\n\n/**\n * Extract room messages type from a Room constructor or instance type.\n * Supports both constructor types (typeof MyRoom) and instance types (MyRoom)\n */\nexport type ExtractRoomMessages<T> = T extends abstract new (...args: any) => { messages: infer M }\n ? M\n : T extends { messages: infer M }\n ? M\n : {};\n\n/**\n * Extract client-side messages type from a Room constructor or instance type.\n * These are messages that the server can send to the client.\n */\nexport type ExtractRoomClientMessages<T> = T extends abstract new (...args: any) => { '~client': { '~messages': infer M } }\n ? M\n : T extends { '~client': { '~messages': infer M } }\n ? M\n : {};\n\n/**\n * Message handler with automatic type inference from format schema.\n * When a format is provided, the message type is automatically inferred from the schema.\n *\n * @template T - The StandardSchema type for message validation\n * @template Client - The client type (from @colyseus/core Transport)\n * @template This - The Room class context\n */\nexport type MessageHandlerWithFormat<T extends StandardSchemaV1 = any, Client = any, This = any> = {\n format: T;\n handler: (this: This, client: Client, message: StandardSchemaV1.InferOutput<T>) => void;\n};\n\n/**\n * Message handler type that can be either a function or a format handler with validation.\n *\n * @template Client - The client type (from @colyseus/core Transport)\n * @template This - The Room class context\n */\nexport type MessageHandler<Client = any, This = any> =\n | ((this: This, client: Client, message: any) => void)\n | MessageHandlerWithFormat<any, Client, This>;\n\n/**\n * Extract the message payload type from a message handler.\n * Works with both function handlers and format handlers.\n */\nexport type ExtractMessageType<T> =\n T extends { format: infer Format extends StandardSchemaV1; handler: any }\n ? StandardSchemaV1.InferOutput<Format>\n : T extends (this: any, client: any, message: infer Message) => void\n ? Message\n : any;\n\n/**\n * A map of message types to message handlers.\n *\n * @template Room - The Room class type\n * @template Client - The client type\n */\nexport type Messages<Room = any, Client = any> = Record<string, MessageHandler<Client, Room>>;\n\n/**\n * Exposed types for the client-side SDK.\n * Used by defineServer() to expose room and route types to the client.\n *\n * @template RoomTypes - Record of room names to their RegisteredHandler types\n * @template Routes - Router type from @colyseus/better-call\n */\nexport interface SDKTypes<\n RoomTypes extends Record<string, any> = any,\n Routes = any\n> {\n '~rooms': RoomTypes;\n '~routes': Routes;\n}\n"],
5
5
  "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAMA,sBAA+C;",
6
6
  "names": []
7
7
  }
package/build/index.d.ts CHANGED
@@ -28,12 +28,25 @@ export interface ISeatReservation {
28
28
  /**
29
29
  * Helper types for flexible Room generics on the client SDK.
30
30
  * Allows: Room<State>, Room<ServerRoom>, or Room<ServerRoom, State>
31
+ *
32
+ * Uses `~state` phantom property to distinguish Room types from plain state types.
33
+ * This prevents incorrectly extracting `state` from a state type that happens to have a `state` property.
31
34
  */
32
35
  export type InferState<T, S> = [S] extends [never] ? (T extends abstract new (...args: any) => {
33
- state: infer ST;
36
+ '~state': infer ST;
34
37
  } ? ST : T extends {
35
- state: infer ST;
38
+ '~state': infer ST;
36
39
  } ? ST : T) : S;
40
+ /**
41
+ * Normalizes T for message extraction: returns T if it has ~state (Room type),
42
+ * otherwise returns any (plain state type). This ensures Room<State> is equivalent
43
+ * to Room<any, State> when State doesn't have ~state.
44
+ */
45
+ export type NormalizeRoomType<T> = T extends abstract new (...args: any) => {
46
+ '~state': any;
47
+ } ? T : T extends {
48
+ '~state': any;
49
+ } ? T : any;
37
50
  /**
38
51
  * Extract room messages type from a Room constructor or instance type.
39
52
  * Supports both constructor types (typeof MyRoom) and instance types (MyRoom)
package/build/index.js CHANGED
@@ -1,2 +1,3 @@
1
- export {};
1
+ // Re-export Protocol types
2
+ export { Protocol, ErrorCode, CloseCode } from './Protocol.js';
2
3
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"","sourcesContent":["import type { StandardSchemaV1 } from '@standard-schema/spec';\n\n// Re-export StandardSchemaV1 for convenience\nexport type { StandardSchemaV1 };\n\n/**\n * Minimal Room-like interface for SDK type inference.\n * Allows typing SDK methods without depending on @colyseus/core.\n */\nexport interface ServerRoomLike<State = any, Options = any> {\n state: State;\n onJoin: (client: any, options?: Options, auth?: any) => any;\n messages?: Record<string, any>;\n '~client'?: { '~messages'?: Record<string, any> };\n}\n\n/**\n * Seat reservation returned by matchmaking operations.\n */\nexport interface ISeatReservation {\n name: string;\n sessionId: string;\n roomId: string;\n publicAddress?: string;\n processId?: string;\n reconnectionToken?: string;\n devMode?: boolean;\n}\n\n/**\n * Helper types for flexible Room generics on the client SDK.\n * Allows: Room<State>, Room<ServerRoom>, or Room<ServerRoom, State>\n */\nexport type InferState<T, S> = [S] extends [never]\n ? (T extends abstract new (...args: any) => { state: infer ST }\n ? ST // Constructor type (typeof MyRoom): extract state from instance\n : T extends { state: infer ST }\n ? ST // Instance type (MyRoom): extract state directly\n : T) // State type or other: return as-is\n : S;\n\n/**\n * Extract room messages type from a Room constructor or instance type.\n * Supports both constructor types (typeof MyRoom) and instance types (MyRoom)\n */\nexport type ExtractRoomMessages<T> = T extends abstract new (...args: any) => { messages: infer M }\n ? M\n : T extends { messages: infer M }\n ? M\n : {};\n\n/**\n * Extract client-side messages type from a Room constructor or instance type.\n * These are messages that the server can send to the client.\n */\nexport type ExtractRoomClientMessages<T> = T extends abstract new (...args: any) => { '~client': { '~messages': infer M } }\n ? M\n : T extends { '~client': { '~messages': infer M } }\n ? M\n : {};\n\n/**\n * Message handler with automatic type inference from format schema.\n * When a format is provided, the message type is automatically inferred from the schema.\n *\n * @template T - The StandardSchema type for message validation\n * @template Client - The client type (from @colyseus/core Transport)\n * @template This - The Room class context\n */\nexport type MessageHandlerWithFormat<T extends StandardSchemaV1 = any, Client = any, This = any> = {\n format: T;\n handler: (this: This, client: Client, message: StandardSchemaV1.InferOutput<T>) => void;\n};\n\n/**\n * Message handler type that can be either a function or a format handler with validation.\n *\n * @template Client - The client type (from @colyseus/core Transport)\n * @template This - The Room class context\n */\nexport type MessageHandler<Client = any, This = any> =\n | ((this: This, client: Client, message: any) => void)\n | MessageHandlerWithFormat<any, Client, This>;\n\n/**\n * Extract the message payload type from a message handler.\n * Works with both function handlers and format handlers.\n */\nexport type ExtractMessageType<T> =\n T extends { format: infer Format extends StandardSchemaV1; handler: any }\n ? StandardSchemaV1.InferOutput<Format>\n : T extends (this: any, client: any, message: infer Message) => void\n ? Message\n : any;\n\n/**\n * A map of message types to message handlers.\n *\n * @template Room - The Room class type\n * @template Client - The client type\n */\nexport type Messages<Room = any, Client = any> = Record<string, MessageHandler<Client, Room>>;\n\n/**\n * Exposed types for the client-side SDK.\n * Used by defineServer() to expose room and route types to the client.\n *\n * @template RoomTypes - Record of room names to their RegisteredHandler types\n * @template Routes - Router type from @colyseus/better-call\n */\nexport interface SDKTypes<\n RoomTypes extends Record<string, any> = any,\n Routes = any\n> {\n '~rooms': RoomTypes;\n '~routes': Routes;\n}\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAKA,2BAA2B;AAC3B,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC","sourcesContent":["import type { StandardSchemaV1 } from '@standard-schema/spec';\n\n// Re-export StandardSchemaV1 for convenience\nexport type { StandardSchemaV1 };\n\n// Re-export Protocol types\nexport { Protocol, ErrorCode, CloseCode } from './Protocol.js';\n\n/**\n * Minimal Room-like interface for SDK type inference.\n * Allows typing SDK methods without depending on @colyseus/core.\n */\nexport interface ServerRoomLike<State = any, Options = any> {\n state: State;\n onJoin: (client: any, options?: Options, auth?: any) => any;\n messages?: Record<string, any>;\n '~client'?: { '~messages'?: Record<string, any> };\n}\n\n/**\n * Seat reservation returned by matchmaking operations.\n */\nexport interface ISeatReservation {\n name: string;\n sessionId: string;\n roomId: string;\n publicAddress?: string;\n processId?: string;\n reconnectionToken?: string;\n devMode?: boolean;\n}\n\n/**\n * Helper types for flexible Room generics on the client SDK.\n * Allows: Room<State>, Room<ServerRoom>, or Room<ServerRoom, State>\n *\n * Uses `~state` phantom property to distinguish Room types from plain state types.\n * This prevents incorrectly extracting `state` from a state type that happens to have a `state` property.\n */\nexport type InferState<T, S> = [S] extends [never]\n ? (T extends abstract new (...args: any) => { '~state': infer ST }\n ? ST // Constructor type (typeof MyRoom): extract state via ~state phantom\n : T extends { '~state': infer ST }\n ? ST // Instance type (MyRoom): extract state via ~state phantom\n : T) // State type or other: return as-is\n : S;\n\n/**\n * Normalizes T for message extraction: returns T if it has ~state (Room type),\n * otherwise returns any (plain state type). This ensures Room<State> is equivalent\n * to Room<any, State> when State doesn't have ~state.\n */\nexport type NormalizeRoomType<T> = T extends abstract new (...args: any) => { '~state': any }\n ? T // Constructor type with ~state: keep as-is\n : T extends { '~state': any }\n ? T // Instance type with ~state: keep as-is\n : any; // Plain state type: normalize to any\n\n/**\n * Extract room messages type from a Room constructor or instance type.\n * Supports both constructor types (typeof MyRoom) and instance types (MyRoom)\n */\nexport type ExtractRoomMessages<T> = T extends abstract new (...args: any) => { messages: infer M }\n ? M\n : T extends { messages: infer M }\n ? M\n : {};\n\n/**\n * Extract client-side messages type from a Room constructor or instance type.\n * These are messages that the server can send to the client.\n */\nexport type ExtractRoomClientMessages<T> = T extends abstract new (...args: any) => { '~client': { '~messages': infer M } }\n ? M\n : T extends { '~client': { '~messages': infer M } }\n ? M\n : {};\n\n/**\n * Message handler with automatic type inference from format schema.\n * When a format is provided, the message type is automatically inferred from the schema.\n *\n * @template T - The StandardSchema type for message validation\n * @template Client - The client type (from @colyseus/core Transport)\n * @template This - The Room class context\n */\nexport type MessageHandlerWithFormat<T extends StandardSchemaV1 = any, Client = any, This = any> = {\n format: T;\n handler: (this: This, client: Client, message: StandardSchemaV1.InferOutput<T>) => void;\n};\n\n/**\n * Message handler type that can be either a function or a format handler with validation.\n *\n * @template Client - The client type (from @colyseus/core Transport)\n * @template This - The Room class context\n */\nexport type MessageHandler<Client = any, This = any> =\n | ((this: This, client: Client, message: any) => void)\n | MessageHandlerWithFormat<any, Client, This>;\n\n/**\n * Extract the message payload type from a message handler.\n * Works with both function handlers and format handlers.\n */\nexport type ExtractMessageType<T> =\n T extends { format: infer Format extends StandardSchemaV1; handler: any }\n ? StandardSchemaV1.InferOutput<Format>\n : T extends (this: any, client: any, message: infer Message) => void\n ? Message\n : any;\n\n/**\n * A map of message types to message handlers.\n *\n * @template Room - The Room class type\n * @template Client - The client type\n */\nexport type Messages<Room = any, Client = any> = Record<string, MessageHandler<Client, Room>>;\n\n/**\n * Exposed types for the client-side SDK.\n * Used by defineServer() to expose room and route types to the client.\n *\n * @template RoomTypes - Record of room names to their RegisteredHandler types\n * @template Routes - Router type from @colyseus/better-call\n */\nexport interface SDKTypes<\n RoomTypes extends Record<string, any> = any,\n Routes = any\n> {\n '~rooms': RoomTypes;\n '~routes': Routes;\n}\n"]}
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/index.ts"],
4
- "sourcesContent": ["import type { StandardSchemaV1 } from '@standard-schema/spec';\n\n// Re-export StandardSchemaV1 for convenience\nexport type { StandardSchemaV1 };\n\n// Re-export Protocol types\nexport { Protocol, ErrorCode, CloseCode } from './Protocol.js';\n\n/**\n * Minimal Room-like interface for SDK type inference.\n * Allows typing SDK methods without depending on @colyseus/core.\n */\nexport interface ServerRoomLike<State = any, Options = any> {\n state: State;\n onJoin: (client: any, options?: Options, auth?: any) => any;\n messages?: Record<string, any>;\n '~client'?: { '~messages'?: Record<string, any> };\n}\n\n/**\n * Seat reservation returned by matchmaking operations.\n */\nexport interface ISeatReservation {\n name: string;\n sessionId: string;\n roomId: string;\n publicAddress?: string;\n processId?: string;\n reconnectionToken?: string;\n devMode?: boolean;\n}\n\n/**\n * Helper types for flexible Room generics on the client SDK.\n * Allows: Room<State>, Room<ServerRoom>, or Room<ServerRoom, State>\n */\nexport type InferState<T, S> = [S] extends [never]\n ? (T extends abstract new (...args: any) => { state: infer ST }\n ? ST // Constructor type (typeof MyRoom): extract state from instance\n : T extends { state: infer ST }\n ? ST // Instance type (MyRoom): extract state directly\n : T) // State type or other: return as-is\n : S;\n\n/**\n * Extract room messages type from a Room constructor or instance type.\n * Supports both constructor types (typeof MyRoom) and instance types (MyRoom)\n */\nexport type ExtractRoomMessages<T> = T extends abstract new (...args: any) => { messages: infer M }\n ? M\n : T extends { messages: infer M }\n ? M\n : {};\n\n/**\n * Extract client-side messages type from a Room constructor or instance type.\n * These are messages that the server can send to the client.\n */\nexport type ExtractRoomClientMessages<T> = T extends abstract new (...args: any) => { '~client': { '~messages': infer M } }\n ? M\n : T extends { '~client': { '~messages': infer M } }\n ? M\n : {};\n\n/**\n * Message handler with automatic type inference from format schema.\n * When a format is provided, the message type is automatically inferred from the schema.\n *\n * @template T - The StandardSchema type for message validation\n * @template Client - The client type (from @colyseus/core Transport)\n * @template This - The Room class context\n */\nexport type MessageHandlerWithFormat<T extends StandardSchemaV1 = any, Client = any, This = any> = {\n format: T;\n handler: (this: This, client: Client, message: StandardSchemaV1.InferOutput<T>) => void;\n};\n\n/**\n * Message handler type that can be either a function or a format handler with validation.\n *\n * @template Client - The client type (from @colyseus/core Transport)\n * @template This - The Room class context\n */\nexport type MessageHandler<Client = any, This = any> =\n | ((this: This, client: Client, message: any) => void)\n | MessageHandlerWithFormat<any, Client, This>;\n\n/**\n * Extract the message payload type from a message handler.\n * Works with both function handlers and format handlers.\n */\nexport type ExtractMessageType<T> =\n T extends { format: infer Format extends StandardSchemaV1; handler: any }\n ? StandardSchemaV1.InferOutput<Format>\n : T extends (this: any, client: any, message: infer Message) => void\n ? Message\n : any;\n\n/**\n * A map of message types to message handlers.\n *\n * @template Room - The Room class type\n * @template Client - The client type\n */\nexport type Messages<Room = any, Client = any> = Record<string, MessageHandler<Client, Room>>;\n\n/**\n * Exposed types for the client-side SDK.\n * Used by defineServer() to expose room and route types to the client.\n *\n * @template RoomTypes - Record of room names to their RegisteredHandler types\n * @template Routes - Router type from @colyseus/better-call\n */\nexport interface SDKTypes<\n RoomTypes extends Record<string, any> = any,\n Routes = any\n> {\n '~rooms': RoomTypes;\n '~routes': Routes;\n}\n"],
4
+ "sourcesContent": ["import type { StandardSchemaV1 } from '@standard-schema/spec';\n\n// Re-export StandardSchemaV1 for convenience\nexport type { StandardSchemaV1 };\n\n// Re-export Protocol types\nexport { Protocol, ErrorCode, CloseCode } from './Protocol.js';\n\n/**\n * Minimal Room-like interface for SDK type inference.\n * Allows typing SDK methods without depending on @colyseus/core.\n */\nexport interface ServerRoomLike<State = any, Options = any> {\n state: State;\n onJoin: (client: any, options?: Options, auth?: any) => any;\n messages?: Record<string, any>;\n '~client'?: { '~messages'?: Record<string, any> };\n}\n\n/**\n * Seat reservation returned by matchmaking operations.\n */\nexport interface ISeatReservation {\n name: string;\n sessionId: string;\n roomId: string;\n publicAddress?: string;\n processId?: string;\n reconnectionToken?: string;\n devMode?: boolean;\n}\n\n/**\n * Helper types for flexible Room generics on the client SDK.\n * Allows: Room<State>, Room<ServerRoom>, or Room<ServerRoom, State>\n *\n * Uses `~state` phantom property to distinguish Room types from plain state types.\n * This prevents incorrectly extracting `state` from a state type that happens to have a `state` property.\n */\nexport type InferState<T, S> = [S] extends [never]\n ? (T extends abstract new (...args: any) => { '~state': infer ST }\n ? ST // Constructor type (typeof MyRoom): extract state via ~state phantom\n : T extends { '~state': infer ST }\n ? ST // Instance type (MyRoom): extract state via ~state phantom\n : T) // State type or other: return as-is\n : S;\n\n/**\n * Normalizes T for message extraction: returns T if it has ~state (Room type),\n * otherwise returns any (plain state type). This ensures Room<State> is equivalent\n * to Room<any, State> when State doesn't have ~state.\n */\nexport type NormalizeRoomType<T> = T extends abstract new (...args: any) => { '~state': any }\n ? T // Constructor type with ~state: keep as-is\n : T extends { '~state': any }\n ? T // Instance type with ~state: keep as-is\n : any; // Plain state type: normalize to any\n\n/**\n * Extract room messages type from a Room constructor or instance type.\n * Supports both constructor types (typeof MyRoom) and instance types (MyRoom)\n */\nexport type ExtractRoomMessages<T> = T extends abstract new (...args: any) => { messages: infer M }\n ? M\n : T extends { messages: infer M }\n ? M\n : {};\n\n/**\n * Extract client-side messages type from a Room constructor or instance type.\n * These are messages that the server can send to the client.\n */\nexport type ExtractRoomClientMessages<T> = T extends abstract new (...args: any) => { '~client': { '~messages': infer M } }\n ? M\n : T extends { '~client': { '~messages': infer M } }\n ? M\n : {};\n\n/**\n * Message handler with automatic type inference from format schema.\n * When a format is provided, the message type is automatically inferred from the schema.\n *\n * @template T - The StandardSchema type for message validation\n * @template Client - The client type (from @colyseus/core Transport)\n * @template This - The Room class context\n */\nexport type MessageHandlerWithFormat<T extends StandardSchemaV1 = any, Client = any, This = any> = {\n format: T;\n handler: (this: This, client: Client, message: StandardSchemaV1.InferOutput<T>) => void;\n};\n\n/**\n * Message handler type that can be either a function or a format handler with validation.\n *\n * @template Client - The client type (from @colyseus/core Transport)\n * @template This - The Room class context\n */\nexport type MessageHandler<Client = any, This = any> =\n | ((this: This, client: Client, message: any) => void)\n | MessageHandlerWithFormat<any, Client, This>;\n\n/**\n * Extract the message payload type from a message handler.\n * Works with both function handlers and format handlers.\n */\nexport type ExtractMessageType<T> =\n T extends { format: infer Format extends StandardSchemaV1; handler: any }\n ? StandardSchemaV1.InferOutput<Format>\n : T extends (this: any, client: any, message: infer Message) => void\n ? Message\n : any;\n\n/**\n * A map of message types to message handlers.\n *\n * @template Room - The Room class type\n * @template Client - The client type\n */\nexport type Messages<Room = any, Client = any> = Record<string, MessageHandler<Client, Room>>;\n\n/**\n * Exposed types for the client-side SDK.\n * Used by defineServer() to expose room and route types to the client.\n *\n * @template RoomTypes - Record of room names to their RegisteredHandler types\n * @template Routes - Router type from @colyseus/better-call\n */\nexport interface SDKTypes<\n RoomTypes extends Record<string, any> = any,\n Routes = any\n> {\n '~rooms': RoomTypes;\n '~routes': Routes;\n}\n"],
5
5
  "mappings": ";AAMA,SAAS,UAAU,WAAW,iBAAiB;",
6
6
  "names": []
7
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@colyseus/shared-types",
3
- "version": "0.17.1",
3
+ "version": "0.17.2",
4
4
  "description": "Shared TypeScript types for Colyseus server and client SDK",
5
5
  "type": "module",
6
6
  "input": "./src/index.ts",
@@ -45,7 +45,15 @@
45
45
  "dependencies": {
46
46
  "@standard-schema/spec": "^1.0.0"
47
47
  },
48
+ "devDependencies": {
49
+ "vitest": "^3.1.1",
50
+ "@colyseus/sdk": "^0.17.18",
51
+ "@colyseus/core": "^0.17.17"
52
+ },
48
53
  "publishConfig": {
49
54
  "access": "public"
55
+ },
56
+ "scripts": {
57
+ "test": "vitest"
50
58
  }
51
59
  }
package/src/Protocol.ts CHANGED
@@ -45,6 +45,6 @@ export const CloseCode = {
45
45
  CONSENTED: 4000,
46
46
  SERVER_SHUTDOWN: 4001,
47
47
  WITH_ERROR: 4002,
48
- DEVMODE_RESTART: 4010
48
+ MAY_TRY_RECONNECT: 4010
49
49
  } as const;
50
50
  export type CloseCode = typeof CloseCode[keyof typeof CloseCode];
package/src/index.ts CHANGED
@@ -33,15 +33,29 @@ export interface ISeatReservation {
33
33
  /**
34
34
  * Helper types for flexible Room generics on the client SDK.
35
35
  * Allows: Room<State>, Room<ServerRoom>, or Room<ServerRoom, State>
36
+ *
37
+ * Uses `~state` phantom property to distinguish Room types from plain state types.
38
+ * This prevents incorrectly extracting `state` from a state type that happens to have a `state` property.
36
39
  */
37
40
  export type InferState<T, S> = [S] extends [never]
38
- ? (T extends abstract new (...args: any) => { state: infer ST }
39
- ? ST // Constructor type (typeof MyRoom): extract state from instance
40
- : T extends { state: infer ST }
41
- ? ST // Instance type (MyRoom): extract state directly
41
+ ? (T extends abstract new (...args: any) => { '~state': infer ST }
42
+ ? ST // Constructor type (typeof MyRoom): extract state via ~state phantom
43
+ : T extends { '~state': infer ST }
44
+ ? ST // Instance type (MyRoom): extract state via ~state phantom
42
45
  : T) // State type or other: return as-is
43
46
  : S;
44
47
 
48
+ /**
49
+ * Normalizes T for message extraction: returns T if it has ~state (Room type),
50
+ * otherwise returns any (plain state type). This ensures Room<State> is equivalent
51
+ * to Room<any, State> when State doesn't have ~state.
52
+ */
53
+ export type NormalizeRoomType<T> = T extends abstract new (...args: any) => { '~state': any }
54
+ ? T // Constructor type with ~state: keep as-is
55
+ : T extends { '~state': any }
56
+ ? T // Instance type with ~state: keep as-is
57
+ : any; // Plain state type: normalize to any
58
+
45
59
  /**
46
60
  * Extract room messages type from a Room constructor or instance type.
47
61
  * Supports both constructor types (typeof MyRoom) and instance types (MyRoom)