@rpgjs/client 5.0.0-alpha.10 → 5.0.0-alpha.12

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/dist/Game/Map.d.ts +4 -0
  2. package/dist/Gui/Gui.d.ts +128 -5
  3. package/dist/RpgClient.d.ts +35 -5
  4. package/dist/RpgClientEngine.d.ts +3 -0
  5. package/dist/components/gui/index.d.ts +3 -3
  6. package/dist/index.d.ts +2 -0
  7. package/dist/index.js +7 -5
  8. package/dist/index.js.map +1 -1
  9. package/dist/index10.js +150 -4
  10. package/dist/index10.js.map +1 -1
  11. package/dist/index11.js +20 -7
  12. package/dist/index11.js.map +1 -1
  13. package/dist/index12.js +6 -8
  14. package/dist/index12.js.map +1 -1
  15. package/dist/index13.js +11 -14
  16. package/dist/index13.js.map +1 -1
  17. package/dist/index14.js +8 -100
  18. package/dist/index14.js.map +1 -1
  19. package/dist/index15.js +141 -35
  20. package/dist/index15.js.map +1 -1
  21. package/dist/index16.js +42 -0
  22. package/dist/index16.js.map +1 -1
  23. package/dist/index17.js +48 -7
  24. package/dist/index17.js.map +1 -1
  25. package/dist/index18.js +5 -383
  26. package/dist/index18.js.map +1 -1
  27. package/dist/index19.js +384 -28
  28. package/dist/index19.js.map +1 -1
  29. package/dist/index2.js +32 -13
  30. package/dist/index2.js.map +1 -1
  31. package/dist/index20.js +31 -17
  32. package/dist/index20.js.map +1 -1
  33. package/dist/index21.js +17 -2413
  34. package/dist/index21.js.map +1 -1
  35. package/dist/index22.js +2587 -88
  36. package/dist/index22.js.map +1 -1
  37. package/dist/index23.js +108 -103
  38. package/dist/index23.js.map +1 -1
  39. package/dist/index29.js +22 -3
  40. package/dist/index29.js.map +1 -1
  41. package/dist/index3.js +2 -2
  42. package/dist/index30.js +1 -318
  43. package/dist/index30.js.map +1 -1
  44. package/dist/index31.js +332 -24
  45. package/dist/index31.js.map +1 -1
  46. package/dist/index32.js +24 -8
  47. package/dist/index32.js.map +1 -1
  48. package/dist/index33.js +4 -4
  49. package/dist/index33.js.map +1 -1
  50. package/dist/index34.js +8 -9
  51. package/dist/index34.js.map +1 -1
  52. package/dist/index35.js +9 -4400
  53. package/dist/index35.js.map +1 -1
  54. package/dist/index36.js +4394 -307
  55. package/dist/index36.js.map +1 -1
  56. package/dist/index37.js +307 -165
  57. package/dist/index37.js.map +1 -1
  58. package/dist/index38.js +162 -489
  59. package/dist/index38.js.map +1 -1
  60. package/dist/index39.js +496 -56
  61. package/dist/index39.js.map +1 -1
  62. package/dist/index4.js +2 -2
  63. package/dist/index40.js +67 -10
  64. package/dist/index40.js.map +1 -1
  65. package/dist/index41.js +16 -78
  66. package/dist/index41.js.map +1 -1
  67. package/dist/index42.js +96 -0
  68. package/dist/index42.js.map +1 -0
  69. package/dist/index9.js +228 -14
  70. package/dist/index9.js.map +1 -1
  71. package/dist/presets/faceset.d.ts +30 -0
  72. package/dist/presets/index.d.ts +1 -0
  73. package/dist/services/mmorpg.d.ts +1 -1
  74. package/dist/services/standalone.d.ts +1 -1
  75. package/package.json +7 -5
  76. package/src/Game/AnimationManager.ts +1 -0
  77. package/src/Game/Map.ts +9 -1
  78. package/src/Game/Object.ts +28 -6
  79. package/src/Gui/Gui.ts +300 -17
  80. package/src/RpgClient.ts +36 -5
  81. package/src/RpgClientEngine.ts +32 -9
  82. package/src/components/character.ce +11 -9
  83. package/src/components/gui/box.ce +17 -0
  84. package/src/components/gui/dialogbox/index.ce +74 -35
  85. package/src/components/gui/dialogbox/selection.ce +16 -1
  86. package/src/components/gui/index.ts +3 -4
  87. package/src/components/scenes/event-layer.ce +6 -0
  88. package/src/index.ts +3 -1
  89. package/src/presets/faceset.ts +60 -0
  90. package/src/presets/index.ts +3 -1
package/dist/index22.js CHANGED
@@ -1,114 +1,2613 @@
1
- import { __name } from './index29.js';
2
- import { load } from './index18.js';
3
- import { PartySocket } from './index37.js';
4
- import './index38.js';
1
+ import { dset } from './index35.js';
2
+ import z from './index36.js';
3
+ import { createStatesSnapshot, syncClass, generateShortUUID as generateShortUUID$1, load, id, sync, persist, getByPath, DELETE_TOKEN } from './index19.js';
4
+ import { signal } from './index37.js';
5
5
 
