@colyseus/schema 3.0.0-alpha.19 → 3.0.0-alpha.22

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 (75) hide show
  1. package/build/cjs/index.js +94 -63
  2. package/build/cjs/index.js.map +1 -1
  3. package/build/esm/index.mjs +94 -61
  4. package/build/esm/index.mjs.map +1 -1
  5. package/build/umd/index.js +94 -63
  6. package/lib/Metadata.d.ts +1 -0
  7. package/lib/Metadata.js +32 -1
  8. package/lib/Metadata.js.map +1 -1
  9. package/lib/Reflection.d.ts +0 -1
  10. package/lib/Reflection.js +27 -20
  11. package/lib/Reflection.js.map +1 -1
  12. package/lib/Schema.d.ts +1 -1
  13. package/lib/annotations.js +12 -10
  14. package/lib/annotations.js.map +1 -1
  15. package/lib/codegen/api.js +1 -2
  16. package/lib/codegen/api.js.map +1 -1
  17. package/lib/codegen/languages/cpp.js +1 -2
  18. package/lib/codegen/languages/cpp.js.map +1 -1
  19. package/lib/codegen/languages/csharp.js +1 -2
  20. package/lib/codegen/languages/csharp.js.map +1 -1
  21. package/lib/codegen/languages/haxe.js +1 -2
  22. package/lib/codegen/languages/haxe.js.map +1 -1
  23. package/lib/codegen/languages/java.js +1 -2
  24. package/lib/codegen/languages/java.js.map +1 -1
  25. package/lib/codegen/languages/js.js +1 -2
  26. package/lib/codegen/languages/js.js.map +1 -1
  27. package/lib/codegen/languages/lua.js +1 -2
  28. package/lib/codegen/languages/lua.js.map +1 -1
  29. package/lib/codegen/languages/ts.js +1 -2
  30. package/lib/codegen/languages/ts.js.map +1 -1
  31. package/lib/codegen/parser.js +2 -3
  32. package/lib/codegen/parser.js.map +1 -1
  33. package/lib/codegen/types.js +3 -3
  34. package/lib/codegen/types.js.map +1 -1
  35. package/lib/decoder/DecodeOperation.d.ts +0 -1
  36. package/lib/decoder/DecodeOperation.js +2 -2
  37. package/lib/decoder/DecodeOperation.js.map +1 -1
  38. package/lib/decoder/Decoder.d.ts +0 -1
  39. package/lib/decoder/strategy/RawChanges.js +1 -2
  40. package/lib/decoder/strategy/RawChanges.js.map +1 -1
  41. package/lib/decoder/strategy/StateCallbacks.js +1 -2
  42. package/lib/decoder/strategy/StateCallbacks.js.map +1 -1
  43. package/lib/encoder/EncodeOperation.d.ts +0 -1
  44. package/lib/encoder/EncodeOperation.js +3 -3
  45. package/lib/encoder/EncodeOperation.js.map +1 -1
  46. package/lib/encoder/Encoder.d.ts +0 -1
  47. package/lib/encoder/Encoder.js +5 -5
  48. package/lib/encoder/Encoder.js.map +1 -1
  49. package/lib/encoder/StateView.js +2 -2
  50. package/lib/encoder/StateView.js.map +1 -1
  51. package/lib/encoding/assert.js +3 -3
  52. package/lib/encoding/assert.js.map +1 -1
  53. package/lib/encoding/decode.js +20 -21
  54. package/lib/encoding/decode.js.map +1 -1
  55. package/lib/encoding/encode.d.ts +0 -1
  56. package/lib/encoding/encode.js +17 -17
  57. package/lib/encoding/encode.js.map +1 -1
  58. package/lib/index.d.ts +1 -0
  59. package/lib/index.js.map +1 -1
  60. package/lib/types/custom/ArraySchema.d.ts +2 -2
  61. package/lib/types/custom/ArraySchema.js +0 -8
  62. package/lib/types/custom/ArraySchema.js.map +1 -1
  63. package/lib/types/registry.js +3 -4
  64. package/lib/types/registry.js.map +1 -1
  65. package/lib/types/utils.js +1 -2
  66. package/lib/types/utils.js.map +1 -1
  67. package/lib/utils.js +3 -4
  68. package/lib/utils.js.map +1 -1
  69. package/package.json +5 -5
  70. package/src/Metadata.ts +39 -1
  71. package/src/Reflection.ts +30 -20
  72. package/src/annotations.ts +6 -2
  73. package/src/encoder/Encoder.ts +5 -5
  74. package/src/index.ts +2 -0
  75. package/src/types/custom/ArraySchema.ts +2 -1
