@colyseus/schema 4.0.20 → 5.0.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.
Files changed (96) hide show
  1. package/README.md +2 -0
  2. package/build/Metadata.d.ts +55 -2
  3. package/build/Reflection.d.ts +24 -30
  4. package/build/Schema.d.ts +70 -9
  5. package/build/annotations.d.ts +56 -13
  6. package/build/codegen/cli.cjs +84 -67
  7. package/build/codegen/cli.cjs.map +1 -1
  8. package/build/decoder/DecodeOperation.d.ts +48 -5
  9. package/build/decoder/Decoder.d.ts +2 -2
  10. package/build/decoder/strategy/Callbacks.d.ts +1 -1
  11. package/build/encoder/ChangeRecorder.d.ts +107 -0
  12. package/build/encoder/ChangeTree.d.ts +218 -69
  13. package/build/encoder/EncodeDescriptor.d.ts +63 -0
  14. package/build/encoder/EncodeOperation.d.ts +25 -2
  15. package/build/encoder/Encoder.d.ts +59 -3
  16. package/build/encoder/MapJournal.d.ts +62 -0
  17. package/build/encoder/RefIdAllocator.d.ts +35 -0
  18. package/build/encoder/Root.d.ts +94 -13
  19. package/build/encoder/StateView.d.ts +116 -8
  20. package/build/encoder/changeTree/inheritedFlags.d.ts +34 -0
  21. package/build/encoder/changeTree/liveIteration.d.ts +3 -0
  22. package/build/encoder/changeTree/parentChain.d.ts +24 -0
  23. package/build/encoder/changeTree/treeAttachment.d.ts +13 -0
  24. package/build/encoder/streaming.d.ts +73 -0
  25. package/build/encoder/subscriptions.d.ts +25 -0
  26. package/build/index.cjs +5202 -1552
  27. package/build/index.cjs.map +1 -1
  28. package/build/index.d.ts +7 -3
  29. package/build/index.js +5202 -1552
  30. package/build/index.mjs +5193 -1552
  31. package/build/index.mjs.map +1 -1
  32. package/build/input/InputDecoder.d.ts +32 -0
  33. package/build/input/InputEncoder.d.ts +117 -0
  34. package/build/input/index.cjs +7429 -0
  35. package/build/input/index.cjs.map +1 -0
  36. package/build/input/index.d.ts +3 -0
  37. package/build/input/index.mjs +7426 -0
  38. package/build/input/index.mjs.map +1 -0
  39. package/build/types/HelperTypes.d.ts +22 -8
  40. package/build/types/TypeContext.d.ts +9 -0
  41. package/build/types/builder.d.ts +162 -0
  42. package/build/types/custom/ArraySchema.d.ts +25 -4
  43. package/build/types/custom/CollectionSchema.d.ts +30 -2
  44. package/build/types/custom/MapSchema.d.ts +52 -3
  45. package/build/types/custom/SetSchema.d.ts +32 -2
  46. package/build/types/custom/StreamSchema.d.ts +114 -0
  47. package/build/types/symbols.d.ts +48 -5
  48. package/package.json +9 -3
  49. package/src/Metadata.ts +258 -31
  50. package/src/Reflection.ts +15 -13
  51. package/src/Schema.ts +176 -134
  52. package/src/annotations.ts +308 -236
  53. package/src/bench_bloat.ts +173 -0
  54. package/src/bench_decode.ts +221 -0
  55. package/src/bench_decode_mem.ts +165 -0
  56. package/src/bench_encode.ts +108 -0
  57. package/src/bench_init.ts +150 -0
  58. package/src/bench_static.ts +109 -0
  59. package/src/bench_stream.ts +295 -0
  60. package/src/bench_view_cmp.ts +142 -0
  61. package/src/codegen/languages/csharp.ts +0 -24
  62. package/src/codegen/parser.ts +83 -61
  63. package/src/decoder/DecodeOperation.ts +168 -63
  64. package/src/decoder/Decoder.ts +20 -10
  65. package/src/decoder/ReferenceTracker.ts +4 -0
  66. package/src/decoder/strategy/Callbacks.ts +30 -26
  67. package/src/decoder/strategy/getDecoderStateCallbacks.ts +16 -13
  68. package/src/encoder/ChangeRecorder.ts +276 -0
  69. package/src/encoder/ChangeTree.ts +674 -519
  70. package/src/encoder/EncodeDescriptor.ts +213 -0
  71. package/src/encoder/EncodeOperation.ts +107 -65
  72. package/src/encoder/Encoder.ts +630 -119
  73. package/src/encoder/MapJournal.ts +124 -0
  74. package/src/encoder/RefIdAllocator.ts +68 -0
  75. package/src/encoder/Root.ts +247 -120
  76. package/src/encoder/StateView.ts +592 -121
  77. package/src/encoder/changeTree/inheritedFlags.ts +217 -0
  78. package/src/encoder/changeTree/liveIteration.ts +74 -0
  79. package/src/encoder/changeTree/parentChain.ts +131 -0
  80. package/src/encoder/changeTree/treeAttachment.ts +171 -0
  81. package/src/encoder/streaming.ts +232 -0
  82. package/src/encoder/subscriptions.ts +71 -0
  83. package/src/index.ts +15 -3
  84. package/src/input/InputDecoder.ts +57 -0
  85. package/src/input/InputEncoder.ts +303 -0
  86. package/src/input/index.ts +3 -0
  87. package/src/types/HelperTypes.ts +21 -9
  88. package/src/types/TypeContext.ts +14 -2
  89. package/src/types/builder.ts +285 -0
  90. package/src/types/custom/ArraySchema.ts +210 -197
  91. package/src/types/custom/CollectionSchema.ts +115 -35
  92. package/src/types/custom/MapSchema.ts +162 -58
  93. package/src/types/custom/SetSchema.ts +128 -39
  94. package/src/types/custom/StreamSchema.ts +310 -0
  95. package/src/types/symbols.ts +54 -6
  96. package/src/utils.ts +4 -6
