@abyss-project/runtime 3.0.0 → 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +31 -0
- package/dist/discord/discord.types.d.ts +135 -0
- package/dist/discord/discord.types.js +59 -0
- package/dist/discord/discord.types.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/runner-protocol/execution-protocol.types.js.map +1 -1
- package/package.json +1 -1
- package/scripts/build-engine.cjs +70 -70
- package/src/__tests__/engine-bundle.test.ts +121 -121
- package/src/cost/__tests__/execution-cost.test.ts +196 -196
- package/src/cost/execution-cost.ts +186 -186
- package/src/definition/__tests__/validate-definition.test.ts +354 -354
- package/src/definition/definition.limits.ts +9 -9
- package/src/definition/definition.schema.ts +172 -172
- package/src/definition/definition.types.ts +171 -171
- package/src/discord/discord.types.ts +181 -0
- package/src/engine/__tests__/run-in-isolate.test.ts +468 -468
- package/src/engine/engine-entry.ts +3 -3
- package/src/engine/engine-global.ts +2 -2
- package/src/engine/run-in-isolate.ts +404 -404
- package/src/errors/runtime-error-code.enum.ts +37 -37
- package/src/index.ts +31 -30
- package/src/interpreter/__tests__/interpreter.test.ts +854 -854
- package/src/interpreter/__tests__/log-respond-resume.test.ts +223 -223
- package/src/interpreter/interpreter.ts +461 -461
- package/src/interpreter/interpreter.types.ts +192 -192
- package/src/interpreter/test-host.ts +85 -85
- package/src/runner-protocol/evaluation.types.ts +21 -21
- package/src/runner-protocol/execution-protocol.types.ts +231 -225
- 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 +133 -113
- package/src/steps/execute-step.ts +186 -186
- package/src/steps/global-references.ts +48 -48
- package/src/steps/step-access.ts +56 -56
- package/src/steps/step-fields.ts +87 -87
- package/src/steps/value-source.ts +69 -69
|
@@ -1,48 +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
|
-
};
|
|
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
|
+
};
|
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
|
@@ -1,87 +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
|
-
};
|
|
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
|
+
};
|
|
@@ -1,69 +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
|
-
};
|
|
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
|
+
};
|