@caisual/kit 0.15.0 → 0.16.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.
package/dist/node.js CHANGED
@@ -45,6 +45,12 @@ function modalitaLocale(manifest, mode) {
45
45
  var MASSIMO_SPETTATORI = 100;
46
46
  var RITARDO_SPETTATORI_MS = 3e3;
47
47
 
48
+ // ../contracts/src/replay.ts
49
+ var REPLAY_MAX_BYTES = 10 * 1024 * 1024;
50
+ var REPLAY_MAX_DURATION_MS = 30 * 60 * 1e3;
51
+ var REPLAY_CHUNK_BYTES = 512 * 1024;
52
+ var REPLAY_RETENTION_MS = 30 * 24 * 60 * 60 * 1e3;
53
+
48
54
  // ../contracts/src/overlay.ts
49
55
  function validBoardDay(value) {
50
56
  if (typeof value !== "string" || !/^\d{4}-\d{2}-\d{2}$/.test(value)) return false;
@@ -53,8 +59,9 @@ function validBoardDay(value) {
53
59
  }
54
60
 
55
61
  // ../contracts/src/room-limits.ts
56
- var MESSAGGI_GIOCO_AL_SECONDO = 20;
57
- var MESSAGGI_SERVIZIO_AL_SECONDO = 20;
62
+ var MASSIMO_BYTE_FRAME_STANZA = 64 * 1024;
63
+ var MESSAGGI_GIOCO_AL_SECONDO = 30;
64
+ var MESSAGGI_SERVIZIO_AL_SECONDO = 30;
58
65
  var LimiteMessaggiStanza = class {
59
66
  connessioni = /* @__PURE__ */ new Map();
60
67
  delete(connessione) {
@@ -335,10 +342,10 @@ function seedStanza(roomId) {
335
342
  // src/stanza/nucleo.ts
336
343
  var CHIAVE_NUCLEO = "nucleo";
337
344
  var PREFISSO_SAVE = "save:";
338
- var LIMITE_FRAME = 16 * 1024;
339
- var LIMITE_STATO = 256 * 1024;
340
- var LIMITE_SAVE = 128 * 1024;
341
- var LIMITE_DEPOSITO = 64 * 1024;
345
+ var LIMITE_FRAME = MASSIMO_BYTE_FRAME_STANZA;
346
+ var LIMITE_STATO = 512 * 1024;
347
+ var LIMITE_SAVE = 256 * 1024;
348
+ var LIMITE_DEPOSITO = 256 * 1024;
342
349
  var LIMITE_OPERAZIONI_DEPOSITO = 120;
343
350
  var GRAZIA_MS = 6e4;
344
351
  var STANZA_VUOTA_MS = 5 * 6e4;
@@ -348,6 +355,10 @@ var GRAZIA_GIORNALIERA_MS = 10 * 6e4;
348
355
  var RIPOSO_TICK_MS = 3e4;
349
356
  var INATTIVITA_MS = 10 * 6e4;
350
357
  var SCADENZA_PERSISTENTE_MS = 30 * 24 * 60 * 6e4;
358
+ var BLOCCO_INATTIVITA_MS = 3e4;
359
+ function scadenzaInattivita(ultimoEvento, durata) {
360
+ return Math.ceil((ultimoEvento + durata) / BLOCCO_INATTIVITA_MS) * BLOCCO_INATTIVITA_MS;
361
+ }
351
362
  var CHIAVE = /^[a-z0-9][a-z0-9_-]{0,31}$/;
352
363
  var PREFISSO_CHIAVE = /^[a-z0-9_-]{0,32}$/;
353
364
  var GAME_DEFINITION = /* @__PURE__ */ Symbol.for("@caisual/kit/game-definition");
@@ -725,6 +736,7 @@ var NucleoStanza = class _NucleoStanza {
725
736
  }
726
737
  if (primaConnessione) {
727
738
  await this.chiama(this.definizione.onStart, this.room);
739
+ this.iniziaReplay();
728
740
  this.inviaStatus(ora);
729
741
  }
730
742
  await this.concludiEvento();
@@ -944,6 +956,7 @@ var NucleoStanza = class _NucleoStanza {
944
956
  this.dati.ultimoInputAt = ora;
945
957
  this.dati.countdownAt = null;
946
958
  await this.chiama(this.definizione.onStart, this.room);
959
+ this.iniziaReplay();
947
960
  this.inviaStatus(ora);
948
961
  }
949
962
  }
@@ -1168,6 +1181,7 @@ var NucleoStanza = class _NucleoStanza {
1168
1181
  await this.applicaAzioni();
1169
1182
  if (dati.status === "playing") {
1170
1183
  await this.chiama(this.definizione.onStart, this.room);
1184
+ this.iniziaReplay();
1171
1185
  }
1172
1186
  await this.concludiEvento();
1173
1187
  if (dati.status === "lobby" || dati.status === "playing" || dati.status === "countdown") {
@@ -1321,7 +1335,7 @@ var NucleoStanza = class _NucleoStanza {
1321
1335
  const json = analizzaJson(value);
1322
1336
  if (!json.ok) throw new TypeError("Saved values must be valid JSON.");
1323
1337
  if (json.bytes > LIMITE_SAVE) {
1324
- throw new RangeError("Saved values must be at most 131072 bytes.");
1338
+ throw new RangeError("Saved values must be at most 262144 bytes.");
1325
1339
  }
1326
1340
  await this.adattatore.storage.put(PREFISSO_SAVE + key, json.valore);
1327
1341
  }
@@ -1392,7 +1406,7 @@ var NucleoStanza = class _NucleoStanza {
1392
1406
  if (!json.ok || json.bytes > LIMITE_DEPOSITO) {
1393
1407
  throw erroreConCodice(
1394
1408
  "store_too_large",
1395
- "Shared store values must be valid JSON of at most 65536 bytes."
1409
+ "Shared store values must be valid JSON of at most 262144 bytes."
1396
1410
  );
1397
1411
  }
1398
1412
  await this.usaDeposito((deposito) => deposito.set(key, json.valore));
@@ -1407,7 +1421,7 @@ var NucleoStanza = class _NucleoStanza {
1407
1421
  if (!Array.isArray(keys) || keys.some((key) => typeof key !== "string" || !CHIAVE.test(key))) {
1408
1422
  throw erroreConCodice("store_unavailable", "The shared store returned invalid keys.");
1409
1423
  }
1410
- return [...keys].sort().slice(0, 1024);
1424
+ return [...keys].sort().slice(0, 4096);
1411
1425
  }
1412
1426
  async incrementaDeposito(key, amount) {
1413
1427
  this.verificaChiaveDeposito(key);
@@ -1420,18 +1434,24 @@ var NucleoStanza = class _NucleoStanza {
1420
1434
  }
1421
1435
  return value;
1422
1436
  }
1423
- broadcastCreatore(message) {
1437
+ messaggioCreatore(message) {
1424
1438
  const json = analizzaJson(message);
1425
1439
  if (!json.ok) throw new TypeError("Messages must be valid JSON.");
1426
- this.broadcast({ t: "msg", m: json.valore });
1440
+ const frame = { t: "msg", m: json.valore };
1441
+ if (new TextEncoder().encode(JSON.stringify(frame)).byteLength > LIMITE_FRAME) {
1442
+ throw new RangeError("Game message frames must be at most 65536 bytes.");
1443
+ }
1444
+ return frame;
1445
+ }
1446
+ broadcastCreatore(message) {
1447
+ this.broadcast(this.messaggioCreatore(message));
1427
1448
  }
1428
1449
  inviaCreatore(playerId, message) {
1429
- const json = analizzaJson(message);
1430
- if (!json.ok) throw new TypeError("Messages must be valid JSON.");
1450
+ const frame = this.messaggioCreatore(message);
1431
1451
  const player = this.richiediDati().giocatori.find((item) => item.id === playerId);
1432
1452
  if (player === void 0) throw new Error("Player not found.");
1433
1453
  if (player.connected && player.connessione !== null) {
1434
- this.adattatore.invia(player.connessione, { t: "msg", m: json.valore });
1454
+ this.adattatore.invia(player.connessione, frame);
1435
1455
  }
1436
1456
  }
1437
1457
  broadcast(message) {
@@ -1526,6 +1546,13 @@ var NucleoStanza = class _NucleoStanza {
1526
1546
  dati.statoSincronizzato = copiaJson(dati.state);
1527
1547
  dati.tickSincronizzato = dati.tick;
1528
1548
  }
1549
+ iniziaReplay() {
1550
+ if (!this.manifest.replays || !this.adattatore.inizioReplay) return;
1551
+ const json = analizzaJson(this.richiediDati().state);
1552
+ if (!json.ok || json.bytes > LIMITE_STATO) return;
1553
+ this.inviaSnapshotTutti();
1554
+ this.adattatore.inizioReplay(this.fotografia());
1555
+ }
1529
1556
  inviaSnapshotTutti() {
1530
1557
  const dati = this.richiediDati();
1531
1558
  const stato = analizzaJson(dati.state);
@@ -1782,10 +1809,13 @@ var NucleoStanza = class _NucleoStanza {
1782
1809
  if (this.dati.status === "finished") prossime.push(this.dati.rivincitaFinoA ?? this.adattatore.ora());
1783
1810
  if (this.manifest.persistent === true) {
1784
1811
  prossime.push(
1785
- Math.max(this.dati.ultimoInputAt, this.dati.ultimoCambioStatoAt) + SCADENZA_PERSISTENTE_MS
1812
+ scadenzaInattivita(
1813
+ Math.max(this.dati.ultimoInputAt, this.dati.ultimoCambioStatoAt),
1814
+ SCADENZA_PERSISTENTE_MS
1815
+ )
1786
1816
  );
1787
1817
  } else if (this.dati.status === "playing") {
1788
- prossime.push(this.dati.ultimoInputAt + INATTIVITA_MS);
1818
+ prossime.push(scadenzaInattivita(this.dati.ultimoInputAt, INATTIVITA_MS));
1789
1819
  }
1790
1820
  if (this.dati.countdownAt !== null) prossime.push(this.dati.countdownAt);
1791
1821
  for (const player of this.dati.giocatori) {
@@ -2343,7 +2373,7 @@ var StanzaNode = class {
2343
2373
  return;
2344
2374
  }
2345
2375
  const message = typeof value === "object" && value !== null && !Array.isArray(value) ? value : null;
2346
- if (Buffer.byteLength(frame, "utf8") > (message?.t === "voice" ? 64 : 16) * 1024) {
2376
+ if (Buffer.byteLength(frame, "utf8") > MASSIMO_BYTE_FRAME_STANZA) {
2347
2377
  this.adattatore.chiudi(connessione, 4009, "message_too_large");
2348
2378
  this.rimuoviConnessione(connessione);
2349
2379
  await this.nucleo.disconnetti(connessione);
@@ -2390,7 +2420,7 @@ var StanzaNode = class {
2390
2420
  }));
2391
2421
  }
2392
2422
  async riceviSpettatore(connessione, frame) {
2393
- if (Buffer.byteLength(frame, "utf8") > 16 * 1024) {
2423
+ if (Buffer.byteLength(frame, "utf8") > MASSIMO_BYTE_FRAME_STANZA) {
2394
2424
  this.adattatore.chiudiSpettatore(connessione, 4009, "message_too_large");
2395
2425
  this.frameFrequenza.delete(connessione);
2396
2426
  return;