@bikky/replication 1.0.1

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.
Files changed (140) hide show
  1. package/Constants/Errors.d.ts +27 -0
  2. package/Constants/Errors.js +75 -0
  3. package/Constants/Logging.d.ts +17 -0
  4. package/Constants/Logging.js +97 -0
  5. package/Constants/ReplicableRegistry.d.ts +37 -0
  6. package/Constants/ReplicableRegistry.js +234 -0
  7. package/Constants/SerialisationTypes.d.ts +82 -0
  8. package/Constants/SerialisationTypes.js +160 -0
  9. package/Constants/SourceMaps.d.ts +10 -0
  10. package/Constants/SourceMaps.js +12 -0
  11. package/Constants/TraversalStep.d.ts +5 -0
  12. package/Constants/TraversalStep.js +2 -0
  13. package/Constants/Versions.d.ts +15 -0
  14. package/Constants/Versions.js +63 -0
  15. package/Expressions/Compiler/BuiltinGrammar.d.ts +234 -0
  16. package/Expressions/Compiler/BuiltinGrammar.js +446 -0
  17. package/Expressions/Compiler/ExpressionGrammar.d.ts +89 -0
  18. package/Expressions/Compiler/ExpressionGrammar.js +70 -0
  19. package/Expressions/Compiler/Parser.d.ts +56 -0
  20. package/Expressions/Compiler/Parser.js +314 -0
  21. package/Expressions/Compiler/Tokenizer.d.ts +52 -0
  22. package/Expressions/Compiler/Tokenizer.js +222 -0
  23. package/Expressions/Compiler/__tests__/Replicable.Expressions.Parser.test.d.ts +1 -0
  24. package/Expressions/Compiler/__tests__/Replicable.Expressions.Parser.test.js +516 -0
  25. package/Expressions/Compiler/__tests__/Replicable.Expressions.Tokenizer.test.d.ts +1 -0
  26. package/Expressions/Compiler/__tests__/Replicable.Expressions.Tokenizer.test.js +68 -0
  27. package/Expressions/CreateEvaluator.d.ts +4 -0
  28. package/Expressions/CreateEvaluator.js +85 -0
  29. package/Expressions/EvaluatorChain.d.ts +19 -0
  30. package/Expressions/EvaluatorChain.js +137 -0
  31. package/Expressions/EvaluatorSteps.d.ts +19 -0
  32. package/Expressions/EvaluatorSteps.js +12 -0
  33. package/Expressions/EvaluatorString.d.ts +21 -0
  34. package/Expressions/EvaluatorString.js +26 -0
  35. package/Expressions/Expression.d.ts +36 -0
  36. package/Expressions/Expression.js +147 -0
  37. package/Expressions/Traverser.d.ts +28 -0
  38. package/Expressions/Traverser.js +348 -0
  39. package/Expressions/TypeRegistry/Accessors.d.ts +26 -0
  40. package/Expressions/TypeRegistry/Accessors.js +58 -0
  41. package/Expressions/TypeRegistry/ChainCollections.d.ts +51 -0
  42. package/Expressions/TypeRegistry/ChainCollections.js +134 -0
  43. package/Expressions/TypeRegistry/ChainTypes.d.ts +23 -0
  44. package/Expressions/TypeRegistry/ChainTypes.js +46 -0
  45. package/Expressions/TypeRegistry/CustomAPI.d.ts +36 -0
  46. package/Expressions/TypeRegistry/CustomAPI.js +181 -0
  47. package/Expressions/TypeRegistry/Primitive.d.ts +19 -0
  48. package/Expressions/TypeRegistry/Primitive.js +47 -0
  49. package/Expressions/TypeRegistry/Registry.d.ts +27 -0
  50. package/Expressions/TypeRegistry/Registry.js +270 -0
  51. package/Expressions/TypeRegistry/ReplAPI.d.ts +41 -0
  52. package/Expressions/TypeRegistry/ReplAPI.js +220 -0
  53. package/Expressions/TypeRegistry/Scope.d.ts +24 -0
  54. package/Expressions/TypeRegistry/Scope.js +44 -0
  55. package/Expressions/TypeRegistry/Types.d.ts +23 -0
  56. package/Expressions/TypeRegistry/Types.js +1 -0
  57. package/Expressions/TypeRegistry/__tests__/Replicable.Expressions.Accessors.test.d.ts +1 -0
  58. package/Expressions/TypeRegistry/__tests__/Replicable.Expressions.Accessors.test.js +31 -0
  59. package/Expressions/__tests__/ExpressionExamples.d.ts +28 -0
  60. package/Expressions/__tests__/ExpressionExamples.js +50 -0
  61. package/Expressions/__tests__/Replicable.Expressions.Expressions.test.d.ts +1 -0
  62. package/Expressions/__tests__/Replicable.Expressions.Expressions.test.js +166 -0
  63. package/IDPool.d.ts +18 -0
  64. package/IDPool.data.d.ts +17 -0
  65. package/IDPool.js +139 -0
  66. package/License.txt +1 -0
  67. package/Main.d.ts +13 -0
  68. package/Main.js +13 -0
  69. package/Networking.d.ts +60 -0
  70. package/Networking.js +626 -0
  71. package/Replicatable.d.ts +66 -0
  72. package/Replicatable.js +123 -0
  73. package/Tracking/Buffable.d.ts +68 -0
  74. package/Tracking/Buffable.js +194 -0
  75. package/Tracking/Class.d.ts +97 -0
  76. package/Tracking/Class.js +221 -0
  77. package/Tracking/Functions.d.ts +14 -0
  78. package/Tracking/Functions.js +27 -0
  79. package/Tracking/GlobalGroup.d.ts +5 -0
  80. package/Tracking/GlobalGroup.js +39 -0
  81. package/Tracking/Property.d.ts +95 -0
  82. package/Tracking/Property.js +125 -0
  83. package/Tracking/Types.d.ts +33 -0
  84. package/Tracking/Types.js +1 -0
  85. package/Tracking/__tests__/Replicable.Tracking.Decorator.test.d.ts +1 -0
  86. package/Tracking/__tests__/Replicable.Tracking.Decorator.test.js +151 -0
  87. package/Tracking/__tests__/Replicable.Tracking.Deserialisation.test.d.ts +1 -0
  88. package/Tracking/__tests__/Replicable.Tracking.Deserialisation.test.js +253 -0
  89. package/Tracking/__tests__/Replicable.Tracking.MixinSchemaGeneration.test.d.ts +1 -0
  90. package/Tracking/__tests__/Replicable.Tracking.MixinSchemaGeneration.test.js +135 -0
  91. package/Tracking/__tests__/Replicable.Tracking.Struct.test.d.ts +1 -0
  92. package/Tracking/__tests__/Replicable.Tracking.Struct.test.js +66 -0
  93. package/Tracking/__tests__/Replicable.Tracking.Type.test.d.ts +1 -0
  94. package/Tracking/__tests__/Replicable.Tracking.Type.test.js +67 -0
  95. package/Transformers/Configurer.d.ts +39 -0
  96. package/Transformers/Configurer.js +415 -0
  97. package/Transformers/Constructor.d.ts +12 -0
  98. package/Transformers/Constructor.js +44 -0
  99. package/Transformers/Definitions.d.ts +102 -0
  100. package/Transformers/Definitions.js +626 -0
  101. package/Transformers/Loader.d.ts +45 -0
  102. package/Transformers/Loader.js +350 -0
  103. package/Transformers/Progress.d.ts +32 -0
  104. package/Transformers/Progress.js +429 -0
  105. package/Transformers/Reference.d.ts +37 -0
  106. package/Transformers/Reference.js +212 -0
  107. package/Transformers/SchemaGenerator.d.ts +102 -0
  108. package/Transformers/SchemaGenerator.js +564 -0
  109. package/Transformers/Serialiser.d.ts +31 -0
  110. package/Transformers/Serialiser.js +366 -0
  111. package/Transformers/Utils.d.ts +33 -0
  112. package/Transformers/Utils.js +287 -0
  113. package/Transformers/__tests__/Examples.d.ts +168 -0
  114. package/Transformers/__tests__/Examples.js +263 -0
  115. package/Transformers/__tests__/Replicable.Transformers.Definitions.test.d.ts +1 -0
  116. package/Transformers/__tests__/Replicable.Transformers.Definitions.test.js +457 -0
  117. package/Transformers/__tests__/Replicable.Transformers.Loader.test.d.ts +1 -0
  118. package/Transformers/__tests__/Replicable.Transformers.Loader.test.js +339 -0
  119. package/Transformers/__tests__/Replicable.Transformers.Progress.test.d.ts +1 -0
  120. package/Transformers/__tests__/Replicable.Transformers.Progress.test.js +256 -0
  121. package/Transformers/__tests__/Replicable.Transformers.Reference.test.d.ts +1 -0
  122. package/Transformers/__tests__/Replicable.Transformers.Reference.test.js +167 -0
  123. package/Transformers/__tests__/Replicable.Transformers.SchemaGenerator.test.d.ts +1 -0
  124. package/Transformers/__tests__/Replicable.Transformers.SchemaGenerator.test.js +400 -0
  125. package/Transformers/__tests__/Replicable.Transformers.SchemaGeneratorOutput.test.d.ts +1 -0
  126. package/Transformers/__tests__/Replicable.Transformers.SchemaGeneratorOutput.test.js +441 -0
  127. package/Transformers/__tests__/Replicable.Transformers.Serialiser.test.d.ts +1 -0
  128. package/Transformers/__tests__/Replicable.Transformers.Serialiser.test.js +320 -0
  129. package/Transformers/__tests__/Replicable.Transformers.Utils.test.d.ts +1 -0
  130. package/Transformers/__tests__/Replicable.Transformers.Utils.test.js +534 -0
  131. package/__tests__/Replicable.Expressions.test.d.ts +1 -0
  132. package/__tests__/Replicable.Expressions.test.js +166 -0
  133. package/__tests__/Replicable.IDPool.test.d.ts +1 -0
  134. package/__tests__/Replicable.IDPool.test.js +11 -0
  135. package/__tests__/Replicable.ReplicableRegistry.test.d.ts +1 -0
  136. package/__tests__/Replicable.ReplicableRegistry.test.js +154 -0
  137. package/__tests__/Replicable.Serialisation.test.d.ts +1 -0
  138. package/__tests__/Replicable.Serialisation.test.js +283 -0
  139. package/package.json +14 -0
  140. package/tsconfig.json +19 -0
