@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.
- package/build/cjs/index.js +24 -79
- package/build/cjs/index.js.map +1 -1
- package/build/esm/index.mjs +24 -79
- package/build/esm/index.mjs.map +1 -1
- package/build/umd/index.js +24 -79
- package/lib/Metadata.d.ts +2 -2
- package/lib/Metadata.js.map +1 -1
- package/lib/Reflection.js +2 -1
- package/lib/Reflection.js.map +1 -1
- package/lib/Schema.d.ts +14 -14
- package/lib/Schema.js +4 -7
- package/lib/Schema.js.map +1 -1
- package/lib/annotations.d.ts +35 -5
- package/lib/annotations.js +17 -7
- package/lib/annotations.js.map +1 -1
- package/lib/codegen/api.js.map +1 -1
- package/lib/codegen/argv.d.ts +1 -1
- package/lib/codegen/argv.js.map +1 -1
- package/lib/codegen/cli.js.map +1 -1
- package/lib/codegen/languages/cpp.js.map +1 -1
- package/lib/codegen/languages/csharp.js.map +1 -1
- package/lib/codegen/languages/haxe.js.map +1 -1
- package/lib/codegen/languages/java.js.map +1 -1
- package/lib/codegen/languages/js.js.map +1 -1
- package/lib/codegen/languages/lua.js.map +1 -1
- package/lib/codegen/languages/ts.js.map +1 -1
- package/lib/codegen/parser.js +11 -0
- package/lib/codegen/parser.js.map +1 -1
- package/lib/codegen/types.d.ts +1 -1
- package/lib/codegen/types.js.map +1 -1
- package/lib/decoder/DecodeOperation.d.ts +2 -2
- package/lib/decoder/DecodeOperation.js.map +1 -1
- package/lib/decoder/Decoder.js.map +1 -1
- package/lib/decoder/ReferenceTracker.js.map +1 -1
- package/lib/decoder/strategy/StateCallbacks.js.map +1 -1
- package/lib/encoder/ChangeTree.d.ts +6 -1
- package/lib/encoder/ChangeTree.js.map +1 -1
- package/lib/encoder/EncodeOperation.js.map +1 -1
- package/lib/encoder/Encoder.d.ts +0 -1
- package/lib/encoder/Encoder.js +0 -21
- package/lib/encoder/Encoder.js.map +1 -1
- package/lib/encoder/StateView.js.map +1 -1
- package/lib/encoding/decode.js +1 -22
- package/lib/encoding/decode.js.map +1 -1
- package/lib/encoding/encode.d.ts +1 -23
- package/lib/encoding/encode.js +1 -22
- package/lib/encoding/encode.js.map +1 -1
- package/lib/index.d.ts +3 -2
- package/lib/index.js.map +1 -1
- package/lib/types/HelperTypes.d.ts +14 -5
- package/lib/types/HelperTypes.js.map +1 -1
- package/lib/types/TypeContext.js.map +1 -1
- package/lib/types/custom/ArraySchema.d.ts +9 -5
- package/lib/types/custom/ArraySchema.js.map +1 -1
- package/lib/types/custom/CollectionSchema.d.ts +8 -4
- package/lib/types/custom/CollectionSchema.js.map +1 -1
- package/lib/types/custom/MapSchema.d.ts +8 -6
- package/lib/types/custom/MapSchema.js.map +1 -1
- package/lib/types/custom/SetSchema.d.ts +8 -4
- package/lib/types/custom/SetSchema.js.map +1 -1
- package/lib/types/registry.js.map +1 -1
- package/lib/utils.js.map +1 -1
- package/lib/v3_bench.js.map +1 -1
- package/package.json +2 -2
- package/src/Metadata.ts +2 -2
- package/src/Reflection.ts +5 -4
- package/src/Schema.ts +35 -37
- package/src/annotations.ts +95 -30
- package/src/codegen/api.ts +1 -1
- package/src/codegen/argv.ts +5 -5
- package/src/codegen/cli.ts +1 -1
- package/src/codegen/languages/cpp.ts +5 -4
- package/src/codegen/languages/csharp.ts +2 -2
- package/src/codegen/languages/haxe.ts +2 -2
- package/src/codegen/languages/java.ts +2 -2
- package/src/codegen/languages/js.ts +3 -2
- package/src/codegen/languages/lua.ts +3 -2
- package/src/codegen/languages/ts.ts +3 -2
- package/src/codegen/parser.ts +12 -0
- package/src/codegen/types.ts +2 -2
- package/src/decoder/DecodeOperation.ts +21 -21
- package/src/decoder/Decoder.ts +3 -3
- package/src/decoder/ReferenceTracker.ts +7 -5
- package/src/decoder/strategy/StateCallbacks.ts +10 -10
- package/src/encoder/ChangeTree.ts +21 -15
- package/src/encoder/EncodeOperation.ts +4 -4
- package/src/encoder/Encoder.ts +0 -24
- package/src/encoder/StateView.ts +3 -2
- package/src/encoding/decode.ts +2 -0
- package/src/encoding/encode.ts +3 -1
- package/src/index.ts +5 -2
- package/src/types/HelperTypes.ts +29 -7
- package/src/types/TypeContext.ts +1 -1
- package/src/types/custom/ArraySchema.ts +12 -10
- package/src/types/custom/CollectionSchema.ts +9 -6
- package/src/types/custom/MapSchema.ts +8 -8
- package/src/types/custom/SetSchema.ts +9 -6
- package/src/types/registry.ts +2 -2
- package/src/utils.ts +2 -4
- package/src/v3_bench.ts +1 -1
package/build/esm/index.mjs
CHANGED
|
@@ -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
|
-
//
|
|
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
|
-
|
|
3596
|
-
|
|
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(
|
|
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 (
|
|
3716
|
-
Object.assign(this,
|
|
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
|
|
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.
|