@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.
- package/README.md +2 -0
- package/build/Metadata.d.ts +55 -2
- package/build/Reflection.d.ts +24 -30
- package/build/Schema.d.ts +70 -9
- package/build/annotations.d.ts +56 -13
- package/build/codegen/cli.cjs +84 -67
- package/build/codegen/cli.cjs.map +1 -1
- package/build/decoder/DecodeOperation.d.ts +48 -5
- package/build/decoder/Decoder.d.ts +2 -2
- package/build/decoder/strategy/Callbacks.d.ts +1 -1
- package/build/encoder/ChangeRecorder.d.ts +107 -0
- package/build/encoder/ChangeTree.d.ts +218 -69
- package/build/encoder/EncodeDescriptor.d.ts +63 -0
- package/build/encoder/EncodeOperation.d.ts +25 -2
- package/build/encoder/Encoder.d.ts +59 -3
- package/build/encoder/MapJournal.d.ts +62 -0
- package/build/encoder/RefIdAllocator.d.ts +35 -0
- package/build/encoder/Root.d.ts +94 -13
- package/build/encoder/StateView.d.ts +116 -8
- package/build/encoder/changeTree/inheritedFlags.d.ts +34 -0
- package/build/encoder/changeTree/liveIteration.d.ts +3 -0
- package/build/encoder/changeTree/parentChain.d.ts +24 -0
- package/build/encoder/changeTree/treeAttachment.d.ts +13 -0
- package/build/encoder/streaming.d.ts +73 -0
- package/build/encoder/subscriptions.d.ts +25 -0
- package/build/index.cjs +5202 -1552
- package/build/index.cjs.map +1 -1
- package/build/index.d.ts +7 -3
- package/build/index.js +5202 -1552
- package/build/index.mjs +5193 -1552
- package/build/index.mjs.map +1 -1
- package/build/input/InputDecoder.d.ts +32 -0
- package/build/input/InputEncoder.d.ts +117 -0
- package/build/input/index.cjs +7429 -0
- package/build/input/index.cjs.map +1 -0
- package/build/input/index.d.ts +3 -0
- package/build/input/index.mjs +7426 -0
- package/build/input/index.mjs.map +1 -0
- package/build/types/HelperTypes.d.ts +22 -8
- package/build/types/TypeContext.d.ts +9 -0
- package/build/types/builder.d.ts +162 -0
- package/build/types/custom/ArraySchema.d.ts +25 -4
- package/build/types/custom/CollectionSchema.d.ts +30 -2
- package/build/types/custom/MapSchema.d.ts +52 -3
- package/build/types/custom/SetSchema.d.ts +32 -2
- package/build/types/custom/StreamSchema.d.ts +114 -0
- package/build/types/symbols.d.ts +48 -5
- package/package.json +9 -3
- package/src/Metadata.ts +258 -31
- package/src/Reflection.ts +15 -13
- package/src/Schema.ts +176 -134
- package/src/annotations.ts +308 -236
- package/src/bench_bloat.ts +173 -0
- package/src/bench_decode.ts +221 -0
- package/src/bench_decode_mem.ts +165 -0
- package/src/bench_encode.ts +108 -0
- package/src/bench_init.ts +150 -0
- package/src/bench_static.ts +109 -0
- package/src/bench_stream.ts +295 -0
- package/src/bench_view_cmp.ts +142 -0
- package/src/codegen/languages/csharp.ts +0 -24
- package/src/codegen/parser.ts +83 -61
- package/src/decoder/DecodeOperation.ts +168 -63
- package/src/decoder/Decoder.ts +20 -10
- package/src/decoder/ReferenceTracker.ts +4 -0
- package/src/decoder/strategy/Callbacks.ts +30 -26
- package/src/decoder/strategy/getDecoderStateCallbacks.ts +16 -13
- package/src/encoder/ChangeRecorder.ts +276 -0
- package/src/encoder/ChangeTree.ts +674 -519
- package/src/encoder/EncodeDescriptor.ts +213 -0
- package/src/encoder/EncodeOperation.ts +107 -65
- package/src/encoder/Encoder.ts +630 -119
- package/src/encoder/MapJournal.ts +124 -0
- package/src/encoder/RefIdAllocator.ts +68 -0
- package/src/encoder/Root.ts +247 -120
- package/src/encoder/StateView.ts +592 -121
- package/src/encoder/changeTree/inheritedFlags.ts +217 -0
- package/src/encoder/changeTree/liveIteration.ts +74 -0
- package/src/encoder/changeTree/parentChain.ts +131 -0
- package/src/encoder/changeTree/treeAttachment.ts +171 -0
- package/src/encoder/streaming.ts +232 -0
- package/src/encoder/subscriptions.ts +71 -0
- package/src/index.ts +15 -3
- package/src/input/InputDecoder.ts +57 -0
- package/src/input/InputEncoder.ts +303 -0
- package/src/input/index.ts +3 -0
- package/src/types/HelperTypes.ts +21 -9
- package/src/types/TypeContext.ts +14 -2
- package/src/types/builder.ts +285 -0
- package/src/types/custom/ArraySchema.ts +210 -197
- package/src/types/custom/CollectionSchema.ts +115 -35
- package/src/types/custom/MapSchema.ts +162 -58
- package/src/types/custom/SetSchema.ts +128 -39
- package/src/types/custom/StreamSchema.ts +310 -0
- package/src/types/symbols.ts +54 -6
- package/src/utils.ts +4 -6
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
import type { ArraySchema } from "./custom/ArraySchema.js";
|
|
2
|
+
import type { MapSchema } from "./custom/MapSchema.js";
|
|
3
|
+
import type { SetSchema } from "./custom/SetSchema.js";
|
|
4
|
+
import type { CollectionSchema } from "./custom/CollectionSchema.js";
|
|
5
|
+
import type { StreamSchema } from "./custom/StreamSchema.js";
|
|
6
|
+
import type { Schema } from "../Schema.js";
|
|
7
|
+
import type { DefinitionType, RawPrimitiveType } from "../annotations.js";
|
|
8
|
+
import type { InferValueType, Constructor } from "./HelperTypes.js";
|
|
9
|
+
import { $builder } from "./symbols.js";
|
|
10
|
+
import { ARRAY_STREAM_NOT_SUPPORTED } from "../encoder/streaming.js";
|
|
11
|
+
|
|
12
|
+
type CollectionKind = "array" | "map" | "set" | "collection";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Internal record produced by FieldBuilder#toDefinition() and consumed by schema().
|
|
16
|
+
*/
|
|
17
|
+
export interface BuilderDefinition {
|
|
18
|
+
type: DefinitionType;
|
|
19
|
+
default?: any;
|
|
20
|
+
hasDefault: boolean;
|
|
21
|
+
view?: number; // tag value; undefined = no view
|
|
22
|
+
owned?: boolean;
|
|
23
|
+
unreliable?: boolean;
|
|
24
|
+
transient?: boolean;
|
|
25
|
+
deprecated?: boolean;
|
|
26
|
+
deprecatedThrows?: boolean;
|
|
27
|
+
static?: boolean;
|
|
28
|
+
stream?: boolean;
|
|
29
|
+
/** Declaration-scope priority callback for `.stream()` fields. */
|
|
30
|
+
streamPriority?: (view: any, element: any) => number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Type-function that infers the instance value for a builder.
|
|
35
|
+
*/
|
|
36
|
+
export type BuilderOf<T> = FieldBuilder<T>;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Chainable field builder. Instances are produced by `t.*()` factories.
|
|
40
|
+
*
|
|
41
|
+
* The generic parameter T is the runtime/JS type of the field (e.g. `number`,
|
|
42
|
+
* `string`, `ArraySchema<Item>`). schema() reads the internal configuration
|
|
43
|
+
* via `toDefinition()` and wires up metadata through the existing pipeline.
|
|
44
|
+
*/
|
|
45
|
+
export class FieldBuilder<T = unknown> {
|
|
46
|
+
readonly [$builder]: true = true;
|
|
47
|
+
|
|
48
|
+
// Internal configuration. Public so schema() and tests can read it, but not
|
|
49
|
+
// meant to be mutated by users directly.
|
|
50
|
+
_type: DefinitionType;
|
|
51
|
+
_default: any = undefined;
|
|
52
|
+
_hasDefault = false;
|
|
53
|
+
_view: number | undefined = undefined;
|
|
54
|
+
_owned = false;
|
|
55
|
+
_unreliable = false;
|
|
56
|
+
_transient = false;
|
|
57
|
+
_deprecated = false;
|
|
58
|
+
_deprecatedThrows = true;
|
|
59
|
+
_static = false;
|
|
60
|
+
_stream = false;
|
|
61
|
+
_streamPriority: ((view: any, element: any) => number) | undefined = undefined;
|
|
62
|
+
|
|
63
|
+
constructor(type: DefinitionType) {
|
|
64
|
+
this._type = type;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Provide a default value for this field. */
|
|
68
|
+
default(value: T): this {
|
|
69
|
+
this._default = value;
|
|
70
|
+
this._hasDefault = true;
|
|
71
|
+
return this;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Tag this field with a view tag (DEFAULT_VIEW_TAG when called without arg). */
|
|
75
|
+
view(tag?: number): this {
|
|
76
|
+
// -1 is DEFAULT_VIEW_TAG; kept numeric here to avoid a circular import.
|
|
77
|
+
this._view = tag ?? -1;
|
|
78
|
+
return this;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Mark this field as owned (encoder-side ownership filtering). */
|
|
82
|
+
owned(): this {
|
|
83
|
+
this._owned = true;
|
|
84
|
+
return this;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Mark this field as unreliable — tick patches emit it on the unreliable
|
|
89
|
+
* transport channel. Still persisted to full-sync snapshots unless also
|
|
90
|
+
* tagged with `.transient()`.
|
|
91
|
+
*/
|
|
92
|
+
unreliable(): this {
|
|
93
|
+
this._unreliable = true;
|
|
94
|
+
return this;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Mark this field as transient — NOT persisted to full-sync snapshots
|
|
99
|
+
* (`encodeAll` / `encodeAllView`). Late-joining clients see the field
|
|
100
|
+
* only after its next mutation is emitted on a tick patch. Orthogonal
|
|
101
|
+
* to `.unreliable()`.
|
|
102
|
+
*/
|
|
103
|
+
transient(): this {
|
|
104
|
+
this._transient = true;
|
|
105
|
+
return this;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Mark this field as static.
|
|
110
|
+
* - Primitive / Schema fields: synchronized once, skips change tracking.
|
|
111
|
+
* - Stream fields (`t.stream(X).static()`): child elements are frozen
|
|
112
|
+
* after add — post-add field mutations on elements become no-ops.
|
|
113
|
+
*/
|
|
114
|
+
static(): this {
|
|
115
|
+
this._static = true;
|
|
116
|
+
return this;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Opt a collection field into priority-batched streaming delivery —
|
|
121
|
+
* ADDs drain at most `maxPerTick` per tick per view (or per broadcast
|
|
122
|
+
* tick without a view). Applies to `t.map(X)` / `t.set(X)` /
|
|
123
|
+
* `t.collection(X)`. Redundant on `t.stream(X)` (the factory already
|
|
124
|
+
* sets this flag).
|
|
125
|
+
*
|
|
126
|
+
* **Not supported on `t.array(X)`.** Array positional operations
|
|
127
|
+
* (`splice`, `unshift`, `reverse`) shift every subsequent index —
|
|
128
|
+
* holding some ADDs back for a later tick while indexes mutate
|
|
129
|
+
* underneath would produce a decoder-side state that doesn't match
|
|
130
|
+
* the server. Use `t.stream(X)` (stable monotonic positions) or
|
|
131
|
+
* `t.map(X).stream()` (keys never shift) instead.
|
|
132
|
+
*/
|
|
133
|
+
stream(): this {
|
|
134
|
+
const t = this._type as any;
|
|
135
|
+
if (t && typeof t === "object" && t.array !== undefined) {
|
|
136
|
+
throw new Error(ARRAY_STREAM_NOT_SUPPORTED);
|
|
137
|
+
}
|
|
138
|
+
this._stream = true;
|
|
139
|
+
return this;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Attach a priority callback for per-view `encodeView` delivery. The
|
|
144
|
+
* callback receives the client's StateView and the candidate element;
|
|
145
|
+
* higher return values emit first. Does nothing in broadcast mode
|
|
146
|
+
* (shared `encode()` drains FIFO). Only meaningful on stream fields.
|
|
147
|
+
*
|
|
148
|
+
* ```ts
|
|
149
|
+
* t.stream(Enemy).priority((view, enemy) =>
|
|
150
|
+
* -dist2(view.anchor, enemy)
|
|
151
|
+
* )
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
154
|
+
priority<V = any>(fn: (view: any, element: V) => number): this {
|
|
155
|
+
this._streamPriority = fn as (view: any, element: any) => number;
|
|
156
|
+
return this;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** Mark this field as deprecated. Pass `false` to silence the access error. */
|
|
160
|
+
deprecated(throws = true): this {
|
|
161
|
+
this._deprecated = true;
|
|
162
|
+
this._deprecatedThrows = throws;
|
|
163
|
+
return this;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
toDefinition(): BuilderDefinition {
|
|
167
|
+
return {
|
|
168
|
+
type: this._type,
|
|
169
|
+
default: this._default,
|
|
170
|
+
hasDefault: this._hasDefault,
|
|
171
|
+
view: this._view,
|
|
172
|
+
owned: this._owned,
|
|
173
|
+
unreliable: this._unreliable,
|
|
174
|
+
transient: this._transient,
|
|
175
|
+
deprecated: this._deprecated,
|
|
176
|
+
deprecatedThrows: this._deprecatedThrows,
|
|
177
|
+
static: this._static,
|
|
178
|
+
stream: this._stream,
|
|
179
|
+
streamPriority: this._streamPriority,
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export function isBuilder(value: any): value is FieldBuilder<any> {
|
|
185
|
+
return value != null && value[$builder] === true;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// ---------------------------------------------------------------------------
|
|
189
|
+
// Factory helpers
|
|
190
|
+
// ---------------------------------------------------------------------------
|
|
191
|
+
|
|
192
|
+
function primitive<T>(name: RawPrimitiveType): () => FieldBuilder<T> {
|
|
193
|
+
return () => new FieldBuilder<T>(name);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// Accepts a Schema class, a primitive string, or another FieldBuilder as a child type.
|
|
197
|
+
export type ChildType =
|
|
198
|
+
| RawPrimitiveType
|
|
199
|
+
| Constructor<Schema>
|
|
200
|
+
| FieldBuilder<any>;
|
|
201
|
+
|
|
202
|
+
function resolveChild(child: ChildType): DefinitionType {
|
|
203
|
+
if (isBuilder(child)) {
|
|
204
|
+
return child._type;
|
|
205
|
+
}
|
|
206
|
+
return child as DefinitionType;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// ---------------------------------------------------------------------------
|
|
210
|
+
// t namespace
|
|
211
|
+
// ---------------------------------------------------------------------------
|
|
212
|
+
|
|
213
|
+
// Overloaded factories for collections. Implementation lives in a single function;
|
|
214
|
+
// overloads narrow the return type for Schema/primitive/builder children.
|
|
215
|
+
interface ArrayFactory {
|
|
216
|
+
<C extends Constructor<Schema>>(child: C): FieldBuilder<ArraySchema<InstanceType<C>>>;
|
|
217
|
+
<P extends RawPrimitiveType>(child: P): FieldBuilder<ArraySchema<InferValueType<P>>>;
|
|
218
|
+
<V>(child: FieldBuilder<V>): FieldBuilder<ArraySchema<V>>;
|
|
219
|
+
}
|
|
220
|
+
interface MapFactory {
|
|
221
|
+
<C extends Constructor<Schema>>(child: C): FieldBuilder<MapSchema<InstanceType<C>>>;
|
|
222
|
+
<P extends RawPrimitiveType>(child: P): FieldBuilder<MapSchema<InferValueType<P>>>;
|
|
223
|
+
<V>(child: FieldBuilder<V>): FieldBuilder<MapSchema<V>>;
|
|
224
|
+
}
|
|
225
|
+
interface SetFactory {
|
|
226
|
+
<C extends Constructor<Schema>>(child: C): FieldBuilder<SetSchema<InstanceType<C>>>;
|
|
227
|
+
<P extends RawPrimitiveType>(child: P): FieldBuilder<SetSchema<InferValueType<P>>>;
|
|
228
|
+
<V>(child: FieldBuilder<V>): FieldBuilder<SetSchema<V>>;
|
|
229
|
+
}
|
|
230
|
+
interface CollectionFactory {
|
|
231
|
+
<C extends Constructor<Schema>>(child: C): FieldBuilder<CollectionSchema<InstanceType<C>>>;
|
|
232
|
+
<P extends RawPrimitiveType>(child: P): FieldBuilder<CollectionSchema<InferValueType<P>>>;
|
|
233
|
+
<V>(child: FieldBuilder<V>): FieldBuilder<CollectionSchema<V>>;
|
|
234
|
+
}
|
|
235
|
+
// t.stream(Entity) — priority-batched collection of Schema instances.
|
|
236
|
+
// Element type is restricted to Schema subclasses (no primitives) because
|
|
237
|
+
// priority batching relies on stable refIds, which primitives don't carry.
|
|
238
|
+
interface StreamFactory {
|
|
239
|
+
<C extends Constructor<Schema>>(child: C): FieldBuilder<StreamSchema<InstanceType<C>>>;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const arrayFactory: ArrayFactory = ((child: ChildType) =>
|
|
243
|
+
new FieldBuilder({ array: resolveChild(child) } as DefinitionType)) as ArrayFactory;
|
|
244
|
+
const mapFactory: MapFactory = ((child: ChildType) =>
|
|
245
|
+
new FieldBuilder({ map: resolveChild(child) } as DefinitionType)) as MapFactory;
|
|
246
|
+
const setFactory: SetFactory = ((child: ChildType) =>
|
|
247
|
+
new FieldBuilder({ set: resolveChild(child) } as DefinitionType)) as SetFactory;
|
|
248
|
+
const collectionFactory: CollectionFactory = ((child: ChildType) =>
|
|
249
|
+
new FieldBuilder({ collection: resolveChild(child) } as DefinitionType)) as CollectionFactory;
|
|
250
|
+
const streamFactory: StreamFactory = ((child: ChildType) => {
|
|
251
|
+
const b = new FieldBuilder({ stream: resolveChild(child) } as DefinitionType);
|
|
252
|
+
b._stream = true;
|
|
253
|
+
return b;
|
|
254
|
+
}) as StreamFactory;
|
|
255
|
+
|
|
256
|
+
function refFactory<C extends Constructor<Schema>>(ctor: C): FieldBuilder<InstanceType<C>> {
|
|
257
|
+
return new FieldBuilder<InstanceType<C>>(ctor as unknown as DefinitionType);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
export const t = Object.freeze({
|
|
261
|
+
// Primitives
|
|
262
|
+
string: primitive<string>("string"),
|
|
263
|
+
number: primitive<number>("number"),
|
|
264
|
+
boolean: primitive<boolean>("boolean"),
|
|
265
|
+
int8: primitive<number>("int8"),
|
|
266
|
+
uint8: primitive<number>("uint8"),
|
|
267
|
+
int16: primitive<number>("int16"),
|
|
268
|
+
uint16: primitive<number>("uint16"),
|
|
269
|
+
int32: primitive<number>("int32"),
|
|
270
|
+
uint32: primitive<number>("uint32"),
|
|
271
|
+
int64: primitive<number>("int64"),
|
|
272
|
+
uint64: primitive<number>("uint64"),
|
|
273
|
+
float32: primitive<number>("float32"),
|
|
274
|
+
float64: primitive<number>("float64"),
|
|
275
|
+
bigint64: primitive<bigint>("bigint64"),
|
|
276
|
+
biguint64: primitive<bigint>("biguint64"),
|
|
277
|
+
|
|
278
|
+
/** Reference to a Schema subtype. `t.array(Item)` usually reads better, but this is available when a plain ref is needed. */
|
|
279
|
+
ref: refFactory,
|
|
280
|
+
array: arrayFactory,
|
|
281
|
+
map: mapFactory,
|
|
282
|
+
set: setFactory,
|
|
283
|
+
collection: collectionFactory,
|
|
284
|
+
stream: streamFactory,
|
|
285
|
+
});
|