@friggframework/devtools 2.0.0--canary.703.6a9383b.0 → 2.0.0--canary.704.79d9a63.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 +14 -0
- package/frigg-cli/README.md +6 -0
- package/frigg-cli/__tests__/unit/dependencies.test.js +3 -5
- package/frigg-cli/__tests__/unit/utils/node-version.test.js +66 -0
- package/frigg-cli/index.js +3 -0
- package/frigg-cli/package.json +3 -3
- package/frigg-cli/utils/node-version.js +51 -0
- package/infrastructure/README.md +20 -0
- package/infrastructure/__tests__/package-patterns-composition.test.js +153 -0
- package/infrastructure/domains/admin-scripts/admin-script-builder.js +50 -46
- package/infrastructure/domains/admin-scripts/admin-script-builder.test.js +46 -7
- package/infrastructure/domains/database/migration-builder.js +182 -175
- package/infrastructure/domains/database/migration-builder.test.js +36 -5
- package/infrastructure/domains/integration/integration-builder.js +65 -80
- package/infrastructure/domains/integration/integration-builder.test.js +73 -30
- package/infrastructure/domains/shared/function-environments.js +1 -9
- package/infrastructure/domains/shared/function-environments.test.js +0 -14
- package/infrastructure/domains/shared/utilities/base-definition-factory.js +76 -122
- package/infrastructure/domains/shared/utilities/base-definition-factory.test.js +95 -26
- package/infrastructure/domains/shared/utilities/framework-version.js +14 -0
- package/infrastructure/domains/shared/utilities/framework-version.test.js +27 -0
- package/infrastructure/domains/shared/utilities/logging-config.js +3 -4
- package/infrastructure/domains/shared/utilities/logging-config.test.js +1 -2
- package/infrastructure/domains/shared/utilities/package-patterns.js +35 -0
- package/infrastructure/domains/shared/utilities/package-patterns.test.js +112 -0
- package/infrastructure/infrastructure-composer.js +0 -2
- package/infrastructure/infrastructure-composer.test.js +3 -3
- package/package.json +11 -8
- package/infrastructure/domains/integration/define-integration-provisioning.test.js +0 -59
- package/infrastructure/domains/scheduler/trigger-builder.js +0 -114
- package/infrastructure/domains/scheduler/trigger-builder.test.js +0 -82
|
@@ -6,8 +6,8 @@ const RETENTION_DAYS = [
|
|
|
6
6
|
2192, 2557, 2922, 3288, 3653,
|
|
7
7
|
];
|
|
8
8
|
|
|
9
|
-
//
|
|
10
|
-
|
|
9
|
+
// provider.logs.lambda compiles to LoggingConfig from osls 3.58.0; the base
|
|
10
|
+
// definition's single frameworkVersion (^4.4.0, ADR-050) already covers it.
|
|
11
11
|
|
|
12
12
|
// ADR-048 Open question 3: off until the Phase 0 spike proves Lambda JSON mode
|
|
13
13
|
// keeps direct fd 1 lines intact. Until then Lambda stays on Text and
|
|
@@ -39,7 +39,7 @@ function validateRetentionInDays(retentionInDays) {
|
|
|
39
39
|
*
|
|
40
40
|
* @param {Object} [logging] - appDefinition.logging
|
|
41
41
|
* @param {{ lambdaJson?: boolean }} [options]
|
|
42
|
-
* @returns {{
|
|
42
|
+
* @returns {{ provider?: Object }}
|
|
43
43
|
*/
|
|
44
44
|
function buildLoggingProviderConfig(
|
|
45
45
|
logging,
|
|
@@ -53,7 +53,6 @@ function buildLoggingProviderConfig(
|
|
|
53
53
|
if (logging.level !== undefined) {
|
|
54
54
|
const level = normalizeLogLevel(logging.level);
|
|
55
55
|
if (lambdaJson) {
|
|
56
|
-
result.frameworkVersion = LOGGING_CONFIG_FRAMEWORK_VERSION;
|
|
57
56
|
provider.logs = {
|
|
58
57
|
lambda: {
|
|
59
58
|
logFormat: 'JSON',
|
|
@@ -18,9 +18,8 @@ describe('buildLoggingProviderConfig (ADR-048)', () => {
|
|
|
18
18
|
const build = (logging) =>
|
|
19
19
|
buildLoggingProviderConfig(logging, { lambdaJson: true });
|
|
20
20
|
|
|
21
|
-
it('maps level to provider.logs.lambda and
|
|
21
|
+
it('maps level to provider.logs.lambda and leaves frameworkVersion to the base definition (ADR-050)', () => {
|
|
22
22
|
expect(build({ level: 'warn' })).toEqual({
|
|
23
|
-
frameworkVersion: '>=3.58.0',
|
|
24
23
|
provider: {
|
|
25
24
|
logs: {
|
|
26
25
|
lambda: {
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Function package patterns
|
|
3
|
+
*
|
|
4
|
+
* Utility Layer - Hexagonal Architecture
|
|
5
|
+
*
|
|
6
|
+
* osls 4 rejects `package.include` / `package.exclude` and only reads
|
|
7
|
+
* `package.patterns` (ADR-050). Builders keep their include and exclude lists
|
|
8
|
+
* and pass them through this helper, so one place owns the negation.
|
|
9
|
+
*
|
|
10
|
+
* osls resolves a function's files with `resolveFilePathsFromPatterns`: an
|
|
11
|
+
* ordered list evaluated last-match-wins. On osls 3 a function `exclude` was
|
|
12
|
+
* placed (negated) ahead of the function `include`. Frigg emits no
|
|
13
|
+
* service-level include, exclude or patterns, so `[!exclude..., include...]`
|
|
14
|
+
* reproduces the osls 3 order exactly, and is valid on osls 3 and osls 4.
|
|
15
|
+
*
|
|
16
|
+
* An exclude that already starts with `!` was treated by osls 3 as an include
|
|
17
|
+
* in exclude position; it is emitted un-negated in the same position here.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @param {Object} [lists]
|
|
22
|
+
* @param {string[]} [lists.include] - Globs to (re-)add after the excludes
|
|
23
|
+
* @param {string[]} [lists.exclude] - Globs to remove
|
|
24
|
+
* @returns {string[]} Value for `package.patterns`
|
|
25
|
+
*/
|
|
26
|
+
function toPackagePatterns({ include = [], exclude = [] } = {}) {
|
|
27
|
+
return [
|
|
28
|
+
...exclude.map((pattern) =>
|
|
29
|
+
pattern.startsWith('!') ? pattern.slice(1) : `!${pattern}`
|
|
30
|
+
),
|
|
31
|
+
...include,
|
|
32
|
+
];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
module.exports = { toPackagePatterns };
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for the function package patterns helper (ADR-050)
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
const { toPackagePatterns } = require('./package-patterns');
|
|
6
|
+
const { nestedNodeModulesExcludes } = require('./nested-node-modules');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The ordered list osls 3 built in resolveFilePathsFromPatterns for a function
|
|
10
|
+
* that set `package.include` / `package.exclude` (no service-level patterns,
|
|
11
|
+
* which Frigg never emits, and before the dev-dependency patterns that both
|
|
12
|
+
* versions append identically).
|
|
13
|
+
*/
|
|
14
|
+
function osls3FunctionOrder({ include = [], exclude = [] }) {
|
|
15
|
+
const patterns = [];
|
|
16
|
+
exclude.forEach((p) => patterns.push(p.charAt(0) !== '!' ? `!${p}` : p.substring(1)));
|
|
17
|
+
include.forEach((p) => patterns.push(p));
|
|
18
|
+
return patterns;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The ordered list osls 3 and osls 4 build for a function that sets
|
|
23
|
+
* `package.patterns`: the patterns go in include position, unchanged.
|
|
24
|
+
*/
|
|
25
|
+
function patternsFunctionOrder(patterns) {
|
|
26
|
+
return [...patterns];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
describe('toPackagePatterns', () => {
|
|
30
|
+
const cases = [
|
|
31
|
+
{
|
|
32
|
+
name: 'excludes first (negated), then includes',
|
|
33
|
+
lists: {
|
|
34
|
+
include: ['ca.pem'],
|
|
35
|
+
exclude: ['node_modules/aws-sdk/**', '.env'],
|
|
36
|
+
},
|
|
37
|
+
expected: ['!node_modules/aws-sdk/**', '!.env', 'ca.pem'],
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
name: 'an exclude that already starts with ! becomes an include in exclude position',
|
|
41
|
+
lists: {
|
|
42
|
+
include: [],
|
|
43
|
+
exclude: ['node_modules/**', '!node_modules/keep/**'],
|
|
44
|
+
},
|
|
45
|
+
expected: ['!node_modules/**', 'node_modules/keep/**'],
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
name: 'an include re-adds a path that an exclude removed',
|
|
49
|
+
lists: {
|
|
50
|
+
include: ['config/ca.pem'],
|
|
51
|
+
exclude: ['config/**'],
|
|
52
|
+
},
|
|
53
|
+
expected: ['!config/**', 'config/ca.pem'],
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
name: 'empty lists',
|
|
57
|
+
lists: { include: [], exclude: [] },
|
|
58
|
+
expected: [],
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
name: 'only excludes',
|
|
62
|
+
lists: { exclude: ['test/**'] },
|
|
63
|
+
expected: ['!test/**'],
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
name: 'only includes',
|
|
67
|
+
lists: { include: ['ca.pem'] },
|
|
68
|
+
expected: ['ca.pem'],
|
|
69
|
+
},
|
|
70
|
+
];
|
|
71
|
+
|
|
72
|
+
it.each(cases)('$name', ({ lists, expected }) => {
|
|
73
|
+
const patterns = toPackagePatterns(lists);
|
|
74
|
+
expect(patterns).toEqual(expected);
|
|
75
|
+
// Same evaluation order osls 3 applied to include/exclude.
|
|
76
|
+
expect(patternsFunctionOrder(patterns)).toEqual(osls3FunctionOrder(lists));
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('returns an empty list when called with no arguments', () => {
|
|
80
|
+
expect(toPackagePatterns()).toEqual([]);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('does not mutate its inputs', () => {
|
|
84
|
+
const exclude = ['a/**'];
|
|
85
|
+
const include = ['b'];
|
|
86
|
+
toPackagePatterns({ include, exclude });
|
|
87
|
+
expect(exclude).toEqual(['a/**']);
|
|
88
|
+
expect(include).toEqual(['b']);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
describe('with nestedNodeModulesExcludes output', () => {
|
|
92
|
+
it('negates the single catch-all glob by default', () => {
|
|
93
|
+
const exclude = nestedNodeModulesExcludes({}, true);
|
|
94
|
+
expect(toPackagePatterns({ exclude })).toEqual([
|
|
95
|
+
'!node_modules/**/node_modules/**',
|
|
96
|
+
]);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it('negates every scoped glob when keepNestedNodeModules is set', () => {
|
|
100
|
+
const appDefinition = { lambda: { keepNestedNodeModules: true } };
|
|
101
|
+
const exclude = nestedNodeModulesExcludes(appDefinition, true);
|
|
102
|
+
const patterns = toPackagePatterns({ exclude });
|
|
103
|
+
|
|
104
|
+
expect(patterns).toEqual(exclude.map((p) => `!${p}`));
|
|
105
|
+
expect(patterns).toContain('!node_modules/**/node_modules/@friggframework/**');
|
|
106
|
+
expect(patterns).not.toContain('!node_modules/**/node_modules/**');
|
|
107
|
+
expect(patternsFunctionOrder(patterns)).toEqual(
|
|
108
|
+
osls3FunctionOrder({ exclude })
|
|
109
|
+
);
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
});
|
|
@@ -17,7 +17,6 @@ const { SsmBuilder } = require('./domains/parameters/ssm-builder');
|
|
|
17
17
|
const { WebsocketBuilder } = require('./domains/integration/websocket-builder');
|
|
18
18
|
const { IntegrationBuilder } = require('./domains/integration/integration-builder');
|
|
19
19
|
const { SchedulerBuilder } = require('./domains/scheduler/scheduler-builder');
|
|
20
|
-
const { TriggerBuilder } = require('./domains/scheduler/trigger-builder');
|
|
21
20
|
const { AdminScriptBuilder } = require('./domains/admin-scripts/admin-script-builder');
|
|
22
21
|
|
|
23
22
|
// Utilities
|
|
@@ -93,7 +92,6 @@ const composeServerlessDefinition = async (AppDefinition) => {
|
|
|
93
92
|
new WebsocketBuilder(),
|
|
94
93
|
new IntegrationBuilder(),
|
|
95
94
|
new SchedulerBuilder(), // Add scheduler after IntegrationBuilder (depends on it)
|
|
96
|
-
new TriggerBuilder(), // One trigger tick function per integration with triggers (ADR-055)
|
|
97
95
|
new AdminScriptBuilder(),
|
|
98
96
|
]);
|
|
99
97
|
|
|
@@ -153,7 +153,7 @@ describe('composeServerlessDefinition', () => {
|
|
|
153
153
|
expect(result.provider.runtime).toBe('nodejs22.x');
|
|
154
154
|
expect(result.provider.region).toBe('us-east-1');
|
|
155
155
|
expect(result.provider.stage).toBe('${opt:stage}');
|
|
156
|
-
expect(result.frameworkVersion).toBe('
|
|
156
|
+
expect(result.frameworkVersion).toBe('^4.4.0');
|
|
157
157
|
});
|
|
158
158
|
|
|
159
159
|
it('should use default service name when name not provided', async () => {
|
|
@@ -245,7 +245,7 @@ describe('composeServerlessDefinition', () => {
|
|
|
245
245
|
logging: { level: 'debug', retentionInDays: 30 },
|
|
246
246
|
});
|
|
247
247
|
|
|
248
|
-
expect(result.frameworkVersion).toBe('
|
|
248
|
+
expect(result.frameworkVersion).toBe('^4.4.0');
|
|
249
249
|
expect(result.provider.logs).toBeUndefined();
|
|
250
250
|
expect(result.provider.logRetentionInDays).toBe(30);
|
|
251
251
|
expect(result.provider.environment.FRIGG_LOG_LEVEL).toBe('DEBUG');
|
|
@@ -275,7 +275,7 @@ describe('composeServerlessDefinition', () => {
|
|
|
275
275
|
it('changes nothing when logging is absent', async () => {
|
|
276
276
|
const result = await composeServerlessDefinition({ integrations: [] });
|
|
277
277
|
|
|
278
|
-
expect(result.frameworkVersion).toBe('
|
|
278
|
+
expect(result.frameworkVersion).toBe('^4.4.0');
|
|
279
279
|
expect(result.provider.logs).toBeUndefined();
|
|
280
280
|
expect(result.provider.logRetentionInDays).toBeUndefined();
|
|
281
281
|
expect(result.provider.environment).not.toHaveProperty('FRIGG_LOG_LEVEL');
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@friggframework/devtools",
|
|
3
3
|
"prettier": "@friggframework/prettier-config",
|
|
4
|
-
"version": "2.0.0--canary.
|
|
4
|
+
"version": "2.0.0--canary.704.79d9a63.0",
|
|
5
5
|
"bin": {
|
|
6
6
|
"frigg": "./frigg-cli/index.js"
|
|
7
7
|
},
|
|
@@ -26,9 +26,9 @@
|
|
|
26
26
|
"@babel/eslint-parser": "^7.18.9",
|
|
27
27
|
"@babel/parser": "^7.25.3",
|
|
28
28
|
"@babel/traverse": "^7.25.3",
|
|
29
|
-
"@friggframework/core": "2.0.0--canary.
|
|
30
|
-
"@friggframework/schemas": "2.0.0--canary.
|
|
31
|
-
"@friggframework/test": "2.0.0--canary.
|
|
29
|
+
"@friggframework/core": "2.0.0--canary.704.79d9a63.0",
|
|
30
|
+
"@friggframework/schemas": "2.0.0--canary.704.79d9a63.0",
|
|
31
|
+
"@friggframework/test": "2.0.0--canary.704.79d9a63.0",
|
|
32
32
|
"@hapi/boom": "^10.0.1",
|
|
33
33
|
"@inquirer/prompts": "^5.3.8",
|
|
34
34
|
"axios": "^1.18.0",
|
|
@@ -56,12 +56,12 @@
|
|
|
56
56
|
"validate-npm-package-name": "^5.0.0"
|
|
57
57
|
},
|
|
58
58
|
"devDependencies": {
|
|
59
|
-
"@friggframework/eslint-config": "2.0.0--canary.
|
|
60
|
-
"@friggframework/prettier-config": "2.0.0--canary.
|
|
59
|
+
"@friggframework/eslint-config": "2.0.0--canary.704.79d9a63.0",
|
|
60
|
+
"@friggframework/prettier-config": "2.0.0--canary.704.79d9a63.0",
|
|
61
61
|
"aws-sdk-client-mock": "^4.1.0",
|
|
62
62
|
"aws-sdk-client-mock-jest": "^4.1.0",
|
|
63
63
|
"jest": "^30.1.3",
|
|
64
|
-
"osls": "^
|
|
64
|
+
"osls": "^4.4.0",
|
|
65
65
|
"prettier": "^2.7.1",
|
|
66
66
|
"serverless-dotenv-plugin": "^6.0.0",
|
|
67
67
|
"serverless-esbuild": "^1.54.3",
|
|
@@ -70,6 +70,9 @@
|
|
|
70
70
|
"serverless-offline-sqs": "^8.0.0",
|
|
71
71
|
"serverless-plugin-monorepo": "^0.11.0"
|
|
72
72
|
},
|
|
73
|
+
"engines": {
|
|
74
|
+
"node": "^22.13.0 || >=24"
|
|
75
|
+
},
|
|
73
76
|
"scripts": {
|
|
74
77
|
"lint:fix": "prettier --write --loglevel error . && eslint . --fix",
|
|
75
78
|
"test": "jest --passWithNoTests # TODO"
|
|
@@ -89,5 +92,5 @@
|
|
|
89
92
|
"publishConfig": {
|
|
90
93
|
"access": "public"
|
|
91
94
|
},
|
|
92
|
-
"gitHead": "
|
|
95
|
+
"gitHead": "79d9a635432643710e9fb815cf16453d01f7e12c"
|
|
93
96
|
}
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* A defineIntegration app (ADR-054) provisions through the existing builders with no new path: the contract-2
|
|
3
|
-
* binding gets its own function under the derived key, and `triggers` get the ADR-055 tick.
|
|
4
|
-
*/
|
|
5
|
-
jest.mock('@friggframework/core/database/config', () => ({ DB_TYPE: 'mongodb', getDatabaseType: jest.fn(() => 'mongodb'), PRISMA_LOG_LEVEL: 'error,warn' }));
|
|
6
|
-
|
|
7
|
-
const { defineApp, defineIntegration, use } = require('@friggframework/core/integrations/define');
|
|
8
|
-
const { IntegrationBuilder } = require('./integration-builder');
|
|
9
|
-
const { TriggerBuilder } = require('../scheduler/trigger-builder');
|
|
10
|
-
|
|
11
|
-
/** A module with a contract-2 webhooks extension, reduced to what provisioning reads. */
|
|
12
|
-
const crmModule = {
|
|
13
|
-
Definition: { moduleName: 'crm-vendor', getName: () => 'crm-vendor' },
|
|
14
|
-
extensions: {
|
|
15
|
-
webhooks: {
|
|
16
|
-
name: 'crm-vendor-webhooks',
|
|
17
|
-
contract: 2,
|
|
18
|
-
useDatabase: false,
|
|
19
|
-
routes: [{ path: '/webhooks', method: 'POST' }],
|
|
20
|
-
receive: (input) => [{ externalId: input.body.account, body: input.body }],
|
|
21
|
-
resolve: { module: 'crm-vendor' },
|
|
22
|
-
emits: { 'deal.changed': {} },
|
|
23
|
-
},
|
|
24
|
-
},
|
|
25
|
-
};
|
|
26
|
-
const chatModule = { Definition: { moduleName: 'chat-vendor', getName: () => 'chat-vendor' } };
|
|
27
|
-
|
|
28
|
-
const app = defineApp({
|
|
29
|
-
name: 'deal-alerts',
|
|
30
|
-
integrations: [
|
|
31
|
-
defineIntegration({
|
|
32
|
-
name: 'deal-alerts',
|
|
33
|
-
modules: { crm: use(crmModule, { webhooks: {} }), chat: use(chatModule) },
|
|
34
|
-
on: { 'crm.deal.changed': async () => {} },
|
|
35
|
-
triggers: { digest: { every: '1h', handler: async () => {} } },
|
|
36
|
-
}),
|
|
37
|
-
],
|
|
38
|
-
}, { schemas: null });
|
|
39
|
-
|
|
40
|
-
describe('a defineIntegration app on the existing builders', () => {
|
|
41
|
-
beforeEach(() => { jest.spyOn(console, 'log').mockImplementation(() => {}); });
|
|
42
|
-
afterEach(() => { jest.restoreAllMocks(); });
|
|
43
|
-
|
|
44
|
-
it('gives the contract-2 binding its own function, routed under the module key', async () => {
|
|
45
|
-
const result = await new IntegrationBuilder().build(app.Definition, {});
|
|
46
|
-
const fn = result.functions['deal-alerts__crmwebhooks'];
|
|
47
|
-
expect(fn.handler).toBe('node_modules/@friggframework/core/handlers/routers/integration-defined-routers.handlers.deal-alerts__crmwebhooks.handler');
|
|
48
|
-
expect(fn.events).toEqual([{ httpApi: { path: '/api/deal-alerts-integration/crm-webhooks/webhooks', method: 'POST' } }]);
|
|
49
|
-
expect(fn.layers).toBeUndefined(); // useDatabase: false, so no Prisma layer
|
|
50
|
-
expect(result.functions['deal-alertsQueueWorker']).toBeDefined();
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
it('provisions the ADR-055 tick for its triggers', async () => {
|
|
54
|
-
const builder = new TriggerBuilder();
|
|
55
|
-
expect(builder.shouldExecute(app.Definition)).toBe(true);
|
|
56
|
-
const result = await builder.build(app.Definition, {});
|
|
57
|
-
expect(JSON.stringify(result)).toContain('rate(1 hour)');
|
|
58
|
-
});
|
|
59
|
-
});
|
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Trigger Builder (ADR-055 §2)
|
|
3
|
-
*
|
|
4
|
-
* Domain Layer - Hexagonal Architecture
|
|
5
|
-
*
|
|
6
|
-
* One function per integration that declares recurring triggers: `{integration}Triggers`, with the Prisma layer.
|
|
7
|
-
* It gets one serverless `schedule` event per distinct tick of the integration's `every` / `cron` triggers (and of
|
|
8
|
-
* the triggers it resolves per installation, `Definition.instanceTriggers`), plus one EventBridge Scheduler event per
|
|
9
|
-
* `at` trigger, with its time zone. Each tick pages through the ENABLED installations and queues the runs that are
|
|
10
|
-
* due (core: integrations/use-cases/run-trigger-tick.js). Nothing is created per installation, and these resources
|
|
11
|
-
* change only when a Definition does, so they live in CloudFormation and leave with the stack.
|
|
12
|
-
*
|
|
13
|
-
* IAM, and why this builder adds none:
|
|
14
|
-
* - A `rate()` tick is an EventBridge rule; the framework adds the Lambda permission for it.
|
|
15
|
-
* - An `at` trigger uses `method: scheduler`; the framework adds `scheduler.amazonaws.com` to the function role's
|
|
16
|
-
* trust policy and `lambda:InvokeFunction` on this function to it.
|
|
17
|
-
* - The tick reads the integration table (the Prisma layer and the provider's database access, like the queue
|
|
18
|
-
* worker) and sends to its own `<Name>Queue`, which the provider-level `sqs:SendMessage(Batch)` statement on
|
|
19
|
-
* `${service}--${stage}-*Queue` already allows. It never calls EventBridge Scheduler at runtime, so it needs none
|
|
20
|
-
* of SchedulerBuilder's scheduler:* permissions (those stay for one-time jobs).
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
const { InfrastructureBuilder, ValidationResult } = require('../shared/base-builder');
|
|
24
|
-
const { IntegrationBuilder } = require('../integration/integration-builder');
|
|
25
|
-
const {
|
|
26
|
-
hasTriggers,
|
|
27
|
-
provisioningOf,
|
|
28
|
-
durationOf,
|
|
29
|
-
} = require('@friggframework/core/integrations/triggers');
|
|
30
|
-
|
|
31
|
-
/** EventBridge rate() for a tick in minutes: rate(1 minute), rate(5 minutes), rate(1 hour), rate(2 hours), rate(1 day). */
|
|
32
|
-
function rateExpression(minutes) {
|
|
33
|
-
const [count, unit] =
|
|
34
|
-
minutes % 1440 === 0 ? [minutes / 1440, 'day'] : minutes % 60 === 0 ? [minutes / 60, 'hour'] : [minutes, 'minute'];
|
|
35
|
-
return `rate(${count} ${unit}${count === 1 ? '' : 's'})`;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/** The function name the trigger tick for an integration runs as. Wire contract with function-environments.js. */
|
|
39
|
-
function triggerFunctionName(integrationName) {
|
|
40
|
-
return `${integrationName}Triggers`;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
class TriggerBuilder extends InfrastructureBuilder {
|
|
44
|
-
constructor() {
|
|
45
|
-
super();
|
|
46
|
-
this.name = 'TriggerBuilder';
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
shouldExecute(appDefinition) {
|
|
50
|
-
return Array.isArray(appDefinition.integrations) && appDefinition.integrations.some((i) => hasTriggers(i));
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
getDependencies() {
|
|
54
|
-
return ['IntegrationBuilder']; // the tick sends to the integration queue
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
validate(appDefinition) {
|
|
58
|
-
const result = new ValidationResult();
|
|
59
|
-
for (const integration of appDefinition.integrations || []) {
|
|
60
|
-
if (!hasTriggers(integration)) continue;
|
|
61
|
-
try {
|
|
62
|
-
provisioningOf(integration);
|
|
63
|
-
} catch (error) {
|
|
64
|
-
result.addError(error.message);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
return result;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
async build(appDefinition) {
|
|
71
|
-
console.log(`\n[${this.name}] Configuring recurring triggers...`);
|
|
72
|
-
const result = { functions: {}, resources: {}, environment: {}, custom: {}, iamStatements: [] };
|
|
73
|
-
const usePrismaLayer = appDefinition.usePrismaLambdaLayer !== false;
|
|
74
|
-
const functionPackageConfig = new IntegrationBuilder().createFunctionPackageConfig(usePrismaLayer, appDefinition);
|
|
75
|
-
|
|
76
|
-
for (const integration of appDefinition.integrations || []) {
|
|
77
|
-
if (!hasTriggers(integration)) continue;
|
|
78
|
-
const name = integration.Definition.name;
|
|
79
|
-
const { ticks, at } = provisioningOf(integration);
|
|
80
|
-
const events = [
|
|
81
|
-
...ticks.map((minutes) => ({
|
|
82
|
-
schedule: {
|
|
83
|
-
rate: [rateExpression(minutes)],
|
|
84
|
-
// The rule's own firing time goes in, so a repeated delivery falls in the same tick slot.
|
|
85
|
-
inputTransformer: {
|
|
86
|
-
inputPathsMap: { time: '$.time' },
|
|
87
|
-
inputTemplate: `{"tick":"${durationOf(minutes)}","time":<time>}`,
|
|
88
|
-
},
|
|
89
|
-
},
|
|
90
|
-
})),
|
|
91
|
-
...at.map((t) => ({
|
|
92
|
-
schedule: {
|
|
93
|
-
method: 'scheduler',
|
|
94
|
-
rate: [t.at],
|
|
95
|
-
...(t.timezone ? { timezone: t.timezone } : {}),
|
|
96
|
-
input: { at: t.name, time: '<aws.scheduler.scheduled-time>' },
|
|
97
|
-
},
|
|
98
|
-
})),
|
|
99
|
-
];
|
|
100
|
-
result.functions[triggerFunctionName(name)] = {
|
|
101
|
-
handler: `node_modules/@friggframework/core/handlers/workers/integration-triggers.handlers.${name}.tick`,
|
|
102
|
-
skipEsbuild: true, // Nested exports in node_modules - skip esbuild bundling
|
|
103
|
-
package: functionPackageConfig,
|
|
104
|
-
...(usePrismaLayer && { layers: [{ Ref: 'PrismaLambdaLayer' }] }), // reads the integration table
|
|
105
|
-
timeout: 300, // a tick past its deadline hands its page cursor to the queue
|
|
106
|
-
events,
|
|
107
|
-
};
|
|
108
|
-
console.log(` ✓ ${triggerFunctionName(name)}: ticks ${ticks.map(durationOf).join(', ') || 'none'}; at ${at.map((t) => t.name).join(', ') || 'none'}`);
|
|
109
|
-
}
|
|
110
|
-
return result;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
module.exports = { TriggerBuilder, rateExpression, triggerFunctionName };
|
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* TriggerBuilder (ADR-055 §2): one tick function per integration with triggers, one schedule per distinct tick and
|
|
3
|
-
* one per `at` trigger, never anything per installation.
|
|
4
|
-
*/
|
|
5
|
-
const { TriggerBuilder, rateExpression } = require('./trigger-builder');
|
|
6
|
-
const { applyFunctionEnvironments } = require('../shared/function-environments');
|
|
7
|
-
|
|
8
|
-
const poll = { every: '15m', handler: 'poll' };
|
|
9
|
-
|
|
10
|
-
describe('TriggerBuilder', () => {
|
|
11
|
-
beforeEach(() => { jest.spyOn(console, 'log').mockImplementation(() => {}); });
|
|
12
|
-
afterEach(() => { jest.restoreAllMocks(); });
|
|
13
|
-
|
|
14
|
-
it('runs when, and only when, an integration declares triggers (code or per installation)', () => {
|
|
15
|
-
const b = new TriggerBuilder();
|
|
16
|
-
expect(b.shouldExecute({ integrations: [{ Definition: { name: 'a', triggers: { poll } } }] })).toBe(true);
|
|
17
|
-
expect(b.shouldExecute({ integrations: [{ Definition: { name: 'dd', instanceTriggers: { tick: '5m' } } }] })).toBe(true);
|
|
18
|
-
expect(b.shouldExecute({ integrations: [{ Definition: { name: 'a', webhooks: true } }] })).toBe(false);
|
|
19
|
-
expect(b.shouldExecute({ integrations: [{ Definition: { name: 'a', triggers: {} } }] })).toBe(false);
|
|
20
|
-
expect(b.shouldExecute({})).toBe(false);
|
|
21
|
-
expect(b.getDependencies()).toEqual(['IntegrationBuilder']);
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
it('writes rate() in the largest whole unit EventBridge accepts', () => {
|
|
25
|
-
expect([1, 5, 60, 90, 120, 1440].map(rateExpression)).toEqual(['rate(1 minute)', 'rate(5 minutes)', 'rate(1 hour)', 'rate(90 minutes)', 'rate(2 hours)', 'rate(1 day)']);
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
it('emits one function per integration: a schedule per distinct tick and one per at trigger, with its zone', async () => {
|
|
29
|
-
const appDefinition = {
|
|
30
|
-
integrations: [
|
|
31
|
-
{ Definition: { name: 'crm', triggers: {
|
|
32
|
-
poll,
|
|
33
|
-
sync: { every: '1h', configurable: { min: '15m' }, handler: 'sync' }, // 15m tick: same schedule as poll
|
|
34
|
-
digest: { cron: '0 8 * * 1-5', timezone: 'America/New_York', handler: 'digest' }, // 5m
|
|
35
|
-
nightly: { at: 'cron(0 3 * * ? *)', timezone: 'Europe/London', handler: 'nightly' },
|
|
36
|
-
} } },
|
|
37
|
-
{ Definition: { name: 'plain' } },
|
|
38
|
-
],
|
|
39
|
-
};
|
|
40
|
-
const result = await new TriggerBuilder().build(appDefinition, {});
|
|
41
|
-
expect(Object.keys(result.functions)).toEqual(['crmTriggers']);
|
|
42
|
-
const fn = result.functions.crmTriggers;
|
|
43
|
-
expect(fn).toMatchObject({
|
|
44
|
-
handler: 'node_modules/@friggframework/core/handlers/workers/integration-triggers.handlers.crm.tick',
|
|
45
|
-
skipEsbuild: true,
|
|
46
|
-
layers: [{ Ref: 'PrismaLambdaLayer' }],
|
|
47
|
-
timeout: 300,
|
|
48
|
-
});
|
|
49
|
-
expect(fn.events).toEqual([
|
|
50
|
-
{ schedule: { rate: ['rate(5 minutes)'], inputTransformer: { inputPathsMap: { time: '$.time' }, inputTemplate: '{"tick":"5m","time":<time>}' } } },
|
|
51
|
-
{ schedule: { rate: ['rate(15 minutes)'], inputTransformer: { inputPathsMap: { time: '$.time' }, inputTemplate: '{"tick":"15m","time":<time>}' } } },
|
|
52
|
-
{ schedule: { method: 'scheduler', rate: ['cron(0 3 * * ? *)'], timezone: 'Europe/London', input: { at: 'nightly', time: '<aws.scheduler.scheduled-time>' } } },
|
|
53
|
-
]);
|
|
54
|
-
// The tick never calls EventBridge Scheduler at runtime: no scheduler IAM, no resources.
|
|
55
|
-
expect(result.iamStatements).toEqual([]);
|
|
56
|
-
expect(result.resources).toEqual({});
|
|
57
|
-
// The inputTemplate is valid JSON once EventBridge fills <time>.
|
|
58
|
-
expect(JSON.parse(fn.events[0].schedule.inputTransformer.inputTemplate.replace('<time>', '"2026-10-19T12:00:00Z"'))).toEqual({ tick: '5m', time: '2026-10-19T12:00:00Z' });
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
it('leaves the Prisma layer off when the app bundles Prisma', async () => {
|
|
62
|
-
const result = await new TriggerBuilder().build({ usePrismaLambdaLayer: false, integrations: [{ Definition: { name: 'crm', triggers: { poll } } }] }, {});
|
|
63
|
-
expect(result.functions.crmTriggers.layers).toBeUndefined();
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
it('fails validation, naming the integration, when a trigger cannot be provisioned', () => {
|
|
67
|
-
const v = new TriggerBuilder().validate({ integrations: [{ Definition: { name: 'crm', triggers: { bad: { every: 'often', handler: 'x' } } } }] });
|
|
68
|
-
expect(v.valid).toBe(false);
|
|
69
|
-
expect(v.errors[0]).toMatch(/Integration "crm" declares invalid triggers: bad: every "often"/);
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
it('scoped env: the queue URL reaches the tick function, through the shared function-name contract', async () => {
|
|
73
|
-
const { IntegrationBuilder } = require('../integration/integration-builder');
|
|
74
|
-
const integration = { Definition: { name: 'crm', triggers: { poll } } };
|
|
75
|
-
const ib = new IntegrationBuilder();
|
|
76
|
-
const result = { functionEnvironments: {}, environment: {} };
|
|
77
|
-
ib.setQueueUrlEnvironment('crm', { Ref: 'CrmQueue' }, result, { lambda: { scopedEnvironment: true }, integrations: [integration] });
|
|
78
|
-
const functions = { auth: {}, crm: {}, crmQueueWorker: {}, ...(await new TriggerBuilder().build({ integrations: [integration] }, {})).functions };
|
|
79
|
-
applyFunctionEnvironments(functions, result.functionEnvironments);
|
|
80
|
-
expect(functions.crmTriggers.environment).toEqual({ CRM_QUEUE_URL: { Ref: 'CrmQueue' } });
|
|
81
|
-
});
|
|
82
|
-
});
|