@colyseus/schema 5.0.1 → 5.0.3

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/build/index.d.ts CHANGED
@@ -22,7 +22,7 @@ export { Metadata } from "./Metadata.js";
22
22
  export { type, deprecated, owned, unreliable, transient, view, schema, entity, type DefinitionType, type PrimitiveType, type Definition, type FieldsAndMethods, type SchemaWithExtendsConstructor, type SchemaWithExtends, type SchemaType, } from "./annotations.js";
23
23
  export { t, FieldBuilder, isBuilder, type BuilderDefinition, type ChildType } from "./types/builder.js";
24
24
  export { TypeContext } from "./types/TypeContext.js";
25
- export type { InferValueType, InferSchemaInstanceType, AssignableProps } from "./types/HelperTypes.js";
25
+ export type { InferValueType, InferSchemaInstanceType, AssignableProps, BuilderInitProps } from "./types/HelperTypes.js";
26
26
  export { getDecoderStateCallbacks, type CallbackProxy, type SchemaCallback, type CollectionCallback, type SchemaCallbackProxy } from "./decoder/strategy/getDecoderStateCallbacks.js";
27
27
  export { Callbacks, StateCallbackStrategy } from "./decoder/strategy/Callbacks.js";
28
28
  export { getRawChangesCallback } from "./decoder/strategy/RawChanges.js";
package/build/index.js CHANGED
@@ -1218,6 +1218,33 @@
1218
1218
  getStreamPriority(metadata, index) {
1219
1219
  return metadata?.[$streamPriorities]?.[index];
1220
1220
  },
1221
+ /**
1222
+ * Install a single field with full encoder wiring: accessor descriptor
1223
+ * on the prototype + `metadata[$encoders]` slot for primitives. Shared
1224
+ * between `Metadata.setFields` (build path) and
1225
+ * `Reflection.makeEncodable` (Reflection upgrade path).
1226
+ */
1227
+ defineField(target, metadata, fieldIndex, fieldName, type) {
1228
+ const normalized = getNormalizedType(type);
1229
+ const { complexTypeKlass, childType } = resolveFieldType(normalized);
1230
+ Metadata.addField(metadata, fieldIndex, fieldName, normalized, getPropertyDescriptor(fieldName, fieldIndex, childType, complexTypeKlass));
1231
+ // Install accessor descriptor on the prototype (once per class field).
1232
+ if (metadata[$descriptors][fieldName]) {
1233
+ Object.defineProperty(target.prototype, fieldName, metadata[$descriptors][fieldName]);
1234
+ }
1235
+ // Pre-compute encoder function for primitive types.
1236
+ if (typeof normalized === "string") {
1237
+ if (!metadata[$encoders]) {
1238
+ Object.defineProperty(metadata, $encoders, {
1239
+ value: [],
1240
+ enumerable: false,
1241
+ configurable: true,
1242
+ writable: true,
1243
+ });
1244
+ }
1245
+ metadata[$encoders][fieldIndex] = encode[normalized];
1246
+ }
1247
+ },
1221
1248
  setFields(target, fields) {
1222
1249
  // for inheritance support
1223
1250
  const constructor = target.prototype.constructor;
@@ -1255,17 +1282,7 @@
1255
1282
  });
1256
1283
  }
1257
1284
  for (const field in fields) {
1258
- const type = getNormalizedType(fields[field]);
1259
- const { complexTypeKlass, childType } = resolveFieldType(type);
1260
- Metadata.addField(metadata, fieldIndex, field, type, getPropertyDescriptor(field, fieldIndex, childType, complexTypeKlass));
1261
- // Install accessor descriptor on the prototype (once per class field).
1262
- if (metadata[$descriptors][field]) {
1263
- Object.defineProperty(target.prototype, field, metadata[$descriptors][field]);
1264
- }
1265
- // Pre-compute encoder function for primitive types.
1266
- if (typeof type === "string") {
1267
- metadata[$encoders][fieldIndex] = encode[type];
1268
- }
1285
+ Metadata.defineField(constructor, metadata, fieldIndex, field, fields[field]);
1269
1286
  fieldIndex++;
1270
1287
  }
1271
1288
  return target;
@@ -8112,6 +8129,31 @@
8112
8129
  const state = new (typeContext.get(reflection.rootType || 0))();
8113
8130
  return new Decoder(state, typeContext);
8114
8131
  };
8132
+ Reflection.makeEncodable = function (ctor) {
8133
+ const metadata = ctor[Symbol.metadata];
8134
+ if (!metadata)
8135
+ return ctor;
8136
+ const numFields = metadata[$numFields];
8137
+ if (numFields === undefined)
8138
+ return ctor;
8139
+ // Walk every field index across the inheritance chain. Repeat calls
8140
+ // are cheap: defineField overwrites the same descriptor and re-stamps
8141
+ // the same `metadata[$encoders]` slot (idempotent).
8142
+ for (let i = 0; i <= numFields; i++) {
8143
+ const field = metadata[i];
8144
+ if (!field)
8145
+ continue;
8146
+ Metadata.defineField(ctor, metadata, i, field.name, field.type);
8147
+ }
8148
+ // Invalidate any cached encode descriptor — `getEncodeDescriptor`
8149
+ // memoizes on the constructor. If something already constructed it
8150
+ // (e.g. a prior `InputEncoder(...)` call that threw), drop the stale
8151
+ // entry so the next read sees the upgraded metadata.
8152
+ if (Object.prototype.hasOwnProperty.call(ctor, $encodeDescriptor)) {
8153
+ delete ctor[$encodeDescriptor];
8154
+ }
8155
+ return ctor;
8156
+ };
8115
8157
 
