@colyseus/schema 3.0.62 → 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 (100) hide show
  1. package/build/cjs/index.js +24 -79
  2. package/build/cjs/index.js.map +1 -1
  3. package/build/esm/index.mjs +24 -79
  4. package/build/esm/index.mjs.map +1 -1
  5. package/build/umd/index.js +24 -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.map +1 -1
  59. package/lib/types/custom/SetSchema.d.ts +8 -4
  60. package/lib/types/custom/SetSchema.js.map +1 -1
  61. package/lib/types/registry.js.map +1 -1
  62. package/lib/utils.js.map +1 -1
  63. package/lib/v3_bench.js.map +1 -1
  64. package/package.json +2 -2
  65. package/src/Metadata.ts +2 -2
  66. package/src/Reflection.ts +5 -4
  67. package/src/Schema.ts +35 -37
  68. package/src/annotations.ts +93 -30
  69. package/src/codegen/api.ts +1 -1
  70. package/src/codegen/argv.ts +5 -5
  71. package/src/codegen/cli.ts +1 -1
  72. package/src/codegen/languages/cpp.ts +5 -4
  73. package/src/codegen/languages/csharp.ts +2 -2
  74. package/src/codegen/languages/haxe.ts +2 -2
  75. package/src/codegen/languages/java.ts +2 -2
  76. package/src/codegen/languages/js.ts +3 -2
  77. package/src/codegen/languages/lua.ts +3 -2
  78. package/src/codegen/languages/ts.ts +3 -2
  79. package/src/codegen/parser.ts +12 -0
  80. package/src/codegen/types.ts +2 -2
  81. package/src/decoder/DecodeOperation.ts +21 -21
  82. package/src/decoder/Decoder.ts +3 -3
  83. package/src/decoder/ReferenceTracker.ts +7 -5
  84. package/src/decoder/strategy/StateCallbacks.ts +10 -10
  85. package/src/encoder/ChangeTree.ts +21 -15
  86. package/src/encoder/EncodeOperation.ts +4 -4
  87. package/src/encoder/Encoder.ts +0 -24
  88. package/src/encoder/StateView.ts +3 -2
  89. package/src/encoding/decode.ts +2 -0
  90. package/src/encoding/encode.ts +3 -1
  91. package/src/index.ts +5 -2
  92. package/src/types/HelperTypes.ts +29 -7
  93. package/src/types/TypeContext.ts +1 -1
  94. package/src/types/custom/ArraySchema.ts +12 -10
  95. package/src/types/custom/CollectionSchema.ts +9 -6
  96. package/src/types/custom/MapSchema.ts +8 -8
  97. package/src/types/custom/SetSchema.ts +9 -6
  98. package/src/types/registry.ts +2 -2
  99. package/src/utils.ts +2 -4
  100. 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);
@@ -3433,6 +3391,10 @@ function getPropertyDescriptor(fieldCached, fieldIndex, type, complexTypeKlass)
3433
3391
  if (complexTypeKlass.constructor === MapSchema && !(value instanceof MapSchema)) {
3434
3392
  value = new MapSchema(value);
3435
3393
  }
3394
+ // // automaticallty transform Array into SetSchema
3395
+ // if (complexTypeKlass.constructor === SetSchema && !(value instanceof SetSchema)) {
3396
+ // value = new SetSchema(value);
3397
+ // }
3436
3398
  value[$childType] = type;
3437
3399
  }
