@colyseus/core 0.16.23 → 0.17.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.
Files changed (224) hide show
  1. package/build/Debug.js +16 -4
  2. package/build/Debug.js.map +2 -2
  3. package/build/Debug.mjs +11 -1
  4. package/build/Debug.mjs.map +2 -2
  5. package/build/IPC.js +5 -3
  6. package/build/IPC.js.map +2 -2
  7. package/build/IPC.mjs +1 -0
  8. package/build/IPC.mjs.map +2 -2
  9. package/build/Logger.js +1 -0
  10. package/build/Logger.js.map +1 -1
  11. package/build/MatchMaker.js +148 -84
  12. package/build/MatchMaker.js.map +3 -3
  13. package/build/MatchMaker.mjs +130 -70
  14. package/build/MatchMaker.mjs.map +3 -3
  15. package/build/Protocol.js +54 -41
  16. package/build/Protocol.js.map +3 -3
  17. package/build/Protocol.mjs +52 -41
  18. package/build/Protocol.mjs.map +3 -3
  19. package/build/Room.js +430 -195
  20. package/build/Room.js.map +3 -3
  21. package/build/Room.mjs +417 -175
  22. package/build/Room.mjs.map +3 -3
  23. package/build/Server.js +48 -108
  24. package/build/Server.js.map +3 -3
  25. package/build/Server.mjs +39 -102
  26. package/build/Server.mjs.map +3 -3
  27. package/build/Stats.js +2 -1
  28. package/build/Stats.js.map +2 -2
  29. package/build/Stats.mjs.map +1 -1
  30. package/build/Transport.js +29 -11
  31. package/build/Transport.js.map +3 -3
  32. package/build/Transport.mjs +15 -9
  33. package/build/Transport.mjs.map +3 -3
  34. package/build/errors/RoomExceptions.js +9 -5
  35. package/build/errors/RoomExceptions.js.map +2 -2
  36. package/build/errors/RoomExceptions.mjs +8 -5
  37. package/build/errors/RoomExceptions.mjs.map +2 -2
  38. package/build/errors/SeatReservationError.js +1 -0
  39. package/build/errors/SeatReservationError.js.map +1 -1
  40. package/build/errors/ServerError.js +2 -1
  41. package/build/errors/ServerError.js.map +2 -2
  42. package/build/errors/ServerError.mjs.map +1 -1
  43. package/build/index.js +60 -26
  44. package/build/index.js.map +2 -2
  45. package/build/index.mjs +33 -7
  46. package/build/index.mjs.map +2 -2
  47. package/build/matchmaker/Lobby.js +13 -3
  48. package/build/matchmaker/Lobby.js.map +2 -2
  49. package/build/matchmaker/Lobby.mjs +11 -2
  50. package/build/matchmaker/Lobby.mjs.map +2 -2
  51. package/build/matchmaker/LocalDriver/LocalDriver.js +94 -0
  52. package/build/matchmaker/LocalDriver/LocalDriver.js.map +7 -0
  53. package/build/matchmaker/LocalDriver/LocalDriver.mjs +71 -0
  54. package/build/matchmaker/LocalDriver/LocalDriver.mjs.map +7 -0
  55. package/build/matchmaker/LocalDriver/Query.js +111 -0
  56. package/build/matchmaker/LocalDriver/Query.js.map +7 -0
  57. package/build/matchmaker/LocalDriver/Query.mjs +88 -0
  58. package/build/matchmaker/LocalDriver/Query.mjs.map +7 -0
  59. package/build/matchmaker/RegisteredHandler.js +57 -7
  60. package/build/matchmaker/RegisteredHandler.js.map +2 -2
  61. package/build/matchmaker/RegisteredHandler.mjs +54 -5
  62. package/build/matchmaker/RegisteredHandler.mjs.map +2 -2
  63. package/build/matchmaker/controller.js +8 -8
  64. package/build/matchmaker/controller.js.map +2 -2
  65. package/build/matchmaker/controller.mjs +4 -5
  66. package/build/matchmaker/controller.mjs.map +2 -2
  67. package/build/matchmaker/driver/api.js +21 -2
  68. package/build/matchmaker/driver/api.js.map +2 -2
  69. package/build/matchmaker/driver/api.mjs +18 -1
  70. package/build/matchmaker/driver/api.mjs.map +2 -2
  71. package/build/matchmaker/driver/local/LocalDriver.js +36 -7
  72. package/build/matchmaker/driver/local/LocalDriver.js.map +2 -2
  73. package/build/matchmaker/driver/local/LocalDriver.mjs +33 -5
  74. package/build/matchmaker/driver/local/LocalDriver.mjs.map +2 -2
  75. package/build/matchmaker/driver/local/Query.js +51 -18
  76. package/build/matchmaker/driver/local/Query.js.map +2 -2
  77. package/build/matchmaker/driver/local/Query.mjs +50 -18
  78. package/build/matchmaker/driver/local/Query.mjs.map +2 -2
  79. package/build/matchmaker/driver.js +44 -0
  80. package/build/matchmaker/driver.js.map +7 -0
  81. package/build/matchmaker/driver.mjs +20 -0
  82. package/build/matchmaker/driver.mjs.map +7 -0
  83. package/build/matchmaker/routes.js +79 -0
  84. package/build/matchmaker/routes.js.map +7 -0
  85. package/build/matchmaker/routes.mjs +45 -0
  86. package/build/matchmaker/routes.mjs.map +7 -0
  87. package/build/presence/LocalPresence.js +13 -27
  88. package/build/presence/LocalPresence.js.map +3 -3
  89. package/build/presence/LocalPresence.mjs +11 -16
  90. package/build/presence/LocalPresence.mjs.map +2 -2
  91. package/build/presence/Presence.js +37 -0
  92. package/build/presence/Presence.js.map +2 -2
  93. package/build/presence/Presence.mjs +29 -0
  94. package/build/presence/Presence.mjs.map +3 -3
  95. package/build/rooms/LobbyRoom.js +5 -5
  96. package/build/rooms/LobbyRoom.js.map +2 -2
  97. package/build/rooms/LobbyRoom.mjs +1 -2
  98. package/build/rooms/LobbyRoom.mjs.map +2 -2
  99. package/build/rooms/RankedQueueRoom.js +224 -0
  100. package/build/rooms/RankedQueueRoom.js.map +7 -0
  101. package/build/rooms/RankedQueueRoom.mjs +201 -0
  102. package/build/rooms/RankedQueueRoom.mjs.map +7 -0
  103. package/build/rooms/RelayRoom.js +6 -6
  104. package/build/rooms/RelayRoom.js.map +2 -2
  105. package/build/rooms/RelayRoom.mjs +4 -5
  106. package/build/rooms/RelayRoom.mjs.map +2 -2
  107. package/build/rooms/createRoom.js +51 -0
  108. package/build/rooms/createRoom.js.map +7 -0
  109. package/build/rooms/createRoom.mjs +28 -0
  110. package/build/rooms/createRoom.mjs.map +7 -0
  111. package/build/router/default_routes.js +79 -0
  112. package/build/router/default_routes.js.map +7 -0
  113. package/build/router/default_routes.mjs +45 -0
  114. package/build/router/default_routes.mjs.map +7 -0
  115. package/build/router/index.js +55 -0
  116. package/build/router/index.js.map +7 -0
  117. package/build/router/index.mjs +30 -0
  118. package/build/router/index.mjs.map +7 -0
  119. package/build/serializer/NoneSerializer.js +1 -0
  120. package/build/serializer/NoneSerializer.js.map +2 -2
  121. package/build/serializer/NoneSerializer.mjs.map +2 -2
  122. package/build/serializer/SchemaSerializer.js +6 -7
  123. package/build/serializer/SchemaSerializer.js.map +2 -2
  124. package/build/serializer/SchemaSerializer.mjs +3 -5
  125. package/build/serializer/SchemaSerializer.mjs.map +2 -2
  126. package/build/serializer/SchemaSerializerDebug.js +29 -0
  127. package/build/serializer/SchemaSerializerDebug.js.map +3 -3
  128. package/build/serializer/SchemaSerializerDebug.mjs +7 -0
  129. package/build/serializer/SchemaSerializerDebug.mjs.map +3 -3
  130. package/build/serializer/Serializer.js +1 -0
  131. package/build/serializer/Serializer.js.map +2 -2
  132. package/build/{Debug.d.ts → src/Debug.d.ts} +2 -1
  133. package/build/{IPC.d.ts → src/IPC.d.ts} +2 -2
  134. package/build/{MatchMaker.d.ts → src/MatchMaker.d.ts} +58 -33
  135. package/build/src/Protocol.d.ts +53 -0
  136. package/build/src/Room.d.ts +497 -0
  137. package/build/{Server.d.ts → src/Server.d.ts} +23 -25
  138. package/build/{Transport.d.ts → src/Transport.d.ts} +65 -22
  139. package/build/{errors → src/errors}/RoomExceptions.d.ts +8 -7
  140. package/build/src/index.d.ts +27 -0
  141. package/build/src/matchmaker/Lobby.d.ts +4 -0
  142. package/build/src/matchmaker/LocalDriver/LocalDriver.d.ts +17 -0
  143. package/build/src/matchmaker/LocalDriver/Query.d.ts +12 -0
  144. package/build/src/matchmaker/RegisteredHandler.d.ts +81 -0
  145. package/build/{matchmaker → src/matchmaker}/controller.d.ts +5 -6
  146. package/build/src/matchmaker/driver/api.d.ts +145 -0
  147. package/build/src/matchmaker/driver/local/LocalDriver.d.ts +17 -0
  148. package/build/src/matchmaker/driver/local/Query.d.ts +12 -0
  149. package/build/src/matchmaker/driver.d.ts +145 -0
  150. package/build/src/matchmaker/routes.d.ts +92 -0
  151. package/build/{presence → src/presence}/LocalPresence.d.ts +1 -1
  152. package/build/{presence → src/presence}/Presence.d.ts +2 -0
  153. package/build/{rooms → src/rooms}/LobbyRoom.d.ts +4 -4
  154. package/build/src/rooms/RankedQueueRoom.d.ts +125 -0
  155. package/build/{rooms → src/rooms}/RelayRoom.d.ts +5 -4
  156. package/build/src/rooms/createRoom.d.ts +65 -0
  157. package/build/src/router/default_routes.d.ts +103 -0
  158. package/build/src/router/index.d.ts +68 -0
  159. package/build/{serializer → src/serializer}/NoneSerializer.d.ts +2 -2
  160. package/build/{serializer → src/serializer}/SchemaSerializer.d.ts +9 -9
  161. package/build/{serializer → src/serializer}/Serializer.d.ts +3 -3
  162. package/build/{utils → src/utils}/DevMode.d.ts +5 -4
  163. package/build/{utils → src/utils}/StandardSchema.d.ts +1 -1
  164. package/build/{utils → src/utils}/Utils.d.ts +15 -4
  165. package/build/utils/DevMode.js +54 -26
  166. package/build/utils/DevMode.js.map +3 -3
  167. package/build/utils/DevMode.mjs +44 -19
  168. package/build/utils/DevMode.mjs.map +2 -2
  169. package/build/utils/StandardSchema.js.map +1 -1
  170. package/build/utils/StandardSchema.mjs.map +1 -1
  171. package/build/utils/Utils.js +8 -15
  172. package/build/utils/Utils.js.map +3 -3
  173. package/build/utils/Utils.mjs +6 -4
  174. package/build/utils/Utils.mjs.map +2 -2
  175. package/package.json +20 -14
  176. package/build/Protocol.d.ts +0 -37
  177. package/build/Room.d.ts +0 -265
  178. package/build/discovery/index.d.ts +0 -8
  179. package/build/discovery/index.js +0 -50
  180. package/build/discovery/index.js.map +0 -7
  181. package/build/discovery/index.mjs +0 -26
  182. package/build/discovery/index.mjs.map +0 -7
  183. package/build/index.d.ts +0 -24
  184. package/build/matchmaker/Lobby.d.ts +0 -4
  185. package/build/matchmaker/RegisteredHandler.d.ts +0 -19
  186. package/build/matchmaker/driver/Query.d.ts +0 -8
  187. package/build/matchmaker/driver/Query.js +0 -68
  188. package/build/matchmaker/driver/Query.js.map +0 -7
  189. package/build/matchmaker/driver/Query.mjs +0 -45
  190. package/build/matchmaker/driver/Query.mjs.map +0 -7
  191. package/build/matchmaker/driver/RoomData.d.ts +0 -19
  192. package/build/matchmaker/driver/RoomData.js +0 -79
  193. package/build/matchmaker/driver/RoomData.js.map +0 -7
  194. package/build/matchmaker/driver/RoomData.mjs +0 -56
  195. package/build/matchmaker/driver/RoomData.mjs.map +0 -7
  196. package/build/matchmaker/driver/api.d.ts +0 -104
  197. package/build/matchmaker/driver/index.d.ts +0 -13
  198. package/build/matchmaker/driver/index.js +0 -64
  199. package/build/matchmaker/driver/index.js.map +0 -7
  200. package/build/matchmaker/driver/index.mjs +0 -42
  201. package/build/matchmaker/driver/index.mjs.map +0 -7
  202. package/build/matchmaker/driver/interfaces.d.ts +0 -73
  203. package/build/matchmaker/driver/interfaces.js +0 -15
  204. package/build/matchmaker/driver/interfaces.js.map +0 -7
  205. package/build/matchmaker/driver/interfaces.mjs +0 -0
  206. package/build/matchmaker/driver/interfaces.mjs.map +0 -7
  207. package/build/matchmaker/driver/local/LocalDriver.d.ts +0 -13
  208. package/build/matchmaker/driver/local/Query.d.ts +0 -9
  209. package/build/matchmaker/driver/local/RoomData.d.ts +0 -19
  210. package/build/matchmaker/driver/local/RoomData.js +0 -79
  211. package/build/matchmaker/driver/local/RoomData.js.map +0 -7
  212. package/build/matchmaker/driver/local/RoomData.mjs +0 -57
  213. package/build/matchmaker/driver/local/RoomData.mjs.map +0 -7
  214. package/build/utils/types.d.ts +0 -1
  215. package/build/utils/types.js +0 -15
  216. package/build/utils/types.js.map +0 -7
  217. package/build/utils/types.mjs +0 -0
  218. package/build/utils/types.mjs.map +0 -7
  219. /package/build/{Logger.d.ts → src/Logger.d.ts} +0 -0
  220. /package/build/{Stats.d.ts → src/Stats.d.ts} +0 -0
  221. /package/build/{errors → src/errors}/SeatReservationError.d.ts +0 -0
  222. /package/build/{errors → src/errors}/ServerError.d.ts +0 -0
  223. /package/build/{serializer → src/serializer}/SchemaSerializerDebug.d.ts +0 -0
  224. /package/build/{utils → src/utils}/nanoevents.d.ts +0 -0