6
- function createConnection(options, roomInstance) {
7
- const conn = new PartySocket(options);
8
- conn.addEventListener("message", (event) => {
9
- const object = JSON.parse(event.data);
10
- switch (object.type) {
11
- case "sync":
12
- load(roomInstance, object.value, true);
13
- break;
6
+ var __defProp = Object.defineProperty;
7
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
8
+
9
+ // src/decorators.ts
10
+ function Action(name, bodyValidation) {
11
+ return function(target, propertyKey) {
12
+ if (!target.constructor._actionMetadata) {
13
+ target.constructor._actionMetadata = /* @__PURE__ */ new Map();
14
14
  }
15
- });
16
- return {
17
- emit: /* @__PURE__ */ __name((key, value) => {
18
- conn.send(JSON.stringify({
19
- action: key,
20
- value
21
- }));
22
- }, "emit"),
23
- on: /* @__PURE__ */ __name((key, cb) => {
24
- conn.addEventListener("message", (event) => {
25
- const object = JSON.parse(event.data);
26
- if (object.type === key) {
27
- cb(object.value);
28
- }
29
- });
30
- }, "on"),
31
- off: /* @__PURE__ */ __name((key, cb) => {
32
- conn.removeEventListener("message", (event) => {
33
- const object = JSON.parse(event.data);
34
- if (object.type === key) {
35
- cb(object.value);
15
+ target.constructor._actionMetadata.set(name, {
16
+ key: propertyKey,
17
+ bodyValidation
18
+ });
19
+ };
20
+ }
21
+ __name(Action, "Action");
22
+ function Request2(options, bodyValidation) {
23
+ return function(target, propertyKey) {
24
+ if (!target.constructor._requestMetadata) {
25
+ target.constructor._requestMetadata = /* @__PURE__ */ new Map();
26
+ }
27
+ const path = options.path.startsWith("/") ? options.path : `/${options.path}`;
28
+ const method = options.method || "GET";
29
+ const routeKey = `${method}:${path}`;
30
+ target.constructor._requestMetadata.set(routeKey, {
31
+ key: propertyKey,
32
+ path,
33
+ method,
34
+ bodyValidation
35
+ });
36
+ };
37
+ }
38
+ __name(Request2, "Request");
39
+ function Room(options) {
40
+ return function(target) {
41
+ target.path = options.path;
42
+ target.prototype.maxUsers = options.maxUsers;
43
+ target.prototype.throttleStorage = options.throttleStorage;
44
+ target.prototype.throttleSync = options.throttleSync;
45
+ target.prototype.sessionExpiryTime = options.sessionExpiryTime ?? 5 * 60 * 1e3;
46
+ if (options.guards) {
47
+ target["_roomGuards"] = options.guards;
48
+ }
49
+ };
50
+ }
51
+ __name(Room, "Room");
52
+ function RoomGuard(guards) {
53
+ return function(target) {
54
+ target["_roomGuards"] = guards;
55
+ };
56
+ }
57
+ __name(RoomGuard, "RoomGuard");
58
+ function Guard(guards) {
59
+ return function(target, propertyKey, descriptor) {
60
+ if (!target.constructor["_actionGuards"]) {
61
+ target.constructor["_actionGuards"] = /* @__PURE__ */ new Map();
62
+ }
63
+ if (!Array.isArray(guards)) {
64
+ guards = [
65
+ guards
66
+ ];
67
+ }
68
+ target.constructor["_actionGuards"].set(propertyKey, guards);
69
+ };
70
+ }
71
+ __name(Guard, "Guard");
72
+
73
+ // ../sync/src/utils.ts
74
+ function generateShortUUID() {
75
+ const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
76
+ let uuid = "";
77
+ for (let i = 0; i < 8; i++) {
78
+ const randomIndex = Math.floor(Math.random() * chars.length);
79
+ uuid += chars[randomIndex];
80
+ }
81
+ return uuid;
82
+ }
83
+ __name(generateShortUUID, "generateShortUUID");
84
+
85
+ // src/storage.ts
86
+ var Storage = class {
87
+ static {
88
+ __name(this, "Storage");
89
+ }
90
+ memory = /* @__PURE__ */ new Map();
91
+ async put(key, value) {
92
+ this.memory.set(key, value);
93
+ }
94
+ async get(key) {
95
+ return this.memory.get(key);
96
+ }
97
+ async delete(key) {
98
+ this.memory.delete(key);
99
+ }
100
+ async list() {
101
+ return this.memory;
102
+ }
103
+ };
104
+ function isPromise(value) {
105
+ return value instanceof Promise;
106
+ }
107
+ __name(isPromise, "isPromise");
108
+ async function awaitReturn(val) {
109
+ return isPromise(val) ? await val : val;
110
+ }
111
+ __name(awaitReturn, "awaitReturn");
112
+ function isClass(obj) {
113
+ return typeof obj === "function" && obj.prototype && obj.prototype.constructor === obj;
114
+ }
115
+ __name(isClass, "isClass");
116
+ function throttle(func, wait) {
117
+ let timeout = null;
118
+ let lastArgs = null;
119
+ return function(...args) {
120
+ if (!timeout) {
121
+ func(...args);
122
+ timeout = setTimeout(() => {
123
+ if (lastArgs) {
124
+ func(...lastArgs);
125
+ lastArgs = null;
36
126
  }
37
- });
38
- }, "off"),
39
- close: /* @__PURE__ */ __name(() => conn.close(), "close"),
40
- conn
127
+ timeout = null;
128
+ }, wait);
129
+ } else {
130
+ lastArgs = args;
131
+ }
41
132
  };
42
133
  }
43
- __name(createConnection, "createConnection");
44
- async function connectionRoom(options, roomInstance) {
45
- return createConnection(options, roomInstance);
46
- }
47
- __name(connectionRoom, "connectionRoom");
48
- async function connectionWorld(options, roomInstance) {
49
- const shardInfo = await getOptimalShard(options);
50
- const result = createConnection({
51
- ...options,
52
- party: "shard",
53
- room: shardInfo.url
54
- }, roomInstance);
55
- return {
56
- ...result,
57
- shardInfo
134
+ __name(throttle, "throttle");
135
+ function extractParams(pattern, str) {
136
+ const regexPattern = pattern.replace(/{(\w+)}/g, "(?<$1>[\\w-]+)");
137
+ const regex = new RegExp(`^${regexPattern}$`);
138
+ const match = regex.exec(str);
139
+ if (match && match.groups) {
140
+ return match.groups;
141
+ } else if (pattern === str) {
142
+ return {};
143
+ } else {
144
+ return null;
145
+ }
146
+ }
147
+ __name(extractParams, "extractParams");
148
+ function dremove(obj, keys) {
149
+ if (typeof keys === "string") {
150
+ keys = keys.split(".");
151
+ }
152
+ let i = 0;
153
+ const l = keys.length;
154
+ let t = obj;
155
+ let k;
156
+ while (i < l - 1) {
157
+ k = keys[i++];
158
+ if (k === "__proto__" || k === "constructor" || k === "prototype") return;
159
+ if (typeof t[k] !== "object" || t[k] === null) return;
160
+ t = t[k];
161
+ }
162
+ k = keys[i];
163
+ if (t && typeof t === "object" && !(k === "__proto__" || k === "constructor" || k === "prototype")) {
164
+ delete t[k];
165
+ }
166
+ }
167
+ __name(dremove, "dremove");
168
+ function buildObject(valuesMap, allMemory) {
169
+ let memoryObj = {};
170
+ for (let path of valuesMap.keys()) {
171
+ const value = valuesMap.get(path);
172
+ dset(memoryObj, path, value);
173
+ if (value === "$delete") {
174
+ dremove(allMemory, path);
175
+ } else {
176
+ dset(allMemory, path, value);
177
+ }
178
+ }
179
+ return memoryObj;
180
+ }
181
+ __name(buildObject, "buildObject");
182
+ function response(status, body) {
183
+ return new Response(JSON.stringify(body), {
184
+ status,
185
+ headers: {
186
+ "Content-Type": "application/json"
187
+ }
188
+ });
189
+ }
190
+ __name(response, "response");
191
+
192
+ // src/request/response.ts
193
+ var ServerResponse = class {
194
+ static {
195
+ __name(this, "ServerResponse");
196
+ }
197
+ interceptors;
198
+ statusCode = 200;
199
+ responseBody = {};
200
+ responseHeaders = {
201
+ "Content-Type": "application/json"
58
202
  };
203
+ /**
204
+ * Creates a new ServerResponse instance
205
+ * @param interceptors Array of interceptor functions that can modify the response
206
+ */
207
+ constructor(interceptors = []) {
208
+ this.interceptors = interceptors;
209
+ }
210
+ /**
211
+ * Sets the status code for the response
212
+ * @param code HTTP status code
213
+ * @returns this instance for chaining
214
+ */
215
+ status(code) {
216
+ this.statusCode = code;
217
+ return this;
218
+ }
219
+ /**
220
+ * Sets the response body and returns this instance (chainable method)
221
+ *
222
+ * @param body Response body
223
+ * @returns this instance for chaining
224
+ */
225
+ body(body) {
226
+ this.responseBody = body;
227
+ return this;
228
+ }
229
+ /**
230
+ * Adds a header to the response
231
+ * @param name Header name
232
+ * @param value Header value
233
+ * @returns this instance for chaining
234
+ */
235
+ header(name, value) {
236
+ this.responseHeaders[name] = value;
237
+ return this;
238
+ }
239
+ /**
240
+ * Adds multiple headers to the response
241
+ * @param headers Object containing headers
242
+ * @returns this instance for chaining
243
+ */
244
+ setHeaders(headers) {
245
+ this.responseHeaders = {
246
+ ...this.responseHeaders,
247
+ ...headers
248
+ };
249
+ return this;
250
+ }
251
+ /**
252
+ * Add an interceptor to the chain
253
+ * @param interceptor Function that takes a Response and returns a modified Response
254
+ * @returns this instance for chaining
255
+ */
256
+ use(interceptor) {
257
+ this.interceptors.push(interceptor);
258
+ return this;
259
+ }
260
+ /**
261
+ * Builds and returns the Response object after applying all interceptors
262
+ * @returns Promise<Response> The final Response object
263
+ * @private Internal method used by terminal methods
264
+ */
265
+ async buildResponse() {
266
+ let response2 = new Response(JSON.stringify(this.responseBody), {
267
+ status: this.statusCode,
268
+ headers: this.responseHeaders
269
+ });
270
+ for (const interceptor of this.interceptors) {
271
+ try {
272
+ const interceptedResponse = interceptor(response2);
273
+ if (interceptedResponse instanceof Promise) {
274
+ response2 = await interceptedResponse;
275
+ } else {
276
+ response2 = interceptedResponse;
277
+ }
278
+ } catch (error) {
279
+ console.error("Error in interceptor:", error);
280
+ }
281
+ }
282
+ return response2;
283
+ }
284
+ /**
285
+ * Sets the response body to the JSON-stringified version of the provided value
286
+ * and sends the response (terminal method)
287
+ *
288
+ * @param body Response body to be JSON stringified
289
+ * @returns Promise<Response> The final Response object
290
+ */
291
+ async json(body) {
292
+ this.responseBody = body;
293
+ this.responseHeaders["Content-Type"] = "application/json";
294
+ return this.buildResponse();
295
+ }
296
+ /**
297
+ * Sends the response with the current configuration (terminal method)
298
+ *
299
+ * @param body Optional body to set before sending
300
+ * @returns Promise<Response> The final Response object
301
+ */
302
+ async send(body) {
303
+ if (body !== void 0) {
304
+ this.responseBody = body;
305
+ }
306
+ return this.buildResponse();
307
+ }
308
+ /**
309
+ * Sends a plain text response (terminal method)
310
+ *
311
+ * @param text Text to send
312
+ * @returns Promise<Response> The final Response object
313
+ */
314
+ async text(text) {
315
+ this.responseBody = text;
316
+ this.responseHeaders["Content-Type"] = "text/plain";
317
+ let response2 = new Response(text, {
318
+ status: this.statusCode,
319
+ headers: this.responseHeaders
320
+ });
321
+ for (const interceptor of this.interceptors) {
322
+ try {
323
+ const interceptedResponse = interceptor(response2);
324
+ if (interceptedResponse instanceof Promise) {
325
+ response2 = await interceptedResponse;
326
+ } else {
327
+ response2 = interceptedResponse;
328
+ }
329
+ } catch (error) {
330
+ console.error("Error in interceptor:", error);
331
+ }
332
+ }
333
+ return response2;
334
+ }
335
+ /**
336
+ * Redirects to the specified URL (terminal method)
337
+ *
338
+ * @param url URL to redirect to
339
+ * @param statusCode HTTP status code (default: 302)
340
+ * @returns Promise<Response> The final Response object
341
+ */
342
+ async redirect(url, statusCode = 302) {
343
+ this.statusCode = statusCode;
344
+ this.responseHeaders["Location"] = url;
345
+ return this.buildResponse();
346
+ }
347
+ /**
348
+ * Creates a success response with status 200
349
+ * @param body Response body
350
+ * @returns Promise<Response> The final Response object
351
+ */
352
+ async success(body = {}) {
353
+ return this.status(200).json(body);
354
+ }
355
+ /**
356
+ * Creates an error response with status 400
357
+ * @param message Error message
358
+ * @param details Additional error details
359
+ * @returns Promise<Response> The final Response object
360
+ */
361
+ async badRequest(message, details = {}) {
362
+ return this.status(400).json({
363
+ error: message,
364
+ ...details
365
+ });
366
+ }
367
+ /**
368
+ * Creates an error response with status 403
369
+ * @param message Error message
370
+ * @returns Promise<Response> The final Response object
371
+ */
372
+ async notPermitted(message = "Not permitted") {
373
+ return this.status(403).json({
374
+ error: message
375
+ });
376
+ }
377
+ /**
378
+ * Creates an error response with status 401
379
+ * @param message Error message
380
+ * @returns Promise<Response> The final Response object
381
+ */
382
+ async unauthorized(message = "Unauthorized") {
383
+ return this.status(401).json({
384
+ error: message
385
+ });
386
+ }
387
+ /**
388
+ * Creates an error response with status 404
389
+ * @param message Error message
390
+ * @returns Promise<Response> The final Response object
391
+ */
392
+ async notFound(message = "Not found") {
393
+ return this.status(404).json({
394
+ error: message
395
+ });
396
+ }
397
+ /**
398
+ * Creates an error response with status 500
399
+ * @param message Error message
400
+ * @returns Promise<Response> The final Response object
401
+ */
402
+ async serverError(message = "Internal Server Error") {
403
+ return this.status(500).json({
404
+ error: message
405
+ });
406
+ }
407
+ };
408
+
409
+ // src/request/cors.ts
410
+ function cors(res, options = {}) {
411
+ const newHeaders = new Headers(res.headers);
412
+ const requestOrigin = options.origin || "*";
413
+ newHeaders.set("Access-Control-Allow-Origin", requestOrigin);
414
+ if (options.credentials) {
415
+ newHeaders.set("Access-Control-Allow-Credentials", "true");
416
+ }
417
+ if (options.exposedHeaders && options.exposedHeaders.length) {
418
+ newHeaders.set("Access-Control-Expose-Headers", options.exposedHeaders.join(", "));
419
+ }
420
+ if (options.methods && options.methods.length) {
421
+ newHeaders.set("Access-Control-Allow-Methods", options.methods.join(", "));
422
+ } else {
423
+ newHeaders.set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, PATCH, OPTIONS");
424
+ }
425
+ if (options.allowedHeaders && options.allowedHeaders.length) {
426
+ newHeaders.set("Access-Control-Allow-Headers", options.allowedHeaders.join(", "));
427
+ } else {
428
+ newHeaders.set("Access-Control-Allow-Headers", "Content-Type, Authorization, X-Requested-With");
429
+ }
430
+ if (options.maxAge) {
431
+ newHeaders.set("Access-Control-Max-Age", options.maxAge.toString());
432
+ } else {
433
+ newHeaders.set("Access-Control-Max-Age", "86400");
434
+ }
435
+ return new Response(res.body, {
436
+ status: res.status,
437
+ headers: newHeaders
438
+ });
439
+ }
440
+ __name(cors, "cors");
441
+ function createCorsInterceptor(options = {}) {
442
+ return (res) => cors(res, options);
59
443
  }
60
- __name(connectionWorld, "connectionWorld");
61
- async function getOptimalShard(worldOptions) {
62
- const {
63
- host,
64
- room,
65
- worldId = "world-default",
66
- retryCount = 3,
67
- retryDelay = 1e3,
68
- autoCreate = true
69
- // Default to true for auto-creation
70
- } = worldOptions;
71
- let attempts = 0;
72
- const url = new URL(`${host}/parties/world/${encodeURIComponent(worldId)}/connect`);
73
- const requestUrl = url.toString();
74
- while (attempts < retryCount) {
444
+ __name(createCorsInterceptor, "createCorsInterceptor");
445
+
446
+ // src/server.ts
447
+ var Message = z.object({
448
+ action: z.string(),
449
+ value: z.any()
450
+ });
451
+ var Server = class {
452
+ static {
453
+ __name(this, "Server");
454
+ }
455
+ room;
456
+ subRoom;
457
+ rooms;
458
+ /**
459
+ * @constructor
460
+ * @param {Party.Room} room - The room object representing the current game or application instance.
461
+ *
462
+ * @example
463
+ * ```typescript
464
+ * const server = new MyServer(new ServerIo("game"));
465
+ * ```
466
+ */
467
+ constructor(room) {
468
+ this.room = room;
469
+ this.subRoom = null;
470
+ this.rooms = [];
471
+ }
472
+ /**
473
+ * @readonly
474
+ * @property {boolean} isHibernate - Indicates whether the server is in hibernate mode.
475
+ *
476
+ * @example
477
+ * ```typescript
478
+ * if (!server.isHibernate) {
479
+ * console.log("Server is active");
480
+ * }
481
+ * ```
482
+ */
483
+ get isHibernate() {
484
+ return !!this["options"]?.hibernate;
485
+ }
486
+ get roomStorage() {
487
+ return this.room.storage;
488
+ }
489
+ async send(conn, obj, subRoom) {
490
+ obj = structuredClone(obj);
491
+ if (subRoom.interceptorPacket) {
492
+ const signal2 = this.getUsersProperty(subRoom);
493
+ const { publicId } = conn.state;
494
+ const user = signal2?.()[publicId];
495
+ obj = await awaitReturn(subRoom["interceptorPacket"]?.(user, obj, conn));
496
+ if (obj === null) return;
497
+ }
498
+ conn.send(JSON.stringify(obj));
499
+ }
500
+ broadcast(obj, subRoom) {
501
+ for (let conn of this.room.getConnections()) {
502
+ this.send(conn, obj, subRoom);
503
+ }
504
+ }
505
+ /**
506
+ * @method onStart
507
+ * @async
508
+ * @description Initializes the server and creates the initial room if not in hibernate mode.
509
+ * @returns {Promise<void>}
510
+ *
511
+ * @example
512
+ * ```typescript
513
+ * async function initServer() {
514
+ * await server.onStart();
515
+ * console.log("Server started");
516
+ * }
517
+ * ```
518
+ */
519
+ async onStart() {
520
+ if (!this.isHibernate) {
521
+ this.subRoom = await this.createRoom();
522
+ }
523
+ }
524
+ async garbageCollector(options) {
525
+ const subRoom = await this.getSubRoom();
526
+ if (!subRoom) return;
527
+ const activeConnections = [
528
+ ...this.room.getConnections()
529
+ ];
530
+ const activePrivateIds = new Set(activeConnections.map((conn) => conn.id));
75
531
  try {
76
- const response = await fetch(requestUrl, {
532
+ const sessions = await this.room.storage.list();
533
+ const users = this.getUsersProperty(subRoom);
534
+ const usersPropName = this.getUsersPropName(subRoom);
535
+ const validPublicIds = /* @__PURE__ */ new Set();
536
+ const expiredPublicIds = /* @__PURE__ */ new Set();
537
+ const SESSION_EXPIRY_TIME = options.sessionExpiryTime;
538
+ const now = Date.now();
539
+ for (const [key, session] of sessions) {
540
+ if (!key.startsWith("session:")) continue;
541
+ const privateId = key.replace("session:", "");
542
+ const typedSession = session;
543
+ if (!activePrivateIds.has(privateId) && !typedSession.connected && now - typedSession.created > SESSION_EXPIRY_TIME) {
544
+ await this.deleteSession(privateId);
545
+ expiredPublicIds.add(typedSession.publicId);
546
+ } else if (typedSession && typedSession.publicId) {
547
+ validPublicIds.add(typedSession.publicId);
548
+ }
549
+ }
550
+ if (users && usersPropName) {
551
+ const currentUsers = users();
552
+ for (const publicId in currentUsers) {
553
+ if (expiredPublicIds.has(publicId) && !validPublicIds.has(publicId)) {
554
+ delete currentUsers[publicId];
555
+ await this.room.storage.delete(`${usersPropName}.${publicId}`);
556
+ }
557
+ }
558
+ }
559
+ } catch (error) {
560
+ console.error("Error in garbage collector:", error);
561
+ }
562
+ }
563
+ /**
564
+ * @method createRoom
565
+ * @private
566
+ * @async
567
+ * @param {CreateRoomOptions} [options={}] - Options for creating the room.
568
+ * @returns {Promise<Object>} The created room instance.
569
+ *
570
+ * @example
571
+ * ```typescript
572
+ * // This method is private and called internally
573
+ * async function internalCreateRoom() {
574
+ * const room = await this.createRoom({ getMemoryAll: true });
575
+ * console.log("Room created:", room);
576
+ * }
577
+ * ```
578
+ */
579
+ async createRoom(options = {}) {
580
+ let instance;
581
+ let init = true;
582
+ let initPersist = true;
583
+ for (let room of this.rooms) {
584
+ const params = extractParams(room.path, this.room.id);
585
+ if (params) {
586
+ instance = new room(this.room, params);
587
+ break;
588
+ }
589
+ }
590
+ if (!instance) {
591
+ return null;
592
+ }
593
+ const loadMemory = /* @__PURE__ */ __name(async () => {
594
+ const root = await this.room.storage.get(".");
595
+ const memory = await this.room.storage.list();
596
+ const tmpObject = root || {};
597
+ for (let [key, value] of memory) {
598
+ if (key.startsWith("session:")) {
599
+ continue;
600
+ }
601
+ if (key == ".") {
602
+ continue;
603
+ }
604
+ dset(tmpObject, key, value);
605
+ }
606
+ load(instance, tmpObject, true);
607
+ }, "loadMemory");
608
+ instance.$memoryAll = {};
609
+ instance.$send = (conn, obj) => {
610
+ return this.send(conn, obj, instance);
611
+ };
612
+ instance.$broadcast = (obj) => {
613
+ return this.broadcast(obj, instance);
614
+ };
615
+ instance.$sessionTransfer = async (userOrPublicId, targetRoomId) => {
616
+ let user;
617
+ let publicId = null;
618
+ const signal2 = this.getUsersProperty(instance);
619
+ if (!signal2) {
620
+ console.error("[sessionTransfer] `users` property not defined in the room.");
621
+ return null;
622
+ }
623
+ if (typeof userOrPublicId === "string") {
624
+ publicId = userOrPublicId;
625
+ user = signal2()[publicId];
626
+ if (!user) {
627
+ console.error(`[sessionTransfer] User with publicId ${publicId} not found.`);
628
+ return null;
629
+ }
630
+ } else {
631
+ user = userOrPublicId;
632
+ const users = signal2();
633
+ for (const [id2, u] of Object.entries(users)) {
634
+ if (u === user) {
635
+ publicId = id2;
636
+ break;
637
+ }
638
+ }
639
+ if (!publicId && user && typeof user === "object") {
640
+ for (const [id2, u] of Object.entries(users)) {
641
+ if (u && typeof u === "object") {
642
+ if (u.constructor === user.constructor) {
643
+ publicId = id2;
644
+ break;
645
+ }
646
+ }
647
+ }
648
+ }
649
+ if (!publicId) {
650
+ console.error("[sessionTransfer] User not found in users collection.", {
651
+ userType: user?.constructor?.name,
652
+ userKeys: user ? Object.keys(user) : "null",
653
+ usersCount: Object.keys(users).length,
654
+ userIds: Object.keys(users)
655
+ });
656
+ return null;
657
+ }
658
+ }
659
+ const sessions = await this.room.storage.list();
660
+ let userSession = null;
661
+ let privateId = null;
662
+ for (const [key, session] of sessions) {
663
+ if (key.startsWith("session:") && session.publicId === publicId) {
664
+ userSession = session;
665
+ privateId = key.replace("session:", "");
666
+ break;
667
+ }
668
+ }
669
+ if (!userSession || !privateId) {
670
+ console.error(`[sessionTransfer] Session for publicId ${publicId} not found.`);
671
+ return null;
672
+ }
673
+ const usersPropName = this.getUsersPropName(instance);
674
+ if (!usersPropName) {
675
+ console.error("[sessionTransfer] `users` property not defined in the room.");
676
+ return null;
677
+ }
678
+ const userSnapshot = createStatesSnapshot(user);
679
+ const transferData = {
680
+ privateId,
681
+ userSnapshot,
682
+ sessionState: userSession.state,
683
+ publicId
684
+ };
685
+ try {
686
+ const targetRoomParty = this.room.context.parties.main.get(targetRoomId);
687
+ const response2 = await targetRoomParty.fetch("/session-transfer", {
688
+ method: "POST",
689
+ body: JSON.stringify(transferData),
690
+ headers: {
691
+ "Content-Type": "application/json"
692
+ }
693
+ });
694
+ if (!response2.ok) {
695
+ throw new Error(`Transfer request failed: ${await response2.text()}`);
696
+ }
697
+ const { transferToken } = await response2.json();
698
+ await this.deleteSession(privateId);
699
+ await this.room.storage.delete(`${usersPropName}.${publicId}`);
700
+ const currentUsers = signal2();
701
+ if (currentUsers[publicId]) {
702
+ delete currentUsers[publicId];
703
+ }
704
+ return transferToken;
705
+ } catch (error) {
706
+ console.error(`[sessionTransfer] Failed to transfer session to room ${targetRoomId}:`, error);
707
+ return null;
708
+ }
709
+ };
710
+ const syncCb = /* @__PURE__ */ __name((values) => {
711
+ if (options.getMemoryAll) {
712
+ buildObject(values, instance.$memoryAll);
713
+ }
714
+ if (init && this.isHibernate) {
715
+ init = false;
716
+ return;
717
+ }
718
+ const packet = buildObject(values, instance.$memoryAll);
719
+ this.broadcast({
720
+ type: "sync",
721
+ value: packet
722
+ }, instance);
723
+ values.clear();
724
+ }, "syncCb");
725
+ const persistCb = /* @__PURE__ */ __name(async (values) => {
726
+ if (initPersist) {
727
+ values.clear();
728
+ return;
729
+ }
730
+ for (let [path, value] of values) {
731
+ const _instance = path == "." ? instance : getByPath(instance, path);
732
+ const itemValue = createStatesSnapshot(_instance);
733
+ if (value == DELETE_TOKEN) {
734
+ await this.room.storage.delete(path);
735
+ } else {
736
+ await this.room.storage.put(path, itemValue);
737
+ }
738
+ }
739
+ values.clear();
740
+ }, "persistCb");
741
+ syncClass(instance, {
742
+ onSync: throttle(syncCb, instance["throttleSync"] ?? 500),
743
+ onPersist: throttle(persistCb, instance["throttleStorage"] ?? 2e3)
744
+ });
745
+ await loadMemory();
746
+ initPersist = false;
747
+ return instance;
748
+ }
749
+ /**
750
+ * @method getSubRoom
751
+ * @private
752
+ * @async
753
+ * @param {Object} [options={}] - Options for getting the sub-room.
754
+ * @returns {Promise<Object>} The sub-room instance.
755
+ *
756
+ * @example
757
+ * ```typescript
758
+ * // This method is private and called internally
759
+ * async function internalGetSubRoom() {
760
+ * const subRoom = await this.getSubRoom();
761
+ * console.log("Sub-room retrieved:", subRoom);
762
+ * }
763
+ * ```
764
+ */
765
+ async getSubRoom(options = {}) {
766
+ let subRoom;
767
+ if (this.isHibernate) {
768
+ subRoom = await this.createRoom(options);
769
+ } else {
770
+ subRoom = this.subRoom;
771
+ }
772
+ return subRoom;
773
+ }
774
+ /**
775
+ * @method getUsersProperty
776
+ * @private
777
+ * @param {Object} subRoom - The sub-room instance.
778
+ * @returns {Object|null} The users property of the sub-room, or null if not found.
779
+ *
780
+ * @example
781
+ * ```typescript
782
+ * // This method is private and called internally
783
+ * function internalGetUsers(subRoom) {
784
+ * const users = this.getUsersProperty(subRoom);
785
+ * console.log("Users:", users);
786
+ * }
787
+ * ```
788
+ */
789
+ getUsersProperty(subRoom) {
790
+ const meta = subRoom.constructor["_propertyMetadata"];
791
+ const propId = meta?.get("users");
792
+ if (propId) {
793
+ return subRoom[propId];
794
+ }
795
+ return null;
796
+ }
797
+ getUsersPropName(subRoom) {
798
+ if (!subRoom) return null;
799
+ const metadata = subRoom.constructor._propertyMetadata;
800
+ if (!metadata) return null;
801
+ return metadata.get("users");
802
+ }
803
+ /**
804
+ * Retrieves the connection status property from a user object.
805
+ *
806
+ * @param {any} user - The user object to get the connection property from.
807
+ * @returns {Function|null} - The connection property signal function or null if not found.
808
+ * @private
809
+ */
810
+ getUserConnectionProperty(user) {
811
+ if (!user) return null;
812
+ const metadata = user.constructor._propertyMetadata;
813
+ if (!metadata) return null;
814
+ const connectedPropName = metadata.get("connected");
815
+ if (!connectedPropName) return null;
816
+ return user[connectedPropName];
817
+ }
818
+ /**
819
+ * Updates a user's connection status in the signal.
820
+ *
821
+ * @param {any} user - The user object to update.
822
+ * @param {boolean} isConnected - The new connection status.
823
+ * @returns {boolean} - Whether the update was successful.
824
+ * @private
825
+ */
826
+ updateUserConnectionStatus(user, isConnected) {
827
+ const connectionSignal = this.getUserConnectionProperty(user);
828
+ if (connectionSignal) {
829
+ connectionSignal.set(isConnected);
830
+ return true;
831
+ }
832
+ return false;
833
+ }
834
+ /**
835
+ * @method getSession
836
+ * @private
837
+ * @param {string} privateId - The private ID of the session.
838
+ * @returns {Promise<Object|null>} The session object, or null if not found.
839
+ *
840
+ * @example
841
+ * ```typescript
842
+ * const session = await server.getSession("privateId");
843
+ * console.log(session);
844
+ * ```
845
+ */
846
+ async getSession(privateId) {
847
+ if (!privateId) return null;
848
+ try {
849
+ const session = await this.room.storage.get(`session:${privateId}`);
850
+ return session;
851
+ } catch (e) {
852
+ return null;
853
+ }
854
+ }
855
+ async saveSession(privateId, data) {
856
+ const sessionData = {
857
+ ...data,
858
+ created: data.created || Date.now(),
859
+ connected: data.connected !== void 0 ? data.connected : true
860
+ };
861
+ await this.room.storage.put(`session:${privateId}`, sessionData);
862
+ }
863
+ async updateSessionConnection(privateId, connected) {
864
+ const session = await this.getSession(privateId);
865
+ if (session) {
866
+ await this.saveSession(privateId, {
867
+ ...session,
868
+ connected
869
+ });
870
+ }
871
+ }
872
+ /**
873
+ * @method deleteSession
874
+ * @private
875
+ * @param {string} privateId - The private ID of the session to delete.
876
+ * @returns {Promise<void>}
877
+ *
878
+ * @example
879
+ * ```typescript
880
+ * await server.deleteSession("privateId");
881
+ * ```
882
+ */
883
+ async deleteSession(privateId) {
884
+ await this.room.storage.delete(`session:${privateId}`);
885
+ }
886
+ async onConnectClient(conn, ctx) {
887
+ const subRoom = await this.getSubRoom({
888
+ getMemoryAll: true
889
+ });
890
+ if (!subRoom) {
891
+ conn.close();
892
+ return;
893
+ }
894
+ const sessionExpiryTime = subRoom.constructor.sessionExpiryTime;
895
+ await this.garbageCollector({
896
+ sessionExpiryTime
897
+ });
898
+ const roomGuards = subRoom.constructor["_roomGuards"] || [];
899
+ for (const guard of roomGuards) {
900
+ const isAuthorized = await guard(conn, ctx, this.room);
901
+ if (!isAuthorized) {
902
+ conn.close();
903
+ return;
904
+ }
905
+ }
906
+ let transferToken = null;
907
+ if (ctx.request?.url) {
908
+ const url = new URL(ctx.request.url);
909
+ transferToken = url.searchParams.get("transferToken");
910
+ }
911
+ let transferData = null;
912
+ if (transferToken) {
913
+ transferData = await this.room.storage.get(`transfer:${transferToken}`);
914
+ if (transferData) {
915
+ await this.room.storage.delete(`transfer:${transferToken}`);
916
+ }
917
+ }
918
+ const existingSession = await this.getSession(conn.id);
919
+ const publicId = existingSession?.publicId || transferData?.publicId || generateShortUUID$1();
920
+ let user = null;
921
+ const signal2 = this.getUsersProperty(subRoom);
922
+ const usersPropName = this.getUsersPropName(subRoom);
923
+ if (signal2) {
924
+ const { classType } = signal2.options;
925
+ if (!existingSession?.publicId) {
926
+ if (transferData?.restored && signal2()[publicId]) {
927
+ user = signal2()[publicId];
928
+ } else {
929
+ user = isClass(classType) ? new classType() : classType(conn, ctx);
930
+ signal2()[publicId] = user;
931
+ const snapshot = createStatesSnapshot(user);
932
+ this.room.storage.put(`${usersPropName}.${publicId}`, snapshot);
933
+ }
934
+ } else {
935
+ user = signal2()[existingSession.publicId];
936
+ }
937
+ if (!existingSession) {
938
+ const sessionPrivateId = transferData?.privateId || conn.id;
939
+ await this.saveSession(sessionPrivateId, {
940
+ publicId
941
+ });
942
+ } else {
943
+ await this.updateSessionConnection(conn.id, true);
944
+ }
945
+ }
946
+ this.updateUserConnectionStatus(user, true);
947
+ await awaitReturn(subRoom["onJoin"]?.(user, conn, ctx));
948
+ conn.setState({
949
+ ...conn.state,
950
+ publicId
951
+ });
952
+ this.send(conn, {
953
+ type: "sync",
954
+ value: {
955
+ pId: publicId,
956
+ ...subRoom.$memoryAll
957
+ }
958
+ }, subRoom);
959
+ }
960
+ /**
961
+ * @method onConnect
962
+ * @async
963
+ * @param {Party.Connection} conn - The connection object for the new user.
964
+ * @param {Party.ConnectionContext} ctx - The context of the connection.
965
+ * @description Handles a new user connection, creates a user object, and sends initial sync data.
966
+ * @returns {Promise<void>}
967
+ *
968
+ * @example
969
+ * ```typescript
970
+ * server.onConnect = async (conn, ctx) => {
971
+ * await server.onConnect(conn, ctx);
972
+ * console.log("New user connected:", conn.id);
973
+ * };
974
+ * ```
975
+ */
976
+ async onConnect(conn, ctx) {
977
+ if (ctx.request?.headers.has("x-shard-id")) {
978
+ this.onConnectShard(conn, ctx);
979
+ } else {
980
+ await this.onConnectClient(conn, ctx);
981
+ }
982
+ }
983
+ /**
984
+ * @method onConnectShard
985
+ * @private
986
+ * @param {Party.Connection} conn - The connection object for the new shard.
987
+ * @param {Party.ConnectionContext} ctx - The context of the shard connection.
988
+ * @description Handles a new shard connection, setting up the necessary state.
989
+ * @returns {void}
990
+ */
991
+ onConnectShard(conn, ctx) {
992
+ const shardId = ctx.request?.headers.get("x-shard-id") || "unknown-shard";
993
+ conn.setState({
994
+ shard: true,
995
+ shardId,
996
+ clients: /* @__PURE__ */ new Map()
997
+ // Track clients connected through this shard
998
+ });
999
+ }
1000
+ /**
1001
+ * @method onMessage
1002
+ * @async
1003
+ * @param {string} message - The message received from a user or shard.
1004
+ * @param {Party.Connection} sender - The connection object of the sender.
1005
+ * @description Processes incoming messages, handling differently based on if sender is shard or client.
1006
+ * @returns {Promise<void>}
1007
+ */
1008
+ async onMessage(message, sender) {
1009
+ if (sender.state && sender.state.shard) {
1010
+ await this.handleShardMessage(message, sender);
1011
+ return;
1012
+ }
1013
+ let json;
1014
+ try {
1015
+ json = JSON.parse(message);
1016
+ } catch (e) {
1017
+ return;
1018
+ }
1019
+ const result = Message.safeParse(json);
1020
+ if (!result.success) {
1021
+ return;
1022
+ }
1023
+ const subRoom = await this.getSubRoom();
1024
+ if (!subRoom) {
1025
+ console.warn("Room not found");
1026
+ return;
1027
+ }
1028
+ const roomGuards = subRoom.constructor["_roomGuards"] || [];
1029
+ for (const guard of roomGuards) {
1030
+ const isAuthorized = await guard(sender, result.data.value, this.room);
1031
+ if (!isAuthorized) {
1032
+ return;
1033
+ }
1034
+ }
1035
+ const actions = subRoom.constructor["_actionMetadata"];
1036
+ if (actions) {
1037
+ const signal2 = this.getUsersProperty(subRoom);
1038
+ const { publicId } = sender.state;
1039
+ const user = signal2?.()[publicId];
1040
+ const actionName = actions.get(result.data.action);
1041
+ if (actionName) {
1042
+ const guards = subRoom.constructor["_actionGuards"]?.get(actionName.key) || [];
1043
+ for (const guard of guards) {
1044
+ const isAuthorized = await guard(sender, result.data.value);
1045
+ if (!isAuthorized) {
1046
+ return;
1047
+ }
1048
+ }
1049
+ if (actionName.bodyValidation) {
1050
+ const bodyResult = actionName.bodyValidation.safeParse(result.data.value);
1051
+ if (!bodyResult.success) {
1052
+ return;
1053
+ }
1054
+ }
1055
+ await awaitReturn(subRoom[actionName.key](user, result.data.value, sender));
1056
+ }
1057
+ }
1058
+ }
1059
+ /**
1060
+ * @method handleShardMessage
1061
+ * @private
1062
+ * @async
1063
+ * @param {string} message - The message received from a shard.
1064
+ * @param {Party.Connection} shardConnection - The connection object of the shard.
1065
+ * @description Processes messages from shards, extracting client information.
1066
+ * @returns {Promise<void>}
1067
+ */
1068
+ async handleShardMessage(message, shardConnection) {
1069
+ let parsedMessage;
1070
+ try {
1071
+ parsedMessage = JSON.parse(message);
1072
+ } catch (e) {
1073
+ console.error("Error parsing shard message:", e);
1074
+ return;
1075
+ }
1076
+ const shardState = shardConnection.state;
1077
+ shardState.clients;
1078
+ switch (parsedMessage.type) {
1079
+ case "shard.clientConnected":
1080
+ await this.handleShardClientConnect(parsedMessage, shardConnection);
1081
+ break;
1082
+ case "shard.clientMessage":
1083
+ await this.handleShardClientMessage(parsedMessage, shardConnection);
1084
+ break;
1085
+ case "shard.clientDisconnected":
1086
+ await this.handleShardClientDisconnect(parsedMessage, shardConnection);
1087
+ break;
1088
+ default:
1089
+ console.warn(`Unknown shard message type: ${parsedMessage.type}`);
1090
+ }
1091
+ }
1092
+ /**
1093
+ * @method handleShardClientConnect
1094
+ * @private
1095
+ * @async
1096
+ * @param {Object} message - The client connection message from a shard.
1097
+ * @param {Party.Connection} shardConnection - The connection object of the shard.
1098
+ * @description Handles a new client connection via a shard.
1099
+ * @returns {Promise<void>}
1100
+ */
1101
+ async handleShardClientConnect(message, shardConnection) {
1102
+ const { privateId, requestInfo } = message;
1103
+ const shardState = shardConnection.state;
1104
+ const virtualContext = {
1105
+ request: requestInfo ? {
1106
+ headers: new Headers(requestInfo.headers),
1107
+ method: requestInfo.method,
1108
+ url: requestInfo.url
1109
+ } : void 0
1110
+ };
1111
+ const virtualConnection = {
1112
+ id: privateId,
1113
+ send: /* @__PURE__ */ __name((data) => {
1114
+ shardConnection.send(JSON.stringify({
1115
+ targetClientId: privateId,
1116
+ data
1117
+ }));
1118
+ }, "send"),
1119
+ state: {},
1120
+ setState: /* @__PURE__ */ __name((state) => {
1121
+ const clients = shardState.clients;
1122
+ const currentState = clients.get(privateId) || {};
1123
+ const mergedState = Object.assign({}, currentState, state);
1124
+ clients.set(privateId, mergedState);
1125
+ virtualConnection.state = clients.get(privateId);
1126
+ return virtualConnection.state;
1127
+ }, "setState"),
1128
+ close: /* @__PURE__ */ __name(() => {
1129
+ shardConnection.send(JSON.stringify({
1130
+ type: "shard.closeClient",
1131
+ privateId
1132
+ }));
1133
+ if (shardState.clients) {
1134
+ shardState.clients.delete(privateId);
1135
+ }
1136
+ }, "close")
1137
+ };
1138
+ if (!shardState.clients.has(privateId)) {
1139
+ shardState.clients.set(privateId, {});
1140
+ }
1141
+ await this.onConnectClient(virtualConnection, virtualContext);
1142
+ }
1143
+ /**
1144
+ * @method handleShardClientMessage
1145
+ * @private
1146
+ * @async
1147
+ * @param {Object} message - The client message from a shard.
1148
+ * @param {Party.Connection} shardConnection - The connection object of the shard.
1149
+ * @description Handles a message from a client via a shard.
1150
+ * @returns {Promise<void>}
1151
+ */
1152
+ async handleShardClientMessage(message, shardConnection) {
1153
+ const { privateId, publicId, payload } = message;
1154
+ const shardState = shardConnection.state;
1155
+ const clients = shardState.clients;
1156
+ if (!clients.has(privateId)) {
1157
+ console.warn(`Received message from unknown client ${privateId}, creating virtual connection`);
1158
+ clients.set(privateId, {
1159
+ publicId
1160
+ });
1161
+ }
1162
+ const virtualConnection = {
1163
+ id: privateId,
1164
+ send: /* @__PURE__ */ __name((data) => {
1165
+ shardConnection.send(JSON.stringify({
1166
+ targetClientId: privateId,
1167
+ data
1168
+ }));
1169
+ }, "send"),
1170
+ state: clients.get(privateId),
1171
+ setState: /* @__PURE__ */ __name((state) => {
1172
+ const currentState = clients.get(privateId) || {};
1173
+ const mergedState = Object.assign({}, currentState, state);
1174
+ clients.set(privateId, mergedState);
1175
+ virtualConnection.state = clients.get(privateId);
1176
+ return virtualConnection.state;
1177
+ }, "setState"),
1178
+ close: /* @__PURE__ */ __name(() => {
1179
+ shardConnection.send(JSON.stringify({
1180
+ type: "shard.closeClient",
1181
+ privateId
1182
+ }));
1183
+ if (shardState.clients) {
1184
+ shardState.clients.delete(privateId);
1185
+ }
1186
+ }, "close")
1187
+ };
1188
+ const payloadString = typeof payload === "string" ? payload : JSON.stringify(payload);
1189
+ await this.onMessage(payloadString, virtualConnection);
1190
+ }
1191
+ /**
1192
+ * @method handleShardClientDisconnect
1193
+ * @private
1194
+ * @async
1195
+ * @param {Object} message - The client disconnection message from a shard.
1196
+ * @param {Party.Connection} shardConnection - The connection object of the shard.
1197
+ * @description Handles a client disconnection via a shard.
1198
+ * @returns {Promise<void>}
1199
+ */
1200
+ async handleShardClientDisconnect(message, shardConnection) {
1201
+ const { privateId, publicId } = message;
1202
+ const shardState = shardConnection.state;
1203
+ const clients = shardState.clients;
1204
+ const clientState = clients.get(privateId);
1205
+ if (!clientState) {
1206
+ console.warn(`Disconnection for unknown client ${privateId}`);
1207
+ return;
1208
+ }
1209
+ const virtualConnection = {
1210
+ id: privateId,
1211
+ send: /* @__PURE__ */ __name(() => {
1212
+ }, "send"),
1213
+ state: clientState,
1214
+ setState: /* @__PURE__ */ __name(() => {
1215
+ return {};
1216
+ }, "setState"),
1217
+ close: /* @__PURE__ */ __name(() => {
1218
+ }, "close")
1219
+ };
1220
+ await this.onClose(virtualConnection);
1221
+ clients.delete(privateId);
1222
+ }
1223
+ /**
1224
+ * @method onClose
1225
+ * @async
1226
+ * @param {Party.Connection} conn - The connection object of the disconnecting user.
1227
+ * @description Handles user disconnection, removing them from the room and triggering the onLeave event..
1228
+ * @returns {Promise<void>}
1229
+ *
1230
+ * @example
1231
+ * ```typescript
1232
+ * server.onClose = async (conn) => {
1233
+ * await server.onClose(conn);
1234
+ * console.log("User disconnected:", conn.id);
1235
+ * };
1236
+ * ```
1237
+ */
1238
+ async onClose(conn) {
1239
+ const subRoom = await this.getSubRoom();
1240
+ if (!subRoom) {
1241
+ return;
1242
+ }
1243
+ const signal2 = this.getUsersProperty(subRoom);
1244
+ if (!conn.state) {
1245
+ return;
1246
+ }
1247
+ const privateId = conn.id;
1248
+ const { publicId } = conn.state;
1249
+ const user = signal2?.()[publicId];
1250
+ if (!user) return;
1251
+ await this.updateSessionConnection(privateId, false);
1252
+ const connectionUpdated = this.updateUserConnectionStatus(user, false);
1253
+ await awaitReturn(subRoom["onLeave"]?.(user, conn));
1254
+ if (!connectionUpdated) {
1255
+ this.broadcast({
1256
+ type: "user_disconnected",
1257
+ value: {
1258
+ publicId
1259
+ }
1260
+ }, subRoom);
1261
+ }
1262
+ }
1263
+ async onAlarm() {
1264
+ const subRoom = await this.getSubRoom();
1265
+ await awaitReturn(subRoom["onAlarm"]?.(subRoom));
1266
+ }
1267
+ async onError(connection, error) {
1268
+ const subRoom = await this.getSubRoom();
1269
+ await awaitReturn(subRoom["onError"]?.(connection, error));
1270
+ }
1271
+ /**
1272
+ * @method onRequest
1273
+ * @async
1274
+ * @param {Party.Request} req - The HTTP request to handle
1275
+ * @description Handles HTTP requests, either directly from clients or forwarded by shards
1276
+ * @returns {Promise<Response>} The response to return to the client
1277
+ */
1278
+ async onRequest(req) {
1279
+ const isFromShard = req.headers.has("x-forwarded-by-shard");
1280
+ const shardId = req.headers.get("x-shard-id");
1281
+ const res = new ServerResponse([
1282
+ createCorsInterceptor()
1283
+ ]);
1284
+ if (req.method === "OPTIONS") {
1285
+ return res.status(200).send({});
1286
+ }
1287
+ if (isFromShard) {
1288
+ return this.handleShardRequest(req, res, shardId);
1289
+ }
1290
+ return this.handleDirectRequest(req, res);
1291
+ }
1292
+ /**
1293
+ * @method handleSessionRestore
1294
+ * @private
1295
+ * @async
1296
+ * @param {Party.Request} req - The HTTP request for session restore
1297
+ * @param {ServerResponse} res - The response object
1298
+ * @description Handles session restoration from transfer data, creates session from privateId
1299
+ * @returns {Promise<Response>} The response to return to the client
1300
+ */
1301
+ async handleSessionRestore(req, res) {
1302
+ try {
1303
+ const transferData = await req.json();
1304
+ const { privateId, userSnapshot, sessionState, publicId } = transferData;
1305
+ if (!privateId || !publicId) {
1306
+ return res.badRequest("Missing privateId or publicId in transfer data");
1307
+ }
1308
+ const subRoom = await this.getSubRoom();
1309
+ if (!subRoom) {
1310
+ return res.serverError("Room not available");
1311
+ }
1312
+ await this.saveSession(privateId, {
1313
+ publicId,
1314
+ state: sessionState,
1315
+ created: Date.now(),
1316
+ connected: false
1317
+ // Will be set to true when user connects
1318
+ });
1319
+ if (userSnapshot) {
1320
+ const signal2 = this.getUsersProperty(subRoom);
1321
+ const usersPropName = this.getUsersPropName(subRoom);
1322
+ if (signal2 && usersPropName) {
1323
+ const { classType } = signal2.options;
1324
+ const user = isClass(classType) ? new classType() : classType();
1325
+ load(user, userSnapshot, true);
1326
+ signal2()[publicId] = user;
1327
+ await this.room.storage.put(`${usersPropName}.${publicId}`, userSnapshot);
1328
+ }
1329
+ }
1330
+ const transferToken = generateShortUUID$1();
1331
+ await this.room.storage.put(`transfer:${transferToken}`, {
1332
+ privateId,
1333
+ publicId,
1334
+ restored: true
1335
+ });
1336
+ return res.success({
1337
+ transferToken
1338
+ });
1339
+ } catch (error) {
1340
+ console.error("Error restoring session:", error);
1341
+ return res.serverError("Failed to restore session");
1342
+ }
1343
+ }
1344
+ /**
1345
+ * @method handleDirectRequest
1346
+ * @private
1347
+ * @async
1348
+ * @param {Party.Request} req - The HTTP request received directly from a client
1349
+ * @description Processes requests received directly from clients
1350
+ * @returns {Promise<Response>} The response to return to the client
1351
+ */
1352
+ async handleDirectRequest(req, res) {
1353
+ const subRoom = await this.getSubRoom();
1354
+ if (!subRoom) {
1355
+ return res.notFound();
1356
+ }
1357
+ const url = new URL(req.url);
1358
+ if (url.pathname.endsWith("/session-transfer") && req.method === "POST") {
1359
+ return this.handleSessionRestore(req, res);
1360
+ }
1361
+ const response2 = await this.tryMatchRequestHandler(req, res, subRoom);
1362
+ if (response2) {
1363
+ return response2;
1364
+ }
1365
+ const legacyResponse = await awaitReturn(subRoom["onRequest"]?.(req, res));
1366
+ if (!legacyResponse) {
1367
+ return res.notFound();
1368
+ }
1369
+ if (legacyResponse instanceof Response) {
1370
+ return legacyResponse;
1371
+ }
1372
+ return res.success(legacyResponse);
1373
+ }
1374
+ /**
1375
+ * @method tryMatchRequestHandler
1376
+ * @private
1377
+ * @async
1378
+ * @param {Party.Request} req - The HTTP request to handle
1379
+ * @param {Object} subRoom - The room instance
1380
+ * @description Attempts to match the request to a registered @Request handler
1381
+ * @returns {Promise<Response | null>} The response or null if no handler matched
1382
+ */
1383
+ async tryMatchRequestHandler(req, res, subRoom) {
1384
+ const requestHandlers = subRoom.constructor["_requestMetadata"];
1385
+ if (!requestHandlers) {
1386
+ return null;
1387
+ }
1388
+ const url = new URL(req.url);
1389
+ const method = req.method;
1390
+ let pathname = url.pathname;
1391
+ pathname = "/" + pathname.split("/").slice(4).join("/");
1392
+ for (const [routeKey, handler] of requestHandlers.entries()) {
1393
+ const firstColonIndex = routeKey.indexOf(":");
1394
+ const handlerMethod = routeKey.substring(0, firstColonIndex);
1395
+ const handlerPath = routeKey.substring(firstColonIndex + 1);
1396
+ if (handlerMethod !== method) {
1397
+ continue;
1398
+ }
1399
+ if (this.pathMatches(pathname, handlerPath)) {
1400
+ const params = this.extractPathParams(pathname, handlerPath);
1401
+ const guards = subRoom.constructor["_actionGuards"]?.get(handler.key) || [];
1402
+ for (const guard of guards) {
1403
+ const isAuthorized = await guard(null, req, this.room);
1404
+ if (isAuthorized instanceof Response) {
1405
+ return isAuthorized;
1406
+ }
1407
+ if (!isAuthorized) {
1408
+ return res.notPermitted();
1409
+ }
1410
+ }
1411
+ let bodyData = null;
1412
+ if (handler.bodyValidation && [
1413
+ "POST",
1414
+ "PUT",
1415
+ "PATCH"
1416
+ ].includes(method)) {
1417
+ try {
1418
+ const contentType = req.headers.get("content-type") || "";
1419
+ if (contentType.includes("application/json")) {
1420
+ const body = await req.json();
1421
+ const validation = handler.bodyValidation.safeParse(body);
1422
+ if (!validation.success) {
1423
+ return res.badRequest("Invalid request body", {
1424
+ details: validation.error
1425
+ });
1426
+ }
1427
+ bodyData = validation.data;
1428
+ }
1429
+ } catch (error) {
1430
+ return res.badRequest("Failed to parse request body");
1431
+ }
1432
+ }
1433
+ try {
1434
+ req["data"] = bodyData;
1435
+ req["params"] = params;
1436
+ const result = await awaitReturn(subRoom[handler.key](req, res));
1437
+ if (result instanceof Response) {
1438
+ return result;
1439
+ }
1440
+ return res.success(result);
1441
+ } catch (error) {
1442
+ console.error("Error executing request handler:", error);
1443
+ return res.serverError();
1444
+ }
1445
+ }
1446
+ }
1447
+ return null;
1448
+ }
1449
+ /**
1450
+ * @method pathMatches
1451
+ * @private
1452
+ * @param {string} requestPath - The path from the request
1453
+ * @param {string} handlerPath - The path pattern from the handler
1454
+ * @description Checks if a request path matches a handler path pattern
1455
+ * @returns {boolean} True if the paths match
1456
+ */
1457
+ pathMatches(requestPath, handlerPath) {
1458
+ const pathRegexString = handlerPath.replace(/\//g, "\\/").replace(/:([^\/]+)/g, "([^/]+)");
1459
+ const pathRegex = new RegExp(`^${pathRegexString}`);
1460
+ return pathRegex.test(requestPath);
1461
+ }
1462
+ /**
1463
+ * @method extractPathParams
1464
+ * @private
1465
+ * @param {string} requestPath - The path from the request
1466
+ * @param {string} handlerPath - The path pattern from the handler
1467
+ * @description Extracts path parameters from the request path based on the handler pattern
1468
+ * @returns {Object} An object containing the path parameters
1469
+ */
1470
+ extractPathParams(requestPath, handlerPath) {
1471
+ const params = {};
1472
+ const paramNames = [];
1473
+ handlerPath.split("/").forEach((segment) => {
1474
+ if (segment.startsWith(":")) {
1475
+ paramNames.push(segment.substring(1));
1476
+ }
1477
+ });
1478
+ const pathRegexString = handlerPath.replace(/\//g, "\\/").replace(/:([^\/]+)/g, "([^/]+)");
1479
+ const pathRegex = new RegExp(`^${pathRegexString}`);
1480
+ const matches = requestPath.match(pathRegex);
1481
+ if (matches && matches.length > 1) {
1482
+ for (let i = 0; i < paramNames.length; i++) {
1483
+ params[paramNames[i]] = matches[i + 1];
1484
+ }
1485
+ }
1486
+ return params;
1487
+ }
1488
+ /**
1489
+ * @method handleShardRequest
1490
+ * @private
1491
+ * @async
1492
+ * @param {Party.Request} req - The HTTP request forwarded by a shard
1493
+ * @param {string | null} shardId - The ID of the shard that forwarded the request
1494
+ * @description Processes requests forwarded by shards, preserving client context
1495
+ * @returns {Promise<Response>} The response to return to the shard (which will forward it to the client)
1496
+ */
1497
+ async handleShardRequest(req, res, shardId) {
1498
+ const subRoom = await this.getSubRoom();
1499
+ if (!subRoom) {
1500
+ return res.notFound();
1501
+ }
1502
+ const originalClientIp = req.headers.get("x-original-client-ip");
1503
+ const enhancedReq = this.createEnhancedRequest(req, originalClientIp);
1504
+ try {
1505
+ const response2 = await this.tryMatchRequestHandler(enhancedReq, res, subRoom);
1506
+ if (response2) {
1507
+ return response2;
1508
+ }
1509
+ const legacyResponse = await awaitReturn(subRoom["onRequest"]?.(enhancedReq, res));
1510
+ if (!legacyResponse) {
1511
+ return res.notFound();
1512
+ }
1513
+ if (legacyResponse instanceof Response) {
1514
+ return legacyResponse;
1515
+ }
1516
+ return res.success(legacyResponse);
1517
+ } catch (error) {
1518
+ console.error(`Error processing request from shard ${shardId}:`, error);
1519
+ return res.serverError();
1520
+ }
1521
+ }
1522
+ /**
1523
+ * @method createEnhancedRequest
1524
+ * @private
1525
+ * @param {Party.Request} originalReq - The original request received from the shard
1526
+ * @param {string | null} originalClientIp - The original client IP, if available
1527
+ * @description Creates an enhanced request object that preserves the original client context
1528
+ * @returns {Party.Request} The enhanced request object
1529
+ */
1530
+ createEnhancedRequest(originalReq, originalClientIp) {
1531
+ const clonedReq = originalReq.clone();
1532
+ clonedReq.viaShard = true;
1533
+ if (originalClientIp) {
1534
+ clonedReq.originalClientIp = originalClientIp;
1535
+ }
1536
+ return clonedReq;
1537
+ }
1538
+ };
1539
+
1540
+ // src/shard.ts
1541
+ var Shard = class {
1542
+ static {
1543
+ __name(this, "Shard");
1544
+ }
1545
+ room;
1546
+ ws;
1547
+ connectionMap;
1548
+ mainServerStub;
1549
+ worldUrl;
1550
+ worldId;
1551
+ lastReportedConnections;
1552
+ statsInterval;
1553
+ statsIntervalId;
1554
+ constructor(room) {
1555
+ this.room = room;
1556
+ this.connectionMap = /* @__PURE__ */ new Map();
1557
+ this.worldUrl = null;
1558
+ this.worldId = "default";
1559
+ this.lastReportedConnections = 0;
1560
+ this.statsInterval = 3e4;
1561
+ this.statsIntervalId = null;
1562
+ }
1563
+ async onStart() {
1564
+ const roomId = this.room.id.split(":")[0];
1565
+ const roomStub = this.room.context.parties.main.get(roomId);
1566
+ if (!roomStub) {
1567
+ console.warn("No room room stub found in main party context");
1568
+ return;
1569
+ }
1570
+ this.mainServerStub = roomStub;
1571
+ this.ws = await roomStub.socket({
1572
+ headers: {
1573
+ "x-shard-id": this.room.id
1574
+ }
1575
+ });
1576
+ this.ws.addEventListener("message", (event) => {
1577
+ try {
1578
+ const message = JSON.parse(event.data);
1579
+ if (message.targetClientId) {
1580
+ const clientConn = this.connectionMap.get(message.targetClientId);
1581
+ if (clientConn) {
1582
+ delete message.targetClientId;
1583
+ clientConn.send(message.data);
1584
+ }
1585
+ } else {
1586
+ this.room.broadcast(event.data);
1587
+ }
1588
+ } catch (error) {
1589
+ console.error("Error processing message from main server:", error);
1590
+ }
1591
+ });
1592
+ await this.updateWorldStats();
1593
+ this.startPeriodicStatsUpdates();
1594
+ }
1595
+ startPeriodicStatsUpdates() {
1596
+ if (!this.worldUrl) {
1597
+ return;
1598
+ }
1599
+ if (this.statsIntervalId) {
1600
+ clearInterval(this.statsIntervalId);
1601
+ }
1602
+ this.statsIntervalId = setInterval(() => {
1603
+ this.updateWorldStats().catch((error) => {
1604
+ console.error("Error in periodic stats update:", error);
1605
+ });
1606
+ }, this.statsInterval);
1607
+ }
1608
+ stopPeriodicStatsUpdates() {
1609
+ if (this.statsIntervalId) {
1610
+ clearInterval(this.statsIntervalId);
1611
+ this.statsIntervalId = null;
1612
+ }
1613
+ }
1614
+ onConnect(conn, ctx) {
1615
+ this.connectionMap.set(conn.id, conn);
1616
+ const headers = {};
1617
+ if (ctx.request?.headers) {
1618
+ ctx.request.headers.forEach((value, key) => {
1619
+ headers[key] = value;
1620
+ });
1621
+ }
1622
+ const requestInfo = ctx.request ? {
1623
+ headers,
1624
+ url: ctx.request.url,
1625
+ method: ctx.request.method
1626
+ } : null;
1627
+ this.ws.send(JSON.stringify({
1628
+ type: "shard.clientConnected",
1629
+ privateId: conn.id,
1630
+ requestInfo
1631
+ }));
1632
+ this.updateWorldStats();
1633
+ }
1634
+ onMessage(message, sender) {
1635
+ try {
1636
+ const parsedMessage = typeof message === "string" ? JSON.parse(message) : message;
1637
+ const wrappedMessage = JSON.stringify({
1638
+ type: "shard.clientMessage",
1639
+ privateId: sender.id,
1640
+ publicId: sender.state?.publicId,
1641
+ payload: parsedMessage
1642
+ });
1643
+ this.ws.send(wrappedMessage);
1644
+ } catch (error) {
1645
+ console.error("Error forwarding message to main server:", error);
1646
+ }
1647
+ }
1648
+ onClose(conn) {
1649
+ this.connectionMap.delete(conn.id);
1650
+ this.ws.send(JSON.stringify({
1651
+ type: "shard.clientDisconnected",
1652
+ privateId: conn.id,
1653
+ publicId: conn.state?.publicId
1654
+ }));
1655
+ this.updateWorldStats();
1656
+ }
1657
+ async updateWorldStats() {
1658
+ const currentConnections = this.connectionMap.size;
1659
+ if (currentConnections === this.lastReportedConnections) {
1660
+ return true;
1661
+ }
1662
+ try {
1663
+ const worldRoom = this.room.context.parties.world.get("world-default");
1664
+ const response2 = await worldRoom.fetch("/update-shard", {
77
1665
  method: "POST",
78
1666
  headers: {
79
- "Content-Type": "application/json"
1667
+ "Content-Type": "application/json",
1668
+ "x-access-shard": this.room.env.SHARD_SECRET
80
1669
  },
81
1670
  body: JSON.stringify({
82
- roomId: room,
83
- autoCreate
1671
+ shardId: this.room.id,
1672
+ connections: currentConnections
84
1673
  })
85
1674
  });
86
- if (!response.ok) {
87
- const errorData = await response.json().catch(() => ({
1675
+ if (!response2.ok) {
1676
+ const errorData = await response2.json().catch(() => ({
88
1677
  error: "Unknown error"
89
1678
  }));
90
- throw new Error(`World service returned ${response.status}: ${errorData.error || "Unknown error"}`);
1679
+ console.error(`Failed to update World stats: ${response2.status} - ${errorData.error || "Unknown error"}`);
1680
+ return false;
91
1681
  }
92
- const data = await response.json();
93
- if (!data.url || !data.shardId) {
94
- throw new Error("Invalid response from World service: missing url or shardId");
1682
+ this.lastReportedConnections = currentConnections;
1683
+ return true;
1684
+ } catch (error) {
1685
+ console.error("Error updating World stats:", error);
1686
+ return false;
1687
+ }
1688
+ }
1689
+ /**
1690
+ * @method onRequest
1691
+ * @async
1692
+ * @param {Party.Request} req - The HTTP request to handle
1693
+ * @description Forwards HTTP requests to the main server, preserving client context
1694
+ * @returns {Promise<Response>} The response from the main server
1695
+ */
1696
+ async onRequest(req) {
1697
+ if (!this.mainServerStub) {
1698
+ return response(503, {
1699
+ error: "Shard not connected to main server"
1700
+ });
1701
+ }
1702
+ try {
1703
+ const url = new URL(req.url);
1704
+ const path = url.pathname;
1705
+ const method = req.method;
1706
+ let body = null;
1707
+ if (method !== "GET" && method !== "HEAD") {
1708
+ body = await req.text();
95
1709
  }
96
- return {
97
- shardId: data.shardId,
98
- url: data.url
1710
+ const headers = new Headers();
1711
+ req.headers.forEach((value, key) => {
1712
+ headers.set(key, value);
1713
+ });
1714
+ headers.set("x-shard-id", this.room.id);
1715
+ headers.set("x-forwarded-by-shard", "true");
1716
+ const clientIp = req.headers.get("x-forwarded-for") || "unknown";
1717
+ if (clientIp) {
1718
+ headers.set("x-original-client-ip", clientIp);
1719
+ }
1720
+ const requestInit = {
1721
+ method,
1722
+ headers,
1723
+ body
99
1724
  };
1725
+ const response2 = await this.mainServerStub.fetch(path, requestInit);
1726
+ return response2;
1727
+ } catch (error) {
1728
+ return response(500, {
1729
+ error: "Error forwarding request"
1730
+ });
1731
+ }
1732
+ }
1733
+ /**
1734
+ * @method onAlarm
1735
+ * @async
1736
+ * @description Executed periodically, used to perform maintenance tasks
1737
+ */
1738
+ async onAlarm() {
1739
+ await this.updateWorldStats();
1740
+ }
1741
+ };
1742
+
1743
+ // src/testing.ts
1744
+ async function testRoom(Room3, options = {}) {
1745
+ const createServer = /* @__PURE__ */ __name((io2) => {
1746
+ const server2 = new Server(io2);
1747
+ server2.rooms = [
1748
+ Room3
1749
+ ];
1750
+ return server2;
1751
+ }, "createServer");
1752
+ const isShard = options.shard || false;
1753
+ const io = new ServerIo(Room3.path, isShard ? {
1754
+ parties: {
1755
+ game: createServer
1756
+ },
1757
+ env: options.env
1758
+ } : {
1759
+ env: options.env
1760
+ });
1761
+ Room3.prototype.throttleSync = 0;
1762
+ Room3.prototype.throttleStorage = 0;
1763
+ Room3.prototype.options = options;
1764
+ let server;
1765
+ if (options.shard) {
1766
+ const shardServer = new Shard(io);
1767
+ shardServer.subRoom = null;
1768
+ server = shardServer;
1769
+ for (const lobby of io.context.parties.main.values()) {
1770
+ await lobby.server.onStart();
1771
+ }
1772
+ } else {
1773
+ server = await createServer(io);
1774
+ }
1775
+ await server.onStart();
1776
+ return {
1777
+ server,
1778
+ room: server.subRoom,
1779
+ createClient: /* @__PURE__ */ __name(async (id2) => {
1780
+ const client = await io.connection(server, id2);
1781
+ return client;
1782
+ }, "createClient")
1783
+ };
1784
+ }
1785
+ __name(testRoom, "testRoom");
1786
+ async function request(room, path, options = {
1787
+ method: "GET"
1788
+ }) {
1789
+ const url = new URL("http://localhost" + path);
1790
+ const request1 = new Request(url.toString(), options);
1791
+ const response2 = await room.onRequest(request1);
1792
+ return response2;
1793
+ }
1794
+ __name(request, "request");
1795
+
1796
+ // src/mock.ts
1797
+ var MockPartyClient = class {
1798
+ static {
1799
+ __name(this, "MockPartyClient");
1800
+ }
1801
+ server;
1802
+ events;
1803
+ id;
1804
+ conn;
1805
+ constructor(server, id2) {
1806
+ this.server = server;
1807
+ this.events = /* @__PURE__ */ new Map();
1808
+ this.id = id2 || generateShortUUID();
1809
+ this.conn = new MockConnection(this);
1810
+ }
1811
+ addEventListener(event, cb) {
1812
+ if (!this.events.has(event)) {
1813
+ this.events.set(event, []);
1814
+ }
1815
+ this.events.get(event).push(cb);
1816
+ }
1817
+ removeEventListener(event, cb) {
1818
+ if (!this.events.has(event)) return;
1819
+ const callbacks = this.events.get(event);
1820
+ const index = callbacks.indexOf(cb);
1821
+ if (index !== -1) {
1822
+ callbacks.splice(index, 1);
1823
+ }
1824
+ if (callbacks.length === 0) {
1825
+ this.events.delete(event);
1826
+ }
1827
+ }
1828
+ _trigger(event, data) {
1829
+ const callbacks = this.events.get(event);
1830
+ if (callbacks) {
1831
+ for (const cb of callbacks) {
1832
+ cb(data);
1833
+ }
1834
+ }
1835
+ }
1836
+ send(data) {
1837
+ return this.server.onMessage(JSON.stringify(data), this.conn);
1838
+ }
1839
+ };
1840
+ var MockLobby = class MockLobby2 {
1841
+ static {
1842
+ __name(this, "MockLobby");
1843
+ }
1844
+ server;
1845
+ constructor(server) {
1846
+ this.server = server;
1847
+ }
1848
+ socket() {
1849
+ return new MockPartyClient(this.server);
1850
+ }
1851
+ fetch(url, options) {
1852
+ return request(this.server, url, options);
1853
+ }
1854
+ };
1855
+ var MockContext = class MockContext2 {
1856
+ static {
1857
+ __name(this, "MockContext");
1858
+ }
1859
+ room;
1860
+ parties;
1861
+ constructor(room, options = {}) {
1862
+ this.room = room;
1863
+ this.parties = {
1864
+ main: /* @__PURE__ */ new Map()
1865
+ };
1866
+ const parties = options.parties || {};
1867
+ for (let lobbyId in parties) {
1868
+ this.parties.main.set(lobbyId, new MockLobby(parties[lobbyId](room)));
1869
+ }
1870
+ }
1871
+ };
1872
+ var MockPartyRoom = class MockPartyRoom2 {
1873
+ static {
1874
+ __name(this, "MockPartyRoom");
1875
+ }
1876
+ id;
1877
+ clients;
1878
+ storage;
1879
+ context;
1880
+ env;
1881
+ constructor(id2, options = {}) {
1882
+ this.id = id2;
1883
+ this.clients = /* @__PURE__ */ new Map();
1884
+ this.storage = new Storage();
1885
+ this.env = {};
1886
+ this.id = id2 || generateShortUUID();
1887
+ this.context = new MockContext(this, {
1888
+ parties: options.parties || {}
1889
+ });
1890
+ this.env = options.env || {};
1891
+ }
1892
+ async connection(server, id2) {
1893
+ const socket = new MockPartyClient(server, id2);
1894
+ const url = new URL("http://localhost");
1895
+ const request2 = new Request(url.toString(), {
1896
+ method: "GET",
1897
+ headers: {
1898
+ "Content-Type": "application/json"
1899
+ }
1900
+ });
1901
+ await server.onConnect(socket.conn, {
1902
+ request: request2
1903
+ });
1904
+ this.clients.set(socket.id, socket);
1905
+ return socket;
1906
+ }
1907
+ broadcast(data) {
1908
+ this.clients.forEach((client) => {
1909
+ client._trigger("message", data);
1910
+ });
1911
+ }
1912
+ getConnection(id2) {
1913
+ return this.clients.get(id2);
1914
+ }
1915
+ getConnections() {
1916
+ return Array.from(this.clients.values()).map((client) => client.conn);
1917
+ }
1918
+ clear() {
1919
+ this.clients.clear();
1920
+ }
1921
+ };
1922
+ var MockConnection = class {
1923
+ static {
1924
+ __name(this, "MockConnection");
1925
+ }
1926
+ client;
1927
+ server;
1928
+ id;
1929
+ constructor(client) {
1930
+ this.client = client;
1931
+ this.state = {};
1932
+ this.server = client.server;
1933
+ this.id = client.id;
1934
+ }
1935
+ state;
1936
+ setState(value) {
1937
+ this.state = value;
1938
+ }
1939
+ send(data) {
1940
+ this.client._trigger("message", data);
1941
+ }
1942
+ close() {
1943
+ this.server.onClose(this);
1944
+ }
1945
+ };
1946
+ var ServerIo = MockPartyRoom;
1947
+ var ClientIo = MockPartyClient;
1948
+
1949
+ // src/jwt.ts
1950
+ var JWTAuth = class {
1951
+ static {
1952
+ __name(this, "JWTAuth");
1953
+ }
1954
+ secret;
1955
+ encoder;
1956
+ decoder;
1957
+ /**
1958
+ * Constructor for the JWTAuth class
1959
+ * @param {string} secret - The secret key used for signing and verifying tokens
1960
+ */
1961
+ constructor(secret) {
1962
+ if (!secret || typeof secret !== "string") {
1963
+ throw new Error("Secret is required and must be a string");
1964
+ }
1965
+ this.secret = secret;
1966
+ this.encoder = new TextEncoder();
1967
+ this.decoder = new TextDecoder();
1968
+ }
1969
+ /**
1970
+ * Convert the secret to a CryptoKey for HMAC operations
1971
+ * @returns {Promise<CryptoKey>} - The CryptoKey for HMAC operations
1972
+ */
1973
+ async getSecretKey() {
1974
+ const keyData = this.encoder.encode(this.secret);
1975
+ return await crypto.subtle.importKey(
1976
+ "raw",
1977
+ keyData,
1978
+ {
1979
+ name: "HMAC",
1980
+ hash: {
1981
+ name: "SHA-256"
1982
+ }
1983
+ },
1984
+ false,
1985
+ [
1986
+ "sign",
1987
+ "verify"
1988
+ ]
1989
+ // key usages
1990
+ );
1991
+ }
1992
+ /**
1993
+ * Base64Url encode a buffer
1994
+ * @param {ArrayBuffer} buffer - The buffer to encode
1995
+ * @returns {string} - The base64url encoded string
1996
+ */
1997
+ base64UrlEncode(buffer) {
1998
+ const base64 = btoa(String.fromCharCode(...new Uint8Array(buffer)));
1999
+ return base64.replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_");
2000
+ }
2001
+ /**
2002
+ * Base64Url decode a string
2003
+ * @param {string} base64Url - The base64url encoded string
2004
+ * @returns {ArrayBuffer} - The decoded buffer
2005
+ */
2006
+ base64UrlDecode(base64Url) {
2007
+ const padding = "=".repeat((4 - base64Url.length % 4) % 4);
2008
+ const base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/") + padding;
2009
+ const rawData = atob(base64);
2010
+ const buffer = new Uint8Array(rawData.length);
2011
+ for (let i = 0; i < rawData.length; i++) {
2012
+ buffer[i] = rawData.charCodeAt(i);
2013
+ }
2014
+ return buffer.buffer;
2015
+ }
2016
+ /**
2017
+ * Sign a payload and create a JWT token
2018
+ * @param {JWTPayload} payload - The payload to include in the token
2019
+ * @param {JWTOptions} [options={}] - Options for the token
2020
+ * @param {string | number} [options.expiresIn='1h'] - Token expiration time
2021
+ * @returns {Promise<string>} - The JWT token
2022
+ */
2023
+ async sign(payload, options = {}) {
2024
+ if (!payload || typeof payload !== "object") {
2025
+ throw new Error("Payload must be an object");
2026
+ }
2027
+ const expiresIn = options.expiresIn || "1h";
2028
+ let exp;
2029
+ if (typeof expiresIn === "number") {
2030
+ exp = Math.floor(Date.now() / 1e3) + expiresIn;
2031
+ } else if (typeof expiresIn === "string") {
2032
+ const match = expiresIn.match(/^(\d+)([smhd])$/);
2033
+ if (match) {
2034
+ const value = parseInt(match[1]);
2035
+ const unit = match[2];
2036
+ const seconds = {
2037
+ "s": value,
2038
+ "m": value * 60,
2039
+ "h": value * 60 * 60,
2040
+ "d": value * 60 * 60 * 24
2041
+ }[unit];
2042
+ exp = Math.floor(Date.now() / 1e3) + seconds;
2043
+ } else {
2044
+ throw new Error('Invalid expiresIn format. Use a number (seconds) or a string like "1h", "30m", etc.');
2045
+ }
2046
+ }
2047
+ const fullPayload = {
2048
+ ...payload,
2049
+ iat: Math.floor(Date.now() / 1e3),
2050
+ exp
2051
+ };
2052
+ const header = {
2053
+ alg: "HS256",
2054
+ typ: "JWT"
2055
+ };
2056
+ const encodedHeader = this.base64UrlEncode(this.encoder.encode(JSON.stringify(header)));
2057
+ const encodedPayload = this.base64UrlEncode(this.encoder.encode(JSON.stringify(fullPayload)));
2058
+ const signatureBase = `${encodedHeader}.${encodedPayload}`;
2059
+ const key = await this.getSecretKey();
2060
+ const signature = await crypto.subtle.sign({
2061
+ name: "HMAC"
2062
+ }, key, this.encoder.encode(signatureBase));
2063
+ const encodedSignature = this.base64UrlEncode(signature);
2064
+ return `${signatureBase}.${encodedSignature}`;
2065
+ }
2066
+ /**
2067
+ * Verify a JWT token and return the decoded payload
2068
+ * @param {string} token - The JWT token to verify
2069
+ * @returns {Promise<JWTPayload>} - The decoded payload if verification succeeds
2070
+ * @throws {Error} - If verification fails
2071
+ */
2072
+ async verify(token) {
2073
+ if (!token || typeof token !== "string") {
2074
+ throw new Error("Token is required and must be a string");
2075
+ }
2076
+ const parts = token.split(".");
2077
+ if (parts.length !== 3) {
2078
+ throw new Error("Invalid token format");
2079
+ }
2080
+ const [encodedHeader, encodedPayload, encodedSignature] = parts;
2081
+ try {
2082
+ const header = JSON.parse(this.decoder.decode(this.base64UrlDecode(encodedHeader)));
2083
+ const payload = JSON.parse(this.decoder.decode(this.base64UrlDecode(encodedPayload)));
2084
+ if (header.alg !== "HS256") {
2085
+ throw new Error(`Unsupported algorithm: ${header.alg}`);
2086
+ }
2087
+ const now = Math.floor(Date.now() / 1e3);
2088
+ if (payload.exp && payload.exp < now) {
2089
+ throw new Error("Token has expired");
2090
+ }
2091
+ const key = await this.getSecretKey();
2092
+ const signatureBase = `${encodedHeader}.${encodedPayload}`;
2093
+ const signature = this.base64UrlDecode(encodedSignature);
2094
+ const isValid = await crypto.subtle.verify({
2095
+ name: "HMAC"
2096
+ }, key, signature, this.encoder.encode(signatureBase));
2097
+ if (!isValid) {
2098
+ throw new Error("Invalid signature");
2099
+ }
2100
+ return payload;
100
2101
  } catch (error) {
101
- attempts++;
102
- if (attempts >= retryCount) {
103
- throw error;
2102
+ if (error instanceof Error) {
2103
+ throw new Error(`Token verification failed: ${error.message}`);
104
2104
  }
105
- console.warn(`Failed to get shard (attempt ${attempts}/${retryCount}). Retrying in ${retryDelay}ms...`);
106
- await new Promise((resolve) => setTimeout(resolve, retryDelay));
2105
+ throw new Error("Token verification failed: Unknown error");
2106
+ }
2107
+ }
2108
+ };
2109
+
2110
+ // src/world.guard.ts
2111
+ var guardManageWorld = /* @__PURE__ */ __name(async (_, req, room) => {
2112
+ const tokenShard = req.headers.get("x-access-shard");
2113
+ if (tokenShard) {
2114
+ if (tokenShard !== room.env.SHARD_SECRET) {
2115
+ return false;
2116
+ }
2117
+ return true;
2118
+ }
2119
+ const url = new URL(req.url);
2120
+ const token = req.headers.get("Authorization") ?? url.searchParams.get("world-auth-token");
2121
+ if (!token) {
2122
+ return false;
2123
+ }
2124
+ const jwt = new JWTAuth(room.env.AUTH_JWT_SECRET);
2125
+ try {
2126
+ const payload = await jwt.verify(token);
2127
+ if (!payload) {
2128
+ return false;
2129
+ }
2130
+ } catch (error) {
2131
+ return false;
2132
+ }
2133
+ return true;
2134
+ }, "guardManageWorld");
2135
+
2136
+ // src/world.ts
2137
+ function _ts_decorate(decorators, target, key, desc) {
2138
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
2139
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
2140
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
2141
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
2142
+ }
2143
+ __name(_ts_decorate, "_ts_decorate");
2144
+ function _ts_metadata(k, v) {
2145
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
2146
+ }
2147
+ __name(_ts_metadata, "_ts_metadata");
2148
+ var MAX_PLAYERS_PER_SHARD = 75;
2149
+ z.object({
2150
+ name: z.string(),
2151
+ balancingStrategy: z.enum([
2152
+ "round-robin",
2153
+ "least-connections",
2154
+ "random"
2155
+ ]),
2156
+ public: z.boolean(),
2157
+ maxPlayersPerShard: z.number().int().positive(),
2158
+ minShards: z.number().int().min(0),
2159
+ maxShards: z.number().int().positive().optional()
2160
+ });
2161
+ z.object({
2162
+ shardId: z.string(),
2163
+ roomId: z.string(),
2164
+ url: z.string().url(),
2165
+ maxConnections: z.number().int().positive()
2166
+ });
2167
+ z.object({
2168
+ connections: z.number().int().min(0),
2169
+ status: z.enum([
2170
+ "active",
2171
+ "maintenance",
2172
+ "draining"
2173
+ ]).optional()
2174
+ });
2175
+ z.object({
2176
+ roomId: z.string(),
2177
+ targetShardCount: z.number().int().positive(),
2178
+ shardTemplate: z.object({
2179
+ urlTemplate: z.string(),
2180
+ maxConnections: z.number().int().positive()
2181
+ }).optional()
2182
+ });
2183
+ var RoomConfig = class RoomConfig2 {
2184
+ static {
2185
+ __name(this, "RoomConfig");
2186
+ }
2187
+ id;
2188
+ name = signal("");
2189
+ balancingStrategy = signal("round-robin");
2190
+ public = signal(true);
2191
+ maxPlayersPerShard = signal(MAX_PLAYERS_PER_SHARD);
2192
+ minShards = signal(1);
2193
+ maxShards = signal(void 0);
2194
+ };
2195
+ _ts_decorate([
2196
+ id(),
2197
+ _ts_metadata("design:type", String)
2198
+ ], RoomConfig.prototype, "id", void 0);
2199
+ _ts_decorate([
2200
+ sync()
2201
+ ], RoomConfig.prototype, "name", void 0);
2202
+ _ts_decorate([
2203
+ sync()
2204
+ ], RoomConfig.prototype, "balancingStrategy", void 0);
2205
+ _ts_decorate([
2206
+ sync()
2207
+ ], RoomConfig.prototype, "public", void 0);
2208
+ _ts_decorate([
2209
+ sync()
2210
+ ], RoomConfig.prototype, "maxPlayersPerShard", void 0);
2211
+ _ts_decorate([
2212
+ sync()
2213
+ ], RoomConfig.prototype, "minShards", void 0);
2214
+ _ts_decorate([
2215
+ sync()
2216
+ ], RoomConfig.prototype, "maxShards", void 0);
2217
+ var ShardInfo = class ShardInfo2 {
2218
+ static {
2219
+ __name(this, "ShardInfo");
2220
+ }
2221
+ id;
2222
+ roomId = signal("");
2223
+ url = signal("");
2224
+ currentConnections = signal(0);
2225
+ maxConnections = signal(MAX_PLAYERS_PER_SHARD);
2226
+ status = signal("active");
2227
+ lastHeartbeat = signal(0);
2228
+ };
2229
+ _ts_decorate([
2230
+ id(),
2231
+ _ts_metadata("design:type", String)
2232
+ ], ShardInfo.prototype, "id", void 0);
2233
+ _ts_decorate([
2234
+ sync()
2235
+ ], ShardInfo.prototype, "roomId", void 0);
2236
+ _ts_decorate([
2237
+ sync()
2238
+ ], ShardInfo.prototype, "url", void 0);
2239
+ _ts_decorate([
2240
+ sync({
2241
+ persist: false
2242
+ })
2243
+ ], ShardInfo.prototype, "currentConnections", void 0);
2244
+ _ts_decorate([
2245
+ sync()
2246
+ ], ShardInfo.prototype, "maxConnections", void 0);
2247
+ _ts_decorate([
2248
+ sync()
2249
+ ], ShardInfo.prototype, "status", void 0);
2250
+ _ts_decorate([
2251
+ sync()
2252
+ ], ShardInfo.prototype, "lastHeartbeat", void 0);
2253
+ var WorldRoom = class {
2254
+ static {
2255
+ __name(this, "WorldRoom");
2256
+ }
2257
+ room;
2258
+ // Synchronized state
2259
+ rooms;
2260
+ shards;
2261
+ // Only persisted state (not synced to clients)
2262
+ rrCounters;
2263
+ // Configuration
2264
+ defaultShardUrlTemplate;
2265
+ defaultMaxConnectionsPerShard;
2266
+ constructor(room) {
2267
+ this.room = room;
2268
+ this.rooms = signal({});
2269
+ this.shards = signal({});
2270
+ this.rrCounters = signal({});
2271
+ this.defaultShardUrlTemplate = signal("{shardId}");
2272
+ this.defaultMaxConnectionsPerShard = signal(MAX_PLAYERS_PER_SHARD);
2273
+ const { AUTH_JWT_SECRET, SHARD_SECRET } = this.room.env;
2274
+ if (!AUTH_JWT_SECRET) {
2275
+ throw new Error("AUTH_JWT_SECRET env variable is not set");
2276
+ }
2277
+ if (!SHARD_SECRET) {
2278
+ throw new Error("SHARD_SECRET env variable is not set");
2279
+ }
2280
+ }
2281
+ async onJoin(user, conn, ctx) {
2282
+ const canConnect = await guardManageWorld(user, ctx.request, this.room);
2283
+ conn.setState({
2284
+ ...conn.state,
2285
+ isAdmin: canConnect
2286
+ });
2287
+ }
2288
+ interceptorPacket(_, obj, conn) {
2289
+ if (!conn.state["isAdmin"]) {
2290
+ return null;
107
2291
  }
2292
+ return obj;
108
2293
  }
109
- throw new Error("Failed to get shard after all retry attempts");
2294
+ // Helper methods
2295
+ cleanupInactiveShards() {
2296
+ const now = Date.now();
2297
+ const timeout = 5 * 60 * 1e3;
2298
+ const shardsValue = this.shards();
2299
+ Object.values(shardsValue).forEach((shard) => {
2300
+ if (now - shard.lastHeartbeat() > timeout) {
2301
+ delete this.shards()[shard.id];
2302
+ }
2303
+ });
2304
+ setTimeout(() => this.cleanupInactiveShards(), 6e4);
2305
+ }
2306
+ // Actions
2307
+ async registerRoom(req) {
2308
+ const roomConfig = await req.json();
2309
+ const roomId = roomConfig.name;
2310
+ if (!this.rooms()[roomId]) {
2311
+ const newRoom = new RoomConfig();
2312
+ newRoom.id = roomId;
2313
+ newRoom.name.set(roomConfig.name);
2314
+ newRoom.balancingStrategy.set(roomConfig.balancingStrategy);
2315
+ newRoom.public.set(roomConfig.public);
2316
+ newRoom.maxPlayersPerShard.set(roomConfig.maxPlayersPerShard);
2317
+ newRoom.minShards.set(roomConfig.minShards);
2318
+ newRoom.maxShards.set(roomConfig.maxShards);
2319
+ this.rooms()[roomId] = newRoom;
2320
+ if (roomConfig.minShards > 0) {
2321
+ for (let i = 0; i < roomConfig.minShards; i++) {
2322
+ await this.createShard(roomId);
2323
+ }
2324
+ }
2325
+ } else {
2326
+ const room = this.rooms()[roomId];
2327
+ room.balancingStrategy.set(roomConfig.balancingStrategy);
2328
+ room.public.set(roomConfig.public);
2329
+ room.maxPlayersPerShard.set(roomConfig.maxPlayersPerShard);
2330
+ room.minShards.set(roomConfig.minShards);
2331
+ room.maxShards.set(roomConfig.maxShards);
2332
+ }
2333
+ }
2334
+ async updateShardStats(req, res) {
2335
+ const body = await req.json();
2336
+ const { shardId, connections, status } = body;
2337
+ const shard = this.shards()[shardId];
2338
+ if (!shard) {
2339
+ return res.notFound(`Shard ${shardId} not found`);
2340
+ }
2341
+ shard.currentConnections.set(connections);
2342
+ if (status) {
2343
+ shard.status.set(status);
2344
+ }
2345
+ shard.lastHeartbeat.set(Date.now());
2346
+ }
2347
+ async scaleRoom(req, res) {
2348
+ const data = await req.json();
2349
+ const { targetShardCount, shardTemplate, roomId } = data;
2350
+ const room = this.rooms()[roomId];
2351
+ if (!room) {
2352
+ return res.notFound(`Room ${roomId} does not exist`);
2353
+ }
2354
+ const roomShards = Object.values(this.shards()).filter((shard) => shard.roomId() === roomId);
2355
+ const previousShardCount = roomShards.length;
2356
+ if (room.maxShards() !== void 0 && targetShardCount > room.maxShards()) {
2357
+ return res.badRequest(`Cannot scale beyond maximum allowed shards (${room.maxShards()})`, {
2358
+ roomId,
2359
+ currentShardCount: previousShardCount
2360
+ });
2361
+ }
2362
+ if (targetShardCount < previousShardCount) {
2363
+ const shardsToRemove = [
2364
+ ...roomShards
2365
+ ].sort((a, b) => {
2366
+ if (a.status() === "draining" && b.status() !== "draining") return -1;
2367
+ if (a.status() !== "draining" && b.status() === "draining") return 1;
2368
+ return a.currentConnections() - b.currentConnections();
2369
+ }).slice(0, previousShardCount - targetShardCount);
2370
+ roomShards.filter((shard) => !shardsToRemove.some((s) => s.id === shard.id));
2371
+ for (const shard of shardsToRemove) {
2372
+ delete this.shards()[shard.id];
2373
+ }
2374
+ return;
2375
+ }
2376
+ if (targetShardCount > previousShardCount) {
2377
+ for (let i = 0; i < targetShardCount - previousShardCount; i++) {
2378
+ await this.createShard(roomId, shardTemplate?.urlTemplate, shardTemplate?.maxConnections);
2379
+ }
2380
+ }
2381
+ }
2382
+ async connect(req, res) {
2383
+ try {
2384
+ let data;
2385
+ try {
2386
+ const body = await req.text();
2387
+ if (!body || body.trim() === "") {
2388
+ return res.badRequest("Request body is empty");
2389
+ }
2390
+ data = JSON.parse(body);
2391
+ } catch (parseError) {
2392
+ return res.badRequest("Invalid JSON in request body");
2393
+ }
2394
+ if (!data.roomId) {
2395
+ return res.badRequest("roomId parameter is required");
2396
+ }
2397
+ const autoCreate = data.autoCreate !== void 0 ? data.autoCreate : true;
2398
+ const result = await this.findOptimalShard(data.roomId, autoCreate);
2399
+ if ("error" in result) {
2400
+ return res.notFound(result.error);
2401
+ }
2402
+ return res.success({
2403
+ success: true,
2404
+ shardId: result.shardId,
2405
+ url: result.url
2406
+ });
2407
+ } catch (error) {
2408
+ console.error("Error connecting to shard:", error);
2409
+ return res.serverError();
2410
+ }
2411
+ }
2412
+ async findOptimalShard(roomId, autoCreate = true) {
2413
+ let room = this.rooms()[roomId];
2414
+ if (!room) {
2415
+ if (autoCreate) {
2416
+ const mockRequest = {
2417
+ json: /* @__PURE__ */ __name(async () => ({
2418
+ name: roomId,
2419
+ balancingStrategy: "round-robin",
2420
+ public: true,
2421
+ maxPlayersPerShard: this.defaultMaxConnectionsPerShard(),
2422
+ minShards: 1,
2423
+ maxShards: void 0
2424
+ }), "json")
2425
+ };
2426
+ await this.registerRoom(mockRequest);
2427
+ room = this.rooms()[roomId];
2428
+ if (!room) {
2429
+ return {
2430
+ error: `Failed to create room ${roomId}`
2431
+ };
2432
+ }
2433
+ } else {
2434
+ return {
2435
+ error: `Room ${roomId} does not exist`
2436
+ };
2437
+ }
2438
+ }
2439
+ const roomShards = Object.values(this.shards()).filter((shard) => shard.roomId() === roomId);
2440
+ if (roomShards.length === 0) {
2441
+ if (autoCreate) {
2442
+ const newShard = await this.createShard(roomId);
2443
+ if (newShard) {
2444
+ return {
2445
+ shardId: newShard.id,
2446
+ url: newShard.url()
2447
+ };
2448
+ } else {
2449
+ return {
2450
+ error: `Failed to create shard for room ${roomId}`
2451
+ };
2452
+ }
2453
+ } else {
2454
+ return {
2455
+ error: `No shards available for room ${roomId}`
2456
+ };
2457
+ }
2458
+ }
2459
+ const activeShards = roomShards.filter((shard) => shard && shard.status() === "active");
2460
+ if (activeShards.length === 0) {
2461
+ return {
2462
+ error: `No active shards available for room ${roomId}`
2463
+ };
2464
+ }
2465
+ const balancingStrategy = room.balancingStrategy();
2466
+ let selectedShard;
2467
+ switch (balancingStrategy) {
2468
+ case "least-connections":
2469
+ selectedShard = activeShards.reduce((min, shard) => shard.currentConnections() < min.currentConnections() ? shard : min, activeShards[0]);
2470
+ break;
2471
+ case "random":
2472
+ selectedShard = activeShards[Math.floor(Math.random() * activeShards.length)];
2473
+ break;
2474
+ case "round-robin":
2475
+ default:
2476
+ const counter = this.rrCounters()[roomId] || 0;
2477
+ const nextCounter = (counter + 1) % activeShards.length;
2478
+ this.rrCounters()[roomId] = nextCounter;
2479
+ selectedShard = activeShards[counter];
2480
+ break;
2481
+ }
2482
+ return {
2483
+ shardId: selectedShard.id,
2484
+ url: selectedShard.url()
2485
+ };
2486
+ }
2487
+ // Private methods
2488
+ async createShard(roomId, urlTemplate, maxConnections) {
2489
+ const room = this.rooms()[roomId];
2490
+ if (!room) {
2491
+ console.error(`Cannot create shard for non-existent room: ${roomId}`);
2492
+ return null;
2493
+ }
2494
+ const shardId = `${roomId}:${Date.now()}-${Math.floor(Math.random() * 1e4)}`;
2495
+ const template = urlTemplate || this.defaultShardUrlTemplate();
2496
+ const url = template.replace("{shardId}", shardId).replace("{roomId}", roomId);
2497
+ const max = maxConnections || room.maxPlayersPerShard();
2498
+ const newShard = new ShardInfo();
2499
+ newShard.id = shardId;
2500
+ newShard.roomId.set(roomId);
2501
+ newShard.url.set(url);
2502
+ newShard.maxConnections.set(max);
2503
+ newShard.currentConnections.set(0);
2504
+ newShard.status.set("active");
2505
+ newShard.lastHeartbeat.set(Date.now());
2506
+ this.shards()[shardId] = newShard;
2507
+ return newShard;
2508
+ }
2509
+ };
2510
+ _ts_decorate([
2511
+ sync(RoomConfig)
2512
+ ], WorldRoom.prototype, "rooms", void 0);
2513
+ _ts_decorate([
2514
+ sync(ShardInfo)
2515
+ ], WorldRoom.prototype, "shards", void 0);
2516
+ _ts_decorate([
2517
+ persist()
2518
+ ], WorldRoom.prototype, "rrCounters", void 0);
2519
+ _ts_decorate([
2520
+ Request2({
2521
+ path: "register-room",
2522
+ method: "POST"
2523
+ }),
2524
+ Guard([
2525
+ guardManageWorld
2526
+ ]),
2527
+ _ts_metadata("design:type", Function),
2528
+ _ts_metadata("design:paramtypes", [
2529
+ Object
2530
+ ]),
2531
+ _ts_metadata("design:returntype", Promise)
2532
+ ], WorldRoom.prototype, "registerRoom", null);
2533
+ _ts_decorate([
2534
+ Request2({
2535
+ path: "update-shard",
2536
+ method: "POST"
2537
+ }),
2538
+ Guard([
2539
+ guardManageWorld
2540
+ ]),
2541
+ _ts_metadata("design:type", Function),
2542
+ _ts_metadata("design:paramtypes", [
2543
+ Object ,
2544
+ typeof ServerResponse === "undefined" ? Object : ServerResponse
2545
+ ]),
2546
+ _ts_metadata("design:returntype", Promise)
2547
+ ], WorldRoom.prototype, "updateShardStats", null);
2548
+ _ts_decorate([
2549
+ Request2({
2550
+ path: "scale-room",
2551
+ method: "POST"
2552
+ }),
2553
+ Guard([
2554
+ guardManageWorld
2555
+ ]),
2556
+ _ts_metadata("design:type", Function),
2557
+ _ts_metadata("design:paramtypes", [
2558
+ Object ,
2559
+ typeof ServerResponse === "undefined" ? Object : ServerResponse
2560
+ ]),
2561
+ _ts_metadata("design:returntype", Promise)
2562
+ ], WorldRoom.prototype, "scaleRoom", null);
2563
+ _ts_decorate([
2564
+ Request2({
2565
+ path: "connect",
2566
+ method: "POST"
2567
+ }),
2568
+ _ts_metadata("design:type", Function),
2569
+ _ts_metadata("design:paramtypes", [
2570
+ Object ,
2571
+ typeof ServerResponse === "undefined" ? Object : ServerResponse
2572
+ ]),
2573
+ _ts_metadata("design:returntype", Promise)
2574
+ ], WorldRoom.prototype, "connect", null);
2575
+ WorldRoom = _ts_decorate([
2576
+ Room({
2577
+ path: "world-{worldId}",
2578
+ maxUsers: 100,
2579
+ throttleStorage: 2e3,
2580
+ throttleSync: 500
2581
+ }),
2582
+ _ts_metadata("design:type", Function),
2583
+ _ts_metadata("design:paramtypes", [
2584
+ Object
2585
+ ])
2586
+ ], WorldRoom);
2587
+
2588
+ // src/session.guard.ts
2589
+ function createRequireSessionGuard(storage) {
2590
+ return async (sender, value) => {
2591
+ if (!sender || !sender.id) {
2592
+ return false;
2593
+ }
2594
+ try {
2595
+ const session = await storage.get(`session:${sender.id}`);
2596
+ if (!session) {
2597
+ return false;
2598
+ }
2599
+ const typedSession = session;
2600
+ if (!typedSession.publicId) {
2601
+ return false;
2602
+ }
2603
+ return true;
2604
+ } catch (error) {
2605
+ console.error("Error checking session in requireSession guard:", error);
2606
+ return false;
2607
+ }
2608
+ };
110
2609
  }
111
- __name(getOptimalShard, "getOptimalShard");
2610
+ __name(createRequireSessionGuard, "createRequireSessionGuard");
112
2611
 
113
- export { PartySocket, connectionRoom, connectionWorld };
2612
+ export { Action, ClientIo, Guard, MockConnection, Request2 as Request, Room, RoomGuard, Server, ServerIo, ServerResponse, Shard, WorldRoom, createRequireSessionGuard, request, testRoom };
114
2613
  //# sourceMappingURL=index22.js.map