@colyseus/sdk 0.17.37 → 0.17.39

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 (62) 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 +2 -2
  6. package/build/Client.cjs.map +1 -1
  7. package/build/Client.d.ts +2 -1
  8. package/build/Client.mjs +2 -2
  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 +19 -6
  13. package/build/HTTP.cjs.map +1 -1
  14. package/build/HTTP.d.ts +8 -1
  15. package/build/HTTP.mjs +20 -6
  16. package/build/HTTP.mjs.map +1 -1
  17. package/build/Room.cjs +1 -1
  18. package/build/Room.mjs +1 -1
  19. package/build/Storage.cjs +1 -1
  20. package/build/Storage.mjs +1 -1
  21. package/build/core/nanoevents.cjs +1 -1
  22. package/build/core/nanoevents.mjs +1 -1
  23. package/build/core/signal.cjs +1 -1
  24. package/build/core/signal.mjs +1 -1
  25. package/build/core/utils.cjs +1 -1
  26. package/build/core/utils.mjs +1 -1
  27. package/build/debug.cjs +1 -1
  28. package/build/debug.mjs +1 -1
  29. package/build/errors/Errors.cjs +1 -1
  30. package/build/errors/Errors.mjs +1 -1
  31. package/build/fetchXHR.cjs +91 -0
  32. package/build/fetchXHR.cjs.map +1 -0
  33. package/build/fetchXHR.d.ts +6 -0
  34. package/build/fetchXHR.mjs +84 -0
  35. package/build/fetchXHR.mjs.map +1 -0
  36. package/build/index.cjs +1 -1
  37. package/build/index.cjs.map +1 -1
  38. package/build/index.d.ts +1 -0
  39. package/build/index.mjs +1 -1
  40. package/build/index.mjs.map +1 -1
  41. package/build/legacy.cjs +1 -1
  42. package/build/legacy.mjs +1 -1
  43. package/build/serializer/NoneSerializer.cjs +1 -1
  44. package/build/serializer/NoneSerializer.mjs +1 -1
  45. package/build/serializer/SchemaSerializer.cjs +1 -1
  46. package/build/serializer/SchemaSerializer.mjs +1 -1
  47. package/build/serializer/Serializer.cjs +1 -1
  48. package/build/serializer/Serializer.mjs +1 -1
  49. package/build/transport/H3Transport.cjs +1 -1
  50. package/build/transport/H3Transport.mjs +1 -1
  51. package/build/transport/WebSocketTransport.cjs +1 -1
  52. package/build/transport/WebSocketTransport.mjs +1 -1
  53. package/dist/colyseus.js +98 -7
  54. package/dist/colyseus.js.map +1 -1
  55. package/dist/debug.js +1 -1
  56. package/package.json +7 -6
  57. package/src/Client.ts +3 -2
  58. package/src/HTTP.ts +22 -5
  59. package/src/fetchXHR.ts +88 -0
  60. package/src/index.ts +1 -0
  61. package/dist/colyseus-cocos-creator.js +0 -9668
  62. package/dist/colyseus-cocos-creator.js.map +0 -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.37
6
+ // colyseus.js@0.17.39
7
7
  (function (Client_ts, sharedTypes) {
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.37",
3
+ "version": "0.17.39",
4
4
  "description": "Colyseus Multiplayer SDK for JavaScript/TypeScript",
5
5
  "author": "Endel Dreyer",
6
6
  "license": "MIT",
@@ -55,8 +55,8 @@
55
55
  "@colyseus/schema": "^4.0.7",
56
56
  "tslib": "^2.1.0",
57
57
  "ws": "^8.13.0",
58
- "@colyseus/shared-types": "^0.17.6",
59
- "@colyseus/better-call": "^1.3.1"
58
+ "@colyseus/better-call": "^1.3.1",
59
+ "@colyseus/shared-types": "^0.17.6"
60
60
  },
61
61
  "devDependencies": {
62
62
  "@rollup/plugin-alias": "^5.1.1",
@@ -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.39"
85
+ "@colyseus/core": "^0.17.40"
86
86
  },
87
87
  "peerDependencies": {
88
88
  "@colyseus/core": "0.17.x"
@@ -96,16 +96,17 @@
96
96
  }
97
97
  },
