@colyseus/core 0.17.9 → 0.17.11

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 (67) hide show
  1. package/build/MatchMaker.cjs +13 -13
  2. package/build/MatchMaker.cjs.map +2 -2
  3. package/build/MatchMaker.d.ts +2 -9
  4. package/build/MatchMaker.mjs +1 -1
  5. package/build/MatchMaker.mjs.map +2 -2
  6. package/build/Protocol.cjs +9 -49
  7. package/build/Protocol.cjs.map +2 -2
  8. package/build/Protocol.d.ts +1 -40
  9. package/build/Protocol.mjs +1 -38
  10. package/build/Protocol.mjs.map +2 -2
  11. package/build/Room.cjs +28 -27
  12. package/build/Room.cjs.map +2 -2
  13. package/build/Room.d.ts +4 -16
  14. package/build/Room.mjs +6 -1
  15. package/build/Room.mjs.map +2 -2
  16. package/build/Server.cjs +5 -3
  17. package/build/Server.cjs.map +2 -2
  18. package/build/Server.d.ts +8 -4
  19. package/build/Server.mjs +5 -3
  20. package/build/Server.mjs.map +2 -2
  21. package/build/Transport.cjs +2 -2
  22. package/build/Transport.cjs.map +2 -2
  23. package/build/Transport.mjs +1 -1
  24. package/build/Transport.mjs.map +2 -2
  25. package/build/errors/RoomExceptions.cjs.map +2 -2
  26. package/build/errors/RoomExceptions.d.ts +2 -1
  27. package/build/errors/RoomExceptions.mjs.map +2 -2
  28. package/build/errors/ServerError.cjs +2 -2
  29. package/build/errors/ServerError.cjs.map +2 -2
  30. package/build/errors/ServerError.mjs +1 -1
  31. package/build/errors/ServerError.mjs.map +1 -1
  32. package/build/index.cjs +4 -3
  33. package/build/index.cjs.map +2 -2
  34. package/build/index.d.ts +3 -2
  35. package/build/index.mjs +6 -1
  36. package/build/index.mjs.map +2 -2
  37. package/build/matchmaker/controller.cjs +3 -3
  38. package/build/matchmaker/controller.cjs.map +2 -2
  39. package/build/matchmaker/controller.mjs +1 -1
  40. package/build/matchmaker/controller.mjs.map +1 -1
  41. package/build/rooms/RankedQueueRoom.cjs +21 -7
  42. package/build/rooms/RankedQueueRoom.cjs.map +2 -2
  43. package/build/rooms/RankedQueueRoom.d.ts +3 -1
  44. package/build/rooms/RankedQueueRoom.mjs +5 -1
  45. package/build/rooms/RankedQueueRoom.mjs.map +2 -2
  46. package/build/rooms/RelayRoom.cjs +2 -2
  47. package/build/rooms/RelayRoom.cjs.map +2 -2
  48. package/build/rooms/RelayRoom.mjs +1 -1
  49. package/build/rooms/RelayRoom.mjs.map +2 -2
  50. package/build/serializer/SchemaSerializer.cjs +4 -4
  51. package/build/serializer/SchemaSerializer.cjs.map +2 -2
  52. package/build/serializer/SchemaSerializer.mjs +1 -1
  53. package/build/serializer/SchemaSerializer.mjs.map +2 -2
  54. package/package.json +3 -2
  55. package/src/MatchMaker.ts +3 -11
  56. package/src/Protocol.ts +1 -48
  57. package/src/Room.ts +14 -22
  58. package/src/Server.ts +15 -9
  59. package/src/Transport.ts +3 -1
  60. package/src/errors/RoomExceptions.ts +2 -1
  61. package/src/errors/ServerError.ts +1 -1
  62. package/src/index.ts +12 -2
  63. package/src/matchmaker/controller.ts +1 -1
  64. package/src/rooms/RankedQueueRoom.ts +7 -1
  65. package/src/rooms/RelayRoom.ts +1 -1
  66. package/src/serializer/SchemaSerializer.ts +2 -1
  67. package/src/serializer/SchemaSerializerDebug.ts +0 -148
package/src/Protocol.ts CHANGED
@@ -1,53 +1,6 @@
1
1
  import { Packr } from '@colyseus/msgpackr';
2
2
  import { encode, type Iterator } from '@colyseus/schema';
