@colyseus/sdk 0.17.7 → 0.17.8

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 (70) hide show
  1. package/build/3rd_party/discord.cjs +1 -1
  2. package/build/3rd_party/discord.mjs +1 -1
  3. package/build/Auth.cjs +1 -2
  4. package/build/Auth.cjs.map +1 -1
  5. package/build/Auth.d.ts +27 -10
  6. package/build/Auth.mjs +1 -2
  7. package/build/Auth.mjs.map +1 -1
  8. package/build/Client.cjs +1 -1
  9. package/build/Client.cjs.map +1 -1
  10. package/build/Client.d.ts +3 -3
  11. package/build/Client.mjs +1 -1
  12. package/build/Client.mjs.map +1 -1
  13. package/build/Connection.cjs +1 -1
  14. package/build/Connection.mjs +1 -1
  15. package/build/HTTP.cjs +5 -1
  16. package/build/HTTP.cjs.map +1 -1
  17. package/build/HTTP.mjs +5 -1
  18. package/build/HTTP.mjs.map +1 -1
  19. package/build/Protocol.cjs +1 -1
  20. package/build/Protocol.mjs +1 -1
  21. package/build/Room.cjs +2 -2
  22. package/build/Room.cjs.map +1 -1
  23. package/build/Room.d.ts +1 -5
  24. package/build/Room.mjs +2 -2
  25. package/build/Room.mjs.map +1 -1
  26. package/build/Storage.cjs +1 -1
  27. package/build/Storage.mjs +1 -1
  28. package/build/core/nanoevents.cjs +1 -1
  29. package/build/core/nanoevents.cjs.map +1 -1
  30. package/build/core/nanoevents.d.ts +9 -5
  31. package/build/core/nanoevents.mjs +1 -1
  32. package/build/core/nanoevents.mjs.map +1 -1
  33. package/build/core/signal.cjs +1 -1
  34. package/build/core/signal.mjs +1 -1
  35. package/build/core/utils.cjs +1 -1
  36. package/build/core/utils.mjs +1 -1
  37. package/build/errors/Errors.cjs +1 -1
  38. package/build/errors/Errors.mjs +1 -1
  39. package/build/index.cjs +1 -1
  40. package/build/index.cjs.map +1 -1
  41. package/build/index.d.ts +1 -1
  42. package/build/index.mjs +1 -1
  43. package/build/index.mjs.map +1 -1
  44. package/build/legacy.cjs +1 -1
  45. package/build/legacy.mjs +1 -1
  46. package/build/serializer/NoneSerializer.cjs +1 -1
  47. package/build/serializer/NoneSerializer.mjs +1 -1
  48. package/build/serializer/SchemaSerializer.cjs +1 -1
  49. package/build/serializer/SchemaSerializer.mjs +1 -1
  50. package/build/serializer/Serializer.cjs +1 -1
  51. package/build/serializer/Serializer.mjs +1 -1
  52. package/build/transport/H3Transport.cjs +1 -1
  53. package/build/transport/H3Transport.mjs +1 -1
  54. package/build/transport/WebSocketTransport.cjs +2 -2
  55. package/build/transport/WebSocketTransport.cjs.map +1 -1
  56. package/build/transport/WebSocketTransport.mjs +2 -2
  57. package/build/transport/WebSocketTransport.mjs.map +1 -1
  58. package/dist/colyseus-cocos-creator.js +7 -4
  59. package/dist/colyseus-cocos-creator.js.map +1 -1
  60. package/dist/colyseus.js +7 -4
  61. package/dist/colyseus.js.map +1 -1
  62. package/dist/debug.js +1 -1
  63. package/package.json +2 -2
  64. package/src/Auth.ts +49 -18
  65. package/src/Client.ts +4 -4
  66. package/src/HTTP.ts +5 -0
  67. package/src/Room.ts +1 -1
  68. package/src/core/nanoevents.ts +11 -3
  69. package/src/index.ts +1 -1
  70. package/src/transport/WebSocketTransport.ts +1 -1
