@colyseus/sdk 0.17.16 → 0.17.18

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/3rd_party/discord.cjs +1 -1
  2. package/build/3rd_party/discord.mjs +1 -1
  3. package/build/Auth.cjs +1 -1
  4. package/build/Auth.mjs +1 -1
  5. package/build/Client.cjs +20 -26
  6. package/build/Client.cjs.map +1 -1
  7. package/build/Client.d.ts +0 -4
  8. package/build/Client.mjs +22 -28
  9. package/build/Client.mjs.map +1 -1
  10. package/build/Connection.cjs +1 -1
  11. package/build/Connection.mjs +1 -1
  12. package/build/HTTP.cjs +20 -19
  13. package/build/HTTP.cjs.map +1 -1
  14. package/build/HTTP.d.ts +2 -18
  15. package/build/HTTP.mjs +19 -18
  16. package/build/HTTP.mjs.map +1 -1
  17. package/build/Protocol.cjs +1 -1
  18. package/build/Protocol.mjs +1 -1
  19. package/build/Room.cjs +1 -1
  20. package/build/Room.cjs.map +1 -1
  21. package/build/Room.d.ts +6 -5
  22. package/build/Room.mjs +1 -1
  23. package/build/Room.mjs.map +1 -1
  24. package/build/Storage.cjs +1 -1
  25. package/build/Storage.mjs +1 -1
  26. package/build/core/nanoevents.cjs +1 -1
  27. package/build/core/nanoevents.mjs +1 -1
  28. package/build/core/signal.cjs +1 -1
  29. package/build/core/signal.mjs +1 -1
  30. package/build/core/utils.cjs +1 -1
  31. package/build/core/utils.mjs +1 -1
  32. package/build/debug.cjs +1 -1
  33. package/build/debug.mjs +1 -1
  34. package/build/errors/Errors.cjs +17 -2
  35. package/build/errors/Errors.cjs.map +1 -1
  36. package/build/errors/Errors.d.ts +15 -2
  37. package/build/errors/Errors.mjs +22 -3
  38. package/build/errors/Errors.mjs.map +1 -1
  39. package/build/index.cjs +3 -2
  40. package/build/index.cjs.map +1 -1
  41. package/build/index.d.ts +2 -2
  42. package/build/index.mjs +3 -3
  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 +1 -1
  55. package/build/transport/WebSocketTransport.mjs +1 -1
  56. package/dist/colyseus-cocos-creator.js +61 -44
  57. package/dist/colyseus-cocos-creator.js.map +1 -1
  58. package/dist/colyseus.js +61 -44
  59. package/dist/colyseus.js.map +1 -1
  60. package/dist/debug.js +54 -44
  61. package/dist/debug.js.map +1 -1
  62. package/package.json +3 -3
  63. package/src/Client.ts +20 -28
  64. package/src/HTTP.ts +20 -40
  65. package/src/Room.ts +11 -10
  66. package/src/errors/Errors.ts +33 -11
  67. package/src/index.ts +2 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@colyseus/sdk",
3
- "version": "0.17.16",
3
+ "version": "0.17.18",
4
4
  "description": "Colyseus Multiplayer SDK for JavaScript/TypeScript",
5
5
  "author": "Endel Dreyer",
6
6
  "license": "MIT",
@@ -55,7 +55,7 @@
55
55
  "@colyseus/schema": "^4.0.4",
56
56
  "tslib": "^2.1.0",
57
57
  "ws": "^8.13.0",
58
- "@colyseus/shared-types": "^0.17.1",
58
+ "@colyseus/shared-types": "^0.17.2",
59
59
  "@colyseus/better-call": "^1.2.0"
60
60
  },
61
61
  "devDependencies": {
@@ -82,7 +82,7 @@
82
82
  "typescript": "^5.9.3",
83
83
  "vite": "^5.0.11",
84
84
  "vitest": "^2.1.1",
85
- "@colyseus/core": "^0.17.16"
85
+ "@colyseus/core": "^0.17.17"
86
86
  },