@@ -1,31 +1,51 @@
1
1
  import * as http from 'http';
2
2
  import * as https from 'https';
3
- import * as net from 'net';
4
- import { Schema, StateView } from '@colyseus/schema';
3
+ import { StateView } from '@colyseus/schema';
5
4
  import { EventEmitter } from 'events';
5
+ import type { Room } from './Room.ts';
6
6
  export declare abstract class Transport {
7
7
  protocol?: string;
8
- server?: net.Server | http.Server | https.Server;
8
+ server?: http.Server | https.Server;
9
9
  abstract listen(port?: number, hostname?: string, backlog?: number, listeningListener?: Function): this;
10
10
  abstract shutdown(): void;
11
11
  abstract simulateLatency(milliseconds: number): void;
12
12
  }
13
13
  export type AuthContext = {
14
14
  token?: string;
15
- headers: http.IncomingHttpHeaders;
15
+ headers: Headers;
16
16
  ip: string | string[];
17
- req?: http.IncomingMessage;
17
+ req?: any;
18
18
  };
19
19
  export interface ISendOptions {
20
20
  afterNextPatch?: boolean;
21
21
  }
22
- export declare enum ClientState {
23
- JOINING = 0,
24
- JOINED = 1,
25
- RECONNECTED = 2,
26
- LEAVING = 3,
27
- CLOSED = 4
28
- }
22
+ export declare const ClientState: {
23
+ readonly JOINING: 0;
24
+ readonly JOINED: 1;
25
+ readonly RECONNECTED: 2;
26
+ readonly LEAVING: 3;
27
+ readonly CLOSED: 4;
28
+ };
29
+ export type ClientState = (typeof ClientState)[keyof typeof ClientState];
30
+ type ExtractClientUserData<T> = T extends {
31
+ userData: infer U;
32
+ } ? U : T;
33
+ type ExtractClientAuth<T> = T extends {
34
+ auth: infer A;
35
+ } ? A : any;
36
+ type ExtractClientMessages<T> = T extends {
37
+ messages: infer M;
38
+ } ? M : any;
39
+ export type MessageArgs<M, Options> = unknown extends M ? [message?: M, options?: Options] : [
40
+ M
41
+ ] extends [never] ? [message?: M, options?: Options] : [
42
+ M
43
+ ] extends [void] ? [message?: M, options?: Options] : [
44
+ M
45
+ ] extends [undefined] ? [message?: M, options?: Options] : undefined extends M ? [message?: M, options?: Options] : [
46
+ message: M,
47
+ options?: Options
48
+ ];
29
49
  /**
30
50
  * The client instance from the server-side is responsible for the transport layer between the server and the client.
31
51
  * It should not be confused with the Client from the client-side SDK, as they have completely different purposes!
@@ -34,7 +54,12 @@ export declare enum ClientState {
34
54
  * - This is the raw WebSocket connection coming from the `ws` package. There are more methods available which aren't
35
55
  * encouraged to use along with Colyseus.
36
56
  */
