@colyseus/schema 3.0.61 → 3.0.63

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 (101) hide show
  1. package/build/cjs/index.js +27 -79
  2. package/build/cjs/index.js.map +1 -1
  3. package/build/esm/index.mjs +27 -79
  4. package/build/esm/index.mjs.map +1 -1
  5. package/build/umd/index.js +27 -79
  6. package/lib/Metadata.d.ts +2 -2
  7. package/lib/Metadata.js.map +1 -1
  8. package/lib/Reflection.js +2 -1
  9. package/lib/Reflection.js.map +1 -1
  10. package/lib/Schema.d.ts +14 -14
  11. package/lib/Schema.js +4 -7
  12. package/lib/Schema.js.map +1 -1
  13. package/lib/annotations.d.ts +37 -5
  14. package/lib/annotations.js +17 -7
  15. package/lib/annotations.js.map +1 -1
  16. package/lib/codegen/api.js.map +1 -1
  17. package/lib/codegen/argv.d.ts +1 -1
  18. package/lib/codegen/argv.js.map +1 -1
  19. package/lib/codegen/cli.js.map +1 -1
  20. package/lib/codegen/languages/cpp.js.map +1 -1
  21. package/lib/codegen/languages/csharp.js.map +1 -1
  22. package/lib/codegen/languages/haxe.js.map +1 -1
  23. package/lib/codegen/languages/java.js.map +1 -1
  24. package/lib/codegen/languages/js.js.map +1 -1
  25. package/lib/codegen/languages/lua.js.map +1 -1
  26. package/lib/codegen/languages/ts.js.map +1 -1
  27. package/lib/codegen/parser.js +11 -0
  28. package/lib/codegen/parser.js.map +1 -1
  29. package/lib/codegen/types.d.ts +1 -1
  30. package/lib/codegen/types.js.map +1 -1
  31. package/lib/decoder/DecodeOperation.d.ts +2 -2
  32. package/lib/decoder/DecodeOperation.js.map +1 -1
  33. package/lib/decoder/Decoder.js.map +1 -1
  34. package/lib/decoder/ReferenceTracker.js.map +1 -1
  35. package/lib/decoder/strategy/StateCallbacks.js.map +1 -1
  36. package/lib/encoder/ChangeTree.d.ts +6 -1
  37. package/lib/encoder/ChangeTree.js.map +1 -1
  38. package/lib/encoder/EncodeOperation.js.map +1 -1
  39. package/lib/encoder/Encoder.d.ts +0 -1
  40. package/lib/encoder/Encoder.js +0 -21
  41. package/lib/encoder/Encoder.js.map +1 -1
  42. package/lib/encoder/StateView.js.map +1 -1
  43. package/lib/encoding/decode.js +1 -22
  44. package/lib/encoding/decode.js.map +1 -1
  45. package/lib/encoding/encode.d.ts +1 -23
  46. package/lib/encoding/encode.js +1 -22
  47. package/lib/encoding/encode.js.map +1 -1
  48. package/lib/index.d.ts +3 -2
  49. package/lib/index.js.map +1 -1
  50. package/lib/types/HelperTypes.d.ts +14 -5
  51. package/lib/types/HelperTypes.js.map +1 -1
  52. package/lib/types/TypeContext.js.map +1 -1
  53. package/lib/types/custom/ArraySchema.d.ts +9 -5
  54. package/lib/types/custom/ArraySchema.js.map +1 -1
  55. package/lib/types/custom/CollectionSchema.d.ts +8 -4
  56. package/lib/types/custom/CollectionSchema.js.map +1 -1
  57. package/lib/types/custom/MapSchema.d.ts +8 -6
  58. package/lib/types/custom/MapSchema.js +3 -0
  59. package/lib/types/custom/MapSchema.js.map +1 -1
  60. package/lib/types/custom/SetSchema.d.ts +8 -4
  61. package/lib/types/custom/SetSchema.js.map +1 -1
  62. package/lib/types/registry.js.map +1 -1
  63. package/lib/utils.js.map +1 -1
  64. package/lib/v3_bench.js.map +1 -1
  65. package/package.json +2 -2
  66. package/src/Metadata.ts +2 -2
  67. package/src/Reflection.ts +5 -4
  68. package/src/Schema.ts +35 -37
  69. package/src/annotations.ts +93 -30
  70. package/src/codegen/api.ts +1 -1
  71. package/src/codegen/argv.ts +5 -5
  72. package/src/codegen/cli.ts +1 -1
  73. package/src/codegen/languages/cpp.ts +5 -4
  74. package/src/codegen/languages/csharp.ts +2 -2
  75. package/src/codegen/languages/haxe.ts +2 -2
  76. package/src/codegen/languages/java.ts +2 -2
  77. package/src/codegen/languages/js.ts +3 -2
  78. package/src/codegen/languages/lua.ts +3 -2
  79. package/src/codegen/languages/ts.ts +3 -2
  80. package/src/codegen/parser.ts +12 -0
  81. package/src/codegen/types.ts +2 -2
  82. package/src/decoder/DecodeOperation.ts +21 -21
  83. package/src/decoder/Decoder.ts +3 -3
  84. package/src/decoder/ReferenceTracker.ts +7 -5
  85. package/src/decoder/strategy/StateCallbacks.ts +10 -10
  86. package/src/encoder/ChangeTree.ts +21 -15
  87. package/src/encoder/EncodeOperation.ts +4 -4
  88. package/src/encoder/Encoder.ts +0 -24
  89. package/src/encoder/StateView.ts +3 -2
  90. package/src/encoding/decode.ts +2 -0
  91. package/src/encoding/encode.ts +3 -1
  92. package/src/index.ts +5 -2
  93. package/src/types/HelperTypes.ts +29 -7
  94. package/src/types/TypeContext.ts +1 -1
  95. package/src/types/custom/ArraySchema.ts +12 -10
  96. package/src/types/custom/CollectionSchema.ts +9 -6
  97. package/src/types/custom/MapSchema.ts +12 -8
  98. package/src/types/custom/SetSchema.ts +9 -6
  99. package/src/types/registry.ts +2 -2
  100. package/src/utils.ts +2 -4
  101. package/src/v3_bench.ts +1 -1