87
87
  "peerDependencies": {
88
88
  "@colyseus/core": "0.17.x"
package/src/Client.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { CloseCode, type SDKTypes, type ServerRoomLike, type ISeatReservation } from '@colyseus/shared-types';
2
2
 
3
- import { ServerError } from './errors/Errors.ts';
3
+ import { MatchMakeError, ServerError } from './errors/Errors.ts';
4
4
  import { Room } from './Room.ts';
5
5
  import { SchemaConstructor } from './serializer/SchemaSerializer.ts';
6
6
  import { HTTP } from './HTTP.ts';
@@ -12,15 +12,6 @@ import { discordURLBuilder } from './3rd_party/discord.ts';
12
12
  export type JoinOptions = any;
13
13
  export type { ISeatReservation };
14
14
 
15
- export class MatchMakeError extends Error {
16
- code: number;
17
- constructor(message: string, code: number) {
18
- super(message);
19
- this.code = code;
20
- this.name = "MatchMakeError";
21
- Object.setPrototypeOf(this, MatchMakeError.prototype);
22
- }
23
- }
24
15
 
25
16
  // - React Native does not provide `window.location`
26
17
  // - Cocos Creator (Native) does not provide `window.location.hostname`
@@ -368,28 +359,29 @@ export class ColyseusSDK<ServerType extends SDKTypes = any, UserData = any> {
368
359
  options: JoinOptions = {},
369
360
  rootSchema?: SchemaConstructor<T>,
370
361
  ) {
371
- const httpResponse = await (this.http as HTTP<any>).post(`/matchmake/${method}/${roomName}`, {
372
- headers: {
373
- 'Accept': 'application/json',
374
- 'Content-Type': 'application/json'
375
- },
376
- body: options
377
- });
362
+ try {
363
+ const httpResponse = await (this.http as HTTP<any>).post(`/matchmake/${method}/${roomName}`, {
364
+ headers: {
365
+ 'Accept': 'application/json',
366
+ 'Content-Type': 'application/json'
367
+ },
368
+ body: options
369
+ });
378
370
 
379
- // Handle HTTP error responses
380
- if (!httpResponse.ok) {
381
- // @ts-ignore
382
- throw new MatchMakeError(httpResponse.error.message || httpResponse.error, httpResponse.error.code || httpResponse.status);
383
- }
371
+ const response = httpResponse.data as unknown as ISeatReservation;
384
372
 
385
- const response = httpResponse.data as unknown as ISeatReservation;
373
+ // forward reconnection token during "reconnect" methods.
374
+ if (method === "reconnect") {
375
+ response.reconnectionToken = options.reconnectionToken;
376
+ }
386
377
 
387
- // forward reconnection token during "reconnect" methods.
388
- if (method === "reconnect") {
389
- response.reconnectionToken = options.reconnectionToken;
378
+ return await this.consumeSeatReservation<T>(response, rootSchema);
379
+ } catch (error) {
380
+ if (error instanceof ServerError) {
381
+ throw new MatchMakeError(error.message, error.code);
382
+ }
383
+ throw error;
390
384
  }
391
-
392
- return await this.consumeSeatReservation<T>(response, rootSchema);
393
385
  }
394
386
 
395
387
  protected createRoom<T>(roomName: string, rootSchema?: SchemaConstructor<T>) {
package/src/HTTP.ts CHANGED
@@ -146,35 +146,14 @@ type FetchRequestOptions<
146
146
  params?: Params;
147
147
  }>
148
148
 
149
- type ResponseData<T> = {
150
- ok: true;
149
+ type FetchResponse<T> = {
150
+ raw: Response;
151
151
  data: T;
152
- error: null,
153
- response: Response;
154
152
  headers: Headers;
155
153
  status: number;
156
154
  statusText: string;
157
155
  };
158
156
 
159
- type ResponseError<E> = {
160
- ok: false,
161
- data: null,
162
- error: Prettify<(E extends Record<string, any> ? E : {
163
- message?: string;
164
- }) & {
165
- code?: string;
166
- }>;
167
- response: Response;
168
- headers: Headers;
169
- status: number;
170
- statusText: string;
171
- };
172
-
173
- type FetchResponse<T, E extends Record<string, unknown> | unknown = unknown, Throw extends boolean = false> =
174
- Throw extends true
175
- ? T
176
- : ResponseData<T> | ResponseError<E>;
177
-
178
157
  export function isJSONSerializable(value: any) {
179
158
  if (value === undefined) {
180
159
  return false;
@@ -519,9 +498,9 @@ export class HTTP<R extends Router | Router["endpoints"]> {
519
498
 
520
499
  const url = getURLWithQueryParams(this.sdk['getHttpEndpoint'](path.toString()), mergedOptions);
521
500
 
522
- let response: Response;
501
+ let raw: Response;
523
502
  try {
524
- response = await fetch(url, mergedOptions);
503
+ raw = await fetch(url, mergedOptions);
525
504
  } catch (err: any) {
526
505
  // If it's an AbortError, re-throw as-is
527
506
  if (err.name === 'AbortError') {
@@ -529,39 +508,40 @@ export class HTTP<R extends Router | Router["endpoints"]> {
529
508
  }
530
509
  // Re-throw with network error code at top level (e.g. ECONNREFUSED)
531
510
  const networkError: ServerError = new ServerError(err.cause?.code || err.code, err.message);
511
+ networkError.response = raw;
532
512
  networkError.cause = err.cause;
533
513
  throw networkError;
534
514
  }
535
- const contentType = response.headers.get("content-type");
515
+ const contentType = raw.headers.get("content-type");
536
516
 
537
517
  let data: any;
538
- let error = null;
539
518
 
540
519
  // TODO: improve content-type detection here!
541
520
  if (contentType?.indexOf("json")) {
542
- data = await response.json();
521
+ data = await raw.json();
543
522
 
544
523
  } else if (contentType?.indexOf("text")) {
545
- data = await response.text();
524
+ data = await raw.text();
546
525
 
547
526
  } else {
548
- data = await response.blob();
527
+ data = await raw.blob();
549
528
  }
550
529
 
551
- if (!response.ok) {
552
- // TODO: throw error here?!
553
- error = data;
554
- data = null;
530
+ if (!raw.ok) {
531
+ throw new ServerError(data.code ?? raw.status, data.message ?? raw.statusText, {
532
+ headers: raw.headers,
533
+ status: raw.status,
534
+ response: raw,
535
+ data
536
+ });
555
537
  }
556
538
 
557
539
  return {
558
- ok: response.ok,
559
- headers: response.headers,
540
+ raw,
560
541
  data,
561
- error,
562
- status: response.status,
563
- statusText: response.statusText,
564
- response,
542
+ headers: raw.headers,
543
+ status: raw.status,
544
+ statusText: raw.statusText,
565
545
  };
566
546
  }
567
547
  }
package/src/Room.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { CloseCode, type InferState, type ExtractRoomMessages, type ExtractRoomClientMessages, type ExtractMessageType } from '@colyseus/shared-types';
1
+ import { CloseCode, type InferState, type NormalizeRoomType, type ExtractRoomMessages, type ExtractRoomClientMessages, type ExtractMessageType } from '@colyseus/shared-types';
2
2
  import { decode, encode, Iterator, Schema } from '@colyseus/schema';
3
3
 
4
4
  import { Packr, unpack } from '@colyseus/msgpackr';
@@ -213,12 +213,12 @@ export class Room<
213
213
  });
214
214
  }
215
215
 
216
- public onMessage<MessageType extends keyof ExtractRoomClientMessages<T>>(
216
+ public onMessage<MessageType extends keyof ExtractRoomClientMessages<NormalizeRoomType<T>>>(
217
217
  message: MessageType,
218
- callback: (payload: ExtractRoomClientMessages<T>[MessageType]) => void
219
- )
220
- public onMessage<T = any>(type: "*", callback: (messageType: string | number, payload: T) => void)
221
- public onMessage<T = any>(type: string | number, callback: (payload: T) => void)
218
+ callback: (payload: ExtractRoomClientMessages<NormalizeRoomType<T>>[MessageType]) => void
219
+ ): () => void
220
+ public onMessage<Payload = any>(type: "*", callback: (messageType: string | number, payload: Payload) => void): () => void
221
+ public onMessage<Payload = any>(type: string | number, callback: (payload: Payload) => void): () => void
222
222
  public onMessage(type: '*' | string | number, callback: (...args: any[]) => void) {
223
223
  return this.onMessageHandlers.on(this.getMessageHandlerKey(type), callback);
224
224
  }
@@ -235,11 +235,12 @@ export class Room<
235
235
  this.connection.send(this.packr.buffer.subarray(0, 1));
236
236
  }
237
237
 
238
- public send<MessageType extends keyof ExtractRoomMessages<T>>(
238
+ public send<MessageType extends keyof ExtractRoomMessages<NormalizeRoomType<T>>>(
239
239
  messageType: MessageType,
240
- payload?: ExtractMessageType<ExtractRoomMessages<T>[MessageType]>
241
- )
242
- public send<T = any>(messageType: string | number, payload?: T): void {
240
+ payload?: ExtractMessageType<ExtractRoomMessages<NormalizeRoomType<T>>[MessageType]>
241
+ ): void
242
+ public send<Payload = any>(messageType: string | number, payload?: Payload): void
243
+ public send(messageType: string | number, payload?: any): void {
243
244
  const it: Iterator = { offset: 1 };
244
245
  this.packr.buffer[0] = Protocol.ROOM_DATA;
245
246
 
@@ -1,18 +1,40 @@
1
1
 
2
- export class ServerError extends Error {
3
- public code: number;
2
+ export class ServerError<DATA=any> extends Error {
3
+ public code: number;
4
4
 
5
- constructor(code: number, message: string) {
6
- super(message);
5
+ public headers?: Headers;
6
+ public status?: number;
7
+ public response?: Response;
8
+ public data?: DATA;
7
9
 
8
- this.name = "ServerError";
9
- this.code = code;
10
- }
10
+ constructor(code: number, message: string, opts?: { headers?: Headers, status?: number, response?: Response, data?: any }) {
11
+ super(message);
12
+
13
+ this.name = "ServerError";
14
+ this.code = code;
15
+
16
+ if (opts) {
17
+ this.headers = opts.headers;
18
+ this.status = opts.status;
19
+ this.response = opts.response;
20
+ this.data = opts.data;
21
+ }
22
+ }
11
23
  }
12
24
 
13
25
  export class AbortError extends Error {
14
- constructor(message: string) {
15
- super(message);
16
- this.name = "AbortError";
17
- }
26
+ constructor(message: string) {
27
+ super(message);
28
+ this.name = "AbortError";
29
+ }
18
30
  }
31
+
32
+ export class MatchMakeError extends Error {
33
+ public code: number;
34
+ constructor(message: string, code: number) {
35
+ super(message);
36
+ this.code = code;
37
+ this.name = "MatchMakeError";
38
+ Object.setPrototypeOf(this, MatchMakeError.prototype);
39
+ }
40
+ }
package/src/index.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import './legacy';
2
2
 
3
- export { ColyseusSDK, Client, MatchMakeError, type JoinOptions, type EndpointSettings, type ClientOptions, type ISeatReservation as SeatReservation } from './Client.ts';
3
+ export { ColyseusSDK, Client, type JoinOptions, type EndpointSettings, type ClientOptions, type ISeatReservation as SeatReservation } from './Client.ts';
4
4
  export { Room, type RoomAvailable } from './Room.ts';
5
5
  export { Auth, type AuthSettings, type PopupSettings, type AuthResponse, type UserDataResponse, type ForgotPasswordResponse, type AuthData } from "./Auth.ts";
6
- export { ServerError } from './errors/Errors.ts';
6
+ export { ServerError, AbortError, MatchMakeError } from './errors/Errors.ts';
7
7
  export { CloseCode, ErrorCode, Protocol } from '@colyseus/shared-types'; // convenience re-export / backwards compatibility
8
8
  export type { InferRoomConstructor } from './core/utils.ts';
9
9