@zaisma/game-server 1.36.0 → 1.38.0

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.
@@ -4,7 +4,8 @@
4
4
  import { createServer } from "node:net";
5
5
  import { pathToFileURL } from "node:url";
6
6
  import { ROOM_MESSAGE_LIMITS, checkRoomData, isRoomCode, relay } from "../src/internal.js";
7
- import { localDatabase } from "./node-parts.js";
7
+ import { dirname } from "node:path";
8
+ import { loadWasmAsModules, localDatabase } from "./node-parts.js";
8
9
  import { startServer } from "./node.js";
9
10
 
10
11
  // How startServer hands this runner its way to send room messages as the developer (node.js).
@@ -18,6 +19,7 @@ try {
18
19
  probe.once("error", (error) => reject(error?.code === "EADDRINUSE" ? new Error(`Port ${config.port} is already in use, so your game server can't start. Stop what's using it, or start the bench with another --port.`) : error));
19
20
  probe.listen(config.port, () => probe.close(resolve));
20
21
  });
22
+ if (config.entry) loadWasmAsModules(dirname(config.entry));
21
23
  const gameServer = config.entry ? (await import(pathToFileURL(config.entry).href)).default : relay;
22
24
  const running = startServer({
23
25
  gameServer,
package/bin/local-play.js CHANGED
@@ -1476,9 +1476,11 @@ export async function startLocalPlay(givenOptions) {
1476
1476
  forget();
1477
1477
  if (client.readyState === WebSocket.OPEN) client.close(BUSY_CLOSE_CODE, "busy");
1478
1478
  });
1479
- client.on("close", () => {
1479
+ // A player leaving on purpose (1000, 1001) reaches the game server as leaving, so onLeave runs at once; anything else is a drop.
1480
+ client.on("close", (clientCloseCode, reason) => {
1480
1481
  forget();
1481
- upstream.close();
1482
+ if (clientCloseCode === 1000 || clientCloseCode === 1001) upstream.close(clientCloseCode, reason.toString().slice(0, 120));
1483
+ else upstream.close();
1482
1484
  });
1483
1485
  client.on("error", () => upstream.terminate());
1484
1486
  }
package/bin/node-parts.js CHANGED
@@ -3,7 +3,9 @@
3
3
  // public package: tests reach it by path.
4
4
  import { randomUUID } from "node:crypto";
5
5
  import { existsSync, readFileSync, realpathSync, statSync } from "node:fs";
6
+ import { registerHooks } from "node:module";
6
7
  import { resolve, sep } from "node:path";
8
+ import { pathToFileURL } from "node:url";
7
9
  import { DATABASE_LIMITS, GameServerError, answerDatabase, checkBatch, checkPlayerId, checkStatement, checkedRow, eraseRefused, eraseStatements, readDatabaseAnswer, tablesFromColumns, tooManyRows, TABLE_COLUMNS_SQL } from "../src/internal.js";
8
10
 
9
11
  /** How long the pass keys and the profiles of accounts are kept before they're fetched again. */
@@ -450,3 +452,24 @@ export function dataFilesFrom(dataFolder) {
450
452
  return found;
451
453
  };
452
454
  }
455
+
456
+ /**
457
+ * Makes `import mod from "./x.wasm"` inside the game server's folder give a WebAssembly.Module, as on Zaisma's rooms,
458
+ * instead of Node's own instance exports (which also fail on a module with imports, like wasm-bindgen's).
459
+ * Called once, before the game server is imported.
460
+ */
461
+ export function loadWasmAsModules(gameServerFolder) {
462
+ // Node loads a module from its real path (macOS's /var is /private/var), so the folder is compared by its real path too.
463
+ const realFolder = realpathSync(gameServerFolder);
464
+ const folderUrl = pathToFileURL(realFolder.endsWith(sep) ? realFolder : realFolder + sep).href;
465
+ registerHooks({
466
+ load(url, loadContext, nextLoad) {
467
+ if (!url.startsWith(folderUrl) || !new URL(url).pathname.endsWith(".wasm")) return nextLoad(url, loadContext);
468
+ return {
469
+ format: "module",
470
+ shortCircuit: true,
471
+ source: `import { readFileSync } from "node:fs";\nexport default new WebAssembly.Module(readFileSync(new URL(${JSON.stringify(url)})));\n`,
472
+ };
473
+ },
474
+ });
475
+ }
package/bin/node.js CHANGED
@@ -29,7 +29,7 @@ import { readFileSync, realpathSync } from "node:fs";
29
29
  import { roomMessageProblem, BUSY_CLOSE_CODE, ENDED_CLOSE_CODE, ERROR_TEXT, NO_ANSWER_CLOSE_CODE, PASS_ISSUER_URL, OUTSIDE_ZAISMA_CHAT_SETTINGS, PassError, ReceiptError, REMOVED_CLOSE_CODE, REQUEST_ERROR_HTTP_STATUS, REQUEST_LIMITS, ROOM_LIMITS, ROOM_MESSAGE_LIMITS, ROOM_TUNING, WEBHOOK_LIMITS, GameServerError, RoomCore, chatSettingsOf, checkChat, readChatMessage, restoreInfoOf, passesAreFriends, passesHaveBlockBetween, answerRequest, startSchedules, checkRoomData, checkedOutsidePlayer, clientIdle, defineGameServer, errorMessage, guestPlayer, newServer, isPlayerToken, isRoomCode, readRequestBody, relay, requestError, runWebhook, tidyRoomInfo, verifyPass, verifyReceipt, verifyWebhook } from "../src/internal.js";
30
30
  import { OutsideCallError } from "../src/outside-calls.js";
31
31
  import { startFastLane } from "./fast-lane.js";
32
- import { checkedStartServerOptions, dataFilesFrom, localDatabase, localRoomStates, passKeysFrom, profilesFrom, receiptKeysFrom, secretsFromEnvironment } from "./node-parts.js";
32
+ import { checkedStartServerOptions, dataFilesFrom, loadWasmAsModules, localDatabase, localRoomStates, passKeysFrom, profilesFrom, receiptKeysFrom, secretsFromEnvironment } from "./node-parts.js";
33
33
 
34
34
  // Code that names OutsideCallError, as Zaisma's sandbox lets it, moves unchanged: nothing here refuses a call, so instanceof is simply false.
35
35
  globalThis.OutsideCallError ??= OutsideCallError;
@@ -1000,6 +1000,7 @@ async function runCommand() {
1000
1000
  console.log(`The fast lane needs ${missingFastLaneFlags.map((name) => `--${name}`).join(" and ")} too: give all three of ${FAST_LANE_FLAGS.map((name) => `--${name}`).join(", ")}, or none.\n${USAGE}`);
1001
1001
  process.exit(1);
1002
1002
  }
1003
+ if (file !== "relay") loadWasmAsModules(dirname(resolve(file)));
1003
1004
  const gameServer = file === "relay" ? relay : (await import(pathToFileURL(resolve(file)).href)).default;
1004
1005
  const portNumber = Number(flags.port ?? "8080");
1005
1006
  let running;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zaisma/game-server",
3
- "version": "1.36.0",
3
+ "version": "1.38.0",
4
4
  "description": "Run your Zaisma game server: the same rooms on Zaisma or on your own server.",
5
5
  "license": "MIT",
6
6
  "homepage": "https://zaisma.com",
package/sdk/zaisma.d.ts CHANGED
@@ -1759,7 +1759,8 @@ declare namespace Zaisma {
1759
1759
 
1760
1760
  /**
1761
1761
  * The codes the SDK's errors have: a room's ({@link RoomErrorCode}), and:
1762
- * - unchecked: no player pass, because the game isn't played on its zaisma.com page or Zaisma couldn't check the browser.
1762
+ * - unchecked: no player pass, because the game isn't played on its zaisma.com page, Zaisma couldn't check the browser, or it's a preview
1763
+ * of a version players don't have yet (its message says so: test online play with play-locally or a tester link).
1763
1764
  * - unknown_option, unknown_event: a call the SDK doesn't take; the message lists what it does.
1764
1765
  * - slow_down: too many calls; try again in a minute.
1765
1766
  * - zaisma_rooms_not_used: the game doesn't use Zaisma rooms.
package/sdk/zaisma.js CHANGED
@@ -210,7 +210,8 @@
210
210
  removeEventListener("message", onMessage);
211
211
  clearTimeout(timer);
212
212
  if (typeof event.data.key === "string") resolve({ key: event.data.key, playerKind: event.data.playerKind === "account" ? "account" : "guest" });
213
- else fail(UNCHECKED);
213
+ // The page may say why it has no key (a preview of a version players don't have yet), instead of the browser check.
214
+ else fail(typeof event.data.message === "string" && event.data.message ? event.data.message.slice(0, 300) : UNCHECKED);
214
215
  }
215
216
  addEventListener("message", onMessage);
216
217
  window.parent.postMessage({ type: "zaisma:room-key" }, config.site);
package/src/room.d.ts CHANGED
@@ -802,10 +802,15 @@ export declare const GAME_SERVER_OPTIONS: readonly ["maximumPlayerCount", "maxim
802
802
  */
803
803
  export declare const GAME_SERVER_LIMITS: {
804
804
  /**
805
- * Every file of the game server together, code and data files. Cloudflare takes a Worker of 64 MB, but a room holds the
806
- * files in memory (128 MB, shared) while it hands them to the sandbox, so half of that is what a room can carry safely.
805
+ * Every file of the game server together, code and data files. The data files stream from storage straight into the
806
+ * game server's own memory (maximumMemorySizeBytes), so this leaves half of it for the runtime and the rooms' state.
807
807
  */
808
808
  readonly maximumSizeBytes: number;
809
+ /**
810
+ * The code alone (.js, .json and .wasm): a room holds it in its memory (128 MB, shared with other rooms) while it hands
811
+ * it to the sandbox, so this is what a room carries safely.
812
+ */
813
+ readonly maximumCodeSizeBytes: number;
809
814
  /** Files in the zaisma-server/ folder: each is read once whenever a copy of the game server loads. */
810
815
  readonly maximumFileCount: 500;
811
816
  /** CPU for one call: a tick with the messages since the last one, a join, a request, a schedule's run. */
package/src/room.js CHANGED
@@ -141,10 +141,15 @@ export const GAME_SERVER_OPTIONS = [
141
141
  */
142
142
  export const GAME_SERVER_LIMITS = {
143
143
  /**
144
- * Every file of the game server together, code and data files. Cloudflare takes a Worker of 64 MB, but a room holds the
145
- * files in memory (128 MB, shared) while it hands them to the sandbox, so half of that is what a room can carry safely.
144
+ * Every file of the game server together, code and data files. The data files stream from storage straight into the
145
+ * game server's own memory (maximumMemorySizeBytes), so this leaves half of it for the runtime and the rooms' state.
146
146
  */
147
- maximumSizeBytes: 32 * 1024 * 1024,
147
+ maximumSizeBytes: 64 * 1024 * 1024,
148
+ /**
149
+ * The code alone (.js, .json and .wasm): a room holds it in its memory (128 MB, shared with other rooms) while it hands
150
+ * it to the sandbox, so this is what a room carries safely.
151
+ */
152
+ maximumCodeSizeBytes: 32 * 1024 * 1024,
148
153
  /** Files in the zaisma-server/ folder: each is read once whenever a copy of the game server loads. */
149
154
  maximumFileCount: 500,
150
155
  /** CPU for one call: a tick with the messages since the last one, a join, a request, a schedule's run. */