98
98
  "scripts": {
99
- "test": "vitest --dir test --reporter verbose",
99
+ "test": "vitest run --dir test --reporter verbose",
100
100
  "start": "vite --config example/vite.config.ts",
101
101
  "build": "rollup -c rollup.config.mjs",
102
102
  "build-dist-dts": "dts-bundle-generator --config dts-generator.json",
103
103
  "build-ci": "tsc && npm run build && npm run build-dist-dts && npm run build-release-assets",
104
104
  "build-release-assets": "zip --junk-paths colyseus-sdk dist/colyseus*",
105
105
  "build-all": "tsc && npm run build && npm run build-dist-dts && npm run copy-cocos-creator-files && npm run copy-construct3-files",
106
+ "copy-cocos-creator-files": "tsc test/dist-types-check.ts --noEmit --skipLibCheck && cp dist/colyseus.js cocos-creator-3-extension/colyseus-sdk/runtime/colyseus.js && cp dist/debug.js cocos-creator-3-extension/colyseus-sdk/runtime/debug.js && cp dist/colyseus.d.ts cocos-creator-3-extension/colyseus-sdk/runtime",
106
107
  "copy-construct3-files": "cp dist/colyseus.js ../../../colyseus-construct3/plugin/colyseus.js && cp dist/debug.js ../../../colyseus-construct3/plugin/colyseus-debug.js",
107
- "copy-cocos-creator-files": "tsc test/dist-types-check.ts --noEmit --skipLibCheck && cp dist/colyseus-cocos-creator.js cocos-creator-3-extension/colyseus-sdk/runtime/colyseus.js && cp dist/debug.js cocos-creator-3-extension/colyseus-sdk/runtime/debug.js && cp dist/colyseus.d.ts cocos-creator-3-extension/colyseus-sdk/runtime",
108
108
  "watch": "tsc -w",
109
+ "typecheck": "tsc --noEmit",
109
110
  "tslint": "tslint --project ."
110
111
  }
111
112
  }
