@colyseus/core 0.17.42 → 0.18.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 (90) hide show
  1. package/build/MatchMaker.cjs +19 -6
  2. package/build/MatchMaker.cjs.map +2 -2
  3. package/build/MatchMaker.d.ts +10 -0
  4. package/build/MatchMaker.mjs +18 -6
  5. package/build/MatchMaker.mjs.map +2 -2
  6. package/build/Protocol.cjs +102 -37
  7. package/build/Protocol.cjs.map +2 -2
  8. package/build/Protocol.d.ts +33 -2
  9. package/build/Protocol.mjs +102 -37
  10. package/build/Protocol.mjs.map +2 -2
  11. package/build/Room.cjs +296 -19
  12. package/build/Room.cjs.map +3 -3
  13. package/build/Room.d.ts +186 -3
  14. package/build/Room.mjs +303 -21
  15. package/build/Room.mjs.map +3 -3
  16. package/build/RoomPlugin.cjs +252 -0
  17. package/build/RoomPlugin.cjs.map +7 -0
  18. package/build/RoomPlugin.d.ts +271 -0
  19. package/build/RoomPlugin.mjs +220 -0
  20. package/build/RoomPlugin.mjs.map +7 -0
  21. package/build/Server.cjs +40 -7
  22. package/build/Server.cjs.map +2 -2
  23. package/build/Server.d.ts +25 -0
  24. package/build/Server.mjs +41 -8
  25. package/build/Server.mjs.map +2 -2
  26. package/build/Transport.cjs +38 -2
  27. package/build/Transport.cjs.map +2 -2
  28. package/build/Transport.d.ts +40 -4
  29. package/build/Transport.mjs +38 -2
  30. package/build/Transport.mjs.map +2 -2
  31. package/build/index.cjs +11 -2
  32. package/build/index.cjs.map +2 -2
  33. package/build/index.d.ts +2 -1
  34. package/build/index.mjs +12 -2
  35. package/build/index.mjs.map +2 -2
  36. package/build/input/InputBuffer.cjs +113 -0
  37. package/build/input/InputBuffer.cjs.map +7 -0
  38. package/build/input/InputBuffer.d.ts +136 -0
  39. package/build/input/InputBuffer.mjs +86 -0
  40. package/build/input/InputBuffer.mjs.map +7 -0
  41. package/build/internal.cjs +61 -0
  42. package/build/internal.cjs.map +7 -0
  43. package/build/internal.d.ts +9 -0
  44. package/build/internal.mjs +29 -0
  45. package/build/internal.mjs.map +7 -0
  46. package/build/matchmaker/LocalDriver/LocalDriver.cjs +13 -0
  47. package/build/matchmaker/LocalDriver/LocalDriver.cjs.map +2 -2
  48. package/build/matchmaker/LocalDriver/LocalDriver.d.ts +1 -0
  49. package/build/matchmaker/LocalDriver/LocalDriver.mjs +13 -0
  50. package/build/matchmaker/LocalDriver/LocalDriver.mjs.map +2 -2
  51. package/build/matchmaker/driver.cjs.map +1 -1
  52. package/build/matchmaker/driver.d.ts +12 -0
  53. package/build/matchmaker/driver.mjs.map +1 -1
  54. package/build/presence/LocalPresence.d.ts +1 -1
  55. package/build/rooms/LobbyRoom.cjs +8 -10
  56. package/build/rooms/LobbyRoom.cjs.map +2 -2
  57. package/build/rooms/LobbyRoom.d.ts +4 -3
  58. package/build/rooms/LobbyRoom.mjs +8 -10
  59. package/build/rooms/LobbyRoom.mjs.map +2 -2
  60. package/build/rooms/RelayRoom.cjs +12 -16
  61. package/build/rooms/RelayRoom.cjs.map +2 -2
  62. package/build/rooms/RelayRoom.d.ts +32 -11
  63. package/build/rooms/RelayRoom.mjs +10 -16
  64. package/build/rooms/RelayRoom.mjs.map +2 -2
  65. package/build/router/index.cjs +65 -4
  66. package/build/router/index.cjs.map +2 -2
  67. package/build/router/index.d.ts +30 -6
  68. package/build/router/index.mjs +66 -6
  69. package/build/router/index.mjs.map +3 -3
  70. package/build/utils/UserSessionIndex.cjs +162 -0
  71. package/build/utils/UserSessionIndex.cjs.map +7 -0
  72. package/build/utils/UserSessionIndex.d.ts +166 -0
  73. package/build/utils/UserSessionIndex.mjs +130 -0
  74. package/build/utils/UserSessionIndex.mjs.map +7 -0
  75. package/package.json +19 -14
  76. package/src/MatchMaker.ts +40 -6
  77. package/src/Protocol.ts +130 -59
  78. package/src/Room.ts +475 -22
  79. package/src/RoomPlugin.ts +563 -0
  80. package/src/Server.ts +72 -11
  81. package/src/Transport.ts +76 -8
  82. package/src/index.ts +10 -1
  83. package/src/input/InputBuffer.ts +192 -0
  84. package/src/internal.ts +46 -0
  85. package/src/matchmaker/LocalDriver/LocalDriver.ts +10 -0
  86. package/src/matchmaker/driver.ts +13 -0
  87. package/src/rooms/LobbyRoom.ts +12 -8
  88. package/src/rooms/RelayRoom.ts +9 -15
  89. package/src/router/index.ts +112 -11
  90. package/src/utils/UserSessionIndex.ts +311 -0
