@stryke/trpc-next 0.5.48 → 0.5.49
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/CHANGELOG.md +8 -0
- package/dist/_virtual/rolldown_runtime.cjs +29 -1
- package/dist/action-handler.cjs +20 -1
- package/dist/action-handler.mjs +18 -1
- package/dist/action-handler.mjs.map +1 -1
- package/dist/client.cjs +39 -1
- package/dist/client.mjs +37 -1
- package/dist/client.mjs.map +1 -1
- package/dist/env/src/ci-checks.cjs +13 -1
- package/dist/env/src/ci-checks.mjs +12 -1
- package/dist/env/src/ci-checks.mjs.map +1 -1
- package/dist/env/src/environment-checks.cjs +87 -1
- package/dist/env/src/environment-checks.mjs +87 -1
- package/dist/env/src/environment-checks.mjs.map +1 -1
- package/dist/index.cjs +30 -1
- package/dist/index.mjs +8 -1
- package/dist/path/src/is-type.cjs +28 -1
- package/dist/path/src/is-type.mjs +28 -1
- package/dist/path/src/is-type.mjs.map +1 -1
- package/dist/path/src/join-paths.cjs +106 -1
- package/dist/path/src/join-paths.mjs +106 -1
- package/dist/path/src/join-paths.mjs.map +1 -1
- package/dist/path/src/regex.cjs +12 -1
- package/dist/path/src/regex.mjs +8 -1
- package/dist/path/src/regex.mjs.map +1 -1
- package/dist/path/src/slash.cjs +15 -1
- package/dist/path/src/slash.mjs +14 -1
- package/dist/path/src/slash.mjs.map +1 -1
- package/dist/server.cjs +46 -1
- package/dist/server.mjs +33 -1
- package/dist/server.mjs.map +1 -1
- package/dist/shared.cjs +43 -1
- package/dist/shared.mjs +38 -1
- package/dist/shared.mjs.map +1 -1
- package/dist/shield/constructors.cjs +86 -1
- package/dist/shield/constructors.mjs +79 -1
- package/dist/shield/constructors.mjs.map +1 -1
- package/dist/shield/generator.cjs +28 -1
- package/dist/shield/generator.mjs +27 -1
- package/dist/shield/generator.mjs.map +1 -1
- package/dist/shield/index.cjs +12 -1
- package/dist/shield/index.mjs +4 -1
- package/dist/shield/rules.cjs +200 -1
- package/dist/shield/rules.mjs +191 -1
- package/dist/shield/rules.mjs.map +1 -1
- package/dist/shield/shield.cjs +31 -1
- package/dist/shield/shield.mjs +31 -1
- package/dist/shield/shield.mjs.map +1 -1
- package/dist/shield/utils.cjs +56 -1
- package/dist/shield/utils.mjs +51 -1
- package/dist/shield/utils.mjs.map +1 -1
- package/dist/shield/validation.cjs +59 -1
- package/dist/shield/validation.mjs +58 -1
- package/dist/shield/validation.mjs.map +1 -1
- package/dist/tanstack-query/client.cjs +42 -1
- package/dist/tanstack-query/client.mjs +41 -1
- package/dist/tanstack-query/client.mjs.map +1 -1
- package/dist/tanstack-query/server.cjs +54 -1
- package/dist/tanstack-query/server.mjs +51 -1
- package/dist/tanstack-query/server.mjs.map +1 -1
- package/package.json +2 -2
package/dist/shield/rules.mjs
CHANGED
|
@@ -1,2 +1,192 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/shield/rules.ts
|
|
2
|
+
var Rule = class {
|
|
3
|
+
name;
|
|
4
|
+
func;
|
|
5
|
+
constructor(name, func) {
|
|
6
|
+
this.name = name;
|
|
7
|
+
this.func = func;
|
|
8
|
+
}
|
|
9
|
+
async resolve(ctx, type, path, input, rawInput, options) {
|
|
10
|
+
try {
|
|
11
|
+
const res = await this.executeRule(ctx, type, path, input, rawInput, options);
|
|
12
|
+
if (res instanceof Error) return res;
|
|
13
|
+
else if (typeof res === "string") return new Error(res);
|
|
14
|
+
else if (res === true) return true;
|
|
15
|
+
else return false;
|
|
16
|
+
} catch (err) {
|
|
17
|
+
if (options.debug) throw err;
|
|
18
|
+
else return false;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
*
|
|
23
|
+
* Compares a given rule with the current one
|
|
24
|
+
* and checks whether their functions are equal.
|
|
25
|
+
*
|
|
26
|
+
*/
|
|
27
|
+
equals(rule) {
|
|
28
|
+
return this.func === rule.func;
|
|
29
|
+
}
|
|
30
|
+
executeRule(ctx, type, path, input, rawInput, options) {
|
|
31
|
+
return this.func(ctx, type, path, input, rawInput, options);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
var LogicRule = class extends Rule {
|
|
35
|
+
rules;
|
|
36
|
+
constructor(rules) {
|
|
37
|
+
super("LogicRule");
|
|
38
|
+
this.rules = rules;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* By default logic rule resolves to false.
|
|
42
|
+
*/
|
|
43
|
+
async resolve(_ctx, _type, _path, _input, _rawInput, _options) {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Evaluates all the rules.
|
|
48
|
+
*/
|
|
49
|
+
async evaluate(ctx, type, path, input, rawInput, options) {
|
|
50
|
+
const tasks = this.getRules().map(async (rule) => rule.resolve(ctx, type, path, input, rawInput, options));
|
|
51
|
+
return Promise.all(tasks);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Returns rules in a logic rule.
|
|
55
|
+
*/
|
|
56
|
+
getRules() {
|
|
57
|
+
return this.rules;
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
var RuleOr = class extends LogicRule {
|
|
61
|
+
constructor(rules) {
|
|
62
|
+
super(rules);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Makes sure that at least one of them has evaluated to true.
|
|
66
|
+
*/
|
|
67
|
+
async resolve(ctx, type, path, input, rawInput, options) {
|
|
68
|
+
const result = await this.evaluate(ctx, type, path, input, rawInput, options);
|
|
69
|
+
if (result.every((res) => res !== true)) return result.find((res) => res instanceof Error) ?? false;
|
|
70
|
+
else return true;
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
var RuleAnd = class extends LogicRule {
|
|
74
|
+
constructor(rules) {
|
|
75
|
+
super(rules);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Makes sure that all of them have resolved to true.
|
|
79
|
+
*/
|
|
80
|
+
async resolve(ctx, type, path, input, rawInput, options) {
|
|
81
|
+
const result = await this.evaluate(ctx, type, path, input, rawInput, options);
|
|
82
|
+
if (result.some((res) => res !== true)) return result.find((res) => res instanceof Error) ?? false;
|
|
83
|
+
else return true;
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
var RuleChain = class extends LogicRule {
|
|
87
|
+
constructor(rules) {
|
|
88
|
+
super(rules);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Makes sure that all of them have resolved to true.
|
|
92
|
+
*/
|
|
93
|
+
async resolve(ctx, type, path, input, rawInput, options) {
|
|
94
|
+
const result = await this.evaluate(ctx, type, path, input, rawInput, options);
|
|
95
|
+
if (result.some((res) => res !== true)) return result.find((res) => res instanceof Error) ?? false;
|
|
96
|
+
else return true;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Evaluates all the rules.
|
|
100
|
+
*/
|
|
101
|
+
async evaluate(ctx, type, path, input, rawInput, options) {
|
|
102
|
+
return iterate(this.getRules());
|
|
103
|
+
async function iterate([rule, ...otherRules]) {
|
|
104
|
+
if (rule === void 0) return [];
|
|
105
|
+
return rule.resolve(ctx, type, path, input, rawInput, options).then(async (res) => {
|
|
106
|
+
if (res !== true) return [res];
|
|
107
|
+
else return iterate(otherRules).then((ress) => ress.concat(res));
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
var RuleRace = class extends LogicRule {
|
|
113
|
+
constructor(rules) {
|
|
114
|
+
super(rules);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Makes sure that at least one of them resolved to true.
|
|
118
|
+
*/
|
|
119
|
+
async resolve(ctx, type, path, input, rawInput, options) {
|
|
120
|
+
const result = await this.evaluate(ctx, type, path, input, rawInput, options);
|
|
121
|
+
if (result.includes(true)) return true;
|
|
122
|
+
else return result.find((res) => res instanceof Error) ?? false;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Evaluates all the rules.
|
|
126
|
+
*/
|
|
127
|
+
async evaluate(ctx, type, path, input, rawInput, options) {
|
|
128
|
+
return iterate(this.getRules());
|
|
129
|
+
async function iterate([rule, ...otherRules]) {
|
|
130
|
+
if (rule === void 0) return [];
|
|
131
|
+
return rule.resolve(ctx, type, path, input, rawInput, options).then(async (res) => {
|
|
132
|
+
if (res === true) return [res];
|
|
133
|
+
else return iterate(otherRules).then((ress) => ress.concat(res));
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
var RuleNot = class extends LogicRule {
|
|
139
|
+
error;
|
|
140
|
+
name = "RuleNot";
|
|
141
|
+
equals;
|
|
142
|
+
constructor(rule, error) {
|
|
143
|
+
super([rule]);
|
|
144
|
+
this.error = error;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Negates the result.
|
|
148
|
+
*/
|
|
149
|
+
async resolve(ctx, type, path, input, rawInput, options) {
|
|
150
|
+
const [res] = await this.evaluate(ctx, type, path, input, rawInput, options);
|
|
151
|
+
if (res instanceof Error) return true;
|
|
152
|
+
else if (res !== true) return true;
|
|
153
|
+
else {
|
|
154
|
+
if (this.error) return this.error;
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
var RuleTrue = class extends LogicRule {
|
|
160
|
+
name = "RuleTrue";
|
|
161
|
+
equals;
|
|
162
|
+
constructor() {
|
|
163
|
+
super([]);
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
*
|
|
167
|
+
* Always true.
|
|
168
|
+
*
|
|
169
|
+
*/
|
|
170
|
+
async resolve() {
|
|
171
|
+
return true;
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
var RuleFalse = class extends LogicRule {
|
|
175
|
+
name = "RuleTrue";
|
|
176
|
+
equals;
|
|
177
|
+
constructor() {
|
|
178
|
+
super([]);
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
*
|
|
182
|
+
* Always false.
|
|
183
|
+
*
|
|
184
|
+
*/
|
|
185
|
+
async resolve() {
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
//#endregion
|
|
191
|
+
export { LogicRule, Rule, RuleAnd, RuleChain, RuleFalse, RuleNot, RuleOr, RuleRace, RuleTrue };
|
|
2
192
|
//# sourceMappingURL=rules.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rules.mjs","names":[],"sources":["../../src/shield/rules.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport type {\n LogicRuleInterface,\n OptionsInterface,\n RuleFunctionInterface,\n RuleInterface,\n RuleResultInterface,\n ShieldRule\n} from \"./types\";\n\nexport class Rule<\n TContext extends Record<string, any>\n> implements RuleInterface<TContext> {\n readonly name: string;\n\n func?: RuleFunctionInterface<TContext>;\n\n constructor(name: string, func?: RuleFunctionInterface<TContext>) {\n this.name = name;\n this.func = func;\n }\n\n async resolve(\n ctx: TContext,\n type: string,\n path: string,\n input: { [name: string]: any },\n rawInput: unknown,\n options: OptionsInterface<TContext>\n ): Promise<RuleResultInterface> {\n try {\n /* Resolve */\n const res = await this.executeRule(\n ctx,\n type,\n path,\n input,\n rawInput,\n options\n );\n\n if (res instanceof Error) {\n return res;\n } else if (typeof res === \"string\") {\n return new Error(res);\n } else if (res === true) {\n return true;\n } else {\n return false;\n }\n } catch (err) {\n if (options.debug) {\n throw err;\n } else {\n return false;\n }\n }\n }\n\n /**\n *\n * Compares a given rule with the current one\n * and checks whether their functions are equal.\n *\n */\n equals(rule: Rule<TContext>): boolean {\n return this.func === rule.func;\n }\n\n executeRule<TContext extends Record<string, any>>(\n ctx: TContext,\n type: string,\n path: string,\n input: { [name: string]: any },\n rawInput: unknown,\n options: OptionsInterface<TContext>\n ): string | boolean | Error | Promise<RuleResultInterface> {\n // @ts-ignore\n return this.func(ctx, type, path, input, rawInput, options);\n }\n}\n\nexport class LogicRule<TContext extends Record<string, any>>\n extends Rule<TContext>\n implements LogicRuleInterface<TContext>\n{\n private rules: ShieldRule<TContext>[];\n\n constructor(rules: ShieldRule<TContext>[]) {\n super(\"LogicRule\");\n\n this.rules = rules;\n }\n\n /**\n * By default logic rule resolves to false.\n */\n override async resolve(\n _ctx: TContext,\n _type: string,\n _path: string,\n _input: { [name: string]: any },\n _rawInput: unknown,\n _options: OptionsInterface<TContext>\n ): Promise<RuleResultInterface> {\n return false;\n }\n\n /**\n * Evaluates all the rules.\n */\n async evaluate(\n ctx: TContext,\n type: string,\n path: string,\n input: { [name: string]: any },\n rawInput: unknown,\n options: OptionsInterface<TContext>\n ): Promise<RuleResultInterface[]> {\n const rules = this.getRules();\n const tasks = rules.map(async rule =>\n rule.resolve(ctx, type, path, input, rawInput, options)\n );\n\n return Promise.all(tasks);\n }\n\n /**\n * Returns rules in a logic rule.\n */\n getRules() {\n return this.rules;\n }\n}\n\n// Extended Types\n\nexport class RuleOr<\n TContext extends Record<string, any>\n> extends LogicRule<TContext> {\n constructor(rules: ShieldRule<TContext>[]) {\n super(rules);\n }\n\n /**\n * Makes sure that at least one of them has evaluated to true.\n */\n override async resolve(\n ctx: TContext,\n type: string,\n path: string,\n input: { [name: string]: any },\n rawInput: unknown,\n options: OptionsInterface<TContext>\n ): Promise<RuleResultInterface> {\n const result = await this.evaluate(\n ctx,\n type,\n path,\n input,\n rawInput,\n options\n );\n\n if (result.every(res => res !== true)) {\n const customError = result.find(res => res instanceof Error);\n\n return customError ?? false;\n } else {\n return true;\n }\n }\n}\n\nexport class RuleAnd<\n TContext extends Record<string, any>\n> extends LogicRule<TContext> {\n constructor(rules: ShieldRule<TContext>[]) {\n super(rules);\n }\n\n /**\n * Makes sure that all of them have resolved to true.\n */\n override async resolve(\n ctx: TContext,\n type: string,\n path: string,\n input: { [name: string]: any },\n rawInput: unknown,\n options: OptionsInterface<TContext>\n ): Promise<RuleResultInterface> {\n const result = await this.evaluate(\n ctx,\n type,\n path,\n input,\n rawInput,\n options\n );\n\n if (result.some(res => res !== true)) {\n const customError = result.find(res => res instanceof Error);\n\n return customError ?? false;\n } else {\n return true;\n }\n }\n}\n\nexport class RuleChain<\n TContext extends Record<string, any>\n> extends LogicRule<TContext> {\n constructor(rules: ShieldRule<TContext>[]) {\n super(rules);\n }\n\n /**\n * Makes sure that all of them have resolved to true.\n */\n override async resolve(\n ctx: TContext,\n type: string,\n path: string,\n input: { [name: string]: any },\n rawInput: unknown,\n options: OptionsInterface<TContext>\n ): Promise<RuleResultInterface> {\n const result = await this.evaluate(\n ctx,\n type,\n path,\n input,\n rawInput,\n options\n );\n\n if (result.some(res => res !== true)) {\n const customError = result.find(res => res instanceof Error);\n\n return customError ?? false;\n } else {\n return true;\n }\n }\n\n /**\n * Evaluates all the rules.\n */\n override async evaluate(\n ctx: TContext,\n type: string,\n path: string,\n input: { [name: string]: any },\n rawInput: unknown,\n options: OptionsInterface<TContext>\n ): Promise<RuleResultInterface[]> {\n const rules = this.getRules();\n\n return iterate(rules);\n\n async function iterate([\n rule,\n ...otherRules\n ]: ShieldRule<TContext>[]): Promise<RuleResultInterface[]> {\n if (rule === undefined) return [];\n return rule\n .resolve(ctx, type, path, input, rawInput, options)\n .then(async res => {\n if (res !== true) {\n return [res];\n } else {\n return iterate(otherRules).then(ress => ress.concat(res));\n }\n });\n }\n }\n}\n\nexport class RuleRace<\n TContext extends Record<string, any>\n> extends LogicRule<TContext> {\n constructor(rules: ShieldRule<TContext>[]) {\n super(rules);\n }\n\n /**\n * Makes sure that at least one of them resolved to true.\n */\n override async resolve(\n ctx: TContext,\n type: string,\n path: string,\n input: { [name: string]: any },\n rawInput: unknown,\n options: OptionsInterface<TContext>\n ): Promise<RuleResultInterface> {\n const result = await this.evaluate(\n ctx,\n type,\n path,\n input,\n rawInput,\n options\n );\n\n if (result.includes(true)) {\n return true;\n } else {\n const customError = result.find(res => res instanceof Error);\n\n return customError ?? false;\n }\n }\n\n /**\n * Evaluates all the rules.\n */\n override async evaluate(\n ctx: TContext,\n type: string,\n path: string,\n input: { [name: string]: any },\n rawInput: unknown,\n options: OptionsInterface<TContext>\n ): Promise<RuleResultInterface[]> {\n const rules = this.getRules();\n\n return iterate(rules);\n\n async function iterate([\n rule,\n ...otherRules\n ]: ShieldRule<TContext>[]): Promise<RuleResultInterface[]> {\n if (rule === undefined) return [];\n return rule\n .resolve(ctx, type, path, input, rawInput, options)\n .then(async res => {\n if (res === true) {\n return [res];\n } else {\n return iterate(otherRules).then(ress => ress.concat(res));\n }\n });\n }\n }\n}\n\nexport class RuleNot<TContext extends Record<string, any>>\n extends LogicRule<TContext>\n implements LogicRuleInterface<TContext>\n{\n error?: Error;\n\n override name: string = \"RuleNot\";\n\n override equals!: (rule: RuleInterface<TContext>) => boolean;\n\n constructor(rule: ShieldRule<TContext>, error?: Error) {\n super([rule]);\n this.error = error;\n }\n\n /**\n * Negates the result.\n */\n override async resolve(\n ctx: TContext,\n type: string,\n path: string,\n input: { [name: string]: any },\n rawInput: unknown,\n options: OptionsInterface<TContext>\n ): Promise<RuleResultInterface> {\n const [res] = await this.evaluate(\n ctx,\n type,\n path,\n input,\n rawInput,\n options\n );\n\n if (res instanceof Error) {\n return true;\n } else if (res !== true) {\n return true;\n } else {\n if (this.error) return this.error;\n return false;\n }\n }\n}\n\nexport class RuleTrue<TContext extends Record<string, any>>\n extends LogicRule<TContext>\n implements LogicRuleInterface<TContext>\n{\n override name: string = \"RuleTrue\";\n\n override equals!: (rule: RuleInterface<TContext>) => boolean;\n\n constructor() {\n super([]);\n }\n\n /**\n *\n * Always true.\n *\n */\n override async resolve(): Promise<RuleResultInterface> {\n return true;\n }\n}\n\nexport class RuleFalse<TContext extends Record<string, any>>\n extends LogicRule<TContext>\n implements LogicRuleInterface<TContext>\n{\n override name: string = \"RuleTrue\";\n\n override equals!: (rule: RuleInterface<TContext>) => boolean;\n\n constructor() {\n super([]);\n }\n\n /**\n *\n * Always false.\n *\n */\n override async resolve(): Promise<RuleResultInterface> {\n return false;\n }\n}\n"],"mappings":"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"}
|
|
1
|
+
{"version":3,"file":"rules.mjs","names":[],"sources":["../../src/shield/rules.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport type {\n LogicRuleInterface,\n OptionsInterface,\n RuleFunctionInterface,\n RuleInterface,\n RuleResultInterface,\n ShieldRule\n} from \"./types\";\n\nexport class Rule<\n TContext extends Record<string, any>\n> implements RuleInterface<TContext> {\n readonly name: string;\n\n func?: RuleFunctionInterface<TContext>;\n\n constructor(name: string, func?: RuleFunctionInterface<TContext>) {\n this.name = name;\n this.func = func;\n }\n\n async resolve(\n ctx: TContext,\n type: string,\n path: string,\n input: { [name: string]: any },\n rawInput: unknown,\n options: OptionsInterface<TContext>\n ): Promise<RuleResultInterface> {\n try {\n /* Resolve */\n const res = await this.executeRule(\n ctx,\n type,\n path,\n input,\n rawInput,\n options\n );\n\n if (res instanceof Error) {\n return res;\n } else if (typeof res === \"string\") {\n return new Error(res);\n } else if (res === true) {\n return true;\n } else {\n return false;\n }\n } catch (err) {\n if (options.debug) {\n throw err;\n } else {\n return false;\n }\n }\n }\n\n /**\n *\n * Compares a given rule with the current one\n * and checks whether their functions are equal.\n *\n */\n equals(rule: Rule<TContext>): boolean {\n return this.func === rule.func;\n }\n\n executeRule<TContext extends Record<string, any>>(\n ctx: TContext,\n type: string,\n path: string,\n input: { [name: string]: any },\n rawInput: unknown,\n options: OptionsInterface<TContext>\n ): string | boolean | Error | Promise<RuleResultInterface> {\n // @ts-ignore\n return this.func(ctx, type, path, input, rawInput, options);\n }\n}\n\nexport class LogicRule<TContext extends Record<string, any>>\n extends Rule<TContext>\n implements LogicRuleInterface<TContext>\n{\n private rules: ShieldRule<TContext>[];\n\n constructor(rules: ShieldRule<TContext>[]) {\n super(\"LogicRule\");\n\n this.rules = rules;\n }\n\n /**\n * By default logic rule resolves to false.\n */\n override async resolve(\n _ctx: TContext,\n _type: string,\n _path: string,\n _input: { [name: string]: any },\n _rawInput: unknown,\n _options: OptionsInterface<TContext>\n ): Promise<RuleResultInterface> {\n return false;\n }\n\n /**\n * Evaluates all the rules.\n */\n async evaluate(\n ctx: TContext,\n type: string,\n path: string,\n input: { [name: string]: any },\n rawInput: unknown,\n options: OptionsInterface<TContext>\n ): Promise<RuleResultInterface[]> {\n const rules = this.getRules();\n const tasks = rules.map(async rule =>\n rule.resolve(ctx, type, path, input, rawInput, options)\n );\n\n return Promise.all(tasks);\n }\n\n /**\n * Returns rules in a logic rule.\n */\n getRules() {\n return this.rules;\n }\n}\n\n// Extended Types\n\nexport class RuleOr<\n TContext extends Record<string, any>\n> extends LogicRule<TContext> {\n constructor(rules: ShieldRule<TContext>[]) {\n super(rules);\n }\n\n /**\n * Makes sure that at least one of them has evaluated to true.\n */\n override async resolve(\n ctx: TContext,\n type: string,\n path: string,\n input: { [name: string]: any },\n rawInput: unknown,\n options: OptionsInterface<TContext>\n ): Promise<RuleResultInterface> {\n const result = await this.evaluate(\n ctx,\n type,\n path,\n input,\n rawInput,\n options\n );\n\n if (result.every(res => res !== true)) {\n const customError = result.find(res => res instanceof Error);\n\n return customError ?? false;\n } else {\n return true;\n }\n }\n}\n\nexport class RuleAnd<\n TContext extends Record<string, any>\n> extends LogicRule<TContext> {\n constructor(rules: ShieldRule<TContext>[]) {\n super(rules);\n }\n\n /**\n * Makes sure that all of them have resolved to true.\n */\n override async resolve(\n ctx: TContext,\n type: string,\n path: string,\n input: { [name: string]: any },\n rawInput: unknown,\n options: OptionsInterface<TContext>\n ): Promise<RuleResultInterface> {\n const result = await this.evaluate(\n ctx,\n type,\n path,\n input,\n rawInput,\n options\n );\n\n if (result.some(res => res !== true)) {\n const customError = result.find(res => res instanceof Error);\n\n return customError ?? false;\n } else {\n return true;\n }\n }\n}\n\nexport class RuleChain<\n TContext extends Record<string, any>\n> extends LogicRule<TContext> {\n constructor(rules: ShieldRule<TContext>[]) {\n super(rules);\n }\n\n /**\n * Makes sure that all of them have resolved to true.\n */\n override async resolve(\n ctx: TContext,\n type: string,\n path: string,\n input: { [name: string]: any },\n rawInput: unknown,\n options: OptionsInterface<TContext>\n ): Promise<RuleResultInterface> {\n const result = await this.evaluate(\n ctx,\n type,\n path,\n input,\n rawInput,\n options\n );\n\n if (result.some(res => res !== true)) {\n const customError = result.find(res => res instanceof Error);\n\n return customError ?? false;\n } else {\n return true;\n }\n }\n\n /**\n * Evaluates all the rules.\n */\n override async evaluate(\n ctx: TContext,\n type: string,\n path: string,\n input: { [name: string]: any },\n rawInput: unknown,\n options: OptionsInterface<TContext>\n ): Promise<RuleResultInterface[]> {\n const rules = this.getRules();\n\n return iterate(rules);\n\n async function iterate([\n rule,\n ...otherRules\n ]: ShieldRule<TContext>[]): Promise<RuleResultInterface[]> {\n if (rule === undefined) return [];\n return rule\n .resolve(ctx, type, path, input, rawInput, options)\n .then(async res => {\n if (res !== true) {\n return [res];\n } else {\n return iterate(otherRules).then(ress => ress.concat(res));\n }\n });\n }\n }\n}\n\nexport class RuleRace<\n TContext extends Record<string, any>\n> extends LogicRule<TContext> {\n constructor(rules: ShieldRule<TContext>[]) {\n super(rules);\n }\n\n /**\n * Makes sure that at least one of them resolved to true.\n */\n override async resolve(\n ctx: TContext,\n type: string,\n path: string,\n input: { [name: string]: any },\n rawInput: unknown,\n options: OptionsInterface<TContext>\n ): Promise<RuleResultInterface> {\n const result = await this.evaluate(\n ctx,\n type,\n path,\n input,\n rawInput,\n options\n );\n\n if (result.includes(true)) {\n return true;\n } else {\n const customError = result.find(res => res instanceof Error);\n\n return customError ?? false;\n }\n }\n\n /**\n * Evaluates all the rules.\n */\n override async evaluate(\n ctx: TContext,\n type: string,\n path: string,\n input: { [name: string]: any },\n rawInput: unknown,\n options: OptionsInterface<TContext>\n ): Promise<RuleResultInterface[]> {\n const rules = this.getRules();\n\n return iterate(rules);\n\n async function iterate([\n rule,\n ...otherRules\n ]: ShieldRule<TContext>[]): Promise<RuleResultInterface[]> {\n if (rule === undefined) return [];\n return rule\n .resolve(ctx, type, path, input, rawInput, options)\n .then(async res => {\n if (res === true) {\n return [res];\n } else {\n return iterate(otherRules).then(ress => ress.concat(res));\n }\n });\n }\n }\n}\n\nexport class RuleNot<TContext extends Record<string, any>>\n extends LogicRule<TContext>\n implements LogicRuleInterface<TContext>\n{\n error?: Error;\n\n override name: string = \"RuleNot\";\n\n override equals!: (rule: RuleInterface<TContext>) => boolean;\n\n constructor(rule: ShieldRule<TContext>, error?: Error) {\n super([rule]);\n this.error = error;\n }\n\n /**\n * Negates the result.\n */\n override async resolve(\n ctx: TContext,\n type: string,\n path: string,\n input: { [name: string]: any },\n rawInput: unknown,\n options: OptionsInterface<TContext>\n ): Promise<RuleResultInterface> {\n const [res] = await this.evaluate(\n ctx,\n type,\n path,\n input,\n rawInput,\n options\n );\n\n if (res instanceof Error) {\n return true;\n } else if (res !== true) {\n return true;\n } else {\n if (this.error) return this.error;\n return false;\n }\n }\n}\n\nexport class RuleTrue<TContext extends Record<string, any>>\n extends LogicRule<TContext>\n implements LogicRuleInterface<TContext>\n{\n override name: string = \"RuleTrue\";\n\n override equals!: (rule: RuleInterface<TContext>) => boolean;\n\n constructor() {\n super([]);\n }\n\n /**\n *\n * Always true.\n *\n */\n override async resolve(): Promise<RuleResultInterface> {\n return true;\n }\n}\n\nexport class RuleFalse<TContext extends Record<string, any>>\n extends LogicRule<TContext>\n implements LogicRuleInterface<TContext>\n{\n override name: string = \"RuleTrue\";\n\n override equals!: (rule: RuleInterface<TContext>) => boolean;\n\n constructor() {\n super([]);\n }\n\n /**\n *\n * Always false.\n *\n */\n override async resolve(): Promise<RuleResultInterface> {\n return false;\n }\n}\n"],"mappings":"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"}
|
package/dist/shield/shield.cjs
CHANGED
|
@@ -1 +1,31 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_shield_constructors = require('./constructors.cjs');
|
|
2
|
+
const require_shield_generator = require('./generator.cjs');
|
|
3
|
+
const require_shield_utils = require('./utils.cjs');
|
|
4
|
+
const require_shield_validation = require('./validation.cjs');
|
|
5
|
+
|
|
6
|
+
//#region src/shield/shield.ts
|
|
7
|
+
/**
|
|
8
|
+
* Makes sure all of defined rules are in accord with the options
|
|
9
|
+
* shield can process.
|
|
10
|
+
*/
|
|
11
|
+
function normalizeOptions(options) {
|
|
12
|
+
if (typeof options.fallbackError === "string") options.fallbackError = new Error(options.fallbackError);
|
|
13
|
+
return {
|
|
14
|
+
debug: options.debug ?? false,
|
|
15
|
+
allowExternalErrors: require_shield_utils.withDefault(false)(options.allowExternalErrors),
|
|
16
|
+
fallbackRule: require_shield_utils.withDefault(require_shield_constructors.allow)(options.fallbackRule),
|
|
17
|
+
fallbackError: require_shield_utils.withDefault(/* @__PURE__ */ new Error("Authorization error"))(options.fallbackError)
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Validates rules and generates middleware from defined rule tree.
|
|
22
|
+
*/
|
|
23
|
+
function shield(ruleTree, options = {}) {
|
|
24
|
+
const normalizedOptions = normalizeOptions(options);
|
|
25
|
+
const ruleTreeValidity = require_shield_validation.validateRuleTree(ruleTree);
|
|
26
|
+
if (ruleTreeValidity.status === "ok") return require_shield_generator.generateMiddlewareFromRuleTree(ruleTree, normalizedOptions);
|
|
27
|
+
else throw new require_shield_validation.ValidationError(ruleTreeValidity.message);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
//#endregion
|
|
31
|
+
exports.shield = shield;
|
package/dist/shield/shield.mjs
CHANGED
|
@@ -1,2 +1,32 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { allow } from "./constructors.mjs";
|
|
2
|
+
import { generateMiddlewareFromRuleTree } from "./generator.mjs";
|
|
3
|
+
import { withDefault } from "./utils.mjs";
|
|
4
|
+
import { ValidationError, validateRuleTree } from "./validation.mjs";
|
|
5
|
+
|
|
6
|
+
//#region src/shield/shield.ts
|
|
7
|
+
/**
|
|
8
|
+
* Makes sure all of defined rules are in accord with the options
|
|
9
|
+
* shield can process.
|
|
10
|
+
*/
|
|
11
|
+
function normalizeOptions(options) {
|
|
12
|
+
if (typeof options.fallbackError === "string") options.fallbackError = new Error(options.fallbackError);
|
|
13
|
+
return {
|
|
14
|
+
debug: options.debug ?? false,
|
|
15
|
+
allowExternalErrors: withDefault(false)(options.allowExternalErrors),
|
|
16
|
+
fallbackRule: withDefault(allow)(options.fallbackRule),
|
|
17
|
+
fallbackError: withDefault(/* @__PURE__ */ new Error("Authorization error"))(options.fallbackError)
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Validates rules and generates middleware from defined rule tree.
|
|
22
|
+
*/
|
|
23
|
+
function shield(ruleTree, options = {}) {
|
|
24
|
+
const normalizedOptions = normalizeOptions(options);
|
|
25
|
+
const ruleTreeValidity = validateRuleTree(ruleTree);
|
|
26
|
+
if (ruleTreeValidity.status === "ok") return generateMiddlewareFromRuleTree(ruleTree, normalizedOptions);
|
|
27
|
+
else throw new ValidationError(ruleTreeValidity.message);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
//#endregion
|
|
31
|
+
export { shield };
|
|
2
32
|
//# sourceMappingURL=shield.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shield.mjs","names":[],"sources":["../../src/shield/shield.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport type { MiddlewareFunction } from \"@trpc/server/unstable-core-do-not-import\";\nimport { allow } from \"./constructors\";\nimport { generateMiddlewareFromRuleTree } from \"./generator\";\nimport type {\n IFallbackErrorType,\n IRules,\n OptionsConstructorInterface,\n OptionsInterface,\n ShieldRule\n} from \"./types\";\nimport { withDefault } from \"./utils\";\nimport { ValidationError, validateRuleTree } from \"./validation\";\n\n/**\n * Makes sure all of defined rules are in accord with the options\n * shield can process.\n */\nfunction normalizeOptions<TContext extends Record<string, any>>(\n options: OptionsConstructorInterface<TContext>\n): OptionsInterface<TContext> {\n if (typeof options.fallbackError === \"string\") {\n options.fallbackError = new Error(options.fallbackError);\n }\n\n return {\n debug: options.debug ?? false,\n allowExternalErrors: withDefault(false)(options.allowExternalErrors),\n fallbackRule: withDefault<ShieldRule<TContext>>(\n allow as ShieldRule<TContext>\n )(options.fallbackRule),\n fallbackError: withDefault<IFallbackErrorType<TContext>>(\n new Error(\"Authorization error\")\n )(options.fallbackError)\n };\n}\n\n/**\n * Validates rules and generates middleware from defined rule tree.\n */\nexport function shield<\n TContext extends Record<string, any>,\n TMeta extends object = object\n>(\n ruleTree: IRules<TContext>,\n options: OptionsConstructorInterface<TContext> = {}\n): MiddlewareFunction<TContext, TMeta, TContext, TContext, unknown> {\n const normalizedOptions = normalizeOptions<TContext>(options);\n const ruleTreeValidity = validateRuleTree<TContext>(ruleTree);\n\n if (ruleTreeValidity.status === \"ok\") {\n return generateMiddlewareFromRuleTree<TContext>(\n ruleTree,\n normalizedOptions\n ) as any;\n } else {\n throw new ValidationError(ruleTreeValidity.message);\n }\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"shield.mjs","names":[],"sources":["../../src/shield/shield.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport type { MiddlewareFunction } from \"@trpc/server/unstable-core-do-not-import\";\nimport { allow } from \"./constructors\";\nimport { generateMiddlewareFromRuleTree } from \"./generator\";\nimport type {\n IFallbackErrorType,\n IRules,\n OptionsConstructorInterface,\n OptionsInterface,\n ShieldRule\n} from \"./types\";\nimport { withDefault } from \"./utils\";\nimport { ValidationError, validateRuleTree } from \"./validation\";\n\n/**\n * Makes sure all of defined rules are in accord with the options\n * shield can process.\n */\nfunction normalizeOptions<TContext extends Record<string, any>>(\n options: OptionsConstructorInterface<TContext>\n): OptionsInterface<TContext> {\n if (typeof options.fallbackError === \"string\") {\n options.fallbackError = new Error(options.fallbackError);\n }\n\n return {\n debug: options.debug ?? false,\n allowExternalErrors: withDefault(false)(options.allowExternalErrors),\n fallbackRule: withDefault<ShieldRule<TContext>>(\n allow as ShieldRule<TContext>\n )(options.fallbackRule),\n fallbackError: withDefault<IFallbackErrorType<TContext>>(\n new Error(\"Authorization error\")\n )(options.fallbackError)\n };\n}\n\n/**\n * Validates rules and generates middleware from defined rule tree.\n */\nexport function shield<\n TContext extends Record<string, any>,\n TMeta extends object = object\n>(\n ruleTree: IRules<TContext>,\n options: OptionsConstructorInterface<TContext> = {}\n): MiddlewareFunction<TContext, TMeta, TContext, TContext, unknown> {\n const normalizedOptions = normalizeOptions<TContext>(options);\n const ruleTreeValidity = validateRuleTree<TContext>(ruleTree);\n\n if (ruleTreeValidity.status === \"ok\") {\n return generateMiddlewareFromRuleTree<TContext>(\n ruleTree,\n normalizedOptions\n ) as any;\n } else {\n throw new ValidationError(ruleTreeValidity.message);\n }\n}\n"],"mappings":";;;;;;;;;;AAmCA,SAAS,iBACP,SAC4B;AAC5B,KAAI,OAAO,QAAQ,kBAAkB,SACnC,SAAQ,gBAAgB,IAAI,MAAM,QAAQ,cAAc;AAG1D,QAAO;EACL,OAAO,QAAQ,SAAS;EACxB,qBAAqB,YAAY,MAAM,CAAC,QAAQ,oBAAoB;EACpE,cAAc,YACZ,MACD,CAAC,QAAQ,aAAa;EACvB,eAAe,4BACb,IAAI,MAAM,sBAAsB,CACjC,CAAC,QAAQ,cAAc;EACzB;;;;;AAMH,SAAgB,OAId,UACA,UAAiD,EAAE,EACe;CAClE,MAAM,oBAAoB,iBAA2B,QAAQ;CAC7D,MAAM,mBAAmB,iBAA2B,SAAS;AAE7D,KAAI,iBAAiB,WAAW,KAC9B,QAAO,+BACL,UACA,kBACD;KAED,OAAM,IAAI,gBAAgB,iBAAiB,QAAQ"}
|
package/dist/shield/utils.cjs
CHANGED
|
@@ -1 +1,56 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_shield_rules = require('./rules.cjs');
|
|
2
|
+
|
|
3
|
+
//#region src/shield/utils.ts
|
|
4
|
+
/**
|
|
5
|
+
* Makes sure that a certain field is a rule.
|
|
6
|
+
*/
|
|
7
|
+
function isRule(x) {
|
|
8
|
+
return x instanceof require_shield_rules.Rule || x && x.constructor && x.constructor.name === "Rule";
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Makes sure that a certain field is a logic rule.
|
|
12
|
+
*/
|
|
13
|
+
function isLogicRule(x) {
|
|
14
|
+
return x instanceof require_shield_rules.LogicRule || x && x.constructor && (x.constructor.name === "RuleOr" || x.constructor.name === "RuleAnd" || x.constructor.name === "RuleChain" || x.constructor.name === "RuleRace" || x.constructor.name === "RuleNot" || x.constructor.name === "RuleTrue" || x.constructor.name === "RuleFalse");
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Makes sure that a certain field is a rule or a logic rule.
|
|
18
|
+
*/
|
|
19
|
+
function isRuleFunction(x) {
|
|
20
|
+
return isRule(x) || isLogicRule(x);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Determines whether a certain field is rule field map or not.
|
|
24
|
+
*/
|
|
25
|
+
function isRuleFieldMap(x) {
|
|
26
|
+
return typeof x === "object" && Object.values(x).every((rule) => isRuleFunction(rule));
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Flattens object of particular type by checking if the leaf
|
|
30
|
+
* evaluates to true from particular function.
|
|
31
|
+
*/
|
|
32
|
+
function flattenObjectOf(obj, f) {
|
|
33
|
+
return Object.keys(obj).reduce((acc, key) => {
|
|
34
|
+
const val = obj[key];
|
|
35
|
+
if (f(val)) return [...acc, val];
|
|
36
|
+
else if (typeof val === "object" && !f(val)) return [...acc, ...flattenObjectOf(val, f)];
|
|
37
|
+
else return acc;
|
|
38
|
+
}, []);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Returns fallback is provided value is undefined
|
|
42
|
+
*/
|
|
43
|
+
function withDefault(fallback) {
|
|
44
|
+
return (value) => {
|
|
45
|
+
if (value === void 0) return fallback;
|
|
46
|
+
return value;
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
//#endregion
|
|
51
|
+
exports.flattenObjectOf = flattenObjectOf;
|
|
52
|
+
exports.isLogicRule = isLogicRule;
|
|
53
|
+
exports.isRule = isRule;
|
|
54
|
+
exports.isRuleFieldMap = isRuleFieldMap;
|
|
55
|
+
exports.isRuleFunction = isRuleFunction;
|
|
56
|
+
exports.withDefault = withDefault;
|
package/dist/shield/utils.mjs
CHANGED
|
@@ -1,2 +1,52 @@
|
|
|
1
|
-
import{LogicRule
|
|
1
|
+
import { LogicRule, Rule } from "./rules.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/shield/utils.ts
|
|
4
|
+
/**
|
|
5
|
+
* Makes sure that a certain field is a rule.
|
|
6
|
+
*/
|
|
7
|
+
function isRule(x) {
|
|
8
|
+
return x instanceof Rule || x && x.constructor && x.constructor.name === "Rule";
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Makes sure that a certain field is a logic rule.
|
|
12
|
+
*/
|
|
13
|
+
function isLogicRule(x) {
|
|
14
|
+
return x instanceof LogicRule || x && x.constructor && (x.constructor.name === "RuleOr" || x.constructor.name === "RuleAnd" || x.constructor.name === "RuleChain" || x.constructor.name === "RuleRace" || x.constructor.name === "RuleNot" || x.constructor.name === "RuleTrue" || x.constructor.name === "RuleFalse");
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Makes sure that a certain field is a rule or a logic rule.
|
|
18
|
+
*/
|
|
19
|
+
function isRuleFunction(x) {
|
|
20
|
+
return isRule(x) || isLogicRule(x);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Determines whether a certain field is rule field map or not.
|
|
24
|
+
*/
|
|
25
|
+
function isRuleFieldMap(x) {
|
|
26
|
+
return typeof x === "object" && Object.values(x).every((rule) => isRuleFunction(rule));
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Flattens object of particular type by checking if the leaf
|
|
30
|
+
* evaluates to true from particular function.
|
|
31
|
+
*/
|
|
32
|
+
function flattenObjectOf(obj, f) {
|
|
33
|
+
return Object.keys(obj).reduce((acc, key) => {
|
|
34
|
+
const val = obj[key];
|
|
35
|
+
if (f(val)) return [...acc, val];
|
|
36
|
+
else if (typeof val === "object" && !f(val)) return [...acc, ...flattenObjectOf(val, f)];
|
|
37
|
+
else return acc;
|
|
38
|
+
}, []);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Returns fallback is provided value is undefined
|
|
42
|
+
*/
|
|
43
|
+
function withDefault(fallback) {
|
|
44
|
+
return (value) => {
|
|
45
|
+
if (value === void 0) return fallback;
|
|
46
|
+
return value;
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
//#endregion
|
|
51
|
+
export { flattenObjectOf, isLogicRule, isRule, isRuleFieldMap, isRuleFunction, withDefault };
|
|
2
52
|
//# sourceMappingURL=utils.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.mjs","names":[],"sources":["../../src/shield/utils.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { LogicRule, Rule } from \"./rules\";\nimport type {\n LogicRuleInterface,\n RuleFieldMapInterface,\n RuleInterface,\n ShieldRule\n} from \"./types\";\n\n/**\n * Makes sure that a certain field is a rule.\n */\nexport function isRule<TContext extends Record<string, any>>(\n x: any\n): x is RuleInterface<TContext> {\n return (\n x instanceof Rule || (x && x.constructor && x.constructor.name === \"Rule\")\n );\n}\n\n/**\n * Makes sure that a certain field is a logic rule.\n */\nexport function isLogicRule<TContext extends Record<string, any>>(\n x: any\n): x is LogicRuleInterface<TContext> {\n return (\n x instanceof LogicRule ||\n (x &&\n x.constructor &&\n (x.constructor.name === \"RuleOr\" ||\n x.constructor.name === \"RuleAnd\" ||\n x.constructor.name === \"RuleChain\" ||\n x.constructor.name === \"RuleRace\" ||\n x.constructor.name === \"RuleNot\" ||\n x.constructor.name === \"RuleTrue\" ||\n x.constructor.name === \"RuleFalse\"))\n );\n}\n\n/**\n * Makes sure that a certain field is a rule or a logic rule.\n */\nexport function isRuleFunction<TContext extends Record<string, any>>(\n x: any\n): x is ShieldRule<TContext> {\n return isRule(x) || isLogicRule(x);\n}\n\n/**\n * Determines whether a certain field is rule field map or not.\n */\nexport function isRuleFieldMap<TContext extends Record<string, any>>(\n x: any\n): x is RuleFieldMapInterface<TContext> {\n return (\n typeof x === \"object\" &&\n Object.values(x).every(rule => isRuleFunction(rule))\n );\n}\n\n/**\n * Flattens object of particular type by checking if the leaf\n * evaluates to true from particular function.\n */\nexport function flattenObjectOf<T>(\n obj: { [key: string]: any },\n f: (x: any) => boolean\n): T[] {\n const values = Object.keys(obj).reduce<T[]>((acc, key) => {\n const val = obj[key];\n if (f(val)) {\n return [...acc, val];\n } else if (typeof val === \"object\" && !f(val)) {\n return [...acc, ...flattenObjectOf(val, f)];\n } else {\n return acc;\n }\n }, []);\n\n return values;\n}\n\n/**\n * Returns fallback is provided value is undefined\n */\nexport function withDefault<T>(fallback: T): (value: T | undefined) => T {\n return value => {\n if (value === undefined) return fallback;\n return value;\n };\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"utils.mjs","names":[],"sources":["../../src/shield/utils.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { LogicRule, Rule } from \"./rules\";\nimport type {\n LogicRuleInterface,\n RuleFieldMapInterface,\n RuleInterface,\n ShieldRule\n} from \"./types\";\n\n/**\n * Makes sure that a certain field is a rule.\n */\nexport function isRule<TContext extends Record<string, any>>(\n x: any\n): x is RuleInterface<TContext> {\n return (\n x instanceof Rule || (x && x.constructor && x.constructor.name === \"Rule\")\n );\n}\n\n/**\n * Makes sure that a certain field is a logic rule.\n */\nexport function isLogicRule<TContext extends Record<string, any>>(\n x: any\n): x is LogicRuleInterface<TContext> {\n return (\n x instanceof LogicRule ||\n (x &&\n x.constructor &&\n (x.constructor.name === \"RuleOr\" ||\n x.constructor.name === \"RuleAnd\" ||\n x.constructor.name === \"RuleChain\" ||\n x.constructor.name === \"RuleRace\" ||\n x.constructor.name === \"RuleNot\" ||\n x.constructor.name === \"RuleTrue\" ||\n x.constructor.name === \"RuleFalse\"))\n );\n}\n\n/**\n * Makes sure that a certain field is a rule or a logic rule.\n */\nexport function isRuleFunction<TContext extends Record<string, any>>(\n x: any\n): x is ShieldRule<TContext> {\n return isRule(x) || isLogicRule(x);\n}\n\n/**\n * Determines whether a certain field is rule field map or not.\n */\nexport function isRuleFieldMap<TContext extends Record<string, any>>(\n x: any\n): x is RuleFieldMapInterface<TContext> {\n return (\n typeof x === \"object\" &&\n Object.values(x).every(rule => isRuleFunction(rule))\n );\n}\n\n/**\n * Flattens object of particular type by checking if the leaf\n * evaluates to true from particular function.\n */\nexport function flattenObjectOf<T>(\n obj: { [key: string]: any },\n f: (x: any) => boolean\n): T[] {\n const values = Object.keys(obj).reduce<T[]>((acc, key) => {\n const val = obj[key];\n if (f(val)) {\n return [...acc, val];\n } else if (typeof val === \"object\" && !f(val)) {\n return [...acc, ...flattenObjectOf(val, f)];\n } else {\n return acc;\n }\n }, []);\n\n return values;\n}\n\n/**\n * Returns fallback is provided value is undefined\n */\nexport function withDefault<T>(fallback: T): (value: T | undefined) => T {\n return value => {\n if (value === undefined) return fallback;\n return value;\n };\n}\n"],"mappings":";;;;;;AA6BA,SAAgB,OACd,GAC8B;AAC9B,QACE,aAAa,QAAS,KAAK,EAAE,eAAe,EAAE,YAAY,SAAS;;;;;AAOvE,SAAgB,YACd,GACmC;AACnC,QACE,aAAa,aACZ,KACC,EAAE,gBACD,EAAE,YAAY,SAAS,YACtB,EAAE,YAAY,SAAS,aACvB,EAAE,YAAY,SAAS,eACvB,EAAE,YAAY,SAAS,cACvB,EAAE,YAAY,SAAS,aACvB,EAAE,YAAY,SAAS,cACvB,EAAE,YAAY,SAAS;;;;;AAO/B,SAAgB,eACd,GAC2B;AAC3B,QAAO,OAAO,EAAE,IAAI,YAAY,EAAE;;;;;AAMpC,SAAgB,eACd,GACsC;AACtC,QACE,OAAO,MAAM,YACb,OAAO,OAAO,EAAE,CAAC,OAAM,SAAQ,eAAe,KAAK,CAAC;;;;;;AAQxD,SAAgB,gBACd,KACA,GACK;AAYL,QAXe,OAAO,KAAK,IAAI,CAAC,QAAa,KAAK,QAAQ;EACxD,MAAM,MAAM,IAAI;AAChB,MAAI,EAAE,IAAI,CACR,QAAO,CAAC,GAAG,KAAK,IAAI;WACX,OAAO,QAAQ,YAAY,CAAC,EAAE,IAAI,CAC3C,QAAO,CAAC,GAAG,KAAK,GAAG,gBAAgB,KAAK,EAAE,CAAC;MAE3C,QAAO;IAER,EAAE,CAAC;;;;;AAQR,SAAgB,YAAe,UAA0C;AACvE,SAAO,UAAS;AACd,MAAI,UAAU,OAAW,QAAO;AAChC,SAAO"}
|
|
@@ -1 +1,59 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_shield_utils = require('./utils.cjs');
|
|
2
|
+
|
|
3
|
+
//#region src/shield/validation.ts
|
|
4
|
+
/**
|
|
5
|
+
* Validates the rule tree declaration by checking references of rule
|
|
6
|
+
* functions. We deem rule tree valid if no two rules with the same name point
|
|
7
|
+
* to different rules.
|
|
8
|
+
*/
|
|
9
|
+
function validateRuleTree(ruleTree) {
|
|
10
|
+
const valid = extractRules(ruleTree).reduce(({ map, duplicates }, rule) => {
|
|
11
|
+
if (!map.has(rule.name)) return {
|
|
12
|
+
map: map.set(rule.name, rule),
|
|
13
|
+
duplicates
|
|
14
|
+
};
|
|
15
|
+
else if (!map.get(rule.name).equals(rule) && !duplicates.includes(rule.name)) return {
|
|
16
|
+
map: map.set(rule.name, rule),
|
|
17
|
+
duplicates: [...duplicates, rule.name]
|
|
18
|
+
};
|
|
19
|
+
else return {
|
|
20
|
+
map,
|
|
21
|
+
duplicates
|
|
22
|
+
};
|
|
23
|
+
}, {
|
|
24
|
+
map: /* @__PURE__ */ new Map(),
|
|
25
|
+
duplicates: []
|
|
26
|
+
});
|
|
27
|
+
if (valid.duplicates.length === 0) return { status: "ok" };
|
|
28
|
+
else return {
|
|
29
|
+
status: "err",
|
|
30
|
+
message: `There seem to be multiple definitions of these rules: ${valid.duplicates.join(", ")}`
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Extracts rules from rule tree.
|
|
34
|
+
*/
|
|
35
|
+
function extractRules(ruleTree$1) {
|
|
36
|
+
return require_shield_utils.flattenObjectOf(ruleTree$1, require_shield_utils.isRuleFunction).reduce((rules, rule) => {
|
|
37
|
+
if (require_shield_utils.isLogicRule(rule)) return [...rules, ...extractLogicRules(rule)];
|
|
38
|
+
else return [...rules, rule];
|
|
39
|
+
}, []);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Recursively extracts Rules from LogicRule
|
|
43
|
+
*/
|
|
44
|
+
function extractLogicRules(rule) {
|
|
45
|
+
return rule.getRules().reduce((acc, shieldRule) => {
|
|
46
|
+
if (require_shield_utils.isLogicRule(shieldRule)) return [...acc, ...extractLogicRules(shieldRule)];
|
|
47
|
+
else return [...acc, shieldRule];
|
|
48
|
+
}, []);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
var ValidationError = class extends Error {
|
|
52
|
+
constructor(message) {
|
|
53
|
+
super(message);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
//#endregion
|
|
58
|
+
exports.ValidationError = ValidationError;
|
|
59
|
+
exports.validateRuleTree = validateRuleTree;
|
|
@@ -1,2 +1,59 @@
|
|
|
1
|
-
import{flattenObjectOf
|
|
1
|
+
import { flattenObjectOf, isLogicRule, isRuleFunction } from "./utils.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/shield/validation.ts
|
|
4
|
+
/**
|
|
5
|
+
* Validates the rule tree declaration by checking references of rule
|
|
6
|
+
* functions. We deem rule tree valid if no two rules with the same name point
|
|
7
|
+
* to different rules.
|
|
8
|
+
*/
|
|
9
|
+
function validateRuleTree(ruleTree) {
|
|
10
|
+
const valid = extractRules(ruleTree).reduce(({ map, duplicates }, rule) => {
|
|
11
|
+
if (!map.has(rule.name)) return {
|
|
12
|
+
map: map.set(rule.name, rule),
|
|
13
|
+
duplicates
|
|
14
|
+
};
|
|
15
|
+
else if (!map.get(rule.name).equals(rule) && !duplicates.includes(rule.name)) return {
|
|
16
|
+
map: map.set(rule.name, rule),
|
|
17
|
+
duplicates: [...duplicates, rule.name]
|
|
18
|
+
};
|
|
19
|
+
else return {
|
|
20
|
+
map,
|
|
21
|
+
duplicates
|
|
22
|
+
};
|
|
23
|
+
}, {
|
|
24
|
+
map: /* @__PURE__ */ new Map(),
|
|
25
|
+
duplicates: []
|
|
26
|
+
});
|
|
27
|
+
if (valid.duplicates.length === 0) return { status: "ok" };
|
|
28
|
+
else return {
|
|
29
|
+
status: "err",
|
|
30
|
+
message: `There seem to be multiple definitions of these rules: ${valid.duplicates.join(", ")}`
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Extracts rules from rule tree.
|
|
34
|
+
*/
|
|
35
|
+
function extractRules(ruleTree$1) {
|
|
36
|
+
return flattenObjectOf(ruleTree$1, isRuleFunction).reduce((rules, rule) => {
|
|
37
|
+
if (isLogicRule(rule)) return [...rules, ...extractLogicRules(rule)];
|
|
38
|
+
else return [...rules, rule];
|
|
39
|
+
}, []);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Recursively extracts Rules from LogicRule
|
|
43
|
+
*/
|
|
44
|
+
function extractLogicRules(rule) {
|
|
45
|
+
return rule.getRules().reduce((acc, shieldRule) => {
|
|
46
|
+
if (isLogicRule(shieldRule)) return [...acc, ...extractLogicRules(shieldRule)];
|
|
47
|
+
else return [...acc, shieldRule];
|
|
48
|
+
}, []);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
var ValidationError = class extends Error {
|
|
52
|
+
constructor(message) {
|
|
53
|
+
super(message);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
//#endregion
|
|
58
|
+
export { ValidationError, validateRuleTree };
|
|
2
59
|
//# sourceMappingURL=validation.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validation.mjs","names":["ruleTree"],"sources":["../../src/shield/validation.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport type {\n IRules,\n LogicRuleInterface,\n RuleInterface,\n ShieldRule\n} from \"./types\";\nimport { flattenObjectOf, isLogicRule, isRuleFunction } from \"./utils\";\n\n/**\n * Validates the rule tree declaration by checking references of rule\n * functions. We deem rule tree valid if no two rules with the same name point\n * to different rules.\n */\nexport function validateRuleTree<TContext extends Record<string, any>>(\n ruleTree: IRules<TContext>\n): { status: \"ok\" } | { status: \"err\"; message: string } {\n const rules = extractRules(ruleTree);\n\n const valid = rules.reduce<{\n map: Map<string, RuleInterface<TContext>>;\n duplicates: string[];\n }>(\n ({ map, duplicates }, rule) => {\n if (!map.has(rule.name)) {\n return { map: map.set(rule.name, rule), duplicates };\n } else if (\n !map.get(rule.name)!.equals(rule) &&\n !duplicates.includes(rule.name)\n ) {\n return {\n map: map.set(rule.name, rule),\n duplicates: [...duplicates, rule.name]\n };\n } else {\n return { map, duplicates };\n }\n },\n { map: new Map<string, RuleInterface<TContext>>(), duplicates: [] }\n );\n\n if (valid.duplicates.length === 0) {\n return { status: \"ok\" };\n } else {\n const duplicates = valid.duplicates.join(\", \");\n\n return {\n status: \"err\",\n message: `There seem to be multiple definitions of these rules: ${duplicates}`\n };\n }\n\n /**\n * Extracts rules from rule tree.\n */\n function extractRules<TContext extends Record<string, any>>(\n ruleTree: IRules<TContext>\n ): RuleInterface<TContext>[] {\n const resolvers = flattenObjectOf<ShieldRule<TContext>>(\n ruleTree,\n isRuleFunction\n );\n\n const rules = resolvers.reduce<RuleInterface<TContext>[]>((rules, rule) => {\n if (isLogicRule(rule)) {\n return [\n ...rules,\n ...extractLogicRules(rule)\n ] as RuleInterface<TContext>[];\n } else {\n return [...rules, rule] as RuleInterface<TContext>[];\n }\n }, []);\n\n return rules;\n }\n\n /**\n * Recursively extracts Rules from LogicRule\n */\n function extractLogicRules<TContext extends Record<string, any>>(\n rule: LogicRuleInterface<TContext>\n ): RuleInterface<TContext>[] {\n return rule\n .getRules()\n .reduce<RuleInterface<TContext>[]>((acc, shieldRule) => {\n if (isLogicRule(shieldRule)) {\n return [\n ...acc,\n ...extractLogicRules(shieldRule)\n ] as RuleInterface<TContext>[];\n } else {\n return [...acc, shieldRule] as RuleInterface<TContext>[];\n }\n }, []);\n }\n}\n\nexport class ValidationError extends Error {\n constructor(message: string) {\n super(message);\n }\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"validation.mjs","names":["ruleTree"],"sources":["../../src/shield/validation.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport type {\n IRules,\n LogicRuleInterface,\n RuleInterface,\n ShieldRule\n} from \"./types\";\nimport { flattenObjectOf, isLogicRule, isRuleFunction } from \"./utils\";\n\n/**\n * Validates the rule tree declaration by checking references of rule\n * functions. We deem rule tree valid if no two rules with the same name point\n * to different rules.\n */\nexport function validateRuleTree<TContext extends Record<string, any>>(\n ruleTree: IRules<TContext>\n): { status: \"ok\" } | { status: \"err\"; message: string } {\n const rules = extractRules(ruleTree);\n\n const valid = rules.reduce<{\n map: Map<string, RuleInterface<TContext>>;\n duplicates: string[];\n }>(\n ({ map, duplicates }, rule) => {\n if (!map.has(rule.name)) {\n return { map: map.set(rule.name, rule), duplicates };\n } else if (\n !map.get(rule.name)!.equals(rule) &&\n !duplicates.includes(rule.name)\n ) {\n return {\n map: map.set(rule.name, rule),\n duplicates: [...duplicates, rule.name]\n };\n } else {\n return { map, duplicates };\n }\n },\n { map: new Map<string, RuleInterface<TContext>>(), duplicates: [] }\n );\n\n if (valid.duplicates.length === 0) {\n return { status: \"ok\" };\n } else {\n const duplicates = valid.duplicates.join(\", \");\n\n return {\n status: \"err\",\n message: `There seem to be multiple definitions of these rules: ${duplicates}`\n };\n }\n\n /**\n * Extracts rules from rule tree.\n */\n function extractRules<TContext extends Record<string, any>>(\n ruleTree: IRules<TContext>\n ): RuleInterface<TContext>[] {\n const resolvers = flattenObjectOf<ShieldRule<TContext>>(\n ruleTree,\n isRuleFunction\n );\n\n const rules = resolvers.reduce<RuleInterface<TContext>[]>((rules, rule) => {\n if (isLogicRule(rule)) {\n return [\n ...rules,\n ...extractLogicRules(rule)\n ] as RuleInterface<TContext>[];\n } else {\n return [...rules, rule] as RuleInterface<TContext>[];\n }\n }, []);\n\n return rules;\n }\n\n /**\n * Recursively extracts Rules from LogicRule\n */\n function extractLogicRules<TContext extends Record<string, any>>(\n rule: LogicRuleInterface<TContext>\n ): RuleInterface<TContext>[] {\n return rule\n .getRules()\n .reduce<RuleInterface<TContext>[]>((acc, shieldRule) => {\n if (isLogicRule(shieldRule)) {\n return [\n ...acc,\n ...extractLogicRules(shieldRule)\n ] as RuleInterface<TContext>[];\n } else {\n return [...acc, shieldRule] as RuleInterface<TContext>[];\n }\n }, []);\n }\n}\n\nexport class ValidationError extends Error {\n constructor(message: string) {\n super(message);\n }\n}\n"],"mappings":";;;;;;;;AA+BA,SAAgB,iBACd,UACuD;CAGvD,MAAM,QAFQ,aAAa,SAAS,CAEhB,QAIjB,EAAE,KAAK,cAAc,SAAS;AAC7B,MAAI,CAAC,IAAI,IAAI,KAAK,KAAK,CACrB,QAAO;GAAE,KAAK,IAAI,IAAI,KAAK,MAAM,KAAK;GAAE;GAAY;WAEpD,CAAC,IAAI,IAAI,KAAK,KAAK,CAAE,OAAO,KAAK,IACjC,CAAC,WAAW,SAAS,KAAK,KAAK,CAE/B,QAAO;GACL,KAAK,IAAI,IAAI,KAAK,MAAM,KAAK;GAC7B,YAAY,CAAC,GAAG,YAAY,KAAK,KAAK;GACvC;MAED,QAAO;GAAE;GAAK;GAAY;IAG9B;EAAE,qBAAK,IAAI,KAAsC;EAAE,YAAY,EAAE;EAAE,CACpE;AAED,KAAI,MAAM,WAAW,WAAW,EAC9B,QAAO,EAAE,QAAQ,MAAM;KAIvB,QAAO;EACL,QAAQ;EACR,SAAS,yDAJQ,MAAM,WAAW,KAAK,KAAK;EAK7C;;;;CAMH,SAAS,aACP,YAC2B;AAiB3B,SAhBkB,gBAChBA,YACA,eACD,CAEuB,QAAmC,OAAO,SAAS;AACzE,OAAI,YAAY,KAAK,CACnB,QAAO,CACL,GAAG,OACH,GAAG,kBAAkB,KAAK,CAC3B;OAED,QAAO,CAAC,GAAG,OAAO,KAAK;KAExB,EAAE,CAAC;;;;;CAQR,SAAS,kBACP,MAC2B;AAC3B,SAAO,KACJ,UAAU,CACV,QAAmC,KAAK,eAAe;AACtD,OAAI,YAAY,WAAW,CACzB,QAAO,CACL,GAAG,KACH,GAAG,kBAAkB,WAAW,CACjC;OAED,QAAO,CAAC,GAAG,KAAK,WAAW;KAE5B,EAAE,CAAC;;;AAIZ,IAAa,kBAAb,cAAqC,MAAM;CACzC,YAAY,SAAiB;AAC3B,QAAM,QAAQ"}
|