@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
@@ -1,7 +1,5 @@
1
1
  'use strict';
2
2
 
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
3
  const SWITCH_TO_STRUCTURE = 255; // (decoding collides with DELETE_AND_ADD + fieldIndex = 63)
6
4
  const TYPE_ID = 213;
7
5
  /**
@@ -147,10 +145,41 @@ const Metadata = {
147
145
  init(klass) {
148
146
  //
149
147
  // Used only to initialize an empty Schema (Encoder#constructor)
148
+ // TODO: remove/refactor this...
150
149
  //
151
150
  const metadata = {};
152
151
  klass.constructor[Symbol.metadata] = metadata;
153
- Object.defineProperty(metadata, -1, { value: 0, enumerable: false, configurable: true });
152
+ Object.defineProperty(metadata, -1, {
153
+ value: 0,
154
+ enumerable: false,
155
+ configurable: true,
156
+ });
157
+ },
158
+ initialize(constructor, parentMetadata) {
159
+ let metadata = constructor[Symbol.metadata] ?? Object.create(null);
160
+ // make sure inherited classes have their own metadata object.
161
+ if (constructor[Symbol.metadata] === parentMetadata) {
162
+ metadata = Object.create(null);
163
+ if (parentMetadata) {
164
+ // assign parent metadata to current
165
+ Object.assign(metadata, parentMetadata);
166
+ for (let i = 0; i <= parentMetadata[-1]; i++) {
167
+ Object.defineProperty(metadata, i, {
168
+ value: parentMetadata[i],
169
+ enumerable: false,
170
+ configurable: true,
171
+ });
172
+ }
173
+ Object.defineProperty(metadata, -1, {
174
+ value: parentMetadata[-1],
175
+ enumerable: false,
176
+ configurable: true,
177
+ writable: true,
178
+ });
179
+ }
180
+ }
181
+ constructor[Symbol.metadata] = metadata;
182
+ return metadata;
154
183
  },
155
184
  isValidInstance(klass) {
156
185
  return (klass.constructor[Symbol.metadata] &&
@@ -797,23 +826,23 @@ function number$1(bytes, value, it) {
797
826
 
798
827
  var encode = /*#__PURE__*/Object.freeze({
799
828
  __proto__: null,
800
- utf8Length: utf8Length,
801
- utf8Write: utf8Write,
802
- int8: int8$1,
803
- uint8: uint8$1,
829
+ boolean: boolean$1,
830
+ float32: float32$1,
831
+ float64: float64$1,
804
832
  int16: int16$1,
805
- uint16: uint16$1,
806
833
  int32: int32$1,
807
- uint32: uint32$1,
808
834
  int64: int64$1,
835
+ int8: int8$1,
836
+ number: number$1,
837
+ string: string$1,
838
+ uint16: uint16$1,
839
+ uint32: uint32$1,
809
840
  uint64: uint64$1,
810
- float32: float32$1,
811
- float64: float64$1,
841
+ uint8: uint8$1,
842
+ utf8Length: utf8Length,
843
+ utf8Write: utf8Write,
812
844
  writeFloat32: writeFloat32,
813
- writeFloat64: writeFloat64,
814
- boolean: boolean$1,
815
- string: string$1,
816
- number: number$1
845
+ writeFloat64: writeFloat64
817
846
  });
818
847
 