3
-
4
- /**
5
- * Colyseus protocol codes range between 0~100
6
- */
7
- export const Protocol = {
8
- // Room-related (10~19)
9
- JOIN_ROOM: 10,
10
- ERROR: 11,
11
- LEAVE_ROOM: 12,
12
- ROOM_DATA: 13,
13
- ROOM_STATE: 14,
14
- ROOM_STATE_PATCH: 15,
15
- // ROOM_DATA_SCHEMA: 16, // DEPRECATED: used to send schema instances via room.send()
16
- ROOM_DATA_BYTES: 17,
17
- PING: 18,
18
- } as const;
19
- export type Protocol = typeof Protocol[keyof typeof Protocol];
20
-
21
- /**
22
- * WebSocket close codes
23
- * (See https://github.com/Luka967/websocket-close-codes)
24
- */
25
- export const CloseCode = {
26
- NORMAL_CLOSURE: 1000,
27
- GOING_AWAY: 1001,
28
- NO_STATUS_RECEIVED: 1005,
29
- ABNORMAL_CLOSURE: 1006,
30
-
31
- CONSENTED: 4000,
32
- SERVER_SHUTDOWN: 4001,
33
- WITH_ERROR: 4002,
34
- DEVMODE_RESTART: 4010
35
- } as const;
36
- export type CloseCode = typeof CloseCode[keyof typeof CloseCode];
37
-
38
- export const ErrorCode = {
39
- // HTTP MatchMaking Error Codes
40
- MATCHMAKE_NO_HANDLER: 520,
41
- MATCHMAKE_INVALID_CRITERIA: 521,
42
- MATCHMAKE_INVALID_ROOM_ID: 522,
43
- MATCHMAKE_UNHANDLED: 523, // generic exception during onCreate/onJoin
44
- MATCHMAKE_EXPIRED: 524, // generic exception during onCreate/onJoin
45
- AUTH_FAILED: 525,
46
- APPLICATION_ERROR: 526,
47
-
48
- INVALID_PAYLOAD: 4217,
49
- } as const;
50
- export type ErrorCode = typeof ErrorCode[keyof typeof ErrorCode];
3
+ import { Protocol } from '@colyseus/shared-types';
51
4
 
52
5
  // Inter-process communication protocol
