@colyseus/schema 1.0.34 → 1.0.38
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/README.md +0 -4
- package/build/cjs/index.js +35 -33
- package/build/cjs/index.js.map +1 -1
- package/build/esm/index.mjs +113 -83
- package/build/esm/index.mjs.map +1 -1
- package/build/umd/index.js +37 -35
- package/lib/Reflection.js +11 -11
- package/lib/Reflection.js.map +1 -1
- package/lib/Schema.js +15 -13
- package/lib/Schema.js.map +1 -1
- package/lib/annotations.js +17 -13
- package/lib/annotations.js.map +1 -1
- package/lib/changes/ChangeTree.js +2 -2
- package/lib/changes/ChangeTree.js.map +1 -1
- package/lib/codegen/api.js +1 -1
- package/lib/codegen/api.js.map +1 -1
- package/lib/codegen/argv.d.ts +1 -1
- package/lib/codegen/cli.js +5 -5
- package/lib/codegen/cli.js.map +1 -1
- package/lib/codegen/languages/cpp.js +38 -38
- package/lib/codegen/languages/cpp.js.map +1 -1
- package/lib/codegen/languages/csharp.js +25 -21
- package/lib/codegen/languages/csharp.js.map +1 -1
- package/lib/codegen/languages/haxe.js +13 -13
- package/lib/codegen/languages/haxe.js.map +1 -1
- package/lib/codegen/languages/java.js +11 -11
- package/lib/codegen/languages/java.js.map +1 -1
- package/lib/codegen/languages/js.js +16 -16
- package/lib/codegen/languages/js.js.map +1 -1
- package/lib/codegen/languages/lua.js +12 -12
- package/lib/codegen/languages/lua.js.map +1 -1
- package/lib/codegen/languages/ts.js +37 -33
- package/lib/codegen/languages/ts.js.map +1 -1
- package/lib/codegen/parser.js +3 -3
- package/lib/codegen/parser.js.map +1 -1
- package/lib/codegen/types.js +2 -2
- package/lib/codegen/types.js.map +1 -1
- package/lib/events/EventEmitter.js +10 -6
- package/lib/events/EventEmitter.js.map +1 -1
- package/lib/index.js +4 -4
- package/lib/index.js.map +1 -1
- package/lib/types/ArraySchema.js +12 -8
- package/lib/types/ArraySchema.js.map +1 -1
- package/lib/types/MapSchema.d.ts +19 -19
- package/lib/types/MapSchema.js +2 -2
- package/lib/types/MapSchema.js.map +1 -1
- package/package.json +2 -1
- package/src/Reflection.ts +159 -0
- package/src/Schema.ts +1085 -0
- package/src/annotations.ts +357 -0
- package/src/changes/ChangeTree.ts +373 -0
- package/src/codegen/api.ts +46 -0
- package/src/codegen/argv.ts +40 -0
- package/src/codegen/cli.ts +65 -0
- package/src/codegen/languages/cpp.ts +297 -0
- package/src/codegen/languages/csharp.ts +119 -0
- package/src/codegen/languages/haxe.ts +110 -0
- package/src/codegen/languages/java.ts +115 -0
- package/src/codegen/languages/js.ts +115 -0
- package/src/codegen/languages/lua.ts +125 -0
- package/src/codegen/languages/ts.ts +129 -0
- package/src/codegen/parser.ts +251 -0
- package/src/codegen/types.ts +164 -0
- package/src/encoding/decode.ts +278 -0
- package/src/encoding/encode.ts +283 -0
- package/src/events/EventEmitter.ts +32 -0
- package/src/filters/index.ts +23 -0
- package/src/index.ts +59 -0
- package/src/spec.ts +49 -0
- package/src/types/ArraySchema.ts +608 -0
- package/src/types/CollectionSchema.ts +188 -0
- package/src/types/HelperTypes.ts +34 -0
- package/src/types/MapSchema.ts +255 -0
- package/src/types/SetSchema.ts +197 -0
- package/src/types/index.ts +19 -0
- package/src/utils.ts +28 -0
package/README.md
CHANGED
|
@@ -7,10 +7,6 @@
|
|
|
7
7
|
An incremental binary state serializer with delta encoding for games.<br>
|
|
8
8
|
Although it was born to be used on <a href="https://github.com/colyseus/colyseus">Colyseus</a>, this library can be used as standalone.
|
|
9
9
|
</p>
|
|
10
|
-
|
|
11
|
-
<a href="https://travis-ci.org/colyseus/schema">
|
|
12
|
-
<img src="https://img.shields.io/travis/colyseus/schema.svg?style=for-the-badge" alt="Build status" />
|
|
13
|
-
</a>
|
|
14
10
|
</div>
|
|
15
11
|
|
|
16
12
|
## Defining Schema
|
package/build/cjs/index.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
/******************************************************************************
|
|
6
6
|
Copyright (c) Microsoft Corporation.
|
|
7
7
|
|
|
8
8
|
Permission to use, copy, modify, and/or distribute this software for any
|
|
@@ -40,10 +40,14 @@ function __decorate(decorators, target, key, desc) {
|
|
|
40
40
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
function __spreadArray(to, from) {
|
|
44
|
-
for (var i = 0,
|
|
45
|
-
|
|
46
|
-
|
|
43
|
+
function __spreadArray(to, from, pack) {
|
|
44
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
45
|
+
if (ar || !(i in from)) {
|
|
46
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
47
|
+
ar[i] = from[i];
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
47
51
|
}
|
|
48
52
|
|
|
49
53
|
// export const SWITCH_TO_STRUCTURE = 193; (easily collides with DELETE_AND_ADD + fieldIndex = 2)
|
|
@@ -280,7 +284,7 @@ var ChangeTree = /** @class */ (function () {
|
|
|
280
284
|
? fieldName
|
|
281
285
|
: this.indexes[fieldName];
|
|
282
286
|
if (index === undefined) {
|
|
283
|
-
console.warn("@colyseus/schema "
|
|
287
|
+
console.warn("@colyseus/schema ".concat(this.ref.constructor.name, ": trying to delete non-existing index: ").concat(fieldName, " (").concat(index, ")"));
|
|
284
288
|
return;
|
|
285
289
|
}
|
|
286
290
|
var previousValue = this.getValue(index);
|
|
@@ -352,7 +356,7 @@ var ChangeTree = /** @class */ (function () {
|
|
|
352
356
|
};
|
|
353
357
|
ChangeTree.prototype.assertValidIndex = function (index, fieldName) {
|
|
354
358
|
if (index === undefined) {
|
|
355
|
-
throw new Error("ChangeTree: missing index for field \""
|
|
359
|
+
throw new Error("ChangeTree: missing index for field \"".concat(fieldName, "\""));
|
|
356
360
|
}
|
|
357
361
|
};
|
|
358
362
|
return ChangeTree;
|
|
@@ -545,7 +549,7 @@ var ArraySchema = /** @class */ (function () {
|
|
|
545
549
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
546
550
|
items[_i] = arguments[_i];
|
|
547
551
|
}
|
|
548
|
-
return new (ArraySchema.bind.apply(ArraySchema, __spreadArray([void 0], (_a = Array.from(this.$items.values())).concat.apply(_a, items))))();
|
|
552
|
+
return new (ArraySchema.bind.apply(ArraySchema, __spreadArray([void 0], (_a = Array.from(this.$items.values())).concat.apply(_a, items), false)))();
|
|
549
553
|
};
|
|
550
554
|
/**
|
|
551
555
|
* Adds all the elements of an array separated by the specified separator string.
|
|
@@ -585,7 +589,7 @@ var ArraySchema = /** @class */ (function () {
|
|
|
585
589
|
* @param end The end of the specified portion of the array. This is exclusive of the element at the index 'end'.
|
|
586
590
|
*/
|
|
587
591
|
ArraySchema.prototype.slice = function (start, end) {
|
|
588
|
-
return new (ArraySchema.bind.apply(ArraySchema, __spreadArray([void 0], Array.from(this.$items.values()).slice(start, end))))();
|
|
592
|
+
return new (ArraySchema.bind.apply(ArraySchema, __spreadArray([void 0], Array.from(this.$items.values()).slice(start, end), false)))();
|
|
589
593
|
};
|
|
590
594
|
/**
|
|
591
595
|
* Sorts an array.
|
|
@@ -614,10 +618,6 @@ var ArraySchema = /** @class */ (function () {
|
|
|
614
618
|
*/
|
|
615
619
|
ArraySchema.prototype.splice = function (start, deleteCount) {
|
|
616
620
|
if (deleteCount === void 0) { deleteCount = this.length - start; }
|
|
617
|
-
var items = [];
|
|
618
|
-
for (var _i = 2; _i < arguments.length; _i++) {
|
|
619
|
-
items[_i - 2] = arguments[_i];
|
|
620
|
-
}
|
|
621
621
|
var indexes = Array.from(this.$items.keys());
|
|
622
622
|
var removedItems = [];
|
|
623
623
|
for (var i = start; i < start + deleteCount; i++) {
|
|
@@ -869,12 +869,12 @@ var ArraySchema = /** @class */ (function () {
|
|
|
869
869
|
ArraySchema.prototype.clone = function (isDecoding) {
|
|
870
870
|
var cloned;
|
|
871
871
|
if (isDecoding) {
|
|
872
|
-
cloned = new (ArraySchema.bind.apply(ArraySchema, __spreadArray([void 0], Array.from(this.$items.values()))))();
|
|
872
|
+
cloned = new (ArraySchema.bind.apply(ArraySchema, __spreadArray([void 0], Array.from(this.$items.values()), false)))();
|
|
873
873
|
}
|
|
874
874
|
else {
|
|
875
875
|
cloned = new (ArraySchema.bind.apply(ArraySchema, __spreadArray([void 0], this.map(function (item) { return ((item['$changes'])
|
|
876
876
|
? item.clone()
|
|
877
|
-
: item); }))))();
|
|
877
|
+
: item); }), false)))();
|
|
878
878
|
}
|
|
879
879
|
return cloned;
|
|
880
880
|
};
|
|
@@ -946,7 +946,7 @@ var MapSchema = /** @class */ (function () {
|
|
|
946
946
|
});
|
|
947
947
|
MapSchema.prototype.set = function (key, value) {
|
|
948
948
|
if (value === undefined || value === null) {
|
|
949
|
-
throw new Error("MapSchema#set('"
|
|
949
|
+
throw new Error("MapSchema#set('".concat(key, "', ").concat(value, "): trying to set ").concat(value, " value on '").concat(key, "'."));
|
|
950
950
|
}
|
|
951
951
|
// get "index" for this value.
|
|
952
952
|
var hasIndex = typeof (this.$changes.indexes[key]) !== "undefined";
|
|
@@ -1140,7 +1140,7 @@ var SchemaDefinition = /** @class */ (function () {
|
|
|
1140
1140
|
return true;
|
|
1141
1141
|
}
|
|
1142
1142
|
else {
|
|
1143
|
-
console.warn("@filterChildren: field '"
|
|
1143
|
+
console.warn("@filterChildren: field '".concat(field, "' can't have children. Ignoring filter."));
|
|
1144
1144
|
}
|
|
1145
1145
|
};
|
|
1146
1146
|
SchemaDefinition.prototype.getChildrenFilter = function (field) {
|
|
@@ -1215,11 +1215,11 @@ function type(type, context) {
|
|
|
1215
1215
|
// trying to define same property multiple times across inheritance.
|
|
1216
1216
|
// https://github.com/colyseus/colyseus-unity3d/issues/131#issuecomment-814308572
|
|
1217
1217
|
try {
|
|
1218
|
-
throw new Error("@colyseus/schema: Duplicate '"
|
|
1218
|
+
throw new Error("@colyseus/schema: Duplicate '".concat(field, "' definition on '").concat(constructor.name, "'.\nCheck @type() annotation"));
|
|
1219
1219
|
}
|
|
1220
1220
|
catch (e) {
|
|
1221
1221
|
var definitionAtLine = e.stack.split("\n")[4].trim();
|
|
1222
|
-
throw new Error(e.message
|
|
1222
|
+
throw new Error("".concat(e.message, " ").concat(definitionAtLine));
|
|
1223
1223
|
}
|
|
1224
1224
|
}
|
|
1225
1225
|
}
|
|
@@ -1234,7 +1234,7 @@ function type(type, context) {
|
|
|
1234
1234
|
context.add(childType);
|
|
1235
1235
|
}
|
|
1236
1236
|
}
|
|
1237
|
-
var fieldCached = "_"
|
|
1237
|
+
var fieldCached = "_".concat(field);
|
|
1238
1238
|
definition.descriptors[fieldCached] = {
|
|
1239
1239
|
enumerable: false,
|
|
1240
1240
|
configurable: false,
|
|
@@ -1256,7 +1256,7 @@ function type(type, context) {
|
|
|
1256
1256
|
value !== null) {
|
|
1257
1257
|
// automaticallty transform Array into ArraySchema
|
|
1258
1258
|
if (isArray && !(value instanceof ArraySchema)) {
|
|
1259
|
-
value = new (ArraySchema.bind.apply(ArraySchema, __spreadArray([void 0], value)))();
|
|
1259
|
+
value = new (ArraySchema.bind.apply(ArraySchema, __spreadArray([void 0], value, false)))();
|
|
1260
1260
|
}
|
|
1261
1261
|
// automaticallty transform Map into MapSchema
|
|
1262
1262
|
if (isMap && !(value instanceof MapSchema)) {
|
|
@@ -1327,7 +1327,7 @@ function deprecated(throws, context) {
|
|
|
1327
1327
|
definition.deprecated[field] = true;
|
|
1328
1328
|
if (throws) {
|
|
1329
1329
|
definition.descriptors[field] = {
|
|
1330
|
-
get: function () { throw new Error(field
|
|
1330
|
+
get: function () { throw new Error("".concat(field, " is deprecated.")); },
|
|
1331
1331
|
set: function (value) { },
|
|
1332
1332
|
enumerable: false,
|
|
1333
1333
|
configurable: true
|
|
@@ -2249,7 +2249,7 @@ function assertType(value, type, klass, field) {
|
|
|
2249
2249
|
case "float64":
|
|
2250
2250
|
typeofTarget = "number";
|
|
2251
2251
|
if (isNaN(value)) {
|
|
2252
|
-
console.log("trying to encode \"NaN\" in "
|
|
2252
|
+
console.log("trying to encode \"NaN\" in ".concat(klass.constructor.name, "#").concat(field));
|
|
2253
2253
|
}
|
|
2254
2254
|
break;
|
|
2255
2255
|
case "string":
|
|
@@ -2261,13 +2261,13 @@ function assertType(value, type, klass, field) {
|
|
|
2261
2261
|
return;
|
|
2262
2262
|
}
|
|
2263
2263
|
if (typeof (value) !== typeofTarget && (!allowNull || (allowNull && value !== null))) {
|
|
2264
|
-
var foundValue = "'"
|
|
2265
|
-
throw new EncodeSchemaError("a '"
|
|
2264
|
+
var foundValue = "'".concat(JSON.stringify(value), "'").concat((value && value.constructor && " (".concat(value.constructor.name, ")")) || '');
|
|
2265
|
+
throw new EncodeSchemaError("a '".concat(typeofTarget, "' was expected, but ").concat(foundValue, " was provided in ").concat(klass.constructor.name, "#").concat(field));
|
|
2266
2266
|
}
|
|
2267
2267
|
}
|
|
2268
2268
|
function assertInstanceType(value, type, klass, field) {
|
|
2269
2269
|
if (!(value instanceof type)) {
|
|
2270
|
-
throw new EncodeSchemaError("a '"
|
|
2270
|
+
throw new EncodeSchemaError("a '".concat(type.name, "' was expected, but '").concat(value.constructor.name, "' was provided in ").concat(klass.constructor.name, "#").concat(field));
|
|
2271
2271
|
}
|
|
2272
2272
|
}
|
|
2273
2273
|
function encodePrimitiveType(type, bytes, value, klass, field) {
|
|
@@ -2277,7 +2277,7 @@ function encodePrimitiveType(type, bytes, value, klass, field) {
|
|
|
2277
2277
|
encodeFunc(bytes, value);
|
|
2278
2278
|
}
|
|
2279
2279
|
else {
|
|
2280
|
-
throw new EncodeSchemaError("a '"
|
|
2280
|
+
throw new EncodeSchemaError("a '".concat(type, "' was expected, but ").concat(value, " was provided in ").concat(klass.constructor.name, "#").concat(field));
|
|
2281
2281
|
}
|
|
2282
2282
|
}
|
|
2283
2283
|
function decodePrimitiveType(type, bytes, it) {
|
|
@@ -2363,7 +2363,7 @@ var Schema = /** @class */ (function () {
|
|
|
2363
2363
|
// Trying to access a reference that haven't been decoded yet.
|
|
2364
2364
|
//
|
|
2365
2365
|
if (!nextRef) {
|
|
2366
|
-
throw new Error("\"refId\" not found: "
|
|
2366
|
+
throw new Error("\"refId\" not found: ".concat(refId));
|
|
2367
2367
|
}
|
|
2368
2368
|
ref = nextRef;
|
|
2369
2369
|
// create empty list of changes for this refId.
|
|
@@ -2409,7 +2409,7 @@ var Schema = /** @class */ (function () {
|
|
|
2409
2409
|
}
|
|
2410
2410
|
}
|
|
2411
2411
|
else {
|
|
2412
|
-
previousValue = ref["_"
|
|
2412
|
+
previousValue = ref["_".concat(fieldName)];
|
|
2413
2413
|
}
|
|
2414
2414
|
//
|
|
2415
2415
|
// Delete operations
|
|
@@ -2535,6 +2535,7 @@ var Schema = /** @class */ (function () {
|
|
|
2535
2535
|
var key = dynamicIndex;
|
|
2536
2536
|
// ref.set(key, value);
|
|
2537
2537
|
ref['$items'].set(key, value);
|
|
2538
|
+
ref['$changes'].allChanges.add(fieldIndex);
|
|
2538
2539
|
}
|
|
2539
2540
|
else if (ref instanceof ArraySchema) {
|
|
2540
2541
|
// const key = ref['$indexes'][field];
|
|
@@ -2691,7 +2692,7 @@ var Schema = /** @class */ (function () {
|
|
|
2691
2692
|
//
|
|
2692
2693
|
// ensure a ArraySchema has been provided
|
|
2693
2694
|
//
|
|
2694
|
-
assertInstanceType(ref["_"
|
|
2695
|
+
assertInstanceType(ref["_".concat(field)], definition.constructor, ref, field);
|
|
2695
2696
|
//
|
|
2696
2697
|
// Encode refId for this instance.
|
|
2697
2698
|
// The actual instance is going to be encoded on next `changeTree` iteration.
|
|
@@ -2893,11 +2894,12 @@ var Schema = /** @class */ (function () {
|
|
|
2893
2894
|
return filteredBytes;
|
|
2894
2895
|
};
|
|
2895
2896
|
Schema.prototype.clone = function () {
|
|
2897
|
+
var _a;
|
|
2896
2898
|
var cloned = new (this.constructor);
|
|
2897
2899
|
var schema = this._definition.schema;
|
|
2898
2900
|
for (var field in schema) {
|
|
2899
2901
|
if (typeof (this[field]) === "object" &&
|
|
2900
|
-
typeof (this[field].clone) === "function") {
|
|
2902
|
+
typeof ((_a = this[field]) === null || _a === void 0 ? void 0 : _a.clone) === "function") {
|
|
2901
2903
|
// deep clone
|
|
2902
2904
|
cloned[field] = this[field].clone();
|
|
2903
2905
|
}
|
|
@@ -2930,7 +2932,7 @@ var Schema = /** @class */ (function () {
|
|
|
2930
2932
|
if (!deprecated[field] && this[field] !== null && typeof (this[field]) !== "undefined") {
|
|
2931
2933
|
obj[field] = (typeof (this[field]['toJSON']) === "function")
|
|
2932
2934
|
? this[field]['toJSON']()
|
|
2933
|
-
: this["_"
|
|
2935
|
+
: this["_".concat(field)];
|
|
2934
2936
|
}
|
|
2935
2937
|
}
|
|
2936
2938
|
return obj;
|
|
@@ -2973,7 +2975,7 @@ var Schema = /** @class */ (function () {
|
|
|
2973
2975
|
if (ref instanceof Schema) {
|
|
2974
2976
|
var schema = ref._definition.schema;
|
|
2975
2977
|
for (var fieldName in schema) {
|
|
2976
|
-
var _field = "_"
|
|
2978
|
+
var _field = "_".concat(fieldName);
|
|
2977
2979
|
var value = ref[_field];
|
|
2978
2980
|
if (value !== undefined) {
|
|
2979
2981
|
changes.push({
|