@@ -0,0 +1,32 @@
1
+ import type { Schema } from "../Schema.js";
2
+ /**
3
+ * Bound single-struct decoder for input packets. Wraps the standard
4
+ * `Decoder` so bytes emitted by `InputEncoder` land on the bound instance.
5
+ *
6
+ * - `decode(bytes)`: single-input packet (reliable mode).
7
+ * - `decodeAll(bytes, cb)`: multi-input length-framed packet (unreliable
8
+ * mode). Invokes `cb` with the mutated instance once per framed input,
9
+ * oldest → newest. The instance is re-used across callbacks — consume
10
+ * synchronously (apply to game state) rather than holding the reference.
11
+ */
12
+ export declare class InputDecoder<T extends Schema = any> {
13
+ readonly instance: T;
14
+ private readonly _decoder;
15
+ private readonly _it;
16
+ constructor(instance: T);
17
+ /**
18
+ * Decode a single-input (reliable) packet into the bound instance.
19
+ * Returns the instance for chaining.
20
+ */
21
+ decode(bytes: Uint8Array): T;
22
+ /**
23
+ * Walk a multi-input (unreliable) packet, decoding each length-framed
24
+ * input into the bound instance in order and invoking `onInput` after
25
+ * each decode. `onInput` receives the bound instance itself — reads
26
+ * must be synchronous; downstream code should apply the input to game
27
+ * state, not retain the reference.
28
+ *
29
+ * Returns the number of inputs decoded.
30
+ */
31
+ decodeAll(bytes: Uint8Array, onInput: (instance: T, index: number) => void): number;
32
+ }
@@ -0,0 +1,117 @@
1
+ import type { Schema } from "../Schema.js";
2
+ /**
3
+ * Delivery-channel hint. Controls the wire layout:
4
+ * - `"reliable"`: single input per packet, no framing — bytes are
5
+ * wire-compatible with the standard {@link Decoder}.
6
+ * - `"unreliable"`: ring buffer of the last `historySize` inputs packed
7
+ * into one packet, each prefixed with a varint length. Gives the
8
+ * receiver redundancy against dropped packets. Use
9
+ * {@link InputDecoder.decodeAll} on the receiving end.
10
+ */
11
+ export type InputMode = "reliable" | "unreliable";
12
+ export interface InputEncoderOptions {
13
+ /** Defaults to `"reliable"`. */
14
+ mode?: InputMode;
15
+ /**
16
+ * Unreliable-mode only. Number of past inputs to pack into each
17
+ * packet as redundancy against drops. Default: 3. Ignored in
18
+ * reliable mode (always exactly one input per packet).
19
+ */
20
+ historySize?: number;
21
+ /**
22
+ * When `true`, `encode()` emits only fields that changed since the
23
+ * previous call. First call (or first after `reset()`) still emits
24
+ * a full snapshot since there's no baseline to diff against.
25
+ *
26
+ * **Reliable mode:** returns an empty `Uint8Array` when nothing
27
+ * changed — caller can skip sending.
28
+ *
29
+ * **Unreliable mode:** no-change ticks don't push a new ring slot
30
+ * (avoids bloating the ring with empties); the existing ring is
31
+ * still re-emitted for redundancy. Wire ops use absolute values so
32
+ * cross-packet re-application is per-field idempotent.
33
+ *
34
+ * Decoder side is unchanged in either case: `(index|ADD)` wire ops
35
+ * apply to the bound instance; fields absent from a packet stay at
36
+ * their previously decoded value.
37
+ */
38
+ delta?: boolean;
39
+ /**
40
+ * Override the reliable-mode output buffer. Default: 256 bytes,
41
+ * auto-grown on overflow.
42
+ */
43
+ buffer?: Uint8Array;
44
+ }
45
+ /**
46
+ * Bound single-struct encoder for client→server input packets. Holds a
47
+ * reference to a Schema instance and produces wire-compatible bytes.
48
+ *
49
+ * **Reliable mode** emits one snapshot per `encode()`. The bytes decode
50
+ * cleanly through the standard {@link Decoder}.
51
+ *
52
+ * **Unreliable mode** pushes each snapshot onto a ring buffer of size
53
+ * `historySize` and emits the last N snapshots in one packet, each
54
+ * framed with a varint length prefix. Use {@link InputDecoder.decodeAll}
55
+ * to walk the framed packet on the receiving end.
56
+ *
57
+ * **Delta** (`delta: true`, any mode) wraps a standard {@link Encoder}
58
+ * and emits only fields that changed since the last call, via the
59
+ * setter-populated ChangeTree dirty set.
60
+ *
61
+ * Flat primitive fields only. Nested Schema / collection fields throw
62
+ * at construction.
63
+ */
64
+ export declare class InputEncoder<T extends Schema = any> {
65
+ readonly instance: T;
66
+ readonly mode: InputMode;
67
+ readonly historySize: number;
68
+ readonly delta: boolean;
69
+ private readonly _desc;
70
+ private readonly _numFields;
71
+ private _buffer;
72
+ private readonly _it;
73
+ private _slots?;
74
+ private readonly _slotLens?;
75
+ private _slotHead;
76
+ private _slotCount;
77
+ private _outBuffer?;
78
+ private readonly _encoder?;
79
+ constructor(instance: T, options?: InputEncoderOptions);
80
+ /**
81
+ * Encode the bound instance. Returns a subarray of an internal
82
+ * buffer — copy if retaining across calls.
83
+ *
84
+ * Output shape by configuration:
85
+ * - `reliable` + full: one snapshot's worth of bytes.
86
+ * - `reliable` + delta: only changed fields, or empty when nothing
87
+ * changed.
88
+ * - `unreliable` + full: ring of last `historySize` snapshots,
89
+ * length-framed per slot.
90
+ * - `unreliable` + delta: ring of last `historySize` deltas. No-
91
+ * change ticks don't push a new slot but still re-emit the ring.
92
+ * Empty only until the first change has been pushed.
93
+ *
94
+ * Buffers auto-grow on overflow; a one-time `console.warn` is
95
+ * emitted the first time it happens.
96
+ */
97
+ encode(): Uint8Array;
98
+ /**
99
+ * Reset the encoder's internal state:
100
+ * - Unreliable mode: drops the ring buffer.
101
+ * - Delta mode: re-marks every currently populated field as dirty,
102
+ * so the next `encode()` emits a fresh full snapshot.
103
+ *
104
+ * Useful on disconnect / reconnect / scene transitions.
105
+ */
106
+ reset(): void;
107
+ /** Write every populated primitive field into `_buffer`. */
108
+ private _produceFull;
109
+ /** Delegate to the wrapped Encoder, then clear its dirty set. */
110
+ private _produceDelta;
111
+ /** Emit every populated field as `(index|ADD)` + value. */
112
+ private _writeFields;
113
+ private _pushAndEmitRing;
114
+ private _emitRing;
115
+ private static _warned;
116
+ private static _grow;
117
+ }