53
6
  export const IpcProtocol = {
package/src/Room.ts CHANGED
@@ -12,7 +12,7 @@ import type { IRoomCache } from './matchmaker/driver.ts';
12
12
  import { NoneSerializer } from './serializer/NoneSerializer.ts';
13
13
  import { SchemaSerializer } from './serializer/SchemaSerializer.ts';
14
14
 
15
- import { CloseCode, ErrorCode, getMessageBytes, Protocol } from './Protocol.ts';
15
+ import { getMessageBytes } from './Protocol.ts';
16
16
  import { type Type, Deferred, generateId, wrapTryCatch } from './utils/Utils.ts';
17
17
  import { createNanoEvents } from './utils/nanoevents.ts';
18
18
  import { isDevMode } from './utils/DevMode.ts';
@@ -25,6 +25,15 @@ import { type RoomMethodName, OnAuthException, OnCreateException, OnDisposeExcep
25
25
  import { standardValidate, type StandardSchemaV1 } from './utils/StandardSchema.ts';
26
26
  import { matchMaker } from '@colyseus/core';
27
27
 
28
+ import {
29
+ CloseCode,
30
+ ErrorCode,
31
+ Protocol,
32
+ type MessageHandlerWithFormat as SharedMessageHandlerWithFormat,
33
+ type MessageHandler as SharedMessageHandler,
34
+ type Messages as SharedMessages,
35
+ } from '@colyseus/shared-types';
36
+
28
37
  const DEFAULT_PATCH_RATE = 1000 / 20; // 20fps (50ms)
29
38
  const DEFAULT_SIMULATION_INTERVAL = 1000 / 60; // 60fps (16.66ms)
30
39
  const noneSerializer = new NoneSerializer();
@@ -52,32 +61,15 @@ export interface IBroadcastOptions extends ISendOptions {
52
61
  * Message handler with automatic type inference from format schema.
53
62
  * When a format is provided, the message type is automatically inferred from the schema.
54
63
  */
55
- export type MessageHandlerWithFormat<T extends StandardSchemaV1 = any, This = any> = {
56
- format: T;
57
- handler: (this: This, client: Client, message: StandardSchemaV1.InferOutput<T>) => void;
58
- };
59
-
60
- export type MessageHandler<This = any> =
61
- | ((this: This, client: Client, message: any) => void)
62
- | MessageHandlerWithFormat<any, This>;
64
+ export type MessageHandlerWithFormat<T extends StandardSchemaV1 = any, This = any> =
65
+ SharedMessageHandlerWithFormat<T, Client, This>;
63
66
 
64
- /**
65
- * Extract the message payload type from a message handler.
66
- * Works with both function handlers and format handlers.
67
- *
68
- * (Imported from @colyseus/sdk, not used in the server-side)
69
- */
70
- export type ExtractMessageType<T> =
71
- T extends { format: infer Format extends StandardSchemaV1; handler: any }
72
- ? StandardSchemaV1.InferOutput<Format>
73
- : T extends (this: any, client: any, message: infer Message) => void
74
- ? Message
75
- : any;
67
+ export type MessageHandler<This = any> = SharedMessageHandler<Client, This>;
76
68
 
77
69
  /**
78
70
  * A map of message types to message handlers.
79
71
  */
80
- export type Messages<This extends Room> = Record<string, MessageHandler<This>>;
72
+ export type Messages<This extends Room> = SharedMessages<This, Client>;
81
73
 
82
74
  /**
83
75
  * Helper function to create a validated message handler with automatic type inference.
package/src/Server.ts CHANGED
@@ -16,6 +16,7 @@ import { logger, setLogger } from './Logger.ts';
16
16
  import { setDevMode, isDevMode } from './utils/DevMode.ts';
17
17
  import { bindRouterToServer, type Router } from './router/index.ts';
18
18
  import { getDefaultRouter } from "./router/default_routes.ts";
19
+ import { type SDKTypes as SharedSDKTypes } from '@colyseus/shared-types';
19
20
 
20
21
  export type ServerOptions = {
21
22
  publicAddress?: string,
@@ -53,14 +54,12 @@ export type ServerOptions = {
53
54
 
54
55
  /**
55
56
  * Exposed types for the client-side SDK.
57
+ * Re-exported from @colyseus/shared-types with specific type constraints.
56
58
  */
57
59
  export interface SDKTypes<
58
60
  RoomTypes extends Record<string, RegisteredHandler> = any,
59
61
  Routes extends Router = any
60
- > {
61
- '~rooms': RoomTypes;
62
- '~routes': Routes;
63
- }
62
+ > extends SharedSDKTypes<RoomTypes, Routes> {}
64
63
 
65
64
  export class Server<
66
65
  RoomTypes extends Record<string, RegisteredHandler> = any,
@@ -297,19 +296,26 @@ export class Server<
297
296
  () => Promise.resolve()
298
297
  }
299
298
 
299
+ export type DefineServerOptions<
300
+ T extends Record<string, RegisteredHandler>,
301
+ R extends Router
302
+ > = ServerOptions & {
303
+ rooms: T,
304
+ routes?: R,
305
+ };
306
+
300
307
  export function defineServer<
301
308
  T extends Record<string, RegisteredHandler>,
302
309
  R extends Router
303
310
  >(
304
- roomHandlers: T,
305
- router?: R,
306
- serverOptions?: ServerOptions
311
+ options: DefineServerOptions<T, R>,
307
312
  ): Server<T, R> {
313
+ const { rooms, routes, ...serverOptions } = options;
308
314
  const server = new Server<T, R>(serverOptions);
309
315
 
310
- server.router = router;
316
+ server.router = routes;
311
317
 
312
- for (const [name, handler] of Object.entries(roomHandlers)) {
318
+ for (const [name, handler] of Object.entries(rooms)) {
313
319
  handler.name = name;
314
320
  matchMaker.addRoomType(handler);
315
321
  }
package/src/Transport.ts CHANGED
@@ -1,11 +1,13 @@
1
1
  import * as http from 'http';
2
2
  import * as https from 'https';
3
3
 
4
+ import { ErrorCode } from '@colyseus/shared-types';
4
5
  import { StateView } from '@colyseus/schema';
6
+
5
7
  import { EventEmitter } from 'events';
6
8
  import { spliceOne } from './utils/Utils.ts';
7
9
  import { ServerError } from './errors/ServerError.ts';
8
- import { ErrorCode } from './Protocol.ts';
10
+
9
11
  import type { Room } from './Room.ts';
10
12
 
11
13
  export abstract class Transport {
@@ -1,4 +1,5 @@
1
- import type { ExtractMessageType, Room } from '../Room.ts';
1
+ import type { ExtractMessageType } from '@colyseus/shared-types';
2
+ import type { Room } from '../Room.ts';
2
3
 
3
4
  export type RoomMethodName = 'onCreate'
4
5
  | 'onAuth'
@@ -1,4 +1,4 @@
1
- import { ErrorCode } from '../Protocol.ts';
1
+ import { ErrorCode } from '@colyseus/shared-types';
2
2
 
3
3
  export class ServerError extends Error {
4
4
  public code: number;
package/src/index.ts CHANGED
@@ -1,9 +1,19 @@
1
1
  import { ClockTimer as Clock, Delayed } from '@colyseus/timer';
2
2
 
3
+ // Shared types - re-export from @colyseus/shared-types for convenience
4
+ export {
5
+ Protocol,
6
+ ErrorCode,
7
+ CloseCode,
8
+ type InferState,
9
+ type ExtractRoomMessages,
10
+ type ExtractRoomClientMessages,
11
+ } from '@colyseus/shared-types';
12
+
3
13
  // Core classes
4
14
  export { Server, defineRoom, defineServer, type ServerOptions, type SDKTypes } from './Server.ts';
5
- export { Room, room, RoomInternalState, validate, type RoomOptions, type MessageHandlerWithFormat, type ExtractMessageType, type Messages, type ExtractRoomState, type ExtractRoomMetadata, type ExtractRoomClient } from './Room.ts';
6
- export { Protocol, ErrorCode, getMessageBytes, CloseCode } from './Protocol.ts';
15
+ export { Room, room, RoomInternalState, validate, type RoomOptions, type MessageHandlerWithFormat, type Messages, type ExtractRoomState, type ExtractRoomMetadata, type ExtractRoomClient } from './Room.ts';
16
+ export { getMessageBytes } from './Protocol.ts';
7
17
  export { RegisteredHandler } from './matchmaker/RegisteredHandler.ts';
8
18
  export { ServerError } from './errors/ServerError.ts';
9
19
 
@@ -3,7 +3,7 @@
3
3
  * (for interoperability between different http frameworks, e.g. express, uWebSockets.js, etc)
4
4
  */
5
5
 
6
- import { ErrorCode } from '../Protocol.ts';
6
+ import { ErrorCode } from '@colyseus/shared-types';
7
7
  import { ServerError } from '../errors/ServerError.ts';
8
8
  import * as matchMaker from '../MatchMaker.ts';
9
9
  import type { AuthContext } from '../Transport.ts';
@@ -1,4 +1,10 @@
1
- import { Room, type Client, matchMaker, type IRoomCache, debugMatchMaking, ServerError, ErrorCode } from "@colyseus/core";
1
+ import { Room } from '../Room.ts';
2
+ import type { Client } from '../Transport.ts';
3
+ import type { IRoomCache } from '../matchmaker/driver.ts';
4
+ import * as matchMaker from '../MatchMaker.ts';
5
+ import { debugMatchMaking } from '../Debug.ts';
6
+ import { ServerError } from '../errors/ServerError.ts';
7
+ import { ErrorCode } from '@colyseus/shared-types';
2
8
 
3
9
  export interface RankedQueueOptions {
4
10
  /**
@@ -1,8 +1,8 @@
1
+ import { CloseCode } from '@colyseus/shared-types';
1
2
  import { defineTypes, MapSchema, Schema } from '@colyseus/schema';
2
3
 
3
4
  import { Room } from '../Room.ts';
4
5
  import type { Client } from '../Transport.ts';
5
- import { CloseCode } from '../Protocol.ts';
6
6
 
7
7
  class Player extends Schema {
8
8
  public connected: boolean;
@@ -1,9 +1,10 @@
1
+ import { Protocol } from '@colyseus/shared-types';
2
+
1
3
  import type { Serializer } from './Serializer.ts';
2
4
  import { type Client, ClientState } from '../Transport.ts';
3
5
 
4
6
  import { type Iterator, Encoder, dumpChanges, Reflection, Schema, StateView } from '@colyseus/schema';
5
7
  import { debugPatch } from '../Debug.ts';
6
- import { Protocol } from '../Protocol.ts';
7
8
 
8
9
  const SHARED_VIEW = {};
9
10
 
@@ -1,148 +0,0 @@
1
- /**
2
- * This serializer is a copy of SchemaSerializer,
3
- * but it writes debug information to a file.
4
- *
5
- * This script must be used
6
- */
7
-
8
- import fs from 'fs';
9
- import { Schema, dumpChanges, Iterator } from '@colyseus/schema';
10
- import { SchemaSerializer } from './SchemaSerializer.ts';
11
- import { Client, ClientState } from "../Transport.ts";
12
- import { Protocol} from '../Protocol.ts';
13
- import { debugPatch } from '../Debug.ts';
14
-
15
- /*
16
- const SHARED_VIEW = {};
17
-
18
- export class SchemaSerializerDebug<T> extends SchemaSerializer<T> {
19
- protected debugStream: fs.WriteStream;
20
-
21
- constructor(fileName: string = "schema-debug.txt") {
22
- super();
23
-
24
- try { fs.unlinkSync(fileName); } catch (e) {}
25
- this.debugStream = fs.createWriteStream(fileName, { flags: "a" });
26
- }
27
-
28
- public getFullState(client?: Client): Buffer {
29
- const buf = super.getFullState(client);
30
- this.debugStream.write(`state:${client.sessionId}:${Array.from(buf).slice(1).join(",")}\n`);
31
- return buf;
32
- }
33
-
34
- public applyPatches(clients: Client[]) {
35
- let numClients = clients.length;
36
-
37
- const debugChangesDeep = Schema.debugChangesDeep(this.encoder.state);
38
-
39
- if (
40
- numClients == 0 ||
41
- (
42
- this.encoder.root.changes.size === 0 &&
43
- (!this.hasFilters || this.encoder.root.filteredChanges.size === 0)
44
- )
45
- ) {
46
- // skip patching state if:
47
- // - no clients are connected
48
- // - no changes were made
49
- // - no "filtered changes" were made when using filters
50
- return false;
51
- }
52
-
53
- this.needFullEncode = true;
54
-
55
- // dump changes for patch debugging
56
- if (debugPatch.enabled) {
57
- (debugPatch as any).dumpChanges = dumpChanges(this.encoder.state);
58
- }
59
-
60
- // get patch bytes
61
- const it: Iterator = { offset: 1 };
62
- this.encoder.sharedBuffer[0] = Protocol.ROOM_STATE_PATCH;
63
-
64
- // encode changes once, for all clients
65
- const encodedChanges = this.encoder.encode(it);
66
-
67
- if (!this.hasFilters) {
68
- while (numClients--) {
69
- const client = clients[numClients];
70
-
71
- //
72
- // FIXME: avoid this check for each client
73
- //
74
- if (client.state !== ClientState.JOINED) {
75
- continue;
76
- }
77
-
78
- debugChangesDeep.split("\n").forEach((line) => {
79
- this.debugStream.write(`#${client.sessionId}:${line}\n`);
80
- });
81
- this.debugStream.write(`patch:${client.sessionId}:${Array.from(encodedChanges).slice(1).join(",")}\n`);
82
-
83
- client.raw(encodedChanges);
84
- }
85
-
86
- } else {
87
- // cache shared offset
88
- const sharedOffset = it.offset;
89
-
90
- // encode state multiple times, for each client
91
- while (numClients--) {
92
- const client = clients[numClients];
93
-
94
- //
95
- // FIXME: avoid this check for each client
96
- //
97
- if (client.state !== ClientState.JOINED) {
98
- continue;
99
- }
100
-
101
- const view = client.view || SHARED_VIEW;
102
-
103
- let encodedView = this.views.get(view);
104
-
105
- // allow to pass the same encoded view for multiple clients
106
- if (encodedView === undefined) {
107
- encodedView = (view === SHARED_VIEW)
108
- ? encodedChanges
109
- : this.encoder.encodeView(client.view, sharedOffset, it);
110
- this.views.set(view, encodedView);
111
- }
112
-
113
- debugChangesDeep.split("\n").forEach((line) => {
114
- this.debugStream.write(`#${client.sessionId}:${line}\n`);
115
- });
116
- this.debugStream.write(`patch:${client.sessionId}:${Array.from(encodedView).slice(1).join(",")}\n`);
117
-
118
- client.raw(encodedView);
119
- }
120
-
121
- // clear views
122
- this.views.clear();
123
- }
124
-
125
- // discard changes after sending
126
- this.encoder.discardChanges();
127
-
128
- // debug patches
129
- if (debugPatch.enabled) {
130
- debugPatch(
131
- '%d bytes sent to %d clients, %j',
132
- encodedChanges.length,
133
- clients.length,
134
- (debugPatch as any).dumpChanges,
135
- );
136
- }
137
-
138
- return true;
139
- }
140
-
141
- public handshake(): Buffer {
142
- const buf = super.handshake();
143
- this.debugStream.write(`handshake:${Array.from(buf).join(",")}\n`);
144
- return buf;
145
- }
146
- }
147
-
148
- */