@@ -0,0 +1,220 @@
1
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
+ if (kind === "m") throw new TypeError("Private method is not writable");
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
+ };
7
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
+ };
12
+ var _ReplAPI_private;
13
+ import { ChainType } from "./ChainTypes.js";
14
+ import { ReplicableClass } from "../../Tracking/Class.js";
15
+ import { GetVersionController } from "../../Constants/Versions.js";
16
+ import { ChainFunction, ChainOverride, ChainProperty } from "./Accessors.js";
17
+ import { isPrimitiveType } from "../../Constants/SerialisationTypes.js";
18
+ import { VersionMap } from "@bikky/smart-collections";
19
+ import { ValidExpressionTypes } from "./Registry.js";
20
+ import { ArrayType, DictType, MapType } from "./ChainCollections.js";
21
+ var GetTypeChain = ValidExpressionTypes.GetTypeChain;
22
+ import { PrimitiveType } from "./Primitive.js";
23
+ import { Pointer } from "../../Transformers/Reference.js";
24
+ import { CustomType } from "./CustomAPI.js";
25
+ let IsLoadableDataForType;
26
+ let getReplicableClassByName = null;
27
+ let getReplicableClassOrPrimByAlias = null;
28
+ export var __ReplTypeHelperFunctions;
29
+ (function (__ReplTypeHelperFunctions) {
30
+ function SetIsLoadableDataForType(func) {
31
+ IsLoadableDataForType = func;
32
+ }
33
+ __ReplTypeHelperFunctions.SetIsLoadableDataForType = SetIsLoadableDataForType;
34
+ function SetDataNameTypeLookupFunction(lookupFunc) {
35
+ getReplicableClassByName = lookupFunc;
36
+ }
37
+ __ReplTypeHelperFunctions.SetDataNameTypeLookupFunction = SetDataNameTypeLookupFunction;
38
+ function SetPrimOrClassLookupFunction(lookupFunc) {
39
+ getReplicableClassOrPrimByAlias = lookupFunc;
40
+ }
41
+ __ReplTypeHelperFunctions.SetPrimOrClassLookupFunction = SetPrimOrClassLookupFunction;
42
+ })(__ReplTypeHelperFunctions || (__ReplTypeHelperFunctions = {}));
43
+ let replMap = new Map();
44
+ export class ReplType extends ChainType {
45
+ constructor(repl) {
46
+ super(repl.opts.dataTypeName);
47
+ this.properties = new VersionMap(GetVersionController());
48
+ this.dataProperties = new VersionMap(GetVersionController());
49
+ this.repl = repl;
50
+ ValidExpressionTypes.SetTypeChain(repl, this);
51
+ ValidExpressionTypes.AddAliasForType(repl.opts.typescriptTypeName, repl);
52
+ ValidExpressionTypes.AddAliasForType(repl.opts.dataTypeName, repl);
53
+ ValidExpressionTypes.AddAliasForType(repl.classConstructor, repl);
54
+ }
55
+ getOwnOutputStyle() {
56
+ return "Single";
57
+ }
58
+ getOwnOutputTypes() {
59
+ return [this.repl.classConstructor];
60
+ }
61
+ isAssignableCompile(version, type) {
62
+ return this.repl.isChildTypeOfThisType(type);
63
+ }
64
+ isAssignableLive(version, value) {
65
+ return this.repl.isObjectOfThisType(value);
66
+ }
67
+ isAssignableData(version, value, stack) {
68
+ return IsLoadableDataForType(this.repl, value, version, stack);
69
+ }
70
+ isCastableCompile(version, type) {
71
+ if (isPrimitiveType(type))
72
+ return false;
73
+ if (type instanceof PrimitiveType)
74
+ return false;
75
+ if (!getReplicableClassByName || !getReplicableClassOrPrimByAlias) {
76
+ throw new Error(`Error, ReplType is missing a lookup function for data types.`);
77
+ }
78
+ let repl;
79
+ if (type instanceof ReplType) {
80
+ repl = type.repl;
81
+ }
82
+ else if (type instanceof ReplicableClass) {
83
+ repl = type;
84
+ }
85
+ else if (typeof type === "string") {
86
+ repl = getReplicableClassByName(type);
87
+ }
88
+ else {
89
+ //Handle aliases.
90
+ let result = getReplicableClassOrPrimByAlias(type instanceof CustomType ? type.name : type);
91
+ if (!result) {
92
+ throw new Error(`Error, ${type} is not a valid type.`);
93
+ }
94
+ for (let val of result) {
95
+ if (val instanceof ReplicableClass && (val.isChildTypeOfThisType(this.repl) || this.repl.isChildTypeOfThisType(val))) {
96
+ return true;
97
+ }
98
+ }
99
+ return false;
100
+ }
101
+ if (!repl)
102
+ return false;
103
+ return repl.isChildTypeOfThisType(this.repl) || this.repl.isChildTypeOfThisType(repl);
104
+ }
105
+ isCastableLive(version, value) {
106
+ if (value instanceof Pointer) {
107
+ value = value.valOrNull;
108
+ }
109
+ return this.repl.isObjectOfThisType(value);
110
+ }
111
+ isCastableData(version, value, stack) {
112
+ return IsLoadableDataForType(this.repl, value, version, stack);
113
+ }
114
+ getLive(version, prop, node, contexts) {
115
+ let value = this.properties.get(version, prop);
116
+ if (value)
117
+ return value;
118
+ return this.checkContextScopes(version, prop, node, contexts);
119
+ }
120
+ getData(version, prop, node, contexts) {
121
+ let value = this.dataProperties.get(version, prop);
122
+ if (value)
123
+ return value;
124
+ return this.checkContextsData(version, prop, node, contexts);
125
+ }
126
+ }
127
+ export class ReplAPI {
128
+ constructor(type, opts) {
129
+ _ReplAPI_private.set(this, void 0);
130
+ this.replicable = opts;
131
+ __classPrivateFieldSet(this, _ReplAPI_private, new ReplType(opts), "f");
132
+ replMap.set(this, __classPrivateFieldGet(this, _ReplAPI_private, "f"));
133
+ }
134
+ async addProperty(version, def) {
135
+ var _a;
136
+ if ("doesntExist" in def) {
137
+ //pass
138
+ }
139
+ else if ("removed" in def) {
140
+ if (def.expressionMigration) {
141
+ __classPrivateFieldGet(this, _ReplAPI_private, "f").properties.set(version, def.dataName, new ChainOverride(def.property, "self", def.type, def.expressionMigration));
142
+ }
143
+ if (def.dataMigration) {
144
+ __classPrivateFieldGet(this, _ReplAPI_private, "f").dataProperties.set(version, def.dataName, new ChainOverride(def.property, "self", (_a = def.customDataType) !== null && _a !== void 0 ? _a : def.type, def.dataMigration));
145
+ }
146
+ }
147
+ else {
148
+ if (!def.expressionForProperty) {
149
+ return;
150
+ }
151
+ //Force this to wait so that any string types' classes have been registered.
152
+ this.addPropertyHelper(__classPrivateFieldGet(this, _ReplAPI_private, "f").properties, def, def.expressionForProperty[version], version);
153
+ this.addPropertyHelper(__classPrivateFieldGet(this, _ReplAPI_private, "f").dataProperties, def, def.expressionForProperty[version], version);
154
+ }
155
+ }
156
+ addPropertyHelper(target, def, liveInfo, version) {
157
+ let outputTypes = liveInfo.outputTypes.map(e => {
158
+ let result = e instanceof ChainType ? e : GetTypeChain(e);
159
+ if (!result) {
160
+ throw new Error(`Error, ${def.load.name} has an unknown type ${e}.`);
161
+ }
162
+ return result;
163
+ });
164
+ if (liveInfo.outputArray == 2) {
165
+ let array = ValidExpressionTypes.GetArrayType([ValidExpressionTypes.GetArrayType(outputTypes)]);
166
+ target.set(version, def.load.name, new ChainProperty(def.name, "self", [array]));
167
+ }
168
+ else if (liveInfo.outputArray == 1) {
169
+ let array = ValidExpressionTypes.GetTypeChain(ArrayType.generateName(outputTypes));
170
+ if (!array) {
171
+ array = ValidExpressionTypes.GetArrayType(outputTypes);
172
+ }
173
+ target.set(version, def.load.name, new ChainProperty(def.name, "self", [array]));
174
+ }
175
+ else if (liveInfo.outputDict) {
176
+ let dictionary = ValidExpressionTypes.GetTypeChain(DictType.generateName(outputTypes));
177
+ if (!dictionary) {
178
+ dictionary = ValidExpressionTypes.GetDictType(outputTypes);
179
+ }
180
+ target.set(version, def.load.name, new ChainProperty(def.name, "self", [dictionary]));
181
+ }
182
+ else if (liveInfo.outputMap) {
183
+ let map = ValidExpressionTypes.GetTypeChain(MapType.generateName(outputTypes));
184
+ if (!map) {
185
+ map = ValidExpressionTypes.GetMapType(outputTypes);
186
+ }
187
+ target.set(version, def.load.name, new ChainProperty(def.name, "self", [map]));
188
+ }
189
+ else {
190
+ target.set(version, def.load.name, new ChainProperty(def.name, "self", outputTypes));
191
+ }
192
+ }
193
+ addFunction(version, opts, types, parameters) {
194
+ for (let type of [...types, ...parameters].flat()) {
195
+ if ("array" in type) {
196
+ throw new Error(`Error, ${opts.name} has an array type in a function signature.`);
197
+ }
198
+ if ("mapKey" in type) {
199
+ throw new Error(`Error, ${opts.name} has a map type in a function signature.`);
200
+ }
201
+ if ("dictKey" in type) {
202
+ throw new Error(`Error, ${opts.name} has a dict type in a function signature.`);
203
+ }
204
+ }
205
+ let accessor = new ChainFunction(opts.name, "self", types.flatMap((e) => e.type),
206
+ //Convert [[{type: Type[]}], [{type: Type[]}]] -> [Type[], Type[]]
207
+ parameters.map((e) => {
208
+ return e.flatMap((f) => {
209
+ if (Array.isArray(f.type)) {
210
+ return f.type.map((g) => GetTypeChain(g));
211
+ }
212
+ else {
213
+ return [GetTypeChain(f.type)];
214
+ }
215
+ });
216
+ }));
217
+ __classPrivateFieldGet(this, _ReplAPI_private, "f").properties.set(version, opts.exprName, accessor);
218
+ }
219
+ }
220
+ _ReplAPI_private = new WeakMap();
@@ -0,0 +1,24 @@
1
+ import { ChainType } from "./ChainTypes.js";
2
+ import { ChainAccessor } from "./Accessors.js";
3
+ import { ErrorStack } from "../../Constants/Errors.js";
4
+ import { Versions } from "../../Constants/Versions.js";
5
+ import { ExpressionGrammar } from "../Compiler/ExpressionGrammar.js";
6
+ export declare class ScopeType extends ChainType {
7
+ liveScope: {
8
+ [variable: string]: ChainAccessor;
9
+ };
10
+ dataScope: {
11
+ [variable: string]: ChainAccessor;
12
+ };
13
+ constructor(debugName: string, liveScope: ScopeType["liveScope"], dataScope: ScopeType["dataScope"]);
14
+ getOwnOutputStyle(): ReturnType<ChainType["getOwnOutputStyle"]>;
15
+ getOwnOutputTypes(): string[];
16
+ isAssignableCompile(type: any): boolean;
17
+ isAssignableData(value: any, stack: ErrorStack): boolean;
18
+ isAssignableLive(value: any): boolean;
19
+ isCastableCompile(type: any): boolean;
20
+ isCastableData(value: any, stack: ErrorStack): boolean;
21
+ isCastableLive(value: any): boolean;
22
+ getData(version: Versions, prop: string, node: ExpressionGrammar.Node, parents?: ChainType[]): ChainAccessor;
23
+ getLive(version: Versions, prop: string, node: ExpressionGrammar.Node, parents?: ChainType[]): ChainAccessor;
24
+ }
@@ -0,0 +1,44 @@
1
+ import { ChainType } from "./ChainTypes.js";
2
+ export class ScopeType extends ChainType {
3
+ constructor(debugName, liveScope, dataScope) {
4
+ super(debugName);
5
+ this.liveScope = liveScope;
6
+ this.dataScope = dataScope;
7
+ }
8
+ getOwnOutputStyle() {
9
+ return "Map";
10
+ }
11
+ getOwnOutputTypes() {
12
+ return [this.debugName];
13
+ }
14
+ isAssignableCompile(type) {
15
+ return false;
16
+ }
17
+ isAssignableData(value, stack) {
18
+ return false;
19
+ }
20
+ isAssignableLive(value) {
21
+ return false;
22
+ }
23
+ isCastableCompile(type) {
24
+ return false;
25
+ }
26
+ isCastableData(value, stack) {
27
+ return false;
28
+ }
29
+ isCastableLive(value) {
30
+ return false;
31
+ }
32
+ getData(version, prop, node, parents) {
33
+ if (prop in this.dataScope) {
34
+ return this.dataScope[prop];
35
+ }
36
+ return this.checkContextsData(version, prop, node, parents);
37
+ }
38
+ getLive(version, prop, node, parents) {
39
+ if (prop in this.liveScope) {
40
+ return this.liveScope[prop];
41
+ }
42
+ return this.checkContextScopes(version, prop, node, parents);
43
+ }
44
+ }
@@ -0,0 +1,23 @@
1
+ import { ChainType } from "./ChainTypes.js";
2
+ import { Versions } from "../../Constants/Versions.js";
3
+ import { ChainAccessor } from "./Accessors.js";
4
+ export interface TraversalScope<localScopeType = any> {
5
+ data: boolean;
6
+ version: Versions.v1_0_0;
7
+ global: {
8
+ [category: string]: {
9
+ [key: string]: any;
10
+ } | string;
11
+ };
12
+ local: {
13
+ [keyword: string]: any;
14
+ };
15
+ self: localScopeType;
16
+ }
17
+ export interface ChainPath {
18
+ node: ChainType;
19
+ expression: ChainAccessor;
20
+ eval: (scope: TraversalScope) => any;
21
+ set?: (scope: TraversalScope, val: any) => void;
22
+ next?: ChainPath[];
23
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,31 @@
1
+ import { describe, expect, test } from "vitest";
2
+ import { ChainFunction } from "../Accessors.js";
3
+ import { Versions } from "../../../Constants/Versions.js";
4
+ import { ValidExpressionTypes } from "../Registry.js";
5
+ var GetTypeChain = ValidExpressionTypes.GetTypeChain;
6
+ //TODO: Test scopes.
7
+ //TODO: Test that expression's outputs are validated against the desired outputs.
8
+ //TODO: Test that trying to set the local scope for an entity checks for a valid parent scope.
9
+ //TODO: Test that getting the scope for an entity gets a valid parent scope.
10
+ //TODO: Test that versioning for repl properties and functions is honoured correctly ("from now on").
11
+ //TODO: Write a series of tests for customAPI, including for registering rules before their types have loaded.
12
+ //TODO: Test casting an alias down to one of it's types.
13
+ describe("Accessors", async () => {
14
+ const scope = {
15
+ version: Versions.v1_0_0,
16
+ data: false,
17
+ local: {},
18
+ global: {},
19
+ self: {
20
+ testString: "world",
21
+ testFunction: (a) => "hello " + a
22
+ }
23
+ };
24
+ test("ChainFunction", async () => {
25
+ const StringChainType = GetTypeChain(String);
26
+ const NullChainType = GetTypeChain(null);
27
+ let chain = new ChainFunction("testFunction", "self", [StringChainType], [[StringChainType, NullChainType]]);
28
+ let result = chain.evaluate(scope, ["world"]);
29
+ expect(result).toBe("hello world");
30
+ });
31
+ });
@@ -0,0 +1,28 @@
1
+ import { ChainType } from "../TypeRegistry/ChainTypes.js";
2
+ import { ChainAccessor } from "../TypeRegistry/Accessors.js";
3
+ import { ErrorStack } from "../../Constants/Errors.js";
4
+ import { Versions } from "../../Constants/Versions.js";
5
+ import { ExpressionGrammar } from "../Compiler/ExpressionGrammar.js";
6
+ export declare class ExpressionTestType extends ChainType {
7
+ access: {
8
+ [key: string]: ChainAccessor;
9
+ };
10
+ value: any;
11
+ constructor(name: string, compareValue: any, properties: {
12
+ [key: string]: {
13
+ type: any[];
14
+ value: any;
15
+ };
16
+ });
17
+ getOwnOutputTypes(): any[];
18
+ getOwnOutputStyle(): ReturnType<ChainType["getOwnOutputStyle"]>;
19
+ addProperty(prop: string, chain: ChainAccessor): void;
20
+ isAssignableCompile(version: Versions, type: any): boolean;
21
+ isAssignableData(version: Versions, value: any, stack: ErrorStack): boolean;
22
+ isAssignableLive(version: Versions, value: any): boolean;
23
+ isCastableCompile(version: Versions, type: any): boolean;
24
+ isCastableData(version: Versions, value: any, stack: ErrorStack): boolean;
25
+ isCastableLive(version: Versions, value: any): boolean;
26
+ getData(version: Versions, prop: string, node: ExpressionGrammar.Node, parents?: ChainType[]): ChainAccessor;
27
+ getLive(version: Versions, prop: string, node: ExpressionGrammar.Node, parents?: ChainType[]): ChainAccessor;
28
+ }
@@ -0,0 +1,50 @@
1
+ import { ChainType } from "../TypeRegistry/ChainTypes.js";
2
+ import { ChainOverride } from "../TypeRegistry/Accessors.js";
3
+ import { ExpressionSyntaxError } from "../../Constants/Errors.js";
4
+ import { ValidExpressionTypes } from "../TypeRegistry/Registry.js";
5
+ export class ExpressionTestType extends ChainType {
6
+ constructor(name, compareValue, properties) {
7
+ super(name);
8
+ this.access = {};
9
+ this.value = compareValue;
10
+ for (let prop in properties) {
11
+ let value = properties[prop].value;
12
+ this.access[prop] = new ChainOverride(prop, "self", properties[prop].type, () => value);
13
+ }
14
+ }
15
+ getOwnOutputTypes() {
16
+ return ValidExpressionTypes.ToRawTypes([this.debugName]);
17
+ }
18
+ getOwnOutputStyle() {
19
+ return "Single";
20
+ }
21
+ addProperty(prop, chain) {
22
+ this.access[prop] = chain;
23
+ }
24
+ isAssignableCompile(version, type) { debugger; return type === "Custom2"; }
25
+ isAssignableData(version, value, stack) { return value === this.value; }
26
+ isAssignableLive(version, value) { return value === this.value; }
27
+ isCastableCompile(version, type) { return false; }
28
+ isCastableData(version, value, stack) { return false; }
29
+ isCastableLive(version, value) { return false; }
30
+ getData(version, prop, node, parents) {
31
+ if (prop in this.access) {
32
+ return this.access[prop];
33
+ }
34
+ if (parents && parents.length > 0) {
35
+ let [parent, ...rest] = parents;
36
+ return parent.getLive(version, prop, node, rest);
37
+ }
38
+ throw new ExpressionSyntaxError(`CustomType doesn't have property ${prop}`, node);
39
+ }
40
+ getLive(version, prop, node, parents) {
41
+ if (prop in this.access) {
42
+ return this.access[prop];
43
+ }
44
+ if (parents && parents.length > 0) {
45
+ let [parent, ...rest] = parents;
46
+ return parent.getLive(version, prop, node, rest);
47
+ }
48
+ throw new ExpressionSyntaxError(`CustomType doesn't have property ${prop}`, node);
49
+ }
50
+ }
@@ -0,0 +1 @@
1
+ import "../Compiler/BuiltinGrammar.js";
@@ -0,0 +1,166 @@
1
+ import { beforeAll, beforeEach, describe, expect, test } from "vitest";
2
+ import { AddVersion, GetVersionIndex, Versions } from "../../Constants/Versions.js";
3
+ import "../Compiler/BuiltinGrammar.js";
4
+ import { CreateExpression } from "../CreateEvaluator.js";
5
+ import { EvaluationChain } from "../EvaluatorChain.js";
6
+ import { ExpressionScopes, ValidExpressionTypes } from "../TypeRegistry/Registry.js";
7
+ import { PrimitiveType } from "../TypeRegistry/Primitive.js";
8
+ import { ChainFunction, ChainProperty } from "../TypeRegistry/Accessors.js";
9
+ import { ExpressionTestType } from "./ExpressionExamples.js";
10
+ import { ScopeType } from "../TypeRegistry/Scope.js";
11
+ var GetTypeChain = ValidExpressionTypes.GetTypeChain;
12
+ let version = "version";
13
+ const config = {
14
+ startType: "Custom",
15
+ cacheable: false,
16
+ version: Versions.v1_0_0,
17
+ outputArray: 0,
18
+ outputMap: false,
19
+ outputDict: false,
20
+ outputTypes: [String],
21
+ duringLoad: false,
22
+ };
23
+ const basicScope = {
24
+ version: Versions.v1_0_0,
25
+ data: false,
26
+ global: {},
27
+ local: {},
28
+ self: {},
29
+ };
30
+ beforeEach(() => {
31
+ ExpressionScopes.ResetScopes();
32
+ });
33
+ beforeAll(() => {
34
+ AddVersion(version, Versions.v1_0_0);
35
+ });
36
+ //TODO: Test that expression exits properly when currentStep.next is an empty array (from casting?)
37
+ //TODO: Test that expression can return child types and that's OK.
38
+ //TODO: Test that expression can access property on parent type (think this was already done?)
39
+ //TODO: Test IsLoadableDataForRepl with a primitive type that is valid DataIsType.
40
+ //TODO: Test that setting local scope crawls up the heirarchy correctly (i.e. if a local scope is set on a child type,
41
+ // it should use the typing from the parent type)
42
+ //TODO: Creating an expression with a current set child type will crawl up the heirarchy correctly (same as above but
43
+ // for creating expressions)
44
+ test("AddVersion", () => {
45
+ let idx1 = GetVersionIndex(Versions.v1_0_0);
46
+ let idx2 = GetVersionIndex(version);
47
+ expect(idx1).not.toBeUndefined();
48
+ expect(idx1).not.toBe(idx2);
49
+ expect(idx1 + 1).toBe(idx2);
50
+ });
51
+ describe("Simple Global Expression", () => {
52
+ let expr;
53
+ beforeEach(() => {
54
+ ExpressionScopes.addLocalVariable("Custom", "value", [String]);
55
+ ExpressionScopes.setLocalScope("Custom", { value: "true" });
56
+ });
57
+ test("Create Simple Expression", () => {
58
+ let newExpression = CreateExpression("@{value}", "x", config);
59
+ expect(newExpression).not.toBeUndefined();
60
+ expect(newExpression).instanceOf(EvaluationChain);
61
+ if (!(newExpression instanceof EvaluationChain))
62
+ throw new Error(`Nyah!`);
63
+ expr = newExpression;
64
+ });
65
+ test("Simple expression has correct outputs", () => {
66
+ expect(expr.outputTypes).toHaveLength(1);
67
+ expect(expr.outputTypes[0]).toBe(String);
68
+ expect(expr.outputChains).toHaveLength(1);
69
+ expect(expr.outputChains[0]).instanceof(PrimitiveType);
70
+ let primChain = expr.outputChains[0];
71
+ expect(primChain.debugName).toBe("String");
72
+ expect(primChain.type).toBe(String);
73
+ });
74
+ test("Simple expression constructed correctly", () => {
75
+ expect(expr.steps).not.toBeUndefined();
76
+ expect(expr.steps.node).instanceof(ScopeType);
77
+ expect(expr.steps.expression).instanceof(ChainProperty);
78
+ expect(expr.steps.next).toBeUndefined();
79
+ });
80
+ test("Run Simple Expression", () => {
81
+ expect(expr).not.toBeUndefined();
82
+ expect(expr).instanceOf(EvaluationChain);
83
+ if (!(expr instanceof EvaluationChain))
84
+ throw new Error(`Nyah!`);
85
+ let value = expr.get(Object.assign(Object.assign({}, basicScope), { local: { value: "true" } }));
86
+ expect(value).toBe("true");
87
+ });
88
+ test("Expression returns incorrect result", () => {
89
+ expect(() => {
90
+ let value = expr.get(Object.assign(Object.assign({}, basicScope), { local: { value: false } }));
91
+ expect(value).toBe("true");
92
+ }).toThrowError();
93
+ });
94
+ });
95
+ describe("Lookup Value on Property", () => {
96
+ const StringType = GetTypeChain(String);
97
+ const CustomType = new ExpressionTestType("Custom", "value lookup", { second: { type: [String], value: "second lookup" } });
98
+ CustomType.addProperty("func", new ChainFunction("func", "self", [StringType], [[StringType, StringType]]));
99
+ test("Run multipart expression", () => {
100
+ ExpressionScopes.addLocalVariable("Custom", "value", ["Custom2"]);
101
+ ExpressionScopes.setLocalScope("Custom", { value: "value lookup" });
102
+ ValidExpressionTypes.SetTypeChain("Custom2", CustomType);
103
+ let exp = CreateExpression("@{value.second}", "x", config);
104
+ expect(exp).not.toBeUndefined();
105
+ expect(exp).instanceOf(EvaluationChain);
106
+ if (!(exp instanceof EvaluationChain))
107
+ throw new Error(`Nyah!`);
108
+ let value = exp.get({
109
+ version: Versions.v1_0_0,
110
+ data: false,
111
+ global: {},
112
+ local: { value: "value lookup" },
113
+ self: {},
114
+ });
115
+ expect(value).toBe("second lookup");
116
+ });
117
+ test("Expression with multiple output types", () => {
118
+ ExpressionScopes.addLocalVariable("Custom", "value", [String, null]);
119
+ ExpressionScopes.setLocalScope("Custom", { value: "true" });
120
+ let exp = CreateExpression("@{value}", "x", config);
121
+ expect(exp).not.toBeUndefined();
122
+ expect(exp).instanceOf(EvaluationChain);
123
+ if (!(exp instanceof EvaluationChain))
124
+ throw new Error(`Nyah!`);
125
+ let value = exp.get({
126
+ version: Versions.v1_0_0,
127
+ data: false,
128
+ global: {},
129
+ local: { value: "true" },
130
+ self: {},
131
+ });
132
+ expect(value).toBe("true");
133
+ });
134
+ test("Test multipart expression throws nice error", () => {
135
+ ExpressionScopes.addLocalVariable("CustomA", "thisValue", [String]);
136
+ ExpressionScopes.setLocalScope("CustomA", { thisValue: "true" });
137
+ expect(() => {
138
+ CreateExpression("@{thisValue.other}", "x", Object.assign(Object.assign({}, config), { startType: "CustomA" }));
139
+ }).toThrowError(`Error: Key "other" doesn't exist on type "String". Did you mean to write "Other"?
140
+ ---> thisValue.other
141
+ ^^
142
+ Found in file x on line 1 at 10.
143
+ `.replace(/\n /g, "\n"));
144
+ });
145
+ test("Call a function on a custom object", () => {
146
+ ExpressionScopes.addGlobalVariable("value", [String], "true");
147
+ ExpressionScopes.addLocalVariable("Custom", "func", [String], "Function", [[String, String]]);
148
+ let exp = CreateExpression("@{func(value, value)}", "x", config);
149
+ expect(exp).not.toBeUndefined();
150
+ expect(exp).instanceOf(EvaluationChain);
151
+ if (!(exp instanceof EvaluationChain))
152
+ throw new Error(`Nyah!`);
153
+ let value = exp.get({
154
+ version: Versions.v1_0_0,
155
+ data: false,
156
+ global: {
157
+ value: "true"
158
+ },
159
+ local: {
160
+ func: (first, second) => first + " " + second
161
+ },
162
+ self: {},
163
+ });
164
+ expect(value).toBe("true true");
165
+ });
166
+ });
package/IDPool.d.ts ADDED
@@ -0,0 +1,18 @@
1
+ export declare namespace IDPool {
2
+ function initialise(makeUUIDFunction: () => UUID): Promise<void>;
3
+ class UUID {
4
+ private secret;
5
+ toString(): string;
6
+ }
7
+ namespace UUID {
8
+ const name = "UUID";
9
+ function isUUID(input: any, complex?: boolean): input is UUID;
10
+ }
11
+ function useUUID(): UUID;
12
+ /**
13
+ * Verifies the client UUID and removes it from the pool of client UUIDs.
14
+ * @param id
15
+ * @param remoteID
16
+ */
17
+ function verifyClientUUID(id: UUID, remoteID: UUID): boolean;
18
+ }
@@ -0,0 +1,17 @@
1
+
2
+
3
+
4
+ // This file is automatically generated. Do not edit it, all edits will be overwritten on compile.// This file contains the typescript types for the data definitions for the corresponding
5
+ // types in the .ts file this file is named after.
6
+
7
+
8
+ export type UUIDDefinition = IDPool.UUID;
9
+
10
+
11
+
12
+ export type UUIDSerialised = IDPool.UUID;
13
+
14
+ export type UUIDSerialisedReference = [ string, UUIDSerialised ];
15
+
16
+
17
+