@@ -59,28 +59,7 @@ const $refTypeFieldIndexes = "~__refTypeFieldIndexes";
59
59
  const $viewFieldIndexes = "~__viewFieldIndexes";
60
60
  const $fieldIndexesByViewTag = "$__fieldIndexesByViewTag";
61
61
 
62
- /**
63
- * Copyright (c) 2018 Endel Dreyer
64
- * Copyright (c) 2014 Ion Drive Software Ltd.
65
- *
66
- * Permission is hereby granted, free of charge, to any person obtaining a copy
67
- * of this software and associated documentation files (the "Software"), to deal
68
- * in the Software without restriction, including without limitation the rights
69
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
70
- * copies of the Software, and to permit persons to whom the Software is
71
- * furnished to do so, subject to the following conditions:
72
- *
73
- * The above copyright notice and this permission notice shall be included in all
74
- * copies or substantial portions of the Software.
75
- *
76
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
77
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
78
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
79
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
80
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
81
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
82
- * SOFTWARE
83
- */
62
+ // @ts-nocheck
84
63
  /**
85
64
  * msgpack implementation highly based on notepack.io
86
65
  * https://github.com/darrachequesne/notepack
@@ -348,28 +327,7 @@ const encode = {
348
327
  utf8Length,
349
328
  };
350
329
 
351
- /**
352
- * Copyright (c) 2018 Endel Dreyer
353
- * Copyright (c) 2014 Ion Drive Software Ltd.
354
- *
355
- * Permission is hereby granted, free of charge, to any person obtaining a copy
356
- * of this software and associated documentation files (the "Software"), to deal
357
- * in the Software without restriction, including without limitation the rights
358
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
359
- * copies of the Software, and to permit persons to whom the Software is
360
- * furnished to do so, subject to the following conditions:
361
- *
362
- * The above copyright notice and this permission notice shall be included in all
363
- * copies or substantial portions of the Software.
364
- *
365
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
366
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
367
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
368
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
369
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
370
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
371
- * SOFTWARE
372
- */
330
+ // @ts-nocheck
373
331
  // force little endian to facilitate decoding on multiple implementations
374
332
  const _convoBuffer = new ArrayBuffer(8);
375
333
  const _int32 = new Int32Array(_convoBuffer);
@@ -2744,6 +2702,9 @@ class MapSchema {
2744
2702
  return this.$items.get(key);
2745
2703
  }
2746
2704
  delete(key) {
2705
+ if (!this.$items.has(key)) {
2706
+ return false;
2707
+ }
2747
2708
  const index = this[$changes].indexes[key];
2748
2709
  this.deletedItems[index] = this[$changes].delete(index);
2749
2710
  return this.$items.delete(key);
@@ -3430,6 +3391,10 @@ function getPropertyDescriptor(fieldCached, fieldIndex, type, complexTypeKlass)
3430
3391
  if (complexTypeKlass.constructor === MapSchema && !(value instanceof MapSchema)) {
3431
3392
  value = new MapSchema(value);
3432
3393
  }
3394
+ // // automaticallty transform Array into SetSchema
3395
+ // if (complexTypeKlass.constructor === SetSchema && !(value instanceof SetSchema)) {
3396
+ // value = new SetSchema(value);
3397
+ // }
3433
3398
  value[$childType] = type;
3434
3399
  }
