@series-inc/rundot-syncplay 6.0.0-rc.111 → 6.0.0-rc.113

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/core/runtime.d.ts CHANGED
@@ -10,10 +10,16 @@ export interface KinetixPinnedSnapshot {
10
10
  release(): void;
11
11
  }
12
12
 
13
+ /**
14
+ * Passed to an async snapshot serializer. Hand it to
15
+ * `world.serializeCheckpointAsync` unchanged.
16
+ */
13
17
  export interface SnapshotSerializationWork {
18
+ /** Aborts when the donation is cancelled. */
14
19
  readonly signal: AbortSignal;
15
- readonly digest: (bytes: Uint8Array) => Promise<string>;
20
+ /** Resolves on a later task, to yield the main thread. */
16
21
  readonly nextTick: () => Promise<void>;
22
+ /** Measures game packet work that runs after an await. */
17
23
  readonly measureGamePacket?: <T>(encode: () => T) => T;
18
24
  }
19
25
 
@@ -36,7 +36,7 @@ export const physicsProfileWasmMetadata = Object.freeze({
36
36
  "sourceHash": "4ca971eb52456e0141b37c4700f877b15b776df4be327000ff7a42d0aef106e6",
37
37
  "wasmSha256": "4eb4d6848a74b25b5d21c377be92ad1a0d78f1f96f12934ddae683c99a7c0afc",
38
38
  "artifact": "profile",
39
- "packageVersion": "6.0.0-rc.111",
39
+ "packageVersion": "6.0.0-rc.113",
40
40
  "compilerFlags": [
41
41
  "-std=c++20",
42
42
  "-O3",
@@ -36,7 +36,7 @@ export const physicsWasmMetadata = Object.freeze({
36
36
  "sourceHash": "76bbbac134fa1abfc823dc6d324b663da3bcdb80bc09c28fc025736d2d9cc03f",
37
37
  "wasmSha256": "19d53336de18d53eed555cd80f25cdb5e9e43c6491c06e93f672be7683737ac7",
38
38
  "artifact": "production",
39
- "packageVersion": "6.0.0-rc.111",
39
+ "packageVersion": "6.0.0-rc.113",
40
40
  "compilerFlags": [
41
41
  "-std=c++20",
42
42
  "-O3",
@@ -174,8 +174,6 @@ export interface KinetixNativeRuntimeController {
174
174
  restoreCheckpoint(checkpoint: KinetixNativeRuntimeCheckpoint): void;
175
175
  releaseCheckpoint(checkpoint: KinetixNativeRuntimeCheckpoint): void;
176
176
  serializeCheckpoint(checkpoint: KinetixNativeRuntimeCheckpoint): Uint8Array;
177
- serializeCheckpointTransfer(checkpoint: KinetixNativeRuntimeCheckpoint): Uint8Array;
178
- stageCheckpointTransfer(checkpoint: KinetixNativeRuntimeCheckpoint): { bytes: Uint8Array; sectionOffset: number };
179
177
  digestCheckpoint(checkpoint: KinetixNativeRuntimeCheckpoint): bigint;
180
178
  strongDigestCheckpoint(checkpoint: KinetixNativeRuntimeCheckpoint): Uint8Array;
181
179
  checkpointInfo(checkpoint: KinetixNativeRuntimeCheckpoint): ReturnType<KinetixNativePhysicsWorld['checkpointInfo']>;
@@ -50,7 +50,7 @@ export const physics2dWasmMetadata = Object.freeze({
50
50
  "sourceHash": "b4ae2139eb68bf6c797af143c07cec8b80e732662f9f9cda219bf76de63e0934",
51
51
  "wasmSha256": "633aeb98cb199b120cc7b5a855a620e01ab73d5cd8b6427cb7949eafecfdf1f0",
52
52
  "artifact": "production",
53
- "packageVersion": "6.0.0-rc.111",
53
+ "packageVersion": "6.0.0-rc.113",
54
54
  "compilerFlags": [
55
55
  "-std=c++20",
56
56
  "-O3",
package/core/world.d.ts CHANGED
@@ -897,8 +897,6 @@ export interface KinetixNativePhysicsWorld {
897
897
  captureCheckpoint(): KinetixNativeWorldCheckpoint;
898
898
  restoreCheckpoint(checkpoint: KinetixNativeWorldCheckpoint): void;
899
899
  serializeCheckpoint(checkpoint: KinetixNativeWorldCheckpoint): Uint8Array;
900
- serializeCheckpointTransfer(checkpoint: KinetixNativeWorldCheckpoint): Uint8Array;
901
- stageCheckpointTransfer(checkpoint: KinetixNativeWorldCheckpoint): { bytes: Uint8Array; sectionOffset: number };
902
900
  /**
903
901
  * The latest outputs of the frame of the checkpoint. `hitEventThreshold` is the threshold that the checkpoint
904
902
  * stores; a hit selection of these rows uses it, not the live threshold of the world.
@@ -8,12 +8,13 @@ export interface SnapshotDonationDiagnostic {
8
8
  readonly gameSerializerMs: number;
9
9
  readonly compressionMs: number;
10
10
  readonly digestMs: number;
11
+ readonly digestWaitMs: number;
11
12
  readonly longestMainThreadTaskMs: number;
12
13
  readonly rawBytes: number;
13
14
  readonly outputBytes: number;
14
15
  }
15
16
  export interface SyncplayDiagnostic {
16
- readonly code: 'api.deprecated' | 'snapshot.donor';
17
+ readonly code: 'api.deprecated' | 'snapshot.donor' | 'snapshot.joiner';
17
18
  readonly key: string;
18
19
  readonly message: string;
19
20
  readonly replacement: string;
@@ -84,9 +84,9 @@ export interface DeterministicAuthorityRoomConfig {
84
84
  /** Abandon a donor transfer that has not completed after this many ticks. Default 600. */
85
85
  readonly transferTimeoutTicks?: number;
86
86
  /**
87
- * Snapshot transfer ceiling in bytes for this room's collector (default
88
- * DEFAULT_MAX_SNAPSHOT_BYTES). Offers above it are rejected with
89
- * `snapshot-too-large`; keep it equal to the clients' configured ceiling.
87
+ * Snapshot wire limit for this room (default 4 MiB).
88
+ * The room rejects offers above the limit with `snapshot-too-large`.
89
+ * A client accepts up to 6,000,000 bytes.
90
90
  */
91
91
  readonly maxSnapshotBytes?: number;
92
92
  /**
@@ -2,8 +2,9 @@ import { canonicalStringify, defaultChecksum, encodeUtf8 } from './canonical.js'
2
2
  import { assertKinetixRuntimeIdentity, kinetixRuntimeIdentitiesEqual as equalRuntimeIdentity, kinetixSessionConfigDigest, } from '@series-inc/rundot-syncplay/core/runtime';
3
3
  import { invariant } from './errors.js';
4
4
  import { createInputAuthority } from './input-authority.js';
5
- import { createBinaryDeterministicLocalAuthoritySnapshotChunkEnvelope, createBinaryDeterministicLocalAuthoritySnapshotCompleteEnvelope, createBinaryDeterministicLocalAuthoritySnapshotOfferEnvelope, createBinaryDeterministicLocalAuthoritySnapshotRequestEnvelope, createDeterministicLocalAuthoritySnapshotChunkEnvelope, createDeterministicLocalAuthoritySnapshotCompleteEnvelope, createDeterministicLocalAuthoritySnapshotOfferEnvelope, createDeterministicLocalAuthoritySnapshotRequestEnvelope, decodeBinaryDeterministicLocalAuthorityWireEnvelope, decodeDeterministicLocalAuthorityWireEnvelope, } from './local-authority-wire.js';
6
- import { DEFAULT_BINARY_SNAPSHOT_CHUNK_SIZE, snapshotHydratedByteLimit, DEFAULT_MAX_SNAPSHOT_BYTES, DEFAULT_SNAPSHOT_CHUNK_SIZE, TEXT_COMPRESSED_SNAPSHOT_PREFIX, createBinarySnapshotTransferFromBytes, createSnapshotTransferCollector, createSnapshotTransferFromBytes, decodeDeterministicStateSnapshotInternal, encodeDeterministicStateSnapshot, encodeTextWrappedBinarySnapshot, deterministicRoomDescriptorHash, restampBinarySnapshotWitnesses, } from './session-snapshot.js';
5
+ import { createBinaryDeterministicLocalAuthoritySnapshotChunkEnvelope, createBinaryDeterministicLocalAuthoritySnapshotCompleteEnvelope, createBinaryDeterministicLocalAuthoritySnapshotOfferEnvelope, createBinaryDeterministicLocalAuthoritySnapshotRefusalEnvelope, createBinaryDeterministicLocalAuthoritySnapshotRequestEnvelope, createDeterministicLocalAuthoritySnapshotChunkEnvelope, createDeterministicLocalAuthoritySnapshotCompleteEnvelope, createDeterministicLocalAuthoritySnapshotOfferEnvelope, createDeterministicLocalAuthoritySnapshotRefusalEnvelope, createDeterministicLocalAuthoritySnapshotRequestEnvelope, decodeBinaryDeterministicLocalAuthorityWireEnvelope, decodeDeterministicLocalAuthorityWireEnvelope, } from './local-authority-wire.js';
6
+ import { DEFAULT_BINARY_SNAPSHOT_CHUNK_SIZE, snapshotHydratedByteLimit, DEFAULT_SNAPSHOT_CHUNK_SIZE, TEXT_COMPRESSED_SNAPSHOT_PREFIX, createBinarySnapshotTransferFromBytes, createSnapshotTransferCollector, createSnapshotTransferFromBytes, decodeDeterministicStateSnapshotInternal, encodeDeterministicStateSnapshot, encodeTextWrappedBinarySnapshot, deterministicRoomDescriptorHash, restampBinarySnapshotWitnesses, } from './session-snapshot.js';
7
+ import { DEFAULT_MAX_SNAPSHOT_BYTES, MAX_SNAPSHOT_WIRE_BYTES, isPositiveSnapshotByteLimit } from './snapshot-limits.js';
7
8
  import { MAX_KINETIX_SESSION_CONFIG_BYTES, MAX_SYNCPLAY_COMMAND_PAYLOAD_BYTES, MAX_SYNCPLAY_EPHEMERAL_PAYLOAD_BYTES, decodeDeterministicSessionMessage, encodeDeterministicSessionMessage, } from './session-wire.js';
8
9
  import { createSyncplaySecretAuthority, } from './secret-authority.js';
9
10
  import { DEFAULT_SEAT_POLICY, validateSeatPolicy, createInitialSeatControlState, allocatable, findAvailableSeat, applyTransition, expireSeatHolds, createSeatCheckpoint, restoreSeatControlState, } from './seat-control.js';
@@ -138,6 +139,11 @@ export function createDeterministicAuthorityRoom(transport, config) {
138
139
  }
139
140
  const sessionMsgType = config.sessionMsgType ?? DEFAULT_SESSION_MSG_TYPE;
140
141
  const binarySessionMsgType = config.binarySessionMsgType ?? `${sessionMsgType}-bin`;
142
+ const storedSnapshotLimitBytes = config.maxSnapshotBytes ?? DEFAULT_MAX_SNAPSHOT_BYTES;
143
+ // Low-level rooms and stored bundles can use a limit above the client maximum.
144
+ const advertisedSnapshotLimitBytes = storedSnapshotLimitBytes === DEFAULT_MAX_SNAPSHOT_BYTES
145
+ ? undefined
146
+ : Math.min(storedSnapshotLimitBytes, MAX_SNAPSHOT_WIRE_BYTES);
141
147
  const historyRetentionTicks = requireTickCount('historyRetentionTicks', config.historyRetentionTicks ?? DEFAULT_HISTORY_RETENTION_TICKS);
142
148
  // The ring keeps at least four redundancy windows, see createSessionAuthority.
143
149
  const historyRingTicks = Math.max(historyRetentionTicks, config.redundancyWindowTicks * 4);
@@ -154,13 +160,13 @@ export function createDeterministicAuthorityRoom(transport, config) {
154
160
  let seed = config.seed;
155
161
  let generation = config.restoreFrom?.generation ?? 0;
156
162
  invariant(Number.isInteger(generation) && generation >= 0, 'restored generation must be a non-negative integer', 'authority-room.restore-generation');
157
- if (config.maxSnapshotBytes !== undefined && (!Number.isInteger(config.maxSnapshotBytes) || config.maxSnapshotBytes < 1)) {
163
+ if (config.maxSnapshotBytes !== undefined && !isPositiveSnapshotByteLimit(config.maxSnapshotBytes)) {
158
164
  throw new Error(`createDeterministicAuthorityRoom: maxSnapshotBytes must be a positive integer, received ${config.maxSnapshotBytes}`);
159
165
  }
160
166
  // With no explicit hydrated limit, the room must accept at least what its own wire limit allows.
161
167
  // The client uses the same floor, so the two sides agree.
162
168
  const snapshotDecodeLimits = config.maxHydratedSnapshotBytes === undefined
163
- ? { defaultHydratedByteFloor: config.maxSnapshotBytes ?? 0 }
169
+ ? { defaultHydratedByteFloor: storedSnapshotLimitBytes }
164
170
  : { maxHydratedBytes: config.maxHydratedSnapshotBytes };
165
171
  const maxHydratedSnapshotBytes = snapshotHydratedByteLimit(undefined, snapshotDecodeLimits);
166
172
  if (!Number.isInteger(maxHydratedSnapshotBytes) || maxHydratedSnapshotBytes < 1) {
@@ -786,7 +792,7 @@ export function createDeterministicAuthorityRoom(transport, config) {
786
792
  transferId,
787
793
  chunkSize: DEFAULT_BINARY_SNAPSHOT_CHUNK_SIZE,
788
794
  });
789
- transport.sendBinaryTo(playerId, binarySessionMsgType, createBinaryDeterministicLocalAuthoritySnapshotOfferEnvelope(nextWireSequence(), binaryTransfer));
795
+ transport.sendBinaryTo(playerId, binarySessionMsgType, createBinaryDeterministicLocalAuthoritySnapshotOfferEnvelope(nextWireSequence(), binaryTransfer, advertisedSnapshotLimitBytes));
790
796
  for (let index = 0; index < binaryTransfer.chunks.length; index += 1) {
791
797
  transport.sendBinaryTo(playerId, binarySessionMsgType, createBinaryDeterministicLocalAuthoritySnapshotChunkEnvelope(nextWireSequence(), binaryTransfer, index));
792
798
  }
@@ -799,7 +805,7 @@ export function createDeterministicAuthorityRoom(transport, config) {
799
805
  transferId,
800
806
  chunkSize: DEFAULT_SNAPSHOT_CHUNK_SIZE,
801
807
  });
802
- transport.sendTo(playerId, sessionMsgType, createDeterministicLocalAuthoritySnapshotOfferEnvelope(nextWireSequence(), transfer));
808
+ transport.sendTo(playerId, sessionMsgType, createDeterministicLocalAuthoritySnapshotOfferEnvelope(nextWireSequence(), transfer, advertisedSnapshotLimitBytes));
803
809
  for (let index = 0; index < transfer.chunks.length; index += 1) {
804
810
  transport.sendTo(playerId, sessionMsgType, createDeterministicLocalAuthoritySnapshotChunkEnvelope(nextWireSequence(), transfer, index));
805
811
  }
@@ -1920,7 +1926,7 @@ export function createDeterministicAuthorityRoom(transport, config) {
1920
1926
  }
1921
1927
  }
1922
1928
  const isBinaryTransfer = collected.binarySnapshotBytes !== undefined;
1923
- const effectiveMaxSnapshotBytes = config.maxSnapshotBytes ?? DEFAULT_MAX_SNAPSHOT_BYTES;
1929
+ const effectiveMaxSnapshotBytes = storedSnapshotLimitBytes;
1924
1930
  if (decoded.source.kind === 'raw-binary' || decoded.source.kind === 'wrapped-binary') {
1925
1931
  const rawBinary = decoded.source.binarySnapshotBytes;
1926
1932
  const binarySnapshotBytes = snapshot.v === 3
@@ -1998,6 +2004,9 @@ export function createDeterministicAuthorityRoom(transport, config) {
1998
2004
  return;
1999
2005
  const failed = pendingTransfer;
2000
2006
  pendingTransfer = undefined;
2007
+ // Tell the donor, so it does not upload a snapshot that the room cannot store.
2008
+ if (reason === 'snapshot-too-large' && options?.donorGone !== true)
2009
+ refuseDonorTransfer(failed, reason);
2001
2010
  lastSnapshotRejection = reason;
2002
2011
  rejectionsSinceStore.add(reason);
2003
2012
  if (options?.donorGone === true) {
@@ -2015,6 +2024,14 @@ export function createDeterministicAuthorityRoom(transport, config) {
2015
2024
  config.onSnapshotEvent?.({ kind: 'rejected', donorId: failed.donorId, reason });
2016
2025
  beginSnapshotAcquisition(true);
2017
2026
  }
2027
+ function refuseDonorTransfer(failed, reason) {
2028
+ const identity = { descriptorHash: roomDescriptorHash, configHash: failed.transferRequestId };
2029
+ if (transport.sendBinaryTo !== undefined && binaryCapablePlayers.has(failed.donorId)) {
2030
+ transport.sendBinaryTo(failed.donorId, binarySessionMsgType, createBinaryDeterministicLocalAuthoritySnapshotRefusalEnvelope(nextWireSequence(), identity, reason, storedSnapshotLimitBytes));
2031
+ return;
2032
+ }
2033
+ transport.sendTo(failed.donorId, sessionMsgType, createDeterministicLocalAuthoritySnapshotRefusalEnvelope(nextWireSequence(), identity, reason, storedSnapshotLimitBytes));
2034
+ }
2018
2035
  /**
2019
2036
  * Start a snapshot transfer to the best target that has a donor left. A
2020
2037
  * (tick, donor) pair that failed stays failed until a snapshot is stored,
@@ -2064,7 +2081,7 @@ export function createDeterministicAuthorityRoom(transport, config) {
2064
2081
  minFrame: tick + 1,
2065
2082
  startedAt: now,
2066
2083
  timeoutTicks: transferTimeoutTicks,
2067
- collector: createSnapshotTransferCollector(config.maxSnapshotBytes === undefined ? {} : { maxSnapshotBytes: config.maxSnapshotBytes }),
2084
+ collector: createSnapshotTransferCollector({ maxSnapshotBytes: storedSnapshotLimitBytes }),
2068
2085
  isRetry,
2069
2086
  targetTick: tick,
2070
2087
  generation,
@@ -2074,10 +2091,10 @@ export function createDeterministicAuthorityRoom(transport, config) {
2074
2091
  const requestIdentity = { descriptorHash: roomDescriptorHash, configHash: transferRequestId };
2075
2092
  const transferBudgetMs = pendingTransfer.timeoutTicks * 1000 / config.tickRateHz;
2076
2093
  if (transport.sendBinaryTo !== undefined && binaryCapablePlayers.has(donorId)) {
2077
- transport.sendBinaryTo(donorId, binarySessionMsgType, createBinaryDeterministicLocalAuthoritySnapshotRequestEnvelope(nextWireSequence(), requestIdentity, tick + 1, transferBudgetMs));
2094
+ transport.sendBinaryTo(donorId, binarySessionMsgType, createBinaryDeterministicLocalAuthoritySnapshotRequestEnvelope(nextWireSequence(), requestIdentity, tick + 1, transferBudgetMs, advertisedSnapshotLimitBytes));
2078
2095
  return;
2079
2096
  }
2080
- transport.sendTo(donorId, sessionMsgType, createDeterministicLocalAuthoritySnapshotRequestEnvelope(nextWireSequence(), requestIdentity, tick + 1, transferBudgetMs));
2097
+ transport.sendTo(donorId, sessionMsgType, createDeterministicLocalAuthoritySnapshotRequestEnvelope(nextWireSequence(), requestIdentity, tick + 1, transferBudgetMs, advertisedSnapshotLimitBytes));
2081
2098
  }
2082
2099
  /**
2083
2100
  * The newest fully reported tick above the stored snapshot that has a donor
@@ -1,6 +1,8 @@
1
1
  export declare const deterministicLocalAuthorityWireProtocol = "rdm-local-authority/v2";
2
2
  export declare const deterministicLocalAuthorityBinaryWireMagic = "SYNWIR02";
3
- export type DeterministicLocalAuthorityWireKind = 'snapshot-request' | 'snapshot-offer' | 'snapshot-chunk' | 'snapshot-complete';
3
+ export type DeterministicLocalAuthorityWireKind = 'snapshot-request' | 'snapshot-offer' | 'snapshot-chunk' | 'snapshot-complete'
4
+ /** Room to donor: the room refused this transfer. Since 6.0.0-rc.112. */
5
+ | 'snapshot-refusal';
4
6
  export interface DeterministicLocalAuthoritySnapshotIdentity {
5
7
  readonly descriptorHash: string;
6
8
  readonly schemaHash?: string;
@@ -48,10 +50,17 @@ export declare function encodeBinaryDeterministicLocalAuthorityWireEnvelope(enve
48
50
  export declare function frameBinaryWireSteps(kindCode: number, sequence: number, headerBytes: Uint8Array, chunk?: Uint8Array): Generator<void, Uint8Array>;
49
51
  export declare const binarySnapshotChunkKindCode: number;
50
52
  export declare function decodeBinaryDeterministicLocalAuthorityWireEnvelope(encoded: Uint8Array): DeterministicLocalAuthorityWireEnvelope;
51
- export declare function createDeterministicLocalAuthoritySnapshotRequestEnvelope(sequence: number, identity: DeterministicLocalAuthoritySnapshotIdentity, minFrame: number, transferBudgetMs?: number): string;
52
- export declare function createBinaryDeterministicLocalAuthoritySnapshotRequestEnvelope(sequence: number, identity: DeterministicLocalAuthoritySnapshotIdentity, minFrame: number, transferBudgetMs?: number): Uint8Array;
53
- export declare function createDeterministicLocalAuthoritySnapshotOfferEnvelope(sequence: number, transfer: DeterministicLocalAuthoritySnapshotTransfer): string;
54
- export declare function createBinaryDeterministicLocalAuthoritySnapshotOfferEnvelope(sequence: number, transfer: DeterministicLocalAuthorityBinarySnapshotTransfer): Uint8Array;
53
+ export declare function createDeterministicLocalAuthoritySnapshotRequestEnvelope(sequence: number, identity: DeterministicLocalAuthoritySnapshotIdentity, minFrame: number, transferBudgetMs?: number, maxSnapshotBytes?: number): string;
54
+ export declare function createBinaryDeterministicLocalAuthoritySnapshotRequestEnvelope(sequence: number, identity: DeterministicLocalAuthoritySnapshotIdentity, minFrame: number, transferBudgetMs?: number, maxSnapshotBytes?: number): Uint8Array;
55
+ /**
56
+ * Room to donor: the room refused the transfer of the request with this
57
+ * identity, so the donor stops uploading it. Since 6.0.0-rc.112. An older
58
+ * client ignores the unknown kind.
59
+ */
60
+ export declare function createDeterministicLocalAuthoritySnapshotRefusalEnvelope(sequence: number, identity: DeterministicLocalAuthoritySnapshotIdentity, reason: string, maxSnapshotBytes: number): string;
61
+ export declare function createBinaryDeterministicLocalAuthoritySnapshotRefusalEnvelope(sequence: number, identity: DeterministicLocalAuthoritySnapshotIdentity, reason: string, maxSnapshotBytes: number): Uint8Array;
62
+ export declare function createDeterministicLocalAuthoritySnapshotOfferEnvelope(sequence: number, transfer: DeterministicLocalAuthoritySnapshotTransfer, maxSnapshotBytes?: number): string;
63
+ export declare function createBinaryDeterministicLocalAuthoritySnapshotOfferEnvelope(sequence: number, transfer: DeterministicLocalAuthorityBinarySnapshotTransfer, maxSnapshotBytes?: number): Uint8Array;
55
64
  export declare function createDeterministicLocalAuthoritySnapshotChunkEnvelope(sequence: number, transfer: DeterministicLocalAuthoritySnapshotTransfer, chunkIndex: number): string;
56
65
  export declare function createBinaryDeterministicLocalAuthoritySnapshotChunkEnvelope(sequence: number, transfer: DeterministicLocalAuthorityBinarySnapshotTransfer, chunkIndex: number): Uint8Array;
57
66
  export declare function createDeterministicLocalAuthoritySnapshotCompleteEnvelope(sequence: number, transferId: string): string;
@@ -1,4 +1,5 @@
1
1
  import { canonicalStringify, cloneCanonical, defaultChecksum, decodeUtf8, encodeUtf8 } from './canonical.js';
2
+ import { MAX_SNAPSHOT_WIRE_BYTES, isSnapshotWireByteLimit } from './snapshot-limits.js';
2
3
  export const deterministicLocalAuthorityWireProtocol = 'rdm-local-authority/v2';
3
4
  export const deterministicLocalAuthorityBinaryWireMagic = 'SYNWIR02';
4
5
  const MAX_ENVELOPE_BYTES = 2 * 1024 * 1024;
@@ -17,18 +18,21 @@ const KIND_TO_CODE = {
17
18
  'snapshot-offer': 2,
18
19
  'snapshot-chunk': 3,
19
20
  'snapshot-complete': 4,
21
+ 'snapshot-refusal': 5,
20
22
  };
21
23
  const CODE_TO_KIND = {
22
24
  1: 'snapshot-request',
23
25
  2: 'snapshot-offer',
24
26
  3: 'snapshot-chunk',
25
27
  4: 'snapshot-complete',
28
+ 5: 'snapshot-refusal',
26
29
  };
27
30
  const kinds = new Set([
28
31
  'snapshot-request',
29
32
  'snapshot-offer',
30
33
  'snapshot-chunk',
31
34
  'snapshot-complete',
35
+ 'snapshot-refusal',
32
36
  ]);
33
37
  export function encodeDeterministicLocalAuthorityWireEnvelope(envelope) {
34
38
  assertSequence(envelope.sequence);
@@ -236,26 +240,60 @@ export function decodeBinaryDeterministicLocalAuthorityWireEnvelope(encoded) {
236
240
  checksum: expectedDigest.toString(16).padStart(8, '0'),
237
241
  };
238
242
  }
239
- export function createDeterministicLocalAuthoritySnapshotRequestEnvelope(sequence, identity, minFrame, transferBudgetMs) {
243
+ /** A limit a room puts on the wire: absent, or an integer the receiver accepts. */
244
+ function assertWireByteLimit(message, maxSnapshotBytes) {
245
+ if (maxSnapshotBytes !== undefined && !isSnapshotWireByteLimit(maxSnapshotBytes)) {
246
+ throw new Error(`Syncplay snapshot ${message} maxSnapshotBytes must be an integer from 1 to ${MAX_SNAPSHOT_WIRE_BYTES}`);
247
+ }
248
+ }
249
+ /**
250
+ * `maxSnapshotBytes` (since 6.0.0-rc.112) is the room's stored snapshot
251
+ * limit. A donor without a Worker uses it to choose a payload that fits.
252
+ */
253
+ function snapshotRequestPayload(identity, minFrame, transferBudgetMs, maxSnapshotBytes) {
240
254
  assertIdentity(identity);
241
255
  assertFrame(minFrame);
256
+ assertWireByteLimit('request', maxSnapshotBytes);
257
+ return {
258
+ identity, minFrame,
259
+ ...(transferBudgetMs === undefined ? {} : { transferBudgetMs }),
260
+ ...(maxSnapshotBytes === undefined ? {} : { maxSnapshotBytes }),
261
+ };
262
+ }
263
+ export function createDeterministicLocalAuthoritySnapshotRequestEnvelope(sequence, identity, minFrame, transferBudgetMs, maxSnapshotBytes) {
242
264
  return encodeDeterministicLocalAuthorityWireEnvelope({
243
265
  kind: 'snapshot-request',
244
266
  sequence,
245
- payload: { identity, minFrame, ...(transferBudgetMs === undefined ? {} : { transferBudgetMs }) },
267
+ payload: snapshotRequestPayload(identity, minFrame, transferBudgetMs, maxSnapshotBytes),
246
268
  });
247
269
  }
248
- export function createBinaryDeterministicLocalAuthoritySnapshotRequestEnvelope(sequence, identity, minFrame, transferBudgetMs) {
249
- assertIdentity(identity);
250
- assertFrame(minFrame);
270
+ export function createBinaryDeterministicLocalAuthoritySnapshotRequestEnvelope(sequence, identity, minFrame, transferBudgetMs, maxSnapshotBytes) {
251
271
  return encodeBinaryDeterministicLocalAuthorityWireEnvelope({
252
272
  kind: 'snapshot-request',
253
273
  sequence,
254
- payload: { identity, minFrame, ...(transferBudgetMs === undefined ? {} : { transferBudgetMs }) },
274
+ payload: snapshotRequestPayload(identity, minFrame, transferBudgetMs, maxSnapshotBytes),
275
+ });
276
+ }
277
+ /**
278
+ * Room to donor: the room refused the transfer of the request with this
279
+ * identity, so the donor stops uploading it. Since 6.0.0-rc.112. An older
280
+ * client ignores the unknown kind.
281
+ */
282
+ export function createDeterministicLocalAuthoritySnapshotRefusalEnvelope(sequence, identity, reason, maxSnapshotBytes) {
283
+ assertIdentity(identity);
284
+ return encodeDeterministicLocalAuthorityWireEnvelope({
285
+ kind: 'snapshot-refusal', sequence, payload: { identity, reason, maxSnapshotBytes },
286
+ });
287
+ }
288
+ export function createBinaryDeterministicLocalAuthoritySnapshotRefusalEnvelope(sequence, identity, reason, maxSnapshotBytes) {
289
+ assertIdentity(identity);
290
+ return encodeBinaryDeterministicLocalAuthorityWireEnvelope({
291
+ kind: 'snapshot-refusal', sequence, payload: { identity, reason, maxSnapshotBytes },
255
292
  });
256
293
  }
257
- export function createDeterministicLocalAuthoritySnapshotOfferEnvelope(sequence, transfer) {
294
+ export function createDeterministicLocalAuthoritySnapshotOfferEnvelope(sequence, transfer, maxSnapshotBytes) {
258
295
  assertTransfer(transfer);
296
+ assertWireByteLimit('offer', maxSnapshotBytes);
259
297
  return encodeDeterministicLocalAuthorityWireEnvelope({
260
298
  kind: 'snapshot-offer',
261
299
  sequence,
@@ -267,12 +305,14 @@ export function createDeterministicLocalAuthoritySnapshotOfferEnvelope(sequence,
267
305
  chunkCount: transfer.chunks.length,
268
306
  totalBytes: transfer.snapshotBytes.length,
269
307
  chunkChecksums: transfer.chunkChecksums,
308
+ ...(maxSnapshotBytes === undefined ? {} : { maxSnapshotBytes }),
270
309
  ...(transfer.compressed === true ? { compressed: true } : {}),
271
310
  },
272
311
  });
273
312
  }
274
- export function createBinaryDeterministicLocalAuthoritySnapshotOfferEnvelope(sequence, transfer) {
313
+ export function createBinaryDeterministicLocalAuthoritySnapshotOfferEnvelope(sequence, transfer, maxSnapshotBytes) {
275
314
  assertBinaryTransfer(transfer);
315
+ assertWireByteLimit('offer', maxSnapshotBytes);
276
316
  return encodeBinaryDeterministicLocalAuthorityWireEnvelope({
277
317
  kind: 'snapshot-offer',
278
318
  sequence,
@@ -284,6 +324,7 @@ export function createBinaryDeterministicLocalAuthoritySnapshotOfferEnvelope(seq
284
324
  chunkCount: transfer.chunks.length,
285
325
  totalBytes: transfer.binarySnapshotBytes.byteLength,
286
326
  chunkChecksums: transfer.chunkChecksums,
327
+ ...(maxSnapshotBytes === undefined ? {} : { maxSnapshotBytes }),
287
328
  binary: true,
288
329
  },
289
330
  });
@@ -122,11 +122,7 @@ export interface NetworkedSyncplayClientOptions<State, Input, Checkpoint = unkno
122
122
  * pulse/edge command codecs so a one-shot can never be replayed speculatively.
123
123
  */
124
124
  readonly remoteInputPrediction?: SyncplayRemoteInputPrediction;
125
- /**
126
- * Snapshot transfer ceiling in bytes for the client collector (default
127
- * DEFAULT_MAX_SNAPSHOT_BYTES). Must match the room's configured ceiling — a
128
- * client ceiling below the room's snapshot size silently breaks late-join.
129
- */
125
+ /** Optional cap on received snapshots and staged donor fallback. The room advertises its own limit. */
130
126
  readonly maxSnapshotBytes?: number;
131
127
  /**
132
128
  * Decoded/hydrated raw snapshot payload ceiling in bytes (default