@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
@@ -61,28 +61,7 @@ const $refTypeFieldIndexes = "~__refTypeFieldIndexes";
61
61
  const $viewFieldIndexes = "~__viewFieldIndexes";
62
62
  const $fieldIndexesByViewTag = "$__fieldIndexesByViewTag";
63
63
 
64
- /**
65
- * Copyright (c) 2018 Endel Dreyer
66
- * Copyright (c) 2014 Ion Drive Software Ltd.
67
- *
68
- * Permission is hereby granted, free of charge, to any person obtaining a copy
69
- * of this software and associated documentation files (the "Software"), to deal
70
- * in the Software without restriction, including without limitation the rights
71
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
72
- * copies of the Software, and to permit persons to whom the Software is
73
- * furnished to do so, subject to the following conditions:
74
- *
75
- * The above copyright notice and this permission notice shall be included in all
76
- * copies or substantial portions of the Software.
77
- *
78
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
79
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
80
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
81
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
82
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
83
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
84
- * SOFTWARE
85
- */
64
+ // @ts-nocheck
86
65
  /**
87
66
  * msgpack implementation highly based on notepack.io
88
67
  * https://github.com/darrachequesne/notepack
@@ -350,28 +329,7 @@ const encode = {
350
329
  utf8Length,
351
330
  };
352
331
 
353
- /**
354
- * Copyright (c) 2018 Endel Dreyer
355
- * Copyright (c) 2014 Ion Drive Software Ltd.
356
- *
357
- * Permission is hereby granted, free of charge, to any person obtaining a copy
358
- * of this software and associated documentation files (the "Software"), to deal
359
- * in the Software without restriction, including without limitation the rights
360
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
361
- * copies of the Software, and to permit persons to whom the Software is
362
- * furnished to do so, subject to the following conditions:
363
- *
364
- * The above copyright notice and this permission notice shall be included in all
365
- * copies or substantial portions of the Software.
366
- *
367
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
368
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
369
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
370
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
371
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
372
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
373
- * SOFTWARE
374
- */
332
+ // @ts-nocheck
375
333
  // force little endian to facilitate decoding on multiple implementations
376
334
  const _convoBuffer = new ArrayBuffer(8);
377
335
  const _int32 = new Int32Array(_convoBuffer);
@@ -2746,6 +2704,9 @@ class MapSchema {
2746
2704
  return this.$items.get(key);
2747
2705
  }
2748
2706
  delete(key) {
2707
+ if (!this.$items.has(key)) {
2708
+ return false;
2709
+ }
2749
2710
  const index = this[$changes].indexes[key];
2750
2711
  this.deletedItems[index] = this[$changes].delete(index);
2751
2712
  return this.$items.delete(key);
@@ -3432,6 +3393,10 @@ function getPropertyDescriptor(fieldCached, fieldIndex, type, complexTypeKlass)
3432
3393
  if (complexTypeKlass.constructor === MapSchema && !(value instanceof MapSchema)) {
3433
3394
  value = new MapSchema(value);
3434
3395
  }
3396
+ // // automaticallty transform Array into SetSchema
3397
+ // if (complexTypeKlass.constructor === SetSchema && !(value instanceof SetSchema)) {
3398
+ // value = new SetSchema(value);
3399
+ // }
3435
3400
  value[$childType] = type;
3436
3401
  }