3438
3400
  else if (typeof (type) !== "string") {
@@ -3577,31 +3539,37 @@ function schema(fieldsAndMethods, name, inherits = Schema) {
3577
3539
  }
3578
3540
  const getDefaultValues = () => {
3579
3541
  const defaults = {};
3542
+ // use current class default values
3580
3543
  for (const fieldName in defaultValues) {
3581
3544
  const defaultValue = defaultValues[fieldName];
3582
- // If the default value has a clone method, use it to get a fresh instance
3583
3545
  if (defaultValue && typeof defaultValue.clone === 'function') {
3546
+ // complex, cloneable values, e.g. Schema, ArraySchema, MapSchema, CollectionSchema, SetSchema
3584
3547
  defaults[fieldName] = defaultValue.clone();
3585
3548
  }
3586
3549
  else {
3587
- // Otherwise, use the value as-is (for primitives and non-cloneable objects)
3550
+ // primitives and non-cloneable values
3588
3551
  defaults[fieldName] = defaultValue;
3589
3552
  }
3590
3553
  }
3591
3554
  return defaults;
3592
3555
  };
3556
+ /** @codegen-ignore */
3593
3557
  const klass = Metadata.setFields(class extends inherits {
3594
3558
  constructor(...args) {
3595
- args[0] = Object.assign({}, getDefaultValues(), args[0]);
3596
- 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
+ }
3597
3564
  }
3598
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);
3599
3570
  for (let fieldName in viewTagFields) {
3600
3571
  view(viewTagFields[fieldName])(klass.prototype, fieldName);
3601
3572
  }
3602
- for (let methodName in methods) {
3603
- klass.prototype[methodName] = methods[methodName];
3604
- }
3605
3573
  if (name) {
3606
3574
  Object.defineProperty(klass, "name", { value: name });
3607
3575
  }
@@ -3663,8 +3631,6 @@ class Schema {
3663
3631
  }
3664
3632
  static is(type) {
3665
3633
  return typeof (type[Symbol.metadata]) === "object";
3666
- // const metadata = type[Symbol.metadata];
3667
- // return metadata && Object.prototype.hasOwnProperty.call(metadata, -1);
3668
3634
  }
3669
3635
  /**
3670
3636
  * Track property changes
@@ -3703,7 +3669,7 @@ class Schema {
3703
3669
  }
3704
3670
  }
3705
3671
  // allow inherited classes to have a constructor
3706
- constructor(...args) {
3672
+ constructor(arg) {
3707
3673
  //
3708
3674
  // inline
3709
3675
  // Schema.initialize(this);
@@ -3712,8 +3678,8 @@ class Schema {
3712
3678
  //
3713
3679
  // Assign initial values
3714
3680
  //
3715
- if (args[0]) {
3716
- Object.assign(this, args[0]);
3681
+ if (arg) {
3682
+ Object.assign(this, arg);
3717
3683
  }
3718
3684
  }
3719
3685
  assign(props) {
@@ -3738,7 +3704,6 @@ class Schema {
3738
3704
  //
3739
3705
  // for (const field in this) {
3740
3706
  for (const fieldIndex in metadata) {
3741
- // const field = metadata[metadata[fieldIndex]].name;
3742
3707
  const field = metadata[fieldIndex].name;
3743
3708
  if (typeof (this[field]) === "object" &&
3744
3709
  typeof (this[field]?.clone) === "function") {
@@ -4293,27 +4258,6 @@ class Encoder {
4293
4258
  bytes.subarray(viewOffset, it.offset)
4294
4259
  ]);
4295
4260
  }
4296
- debugChanges(field) {
4297
- const rootChangeSet = (typeof (field) === "string")
4298
- ? this.root[field]
4299
- : field;
4300
- let current = rootChangeSet.next;
4301
- while (current) {
4302
- const changeTree = current.changeTree;
4303
- const changeSet = changeTree[field];
4304
- const metadata = changeTree.ref.constructor[Symbol.metadata];
4305
- console.log("->", { ref: changeTree.ref.constructor.name, refId: changeTree.refId, changes: Object.keys(changeSet).length });
4306
- for (const index in changeSet) {
4307
- const op = changeSet[index];
4308
- console.log(" ->", {
4309
- index,
4310
- field: metadata?.[index],
4311
- op: OPERATION[op],
4312
- });
4313
- }
4314
- current = current.next;
4315
- }
4316
- }
4317
4261
  encodeView(view, sharedOffset, it, bytes = this.sharedBuffer) {
4318
4262
  const viewOffset = it.offset;
4319
4263
  // encode visibility changes (add/remove for this view)
@@ -4778,7 +4722,8 @@ class Reflection extends Schema {
4778
4722
  pendingReflectionTypes[typeid].forEach((type) => reflection.types.push(type));
4779
4723
  }
4780
4724
  const buf = reflectionEncoder.encodeAll(it);
4781
- return Buffer.from(buf, 0, it.offset);
4725
+ return buf.slice(0, it.offset);
4726
+ // return Buffer.from(buf, 0, it.offset);
4782
4727
  }
4783
4728
  /**
4784
4729
  * Decodes the TypeContext from a buffer into a Decoder instance.