package/src/Client.ts CHANGED
@@ -3,7 +3,7 @@ import { CloseCode, Protocol, type InferState, type SDKTypes, type ServerRoomLik
3
3
  import { MatchMakeError, ServerError } from './errors/Errors.ts';
4
4
  import { Room } from './Room.ts';
5
5
  import { SchemaConstructor } from './serializer/SchemaSerializer.ts';
6
- import { HTTP } from './HTTP.ts';
6
+ import { HTTP, type FetchFn } from './HTTP.ts';
7
7
  import { Auth } from './Auth.ts';
8
8
  import { Connection } from './Connection.ts';
9
9
  import { discordURLBuilder } from './3rd_party/discord.ts';
@@ -30,6 +30,7 @@ export interface ClientOptions {
30
30
  headers?: { [id: string]: string };
31
31
  urlBuilder?: (url: URL) => string;
32
32
  protocol?: "ws" | "h3";
33
+ fetchFn?: FetchFn;
33
34
  }
34
35
 
35
36
  export interface LatencyOptions {
@@ -108,7 +109,7 @@ export class ColyseusSDK<ServerType extends SDKTypes = any, UserData = any> {
108
109
 
109
110
  this.http = new HTTP(this, {
110
111
  headers: options?.headers || {},
111
- });
112
+ }, options?.fetchFn);
112
113
  this.auth = new Auth(this.http);
113
114
 
114
115
  this.urlBuilder = options?.urlBuilder;
package/src/HTTP.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import type { Router, HasRequiredKeys, Prettify, UnionToIntersection, Endpoint, HTTPMethod } from "@colyseus/better-call";
2
2
  import { ColyseusSDK } from "./Client.ts";
3
3
  import { ServerError } from "./errors/Errors.ts";
4
+ import { xhrFetch } from "./fetchXHR.ts";
4
5
 
5
6
  /**
6
7
  * TODO: we should clean up the types repetition in this file.
@@ -249,18 +250,35 @@ type InferReturnType<R, OPT, K extends keyof OPT> =
249
250
  ? any
250
251
  : Awaited<ReturnType<OPT[K] extends Endpoint ? OPT[K] : never>>;
251
252
 
253
+ export type FetchFn = (url: string | URL | Request, init?: RequestInit) => Promise<Response>;
254
+
252
255
  export class HTTP<R extends Router | Router["endpoints"]> {
253
256
  public authToken: string | undefined;
254
257
  public options: FetchRequestOptions;
255
258
 
256
259
  private sdk: ColyseusSDK;
260
+ private _fetchFn: FetchFn | undefined;
257
261
 
258
262
  // alias "del()" to "delete()"
259
263
  public del = this.delete;
260
264
 
261
- constructor(sdk: ColyseusSDK, baseOptions: FetchRequestOptions) {
265
+ constructor(sdk: ColyseusSDK, baseOptions: FetchRequestOptions, fetchFn?: FetchFn) {
262
266
  this.sdk = sdk;
263
267
  this.options = baseOptions;
268
+ this._fetchFn = fetchFn;
269
+ }
270
+
271
+ /**
272
+ * Lazily resolve the fetch implementation.
273
+ * Falls back to XMLHttpRequest when fetch is unavailable (e.g. Cocos Creator Native).
274
+ */
275
+ private get fetchFn(): FetchFn {
276
+ if (!this._fetchFn) {
277
+ this._fetchFn = (typeof(globalThis.fetch) !== 'undefined')
278
+ ? globalThis.fetch.bind(globalThis)
279
+ : xhrFetch;
280
+ }
281
+ return this._fetchFn;
264
282
  }
265
283
 
266
284
  private async request<
@@ -509,7 +527,7 @@ export class HTTP<R extends Router | Router["endpoints"]> {
509
527
 
510
528
  let raw: Response;
511
529
  try {
512
- raw = await fetch(url, mergedOptions);
530
+ raw = await this.fetchFn(url, mergedOptions);
513
531
  } catch (err: any) {
514
532
  // If it's an AbortError, re-throw as-is
515
533
  if (err.name === 'AbortError') {
@@ -525,11 +543,10 @@ export class HTTP<R extends Router | Router["endpoints"]> {
525
543
 
526
544
  let data: any;
527
545
 
528
- // TODO: improve content-type detection here!
529
- if (contentType?.indexOf("json")) {
546
+ if (contentType?.includes("json")) {
530
547
  data = await raw.json();
531
548
 
532
- } else if (contentType?.indexOf("text")) {
549
+ } else if (contentType?.includes("text")) {
533
550
  data = await raw.text();
534
551
 
535
552
  } else {
@@ -0,0 +1,88 @@
1
+ /**
2
+ * Minimal fetch-compatible wrapper around XMLHttpRequest.
3
+ * Used as an automatic fallback when globalThis.fetch is unavailable
4
+ * (e.g. Cocos Creator Native).
5
+ */
6
+ export function xhrFetch(url: string | URL | Request, init?: RequestInit): Promise<Response> {
7
+ return new Promise((resolve, reject) => {
8
+ const xhr = new XMLHttpRequest();
9
+ const method = init?.method || "GET";
10
+
11
+ xhr.open(method, url.toString());
12
+ xhr.withCredentials = (init?.credentials === "include");
13
+
14
+ // Apply request headers
15
+ if (init?.headers) {
16
+ const headers = (init.headers instanceof Headers)
17
+ ? init.headers
18
+ : new Headers(init.headers as HeadersInit);
19
+ headers.forEach((value, key) => {
20
+ xhr.setRequestHeader(key, value);
21
+ });
22
+ }
23
+
24
+ xhr.onload = () => {
25
+ // Parse response headers
26
+ const headers = new Headers();
27
+ const rawHeaders = xhr.getAllResponseHeaders().trim();
28
+ if (rawHeaders) {
29
+ for (const line of rawHeaders.split(/[\r\n]+/)) {
30
+ const idx = line.indexOf(": ");
31
+ if (idx > 0) {
32
+ headers.append(line.substring(0, idx), line.substring(idx + 2));
33
+ }
34
+ }
35
+ }
36
+
37
+ const responseBody = xhr.response ?? xhr.responseText;
38
+
39
+ resolve(new XHRResponse(responseBody, {
40
+ status: xhr.status,
41
+ statusText: xhr.statusText,
42
+ headers,
43
+ }) as unknown as Response);
44
+ };
45
+
46
+ xhr.onerror = () => reject(new TypeError("Network request failed"));
47
+ xhr.ontimeout = () => reject(new TypeError("Network request timed out"));
48
+
49
+ xhr.send(init?.body as XMLHttpRequestBodyInit | null ?? null);
50
+ });
51
+ }
52
+
53
+ /**
54
+ * Minimal Response-compatible class backed by XHR response data.
55
+ * Implements only the surface used by HTTP.executeRequest().
56
+ */
57
+ class XHRResponse {
58
+ readonly status: number;
59
+ readonly statusText: string;
60
+ readonly headers: Headers;
61
+ readonly ok: boolean;
62
+
63
+ private body: any;
64
+
65
+ constructor(body: any, init: { status: number; statusText: string; headers: Headers }) {
66
+ this.body = body;
67
+ this.status = init.status;
68
+ this.statusText = init.statusText;
69
+ this.headers = init.headers;
70
+ this.ok = init.status >= 200 && init.status < 300;
71
+ }
72
+
73
+ async json(): Promise<any> {
74
+ return typeof this.body === "string"
75
+ ? JSON.parse(this.body)
76
+ : this.body;
77
+ }
78
+
79
+ async text(): Promise<string> {
80
+ return typeof this.body === "string"
81
+ ? this.body
82
+ : JSON.stringify(this.body);
83
+ }
84
+
85
+ async blob(): Promise<Blob> {
86
+ return new Blob([this.body]);
87
+ }
88
+ }
package/src/index.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import './legacy';
2
2
 
3
3
  export { ColyseusSDK, Client, type JoinOptions, type EndpointSettings, type ClientOptions, type ISeatReservation as SeatReservation } from './Client.ts';
4
+ export { type FetchFn } from './HTTP.ts';
4
5
  export { Room, type RoomAvailable } from './Room.ts';
5
6
  export { Auth, type AuthSettings, type PopupSettings, type AuthResponse, type UserDataResponse, type ForgotPasswordResponse, type AuthData } from "./Auth.ts";
6
7
  export { ServerError, AbortError, MatchMakeError } from './errors/Errors.ts';