@abyss-project/runtime 0.0.0-stage → 2.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 +28 -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/cost/execution-cost.d.ts +45 -0
- package/dist/cost/execution-cost.js +114 -0
- package/dist/cost/execution-cost.js.map +1 -0
- package/dist/definition/definition.limits.d.ts +4 -0
- package/dist/definition/definition.limits.js +8 -0
- package/dist/definition/definition.limits.js.map +1 -0
- package/dist/definition/definition.schema.d.ts +8 -0
- package/dist/definition/definition.schema.js +123 -0
- package/dist/definition/definition.schema.js.map +1 -0
- package/dist/definition/definition.types.d.ts +117 -0
- package/dist/definition/definition.types.js +37 -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/engine/engine-entry.d.ts +1 -0
- package/dist/engine/engine-entry.js +6 -0
- package/dist/engine/engine-entry.js.map +1 -0
- package/dist/engine/engine-global.d.ts +1 -0
- package/dist/engine/engine-global.js +5 -0
- package/dist/engine/engine-global.js.map +1 -0
- package/dist/engine/engine-source.d.ts +2 -0
- package/dist/engine/engine-source.js +3 -0
- package/dist/engine/run-in-isolate.d.ts +34 -0
- package/dist/engine/run-in-isolate.js +267 -0
- package/dist/engine/run-in-isolate.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 +21 -0
- package/dist/index.js +47 -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 +30 -0
- package/dist/interpreter/interpreter.js +313 -0
- package/dist/interpreter/interpreter.js.map +1 -0
- package/dist/interpreter/interpreter.types.d.ts +152 -0
- package/dist/interpreter/interpreter.types.js +31 -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 +18 -0
- package/dist/interpreter/test-host.js +43 -0
- package/dist/interpreter/test-host.js.map +1 -0
- package/dist/runner-protocol/evaluation.types.d.ts +20 -0
- package/dist/runner-protocol/evaluation.types.js +10 -0
- package/dist/runner-protocol/evaluation.types.js.map +1 -0
- package/dist/runner-protocol/execution-protocol.types.d.ts +132 -0
- package/dist/runner-protocol/execution-protocol.types.js +48 -0
- package/dist/runner-protocol/execution-protocol.types.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/code-runner-session.d.ts +28 -0
- package/dist/server/code-runner-session.js +208 -0
- package/dist/server/code-runner-session.js.map +1 -0
- package/dist/server/index.d.ts +3 -0
- package/dist/server/index.js +10 -0
- package/dist/server/index.js.map +1 -0
- package/dist/steps/execute-step.d.ts +50 -0
- package/dist/steps/execute-step.js +136 -0
- package/dist/steps/execute-step.js.map +1 -0
- package/dist/steps/global-references.d.ts +2 -0
- package/dist/steps/global-references.js +28 -0
- package/dist/steps/global-references.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 +68 -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 +68 -3
- package/scripts/build-engine.cjs +70 -0
- package/src/__tests__/engine-bundle.test.ts +121 -0
- 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/cost/__tests__/execution-cost.test.ts +200 -0
- package/src/cost/execution-cost.ts +192 -0
- package/src/definition/__tests__/validate-definition.test.ts +354 -0
- package/src/definition/definition.limits.ts +9 -0
- package/src/definition/definition.schema.ts +172 -0
- package/src/definition/definition.types.ts +171 -0
- package/src/definition/validate-definition.ts +206 -0
- package/src/definition/walk-steps.ts +44 -0
- package/src/engine/__tests__/run-in-isolate.test.ts +468 -0
- package/src/engine/engine-entry.ts +3 -0
- package/src/engine/engine-global.ts +2 -0
- package/src/engine/run-in-isolate.ts +404 -0
- package/src/errors/runtime-error-code.enum.ts +37 -0
- package/src/index.ts +30 -0
- package/src/interpreter/__tests__/interpreter.test.ts +854 -0
- package/src/interpreter/__tests__/log-respond-resume.test.ts +223 -0
- package/src/interpreter/interpreter-error.ts +12 -0
- package/src/interpreter/interpreter.ts +461 -0
- package/src/interpreter/interpreter.types.ts +192 -0
- package/src/interpreter/sequence-path.ts +67 -0
- package/src/interpreter/test-host.ts +85 -0
- package/src/runner-protocol/evaluation.types.ts +21 -0
- package/src/runner-protocol/execution-protocol.types.ts +221 -0
- package/src/server/__tests__/code-runner-session.test.ts +311 -0
- package/src/server/code-runner-error.ts +12 -0
- package/src/server/code-runner-session.ts +310 -0
- package/src/server/index.ts +11 -0
- package/src/steps/__tests__/global-references.test.ts +113 -0
- package/src/steps/execute-step.ts +186 -0
- package/src/steps/global-references.ts +48 -0
- package/src/steps/step-access.ts +56 -0
- package/src/steps/step-fields.ts +87 -0
- package/src/steps/value-source.ts +69 -0
- package/tsconfig.check.json +15 -0
- package/tsconfig.json +35 -0
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { CURRENT_FORMAT_VERSION, HttpMethod, StepType } from '../../definition/definition.types';
|
|
2
|
+
import type { RuntimeDefinition, Step } from '../../definition/definition.types';
|
|
3
|
+
import { collectGlobalReferences } from '../global-references';
|
|
4
|
+
|
|
5
|
+
const definitionOf = (steps: Step[]): RuntimeDefinition => ({
|
|
6
|
+
formatVersion: CURRENT_FORMAT_VERSION,
|
|
7
|
+
accesses: [],
|
|
8
|
+
steps,
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
describe('collectGlobalReferences', () => {
|
|
12
|
+
it('finds a reference in a template placeholder, an expression and a CODE source', () => {
|
|
13
|
+
const definition = definitionOf([
|
|
14
|
+
{
|
|
15
|
+
id: 'call',
|
|
16
|
+
type: StepType.HTTP,
|
|
17
|
+
method: HttpMethod.GET,
|
|
18
|
+
url: 'https://api.example.com/{{ input.id }}',
|
|
19
|
+
headers: { Authorization: 'Bearer {{ secrets.API_KEY }}' },
|
|
20
|
+
body: { $expression: 'secrets.BODY_KEY + "x"' },
|
|
21
|
+
},
|
|
22
|
+
{ id: 'code', type: StepType.CODE, source: 'const t = secrets.CODE_KEY; return t;' },
|
|
23
|
+
]);
|
|
24
|
+
expect(collectGlobalReferences(definition, 'secrets')).toEqual([
|
|
25
|
+
'API_KEY',
|
|
26
|
+
'BODY_KEY',
|
|
27
|
+
'CODE_KEY',
|
|
28
|
+
]);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('reads the references of the branches of CONDITION and SWITCH steps', () => {
|
|
32
|
+
const definition = definitionOf([
|
|
33
|
+
{
|
|
34
|
+
id: 'cond',
|
|
35
|
+
type: StepType.CONDITION,
|
|
36
|
+
if: { $expression: 'true' },
|
|
37
|
+
then: [{ id: 'a', type: StepType.SET, variable: 'a', value: '{{ secrets.THEN }}' }],
|
|
38
|
+
else: [{ id: 'b', type: StepType.SET, variable: 'b', value: '{{ secrets.ELSE }}' }],
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
id: 'sw',
|
|
42
|
+
type: StepType.SWITCH,
|
|
43
|
+
value: { $expression: 'input.kind' },
|
|
44
|
+
cases: [
|
|
45
|
+
{
|
|
46
|
+
match: 'x',
|
|
47
|
+
steps: [
|
|
48
|
+
{
|
|
49
|
+
id: 'deep',
|
|
50
|
+
type: StepType.CONDITION,
|
|
51
|
+
if: { $expression: 'secrets.CASE_CONDITION === "1"' },
|
|
52
|
+
then: [],
|
|
53
|
+
},
|
|
54
|
+
],
|
|
55
|
+
},
|
|
56
|
+
],
|
|
57
|
+
default: [{ id: 'c', type: StepType.SET, variable: 'c', value: '{{ secrets.DEFAULT }}' }],
|
|
58
|
+
},
|
|
59
|
+
]);
|
|
60
|
+
expect(collectGlobalReferences(definition, 'secrets')).toEqual([
|
|
61
|
+
'THEN',
|
|
62
|
+
'ELSE',
|
|
63
|
+
'CASE_CONDITION',
|
|
64
|
+
'DEFAULT',
|
|
65
|
+
]);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('reads the bracket form with either quote, and the optional chain', () => {
|
|
69
|
+
const definition = definitionOf([
|
|
70
|
+
{
|
|
71
|
+
id: 'code',
|
|
72
|
+
type: StepType.CODE,
|
|
73
|
+
source: `return [secrets['A'], secrets["B"], secrets [ 'C' ], secrets?.D];`,
|
|
74
|
+
},
|
|
75
|
+
]);
|
|
76
|
+
expect(collectGlobalReferences(definition, 'secrets')).toEqual(['A', 'B', 'C', 'D']);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('lists a name once, in order of first appearance', () => {
|
|
80
|
+
const definition = definitionOf([
|
|
81
|
+
{ id: 'one', type: StepType.SET, variable: 'x', value: '{{ secrets.B }}-{{ secrets.A }}' },
|
|
82
|
+
{
|
|
83
|
+
id: 'two',
|
|
84
|
+
type: StepType.SET,
|
|
85
|
+
variable: 'y',
|
|
86
|
+
value: { $expression: 'secrets.B + secrets["A"]' },
|
|
87
|
+
},
|
|
88
|
+
]);
|
|
89
|
+
expect(collectGlobalReferences(definition, 'secrets')).toEqual(['B', 'A']);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('ignores dynamic access, other globals, member chains and text outside placeholders', () => {
|
|
93
|
+
const definition = definitionOf([
|
|
94
|
+
{
|
|
95
|
+
id: 'code',
|
|
96
|
+
type: StepType.CODE,
|
|
97
|
+
source: 'const n = "K"; return [secrets[n], other.secrets.X, mysecrets.Y, secrets];',
|
|
98
|
+
},
|
|
99
|
+
{ id: 'text', type: StepType.SET, variable: 't', value: 'secrets.OUTSIDE {{ input.x }}' },
|
|
100
|
+
{ id: 'literal', type: StepType.SET, variable: 'l', value: 'secrets.LITERAL' },
|
|
101
|
+
]);
|
|
102
|
+
expect(collectGlobalReferences(definition, 'secrets')).toEqual([]);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('works for any global name, and escapes it', () => {
|
|
106
|
+
const definition = definitionOf([
|
|
107
|
+
{ id: 'a', type: StepType.SET, variable: 'a', value: '{{ workflow.project + secrets.S }}' },
|
|
108
|
+
]);
|
|
109
|
+
expect(collectGlobalReferences(definition, 'workflow')).toEqual(['project']);
|
|
110
|
+
expect(collectGlobalReferences(definition, 'work.low')).toEqual([]);
|
|
111
|
+
expect(collectGlobalReferences(definition, '$')).toEqual([]);
|
|
112
|
+
});
|
|
113
|
+
});
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { RuntimeErrorCode } from '../errors/runtime-error-code.enum';
|
|
2
|
+
import type { RuntimeErrorDetail } from '../errors/runtime-error-code.enum';
|
|
3
|
+
import { MAX_RESPONSE_STATUS, MIN_RESPONSE_STATUS } from '../definition/definition.limits';
|
|
4
|
+
import { StepType } from '../definition/definition.types';
|
|
5
|
+
import type {
|
|
6
|
+
LogLevel,
|
|
7
|
+
LogStep,
|
|
8
|
+
RespondStep,
|
|
9
|
+
Step,
|
|
10
|
+
StopStatus,
|
|
11
|
+
WaitStep,
|
|
12
|
+
} from '../definition/definition.types';
|
|
13
|
+
import type { HttpRequest, HttpResponseToSend } from '../interpreter/interpreter.types';
|
|
14
|
+
import { propertyPath } from './value-source';
|
|
15
|
+
import type { ResolvedFields } from './step-fields';
|
|
16
|
+
|
|
17
|
+
export enum StepActionKind {
|
|
18
|
+
/** The step finished without any effect. */
|
|
19
|
+
DONE = 'DONE',
|
|
20
|
+
STOP = 'STOP',
|
|
21
|
+
WAIT = 'WAIT',
|
|
22
|
+
HTTP = 'HTTP',
|
|
23
|
+
HOST = 'HOST',
|
|
24
|
+
LOG = 'LOG',
|
|
25
|
+
RESPOND = 'RESPOND',
|
|
26
|
+
FAIL = 'FAIL',
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export type StepAction =
|
|
30
|
+
| {
|
|
31
|
+
kind: StepActionKind.DONE;
|
|
32
|
+
output: unknown;
|
|
33
|
+
variable?: { name: string; value: unknown };
|
|
34
|
+
/** Sequence to enter, relative to the step: `then`, `else`, `cases[2].steps`, `default`. */
|
|
35
|
+
branch?: string;
|
|
36
|
+
}
|
|
37
|
+
| { kind: StepActionKind.STOP; status: StopStatus; output: unknown }
|
|
38
|
+
| { kind: StepActionKind.WAIT; durationMs?: number; until?: string }
|
|
39
|
+
| { kind: StepActionKind.HTTP; request: HttpRequest }
|
|
40
|
+
| { kind: StepActionKind.HOST; action: string; input: Record<string, unknown> }
|
|
41
|
+
| { kind: StepActionKind.LOG; level: LogLevel; message: string; data?: unknown }
|
|
42
|
+
| { kind: StepActionKind.RESPOND; response: HttpResponseToSend }
|
|
43
|
+
| { kind: StepActionKind.FAIL; error: RuntimeErrorDetail };
|
|
44
|
+
|
|
45
|
+
const fail = (message: string): StepAction => ({
|
|
46
|
+
kind: StepActionKind.FAIL,
|
|
47
|
+
error: { code: RuntimeErrorCode.EXPRESSION_ERROR, message },
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
const headerValue = (value: unknown): string =>
|
|
51
|
+
typeof value === 'string' ? value : JSON.stringify(value) ?? '';
|
|
52
|
+
|
|
53
|
+
const resolvedRecord = (
|
|
54
|
+
base: string,
|
|
55
|
+
keys: string[],
|
|
56
|
+
fields: ResolvedFields,
|
|
57
|
+
): Record<string, unknown> =>
|
|
58
|
+
Object.fromEntries(keys.map((key) => [key, fields.get(propertyPath(base, key))]));
|
|
59
|
+
|
|
60
|
+
const executeWait = (step: WaitStep, fields: ResolvedFields): StepAction => {
|
|
61
|
+
if (step.durationMs !== undefined) {
|
|
62
|
+
const durationMs = fields.get('durationMs');
|
|
63
|
+
if (typeof durationMs !== 'number' || !Number.isInteger(durationMs) || durationMs < 0) {
|
|
64
|
+
return fail('Wait duration must resolve to a non-negative integer number of milliseconds');
|
|
65
|
+
}
|
|
66
|
+
return { kind: StepActionKind.WAIT, durationMs };
|
|
67
|
+
}
|
|
68
|
+
const until = fields.get('until');
|
|
69
|
+
if (typeof until !== 'string' || Number.isNaN(Date.parse(until))) {
|
|
70
|
+
return fail('Wait date must resolve to an ISO 8601 date');
|
|
71
|
+
}
|
|
72
|
+
return { kind: StepActionKind.WAIT, until };
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const executeLog = (step: LogStep, fields: ResolvedFields): StepAction => {
|
|
76
|
+
const message = fields.get('message');
|
|
77
|
+
const action: Extract<StepAction, { kind: StepActionKind.LOG }> = {
|
|
78
|
+
kind: StepActionKind.LOG,
|
|
79
|
+
level: step.level,
|
|
80
|
+
message: typeof message === 'string' ? message : JSON.stringify(message) ?? '',
|
|
81
|
+
};
|
|
82
|
+
if (step.data !== undefined) {
|
|
83
|
+
action.data = fields.get('data');
|
|
84
|
+
}
|
|
85
|
+
return action;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const executeRespond = (step: RespondStep, fields: ResolvedFields): StepAction => {
|
|
89
|
+
const status = fields.get('status');
|
|
90
|
+
if (
|
|
91
|
+
typeof status !== 'number' ||
|
|
92
|
+
!Number.isInteger(status) ||
|
|
93
|
+
status < MIN_RESPONSE_STATUS ||
|
|
94
|
+
status > MAX_RESPONSE_STATUS
|
|
95
|
+
) {
|
|
96
|
+
return fail(
|
|
97
|
+
`Response status must resolve to an integer from ${MIN_RESPONSE_STATUS} to ${MAX_RESPONSE_STATUS}`,
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
const headers = resolvedRecord('headers', Object.keys(step.headers ?? {}), fields);
|
|
101
|
+
const response: HttpResponseToSend = {
|
|
102
|
+
status,
|
|
103
|
+
headers: Object.fromEntries(
|
|
104
|
+
Object.entries(headers).map(([key, value]) => [key, headerValue(value)]),
|
|
105
|
+
),
|
|
106
|
+
};
|
|
107
|
+
if (step.body !== undefined) {
|
|
108
|
+
response.body = fields.get('body');
|
|
109
|
+
}
|
|
110
|
+
return { kind: StepActionKind.RESPOND, response };
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Decides what a step does once its values are resolved. Pure: the access and schema checks of
|
|
115
|
+
* HTTP and HOST effects belong to the caller.
|
|
116
|
+
*/
|
|
117
|
+
export const executeStep = (step: Step, fields: ResolvedFields): StepAction => {
|
|
118
|
+
switch (step.type) {
|
|
119
|
+
case StepType.CONDITION: {
|
|
120
|
+
if (fields.get('if')) {
|
|
121
|
+
return { kind: StepActionKind.DONE, output: 'then', branch: 'then' };
|
|
122
|
+
}
|
|
123
|
+
return step.else
|
|
124
|
+
? { kind: StepActionKind.DONE, output: 'else', branch: 'else' }
|
|
125
|
+
: { kind: StepActionKind.DONE, output: 'else' };
|
|
126
|
+
}
|
|
127
|
+
case StepType.SWITCH: {
|
|
128
|
+
const value = fields.get('value');
|
|
129
|
+
const index = step.cases.findIndex(
|
|
130
|
+
(_, caseIndex) => fields.get(`cases[${caseIndex}].match`) === value,
|
|
131
|
+
);
|
|
132
|
+
if (index >= 0) {
|
|
133
|
+
const branch = `cases[${index}].steps`;
|
|
134
|
+
return { kind: StepActionKind.DONE, output: `cases[${index}]`, branch };
|
|
135
|
+
}
|
|
136
|
+
return step.default
|
|
137
|
+
? { kind: StepActionKind.DONE, output: 'default', branch: 'default' }
|
|
138
|
+
: { kind: StepActionKind.DONE, output: null };
|
|
139
|
+
}
|
|
140
|
+
case StepType.WAIT:
|
|
141
|
+
return executeWait(step, fields);
|
|
142
|
+
case StepType.SET: {
|
|
143
|
+
const value = fields.get('value');
|
|
144
|
+
return {
|
|
145
|
+
kind: StepActionKind.DONE,
|
|
146
|
+
output: value,
|
|
147
|
+
variable: { name: step.variable, value },
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
case StepType.CODE:
|
|
151
|
+
return { kind: StepActionKind.DONE, output: fields.get('source') };
|
|
152
|
+
case StepType.HTTP: {
|
|
153
|
+
const url = fields.get('url');
|
|
154
|
+
if (typeof url !== 'string') {
|
|
155
|
+
return fail('HTTP url must resolve to a string');
|
|
156
|
+
}
|
|
157
|
+
const headers = resolvedRecord('headers', Object.keys(step.headers ?? {}), fields);
|
|
158
|
+
const request: HttpRequest = {
|
|
159
|
+
method: step.method,
|
|
160
|
+
url,
|
|
161
|
+
headers: Object.fromEntries(
|
|
162
|
+
Object.entries(headers).map(([key, value]) => [key, headerValue(value)]),
|
|
163
|
+
),
|
|
164
|
+
};
|
|
165
|
+
if (step.body !== undefined) {
|
|
166
|
+
request.body = fields.get('body');
|
|
167
|
+
}
|
|
168
|
+
if (step.timeoutMs !== undefined) {
|
|
169
|
+
request.timeoutMs = step.timeoutMs;
|
|
170
|
+
}
|
|
171
|
+
return { kind: StepActionKind.HTTP, request };
|
|
172
|
+
}
|
|
173
|
+
case StepType.HOST:
|
|
174
|
+
return {
|
|
175
|
+
kind: StepActionKind.HOST,
|
|
176
|
+
action: step.action,
|
|
177
|
+
input: resolvedRecord('input', Object.keys(step.input), fields),
|
|
178
|
+
};
|
|
179
|
+
case StepType.LOG:
|
|
180
|
+
return executeLog(step, fields);
|
|
181
|
+
case StepType.RESPOND:
|
|
182
|
+
return executeRespond(step, fields);
|
|
183
|
+
case StepType.STOP:
|
|
184
|
+
return { kind: StepActionKind.STOP, status: step.status, output: fields.get('output') };
|
|
185
|
+
}
|
|
186
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { EvaluationKind } from '../runner-protocol/evaluation.types';
|
|
2
|
+
import type { RuntimeDefinition } from '../definition/definition.types';
|
|
3
|
+
import { walkSteps } from '../definition/walk-steps';
|
|
4
|
+
import { collectStepItems } from './step-fields';
|
|
5
|
+
|
|
6
|
+
const PLACEHOLDER = /\{\{([\s\S]*?)\}\}/g;
|
|
7
|
+
|
|
8
|
+
const escapeRegExp = (text: string): string => text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
9
|
+
|
|
10
|
+
/** The JavaScript an item runs: a template only runs what is inside its placeholders. */
|
|
11
|
+
const codeOf = (kind: EvaluationKind, source: string): string[] =>
|
|
12
|
+
kind === EvaluationKind.TEMPLATE
|
|
13
|
+
? [...source.matchAll(PLACEHOLDER)].map((match) => match[1])
|
|
14
|
+
: [source];
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The property names a definition reads statically from the host global `globalName`
|
|
18
|
+
* (`ExecuteMessage.globals`), as `<globalName>.<IDENT>`, `<globalName>?.<IDENT>` or
|
|
19
|
+
* `<globalName>['<IDENT>']` (either quote), in every expression, template placeholder and CODE
|
|
20
|
+
* source, branches included. Names come once each, in order of first appearance.
|
|
21
|
+
*
|
|
22
|
+
* It is a textual scan, so a string literal that spells such an access counts too: a host that
|
|
23
|
+
* resolves the names it finds only over-resolves. A dynamic access (`secrets[name]`) cannot be
|
|
24
|
+
* resolved ahead of time: it yields `undefined` at run time. `abyss.secrets.get(name)` is the
|
|
25
|
+
* dynamic path.
|
|
26
|
+
*/
|
|
27
|
+
export const collectGlobalReferences = (
|
|
28
|
+
definition: Pick<RuntimeDefinition, 'steps'>,
|
|
29
|
+
globalName: string,
|
|
30
|
+
): string[] => {
|
|
31
|
+
const name = escapeRegExp(globalName);
|
|
32
|
+
// Not preceded by an identifier character or a dot: `other.secrets.A` is not a read of `secrets`.
|
|
33
|
+
const access = new RegExp(
|
|
34
|
+
`(?<![\\w$.])${name}\\s*(?:\\??\\.\\s*([A-Za-z_$][\\w$]*)|\\[\\s*(['"])([A-Za-z_$][\\w$]*)\\2\\s*\\])`,
|
|
35
|
+
'g',
|
|
36
|
+
);
|
|
37
|
+
const found = new Set<string>();
|
|
38
|
+
walkSteps(definition.steps, (step) => {
|
|
39
|
+
for (const item of collectStepItems(step)) {
|
|
40
|
+
for (const code of codeOf(item.kind, item.source)) {
|
|
41
|
+
for (const match of code.matchAll(access)) {
|
|
42
|
+
found.add(match[1] ?? match[3]);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
return [...found];
|
|
48
|
+
};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { AccessKind } from '../access/access.types';
|
|
2
|
+
import type { AccessDeclaration } from '../access/access.types';
|
|
3
|
+
import { hostMatches } from '../access/host-match';
|
|
4
|
+
|
|
5
|
+
export enum AccessVerdict {
|
|
6
|
+
ALLOWED = 'ALLOWED',
|
|
7
|
+
NOT_DECLARED = 'NOT_DECLARED',
|
|
8
|
+
NOT_GRANTED = 'NOT_GRANTED',
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const coversHost = (accesses: AccessDeclaration[], host: string): boolean =>
|
|
12
|
+
accesses.some(
|
|
13
|
+
(access) =>
|
|
14
|
+
access.kind === AccessKind.HTTP && access.hosts.some((pattern) => hostMatches(host, pattern)),
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
const coversAction = (accesses: AccessDeclaration[], action: string): boolean =>
|
|
18
|
+
accesses.some((access) => access.kind === AccessKind.HOST_ACTION && access.action === action);
|
|
19
|
+
|
|
20
|
+
const verdict = (declared: boolean, granted: boolean): AccessVerdict => {
|
|
21
|
+
if (!declared) {
|
|
22
|
+
return AccessVerdict.NOT_DECLARED;
|
|
23
|
+
}
|
|
24
|
+
return granted ? AccessVerdict.ALLOWED : AccessVerdict.NOT_GRANTED;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const hostOf = (url: string): string | null => {
|
|
28
|
+
try {
|
|
29
|
+
const parsed = new URL(url);
|
|
30
|
+
return parsed.protocol === 'http:' || parsed.protocol === 'https:' ? parsed.hostname : null;
|
|
31
|
+
} catch {
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* An HTTP effect needs its url's host in a declaration AND in a grant; an unparsable url or one
|
|
38
|
+
* whose scheme is not http(s) (`file:`, `ftp:`...) is never covered.
|
|
39
|
+
*/
|
|
40
|
+
export const checkHttpAccess = (
|
|
41
|
+
url: string,
|
|
42
|
+
declared: AccessDeclaration[],
|
|
43
|
+
granted: AccessDeclaration[],
|
|
44
|
+
): AccessVerdict => {
|
|
45
|
+
const host = hostOf(url);
|
|
46
|
+
if (host === null) {
|
|
47
|
+
return AccessVerdict.NOT_DECLARED;
|
|
48
|
+
}
|
|
49
|
+
return verdict(coversHost(declared, host), coversHost(granted, host));
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export const checkHostActionAccess = (
|
|
53
|
+
action: string,
|
|
54
|
+
declared: AccessDeclaration[],
|
|
55
|
+
granted: AccessDeclaration[],
|
|
56
|
+
): AccessVerdict => verdict(coversAction(declared, action), coversAction(granted, action));
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { EvaluationKind } from '../runner-protocol/evaluation.types';
|
|
2
|
+
import type { EvaluationItem } from '../runner-protocol/evaluation.types';
|
|
3
|
+
import { StepType } from '../definition/definition.types';
|
|
4
|
+
import type { Step, ValueSource } from '../definition/definition.types';
|
|
5
|
+
import { collectItems, propertyPath, resolveValue } from './value-source';
|
|
6
|
+
|
|
7
|
+
interface StepField {
|
|
8
|
+
path: string;
|
|
9
|
+
source: ValueSource;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Resolved top-level fields of a step, by field path (`url`, `headers.Authorization`...). */
|
|
13
|
+
export type ResolvedFields = ReadonlyMap<string, unknown>;
|
|
14
|
+
|
|
15
|
+
const recordFields = (base: string, record: Record<string, ValueSource> | undefined): StepField[] =>
|
|
16
|
+
Object.entries(record ?? {}).map(([key, source]) => ({ path: propertyPath(base, key), source }));
|
|
17
|
+
|
|
18
|
+
const optionalField = (path: string, source: ValueSource | undefined): StepField[] =>
|
|
19
|
+
source === undefined ? [] : [{ path, source }];
|
|
20
|
+
|
|
21
|
+
/** The value sources of a step, each under the path used as evaluation item id. */
|
|
22
|
+
const stepFields = (step: Step): StepField[] => {
|
|
23
|
+
switch (step.type) {
|
|
24
|
+
case StepType.CONDITION:
|
|
25
|
+
return [{ path: 'if', source: step.if }];
|
|
26
|
+
case StepType.SWITCH:
|
|
27
|
+
return [
|
|
28
|
+
{ path: 'value', source: step.value },
|
|
29
|
+
...step.cases.map((switchCase, index) => ({
|
|
30
|
+
path: `cases[${index}].match`,
|
|
31
|
+
source: switchCase.match,
|
|
32
|
+
})),
|
|
33
|
+
];
|
|
34
|
+
case StepType.WAIT:
|
|
35
|
+
return [
|
|
36
|
+
...optionalField('durationMs', step.durationMs),
|
|
37
|
+
...optionalField('until', step.until),
|
|
38
|
+
];
|
|
39
|
+
case StepType.SET:
|
|
40
|
+
return [{ path: 'value', source: step.value }];
|
|
41
|
+
case StepType.CODE:
|
|
42
|
+
return [];
|
|
43
|
+
case StepType.HTTP:
|
|
44
|
+
return [
|
|
45
|
+
{ path: 'url', source: step.url },
|
|
46
|
+
...recordFields('headers', step.headers),
|
|
47
|
+
...optionalField('body', step.body),
|
|
48
|
+
];
|
|
49
|
+
case StepType.HOST:
|
|
50
|
+
return recordFields('input', step.input);
|
|
51
|
+
case StepType.LOG:
|
|
52
|
+
return [{ path: 'message', source: step.message }, ...optionalField('data', step.data)];
|
|
53
|
+
case StepType.RESPOND:
|
|
54
|
+
return [
|
|
55
|
+
{ path: 'status', source: step.status },
|
|
56
|
+
...recordFields('headers', step.headers),
|
|
57
|
+
...optionalField('body', step.body),
|
|
58
|
+
];
|
|
59
|
+
case StepType.STOP:
|
|
60
|
+
return optionalField('output', step.output);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/** Every non-literal value of the step, to evaluate in one lot. A CODE step always has one item. */
|
|
65
|
+
export const collectStepItems = (step: Step): EvaluationItem[] => {
|
|
66
|
+
if (step.type === StepType.CODE) {
|
|
67
|
+
return [{ id: 'source', kind: EvaluationKind.CODE, source: step.source }];
|
|
68
|
+
}
|
|
69
|
+
const items: EvaluationItem[] = [];
|
|
70
|
+
for (const field of stepFields(step)) {
|
|
71
|
+
collectItems(field.path, field.source, items);
|
|
72
|
+
}
|
|
73
|
+
return items;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/** `results` maps item ids to evaluated values; a CODE step resolves to its single `source` item. */
|
|
77
|
+
export const resolveStepFields = (
|
|
78
|
+
step: Step,
|
|
79
|
+
results: ReadonlyMap<string, unknown>,
|
|
80
|
+
): ResolvedFields => {
|
|
81
|
+
if (step.type === StepType.CODE) {
|
|
82
|
+
return new Map([['source', results.get('source')]]);
|
|
83
|
+
}
|
|
84
|
+
return new Map(
|
|
85
|
+
stepFields(step).map((field) => [field.path, resolveValue(field.path, field.source, results)]),
|
|
86
|
+
);
|
|
87
|
+
};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { EvaluationKind } from '../runner-protocol/evaluation.types';
|
|
2
|
+
import type { EvaluationItem } from '../runner-protocol/evaluation.types';
|
|
3
|
+
import type { ExpressionValue, ValueSource } from '../definition/definition.types';
|
|
4
|
+
|
|
5
|
+
const TEMPLATE_PATTERN = /\{\{[\s\S]*?\}\}/;
|
|
6
|
+
const IDENTIFIER_KEY = /^[A-Za-z_$][\w$-]*$/;
|
|
7
|
+
|
|
8
|
+
export const isExpressionValue = (value: ValueSource): value is ExpressionValue =>
|
|
9
|
+
typeof value === 'object' &&
|
|
10
|
+
value !== null &&
|
|
11
|
+
!Array.isArray(value) &&
|
|
12
|
+
Object.keys(value).length === 1 &&
|
|
13
|
+
typeof (value as ExpressionValue).$expression === 'string';
|
|
14
|
+
|
|
15
|
+
/** Field path of a property, unambiguous whatever the key: `a.b`, `a["x.y"]`. */
|
|
16
|
+
export const propertyPath = (base: string, key: string): string =>
|
|
17
|
+
IDENTIFIER_KEY.test(key) ? `${base}.${key}` : `${base}[${JSON.stringify(key)}]`;
|
|
18
|
+
|
|
19
|
+
const indexPath = (base: string, index: number): string => `${base}[${index}]`;
|
|
20
|
+
|
|
21
|
+
/** Appends one item per non-literal value found in `source`; the item id is its field path. */
|
|
22
|
+
export const collectItems = (path: string, source: ValueSource, items: EvaluationItem[]): void => {
|
|
23
|
+
if (typeof source === 'string') {
|
|
24
|
+
if (TEMPLATE_PATTERN.test(source)) {
|
|
25
|
+
items.push({ id: path, kind: EvaluationKind.TEMPLATE, source });
|
|
26
|
+
}
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
if (source === null || typeof source !== 'object') {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
if (isExpressionValue(source)) {
|
|
33
|
+
items.push({ id: path, kind: EvaluationKind.EXPRESSION, source: source.$expression });
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
if (Array.isArray(source)) {
|
|
37
|
+
source.forEach((entry, index) => collectItems(indexPath(path, index), entry, items));
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
for (const [key, entry] of Object.entries(source)) {
|
|
41
|
+
collectItems(propertyPath(path, key), entry, items);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/** Rebuilds `source` with every non-literal value replaced by its evaluated result. */
|
|
46
|
+
export const resolveValue = (
|
|
47
|
+
path: string,
|
|
48
|
+
source: ValueSource,
|
|
49
|
+
results: ReadonlyMap<string, unknown>,
|
|
50
|
+
): unknown => {
|
|
51
|
+
if (typeof source === 'string') {
|
|
52
|
+
return TEMPLATE_PATTERN.test(source) ? results.get(path) : source;
|
|
53
|
+
}
|
|
54
|
+
if (source === null || typeof source !== 'object') {
|
|
55
|
+
return source;
|
|
56
|
+
}
|
|
57
|
+
if (isExpressionValue(source)) {
|
|
58
|
+
return results.get(path);
|
|
59
|
+
}
|
|
60
|
+
if (Array.isArray(source)) {
|
|
61
|
+
return source.map((entry, index) => resolveValue(indexPath(path, index), entry, results));
|
|
62
|
+
}
|
|
63
|
+
return Object.fromEntries(
|
|
64
|
+
Object.entries(source).map(([key, entry]) => [
|
|
65
|
+
key,
|
|
66
|
+
resolveValue(propertyPath(path, key), entry, results),
|
|
67
|
+
]),
|
|
68
|
+
);
|
|
69
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
// Perimetre de VERIFICATION, distinct de celui du build.
|
|
3
|
+
//
|
|
4
|
+
// `tsconfig.json` exclut `src/**/*.spec.ts` parce qu'il sert a emettre `dist/`, ou une spec
|
|
5
|
+
// n'a rien a faire. Mais eslint s'appuie sur ce meme fichier via `parserOptions.project` :
|
|
6
|
+
// les cinq specs du paquet sortaient donc en « Parsing error: that TSConfig does not include
|
|
7
|
+
// this file », mesure du 2026-09-09, et n'etaient lintees par personne.
|
|
8
|
+
//
|
|
9
|
+
// Ce fichier sert uniquement a eslint. Il n'emet rien et ne change pas ce que `build` produit.
|
|
10
|
+
"extends": "./tsconfig.json",
|
|
11
|
+
"compilerOptions": {
|
|
12
|
+
"noEmit": true
|
|
13
|
+
},
|
|
14
|
+
"exclude": ["./dist/**/*"]
|
|
15
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"incremental": false,
|
|
4
|
+
"target": "es2021",
|
|
5
|
+
"lib": [
|
|
6
|
+
"dom",
|
|
7
|
+
"es2021"
|
|
8
|
+
],
|
|
9
|
+
"module": "commonjs",
|
|
10
|
+
"rootDir": "src",
|
|
11
|
+
"moduleResolution": "node",
|
|
12
|
+
"resolveJsonModule": true,
|
|
13
|
+
"declaration": true,
|
|
14
|
+
"sourceMap": true,
|
|
15
|
+
"outDir": "./dist",
|
|
16
|
+
"removeComments": true,
|
|
17
|
+
"allowSyntheticDefaultImports": true,
|
|
18
|
+
"esModuleInterop": true,
|
|
19
|
+
"forceConsistentCasingInFileNames": true,
|
|
20
|
+
"strict": true,
|
|
21
|
+
"strictNullChecks": true,
|
|
22
|
+
"strictFunctionTypes": true,
|
|
23
|
+
"strictPropertyInitialization": false,
|
|
24
|
+
"skipLibCheck": true
|
|
25
|
+
},
|
|
26
|
+
"include": [
|
|
27
|
+
"./src"
|
|
28
|
+
],
|
|
29
|
+
"exclude": [
|
|
30
|
+
"./dist/**/*",
|
|
31
|
+
"./src/**/*.spec.ts",
|
|
32
|
+
"./src/**/*.test.ts",
|
|
33
|
+
"./src/**/__tests__/**"
|
|
34
|
+
]
|
|
35
|
+
}
|