package/dist/debug.js CHANGED
@@ -3,7 +3,7 @@
3
3
  // This software is released under the MIT License.
4
4
  // https://opensource.org/license/MIT
5
5
  //
6
- // colyseus.js@0.17.7
6
+ // colyseus.js@0.17.8
7
7
  (function (Client) {
8
8
  'use strict';
9
9
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@colyseus/sdk",
3
- "version": "0.17.7",
3
+ "version": "0.17.8",
4
4
  "description": "Colyseus Multiplayer SDK for JavaScript/TypeScript",
5
5
  "author": "Endel Dreyer",
6
6
  "license": "MIT",
@@ -81,7 +81,7 @@
81
81
  "typescript": "^5.9.3",
82
82
  "vite": "^5.0.11",
83
83
  "vitest": "^2.1.1",
84
- "@colyseus/core": "^0.17.7"
84
+ "@colyseus/core": "^0.17.9"
85
85
  },
86
86
  "peerDependencies": {
87
87
  "@colyseus/core": "0.17.x"
package/src/Auth.ts CHANGED
@@ -13,12 +13,32 @@ export interface PopupSettings {
13
13
  height: number;
14
14
  }
15
15
 
16
- export interface AuthData {
17
- user: any;
16
+ /**
17
+ * Response from getUserData()
18
+ */
19
+ export interface UserDataResponse<UserData = any> {
20
+ user: UserData;
21
+ }
22
+
23
+ /**
24
+ * Response from authentication methods (login, register, anonymous, OAuth)
25
+ */
26
+ export interface AuthResponse<UserData = any> {
27
+ user: UserData;
18
28
  token: string;
19
29
  }
20
30
 
21
- export class Auth {
31
+ /**
32
+ * Response from sendPasswordResetEmail()
33
+ */
34
+ export type ForgotPasswordResponse = boolean | unknown;
35
+
36
+ /**
37
+ * @deprecated Use AuthResponse instead
38
+ */
39
+ export type AuthData<UserData = any> = AuthResponse<UserData>;
40
+
41
+ export class Auth<UserData = any> {
22
42
  settings: AuthSettings = {
23
43
  path: "/auth",
24
44
  key: "colyseus-auth-token",
@@ -26,7 +46,9 @@ export class Auth {
26
46
 
27
47
  #_initialized = false;
28
48
  #_signInWindow: WindowProxy | null = null;
29
- #_events = createNanoEvents();
49
+ #_events = createNanoEvents<{
50
+ change: (response: AuthResponse) => void;
51
+ }>();
30
52
 
31
53
  protected http: HTTP<any>;
32
54
 
@@ -43,10 +65,10 @@ export class Auth {
43
65
  return this.http.authToken;
44
66
  }
45
67
 
46
- public onChange(callback: (response: AuthData) => void) {
68
+ public onChange<U = UserData>(callback: (response: AuthResponse<U | null>) => void) {
47
69
  const unbindChange = this.#_events.on("change", callback);
48
70
  if (!this.#_initialized) {
49
- this.getUserData().then((userData: any) => {
71
+ this.getUserData().then((userData) => {
50
72
  this.emitChange({ ...userData, token: this.token });
51
73
 
52
74
  }).catch((e) => {
@@ -58,7 +80,7 @@ export class Auth {
58
80
  return unbindChange;
59
81
  }
60
82
 
61
- public async getUserData() {
83
+ public async getUserData<U = UserData>(): Promise<UserDataResponse<U>> {
62
84
  if (this.token) {
63
85
  return (await this.http.get(`${this.settings.path}/userdata`)).data;
64
86
  } else {
@@ -66,43 +88,53 @@ export class Auth {
66
88
  }
67
89
  }
68
90
 
69
- public async registerWithEmailAndPassword(email: string, password: string, options?: any) {
91
+ public async registerWithEmailAndPassword<U = UserData>(
92
+ email: string,
93
+ password: string,
94
+ options?: any
95
+ ): Promise<AuthResponse<U>> {
70
96
  const data = (await this.http.post(`${this.settings.path}/register`, {
71
97
  body: { email, password, options, },
72
98
  })).data;
73
99
 
74
- this.emitChange(data as any);
100
+ this.emitChange(data);
75
101
 
76
102
  return data;
77
103
  }
78
104
 
79
- public async signInWithEmailAndPassword(email: string, password: string) {
105
+ public async signInWithEmailAndPassword<U = UserData>(
106
+ email: string,
107
+ password: string
108
+ ): Promise<AuthResponse<U>> {
80
109
  const data = (await this.http.post(`${this.settings.path}/login`, {
81
110
  body: { email, password, },
82
111
  })).data;
83
112
 
84
- this.emitChange(data as any);
113
+ this.emitChange(data);
85
114
 
86
115
  return data;
87
116
  }
88
117
 
89
- public async signInAnonymously(options?: any) {
118
+ public async signInAnonymously<U = UserData>(options?: any): Promise<AuthResponse<U>> {
90
119
  const data = (await this.http.post(`${this.settings.path}/anonymous`, {
91
120
  body: { options, }
92
121
  })).data;
93
122
 
94
- this.emitChange(data as any);
123
+ this.emitChange(data);
95
124
 
96
125
  return data;
97
126
  }
98
127
 
99
- public async sendPasswordResetEmail(email: string) {
128
+ public async sendPasswordResetEmail(email: string): Promise<ForgotPasswordResponse> {
100
129
  return (await this.http.post(`${this.settings.path}/forgot-password`, {
101
130
  body: { email, }
102
131
  })).data;
103
132
  }
104
133
 
105
- public async signInWithProvider(providerName: string, settings: Partial<PopupSettings> = {}) {
134
+ public async signInWithProvider<U = UserData>(
135
+ providerName: string,
136
+ settings: Partial<PopupSettings> = {}
137
+ ): Promise<AuthResponse<U>> {
106
138
  return new Promise((resolve, reject) => {
107
139
  const w = settings.width || 480;
108
140
  const h = settings.height || 768;
@@ -153,12 +185,11 @@ export class Auth {
153
185
  });
154
186
  }
155
187
 
156
- public async signOut() {
157
- // @ts-ignore
188
+ public async signOut(): Promise<void> {
158
189
  this.emitChange({ user: null, token: null });
159
190
  }
160
191
 
161
- private emitChange(authData: Partial<AuthData>) {
192
+ private emitChange(authData: AuthResponse<UserData | null>) {
162
193
  if (authData.token !== undefined) {
163
194
  this.token = authData.token;
164
195
 
package/src/Client.ts CHANGED
@@ -50,7 +50,7 @@ export interface LatencyOptions {
50
50
  pingCount?: number;
51
51
  }
52
52
 
53
- export class ColyseusSDK<ServerType extends SDKTypes = any> {
53
+ export class ColyseusSDK<ServerType extends SDKTypes = any, UserData = any> {
54
54
  static VERSION = "0.17";
55
55
 
56
56
  /**
@@ -61,7 +61,7 @@ export class ColyseusSDK<ServerType extends SDKTypes = any> {
61
61
  /**
62
62
  * The authentication module to authenticate into requests and rooms.
63
63
  */
64
- public auth: Auth;
64
+ public auth: Auth<UserData>;
65
65
 
66
66
  /**
67
67
  * The settings used to connect to the server.
@@ -144,12 +144,12 @@ export class ColyseusSDK<ServerType extends SDKTypes = any> {
144
144
  * @param latencyOptions Latency measurement options (protocol, pingCount).
145
145
  * @returns The client with the lowest latency.
146
146
  */
147
- static async selectByLatency<ServerType extends SDKTypes = any>(
147
+ static async selectByLatency<ServerType extends SDKTypes = any, UserData = any>(
148
148
  endpoints: Array<string | EndpointSettings>,
149
149
  options?: ClientOptions,
150
150
  latencyOptions: LatencyOptions = {}
151
151
  ) {
152
- const clients = endpoints.map(endpoint => new ColyseusSDK<ServerType>(endpoint, options));
152
+ const clients = endpoints.map(endpoint => new ColyseusSDK<ServerType, UserData>(endpoint, options));
153
153
 
154
154
  const latencies = (await Promise.allSettled(clients.map((client, index) => client.getLatency(latencyOptions).then(latency => {
155
155
  const settings = clients[index].settings;
package/src/HTTP.ts CHANGED
@@ -483,6 +483,11 @@ export class HTTP<R extends Router | Router["endpoints"]> {
483
483
  : requestOptions?.headers
484
484
  );
485
485
 
486
+ // Add Authorization header if authToken is set
487
+ if (this.authToken && !headers.has("authorization")) {
488
+ headers.set("authorization", `Bearer ${this.authToken}`);
489
+ }
490
+
486
491
  // Stringify JSON-serializable objects for fetch() body
487
492
  if (isJSONSerializable(body) && typeof body === 'object' && body !== null) {
488
493
  if (!headers.has("content-type")) {
package/src/Room.ts CHANGED
@@ -430,7 +430,7 @@ export class Room<
430
430
  this.onMessageHandlers.emit('*', type, message);
431
431
 
432
432
  } else if (!messageType.startsWith("__")) { // ignore internal messages
433
- console.warn?.(`colyseus.js: onMessage() not registered for type '${type}'.`);
433
+ console.warn?.(`@colyseus/sdk: onMessage() not registered for type '${type}'.`);
434
434
  }
435
435
  }
436
436
 
@@ -21,15 +21,23 @@
21
21
  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
22
  */
23
23
 
24
- export const createNanoEvents = () => ({
25
- emit(event: string, ...args: any[]) {
24
+ export type EventsMap = Record<string, (...args: any[]) => void>;
25
+
26
+ export interface Emitter<Events extends EventsMap> {
27
+ events: { [K in keyof Events]?: Array<Events[K]> };
28
+ emit<K extends keyof Events>(event: K, ...args: Parameters<Events[K]>): void;
29
+ on<K extends keyof Events>(event: K, cb: Events[K]): () => void;
30
+ }
31
+
32
+ export const createNanoEvents = <Events extends EventsMap = EventsMap>(): Emitter<Events> => ({
33
+ emit(event, ...args) {
26
34
  let callbacks = this.events[event] || []
27
35
  for (let i = 0, length = callbacks.length; i < length; i++) {
28
36
  callbacks[i](...args)
29
37
  }
30
38
  },
31
39
  events: {},
32
- on(event: string, cb: (...args: any[]) => void) {
40
+ on(event, cb) {
33
41
  this.events[event]?.push(cb) || (this.events[event] = [cb])
34
42
  return () => {
35
43
  this.events[event] = this.events[event]?.filter(i => cb !== i)
package/src/index.ts CHANGED
@@ -3,7 +3,7 @@ import './legacy';
3
3
  export { ColyseusSDK, Client, MatchMakeError, type JoinOptions, type EndpointSettings, type ClientOptions, type ISeatReservation as SeatReservation } from './Client.ts';
4
4
  export { Protocol, ErrorCode } from './Protocol.ts';
5
5
  export { Room, type RoomAvailable } from './Room.ts';
6
- export { Auth, type AuthSettings, type PopupSettings } from "./Auth.ts";
6
+ export { Auth, type AuthSettings, type PopupSettings, type AuthResponse, type UserDataResponse, type ForgotPasswordResponse, type AuthData } from "./Auth.ts";
7
7
  export { ServerError, CloseCode } from './errors/Errors.ts';
8
8
 
9
9
  /*
@@ -18,7 +18,7 @@ export class WebSocketTransport implements ITransport {
18
18
  }
19
19
 
20
20
  public sendUnreliable(data: Uint8Array): void {
21
- console.warn("colyseus.js: The WebSocket transport does not support unreliable messages");
21
+ console.warn("@colyseus/sdk: The WebSocket transport does not support unreliable messages");
22
22
  }
23
23
 
24
24
  /**