@@ -0,0 +1,220 @@
1
+ // packages/core/src/RoomPlugin.ts
2
+ var RoomPlugin = class {
3
+ };
4
+ function definePlugins(plugins) {
5
+ if (!Array.isArray(plugins)) {
6
+ return plugins;
7
+ }
8
+ const out = {};
9
+ for (const p of plugins) {
10
+ const key = p["pluginName"];
11
+ if (typeof key !== "string" || key.length === 0) {
12
+ throw new Error(
13
+ `[Room] plugin ${p.constructor.name} is missing a 'pluginName' field. Declare \`readonly pluginName = '<key>' as const\` on the class, or register it via the keyed-record form \`definePlugins({ <key>: <plugin> })\`.`
14
+ );
15
+ }
16
+ if (out[key]) {
17
+ throw new Error(
18
+ `[Room] two plugins resolve to pluginName "${key}". Configure one of them via constructor argument, or use the keyed-record form to disambiguate.`
19
+ );
20
+ }
21
+ out[key] = p;
22
+ }
23
+ return out;
24
+ }
25
+ var PLUGIN_LIFECYCLE_KEYS = ["onCreate", "onAuth", "onJoin", "onLeave", "onDispose"];
26
+ function attachToTestRoom(plugin, roomStub = {}) {
27
+ plugin.room = roomStub;
28
+ return roomStub;
29
+ }
30
+ var DEP_KEY_PREFIX = "__dep:";
31
+ var DEFAULT_PLUGIN_ORDER = {
32
+ onCreate: "before",
33
+ onAuth: "before",
34
+ onJoin: "before",
35
+ onLeave: "after",
36
+ onDispose: "after"
37
+ };
38
+ function computePluginLayout(plugins) {
39
+ const resolved = resolveDependencies(plugins);
40
+ const hooks = {};
41
+ let anyHook = false;
42
+ for (const hook of PLUGIN_LIFECYCLE_KEYS) {
43
+ const before = [];
44
+ const after = [];
45
+ for (const { key, plugin } of resolved.entries) {
46
+ if (typeof plugin[hook] !== "function") {
47
+ continue;
48
+ }
49
+ const order = plugin["order"]?.[hook] ?? DEFAULT_PLUGIN_ORDER[hook];
50
+ (order === "before" ? before : after).push(key);
51
+ anyHook = true;
52
+ }
53
+ hooks[hook] = { before, after };
54
+ }
55
+ const messageOwners = /* @__PURE__ */ new Map();
56
+ for (const { key, plugin } of resolved.entries) {
57
+ const messages = plugin["messages"];
58
+ if (!messages) {
59
+ continue;
60
+ }
61
+ for (const messageKey of Object.keys(messages)) {
62
+ const prior = messageOwners.get(messageKey);
63
+ if (prior !== void 0) {
64
+ throw new Error(
65
+ `[Room] message key "${messageKey}" declared by multiple plugins: "${prior}" and "${key}". Resolve by giving one of them a different key, or override on the room's own \`messages\`.`
66
+ );
67
+ }
68
+ messageOwners.set(messageKey, key);
69
+ }
70
+ }
71
+ if (!anyHook && messageOwners.size === 0 && resolved.autoDeps.length === 0) {
72
+ return null;
73
+ }
74
+ return { hooks, messageOwners, autoDeps: resolved.autoDeps };
75
+ }
76
+ function resolveDependencies(plugins) {
77
+ const entries = [];
78
+ for (const [key, plugin] of Object.entries(plugins)) {
79
+ entries.push({ key, plugin });
80
+ }
81
+ const present = /* @__PURE__ */ new Set();
82
+ for (const { plugin } of entries) {
83
+ present.add(plugin.constructor);
84
+ }
85
+ const autoDeps = [];
86
+ const visiting = /* @__PURE__ */ new Set();
87
+ const path = [];
88
+ function walk(depCtor) {
89
+ if (present.has(depCtor)) {
90
+ return;
91
+ }
92
+ if (visiting.has(depCtor)) {
93
+ const cycle = [...path, depCtor.name].join(" \u2192 ");
94
+ throw new Error(`[Room] plugin dependency cycle: ${cycle}`);
95
+ }
96
+ visiting.add(depCtor);
97
+ path.push(depCtor.name);
98
+ const deeper = depCtor.dependencies;
99
+ if (Array.isArray(deeper)) {
100
+ for (const inner of deeper) {
101
+ walk(inner);
102
+ }
103
+ }
104
+ let instance;
105
+ try {
106
+ instance = new depCtor();
107
+ } catch (err) {
108
+ throw new Error(
109
+ `[Room] auto-included plugin "${depCtor.name}" must be constructible with no arguments. If it needs options, register it explicitly in definePlugins({...}). (cause: ${err?.message ?? err})`
110
+ );
111
+ }
112
+ const key = DEP_KEY_PREFIX + depCtor.name;
113
+ entries.push({ key, plugin: instance });
114
+ autoDeps.push({ key, ctor: depCtor });
115
+ present.add(depCtor);
116
+ visiting.delete(depCtor);
117
+ path.pop();
118
+ }
119
+ const userEntriesSnapshot = entries.slice();
120
+ for (const { plugin } of userEntriesSnapshot) {
121
+ const deps = plugin.constructor.dependencies;
122
+ if (!Array.isArray(deps)) {
123
+ continue;
124
+ }
125
+ for (const depCtor of deps) {
126
+ walk(depCtor);
127
+ }
128
+ }
129
+ return { entries, autoDeps };
130
+ }
131
+ function installPluginHookWrappers(ctor, layout) {
132
+ if (layout === null) {
133
+ return;
134
+ }
135
+ const proto = ctor.prototype;
136
+ for (const hook of PLUGIN_LIFECYCLE_KEYS) {
137
+ const { before, after } = layout.hooks[hook];
138
+ if (before.length === 0 && after.length === 0) {
139
+ continue;
140
+ }
141
+ const original = proto[hook];
142
+ proto[hook] = async function(...args) {
143
+ const lookup = (k) => k.startsWith(DEP_KEY_PREFIX) ? this._autoPlugins[k] : this.plugins[k];
144
+ for (const k of before) {
145
+ const p = lookup(k);
146
+ await p[hook].call(p, ...args);
147
+ }
148
+ let result;
149
+ if (original) {
150
+ result = await original.apply(this, args);
151
+ }
152
+ for (const k of after) {
153
+ const p = lookup(k);
154
+ await p[hook].call(p, ...args);
155
+ }
156
+ return result;
157
+ };
158
+ }
159
+ }
160
+ function setupRoomPlugins(room) {
161
+ const plugins = room.plugins;
162
+ const layout = resolveOrComputeLayout(room.constructor, plugins);
163
+ attachRoomReference(room, plugins);
164
+ if (layout && layout.autoDeps.length > 0) {
165
+ room._autoPlugins = instantiateAutoDeps(room, layout);
166
+ }
167
+ if (layout && layout.messageOwners.size > 0) {
168
+ mergePluginMessages(room, layout);
169
+ }
170
+ Object.freeze(plugins);
171
+ if (room._autoPlugins) {
172
+ Object.freeze(room._autoPlugins);
173
+ }
174
+ }
175
+ function resolveOrComputeLayout(ctor, plugins) {
176
+ if (Object.prototype.hasOwnProperty.call(ctor, "__pluginLayout")) {
177
+ return ctor.__pluginLayout;
178
+ }
179
+ const layout = computePluginLayout(plugins);
180
+ installPluginHookWrappers(ctor, layout);
181
+ ctor.__pluginLayout = layout;
182
+ return layout;
183
+ }
184
+ function attachRoomReference(room, plugins) {
185
+ for (const plugin of Object.values(plugins)) {
186
+ plugin.room = room;
187
+ }
188
+ }
189
+ function instantiateAutoDeps(room, layout) {
190
+ const auto = {};
191
+ for (const { key, ctor: depCtor } of layout.autoDeps) {
192
+ const instance = new depCtor();
193
+ instance.room = room;
194
+ auto[key] = instance;
195
+ }
196
+ return auto;
197
+ }
198
+ function mergePluginMessages(room, layout) {
199
+ const plugins = room.plugins;
200
+ for (const [messageKey, pluginKey] of layout.messageOwners) {
201
+ if (room.messages?.[messageKey]) {
202
+ continue;
203
+ }
204
+ const source = pluginKey.startsWith(DEP_KEY_PREFIX) ? room._autoPlugins[pluginKey] : plugins[pluginKey];
205
+ const handler = source["messages"]?.[messageKey];
206
+ if (handler !== void 0) {
207
+ (room.messages ??= {})[messageKey] = handler;
208
+ }
209
+ }
210
+ }
211
+ export {
212
+ DEFAULT_PLUGIN_ORDER,
213
+ PLUGIN_LIFECYCLE_KEYS,
214
+ RoomPlugin,
215
+ attachToTestRoom,
216
+ computePluginLayout,
217
+ definePlugins,
218
+ installPluginHookWrappers,
219
+ setupRoomPlugins
220
+ };
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/RoomPlugin.ts"],
4
+ "sourcesContent": ["/**\n * Room plugins.\n *\n * A plugin is a composable extension to a Room \u2014 it can declare message\n * handlers, attach lifecycle hooks, and expose public methods that the\n * room can call (`this.plugins.<key>.someMethod()`). The framework\n * instantiates one plugin per room, sets `this.room` after the room is\n * constructed, then merges the plugin's `messages` with the room's own\n * and wires its lifecycle hooks alongside the room's.\n *\n * Example: a tiny chat plugin contributing a single message handler:\n *\n * class ChatPlugin extends RoomPlugin {\n * private history: string[] = [];\n * constructor(private opts: { historyLimit: number }) { super(); }\n *\n * messages = {\n * chat: (client: Client, msg: { text: string }) => {\n * this.history.push(msg.text);\n * if (this.history.length > this.opts.historyLimit) this.history.shift();\n * this.room.broadcast('chat', { from: client.userId, text: msg.text });\n * },\n * };\n *\n * // Public \u2014 callable as `this.plugins.chat.getHistory()` from the room\n * getHistory(): readonly string[] { return this.history; }\n * }\n *\n * Use via `definePlugins`:\n *\n * class MyRoom extends Room<{ state: MyState }> {\n * plugins = definePlugins({\n * chat: new ChatPlugin({ historyLimit: 100 }),\n * });\n * }\n */\nimport type { Room } from './Room.ts';\nimport type { AuthContext, Client } from './Transport.ts';\nimport type { Messages } from '@colyseus/shared-types';\n\n/**\n * Ordering hint for a plugin's lifecycle hook relative to the room's own\n * hook. Sensible per-hook defaults are applied when omitted (see\n * `Room.__init` for the exact ordering policy).\n *\n * onCreate / onAuth / onJoin \u2192 plugins run BEFORE room (guards + setup)\n * onLeave / onDispose \u2192 plugins run AFTER room (capture final state)\n */\nexport interface RoomPluginOrder {\n onCreate?: 'before' | 'after';\n onAuth?: 'before' | 'after';\n onJoin?: 'before' | 'after';\n onLeave?: 'before' | 'after';\n onDispose?: 'before' | 'after';\n}\n\n/**\n * Base class for room plugins. Subclass to define a plugin; the framework\n * sets `this.room` after the host room is fully constructed.\n *\n * Don't access `this.room` from the plugin's constructor \u2014 it hasn't been\n * wired yet. Everything room-dependent goes in `onCreate` / `onJoin` /\n * etc. or in public methods that are called from the room post-init.\n *\n * @typeParam This - The Room subclass this plugin is attached to. Narrow\n * for schema-driven plugins that need a specific state shape, e.g.\n * `class PhysicsPlugin extends RoomPlugin<Room<{ state: PhysicsContract }>>`.\n */\nexport abstract class RoomPlugin<This extends Room = Room> {\n /**\n * Live room reference. Wired by the framework at __init, AFTER the\n * room's own construction \u2014 accessing it from the plugin's\n * constructor throws.\n */\n protected readonly room!: This;\n\n /**\n * Canonical key for the plugin when registered via `definePlugins([...])`.\n * Declared on the subclass with `as const` so the literal type flows\n * into `room.plugins.<key>`:\n *\n * class ChatPlugin extends RoomPlugin {\n * readonly pluginName = 'chat' as const;\n * }\n *\n * Optional under the keyed-record form (`definePlugins({ chat: ... })`),\n * required under the array form. Must stay `public` so `extends` /\n * `keyof` can see the literal \u2014 those are blind to protected from\n * outside the class. End-user autocomplete hides it via `Omit` in\n * `definePlugins`'s return type.\n *\n * For multi-instance use, accept the name at construction:\n *\n * readonly pluginName: string;\n * constructor(name = 'chat') { super(); this.pluginName = name; }\n */\n readonly pluginName?: string;\n\n /**\n * Declarative message handlers \u2014 merged into the room's `messages` at\n * __init. Conflict against the room's own key: room wins. Conflict\n * between two plugins: throws at __init.\n */\n protected messages?: Messages<This>;\n\n /** Optional per-hook ordering vs the room's own hook. */\n protected order?: RoomPluginOrder;\n\n // Lifecycle hooks \u2014 override any subset. `protected` so they don't\n // leak into `this.plugins.<key>.onJoin(...)` autocomplete; subclass\n // overrides must keep `protected` (TS widens to public silently\n // otherwise).\n protected onCreate?(options: any): void | Promise<void>;\n protected onAuth?(client: Client, options: any, context: AuthContext): void | Promise<void>;\n protected onJoin?(client: Client, options?: any): void | Promise<void>;\n protected onLeave?(client: Client, code?: number): void | Promise<void>;\n protected onDispose?(): void | Promise<void>;\n}\n\n/**\n * Plugin-class constructor type used by the `dependencies` static\n * declaration. Constrained to zero-arg constructors because the\n * framework auto-instantiates missing dependencies with no\n * configuration \u2014 plugins that need options can't be auto-included\n * and must be registered explicitly in `definePlugins({...})`.\n */\nexport type RoomPluginClass = new () => RoomPlugin<any>;\n\n/**\n * Static `dependencies` declaration. List other plugin classes this\n * plugin needs alongside it; the framework auto-instantiates any\n * missing ones at room construction time. Transitive deps are\n * resolved recursively. Cycles throw at class-init.\n *\n * Example:\n * class UniqueSessionPlugin extends RoomPlugin {\n * static dependencies: PluginDependencies = [TrackUserSessionsPlugin];\n * }\n */\nexport type PluginDependencies = ReadonlyArray<RoomPluginClass>;\n\n/**\n * Define a Room's plugin record. The framework wires plugins at\n * `__init` \u2014 first construct of the class computes the layout\n * (cached on the constructor) and installs hook wrappers on the\n * prototype. The `const T` modifier preserves literal types so\n * `this.plugins.<key>.method()` autocompletes against each plugin's\n * specific subclass.\n */\n// Pulls each plugin's `pluginName` literal as the record key. Subclass\n// must narrow with `as const` \u2014 plain `string` resolves to `never`\n// and the entry is dropped (callers see a TS error on\n// `plugins.<thatKey>`).\ntype ExtractPluginName<P> = P extends { pluginName: infer K }\n ? (K extends string ? K : never)\n : never;\n\n/** Hide `pluginName` from end-user autocomplete on `this.plugins.<key>`. */\ntype PluginPublicSurface<P> = Omit<P, 'pluginName'>;\n\ntype PluginsArrayToRecord<T extends readonly RoomPlugin<any>[]> = {\n [P in T[number] as ExtractPluginName<P>]: PluginPublicSurface<P>;\n};\n\n/**\n * Array form (recommended). Each plugin declares its own canonical\n * key via `readonly pluginName = '...' as const`; the framework\n * turns the array into a typed record so `plugins.<pluginName>`\n * autocompletes the right instance type.\n *\n * class GameRoom extends Room {\n * plugins = definePlugins([\n * new ChatPlugin(), // pluginName: 'chat'\n * new UniqueSessionPlugin({ max: 1 }), // pluginName: 'uniqueSession'\n * ]);\n * }\n * this.plugins.chat.send('hi');\n *\n * Throws at runtime if any plugin is missing `pluginName` or if two\n * plugins resolve to the same key.\n */\nexport function definePlugins<const T extends readonly RoomPlugin<any>[]>(\n plugins: T,\n): PluginsArrayToRecord<T>;\n\n/**\n * Record form (legacy / multi-instance escape hatch). The caller\n * chooses the key per-room. Useful when registering two instances of\n * the same plugin class without configuring `pluginName` per\n * instance \u2014 e.g. `{ adminChat: new ChatPlugin(), playerChat: new ChatPlugin() }`.\n */\nexport function definePlugins<const T extends Record<string, RoomPlugin<any>>>(\n plugins: T,\n): { [K in keyof T]: PluginPublicSurface<T[K]> };\n\nexport function definePlugins(plugins: any): any {\n if (!Array.isArray(plugins)) { return plugins; }\n const out: Record<string, RoomPlugin> = {};\n for (const p of plugins) {\n const key = p['pluginName'];\n if (typeof key !== 'string' || key.length === 0) {\n throw new Error(\n `[Room] plugin ${p.constructor.name} is missing a 'pluginName' field. ` +\n `Declare \\`readonly pluginName = '<key>' as const\\` on the class, ` +\n `or register it via the keyed-record form \\`definePlugins({ <key>: <plugin> })\\`.`,\n );\n }\n if (out[key]) {\n throw new Error(\n `[Room] two plugins resolve to pluginName \"${key}\". Configure one of ` +\n `them via constructor argument, or use the keyed-record form to disambiguate.`,\n );\n }\n out[key] = p;\n }\n return out;\n}\n\n/**\n * Lifecycle hook keys recognized by the framework \u2014 used internally to\n * separate \"framework-recognized methods\" from \"user-defined public\n * methods\" when wiring a plugin into a room. Exported for the test\n * harness; downstream code should not need it.\n */\nexport const PLUGIN_LIFECYCLE_KEYS = ['onCreate', 'onAuth', 'onJoin', 'onLeave', 'onDispose'] as const;\nexport type PluginLifecycleKey = (typeof PLUGIN_LIFECYCLE_KEYS)[number];\n\n/**\n * Test helper \u2014 attach a stub or fake room to a plugin so its methods\n * and lifecycle hooks can be exercised in isolation without spinning up\n * a real Colyseus server.\n *\n * const plugin = new ChatPlugin({ historyLimit: 5 });\n * const room = attachToTestRoom(plugin, { broadcast: sinon.spy() });\n * await plugin.messages!.chat!.call(plugin, { userId: 'u1' }, { text: 'hi' });\n *\n * The second arg is shallow-merged onto the stub so tests only declare\n * the room properties they actually exercise. Returns the room stub for\n * post-call assertions.\n */\nexport function attachToTestRoom<This extends Room, R extends Partial<This>>(\n plugin: RoomPlugin<This>,\n roomStub: R = {} as R,\n): R {\n (plugin as any).room = roomStub;\n return roomStub;\n}\n\n// ---------------------------------------------------------------------------\n// Layout machinery \u2014 used by Room.__init to set plugins up once per class.\n// Lives in this file (rather than Room.ts) so the plugin-related types and\n// helpers stay co-located. These are framework internals; callers outside\n// `@colyseus/core` should not depend on them.\n// ---------------------------------------------------------------------------\n\n/**\n * Precomputed plugin layout for a Room subclass \u2014 populated on first\n * construct, cached on the constructor. Hook wrappers are installed\n * on the prototype in the same pass (see `installPluginHookWrappers`).\n *\n * @internal\n */\nexport interface PluginLayout {\n /** Per-hook participation: which plugin keys run before/after the room's own hook. */\n hooks: Record<PluginLifecycleKey, { before: string[]; after: string[] }>;\n /** Message key \u2192 plugin key. Conflict detection ran when this was built. */\n messageOwners: Map<string, string>;\n /** Sentinel-keyed plugin classes to instantiate per room. */\n autoDeps: Array<{ key: string; ctor: RoomPluginClass }>;\n}\n\n/** Sentinel prefix for framework-instantiated deps. The colon prevents\n * collisions with any JS identifier the user could use as a key. */\nconst DEP_KEY_PREFIX = '__dep:';\n\n/**\n * Default before/after policy for lifecycle hooks vs the room's own\n * hook. Plugins can override per-hook via the `order` field on the\n * plugin instance.\n *\n * onCreate / onAuth / onJoin \u2192 plugins run BEFORE room (guards + setup)\n * onLeave / onDispose \u2192 plugins run AFTER room (capture final state)\n *\n * @internal\n */\nexport const DEFAULT_PLUGIN_ORDER: Record<PluginLifecycleKey, 'before' | 'after'> = {\n onCreate: 'before',\n onAuth: 'before',\n onJoin: 'before',\n onLeave: 'after',\n onDispose: 'after',\n};\n\n/**\n * Walk the room's plugin instances and produce the lifecycle + message\n * layout for the Room class. Called once per Room subclass \u2014 on the\n * first construct \u2014 and the result is cached on the constructor. Throws\n * on duplicate message keys (named both plugin keys) so the failure is\n * visible at class-init rather than at first message dispatch.\n *\n * Returns `null` when no plugins participate in any hook AND none\n * declare a message \u2014 distinguishes \"computed, nothing to do\" from\n * \"not computed yet\" in the `__pluginLayout` cache.\n *\n * @internal\n */\nexport function computePluginLayout(plugins: Record<string, RoomPlugin>): PluginLayout | null {\n // Resolve `static dependencies` closures over the user's record.\n // Returns an expanded list (user + auto-deps), the auto-dep\n // class table for per-room instantiation, and a unified view of\n // entries to iterate when computing hooks / message owners.\n const resolved = resolveDependencies(plugins);\n\n const hooks = {} as Record<PluginLifecycleKey, { before: string[]; after: string[] }>;\n let anyHook = false;\n\n for (const hook of PLUGIN_LIFECYCLE_KEYS) {\n const before: string[] = [];\n const after: string[] = [];\n for (const { key, plugin } of resolved.entries) {\n if (typeof plugin[hook] !== 'function') { continue; }\n // Bracket access \u2014 `order` and `messages` are protected; TS\n // skips visibility checks on indexed access.\n const order = plugin['order']?.[hook] ?? DEFAULT_PLUGIN_ORDER[hook];\n (order === 'before' ? before : after).push(key);\n anyHook = true;\n }\n hooks[hook] = { before, after };\n }\n\n const messageOwners = new Map<string, string>();\n for (const { key, plugin } of resolved.entries) {\n const messages = plugin['messages'];\n if (!messages) { continue; }\n for (const messageKey of Object.keys(messages)) {\n const prior = messageOwners.get(messageKey);\n if (prior !== undefined) {\n throw new Error(\n `[Room] message key \"${messageKey}\" declared by multiple plugins: ` +\n `\"${prior}\" and \"${key}\". Resolve by giving one of them a ` +\n `different key, or override on the room's own \\`messages\\`.`,\n );\n }\n messageOwners.set(messageKey, key);\n }\n }\n\n if (!anyHook && messageOwners.size === 0 && resolved.autoDeps.length === 0) { return null; }\n return { hooks, messageOwners, autoDeps: resolved.autoDeps };\n}\n\n/**\n * Walk every plugin's `static dependencies` recursively, instantiating\n * missing classes (zero-arg only). Throws on cycles. Auto-deps are\n * keyed `__dep:<ClassName>` in the returned entries.\n */\nfunction resolveDependencies(plugins: Record<string, RoomPlugin>): {\n entries: Array<{ key: string; plugin: RoomPlugin }>;\n autoDeps: Array<{ key: string; ctor: RoomPluginClass }>;\n} {\n const entries: Array<{ key: string; plugin: RoomPlugin }> = [];\n for (const [key, plugin] of Object.entries(plugins)) {\n entries.push({ key, plugin });\n }\n\n const present = new Set<RoomPluginClass>();\n for (const { plugin } of entries) {\n present.add(plugin.constructor as RoomPluginClass);\n }\n\n const autoDeps: Array<{ key: string; ctor: RoomPluginClass }> = [];\n const visiting = new Set<RoomPluginClass>();\n const path: string[] = [];\n\n function walk(depCtor: RoomPluginClass): void {\n if (present.has(depCtor)) { return; }\n if (visiting.has(depCtor)) {\n const cycle = [...path, depCtor.name].join(' \u2192 ');\n throw new Error(`[Room] plugin dependency cycle: ${cycle}`);\n }\n visiting.add(depCtor);\n path.push(depCtor.name);\n\n // Recurse into deeper deps first \u2192 topological order in `entries`.\n const deeper = (depCtor as any).dependencies as PluginDependencies | undefined;\n if (Array.isArray(deeper)) {\n for (const inner of deeper) { walk(inner); }\n }\n\n let instance: RoomPlugin;\n try { instance = new depCtor(); }\n catch (err: any) {\n throw new Error(\n `[Room] auto-included plugin \"${depCtor.name}\" must be ` +\n `constructible with no arguments. If it needs options, ` +\n `register it explicitly in definePlugins({...}). ` +\n `(cause: ${err?.message ?? err})`,\n );\n }\n const key = DEP_KEY_PREFIX + depCtor.name;\n entries.push({ key, plugin: instance });\n autoDeps.push({ key, ctor: depCtor });\n present.add(depCtor);\n\n visiting.delete(depCtor);\n path.pop();\n }\n\n // Snapshot before mutating \u2014 transitive deps are handled recursively\n // inside `walk()`.\n const userEntriesSnapshot = entries.slice();\n for (const { plugin } of userEntriesSnapshot) {\n const deps = (plugin.constructor as any).dependencies as PluginDependencies | undefined;\n if (!Array.isArray(deps)) { continue; }\n for (const depCtor of deps) { walk(depCtor); }\n }\n\n return { entries, autoDeps };\n}\n\n/**\n * Install one wrapper per participating hook on the Room subclass's\n * prototype. The wrapper closes over plugin KEYS (resolved to refs at\n * call time) and invokes the captured original room hook between the\n * before/after plugin runs.\n *\n * @internal\n */\nexport function installPluginHookWrappers(\n ctor: { prototype: any },\n layout: PluginLayout | null,\n): void {\n if (layout === null) { return; }\n const proto = ctor.prototype;\n for (const hook of PLUGIN_LIFECYCLE_KEYS) {\n const { before, after } = layout.hooks[hook];\n if (before.length === 0 && after.length === 0) { continue; }\n\n const original = proto[hook] as Function | undefined;\n proto[hook] = async function (\n this: { plugins?: Record<string, RoomPlugin>; _autoPlugins?: Record<string, RoomPlugin> },\n ...args: any[]\n ) {\n // User plugins live on `this.plugins`, auto-deps (sentinel-keyed)\n // on `this._autoPlugins` \u2014 kept separate so user types stay clean.\n const lookup = (k: string): RoomPlugin =>\n (k.startsWith(DEP_KEY_PREFIX) ? this._autoPlugins![k] : this.plugins![k]);\n for (const k of before) {\n const p = lookup(k);\n await (p[hook] as Function).call(p, ...args);\n }\n let result: unknown;\n if (original) { result = await original.apply(this, args); }\n for (const k of after) {\n const p = lookup(k);\n await (p[hook] as Function).call(p, ...args);\n }\n return result;\n };\n }\n}\n\n/** Structural shape used by `setupRoomPlugins` \u2014 avoids a Room import\n * so the dependency graph stays one-way (Room \u2192 RoomPlugin). */\ninterface RoomPluginHost {\n plugins?: Record<string, RoomPlugin<any>>;\n _autoPlugins?: Record<string, RoomPlugin<any>>;\n messages?: Record<string, Function> | any;\n constructor: { __pluginLayout?: PluginLayout | null; prototype: any };\n}\n\n/**\n * Wire a Room instance's plugins. Once-per-class layout (hook\n * participation, message owners, dep resolution) is cached on the\n * constructor; per-instance work attaches `room` refs, instantiates\n * auto-deps, and merges plugin messages.\n *\n * @internal\n */\nexport function setupRoomPlugins(room: RoomPluginHost): void {\n const plugins = room.plugins!;\n const layout = resolveOrComputeLayout(room.constructor, plugins);\n\n attachRoomReference(room, plugins);\n\n if (layout && layout.autoDeps.length > 0) {\n room._autoPlugins = instantiateAutoDeps(room, layout);\n }\n\n if (layout && layout.messageOwners.size > 0) {\n mergePluginMessages(room, layout);\n }\n\n Object.freeze(plugins);\n if (room._autoPlugins) { Object.freeze(room._autoPlugins); }\n}\n\n/**\n * Read cached layout for this subclass, or compute + install on\n * first construct. `hasOwnProperty` so a subclass that redeclares\n * `plugins` doesn't inherit the parent's wrapping.\n */\nfunction resolveOrComputeLayout(\n ctor: RoomPluginHost['constructor'],\n plugins: Record<string, RoomPlugin>,\n): PluginLayout | null | undefined {\n if (Object.prototype.hasOwnProperty.call(ctor, '__pluginLayout')) {\n return ctor.__pluginLayout;\n }\n const layout = computePluginLayout(plugins);\n installPluginHookWrappers(ctor, layout);\n ctor.__pluginLayout = layout;\n return layout;\n}\n\n/** Wire `plugin.room = room` on every user-explicit plugin. */\nfunction attachRoomReference(\n room: RoomPluginHost,\n plugins: Record<string, RoomPlugin>,\n): void {\n for (const plugin of Object.values(plugins)) {\n (plugin as any).room = room;\n }\n}\n\n/**\n * Build `_autoPlugins` \u2014 one fresh instance per `static dependencies`\n * entry. Kept separate from `room.plugins` so sentinel keys\n * (`__dep:<ClassName>`) don't leak into the user's typed view.\n */\nfunction instantiateAutoDeps(\n room: RoomPluginHost,\n layout: PluginLayout,\n): Record<string, RoomPlugin> {\n const auto: Record<string, RoomPlugin> = {};\n for (const { key, ctor: depCtor } of layout.autoDeps) {\n const instance = new depCtor();\n (instance as any).room = room;\n auto[key] = instance;\n }\n return auto;\n}\n\n/**\n * Copy plugin message handlers into `room.messages` (room's own key\n * wins; plugin-vs-plugin conflicts already threw at layout time).\n */\nfunction mergePluginMessages(\n room: RoomPluginHost,\n layout: PluginLayout,\n): void {\n const plugins = room.plugins!;\n for (const [messageKey, pluginKey] of layout.messageOwners) {\n if (room.messages?.[messageKey]) { continue; }\n const source = pluginKey.startsWith(DEP_KEY_PREFIX)\n ? room._autoPlugins![pluginKey]\n : plugins[pluginKey];\n const handler = source['messages']?.[messageKey];\n if (handler !== undefined) {\n (room.messages ??= {} as any)[messageKey] = handler;\n }\n }\n}\n"],
5
+ "mappings": ";AAoEO,IAAe,aAAf,MAAoD;AAiD3D;AA8EO,SAAS,cAAc,SAAmB;AAC/C,MAAI,CAAC,MAAM,QAAQ,OAAO,GAAG;AAAE,WAAO;AAAA,EAAS;AAC/C,QAAM,MAAkC,CAAC;AACzC,aAAW,KAAK,SAAS;AACvB,UAAM,MAAM,EAAE,YAAY;AAC1B,QAAI,OAAO,QAAQ,YAAY,IAAI,WAAW,GAAG;AAC/C,YAAM,IAAI;AAAA,QACR,iBAAiB,EAAE,YAAY,IAAI;AAAA,MAGrC;AAAA,IACF;AACA,QAAI,IAAI,GAAG,GAAG;AACZ,YAAM,IAAI;AAAA,QACR,6CAA6C,GAAG;AAAA,MAElD;AAAA,IACF;AACA,QAAI,GAAG,IAAI;AAAA,EACb;AACA,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ED
@@ -51,7 +51,7 @@ var import_DevMode = require("./utils/DevMode.cjs");
51
51
  var import_router = require("./router/index.cjs");