8116
8158
  /**
8117
8159
  * Legacy callback system
package/build/index.mjs CHANGED
@@ -1212,6 +1212,33 @@ const Metadata = {
1212
1212
  getStreamPriority(metadata, index) {
1213
1213
  return metadata?.[$streamPriorities]?.[index];
1214
1214
  },
1215
+ /**
1216
+ * Install a single field with full encoder wiring: accessor descriptor
1217
+ * on the prototype + `metadata[$encoders]` slot for primitives. Shared
1218
+ * between `Metadata.setFields` (build path) and
1219
+ * `Reflection.makeEncodable` (Reflection upgrade path).
1220
+ */
1221
+ defineField(target, metadata, fieldIndex, fieldName, type) {
1222
+ const normalized = getNormalizedType(type);
1223
+ const { complexTypeKlass, childType } = resolveFieldType(normalized);
1224
+ Metadata.addField(metadata, fieldIndex, fieldName, normalized, getPropertyDescriptor(fieldName, fieldIndex, childType, complexTypeKlass));
1225
+ // Install accessor descriptor on the prototype (once per class field).
1226
+ if (metadata[$descriptors][fieldName]) {
1227
+ Object.defineProperty(target.prototype, fieldName, metadata[$descriptors][fieldName]);
1228
+ }
1229
+ // Pre-compute encoder function for primitive types.
1230
+ if (typeof normalized === "string") {
1231
+ if (!metadata[$encoders]) {
1232
+ Object.defineProperty(metadata, $encoders, {
1233
+ value: [],
1234
+ enumerable: false,
1235
+ configurable: true,
1236
+ writable: true,
1237
+ });
1238
+ }
1239
+ metadata[$encoders][fieldIndex] = encode[normalized];
1240
+ }
1241
+ },
1215
1242
  setFields(target, fields) {
1216
1243
  // for inheritance support
1217
1244
  const constructor = target.prototype.constructor;
@@ -1249,17 +1276,7 @@ const Metadata = {
1249
1276
  });
1250
1277
  }
1251
1278
  for (const field in fields) {
1252
- const type = getNormalizedType(fields[field]);
1253
- const { complexTypeKlass, childType } = resolveFieldType(type);
1254
- Metadata.addField(metadata, fieldIndex, field, type, getPropertyDescriptor(field, fieldIndex, childType, complexTypeKlass));
1255
- // Install accessor descriptor on the prototype (once per class field).
1256
- if (metadata[$descriptors][field]) {
1257
- Object.defineProperty(target.prototype, field, metadata[$descriptors][field]);
1258
- }
1259
- // Pre-compute encoder function for primitive types.
1260
- if (typeof type === "string") {
1261
- metadata[$encoders][fieldIndex] = encode[type];
1262
- }
1279
+ Metadata.defineField(constructor, metadata, fieldIndex, field, fields[field]);
1263
1280
  fieldIndex++;
1264
1281
  }
1265
1282
  return target;
@@ -8106,6 +8123,31 @@ Reflection.decode = function (bytes, it) {
8106
8123
  const state = new (typeContext.get(reflection.rootType || 0))();
8107
8124
  return new Decoder(state, typeContext);
8108
8125
  };
8126
+ Reflection.makeEncodable = function (ctor) {
8127
+ const metadata = ctor[Symbol.metadata];
8128
+ if (!metadata)
8129
+ return ctor;
8130
+ const numFields = metadata[$numFields];
8131
+ if (numFields === undefined)
8132
+ return ctor;
8133
+ // Walk every field index across the inheritance chain. Repeat calls
8134
+ // are cheap: defineField overwrites the same descriptor and re-stamps
8135
+ // the same `metadata[$encoders]` slot (idempotent).
8136
+ for (let i = 0; i <= numFields; i++) {
8137
+ const field = metadata[i];
8138
+ if (!field)
8139
+ continue;
8140
+ Metadata.defineField(ctor, metadata, i, field.name, field.type);
8141
+ }
8142
+ // Invalidate any cached encode descriptor — `getEncodeDescriptor`
8143
+ // memoizes on the constructor. If something already constructed it
8144
+ // (e.g. a prior `InputEncoder(...)` call that threw), drop the stale
8145
+ // entry so the next read sees the upgraded metadata.
8146
+ if (Object.prototype.hasOwnProperty.call(ctor, $encodeDescriptor)) {
8147
+ delete ctor[$encodeDescriptor];
8148
+ }
8149
+ return ctor;
8150
+ };
8109
8151
 
8110
8152
  /**
8111
8153
  * Legacy callback system