819
848
  class EncodeSchemaError extends Error {
@@ -1246,26 +1275,26 @@ function switchStructureCheck(bytes, it) {
1246
1275
 
1247
1276
  var decode = /*#__PURE__*/Object.freeze({
1248
1277
  __proto__: null,
1249
- utf8Read: utf8Read,
1250
- int8: int8,
1251
- uint8: uint8,
1252
- int16: int16,
1253
- uint16: uint16,
1254
- int32: int32,
1255
- uint32: uint32,
1278
+ arrayCheck: arrayCheck,
1279
+ boolean: boolean,
1256
1280
  float32: float32,
1257
1281
  float64: float64,
1282
+ int16: int16,
1283
+ int32: int32,
1258
1284
  int64: int64,
1259
- uint64: uint64,
1285
+ int8: int8,
1286
+ number: number,
1287
+ numberCheck: numberCheck,
1260
1288
  readFloat32: readFloat32,
1261
1289
  readFloat64: readFloat64,
1262
- boolean: boolean,
1263
1290
  string: string,
1264
1291
  stringCheck: stringCheck,
1265
- number: number,
1266
- numberCheck: numberCheck,
1267
- arrayCheck: arrayCheck,
1268
- switchStructureCheck: switchStructureCheck
1292
+ switchStructureCheck: switchStructureCheck,
1293
+ uint16: uint16,
1294
+ uint32: uint32,
1295
+ uint64: uint64,
1296
+ uint8: uint8,
1297
+ utf8Read: utf8Read
1269
1298
  });
1270
1299
 
1271
1300
  const DEFINITION_MISMATCH = -1;
@@ -1905,14 +1934,6 @@ class ArraySchema {
1905
1934
  lastIndexOf(searchElement, fromIndex = this.length - 1) {
1906
1935
  return this.items.lastIndexOf(searchElement, fromIndex);
1907
1936
  }
1908
- /**
1909
- * Determines whether all the members of an array satisfy the specified test.
1910
- * @param callbackfn A function that accepts up to three arguments. The every method calls
1911
- * the callbackfn function for each element in the array until the callbackfn returns a value
1912
- * which is coercible to the Boolean value false, or until the end of the array.
1913
- * @param thisArg An object to which the this keyword can refer in the callbackfn function.
1914
- * If thisArg is omitted, undefined is used as the this value.
1915
- */
1916
1937
  every(callbackfn, thisArg) {
1917
1938
  return this.items.every(callbackfn, thisArg);
1918
1939
  }
@@ -2591,6 +2612,7 @@ function view(tag = DEFAULT_VIEW_TAG) {
2591
2612
  const constructor = target.constructor;
2592
2613
  const parentClass = Object.getPrototypeOf(constructor);
2593
2614
  const parentMetadata = parentClass[Symbol.metadata];
2615
+ // TODO: use Metadata.initialize()
2594
2616
  const metadata = (constructor[Symbol.metadata] ??= Object.assign({}, constructor[Symbol.metadata], parentMetadata ?? Object.create(null)));
2595
2617
  if (!metadata[fieldName]) {
2596
2618
  //
@@ -2615,8 +2637,8 @@ function type(type, options) {
2615
2637
  // for inheritance support
2616
2638
  TypeContext.register(constructor);
2617
2639
  const parentClass = Object.getPrototypeOf(constructor);
2618
- const parentMetadata = parentClass[Symbol.metadata];
2619
- const metadata = (constructor[Symbol.metadata] ??= Object.assign({}, constructor[Symbol.metadata], parentMetadata ?? Object.create(null)));
2640
+ const parentMetadata = parentClass && parentClass[Symbol.metadata];
2641
+ const metadata = Metadata.initialize(constructor, parentMetadata);
2620
2642
  let fieldIndex;
2621
2643
  /**
2622
2644
  * skip if descriptor already exists for this field (`@deprecated()`)
@@ -3389,6 +3411,8 @@ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
3389
3411
  OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
3390
3412
  PERFORMANCE OF THIS SOFTWARE.
3391
3413
  ***************************************************************************** */
3414
+ /* global Reflect, Promise, SuppressedError, Symbol */
3415
+
3392
3416
 
3393
3417
  function __decorate(decorators, target, key, desc) {
3394
3418
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
@@ -3474,11 +3498,11 @@ class Encoder {
3474
3498
  // try { throw new Error(); } catch (e) {
3475
3499
  // // only print if not coming from Reflection.ts
3476
3500
  // if (!e.stack.includes("src/Reflection.ts")) {
3477
- // // console.log("WILL ENCODE", {
3478
- // // ref: changeTree.ref.constructor.name,
3479
- // // fieldIndex,
3480
- // // operation: OPERATION[operation],
3481
- // // });
3501
+ // console.log("WILL ENCODE", {
3502
+ // ref: changeTree.ref.constructor.name,
3503
+ // fieldIndex,
3504
+ // operation: OPERATION[operation],
3505
+ // });
3482
3506
  // }
3483
3507
  // }
3484
3508
  encoder(this, buffer, changeTree, fieldIndex, operation, it, isEncodeAll, hasView);
@@ -3939,53 +3963,60 @@ class Reflection extends Schema {
3939
3963
  const reflection = new Reflection();
3940
3964
  const reflectionDecoder = new Decoder(reflection);
3941
3965
  reflectionDecoder.decode(bytes, it);
3942
- const context = new TypeContext();
3943
- const schemaTypes = reflection.types.reduce((types, reflectionType) => {
3944
- const parentKlass = types[reflectionType.extendsId] || Schema;
3945
- const schema = class _ extends parentKlass {
3966
+ const typeContext = new TypeContext();
3967
+ // 1st pass, initialize metadata + inheritance
3968
+ reflection.types.forEach((reflectionType) => {
3969
+ const parentClass = typeContext.get(reflectionType.extendsId) ?? Schema;
3970
+ const schema = class _ extends parentClass {
3946
3971
  };
3947
- // const _metadata = Object.create(_classSuper[Symbol.metadata] ?? null);
3948
- const _metadata = parentKlass && parentKlass[Symbol.metadata] || Object.create(null);
3949
- Object.defineProperty(schema, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
3972
+ const parentMetadata = parentClass[Symbol.metadata];
3950
3973
  // register for inheritance support
3951
3974
  TypeContext.register(schema);
3952
- const typeid = reflectionType.id;
3953
- types[typeid] = schema;
3954
- context.add(schema, typeid);
3955
- return types;
3975
+ // for inheritance support
3976
+ Metadata.initialize(schema, parentMetadata);
3977
+ typeContext.add(schema, reflectionType.id);
3956
3978
  }, {});
3979
+ // 2nd pass, set fields
3957
3980
  reflection.types.forEach((reflectionType) => {
3958
- const schemaType = schemaTypes[reflectionType.id];
3981
+ const schemaType = typeContext.get(reflectionType.id);
3959
3982
  const metadata = schemaType[Symbol.metadata];
3960
- const parentKlass = reflection.types[reflectionType.extendsId];
3961
- const parentFieldIndex = parentKlass && parentKlass.fields.length || 0;
3983
+ // FIXME: use metadata[-1] to get field count
3984
+ const parentFieldIndex = 0;
3985
+ // console.log("--------------------");
3986
+ // // console.log("reflectionType", reflectionType.toJSON());
3987
+ // console.log("reflectionType.fields", reflectionType.fields.toJSON());
3988
+ // console.log("parentFieldIndex", parentFieldIndex);
3989
+ //
3990
+ // FIXME: set fields using parentKlass as well
3991
+ // currently the fields are duplicated on inherited classes
3992
+ //
3993
+ // // const parentKlass = reflection.types[reflectionType.extendsId];
3994
+ // // parentKlass.fields
3962
3995
  reflectionType.fields.forEach((field, i) => {
3963
3996
  const fieldIndex = parentFieldIndex + i;
3964
3997
  if (field.referencedType !== undefined) {
3965
3998
  let fieldType = field.type;
3966
- let refType = schemaTypes[field.referencedType];
3999
+ let refType = typeContext.get(field.referencedType);
3967
4000
  // map or array of primitive type (-1)
3968
4001
  if (!refType) {
3969
4002
  const typeInfo = field.type.split(":");
3970
4003
  fieldType = typeInfo[0];
3971
- refType = typeInfo[1];
4004
+ refType = typeInfo[1]; // string
3972
4005
  }
3973
4006
  if (fieldType === "ref") {
3974
- // type(refType)(schemaType.prototype, field.name);
3975
4007
  Metadata.addField(metadata, fieldIndex, field.name, refType);
3976
4008
  }
3977
4009
  else {
3978
- // type({ [fieldType]: refType } as DefinitionType)(schemaType.prototype, field.name);
3979
4010
  Metadata.addField(metadata, fieldIndex, field.name, { [fieldType]: refType });
3980
4011
  }
3981
4012
  }
3982
4013
  else {
3983
- // type(field.type as PrimitiveType)(schemaType.prototype, field.name);
3984
4014
  Metadata.addField(metadata, fieldIndex, field.name, field.type);
3985
4015
  }
3986
4016
  });
3987
4017
  });
3988
- return new (schemaTypes[0])();
4018
+ // @ts-ignore
4019
+ return new (typeContext.get(0))();
3989
4020
  }
3990
4021
  }
3991
4022
  __decorate([