@colyseus/schema 3.0.62 → 3.0.64

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 +35 -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 +95 -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
@@ -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);
@@ -3435,6 +3393,10 @@ function getPropertyDescriptor(fieldCached, fieldIndex, type, complexTypeKlass)
3435
3393
  if (complexTypeKlass.constructor === MapSchema && !(value instanceof MapSchema)) {
3436
3394
  value = new MapSchema(value);
3437
3395
  }
3396
+ // // automaticallty transform Array into SetSchema
3397
+ // if (complexTypeKlass.constructor === SetSchema && !(value instanceof SetSchema)) {
3398
+ // value = new SetSchema(value);
3399
+ // }
3438
3400
  value[$childType] = type;
3439
3401
  }
3440
3402
  else if (typeof (type) !== "string") {
@@ -3579,31 +3541,37 @@ function schema(fieldsAndMethods, name, inherits = Schema) {
3579
3541
  }
3580
3542
  const getDefaultValues = () => {
3581
3543
  const defaults = {};
3544
+ // use current class default values
3582
3545
  for (const fieldName in defaultValues) {
3583
3546
  const defaultValue = defaultValues[fieldName];
3584
- // If the default value has a clone method, use it to get a fresh instance
3585
3547
  if (defaultValue && typeof defaultValue.clone === 'function') {
3548
+ // complex, cloneable values, e.g. Schema, ArraySchema, MapSchema, CollectionSchema, SetSchema
3586
3549
  defaults[fieldName] = defaultValue.clone();
3587
3550
  }
3588
3551
  else {
3589
- // Otherwise, use the value as-is (for primitives and non-cloneable objects)
3552
+ // primitives and non-cloneable values
3590
3553
  defaults[fieldName] = defaultValue;
3591
3554
  }
3592
3555
  }
3593
3556
  return defaults;
3594
3557
  };
3558
+ /** @codegen-ignore */
3595
3559
  const klass = Metadata.setFields(class extends inherits {
3596
3560
  constructor(...args) {
3597
- args[0] = Object.assign({}, getDefaultValues(), args[0]);
3598
- 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
+ }
3599
3566
  }
3600
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);
3601
3572
  for (let fieldName in viewTagFields) {
3602
3573
  view(viewTagFields[fieldName])(klass.prototype, fieldName);
3603
3574
  }
3604
- for (let methodName in methods) {
3605
- klass.prototype[methodName] = methods[methodName];
3606
- }
3607
3575
  if (name) {
3608
3576
  Object.defineProperty(klass, "name", { value: name });
3609
3577
  }
@@ -3665,8 +3633,6 @@ class Schema {
3665
3633
  }
3666
3634
  static is(type) {
3667
3635
  return typeof (type[Symbol.metadata]) === "object";
3668
- // const metadata = type[Symbol.metadata];
3669
- // return metadata && Object.prototype.hasOwnProperty.call(metadata, -1);
3670
3636
  }
3671
3637
  /**
3672
3638
  * Track property changes
@@ -3705,7 +3671,7 @@ class Schema {
3705
3671
  }
3706
3672
  }
3707
3673
  // allow inherited classes to have a constructor
3708
- constructor(...args) {
3674
+ constructor(arg) {
3709
3675
  //
3710
3676
  // inline
3711
3677
  // Schema.initialize(this);
@@ -3714,8 +3680,8 @@ class Schema {
3714
3680
  //
3715
3681
  // Assign initial values
3716
3682
  //
3717
- if (args[0]) {
3718
- Object.assign(this, args[0]);
3683
+ if (arg) {
3684
+ Object.assign(this, arg);
3719
3685
  }
3720
3686
  }
3721
3687
  assign(props) {
@@ -3740,7 +3706,6 @@ class Schema {
3740
3706
  //
3741
3707
  // for (const field in this) {
3742
3708
  for (const fieldIndex in metadata) {
3743
- // const field = metadata[metadata[fieldIndex]].name;
3744
3709
  const field = metadata[fieldIndex].name;
3745
3710
  if (typeof (this[field]) === "object" &&
3746
3711
  typeof (this[field]?.clone) === "function") {
@@ -4295,27 +4260,6 @@ class Encoder {
4295
4260
  bytes.subarray(viewOffset, it.offset)
4296
4261
  ]);
4297
4262
  }
4298
- debugChanges(field) {
4299
- const rootChangeSet = (typeof (field) === "string")
4300
- ? this.root[field]
4301
- : field;
4302
- let current = rootChangeSet.next;
4303
- while (current) {
4304
- const changeTree = current.changeTree;
4305
- const changeSet = changeTree[field];
4306
- const metadata = changeTree.ref.constructor[Symbol.metadata];
4307
- console.log("->", { ref: changeTree.ref.constructor.name, refId: changeTree.refId, changes: Object.keys(changeSet).length });
4308
- for (const index in changeSet) {
4309
- const op = changeSet[index];
4310
- console.log(" ->", {
4311
- index,
4312
- field: metadata?.[index],
4313
- op: exports.OPERATION[op],
4314
- });
4315
- }
4316
- current = current.next;
4317
- }
4318
- }
4319
4263
  encodeView(view, sharedOffset, it, bytes = this.sharedBuffer) {
4320
4264
  const viewOffset = it.offset;
4321
4265
  // encode visibility changes (add/remove for this view)
@@ -4780,7 +4724,8 @@ class Reflection extends Schema {
4780
4724
  pendingReflectionTypes[typeid].forEach((type) => reflection.types.push(type));
4781
4725
  }
4782
4726
  const buf = reflectionEncoder.encodeAll(it);
4783
- return Buffer.from(buf, 0, it.offset);
4727
+ return buf.slice(0, it.offset);
4728
+ // return Buffer.from(buf, 0, it.offset);
4784
4729
  }
4785
4730
  /**
4786
4731
  * Decodes the TypeContext from a buffer into a Decoder instance.