@abyss-project/runtime 3.12.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/.prettierrc +5 -5
- package/README.md +88 -88
- 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/discord/discord.types.d.ts +2 -34
- package/dist/discord/discord.types.js +1 -9
- package/dist/discord/discord.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 -88
- 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 -108
- package/src/discord/discord.types.ts +696 -738
- 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,186 +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
|
-
};
|
|
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
|
+
};
|
|
@@ -1,48 +1,56 @@
|
|
|
1
|
-
import { EvaluationKind } from '../runner-protocol/evaluation.types';
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
'
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
1
|
+
import { EvaluationKind } from '../runner-protocol/evaluation.types';
|
|
2
|
+
import { StepType } from '../definition/definition.types';
|
|
3
|
+
import type { RuntimeDefinition } from '../definition/definition.types';
|
|
4
|
+
import { walkSteps } from '../definition/walk-steps';
|
|
5
|
+
import { collectStepItems } from './step-fields';
|
|
6
|
+
|
|
7
|
+
const PLACEHOLDER = /\{\{([\s\S]*?)\}\}/g;
|
|
8
|
+
|
|
9
|
+
const escapeRegExp = (text: string): string => text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
10
|
+
|
|
11
|
+
/** The JavaScript an item runs: a template only runs what is inside its placeholders. */
|
|
12
|
+
const codeOf = (kind: EvaluationKind, source: string): string[] =>
|
|
13
|
+
kind === EvaluationKind.TEMPLATE
|
|
14
|
+
? [...source.matchAll(PLACEHOLDER)].map((match) => match[1])
|
|
15
|
+
: [source];
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The property names a definition reads statically from the host global `globalName`
|
|
19
|
+
* (`ExecuteMessage.globals`), as `<globalName>.<IDENT>`, `<globalName>?.<IDENT>` or
|
|
20
|
+
* `<globalName>['<IDENT>']` (either quote), in every expression, template placeholder and CODE
|
|
21
|
+
* source, branches included. Names come once each, in order of first appearance.
|
|
22
|
+
*
|
|
23
|
+
* It is a textual scan, so a string literal that spells such an access counts too: a host that
|
|
24
|
+
* resolves the names it finds only over-resolves. A dynamic access (`secrets[name]`) cannot be
|
|
25
|
+
* resolved ahead of time: it yields `undefined` at run time. `abyss.secrets.get(name)` is the
|
|
26
|
+
* dynamic path.
|
|
27
|
+
*/
|
|
28
|
+
export const collectGlobalReferences = (
|
|
29
|
+
definition: Pick<RuntimeDefinition, 'steps'>,
|
|
30
|
+
globalName: string,
|
|
31
|
+
): string[] => {
|
|
32
|
+
const name = escapeRegExp(globalName);
|
|
33
|
+
// Not preceded by an identifier character or a dot: `other.secrets.A` is not a read of `secrets`.
|
|
34
|
+
const access = new RegExp(
|
|
35
|
+
`(?<![\\w$.])${name}\\s*(?:\\??\\.\\s*([A-Za-z_$][\\w$]*)|\\[\\s*(['"])([A-Za-z_$][\\w$]*)\\2\\s*\\])`,
|
|
36
|
+
'g',
|
|
37
|
+
);
|
|
38
|
+
const found = new Set<string>();
|
|
39
|
+
walkSteps(definition.steps, (step) => {
|
|
40
|
+
// A compiled CODE step embeds library code: its own source is what the user wrote.
|
|
41
|
+
if (step.type === StepType.CODE) {
|
|
42
|
+
for (const match of step.source.matchAll(access)) {
|
|
43
|
+
found.add(match[1] ?? match[3]);
|
|
44
|
+
}
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
for (const item of collectStepItems(step)) {
|
|
48
|
+
for (const code of codeOf(item.kind, item.source)) {
|
|
49
|
+
for (const match of code.matchAll(access)) {
|
|
50
|
+
found.add(match[1] ?? match[3]);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
return [...found];
|
|
56
|
+
};
|
package/src/steps/step-access.ts
CHANGED
|
@@ -1,56 +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));
|
|
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));
|
package/src/steps/step-fields.ts
CHANGED
|
@@ -64,6 +64,16 @@ const stepFields = (step: Step): StepField[] => {
|
|
|
64
64
|
/** Every non-literal value of the step, to evaluate in one lot. A CODE step always has one item. */
|
|
65
65
|
export const collectStepItems = (step: Step): EvaluationItem[] => {
|
|
66
66
|
if (step.type === StepType.CODE) {
|
|
67
|
+
if (step.compiled) {
|
|
68
|
+
return [
|
|
69
|
+
{
|
|
70
|
+
id: 'source',
|
|
71
|
+
kind: EvaluationKind.CODE,
|
|
72
|
+
source: step.compiled.code,
|
|
73
|
+
sourceMap: step.compiled.sourceMap,
|
|
74
|
+
},
|
|
75
|
+
];
|
|
76
|
+
}
|
|
67
77
|
return [{ id: 'source', kind: EvaluationKind.CODE, source: step.source }];
|
|
68
78
|
}
|
|
69
79
|
const items: EvaluationItem[] = [];
|