3437
3402
  else if (typeof (type) !== "string") {
@@ -3576,31 +3541,37 @@ function schema(fieldsAndMethods, name, inherits = Schema) {
3576
3541
  }
3577
3542
  const getDefaultValues = () => {
3578
3543
  const defaults = {};
3544
+ // use current class default values
3579
3545
  for (const fieldName in defaultValues) {
3580
3546
  const defaultValue = defaultValues[fieldName];
3581
- // If the default value has a clone method, use it to get a fresh instance
3582
3547
  if (defaultValue && typeof defaultValue.clone === 'function') {
3548
+ // complex, cloneable values, e.g. Schema, ArraySchema, MapSchema, CollectionSchema, SetSchema
3583
3549
  defaults[fieldName] = defaultValue.clone();
3584
3550
  }
3585
3551
  else {
3586
- // Otherwise, use the value as-is (for primitives and non-cloneable objects)
3552
+ // primitives and non-cloneable values
3587
3553
  defaults[fieldName] = defaultValue;
3588
3554
  }
3589
3555
  }
3590
3556
  return defaults;
3591
3557
  };
3558
+ /** @codegen-ignore */
3592
3559
  const klass = Metadata.setFields(class extends inherits {
3593
3560
  constructor(...args) {
3594
- args[0] = Object.assign({}, getDefaultValues(), args[0]);
3595
- super(...args);
3561
+ super(Object.assign({}, getDefaultValues(), args[0] || {}));
3562
+ // call initialize method
3563
+ if (methods.initialize && typeof methods.initialize === 'function') {
3564
+ methods.initialize.apply(this, args);
3565
+ }
3596
3566
  }
3597
3567
  }, fields);
3568
+ // Store the getDefaultValues function on the class for inheritance
3569
+ klass._getDefaultValues = getDefaultValues;
3570
+ // Add methods to the prototype
3571
+ Object.assign(klass.prototype, methods);
3598
3572
  for (let fieldName in viewTagFields) {
3599
3573
  view(viewTagFields[fieldName])(klass.prototype, fieldName);
3600
3574
  }
3601
- for (let methodName in methods) {
3602
- klass.prototype[methodName] = methods[methodName];
3603
- }
3604
3575
  if (name) {
3605
3576
  Object.defineProperty(klass, "name", { value: name });
3606
3577
  }
@@ -3662,8 +3633,6 @@ class Schema {
3662
3633
  }
3663
3634
  static is(type) {
3664
3635
  return typeof (type[Symbol.metadata]) === "object";
3665
- // const metadata = type[Symbol.metadata];
3666
- // return metadata && Object.prototype.hasOwnProperty.call(metadata, -1);
3667
3636
  }
3668
3637
  /**
3669
3638
  * Track property changes
@@ -3702,7 +3671,7 @@ class Schema {
3702
3671
  }
3703
3672
  }
3704
3673
  // allow inherited classes to have a constructor
3705
- constructor(...args) {
3674
+ constructor(arg) {
3706
3675
  //
3707
3676
  // inline
3708
3677
  // Schema.initialize(this);
@@ -3711,8 +3680,8 @@ class Schema {
3711
3680
  //
3712
3681
  // Assign initial values
3713
3682
  //
3714
- if (args[0]) {
3715
- Object.assign(this, args[0]);
3683
+ if (arg) {
3684
+ Object.assign(this, arg);
3716
3685
  }
3717
3686
  }
3718
3687
  assign(props) {
@@ -3737,7 +3706,6 @@ class Schema {
3737
3706
  //
3738
3707
  // for (const field in this) {
3739
3708
  for (const fieldIndex in metadata) {
3740
- // const field = metadata[metadata[fieldIndex]].name;
3741
3709
  const field = metadata[fieldIndex].name;
3742
3710
  if (typeof (this[field]) === "object" &&
3743
3711
  typeof (this[field]?.clone) === "function") {
@@ -4292,27 +4260,6 @@ class Encoder {
4292
4260
  bytes.subarray(viewOffset, it.offset)
4293
4261
  ]);
4294
4262
  }
4295
- debugChanges(field) {
4296
- const rootChangeSet = (typeof (field) === "string")
4297
- ? this.root[field]
4298
- : field;
4299
- let current = rootChangeSet.next;
4300
- while (current) {
4301
- const changeTree = current.changeTree;
4302
- const changeSet = changeTree[field];
4303
- const metadata = changeTree.ref.constructor[Symbol.metadata];
4304
- console.log("->", { ref: changeTree.ref.constructor.name, refId: changeTree.refId, changes: Object.keys(changeSet).length });
4305
- for (const index in changeSet) {
4306
- const op = changeSet[index];
4307
- console.log(" ->", {
4308
- index,
4309
- field: metadata?.[index],
4310
- op: exports.OPERATION[op],
4311
- });
4312
- }
4313
- current = current.next;
4314
- }
4315
- }
4316
4263
  encodeView(view, sharedOffset, it, bytes = this.sharedBuffer) {
4317
4264
  const viewOffset = it.offset;
4318
4265
  // encode visibility changes (add/remove for this view)
@@ -4777,7 +4724,8 @@ class Reflection extends Schema {
4777
4724
  pendingReflectionTypes[typeid].forEach((type) => reflection.types.push(type));
4778
4725
  }
4779
4726
  const buf = reflectionEncoder.encodeAll(it);
4780
- return Buffer.from(buf, 0, it.offset);
4727
+ return buf.slice(0, it.offset);
4728
+ // return Buffer.from(buf, 0, it.offset);
4781
4729
  }
4782
4730
  /**
4783
4731
  * Decodes the TypeContext from a buffer into a Decoder instance.