@abyss-project/runtime 0.0.0-stage → 1.0.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 -0
- package/.prettierrc +5 -0
- package/README.md +25 -3
- package/dist/access/access.types.d.ts +14 -0
- package/dist/access/access.types.js +9 -0
- package/dist/access/access.types.js.map +1 -0
- package/dist/access/host-match.d.ts +1 -0
- package/dist/access/host-match.js +14 -0
- package/dist/access/host-match.js.map +1 -0
- package/dist/definition/definition.schema.d.ts +8 -0
- package/dist/definition/definition.schema.js +107 -0
- package/dist/definition/definition.schema.js.map +1 -0
- package/dist/definition/definition.types.d.ts +98 -0
- package/dist/definition/definition.types.js +29 -0
- package/dist/definition/definition.types.js.map +1 -0
- package/dist/definition/validate-definition.d.ts +9 -0
- package/dist/definition/validate-definition.js +163 -0
- package/dist/definition/validate-definition.js.map +1 -0
- package/dist/definition/walk-steps.d.ts +5 -0
- package/dist/definition/walk-steps.js +31 -0
- package/dist/definition/walk-steps.js.map +1 -0
- package/dist/errors/runtime-error-code.enum.d.ts +18 -0
- package/dist/errors/runtime-error-code.enum.js +19 -0
- package/dist/errors/runtime-error-code.enum.js.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +38 -0
- package/dist/index.js.map +1 -0
- package/dist/interpreter/interpreter-error.d.ts +5 -0
- package/dist/interpreter/interpreter-error.js +12 -0
- package/dist/interpreter/interpreter-error.js.map +1 -0
- package/dist/interpreter/interpreter.d.ts +4 -0
- package/dist/interpreter/interpreter.js +241 -0
- package/dist/interpreter/interpreter.js.map +1 -0
- package/dist/interpreter/interpreter.types.d.ts +121 -0
- package/dist/interpreter/interpreter.types.js +25 -0
- package/dist/interpreter/interpreter.types.js.map +1 -0
- package/dist/interpreter/sequence-path.d.ts +6 -0
- package/dist/interpreter/sequence-path.js +53 -0
- package/dist/interpreter/sequence-path.js.map +1 -0
- package/dist/interpreter/test-host.d.ts +16 -0
- package/dist/interpreter/test-host.js +37 -0
- package/dist/interpreter/test-host.js.map +1 -0
- package/dist/runner-protocol/runner-protocol.types.d.ts +92 -0
- package/dist/runner-protocol/runner-protocol.types.js +26 -0
- package/dist/runner-protocol/runner-protocol.types.js.map +1 -0
- package/dist/server/code-runner-client.d.ts +13 -0
- package/dist/server/code-runner-client.js +155 -0
- package/dist/server/code-runner-client.js.map +1 -0
- package/dist/server/code-runner-error.d.ts +5 -0
- package/dist/server/code-runner-error.js +12 -0
- package/dist/server/code-runner-error.js.map +1 -0
- package/dist/server/index.d.ts +3 -0
- package/dist/server/index.js +8 -0
- package/dist/server/index.js.map +1 -0
- package/dist/steps/execute-step.d.ts +40 -0
- package/dist/steps/execute-step.js +99 -0
- package/dist/steps/execute-step.js.map +1 -0
- package/dist/steps/step-access.d.ts +8 -0
- package/dist/steps/step-access.js +39 -0
- package/dist/steps/step-access.js.map +1 -0
- package/dist/steps/step-fields.d.ts +5 -0
- package/dist/steps/step-fields.js +60 -0
- package/dist/steps/step-fields.js.map +1 -0
- package/dist/steps/value-source.d.ts +6 -0
- package/dist/steps/value-source.js +58 -0
- package/dist/steps/value-source.js.map +1 -0
- package/jest.config.ts +11 -0
- package/package.json +60 -3
- package/src/__tests__/surface.test.ts +53 -0
- package/src/access/__tests__/host-match.test.ts +24 -0
- package/src/access/access.types.ts +25 -0
- package/src/access/host-match.ts +13 -0
- package/src/definition/__tests__/validate-definition.test.ts +303 -0
- package/src/definition/definition.schema.ts +139 -0
- package/src/definition/definition.types.ts +143 -0
- package/src/definition/validate-definition.ts +206 -0
- package/src/definition/walk-steps.ts +44 -0
- package/src/errors/runtime-error-code.enum.ts +37 -0
- package/src/index.ts +18 -0
- package/src/interpreter/__tests__/interpreter.test.ts +854 -0
- package/src/interpreter/interpreter-error.ts +12 -0
- package/src/interpreter/interpreter.ts +342 -0
- package/src/interpreter/interpreter.types.ts +140 -0
- package/src/interpreter/sequence-path.ts +67 -0
- package/src/interpreter/test-host.ts +74 -0
- package/src/runner-protocol/runner-protocol.types.ts +117 -0
- package/src/server/__tests__/code-runner-client.test.ts +262 -0
- package/src/server/code-runner-client.ts +201 -0
- package/src/server/code-runner-error.ts +12 -0
- package/src/server/index.ts +4 -0
- package/src/steps/execute-step.ts +132 -0
- package/src/steps/step-access.ts +56 -0
- package/src/steps/step-fields.ts +79 -0
- package/src/steps/value-source.ts +69 -0
- package/tsconfig.check.json +15 -0
- package/tsconfig.json +35 -0
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import Joi from 'joi';
|
|
2
|
+
import { AccessKind } from '../access/access.types';
|
|
3
|
+
import { CURRENT_FORMAT_VERSION, HttpMethod, StepType, StopStatus } from './definition.types';
|
|
4
|
+
|
|
5
|
+
export const MAX_HTTP_TIMEOUT_MS = 30_000;
|
|
6
|
+
export const MAX_WAIT_DURATION_MS = 30 * 24 * 60 * 60 * 1000;
|
|
7
|
+
|
|
8
|
+
/** Names that would hit `Object.prototype` when used as a key of `outputs` or `vars`. */
|
|
9
|
+
const RESERVED_NAMES = ['__proto__', 'constructor', 'prototype'];
|
|
10
|
+
|
|
11
|
+
const IDENTIFIER = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
|
|
12
|
+
const HOST_PATTERN = /^(\*\.)?[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)*$/i;
|
|
13
|
+
|
|
14
|
+
let valueSourceCount = 0;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Literal JSON, `{ $expression }` (that key alone), arrays and objects of value sources. Joi
|
|
18
|
+
* links resolve by id and ids must be unique inside one schema tree, hence a fresh id per use.
|
|
19
|
+
*/
|
|
20
|
+
const valueSource = (): Joi.AlternativesSchema => {
|
|
21
|
+
const id = `valueSource${valueSourceCount++}`;
|
|
22
|
+
return Joi.alternatives()
|
|
23
|
+
.try(
|
|
24
|
+
Joi.string().allow(''),
|
|
25
|
+
Joi.number(),
|
|
26
|
+
Joi.boolean(),
|
|
27
|
+
Joi.valid(null),
|
|
28
|
+
Joi.array().items(Joi.link(`#${id}`)),
|
|
29
|
+
Joi.object({ $expression: Joi.string().required() }),
|
|
30
|
+
Joi.object().pattern(Joi.string().invalid('$expression'), Joi.link(`#${id}`)),
|
|
31
|
+
)
|
|
32
|
+
.id(id);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const valueMap = () => Joi.object().pattern(Joi.string(), valueSource());
|
|
36
|
+
|
|
37
|
+
/** A number is bounded here; anything else (template, `$expression`) is checked at run time. */
|
|
38
|
+
const durationSource = (): Joi.AlternativesSchema =>
|
|
39
|
+
Joi.alternatives().try(
|
|
40
|
+
Joi.number().integer().min(0).max(MAX_WAIT_DURATION_MS),
|
|
41
|
+
Joi.string().allow(''),
|
|
42
|
+
Joi.object({ $expression: Joi.string().required() }),
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
/** A literal must be a date; a template or `$expression` is checked at run time. */
|
|
46
|
+
const untilSource = (): Joi.AlternativesSchema =>
|
|
47
|
+
Joi.alternatives().try(
|
|
48
|
+
Joi.string().pattern(/\{\{[\s\S]*?\}\}/),
|
|
49
|
+
Joi.string().isoDate(),
|
|
50
|
+
Joi.object({ $expression: Joi.string().required() }),
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
const stepBase = {
|
|
54
|
+
id: Joi.string()
|
|
55
|
+
.min(1)
|
|
56
|
+
.invalid(...RESERVED_NAMES)
|
|
57
|
+
.required(),
|
|
58
|
+
name: Joi.string().allow(''),
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
// Children (`then`, `else`, `steps`, `default`) are only checked to be arrays here: the
|
|
62
|
+
// validator walks them to give each nested step its own path.
|
|
63
|
+
export const STEP_SCHEMAS: Record<StepType, Joi.ObjectSchema> = {
|
|
64
|
+
[StepType.CONDITION]: Joi.object({
|
|
65
|
+
...stepBase,
|
|
66
|
+
type: Joi.valid(StepType.CONDITION).required(),
|
|
67
|
+
if: valueSource().required(),
|
|
68
|
+
then: Joi.array().required(),
|
|
69
|
+
else: Joi.array(),
|
|
70
|
+
}),
|
|
71
|
+
[StepType.SWITCH]: Joi.object({
|
|
72
|
+
...stepBase,
|
|
73
|
+
type: Joi.valid(StepType.SWITCH).required(),
|
|
74
|
+
value: valueSource().required(),
|
|
75
|
+
cases: Joi.array()
|
|
76
|
+
.min(1)
|
|
77
|
+
.items(Joi.object({ match: valueSource().required(), steps: Joi.array().required() }))
|
|
78
|
+
.required(),
|
|
79
|
+
default: Joi.array(),
|
|
80
|
+
}),
|
|
81
|
+
[StepType.WAIT]: Joi.object({
|
|
82
|
+
...stepBase,
|
|
83
|
+
type: Joi.valid(StepType.WAIT).required(),
|
|
84
|
+
durationMs: durationSource(),
|
|
85
|
+
until: untilSource(),
|
|
86
|
+
}).xor('durationMs', 'until'),
|
|
87
|
+
[StepType.SET]: Joi.object({
|
|
88
|
+
...stepBase,
|
|
89
|
+
type: Joi.valid(StepType.SET).required(),
|
|
90
|
+
variable: Joi.string()
|
|
91
|
+
.pattern(IDENTIFIER)
|
|
92
|
+
.invalid(...RESERVED_NAMES)
|
|
93
|
+
.required(),
|
|
94
|
+
value: valueSource().required(),
|
|
95
|
+
}),
|
|
96
|
+
[StepType.CODE]: Joi.object({
|
|
97
|
+
...stepBase,
|
|
98
|
+
type: Joi.valid(StepType.CODE).required(),
|
|
99
|
+
source: Joi.string().min(1).required(),
|
|
100
|
+
}),
|
|
101
|
+
[StepType.HTTP]: Joi.object({
|
|
102
|
+
...stepBase,
|
|
103
|
+
type: Joi.valid(StepType.HTTP).required(),
|
|
104
|
+
method: Joi.valid(...Object.values(HttpMethod)).required(),
|
|
105
|
+
url: valueSource().required(),
|
|
106
|
+
headers: valueMap(),
|
|
107
|
+
body: valueSource(),
|
|
108
|
+
timeoutMs: Joi.number().integer().min(1).max(MAX_HTTP_TIMEOUT_MS),
|
|
109
|
+
}),
|
|
110
|
+
[StepType.HOST]: Joi.object({
|
|
111
|
+
...stepBase,
|
|
112
|
+
type: Joi.valid(StepType.HOST).required(),
|
|
113
|
+
action: Joi.string().min(1).required(),
|
|
114
|
+
input: valueMap().required(),
|
|
115
|
+
}),
|
|
116
|
+
[StepType.STOP]: Joi.object({
|
|
117
|
+
...stepBase,
|
|
118
|
+
type: Joi.valid(StepType.STOP).required(),
|
|
119
|
+
status: Joi.valid(...Object.values(StopStatus)).required(),
|
|
120
|
+
output: valueSource(),
|
|
121
|
+
}),
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
export const DEFINITION_SCHEMA = Joi.object({
|
|
125
|
+
formatVersion: Joi.valid(CURRENT_FORMAT_VERSION).required(),
|
|
126
|
+
accesses: Joi.array().required(),
|
|
127
|
+
steps: Joi.array().required(),
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
export const ACCESS_SCHEMAS: Record<AccessKind, Joi.ObjectSchema> = {
|
|
131
|
+
[AccessKind.HTTP]: Joi.object({
|
|
132
|
+
kind: Joi.valid(AccessKind.HTTP).required(),
|
|
133
|
+
hosts: Joi.array().items(Joi.string().pattern(HOST_PATTERN)).min(1).required(),
|
|
134
|
+
}),
|
|
135
|
+
[AccessKind.HOST_ACTION]: Joi.object({
|
|
136
|
+
kind: Joi.valid(AccessKind.HOST_ACTION).required(),
|
|
137
|
+
action: Joi.string().min(1).required(),
|
|
138
|
+
}),
|
|
139
|
+
};
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import type { ObjectSchema } from 'joi';
|
|
2
|
+
import type { AccessDeclaration } from '../access/access.types';
|
|
3
|
+
|
|
4
|
+
export const CURRENT_FORMAT_VERSION = 1;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A full JavaScript expression, evaluated by the code runner. Any other string may embed
|
|
8
|
+
* `{{ … }}` templates; an object whose only key is `$expression` is an expression.
|
|
9
|
+
*/
|
|
10
|
+
export interface ExpressionValue {
|
|
11
|
+
$expression: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export type ValueSource =
|
|
15
|
+
| string
|
|
16
|
+
| number
|
|
17
|
+
| boolean
|
|
18
|
+
| null
|
|
19
|
+
| ExpressionValue
|
|
20
|
+
| ValueSource[]
|
|
21
|
+
| { [key: string]: ValueSource };
|
|
22
|
+
|
|
23
|
+
export enum StepType {
|
|
24
|
+
CONDITION = 'CONDITION',
|
|
25
|
+
SWITCH = 'SWITCH',
|
|
26
|
+
WAIT = 'WAIT',
|
|
27
|
+
SET = 'SET',
|
|
28
|
+
CODE = 'CODE',
|
|
29
|
+
HTTP = 'HTTP',
|
|
30
|
+
HOST = 'HOST',
|
|
31
|
+
STOP = 'STOP',
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export enum HttpMethod {
|
|
35
|
+
GET = 'GET',
|
|
36
|
+
POST = 'POST',
|
|
37
|
+
PUT = 'PUT',
|
|
38
|
+
PATCH = 'PATCH',
|
|
39
|
+
DELETE = 'DELETE',
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export enum StopStatus {
|
|
43
|
+
SUCCEEDED = 'SUCCEEDED',
|
|
44
|
+
FAILED = 'FAILED',
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
interface StepBase {
|
|
48
|
+
/** Stable, unique in the definition; outputs are read as `steps.<id>.output`. */
|
|
49
|
+
id: string;
|
|
50
|
+
name?: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface ConditionStep extends StepBase {
|
|
54
|
+
type: StepType.CONDITION;
|
|
55
|
+
if: ValueSource;
|
|
56
|
+
then: Step[];
|
|
57
|
+
else?: Step[];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface SwitchCase {
|
|
61
|
+
match: ValueSource;
|
|
62
|
+
steps: Step[];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface SwitchStep extends StepBase {
|
|
66
|
+
type: StepType.SWITCH;
|
|
67
|
+
value: ValueSource;
|
|
68
|
+
cases: SwitchCase[];
|
|
69
|
+
default?: Step[];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Exactly one of `durationMs` and `until` (an ISO 8601 date); the host schedules the wake-up. */
|
|
73
|
+
export interface WaitStep extends StepBase {
|
|
74
|
+
type: StepType.WAIT;
|
|
75
|
+
durationMs?: ValueSource;
|
|
76
|
+
until?: ValueSource;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export interface SetStep extends StepBase {
|
|
80
|
+
type: StepType.SET;
|
|
81
|
+
/** Read back as `vars.<variable>`. */
|
|
82
|
+
variable: string;
|
|
83
|
+
value: ValueSource;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** JavaScript statements; the value of their `return` becomes the step's output. */
|
|
87
|
+
export interface CodeStep extends StepBase {
|
|
88
|
+
type: StepType.CODE;
|
|
89
|
+
source: string;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export interface HttpStep extends StepBase {
|
|
93
|
+
type: StepType.HTTP;
|
|
94
|
+
method: HttpMethod;
|
|
95
|
+
url: ValueSource;
|
|
96
|
+
headers?: Record<string, ValueSource>;
|
|
97
|
+
body?: ValueSource;
|
|
98
|
+
timeoutMs?: number;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export interface HostStep extends StepBase {
|
|
102
|
+
type: StepType.HOST;
|
|
103
|
+
action: string;
|
|
104
|
+
input: Record<string, ValueSource>;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export interface StopStep extends StepBase {
|
|
108
|
+
type: StepType.STOP;
|
|
109
|
+
status: StopStatus;
|
|
110
|
+
output?: ValueSource;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export type Step =
|
|
114
|
+
| ConditionStep
|
|
115
|
+
| SwitchStep
|
|
116
|
+
| WaitStep
|
|
117
|
+
| SetStep
|
|
118
|
+
| CodeStep
|
|
119
|
+
| HttpStep
|
|
120
|
+
| HostStep
|
|
121
|
+
| StopStep;
|
|
122
|
+
|
|
123
|
+
export interface RuntimeDefinition {
|
|
124
|
+
formatVersion: typeof CURRENT_FORMAT_VERSION;
|
|
125
|
+
accesses: AccessDeclaration[];
|
|
126
|
+
steps: Step[];
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** One action a product offers to its definitions (e-mail for Flow, Discord for Reflex…). */
|
|
130
|
+
export interface HostActionDefinition {
|
|
131
|
+
name: string;
|
|
132
|
+
/** Validates the input once every expression in it is resolved. */
|
|
133
|
+
inputSchema: ObjectSchema;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Supplied by each product; the core knows no Abyss service. */
|
|
137
|
+
export type HostActionCatalog = ReadonlyMap<string, HostActionDefinition>;
|
|
138
|
+
|
|
139
|
+
export interface DefinitionError {
|
|
140
|
+
/** JSON path of the offending node, e.g. `steps[2].then[0].action`. */
|
|
141
|
+
path: string;
|
|
142
|
+
message: string;
|
|
143
|
+
}
|
|
@@ -0,0 +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
|
+
};
|
|
@@ -0,0 +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);
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every failure a run, an evaluation or the code runner can report. The same codes travel in
|
|
3
|
+
* the interpreter's events, in the runner protocol and in the errors thrown by the Node client,
|
|
4
|
+
* so a caller maps one list to its own failure codes.
|
|
5
|
+
*/
|
|
6
|
+
export enum RuntimeErrorCode {
|
|
7
|
+
/** An expression or a template threw or did not parse. */
|
|
8
|
+
EXPRESSION_ERROR = 'EXPRESSION_ERROR',
|
|
9
|
+
/** A `code` item threw or did not parse. */
|
|
10
|
+
CODE_ERROR = 'CODE_ERROR',
|
|
11
|
+
/** An item exceeded its wall-clock or CPU time limit. */
|
|
12
|
+
TIMEOUT = 'TIMEOUT',
|
|
13
|
+
/** An item exceeded the memory limit of its lot. */
|
|
14
|
+
MEMORY_LIMIT = 'MEMORY_LIMIT',
|
|
15
|
+
/** A host function called through `fn.*` failed, or was called too often. */
|
|
16
|
+
HOST_FUNCTION_ERROR = 'HOST_FUNCTION_ERROR',
|
|
17
|
+
/** An effect is not covered by both the definition's declarations and the run's grants. */
|
|
18
|
+
ACCESS_DENIED = 'ACCESS_DENIED',
|
|
19
|
+
/** The run exceeded its step or effect budget. */
|
|
20
|
+
BUDGET_EXCEEDED = 'BUDGET_EXCEEDED',
|
|
21
|
+
/** A host action failed, or its resolved input does not match the action's schema. */
|
|
22
|
+
HOST_ERROR = 'HOST_ERROR',
|
|
23
|
+
/** The host could not perform an HTTP effect (network, SSRF guard, timeout). */
|
|
24
|
+
HTTP_ERROR = 'HTTP_ERROR',
|
|
25
|
+
/** The code runner could not be reached, or the connection dropped mid-lot. */
|
|
26
|
+
RUNNER_UNAVAILABLE = 'RUNNER_UNAVAILABLE',
|
|
27
|
+
/** The code runner refused the lot: its queue is full or the wait timed out. */
|
|
28
|
+
RUNNER_SATURATED = 'RUNNER_SATURATED',
|
|
29
|
+
/** A message or a definition does not match the contract. */
|
|
30
|
+
INVALID_REQUEST = 'INVALID_REQUEST',
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface RuntimeErrorDetail {
|
|
34
|
+
code: RuntimeErrorCode;
|
|
35
|
+
/** Human-readable cause. Never contains the client's code, context or values. */
|
|
36
|
+
message: string;
|
|
37
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// Package root: browser-compatible. No Node import (`fs`, `net`, `http`, `ws`...) here nor in what
|
|
2
|
+
// this root re-exports; everything touching the network or the system lives under `src/server`
|
|
3
|
+
// (entry point `@abyss-project/runtime/server`).
|
|
4
|
+
export * from './errors/runtime-error-code.enum';
|
|
5
|
+
export * from './access/access.types';
|
|
6
|
+
export { hostMatches } from './access/host-match';
|
|
7
|
+
export * from './definition/definition.types';
|
|
8
|
+
export { validateDefinition } from './definition/validate-definition';
|
|
9
|
+
export type { ValidationResult } from './definition/validate-definition';
|
|
10
|
+
export { MAX_WAIT_DURATION_MS } from './definition/definition.schema';
|
|
11
|
+
export { walkSteps } from './definition/walk-steps';
|
|
12
|
+
export type { StepVisitor } from './definition/walk-steps';
|
|
13
|
+
export * from './interpreter/interpreter.types';
|
|
14
|
+
export { start, step } from './interpreter/interpreter';
|
|
15
|
+
export { InterpreterError } from './interpreter/interpreter-error';
|
|
16
|
+
export { runToCompletion } from './interpreter/test-host';
|
|
17
|
+
export type { TestHostHandlers, TestRunResult } from './interpreter/test-host';
|
|
18
|
+
export * from './runner-protocol/runner-protocol.types';
|