3435
3400
  else if (typeof (type) !== "string") {
@@ -3574,31 +3539,37 @@ function schema(fieldsAndMethods, name, inherits = Schema) {
3574
3539
  }
3575
3540
  const getDefaultValues = () => {
3576
3541
  const defaults = {};
3542
+ // use current class default values
3577
3543
  for (const fieldName in defaultValues) {
3578
3544
  const defaultValue = defaultValues[fieldName];
3579
- // If the default value has a clone method, use it to get a fresh instance
3580
3545
  if (defaultValue && typeof defaultValue.clone === 'function') {
3546
+ // complex, cloneable values, e.g. Schema, ArraySchema, MapSchema, CollectionSchema, SetSchema
3581
3547
  defaults[fieldName] = defaultValue.clone();
3582
3548
  }
3583
3549
  else {
3584
- // Otherwise, use the value as-is (for primitives and non-cloneable objects)
3550
+ // primitives and non-cloneable values
3585
3551
  defaults[fieldName] = defaultValue;
3586
3552
  }
3587
3553
  }
3588
3554
  return defaults;
3589
3555
  };
3556
+ /** @codegen-ignore */
3590
3557
  const klass = Metadata.setFields(class extends inherits {
3591
3558
  constructor(...args) {
3592
- args[0] = Object.assign({}, getDefaultValues(), args[0]);
3593
- super(...args);
3559
+ super(Object.assign({}, getDefaultValues(), args[0] || {}));
3560
+ // call initialize method
3561
+ if (methods.initialize && typeof methods.initialize === 'function') {
3562
+ methods.initialize.apply(this, args);
3563
+ }
3594
3564
  }
3595
3565
  }, fields);
3566
+ // Store the getDefaultValues function on the class for inheritance
3567
+ klass._getDefaultValues = getDefaultValues;
3568
+ // Add methods to the prototype
3569
+ Object.assign(klass.prototype, methods);
3596
3570
  for (let fieldName in viewTagFields) {
3597
3571
  view(viewTagFields[fieldName])(klass.prototype, fieldName);
3598
3572
  }
3599
- for (let methodName in methods) {
3600
- klass.prototype[methodName] = methods[methodName];
3601
- }
3602
3573
  if (name) {
3603
3574
  Object.defineProperty(klass, "name", { value: name });
3604
3575
  }
@@ -3660,8 +3631,6 @@ class Schema {
3660
3631
  }
3661
3632
  static is(type) {
3662
3633
  return typeof (type[Symbol.metadata]) === "object";
3663
- // const metadata = type[Symbol.metadata];
3664
- // return metadata && Object.prototype.hasOwnProperty.call(metadata, -1);
3665
3634
  }
3666
3635
  /**
3667
3636
  * Track property changes
@@ -3700,7 +3669,7 @@ class Schema {
3700
3669
  }
3701
3670
  }
3702
3671
  // allow inherited classes to have a constructor
3703
- constructor(...args) {
3672
+ constructor(arg) {
3704
3673
  //
3705
3674
  // inline
3706
3675
  // Schema.initialize(this);
@@ -3709,8 +3678,8 @@ class Schema {
3709
3678
  //
3710
3679
  // Assign initial values
3711
3680
  //
3712
- if (args[0]) {
3713
- Object.assign(this, args[0]);
3681
+ if (arg) {
3682
+ Object.assign(this, arg);
3714
3683
  }
3715
3684
  }
3716
3685
  assign(props) {
@@ -3735,7 +3704,6 @@ class Schema {
3735
3704
  //
3736
3705
  // for (const field in this) {
3737
3706
  for (const fieldIndex in metadata) {
3738
- // const field = metadata[metadata[fieldIndex]].name;
3739
3707
  const field = metadata[fieldIndex].name;
3740
3708
  if (typeof (this[field]) === "object" &&
3741
3709
  typeof (this[field]?.clone) === "function") {
@@ -4290,27 +4258,6 @@ class Encoder {
4290
4258
  bytes.subarray(viewOffset, it.offset)
4291
4259
  ]);
4292
4260
  }
4293
- debugChanges(field) {
4294
- const rootChangeSet = (typeof (field) === "string")
4295
- ? this.root[field]
4296
- : field;
4297
- let current = rootChangeSet.next;
4298
- while (current) {
4299
- const changeTree = current.changeTree;
4300
- const changeSet = changeTree[field];
4301
- const metadata = changeTree.ref.constructor[Symbol.metadata];
4302
- console.log("->", { ref: changeTree.ref.constructor.name, refId: changeTree.refId, changes: Object.keys(changeSet).length });
4303
- for (const index in changeSet) {
4304
- const op = changeSet[index];
4305
- console.log(" ->", {
4306
- index,
4307
- field: metadata?.[index],
4308
- op: OPERATION[op],
4309
- });
4310
- }
4311
- current = current.next;
4312
- }
4313
- }
4314
4261
  encodeView(view, sharedOffset, it, bytes = this.sharedBuffer) {
4315
4262
  const viewOffset = it.offset;
4316
4263
  // encode visibility changes (add/remove for this view)
@@ -4775,7 +4722,8 @@ class Reflection extends Schema {
4775
4722
  pendingReflectionTypes[typeid].forEach((type) => reflection.types.push(type));
4776
4723
  }
4777
4724
  const buf = reflectionEncoder.encodeAll(it);
4778
- return Buffer.from(buf, 0, it.offset);
4725
+ return buf.slice(0, it.offset);
4726
+ // return Buffer.from(buf, 0, it.offset);
4779
4727
  }
4780
4728
  /**
4781
4729
  * Decodes the TypeContext from a buffer into a Decoder instance.