@bitbybit-dev/base 1.3.2 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/lib/api/GlobalCDNProvider.js +1 -1
- package/lib/api/index.d.ts +2 -0
- package/lib/api/index.js +2 -0
- package/lib/api/inputs/base-inputs.d.ts +10 -4
- package/lib/api/inputs/color-inputs.d.ts +24 -23
- package/lib/api/inputs/color-inputs.js +1 -0
- package/lib/api/inputs/dates-inputs.d.ts +16 -16
- package/lib/api/inputs/frame-inputs.d.ts +515 -0
- package/lib/api/inputs/frame-inputs.js +617 -0
- package/lib/api/inputs/index.d.ts +1 -0
- package/lib/api/inputs/index.js +1 -0
- package/lib/api/inputs/io-inputs.d.ts +3 -3
- package/lib/api/inputs/io-inputs.js +1 -1
- package/lib/api/inputs/lists-inputs.d.ts +23 -23
- package/lib/api/inputs/logic-inputs.d.ts +11 -11
- package/lib/api/inputs/math-inputs.d.ts +49 -49
- package/lib/api/inputs/math-inputs.js +4 -4
- package/lib/api/inputs/mesh-inputs.d.ts +3 -3
- package/lib/api/inputs/mesh-inputs.js +3 -3
- package/lib/api/inputs/point-inputs.d.ts +39 -38
- package/lib/api/inputs/point-inputs.js +6 -5
- package/lib/api/inputs/polyline-inputs.d.ts +5 -4
- package/lib/api/inputs/polyline-inputs.js +3 -3
- package/lib/api/inputs/text-inputs.d.ts +34 -33
- package/lib/api/inputs/text-inputs.js +15 -12
- package/lib/api/inputs/transforms-inputs.d.ts +16 -16
- package/lib/api/inputs/vector-inputs.d.ts +26 -25
- package/lib/api/inputs/vector-inputs.js +2 -1
- package/lib/api/kernel-calls/call-by-path.d.ts +12 -0
- package/lib/api/kernel-calls/call-by-path.js +36 -0
- package/lib/api/kernel-calls/constraints.d.ts +53 -0
- package/lib/api/kernel-calls/constraints.js +21 -0
- package/lib/api/kernel-calls/describe-failure.d.ts +32 -0
- package/lib/api/kernel-calls/describe-failure.js +164 -0
- package/lib/api/kernel-calls/errors.d.ts +55 -0
- package/lib/api/kernel-calls/errors.js +42 -0
- package/lib/api/kernel-calls/failure-message.d.ts +18 -0
- package/lib/api/kernel-calls/failure-message.js +36 -0
- package/lib/api/kernel-calls/index.d.ts +10 -0
- package/lib/api/kernel-calls/index.js +10 -0
- package/lib/api/kernel-calls/input-rules.d.ts +67 -0
- package/lib/api/kernel-calls/input-rules.js +135 -0
- package/lib/api/kernel-calls/input-validation.d.ts +130 -0
- package/lib/api/kernel-calls/input-validation.js +221 -0
- package/lib/api/kernel-calls/rehydrate.d.ts +16 -0
- package/lib/api/kernel-calls/rehydrate.js +38 -0
- package/lib/api/kernel-calls/resolve-dto.d.ts +59 -0
- package/lib/api/kernel-calls/resolve-dto.js +61 -0
- package/lib/api/kernel-calls/with-defaults.d.ts +16 -0
- package/lib/api/kernel-calls/with-defaults.js +53 -0
- package/lib/api/resolved-inputs/index.d.ts +304 -0
- package/lib/api/resolved-inputs/index.js +1 -0
- package/lib/api/services/color.js +43 -26
- package/lib/api/services/dates.js +53 -33
- package/lib/api/services/frame.d.ts +670 -0
- package/lib/api/services/frame.js +1040 -0
- package/lib/api/services/helpers/dxf/dxf-generator.d.ts +2 -3
- package/lib/api/services/helpers/dxf/dxf-generator.js +27 -37
- package/lib/api/services/helpers/dxf/dxf.js +10 -4
- package/lib/api/services/helpers/frame-axes.d.ts +36 -0
- package/lib/api/services/helpers/frame-axes.js +57 -0
- package/lib/api/services/helpers/matrices.d.ts +17 -0
- package/lib/api/services/helpers/matrices.js +51 -0
- package/lib/api/services/index.d.ts +1 -0
- package/lib/api/services/index.js +1 -0
- package/lib/api/services/line.js +15 -12
- package/lib/api/services/lists.js +174 -136
- package/lib/api/services/logic.js +44 -34
- package/lib/api/services/math.js +127 -82
- package/lib/api/services/mesh.js +20 -13
- package/lib/api/services/point.js +62 -46
- package/lib/api/services/polyline.js +22 -18
- package/lib/api/services/text.js +81 -46
- package/lib/api/services/transforms.js +38 -25
- package/lib/api/services/vector.js +37 -22
- package/package.json +1 -1
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
const numberAt = (inputs, key) => {
|
|
2
|
+
const value = inputs[key];
|
|
3
|
+
return typeof value === "number" ? value : undefined;
|
|
4
|
+
};
|
|
5
|
+
const listAt = (inputs, key) => {
|
|
6
|
+
const value = inputs[key];
|
|
7
|
+
return Array.isArray(value) ? value : undefined;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* The rules one DTO's inputs have to satisfy together, beyond what each property accepts on its own.
|
|
11
|
+
* They are written against the DTO as its defaults leave it - `Resolved.<Namespace>.<Dto>` - since
|
|
12
|
+
* they run after the defaults are laid under the inputs.
|
|
13
|
+
* @param dto - The DTO class the rules are for; rules for an abstract parent apply to every DTO that
|
|
14
|
+
* extends it
|
|
15
|
+
* @param rules - The rules, built with `custom`, `lessThan`, `sameLength` and the rest
|
|
16
|
+
* @returns The rules, ready for `ruleBook`
|
|
17
|
+
*/
|
|
18
|
+
export function defineRules(dto, rules) {
|
|
19
|
+
return {
|
|
20
|
+
dto,
|
|
21
|
+
rules: rules.map((rule) => ({ reads: rule.reads, check: (inputs) => rule.check(inputs) })),
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* A rule stated as a test: the inputs pass when `holds` returns true for them.
|
|
26
|
+
* @param property - The property the issue is reported on
|
|
27
|
+
* @param holds - Whether the inputs are fine
|
|
28
|
+
* @param message - What is wrong, said of `property`
|
|
29
|
+
* @param reads - The properties `holds` looks at, when there are others than `property`
|
|
30
|
+
* @returns The rule
|
|
31
|
+
*/
|
|
32
|
+
export function custom(property, holds, message, reads = [property]) {
|
|
33
|
+
return { reads, check: (inputs) => (holds(inputs) ? undefined : { property, code: "custom", message }) };
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* `property` has to be less than another property, or than a limit worked out from the inputs.
|
|
37
|
+
* @param property - The property that has to be smaller
|
|
38
|
+
* @param limit - The property it is compared with, or a function giving the limit
|
|
39
|
+
* @param message - What is wrong, said of `property`; by default "must be less than" the other property
|
|
40
|
+
* @param reads - The properties a limit function looks at
|
|
41
|
+
* @returns The rule
|
|
42
|
+
*/
|
|
43
|
+
export function lessThan(property, limit, message, reads = []) {
|
|
44
|
+
const limitOf = (inputs) => (typeof limit === "function" ? limit(inputs) : numberAt(inputs, limit));
|
|
45
|
+
return {
|
|
46
|
+
reads: [property, ...(typeof limit === "function" ? reads : [limit])],
|
|
47
|
+
check: (inputs) => {
|
|
48
|
+
const value = numberAt(inputs, property);
|
|
49
|
+
const bound = limitOf(inputs);
|
|
50
|
+
if (value === undefined || bound === undefined || value < bound)
|
|
51
|
+
return undefined;
|
|
52
|
+
return { property, code: "less-than", params: { limit: bound }, message: message !== null && message !== void 0 ? message : `must be less than ${typeof limit === "function" ? bound : limit}` };
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Two lists that pair up by position must have the same length. A list left out is not compared.
|
|
58
|
+
* @param property - The list the issue is reported on
|
|
59
|
+
* @param other - The list it pairs with
|
|
60
|
+
* @returns The rule
|
|
61
|
+
*/
|
|
62
|
+
export function sameLength(property, other) {
|
|
63
|
+
return {
|
|
64
|
+
reads: [property, other],
|
|
65
|
+
check: (inputs) => {
|
|
66
|
+
const list = listAt(inputs, property);
|
|
67
|
+
const partner = listAt(inputs, other);
|
|
68
|
+
if (!list || !partner || list.length === partner.length)
|
|
69
|
+
return undefined;
|
|
70
|
+
return { property, code: "same-length", params: { expected: partner.length, actual: list.length }, message: `must have as many items as ${other} (${partner.length}), not ${list.length}` };
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Two points have to differ.
|
|
76
|
+
* @param property - The point the issue is reported on
|
|
77
|
+
* @param other - The point it must differ from
|
|
78
|
+
* @returns The rule
|
|
79
|
+
*/
|
|
80
|
+
export function distinct(property, other) {
|
|
81
|
+
return {
|
|
82
|
+
reads: [property, other],
|
|
83
|
+
check: (inputs) => {
|
|
84
|
+
const a = listAt(inputs, property);
|
|
85
|
+
const b = listAt(inputs, other);
|
|
86
|
+
if (!a || !b || a.length !== b.length || a.some((value, index) => value !== b[index]))
|
|
87
|
+
return undefined;
|
|
88
|
+
return { property, code: "distinct", message: `must differ from ${other}` };
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* A direction has to have a length: a vector of zeros points nowhere.
|
|
94
|
+
* @param property - The vector
|
|
95
|
+
* @returns The rule
|
|
96
|
+
*/
|
|
97
|
+
export function notZeroVector(property) {
|
|
98
|
+
return {
|
|
99
|
+
reads: [property],
|
|
100
|
+
check: (inputs) => {
|
|
101
|
+
const vector = listAt(inputs, property);
|
|
102
|
+
if (!vector || vector.some((value) => value !== 0))
|
|
103
|
+
return undefined;
|
|
104
|
+
return { property, code: "zero-vector", message: "must not be a zero vector" };
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* At least one item of a list has to pass a test.
|
|
110
|
+
* @param property - The list
|
|
111
|
+
* @param holds - The test an item has to pass
|
|
112
|
+
* @param message - What is wrong, said of `property`
|
|
113
|
+
* @returns The rule
|
|
114
|
+
*/
|
|
115
|
+
export function atLeastOne(property, holds, message) {
|
|
116
|
+
return {
|
|
117
|
+
reads: [property],
|
|
118
|
+
check: (inputs) => {
|
|
119
|
+
const list = listAt(inputs, property);
|
|
120
|
+
if (!list || list.some(holds))
|
|
121
|
+
return undefined;
|
|
122
|
+
return { property, code: "at-least-one", message };
|
|
123
|
+
},
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* A rule that applies only when a condition holds, such as a list that has to match another only
|
|
128
|
+
* when it is given.
|
|
129
|
+
* @param condition - When the rule applies
|
|
130
|
+
* @param rule - The rule
|
|
131
|
+
* @returns The rule, checked only when the condition holds
|
|
132
|
+
*/
|
|
133
|
+
export function when(condition, rule) {
|
|
134
|
+
return { reads: rule.reads, check: (inputs) => (condition(inputs) ? rule.check(inputs) : undefined) };
|
|
135
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { DtoConstraints } from "./constraints";
|
|
2
|
+
import { DtoRegistry } from "./resolve-dto";
|
|
3
|
+
/**
|
|
4
|
+
* Something wrong with the inputs of a call. `property` names the input at fault, `code` says what
|
|
5
|
+
* is wrong in a form a program can match - an editor can translate it and mark the field - and
|
|
6
|
+
* `params` carries the values the message is built from.
|
|
7
|
+
*/
|
|
8
|
+
export type InputIssue = {
|
|
9
|
+
readonly property: string;
|
|
10
|
+
readonly code: string;
|
|
11
|
+
readonly params?: Readonly<Record<string, unknown>>;
|
|
12
|
+
readonly message: string;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* One rule over several properties of a DTO. `reads` names the properties it looks at, so it runs
|
|
16
|
+
* only when each of them passed its own check.
|
|
17
|
+
*/
|
|
18
|
+
export type InputRule<T> = {
|
|
19
|
+
readonly reads: readonly string[];
|
|
20
|
+
readonly check: (inputs: T) => InputIssue | undefined;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* A DTO class rules can be written for, abstract parents included: rules written for a parent
|
|
24
|
+
* apply to every DTO that extends it.
|
|
25
|
+
*/
|
|
26
|
+
export type RuleTarget = abstract new () => object;
|
|
27
|
+
/**
|
|
28
|
+
* The rules of one DTO, as `defineRules` records them.
|
|
29
|
+
*/
|
|
30
|
+
export type DtoRules = {
|
|
31
|
+
readonly dto: RuleTarget;
|
|
32
|
+
readonly rules: readonly InputRule<unknown>[];
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Every DTO's rules, by the DTO class.
|
|
36
|
+
*/
|
|
37
|
+
export type RuleBook = ReadonlyMap<RuleTarget, readonly InputRule<unknown>[]>;
|
|
38
|
+
/**
|
|
39
|
+
* Checks each property of the inputs against its constraint: present when required, of its kind,
|
|
40
|
+
* a number that is not NaN, a point with as many coordinates as it should have, a hex color, a
|
|
41
|
+
* value its enum lists. A missing value and `null` are the same to it.
|
|
42
|
+
* @param constraints - The constraints of the DTO the inputs are for
|
|
43
|
+
* @param inputs - The inputs, with the DTO's defaults already laid under them
|
|
44
|
+
* @returns Every property that fails, one issue each
|
|
45
|
+
*/
|
|
46
|
+
export declare function checkStructure(constraints: DtoConstraints, inputs: unknown): InputIssue[];
|
|
47
|
+
/**
|
|
48
|
+
* Collects the rules of several DTOs into one book, looked up by the DTO class.
|
|
49
|
+
* @param entries - What `defineRules` returned for each DTO
|
|
50
|
+
* @returns The rules by DTO class
|
|
51
|
+
*/
|
|
52
|
+
export declare function ruleBook(...entries: readonly DtoRules[]): RuleBook;
|
|
53
|
+
/**
|
|
54
|
+
* Everything wrong with the inputs of a call: each property checked against the constraints the
|
|
55
|
+
* registry lists for its operation, then the DTO's rules whose properties all passed. An operation
|
|
56
|
+
* the registry does not list, or lists without constraints, has nothing to check. An issue a rule
|
|
57
|
+
* found remembers which of the DTO's rules found it, so `reportInputIssues` tells apart two rules on
|
|
58
|
+
* the same property that report the same code.
|
|
59
|
+
* @param registry - A kernel's operations by dotted path
|
|
60
|
+
* @param path - The dotted path of the operation
|
|
61
|
+
* @param inputs - The inputs, with the DTO's defaults already laid under them
|
|
62
|
+
* @param rules - The kernel's rules, when it has any
|
|
63
|
+
* @returns The issues found, empty when the inputs are fine
|
|
64
|
+
*/
|
|
65
|
+
export declare function validateInputs(registry: DtoRegistry, path: string, inputs: unknown, rules?: RuleBook): InputIssue[];
|
|
66
|
+
/**
|
|
67
|
+
* The properties a call passed that its operation's DTO does not have. They are ignored, which is
|
|
68
|
+
* usually a typo - `widht` leaves `width` at its default.
|
|
69
|
+
* @param registry - A kernel's operations by dotted path
|
|
70
|
+
* @param path - The dotted path of the operation
|
|
71
|
+
* @param inputs - What the caller passed
|
|
72
|
+
* @returns The names the DTO does not know, empty when the operation has no constraints
|
|
73
|
+
*/
|
|
74
|
+
export declare function unknownProperties(registry: DtoRegistry, path: string, inputs: unknown): string[];
|
|
75
|
+
/**
|
|
76
|
+
* One issue as the reporter hands it on: which kernel and operation it came from, and whether it is
|
|
77
|
+
* an issue with a value or a property the operation does not know.
|
|
78
|
+
*/
|
|
79
|
+
export type InputIssueReport = {
|
|
80
|
+
readonly kernel: string;
|
|
81
|
+
readonly path: string;
|
|
82
|
+
readonly issue: InputIssue;
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* Where input issues go; by default a console warning. Passing nothing restores the default. The
|
|
86
|
+
* reporter says each distinct issue once, so a configurator that redraws on every change is not
|
|
87
|
+
* flooded.
|
|
88
|
+
* @param next - The function that receives each new issue, or undefined for the console
|
|
89
|
+
*/
|
|
90
|
+
export declare function setInputIssueSink(next?: (report: InputIssueReport) => void): void;
|
|
91
|
+
/**
|
|
92
|
+
* Reports the issues of one call, and the properties it passed that the operation does not know,
|
|
93
|
+
* each distinct one once. An issue is the same one when it comes from the same kernel, operation,
|
|
94
|
+
* property and code - and, for an issue a rule found, from the same rule, whatever values it fired
|
|
95
|
+
* on. The last thousand issues are remembered, so a long session does not grow the memory without
|
|
96
|
+
* bound, and one that fell out of it is reported again when it recurs. Nothing is thrown by the
|
|
97
|
+
* checks: this is how a kernel says what it would reject. The sink is called as it is, so what it
|
|
98
|
+
* throws reaches the caller; a worker calls this through `prepareKernelCall`, which keeps it from
|
|
99
|
+
* failing the call.
|
|
100
|
+
* @param kernel - The kernel the call went to, such as "OCCT"
|
|
101
|
+
* @param path - The dotted path of the operation
|
|
102
|
+
* @param issues - What `validateInputs` found
|
|
103
|
+
* @param unknown - What `unknownProperties` found
|
|
104
|
+
*/
|
|
105
|
+
export declare function reportInputIssues(kernel: string, path: string, issues: readonly InputIssue[], unknown?: readonly string[]): void;
|
|
106
|
+
/**
|
|
107
|
+
* A call on its way to a kernel: its inputs over the defaults of the DTO the operation takes, and a
|
|
108
|
+
* way to report what those inputs would be rejected for.
|
|
109
|
+
*/
|
|
110
|
+
export type PreparedKernelCall = {
|
|
111
|
+
/** The inputs over the DTO's defaults: what the operation receives and what the call is cached under. */
|
|
112
|
+
readonly inputs: unknown;
|
|
113
|
+
/**
|
|
114
|
+
* Reports what `validateInputs` and `unknownProperties` find in the call, each distinct issue once.
|
|
115
|
+
* It never throws - neither a failing check nor a sink that throws fails a call while issues are
|
|
116
|
+
* only reported - so a worker calls it on a cache miss, just before the kernel runs.
|
|
117
|
+
*/
|
|
118
|
+
readonly reportIssues: () => void;
|
|
119
|
+
};
|
|
120
|
+
/**
|
|
121
|
+
* Prepares a call the way every kernel worker does: the inputs are laid over the DTO's defaults now,
|
|
122
|
+
* and the report of what they would be rejected for waits until the call misses the cache.
|
|
123
|
+
* @param kernel - The kernel the call goes to, such as "OCCT"
|
|
124
|
+
* @param registry - The kernel's operations by dotted path
|
|
125
|
+
* @param path - The dotted path of the operation
|
|
126
|
+
* @param given - The inputs as the caller passed them
|
|
127
|
+
* @param rules - The kernel's rules, when it has any
|
|
128
|
+
* @returns The resolved inputs and the report of their issues
|
|
129
|
+
*/
|
|
130
|
+
export declare function prepareKernelCall(kernel: string, registry: DtoRegistry, path: string, given: unknown, rules?: RuleBook): PreparedKernelCall;
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { isRegisteredOperation, resolveInputs } from "./resolve-dto";
|
|
2
|
+
const HEX_COLOR = /^#[0-9a-f]{6}$/i;
|
|
3
|
+
const TUPLE_LENGTH = { point2: 2, vector2: 2, point3: 3, vector3: 3 };
|
|
4
|
+
const isRecord = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
|
|
5
|
+
const issue = (property, code, message, params) => (params ? { property, code, message, params } : { property, code, message });
|
|
6
|
+
function checkBounds(property, value, bounds) {
|
|
7
|
+
const { min, max, exclusiveMin, exclusiveMax } = bounds;
|
|
8
|
+
if (min !== undefined && (exclusiveMin ? value <= min : value < min)) {
|
|
9
|
+
return issue(property, "minimum", `must be ${exclusiveMin ? "above" : "at least"} ${min}`, { limit: min, exclusive: !!exclusiveMin, actual: value });
|
|
10
|
+
}
|
|
11
|
+
if (max !== undefined && (exclusiveMax ? value >= max : value > max)) {
|
|
12
|
+
return issue(property, "maximum", `must be ${exclusiveMax ? "below" : "at most"} ${max}`, { limit: max, exclusive: !!exclusiveMax, actual: value });
|
|
13
|
+
}
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
function checkValue(property, value, constraint) {
|
|
17
|
+
var _a, _b;
|
|
18
|
+
if (value === undefined || value === null) {
|
|
19
|
+
return constraint.required ? issue(property, "required", "is required") : undefined;
|
|
20
|
+
}
|
|
21
|
+
switch (constraint.kind) {
|
|
22
|
+
case "number":
|
|
23
|
+
if (typeof value !== "number")
|
|
24
|
+
return issue(property, "type", "must be a number");
|
|
25
|
+
if (Number.isNaN(value))
|
|
26
|
+
return issue(property, "not-a-number", "is not a number (NaN)");
|
|
27
|
+
return constraint.bounds ? checkBounds(property, value, constraint.bounds) : undefined;
|
|
28
|
+
case "boolean":
|
|
29
|
+
return typeof value === "boolean" ? undefined : issue(property, "type", "must be true or false");
|
|
30
|
+
case "string":
|
|
31
|
+
return typeof value === "string" ? undefined : issue(property, "type", "must be text");
|
|
32
|
+
case "color":
|
|
33
|
+
return typeof value === "string" && HEX_COLOR.test(value) ? undefined : issue(property, "color", "must be a hex color such as #ff0000");
|
|
34
|
+
case "point":
|
|
35
|
+
case "point2":
|
|
36
|
+
case "point3":
|
|
37
|
+
case "vector2":
|
|
38
|
+
case "vector3": {
|
|
39
|
+
const expected = TUPLE_LENGTH[constraint.kind];
|
|
40
|
+
const count = expected === undefined ? "2 or 3" : String(expected);
|
|
41
|
+
if (!Array.isArray(value))
|
|
42
|
+
return issue(property, "type", `must be a list of ${count} numbers`);
|
|
43
|
+
const fits = expected === undefined ? value.length === 2 || value.length === 3 : value.length === expected;
|
|
44
|
+
if (!fits)
|
|
45
|
+
return issue(property, "arity", `must have ${count} numbers, not ${value.length}`, { expected: expected !== null && expected !== void 0 ? expected : [2, 3], actual: value.length });
|
|
46
|
+
return value.every((n) => typeof n === "number" && !Number.isNaN(n)) ? undefined : issue(property, "type", `must be a list of ${count} numbers`);
|
|
47
|
+
}
|
|
48
|
+
case "list": {
|
|
49
|
+
if (!Array.isArray(value) && !ArrayBuffer.isView(value))
|
|
50
|
+
return issue(property, "type", "must be a list");
|
|
51
|
+
if (!constraint.items || !Array.isArray(value))
|
|
52
|
+
return undefined;
|
|
53
|
+
for (let index = 0; index < value.length; index++) {
|
|
54
|
+
const itemIssue = checkValue(property, value[index], Object.assign({ required: true }, constraint.items));
|
|
55
|
+
if (itemIssue)
|
|
56
|
+
return Object.assign(Object.assign({}, itemIssue), { message: `item ${index} ${itemIssue.message}`, params: Object.assign(Object.assign({}, itemIssue.params), { index }) });
|
|
57
|
+
}
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
case "oneOf":
|
|
61
|
+
return ((_a = constraint.values) === null || _a === void 0 ? void 0 : _a.includes(value)) ? undefined : issue(property, "enum", `must be one of ${((_b = constraint.values) !== null && _b !== void 0 ? _b : []).join(", ")}`, { allowed: constraint.values });
|
|
62
|
+
default:
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Checks each property of the inputs against its constraint: present when required, of its kind,
|
|
68
|
+
* a number that is not NaN, a point with as many coordinates as it should have, a hex color, a
|
|
69
|
+
* value its enum lists. A missing value and `null` are the same to it.
|
|
70
|
+
* @param constraints - The constraints of the DTO the inputs are for
|
|
71
|
+
* @param inputs - The inputs, with the DTO's defaults already laid under them
|
|
72
|
+
* @returns Every property that fails, one issue each
|
|
73
|
+
*/
|
|
74
|
+
export function checkStructure(constraints, inputs) {
|
|
75
|
+
const record = isRecord(inputs) ? inputs : {};
|
|
76
|
+
const issues = [];
|
|
77
|
+
for (const [property, constraint] of Object.entries(constraints)) {
|
|
78
|
+
const found = checkValue(property, record[property], constraint);
|
|
79
|
+
if (found)
|
|
80
|
+
issues.push(found);
|
|
81
|
+
}
|
|
82
|
+
return issues;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Collects the rules of several DTOs into one book, looked up by the DTO class.
|
|
86
|
+
* @param entries - What `defineRules` returned for each DTO
|
|
87
|
+
* @returns The rules by DTO class
|
|
88
|
+
*/
|
|
89
|
+
export function ruleBook(...entries) {
|
|
90
|
+
var _a;
|
|
91
|
+
const book = new Map();
|
|
92
|
+
for (const entry of entries) {
|
|
93
|
+
book.set(entry.dto, [...((_a = book.get(entry.dto)) !== null && _a !== void 0 ? _a : []), ...entry.rules]);
|
|
94
|
+
}
|
|
95
|
+
return book;
|
|
96
|
+
}
|
|
97
|
+
const rulesOf = (dto, rules) => {
|
|
98
|
+
var _a;
|
|
99
|
+
const found = [];
|
|
100
|
+
for (let target = dto; rules && typeof target === "function" && target !== Function.prototype; target = Object.getPrototypeOf(target)) {
|
|
101
|
+
found.push(...((_a = rules.get(target)) !== null && _a !== void 0 ? _a : []));
|
|
102
|
+
}
|
|
103
|
+
return found;
|
|
104
|
+
};
|
|
105
|
+
const ruleOfIssue = new WeakMap();
|
|
106
|
+
/**
|
|
107
|
+
* Everything wrong with the inputs of a call: each property checked against the constraints the
|
|
108
|
+
* registry lists for its operation, then the DTO's rules whose properties all passed. An operation
|
|
109
|
+
* the registry does not list, or lists without constraints, has nothing to check. An issue a rule
|
|
110
|
+
* found remembers which of the DTO's rules found it, so `reportInputIssues` tells apart two rules on
|
|
111
|
+
* the same property that report the same code.
|
|
112
|
+
* @param registry - A kernel's operations by dotted path
|
|
113
|
+
* @param path - The dotted path of the operation
|
|
114
|
+
* @param inputs - The inputs, with the DTO's defaults already laid under them
|
|
115
|
+
* @param rules - The kernel's rules, when it has any
|
|
116
|
+
* @returns The issues found, empty when the inputs are fine
|
|
117
|
+
*/
|
|
118
|
+
export function validateInputs(registry, path, inputs, rules) {
|
|
119
|
+
if (!isRegisteredOperation(registry, path)) {
|
|
120
|
+
return [];
|
|
121
|
+
}
|
|
122
|
+
const entry = registry[path];
|
|
123
|
+
if (!entry.constraints) {
|
|
124
|
+
return [];
|
|
125
|
+
}
|
|
126
|
+
const issues = checkStructure(entry.constraints, inputs);
|
|
127
|
+
const failed = new Set(issues.map((found) => found.property));
|
|
128
|
+
rulesOf(entry.dto, rules).forEach((rule, index) => {
|
|
129
|
+
if (rule.reads.some((property) => failed.has(property)))
|
|
130
|
+
return;
|
|
131
|
+
const found = rule.check(inputs);
|
|
132
|
+
if (found) {
|
|
133
|
+
ruleOfIssue.set(found, index);
|
|
134
|
+
issues.push(found);
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
return issues;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* The properties a call passed that its operation's DTO does not have. They are ignored, which is
|
|
141
|
+
* usually a typo - `widht` leaves `width` at its default.
|
|
142
|
+
* @param registry - A kernel's operations by dotted path
|
|
143
|
+
* @param path - The dotted path of the operation
|
|
144
|
+
* @param inputs - What the caller passed
|
|
145
|
+
* @returns The names the DTO does not know, empty when the operation has no constraints
|
|
146
|
+
*/
|
|
147
|
+
export function unknownProperties(registry, path, inputs) {
|
|
148
|
+
const constraints = isRegisteredOperation(registry, path) ? registry[path].constraints : undefined;
|
|
149
|
+
if (!constraints || !isRecord(inputs)) {
|
|
150
|
+
return [];
|
|
151
|
+
}
|
|
152
|
+
return Object.keys(inputs).filter((key) => !Object.prototype.hasOwnProperty.call(constraints, key));
|
|
153
|
+
}
|
|
154
|
+
const defaultSink = (report) => {
|
|
155
|
+
console.warn(`${report.kernel} ${report.path}: ${report.issue.property} ${report.issue.message}`);
|
|
156
|
+
};
|
|
157
|
+
let sink = defaultSink;
|
|
158
|
+
const MAX_REMEMBERED_ISSUES = 1000;
|
|
159
|
+
const reported = new Set();
|
|
160
|
+
/**
|
|
161
|
+
* Where input issues go; by default a console warning. Passing nothing restores the default. The
|
|
162
|
+
* reporter says each distinct issue once, so a configurator that redraws on every change is not
|
|
163
|
+
* flooded.
|
|
164
|
+
* @param next - The function that receives each new issue, or undefined for the console
|
|
165
|
+
*/
|
|
166
|
+
export function setInputIssueSink(next) {
|
|
167
|
+
sink = next !== null && next !== void 0 ? next : defaultSink;
|
|
168
|
+
reported.clear();
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Reports the issues of one call, and the properties it passed that the operation does not know,
|
|
172
|
+
* each distinct one once. An issue is the same one when it comes from the same kernel, operation,
|
|
173
|
+
* property and code - and, for an issue a rule found, from the same rule, whatever values it fired
|
|
174
|
+
* on. The last thousand issues are remembered, so a long session does not grow the memory without
|
|
175
|
+
* bound, and one that fell out of it is reported again when it recurs. Nothing is thrown by the
|
|
176
|
+
* checks: this is how a kernel says what it would reject. The sink is called as it is, so what it
|
|
177
|
+
* throws reaches the caller; a worker calls this through `prepareKernelCall`, which keeps it from
|
|
178
|
+
* failing the call.
|
|
179
|
+
* @param kernel - The kernel the call went to, such as "OCCT"
|
|
180
|
+
* @param path - The dotted path of the operation
|
|
181
|
+
* @param issues - What `validateInputs` found
|
|
182
|
+
* @param unknown - What `unknownProperties` found
|
|
183
|
+
*/
|
|
184
|
+
export function reportInputIssues(kernel, path, issues, unknown = []) {
|
|
185
|
+
const all = [...issues, ...unknown.map((property) => issue(property, "unknown-property", "is not an input of this operation and is ignored"))];
|
|
186
|
+
for (const found of all) {
|
|
187
|
+
const rule = ruleOfIssue.get(found);
|
|
188
|
+
const key = `${kernel}|${path}|${found.property}|${found.code}${rule === undefined ? "" : `|rule ${rule}`}`;
|
|
189
|
+
if (reported.has(key))
|
|
190
|
+
continue;
|
|
191
|
+
reported.add(key);
|
|
192
|
+
if (reported.size > MAX_REMEMBERED_ISSUES) {
|
|
193
|
+
const oldest = reported.values().next();
|
|
194
|
+
if (!oldest.done)
|
|
195
|
+
reported.delete(oldest.value);
|
|
196
|
+
}
|
|
197
|
+
sink({ kernel, path, issue: found });
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Prepares a call the way every kernel worker does: the inputs are laid over the DTO's defaults now,
|
|
202
|
+
* and the report of what they would be rejected for waits until the call misses the cache.
|
|
203
|
+
* @param kernel - The kernel the call goes to, such as "OCCT"
|
|
204
|
+
* @param registry - The kernel's operations by dotted path
|
|
205
|
+
* @param path - The dotted path of the operation
|
|
206
|
+
* @param given - The inputs as the caller passed them
|
|
207
|
+
* @param rules - The kernel's rules, when it has any
|
|
208
|
+
* @returns The resolved inputs and the report of their issues
|
|
209
|
+
*/
|
|
210
|
+
export function prepareKernelCall(kernel, registry, path, given, rules) {
|
|
211
|
+
const inputs = resolveInputs(registry, path, given);
|
|
212
|
+
const reportIssues = () => {
|
|
213
|
+
try {
|
|
214
|
+
reportInputIssues(kernel, path, validateInputs(registry, path, inputs, rules), unknownProperties(registry, path, given));
|
|
215
|
+
}
|
|
216
|
+
catch (_a) {
|
|
217
|
+
// Issues are only reported for now, so a check or a sink that throws must not fail the call.
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
return { inputs, reportIssues };
|
|
221
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The hash a reference carries, or undefined when the value is not a reference.
|
|
3
|
+
*/
|
|
4
|
+
export type ReferenceHash = (value: object) => string | number | undefined;
|
|
5
|
+
/**
|
|
6
|
+
* Replaces every reference in `value` with the object it stands for, at any depth of arrays and
|
|
7
|
+
* plain objects, and returns a new structure: the value passed in is never changed. Binary data is
|
|
8
|
+
* returned as it is, and so is any object that is not a plain object or an array, and any the
|
|
9
|
+
* caller marks `opaque` - a large value known to hold no reference, which is not walked.
|
|
10
|
+
* @param value - The inputs as they arrived
|
|
11
|
+
* @param hashOf - The hash a reference carries, or undefined for any other value
|
|
12
|
+
* @param lookup - The object a hash stands for; it throws when there is none
|
|
13
|
+
* @param opaque - True for an object that is returned as it is without being walked
|
|
14
|
+
* @returns The inputs with every reference replaced
|
|
15
|
+
*/
|
|
16
|
+
export declare function rehydrateReferences(value: unknown, hashOf: ReferenceHash, lookup: (hash: string | number) => unknown, opaque?: (value: object) => boolean): unknown;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
const isPlainObject = (value) => {
|
|
2
|
+
const prototype = Object.getPrototypeOf(value);
|
|
3
|
+
return prototype === Object.prototype || prototype === null;
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* Replaces every reference in `value` with the object it stands for, at any depth of arrays and
|
|
7
|
+
* plain objects, and returns a new structure: the value passed in is never changed. Binary data is
|
|
8
|
+
* returned as it is, and so is any object that is not a plain object or an array, and any the
|
|
9
|
+
* caller marks `opaque` - a large value known to hold no reference, which is not walked.
|
|
10
|
+
* @param value - The inputs as they arrived
|
|
11
|
+
* @param hashOf - The hash a reference carries, or undefined for any other value
|
|
12
|
+
* @param lookup - The object a hash stands for; it throws when there is none
|
|
13
|
+
* @param opaque - True for an object that is returned as it is without being walked
|
|
14
|
+
* @returns The inputs with every reference replaced
|
|
15
|
+
*/
|
|
16
|
+
export function rehydrateReferences(value, hashOf, lookup, opaque) {
|
|
17
|
+
if (value === null || typeof value !== "object") {
|
|
18
|
+
return value;
|
|
19
|
+
}
|
|
20
|
+
if (ArrayBuffer.isView(value) || value instanceof ArrayBuffer) {
|
|
21
|
+
return value;
|
|
22
|
+
}
|
|
23
|
+
const hash = hashOf(value);
|
|
24
|
+
if (hash !== undefined) {
|
|
25
|
+
return lookup(hash);
|
|
26
|
+
}
|
|
27
|
+
if (Array.isArray(value)) {
|
|
28
|
+
return value.map((item) => rehydrateReferences(item, hashOf, lookup, opaque));
|
|
29
|
+
}
|
|
30
|
+
if (!isPlainObject(value) || (opaque === null || opaque === void 0 ? void 0 : opaque(value))) {
|
|
31
|
+
return value;
|
|
32
|
+
}
|
|
33
|
+
const rehydrated = {};
|
|
34
|
+
for (const [key, item] of Object.entries(value)) {
|
|
35
|
+
rehydrated[key] = rehydrateReferences(item, hashOf, lookup, opaque);
|
|
36
|
+
}
|
|
37
|
+
return rehydrated;
|
|
38
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { DtoConstraints } from "./constraints";
|
|
2
|
+
/**
|
|
3
|
+
* A DTO class that can be constructed with no arguments, which gives every defaulted property its
|
|
4
|
+
* default.
|
|
5
|
+
*/
|
|
6
|
+
export type DtoConstructor<T extends object = object> = new () => T;
|
|
7
|
+
/**
|
|
8
|
+
* What an operation takes: its inputs DTO, the properties of that DTO that hold another DTO
|
|
9
|
+
* whose defaults apply too, and what each property accepts. An operation that takes no DTO has none
|
|
10
|
+
* of them.
|
|
11
|
+
*/
|
|
12
|
+
export type DtoEntry = {
|
|
13
|
+
readonly dto?: DtoConstructor;
|
|
14
|
+
readonly nested?: Readonly<Record<string, DtoConstructor>>;
|
|
15
|
+
readonly constraints?: DtoConstraints;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Every public operation of a kernel by its dotted path, and what it takes.
|
|
19
|
+
*/
|
|
20
|
+
export type DtoRegistry = Readonly<Record<string, DtoEntry>>;
|
|
21
|
+
/**
|
|
22
|
+
* A DTO as its defaults leave it: the defaulted keys `K` are present and never undefined, and every
|
|
23
|
+
* other property keeps its declared type.
|
|
24
|
+
*/
|
|
25
|
+
export type WithDefaults<T, K extends keyof T> = Omit<T, K> & {
|
|
26
|
+
[P in K]-?: Exclude<T[P], undefined>;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Builds the DTO a service reads from what a caller passed: the defaults a new `Dto` holds, with the
|
|
30
|
+
* caller's properties laid over them. A property the caller set to undefined keeps its default, and
|
|
31
|
+
* so does one the caller set to null when the DTO has a default for it - a visual editor or a JSON
|
|
32
|
+
* payload spells "not set" as null - while null on a property with no default stays null. A caller
|
|
33
|
+
* that passed nothing gets the defaults alone. The result is a new plain object - a DTO carries data
|
|
34
|
+
* and no behaviour, and a plain object is what crosses a worker boundary and what a walk over the
|
|
35
|
+
* inputs treats as data - and the caller's object is never changed. Properties named in `nested`
|
|
36
|
+
* hold a DTO of their own and get its defaults the same way.
|
|
37
|
+
* @param Dto - The DTO class whose initializers are the defaults
|
|
38
|
+
* @param inputs - What the caller passed, usually an object literal
|
|
39
|
+
* @param nested - The properties that hold a DTO, and its class
|
|
40
|
+
* @returns A new plain object holding every default the caller did not override
|
|
41
|
+
*/
|
|
42
|
+
export declare function resolveDto<T extends object>(Dto: DtoConstructor<T>, inputs: unknown, nested?: Readonly<Record<string, DtoConstructor>>): T;
|
|
43
|
+
/**
|
|
44
|
+
* Whether `path` names an operation the registry lists.
|
|
45
|
+
* @param registry - A kernel's operations by dotted path
|
|
46
|
+
* @param path - The dotted path to look up
|
|
47
|
+
* @returns True when the registry lists the path
|
|
48
|
+
*/
|
|
49
|
+
export declare function isRegisteredOperation(registry: DtoRegistry, path: string): boolean;
|
|
50
|
+
/**
|
|
51
|
+
* The inputs an operation should receive: `inputs` over the defaults of the DTO the registry names
|
|
52
|
+
* for `path`. Inputs to an operation the registry does not list, or that takes no DTO, are returned
|
|
53
|
+
* as they are.
|
|
54
|
+
* @param registry - A kernel's operations by dotted path
|
|
55
|
+
* @param path - The dotted path of the operation
|
|
56
|
+
* @param inputs - What the caller passed
|
|
57
|
+
* @returns The inputs with the DTO's defaults applied
|
|
58
|
+
*/
|
|
59
|
+
export declare function resolveInputs(registry: DtoRegistry, path: string, inputs: unknown): unknown;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
const isRecord = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
|
|
2
|
+
/**
|
|
3
|
+
* Builds the DTO a service reads from what a caller passed: the defaults a new `Dto` holds, with the
|
|
4
|
+
* caller's properties laid over them. A property the caller set to undefined keeps its default, and
|
|
5
|
+
* so does one the caller set to null when the DTO has a default for it - a visual editor or a JSON
|
|
6
|
+
* payload spells "not set" as null - while null on a property with no default stays null. A caller
|
|
7
|
+
* that passed nothing gets the defaults alone. The result is a new plain object - a DTO carries data
|
|
8
|
+
* and no behaviour, and a plain object is what crosses a worker boundary and what a walk over the
|
|
9
|
+
* inputs treats as data - and the caller's object is never changed. Properties named in `nested`
|
|
10
|
+
* hold a DTO of their own and get its defaults the same way.
|
|
11
|
+
* @param Dto - The DTO class whose initializers are the defaults
|
|
12
|
+
* @param inputs - What the caller passed, usually an object literal
|
|
13
|
+
* @param nested - The properties that hold a DTO, and its class
|
|
14
|
+
* @returns A new plain object holding every default the caller did not override
|
|
15
|
+
*/
|
|
16
|
+
export function resolveDto(Dto, inputs, nested) {
|
|
17
|
+
const resolved = {};
|
|
18
|
+
Object.assign(resolved, new Dto());
|
|
19
|
+
const hasDefault = (key) => Object.prototype.hasOwnProperty.call(resolved, key) && resolved[key] !== undefined;
|
|
20
|
+
if (isRecord(inputs)) {
|
|
21
|
+
for (const [key, value] of Object.entries(inputs)) {
|
|
22
|
+
if (value !== undefined && !(value === null && hasDefault(key))) {
|
|
23
|
+
resolved[key] = value;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
if (nested) {
|
|
28
|
+
for (const [key, NestedDto] of Object.entries(nested)) {
|
|
29
|
+
const value = resolved[key];
|
|
30
|
+
if (isRecord(value)) {
|
|
31
|
+
resolved[key] = resolveDto(NestedDto, value);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return resolved;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Whether `path` names an operation the registry lists.
|
|
39
|
+
* @param registry - A kernel's operations by dotted path
|
|
40
|
+
* @param path - The dotted path to look up
|
|
41
|
+
* @returns True when the registry lists the path
|
|
42
|
+
*/
|
|
43
|
+
export function isRegisteredOperation(registry, path) {
|
|
44
|
+
return Object.prototype.hasOwnProperty.call(registry, path);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* The inputs an operation should receive: `inputs` over the defaults of the DTO the registry names
|
|
48
|
+
* for `path`. Inputs to an operation the registry does not list, or that takes no DTO, are returned
|
|
49
|
+
* as they are.
|
|
50
|
+
* @param registry - A kernel's operations by dotted path
|
|
51
|
+
* @param path - The dotted path of the operation
|
|
52
|
+
* @param inputs - What the caller passed
|
|
53
|
+
* @returns The inputs with the DTO's defaults applied
|
|
54
|
+
*/
|
|
55
|
+
export function resolveInputs(registry, path, inputs) {
|
|
56
|
+
if (!isRegisteredOperation(registry, path)) {
|
|
57
|
+
return inputs;
|
|
58
|
+
}
|
|
59
|
+
const entry = registry[path];
|
|
60
|
+
return entry.dto ? resolveDto(entry.dto, inputs, entry.nested) : inputs;
|
|
61
|
+
}
|