@colyseus/schema 2.0.4 → 2.0.6
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 +48 -48
- package/build/cjs/index.js.map +1 -1
- package/build/esm/index.mjs +130 -104
- package/build/esm/index.mjs.map +1 -1
- package/build/umd/index.js +50 -50
- package/lib/Reflection.js +87 -119
- package/lib/Reflection.js.map +1 -1
- package/lib/Schema.js +195 -257
- package/lib/Schema.js.map +1 -1
- package/lib/annotations.d.ts +6 -6
- package/lib/annotations.js +64 -92
- package/lib/annotations.js.map +1 -1
- package/lib/changes/ChangeTree.d.ts +1 -1
- package/lib/changes/ChangeTree.js +63 -70
- package/lib/changes/ChangeTree.js.map +1 -1
- package/lib/changes/ReferenceTracker.js +24 -27
- package/lib/changes/ReferenceTracker.js.map +1 -1
- package/lib/codegen/api.js +9 -9
- package/lib/codegen/api.js.map +1 -1
- package/lib/codegen/argv.d.ts +1 -1
- package/lib/codegen/argv.js +11 -11
- package/lib/codegen/argv.js.map +1 -1
- package/lib/codegen/cli.js +21 -10
- package/lib/codegen/cli.js.map +1 -1
- package/lib/codegen/languages/cpp.js +126 -77
- package/lib/codegen/languages/cpp.js.map +1 -1
- package/lib/codegen/languages/csharp.js +121 -62
- package/lib/codegen/languages/csharp.js.map +1 -1
- package/lib/codegen/languages/haxe.js +34 -26
- package/lib/codegen/languages/haxe.js.map +1 -1
- package/lib/codegen/languages/java.js +39 -27
- package/lib/codegen/languages/java.js.map +1 -1
- package/lib/codegen/languages/js.js +48 -32
- package/lib/codegen/languages/js.js.map +1 -1
- package/lib/codegen/languages/lua.js +35 -24
- package/lib/codegen/languages/lua.js.map +1 -1
- package/lib/codegen/languages/ts.js +63 -68
- package/lib/codegen/languages/ts.js.map +1 -1
- package/lib/codegen/parser.d.ts +9 -1
- package/lib/codegen/parser.js +88 -46
- package/lib/codegen/parser.js.map +1 -1
- package/lib/codegen/types.d.ts +8 -0
- package/lib/codegen/types.js +64 -54
- package/lib/codegen/types.js.map +1 -1
- package/lib/encoding/decode.js +15 -15
- package/lib/encoding/decode.js.map +1 -1
- package/lib/encoding/encode.js +14 -14
- package/lib/encoding/encode.js.map +1 -1
- package/lib/events/EventEmitter.d.ts +1 -1
- package/lib/events/EventEmitter.js +16 -47
- package/lib/events/EventEmitter.js.map +1 -1
- package/lib/filters/index.js +7 -8
- package/lib/filters/index.js.map +1 -1
- package/lib/index.js +11 -11
- package/lib/index.js.map +1 -1
- package/lib/types/ArraySchema.d.ts +1 -1
- package/lib/types/ArraySchema.js +161 -219
- package/lib/types/ArraySchema.js.map +1 -1
- package/lib/types/CollectionSchema.d.ts +1 -1
- package/lib/types/CollectionSchema.js +63 -71
- package/lib/types/CollectionSchema.js.map +1 -1
- package/lib/types/HelperTypes.d.ts +9 -9
- package/lib/types/MapSchema.d.ts +16 -16
- package/lib/types/MapSchema.js +68 -78
- package/lib/types/MapSchema.js.map +1 -1
- package/lib/types/SetSchema.js +62 -71
- package/lib/types/SetSchema.js.map +1 -1
- package/lib/types/index.js +1 -1
- package/lib/types/index.js.map +1 -1
- package/lib/types/typeRegistry.js +1 -1
- package/lib/types/typeRegistry.js.map +1 -1
- package/lib/types/utils.js +9 -10
- package/lib/types/utils.js.map +1 -1
- package/lib/utils.js +10 -13
- package/lib/utils.js.map +1 -1
- package/package.json +18 -15
- package/src/Reflection.ts +159 -0
- package/src/Schema.ts +1024 -0
- package/src/annotations.ts +400 -0
- package/src/changes/ChangeTree.ts +295 -0
- package/src/changes/ReferenceTracker.ts +81 -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 +208 -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 +299 -0
- package/src/codegen/types.ts +177 -0
- package/src/encoding/decode.ts +278 -0
- package/src/encoding/encode.ts +283 -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 +612 -0
- package/src/types/CollectionSchema.ts +199 -0
- package/src/types/HelperTypes.ts +34 -0
- package/src/types/MapSchema.ts +268 -0
- package/src/types/SetSchema.ts +208 -0
- package/src/types/typeRegistry.ts +19 -0
- package/src/types/utils.ts +62 -0
- package/src/utils.ts +28 -0
package/lib/types/utils.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.spliceOne = exports.removeChildRefs = exports.addCallback = void 0;
|
|
4
|
-
|
|
4
|
+
const spec_1 = require("../spec");
|
|
5
5
|
function addCallback($callbacks, op, callback, existing) {
|
|
6
6
|
// initialize list of callbacks
|
|
7
7
|
if (!$callbacks[op]) {
|
|
@@ -13,23 +13,22 @@ function addCallback($callbacks, op, callback, existing) {
|
|
|
13
13
|
// - OPERATION.ADD
|
|
14
14
|
// - OPERATION.REPLACE
|
|
15
15
|
//
|
|
16
|
-
existing
|
|
17
|
-
return
|
|
16
|
+
existing?.forEach((item, key) => callback(item, key));
|
|
17
|
+
return () => spliceOne($callbacks[op], $callbacks[op].indexOf(callback));
|
|
18
18
|
}
|
|
19
19
|
exports.addCallback = addCallback;
|
|
20
20
|
function removeChildRefs(changes) {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
this.$items.forEach(function (item, key) {
|
|
21
|
+
const needRemoveRef = (typeof (this.$changes.getType()) !== "string");
|
|
22
|
+
this.$items.forEach((item, key) => {
|
|
24
23
|
changes.push({
|
|
25
|
-
refId:
|
|
24
|
+
refId: this.$changes.refId,
|
|
26
25
|
op: spec_1.OPERATION.DELETE,
|
|
27
26
|
field: key,
|
|
28
27
|
value: undefined,
|
|
29
28
|
previousValue: item
|
|
30
29
|
});
|
|
31
30
|
if (needRemoveRef) {
|
|
32
|
-
|
|
31
|
+
this.$changes.root.removeRef(item['$changes'].refId);
|
|
33
32
|
}
|
|
34
33
|
});
|
|
35
34
|
}
|
|
@@ -39,8 +38,8 @@ function spliceOne(arr, index) {
|
|
|
39
38
|
if (index === -1 || index >= arr.length) {
|
|
40
39
|
return false;
|
|
41
40
|
}
|
|
42
|
-
|
|
43
|
-
for (
|
|
41
|
+
const len = arr.length - 1;
|
|
42
|
+
for (let i = index; i < len; i++) {
|
|
44
43
|
arr[i] = arr[i + 1];
|
|
45
44
|
}
|
|
46
45
|
arr.length = len;
|
package/lib/types/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/types/utils.ts"],"names":[],"mappings":";;;AACA,
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/types/utils.ts"],"names":[],"mappings":";;;AACA,kCAAoC;AAEpC,SAAgB,WAAW,CACvB,UAAwC,EACxC,EAAa,EACb,QAAuC,EACvC,QAAsE;IAEtE,+BAA+B;IAC/B,IAAI,CAAC,UAAU,CAAC,EAAE,CAAC,EAAE;QACjB,UAAU,CAAC,EAAE,CAAC,GAAG,EAAE,CAAC;KACvB;IAED,UAAU,CAAC,EAAE,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IAE9B,EAAE;IACF,yCAAyC;IACzC,kBAAkB;IAClB,sBAAsB;IACtB,EAAE;IACF,QAAQ,EAAE,OAAO,CAAC,CAAC,IAAI,EAAE,GAAG,EAAE,EAAE,CAAC,QAAQ,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC,CAAC;IAEtD,OAAO,GAAG,EAAE,CAAC,SAAS,CAAC,UAAU,CAAC,EAAE,CAAC,EAAE,UAAU,CAAC,EAAE,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC;AAC7E,CAAC;AArBD,kCAqBC;AAGD,SAAgB,eAAe,CAAyB,OAAqB;IACzE,MAAM,aAAa,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,OAAO,EAAE,CAAC,KAAK,QAAQ,CAAC,CAAC;IAEtE,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,IAAS,EAAE,GAAQ,EAAE,EAAE;QACxC,OAAO,CAAC,IAAI,CAAC;YACT,KAAK,EAAE,IAAI,CAAC,QAAQ,CAAC,KAAK;YAC1B,EAAE,EAAE,gBAAS,CAAC,MAAM;YACpB,KAAK,EAAE,GAAG;YACV,KAAK,EAAE,SAAS;YAChB,aAAa,EAAE,IAAI;SACtB,CAAC,CAAC;QAEH,IAAI,aAAa,EAAE;YACf,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,KAAK,CAAC,CAAC;SACxD;IACL,CAAC,CAAC,CAAC;AACP,CAAC;AAhBD,0CAgBC;AAGD,SAAgB,SAAS,CAAC,GAAU,EAAE,KAAa;IAC/C,2BAA2B;IAC3B,IAAI,KAAK,KAAK,CAAC,CAAC,IAAI,KAAK,IAAI,GAAG,CAAC,MAAM,EAAE;QACrC,OAAO,KAAK,CAAC;KAChB;IAED,MAAM,GAAG,GAAG,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC;IAE3B,KAAK,IAAI,CAAC,GAAG,KAAK,EAAE,CAAC,GAAG,GAAG,EAAE,CAAC,EAAE,EAAE;QAC9B,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;KACvB;IAED,GAAG,CAAC,MAAM,GAAG,GAAG,CAAC;IAEjB,OAAO,IAAI,CAAC;AAChB,CAAC;AAfD,8BAeC","sourcesContent":["import { CollectionSchema, DataChange } from \"..\";\nimport { OPERATION } from \"../spec\";\n\nexport function addCallback(\n $callbacks: { [op: number]: Function[] },\n op: OPERATION,\n callback: (item: any, key: any) => void,\n existing?: { forEach(callback: (item: any, key: any) => void): void; }\n) {\n // initialize list of callbacks\n if (!$callbacks[op]) {\n $callbacks[op] = [];\n }\n\n $callbacks[op].push(callback);\n\n //\n // Trigger callback for existing elements\n // - OPERATION.ADD\n // - OPERATION.REPLACE\n //\n existing?.forEach((item, key) => callback(item, key));\n\n return () => spliceOne($callbacks[op], $callbacks[op].indexOf(callback));\n}\n\n\nexport function removeChildRefs(this: CollectionSchema, changes: DataChange[]) {\n const needRemoveRef = (typeof (this.$changes.getType()) !== \"string\");\n\n this.$items.forEach((item: any, key: any) => {\n changes.push({\n refId: this.$changes.refId,\n op: OPERATION.DELETE,\n field: key,\n value: undefined,\n previousValue: item\n });\n\n if (needRemoveRef) {\n this.$changes.root.removeRef(item['$changes'].refId);\n }\n });\n}\n\n\nexport function spliceOne(arr: any[], index: number): boolean {\n // manually splice an array\n if (index === -1 || index >= arr.length) {\n return false;\n }\n\n const len = arr.length - 1;\n\n for (let i = index; i < len; i++) {\n arr[i] = arr[i + 1];\n }\n\n arr.length = len;\n\n return true;\n}"]}
|
package/lib/utils.js
CHANGED
|
@@ -2,23 +2,20 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.dumpChanges = void 0;
|
|
4
4
|
function dumpChanges(schema) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
changeTree.changes.forEach(
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
5
|
+
const changeTrees = [schema['$changes']];
|
|
6
|
+
let numChangeTrees = 1;
|
|
7
|
+
const dump = {};
|
|
8
|
+
let currentStructure = dump;
|
|
9
|
+
for (let i = 0; i < numChangeTrees; i++) {
|
|
10
|
+
const changeTree = changeTrees[i];
|
|
11
|
+
changeTree.changes.forEach((change) => {
|
|
12
|
+
const ref = changeTree.ref;
|
|
13
|
+
const fieldIndex = change.index;
|
|
14
|
+
const field = (ref['_definition'])
|
|
15
15
|
? ref['_definition'].fieldsByIndex[fieldIndex]
|
|
16
16
|
: ref['$indexes'].get(fieldIndex);
|
|
17
17
|
currentStructure[field] = changeTree.getValue(fieldIndex);
|
|
18
18
|
});
|
|
19
|
-
};
|
|
20
|
-
for (var i = 0; i < numChangeTrees; i++) {
|
|
21
|
-
_loop_1(i);
|
|
22
19
|
}
|
|
23
20
|
return dump;
|
|
24
21
|
}
|
package/lib/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;AAGA,SAAgB,WAAW,CAAC,MAAc;IACtC,
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;AAGA,SAAgB,WAAW,CAAC,MAAc;IACtC,MAAM,WAAW,GAAiB,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC;IACvD,IAAI,cAAc,GAAG,CAAC,CAAC;IAEvB,MAAM,IAAI,GAAG,EAAE,CAAC;IAChB,IAAI,gBAAgB,GAAG,IAAI,CAAC;IAE5B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,cAAc,EAAE,CAAC,EAAE,EAAE;QACrC,MAAM,UAAU,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC;QAElC,UAAU,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,MAAM,EAAE,EAAE;YAClC,MAAM,GAAG,GAAG,UAAU,CAAC,GAAG,CAAC;YAC3B,MAAM,UAAU,GAAG,MAAM,CAAC,KAAK,CAAC;YAEhC,MAAM,KAAK,GAAG,CAAE,GAAc,CAAC,aAAa,CAAC,CAAC;gBAC1C,CAAC,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC,aAAa,CAAC,UAAU,CAAC;gBAC9C,CAAC,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;YAEtC,gBAAgB,CAAC,KAAK,CAAC,GAAG,UAAU,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC;QAC9D,CAAC,CAAC,CAAC;KAEN;IAED,OAAO,IAAI,CAAC;AAChB,CAAC;AAxBD,kCAwBC","sourcesContent":["import { Schema } from \"./\";\nimport { ChangeTree } from \"./changes/ChangeTree\";\n\nexport function dumpChanges(schema: Schema) {\n const changeTrees: ChangeTree[] = [schema['$changes']];\n let numChangeTrees = 1;\n\n const dump = {};\n let currentStructure = dump;\n\n for (let i = 0; i < numChangeTrees; i++) {\n const changeTree = changeTrees[i];\n\n changeTree.changes.forEach((change) => {\n const ref = changeTree.ref;\n const fieldIndex = change.index;\n\n const field = ((ref as Schema)['_definition'])\n ? ref['_definition'].fieldsByIndex[fieldIndex]\n : ref['$indexes'].get(fieldIndex);\n\n currentStructure[field] = changeTree.getValue(fieldIndex);\n });\n\n }\n\n return dump;\n}"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@colyseus/schema",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.6",
|
|
4
4
|
"description": "Binary state serializer with delta encoding for games",
|
|
5
5
|
"bin": {
|
|
6
6
|
"schema-codegen": "./bin/schema-codegen"
|
|
@@ -10,20 +10,22 @@
|
|
|
10
10
|
"watch": "tsc -w",
|
|
11
11
|
"test": "mocha --require ts-node/register test/*Test.ts test/**/*Test.ts",
|
|
12
12
|
"coverage": "nyc mocha --require ts-node/register --require source-map-support/register --recursive test/**Test.ts",
|
|
13
|
-
"generate-test-1": "bin/schema-codegen test-external/PrimitiveTypes.ts --namespace SchemaTest.PrimitiveTypes --output ../colyseus-
|
|
14
|
-
"generate-test-2": "bin/schema-codegen test-external/ChildSchemaTypes.ts --namespace SchemaTest.ChildSchemaTypes --output ../colyseus-
|
|
15
|
-
"generate-test-3": "bin/schema-codegen test-external/ArraySchemaTypes.ts --namespace SchemaTest.ArraySchemaTypes --output ../colyseus-
|
|
16
|
-
"generate-test-4": "bin/schema-codegen test-external/MapSchemaTypes.ts --namespace SchemaTest.MapSchemaTypes --output ../colyseus-
|
|
17
|
-
"generate-test-5": "bin/schema-codegen test-external/InheritedTypes.ts --namespace SchemaTest.InheritedTypes --output ../colyseus-
|
|
18
|
-
"generate-test-6": "bin/schema-codegen test-external/MapSchemaInt8.ts --namespace SchemaTest.MapSchemaInt8 --output ../colyseus-
|
|
19
|
-
"generate-test-7": "bin/schema-codegen test-external/BackwardsForwards.ts --namespace SchemaTest.BackwardsForwards --output ../colyseus-
|
|
20
|
-
"generate-test-8": "bin/schema-codegen test-external/FilteredTypes.ts --namespace SchemaTest.FilteredTypes --output ../colyseus-
|
|
21
|
-
"generate-test-9": "bin/schema-codegen test-external/InstanceSharingTypes.ts --namespace SchemaTest.
|
|
22
|
-
"generate-test-10": "bin/schema-codegen test-external/Callbacks.ts --namespace SchemaTest.Callbacks --output ../colyseus-
|
|
23
|
-
"generate-test-11": "bin/schema-codegen test-external/MapSchemaMoveNullifyType.ts --namespace SchemaTest.MapSchemaMoveNullifyType --output ../colyseus-
|
|
13
|
+
"generate-test-1": "bin/schema-codegen test-external/PrimitiveTypes.ts --namespace SchemaTest.PrimitiveTypes --output ../colyseus-unity-sdk/Assets/Editor/ColyseusTests/Schema/PrimitiveTypes",
|
|
14
|
+
"generate-test-2": "bin/schema-codegen test-external/ChildSchemaTypes.ts --namespace SchemaTest.ChildSchemaTypes --output ../colyseus-unity-sdk/Assets/Editor/ColyseusTests/Schema/ChildSchemaTypes",
|
|
15
|
+
"generate-test-3": "bin/schema-codegen test-external/ArraySchemaTypes.ts --namespace SchemaTest.ArraySchemaTypes --output ../colyseus-unity-sdk/Assets/Editor/ColyseusTests/Schema/ArraySchemaTypes",
|
|
16
|
+
"generate-test-4": "bin/schema-codegen test-external/MapSchemaTypes.ts --namespace SchemaTest.MapSchemaTypes --output ../colyseus-unity-sdk/Assets/Editor/ColyseusTests/Schema/MapSchemaTypes",
|
|
17
|
+
"generate-test-5": "bin/schema-codegen test-external/InheritedTypes.ts --namespace SchemaTest.InheritedTypes --output ../colyseus-unity-sdk/Assets/Editor/ColyseusTests/Schema/InheritedTypes",
|
|
18
|
+
"generate-test-6": "bin/schema-codegen test-external/MapSchemaInt8.ts --namespace SchemaTest.MapSchemaInt8 --output ../colyseus-unity-sdk/Assets/Editor/ColyseusTests/Schema/MapSchemaInt8",
|
|
19
|
+
"generate-test-7": "bin/schema-codegen test-external/BackwardsForwards.ts --namespace SchemaTest.BackwardsForwards --output ../colyseus-unity-sdk/Assets/Editor/ColyseusTests/Schema/BackwardsForwards",
|
|
20
|
+
"generate-test-8": "bin/schema-codegen test-external/FilteredTypes.ts --namespace SchemaTest.FilteredTypes --output ../colyseus-unity-sdk/Assets/Editor/ColyseusTests/Schema/FilteredTypes",
|
|
21
|
+
"generate-test-9": "bin/schema-codegen test-external/InstanceSharingTypes.ts --namespace SchemaTest.InstanceSharingTypes --output ../colyseus-unity-sdk/Assets/Editor/ColyseusTests/Schema/InstanceSharingTypes",
|
|
22
|
+
"generate-test-10": "bin/schema-codegen test-external/Callbacks.ts --namespace SchemaTest.Callbacks --output ../colyseus-unity-sdk/Assets/Editor/ColyseusTests/Schema/Callbacks",
|
|
23
|
+
"generate-test-11": "bin/schema-codegen test-external/MapSchemaMoveNullifyType.ts --namespace SchemaTest.MapSchemaMoveNullifyType --output ../colyseus-unity-sdk/Assets/Editor/ColyseusTests/Schema/MapSchemaMoveNullifyType",
|
|
24
|
+
"generate-test-12": "bin/schema-codegen test-external/ArraySchemaClear --namespace SchemaTest.ArraySchemaClear --output ../colyseus-unity-sdk/Assets/Editor/ColyseusTests/Schema/ArraySchemaClear",
|
|
24
25
|
"prepublishOnly": "npm run build"
|
|
25
26
|
},
|
|
26
27
|
"files": [
|
|
28
|
+
"src",
|
|
27
29
|
"lib",
|
|
28
30
|
"build",
|
|
29
31
|
"bin"
|
|
@@ -36,7 +38,8 @@
|
|
|
36
38
|
".": {
|
|
37
39
|
"browser": "./build/umd/index.js",
|
|
38
40
|
"import": "./build/esm/index.mjs",
|
|
39
|
-
"require": "./build/cjs/index.js"
|
|
41
|
+
"require": "./build/cjs/index.js",
|
|
42
|
+
"types": "./lib/index.d.ts"
|
|
40
43
|
}
|
|
41
44
|
},
|
|
42
45
|
"repository": {
|
|
@@ -67,7 +70,7 @@
|
|
|
67
70
|
"glob": "^7.1.5",
|
|
68
71
|
"mocha": "^5.2.0",
|
|
69
72
|
"nanoid": "^3.1.10",
|
|
70
|
-
"
|
|
73
|
+
"msgpackr": "^1.6.1",
|
|
71
74
|
"nyc": "^14.1.1",
|
|
72
75
|
"rimraf": "^3.0.0",
|
|
73
76
|
"rollup": "^2.40.0",
|
|
@@ -75,7 +78,7 @@
|
|
|
75
78
|
"source-map-support": "^0.5.13",
|
|
76
79
|
"ts-node": "^7.0.1",
|
|
77
80
|
"tslib": "^2.1.0",
|
|
78
|
-
"typescript": "^4.
|
|
81
|
+
"typescript": "^4.8.4"
|
|
79
82
|
},
|
|
80
83
|
"nyc": {
|
|
81
84
|
"extension": [
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { type, PrimitiveType, Context, DefinitionType } from "./annotations";
|
|
2
|
+
import { Schema } from "./Schema";
|
|
3
|
+
import { ArraySchema } from "./types/ArraySchema";
|
|
4
|
+
import { getType } from "./types/typeRegistry";
|
|
5
|
+
import { Iterator } from "./encoding/decode";
|
|
6
|
+
|
|
7
|
+
const reflectionContext = { context: new Context() };
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Reflection
|
|
11
|
+
*/
|
|
12
|
+
export class ReflectionField extends Schema {
|
|
13
|
+
@type("string", reflectionContext)
|
|
14
|
+
name: string;
|
|
15
|
+
|
|
16
|
+
@type("string", reflectionContext)
|
|
17
|
+
type: string;
|
|
18
|
+
|
|
19
|
+
@type("number", reflectionContext)
|
|
20
|
+
referencedType: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export class ReflectionType extends Schema {
|
|
24
|
+
@type("number", reflectionContext)
|
|
25
|
+
id: number;
|
|
26
|
+
|
|
27
|
+
@type([ ReflectionField ], reflectionContext)
|
|
28
|
+
fields: ArraySchema<ReflectionField> = new ArraySchema<ReflectionField>();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export class Reflection extends Schema {
|
|
32
|
+
@type([ ReflectionType ], reflectionContext)
|
|
33
|
+
types: ArraySchema<ReflectionType> = new ArraySchema<ReflectionType>();
|
|
34
|
+
|
|
35
|
+
@type("number", reflectionContext)
|
|
36
|
+
rootType: number;
|
|
37
|
+
|
|
38
|
+
static encode (instance: Schema) {
|
|
39
|
+
const rootSchemaType = instance.constructor as typeof Schema;
|
|
40
|
+
|
|
41
|
+
const reflection = new Reflection();
|
|
42
|
+
reflection.rootType = rootSchemaType._typeid;
|
|
43
|
+
|
|
44
|
+
const buildType = (currentType: ReflectionType, schema: any) => {
|
|
45
|
+
for (let fieldName in schema) {
|
|
46
|
+
const field = new ReflectionField();
|
|
47
|
+
field.name = fieldName;
|
|
48
|
+
|
|
49
|
+
let fieldType: string;
|
|
50
|
+
|
|
51
|
+
if (typeof (schema[fieldName]) === "string") {
|
|
52
|
+
fieldType = schema[fieldName];
|
|
53
|
+
|
|
54
|
+
} else {
|
|
55
|
+
const type = schema[fieldName];
|
|
56
|
+
let childTypeSchema: typeof Schema;
|
|
57
|
+
|
|
58
|
+
//
|
|
59
|
+
// TODO: refactor below.
|
|
60
|
+
//
|
|
61
|
+
if (Schema.is(type)) {
|
|
62
|
+
fieldType = "ref";
|
|
63
|
+
childTypeSchema = schema[fieldName];
|
|
64
|
+
|
|
65
|
+
} else {
|
|
66
|
+
fieldType = Object.keys(type)[0];
|
|
67
|
+
|
|
68
|
+
if (typeof(type[fieldType]) === "string") {
|
|
69
|
+
fieldType += ":" + type[fieldType]; // array:string
|
|
70
|
+
|
|
71
|
+
} else {
|
|
72
|
+
childTypeSchema = type[fieldType];
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
field.referencedType = (childTypeSchema)
|
|
77
|
+
? childTypeSchema._typeid
|
|
78
|
+
: -1;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
field.type = fieldType;
|
|
82
|
+
currentType.fields.push(field);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
reflection.types.push(currentType);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const types = rootSchemaType._context.types;
|
|
89
|
+
for (let typeid in types) {
|
|
90
|
+
const type = new ReflectionType();
|
|
91
|
+
type.id = Number(typeid);
|
|
92
|
+
buildType(type, types[typeid]._definition.schema);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return reflection.encodeAll();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
static decode<T extends Schema = Schema>(bytes: number[], it?: Iterator): T {
|
|
99
|
+
const context = new Context();
|
|
100
|
+
|
|
101
|
+
const reflection = new Reflection();
|
|
102
|
+
reflection.decode(bytes, it);
|
|
103
|
+
|
|
104
|
+
const schemaTypes = reflection.types.reduce((types, reflectionType) => {
|
|
105
|
+
const schema: typeof Schema = class _ extends Schema {};
|
|
106
|
+
const typeid = reflectionType.id;
|
|
107
|
+
types[typeid] = schema
|
|
108
|
+
context.add(schema, typeid);
|
|
109
|
+
return types;
|
|
110
|
+
}, {});
|
|
111
|
+
|
|
112
|
+
reflection.types.forEach((reflectionType) => {
|
|
113
|
+
const schemaType = schemaTypes[reflectionType.id];
|
|
114
|
+
|
|
115
|
+
reflectionType.fields.forEach(field => {
|
|
116
|
+
if (field.referencedType !== undefined) {
|
|
117
|
+
let fieldType = field.type;
|
|
118
|
+
let refType = schemaTypes[field.referencedType];
|
|
119
|
+
|
|
120
|
+
// map or array of primitive type (-1)
|
|
121
|
+
if (!refType) {
|
|
122
|
+
const typeInfo = field.type.split(":");
|
|
123
|
+
fieldType = typeInfo[0];
|
|
124
|
+
refType = typeInfo[1];
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (fieldType === "ref") {
|
|
128
|
+
type(refType, { context })(schemaType.prototype, field.name);
|
|
129
|
+
|
|
130
|
+
} else {
|
|
131
|
+
type({ [fieldType]: refType } as DefinitionType, { context })(schemaType.prototype, field.name);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
} else {
|
|
135
|
+
type(field.type as PrimitiveType, { context })(schemaType.prototype, field.name);
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
const rootType: any = schemaTypes[reflection.rootType];
|
|
141
|
+
const rootInstance = new rootType();
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* auto-initialize referenced types on root type
|
|
145
|
+
* to allow registering listeners immediatelly on client-side
|
|
146
|
+
*/
|
|
147
|
+
for (let fieldName in rootType._definition.schema) {
|
|
148
|
+
const fieldType = rootType._definition.schema[fieldName];
|
|
149
|
+
|
|
150
|
+
if (typeof(fieldType) !== "string") {
|
|
151
|
+
rootInstance[fieldName] = (typeof (fieldType) === "function")
|
|
152
|
+
? new (fieldType as any)() // is a schema reference
|
|
153
|
+
: new (getType(Object.keys(fieldType)[0])).constructor(); // is a "collection"
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
return rootInstance;
|
|
158
|
+
}
|
|
159
|
+
}
|