@colyseus/schema 4.0.0 → 4.0.2
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 → index.cjs} +1 -1
- package/build/cjs/index.cjs.map +1 -0
- package/build/esm/index.mjs.map +1 -1
- package/lib/Metadata.js +54 -58
- package/lib/Metadata.js.map +1 -1
- package/lib/Reflection.js +29 -32
- package/lib/Reflection.js.map +1 -1
- package/lib/Schema.js +41 -45
- package/lib/Schema.js.map +1 -1
- package/lib/annotations.d.ts +1 -1
- package/lib/annotations.js +58 -69
- package/lib/annotations.js.map +1 -1
- package/lib/bench_encode.js +24 -59
- package/lib/bench_encode.js.map +1 -1
- package/lib/benchmark.d.ts +1 -0
- package/lib/benchmark.js +218 -0
- package/lib/benchmark.js.map +1 -0
- package/lib/codegen/api.js +10 -41
- package/lib/codegen/api.js.map +1 -1
- package/lib/codegen/argv.js +1 -3
- package/lib/codegen/argv.js.map +1 -1
- package/lib/codegen/cli.js +4 -9
- package/lib/codegen/cli.js.map +1 -1
- package/lib/codegen/languages/cpp.js +5 -8
- package/lib/codegen/languages/cpp.js.map +1 -1
- package/lib/codegen/languages/csharp.js +5 -8
- package/lib/codegen/languages/csharp.js.map +1 -1
- package/lib/codegen/languages/haxe.js +3 -6
- package/lib/codegen/languages/haxe.js.map +1 -1
- package/lib/codegen/languages/java.js +3 -6
- package/lib/codegen/languages/java.js.map +1 -1
- package/lib/codegen/languages/js.js +4 -7
- package/lib/codegen/languages/js.js.map +1 -1
- package/lib/codegen/languages/lua.js +4 -7
- package/lib/codegen/languages/lua.js.map +1 -1
- package/lib/codegen/languages/ts.js +5 -8
- package/lib/codegen/languages/ts.js.map +1 -1
- package/lib/codegen/parser.js +22 -59
- package/lib/codegen/parser.js.map +1 -1
- package/lib/codegen/types.js +12 -51
- package/lib/codegen/types.js.map +1 -1
- package/lib/decoder/DecodeOperation.js +44 -51
- package/lib/decoder/DecodeOperation.js.map +1 -1
- package/lib/decoder/Decoder.js +24 -28
- package/lib/decoder/Decoder.js.map +1 -1
- package/lib/decoder/ReferenceTracker.js +10 -14
- package/lib/decoder/ReferenceTracker.js.map +1 -1
- package/lib/decoder/strategy/Callbacks.js +39 -43
- package/lib/decoder/strategy/Callbacks.js.map +1 -1
- package/lib/decoder/strategy/RawChanges.js +1 -4
- package/lib/decoder/strategy/RawChanges.js.map +1 -1
- package/lib/decoder/strategy/getDecoderStateCallbacks.js +24 -27
- package/lib/decoder/strategy/getDecoderStateCallbacks.js.map +1 -1
- package/lib/encoder/ChangeTree.js +36 -44
- package/lib/encoder/ChangeTree.js.map +1 -1
- package/lib/encoder/EncodeOperation.js +25 -32
- package/lib/encoder/EncodeOperation.js.map +1 -1
- package/lib/encoder/Encoder.d.ts +5 -5
- package/lib/encoder/Encoder.js +25 -29
- package/lib/encoder/Encoder.js.map +1 -1
- package/lib/encoder/Root.js +17 -21
- package/lib/encoder/Root.js.map +1 -1
- package/lib/encoder/StateView.js +40 -45
- package/lib/encoder/StateView.js.map +1 -1
- package/lib/encoding/assert.js +3 -9
- package/lib/encoding/assert.js.map +1 -1
- package/lib/encoding/decode.js +2 -6
- package/lib/encoding/decode.js.map +1 -1
- package/lib/encoding/encode.js +1 -4
- package/lib/encoding/encode.js.map +1 -1
- package/lib/encoding/spec.js +4 -7
- package/lib/encoding/spec.js.map +1 -1
- package/lib/index.d.ts +2 -2
- package/lib/index.js +33 -75
- package/lib/index.js.map +1 -1
- package/lib/src/Metadata.d.ts +49 -0
- package/lib/src/Metadata.js +256 -0
- package/lib/src/Metadata.js.map +1 -0
- package/lib/src/Reflection.d.ts +71 -0
- package/lib/src/Reflection.js +179 -0
- package/lib/src/Reflection.js.map +1 -0
- package/lib/src/Schema.d.ts +86 -0
- package/lib/src/Schema.js +352 -0
- package/lib/src/Schema.js.map +1 -0
- package/lib/src/annotations.d.ts +109 -0
- package/lib/src/annotations.js +473 -0
- package/lib/src/annotations.js.map +1 -0
- package/lib/src/bench_encode.d.ts +1 -0
- package/lib/src/bench_encode.js +91 -0
- package/lib/src/bench_encode.js.map +1 -0
- package/lib/src/codegen/api.d.ts +7 -0
- package/lib/src/codegen/api.js +56 -0
- package/lib/src/codegen/api.js.map +1 -0
- package/lib/src/codegen/argv.d.ts +6 -0
- package/lib/src/codegen/argv.js +39 -0
- package/lib/src/codegen/argv.js.map +1 -0
- package/lib/src/codegen/cli.d.ts +1 -0
- package/lib/src/codegen/cli.js +58 -0
- package/lib/src/codegen/cli.js.map +1 -0
- package/lib/src/codegen/languages/cpp.d.ts +3 -0
- package/lib/src/codegen/languages/cpp.js +258 -0
- package/lib/src/codegen/languages/cpp.js.map +1 -0
- package/lib/src/codegen/languages/csharp.d.ts +4 -0
- package/lib/src/codegen/languages/csharp.js +154 -0
- package/lib/src/codegen/languages/csharp.js.map +1 -0
- package/lib/src/codegen/languages/haxe.d.ts +3 -0
- package/lib/src/codegen/languages/haxe.js +100 -0
- package/lib/src/codegen/languages/haxe.js.map +1 -0
- package/lib/src/codegen/languages/java.d.ts +6 -0
- package/lib/src/codegen/languages/java.js +100 -0
- package/lib/src/codegen/languages/java.js.map +1 -0
- package/lib/src/codegen/languages/js.d.ts +3 -0
- package/lib/src/codegen/languages/js.js +101 -0
- package/lib/src/codegen/languages/js.js.map +1 -0
- package/lib/src/codegen/languages/lua.d.ts +3 -0
- package/lib/src/codegen/languages/lua.js +103 -0
- package/lib/src/codegen/languages/lua.js.map +1 -0
- package/lib/src/codegen/languages/ts.d.ts +3 -0
- package/lib/src/codegen/languages/ts.js +116 -0
- package/lib/src/codegen/languages/ts.js.map +1 -0
- package/lib/src/codegen/parser.d.ts +13 -0
- package/lib/src/codegen/parser.js +327 -0
- package/lib/src/codegen/parser.js.map +1 -0
- package/lib/src/codegen/types.d.ts +52 -0
- package/lib/src/codegen/types.js +142 -0
- package/lib/src/codegen/types.js.map +1 -0
- package/lib/src/decoder/DecodeOperation.d.ts +23 -0
- package/lib/src/decoder/DecodeOperation.js +248 -0
- package/lib/src/decoder/DecodeOperation.js.map +1 -0
- package/lib/src/decoder/Decoder.d.ts +21 -0
- package/lib/src/decoder/Decoder.js +114 -0
- package/lib/src/decoder/Decoder.js.map +1 -0
- package/lib/src/decoder/ReferenceTracker.d.ts +25 -0
- package/lib/src/decoder/ReferenceTracker.js +135 -0
- package/lib/src/decoder/ReferenceTracker.js.map +1 -0
- package/lib/src/decoder/strategy/Callbacks.d.ts +154 -0
- package/lib/src/decoder/strategy/Callbacks.js +336 -0
- package/lib/src/decoder/strategy/Callbacks.js.map +1 -0
- package/lib/src/decoder/strategy/RawChanges.d.ts +3 -0
- package/lib/src/decoder/strategy/RawChanges.js +4 -0
- package/lib/src/decoder/strategy/RawChanges.js.map +1 -0
- package/lib/src/decoder/strategy/getDecoderStateCallbacks.d.ts +76 -0
- package/lib/src/decoder/strategy/getDecoderStateCallbacks.js +274 -0
- package/lib/src/decoder/strategy/getDecoderStateCallbacks.js.map +1 -0
- package/lib/src/encoder/ChangeTree.d.ts +135 -0
- package/lib/src/encoder/ChangeTree.js +534 -0
- package/lib/src/encoder/ChangeTree.js.map +1 -0
- package/lib/src/encoder/EncodeOperation.d.ts +22 -0
- package/lib/src/encoder/EncodeOperation.js +132 -0
- package/lib/src/encoder/EncodeOperation.js.map +1 -0
- package/lib/src/encoder/Encoder.d.ts +22 -0
- package/lib/src/encoder/Encoder.js +204 -0
- package/lib/src/encoder/Encoder.js.map +1 -0
- package/lib/src/encoder/Root.d.ts +28 -0
- package/lib/src/encoder/Root.js +229 -0
- package/lib/src/encoder/Root.js.map +1 -0
- package/lib/src/encoder/StateView.d.ts +34 -0
- package/lib/src/encoder/StateView.js +279 -0
- package/lib/src/encoder/StateView.js.map +1 -0
- package/lib/src/encoding/assert.d.ts +10 -0
- package/lib/src/encoding/assert.js +49 -0
- package/lib/src/encoding/assert.js.map +1 -0
- package/lib/src/encoding/decode.d.ts +67 -0
- package/lib/src/encoding/decode.js +217 -0
- package/lib/src/encoding/decode.js.map +1 -0
- package/lib/src/encoding/encode.d.ts +40 -0
- package/lib/src/encoding/encode.js +279 -0
- package/lib/src/encoding/encode.js.map +1 -0
- package/lib/src/encoding/spec.d.ts +24 -0
- package/lib/src/encoding/spec.js +26 -0
- package/lib/src/encoding/spec.js.map +1 -0
- package/lib/src/index.d.ts +32 -0
- package/lib/src/index.js +39 -0
- package/lib/src/index.js.map +1 -0
- package/lib/src/symbol.shim.d.ts +6 -0
- package/lib/src/symbol.shim.js +3 -0
- package/lib/src/symbol.shim.js.map +1 -0
- package/lib/src/types/HelperTypes.d.ts +77 -0
- package/lib/src/types/HelperTypes.js +2 -0
- package/lib/src/types/HelperTypes.js.map +1 -0
- package/lib/src/types/TypeContext.d.ts +31 -0
- package/lib/src/types/TypeContext.js +143 -0
- package/lib/src/types/TypeContext.js.map +1 -0
- package/lib/src/types/custom/ArraySchema.d.ts +270 -0
- package/lib/src/types/custom/ArraySchema.js +733 -0
- package/lib/src/types/custom/ArraySchema.js.map +1 -0
- package/lib/src/types/custom/CollectionSchema.d.ts +51 -0
- package/lib/src/types/custom/CollectionSchema.js +170 -0
- package/lib/src/types/custom/CollectionSchema.js.map +1 -0
- package/lib/src/types/custom/MapSchema.d.ts +51 -0
- package/lib/src/types/custom/MapSchema.js +222 -0
- package/lib/src/types/custom/MapSchema.js.map +1 -0
- package/lib/src/types/custom/SetSchema.d.ts +48 -0
- package/lib/src/types/custom/SetSchema.js +178 -0
- package/lib/src/types/custom/SetSchema.js.map +1 -0
- package/lib/src/types/registry.d.ts +16 -0
- package/lib/src/types/registry.js +30 -0
- package/lib/src/types/registry.js.map +1 -0
- package/lib/src/types/symbols.d.ts +33 -0
- package/lib/src/types/symbols.js +34 -0
- package/lib/src/types/symbols.js.map +1 -0
- package/lib/src/types/utils.d.ts +1 -0
- package/lib/src/types/utils.js +13 -0
- package/lib/src/types/utils.js.map +1 -0
- package/lib/src/utils.d.ts +13 -0
- package/lib/src/utils.js +49 -0
- package/lib/src/utils.js.map +1 -0
- package/lib/src/v3_bench.d.ts +1 -0
- package/lib/src/v3_bench.js +128 -0
- package/lib/src/v3_bench.js.map +1 -0
- package/lib/symbol.shim.js +1 -2
- package/lib/symbol.shim.js.map +1 -1
- package/lib/types/HelperTypes.js +1 -2
- package/lib/types/TypeContext.js +8 -12
- package/lib/types/TypeContext.js.map +1 -1
- package/lib/types/custom/ArraySchema.js +63 -67
- package/lib/types/custom/ArraySchema.js.map +1 -1
- package/lib/types/custom/CollectionSchema.js +27 -31
- package/lib/types/custom/CollectionSchema.js.map +1 -1
- package/lib/types/custom/MapSchema.js +37 -41
- package/lib/types/custom/MapSchema.js.map +1 -1
- package/lib/types/custom/SetSchema.js +28 -32
- package/lib/types/custom/SetSchema.js.map +1 -1
- package/lib/types/registry.js +13 -20
- package/lib/types/registry.js.map +1 -1
- package/lib/types/symbols.js +16 -19
- package/lib/types/symbols.js.map +1 -1
- package/lib/types/utils.js +1 -4
- package/lib/types/utils.js.map +1 -1
- package/lib/utils.js +9 -14
- package/lib/utils.js.map +1 -1
- package/lib/v3_bench.js +17 -19
- package/lib/v3_bench.js.map +1 -1
- package/package.json +9 -7
- package/src/codegen/api.ts +7 -0
- package/src/codegen/parser.ts +2 -2
- package/src/codegen/types.ts +5 -0
- package/src/encoder/Encoder.ts +24 -7
- package/src/index.ts +2 -2
- package/build/cjs/index.js.map +0 -1
- package/src/bench_encode.ts +0 -64
package/lib/encoding/spec.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"spec.js","sourceRoot":"","sources":["../../src/encoding/spec.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"spec.js","sourceRoot":"","sources":["../../src/encoding/spec.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,mBAAmB,GAAG,GAAG,CAAC,CAAC,4DAA4D;AACpG,MAAM,CAAC,MAAM,OAAO,GAAG,GAAG,CAAC;AAE3B;;GAEG;AACH,MAAM,CAAN,IAAY,SAoBX;AApBD,WAAY,SAAS;IACjB,yCAAS,CAAA;IACT,+CAAW,CAAA;IACX,8CAAW,CAAA;IACX,gEAAoB,CAAA;IACpB,2DAAkB,CAAA;IAClB,+DAAoB,CAAA;IAEpB;;OAEG;IACH,4CAAU,CAAA;IAEV;;OAEG;IACH,gDAAY,CAAA;IACZ,0CAAS,CAAA;IACT,gEAAoB,CAAA;IACpB,2DAAkB,CAAA;AACtB,CAAC,EApBW,SAAS,KAAT,SAAS,QAoBpB","sourcesContent":["export const SWITCH_TO_STRUCTURE = 255; // (decoding collides with DELETE_AND_ADD + fieldIndex = 63)\nexport const TYPE_ID = 213;\n\n/**\n * Encoding Schema field operations.\n */\nexport enum OPERATION {\n ADD = 128, // (10000000) add new structure/primitive\n REPLACE = 0, // (00000001) replace structure/primitive\n DELETE = 64, // (01000000) delete field\n DELETE_AND_MOVE = 96, // () ArraySchema only\n MOVE_AND_ADD = 160, // () ArraySchema only\n DELETE_AND_ADD = 192, // (11000000) DELETE field, followed by an ADD\n\n /**\n * Collection operations\n */\n CLEAR = 10,\n\n /**\n * ArraySchema operations\n */\n REVERSE = 15,\n MOVE = 32,\n DELETE_BY_REFID = 33, // This operation is only used at ENCODING time. During DECODING, DELETE_BY_REFID is converted to DELETE\n ADD_BY_REFID = 129,\n}\n"]}
|
package/lib/index.d.ts
CHANGED
|
@@ -20,12 +20,12 @@ export { Metadata } from "./Metadata";
|
|
|
20
20
|
export { type, deprecated, defineTypes, view, schema, entity, type DefinitionType, type PrimitiveType, type Definition, type SchemaWithExtendsConstructor, type SchemaWithExtends, type SchemaType, } from "./annotations";
|
|
21
21
|
export { TypeContext } from "./types/TypeContext";
|
|
22
22
|
export type { InferValueType, InferSchemaInstanceType, AssignableProps } from "./types/HelperTypes";
|
|
23
|
-
export { getDecoderStateCallbacks, CallbackProxy, SchemaCallback, CollectionCallback, SchemaCallbackProxy } from "./decoder/strategy/getDecoderStateCallbacks";
|
|
23
|
+
export { getDecoderStateCallbacks, type CallbackProxy, type SchemaCallback, type CollectionCallback, type SchemaCallbackProxy } from "./decoder/strategy/getDecoderStateCallbacks";
|
|
24
24
|
export { Callbacks, StateCallbackStrategy } from "./decoder/strategy/Callbacks";
|
|
25
25
|
export { getRawChangesCallback } from "./decoder/strategy/RawChanges";
|
|
26
26
|
export { Encoder } from "./encoder/Encoder";
|
|
27
27
|
export { encodeSchemaOperation, encodeArray, encodeKeyValueOperation } from "./encoder/EncodeOperation";
|
|
28
|
-
export { ChangeTree, Ref, IRef, type ChangeSetName, type ChangeSet } from "./encoder/ChangeTree";
|
|
28
|
+
export { ChangeTree, type Ref, type IRef, type ChangeSetName, type ChangeSet } from "./encoder/ChangeTree";
|
|
29
29
|
export { StateView } from "./encoder/StateView";
|
|
30
30
|
export { Decoder } from "./decoder/Decoder";
|
|
31
31
|
export { decodeSchemaOperation, decodeKeyValueOperation } from "./decoder/DecodeOperation";
|
package/lib/index.js
CHANGED
|
@@ -1,81 +1,39 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
Object.defineProperty(exports, "defineCustomTypes", { enumerable: true, get: function () { return registry_1.defineCustomTypes; } });
|
|
17
|
-
(0, registry_1.registerType)("map", { constructor: MapSchema_1.MapSchema });
|
|
18
|
-
(0, registry_1.registerType)("array", { constructor: ArraySchema_1.ArraySchema });
|
|
19
|
-
(0, registry_1.registerType)("set", { constructor: SetSchema_1.SetSchema });
|
|
20
|
-
(0, registry_1.registerType)("collection", { constructor: CollectionSchema_1.CollectionSchema, });
|
|
1
|
+
export { Schema } from "./Schema";
|
|
2
|
+
import { MapSchema } from "./types/custom/MapSchema";
|
|
3
|
+
export { MapSchema };
|
|
4
|
+
import { ArraySchema } from "./types/custom/ArraySchema";
|
|
5
|
+
export { ArraySchema };
|
|
6
|
+
import { CollectionSchema } from "./types/custom/CollectionSchema";
|
|
7
|
+
export { CollectionSchema };
|
|
8
|
+
import { SetSchema } from "./types/custom/SetSchema";
|
|
9
|
+
export { SetSchema };
|
|
10
|
+
import { registerType, defineCustomTypes } from "./types/registry";
|
|
11
|
+
export { registerType, defineCustomTypes };
|
|
12
|
+
registerType("map", { constructor: MapSchema });
|
|
13
|
+
registerType("array", { constructor: ArraySchema });
|
|
14
|
+
registerType("set", { constructor: SetSchema });
|
|
15
|
+
registerType("collection", { constructor: CollectionSchema, });
|
|
21
16
|
// Utils
|
|
22
|
-
|
|
23
|
-
Object.defineProperty(exports, "dumpChanges", { enumerable: true, get: function () { return utils_1.dumpChanges; } });
|
|
17
|
+
export { dumpChanges } from "./utils";
|
|
24
18
|
// Encoder / Decoder
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
Object.defineProperty(exports, "$decoder", { enumerable: true, get: function () { return symbols_1.$decoder; } });
|
|
29
|
-
Object.defineProperty(exports, "$filter", { enumerable: true, get: function () { return symbols_1.$filter; } });
|
|
30
|
-
Object.defineProperty(exports, "$getByIndex", { enumerable: true, get: function () { return symbols_1.$getByIndex; } });
|
|
31
|
-
Object.defineProperty(exports, "$deleteByIndex", { enumerable: true, get: function () { return symbols_1.$deleteByIndex; } });
|
|
32
|
-
Object.defineProperty(exports, "$changes", { enumerable: true, get: function () { return symbols_1.$changes; } });
|
|
33
|
-
Object.defineProperty(exports, "$childType", { enumerable: true, get: function () { return symbols_1.$childType; } });
|
|
34
|
-
Object.defineProperty(exports, "$refId", { enumerable: true, get: function () { return symbols_1.$refId; } });
|
|
35
|
-
var encode_1 = require("./encoding/encode");
|
|
36
|
-
Object.defineProperty(exports, "encode", { enumerable: true, get: function () { return encode_1.encode; } });
|
|
37
|
-
var decode_1 = require("./encoding/decode");
|
|
38
|
-
Object.defineProperty(exports, "decode", { enumerable: true, get: function () { return decode_1.decode; } });
|
|
19
|
+
export { $track, $encoder, $decoder, $filter, $getByIndex, $deleteByIndex, $changes, $childType, $refId } from "./types/symbols";
|
|
20
|
+
export { encode } from "./encoding/encode";
|
|
21
|
+
export { decode } from "./encoding/decode";
|
|
39
22
|
// Reflection
|
|
40
|
-
|
|
41
|
-
Object.defineProperty(exports, "Reflection", { enumerable: true, get: function () { return Reflection_1.Reflection; } });
|
|
42
|
-
Object.defineProperty(exports, "ReflectionType", { enumerable: true, get: function () { return Reflection_1.ReflectionType; } });
|
|
43
|
-
Object.defineProperty(exports, "ReflectionField", { enumerable: true, get: function () { return Reflection_1.ReflectionField; } });
|
|
23
|
+
export { Reflection, ReflectionType, ReflectionField, } from "./Reflection";
|
|
44
24
|
// Annotations, Metadata and TypeContext
|
|
45
|
-
|
|
46
|
-
Object.defineProperty(exports, "Metadata", { enumerable: true, get: function () { return Metadata_1.Metadata; } });
|
|
25
|
+
export { Metadata } from "./Metadata";
|
|
47
26
|
// Schema definition types
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
Object.defineProperty(exports, "Callbacks", { enumerable: true, get: function () { return Callbacks_1.Callbacks; } });
|
|
61
|
-
Object.defineProperty(exports, "StateCallbackStrategy", { enumerable: true, get: function () { return Callbacks_1.StateCallbackStrategy; } });
|
|
62
|
-
var RawChanges_1 = require("./decoder/strategy/RawChanges");
|
|
63
|
-
Object.defineProperty(exports, "getRawChangesCallback", { enumerable: true, get: function () { return RawChanges_1.getRawChangesCallback; } });
|
|
64
|
-
var Encoder_1 = require("./encoder/Encoder");
|
|
65
|
-
Object.defineProperty(exports, "Encoder", { enumerable: true, get: function () { return Encoder_1.Encoder; } });
|
|
66
|
-
var EncodeOperation_1 = require("./encoder/EncodeOperation");
|
|
67
|
-
Object.defineProperty(exports, "encodeSchemaOperation", { enumerable: true, get: function () { return EncodeOperation_1.encodeSchemaOperation; } });
|
|
68
|
-
Object.defineProperty(exports, "encodeArray", { enumerable: true, get: function () { return EncodeOperation_1.encodeArray; } });
|
|
69
|
-
Object.defineProperty(exports, "encodeKeyValueOperation", { enumerable: true, get: function () { return EncodeOperation_1.encodeKeyValueOperation; } });
|
|
70
|
-
var ChangeTree_1 = require("./encoder/ChangeTree");
|
|
71
|
-
Object.defineProperty(exports, "ChangeTree", { enumerable: true, get: function () { return ChangeTree_1.ChangeTree; } });
|
|
72
|
-
var StateView_1 = require("./encoder/StateView");
|
|
73
|
-
Object.defineProperty(exports, "StateView", { enumerable: true, get: function () { return StateView_1.StateView; } });
|
|
74
|
-
var Decoder_1 = require("./decoder/Decoder");
|
|
75
|
-
Object.defineProperty(exports, "Decoder", { enumerable: true, get: function () { return Decoder_1.Decoder; } });
|
|
76
|
-
var DecodeOperation_1 = require("./decoder/DecodeOperation");
|
|
77
|
-
Object.defineProperty(exports, "decodeSchemaOperation", { enumerable: true, get: function () { return DecodeOperation_1.decodeSchemaOperation; } });
|
|
78
|
-
Object.defineProperty(exports, "decodeKeyValueOperation", { enumerable: true, get: function () { return DecodeOperation_1.decodeKeyValueOperation; } });
|
|
79
|
-
var spec_1 = require("./encoding/spec");
|
|
80
|
-
Object.defineProperty(exports, "OPERATION", { enumerable: true, get: function () { return spec_1.OPERATION; } });
|
|
27
|
+
export { type, deprecated, defineTypes, view, schema, entity, } from "./annotations";
|
|
28
|
+
export { TypeContext } from "./types/TypeContext";
|
|
29
|
+
export { getDecoderStateCallbacks } from "./decoder/strategy/getDecoderStateCallbacks";
|
|
30
|
+
export { Callbacks, StateCallbackStrategy } from "./decoder/strategy/Callbacks";
|
|
31
|
+
export { getRawChangesCallback } from "./decoder/strategy/RawChanges";
|
|
32
|
+
export { Encoder } from "./encoder/Encoder";
|
|
33
|
+
export { encodeSchemaOperation, encodeArray, encodeKeyValueOperation } from "./encoder/EncodeOperation";
|
|
34
|
+
export { ChangeTree } from "./encoder/ChangeTree";
|
|
35
|
+
export { StateView } from "./encoder/StateView";
|
|
36
|
+
export { Decoder } from "./decoder/Decoder";
|
|
37
|
+
export { decodeSchemaOperation, decodeKeyValueOperation } from "./decoder/DecodeOperation";
|
|
38
|
+
export { OPERATION } from "./encoding/spec";
|
|
81
39
|
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAIlC,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAA;AACpD,OAAO,EAAE,SAAS,EAAE,CAAC;AAErB,OAAO,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAC;AACzD,OAAO,EAAE,WAAW,EAAE,CAAC;AAEvB,OAAO,EAAE,gBAAgB,EAAE,MAAM,iCAAiC,CAAC;AACnE,OAAO,EAAE,gBAAgB,EAAE,CAAC;AAE5B,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AACrD,OAAO,EAAE,SAAS,EAAE,CAAC;AAErB,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AACnE,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAAE,CAAC;AAE3C,YAAY,CAAC,KAAK,EAAE,EAAE,WAAW,EAAE,SAAS,EAAE,CAAC,CAAC;AAChD,YAAY,CAAC,OAAO,EAAE,EAAE,WAAW,EAAE,WAAW,EAAE,CAAC,CAAC;AACpD,YAAY,CAAC,KAAK,EAAE,EAAE,WAAW,EAAE,SAAS,EAAE,CAAC,CAAC;AAChD,YAAY,CAAC,YAAY,EAAE,EAAE,WAAW,EAAE,gBAAgB,GAAG,CAAC,CAAC;AAE/D,QAAQ;AACR,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CA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";\nexport type { ToJSON } from \"./types/HelperTypes\";\n\nimport { MapSchema } from \"./types/custom/MapSchema\"\nexport { MapSchema };\n\nimport { ArraySchema } from \"./types/custom/ArraySchema\";\nexport { ArraySchema };\n\nimport { CollectionSchema } from \"./types/custom/CollectionSchema\";\nexport { CollectionSchema };\n\nimport { SetSchema } from \"./types/custom/SetSchema\";\nexport { SetSchema };\n\nimport { registerType, defineCustomTypes } from \"./types/registry\";\nexport { registerType, defineCustomTypes };\n\nregisterType(\"map\", { constructor: MapSchema });\nregisterType(\"array\", { constructor: ArraySchema });\nregisterType(\"set\", { constructor: SetSchema });\nregisterType(\"collection\", { constructor: CollectionSchema, });\n\n// Utils\nexport { dumpChanges } from \"./utils\";\n\n// Encoder / Decoder\nexport { $track, $encoder, $decoder, $filter, $getByIndex, $deleteByIndex, $changes, $childType, $refId } from \"./types/symbols\";\nexport { encode } from \"./encoding/encode\";\nexport { decode, type Iterator } from \"./encoding/decode\";\n\n// Reflection\nexport {\n Reflection,\n ReflectionType,\n ReflectionField,\n} from \"./Reflection\";\n\n// Annotations, Metadata and TypeContext\nexport { Metadata } from \"./Metadata\";\n\n// Schema definition types\nexport {\n type,\n deprecated,\n defineTypes,\n view,\n schema,\n entity,\n type DefinitionType,\n type PrimitiveType,\n type Definition,\n // Raw schema() return types\n type SchemaWithExtendsConstructor,\n type SchemaWithExtends,\n type SchemaType,\n} from \"./annotations\";\n\nexport { TypeContext } from \"./types/TypeContext\";\n\n// Helper types for type inference\nexport type { InferValueType, InferSchemaInstanceType, AssignableProps } from \"./types/HelperTypes\";\n\nexport { getDecoderStateCallbacks, type CallbackProxy, type SchemaCallback, type CollectionCallback, type SchemaCallbackProxy } from \"./decoder/strategy/getDecoderStateCallbacks\";\nexport { Callbacks, StateCallbackStrategy } from \"./decoder/strategy/Callbacks\";\nexport { getRawChangesCallback } from \"./decoder/strategy/RawChanges\";\n\nexport { Encoder } from \"./encoder/Encoder\";\nexport { encodeSchemaOperation, encodeArray, encodeKeyValueOperation } from \"./encoder/EncodeOperation\";\nexport { ChangeTree, type Ref, type IRef, type ChangeSetName, type ChangeSet} from \"./encoder/ChangeTree\";\nexport { StateView } from \"./encoder/StateView\";\n\nexport { Decoder } from \"./decoder/Decoder\";\nexport { decodeSchemaOperation, decodeKeyValueOperation } from \"./decoder/DecodeOperation\";\n\nexport { OPERATION } from \"./encoding/spec\";"]}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { DefinitionType } from "./annotations";
|
|
2
|
+
import { $descriptors, $fieldIndexesByViewTag, $numFields, $refTypeFieldIndexes, $viewFieldIndexes } from "./types/symbols";
|
|
3
|
+
export type MetadataField = {
|
|
4
|
+
type: DefinitionType;
|
|
5
|
+
name: string;
|
|
6
|
+
index: number;
|
|
7
|
+
tag?: number;
|
|
8
|
+
unreliable?: boolean;
|
|
9
|
+
deprecated?: boolean;
|
|
10
|
+
};
|
|
11
|
+
export type Metadata = {
|
|
12
|
+
[$numFields]: number;
|
|
13
|
+
} & // number of fields
|
|
14
|
+
{
|
|
15
|
+
[$viewFieldIndexes]: number[];
|
|
16
|
+
} & // all field indexes with "view" tag
|
|
17
|
+
{
|
|
18
|
+
[$fieldIndexesByViewTag]: {
|
|
19
|
+
[tag: number]: number[];
|
|
20
|
+
};
|
|
21
|
+
} & // field indexes by "view" tag
|
|
22
|
+
{
|
|
23
|
+
[$refTypeFieldIndexes]: number[];
|
|
24
|
+
} & // all field indexes containing Ref types (Schema, ArraySchema, MapSchema, etc)
|
|
25
|
+
{
|
|
26
|
+
[field: number]: MetadataField;
|
|
27
|
+
} & // index => field name
|
|
28
|
+
{
|
|
29
|
+
[field: string]: number;
|
|
30
|
+
} & // field name => field metadata
|
|
31
|
+
{
|
|
32
|
+
[$descriptors]: {
|
|
33
|
+
[field: string]: PropertyDescriptor;
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
export declare function getNormalizedType(type: any): DefinitionType;
|
|
37
|
+
export declare const Metadata: {
|
|
38
|
+
addField(metadata: any, index: number, name: string, type: DefinitionType, descriptor?: PropertyDescriptor): void;
|
|
39
|
+
setTag(metadata: Metadata, fieldName: string, tag: number): void;
|
|
40
|
+
setFields<T extends {
|
|
41
|
+
new (...args: any[]): InstanceType<T>;
|
|
42
|
+
} = any>(target: T, fields: { [field in keyof InstanceType<T>]?: DefinitionType; }): T;
|
|
43
|
+
isDeprecated(metadata: any, field: string): boolean;
|
|
44
|
+
init(klass: any): void;
|
|
45
|
+
initialize(constructor: any): Metadata;
|
|
46
|
+
isValidInstance(klass: any): boolean;
|
|
47
|
+
getFields(klass: any): any;
|
|
48
|
+
hasViewTagAtIndex(metadata: Metadata, index: number): boolean;
|
|
49
|
+
};
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import { getPropertyDescriptor } from "./annotations";
|
|
2
|
+
import { Schema } from "./Schema";
|
|
3
|
+
import { getType, registeredTypes } from "./types/registry";
|
|
4
|
+
import { $decoder, $descriptors, $encoder, $fieldIndexesByViewTag, $numFields, $refTypeFieldIndexes, $track, $viewFieldIndexes } from "./types/symbols";
|
|
5
|
+
import { TypeContext } from "./types/TypeContext";
|
|
6
|
+
export function getNormalizedType(type) {
|
|
7
|
+
if (Array.isArray(type)) {
|
|
8
|
+
return { array: getNormalizedType(type[0]) };
|
|
9
|
+
}
|
|
10
|
+
else if (typeof (type['type']) !== "undefined") {
|
|
11
|
+
return type['type'];
|
|
12
|
+
}
|
|
13
|
+
else if (isTSEnum(type)) {
|
|
14
|
+
// Detect TS Enum type (either string or number)
|
|
15
|
+
return Object.keys(type).every(key => typeof type[key] === "string")
|
|
16
|
+
? "string"
|
|
17
|
+
: "number";
|
|
18
|
+
}
|
|
19
|
+
else if (typeof type === "object" && type !== null) {
|
|
20
|
+
// Handle collection types
|
|
21
|
+
const collectionType = Object.keys(type).find(k => registeredTypes[k] !== undefined);
|
|
22
|
+
if (collectionType) {
|
|
23
|
+
type[collectionType] = getNormalizedType(type[collectionType]);
|
|
24
|
+
return type;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return type;
|
|
28
|
+
}
|
|
29
|
+
function isTSEnum(_enum) {
|
|
30
|
+
if (typeof _enum === 'function' && _enum[Symbol.metadata]) {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
const keys = Object.keys(_enum);
|
|
34
|
+
const numericFields = keys.filter(k => /\d+/.test(k));
|
|
35
|
+
// Check for number enum (has numeric keys and reverse mapping)
|
|
36
|
+
if (numericFields.length > 0 && numericFields.length === (keys.length / 2) && _enum[_enum[numericFields[0]]] == numericFields[0]) {
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
// Check for string enum (all values are strings and keys match values)
|
|
40
|
+
if (keys.length > 0 && keys.every(key => typeof _enum[key] === 'string' && _enum[key] === key)) {
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
export const Metadata = {
|
|
46
|
+
addField(metadata, index, name, type, descriptor) {
|
|
47
|
+
if (index > 64) {
|
|
48
|
+
throw new Error(`Can't define field '${name}'.\nSchema instances may only have up to 64 fields.`);
|
|
49
|
+
}
|
|
50
|
+
metadata[index] = Object.assign(metadata[index] || {}, // avoid overwriting previous field metadata (@owned / @deprecated)
|
|
51
|
+
{
|
|
52
|
+
type: getNormalizedType(type),
|
|
53
|
+
index,
|
|
54
|
+
name,
|
|
55
|
+
});
|
|
56
|
+
// create "descriptors" map
|
|
57
|
+
Object.defineProperty(metadata, $descriptors, {
|
|
58
|
+
value: metadata[$descriptors] || {},
|
|
59
|
+
enumerable: false,
|
|
60
|
+
configurable: true,
|
|
61
|
+
});
|
|
62
|
+
if (descriptor) {
|
|
63
|
+
// for encoder
|
|
64
|
+
metadata[$descriptors][name] = descriptor;
|
|
65
|
+
metadata[$descriptors][`_${name}`] = {
|
|
66
|
+
value: undefined,
|
|
67
|
+
writable: true,
|
|
68
|
+
enumerable: false,
|
|
69
|
+
configurable: true,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
// for decoder
|
|
74
|
+
metadata[$descriptors][name] = {
|
|
75
|
+
value: undefined,
|
|
76
|
+
writable: true,
|
|
77
|
+
enumerable: true,
|
|
78
|
+
configurable: true,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
// map -1 as last field index
|
|
82
|
+
Object.defineProperty(metadata, $numFields, {
|
|
83
|
+
value: index,
|
|
84
|
+
enumerable: false,
|
|
85
|
+
configurable: true
|
|
86
|
+
});
|
|
87
|
+
// map field name => index (non enumerable)
|
|
88
|
+
Object.defineProperty(metadata, name, {
|
|
89
|
+
value: index,
|
|
90
|
+
enumerable: false,
|
|
91
|
+
configurable: true,
|
|
92
|
+
});
|
|
93
|
+
// if child Ref/complex type, add to -4
|
|
94
|
+
if (typeof (metadata[index].type) !== "string") {
|
|
95
|
+
if (metadata[$refTypeFieldIndexes] === undefined) {
|
|
96
|
+
Object.defineProperty(metadata, $refTypeFieldIndexes, {
|
|
97
|
+
value: [],
|
|
98
|
+
enumerable: false,
|
|
99
|
+
configurable: true,
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
metadata[$refTypeFieldIndexes].push(index);
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
setTag(metadata, fieldName, tag) {
|
|
106
|
+
const index = metadata[fieldName];
|
|
107
|
+
const field = metadata[index];
|
|
108
|
+
// add 'tag' to the field
|
|
109
|
+
field.tag = tag;
|
|
110
|
+
if (!metadata[$viewFieldIndexes]) {
|
|
111
|
+
// -2: all field indexes with "view" tag
|
|
112
|
+
Object.defineProperty(metadata, $viewFieldIndexes, {
|
|
113
|
+
value: [],
|
|
114
|
+
enumerable: false,
|
|
115
|
+
configurable: true
|
|
116
|
+
});
|
|
117
|
+
// -3: field indexes by "view" tag
|
|
118
|
+
Object.defineProperty(metadata, $fieldIndexesByViewTag, {
|
|
119
|
+
value: {},
|
|
120
|
+
enumerable: false,
|
|
121
|
+
configurable: true
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
metadata[$viewFieldIndexes].push(index);
|
|
125
|
+
if (!metadata[$fieldIndexesByViewTag][tag]) {
|
|
126
|
+
metadata[$fieldIndexesByViewTag][tag] = [];
|
|
127
|
+
}
|
|
128
|
+
metadata[$fieldIndexesByViewTag][tag].push(index);
|
|
129
|
+
},
|
|
130
|
+
setFields(target, fields) {
|
|
131
|
+
// for inheritance support
|
|
132
|
+
const constructor = target.prototype.constructor;
|
|
133
|
+
TypeContext.register(constructor);
|
|
134
|
+
const parentClass = Object.getPrototypeOf(constructor);
|
|
135
|
+
const parentMetadata = parentClass && parentClass[Symbol.metadata];
|
|
136
|
+
const metadata = Metadata.initialize(constructor);
|
|
137
|
+
// Use Schema's methods if not defined in the class
|
|
138
|
+
if (!constructor[$track]) {
|
|
139
|
+
constructor[$track] = Schema[$track];
|
|
140
|
+
}
|
|
141
|
+
if (!constructor[$encoder]) {
|
|
142
|
+
constructor[$encoder] = Schema[$encoder];
|
|
143
|
+
}
|
|
144
|
+
if (!constructor[$decoder]) {
|
|
145
|
+
constructor[$decoder] = Schema[$decoder];
|
|
146
|
+
}
|
|
147
|
+
if (!constructor.prototype.toJSON) {
|
|
148
|
+
constructor.prototype.toJSON = Schema.prototype.toJSON;
|
|
149
|
+
}
|
|
150
|
+
//
|
|
151
|
+
// detect index for this field, considering inheritance
|
|
152
|
+
//
|
|
153
|
+
let fieldIndex = metadata[$numFields] // current structure already has fields defined
|
|
154
|
+
?? (parentMetadata && parentMetadata[$numFields]) // parent structure has fields defined
|
|
155
|
+
?? -1; // no fields defined
|
|
156
|
+
fieldIndex++;
|
|
157
|
+
for (const field in fields) {
|
|
158
|
+
const type = getNormalizedType(fields[field]);
|
|
159
|
+
// FIXME: this code is duplicated from @type() annotation
|
|
160
|
+
const complexTypeKlass = typeof (Object.keys(type)[0]) === "string" && getType(Object.keys(type)[0]);
|
|
161
|
+
const childType = (complexTypeKlass)
|
|
162
|
+
? Object.values(type)[0]
|
|
163
|
+
: type;
|
|
164
|
+
Metadata.addField(metadata, fieldIndex, field, type, getPropertyDescriptor(`_${field}`, fieldIndex, childType, complexTypeKlass));
|
|
165
|
+
fieldIndex++;
|
|
166
|
+
}
|
|
167
|
+
return target;
|
|
168
|
+
},
|
|
169
|
+
isDeprecated(metadata, field) {
|
|
170
|
+
return metadata[field].deprecated === true;
|
|
171
|
+
},
|
|
172
|
+
init(klass) {
|
|
173
|
+
//
|
|
174
|
+
// Used only to initialize an empty Schema (Encoder#constructor)
|
|
175
|
+
// TODO: remove/refactor this...
|
|
176
|
+
//
|
|
177
|
+
const metadata = {};
|
|
178
|
+
klass[Symbol.metadata] = metadata;
|
|
179
|
+
Object.defineProperty(metadata, $numFields, {
|
|
180
|
+
value: 0,
|
|
181
|
+
enumerable: false,
|
|
182
|
+
configurable: true,
|
|
183
|
+
});
|
|
184
|
+
},
|
|
185
|
+
initialize(constructor) {
|
|
186
|
+
const parentClass = Object.getPrototypeOf(constructor);
|
|
187
|
+
const parentMetadata = parentClass[Symbol.metadata];
|
|
188
|
+
let metadata = constructor[Symbol.metadata] ?? Object.create(null);
|
|
189
|
+
// make sure inherited classes have their own metadata object.
|
|
190
|
+
if (parentClass !== Schema && metadata === parentMetadata) {
|
|
191
|
+
metadata = Object.create(null);
|
|
192
|
+
if (parentMetadata) {
|
|
193
|
+
//
|
|
194
|
+
// assign parent metadata to current
|
|
195
|
+
//
|
|
196
|
+
Object.setPrototypeOf(metadata, parentMetadata);
|
|
197
|
+
// $numFields
|
|
198
|
+
Object.defineProperty(metadata, $numFields, {
|
|
199
|
+
value: parentMetadata[$numFields],
|
|
200
|
+
enumerable: false,
|
|
201
|
+
configurable: true,
|
|
202
|
+
writable: true,
|
|
203
|
+
});
|
|
204
|
+
// $viewFieldIndexes / $fieldIndexesByViewTag
|
|
205
|
+
if (parentMetadata[$viewFieldIndexes] !== undefined) {
|
|
206
|
+
Object.defineProperty(metadata, $viewFieldIndexes, {
|
|
207
|
+
value: [...parentMetadata[$viewFieldIndexes]],
|
|
208
|
+
enumerable: false,
|
|
209
|
+
configurable: true,
|
|
210
|
+
writable: true,
|
|
211
|
+
});
|
|
212
|
+
Object.defineProperty(metadata, $fieldIndexesByViewTag, {
|
|
213
|
+
value: { ...parentMetadata[$fieldIndexesByViewTag] },
|
|
214
|
+
enumerable: false,
|
|
215
|
+
configurable: true,
|
|
216
|
+
writable: true,
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
// $refTypeFieldIndexes
|
|
220
|
+
if (parentMetadata[$refTypeFieldIndexes] !== undefined) {
|
|
221
|
+
Object.defineProperty(metadata, $refTypeFieldIndexes, {
|
|
222
|
+
value: [...parentMetadata[$refTypeFieldIndexes]],
|
|
223
|
+
enumerable: false,
|
|
224
|
+
configurable: true,
|
|
225
|
+
writable: true,
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
// $descriptors
|
|
229
|
+
Object.defineProperty(metadata, $descriptors, {
|
|
230
|
+
value: { ...parentMetadata[$descriptors] },
|
|
231
|
+
enumerable: false,
|
|
232
|
+
configurable: true,
|
|
233
|
+
writable: true,
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
constructor[Symbol.metadata] = metadata;
|
|
238
|
+
return metadata;
|
|
239
|
+
},
|
|
240
|
+
isValidInstance(klass) {
|
|
241
|
+
return (klass.constructor[Symbol.metadata] &&
|
|
242
|
+
Object.prototype.hasOwnProperty.call(klass.constructor[Symbol.metadata], $numFields));
|
|
243
|
+
},
|
|
244
|
+
getFields(klass) {
|
|
245
|
+
const metadata = klass[Symbol.metadata];
|
|
246
|
+
const fields = {};
|
|
247
|
+
for (let i = 0; i <= metadata[$numFields]; i++) {
|
|
248
|
+
fields[metadata[i].name] = metadata[i].type;
|
|
249
|
+
}
|
|
250
|
+
return fields;
|
|
251
|
+
},
|
|
252
|
+
hasViewTagAtIndex(metadata, index) {
|
|
253
|
+
return metadata?.[$viewFieldIndexes]?.includes(index);
|
|
254
|
+
}
|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,GAAa,KAAK,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;QAClD,MAAM,MAAM,GAAQ,EAAE,CAAC;QACvB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,QAAQ,CAAC,UAAU,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;YAC7C,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;QAChD,CAAC;QACD,OAAO,MAAM,CAAC;IAClB,CAAC;IAED,iBAAiB,CAAC,QAAkB,EAAE,KAAa;QAC/C,OAAO,QAAQ,EAAE,CAAC,iBAAiB,CAAC,EAAE,QAAQ,CAAC,KAAK,CAAC,CAAC;IAC1D,CAAC;CACJ,CAAA","sourcesContent":["import { DefinitionType, getPropertyDescriptor } from \"./annotations\";\nimport { Schema } from \"./Schema\";\nimport { getType, registeredTypes } from \"./types/registry\";\nimport { $decoder, $descriptors, $encoder, $fieldIndexesByViewTag, $numFields, $refTypeFieldIndexes, $track, $viewFieldIndexes } from \"./types/symbols\";\nimport { TypeContext } from \"./types/TypeContext\";\n\nexport type MetadataField = {\n type: DefinitionType,\n name: string,\n index: number,\n tag?: number,\n unreliable?: boolean,\n deprecated?: boolean,\n};\n\nexport type Metadata =\n { [$numFields]: number; } & // number of fields\n { [$viewFieldIndexes]: number[]; } & // all field indexes with \"view\" tag\n { [$fieldIndexesByViewTag]: {[tag: number]: number[]}; } & // field indexes by \"view\" tag\n { [$refTypeFieldIndexes]: number[]; } & // all field indexes containing Ref types (Schema, ArraySchema, MapSchema, etc)\n { [field: number]: MetadataField; } & // index => field name\n { [field: string]: number; } & // field name => field metadata\n { [$descriptors]: { [field: string]: PropertyDescriptor } } // property descriptors\n\nexport function getNormalizedType(type: any): DefinitionType {\n if (Array.isArray(type)) {\n return { array: getNormalizedType(type[0]) };\n\n } else if (typeof (type['type']) !== \"undefined\") {\n return type['type'];\n\n } else if (isTSEnum(type)) {\n // Detect TS Enum type (either string or number)\n return Object.keys(type).every(key => typeof type[key] === \"string\")\n ? \"string\"\n : \"number\";\n\n } else if (typeof type === \"object\" && type !== null) {\n // Handle collection types\n const collectionType = Object.keys(type).find(k => registeredTypes[k] !== undefined);\n if (collectionType) {\n type[collectionType] = getNormalizedType(type[collectionType]);\n return type;\n }\n }\n return type;\n}\n\nfunction isTSEnum(_enum: any) {\n if (typeof _enum === 'function' && _enum[Symbol.metadata]) {\n return false;\n }\n\n const keys = Object.keys(_enum);\n const numericFields = keys.filter(k => /\\d+/.test(k));\n\n // Check for number enum (has numeric keys and reverse mapping)\n if (numericFields.length > 0 && numericFields.length === (keys.length / 2) && _enum[_enum[numericFields[0]]] == numericFields[0]) {\n return true;\n }\n\n // Check for string enum (all values are strings and keys match values)\n if (keys.length > 0 && keys.every(key => typeof _enum[key] === 'string' && _enum[key] === key)) {\n return true;\n }\n\n return false;\n}\n\nexport const Metadata = {\n\n addField(metadata: any, index: number, name: string, type: DefinitionType, descriptor?: PropertyDescriptor) {\n if (index > 64) {\n throw new Error(`Can't define field '${name}'.\\nSchema instances may only have up to 64 fields.`);\n }\n\n metadata[index] = Object.assign(\n metadata[index] || {}, // avoid overwriting previous field metadata (@owned / @deprecated)\n {\n type: getNormalizedType(type),\n index,\n name,\n }\n );\n\n // create \"descriptors\" map\n Object.defineProperty(metadata, $descriptors, {\n value: metadata[$descriptors] || {},\n enumerable: false,\n configurable: true,\n });\n\n if (descriptor) {\n // for encoder\n metadata[$descriptors][name] = descriptor;\n metadata[$descriptors][`_${name}`] = {\n value: undefined,\n writable: true,\n enumerable: false,\n configurable: true,\n };\n } else {\n // for decoder\n metadata[$descriptors][name] = {\n value: undefined,\n writable: true,\n enumerable: true,\n configurable: true,\n };\n }\n\n // map -1 as last field index\n Object.defineProperty(metadata, $numFields, {\n value: index,\n enumerable: false,\n configurable: true\n });\n\n // map field name => index (non enumerable)\n Object.defineProperty(metadata, name, {\n value: index,\n enumerable: false,\n configurable: true,\n });\n\n // if child Ref/complex type, add to -4\n if (typeof (metadata[index].type) !== \"string\") {\n if (metadata[$refTypeFieldIndexes] === undefined) {\n Object.defineProperty(metadata, $refTypeFieldIndexes, {\n value: [],\n enumerable: false,\n configurable: true,\n });\n }\n metadata[$refTypeFieldIndexes].push(index);\n }\n },\n\n setTag(metadata: Metadata, fieldName: string, tag: number) {\n const index = metadata[fieldName];\n const field = metadata[index];\n\n // add 'tag' to the field\n field.tag = tag;\n\n if (!metadata[$viewFieldIndexes]) {\n // -2: all field indexes with \"view\" tag\n Object.defineProperty(metadata, $viewFieldIndexes, {\n value: [],\n enumerable: false,\n configurable: true\n });\n\n // -3: field indexes by \"view\" tag\n Object.defineProperty(metadata, $fieldIndexesByViewTag, {\n value: {},\n enumerable: false,\n configurable: true\n });\n }\n\n metadata[$viewFieldIndexes].push(index);\n\n if (!metadata[$fieldIndexesByViewTag][tag]) {\n metadata[$fieldIndexesByViewTag][tag] = [];\n }\n\n metadata[$fieldIndexesByViewTag][tag].push(index);\n },\n\n setFields<T extends { new (...args: any[]): InstanceType<T> } = any>(target: T, fields: { [field in keyof InstanceType<T>]?: DefinitionType }) {\n // for inheritance support\n const constructor = target.prototype.constructor;\n TypeContext.register(constructor);\n\n const parentClass = Object.getPrototypeOf(constructor);\n const parentMetadata = parentClass && parentClass[Symbol.metadata];\n const metadata = Metadata.initialize(constructor);\n\n // Use Schema's methods if not defined in the class\n if (!constructor[$track]) { constructor[$track] = Schema[$track]; }\n if (!constructor[$encoder]) { constructor[$encoder] = Schema[$encoder]; }\n if (!constructor[$decoder]) { constructor[$decoder] = Schema[$decoder]; }\n if (!constructor.prototype.toJSON) { constructor.prototype.toJSON = Schema.prototype.toJSON; }\n\n //\n // detect index for this field, considering inheritance\n //\n let fieldIndex = metadata[$numFields] // current structure already has fields defined\n ?? (parentMetadata && parentMetadata[$numFields]) // parent structure has fields defined\n ?? -1; // no fields defined\n\n fieldIndex++;\n\n for (const field in fields) {\n const type = getNormalizedType(fields[field]);\n\n // FIXME: this code is duplicated from @type() annotation\n const complexTypeKlass = typeof(Object.keys(type)[0]) === \"string\" && getType(Object.keys(type)[0]);\n\n const childType = (complexTypeKlass)\n ? Object.values(type)[0]\n : type;\n\n Metadata.addField(\n metadata,\n fieldIndex,\n field,\n type,\n getPropertyDescriptor(`_${field}`, fieldIndex, childType, complexTypeKlass)\n );\n\n fieldIndex++;\n }\n\n return target;\n },\n\n isDeprecated(metadata: any, field: string) {\n return metadata[field].deprecated === true;\n },\n\n init(klass: any) {\n //\n // Used only to initialize an empty Schema (Encoder#constructor)\n // TODO: remove/refactor this...\n //\n const metadata = {};\n klass[Symbol.metadata] = metadata;\n Object.defineProperty(metadata, $numFields, {\n value: 0,\n enumerable: false,\n configurable: true,\n });\n },\n\n initialize(constructor: any) {\n const parentClass = Object.getPrototypeOf(constructor);\n const parentMetadata: Metadata = parentClass[Symbol.metadata];\n\n let metadata: Metadata = constructor[Symbol.metadata] ?? Object.create(null);\n\n // make sure inherited classes have their own metadata object.\n if (parentClass !== Schema && metadata === parentMetadata) {\n metadata = Object.create(null);\n\n if (parentMetadata) {\n //\n // assign parent metadata to current\n //\n Object.setPrototypeOf(metadata, parentMetadata);\n\n // $numFields\n Object.defineProperty(metadata, $numFields, {\n value: parentMetadata[$numFields],\n enumerable: false,\n configurable: true,\n writable: true,\n });\n\n // $viewFieldIndexes / $fieldIndexesByViewTag\n if (parentMetadata[$viewFieldIndexes] !== undefined) {\n Object.defineProperty(metadata, $viewFieldIndexes, {\n value: [...parentMetadata[$viewFieldIndexes]],\n enumerable: false,\n configurable: true,\n writable: true,\n });\n Object.defineProperty(metadata, $fieldIndexesByViewTag, {\n value: { ...parentMetadata[$fieldIndexesByViewTag] },\n enumerable: false,\n configurable: true,\n writable: true,\n });\n }\n\n // $refTypeFieldIndexes\n if (parentMetadata[$refTypeFieldIndexes] !== undefined) {\n Object.defineProperty(metadata, $refTypeFieldIndexes, {\n value: [...parentMetadata[$refTypeFieldIndexes]],\n enumerable: false,\n configurable: true,\n writable: true,\n });\n }\n\n // $descriptors\n Object.defineProperty(metadata, $descriptors, {\n value: { ...parentMetadata[$descriptors] },\n enumerable: false,\n configurable: true,\n writable: true,\n });\n }\n }\n\n constructor[Symbol.metadata] = metadata;\n\n return metadata;\n },\n\n isValidInstance(klass: any) {\n return (\n klass.constructor[Symbol.metadata] &&\n Object.prototype.hasOwnProperty.call(klass.constructor[Symbol.metadata], $numFields) as boolean\n );\n },\n\n getFields(klass: any) {\n const metadata: Metadata = klass[Symbol.metadata];\n const fields: any = {};\n for (let i = 0; i <= metadata[$numFields]; i++) {\n fields[metadata[i].name] = metadata[i].type;\n }\n return fields;\n },\n\n hasViewTagAtIndex(metadata: Metadata, index: number) {\n return metadata?.[$viewFieldIndexes]?.includes(index);\n }\n}"]}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { schema, SchemaType } from "./annotations";
|
|
2
|
+
import { Iterator } from "./encoding/decode";
|
|
3
|
+
import { Encoder } from "./encoder/Encoder";
|
|
4
|
+
import { Decoder } from "./decoder/Decoder";
|
|
5
|
+
import { Schema } from "./Schema";
|
|
6
|
+
/**
|
|
7
|
+
* Static methods available on Reflection
|
|
8
|
+
*/
|
|
9
|
+
interface ReflectionStatic {
|
|
10
|
+
/**
|
|
11
|
+
* Encodes the TypeContext of an Encoder into a buffer.
|
|
12
|
+
*
|
|
13
|
+
* @param encoder Encoder instance
|
|
14
|
+
* @param it
|
|
15
|
+
* @returns
|
|
16
|
+
*/
|
|
17
|
+
encode: (encoder: Encoder, it?: Iterator) => Uint8Array;
|
|
18
|
+
/**
|
|
19
|
+
* Decodes the TypeContext from a buffer into a Decoder instance.
|
|
20
|
+
*
|
|
21
|
+
* @param bytes Reflection.encode() output
|
|
22
|
+
* @param it
|
|
23
|
+
* @returns Decoder instance
|
|
24
|
+
*/
|
|
25
|
+
decode: <T extends Schema = Schema>(bytes: Uint8Array, it?: Iterator) => Decoder<T>;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Reflection
|
|
29
|
+
*/
|
|
30
|
+
export declare const ReflectionField: import("./annotations").SchemaWithExtendsConstructor<{
|
|
31
|
+
name: "string";
|
|
32
|
+
type: "string";
|
|
33
|
+
referencedType: "number";
|
|
34
|
+
}, import("./index").AssignableProps<import("./index").InferSchemaInstanceType<{
|
|
35
|
+
name: "string";
|
|
36
|
+
type: "string";
|
|
37
|
+
referencedType: "number";
|
|
38
|
+
}>>, typeof Schema>;
|
|
39
|
+
export type ReflectionField = SchemaType<typeof ReflectionField>;
|
|
40
|
+
export declare const ReflectionType: import("./annotations").SchemaWithExtendsConstructor<{
|
|
41
|
+
id: "number";
|
|
42
|
+
extendsId: "number";
|
|
43
|
+
fields: import("./annotations").SchemaWithExtendsConstructor<{
|
|
44
|
+
name: "string";
|
|
45
|
+
type: "string";
|
|
46
|
+
referencedType: "number";
|
|
47
|
+
}, import("./index").AssignableProps<import("./index").InferSchemaInstanceType<{
|
|
48
|
+
name: "string";
|
|
49
|
+
type: "string";
|
|
50
|
+
referencedType: "number";
|
|
51
|
+
}>>, typeof Schema>[];
|
|
52
|
+
}, import("./index").AssignableProps<import("./index").InferSchemaInstanceType<{
|
|
53
|
+
id: "number";
|
|
54
|
+
extendsId: "number";
|
|
55
|
+
fields: import("./annotations").SchemaWithExtendsConstructor<{
|
|
56
|
+
name: "string";
|
|
57
|
+
type: "string";
|
|
58
|
+
referencedType: "number";
|
|
59
|
+
}, import("./index").AssignableProps<import("./index").InferSchemaInstanceType<{
|
|
60
|
+
name: "string";
|
|
61
|
+
type: "string";
|
|
62
|
+
referencedType: "number";
|
|
63
|
+
}>>, typeof Schema>[];
|
|
64
|
+
}>>, typeof Schema>;
|
|
65
|
+
export type ReflectionType = SchemaType<typeof ReflectionType>;
|
|
66
|
+
export declare const Reflection: ReturnType<typeof schema<{
|
|
67
|
+
types: [typeof ReflectionType];
|
|
68
|
+
rootType: "number";
|
|
69
|
+
}>> & ReflectionStatic;
|
|
70
|
+
export type Reflection = SchemaType<typeof Reflection>;
|
|
71
|
+
export {};
|