@caisual/kit 0.15.0 → 0.20.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/runtime.js CHANGED
@@ -1,8 +1,3 @@
1
- // src/daily.ts
2
- function prossimaMezzanotteUtc(ora) {
3
- return (Math.floor(ora / 864e5) + 1) * 864e5;
4
- }
5
-
6
1
  // ../contracts/src/slug.ts
7
2
  var NOMI_RISERVATI = [
8
3
  "www",
@@ -48,9 +43,16 @@ function modalitaLocale(manifest, mode) {
48
43
  return mode !== null && manifest.modes.some((voce) => voce.id === mode && voce.execution === "local");
49
44
  }
50
45
 
46
+ // ../contracts/src/replay.ts
47
+ var REPLAY_MAX_BYTES = 10 * 1024 * 1024;
48
+ var REPLAY_MAX_DURATION_MS = 30 * 60 * 1e3;
49
+ var REPLAY_CHUNK_BYTES = 512 * 1024;
50
+ var REPLAY_RETENTION_MS = 30 * 24 * 60 * 60 * 1e3;
51
+
51
52
  // ../contracts/src/room-limits.ts
52
- var MESSAGGI_GIOCO_AL_SECONDO = 20;
53
- var MESSAGGI_SERVIZIO_AL_SECONDO = 20;
53
+ var MASSIMO_BYTE_FRAME_STANZA = 64 * 1024;
54
+ var MESSAGGI_GIOCO_AL_SECONDO = 30;
55
+ var MESSAGGI_SERVIZIO_AL_SECONDO = 30;
54
56
  var LimiteMessaggiStanza = class {
55
57
  connessioni = /* @__PURE__ */ new Map();
56
58
  delete(connessione) {
@@ -89,6 +91,11 @@ var LimiteMessaggiStanza = class {
89
91
  }
90
92
  };
91
93
 
94
+ // src/daily.ts
95
+ function prossimaMezzanotteUtc(ora) {
96
+ return (Math.floor(ora / 864e5) + 1) * 864e5;
97
+ }
98
+
92
99
  // src/stanza/json.ts
93
100
  function isPlainObject(value) {
94
101
  const prototype = Object.getPrototypeOf(value);
@@ -317,10 +324,10 @@ function seedStanza(roomId) {
317
324
  // src/stanza/nucleo.ts
318
325
  var CHIAVE_NUCLEO = "nucleo";
319
326
  var PREFISSO_SAVE = "save:";
320
- var LIMITE_FRAME = 16 * 1024;
321
- var LIMITE_STATO = 256 * 1024;
322
- var LIMITE_SAVE = 128 * 1024;
323
- var LIMITE_DEPOSITO = 64 * 1024;
327
+ var LIMITE_FRAME = MASSIMO_BYTE_FRAME_STANZA;
328
+ var LIMITE_STATO = 512 * 1024;
329
+ var LIMITE_SAVE = 256 * 1024;
330
+ var LIMITE_DEPOSITO = 256 * 1024;
324
331
  var LIMITE_OPERAZIONI_DEPOSITO = 120;
325
332
  var GRAZIA_MS = 6e4;
326
333
  var STANZA_VUOTA_MS = 5 * 6e4;
@@ -330,6 +337,10 @@ var GRAZIA_GIORNALIERA_MS = 10 * 6e4;
330
337
  var RIPOSO_TICK_MS = 3e4;
331
338
  var INATTIVITA_MS = 10 * 6e4;
332
339
  var SCADENZA_PERSISTENTE_MS = 30 * 24 * 60 * 6e4;
340
+ var BLOCCO_INATTIVITA_MS = 3e4;
341
+ function scadenzaInattivita(ultimoEvento, durata) {
342
+ return Math.ceil((ultimoEvento + durata) / BLOCCO_INATTIVITA_MS) * BLOCCO_INATTIVITA_MS;
343
+ }
333
344
  var CHIAVE = /^[a-z0-9][a-z0-9_-]{0,31}$/;
334
345
  var PREFISSO_CHIAVE = /^[a-z0-9_-]{0,32}$/;
335
346
  var GAME_DEFINITION = /* @__PURE__ */ Symbol.for("@caisual/kit/game-definition");
@@ -707,6 +718,7 @@ var NucleoStanza = class _NucleoStanza {
707
718
  }
708
719
  if (primaConnessione) {
709
720
  await this.chiama(this.definizione.onStart, this.room);
721
+ this.iniziaReplay();
710
722
  this.inviaStatus(ora);
711
723
  }
712
724
  await this.concludiEvento();
@@ -926,6 +938,7 @@ var NucleoStanza = class _NucleoStanza {
926
938
  this.dati.ultimoInputAt = ora;
927
939
  this.dati.countdownAt = null;
928
940
  await this.chiama(this.definizione.onStart, this.room);
941
+ this.iniziaReplay();
929
942
  this.inviaStatus(ora);
930
943
  }
931
944
  }
@@ -1150,6 +1163,7 @@ var NucleoStanza = class _NucleoStanza {
1150
1163
  await this.applicaAzioni();
1151
1164
  if (dati.status === "playing") {
1152
1165
  await this.chiama(this.definizione.onStart, this.room);
1166
+ this.iniziaReplay();
1153
1167
  }
1154
1168
  await this.concludiEvento();
1155
1169
  if (dati.status === "lobby" || dati.status === "playing" || dati.status === "countdown") {
@@ -1303,7 +1317,7 @@ var NucleoStanza = class _NucleoStanza {
1303
1317
  const json = analizzaJson(value);
1304
1318
  if (!json.ok) throw new TypeError("Saved values must be valid JSON.");
1305
1319
  if (json.bytes > LIMITE_SAVE) {
1306
- throw new RangeError("Saved values must be at most 131072 bytes.");
1320
+ throw new RangeError("Saved values must be at most 262144 bytes.");
1307
1321
  }
1308
1322
  await this.adattatore.storage.put(PREFISSO_SAVE + key, json.valore);
1309
1323
  }
@@ -1374,7 +1388,7 @@ var NucleoStanza = class _NucleoStanza {
1374
1388
  if (!json.ok || json.bytes > LIMITE_DEPOSITO) {
1375
1389
  throw erroreConCodice(
1376
1390
  "store_too_large",
1377
- "Shared store values must be valid JSON of at most 65536 bytes."
1391
+ "Shared store values must be valid JSON of at most 262144 bytes."
1378
1392
  );
1379
1393
  }
1380
1394
  await this.usaDeposito((deposito) => deposito.set(key, json.valore));
@@ -1389,7 +1403,7 @@ var NucleoStanza = class _NucleoStanza {
1389
1403
  if (!Array.isArray(keys) || keys.some((key) => typeof key !== "string" || !CHIAVE.test(key))) {
1390
1404
  throw erroreConCodice("store_unavailable", "The shared store returned invalid keys.");
1391
1405
  }
1392
- return [...keys].sort().slice(0, 1024);
1406
+ return [...keys].sort().slice(0, 4096);
1393
1407
  }
1394
1408
  async incrementaDeposito(key, amount) {
1395
1409
  this.verificaChiaveDeposito(key);
@@ -1402,18 +1416,24 @@ var NucleoStanza = class _NucleoStanza {
1402
1416
  }
1403
1417
  return value;
1404
1418
  }
1405
- broadcastCreatore(message) {
1419
+ messaggioCreatore(message) {
1406
1420
  const json = analizzaJson(message);
1407
1421
  if (!json.ok) throw new TypeError("Messages must be valid JSON.");
1408
- this.broadcast({ t: "msg", m: json.valore });
1422
+ const frame = { t: "msg", m: json.valore };
1423
+ if (new TextEncoder().encode(JSON.stringify(frame)).byteLength > LIMITE_FRAME) {
1424
+ throw new RangeError("Game message frames must be at most 65536 bytes.");
1425
+ }
1426
+ return frame;
1427
+ }
1428
+ broadcastCreatore(message) {
1429
+ this.broadcast(this.messaggioCreatore(message));
1409
1430
  }
1410
1431
  inviaCreatore(playerId, message) {
1411
- const json = analizzaJson(message);
1412
- if (!json.ok) throw new TypeError("Messages must be valid JSON.");
1432
+ const frame = this.messaggioCreatore(message);
1413
1433
  const player = this.richiediDati().giocatori.find((item) => item.id === playerId);
1414
1434
  if (player === void 0) throw new Error("Player not found.");
1415
1435
  if (player.connected && player.connessione !== null) {
1416
- this.adattatore.invia(player.connessione, { t: "msg", m: json.valore });
1436
+ this.adattatore.invia(player.connessione, frame);
1417
1437
  }
1418
1438
  }
1419
1439
  broadcast(message) {
@@ -1508,6 +1528,13 @@ var NucleoStanza = class _NucleoStanza {
1508
1528
  dati.statoSincronizzato = copiaJson(dati.state);
1509
1529
  dati.tickSincronizzato = dati.tick;
1510
1530
  }
1531
+ iniziaReplay() {
1532
+ if (!this.manifest.replays || !this.adattatore.inizioReplay) return;
1533
+ const json = analizzaJson(this.richiediDati().state);
1534
+ if (!json.ok || json.bytes > LIMITE_STATO) return;
1535
+ this.inviaSnapshotTutti();
1536
+ this.adattatore.inizioReplay(this.fotografia());
1537
+ }
1511
1538
  inviaSnapshotTutti() {
1512
1539
  const dati = this.richiediDati();
1513
1540
  const stato = analizzaJson(dati.state);
@@ -1764,10 +1791,13 @@ var NucleoStanza = class _NucleoStanza {
1764
1791
  if (this.dati.status === "finished") prossime.push(this.dati.rivincitaFinoA ?? this.adattatore.ora());
1765
1792
  if (this.manifest.persistent === true) {
1766
1793
  prossime.push(
1767
- Math.max(this.dati.ultimoInputAt, this.dati.ultimoCambioStatoAt) + SCADENZA_PERSISTENTE_MS
1794
+ scadenzaInattivita(
1795
+ Math.max(this.dati.ultimoInputAt, this.dati.ultimoCambioStatoAt),
1796
+ SCADENZA_PERSISTENTE_MS
1797
+ )
1768
1798
  );
1769
1799
  } else if (this.dati.status === "playing") {
1770
- prossime.push(this.dati.ultimoInputAt + INATTIVITA_MS);
1800
+ prossime.push(scadenzaInattivita(this.dati.ultimoInputAt, INATTIVITA_MS));
1771
1801
  }
1772
1802
  if (this.dati.countdownAt !== null) prossime.push(this.dati.countdownAt);
1773
1803
  for (const player of this.dati.giocatori) {
@@ -1837,6 +1867,9 @@ var AdattatoreFacet = class {
1837
1867
  invia(connessione, messaggio) {
1838
1868
  this.frames.push({ connessione, testo: JSON.stringify(messaggio) });
1839
1869
  }
1870
+ inizioReplay(fotografia) {
1871
+ this.pubblici.push(JSON.stringify({ t: "replay-start", ...fotografia }));
1872
+ }
1840
1873
  pubblica(messaggio) {
1841
1874
  this.pubblici.push(JSON.stringify(messaggio));
1842
1875
  }
@@ -1954,7 +1987,7 @@ function creaFacetStanza(DurableObject, definizione, manifest) {
1954
1987
  }
1955
1988
  ricevi(connessione, testo, limiteVerificato = false) {
1956
1989
  return this.#esegui(async (nucleo) => {
1957
- if (new TextEncoder().encode(testo).byteLength > 16 * 1024) {
1990
+ if (new TextEncoder().encode(testo).byteLength > MASSIMO_BYTE_FRAME_STANZA) {
1958
1991
  this.#adattatore.chiudi(connessione, 4009, "message_too_large");
1959
1992
  await nucleo.disconnetti(connessione);
1960
1993
  } else await nucleo.ricevi(connessione, testo, limiteVerificato);
@@ -3,6 +3,7 @@ export * from './manifest.js';
3
3
  export * from './device.js';
4
4
  export * from './runtime.js';
5
5
  export * from './server-js.js';
6
+ export * from './server-wasm.js';
6
7
  export * from './slug.js';
7
8
  export * from './biglietto.js';
8
9
  export * from './token-crew.js';
@@ -14,3 +15,5 @@ export * from './player.js';
14
15
  export * from './match-result.js';
15
16
  export * from './immagine.js';
16
17
  export * from './versioni.js';
18
+ export * from './replay.js';
19
+ export * from './pubblicazione-limiti.js';
@@ -93,11 +93,11 @@ export interface Manifest {
93
93
  input: Input[];
94
94
  visibility: Visibility;
95
95
  network: string[];
96
- isolated: boolean;
97
96
  requires: ManifestRequires;
98
97
  players: ManifestPlayers;
99
98
  lobby: boolean;
100
99
  persistent: boolean;
100
+ replays: boolean;
101
101
  spectators: ManifestSpectators | null;
102
102
  boards: Record<string, ManifestBoard>;
103
103
  roles: ManifestRole[];
@@ -105,6 +105,8 @@ export interface Manifest {
105
105
  voice: Voice;
106
106
  modes: ManifestMode[];
107
107
  }
108
+ export declare const AVVISO_ISOLATED = "isolated has no effect: games run inside the portal page, which is not cross-origin isolated";
109
+ export declare function avvisiManifest(value: unknown): string[];
108
110
  export type EsitoManifest = {
109
111
  ok: true;
110
112
  manifest: Manifest;
@@ -1,3 +1,4 @@
1
+ import { type ReplayPlayback } from './replay.js';
1
2
  import { type Manifest } from './manifest.js';
2
3
  export declare const OVERLAY_VERSION: 1;
3
4
  export declare const OVERLAY_PANELS: readonly ['home', 'room', 'invite', 'friends', 'voice', 'boards'];
@@ -56,6 +57,8 @@ export interface SessionResume {
56
57
  updatedAt: number;
57
58
  }
58
59
  export interface OverlayRoom {
60
+ replay?: ReplayPlayback;
61
+ replayId?: string;
59
62
  rematch?: {
60
63
  keepSetup: boolean;
61
64
  autoStart: boolean;
@@ -112,6 +115,14 @@ export interface OverlaySessionState {
112
115
  voice?: OverlayVoice | null;
113
116
  }
114
117
  export interface OverlayOperations {
118
+ 'replay.play': Record<string, never>;
119
+ 'replay.pause': Record<string, never>;
120
+ 'replay.seek': {
121
+ positionMs: number;
122
+ };
123
+ 'replay.speed': {
124
+ speed: number;
125
+ };
115
126
  'local.start': {
116
127
  mode: string;
117
128
  };
@@ -0,0 +1,3 @@
1
+ export declare const MASSIMO_FILE = 3000;
2
+ export declare const MASSIMO_BYTE_TOTALI = 500000000;
3
+ export declare const MASSIMO_BYTE_FILE = 100000000;
@@ -0,0 +1,83 @@
1
+ import type { OverlayPlayer } from './overlay.js';
2
+ export declare const REPLAY_MAX_BYTES: number;
3
+ export declare const REPLAY_MAX_DURATION_MS: number;
4
+ export declare const REPLAY_CHUNK_BYTES: number;
5
+ export declare const REPLAY_RETENTION_MS: number;
6
+ export declare const REPLAY_ID: RegExp;
7
+ export type ReplayJson = null | boolean | number | string | ReplayJson[] | {
8
+ [key: string]: ReplayJson;
9
+ };
10
+ export type ReplayStatus = 'lobby' | 'countdown' | 'playing' | 'finished' | 'ended';
11
+ export interface ReplayRoom {
12
+ id: string;
13
+ seed: number;
14
+ status: ReplayStatus;
15
+ mode: string | null;
16
+ tick: number;
17
+ tickRate: number;
18
+ serverTime: number;
19
+ host: string | null;
20
+ }
21
+ export type ReplayMessage = {
22
+ t: 'start';
23
+ room: ReplayRoom;
24
+ players: OverlayPlayer[];
25
+ state: ReplayJson;
26
+ } | {
27
+ t: 'players';
28
+ players: OverlayPlayer[];
29
+ host?: string | null;
30
+ } | {
31
+ t: 'status';
32
+ status: ReplayStatus;
33
+ at: number;
34
+ result: ReplayJson;
35
+ host?: string | null;
36
+ } | {
37
+ t: 'state';
38
+ tick: number;
39
+ tickRate?: number;
40
+ serverTime: number;
41
+ base: number;
42
+ patch: ({
43
+ op: 'set';
44
+ path: (string | number)[];
45
+ value: ReplayJson;
46
+ } | {
47
+ op: 'del';
48
+ path: (string | number)[];
49
+ })[];
50
+ } | {
51
+ t: 'snapshot';
52
+ tick: number;
53
+ tickRate?: number;
54
+ serverTime: number;
55
+ state: ReplayJson;
56
+ };
57
+ export interface ReplayEvent {
58
+ at: number;
59
+ message: ReplayMessage;
60
+ }
61
+ export interface ReplayIndex {
62
+ format: 1;
63
+ id: string;
64
+ gameId: number;
65
+ n: number;
66
+ roomId: string;
67
+ startedAt: number;
68
+ durationMs: number;
69
+ bytes: number;
70
+ chunks: number;
71
+ players: OverlayPlayer[];
72
+ result: ReplayJson;
73
+ truncated: boolean;
74
+ expiresAt: number;
75
+ }
76
+ export interface ReplayPlayback {
77
+ positionMs: number;
78
+ durationMs: number;
79
+ paused: boolean;
80
+ speed: number;
81
+ truncated: boolean;
82
+ }
83
+ export declare function replayChunkKey(gameId: number, roomId: string, id: string, chunk: number): string;
@@ -1,5 +1,6 @@
1
- export declare const MESSAGGI_GIOCO_AL_SECONDO = 20;
2
- export declare const MESSAGGI_SERVIZIO_AL_SECONDO = 20;
1
+ export declare const MASSIMO_BYTE_FRAME_STANZA: number;
2
+ export declare const MESSAGGI_GIOCO_AL_SECONDO = 30;
3
+ export declare const MESSAGGI_SERVIZIO_AL_SECONDO = 30;
3
4
  export type EsitoLimiteMessaggi = {
4
5
  accetta: boolean;
5
6
  avvisa: boolean;
@@ -1,4 +1,4 @@
1
- export declare const MASSIMO_BYTE_SERVER_JS = 1000000;
1
+ export declare const MASSIMO_BYTE_SERVER_JS = 4000000;
2
2
  /** Il modulo pubblico va risolto nel kit incluso insieme al sorgente del creatore. */
3
3
  export declare function riscriviImportServerJs(sorgente: string): string;
4
4
  export type EsitoServerJs = {
@@ -7,5 +7,13 @@ export type EsitoServerJs = {
7
7
  ok: false;
8
8
  errori: string[];
9
9
  };
10
- /** Valida le regole che rendono server.js componibile come modulo singolo. */
10
+ /** Le posizioni nel codice mascherato escludono esempi in testi e commenti. */
11
+ export declare function importWasmServer(sorgente: string): Array<{
12
+ inizio: number;
13
+ fine: number;
14
+ nome: string;
15
+ path: string;
16
+ }>;
17
+ export declare function percorsiWasmServer(sorgente: string): string[];
18
+ /** Valida le regole che rendono server.js componibile con i soli moduli ammessi. */
11
19
  export declare function validaServerJs(sorgente: string): EsitoServerJs;
@@ -0,0 +1,19 @@
1
+ export declare const MASSIMO_FILE_WASM_SERVER = 8;
2
+ export declare const MASSIMO_BYTE_WASM_SERVER_FILE = 8000000;
3
+ export declare const MASSIMO_BYTE_WASM_SERVER_TOTALI = 16000000;
4
+ export interface FileWasmServer {
5
+ path: string;
6
+ bytes: number;
7
+ sha256: string;
8
+ }
9
+ /** La memoria condivisa non e' disponibile nel server, anche se Node la accetta. */
10
+ export declare function validaBinarioWasmServer(buffer: ArrayBuffer): string | null;
11
+ /** Un nome canonico evita alias fra import, distinta e archivio privato. */
12
+ export declare function percorsoWasmServerValido(path: string): boolean;
13
+ export declare function validaFileWasmServer(value: unknown): {
14
+ ok: true;
15
+ files: FileWasmServer[];
16
+ } | {
17
+ ok: false;
18
+ errori: string[];
19
+ };
@@ -11,6 +11,7 @@ export interface FinestraHandshakeKit extends GenitoreHandshake {
11
11
  clearTimeout(id: number): void;
12
12
  }
13
13
  export interface EsitoHandshake extends VersioneGioco {
14
+ replay?: string;
14
15
  language?: string;
15
16
  uiLanguage?: string;
16
17
  languagePreferences?: string[];
@@ -94,6 +94,7 @@ export interface RoomInvite {
94
94
  url: string;
95
95
  }
96
96
  export interface RoomMetadata {
97
+ replayId?: string | null;
97
98
  rematch?: {
98
99
  keepSetup: boolean;
99
100
  autoStart: boolean;
@@ -106,6 +107,8 @@ export interface RoomMetadata {
106
107
  closedCode: number | null;
107
108
  }
108
109
  export interface Room {
110
+ readonly replay: boolean;
111
+ readonly role: string | null;
109
112
  readonly mode: string | null;
110
113
  readonly countdownAt: number | null;
111
114
  readonly connection: ConnectionState;
@@ -148,6 +151,8 @@ export interface Room {
148
151
  serverTime(): number;
149
152
  }
150
153
  export interface Spectate {
154
+ readonly replay: boolean;
155
+ readonly role: 'spectator';
151
156
  readonly mode: string | null;
152
157
  readonly countdownAt: number | null;
153
158
  readonly connection: ConnectionState;
@@ -184,6 +189,7 @@ export interface MatchOptions {
184
189
  }): void;
185
190
  }
186
191
  export interface Rooms {
192
+ readonly replay?: false;
187
193
  reload(): void;
188
194
  onError(listener: (error: import('./errors.js').ErroreCaisual) => void): () => void;
189
195
  readonly invited: string | null;
@@ -194,6 +200,9 @@ export interface Rooms {
194
200
  watch(code: string): Promise<Spectate>;
195
201
  match(options: MatchOptions): Promise<Room>;
196
202
  }
203
+ export interface ReplayRooms extends Omit<Rooms, 'replay'>, Spectate {
204
+ readonly replay: true;
205
+ }
197
206
  export interface CaisualConnection {
198
207
  text(): Promise<Text>;
199
208
  session: Session;
@@ -229,7 +238,7 @@ export interface CaisualConnection {
229
238
  guests?: boolean;
230
239
  }): Promise<BoardTop>;
231
240
  };
232
- room: Rooms;
241
+ room: Rooms | ReplayRooms;
233
242
  }
234
243
  export type BaseConnection = Omit<CaisualConnection, 'device' | 'session' | 'overlay' | 'text' | 'player'> & {
235
244
  player: Omit<Player, 'language' | 'uiLanguage'>;
@@ -22,6 +22,7 @@ export interface HostBridgeInput {
22
22
  origineLive: string;
23
23
  n: number;
24
24
  watch?: boolean;
25
+ replay?: string | null;
25
26
  reload?(target?: import('../contracts/index.js').RicaricaGioco): void;
26
27
  language?: string;
27
28
  languagePreferences?: readonly string[];
@@ -42,6 +43,7 @@ export declare function stanzaDaMessaggio(value: unknown): {
42
43
  code: string;
43
44
  } | null | undefined;
44
45
  export declare function creaPonteOspite(input: HostBridgeInput): {
46
+ readonly replay: string | null;
45
47
  readonly watch: boolean;
46
48
  reload(): void;
47
49
  readonly epoch: string | null;