@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,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.resolveValue = exports.collectItems = exports.propertyPath = exports.isExpressionValue = void 0;
|
|
4
|
+
const runner_protocol_types_1 = require("../runner-protocol/runner-protocol.types");
|
|
5
|
+
const TEMPLATE_PATTERN = /\{\{[\s\S]*?\}\}/;
|
|
6
|
+
const IDENTIFIER_KEY = /^[A-Za-z_$][\w$-]*$/;
|
|
7
|
+
const isExpressionValue = (value) => typeof value === 'object' &&
|
|
8
|
+
value !== null &&
|
|
9
|
+
!Array.isArray(value) &&
|
|
10
|
+
Object.keys(value).length === 1 &&
|
|
11
|
+
typeof value.$expression === 'string';
|
|
12
|
+
exports.isExpressionValue = isExpressionValue;
|
|
13
|
+
const propertyPath = (base, key) => IDENTIFIER_KEY.test(key) ? `${base}.${key}` : `${base}[${JSON.stringify(key)}]`;
|
|
14
|
+
exports.propertyPath = propertyPath;
|
|
15
|
+
const indexPath = (base, index) => `${base}[${index}]`;
|
|
16
|
+
const collectItems = (path, source, items) => {
|
|
17
|
+
if (typeof source === 'string') {
|
|
18
|
+
if (TEMPLATE_PATTERN.test(source)) {
|
|
19
|
+
items.push({ id: path, kind: runner_protocol_types_1.EvaluationKind.TEMPLATE, source });
|
|
20
|
+
}
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
if (source === null || typeof source !== 'object') {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
if ((0, exports.isExpressionValue)(source)) {
|
|
27
|
+
items.push({ id: path, kind: runner_protocol_types_1.EvaluationKind.EXPRESSION, source: source.$expression });
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
if (Array.isArray(source)) {
|
|
31
|
+
source.forEach((entry, index) => (0, exports.collectItems)(indexPath(path, index), entry, items));
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
for (const [key, entry] of Object.entries(source)) {
|
|
35
|
+
(0, exports.collectItems)((0, exports.propertyPath)(path, key), entry, items);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
exports.collectItems = collectItems;
|
|
39
|
+
const resolveValue = (path, source, results) => {
|
|
40
|
+
if (typeof source === 'string') {
|
|
41
|
+
return TEMPLATE_PATTERN.test(source) ? results.get(path) : source;
|
|
42
|
+
}
|
|
43
|
+
if (source === null || typeof source !== 'object') {
|
|
44
|
+
return source;
|
|
45
|
+
}
|
|
46
|
+
if ((0, exports.isExpressionValue)(source)) {
|
|
47
|
+
return results.get(path);
|
|
48
|
+
}
|
|
49
|
+
if (Array.isArray(source)) {
|
|
50
|
+
return source.map((entry, index) => (0, exports.resolveValue)(indexPath(path, index), entry, results));
|
|
51
|
+
}
|
|
52
|
+
return Object.fromEntries(Object.entries(source).map(([key, entry]) => [
|
|
53
|
+
key,
|
|
54
|
+
(0, exports.resolveValue)((0, exports.propertyPath)(path, key), entry, results),
|
|
55
|
+
]));
|
|
56
|
+
};
|
|
57
|
+
exports.resolveValue = resolveValue;
|
|
58
|
+
//# sourceMappingURL=value-source.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"value-source.js","sourceRoot":"","sources":["../../src/steps/value-source.ts"],"names":[],"mappings":";;;AAAA,oFAA0E;AAI1E,MAAM,gBAAgB,GAAG,kBAAkB,CAAC;AAC5C,MAAM,cAAc,GAAG,qBAAqB,CAAC;AAEtC,MAAM,iBAAiB,GAAG,CAAC,KAAkB,EAA4B,EAAE,CAChF,OAAO,KAAK,KAAK,QA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AAC,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC;IAC5F,CAAC;IACD,OAAO,MAAM,CAAC,WAAW,CACvB,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC;QAC3C,GAAG;QACH,IAAA,oBAAY,EAAC,IAAA,oBAAY,EAAC,IAAI,EAAE,GAAG,CAAC,EAAE,KAAK,EAAE,OAAO,CAAC;KACtD,CAAC,CACH,CAAC;AACJ,CAAC,CAAC;AAvBW,QAAA,YAAY,gBAuBvB"}
|
package/jest.config.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Config } from '@jest/types';
|
|
2
|
+
|
|
3
|
+
const config: Config.InitialOptions = {
|
|
4
|
+
testEnvironment: 'node',
|
|
5
|
+
transform: {
|
|
6
|
+
'^.+\\.tsx?$': ['ts-jest', { isolatedModules: true }],
|
|
7
|
+
},
|
|
8
|
+
modulePathIgnorePatterns: ['dist'],
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export default config;
|
package/package.json
CHANGED
|
@@ -1,6 +1,63 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@abyss-project/runtime",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Abyss automation core: definitions, interpreter, runner protocol",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"default": "./dist/index.js"
|
|
11
|
+
},
|
|
12
|
+
"./server": {
|
|
13
|
+
"types": "./dist/server/index.d.ts",
|
|
14
|
+
"default": "./dist/server/index.js"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"typesVersions": {
|
|
18
|
+
"*": {
|
|
19
|
+
"server": [
|
|
20
|
+
"dist/server/index.d.ts"
|
|
21
|
+
]
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "https://gitlab.com/abyss-group/abyss-runtime.git"
|
|
27
|
+
},
|
|
28
|
+
"keywords": [],
|
|
29
|
+
"author": "scylla",
|
|
30
|
+
"license": "ISC",
|
|
31
|
+
"engines": {
|
|
32
|
+
"node": ">=19.8"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"joi": "^17.13.3",
|
|
36
|
+
"ws": "^8"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@jest/types": "^29.6.0",
|
|
40
|
+
"@types/jest": "^29.5.5",
|
|
41
|
+
"@types/node": "^20.11.0",
|
|
42
|
+
"@types/ws": "^8",
|
|
43
|
+
"@typescript-eslint/eslint-plugin": "8.71.0",
|
|
44
|
+
"@typescript-eslint/parser": "8.71.0",
|
|
45
|
+
"eslint": "^8.57.1",
|
|
46
|
+
"eslint-config-prettier": "^8.3.0",
|
|
47
|
+
"eslint-plugin-filenames-simple": "^0.7.0",
|
|
48
|
+
"eslint-plugin-prettier": "^3.4.0",
|
|
49
|
+
"jest": "^29.7.0",
|
|
50
|
+
"prettier": "2.3.1",
|
|
51
|
+
"ts-jest": "^29.1.0",
|
|
52
|
+
"ts-node": "^10.4.0",
|
|
53
|
+
"typescript": "5.9.3"
|
|
54
|
+
},
|
|
55
|
+
"scripts": {
|
|
56
|
+
"lint": "eslint \"{src,apps,libs,test,core}/**/*.ts\" --fix",
|
|
57
|
+
"lint:check": "eslint \"{src,apps,libs,test,core}/**/*.ts\" --max-warnings 0",
|
|
58
|
+
"tsc": "tsc",
|
|
59
|
+
"build": "tsc",
|
|
60
|
+
"test": "jest",
|
|
61
|
+
"dev": "tsc --watch"
|
|
62
|
+
}
|
|
6
63
|
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { builtinModules } from 'module';
|
|
2
|
+
import * as fs from 'fs';
|
|
3
|
+
import * as path from 'path';
|
|
4
|
+
|
|
5
|
+
const SRC_DIR = path.resolve(__dirname, '..');
|
|
6
|
+
const SERVER_DIR = path.join(SRC_DIR, 'server');
|
|
7
|
+
const TESTS_DIR = path.join(SRC_DIR, '__tests__');
|
|
8
|
+
|
|
9
|
+
// `ws` and every Node built-in module (`fs`, `net`, `http`, `node:*`...) are forbidden outside
|
|
10
|
+
// `src/server`, so that the package root stays usable in a browser.
|
|
11
|
+
const FORBIDDEN = new Set<string>(['ws', ...builtinModules]);
|
|
12
|
+
|
|
13
|
+
const walkTsFiles = (dir: string): string[] =>
|
|
14
|
+
fs.readdirSync(dir, { withFileTypes: true }).flatMap((entry) => {
|
|
15
|
+
const full = path.join(dir, entry.name);
|
|
16
|
+
if (entry.isDirectory()) {
|
|
17
|
+
return full === SERVER_DIR || full === TESTS_DIR ? [] : walkTsFiles(full);
|
|
18
|
+
}
|
|
19
|
+
return entry.name.endsWith('.ts') && !entry.name.endsWith('.d.ts') ? [full] : [];
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
const IMPORT_PATTERNS = [
|
|
23
|
+
/\bfrom\s*['"]([^'"]+)['"]/g,
|
|
24
|
+
/\bimport\s*['"]([^'"]+)['"]/g,
|
|
25
|
+
/\bimport\s*\(\s*['"]([^'"]+)['"]\s*\)/g,
|
|
26
|
+
/\brequire\s*\(\s*['"]([^'"]+)['"]\s*\)/g,
|
|
27
|
+
];
|
|
28
|
+
|
|
29
|
+
const importedSpecifiers = (text: string): string[] =>
|
|
30
|
+
IMPORT_PATTERNS.flatMap((pattern) => [...text.matchAll(pattern)].map((match) => match[1]));
|
|
31
|
+
|
|
32
|
+
const isForbidden = (specifier: string): boolean => {
|
|
33
|
+
const bare = specifier.startsWith('node:') ? specifier.slice('node:'.length) : specifier;
|
|
34
|
+
return FORBIDDEN.has(bare) || FORBIDDEN.has(bare.split('/')[0]);
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
describe('browser-compatible package root', () => {
|
|
38
|
+
it('imports no Node built-in nor ws outside src/server', () => {
|
|
39
|
+
const offenders = walkTsFiles(SRC_DIR).flatMap((file) =>
|
|
40
|
+
importedSpecifiers(fs.readFileSync(file, 'utf8'))
|
|
41
|
+
.filter(isForbidden)
|
|
42
|
+
.map((specifier) => `${path.relative(SRC_DIR, file)} imports ${specifier}`),
|
|
43
|
+
);
|
|
44
|
+
expect(offenders).toEqual([]);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('detects forbidden imports (guard sanity)', () => {
|
|
48
|
+
const source = "import { WebSocket } from 'ws';\nconst n = require('node:net');";
|
|
49
|
+
expect(importedSpecifiers(source)).toEqual(['ws', 'node:net']);
|
|
50
|
+
expect(['ws', 'node:net', 'fs/promises', 'http'].every(isForbidden)).toBe(true);
|
|
51
|
+
expect(['joi', './steps', '../access'].some(isForbidden)).toBe(false);
|
|
52
|
+
});
|
|
53
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { hostMatches } from '../host-match';
|
|
2
|
+
|
|
3
|
+
describe('hostMatches', () => {
|
|
4
|
+
it('matches an exact host', () => {
|
|
5
|
+
expect(hostMatches('api.example.com', 'api.example.com')).toBe(true);
|
|
6
|
+
expect(hostMatches('api.example.com', 'example.com')).toBe(false);
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
it('lets a wildcard cover any sub-domain', () => {
|
|
10
|
+
expect(hostMatches('api.example.com', '*.example.com')).toBe(true);
|
|
11
|
+
expect(hostMatches('a.b.example.com', '*.example.com')).toBe(true);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('does not let a wildcard cover the apex domain', () => {
|
|
15
|
+
expect(hostMatches('example.com', '*.example.com')).toBe(false);
|
|
16
|
+
expect(hostMatches('.example.com', '*.example.com')).toBe(false);
|
|
17
|
+
expect(hostMatches('badexample.com', '*.example.com')).toBe(false);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('ignores case', () => {
|
|
21
|
+
expect(hostMatches('API.Example.COM', 'api.example.com')).toBe(true);
|
|
22
|
+
expect(hostMatches('api.example.com', '*.EXAMPLE.com')).toBe(true);
|
|
23
|
+
});
|
|
24
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export enum AccessKind {
|
|
2
|
+
/** Outbound HTTP to a list of host names (exact, or `*.domain` for any sub-domain). */
|
|
3
|
+
HTTP = 'HTTP',
|
|
4
|
+
/** One action of the host catalog, by name. */
|
|
5
|
+
HOST_ACTION = 'HOST_ACTION',
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface HttpAccess {
|
|
9
|
+
kind: AccessKind.HTTP;
|
|
10
|
+
hosts: string[];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface HostActionAccess {
|
|
14
|
+
kind: AccessKind.HOST_ACTION;
|
|
15
|
+
action: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* What a definition declares it needs (like the `permissions:` block of GitHub Actions), and,
|
|
20
|
+
* with the same shape, what a run is actually granted. An effect is emitted only when both
|
|
21
|
+
* cover it.
|
|
22
|
+
*/
|
|
23
|
+
export type AccessDeclaration = HttpAccess | HostActionAccess;
|
|
24
|
+
|
|
25
|
+
export type AccessGrant = AccessDeclaration;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether `host` is covered by an access `pattern`: an exact host name, or `*.domain`, which
|
|
3
|
+
* covers any sub-domain of `domain` but not `domain` itself. Comparison ignores case.
|
|
4
|
+
*/
|
|
5
|
+
export const hostMatches = (host: string, pattern: string): boolean => {
|
|
6
|
+
const candidate = host.toLowerCase();
|
|
7
|
+
const expected = pattern.toLowerCase();
|
|
8
|
+
if (!expected.startsWith('*.')) {
|
|
9
|
+
return candidate === expected;
|
|
10
|
+
}
|
|
11
|
+
const suffix = expected.slice(1);
|
|
12
|
+
return candidate.length > suffix.length && candidate.endsWith(suffix);
|
|
13
|
+
};
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
import Joi from 'joi';
|
|
2
|
+
import { AccessKind } from '../../access/access.types';
|
|
3
|
+
import { CURRENT_FORMAT_VERSION, HttpMethod, StepType, StopStatus } from '../definition.types';
|
|
4
|
+
import type { HostActionCatalog } from '../definition.types';
|
|
5
|
+
import { MAX_WAIT_DURATION_MS } from '../definition.schema';
|
|
6
|
+
import { validateDefinition } from '../validate-definition';
|
|
7
|
+
import { walkSteps } from '../walk-steps';
|
|
8
|
+
|
|
9
|
+
const catalog: HostActionCatalog = new Map([
|
|
10
|
+
[
|
|
11
|
+
'send-email',
|
|
12
|
+
{
|
|
13
|
+
name: 'send-email',
|
|
14
|
+
inputSchema: Joi.object({ to: Joi.string().required(), subject: Joi.string() }),
|
|
15
|
+
},
|
|
16
|
+
],
|
|
17
|
+
]);
|
|
18
|
+
|
|
19
|
+
const clone = <T>(value: T): T => JSON.parse(JSON.stringify(value));
|
|
20
|
+
|
|
21
|
+
const validDefinition = () => ({
|
|
22
|
+
formatVersion: CURRENT_FORMAT_VERSION,
|
|
23
|
+
accesses: [
|
|
24
|
+
{ kind: AccessKind.HTTP, hosts: ['api.example.com', '*.hooks.example.com'] },
|
|
25
|
+
{ kind: AccessKind.HOST_ACTION, action: 'send-email' },
|
|
26
|
+
],
|
|
27
|
+
steps: [
|
|
28
|
+
{ id: 'init', type: StepType.SET, variable: 'count', value: 0 },
|
|
29
|
+
{
|
|
30
|
+
id: 'check',
|
|
31
|
+
type: StepType.CONDITION,
|
|
32
|
+
if: { $expression: 'input.amount > 10' },
|
|
33
|
+
then: [
|
|
34
|
+
{
|
|
35
|
+
id: 'fetch',
|
|
36
|
+
type: StepType.HTTP,
|
|
37
|
+
method: HttpMethod.POST,
|
|
38
|
+
url: 'https://api.example.com/v1/items',
|
|
39
|
+
headers: { Authorization: 'Bearer {{ input.token }}', 'X-List': ['a', { b: 1 }] },
|
|
40
|
+
body: { items: [1, 2, { $expression: 'input.items' }] },
|
|
41
|
+
timeoutMs: 5000,
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
id: 'mail',
|
|
45
|
+
type: StepType.HOST,
|
|
46
|
+
action: 'send-email',
|
|
47
|
+
input: { to: '{{ input.email }}', subject: 'Hello' },
|
|
48
|
+
},
|
|
49
|
+
],
|
|
50
|
+
else: [{ id: 'pause', type: StepType.WAIT, durationMs: 1000 }],
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
id: 'route',
|
|
54
|
+
type: StepType.SWITCH,
|
|
55
|
+
value: '{{ input.kind }}',
|
|
56
|
+
cases: [
|
|
57
|
+
{
|
|
58
|
+
match: 'a',
|
|
59
|
+
steps: [
|
|
60
|
+
{ id: 'compute', type: StepType.CODE, source: 'return input.n * 2;' },
|
|
61
|
+
{ id: 'later', type: StepType.WAIT, until: '2026-12-31T00:00:00Z' },
|
|
62
|
+
],
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
match: null,
|
|
66
|
+
steps: [
|
|
67
|
+
{
|
|
68
|
+
id: 'hook',
|
|
69
|
+
type: StepType.HTTP,
|
|
70
|
+
method: HttpMethod.GET,
|
|
71
|
+
url: 'https://x.hooks.example.com/ping',
|
|
72
|
+
},
|
|
73
|
+
],
|
|
74
|
+
},
|
|
75
|
+
],
|
|
76
|
+
default: [{ id: 'fail', type: StepType.STOP, status: StopStatus.FAILED, output: 'unknown' }],
|
|
77
|
+
},
|
|
78
|
+
{ id: 'done', type: StepType.STOP, status: StopStatus.SUCCEEDED },
|
|
79
|
+
],
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
const errorsOf = (raw: unknown) => {
|
|
83
|
+
const result = validateDefinition(raw, catalog);
|
|
84
|
+
if (result.ok) {
|
|
85
|
+
throw new Error('expected a refusal');
|
|
86
|
+
}
|
|
87
|
+
return result.errors;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const pathsOf = (raw: unknown) => errorsOf(raw).map((error) => error.path);
|
|
91
|
+
|
|
92
|
+
describe('validateDefinition', () => {
|
|
93
|
+
it('accepts a complete definition with the 8 step types and nested branches', () => {
|
|
94
|
+
const raw = validDefinition();
|
|
95
|
+
const result = validateDefinition(raw, catalog);
|
|
96
|
+
expect(result).toEqual({ ok: true, definition: raw });
|
|
97
|
+
const types = new Set<string>();
|
|
98
|
+
walkSteps(raw.steps as never, (step) => types.add(step.type));
|
|
99
|
+
expect([...types].sort()).toEqual(Object.values(StepType).sort());
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('rejects an unknown formatVersion at "formatVersion"', () => {
|
|
103
|
+
const raw = { ...validDefinition(), formatVersion: 2 };
|
|
104
|
+
expect(pathsOf(raw)).toEqual(['formatVersion']);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('rejects a duplicated id on the second occurrence', () => {
|
|
108
|
+
const raw = validDefinition();
|
|
109
|
+
(raw.steps[1] as any).then[0].id = 'init';
|
|
110
|
+
expect(pathsOf(raw)).toEqual(['steps[1].then[0].id']);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('rejects __proto__, constructor and prototype as step id and as SET variable', () => {
|
|
114
|
+
for (const name of ['__proto__', 'constructor', 'prototype']) {
|
|
115
|
+
const asId = validDefinition();
|
|
116
|
+
(asId.steps[0] as any).id = name;
|
|
117
|
+
expect(pathsOf(asId)).toEqual(['steps[0].id']);
|
|
118
|
+
const asVariable = validDefinition();
|
|
119
|
+
(asVariable.steps[0] as any).variable = name;
|
|
120
|
+
expect(pathsOf(asVariable)).toEqual(['steps[0].variable']);
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it('bounds a literal WAIT duration to MAX_WAIT_DURATION_MS', () => {
|
|
125
|
+
expect(MAX_WAIT_DURATION_MS).toBe(30 * 24 * 60 * 60 * 1000);
|
|
126
|
+
const at = (durationMs: unknown) => ({
|
|
127
|
+
...validDefinition(),
|
|
128
|
+
steps: [{ id: 'w', type: StepType.WAIT, durationMs }],
|
|
129
|
+
});
|
|
130
|
+
expect(validateDefinition(at(MAX_WAIT_DURATION_MS), catalog).ok).toBe(true);
|
|
131
|
+
expect(validateDefinition(at({ $expression: 'input.d' }), catalog).ok).toBe(true);
|
|
132
|
+
expect(validateDefinition(at('{{ input.d }}'), catalog).ok).toBe(true);
|
|
133
|
+
for (const bad of [MAX_WAIT_DURATION_MS + 1, -1, 1.5]) {
|
|
134
|
+
expect(pathsOf(at(bad))).toEqual(['steps[0].durationMs']);
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it('requires a literal WAIT until to be an ISO 8601 date', () => {
|
|
139
|
+
const at = (until: unknown) => ({
|
|
140
|
+
...validDefinition(),
|
|
141
|
+
steps: [{ id: 'w', type: StepType.WAIT, until }],
|
|
142
|
+
});
|
|
143
|
+
expect(validateDefinition(at('2030-01-01T00:00:00.000Z'), catalog).ok).toBe(true);
|
|
144
|
+
expect(validateDefinition(at('{{ input.when }}'), catalog).ok).toBe(true);
|
|
145
|
+
expect(validateDefinition(at({ $expression: 'input.when' }), catalog).ok).toBe(true);
|
|
146
|
+
expect(pathsOf(at('tomorrow'))).toEqual(['steps[0].until']);
|
|
147
|
+
expect(pathsOf(at(12))).toEqual(['steps[0].until']);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it('rejects an action absent from the catalog at the path of "action"', () => {
|
|
151
|
+
const raw = validDefinition();
|
|
152
|
+
(raw.steps[1] as any).then[1].action = 'send-sms';
|
|
153
|
+
const paths = pathsOf(raw);
|
|
154
|
+
expect(paths).toContain('steps[1].then[1].action');
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it('rejects an input key outside the action schema', () => {
|
|
158
|
+
const raw = validDefinition();
|
|
159
|
+
(raw.steps[1] as any).then[1].input.cc = 'x@y.z';
|
|
160
|
+
expect(pathsOf(raw)).toEqual(['steps[1].then[1].input.cc']);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it('rejects an HTTP step towards an undeclared host', () => {
|
|
164
|
+
const raw = validDefinition();
|
|
165
|
+
(raw.steps[1] as any).then[0].url = 'https://evil.example.org/x';
|
|
166
|
+
expect(pathsOf(raw)).toEqual(['steps[1].then[0]']);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it('does not let a wildcard declaration cover the apex host', () => {
|
|
170
|
+
const raw = validDefinition();
|
|
171
|
+
(raw.steps[2] as any).cases[1].steps[0].url = 'https://hooks.example.com/ping';
|
|
172
|
+
expect(pathsOf(raw)).toEqual(['steps[2].cases[1].steps[0]']);
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it('skips the host check when the URL is a template', () => {
|
|
176
|
+
const raw = validDefinition();
|
|
177
|
+
(raw.steps[1] as any).then[0].url = 'https://{{ input.host }}/x';
|
|
178
|
+
expect(validateDefinition(raw, catalog).ok).toBe(true);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it('requires an HTTP declaration for any HTTP step', () => {
|
|
182
|
+
const raw = validDefinition();
|
|
183
|
+
raw.accesses = raw.accesses.filter((access) => access.kind !== AccessKind.HTTP);
|
|
184
|
+
(raw.steps[1] as any).then[0].url = '{{ input.url }}';
|
|
185
|
+
expect(pathsOf(raw)).toContain('steps[1].then[0]');
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it('rejects a HOST step whose action is not declared', () => {
|
|
189
|
+
const raw = validDefinition();
|
|
190
|
+
raw.accesses = raw.accesses.filter((access) => access.kind !== AccessKind.HOST_ACTION);
|
|
191
|
+
expect(pathsOf(raw)).toEqual(['steps[1].then[1]']);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it('rejects a WAIT with both durationMs and until', () => {
|
|
195
|
+
const raw = validDefinition();
|
|
196
|
+
(raw.steps[1] as any).else[0].until = '2026-12-31T00:00:00Z';
|
|
197
|
+
expect(pathsOf(raw)).toEqual(['steps[1].else[0]']);
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it('rejects a WAIT with neither durationMs nor until', () => {
|
|
201
|
+
const raw = validDefinition();
|
|
202
|
+
delete (raw.steps[1] as any).else[0].durationMs;
|
|
203
|
+
expect(pathsOf(raw)).toEqual(['steps[1].else[0]']);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it('rejects an object carrying $expression and another key', () => {
|
|
207
|
+
const raw = validDefinition();
|
|
208
|
+
(raw.steps[0] as any).value = { $expression: '1 + 1', other: 2 };
|
|
209
|
+
expect(pathsOf(raw)).toEqual(['steps[0].value']);
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it('rejects a $expression that is not a string', () => {
|
|
213
|
+
const raw = validDefinition();
|
|
214
|
+
(raw.steps[0] as any).value = { $expression: 3 };
|
|
215
|
+
expect(pathsOf(raw)).toEqual(['steps[0].value']);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it('rejects an invalid SET variable, an empty id and an unknown STOP status', () => {
|
|
219
|
+
const raw = validDefinition();
|
|
220
|
+
(raw.steps[0] as any).variable = 'not valid';
|
|
221
|
+
(raw.steps[3] as any).id = '';
|
|
222
|
+
(raw.steps[2] as any).default[0].status = 'MAYBE';
|
|
223
|
+
expect(pathsOf(raw).sort()).toEqual(
|
|
224
|
+
['steps[0].variable', 'steps[2].default[0].status', 'steps[3].id'].sort(),
|
|
225
|
+
);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it('rejects a SWITCH without case', () => {
|
|
229
|
+
const raw = validDefinition();
|
|
230
|
+
(raw.steps[2] as any).cases = [];
|
|
231
|
+
expect(pathsOf(raw)).toEqual(['steps[2].cases']);
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
it('bounds the HTTP timeout to a positive integer of at most 30000', () => {
|
|
235
|
+
const tooLong = validDefinition();
|
|
236
|
+
(tooLong.steps[1] as any).then[0].timeoutMs = 30001;
|
|
237
|
+
expect(pathsOf(tooLong)).toEqual(['steps[1].then[0].timeoutMs']);
|
|
238
|
+
const fractional = validDefinition();
|
|
239
|
+
(fractional.steps[1] as any).then[0].timeoutMs = 1.5;
|
|
240
|
+
expect(pathsOf(fractional)).toEqual(['steps[1].then[0].timeoutMs']);
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
it('rejects HTTP headers that are not value sources', () => {
|
|
244
|
+
const raw = validDefinition();
|
|
245
|
+
(raw.steps[1] as any).then[0].headers = { 'X-Bad': { $expression: 'a', extra: 1 } };
|
|
246
|
+
expect(pathsOf(raw)).toEqual(['steps[1].then[0].headers.X-Bad']);
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
it('rejects a malformed access declaration', () => {
|
|
250
|
+
const raw = validDefinition();
|
|
251
|
+
(raw.accesses[0] as any).hosts = ['https://api.example.com/'];
|
|
252
|
+
(raw.accesses as any[]).push({ kind: 'FTP' });
|
|
253
|
+
const paths = pathsOf(raw);
|
|
254
|
+
expect(paths).toContain('accesses[0].hosts[0]');
|
|
255
|
+
expect(paths).toContain('accesses[2].kind');
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
it('rejects an unknown step type and a non-object step', () => {
|
|
259
|
+
const raw = validDefinition();
|
|
260
|
+
(raw.steps as any[]).push({ id: 'x', type: 'LOOP' }, 'oops');
|
|
261
|
+
expect(pathsOf(raw)).toEqual(['steps[4].type', 'steps[5]']);
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
it('collects every error instead of stopping at the first', () => {
|
|
265
|
+
const raw = validDefinition();
|
|
266
|
+
(raw.steps[1] as any).then[1].action = 'send-sms';
|
|
267
|
+
(raw.steps[1] as any).then[0].url = 'https://evil.example.org/x';
|
|
268
|
+
(raw.steps[0] as any).variable = '1x';
|
|
269
|
+
expect(errorsOf(raw).length).toBeGreaterThanOrEqual(3);
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it('rejects a document that is not an object', () => {
|
|
273
|
+
expect(pathsOf('nope')).toEqual(['']);
|
|
274
|
+
expect(pathsOf(null)).toEqual(['']);
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it('does not mutate its input', () => {
|
|
278
|
+
const raw = validDefinition();
|
|
279
|
+
const copy = clone(raw);
|
|
280
|
+
validateDefinition(raw, catalog);
|
|
281
|
+
expect(raw).toEqual(copy);
|
|
282
|
+
});
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
describe('walkSteps', () => {
|
|
286
|
+
it('visits steps in document order with their JSON path', () => {
|
|
287
|
+
const visited: string[] = [];
|
|
288
|
+
walkSteps(validDefinition().steps as never, (step, path) => visited.push(`${path}:${step.id}`));
|
|
289
|
+
expect(visited).toEqual([
|
|
290
|
+
'steps[0]:init',
|
|
291
|
+
'steps[1]:check',
|
|
292
|
+
'steps[1].then[0]:fetch',
|
|
293
|
+
'steps[1].then[1]:mail',
|
|
294
|
+
'steps[1].else[0]:pause',
|
|
295
|
+
'steps[2]:route',
|
|
296
|
+
'steps[2].cases[0].steps[0]:compute',
|
|
297
|
+
'steps[2].cases[0].steps[1]:later',
|
|
298
|
+
'steps[2].cases[1].steps[0]:hook',
|
|
299
|
+
'steps[2].default[0]:fail',
|
|
300
|
+
'steps[3]:done',
|
|
301
|
+
]);
|
|
302
|
+
});
|
|
303
|
+
});
|