37
- export interface Client<UserData = any, AuthData = any> {
57
+ export interface Client<T extends {
58
+ userData?: any;
59
+ auth?: any;
60
+ messages?: Record<string | number, any>;
61
+ } = any> {
62
+ '~messages': ExtractClientMessages<T>;
38
63
  ref: EventEmitter;
39
64
  /**
40
65
  * @deprecated use `sessionId` instead.
@@ -57,11 +82,11 @@ export interface Client<UserData = any, AuthData = any> {
57
82
  * - Can be used to store custom data about the client's connection. userData is not synchronized with the client,
58
83
  * and should be used only to keep player-specific with its connection.
59
84
  */
60
- userData?: UserData;
85
+ userData?: ExtractClientUserData<T>;
61
86
  /**
62
87
  * auth data provided by your `onAuth`
63
88
  */
64
- auth?: AuthData;
89
+ auth?: ExtractClientAuth<T>;
65
90
  /**
66
91
  * Reconnection token used to re-join the room after onLeave + allowReconnection().
67
92
  *
@@ -80,8 +105,7 @@ export interface Client<UserData = any, AuthData = any> {
80
105
  * @param message Message payload. (automatically encoded with msgpack.)
81
106
  * @param options
82
107
  */
83
- send(type: string | number, message?: any, options?: ISendOptions): void;
84
- send(message: Schema, options?: ISendOptions): void;
108
+ send<K extends keyof this['~messages']>(type: K, ...args: MessageArgs<this['~messages'][K], ISendOptions>): void;
85
109
  /**
86
110
  * Send raw bytes to this specific client.
87
111
  *
@@ -95,7 +119,7 @@ export interface Client<UserData = any, AuthData = any> {
95
119
  *
96
120
  * @param code Custom close code. Default value is 1000.
97
121
  * @param data
98
- * @see {@link https://docs.colyseus.io/colyseus/server/room/#leavecode-number}
122
+ * @see [Leave room](https://docs.colyseus.io/room#leave-room)
99
123
  */
100
124
  leave(code?: number, data?: string): void;
101
125
  /**
@@ -120,10 +144,29 @@ export interface Client<UserData = any, AuthData = any> {
120
144
  export interface ClientPrivate {
121
145
  readyState: number;
122
146
  _enqueuedMessages?: any[];
123
- _afterNextPatchQueue: Array<[string | Client, IArguments]>;
147
+ _afterNextPatchQueue: Array<[string | number | Client, ArrayLike<any>]>;
124
148
  _joinedAt: number;
149
+ /**
150
+ * Used for rate limiting via maxMessagesPerSecond.
151
+ */
152
+ _numMessagesLastSecond?: number;
153
+ _lastMessageTime?: number;
125
154
  }
126
- export declare class ClientArray<UserData = any, AuthData = any> extends Array<Client<UserData, AuthData>> {
127
- getById(sessionId: string): Client<UserData, AuthData> | undefined;
128
- delete(client: Client<UserData, AuthData>): boolean;
155
+ export declare class ClientArray<C extends Client = Client> extends Array<C> {
156
+ getById(sessionId: string): C | undefined;
157
+ delete(client: C): boolean;
129
158
  }
159
+ /**
160
+ * Shared internal method to connect a Client into a Room.
161
+ * Validates seat reservation and joins the client to the room.
162
+ *
163
+ * @remarks
164
+ * **⚠️ This is an internal API and not intended for end-user use.**
165
+ *
166
+ * @internal
167
+ */
168
+ export declare function connectClientToRoom(room: Room | undefined, client: Client & ClientPrivate, authContext: AuthContext, connectionOptions: {
169
+ reconnectionToken?: string;
170
+ skipHandshake?: boolean;
171
+ }): Promise<void>;
172
+ export {};
@@ -1,5 +1,5 @@
1
- import type { Client } from '../Transport.js';
2
- import type { ExtractAuthData, ExtractUserData, Room } from '../Room.js';
1
+ import type { ExtractMessageType, Room } from '../Room.ts';
2
+ export type RoomMethodName = 'onCreate' | 'onAuth' | 'onJoin' | 'onLeave' | 'onDispose' | 'onMessage' | 'setSimulationInterval' | 'setInterval' | 'setTimeout';
3
3
  export type RoomException<R extends Room = Room> = OnCreateException<R> | OnAuthException<R> | OnJoinException<R> | OnLeaveException<R> | OnDisposeException | OnMessageException<R> | SimulationIntervalException | TimedEventException;
4
4
  export declare class OnCreateException<R extends Room = Room> extends Error {
5
5
  options: Parameters<R['onCreate']>[0];
@@ -24,11 +24,12 @@ export declare class OnLeaveException<R extends Room = Room> extends Error {
24
24
  export declare class OnDisposeException extends Error {
25
25
  constructor(cause: Error, message: string);
26
26
  }
27
- export declare class OnMessageException<R extends Room = Room, MessagePayload = any> extends Error {
28
- client: Client<ExtractUserData<R['clients']>, ExtractAuthData<R['clients']>>;
29
- payload: MessagePayload;
30
- type: string;
31
- constructor(cause: Error, message: string, client: Client<ExtractUserData<R['clients']>, ExtractAuthData<R['clients']>>, payload: MessagePayload, type: string);
27
+ export declare class OnMessageException<R extends Room, MessageType extends keyof R['messages'] = keyof R['messages']> extends Error {
28
+ client: R['~client'];
29
+ payload: ExtractMessageType<R['messages'][MessageType]>;
30
+ type: MessageType;
31
+ constructor(cause: Error, message: string, client: R['~client'], payload: ExtractMessageType<R['messages'][MessageType]>, type: MessageType);
32
+ isType<T extends keyof R['messages']>(type: T): this is OnMessageException<R, T>;
32
33
  }
33
34
  export declare class SimulationIntervalException extends Error {
34
35
  constructor(cause: Error, message: string);
@@ -0,0 +1,27 @@
1
+ import { ClockTimer as Clock, Delayed } from '@colyseus/timer';
2
+ export { Server, defineRoom, defineServer, type ServerOptions, type SDKTypes } from './Server.ts';
3
+ export { Room, room, RoomInternalState, validate, type RoomOptions, type MessageHandlerWithFormat, type ExtractMessageType, type Messages, type ExtractRoomState, type ExtractRoomMetadata, type ExtractRoomClient } from './Room.ts';
4
+ export { Protocol, ErrorCode, getMessageBytes, CloseCode } from './Protocol.ts';
5
+ export { RegisteredHandler } from './matchmaker/RegisteredHandler.ts';
6
+ export { ServerError } from './errors/ServerError.ts';
7
+ export { type RoomException, type RoomMethodName, OnCreateException, OnAuthException, OnJoinException, OnLeaveException, OnDisposeException, OnMessageException, SimulationIntervalException, TimedEventException, } from './errors/RoomExceptions.ts';
8
+ import * as matchMaker from './MatchMaker.ts';
9
+ export { matchMaker };
10
+ export { updateLobby, subscribeLobby } from './matchmaker/Lobby.ts';
11
+ export * from './matchmaker/LocalDriver/LocalDriver.ts';
12
+ export { initializeRoomCache } from './matchmaker/driver.ts';
13
+ export { type Client, type ClientPrivate, type AuthContext, ClientState, ClientArray, Transport, type ISendOptions, connectClientToRoom } from './Transport.ts';
14
+ export { type Presence } from './presence/Presence.ts';
15
+ export { LocalPresence } from './presence/LocalPresence.ts';
16
+ export { type Serializer } from './serializer/Serializer.ts';
17
+ export { SchemaSerializer } from './serializer/SchemaSerializer.ts';
18
+ export { Clock, Delayed };
19
+ export { generateId, Deferred, HttpServerMock, spliceOne, getBearerToken } from './utils/Utils.ts';
20
+ export { isDevMode } from './utils/DevMode.ts';
21
+ export { subscribeIPC, requestFromIPC } from './IPC.ts';
22
+ export { debugMatchMaking, debugMessage, debugPatch, debugError, debugConnection, debugDriver, debugPresence, debugAndPrintError, } from './Debug.ts';
23
+ export { LobbyRoom } from './rooms/LobbyRoom.ts';
24
+ export { RelayRoom } from './rooms/RelayRoom.ts';
25
+ export { RankedQueueRoom, type RankedQueueOptions, type MatchGroup, type MatchTeam, type ClientQueueData } from './rooms/RankedQueueRoom.ts';
26
+ export { createEndpoint, createInternalContext, createMiddleware, createRouter, toNodeHandler, __globalEndpoints, type Router, } from './router/index.ts';
27
+ export { logger } from './Logger.ts';
@@ -0,0 +1,4 @@
1
+ import type { Room } from '../Room.ts';
2
+ import type { IRoomCache } from './driver.ts';
3
+ export declare function updateLobby<T extends Room>(room: T, removed?: boolean): void;
4
+ export declare function subscribeLobby(callback: (roomId: string, roomListing: IRoomCache) => void): Promise<() => any>;
@@ -0,0 +1,17 @@
1
+ import type { IRoomCache, SortOptions, MatchMakerDriver } from '../driver.ts';
2
+ export type { IRoomCache, SortOptions, MatchMakerDriver };
3
+ export declare class LocalDriver implements MatchMakerDriver {
4
+ rooms: IRoomCache[];
5
+ has(roomId: string): boolean;
6
+ query(conditions: Partial<IRoomCache>, sortOptions?: SortOptions): IRoomCache<any>[];
7
+ cleanup(processId: string): Promise<void>;
8
+ findOne(conditions: Partial<IRoomCache>, sortOptions?: SortOptions): Promise<IRoomCache>;
9
+ update(room: IRoomCache, operations: Partial<{
10
+ $set: Partial<IRoomCache>;
11
+ $inc: Partial<IRoomCache>;
12
+ }>): boolean;
13
+ persist(room: IRoomCache, create?: boolean): boolean;
14
+ remove(roomId: string): boolean;
15
+ clear(): void;
16
+ shutdown(): void;
17
+ }
@@ -0,0 +1,12 @@
1
+ import type { IRoomCache, SortOptions } from '../driver.ts';
2
+ export declare class Query<T extends IRoomCache> {
3
+ private $rooms;
4
+ private conditions;
5
+ private sortOptions?;
6
+ constructor(rooms: any[], conditions: any);
7
+ sort(options: SortOptions): this;
8
+ private applySort;
9
+ private applyFilter;
10
+ filter(conditions: any): T[];
11
+ then(resolve: any, reject: any): any;
12
+ }
@@ -0,0 +1,81 @@
1
+ import { EventEmitter } from 'events';
2
+ import { Room } from './../Room.ts';
3
+ import type { IRoomCache, SortOptions, IRoomCacheFilterByKeys, IRoomCacheSortByKeys, ExtractMetadata } from './driver.ts';
4
+ import type { Client } from '../Transport.ts';
5
+ import type { Type } from "../utils/Utils.ts";
6
+ export declare const INVALID_OPTION_KEYS: Array<keyof IRoomCache>;
7
+ /**
8
+ * Type for filterBy that supports both onCreate options and metadata fields
9
+ */
10
+ type FilterByKeys<RoomType extends Room> = IRoomCacheFilterByKeys | (ExtractMetadata<RoomType> extends object ? keyof ExtractMetadata<RoomType> & string : never);
11
+ /**
12
+ * Type for sortBy that supports room cache fields and metadata fields
13
+ */
14
+ type SortByKeys<RoomType extends Room> = IRoomCacheSortByKeys | (ExtractMetadata<RoomType> extends object ? keyof ExtractMetadata<RoomType> & string : never);
15
+ export interface RegisteredHandlerEvents<RoomType extends Type<Room> = any> {
16
+ create: [room: InstanceType<RoomType>];
17
+ lock: [room: InstanceType<RoomType>];
18
+ unlock: [room: InstanceType<RoomType>];
19
+ join: [room: InstanceType<RoomType>, client: Client];
20
+ leave: [room: InstanceType<RoomType>, client: Client, willDispose: boolean];
21
+ dispose: [room: InstanceType<RoomType>];
22
+ 'visibility-change': [room: InstanceType<RoomType>, isVisible: boolean];
23
+ 'metadata-change': [room: InstanceType<RoomType>];
24
+ }
25
+ export declare class RegisteredHandler<RoomType extends Type<Room> = any> extends EventEmitter<RegisteredHandlerEvents<RoomType>> {
26
+ '~room': RoomType;
27
+ klass: RoomType;
28
+ options: any;
29
+ name: string;
30
+ filterOptions: Array<FilterByKeys<InstanceType<RoomType>>>;
31
+ sortOptions?: SortOptions;
32
+ realtimeListingEnabled: boolean;
33
+ constructor(klass: RoomType, options?: any);
34
+ enableRealtimeListing(): this;
35
+ /**
36
+ * Define which fields should be used for filtering rooms.
37
+ * Supports both onCreate options and metadata fields using dot notation.
38
+ *
39
+ * @example
40
+ * // Filter by IRoomCache fields
41
+ * .filterBy(['maxClients'])
42
+ *
43
+ * @example
44
+ * // Filter by metadata fields
45
+ * .filterBy(['difficulty', 'metadata.region'])
46
+ *
47
+ * @example
48
+ * // Mix both
49
+ * .filterBy(['mode', 'difficulty', 'maxClients'])
50
+ */
51
+ filterBy<T extends FilterByKeys<InstanceType<RoomType>>>(options: T[]): this;
52
+ /**
53
+ * Define how rooms should be sorted when querying.
54
+ * Supports both room cache fields and metadata fields using dot notation.
55
+ *
56
+ * @example
57
+ * // Sort by number of clients (descending)
58
+ * .sortBy({ clients: -1 })
59
+ *
60
+ * @example
61
+ * // Sort by metadata field
62
+ * .sortBy({ 'metadata.rating': -1 })
63
+ *
64
+ * @example
65
+ * // Multiple sort criteria
66
+ * .sortBy({ 'metadata.skillLevel': 1, clients: -1 })
67
+ */
68
+ sortBy<T extends SortByKeys<InstanceType<RoomType>>>(options: {
69
+ [K in T]: SortOptions[string];
70
+ }): this;
71
+ getMetadataFromOptions(options: any): {
72
+ metadata: {};
73
+ } | {
74
+ metadata?: undefined;
75
+ };
76
+ /**
77
+ * Extract filter options from client options.
78
+ */
79
+ getFilterOptions(options: any): {};
80
+ }
81
+ export {};
@@ -2,9 +2,8 @@
2
2
  * Matchmaking controller
3
3
  * (for interoperability between different http frameworks, e.g. express, uWebSockets.js, etc)
4
4
  */
5
- import { IncomingMessage } from 'http';
6
- import * as matchMaker from '../MatchMaker.js';
7
- import type { AuthContext } from '../Transport.js';
5
+ import * as matchMaker from '../MatchMaker.ts';
6
+ import type { AuthContext } from '../Transport.ts';
8
7
  declare const _default: {
9
8
  DEFAULT_CORS_HEADERS: {
10
9
  'Access-Control-Allow-Headers': string;
@@ -20,8 +19,8 @@ declare const _default: {
20
19
  * You can manually change the default corsHeaders by overwriting the `getCorsHeaders()` method:
21
20
  * ```
22
21
  * import { matchMaker } from "@colyseus/core";
23
- * matchMaker.controller.getCorsHeaders = function(req) {
24
- * if (req.headers.referer !== "xxx") {
22
+ * matchMaker.controller.getCorsHeaders = function(headers) {
23
+ * if (headers.get('referer') !== "xxx") {
25
24
  * }
26
25
  *
27
26
  * return {
@@ -30,7 +29,7 @@ declare const _default: {
30
29
  * }
31
30
  * ```
32
31
  */
33
- getCorsHeaders(req: IncomingMessage): {
32
+ getCorsHeaders(headers: Headers): {
34
33
  [header: string]: string;
35
34
  };
36
35
  invokeMethod(method: string, roomName: string, clientOptions?: matchMaker.ClientOptions, authOptions?: AuthContext): Promise<any>;
@@ -0,0 +1,145 @@
1
+ import type { Room } from "@colyseus/core";
2
+ /**
3
+ * Sort options for room queries.
4
+ */
5
+ export interface SortOptions {
6
+ [fieldName: string]: 1 | -1 | 'asc' | 'desc' | 'ascending' | 'descending';
7
+ }
8
+ /**
9
+ * Built-in room cache fields that can be used for sorting.
10
+ */
11
+ export type IRoomCacheSortByKeys = 'clients' | 'maxClients' | 'createdAt';
12
+ /**
13
+ * Built-in room cache fields that can be used for filtering.
14
+ */
15
+ export type IRoomCacheFilterByKeys = 'clients' | 'maxClients' | 'processId';
16
+ /**
17
+ * Extract metadata type from Room type
18
+ */
19
+ export type ExtractMetadata<RoomType extends Room> = RoomType extends Room<infer M> ? M : any;
20
+ /**
21
+ * Generates a unique lock ID based on filter options.
22
+ */
23
+ export declare function getLockId(filterOptions: any): string;
24
+ /**
25
+ * Initialize a room cache which contains CRUD operations for room listings.
26
+ *
27
+ * @internal
28
+ * @param initialValues - Predefined room properties.
29
+ * @returns RoomData - New room cache.
30
+ */
31
+ export declare function initializeRoomCache(initialValues?: Partial<IRoomCache>): IRoomCache;
32
+ export interface IRoomCache<Metadata = any> {
33
+ /**
34
+ * Room name.
35
+ */
36
+ name: string;
37
+ /**
38
+ * Unique identifier for the room.
39
+ */
40
+ roomId: string;
41
+ /**
42
+ * Process id where the room is running.
43
+ */
44
+ processId: string;
45
+ /**
46
+ * Number of clients connected to this room.
47
+ */
48
+ clients: number;
49
+ /**
50
+ * Maximum number of clients allowed to join the room.
51
+ */
52
+ maxClients: number;
53
+ /**
54
+ * Indicates if the room is locked (i.e. join requests are rejected).
55
+ */
56
+ locked?: boolean;
57
+ /**
58
+ * Indicates if the room is private
59
+ * Private rooms can't be joined via `join()` or `joinOrCreate()`.
60
+ */
61
+ private?: boolean;
62
+ /**
63
+ * Public address of the server.
64
+ */
65
+ publicAddress?: string;
66
+ /**
67
+ * Do not show this room in lobby listing.
68
+ */
69
+ unlisted?: boolean;
70
+ /**
71
+ * Metadata associated with the room.
72
+ */
73
+ metadata?: Metadata;
74
+ /**
75
+ * When the room was created.
76
+ */
77
+ createdAt?: Date;
78
+ }
79
+ export interface MatchMakerDriver {
80
+ /**
81
+ * Check if a room exists in room cache.
82
+ *
83
+ * @param roomId - The room id.
84
+ *
85
+ * @returns Promise<boolean> | boolean - A promise or a boolean value indicating if the room exists.
86
+ */
87
+ has(roomId: string): Promise<boolean> | boolean;
88
+ /**
89
+ * Query rooms in room cache for given conditions.
90
+ *
91
+ * @param conditions - Filtering conditions.
92
+ *
93
+ * @returns Promise<IRoomCache[]> | IRoomCache[] - A promise or an object contaning room metadata list.
94
+ */
95
+ query<T extends Room = any>(conditions: Partial<IRoomCache & ExtractMetadata<T>>, sortOptions?: SortOptions): Promise<Array<IRoomCache<ExtractMetadata<T>>>> | Array<IRoomCache<ExtractMetadata<T>>>;
96
+ /**
97
+ * Clean up rooms in room cache by process id.
98
+ * @param processId - The process id.
99
+ */
100
+ cleanup?(processId: string): Promise<void>;
101
+ /**
102
+ * Query for a room in room cache for given conditions.
103
+ *
104
+ * @param conditions - Filtering conditions.
105
+ *
106
+ * @returns `IRoomCache` - An object contaning filtered room metadata.
107
+ */
108
+ findOne<T extends Room = any>(conditions: Partial<IRoomCache & ExtractMetadata<T>>, sortOptions?: SortOptions): Promise<IRoomCache<ExtractMetadata<T>>>;
109
+ /**
110
+ * Remove a room from room cache.
111
+ *
112
+ * @param roomId - The room id.
113
+ */
114
+ remove(roomId: string): Promise<boolean> | boolean;
115
+ /**
116
+ * Update a room in room cache.
117
+ *
118
+ * @param IRoomCache - The room to update.
119
+ * @param operations - The operations to update the room.
120
+ */
121
+ update(room: IRoomCache, operations: Partial<{
122
+ $set: Partial<IRoomCache>;
123
+ $inc: Partial<IRoomCache>;
124
+ }>): Promise<boolean> | boolean;
125
+ /**
126
+ * Persist a room in room cache.
127
+ *
128
+ * @param room - The room to persist.
129
+ * @param create - If true, create a new record. If false (default), update existing record.
130
+ */
131
+ persist(room: IRoomCache, create?: boolean): Promise<boolean> | boolean;
132
+ /**
133
+ * Empty the room cache. Used for testing purposes only.
134
+ * @internal Do not call this method yourself.
135
+ */
136
+ clear(): void;
137
+ /**
138
+ * Boot the room cache medium (if available).
139
+ */
140
+ boot?(): Promise<void>;
141
+ /**
142
+ * Dispose the connection of the room cache medium.
143
+ */
144
+ shutdown(): void;
145
+ }
@@ -0,0 +1,17 @@
1
+ import type { IRoomCache, SortOptions, MatchMakerDriver } from '../api.ts';
2
+ export type { IRoomCache, SortOptions, MatchMakerDriver };
3
+ export declare class LocalDriver implements MatchMakerDriver {
4
+ rooms: IRoomCache[];
5
+ has(roomId: string): boolean;
6
+ query(conditions: Partial<IRoomCache>, sortOptions?: SortOptions): IRoomCache<any>[];
7
+ cleanup(processId: string): Promise<void>;
8
+ findOne(conditions: Partial<IRoomCache>, sortOptions?: SortOptions): Promise<IRoomCache>;
9
+ update(room: IRoomCache, operations: Partial<{
10
+ $set: Partial<IRoomCache>;
11
+ $inc: Partial<IRoomCache>;
12
+ }>): boolean;
13
+ persist(room: IRoomCache, create?: boolean): boolean;
14
+ remove(roomId: string): boolean;
15
+ clear(): void;
16
+ shutdown(): void;
17
+ }
@@ -0,0 +1,12 @@
1
+ import type { IRoomCache, SortOptions } from '../api.ts';
2
+ export declare class Query<T extends IRoomCache> {
3
+ private $rooms;
4
+ private conditions;
5
+ private sortOptions?;
6
+ constructor(rooms: any[], conditions: any);
7
+ sort(options: SortOptions): this;
8
+ private applySort;
9
+ private applyFilter;
10
+ filter(conditions: any): T[];
11
+ then(resolve: any, reject: any): any;
12
+ }
@@ -0,0 +1,145 @@
1
+ import type { Room } from "@colyseus/core";
2
+ /**
3
+ * Sort options for room queries.
4
+ */
5
+ export interface SortOptions {
6
+ [fieldName: string]: 1 | -1 | 'asc' | 'desc' | 'ascending' | 'descending';
7
+ }
8
+ /**
9
+ * Built-in room cache fields that can be used for sorting.
10
+ */
11
+ export type IRoomCacheSortByKeys = 'clients' | 'maxClients' | 'createdAt';
12
+ /**
13
+ * Built-in room cache fields that can be used for filtering.
14
+ */
15
+ export type IRoomCacheFilterByKeys = 'clients' | 'maxClients' | 'processId';
16
+ /**
17
+ * Extract metadata type from Room type
18
+ */
19
+ export type ExtractMetadata<RoomType extends Room> = RoomType['~metadata'];
20
+ /**
21
+ * Generates a unique lock ID based on filter options.
22
+ */
23
+ export declare function getLockId(filterOptions: any): string;
24
+ /**
25
+ * Initialize a room cache which contains CRUD operations for room listings.
26
+ *
27
+ * @internal
28
+ * @param initialValues - Predefined room properties.
29
+ * @returns RoomData - New room cache.
30
+ */
31
+ export declare function initializeRoomCache(initialValues?: Partial<IRoomCache>): IRoomCache;
32
+ export interface IRoomCache<Metadata = any> {
33
+ /**
34
+ * Room name.
35
+ */
36
+ name: string;
37
+ /**
38
+ * Unique identifier for the room.
39
+ */
40
+ roomId: string;
41
+ /**
42
+ * Process id where the room is running.
43
+ */
44
+ processId: string;
45
+ /**
46
+ * Number of clients connected to this room.
47
+ */
48
+ clients: number;
49
+ /**
50
+ * Maximum number of clients allowed to join the room.
51
+ */
52
+ maxClients: number;
53
+ /**
54
+ * Indicates if the room is locked (i.e. join requests are rejected).
55
+ */
56
+ locked?: boolean;
57
+ /**
58
+ * Indicates if the room is private
59
+ * Private rooms can't be joined via `join()` or `joinOrCreate()`.
60
+ */
61
+ private?: boolean;
62
+ /**
63
+ * Public address of the server.
64
+ */
65
+ publicAddress?: string;
66
+ /**
67
+ * Do not show this room in lobby listing.
68
+ */
69
+ unlisted?: boolean;
70
+ /**
71
+ * Metadata associated with the room.
72
+ */
73
+ metadata?: Metadata;
74
+ /**
75
+ * When the room was created.
76
+ */
77
+ createdAt?: Date;
78
+ }
79
+ export interface MatchMakerDriver {
80
+ /**
81
+ * Check if a room exists in room cache.
82
+ *
83
+ * @param roomId - The room id.
84
+ *
85
+ * @returns Promise<boolean> | boolean - A promise or a boolean value indicating if the room exists.
86
+ */
87
+ has(roomId: string): Promise<boolean> | boolean;
88
+ /**
89
+ * Query rooms in room cache for given conditions.
90
+ *
91
+ * @param conditions - Filtering conditions.
92
+ *
93
+ * @returns Promise<IRoomCache[]> | IRoomCache[] - A promise or an object contaning room metadata list.
94
+ */
95
+ query<T extends Room = any>(conditions: Partial<IRoomCache & ExtractMetadata<T>>, sortOptions?: SortOptions): Promise<Array<IRoomCache<ExtractMetadata<T>>>> | Array<IRoomCache<ExtractMetadata<T>>>;
96
+ /**
97
+ * Clean up rooms in room cache by process id.
98
+ * @param processId - The process id.
99
+ */
100
+ cleanup?(processId: string): Promise<void>;
101
+ /**
102
+ * Query for a room in room cache for given conditions.
103
+ *
104
+ * @param conditions - Filtering conditions.
105
+ *
106
+ * @returns `IRoomCache` - An object contaning filtered room metadata.
107
+ */
108
+ findOne<T extends Room = any>(conditions: Partial<IRoomCache & ExtractMetadata<T>>, sortOptions?: SortOptions): Promise<IRoomCache<ExtractMetadata<T>>>;
109
+ /**
110
+ * Remove a room from room cache.
111
+ *
112
+ * @param roomId - The room id.
113
+ */
114
+ remove(roomId: string): Promise<boolean> | boolean;
115
+ /**
116
+ * Update a room in room cache.
117
+ *
118
+ * @param IRoomCache - The room to update.
119
+ * @param operations - The operations to update the room.
120
+ */
121
+ update(room: IRoomCache, operations: Partial<{
122
+ $set: Partial<IRoomCache>;
123
+ $inc: Partial<IRoomCache>;
124
+ }>): Promise<boolean> | boolean;
125
+ /**
126
+ * Persist a room in room cache.
127
+ *
128
+ * @param room - The room to persist.
129
+ * @param create - If true, create a new record. If false (default), update existing record.
130
+ */
131
+ persist(room: IRoomCache, create?: boolean): Promise<boolean> | boolean;
132
+ /**
133
+ * Empty the room cache. Used for testing purposes only.
134
+ * @internal Do not call this method yourself.
135
+ */
136
+ clear(): void;
137
+ /**
138
+ * Boot the room cache medium (if available).
139
+ */
140
+ boot?(): Promise<void>;
141
+ /**
142
+ * Dispose the connection of the room cache medium.
143
+ */
144
+ shutdown(): void;
145
+ }