@colyseus/schema 3.0.0-alpha.19 → 3.0.0-alpha.22
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 +94 -63
- package/build/cjs/index.js.map +1 -1
- package/build/esm/index.mjs +94 -61
- package/build/esm/index.mjs.map +1 -1
- package/build/umd/index.js +94 -63
- package/lib/Metadata.d.ts +1 -0
- package/lib/Metadata.js +32 -1
- package/lib/Metadata.js.map +1 -1
- package/lib/Reflection.d.ts +0 -1
- package/lib/Reflection.js +27 -20
- package/lib/Reflection.js.map +1 -1
- package/lib/Schema.d.ts +1 -1
- package/lib/annotations.js +12 -10
- package/lib/annotations.js.map +1 -1
- package/lib/codegen/api.js +1 -2
- package/lib/codegen/api.js.map +1 -1
- package/lib/codegen/languages/cpp.js +1 -2
- package/lib/codegen/languages/cpp.js.map +1 -1
- package/lib/codegen/languages/csharp.js +1 -2
- package/lib/codegen/languages/csharp.js.map +1 -1
- package/lib/codegen/languages/haxe.js +1 -2
- package/lib/codegen/languages/haxe.js.map +1 -1
- package/lib/codegen/languages/java.js +1 -2
- package/lib/codegen/languages/java.js.map +1 -1
- package/lib/codegen/languages/js.js +1 -2
- package/lib/codegen/languages/js.js.map +1 -1
- package/lib/codegen/languages/lua.js +1 -2
- package/lib/codegen/languages/lua.js.map +1 -1
- package/lib/codegen/languages/ts.js +1 -2
- package/lib/codegen/languages/ts.js.map +1 -1
- package/lib/codegen/parser.js +2 -3
- package/lib/codegen/parser.js.map +1 -1
- package/lib/codegen/types.js +3 -3
- package/lib/codegen/types.js.map +1 -1
- package/lib/decoder/DecodeOperation.d.ts +0 -1
- package/lib/decoder/DecodeOperation.js +2 -2
- package/lib/decoder/DecodeOperation.js.map +1 -1
- package/lib/decoder/Decoder.d.ts +0 -1
- package/lib/decoder/strategy/RawChanges.js +1 -2
- package/lib/decoder/strategy/RawChanges.js.map +1 -1
- package/lib/decoder/strategy/StateCallbacks.js +1 -2
- package/lib/decoder/strategy/StateCallbacks.js.map +1 -1
- package/lib/encoder/EncodeOperation.d.ts +0 -1
- package/lib/encoder/EncodeOperation.js +3 -3
- package/lib/encoder/EncodeOperation.js.map +1 -1
- package/lib/encoder/Encoder.d.ts +0 -1
- package/lib/encoder/Encoder.js +5 -5
- package/lib/encoder/Encoder.js.map +1 -1
- package/lib/encoder/StateView.js +2 -2
- package/lib/encoder/StateView.js.map +1 -1
- package/lib/encoding/assert.js +3 -3
- package/lib/encoding/assert.js.map +1 -1
- package/lib/encoding/decode.js +20 -21
- package/lib/encoding/decode.js.map +1 -1
- package/lib/encoding/encode.d.ts +0 -1
- package/lib/encoding/encode.js +17 -17
- package/lib/encoding/encode.js.map +1 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.js.map +1 -1
- package/lib/types/custom/ArraySchema.d.ts +2 -2
- package/lib/types/custom/ArraySchema.js +0 -8
- package/lib/types/custom/ArraySchema.js.map +1 -1
- package/lib/types/registry.js +3 -4
- package/lib/types/registry.js.map +1 -1
- package/lib/types/utils.js +1 -2
- package/lib/types/utils.js.map +1 -1
- package/lib/utils.js +3 -4
- package/lib/utils.js.map +1 -1
- package/package.json +5 -5
- package/src/Metadata.ts +39 -1
- package/src/Reflection.ts +30 -20
- package/src/annotations.ts +6 -2
- package/src/encoder/Encoder.ts +5 -5
- package/src/index.ts +2 -0
- package/src/types/custom/ArraySchema.ts +2 -1
package/build/esm/index.mjs
CHANGED
|
@@ -143,10 +143,41 @@ const Metadata = {
|
|
|
143
143
|
init(klass) {
|
|
144
144
|
//
|
|
145
145
|
// Used only to initialize an empty Schema (Encoder#constructor)
|
|
146
|
+
// TODO: remove/refactor this...
|
|
146
147
|
//
|
|
147
148
|
const metadata = {};
|
|
148
149
|
klass.constructor[Symbol.metadata] = metadata;
|
|
149
|
-
Object.defineProperty(metadata, -1, {
|
|
150
|
+
Object.defineProperty(metadata, -1, {
|
|
151
|
+
value: 0,
|
|
152
|
+
enumerable: false,
|
|
153
|
+
configurable: true,
|
|
154
|
+
});
|
|
155
|
+
},
|
|
156
|
+
initialize(constructor, parentMetadata) {
|
|
157
|
+
let metadata = constructor[Symbol.metadata] ?? Object.create(null);
|
|
158
|
+
// make sure inherited classes have their own metadata object.
|
|
159
|
+
if (constructor[Symbol.metadata] === parentMetadata) {
|
|
160
|
+
metadata = Object.create(null);
|
|
161
|
+
if (parentMetadata) {
|
|
162
|
+
// assign parent metadata to current
|
|
163
|
+
Object.assign(metadata, parentMetadata);
|
|
164
|
+
for (let i = 0; i <= parentMetadata[-1]; i++) {
|
|
165
|
+
Object.defineProperty(metadata, i, {
|
|
166
|
+
value: parentMetadata[i],
|
|
167
|
+
enumerable: false,
|
|
168
|
+
configurable: true,
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
Object.defineProperty(metadata, -1, {
|
|
172
|
+
value: parentMetadata[-1],
|
|
173
|
+
enumerable: false,
|
|
174
|
+
configurable: true,
|
|
175
|
+
writable: true,
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
constructor[Symbol.metadata] = metadata;
|
|
180
|
+
return metadata;
|
|
150
181
|
},
|
|
151
182
|
isValidInstance(klass) {
|
|
152
183
|
return (klass.constructor[Symbol.metadata] &&
|
|
@@ -793,23 +824,23 @@ function number$1(bytes, value, it) {
|
|
|
793
824
|
|
|
794
825
|
var encode = /*#__PURE__*/Object.freeze({
|
|
795
826
|
__proto__: null,
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
uint8: uint8$1,
|
|
827
|
+
boolean: boolean$1,
|
|
828
|
+
float32: float32$1,
|
|
829
|
+
float64: float64$1,
|
|
800
830
|
int16: int16$1,
|
|
801
|
-
uint16: uint16$1,
|
|
802
831
|
int32: int32$1,
|
|
803
|
-
uint32: uint32$1,
|
|
804
832
|
int64: int64$1,
|
|
833
|
+
int8: int8$1,
|
|
834
|
+
number: number$1,
|
|
835
|
+
string: string$1,
|
|
836
|
+
uint16: uint16$1,
|
|
837
|
+
uint32: uint32$1,
|
|
805
838
|
uint64: uint64$1,
|
|
806
|
-
|
|
807
|
-
|
|
839
|
+
uint8: uint8$1,
|
|
840
|
+
utf8Length: utf8Length,
|
|
841
|
+
utf8Write: utf8Write,
|
|
808
842
|
writeFloat32: writeFloat32,
|
|
809
|
-
writeFloat64: writeFloat64
|
|
810
|
-
boolean: boolean$1,
|
|
811
|
-
string: string$1,
|
|
812
|
-
number: number$1
|
|
843
|
+
writeFloat64: writeFloat64
|
|
813
844
|
});
|
|
814
845
|
|
|
815
846
|
class EncodeSchemaError extends Error {
|
|
@@ -1242,26 +1273,26 @@ function switchStructureCheck(bytes, it) {
|
|
|
1242
1273
|
|
|
1243
1274
|
var decode = /*#__PURE__*/Object.freeze({
|
|
1244
1275
|
__proto__: null,
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
uint8: uint8,
|
|
1248
|
-
int16: int16,
|
|
1249
|
-
uint16: uint16,
|
|
1250
|
-
int32: int32,
|
|
1251
|
-
uint32: uint32,
|
|
1276
|
+
arrayCheck: arrayCheck,
|
|
1277
|
+
boolean: boolean,
|
|
1252
1278
|
float32: float32,
|
|
1253
1279
|
float64: float64,
|
|
1280
|
+
int16: int16,
|
|
1281
|
+
int32: int32,
|
|
1254
1282
|
int64: int64,
|
|
1255
|
-
|
|
1283
|
+
int8: int8,
|
|
1284
|
+
number: number,
|
|
1285
|
+
numberCheck: numberCheck,
|
|
1256
1286
|
readFloat32: readFloat32,
|
|
1257
1287
|
readFloat64: readFloat64,
|
|
1258
|
-
boolean: boolean,
|
|
1259
1288
|
string: string,
|
|
1260
1289
|
stringCheck: stringCheck,
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1290
|
+
switchStructureCheck: switchStructureCheck,
|
|
1291
|
+
uint16: uint16,
|
|
1292
|
+
uint32: uint32,
|
|
1293
|
+
uint64: uint64,
|
|
1294
|
+
uint8: uint8,
|
|
1295
|
+
utf8Read: utf8Read
|
|
1265
1296
|
});
|
|
1266
1297
|
|
|
1267
1298
|
const DEFINITION_MISMATCH = -1;
|
|
@@ -1901,14 +1932,6 @@ class ArraySchema {
|
|
|
1901
1932
|
lastIndexOf(searchElement, fromIndex = this.length - 1) {
|
|
1902
1933
|
return this.items.lastIndexOf(searchElement, fromIndex);
|
|
1903
1934
|
}
|
|
1904
|
-
/**
|
|
1905
|
-
* Determines whether all the members of an array satisfy the specified test.
|
|
1906
|
-
* @param callbackfn A function that accepts up to three arguments. The every method calls
|
|
1907
|
-
* the callbackfn function for each element in the array until the callbackfn returns a value
|
|
1908
|
-
* which is coercible to the Boolean value false, or until the end of the array.
|
|
1909
|
-
* @param thisArg An object to which the this keyword can refer in the callbackfn function.
|
|
1910
|
-
* If thisArg is omitted, undefined is used as the this value.
|
|
1911
|
-
*/
|
|
1912
1935
|
every(callbackfn, thisArg) {
|
|
1913
1936
|
return this.items.every(callbackfn, thisArg);
|
|
1914
1937
|
}
|
|
@@ -2587,6 +2610,7 @@ function view(tag = DEFAULT_VIEW_TAG) {
|
|
|
2587
2610
|
const constructor = target.constructor;
|
|
2588
2611
|
const parentClass = Object.getPrototypeOf(constructor);
|
|
2589
2612
|
const parentMetadata = parentClass[Symbol.metadata];
|
|
2613
|
+
// TODO: use Metadata.initialize()
|
|
2590
2614
|
const metadata = (constructor[Symbol.metadata] ??= Object.assign({}, constructor[Symbol.metadata], parentMetadata ?? Object.create(null)));
|
|
2591
2615
|
if (!metadata[fieldName]) {
|
|
2592
2616
|
//
|
|
@@ -2611,8 +2635,8 @@ function type(type, options) {
|
|
|
2611
2635
|
// for inheritance support
|
|
2612
2636
|
TypeContext.register(constructor);
|
|
2613
2637
|
const parentClass = Object.getPrototypeOf(constructor);
|
|
2614
|
-
const parentMetadata = parentClass[Symbol.metadata];
|
|
2615
|
-
const metadata =
|
|
2638
|
+
const parentMetadata = parentClass && parentClass[Symbol.metadata];
|
|
2639
|
+
const metadata = Metadata.initialize(constructor, parentMetadata);
|
|
2616
2640
|
let fieldIndex;
|
|
2617
2641
|
/**
|
|
2618
2642
|
* skip if descriptor already exists for this field (`@deprecated()`)
|
|
@@ -3385,6 +3409,8 @@ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
|
3385
3409
|
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
3386
3410
|
PERFORMANCE OF THIS SOFTWARE.
|
|
3387
3411
|
***************************************************************************** */
|
|
3412
|
+
/* global Reflect, Promise, SuppressedError, Symbol */
|
|
3413
|
+
|
|
3388
3414
|
|
|
3389
3415
|
function __decorate(decorators, target, key, desc) {
|
|
3390
3416
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
@@ -3470,11 +3496,11 @@ class Encoder {
|
|
|
3470
3496
|
// try { throw new Error(); } catch (e) {
|
|
3471
3497
|
// // only print if not coming from Reflection.ts
|
|
3472
3498
|
// if (!e.stack.includes("src/Reflection.ts")) {
|
|
3473
|
-
//
|
|
3474
|
-
//
|
|
3475
|
-
//
|
|
3476
|
-
//
|
|
3477
|
-
//
|
|
3499
|
+
// console.log("WILL ENCODE", {
|
|
3500
|
+
// ref: changeTree.ref.constructor.name,
|
|
3501
|
+
// fieldIndex,
|
|
3502
|
+
// operation: OPERATION[operation],
|
|
3503
|
+
// });
|
|
3478
3504
|
// }
|
|
3479
3505
|
// }
|
|
3480
3506
|
encoder(this, buffer, changeTree, fieldIndex, operation, it, isEncodeAll, hasView);
|
|
@@ -3935,53 +3961,60 @@ class Reflection extends Schema {
|
|
|
3935
3961
|
const reflection = new Reflection();
|
|
3936
3962
|
const reflectionDecoder = new Decoder(reflection);
|
|
3937
3963
|
reflectionDecoder.decode(bytes, it);
|
|
3938
|
-
const
|
|
3939
|
-
|
|
3940
|
-
|
|
3941
|
-
const
|
|
3964
|
+
const typeContext = new TypeContext();
|
|
3965
|
+
// 1st pass, initialize metadata + inheritance
|
|
3966
|
+
reflection.types.forEach((reflectionType) => {
|
|
3967
|
+
const parentClass = typeContext.get(reflectionType.extendsId) ?? Schema;
|
|
3968
|
+
const schema = class _ extends parentClass {
|
|
3942
3969
|
};
|
|
3943
|
-
|
|
3944
|
-
const _metadata = parentKlass && parentKlass[Symbol.metadata] || Object.create(null);
|
|
3945
|
-
Object.defineProperty(schema, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
3970
|
+
const parentMetadata = parentClass[Symbol.metadata];
|
|
3946
3971
|
// register for inheritance support
|
|
3947
3972
|
TypeContext.register(schema);
|
|
3948
|
-
|
|
3949
|
-
|
|
3950
|
-
|
|
3951
|
-
return types;
|
|
3973
|
+
// for inheritance support
|
|
3974
|
+
Metadata.initialize(schema, parentMetadata);
|
|
3975
|
+
typeContext.add(schema, reflectionType.id);
|
|
3952
3976
|
}, {});
|
|
3977
|
+
// 2nd pass, set fields
|
|
3953
3978
|
reflection.types.forEach((reflectionType) => {
|
|
3954
|
-
const schemaType =
|
|
3979
|
+
const schemaType = typeContext.get(reflectionType.id);
|
|
3955
3980
|
const metadata = schemaType[Symbol.metadata];
|
|
3956
|
-
|
|
3957
|
-
const parentFieldIndex =
|
|
3981
|
+
// FIXME: use metadata[-1] to get field count
|
|
3982
|
+
const parentFieldIndex = 0;
|
|
3983
|
+
// console.log("--------------------");
|
|
3984
|
+
// // console.log("reflectionType", reflectionType.toJSON());
|
|
3985
|
+
// console.log("reflectionType.fields", reflectionType.fields.toJSON());
|
|
3986
|
+
// console.log("parentFieldIndex", parentFieldIndex);
|
|
3987
|
+
//
|
|
3988
|
+
// FIXME: set fields using parentKlass as well
|
|
3989
|
+
// currently the fields are duplicated on inherited classes
|
|
3990
|
+
//
|
|
3991
|
+
// // const parentKlass = reflection.types[reflectionType.extendsId];
|
|
3992
|
+
// // parentKlass.fields
|
|
3958
3993
|
reflectionType.fields.forEach((field, i) => {
|
|
3959
3994
|
const fieldIndex = parentFieldIndex + i;
|
|
3960
3995
|
if (field.referencedType !== undefined) {
|
|
3961
3996
|
let fieldType = field.type;
|
|
3962
|
-
let refType =
|
|
3997
|
+
let refType = typeContext.get(field.referencedType);
|
|
3963
3998
|
// map or array of primitive type (-1)
|
|
3964
3999
|
if (!refType) {
|
|
3965
4000
|
const typeInfo = field.type.split(":");
|
|
3966
4001
|
fieldType = typeInfo[0];
|
|
3967
|
-
refType = typeInfo[1];
|
|
4002
|
+
refType = typeInfo[1]; // string
|
|
3968
4003
|
}
|
|
3969
4004
|
if (fieldType === "ref") {
|
|
3970
|
-
// type(refType)(schemaType.prototype, field.name);
|
|
3971
4005
|
Metadata.addField(metadata, fieldIndex, field.name, refType);
|
|
3972
4006
|
}
|
|
3973
4007
|
else {
|
|
3974
|
-
// type({ [fieldType]: refType } as DefinitionType)(schemaType.prototype, field.name);
|
|
3975
4008
|
Metadata.addField(metadata, fieldIndex, field.name, { [fieldType]: refType });
|
|
3976
4009
|
}
|
|
3977
4010
|
}
|
|
3978
4011
|
else {
|
|
3979
|
-
// type(field.type as PrimitiveType)(schemaType.prototype, field.name);
|
|
3980
4012
|
Metadata.addField(metadata, fieldIndex, field.name, field.type);
|
|
3981
4013
|
}
|
|
3982
4014
|
});
|
|
3983
4015
|
});
|
|
3984
|
-
|
|
4016
|
+
// @ts-ignore
|
|
4017
|
+
return new (typeContext.get(0))();
|
|
3985
4018
|
}
|
|
3986
4019
|
}
|
|
3987
4020
|
__decorate([
|