52
52
  var import_shared_types = require("@colyseus/shared-types");
53
53
  var import_default_routes = require("./router/default_routes.cjs");
54
- var Server = class {
54
+ var Server = class _Server {
55
55
  constructor(options = {}) {
56
56
  this._onTransportReady = new import_Utils.Deferred();
57
57
  this._originalRoomOnMessage = null;
@@ -66,6 +66,7 @@ var Server = class {
66
66
  this.driver = options.driver || new import_LocalDriver.LocalDriver();
67
67
  this.options = options;
68
68
  this.greet = greet2;
69
+ _Server.current = this;
69
70
  this.attach(options);
70
71
  matchMaker.setup(
71
72
  this.presence,
@@ -82,10 +83,6 @@ var Server = class {
82
83
  }
83
84
  async attach(options) {
84
85
  this.transport = options.transport || await this.getDefaultTransport(options);
85
- if (options.express && this.transport.getExpressApp) {
86
- const expressApp = await this.transport.getExpressApp();
87
- await options.express(expressApp);
88
- }
89
86
  this._onTransportReady.resolve(this.transport);
90
87
  }
91
88
  /**
@@ -97,8 +94,20 @@ var Server = class {
97
94
  * @param listeningListener
98
95
  */
99
96
  async listen(port, hostname, backlog, listeningListener) {
100
- if (this.options.beforeListen) {
101
- await this.options.beforeListen();
97
+ const { beforeListen, database, express } = this.options;
98
+ if (beforeListen) {
99
+ await beforeListen();
100
+ }
101
+ if (database) {
102
+ await database.boot();
103
+ }
104
+ await this._applyRouterDefaults();
105
+ if (express) {
106
+ await this._onTransportReady;
107
+ if (this.transport.getExpressApp) {
108
+ const expressApp = await this.transport.getExpressApp();
109
+ await express(expressApp);
110
+ }
102
111
  }
103
112
  if (process.env.COLYSEUS_CLOUD !== void 0) {
104
113
  if (typeof hostname === "number") {
@@ -205,6 +214,30 @@ var Server = class {
205
214
  onBeforeShutdown(callback) {
206
215
  this.onBeforeShutdownCallback = callback;
207
216
  }
217
+ // Extend the user's router with framework-contributed endpoints. An explicit
218
+ // `auth` option wins over the database's `applyRouterDefaults` so a non-DB
219
+ // setup can still auto-mount @colyseus/auth, and a DB user can opt out.
220
+ async _applyRouterDefaults() {
221
+ const { auth, database } = this.options;
222
+ const wantsAuth = auth !== void 0 && auth !== false;
223
+ const wantsDatabase = auth === void 0 && !!database?.applyRouterDefaults;
224
+ if (!wantsAuth && !wantsDatabase) {
225
+ return;
226
+ }
227
+ this.router ??= (0, import_router.createRouter)({});
228
+ if (wantsAuth) {
229
+ const authMod = await (0, import_Utils.dynamicImport)("@colyseus/auth").catch(() => void 0);
230
+ const endpointsFn = authMod?.auth?.endpoints ?? authMod?.default?.auth?.endpoints;
231
+ if (typeof endpointsFn === "function") {
232
+ this.router = this.router.extend(endpointsFn(auth));
233
+ }
234
+ } else {
235
+ const updated = await database.applyRouterDefaults(this.router);
236
+ if (updated) {
237
+ this.router = updated;
238
+ }
239
+ }
240
+ }
208
241
  async getDefaultTransport(options) {
209
242
  try {
210
243
  const module2 = await (0, import_Utils.dynamicImport)("@colyseus/ws-transport");
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/Server.ts"],
4
- "sourcesContent": ["import { greet } from \"@colyseus/greeting-banner\";\nimport type express from 'express';\n\nimport { debugAndPrintError } from './Debug.ts';\nimport * as matchMaker from './MatchMaker.ts';\nimport { RegisteredHandler } from './matchmaker/RegisteredHandler.ts';\n\nimport { type OnCreateOptions, Room } from './Room.ts';\nimport { Deferred, registerGracefulShutdown, dynamicImport, type Type } from './utils/Utils.ts';\n\nimport type { Presence } from \"./presence/Presence.ts\";\nimport { LocalPresence } from './presence/LocalPresence.ts';\nimport { LocalDriver } from './matchmaker/LocalDriver/LocalDriver.ts';\n\nimport { setTransport, Transport } from './Transport.ts';\nimport { logger, setLogger } from './Logger.ts';\nimport { setDevMode, isDevMode } from './utils/DevMode.ts';\nimport { type Router, bindRouterToTransport } from './router/index.ts';\nimport { type SDKTypes as SharedSDKTypes } from '@colyseus/shared-types';\nimport { getDefaultRouter } from './router/default_routes.ts';\n\nexport type ServerOptions = {\n publicAddress?: string,\n presence?: Presence,\n driver?: matchMaker.MatchMakerDriver,\n transport?: Transport,\n gracefullyShutdown?: boolean,\n logger?: any;\n\n /**\n * Optional callback to execute before the server listens.\n * This is useful for example to connect into a database or other services before the server listens.\n */\n beforeListen?: () => Promise<void> | void,\n\n /**\n * Optional callback to configure Express routes.\n * When provided, the transport layer will initialize an Express-compatible app\n * and pass it to this callback for custom route configuration.\n *\n * For uWebSockets transport, this uses the uwebsockets-express module.\n */\n express?: (app: express.Application) => Promise<void> | void,\n\n /**\n * Custom function to determine which process should handle room creation.\n * Default: assign new rooms the process with least amount of rooms created\n */\n selectProcessIdToCreateRoom?: matchMaker.SelectProcessIdCallback;\n\n /**\n * Whether this process is running as a standalone match-maker or not. (default: false)\n * When enabled, this process will not spawn rooms and will only be responsible for matchmaking.\n */\n isStandaloneMatchMaker?: boolean; \n\n /**\n * If enabled, rooms are going to be restored in the server-side upon restart,\n * clients are going to automatically re-connect when server reboots.\n *\n * Beware of \"schema mismatch\" issues. When updating Schema structures and\n * reloading existing data, you may see \"schema mismatch\" errors in the\n * client-side.\n *\n * (This operation is costly and should not be used in a production\n * environment)\n */\n devMode?: boolean,\n\n /**\n * Display greeting message on server start.\n * Default: true\n */\n greet?: boolean,\n};\n\n/**\n * Exposed types for the client-side SDK.\n * Re-exported from @colyseus/shared-types with specific type constraints.\n */\nexport interface SDKTypes<\n RoomTypes extends Record<string, RegisteredHandler> = any,\n Routes extends Router = any\n> extends SharedSDKTypes<RoomTypes, Routes> {}\n\nexport class Server<\n RoomTypes extends Record<string, RegisteredHandler> = any,\n Routes extends Router = any\n> implements SDKTypes<RoomTypes, Routes> {\n '~rooms': RoomTypes;\n '~routes': Routes;\n\n public transport: Transport;\n public router: Routes;\n public options: ServerOptions;\n\n protected presence: Presence;\n protected driver: matchMaker.MatchMakerDriver;\n\n protected port: number | string;\n protected greet: boolean;\n\n protected _onTransportReady = new Deferred<Transport>();\n\n private _originalRoomOnMessage: typeof Room.prototype['_onMessage'] | null = null;\n\n constructor(options: ServerOptions = {}) {\n const {\n gracefullyShutdown = true,\n greet = true\n } = options;\n\n setDevMode(options.devMode === true);\n\n this.presence = options.presence || new LocalPresence();\n this.driver = options.driver || new LocalDriver();\n this.options = options;\n this.greet = greet;\n\n this.attach(options);\n\n matchMaker.setup(\n this.presence,\n this.driver,\n options.publicAddress,\n options.selectProcessIdToCreateRoom,\n );\n\n if (gracefullyShutdown) {\n registerGracefulShutdown((err) => this.gracefullyShutdown(true, err));\n }\n\n if (options.logger) {\n setLogger(options.logger);\n }\n }\n\n public async attach(options: ServerOptions) {\n this.transport = options.transport || await this.getDefaultTransport(options);\n\n // Initialize Express if callback is provided\n if (options.express && this.transport.getExpressApp) {\n const expressApp = await this.transport.getExpressApp();\n await options.express(expressApp);\n }\n\n // Resolve the promise when the transport is ready\n this._onTransportReady.resolve(this.transport);\n }\n\n /**\n * Bind the server into the port specified.\n *\n * @param port - Port number or Unix socket path\n * @param hostname\n * @param backlog\n * @param listeningListener\n */\n public async listen(port: number | string, hostname?: string, backlog?: number, listeningListener?: Function) {\n if (this.options.beforeListen) {\n await this.options.beforeListen();\n }\n\n //\n // if Colyseus Cloud is detected, use @colyseus/tools to listen\n //\n if (process.env.COLYSEUS_CLOUD !== undefined ) {\n if (typeof(hostname) === \"number\") {\n //\n // workaround, @colyseus/tools calls server.listen() again with the port as a string\n //\n hostname = undefined;\n\n } else {\n try {\n return (await dynamicImport(\"@colyseus/tools\")).listen(this);\n } catch (error) {\n const err = new Error(\"Please install @colyseus/tools to be able to host on Colyseus Cloud.\");\n err.cause = error;\n throw err;\n }\n }\n }\n\n //\n // otherwise, listen on the port directly\n //\n this.port = port;\n\n //\n // Make sure matchmaker is ready before accepting connections\n // (isDevMode: matchmaker may take extra milliseconds to restore the rooms)\n //\n await matchMaker.accept(this.options.isStandaloneMatchMaker);\n\n /**\n * Greetings!\n */\n if (this.greet) {\n greet();\n }\n\n // Wait for the transport to be ready\n await this._onTransportReady;\n\n return new Promise<void>((resolve, reject) => {\n // TODO: refactor me!\n // set transport globally, to be used by matchmaking route\n setTransport(this.transport);\n\n this.transport.listen(port, hostname, backlog, (err) => {\n if (this.transport.server) {\n this.transport.server.on('error', (err) => reject(err));\n }\n\n // default router is used if no router is provided\n if (!this.router) {\n this.router = getDefaultRouter() as unknown as Routes;\n\n } else {\n // make sure default routes are included\n // https://github.com/Bekacru/better-call/pull/67\n this.router = this.router.extend({ ...getDefaultRouter().endpoints }) as unknown as Routes;\n }\n\n bindRouterToTransport(this.transport, this.router, this.options.express !== undefined);\n\n if (listeningListener) {\n listeningListener(err);\n }\n\n if (err) {\n reject(err);\n\n } else {\n resolve();\n }\n });\n });\n }\n\n /**\n * Define a new type of room for matchmaking.\n *\n * @param name public room identifier for match-making.\n * @param roomClass Room class definition\n * @param defaultOptions default options for `onCreate`\n */\n public define<T extends Type<Room>>(\n roomClass: T,\n defaultOptions?: OnCreateOptions<T>,\n ): RegisteredHandler\n public define<T extends Type<Room>>(\n name: string,\n roomClass: T,\n defaultOptions?: OnCreateOptions<T>,\n ): RegisteredHandler\n public define<T extends Type<Room>>(\n nameOrHandler: string | T,\n handlerOrOptions: T | OnCreateOptions<T>,\n defaultOptions?: OnCreateOptions<T>,\n ): RegisteredHandler {\n const name = (typeof(nameOrHandler) === \"string\")\n ? nameOrHandler\n : nameOrHandler.name;\n\n const roomClass = (typeof(nameOrHandler) === \"string\")\n ? handlerOrOptions\n : nameOrHandler;\n\n const options = (typeof(nameOrHandler) === \"string\")\n ? defaultOptions\n : handlerOrOptions;\n\n return matchMaker.defineRoomType(name, roomClass, options);\n }\n\n /**\n * Remove a room definition from matchmaking.\n * This method does not destroy any room. It only dissallows matchmaking\n */\n public removeRoomType(name: string): void {\n matchMaker.removeRoomType(name);\n }\n\n public async gracefullyShutdown(exit: boolean = true, err?: Error) {\n if (matchMaker.state === matchMaker.MatchMakerState.SHUTTING_DOWN) {\n return;\n }\n\n try {\n // custom \"before shutdown\" method\n await this.onBeforeShutdownCallback();\n\n // this is going to lock all rooms and wait for them to be disposed\n await matchMaker.gracefullyShutdown();\n\n this.transport.shutdown();\n this.presence.shutdown();\n await this.driver.shutdown();\n\n // custom \"after shutdown\" method\n await this.onShutdownCallback();\n\n } catch (e) {\n debugAndPrintError(`error during shutdown: ${e}`);\n\n } finally {\n if (exit) {\n process.exit((err && !isDevMode) ? 1 : 0);\n }\n }\n }\n\n /**\n * Add simulated latency between client and server.\n * @param milliseconds round trip latency in milliseconds.\n */\n public simulateLatency(milliseconds: number) {\n if (milliseconds > 0) {\n logger.warn(`\uD83D\uDCF6\uFE0F\u2757 Colyseus latency simulation enabled \u2192 ${milliseconds}ms latency for round trip.`);\n } else {\n logger.warn(`\uD83D\uDCF6\uFE0F\u2757 Colyseus latency simulation disabled.`);\n }\n\n const halfwayMS = (milliseconds / 2);\n this.transport.simulateLatency(halfwayMS);\n\n if (this._originalRoomOnMessage == null) {\n this._originalRoomOnMessage = Room.prototype['_onMessage'];\n }\n\n const originalOnMessage = this._originalRoomOnMessage;\n\n Room.prototype['_onMessage'] = milliseconds <= Number.EPSILON ? originalOnMessage : function (this: Room, client, buffer) {\n // uWebSockets.js: duplicate buffer because it is cleared at native layer before the timeout.\n const cachedBuffer = Buffer.from(buffer);\n setTimeout(() => originalOnMessage.call(this, client, cachedBuffer), halfwayMS);\n };\n }\n\n /**\n * Register a callback that is going to be executed before the server shuts down.\n * @param callback\n */\n public onShutdown(callback: () => void | Promise<any>) {\n this.onShutdownCallback = callback;\n }\n\n public onBeforeShutdown(callback: () => void | Promise<any>) {\n this.onBeforeShutdownCallback = callback;\n }\n\n protected async getDefaultTransport(options: any): Promise<Transport> {\n try {\n const module = await dynamicImport('@colyseus/ws-transport');\n const WebSocketTransport = module.WebSocketTransport;\n return new WebSocketTransport(options);\n\n } catch (error) {\n this._onTransportReady.reject(error);\n throw new Error(\"Please provide a 'transport' layer. Default transport not set.\");\n }\n }\n\n protected onShutdownCallback: () => void | Promise<any> =\n () => Promise.resolve()\n\n protected onBeforeShutdownCallback: () => void | Promise<any> =\n () => Promise.resolve()\n}\n\nexport type RoomDefinitions = Record<string, RegisteredHandler | Type<Room>>;\n\nfunction isRegisteredHandler(value: RegisteredHandler | Type<Room>): value is RegisteredHandler {\n return value instanceof RegisteredHandler || (\n typeof(value) === \"object\" &&\n value !== null &&\n 'klass' in (value as object)\n );\n}\n\nexport function registerRoomDefinitions<T extends RoomDefinitions>(rooms: T): string[] {\n const roomNames: string[] = [];\n\n for (const [name, value] of Object.entries(rooms)) {\n if (isRegisteredHandler(value)) {\n value.name = name;\n matchMaker.addRoomType(value);\n\n } else {\n matchMaker.defineRoomType(name, value);\n }\n\n roomNames.push(name);\n }\n\n return roomNames;\n}\n\nexport function unregisterRoomDefinitions(roomNames: Iterable<string>) {\n for (const roomName of roomNames) {\n matchMaker.removeRoomType(roomName);\n }\n}\n\nexport type DefineServerOptions<\n T extends Record<string, RegisteredHandler>,\n R extends Router\n> = ServerOptions & {\n rooms: T,\n routes?: R,\n};\n\nexport function defineServer<\n T extends Record<string, RegisteredHandler>,\n R extends Router\n>(\n options: DefineServerOptions<T, R>,\n): Server<T, R> {\n const { rooms, routes, ...serverOptions } = options;\n\n if (isDevMode) {\n // In dev mode, the Vite plugin manages Server/matchMaker lifecycle.\n // Return a config-only object \u2014 no Server instance, no matchMaker.setup().\n return {\n options: serverOptions,\n router: routes,\n '~rooms': rooms,\n } as unknown as Server<T, R>;\n }\n\n const server = new Server<T, R>(serverOptions);\n server.router = routes;\n\n registerRoomDefinitions(rooms);\n\n return server;\n}\n\nexport function defineRoom<T extends Type<Room>>(\n roomKlass: T,\n defaultOptions?: Parameters<NonNullable<InstanceType<T>['onCreate']>>[0],\n): RegisteredHandler<InstanceType<T>> {\n return new RegisteredHandler(roomKlass, defaultOptions) as unknown as RegisteredHandler<InstanceType<T>>;\n}\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA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;AAEhB,0BAAwB,KAAK;AAE7B,SAAO;AACT;AAEO,SAAS,WACd,WACA,gBACoC;AACpC,SAAO,IAAI,2CAAkB,WAAW,cAAc;AACxD;",
4
+ "sourcesContent": ["import { greet } from \"@colyseus/greeting-banner\";\nimport type express from 'express';\n\nimport { debugAndPrintError } from './Debug.ts';\nimport * as matchMaker from './MatchMaker.ts';\nimport { RegisteredHandler } from './matchmaker/RegisteredHandler.ts';\n\nimport { type OnCreateOptions, Room } from './Room.ts';\nimport { Deferred, registerGracefulShutdown, dynamicImport, type Type } from './utils/Utils.ts';\n\nimport type { Presence } from \"./presence/Presence.ts\";\nimport { LocalPresence } from './presence/LocalPresence.ts';\nimport { LocalDriver } from './matchmaker/LocalDriver/LocalDriver.ts';\n\nimport { setTransport, Transport } from './Transport.ts';\nimport { logger, setLogger } from './Logger.ts';\nimport { setDevMode, isDevMode } from './utils/DevMode.ts';\nimport { type Router, bindRouterToTransport, createRouter } from './router/index.ts';\nimport { type SDKTypes as SharedSDKTypes } from '@colyseus/shared-types';\nimport { getDefaultRouter } from './router/default_routes.ts';\n\nexport type ServerOptions = {\n publicAddress?: string,\n presence?: Presence,\n driver?: matchMaker.MatchMakerDriver,\n transport?: Transport,\n gracefullyShutdown?: boolean,\n logger?: any;\n\n /**\n * Optional callback to execute before the server listens.\n * This is useful for example to connect into a database or other services before the server listens.\n */\n beforeListen?: () => Promise<void> | void,\n\n /**\n * Booted before `matchMaker.accept()`. Structural so `@colyseus/core` keeps\n * no runtime dep on `@colyseus/database` \u2014 any `{ boot(): Promise<void> }`\n * works. The optional `applyRouterDefaults` is invoked after boot with the\n * user's router so the database can contribute endpoints (e.g. auth routes).\n */\n database?: {\n boot(): Promise<void>;\n applyRouterDefaults?(router: Router): Router | Promise<Router>;\n },\n\n /**\n * Mount `@colyseus/auth` routes into the router. Pass an options object\n * (forwarded to `auth.endpoints(...)`) to wire it explicitly \u2014 useful when\n * `database` is not in use. `false` disables auto-mounting even if a\n * `database` is present and would have provided defaults.\n */\n auth?: false | {\n settings?: Record<string, any>;\n oauth?: boolean | { cookieSecret?: string };\n prefix?: string;\n },\n\n /**\n * Optional callback to configure Express routes.\n * When provided, the transport layer will initialize an Express-compatible app\n * and pass it to this callback for custom route configuration.\n *\n * For uWebSockets transport, this uses the uwebsockets-express module.\n */\n express?: (app: express.Application) => Promise<void> | void,\n\n /**\n * Custom function to determine which process should handle room creation.\n * Default: assign new rooms the process with least amount of rooms created\n */\n selectProcessIdToCreateRoom?: matchMaker.SelectProcessIdCallback;\n\n /**\n * Whether this process is running as a standalone match-maker or not. (default: false)\n * When enabled, this process will not spawn rooms and will only be responsible for matchmaking.\n */\n isStandaloneMatchMaker?: boolean; \n\n /**\n * If enabled, rooms are going to be restored in the server-side upon restart,\n * clients are going to automatically re-connect when server reboots.\n *\n * Beware of \"schema mismatch\" issues. When updating Schema structures and\n * reloading existing data, you may see \"schema mismatch\" errors in the\n * client-side.\n *\n * (This operation is costly and should not be used in a production\n * environment)\n */\n devMode?: boolean,\n\n /**\n * Display greeting message on server start.\n * Default: true\n */\n greet?: boolean,\n};\n\n/**\n * Exposed types for the client-side SDK.\n * Re-exported from @colyseus/shared-types with specific type constraints.\n */\nexport interface SDKTypes<\n RoomTypes extends Record<string, RegisteredHandler> = any,\n Routes extends Router = any\n> extends SharedSDKTypes<RoomTypes, Routes> {}\n\nexport class Server<\n RoomTypes extends Record<string, RegisteredHandler> = any,\n Routes extends Router = any\n> implements SDKTypes<RoomTypes, Routes> {\n '~rooms': RoomTypes;\n '~routes': Routes;\n\n public transport: Transport;\n public router: Routes;\n public options: ServerOptions;\n\n protected presence: Presence;\n protected driver: matchMaker.MatchMakerDriver;\n\n protected port: number | string;\n protected greet: boolean;\n\n protected _onTransportReady = new Deferred<Transport>();\n\n private _originalRoomOnMessage: typeof Room.prototype['_onMessage'] | null = null;\n\n // Implicit default for callers that omit explicit Server reference \u2014 e.g.\n // `playground()` reads `Server.current.router.endpoints` at request time.\n // Last-construction wins; multi-server setups should reference instances\n // explicitly.\n static current: Server<any, any> | undefined;\n\n constructor(options: ServerOptions = {}) {\n const {\n gracefullyShutdown = true,\n greet = true\n } = options;\n\n setDevMode(options.devMode === true);\n\n this.presence = options.presence || new LocalPresence();\n this.driver = options.driver || new LocalDriver();\n this.options = options;\n this.greet = greet;\n\n (Server as { current: Server<any, any> | undefined }).current = this as Server<any, any>;\n\n this.attach(options);\n\n matchMaker.setup(\n this.presence,\n this.driver,\n options.publicAddress,\n options.selectProcessIdToCreateRoom,\n );\n\n if (gracefullyShutdown) {\n registerGracefulShutdown((err) => this.gracefullyShutdown(true, err));\n }\n\n if (options.logger) {\n setLogger(options.logger);\n }\n }\n\n public async attach(options: ServerOptions) {\n this.transport = options.transport || await this.getDefaultTransport(options);\n // `options.express` runs in `listen()` after `database.boot()` so user\n // code reading `database.auth.settings` etc. finds services instantiated.\n this._onTransportReady.resolve(this.transport);\n }\n\n /**\n * Bind the server into the port specified.\n *\n * @param port - Port number or Unix socket path\n * @param hostname\n * @param backlog\n * @param listeningListener\n */\n public async listen(port: number | string, hostname?: string, backlog?: number, listeningListener?: Function) {\n const { beforeListen, database, express } = this.options;\n\n if (beforeListen) { await beforeListen(); }\n if (database) { await database.boot(); }\n\n await this._applyRouterDefaults();\n\n if (express) {\n await this._onTransportReady;\n if (this.transport.getExpressApp) {\n const expressApp = await this.transport.getExpressApp();\n await express(expressApp);\n }\n }\n\n //\n // if Colyseus Cloud is detected, use @colyseus/tools to listen\n //\n if (process.env.COLYSEUS_CLOUD !== undefined ) {\n if (typeof(hostname) === \"number\") {\n //\n // workaround, @colyseus/tools calls server.listen() again with the port as a string\n //\n hostname = undefined;\n\n } else {\n try {\n return (await dynamicImport(\"@colyseus/tools\")).listen(this);\n } catch (error) {\n const err = new Error(\"Please install @colyseus/tools to be able to host on Colyseus Cloud.\");\n err.cause = error;\n throw err;\n }\n }\n }\n\n //\n // otherwise, listen on the port directly\n //\n this.port = port;\n\n //\n // Make sure matchmaker is ready before accepting connections\n // (isDevMode: matchmaker may take extra milliseconds to restore the rooms)\n //\n await matchMaker.accept(this.options.isStandaloneMatchMaker);\n\n /**\n * Greetings!\n */\n if (this.greet) {\n greet();\n }\n\n // Wait for the transport to be ready\n await this._onTransportReady;\n\n return new Promise<void>((resolve, reject) => {\n // TODO: refactor me!\n // set transport globally, to be used by matchmaking route\n setTransport(this.transport);\n\n this.transport.listen(port, hostname, backlog, (err) => {\n if (this.transport.server) {\n this.transport.server.on('error', (err) => reject(err));\n }\n\n // default router is used if no router is provided\n if (!this.router) {\n this.router = getDefaultRouter() as unknown as Routes;\n\n } else {\n // make sure default routes are included\n // https://github.com/Bekacru/better-call/pull/67\n this.router = this.router.extend({ ...getDefaultRouter().endpoints }) as unknown as Routes;\n }\n\n bindRouterToTransport(this.transport, this.router, this.options.express !== undefined);\n\n if (listeningListener) {\n listeningListener(err);\n }\n\n if (err) {\n reject(err);\n\n } else {\n resolve();\n }\n });\n });\n }\n\n /**\n * Define a new type of room for matchmaking.\n *\n * @param name public room identifier for match-making.\n * @param roomClass Room class definition\n * @param defaultOptions default options for `onCreate`\n */\n public define<T extends Type<Room>>(\n roomClass: T,\n defaultOptions?: OnCreateOptions<T>,\n ): RegisteredHandler\n public define<T extends Type<Room>>(\n name: string,\n roomClass: T,\n defaultOptions?: OnCreateOptions<T>,\n ): RegisteredHandler\n public define<T extends Type<Room>>(\n nameOrHandler: string | T,\n handlerOrOptions: T | OnCreateOptions<T>,\n defaultOptions?: OnCreateOptions<T>,\n ): RegisteredHandler {\n const name = (typeof(nameOrHandler) === \"string\")\n ? nameOrHandler\n : nameOrHandler.name;\n\n const roomClass = (typeof(nameOrHandler) === \"string\")\n ? handlerOrOptions\n : nameOrHandler;\n\n const options = (typeof(nameOrHandler) === \"string\")\n ? defaultOptions\n : handlerOrOptions;\n\n return matchMaker.defineRoomType(name, roomClass, options);\n }\n\n /**\n * Remove a room definition from matchmaking.\n * This method does not destroy any room. It only dissallows matchmaking\n */\n public removeRoomType(name: string): void {\n matchMaker.removeRoomType(name);\n }\n\n public async gracefullyShutdown(exit: boolean = true, err?: Error) {\n if (matchMaker.state === matchMaker.MatchMakerState.SHUTTING_DOWN) {\n return;\n }\n\n try {\n // custom \"before shutdown\" method\n await this.onBeforeShutdownCallback();\n\n // this is going to lock all rooms and wait for them to be disposed\n await matchMaker.gracefullyShutdown();\n\n this.transport.shutdown();\n this.presence.shutdown();\n await this.driver.shutdown();\n\n // custom \"after shutdown\" method\n await this.onShutdownCallback();\n\n } catch (e) {\n debugAndPrintError(`error during shutdown: ${e}`);\n\n } finally {\n if (exit) {\n process.exit((err && !isDevMode) ? 1 : 0);\n }\n }\n }\n\n /**\n * Add simulated latency between client and server.\n * @param milliseconds round trip latency in milliseconds.\n */\n public simulateLatency(milliseconds: number) {\n if (milliseconds > 0) {\n logger.warn(`\uD83D\uDCF6\uFE0F\u2757 Colyseus latency simulation enabled \u2192 ${milliseconds}ms latency for round trip.`);\n } else {\n logger.warn(`\uD83D\uDCF6\uFE0F\u2757 Colyseus latency simulation disabled.`);\n }\n\n const halfwayMS = (milliseconds / 2);\n this.transport.simulateLatency(halfwayMS);\n\n if (this._originalRoomOnMessage == null) {\n this._originalRoomOnMessage = Room.prototype['_onMessage'];\n }\n\n const originalOnMessage = this._originalRoomOnMessage;\n\n Room.prototype['_onMessage'] = milliseconds <= Number.EPSILON ? originalOnMessage : function (this: Room, client, buffer) {\n // uWebSockets.js: duplicate buffer because it is cleared at native layer before the timeout.\n const cachedBuffer = Buffer.from(buffer);\n setTimeout(() => originalOnMessage.call(this, client, cachedBuffer), halfwayMS);\n };\n }\n\n /**\n * Register a callback that is going to be executed before the server shuts down.\n * @param callback\n */\n public onShutdown(callback: () => void | Promise<any>) {\n this.onShutdownCallback = callback;\n }\n\n public onBeforeShutdown(callback: () => void | Promise<any>) {\n this.onBeforeShutdownCallback = callback;\n }\n\n // Extend the user's router with framework-contributed endpoints. An explicit\n // `auth` option wins over the database's `applyRouterDefaults` so a non-DB\n // setup can still auto-mount @colyseus/auth, and a DB user can opt out.\n private async _applyRouterDefaults(): Promise<void> {\n const { auth, database } = this.options;\n const wantsAuth = auth !== undefined && auth !== false;\n const wantsDatabase = auth === undefined && !!database?.applyRouterDefaults;\n if (!wantsAuth && !wantsDatabase) { return; }\n\n // Boot an empty router if the user didn't pass `routes` \u2014 the framework's\n // default routes get layered on later in the `transport.listen()` callback.\n this.router ??= createRouter({}) as unknown as Routes;\n\n if (wantsAuth) {\n const authMod: any = await dynamicImport('@colyseus/auth').catch(() => undefined);\n const endpointsFn = authMod?.auth?.endpoints ?? authMod?.default?.auth?.endpoints;\n if (typeof endpointsFn === 'function') {\n this.router = (this.router as Router).extend(endpointsFn(auth)) as Routes;\n }\n } else {\n const updated = await database!.applyRouterDefaults!(this.router as Router);\n if (updated) { this.router = updated as Routes; }\n }\n }\n\n protected async getDefaultTransport(options: any): Promise<Transport> {\n try {\n const module = await dynamicImport('@colyseus/ws-transport');\n const WebSocketTransport = module.WebSocketTransport;\n return new WebSocketTransport(options);\n\n } catch (error) {\n this._onTransportReady.reject(error);\n throw new Error(\"Please provide a 'transport' layer. Default transport not set.\");\n }\n }\n\n protected onShutdownCallback: () => void | Promise<any> =\n () => Promise.resolve()\n\n protected onBeforeShutdownCallback: () => void | Promise<any> =\n () => Promise.resolve()\n}\n\nexport type RoomDefinitions = Record<string, RegisteredHandler | Type<Room>>;\n\nfunction isRegisteredHandler(value: RegisteredHandler | Type<Room>): value is RegisteredHandler {\n return value instanceof RegisteredHandler || (\n typeof(value) === \"object\" &&\n value !== null &&\n 'klass' in (value as object)\n );\n}\n\nexport function registerRoomDefinitions<T extends RoomDefinitions>(rooms: T): string[] {\n const roomNames: string[] = [];\n\n for (const [name, value] of Object.entries(rooms)) {\n if (isRegisteredHandler(value)) {\n value.name = name;\n matchMaker.addRoomType(value);\n\n } else {\n matchMaker.defineRoomType(name, value);\n }\n\n roomNames.push(name);\n }\n\n return roomNames;\n}\n\nexport function unregisterRoomDefinitions(roomNames: Iterable<string>) {\n for (const roomName of roomNames) {\n matchMaker.removeRoomType(roomName);\n }\n}\n\nexport type DefineServerOptions<\n T extends Record<string, RegisteredHandler>,\n R extends Router\n> = ServerOptions & {\n rooms: T,\n routes?: R,\n};\n\nexport function defineServer<\n T extends Record<string, RegisteredHandler>,\n R extends Router\n>(\n options: DefineServerOptions<T, R>,\n): Server<T, R> {\n const { rooms, routes, ...serverOptions } = options;\n\n if (isDevMode) {\n // In dev mode, the Vite plugin manages Server/matchMaker lifecycle.\n // Return a config-only object \u2014 no Server instance, no matchMaker.setup().\n return {\n options: serverOptions,\n router: routes,\n '~rooms': rooms,\n } as unknown as Server<T, R>;\n }\n\n const server = new Server<T, R>(serverOptions);\n server.router = routes;\n\n registerRoomDefinitions(rooms);\n\n return server;\n}\n\nexport function defineRoom<T extends Type<Room>>(\n roomKlass: T,\n defaultOptions?: Parameters<NonNullable<InstanceType<T>['onCreate']>>[0],\n): RegisteredHandler<InstanceType<T>> {\n return new RegisteredHandler(roomKlass, defaultOptions) as unknown as RegisteredHandler<InstanceType<T>>;\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,6BAAsB;AAGtB,mBAAmC;AACnC,iBAA4B;AAC5B,+BAAkC;AAElC,kBAA2C;AAC3C,mBAA6E;AAG7E,2BAA8B;AAC9B,yBAA4B;AAE5B,uBAAwC;AACxC,oBAAkC;AAClC,qBAAsC;AACtC,oBAAiE;AACjE,0BAAgD;AAChD,4BAAiC;AAyF1B,IAAM,SAAN,MAAM,QAG4B;AAAA,EAwBvC,YAAY,UAAyB,CAAC,GAAG;AAVzC,SAAU,oBAAoB,IAAI,sBAAoB;AAEtD,SAAQ,yBAAqE;AA2S7E,SAAU,qBACR,MAAM,QAAQ,QAAQ;AAExB,SAAU,2BACR,MAAM,QAAQ,QAAQ;AAtStB,UAAM;AAAA,MACJ,oBAAAA,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,EACrB;AAEA,SAAO;AACT;AAEO,SAAS,0BAA0B,WAA6B;AACrE,aAAW,YAAY,WAAW;AAChC,IAAW,0BAAe,QAAQ;AAAA,EACpC;AACF;AAUO,SAAS,aAId,SACc;AACd,QAAM,EAAE,OAAO,QAAQ,GAAG,cAAc,IAAI;AAE5C,MAAI,0BAAW;AAGb,WAAO;AAAA,MACL,SAAS;AAAA,MACT,QAAQ;AAAA,MACR,UAAU;AAAA,IACZ;AAAA,EACF;AAEA,QAAM,SAAS,IAAI,OAAa,aAAa;AAC7C,SAAO,SAAS;AAEhB,0BAAwB,KAAK;AAE7B,SAAO;AACT;AAEO,SAAS,WACd,WACA,gBACoC;AACpC,SAAO,IAAI,2CAAkB,WAAW,cAAc;AACxD;",
6
6
  "names": ["gracefullyShutdown", "greet", "err", "module"]
7
7
  }
package/build/Server.d.ts CHANGED
@@ -19,6 +19,29 @@ export type ServerOptions = {
19
19
  * This is useful for example to connect into a database or other services before the server listens.
20
20
  */
21
21
  beforeListen?: () => Promise<void> | void;
22
+ /**
23
+ * Booted before `matchMaker.accept()`. Structural so `@colyseus/core` keeps
24
+ * no runtime dep on `@colyseus/database` — any `{ boot(): Promise<void> }`
25
+ * works. The optional `applyRouterDefaults` is invoked after boot with the
26
+ * user's router so the database can contribute endpoints (e.g. auth routes).
27
+ */
28
+ database?: {
29
+ boot(): Promise<void>;
30
+ applyRouterDefaults?(router: Router): Router | Promise<Router>;
31
+ };
32
+ /**
33
+ * Mount `@colyseus/auth` routes into the router. Pass an options object
34
+ * (forwarded to `auth.endpoints(...)`) to wire it explicitly — useful when
35
+ * `database` is not in use. `false` disables auto-mounting even if a
36
+ * `database` is present and would have provided defaults.
37
+ */
38
+ auth?: false | {
39
+ settings?: Record<string, any>;
40
+ oauth?: boolean | {
41
+ cookieSecret?: string;
42
+ };
43
+ prefix?: string;
44
+ };
22
45
  /**
23
46
  * Optional callback to configure Express routes.
24
47
  * When provided, the transport layer will initialize an Express-compatible app
@@ -73,6 +96,7 @@ export declare class Server<RoomTypes extends Record<string, RegisteredHandler>
73
96
  protected greet: boolean;
74
97
  protected _onTransportReady: Deferred<Transport>;
75
98
  private _originalRoomOnMessage;
99
+ static current: Server<any, any> | undefined;
76
100
  constructor(options?: ServerOptions);
77
101
  attach(options: ServerOptions): Promise<void>;
78
102
  /**
@@ -110,6 +134,7 @@ export declare class Server<RoomTypes extends Record<string, RegisteredHandler>
110
134
  */
111
135
  onShutdown(callback: () => void | Promise<any>): void;
112
136
  onBeforeShutdown(callback: () => void | Promise<any>): void;
137
+ private _applyRouterDefaults;
113
138
  protected getDefaultTransport(options: any): Promise<Transport>;
114
139
  protected onShutdownCallback: () => void | Promise<any>;
115
140
  protected onBeforeShutdownCallback: () => void | Promise<any>;
package/build/Server.mjs CHANGED
@@ -10,10 +10,10 @@ import { LocalDriver } from "./matchmaker/LocalDriver/LocalDriver.mjs";
10
10
  import { setTransport } from "./Transport.mjs";
11
11
  import { logger, setLogger } from "./Logger.mjs";
12
12
  import { setDevMode, isDevMode } from "./utils/DevMode.mjs";
13
- import { bindRouterToTransport } from "./router/index.mjs";
13
+ import { bindRouterToTransport, createRouter } from "./router/index.mjs";
14
14
  import "@colyseus/shared-types";
15
15
  import { getDefaultRouter } from "./router/default_routes.mjs";
16
- var Server = class {
16
+ var Server = class _Server {
17
17
  constructor(options = {}) {
18
18
  this._onTransportReady = new Deferred();
19
19
  this._originalRoomOnMessage = null;
@@ -28,6 +28,7 @@ var Server = class {
28
28
  this.driver = options.driver || new LocalDriver();
29
29
  this.options = options;
30
30
  this.greet = greet2;
31
+ _Server.current = this;
31
32
  this.attach(options);
32
33
  matchMaker.setup(
33
34
  this.presence,
@@ -44,10 +45,6 @@ var Server = class {
44
45
  }
45
46
  async attach(options) {
46
47
  this.transport = options.transport || await this.getDefaultTransport(options);
47
- if (options.express && this.transport.getExpressApp) {
48
- const expressApp = await this.transport.getExpressApp();
49
- await options.express(expressApp);
50
- }
51
48
  this._onTransportReady.resolve(this.transport);
52
49
  }
53
50
  /**
@@ -59,8 +56,20 @@ var Server = class {
59
56
  * @param listeningListener
60
57
  */
61
58
  async listen(port, hostname, backlog, listeningListener) {
62
- if (this.options.beforeListen) {
63
- await this.options.beforeListen();
59
+ const { beforeListen, database, express } = this.options;
60
+ if (beforeListen) {
61
+ await beforeListen();
62
+ }
63
+ if (database) {
64
+ await database.boot();
65
+ }
66
+ await this._applyRouterDefaults();
67
+ if (express) {
68
+ await this._onTransportReady;
69
+ if (this.transport.getExpressApp) {
70
+ const expressApp = await this.transport.getExpressApp();
71
+ await express(expressApp);
72
+ }
64
73
  }
65
74
  if (process.env.COLYSEUS_CLOUD !== void 0) {
66
75
  if (typeof hostname === "number") {
@@ -167,6 +176,30 @@ var Server = class {
167
176
  onBeforeShutdown(callback) {
168
177
  this.onBeforeShutdownCallback = callback;
169
178
  }
179
+ // Extend the user's router with framework-contributed endpoints. An explicit
180
+ // `auth` option wins over the database's `applyRouterDefaults` so a non-DB
181
+ // setup can still auto-mount @colyseus/auth, and a DB user can opt out.
182
+ async _applyRouterDefaults() {
183
+ const { auth, database } = this.options;
184
+ const wantsAuth = auth !== void 0 && auth !== false;
185
+ const wantsDatabase = auth === void 0 && !!database?.applyRouterDefaults;
186
+ if (!wantsAuth && !wantsDatabase) {
187
+ return;
188
+ }
189
+ this.router ??= createRouter({});
190
+ if (wantsAuth) {
191
+ const authMod = await dynamicImport("@colyseus/auth").catch(() => void 0);
192
+ const endpointsFn = authMod?.auth?.endpoints ?? authMod?.default?.auth?.endpoints;
193
+ if (typeof endpointsFn === "function") {
194
+ this.router = this.router.extend(endpointsFn(auth));
195
+ }
196
+ } else {
197
+ const updated = await database.applyRouterDefaults(this.router);
198
+ if (updated) {
199
+ this.router = updated;
200
+ }
201
+ }
202
+ }
170
203
  async getDefaultTransport(options) {
171
204
  try {
172
205
  const module = await dynamicImport("@colyseus/ws-transport");