@bitbybit-dev/base 1.3.2-rc.1 → 1.4.0-rc.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,36 @@
|
|
|
1
|
+
const UNREACHABLE_SEGMENTS = new Set(["", "__proto__", "prototype", "constructor"]);
|
|
2
|
+
/**
|
|
3
|
+
* Calls the method a dotted path names under `root`, with `inputs` as its one argument:
|
|
4
|
+
* `"shapes.solid.createBox"` calls `root.shapes.solid.createBox(inputs)`, with the owning object as
|
|
5
|
+
* `this`. Any depth is walked.
|
|
6
|
+
* @param root - The object the path starts from
|
|
7
|
+
* @param path - The dotted path of the method
|
|
8
|
+
* @param inputs - The single argument the method receives
|
|
9
|
+
* @returns What the method returns
|
|
10
|
+
* @throws Error when a segment is empty or reaches an object's prototype machinery, when a segment
|
|
11
|
+
* on the way is not an object, or when the last one is not a method
|
|
12
|
+
*/
|
|
13
|
+
export function callByPath(root, path, inputs) {
|
|
14
|
+
const segments = path.split(".");
|
|
15
|
+
const unreachable = segments.find((segment) => UNREACHABLE_SEGMENTS.has(segment));
|
|
16
|
+
if (unreachable !== undefined) {
|
|
17
|
+
throw new Error(`Cannot resolve "${path}": "${unreachable}" is not a segment an operation path may contain`);
|
|
18
|
+
}
|
|
19
|
+
let owner = root;
|
|
20
|
+
let reached = "";
|
|
21
|
+
for (const segment of segments.slice(0, -1)) {
|
|
22
|
+
if (owner === null || typeof owner !== "object") {
|
|
23
|
+
throw new Error(`Cannot resolve "${path}": "${reached}" is not an object`);
|
|
24
|
+
}
|
|
25
|
+
owner = owner[segment];
|
|
26
|
+
reached = reached ? `${reached}.${segment}` : segment;
|
|
27
|
+
}
|
|
28
|
+
if (owner === null || typeof owner !== "object") {
|
|
29
|
+
throw new Error(`Cannot resolve "${path}": "${reached}" is not an object`);
|
|
30
|
+
}
|
|
31
|
+
const method = owner[segments[segments.length - 1]];
|
|
32
|
+
if (typeof method !== "function") {
|
|
33
|
+
throw new Error(`"${path}" is not a function`);
|
|
34
|
+
}
|
|
35
|
+
return method.call(owner, inputs);
|
|
36
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a value has to be for a DTO property to accept it. `point` is a point of either two or three
|
|
3
|
+
* coordinates. `opaque` is anything the table cannot
|
|
4
|
+
* check further - a kernel's shape, a generic value, a typed array - and only its presence is
|
|
5
|
+
* checked, when it is required.
|
|
6
|
+
*/
|
|
7
|
+
export type ValueKind = "number" | "boolean" | "string" | "color" | "point" | "point2" | "point3" | "vector2" | "vector3" | "list" | "oneOf" | "opaque";
|
|
8
|
+
/**
|
|
9
|
+
* The constraint on one DTO property: its kind, whether it has to be there, the kind of the items
|
|
10
|
+
* of a list, and the values a `oneOf` accepts. A list's items have to be there unless their
|
|
11
|
+
* constraint says `required: false` (`optional`), as for a list whose items may be undefined.
|
|
12
|
+
*/
|
|
13
|
+
export type PropertyConstraint = {
|
|
14
|
+
readonly kind: ValueKind;
|
|
15
|
+
readonly required?: boolean;
|
|
16
|
+
readonly items?: PropertyConstraint;
|
|
17
|
+
readonly values?: readonly string[];
|
|
18
|
+
readonly bounds?: NumberBounds;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* The range a number has to fall in. A bound left out is open; an exclusive bound excludes the
|
|
22
|
+
* limit itself, so `{ min: 0, exclusiveMin: true }` asks for a number above 0.
|
|
23
|
+
*/
|
|
24
|
+
export type NumberBounds = {
|
|
25
|
+
readonly min?: number;
|
|
26
|
+
readonly max?: number;
|
|
27
|
+
readonly exclusiveMin?: boolean;
|
|
28
|
+
readonly exclusiveMax?: boolean;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* The constraints on every property of one DTO, by property name.
|
|
32
|
+
*/
|
|
33
|
+
export type DtoConstraints = Readonly<Record<string, PropertyConstraint>>;
|
|
34
|
+
/**
|
|
35
|
+
* The building blocks of a constraint table, as the generated tables spell them.
|
|
36
|
+
*/
|
|
37
|
+
export declare const constraintKinds: {
|
|
38
|
+
number: PropertyConstraint;
|
|
39
|
+
boolean: PropertyConstraint;
|
|
40
|
+
string: PropertyConstraint;
|
|
41
|
+
color: PropertyConstraint;
|
|
42
|
+
point: PropertyConstraint;
|
|
43
|
+
point2: PropertyConstraint;
|
|
44
|
+
point3: PropertyConstraint;
|
|
45
|
+
vector2: PropertyConstraint;
|
|
46
|
+
vector3: PropertyConstraint;
|
|
47
|
+
opaque: PropertyConstraint;
|
|
48
|
+
list: (items: PropertyConstraint) => PropertyConstraint;
|
|
49
|
+
oneOf: (values: readonly string[]) => PropertyConstraint;
|
|
50
|
+
required: (constraint: PropertyConstraint) => PropertyConstraint;
|
|
51
|
+
optional: (constraint: PropertyConstraint) => PropertyConstraint;
|
|
52
|
+
between: (constraint: PropertyConstraint, bounds: NumberBounds) => PropertyConstraint;
|
|
53
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const kind = (value) => ({ kind: value });
|
|
2
|
+
/**
|
|
3
|
+
* The building blocks of a constraint table, as the generated tables spell them.
|
|
4
|
+
*/
|
|
5
|
+
export const constraintKinds = {
|
|
6
|
+
number: kind("number"),
|
|
7
|
+
boolean: kind("boolean"),
|
|
8
|
+
string: kind("string"),
|
|
9
|
+
color: kind("color"),
|
|
10
|
+
point: kind("point"),
|
|
11
|
+
point2: kind("point2"),
|
|
12
|
+
point3: kind("point3"),
|
|
13
|
+
vector2: kind("vector2"),
|
|
14
|
+
vector3: kind("vector3"),
|
|
15
|
+
opaque: kind("opaque"),
|
|
16
|
+
list: (items) => ({ kind: "list", items }),
|
|
17
|
+
oneOf: (values) => ({ kind: "oneOf", values }),
|
|
18
|
+
required: (constraint) => (Object.assign(Object.assign({}, constraint), { required: true })),
|
|
19
|
+
optional: (constraint) => (Object.assign(Object.assign({}, constraint), { required: false })),
|
|
20
|
+
between: (constraint, bounds) => (Object.assign(Object.assign({}, constraint), { bounds })),
|
|
21
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { KernelFailureDetails, KernelFailureKind } from "./errors";
|
|
2
|
+
/**
|
|
3
|
+
* What a kernel worker reports when a call fails: the message a caller reads, whether the inputs
|
|
4
|
+
* or the kernel were at fault, the stable code and the details of a failure the kernel named, and
|
|
5
|
+
* the stack, kept out of the message.
|
|
6
|
+
*/
|
|
7
|
+
export type KernelFailure = {
|
|
8
|
+
message: string;
|
|
9
|
+
kind: KernelFailureKind;
|
|
10
|
+
code: string | undefined;
|
|
11
|
+
details: KernelFailureDetails | undefined;
|
|
12
|
+
stack: string | undefined;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Describes a failed kernel call in one shape for every kernel. An `InputError` reads
|
|
16
|
+
* `<path>: <message>`, because the message already names the input at fault. A WebAssembly trap
|
|
17
|
+
* (`RuntimeError`, such as an out-of-bounds access) is a `crash`: it reads `<kernel> crashed while
|
|
18
|
+
* executing function '<path>': <message>.`, then the inputs. Any other failure
|
|
19
|
+
* reads `<kernel> computation failed while executing function '<path>': <message>.` followed by the
|
|
20
|
+
* inputs; a `KernelOperationError` gives its message without its type's name, its code and its
|
|
21
|
+
* details, when they are a plain record of strings, numbers, booleans and lists of them. A message
|
|
22
|
+
* that already ends a sentence gets no second full stop. The inputs are each cut at 200 characters, with binary data at any depth given only by its kind and size -
|
|
23
|
+
* `[Float32Array length=6000000]` - and a long list or text read only as far as the cut, so a huge
|
|
24
|
+
* input costs no more to describe than a small one. A call that named no function leaves the path
|
|
25
|
+
* out of either form. It never throws: a failure that cannot be described is still reported, as one.
|
|
26
|
+
* @param kernel - The kernel's name as a reader knows it, such as `OCCT`
|
|
27
|
+
* @param functionName - The dotted path that was called
|
|
28
|
+
* @param inputs - The inputs as the call received them
|
|
29
|
+
* @param error - What the call threw
|
|
30
|
+
* @returns The message, the kind of failure and the stack
|
|
31
|
+
*/
|
|
32
|
+
export declare function describeKernelFailure(kernel: string, functionName: string, inputs: unknown, error: unknown): KernelFailure;
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
const MAX_INPUT_TEXT = 200;
|
|
2
|
+
const MAX_ERROR_TEXT = 1000;
|
|
3
|
+
const TRUNCATED = "…(truncated)";
|
|
4
|
+
function cut(text, limit) {
|
|
5
|
+
return text.length > limit ? `${text.slice(0, limit)}${TRUNCATED}` : text;
|
|
6
|
+
}
|
|
7
|
+
function binaryText(value) {
|
|
8
|
+
if (value === null || typeof value !== "object") {
|
|
9
|
+
return undefined;
|
|
10
|
+
}
|
|
11
|
+
const kind = Object.prototype.toString.call(value).slice(8, -1);
|
|
12
|
+
if (ArrayBuffer.isView(value)) {
|
|
13
|
+
return "length" in value && typeof value.length === "number" ? `[${kind} length=${value.length}]` : `[${kind} byteLength=${value.byteLength}]`;
|
|
14
|
+
}
|
|
15
|
+
if ((kind === "ArrayBuffer" || kind === "SharedArrayBuffer") && "byteLength" in value && typeof value.byteLength === "number") {
|
|
16
|
+
return `[${kind} byteLength=${value.byteLength}]`;
|
|
17
|
+
}
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
20
|
+
function boundedJson(value, limit) {
|
|
21
|
+
let written = 0;
|
|
22
|
+
return JSON.stringify(value, (_key, item) => {
|
|
23
|
+
if (written > limit) {
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
const binary = binaryText(item);
|
|
27
|
+
if (binary !== undefined) {
|
|
28
|
+
written += binary.length;
|
|
29
|
+
return binary;
|
|
30
|
+
}
|
|
31
|
+
if (typeof item === "string") {
|
|
32
|
+
written += item.length;
|
|
33
|
+
return item.length > limit ? item.slice(0, limit + 1) : item;
|
|
34
|
+
}
|
|
35
|
+
if (Array.isArray(item)) {
|
|
36
|
+
written += 1;
|
|
37
|
+
return item.length > limit ? item.slice(0, limit + 1) : item;
|
|
38
|
+
}
|
|
39
|
+
if (item !== undefined && typeof item !== "function" && typeof item !== "symbol") {
|
|
40
|
+
written += 1;
|
|
41
|
+
}
|
|
42
|
+
return item;
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
function plainText(value) {
|
|
46
|
+
try {
|
|
47
|
+
return String(value);
|
|
48
|
+
}
|
|
49
|
+
catch (_a) {
|
|
50
|
+
return Object.prototype.toString.call(value);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function errorText(error) {
|
|
54
|
+
if (error instanceof Error) {
|
|
55
|
+
return error.name === "Error" ? error.message : `${error.name}: ${error.message}`;
|
|
56
|
+
}
|
|
57
|
+
if (typeof error === "string") {
|
|
58
|
+
return error;
|
|
59
|
+
}
|
|
60
|
+
try {
|
|
61
|
+
const text = boundedJson(error, MAX_ERROR_TEXT);
|
|
62
|
+
if (text !== undefined) {
|
|
63
|
+
return cut(text, MAX_ERROR_TEXT);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
catch (_a) {
|
|
67
|
+
return plainText(error);
|
|
68
|
+
}
|
|
69
|
+
return plainText(error);
|
|
70
|
+
}
|
|
71
|
+
function inputText(key, value) {
|
|
72
|
+
const binary = binaryText(value);
|
|
73
|
+
if (binary !== undefined) {
|
|
74
|
+
return `${key}: ${binary}`;
|
|
75
|
+
}
|
|
76
|
+
try {
|
|
77
|
+
const text = boundedJson(value, MAX_INPUT_TEXT);
|
|
78
|
+
return text === undefined ? `${key}: undefined` : `${key}: ${cut(text, MAX_INPUT_TEXT)}`;
|
|
79
|
+
}
|
|
80
|
+
catch (_a) {
|
|
81
|
+
return `${key}: [unserializable]`;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/** Whether a thrown value is a WebAssembly trap, which leaves the kernel's memory in an unknown state. */
|
|
85
|
+
function isTrap(error) {
|
|
86
|
+
return error instanceof Error && error.name === "RuntimeError";
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* The code and details of a `KernelOperationError`, whichever copy of the class threw it; undefined
|
|
90
|
+
* for any other error, and for one whose code is not a string.
|
|
91
|
+
*/
|
|
92
|
+
function namedFailure(error) {
|
|
93
|
+
if (!(error instanceof Error) || error.name !== "KernelOperationError") {
|
|
94
|
+
return undefined;
|
|
95
|
+
}
|
|
96
|
+
const code = Reflect.get(error, "code");
|
|
97
|
+
return typeof code === "string" ? { code, details: checkedDetails(Reflect.get(error, "details")) } : undefined;
|
|
98
|
+
}
|
|
99
|
+
function isDetail(value) {
|
|
100
|
+
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
return Array.isArray(value) && (value.every((item) => typeof item === "number") || value.every((item) => typeof item === "string"));
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* A failure's details, when they are a plain record of strings, numbers, booleans and lists of
|
|
107
|
+
* either, which is all that crosses to another thread and fills a template.
|
|
108
|
+
*/
|
|
109
|
+
function checkedDetails(details) {
|
|
110
|
+
if (details === null || typeof details !== "object" || Array.isArray(details)) {
|
|
111
|
+
return undefined;
|
|
112
|
+
}
|
|
113
|
+
const checked = {};
|
|
114
|
+
for (const [name, value] of Object.entries(details)) {
|
|
115
|
+
if (!isDetail(value)) {
|
|
116
|
+
return undefined;
|
|
117
|
+
}
|
|
118
|
+
checked[name] = value;
|
|
119
|
+
}
|
|
120
|
+
return checked;
|
|
121
|
+
}
|
|
122
|
+
/** Ends a sentence with a full stop unless it already ends with one. */
|
|
123
|
+
function sentence(text) {
|
|
124
|
+
return /[.!?]$/.test(text) ? text : `${text}.`;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Describes a failed kernel call in one shape for every kernel. An `InputError` reads
|
|
128
|
+
* `<path>: <message>`, because the message already names the input at fault. A WebAssembly trap
|
|
129
|
+
* (`RuntimeError`, such as an out-of-bounds access) is a `crash`: it reads `<kernel> crashed while
|
|
130
|
+
* executing function '<path>': <message>.`, then the inputs. Any other failure
|
|
131
|
+
* reads `<kernel> computation failed while executing function '<path>': <message>.` followed by the
|
|
132
|
+
* inputs; a `KernelOperationError` gives its message without its type's name, its code and its
|
|
133
|
+
* details, when they are a plain record of strings, numbers, booleans and lists of them. A message
|
|
134
|
+
* that already ends a sentence gets no second full stop. The inputs are each cut at 200 characters, with binary data at any depth given only by its kind and size -
|
|
135
|
+
* `[Float32Array length=6000000]` - and a long list or text read only as far as the cut, so a huge
|
|
136
|
+
* input costs no more to describe than a small one. A call that named no function leaves the path
|
|
137
|
+
* out of either form. It never throws: a failure that cannot be described is still reported, as one.
|
|
138
|
+
* @param kernel - The kernel's name as a reader knows it, such as `OCCT`
|
|
139
|
+
* @param functionName - The dotted path that was called
|
|
140
|
+
* @param inputs - The inputs as the call received them
|
|
141
|
+
* @param error - What the call threw
|
|
142
|
+
* @returns The message, the kind of failure and the stack
|
|
143
|
+
*/
|
|
144
|
+
export function describeKernelFailure(kernel, functionName, inputs, error) {
|
|
145
|
+
try {
|
|
146
|
+
const stack = error instanceof Error ? error.stack : undefined;
|
|
147
|
+
if (error instanceof Error && error.name === "InputError") {
|
|
148
|
+
return { message: functionName ? `${functionName}: ${error.message}` : error.message, kind: "input", code: undefined, details: undefined, stack };
|
|
149
|
+
}
|
|
150
|
+
const where = functionName ? ` while executing function '${functionName}'` : "";
|
|
151
|
+
const entries = inputs !== null && typeof inputs === "object" && binaryText(inputs) === undefined ? Object.entries(inputs) : [];
|
|
152
|
+
const props = entries.length > 0 ? ` Input values were: {${entries.map(([key, value]) => inputText(key, value)).join(", ")}}.` : "";
|
|
153
|
+
if (isTrap(error)) {
|
|
154
|
+
return { message: `${kernel} crashed${where}: ${sentence(errorText(error))}${props}`, kind: "crash", code: undefined, details: undefined, stack };
|
|
155
|
+
}
|
|
156
|
+
const named = namedFailure(error);
|
|
157
|
+
const text = named !== undefined && error instanceof Error ? error.message : errorText(error);
|
|
158
|
+
return { message: `${kernel} computation failed${where}: ${sentence(text)}${props}`, kind: "kernel", code: named === null || named === void 0 ? void 0 : named.code, details: named === null || named === void 0 ? void 0 : named.details, stack };
|
|
159
|
+
}
|
|
160
|
+
catch (_a) {
|
|
161
|
+
const where = typeof functionName === "string" && functionName !== "" ? ` while executing function '${functionName}'` : "";
|
|
162
|
+
return { message: `${kernel} computation failed${where}, and the failure could not be described.`, kind: "kernel", code: undefined, details: undefined, stack: undefined };
|
|
163
|
+
}
|
|
164
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether a kernel call failed on what it was given, inside the kernel itself, by crashing the
|
|
3
|
+
* kernel - a WebAssembly trap such as an out-of-bounds access, after which the kernel's memory can no
|
|
4
|
+
* longer be trusted and every object it held is gone - or was cancelled: stopped on request before it
|
|
5
|
+
* finished, keeping nothing it made.
|
|
6
|
+
*/
|
|
7
|
+
export type KernelFailureKind = "input" | "kernel" | "crash" | "cancelled";
|
|
8
|
+
/**
|
|
9
|
+
* An input the operation cannot accept, thrown before the kernel runs. `property` names the input
|
|
10
|
+
* at fault when one property is.
|
|
11
|
+
*/
|
|
12
|
+
export declare class InputError extends Error {
|
|
13
|
+
/** The input at fault, or undefined when no single input is. */
|
|
14
|
+
readonly property: string | undefined;
|
|
15
|
+
constructor(message: string, property?: string);
|
|
16
|
+
}
|
|
17
|
+
/** A value a failure's message names, such as the numbers of the edges a fillet could not follow. */
|
|
18
|
+
export type KernelFailureDetail = string | number | boolean | readonly string[] | readonly number[];
|
|
19
|
+
/**
|
|
20
|
+
* The values a failure's message names, each under the placeholder it fills in the message's
|
|
21
|
+
* template: `{ edges: [3, 7] }` fills `{edges}`.
|
|
22
|
+
*/
|
|
23
|
+
export type KernelFailureDetails = Readonly<Record<string, KernelFailureDetail>>;
|
|
24
|
+
/**
|
|
25
|
+
* An operation the kernel ran but could not complete, such as a fillet whose radius does not fit
|
|
26
|
+
* the faces around an edge. `code` names the failure the same way in every release and every
|
|
27
|
+
* language, such as `occt.fillet.failedOnEdges`, so a host can translate it; the message says it in
|
|
28
|
+
* English, and `details` carries the values it names, so a translated template can name them too.
|
|
29
|
+
*/
|
|
30
|
+
export declare class KernelOperationError extends Error {
|
|
31
|
+
/** The stable name of the failure, such as `occt.fillet.failedOnEdges`. */
|
|
32
|
+
readonly code: string;
|
|
33
|
+
/** The values the message names, by placeholder, such as `{ edges: [3, 7] }`; unset when it names none. */
|
|
34
|
+
readonly details: KernelFailureDetails | undefined;
|
|
35
|
+
constructor(code: string, message: string, details?: KernelFailureDetails);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The rejection of a call a kernel worker could not complete: the dotted path that was called,
|
|
39
|
+
* whether the inputs or the kernel failed or the kernel crashed, the stable code and the details of a
|
|
40
|
+
* failure the kernel named (see `KernelOperationError`), and the stack the worker reported, kept apart
|
|
41
|
+
* from the message.
|
|
42
|
+
*/
|
|
43
|
+
export declare class KernelCallError extends Error {
|
|
44
|
+
/** The dotted path of the operation that was called. */
|
|
45
|
+
readonly functionName: string;
|
|
46
|
+
/** Whether the inputs or the kernel were at fault, the kernel crashed, or the call was cancelled. */
|
|
47
|
+
readonly kind: KernelFailureKind;
|
|
48
|
+
/** The stack of the failure inside the worker, when it reported one. */
|
|
49
|
+
readonly workerStack: string | undefined;
|
|
50
|
+
/** The stable code of the failure, when the kernel named it, such as `occt.fillet.failed`. */
|
|
51
|
+
readonly code: string | undefined;
|
|
52
|
+
/** The values the named failure's message names, by placeholder, when it names any. */
|
|
53
|
+
readonly details: KernelFailureDetails | undefined;
|
|
54
|
+
constructor(message: string, functionName: string, kind?: KernelFailureKind, workerStack?: string, code?: string, details?: KernelFailureDetails);
|
|
55
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An input the operation cannot accept, thrown before the kernel runs. `property` names the input
|
|
3
|
+
* at fault when one property is.
|
|
4
|
+
*/
|
|
5
|
+
export class InputError extends Error {
|
|
6
|
+
constructor(message, property) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.name = "InputError";
|
|
9
|
+
this.property = property;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* An operation the kernel ran but could not complete, such as a fillet whose radius does not fit
|
|
14
|
+
* the faces around an edge. `code` names the failure the same way in every release and every
|
|
15
|
+
* language, such as `occt.fillet.failedOnEdges`, so a host can translate it; the message says it in
|
|
16
|
+
* English, and `details` carries the values it names, so a translated template can name them too.
|
|
17
|
+
*/
|
|
18
|
+
export class KernelOperationError extends Error {
|
|
19
|
+
constructor(code, message, details) {
|
|
20
|
+
super(message);
|
|
21
|
+
this.name = "KernelOperationError";
|
|
22
|
+
this.code = code;
|
|
23
|
+
this.details = details;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The rejection of a call a kernel worker could not complete: the dotted path that was called,
|
|
28
|
+
* whether the inputs or the kernel failed or the kernel crashed, the stable code and the details of a
|
|
29
|
+
* failure the kernel named (see `KernelOperationError`), and the stack the worker reported, kept apart
|
|
30
|
+
* from the message.
|
|
31
|
+
*/
|
|
32
|
+
export class KernelCallError extends Error {
|
|
33
|
+
constructor(message, functionName, kind = "kernel", workerStack, code, details) {
|
|
34
|
+
super(message);
|
|
35
|
+
this.name = "KernelCallError";
|
|
36
|
+
this.functionName = functionName;
|
|
37
|
+
this.kind = kind;
|
|
38
|
+
this.workerStack = workerStack;
|
|
39
|
+
this.code = code;
|
|
40
|
+
this.details = details;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { KernelFailureDetails } from "./errors";
|
|
2
|
+
/**
|
|
3
|
+
* Writes a list the way English does: `3`, `3 and 7`, `3, 7 and 9`.
|
|
4
|
+
* @param items - The items, already written out
|
|
5
|
+
* @returns The list
|
|
6
|
+
*/
|
|
7
|
+
export declare function englishList(items: readonly string[]): string;
|
|
8
|
+
/**
|
|
9
|
+
* Fills a failure's message template with its details: each `{name}` becomes the detail of that name,
|
|
10
|
+
* a list written by `formatList`. A placeholder with no detail stays as it is, so no text is lost. A
|
|
11
|
+
* host that translates a failure by its code fills its own template with the failure's details, and
|
|
12
|
+
* passes a list formatter for its language, such as `new Intl.ListFormat("de").format`.
|
|
13
|
+
* @param template - The template, such as the translation of a failure's English message
|
|
14
|
+
* @param details - The failure's details, or undefined when it has none
|
|
15
|
+
* @param formatList - Writes a list in the template's language; English by default
|
|
16
|
+
* @returns The message
|
|
17
|
+
*/
|
|
18
|
+
export declare function fillFailureMessage(template: string, details: KernelFailureDetails | undefined, formatList?: (items: readonly string[]) => string): string;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Writes a list the way English does: `3`, `3 and 7`, `3, 7 and 9`.
|
|
3
|
+
* @param items - The items, already written out
|
|
4
|
+
* @returns The list
|
|
5
|
+
*/
|
|
6
|
+
export function englishList(items) {
|
|
7
|
+
if (items.length <= 1) {
|
|
8
|
+
return items.join("");
|
|
9
|
+
}
|
|
10
|
+
return `${items.slice(0, -1).join(", ")} and ${items[items.length - 1]}`;
|
|
11
|
+
}
|
|
12
|
+
function detailText(value, formatList) {
|
|
13
|
+
if (Array.isArray(value)) {
|
|
14
|
+
return formatList(value.map((item) => String(item)));
|
|
15
|
+
}
|
|
16
|
+
return String(value);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Fills a failure's message template with its details: each `{name}` becomes the detail of that name,
|
|
20
|
+
* a list written by `formatList`. A placeholder with no detail stays as it is, so no text is lost. A
|
|
21
|
+
* host that translates a failure by its code fills its own template with the failure's details, and
|
|
22
|
+
* passes a list formatter for its language, such as `new Intl.ListFormat("de").format`.
|
|
23
|
+
* @param template - The template, such as the translation of a failure's English message
|
|
24
|
+
* @param details - The failure's details, or undefined when it has none
|
|
25
|
+
* @param formatList - Writes a list in the template's language; English by default
|
|
26
|
+
* @returns The message
|
|
27
|
+
*/
|
|
28
|
+
export function fillFailureMessage(template, details, formatList = englishList) {
|
|
29
|
+
if (details === undefined) {
|
|
30
|
+
return template;
|
|
31
|
+
}
|
|
32
|
+
return template.replace(/\{([A-Za-z][A-Za-z0-9]*)\}/g, (placeholder, name) => {
|
|
33
|
+
const value = Object.prototype.hasOwnProperty.call(details, name) ? details[name] : undefined;
|
|
34
|
+
return value === undefined ? placeholder : detailText(value, formatList);
|
|
35
|
+
});
|
|
36
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from "./errors";
|
|
2
|
+
export * from "./call-by-path";
|
|
3
|
+
export * from "./describe-failure";
|
|
4
|
+
export * from "./failure-message";
|
|
5
|
+
export * from "./resolve-dto";
|
|
6
|
+
export * from "./constraints";
|
|
7
|
+
export * from "./input-validation";
|
|
8
|
+
export * from "./input-rules";
|
|
9
|
+
export * from "./rehydrate";
|
|
10
|
+
export * from "./with-defaults";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from "./errors";
|
|
2
|
+
export * from "./call-by-path";
|
|
3
|
+
export * from "./describe-failure";
|
|
4
|
+
export * from "./failure-message";
|
|
5
|
+
export * from "./resolve-dto";
|
|
6
|
+
export * from "./constraints";
|
|
7
|
+
export * from "./input-validation";
|
|
8
|
+
export * from "./input-rules";
|
|
9
|
+
export * from "./rehydrate";
|
|
10
|
+
export * from "./with-defaults";
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { DtoRules, InputRule, RuleTarget } from "./input-validation";
|
|
2
|
+
type Key<T> = keyof T & string;
|
|
3
|
+
/**
|
|
4
|
+
* The rules one DTO's inputs have to satisfy together, beyond what each property accepts on its own.
|
|
5
|
+
* They are written against the DTO as its defaults leave it - `Resolved.<Namespace>.<Dto>` - since
|
|
6
|
+
* they run after the defaults are laid under the inputs.
|
|
7
|
+
* @param dto - The DTO class the rules are for; rules for an abstract parent apply to every DTO that
|
|
8
|
+
* extends it
|
|
9
|
+
* @param rules - The rules, built with `custom`, `lessThan`, `sameLength` and the rest
|
|
10
|
+
* @returns The rules, ready for `ruleBook`
|
|
11
|
+
*/
|
|
12
|
+
export declare function defineRules<T>(dto: RuleTarget, rules: readonly InputRule<T>[]): DtoRules;
|
|
13
|
+
/**
|
|
14
|
+
* A rule stated as a test: the inputs pass when `holds` returns true for them.
|
|
15
|
+
* @param property - The property the issue is reported on
|
|
16
|
+
* @param holds - Whether the inputs are fine
|
|
17
|
+
* @param message - What is wrong, said of `property`
|
|
18
|
+
* @param reads - The properties `holds` looks at, when there are others than `property`
|
|
19
|
+
* @returns The rule
|
|
20
|
+
*/
|
|
21
|
+
export declare function custom<T>(property: Key<T>, holds: (inputs: T) => boolean, message: string, reads?: readonly Key<T>[]): InputRule<T>;
|
|
22
|
+
/**
|
|
23
|
+
* `property` has to be less than another property, or than a limit worked out from the inputs.
|
|
24
|
+
* @param property - The property that has to be smaller
|
|
25
|
+
* @param limit - The property it is compared with, or a function giving the limit
|
|
26
|
+
* @param message - What is wrong, said of `property`; by default "must be less than" the other property
|
|
27
|
+
* @param reads - The properties a limit function looks at
|
|
28
|
+
* @returns The rule
|
|
29
|
+
*/
|
|
30
|
+
export declare function lessThan<T>(property: Key<T>, limit: Key<T> | ((inputs: T) => number), message?: string, reads?: readonly Key<T>[]): InputRule<T>;
|
|
31
|
+
/**
|
|
32
|
+
* Two lists that pair up by position must have the same length. A list left out is not compared.
|
|
33
|
+
* @param property - The list the issue is reported on
|
|
34
|
+
* @param other - The list it pairs with
|
|
35
|
+
* @returns The rule
|
|
36
|
+
*/
|
|
37
|
+
export declare function sameLength<T>(property: Key<T>, other: Key<T>): InputRule<T>;
|
|
38
|
+
/**
|
|
39
|
+
* Two points have to differ.
|
|
40
|
+
* @param property - The point the issue is reported on
|
|
41
|
+
* @param other - The point it must differ from
|
|
42
|
+
* @returns The rule
|
|
43
|
+
*/
|
|
44
|
+
export declare function distinct<T>(property: Key<T>, other: Key<T>): InputRule<T>;
|
|
45
|
+
/**
|
|
46
|
+
* A direction has to have a length: a vector of zeros points nowhere.
|
|
47
|
+
* @param property - The vector
|
|
48
|
+
* @returns The rule
|
|
49
|
+
*/
|
|
50
|
+
export declare function notZeroVector<T>(property: Key<T>): InputRule<T>;
|
|
51
|
+
/**
|
|
52
|
+
* At least one item of a list has to pass a test.
|
|
53
|
+
* @param property - The list
|
|
54
|
+
* @param holds - The test an item has to pass
|
|
55
|
+
* @param message - What is wrong, said of `property`
|
|
56
|
+
* @returns The rule
|
|
57
|
+
*/
|
|
58
|
+
export declare function atLeastOne<T>(property: Key<T>, holds: (item: unknown) => boolean, message: string): InputRule<T>;
|
|
59
|
+
/**
|
|
60
|
+
* A rule that applies only when a condition holds, such as a list that has to match another only
|
|
61
|
+
* when it is given.
|
|
62
|
+
* @param condition - When the rule applies
|
|
63
|
+
* @param rule - The rule
|
|
64
|
+
* @returns The rule, checked only when the condition holds
|
|
65
|
+
*/
|
|
66
|
+
export declare function when<T>(condition: (inputs: T) => boolean, rule: InputRule<T>): InputRule<T>;
|
|
67
|
+
export {};
|