@abyss-project/runtime 3.11.0 → 3.13.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/.eslintrc.js +32 -32
- package/dist/definition/definition.schema.js +6 -0
- package/dist/definition/definition.schema.js.map +1 -1
- package/dist/definition/definition.types.d.ts +11 -0
- package/dist/definition/definition.types.js +6 -1
- package/dist/definition/definition.types.js.map +1 -1
- package/dist/engine/engine-source.js +1 -1
- package/dist/errors/runtime-error-code.enum.d.ts +1 -0
- package/dist/errors/runtime-error-code.enum.js +1 -0
- package/dist/errors/runtime-error-code.enum.js.map +1 -1
- package/dist/runner-protocol/evaluation.types.d.ts +1 -0
- package/dist/steps/global-references.js +7 -0
- package/dist/steps/global-references.js.map +1 -1
- package/dist/steps/step-fields.js +10 -0
- package/dist/steps/step-fields.js.map +1 -1
- package/jest.config.ts +11 -11
- package/package.json +1 -1
- package/scripts/build-engine.cjs +70 -70
- package/src/__tests__/engine-bundle.test.ts +121 -121
- package/src/__tests__/surface.test.ts +81 -81
- package/src/access/__tests__/host-match.test.ts +24 -24
- package/src/access/access.types.ts +25 -25
- package/src/access/host-match.ts +13 -13
- package/src/cost/__tests__/execution-cost.test.ts +249 -249
- package/src/cost/execution-cost.ts +203 -203
- package/src/definition/__tests__/code-step.test.ts +56 -0
- package/src/definition/__tests__/validate-definition.test.ts +354 -354
- package/src/definition/definition.limits.ts +9 -9
- package/src/definition/definition.schema.ts +7 -0
- package/src/definition/definition.types.ts +21 -1
- package/src/definition/validate-definition.ts +206 -206
- package/src/definition/walk-steps.ts +44 -44
- package/src/discord/__tests__/discord-event-input.test.ts +90 -90
- package/src/discord/discord.types.ts +696 -696
- package/src/engine/__tests__/run-in-isolate.test.ts +519 -519
- package/src/engine/engine-entry.ts +3 -3
- package/src/engine/engine-global.ts +2 -2
- package/src/engine/run-in-isolate.ts +462 -462
- package/src/errors/runtime-error-code.enum.ts +5 -0
- package/src/index.ts +32 -32
- package/src/interpreter/__tests__/interpreter.test.ts +854 -854
- package/src/interpreter/__tests__/log-respond-resume.test.ts +271 -271
- package/src/interpreter/interpreter-error.ts +12 -12
- package/src/interpreter/interpreter.ts +483 -483
- package/src/interpreter/interpreter.types.ts +204 -204
- package/src/interpreter/sequence-path.ts +67 -67
- package/src/interpreter/test-host.ts +85 -85
- package/src/runner-protocol/__tests__/file-payload.test.ts +26 -26
- package/src/runner-protocol/evaluation.types.ts +2 -0
- package/src/runner-protocol/execution-protocol.types.ts +297 -297
- package/src/schedule/schedule.types.ts +100 -100
- package/src/server/__tests__/code-runner-session.test.ts +311 -311
- package/src/server/code-runner-error.ts +12 -12
- package/src/server/code-runner-session.ts +310 -310
- package/src/server/index.ts +11 -11
- package/src/steps/__tests__/global-references.test.ts +140 -124
- package/src/steps/__tests__/step-fields.test.ts +58 -0
- package/src/steps/execute-step.ts +186 -186
- package/src/steps/global-references.ts +56 -48
- package/src/steps/step-access.ts +56 -56
- package/src/steps/step-fields.ts +10 -0
- package/src/steps/value-source.ts +69 -69
- package/tsconfig.check.json +15 -15
- package/tsconfig.json +35 -35
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
// Bounds of the definition format, without Joi: the interpreter and the isolate engine bundle
|
|
2
|
-
// read them, and neither may pull Joi in (see `engine/run-in-isolate.ts`).
|
|
3
|
-
|
|
4
|
-
export const MAX_HTTP_TIMEOUT_MS = 30_000;
|
|
5
|
-
export const MAX_WAIT_DURATION_MS = 30 * 24 * 60 * 60 * 1000;
|
|
6
|
-
|
|
7
|
-
/** HTTP status range a RESPOND step may send. */
|
|
8
|
-
export const MIN_RESPONSE_STATUS = 200;
|
|
9
|
-
export const MAX_RESPONSE_STATUS = 599;
|
|
1
|
+
// Bounds of the definition format, without Joi: the interpreter and the isolate engine bundle
|
|
2
|
+
// read them, and neither may pull Joi in (see `engine/run-in-isolate.ts`).
|
|
3
|
+
|
|
4
|
+
export const MAX_HTTP_TIMEOUT_MS = 30_000;
|
|
5
|
+
export const MAX_WAIT_DURATION_MS = 30 * 24 * 60 * 60 * 1000;
|
|
6
|
+
|
|
7
|
+
/** HTTP status range a RESPOND step may send. */
|
|
8
|
+
export const MIN_RESPONSE_STATUS = 200;
|
|
9
|
+
export const MAX_RESPONSE_STATUS = 599;
|
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
MIN_RESPONSE_STATUS,
|
|
8
8
|
} from './definition.limits';
|
|
9
9
|
import {
|
|
10
|
+
CodeLanguage,
|
|
10
11
|
CURRENT_FORMAT_VERSION,
|
|
11
12
|
HttpMethod,
|
|
12
13
|
LogLevel,
|
|
@@ -116,6 +117,12 @@ export const STEP_SCHEMAS: Record<StepType, Joi.ObjectSchema> = {
|
|
|
116
117
|
...stepBase,
|
|
117
118
|
type: Joi.valid(StepType.CODE).required(),
|
|
118
119
|
source: Joi.string().min(1).required(),
|
|
120
|
+
language: Joi.valid(...Object.values(CodeLanguage)),
|
|
121
|
+
compiled: Joi.object({
|
|
122
|
+
code: Joi.string().min(1).required(),
|
|
123
|
+
sourceMap: Joi.string().min(1).required(),
|
|
124
|
+
toolchain: Joi.string().min(1).required(),
|
|
125
|
+
}),
|
|
119
126
|
}),
|
|
120
127
|
[StepType.HTTP]: Joi.object({
|
|
121
128
|
...stepBase,
|
|
@@ -93,10 +93,30 @@ export interface SetStep extends StepBase {
|
|
|
93
93
|
value: ValueSource;
|
|
94
94
|
}
|
|
95
95
|
|
|
96
|
-
/**
|
|
96
|
+
/** Language a CODE step's `source` is written in. */
|
|
97
|
+
export enum CodeLanguage {
|
|
98
|
+
JAVASCRIPT = 'JAVASCRIPT',
|
|
99
|
+
TYPESCRIPT = 'TYPESCRIPT',
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** The JavaScript a host produced from a CODE step's `source`, with the map back to it. */
|
|
103
|
+
export interface ICompiledCode {
|
|
104
|
+
/** JavaScript statements evaluated in place of `source`. */
|
|
105
|
+
code: string;
|
|
106
|
+
/** Source map v3 (JSON) of `code` back to `source`. */
|
|
107
|
+
sourceMap: string;
|
|
108
|
+
/** Name and version of the compiler that produced `code`. */
|
|
109
|
+
toolchain: string;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Statements; the value of their `return` becomes the step's output. */
|
|
97
113
|
export interface CodeStep extends StepBase {
|
|
98
114
|
type: StepType.CODE;
|
|
99
115
|
source: string;
|
|
116
|
+
/** Language of `source`; absent means JAVASCRIPT. */
|
|
117
|
+
language?: CodeLanguage;
|
|
118
|
+
/** Set by the host on a frozen version only; evaluated instead of `source` when present. */
|
|
119
|
+
compiled?: ICompiledCode;
|
|
100
120
|
}
|
|
101
121
|
|
|
102
122
|
export interface HttpStep extends StepBase {
|
|
@@ -1,206 +1,206 @@
|
|
|
1
|
-
import type Joi from 'joi';
|
|
2
|
-
import { AccessKind } from '../access/access.types';
|
|
3
|
-
import type { AccessDeclaration } from '../access/access.types';
|
|
4
|
-
import { hostMatches } from '../access/host-match';
|
|
5
|
-
import { ACCESS_SCHEMAS, DEFINITION_SCHEMA, STEP_SCHEMAS } from './definition.schema';
|
|
6
|
-
import { CURRENT_FORMAT_VERSION, StepType } from './definition.types';
|
|
7
|
-
import type { DefinitionError, HostActionCatalog, RuntimeDefinition } from './definition.types';
|
|
8
|
-
import { walkRawSteps } from './walk-steps';
|
|
9
|
-
|
|
10
|
-
export type ValidationResult =
|
|
11
|
-
| { ok: true; definition: RuntimeDefinition }
|
|
12
|
-
| { ok: false; errors: DefinitionError[] };
|
|
13
|
-
|
|
14
|
-
const JOI_OPTIONS: Joi.ValidationOptions = { abortEarly: false, convert: false };
|
|
15
|
-
|
|
16
|
-
const isRecord = (value: unknown): value is Record<string, unknown> =>
|
|
17
|
-
typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
18
|
-
|
|
19
|
-
const joinPath = (base: string, segments: ReadonlyArray<string | number>): string =>
|
|
20
|
-
segments.reduce<string>((path, segment) => {
|
|
21
|
-
if (typeof segment === 'number') {
|
|
22
|
-
return `${path}[${segment}]`;
|
|
23
|
-
}
|
|
24
|
-
return path === '' ? segment : `${path}.${segment}`;
|
|
25
|
-
}, base);
|
|
26
|
-
|
|
27
|
-
const joiErrors = (
|
|
28
|
-
schema: Joi.Schema,
|
|
29
|
-
value: unknown,
|
|
30
|
-
basePath: string,
|
|
31
|
-
errors: DefinitionError[],
|
|
32
|
-
): void => {
|
|
33
|
-
const { error } = schema.validate(value, JOI_OPTIONS);
|
|
34
|
-
error?.details.forEach((detail) =>
|
|
35
|
-
errors.push({ path: joinPath(basePath, detail.path), message: detail.message }),
|
|
36
|
-
);
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
const isAccessKind = (value: unknown): value is AccessKind =>
|
|
40
|
-
Object.values(AccessKind).includes(value as AccessKind);
|
|
41
|
-
|
|
42
|
-
const isStepType = (value: unknown): value is StepType =>
|
|
43
|
-
Object.values(StepType).includes(value as StepType);
|
|
44
|
-
|
|
45
|
-
/** Literal keys of an action's input schema, or `null` when it accepts any key. */
|
|
46
|
-
const allowedInputKeys = (catalog: HostActionCatalog, action: string): Set<string> | null => {
|
|
47
|
-
const description = catalog.get(action)?.inputSchema.describe() as
|
|
48
|
-
| { keys?: Record<string, unknown>; flags?: { unknown?: boolean }; patterns?: unknown }
|
|
49
|
-
| undefined;
|
|
50
|
-
if (!description?.keys || description.flags?.unknown || description.patterns) {
|
|
51
|
-
return null;
|
|
52
|
-
}
|
|
53
|
-
return new Set(Object.keys(description.keys));
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
const checkAccesses = (accesses: unknown, errors: DefinitionError[]): AccessDeclaration[] => {
|
|
57
|
-
if (!Array.isArray(accesses)) {
|
|
58
|
-
return [];
|
|
59
|
-
}
|
|
60
|
-
const valid: AccessDeclaration[] = [];
|
|
61
|
-
accesses.forEach((access, index) => {
|
|
62
|
-
const path = `accesses[${index}]`;
|
|
63
|
-
const kind = isRecord(access) ? access.kind : undefined;
|
|
64
|
-
if (!isAccessKind(kind)) {
|
|
65
|
-
errors.push({ path: `${path}.kind`, message: '"kind" must be one of [HTTP, HOST_ACTION]' });
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
const before = errors.length;
|
|
69
|
-
joiErrors(ACCESS_SCHEMAS[kind], access, path, errors);
|
|
70
|
-
if (errors.length === before) {
|
|
71
|
-
valid.push(access as AccessDeclaration);
|
|
72
|
-
}
|
|
73
|
-
});
|
|
74
|
-
return valid;
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
interface StepChecks {
|
|
78
|
-
catalog: HostActionCatalog;
|
|
79
|
-
accesses: AccessDeclaration[];
|
|
80
|
-
errors: DefinitionError[];
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
const checkHttpAccess = (step: Record<string, unknown>, path: string, checks: StepChecks): void => {
|
|
84
|
-
const httpAccesses = checks.accesses.filter((access) => access.kind === AccessKind.HTTP);
|
|
85
|
-
if (httpAccesses.length === 0) {
|
|
86
|
-
checks.errors.push({
|
|
87
|
-
path,
|
|
88
|
-
message: 'an HTTP step requires an access declaration of kind HTTP',
|
|
89
|
-
});
|
|
90
|
-
return;
|
|
91
|
-
}
|
|
92
|
-
const { url } = step;
|
|
93
|
-
if (typeof url !== 'string' || url.includes('{{')) {
|
|
94
|
-
return;
|
|
95
|
-
}
|
|
96
|
-
let parsed: URL;
|
|
97
|
-
try {
|
|
98
|
-
parsed = new URL(url);
|
|
99
|
-
} catch {
|
|
100
|
-
checks.errors.push({ path: `${path}.url`, message: '"url" must be an absolute http(s) URL' });
|
|
101
|
-
return;
|
|
102
|
-
}
|
|
103
|
-
if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
|
|
104
|
-
checks.errors.push({ path: `${path}.url`, message: '"url" must be an absolute http(s) URL' });
|
|
105
|
-
return;
|
|
106
|
-
}
|
|
107
|
-
const covered = httpAccesses.some(
|
|
108
|
-
(access) =>
|
|
109
|
-
access.kind === AccessKind.HTTP &&
|
|
110
|
-
access.hosts.some((pattern) => hostMatches(parsed.hostname, pattern)),
|
|
111
|
-
);
|
|
112
|
-
if (!covered) {
|
|
113
|
-
checks.errors.push({
|
|
114
|
-
path,
|
|
115
|
-
message: `host "${parsed.hostname}" is not covered by any HTTP access declaration`,
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
const checkHost = (step: Record<string, unknown>, path: string, checks: StepChecks): void => {
|
|
121
|
-
const { action, input } = step;
|
|
122
|
-
if (typeof action !== 'string' || action === '') {
|
|
123
|
-
return;
|
|
124
|
-
}
|
|
125
|
-
if (!checks.catalog.has(action)) {
|
|
126
|
-
checks.errors.push({
|
|
127
|
-
path: `${path}.action`,
|
|
128
|
-
message: `action "${action}" is not in the host catalog`,
|
|
129
|
-
});
|
|
130
|
-
} else if (isRecord(input)) {
|
|
131
|
-
const allowed = allowedInputKeys(checks.catalog, action);
|
|
132
|
-
if (allowed) {
|
|
133
|
-
Object.keys(input)
|
|
134
|
-
.filter((key) => !allowed.has(key))
|
|
135
|
-
.forEach((key) =>
|
|
136
|
-
checks.errors.push({
|
|
137
|
-
path: `${path}.input.${key}`,
|
|
138
|
-
message: `"${key}" is not an input of action "${action}"`,
|
|
139
|
-
}),
|
|
140
|
-
);
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
const declared = checks.accesses.some(
|
|
144
|
-
(access) => access.kind === AccessKind.HOST_ACTION && access.action === action,
|
|
145
|
-
);
|
|
146
|
-
if (!declared) {
|
|
147
|
-
checks.errors.push({
|
|
148
|
-
path,
|
|
149
|
-
message: `action "${action}" requires an access declaration of kind HOST_ACTION`,
|
|
150
|
-
});
|
|
151
|
-
}
|
|
152
|
-
};
|
|
153
|
-
|
|
154
|
-
const checkStep = (node: unknown, path: string, ids: Set<string>, checks: StepChecks): void => {
|
|
155
|
-
if (!isRecord(node)) {
|
|
156
|
-
checks.errors.push({ path, message: 'a step must be an object' });
|
|
157
|
-
return;
|
|
158
|
-
}
|
|
159
|
-
if (!isStepType(node.type)) {
|
|
160
|
-
checks.errors.push({
|
|
161
|
-
path: `${path}.type`,
|
|
162
|
-
message: `"type" must be one of [${Object.values(StepType).join(', ')}]`,
|
|
163
|
-
});
|
|
164
|
-
return;
|
|
165
|
-
}
|
|
166
|
-
joiErrors(STEP_SCHEMAS[node.type], node, path, checks.errors);
|
|
167
|
-
if (typeof node.id === 'string' && node.id !== '') {
|
|
168
|
-
if (ids.has(node.id)) {
|
|
169
|
-
checks.errors.push({ path: `${path}.id`, message: `id "${node.id}" is already used` });
|
|
170
|
-
}
|
|
171
|
-
ids.add(node.id);
|
|
172
|
-
}
|
|
173
|
-
if (node.type === StepType.HTTP) {
|
|
174
|
-
checkHttpAccess(node, path, checks);
|
|
175
|
-
} else if (node.type === StepType.HOST) {
|
|
176
|
-
checkHost(node, path, checks);
|
|
177
|
-
}
|
|
178
|
-
};
|
|
179
|
-
|
|
180
|
-
/**
|
|
181
|
-
* Checks a raw document against the format and against the product's host catalog. Every
|
|
182
|
-
* error is collected, each with the JSON path of its node. An unknown `formatVersion` is
|
|
183
|
-
* reported alone: no other rule is meaningful for a format we do not know.
|
|
184
|
-
*/
|
|
185
|
-
export const validateDefinition = (raw: unknown, catalog: HostActionCatalog): ValidationResult => {
|
|
186
|
-
if (!isRecord(raw)) {
|
|
187
|
-
return { ok: false, errors: [{ path: '', message: 'a definition must be an object' }] };
|
|
188
|
-
}
|
|
189
|
-
if (raw.formatVersion !== CURRENT_FORMAT_VERSION) {
|
|
190
|
-
return {
|
|
191
|
-
ok: false,
|
|
192
|
-
errors: [
|
|
193
|
-
{ path: 'formatVersion', message: `"formatVersion" must be ${CURRENT_FORMAT_VERSION}` },
|
|
194
|
-
],
|
|
195
|
-
};
|
|
196
|
-
}
|
|
197
|
-
const errors: DefinitionError[] = [];
|
|
198
|
-
joiErrors(DEFINITION_SCHEMA, raw, '', errors);
|
|
199
|
-
const accesses = checkAccesses(raw.accesses, errors);
|
|
200
|
-
const ids = new Set<string>();
|
|
201
|
-
const checks: StepChecks = { catalog, accesses, errors };
|
|
202
|
-
walkRawSteps(raw.steps, (node, path) => checkStep(node, path, ids, checks));
|
|
203
|
-
return errors.length === 0
|
|
204
|
-
? { ok: true, definition: raw as unknown as RuntimeDefinition }
|
|
205
|
-
: { ok: false, errors };
|
|
206
|
-
};
|
|
1
|
+
import type Joi from 'joi';
|
|
2
|
+
import { AccessKind } from '../access/access.types';
|
|
3
|
+
import type { AccessDeclaration } from '../access/access.types';
|
|
4
|
+
import { hostMatches } from '../access/host-match';
|
|
5
|
+
import { ACCESS_SCHEMAS, DEFINITION_SCHEMA, STEP_SCHEMAS } from './definition.schema';
|
|
6
|
+
import { CURRENT_FORMAT_VERSION, StepType } from './definition.types';
|
|
7
|
+
import type { DefinitionError, HostActionCatalog, RuntimeDefinition } from './definition.types';
|
|
8
|
+
import { walkRawSteps } from './walk-steps';
|
|
9
|
+
|
|
10
|
+
export type ValidationResult =
|
|
11
|
+
| { ok: true; definition: RuntimeDefinition }
|
|
12
|
+
| { ok: false; errors: DefinitionError[] };
|
|
13
|
+
|
|
14
|
+
const JOI_OPTIONS: Joi.ValidationOptions = { abortEarly: false, convert: false };
|
|
15
|
+
|
|
16
|
+
const isRecord = (value: unknown): value is Record<string, unknown> =>
|
|
17
|
+
typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
18
|
+
|
|
19
|
+
const joinPath = (base: string, segments: ReadonlyArray<string | number>): string =>
|
|
20
|
+
segments.reduce<string>((path, segment) => {
|
|
21
|
+
if (typeof segment === 'number') {
|
|
22
|
+
return `${path}[${segment}]`;
|
|
23
|
+
}
|
|
24
|
+
return path === '' ? segment : `${path}.${segment}`;
|
|
25
|
+
}, base);
|
|
26
|
+
|
|
27
|
+
const joiErrors = (
|
|
28
|
+
schema: Joi.Schema,
|
|
29
|
+
value: unknown,
|
|
30
|
+
basePath: string,
|
|
31
|
+
errors: DefinitionError[],
|
|
32
|
+
): void => {
|
|
33
|
+
const { error } = schema.validate(value, JOI_OPTIONS);
|
|
34
|
+
error?.details.forEach((detail) =>
|
|
35
|
+
errors.push({ path: joinPath(basePath, detail.path), message: detail.message }),
|
|
36
|
+
);
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const isAccessKind = (value: unknown): value is AccessKind =>
|
|
40
|
+
Object.values(AccessKind).includes(value as AccessKind);
|
|
41
|
+
|
|
42
|
+
const isStepType = (value: unknown): value is StepType =>
|
|
43
|
+
Object.values(StepType).includes(value as StepType);
|
|
44
|
+
|
|
45
|
+
/** Literal keys of an action's input schema, or `null` when it accepts any key. */
|
|
46
|
+
const allowedInputKeys = (catalog: HostActionCatalog, action: string): Set<string> | null => {
|
|
47
|
+
const description = catalog.get(action)?.inputSchema.describe() as
|
|
48
|
+
| { keys?: Record<string, unknown>; flags?: { unknown?: boolean }; patterns?: unknown }
|
|
49
|
+
| undefined;
|
|
50
|
+
if (!description?.keys || description.flags?.unknown || description.patterns) {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
return new Set(Object.keys(description.keys));
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const checkAccesses = (accesses: unknown, errors: DefinitionError[]): AccessDeclaration[] => {
|
|
57
|
+
if (!Array.isArray(accesses)) {
|
|
58
|
+
return [];
|
|
59
|
+
}
|
|
60
|
+
const valid: AccessDeclaration[] = [];
|
|
61
|
+
accesses.forEach((access, index) => {
|
|
62
|
+
const path = `accesses[${index}]`;
|
|
63
|
+
const kind = isRecord(access) ? access.kind : undefined;
|
|
64
|
+
if (!isAccessKind(kind)) {
|
|
65
|
+
errors.push({ path: `${path}.kind`, message: '"kind" must be one of [HTTP, HOST_ACTION]' });
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
const before = errors.length;
|
|
69
|
+
joiErrors(ACCESS_SCHEMAS[kind], access, path, errors);
|
|
70
|
+
if (errors.length === before) {
|
|
71
|
+
valid.push(access as AccessDeclaration);
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
return valid;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
interface StepChecks {
|
|
78
|
+
catalog: HostActionCatalog;
|
|
79
|
+
accesses: AccessDeclaration[];
|
|
80
|
+
errors: DefinitionError[];
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const checkHttpAccess = (step: Record<string, unknown>, path: string, checks: StepChecks): void => {
|
|
84
|
+
const httpAccesses = checks.accesses.filter((access) => access.kind === AccessKind.HTTP);
|
|
85
|
+
if (httpAccesses.length === 0) {
|
|
86
|
+
checks.errors.push({
|
|
87
|
+
path,
|
|
88
|
+
message: 'an HTTP step requires an access declaration of kind HTTP',
|
|
89
|
+
});
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
const { url } = step;
|
|
93
|
+
if (typeof url !== 'string' || url.includes('{{')) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
let parsed: URL;
|
|
97
|
+
try {
|
|
98
|
+
parsed = new URL(url);
|
|
99
|
+
} catch {
|
|
100
|
+
checks.errors.push({ path: `${path}.url`, message: '"url" must be an absolute http(s) URL' });
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
|
|
104
|
+
checks.errors.push({ path: `${path}.url`, message: '"url" must be an absolute http(s) URL' });
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
const covered = httpAccesses.some(
|
|
108
|
+
(access) =>
|
|
109
|
+
access.kind === AccessKind.HTTP &&
|
|
110
|
+
access.hosts.some((pattern) => hostMatches(parsed.hostname, pattern)),
|
|
111
|
+
);
|
|
112
|
+
if (!covered) {
|
|
113
|
+
checks.errors.push({
|
|
114
|
+
path,
|
|
115
|
+
message: `host "${parsed.hostname}" is not covered by any HTTP access declaration`,
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
const checkHost = (step: Record<string, unknown>, path: string, checks: StepChecks): void => {
|
|
121
|
+
const { action, input } = step;
|
|
122
|
+
if (typeof action !== 'string' || action === '') {
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
if (!checks.catalog.has(action)) {
|
|
126
|
+
checks.errors.push({
|
|
127
|
+
path: `${path}.action`,
|
|
128
|
+
message: `action "${action}" is not in the host catalog`,
|
|
129
|
+
});
|
|
130
|
+
} else if (isRecord(input)) {
|
|
131
|
+
const allowed = allowedInputKeys(checks.catalog, action);
|
|
132
|
+
if (allowed) {
|
|
133
|
+
Object.keys(input)
|
|
134
|
+
.filter((key) => !allowed.has(key))
|
|
135
|
+
.forEach((key) =>
|
|
136
|
+
checks.errors.push({
|
|
137
|
+
path: `${path}.input.${key}`,
|
|
138
|
+
message: `"${key}" is not an input of action "${action}"`,
|
|
139
|
+
}),
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
const declared = checks.accesses.some(
|
|
144
|
+
(access) => access.kind === AccessKind.HOST_ACTION && access.action === action,
|
|
145
|
+
);
|
|
146
|
+
if (!declared) {
|
|
147
|
+
checks.errors.push({
|
|
148
|
+
path,
|
|
149
|
+
message: `action "${action}" requires an access declaration of kind HOST_ACTION`,
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
const checkStep = (node: unknown, path: string, ids: Set<string>, checks: StepChecks): void => {
|
|
155
|
+
if (!isRecord(node)) {
|
|
156
|
+
checks.errors.push({ path, message: 'a step must be an object' });
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
if (!isStepType(node.type)) {
|
|
160
|
+
checks.errors.push({
|
|
161
|
+
path: `${path}.type`,
|
|
162
|
+
message: `"type" must be one of [${Object.values(StepType).join(', ')}]`,
|
|
163
|
+
});
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
joiErrors(STEP_SCHEMAS[node.type], node, path, checks.errors);
|
|
167
|
+
if (typeof node.id === 'string' && node.id !== '') {
|
|
168
|
+
if (ids.has(node.id)) {
|
|
169
|
+
checks.errors.push({ path: `${path}.id`, message: `id "${node.id}" is already used` });
|
|
170
|
+
}
|
|
171
|
+
ids.add(node.id);
|
|
172
|
+
}
|
|
173
|
+
if (node.type === StepType.HTTP) {
|
|
174
|
+
checkHttpAccess(node, path, checks);
|
|
175
|
+
} else if (node.type === StepType.HOST) {
|
|
176
|
+
checkHost(node, path, checks);
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Checks a raw document against the format and against the product's host catalog. Every
|
|
182
|
+
* error is collected, each with the JSON path of its node. An unknown `formatVersion` is
|
|
183
|
+
* reported alone: no other rule is meaningful for a format we do not know.
|
|
184
|
+
*/
|
|
185
|
+
export const validateDefinition = (raw: unknown, catalog: HostActionCatalog): ValidationResult => {
|
|
186
|
+
if (!isRecord(raw)) {
|
|
187
|
+
return { ok: false, errors: [{ path: '', message: 'a definition must be an object' }] };
|
|
188
|
+
}
|
|
189
|
+
if (raw.formatVersion !== CURRENT_FORMAT_VERSION) {
|
|
190
|
+
return {
|
|
191
|
+
ok: false,
|
|
192
|
+
errors: [
|
|
193
|
+
{ path: 'formatVersion', message: `"formatVersion" must be ${CURRENT_FORMAT_VERSION}` },
|
|
194
|
+
],
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
const errors: DefinitionError[] = [];
|
|
198
|
+
joiErrors(DEFINITION_SCHEMA, raw, '', errors);
|
|
199
|
+
const accesses = checkAccesses(raw.accesses, errors);
|
|
200
|
+
const ids = new Set<string>();
|
|
201
|
+
const checks: StepChecks = { catalog, accesses, errors };
|
|
202
|
+
walkRawSteps(raw.steps, (node, path) => checkStep(node, path, ids, checks));
|
|
203
|
+
return errors.length === 0
|
|
204
|
+
? { ok: true, definition: raw as unknown as RuntimeDefinition }
|
|
205
|
+
: { ok: false, errors };
|
|
206
|
+
};
|
|
@@ -1,44 +1,44 @@
|
|
|
1
|
-
import type { Step } from './definition.types';
|
|
2
|
-
|
|
3
|
-
export type RawStepVisitor = (node: unknown, path: string) => void;
|
|
4
|
-
export type StepVisitor = (step: Step, path: string) => void;
|
|
5
|
-
|
|
6
|
-
const isRecord = (value: unknown): value is Record<string, unknown> =>
|
|
7
|
-
typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
8
|
-
|
|
9
|
-
const walkList = (list: unknown, basePath: string, visit: RawStepVisitor): void => {
|
|
10
|
-
if (!Array.isArray(list)) {
|
|
11
|
-
return;
|
|
12
|
-
}
|
|
13
|
-
list.forEach((node, index) => {
|
|
14
|
-
const path = `${basePath}[${index}]`;
|
|
15
|
-
visit(node, path);
|
|
16
|
-
if (!isRecord(node)) {
|
|
17
|
-
return;
|
|
18
|
-
}
|
|
19
|
-
walkList(node.then, `${path}.then`, visit);
|
|
20
|
-
walkList(node.else, `${path}.else`, visit);
|
|
21
|
-
if (Array.isArray(node.cases)) {
|
|
22
|
-
node.cases.forEach((branch, caseIndex) => {
|
|
23
|
-
if (isRecord(branch)) {
|
|
24
|
-
walkList(branch.steps, `${path}.cases[${caseIndex}].steps`, visit);
|
|
25
|
-
}
|
|
26
|
-
});
|
|
27
|
-
}
|
|
28
|
-
walkList(node.default, `${path}.default`, visit);
|
|
29
|
-
});
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Same traversal as `walkSteps` over a document that is not validated yet: every node is
|
|
34
|
-
* visited as `unknown`, and children are followed only where they are arrays.
|
|
35
|
-
*/
|
|
36
|
-
export const walkRawSteps = (steps: unknown, visit: RawStepVisitor, basePath = 'steps'): void =>
|
|
37
|
-
walkList(steps, basePath, visit);
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Visits every step of the tree, depth first and in document order (a step before its
|
|
41
|
-
* children), with its JSON path, e.g. `steps[2].cases[0].steps[1]`.
|
|
42
|
-
*/
|
|
43
|
-
export const walkSteps = (steps: readonly Step[], visit: StepVisitor, basePath = 'steps'): void =>
|
|
44
|
-
walkRawSteps(steps, (node, path) => visit(node as Step, path), basePath);
|
|
1
|
+
import type { Step } from './definition.types';
|
|
2
|
+
|
|
3
|
+
export type RawStepVisitor = (node: unknown, path: string) => void;
|
|
4
|
+
export type StepVisitor = (step: Step, path: string) => void;
|
|
5
|
+
|
|
6
|
+
const isRecord = (value: unknown): value is Record<string, unknown> =>
|
|
7
|
+
typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
8
|
+
|
|
9
|
+
const walkList = (list: unknown, basePath: string, visit: RawStepVisitor): void => {
|
|
10
|
+
if (!Array.isArray(list)) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
list.forEach((node, index) => {
|
|
14
|
+
const path = `${basePath}[${index}]`;
|
|
15
|
+
visit(node, path);
|
|
16
|
+
if (!isRecord(node)) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
walkList(node.then, `${path}.then`, visit);
|
|
20
|
+
walkList(node.else, `${path}.else`, visit);
|
|
21
|
+
if (Array.isArray(node.cases)) {
|
|
22
|
+
node.cases.forEach((branch, caseIndex) => {
|
|
23
|
+
if (isRecord(branch)) {
|
|
24
|
+
walkList(branch.steps, `${path}.cases[${caseIndex}].steps`, visit);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
walkList(node.default, `${path}.default`, visit);
|
|
29
|
+
});
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Same traversal as `walkSteps` over a document that is not validated yet: every node is
|
|
34
|
+
* visited as `unknown`, and children are followed only where they are arrays.
|
|
35
|
+
*/
|
|
36
|
+
export const walkRawSteps = (steps: unknown, visit: RawStepVisitor, basePath = 'steps'): void =>
|
|
37
|
+
walkList(steps, basePath, visit);
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Visits every step of the tree, depth first and in document order (a step before its
|
|
41
|
+
* children), with its JSON path, e.g. `steps[2].cases[0].steps[1]`.
|
|
42
|
+
*/
|
|
43
|
+
export const walkSteps = (steps: readonly Step[], visit: StepVisitor, basePath = 'steps'): void =>
|
|
44
|
+
walkRawSteps(steps, (node, path) => visit(node as Step, path), basePath);
|