@friggframework/core 2.0.0--canary.663.8de669c.0 → 2.0.0--canary.690.3fe2b27.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/CHANGELOG.md +11 -0
- package/CLAUDE.md +28 -0
- package/README.md +105 -0
- package/application/commands/integration-commands.js +20 -10
- package/application/commands/scheduler-commands.js +11 -7
- package/application/commands/user-commands.js +17 -5
- package/application/index.js +6 -2
- package/core/Worker.js +31 -7
- package/core/create-handler.js +9 -0
- package/credential/repositories/credential-repository-documentdb.js +34 -0
- package/credential/repositories/credential-repository-interface.js +15 -0
- package/credential/repositories/credential-repository-mongo.js +23 -0
- package/credential/repositories/credential-repository-postgres.js +28 -0
- package/credential/use-cases/credential-operations-for-user.js +202 -0
- package/credential/use-cases/reauthorize-credential-for-user.js +73 -0
- package/database/config.js +48 -24
- package/database/documentdb-utils.js +6 -1
- package/database/encryption/README.md +2 -0
- package/database/encryption/encryption-config.js +10 -31
- package/database/encryption/encryption-schema-registry.js +121 -24
- package/database/encryption/field-encryption-service.js +69 -5
- package/database/encryption/test-support/fake-ciphertext.js +15 -0
- package/database/organization-where.js +18 -0
- package/database/prisma.js +4 -2
- package/database/unique-violation.js +25 -0
- package/extensions/bootstrap-runner.js +43 -0
- package/extensions/extension-loader.js +187 -0
- package/extensions/index.js +41 -0
- package/extensions/initialize-app.js +123 -0
- package/extensions/route-mounter.js +103 -0
- package/extensions/schema-composer.js +188 -0
- package/generated/prisma-mongodb/edge.js +43 -4
- package/generated/prisma-mongodb/index-browser.js +40 -1
- package/generated/prisma-mongodb/index.d.ts +4761 -133
- package/generated/prisma-mongodb/index.js +43 -4
- package/generated/prisma-mongodb/package.json +1 -1
- package/generated/prisma-mongodb/schema.prisma +92 -0
- package/generated/prisma-mongodb/wasm.js +43 -4
- package/generated/prisma-postgresql/edge.js +43 -4
- package/generated/prisma-postgresql/index-browser.js +40 -1
- package/generated/prisma-postgresql/index.d.ts +4961 -28
- package/generated/prisma-postgresql/index.js +43 -4
- package/generated/prisma-postgresql/package.json +1 -1
- package/generated/prisma-postgresql/schema.prisma +87 -0
- package/generated/prisma-postgresql/wasm.js +43 -4
- package/handlers/app-definition-loader.js +128 -19
- package/handlers/app-handler-helpers.js +25 -3
- package/handlers/backend-utils.js +31 -25
- package/handlers/middleware/admin-auth.js +14 -1
- package/handlers/routers/auth.js +46 -10
- package/handlers/routers/health.js +13 -4
- package/handlers/routers/integration-defined-routers.js +144 -92
- package/handlers/routers/integration-webhook-routers.js +102 -64
- package/handlers/workers/integration-defined-workers.js +5 -2
- package/index.js +65 -1
- package/infrastructure/scheduler/index.js +19 -6
- package/infrastructure/scheduler/netlify-scheduler-adapter.js +216 -0
- package/infrastructure/scheduler/scheduled-job-repository.js +118 -0
- package/infrastructure/scheduler/scheduler-service-factory.js +55 -7
- package/integrations/index.js +1 -0
- package/integrations/integration-router.js +131 -96
- package/integrations/repositories/integration-not-found.js +14 -0
- package/integrations/repositories/integration-repository-documentdb.js +35 -3
- package/integrations/repositories/integration-repository-interface.js +17 -1
- package/integrations/repositories/integration-repository-mongo.js +59 -76
- package/integrations/repositories/integration-repository-postgres.js +62 -76
- package/integrations/schedule-config.js +80 -0
- package/integrations/tests/doubles/test-integration-repository.js +12 -1
- package/integrations/use-cases/create-integration.js +55 -20
- package/integrations/use-cases/dispatch-ongoing-syncs.js +100 -0
- package/integrations/use-cases/get-integrations-for-user.js +80 -40
- package/integrations/use-cases/index.js +7 -1
- package/integrations/use-cases/integration-operations-for-user.js +256 -0
- package/integrations/use-cases/resolve-integration-ownership.js +177 -0
- package/integrations/utils/map-integration-dto.js +43 -14
- package/management-api/create-management-api-router.js +49 -0
- package/management-api/error-response.js +116 -0
- package/management-api/management-api-config.js +151 -0
- package/management-api/meta-router.js +76 -0
- package/management-api/openapi/build-openapi.js +144 -0
- package/management-api/openapi/components.js +415 -0
- package/management-api/openapi/v1-operations.js +83 -0
- package/management-api/openapi/v2-operations.js +264 -0
- package/management-api/route-registry.js +411 -0
- package/management-api/test-support/http-client.js +39 -0
- package/management-api/use-cases/get-management-api-meta.js +57 -0
- package/management-api/v1-deprecation.js +120 -0
- package/management-api/v2/create-v2-router.js +85 -0
- package/management-api/v2/dependencies.js +281 -0
- package/management-api/v2/dto.js +143 -0
- package/management-api/v2/handlers/authorization.js +94 -0
- package/management-api/v2/handlers/credentials.js +33 -0
- package/management-api/v2/handlers/entities.js +77 -0
- package/management-api/v2/handlers/integrations.js +128 -0
- package/management-api/v2/handlers/proxy.js +34 -0
- package/management-api/v2/request.js +85 -0
- package/management-api/v2/test-support/fake-dependencies.js +103 -0
- package/modules/domain/authorization-session.js +144 -0
- package/modules/entity-scope.js +15 -0
- package/modules/entity-type-registry.js +311 -0
- package/modules/index.js +14 -0
- package/modules/module-factory.js +12 -0
- package/modules/module.js +53 -2
- package/modules/proxy/proxy-bases.js +169 -0
- package/modules/proxy/proxy-errors.js +31 -0
- package/modules/proxy/proxy-path.js +183 -0
- package/modules/proxy/proxy-policy.js +277 -0
- package/modules/repositories/authorization-session-repository-documentdb.js +112 -0
- package/modules/repositories/authorization-session-repository-factory.js +31 -0
- package/modules/repositories/authorization-session-repository-interface.js +47 -0
- package/modules/repositories/authorization-session-repository-prisma.js +87 -0
- package/modules/repositories/entity-not-found.js +18 -0
- package/modules/repositories/module-repository-documentdb.js +51 -6
- package/modules/repositories/module-repository-interface.js +19 -2
- package/modules/repositories/module-repository-mongo.js +67 -74
- package/modules/repositories/module-repository-postgres.js +66 -74
- package/modules/repositories/module-repository.js +2 -1
- package/modules/requester/proxy-request.js +245 -0
- package/modules/requester/requester.js +35 -5
- package/modules/scope-config.js +192 -0
- package/modules/scope-policy.js +112 -0
- package/modules/scope-resolution.js +288 -0
- package/modules/scope-visibility.js +247 -0
- package/modules/tests/doubles/in-memory-authorization-session-repository.js +45 -0
- package/modules/tests/doubles/ownership-matching.js +18 -0
- package/modules/use-cases/authorize-entity-for-user.js +61 -0
- package/modules/use-cases/create-entity-from-credential.js +113 -0
- package/modules/use-cases/entity-operations-for-user.js +162 -0
- package/modules/use-cases/entity-types.js +139 -0
- package/modules/use-cases/execute-entity-proxy-request.js +331 -0
- package/modules/use-cases/get-authorization-requirements.js +100 -0
- package/modules/use-cases/get-entities-for-user.js +26 -13
- package/modules/use-cases/get-entity-module-for-user.js +113 -0
- package/modules/use-cases/get-entity-options-by-id.js +10 -4
- package/modules/use-cases/get-entity-options-by-type.js +1 -1
- package/modules/use-cases/get-module-instance-from-type.js +1 -1
- package/modules/use-cases/get-module.js +10 -4
- package/modules/use-cases/manage-access.js +21 -0
- package/modules/use-cases/multi-step-authorization.js +329 -0
- package/modules/use-cases/process-authorization-callback.js +53 -19
- package/modules/use-cases/refresh-entity-options.js +10 -4
- package/modules/use-cases/test-module-auth.js +10 -4
- package/package.json +6 -5
- package/prisma-mongodb/schema.prisma +92 -0
- package/prisma-postgresql/migrations/20261003000000_add_authorization_session/migration.sql +28 -0
- package/prisma-postgresql/migrations/20261015000000_add_organization_membership/migration.sql +26 -0
- package/prisma-postgresql/migrations/20261016000000_add_entity_integration_organization/migration.sql +25 -0
- package/prisma-postgresql/migrations/20261017000000_add_scheduled_job/migration.sql +28 -0
- package/prisma-postgresql/schema.prisma +87 -0
- package/providers/index.js +23 -0
- package/providers/resolve-provider.js +93 -0
- package/providers/runtime-detection.js +124 -0
- package/queues/index.js +25 -0
- package/queues/providers/index.js +11 -0
- package/queues/providers/netlify-background-provider.js +150 -0
- package/queues/providers/qstash-queue-provider.js +219 -0
- package/queues/providers/sqs-queue-provider.js +70 -0
- package/queues/queue-client-interface.js +55 -0
- package/queues/queue-message-auth.js +193 -0
- package/queues/queue-provider-factory.js +101 -0
- package/queues/queue-provider.js +65 -0
- package/queues/sqs-queue-client.js +81 -0
- package/types/index.d.ts +2 -0
- package/types/modules/index.d.ts +214 -0
- package/types/user/index.d.ts +134 -0
- package/user/adopter-jwt-config.js +177 -0
- package/user/adopter-jwt-errors.js +59 -0
- package/user/adopter-token-verifier.js +122 -0
- package/user/app-user-helpers.js +97 -0
- package/user/create-user-authentication.js +64 -0
- package/user/membership.js +51 -0
- package/user/repositories/organization-membership-record.js +19 -0
- package/user/repositories/organization-membership-repository-documentdb.js +106 -0
- package/user/repositories/organization-membership-repository-factory.js +42 -0
- package/user/repositories/organization-membership-repository-interface.js +57 -0
- package/user/repositories/organization-membership-repository-mongo.js +13 -0
- package/user/repositories/organization-membership-repository-postgres.js +29 -0
- package/user/repositories/organization-membership-repository-prisma.js +81 -0
- package/user/repositories/user-repository-documentdb.js +27 -2
- package/user/repositories/user-repository-interface.js +18 -0
- package/user/repositories/user-repository-mongo.js +24 -0
- package/user/repositories/user-repository-postgres.js +18 -0
- package/user/tests/doubles/adopter-jwt-fixtures.js +220 -0
- package/user/use-cases/authenticate-user.js +7 -1
- package/user/use-cases/ensure-organization-membership.js +92 -0
- package/user/use-cases/find-individual-users-by-organization.js +54 -0
- package/user/use-cases/get-user-from-adopter-jwt.js +95 -124
- package/user/use-cases/get-user-from-x-frigg-headers.js +83 -80
- package/user/user.js +64 -1
|
@@ -6,82 +6,120 @@ const {
|
|
|
6
6
|
IntegrationEventDispatcher,
|
|
7
7
|
} = require('../integration-event-dispatcher');
|
|
8
8
|
|
|
9
|
-
const handlers = {};
|
|
10
|
-
const { integrations: integrationClasses } = loadAppDefinition();
|
|
11
|
-
|
|
12
9
|
// `webhooks.received` is counted at the DB-connected ON_WEBHOOK dispatch (see
|
|
13
10
|
// usage-rollup-subscriber), not in this DB-free receipt handler.
|
|
14
11
|
|
|
15
|
-
|
|
16
|
-
|
|
12
|
+
/**
|
|
13
|
+
* Builds the webhook receiver router for every integration with webhooks
|
|
14
|
+
* enabled. Pure: no handler wrapping, so the AWS handlers below and other
|
|
15
|
+
* hosts (provider-netlify's webhooks function) mount the same routers.
|
|
16
|
+
*
|
|
17
|
+
* @param {Array<Function>} integrationClasses
|
|
18
|
+
* @returns {Array<{ key: string, eventName: string, router: import('express').Router }>}
|
|
19
|
+
*/
|
|
20
|
+
function buildWebhookRouters(integrationClasses) {
|
|
21
|
+
const entries = [];
|
|
22
|
+
for (const IntegrationClass of integrationClasses) {
|
|
23
|
+
const webhookConfig = IntegrationClass.Definition.webhooks;
|
|
17
24
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
+
// Skip if webhooks not enabled
|
|
26
|
+
if (
|
|
27
|
+
!webhookConfig ||
|
|
28
|
+
(typeof webhookConfig === 'object' && !webhookConfig.enabled)
|
|
29
|
+
) {
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
25
32
|
|
|
26
|
-
|
|
27
|
-
|
|
33
|
+
const router = Router();
|
|
34
|
+
const basePath = `/api/${IntegrationClass.Definition.name}-integration/webhooks`;
|
|
28
35
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
36
|
+
console.log(
|
|
37
|
+
`\n│ Configuring webhook routes for ${IntegrationClass.Definition.name}:`
|
|
38
|
+
);
|
|
32
39
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
40
|
+
// General webhook route (no integration ID)
|
|
41
|
+
router.post(basePath, async (req, res, next) => {
|
|
42
|
+
try {
|
|
43
|
+
const integrationInstance = new IntegrationClass();
|
|
44
|
+
const dispatcher = new IntegrationEventDispatcher(
|
|
45
|
+
integrationInstance
|
|
46
|
+
);
|
|
47
|
+
await dispatcher.dispatchHttp({
|
|
48
|
+
event: 'WEBHOOK_RECEIVED',
|
|
49
|
+
req,
|
|
50
|
+
res,
|
|
51
|
+
next,
|
|
52
|
+
});
|
|
53
|
+
} catch (error) {
|
|
54
|
+
next(error);
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
console.log(`│ POST ${basePath}`);
|
|
51
58
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
59
|
+
// Integration-specific webhook route (with integration ID)
|
|
60
|
+
router.post(`${basePath}/:integrationId`, async (req, res, next) => {
|
|
61
|
+
try {
|
|
62
|
+
const integrationInstance = new IntegrationClass();
|
|
63
|
+
const dispatcher = new IntegrationEventDispatcher(
|
|
64
|
+
integrationInstance
|
|
65
|
+
);
|
|
66
|
+
// Logs only: the id comes from an unauthenticated URL, so it must
|
|
67
|
+
// not reach the telemetry context that drives usage attribution.
|
|
68
|
+
await runInContext(
|
|
69
|
+
{
|
|
70
|
+
[LOGGER_SCOPE_KEY]: {
|
|
71
|
+
integrationId: req.params.integrationId,
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
() =>
|
|
75
|
+
dispatcher.dispatchHttp({
|
|
76
|
+
event: 'WEBHOOK_RECEIVED',
|
|
77
|
+
req,
|
|
78
|
+
res,
|
|
79
|
+
next,
|
|
80
|
+
})
|
|
81
|
+
);
|
|
82
|
+
} catch (error) {
|
|
83
|
+
next(error);
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
console.log(`│ POST ${basePath}/:integrationId`);
|
|
87
|
+
console.log('│');
|
|
77
88
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
`HTTP Event: ${IntegrationClass.Definition.name} Webhook`,
|
|
89
|
+
entries.push({
|
|
90
|
+
key: `${IntegrationClass.Definition.name}Webhook`,
|
|
91
|
+
eventName: `HTTP Event: ${IntegrationClass.Definition.name} Webhook`,
|
|
81
92
|
router,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
return entries;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* One router serving every integration's webhook routes, for hosts that run
|
|
100
|
+
* them all in one function (provider-netlify).
|
|
101
|
+
*
|
|
102
|
+
* @param {Array<Function>} [integrationClasses] - defaults to the app's.
|
|
103
|
+
* @returns {import('express').Router}
|
|
104
|
+
*/
|
|
105
|
+
function createWebhookRouter(integrationClasses) {
|
|
106
|
+
const classes = integrationClasses || loadAppDefinition().integrations;
|
|
107
|
+
const combined = Router();
|
|
108
|
+
for (const entry of buildWebhookRouters(classes)) {
|
|
109
|
+
combined.use(entry.router);
|
|
110
|
+
}
|
|
111
|
+
return combined;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const handlers = {};
|
|
115
|
+
for (const entry of buildWebhookRouters(loadAppDefinition().integrations)) {
|
|
116
|
+
handlers[entry.key] = {
|
|
117
|
+
handler: createAppHandler(
|
|
118
|
+
entry.eventName,
|
|
119
|
+
entry.router,
|
|
82
120
|
false // shouldUseDatabase = false
|
|
83
121
|
),
|
|
84
122
|
};
|
|
85
123
|
}
|
|
86
124
|
|
|
87
|
-
module.exports = { handlers };
|
|
125
|
+
module.exports = { handlers, buildWebhookRouters, createWebhookRouter };
|
|
@@ -10,10 +10,13 @@ const { createQueueWorker } = require('../backend-utils');
|
|
|
10
10
|
// const { getExtensionWorkers } = require('../../integrations/extension');
|
|
11
11
|
|
|
12
12
|
const handlers = {};
|
|
13
|
-
const { integrations: integrationClasses } =
|
|
13
|
+
const { integrations: integrationClasses, moduleDefinitions } =
|
|
14
|
+
loadAppDefinition();
|
|
14
15
|
|
|
15
16
|
integrationClasses.forEach((IntegrationClass) => {
|
|
16
|
-
const defaultQueueWorker = createQueueWorker(IntegrationClass
|
|
17
|
+
const defaultQueueWorker = createQueueWorker(IntegrationClass, {
|
|
18
|
+
moduleDefinitions,
|
|
19
|
+
});
|
|
17
20
|
|
|
18
21
|
handlers[`${IntegrationClass.Definition.name}`] = {
|
|
19
22
|
queueWorker: createHandler({
|
package/index.js
CHANGED
|
@@ -25,6 +25,14 @@ const {
|
|
|
25
25
|
UserRepositoryMongo,
|
|
26
26
|
UserRepositoryPostgres,
|
|
27
27
|
} = require('./user/repositories/user-repository-factory');
|
|
28
|
+
const {
|
|
29
|
+
createOrganizationMembershipRepository,
|
|
30
|
+
OrganizationMembershipRepositoryMongo,
|
|
31
|
+
OrganizationMembershipRepositoryPostgres,
|
|
32
|
+
} = require('./user/repositories/organization-membership-repository-factory');
|
|
33
|
+
const {
|
|
34
|
+
EnsureOrganizationMembership,
|
|
35
|
+
} = require('./user/use-cases/ensure-organization-membership');
|
|
28
36
|
const {
|
|
29
37
|
GetUserFromXFriggHeaders,
|
|
30
38
|
} = require('./user/use-cases/get-user-from-x-frigg-headers');
|
|
@@ -99,11 +107,36 @@ const {
|
|
|
99
107
|
Requester,
|
|
100
108
|
ModuleConstants,
|
|
101
109
|
ModuleFactory,
|
|
110
|
+
getEntityTypeRegistry,
|
|
111
|
+
EntityRegistryError,
|
|
112
|
+
resolveScope,
|
|
113
|
+
listBindingScopes,
|
|
114
|
+
OwnershipConfigError,
|
|
102
115
|
} = require('./modules/index');
|
|
116
|
+
const {
|
|
117
|
+
ManagementApiConfigError,
|
|
118
|
+
} = require('./management-api/management-api-config');
|
|
103
119
|
const application = require('./application');
|
|
104
120
|
const utils = require('./utils');
|
|
105
121
|
|
|
106
|
-
const {
|
|
122
|
+
const {
|
|
123
|
+
QueuerUtil,
|
|
124
|
+
QueueProvider,
|
|
125
|
+
QueueClientInterface,
|
|
126
|
+
createQueueProvider,
|
|
127
|
+
QUEUE_PROVIDERS,
|
|
128
|
+
} = require('./queues');
|
|
129
|
+
const {
|
|
130
|
+
resolveProvider,
|
|
131
|
+
determineProviderName,
|
|
132
|
+
providerPackageName,
|
|
133
|
+
KNOWN_PROVIDERS,
|
|
134
|
+
isDeployedRuntime,
|
|
135
|
+
} = require('./providers');
|
|
136
|
+
|
|
137
|
+
// App-level core extensions (appDefinition.coreExtensions): Prisma models, encrypted
|
|
138
|
+
// fields, admin routes and bootstrap hooks (friggframework/frigg#545).
|
|
139
|
+
const extensions = require('./extensions');
|
|
107
140
|
|
|
108
141
|
module.exports = {
|
|
109
142
|
// assertions
|
|
@@ -130,6 +163,10 @@ module.exports = {
|
|
|
130
163
|
createUserRepository,
|
|
131
164
|
UserRepositoryMongo,
|
|
132
165
|
UserRepositoryPostgres,
|
|
166
|
+
createOrganizationMembershipRepository,
|
|
167
|
+
OrganizationMembershipRepositoryMongo,
|
|
168
|
+
OrganizationMembershipRepositoryPostgres,
|
|
169
|
+
EnsureOrganizationMembership,
|
|
133
170
|
GetUserFromXFriggHeaders,
|
|
134
171
|
GetUserFromAdopterJwt,
|
|
135
172
|
AuthenticateUser,
|
|
@@ -150,6 +187,7 @@ module.exports = {
|
|
|
150
187
|
Options,
|
|
151
188
|
checkRequiredParams,
|
|
152
189
|
createIntegrationRouter,
|
|
190
|
+
// Deprecated (ADR-059): use getEntityTypeRegistry.
|
|
153
191
|
getModulesDefinitionFromIntegrationClasses,
|
|
154
192
|
LoadIntegrationContextUseCase,
|
|
155
193
|
CreateProcess,
|
|
@@ -206,8 +244,34 @@ module.exports = {
|
|
|
206
244
|
Requester,
|
|
207
245
|
ModuleConstants,
|
|
208
246
|
ModuleFactory,
|
|
247
|
+
getEntityTypeRegistry,
|
|
248
|
+
EntityRegistryError,
|
|
249
|
+
resolveScope,
|
|
250
|
+
listBindingScopes,
|
|
251
|
+
OwnershipConfigError,
|
|
252
|
+
ManagementApiConfigError,
|
|
209
253
|
// queues
|
|
210
254
|
QueuerUtil,
|
|
255
|
+
QueueProvider,
|
|
256
|
+
QueueClientInterface,
|
|
257
|
+
createQueueProvider,
|
|
258
|
+
QUEUE_PROVIDERS,
|
|
259
|
+
|
|
260
|
+
// provider plugins (appDefinition.provider)
|
|
261
|
+
resolveProvider,
|
|
262
|
+
determineProviderName,
|
|
263
|
+
providerPackageName,
|
|
264
|
+
KNOWN_PROVIDERS,
|
|
265
|
+
isDeployedRuntime,
|
|
266
|
+
|
|
267
|
+
// app-level extensions
|
|
268
|
+
extensions,
|
|
269
|
+
loadExtensions: extensions.loadExtensions,
|
|
270
|
+
composeSchemas: extensions.composeSchemas,
|
|
271
|
+
mountExtensionRoutes: extensions.mountExtensionRoutes,
|
|
272
|
+
createExtensionRoutesRouter: extensions.createExtensionRoutesRouter,
|
|
273
|
+
runExtensionBootstraps: extensions.runExtensionBootstraps,
|
|
274
|
+
initializeApp: extensions.initializeApp,
|
|
211
275
|
|
|
212
276
|
// utils
|
|
213
277
|
...utils,
|
|
@@ -5,13 +5,19 @@
|
|
|
5
5
|
* Follows hexagonal architecture with interface + adapters pattern.
|
|
6
6
|
*
|
|
7
7
|
* Providers:
|
|
8
|
-
* - eventbridge: AWS EventBridge Scheduler (production)
|
|
8
|
+
* - eventbridge: AWS EventBridge Scheduler (production on AWS)
|
|
9
|
+
* - netlify: poll-and-dispatch from a Netlify scheduled function
|
|
9
10
|
* - mock: In-memory mock scheduler (local development)
|
|
11
|
+
*
|
|
12
|
+
* Adapters are lazy getters (friggframework/frigg#545, 1176a004): the
|
|
13
|
+
* EventBridge adapter requires @aws-sdk/client-scheduler, which is not a
|
|
14
|
+
* core dependency, and core/index -> application -> scheduler-commands ->
|
|
15
|
+
* this barrel runs on every require('@friggframework/core'). An eager
|
|
16
|
+
* require crashed any deployment without that SDK (e.g. a pruned Netlify
|
|
17
|
+
* function bundle).
|
|
10
18
|
*/
|
|
11
19
|
|
|
12
20
|
const { SchedulerServiceInterface } = require('./scheduler-service-interface');
|
|
13
|
-
const { EventBridgeSchedulerAdapter } = require('./eventbridge-scheduler-adapter');
|
|
14
|
-
const { MockSchedulerAdapter } = require('./mock-scheduler-adapter');
|
|
15
21
|
const {
|
|
16
22
|
createSchedulerService,
|
|
17
23
|
SCHEDULER_PROVIDERS,
|
|
@@ -22,9 +28,16 @@ module.exports = {
|
|
|
22
28
|
// Interface (Port)
|
|
23
29
|
SchedulerServiceInterface,
|
|
24
30
|
|
|
25
|
-
// Adapters
|
|
26
|
-
EventBridgeSchedulerAdapter
|
|
27
|
-
|
|
31
|
+
// Adapters (loaded on access)
|
|
32
|
+
get EventBridgeSchedulerAdapter() {
|
|
33
|
+
return require('./eventbridge-scheduler-adapter').EventBridgeSchedulerAdapter;
|
|
34
|
+
},
|
|
35
|
+
get MockSchedulerAdapter() {
|
|
36
|
+
return require('./mock-scheduler-adapter').MockSchedulerAdapter;
|
|
37
|
+
},
|
|
38
|
+
get NetlifySchedulerAdapter() {
|
|
39
|
+
return require('./netlify-scheduler-adapter').NetlifySchedulerAdapter;
|
|
40
|
+
},
|
|
28
41
|
|
|
29
42
|
// Factory
|
|
30
43
|
createSchedulerService,
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Netlify Scheduler Adapter
|
|
3
|
+
*
|
|
4
|
+
* Implements SchedulerServiceInterface for Netlify deployments.
|
|
5
|
+
*
|
|
6
|
+
* Netlify Scheduled Functions use cron syntax but are defined statically in
|
|
7
|
+
* netlify.toml or via the @netlify/functions schedule() helper. They cannot
|
|
8
|
+
* be created dynamically at runtime like EventBridge Scheduler.
|
|
9
|
+
*
|
|
10
|
+
* Strategy for one-time scheduled jobs on Netlify:
|
|
11
|
+
* 1. Store the schedule in the database (same as mock adapter pattern)
|
|
12
|
+
* 2. A Netlify Scheduled Function runs on a cron interval (e.g., every 5 min)
|
|
13
|
+
* 3. The cron function queries for due schedules and dispatches them
|
|
14
|
+
* to a background function for execution
|
|
15
|
+
*
|
|
16
|
+
* This is a "poll-and-dispatch" pattern vs EventBridge's "push" pattern.
|
|
17
|
+
* Trade-off: slightly less precise timing (up to cron interval delay),
|
|
18
|
+
* but works on any platform without cloud-specific scheduling APIs.
|
|
19
|
+
*
|
|
20
|
+
* For integrations that need the queue provider to dispatch jobs:
|
|
21
|
+
* - queueResourceId maps to the background function URL path
|
|
22
|
+
* - Payload is stored and forwarded when the cron fires
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
const { SchedulerServiceInterface } = require('./scheduler-service-interface');
|
|
26
|
+
|
|
27
|
+
// The one queue worker every Netlify deployment has (provider-netlify's
|
|
28
|
+
// worker-background function); it dispatches by integrationName.
|
|
29
|
+
const DEFAULT_WORKER_PATH = '/.netlify/functions/worker-background';
|
|
30
|
+
const DEFAULT_LEASE_MS = 5 * 60 * 1000;
|
|
31
|
+
|
|
32
|
+
class NetlifySchedulerAdapter extends SchedulerServiceInterface {
|
|
33
|
+
/**
|
|
34
|
+
* @param {Object} options
|
|
35
|
+
* @param {Object} options.repository - Repository for persisting schedules
|
|
36
|
+
* Must implement: save(schedule), delete(scheduleName),
|
|
37
|
+
* findByName(scheduleName), findDue(now)
|
|
38
|
+
* @param {Object} [options.queueProvider] - Queue provider for dispatching due jobs
|
|
39
|
+
* @param {number} [options.leaseMs] - how long a claimed (PROCESSING) job
|
|
40
|
+
* is held before another tick may take it again (default 5 minutes,
|
|
41
|
+
* well past a Netlify scheduled function's 60-second limit)
|
|
42
|
+
*/
|
|
43
|
+
constructor(options = {}) {
|
|
44
|
+
super();
|
|
45
|
+
this.repository = options.repository;
|
|
46
|
+
this.queueProvider = options.queueProvider;
|
|
47
|
+
this.leaseMs = options.leaseMs ?? DEFAULT_LEASE_MS;
|
|
48
|
+
|
|
49
|
+
if (!this.repository) {
|
|
50
|
+
console.warn(
|
|
51
|
+
'[NetlifyScheduler] No repository provided. ' +
|
|
52
|
+
'Schedules will be stored in memory (lost on function restart). ' +
|
|
53
|
+
'Provide a database-backed repository for production use.'
|
|
54
|
+
);
|
|
55
|
+
this._inMemorySchedules = new Map();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Where a due job is sent. Netlify has no per-integration queues, so an
|
|
61
|
+
* SQS-style queue URL (often unset here) maps to the worker function;
|
|
62
|
+
* a function path is kept as given.
|
|
63
|
+
*
|
|
64
|
+
* @param {string} [queueUrl]
|
|
65
|
+
* @returns {string} a path NetlifyBackgroundProvider.send() accepts
|
|
66
|
+
*/
|
|
67
|
+
toQueueResourceId(queueUrl) {
|
|
68
|
+
return typeof queueUrl === 'string' && queueUrl.startsWith('/')
|
|
69
|
+
? queueUrl
|
|
70
|
+
: DEFAULT_WORKER_PATH;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async scheduleOneTime({
|
|
74
|
+
scheduleName,
|
|
75
|
+
scheduleAt,
|
|
76
|
+
queueResourceId,
|
|
77
|
+
payload,
|
|
78
|
+
}) {
|
|
79
|
+
if (!scheduleName) throw new Error('scheduleName is required');
|
|
80
|
+
if (!scheduleAt || !(scheduleAt instanceof Date))
|
|
81
|
+
throw new Error('scheduleAt must be a valid Date object');
|
|
82
|
+
if (!queueResourceId) throw new Error('queueResourceId is required');
|
|
83
|
+
|
|
84
|
+
const scheduleData = {
|
|
85
|
+
scheduleName,
|
|
86
|
+
scheduledAt: scheduleAt.toISOString(),
|
|
87
|
+
queueResourceId,
|
|
88
|
+
payload,
|
|
89
|
+
createdAt: new Date().toISOString(),
|
|
90
|
+
state: 'PENDING',
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
if (this.repository) {
|
|
94
|
+
await this.repository.save(scheduleData);
|
|
95
|
+
} else {
|
|
96
|
+
this._inMemorySchedules.set(scheduleName, scheduleData);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
console.log(
|
|
100
|
+
`[NetlifyScheduler] Scheduled: ${scheduleName} for ${scheduleAt.toISOString()}`
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
return {
|
|
104
|
+
scheduledJobId: `netlify-schedule-${scheduleName}`,
|
|
105
|
+
scheduledAt: scheduleAt.toISOString(),
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async deleteSchedule(scheduleName) {
|
|
110
|
+
if (!scheduleName) throw new Error('scheduleName is required');
|
|
111
|
+
|
|
112
|
+
if (this.repository) {
|
|
113
|
+
await this.repository.delete(scheduleName);
|
|
114
|
+
} else {
|
|
115
|
+
this._inMemorySchedules.delete(scheduleName);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
console.log(`[NetlifyScheduler] Deleted schedule: ${scheduleName}`);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async getScheduleStatus(scheduleName) {
|
|
122
|
+
if (!scheduleName) throw new Error('scheduleName is required');
|
|
123
|
+
|
|
124
|
+
let schedule;
|
|
125
|
+
if (this.repository) {
|
|
126
|
+
schedule = await this.repository.findByName(scheduleName);
|
|
127
|
+
} else {
|
|
128
|
+
schedule = this._inMemorySchedules.get(scheduleName);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (!schedule) {
|
|
132
|
+
return { exists: false };
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return {
|
|
136
|
+
exists: true,
|
|
137
|
+
scheduledAt: schedule.scheduledAt,
|
|
138
|
+
state: schedule.state,
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Process due schedules. Called by the Netlify cron function.
|
|
144
|
+
*
|
|
145
|
+
* Finds all schedules where scheduledAt <= now, dispatches them
|
|
146
|
+
* to the queue provider, and marks them as completed.
|
|
147
|
+
*
|
|
148
|
+
* @returns {Promise<{ processed: number, errors: number }>}
|
|
149
|
+
*/
|
|
150
|
+
async processDueSchedules() {
|
|
151
|
+
if (!this.repository) {
|
|
152
|
+
console.warn(
|
|
153
|
+
'[NetlifyScheduler] Cannot process due schedules without a repository'
|
|
154
|
+
);
|
|
155
|
+
return { processed: 0, errors: 0, skipped: 0 };
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const now = new Date();
|
|
159
|
+
const lease = { leaseMs: this.leaseMs };
|
|
160
|
+
const dueSchedules = await this.repository.findDue(now, lease);
|
|
161
|
+
|
|
162
|
+
let processed = 0;
|
|
163
|
+
let errors = 0;
|
|
164
|
+
let skipped = 0;
|
|
165
|
+
|
|
166
|
+
for (const schedule of dueSchedules) {
|
|
167
|
+
try {
|
|
168
|
+
// Claim before dispatch. Two overlapping ticks can both see
|
|
169
|
+
// the row in findDue; only the one whose conditional update
|
|
170
|
+
// wins sends it. A repository without claim() (a custom one)
|
|
171
|
+
// keeps the old unconditional PROCESSING write.
|
|
172
|
+
const claimed =
|
|
173
|
+
typeof this.repository.claim === 'function'
|
|
174
|
+
? await this.repository.claim(schedule.scheduleName, now, lease)
|
|
175
|
+
: (await this.repository.save({ ...schedule, state: 'PROCESSING' }), true);
|
|
176
|
+
if (!claimed) {
|
|
177
|
+
skipped++;
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (this.queueProvider) {
|
|
182
|
+
await this.queueProvider.send(
|
|
183
|
+
schedule.payload,
|
|
184
|
+
schedule.queueResourceId
|
|
185
|
+
);
|
|
186
|
+
} else {
|
|
187
|
+
console.log(
|
|
188
|
+
`[NetlifyScheduler] No queue provider — logging payload for: ${schedule.scheduleName}`,
|
|
189
|
+
JSON.stringify(schedule.payload)
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
await this.repository.delete(schedule.scheduleName);
|
|
194
|
+
processed++;
|
|
195
|
+
} catch (error) {
|
|
196
|
+
console.error(
|
|
197
|
+
`[NetlifyScheduler] Error processing schedule ${schedule.scheduleName}:`,
|
|
198
|
+
error
|
|
199
|
+
);
|
|
200
|
+
// Mark as FAILED so it won't be re-dispatched automatically.
|
|
201
|
+
// Operators can inspect FAILED schedules and retry manually.
|
|
202
|
+
await this.repository
|
|
203
|
+
.save({ ...schedule, state: 'FAILED' })
|
|
204
|
+
.catch(() => {});
|
|
205
|
+
errors++;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
console.log(
|
|
210
|
+
`[NetlifyScheduler] Processed ${processed} due schedules, ${errors} errors, ${skipped} claimed elsewhere`
|
|
211
|
+
);
|
|
212
|
+
return { processed, errors, skipped };
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
module.exports = { NetlifySchedulerAdapter, DEFAULT_WORKER_PATH, DEFAULT_LEASE_MS };
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Scheduled Job Repository
|
|
3
|
+
*
|
|
4
|
+
* Prisma-based implementation of the repository interface required by
|
|
5
|
+
* NetlifySchedulerAdapter. Persists one-time scheduled jobs to the
|
|
6
|
+
* ScheduledJob model so they survive function restarts.
|
|
7
|
+
*
|
|
8
|
+
* Repository interface:
|
|
9
|
+
* save(scheduleData) — Upsert a scheduled job
|
|
10
|
+
* delete(scheduleName) — Remove a scheduled job
|
|
11
|
+
* findByName(scheduleName) — Find a single scheduled job
|
|
12
|
+
* findDue(now, { leaseMs }) — Find due jobs: PENDING, or PROCESSING with an
|
|
13
|
+
* expired lease (a dispatcher that died after
|
|
14
|
+
* claiming), where scheduledAt <= now
|
|
15
|
+
* claim(scheduleName, now, { leaseMs })
|
|
16
|
+
* — Atomically move one due job to PROCESSING;
|
|
17
|
+
* true only for the one caller that won it
|
|
18
|
+
*
|
|
19
|
+
* The lease clock is the row's updatedAt: a claim writes it, so a PROCESSING
|
|
20
|
+
* row whose updatedAt is older than now - leaseMs is claimable again.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
const DEFAULT_LEASE_MS = 5 * 60 * 1000;
|
|
24
|
+
|
|
25
|
+
// Rows a dispatcher may take: never claimed, or claimed by one that has not
|
|
26
|
+
// finished within the lease.
|
|
27
|
+
function claimableWhere(now, leaseMs = DEFAULT_LEASE_MS) {
|
|
28
|
+
return {
|
|
29
|
+
OR: [
|
|
30
|
+
{ state: 'PENDING' },
|
|
31
|
+
{
|
|
32
|
+
state: 'PROCESSING',
|
|
33
|
+
updatedAt: { lt: new Date(now.getTime() - leaseMs) },
|
|
34
|
+
},
|
|
35
|
+
],
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
class ScheduledJobRepository {
|
|
40
|
+
/**
|
|
41
|
+
* @param {Object} options
|
|
42
|
+
* @param {import('@prisma/client').PrismaClient} options.prismaClient
|
|
43
|
+
*/
|
|
44
|
+
constructor({ prismaClient }) {
|
|
45
|
+
if (!prismaClient) {
|
|
46
|
+
throw new Error(
|
|
47
|
+
'ScheduledJobRepository requires a prismaClient instance'
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
this.prisma = prismaClient;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async save(scheduleData) {
|
|
54
|
+
return this.prisma.scheduledJob.upsert({
|
|
55
|
+
where: { scheduleName: scheduleData.scheduleName },
|
|
56
|
+
update: {
|
|
57
|
+
scheduledAt: new Date(scheduleData.scheduledAt),
|
|
58
|
+
queueResourceId: scheduleData.queueResourceId,
|
|
59
|
+
payload: scheduleData.payload ?? undefined,
|
|
60
|
+
state: scheduleData.state || 'PENDING',
|
|
61
|
+
},
|
|
62
|
+
create: {
|
|
63
|
+
scheduleName: scheduleData.scheduleName,
|
|
64
|
+
scheduledAt: new Date(scheduleData.scheduledAt),
|
|
65
|
+
queueResourceId: scheduleData.queueResourceId,
|
|
66
|
+
payload: scheduleData.payload ?? undefined,
|
|
67
|
+
state: scheduleData.state || 'PENDING',
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async delete(scheduleName) {
|
|
73
|
+
try {
|
|
74
|
+
await this.prisma.scheduledJob.delete({
|
|
75
|
+
where: { scheduleName },
|
|
76
|
+
});
|
|
77
|
+
} catch (error) {
|
|
78
|
+
// P2025 = record not found — safe to ignore on delete
|
|
79
|
+
if (error.code !== 'P2025') {
|
|
80
|
+
throw error;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async findByName(scheduleName) {
|
|
86
|
+
return this.prisma.scheduledJob.findUnique({
|
|
87
|
+
where: { scheduleName },
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async findDue(now, { leaseMs = DEFAULT_LEASE_MS } = {}) {
|
|
92
|
+
return this.prisma.scheduledJob.findMany({
|
|
93
|
+
where: {
|
|
94
|
+
scheduledAt: { lte: now },
|
|
95
|
+
...claimableWhere(now, leaseMs),
|
|
96
|
+
},
|
|
97
|
+
orderBy: { scheduledAt: 'asc' },
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Conditional update: only a row that is still claimable moves to
|
|
103
|
+
* PROCESSING, so of two overlapping ticks exactly one gets count === 1.
|
|
104
|
+
* (PostgreSQL re-checks the WHERE on the row it locked; MongoDB updates
|
|
105
|
+
* each document atomically.)
|
|
106
|
+
*
|
|
107
|
+
* @returns {Promise<boolean>} true when this caller claimed the job
|
|
108
|
+
*/
|
|
109
|
+
async claim(scheduleName, now, { leaseMs = DEFAULT_LEASE_MS } = {}) {
|
|
110
|
+
const { count } = await this.prisma.scheduledJob.updateMany({
|
|
111
|
+
where: { scheduleName, ...claimableWhere(now, leaseMs) },
|
|
112
|
+
data: { state: 'PROCESSING', updatedAt: now },
|
|
113
|
+
});
|
|
114
|
+
return count === 1;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
module.exports = { ScheduledJobRepository, DEFAULT_LEASE_MS };
|