@@ -143,10 +143,41 @@ const Metadata = {
143
143
  init(klass) {
144
144
  //
145
145
  // Used only to initialize an empty Schema (Encoder#constructor)
146
+ // TODO: remove/refactor this...
146
147
  //
147
148
  const metadata = {};
148
149
  klass.constructor[Symbol.metadata] = metadata;
149
- Object.defineProperty(metadata, -1, { value: 0, enumerable: false, configurable: true });
150
+ Object.defineProperty(metadata, -1, {
151
+ value: 0,
152
+ enumerable: false,
153
+ configurable: true,
154
+ });
155
+ },
156
+ initialize(constructor, parentMetadata) {
157
+ let metadata = constructor[Symbol.metadata] ?? Object.create(null);
158
+ // make sure inherited classes have their own metadata object.
159
+ if (constructor[Symbol.metadata] === parentMetadata) {
160
+ metadata = Object.create(null);
161
+ if (parentMetadata) {
162
+ // assign parent metadata to current
163
+ Object.assign(metadata, parentMetadata);
164
+ for (let i = 0; i <= parentMetadata[-1]; i++) {
165
+ Object.defineProperty(metadata, i, {
166
+ value: parentMetadata[i],
167
+ enumerable: false,
168
+ configurable: true,
169
+ });
170
+ }
171
+ Object.defineProperty(metadata, -1, {
172
+ value: parentMetadata[-1],
173
+ enumerable: false,
174
+ configurable: true,
175
+ writable: true,
176
+ });
177
+ }
178
+ }
179
+ constructor[Symbol.metadata] = metadata;
180
+ return metadata;
150
181
  },
151
182
  isValidInstance(klass) {
152
183
  return (klass.constructor[Symbol.metadata] &&
@@ -793,23 +824,23 @@ function number$1(bytes, value, it) {
793
824
 
794
825
  var encode = /*#__PURE__*/Object.freeze({
795
826
  __proto__: null,
796
- utf8Length: utf8Length,
797
- utf8Write: utf8Write,
798
- int8: int8$1,
799
- uint8: uint8$1,
827
+ boolean: boolean$1,
828
+ float32: float32$1,
829
+ float64: float64$1,
800
830
  int16: int16$1,
801
- uint16: uint16$1,
802
831
  int32: int32$1,
803
- uint32: uint32$1,
804
832
  int64: int64$1,
833
+ int8: int8$1,
834
+ number: number$1,
835
+ string: string$1,
836
+ uint16: uint16$1,
837
+ uint32: uint32$1,
805
838
  uint64: uint64$1,
806
- float32: float32$1,
807
- float64: float64$1,
839
+ uint8: uint8$1,
840
+ utf8Length: utf8Length,
841
+ utf8Write: utf8Write,
808
842
  writeFloat32: writeFloat32,
809
- writeFloat64: writeFloat64,
810
- boolean: boolean$1,
811
- string: string$1,
812
- number: number$1
843
+ writeFloat64: writeFloat64
813
844
  });
814
845
 
815
846
  class EncodeSchemaError extends Error {
@@ -1242,26 +1273,26 @@ function switchStructureCheck(bytes, it) {
1242
1273
 
1243
1274
  var decode = /*#__PURE__*/Object.freeze({
1244
1275
  __proto__: null,
1245
- utf8Read: utf8Read,
1246
- int8: int8,
1247
- uint8: uint8,
1248
- int16: int16,
1249
- uint16: uint16,
1250
- int32: int32,
1251
- uint32: uint32,
1276
+ arrayCheck: arrayCheck,
1277
+ boolean: boolean,
1252
1278
  float32: float32,
1253
1279
  float64: float64,
1280
+ int16: int16,
1281
+ int32: int32,
1254
1282
  int64: int64,
1255
- uint64: uint64,
1283
+ int8: int8,
1284
+ number: number,
1285
+ numberCheck: numberCheck,
1256
1286
  readFloat32: readFloat32,
1257
1287
  readFloat64: readFloat64,
1258
- boolean: boolean,
1259
1288
  string: string,
1260
1289
  stringCheck: stringCheck,
1261
- number: number,
1262
- numberCheck: numberCheck,
1263
- arrayCheck: arrayCheck,
1264
- switchStructureCheck: switchStructureCheck
1290
+ switchStructureCheck: switchStructureCheck,
1291
+ uint16: uint16,
1292
+ uint32: uint32,
1293
+ uint64: uint64,
1294
+ uint8: uint8,
1295
+ utf8Read: utf8Read
1265
1296
  });
1266
1297
 
1267
1298
  const DEFINITION_MISMATCH = -1;
@@ -1901,14 +1932,6 @@ class ArraySchema {
1901
1932
  lastIndexOf(searchElement, fromIndex = this.length - 1) {
1902
1933
  return this.items.lastIndexOf(searchElement, fromIndex);
1903
1934
  }
1904
- /**
1905
- * Determines whether all the members of an array satisfy the specified test.
1906
- * @param callbackfn A function that accepts up to three arguments. The every method calls
1907
- * the callbackfn function for each element in the array until the callbackfn returns a value
1908
- * which is coercible to the Boolean value false, or until the end of the array.
1909
- * @param thisArg An object to which the this keyword can refer in the callbackfn function.
1910
- * If thisArg is omitted, undefined is used as the this value.
1911
- */
1912
1935
  every(callbackfn, thisArg) {
1913
1936
  return this.items.every(callbackfn, thisArg);
1914
1937
  }
@@ -2587,6 +2610,7 @@ function view(tag = DEFAULT_VIEW_TAG) {
2587
2610
  const constructor = target.constructor;
2588
2611
  const parentClass = Object.getPrototypeOf(constructor);
2589
2612
  const parentMetadata = parentClass[Symbol.metadata];
2613
+ // TODO: use Metadata.initialize()
2590
2614
  const metadata = (constructor[Symbol.metadata] ??= Object.assign({}, constructor[Symbol.metadata], parentMetadata ?? Object.create(null)));
2591
2615
  if (!metadata[fieldName]) {
2592
2616
  //
@@ -2611,8 +2635,8 @@ function type(type, options) {
2611
2635
  // for inheritance support
2612
2636
  TypeContext.register(constructor);
2613
2637
  const parentClass = Object.getPrototypeOf(constructor);
2614
- const parentMetadata = parentClass[Symbol.metadata];
2615
- const metadata = (constructor[Symbol.metadata] ??= Object.assign({}, constructor[Symbol.metadata], parentMetadata ?? Object.create(null)));
2638
+ const parentMetadata = parentClass && parentClass[Symbol.metadata];
2639
+ const metadata = Metadata.initialize(constructor, parentMetadata);
2616
2640
  let fieldIndex;
2617
2641
  /**
2618
2642
  * skip if descriptor already exists for this field (`@deprecated()`)
@@ -3385,6 +3409,8 @@ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
3385
3409
  OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
3386
3410
  PERFORMANCE OF THIS SOFTWARE.
3387
3411
  ***************************************************************************** */
3412
+ /* global Reflect, Promise, SuppressedError, Symbol */
3413
+
3388
3414
 
3389
3415
  function __decorate(decorators, target, key, desc) {
3390
3416
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
@@ -3470,11 +3496,11 @@ class Encoder {
3470
3496
  // try { throw new Error(); } catch (e) {
3471
3497
  // // only print if not coming from Reflection.ts
3472
3498
  // if (!e.stack.includes("src/Reflection.ts")) {
3473
- // // console.log("WILL ENCODE", {
3474
- // // ref: changeTree.ref.constructor.name,
3475
- // // fieldIndex,
3476
- // // operation: OPERATION[operation],
3477
- // // });
3499
+ // console.log("WILL ENCODE", {
3500
+ // ref: changeTree.ref.constructor.name,
3501
+ // fieldIndex,
3502
+ // operation: OPERATION[operation],
3503
+ // });
3478
3504
  // }
3479
3505
  // }
3480
3506
  encoder(this, buffer, changeTree, fieldIndex, operation, it, isEncodeAll, hasView);
@@ -3935,53 +3961,60 @@ class Reflection extends Schema {
3935
3961
  const reflection = new Reflection();
3936
3962
  const reflectionDecoder = new Decoder(reflection);
3937
3963
  reflectionDecoder.decode(bytes, it);
3938
- const context = new TypeContext();
3939
- const schemaTypes = reflection.types.reduce((types, reflectionType) => {
3940
- const parentKlass = types[reflectionType.extendsId] || Schema;
3941
- const schema = class _ extends parentKlass {
3964
+ const typeContext = new TypeContext();
3965
+ // 1st pass, initialize metadata + inheritance
3966
+ reflection.types.forEach((reflectionType) => {
3967
+ const parentClass = typeContext.get(reflectionType.extendsId) ?? Schema;
3968
+ const schema = class _ extends parentClass {
3942
3969
  };
3943
- // const _metadata = Object.create(_classSuper[Symbol.metadata] ?? null);
3944
- const _metadata = parentKlass && parentKlass[Symbol.metadata] || Object.create(null);
3945
- Object.defineProperty(schema, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
3970
+ const parentMetadata = parentClass[Symbol.metadata];
3946
3971
  // register for inheritance support
3947
3972
  TypeContext.register(schema);
3948
- const typeid = reflectionType.id;
3949
- types[typeid] = schema;
3950
- context.add(schema, typeid);
3951
- return types;
3973
+ // for inheritance support
3974
+ Metadata.initialize(schema, parentMetadata);
3975
+ typeContext.add(schema, reflectionType.id);
3952
3976
  }, {});
3977
+ // 2nd pass, set fields
3953
3978
  reflection.types.forEach((reflectionType) => {
3954
- const schemaType = schemaTypes[reflectionType.id];
3979
+ const schemaType = typeContext.get(reflectionType.id);
3955
3980
  const metadata = schemaType[Symbol.metadata];
3956
- const parentKlass = reflection.types[reflectionType.extendsId];
3957
- const parentFieldIndex = parentKlass && parentKlass.fields.length || 0;
3981
+ // FIXME: use metadata[-1] to get field count
3982
+ const parentFieldIndex = 0;
3983
+ // console.log("--------------------");
3984
+ // // console.log("reflectionType", reflectionType.toJSON());
3985
+ // console.log("reflectionType.fields", reflectionType.fields.toJSON());
3986
+ // console.log("parentFieldIndex", parentFieldIndex);
3987
+ //
3988
+ // FIXME: set fields using parentKlass as well
3989
+ // currently the fields are duplicated on inherited classes
3990
+ //
3991
+ // // const parentKlass = reflection.types[reflectionType.extendsId];
3992
+ // // parentKlass.fields
3958
3993
  reflectionType.fields.forEach((field, i) => {
3959
3994
  const fieldIndex = parentFieldIndex + i;
3960
3995
  if (field.referencedType !== undefined) {
3961
3996
  let fieldType = field.type;
3962
- let refType = schemaTypes[field.referencedType];
3997
+ let refType = typeContext.get(field.referencedType);
3963
3998
  // map or array of primitive type (-1)
3964
3999
  if (!refType) {
3965
4000
  const typeInfo = field.type.split(":");
3966
4001
  fieldType = typeInfo[0];
3967
- refType = typeInfo[1];
4002
+ refType = typeInfo[1]; // string
3968
4003
  }
3969
4004
  if (fieldType === "ref") {
3970
- // type(refType)(schemaType.prototype, field.name);
3971
4005
  Metadata.addField(metadata, fieldIndex, field.name, refType);
3972
4006
  }
3973
4007
  else {
3974
- // type({ [fieldType]: refType } as DefinitionType)(schemaType.prototype, field.name);
3975
4008
  Metadata.addField(metadata, fieldIndex, field.name, { [fieldType]: refType });
3976
4009
  }
3977
4010
  }
3978
4011
  else {
3979
- // type(field.type as PrimitiveType)(schemaType.prototype, field.name);
3980
4012
  Metadata.addField(metadata, fieldIndex, field.name, field.type);
3981
4013
  }
3982
4014
  });
3983
4015
  });
3984
- return new (schemaTypes[0])();
4016
+ // @ts-ignore
4017
+ return new (typeContext.get(0))();
3985
4018
  }
3986
4019
  }
3987
4020
  __decorate([