@signe/room 2.4.0 → 2.4.2

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.
package/dist/index.d.ts CHANGED
@@ -562,12 +562,16 @@ declare class MockPartyClient {
562
562
  _trigger(event: any, data: any): void;
563
563
  send(data: any): Promise<void>;
564
564
  }
565
+ interface MockContextOptions {
566
+ parties?: any;
567
+ partyFn?: (room: MockPartyRoom) => any;
568
+ }
565
569
  declare class MockContext {
566
570
  room: MockPartyRoom;
567
571
  parties: {
568
- main: Map<string, any>;
572
+ main: any;
569
573
  };
570
- constructor(room: MockPartyRoom, options?: any);
574
+ constructor(room: MockPartyRoom, options?: MockContextOptions);
571
575
  }
572
576
  declare class MockPartyRoom {
573
577
  id?: string;
@@ -576,7 +580,10 @@ declare class MockPartyRoom {
576
580
  context: MockContext;
577
581
  env: {};
578
582
  constructor(id?: string, options?: any);
579
- connection(server: Server, id?: string): Promise<MockPartyClient>;
583
+ connection(server: Server, id?: string, opts?: {
584
+ query?: Record<string, string>;
585
+ headers?: Record<string, string>;
586
+ }): Promise<MockPartyClient>;
580
587
  broadcast(data: any): void;
581
588
  getConnection(id: string): MockPartyClient;
582
589
  getConnections(): MockConnection[];
@@ -668,10 +675,15 @@ declare function testRoom(Room: any, options?: {
668
675
  hibernate?: boolean;
669
676
  shard?: boolean;
670
677
  env?: Record<string, string>;
678
+ parties?: Record<string, (io: any) => any>;
679
+ partyFn?: (io: any) => any;
671
680
  }): Promise<{
672
681
  server: Server | Shard;
673
682
  room: any;
674
- createClient: (id?: string) => Promise<MockPartyClient>;
683
+ createClient: (id?: string, opts?: {
684
+ query?: Record<string, string>;
685
+ headers?: Record<string, string>;
686
+ }) => Promise<MockPartyClient>;
675
687
  }>;
676
688
  declare function request(room: Server | Shard, path: string, options?: {
677
689
  method: 'GET' | 'POST' | 'PUT' | 'DELETE';
package/dist/index.js CHANGED
@@ -686,7 +686,7 @@ var Server = class {
686
686
  publicId
687
687
  };
688
688
  try {
689
- const targetRoomParty = this.room.context.parties.main.get(targetRoomId);
689
+ const targetRoomParty = await this.room.context.parties.main.get(targetRoomId);
690
690
  const response2 = await targetRoomParty.fetch("/session-transfer", {
691
691
  method: "POST",
692
692
  body: JSON.stringify(transferData),
@@ -1755,10 +1755,14 @@ async function testRoom(Room3, options = {}) {
1755
1755
  const isShard = options.shard || false;
1756
1756
  const io = new ServerIo(Room3.path, isShard ? {
1757
1757
  parties: {
1758
- game: createServer
1758
+ game: createServer,
1759
+ ...options.parties || {}
1759
1760
  },
1761
+ partyFn: options.partyFn,
1760
1762
  env: options.env
1761
1763
  } : {
1764
+ parties: options.parties,
1765
+ partyFn: options.partyFn,
1762
1766
  env: options.env
1763
1767
  });
1764
1768
  Room3.prototype.throttleSync = 0;
@@ -1769,18 +1773,27 @@ async function testRoom(Room3, options = {}) {
1769
1773
  const shardServer = new Shard(io);
1770
1774
  shardServer.subRoom = null;
1771
1775
  server = shardServer;
1772
- for (const lobby of io.context.parties.main.values()) {
1773
- await lobby.server.onStart();
1776
+ if (io.context.parties.main instanceof Map) {
1777
+ for (const lobby of io.context.parties.main.values()) {
1778
+ await lobby.server.onStart();
1779
+ }
1774
1780
  }
1775
1781
  } else {
1776
1782
  server = await createServer(io);
1783
+ if (io.context.parties.main instanceof Map) {
1784
+ for (const lobby of io.context.parties.main.values()) {
1785
+ if (lobby.server && lobby.server !== server) {
1786
+ await lobby.server.onStart();
1787
+ }
1788
+ }
1789
+ }
1777
1790
  }
1778
1791
  await server.onStart();
1779
1792
  return {
1780
1793
  server,
1781
1794
  room: server.subRoom,
1782
- createClient: /* @__PURE__ */ __name(async (id2) => {
1783
- const client = await io.connection(server, id2);
1795
+ createClient: /* @__PURE__ */ __name(async (id2, opts) => {
1796
+ const client = await io.connection(server, id2, opts);
1784
1797
  return client;
1785
1798
  }, "createClient")
1786
1799
  };
@@ -1845,14 +1858,22 @@ var MockLobby = class MockLobby2 {
1845
1858
  __name(this, "MockLobby");
1846
1859
  }
1847
1860
  server;
1848
- constructor(server) {
1861
+ lobbyId;
1862
+ constructor(server, lobbyId) {
1849
1863
  this.server = server;
1864
+ this.lobbyId = lobbyId;
1850
1865
  }
1851
- socket() {
1866
+ socket(_init) {
1852
1867
  return new MockPartyClient(this.server);
1853
1868
  }
1869
+ async connection(idOrOptions, maybeOptions) {
1870
+ const id2 = typeof idOrOptions === "string" ? idOrOptions : idOrOptions?.id;
1871
+ const options = (typeof idOrOptions === "string" ? maybeOptions : idOrOptions) || {};
1872
+ return this.server.room.connection(this.server, id2, options);
1873
+ }
1854
1874
  fetch(url, options) {
1855
- return request(this.server, url, options);
1875
+ const baseUrl = url.includes("shard") ? "" : "/parties/main/" + this.lobbyId;
1876
+ return request(this.server, baseUrl + url, options);
1856
1877
  }
1857
1878
  };
1858
1879
  var MockContext = class MockContext2 {
@@ -1867,8 +1888,29 @@ var MockContext = class MockContext2 {
1867
1888
  main: /* @__PURE__ */ new Map()
1868
1889
  };
1869
1890
  const parties = options.parties || {};
1870
- for (let lobbyId in parties) {
1871
- this.parties.main.set(lobbyId, new MockLobby(parties[lobbyId](room)));
1891
+ if (options.partyFn) {
1892
+ const serverCache = /* @__PURE__ */ new Map();
1893
+ this.parties.main = {
1894
+ get: /* @__PURE__ */ __name(async (lobbyId) => {
1895
+ if (!serverCache.has(lobbyId)) {
1896
+ const io = new MockPartyRoom(lobbyId, {
1897
+ env: this.room.env
1898
+ });
1899
+ const server2 = options.partyFn(io);
1900
+ if (typeof server2.onStart === "function") {
1901
+ await server2.onStart();
1902
+ }
1903
+ serverCache.set(lobbyId, server2);
1904
+ }
1905
+ const server = serverCache.get(lobbyId);
1906
+ return new MockLobby(server, lobbyId);
1907
+ }, "get")
1908
+ };
1909
+ } else {
1910
+ for (let lobbyId in parties) {
1911
+ const server = parties[lobbyId](room);
1912
+ this.parties.main.set(lobbyId, new MockLobby(server, lobbyId));
1913
+ }
1872
1914
  }
1873
1915
  }
1874
1916
  };
@@ -1888,17 +1930,24 @@ var MockPartyRoom = class MockPartyRoom2 {
1888
1930
  this.env = {};
1889
1931
  this.id = id2 || generateShortUUID();
1890
1932
  this.context = new MockContext(this, {
1891
- parties: options.parties || {}
1933
+ parties: options.parties,
1934
+ partyFn: options.partyFn
1892
1935
  });
1893
1936
  this.env = options.env || {};
1894
1937
  }
1895
- async connection(server, id2) {
1938
+ async connection(server, id2, opts) {
1896
1939
  const socket = new MockPartyClient(server, id2);
1897
1940
  const url = new URL("http://localhost");
1941
+ if (opts?.query) {
1942
+ for (const [key, value] of Object.entries(opts.query)) {
1943
+ url.searchParams.set(key, String(value));
1944
+ }
1945
+ }
1898
1946
  const request2 = new Request(url.toString(), {
1899
1947
  method: "GET",
1900
1948
  headers: {
1901
- "Content-Type": "application/json"
1949
+ "Content-Type": "application/json",
1950
+ ...opts?.headers || {}
1902
1951
  }
1903
1952
  });
1904
1953
  await server.onConnect(socket.conn, {