@friggframework/core 2.0.0--canary.686.df38e8e.0 → 2.0.0--canary.710.9045bbc.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 +0 -16
- package/CLAUDE.md +41 -29
- package/README.md +0 -105
- package/application/commands/README.md +17 -11
- package/application/commands/entity-commands.js +18 -60
- package/application/commands/integration-commands.js +73 -20
- package/application/commands/scheduler-commands.js +7 -11
- package/application/commands/user-commands.js +5 -17
- package/application/index.js +2 -6
- package/core/Worker.js +7 -31
- package/core/create-handler.js +0 -9
- package/credential/repositories/credential-repository-documentdb.js +0 -34
- package/credential/repositories/credential-repository-interface.js +0 -15
- package/credential/repositories/credential-repository-mongo.js +0 -23
- package/credential/repositories/credential-repository-postgres.js +0 -28
- package/database/config.js +24 -48
- package/database/documentdb-encryption-service.js +22 -9
- package/database/documentdb-utils.js +0 -2
- package/database/encryption/README.md +17 -81
- package/database/encryption/documentdb-encryption-service.md +1 -78
- package/database/encryption/encryption-schema-registry.js +46 -123
- package/database/encryption/field-encryption-service.js +5 -69
- package/database/encryption/integration-mapping-encryption.js +1 -1
- package/database/prisma.js +38 -15
- package/database/use-cases/check-encryption-health-use-case.js +52 -43
- package/errors/index.js +4 -0
- package/errors/mapping-already-exists-error.js +14 -0
- package/generated/prisma-mongodb/edge.js +4 -43
- package/generated/prisma-mongodb/index-browser.js +1 -40
- package/generated/prisma-mongodb/index.d.ts +111 -4739
- package/generated/prisma-mongodb/index.js +4 -43
- package/generated/prisma-mongodb/package.json +1 -1
- package/generated/prisma-mongodb/schema.prisma +0 -92
- package/generated/prisma-mongodb/wasm.js +4 -43
- package/generated/prisma-postgresql/edge.js +4 -43
- package/generated/prisma-postgresql/index-browser.js +1 -40
- package/generated/prisma-postgresql/index.d.ts +33 -4966
- package/generated/prisma-postgresql/index.js +4 -43
- package/generated/prisma-postgresql/package.json +1 -1
- package/generated/prisma-postgresql/schema.prisma +0 -87
- package/generated/prisma-postgresql/wasm.js +4 -43
- package/handlers/WEBHOOKS.md +29 -0
- package/handlers/app-definition-loader.js +19 -121
- package/handlers/app-handler-helpers.js +3 -25
- package/handlers/backend-utils.js +25 -31
- package/handlers/middleware/admin-auth.js +1 -14
- package/handlers/routers/auth.js +10 -46
- package/handlers/routers/health.js +4 -13
- package/handlers/routers/integration-defined-routers.js +92 -144
- package/handlers/routers/integration-webhook-routers.js +64 -102
- package/handlers/workers/integration-defined-workers.js +2 -5
- package/index.js +3 -65
- package/infrastructure/scheduler/eventbridge-scheduler-adapter.js +67 -13
- package/infrastructure/scheduler/index.js +6 -19
- package/infrastructure/scheduler/scheduler-service-factory.js +7 -55
- package/integrations/index.js +0 -1
- package/integrations/integration-base.js +29 -0
- package/integrations/integration-router.js +96 -131
- package/integrations/repositories/integration-mapping-repository-documentdb.js +50 -0
- package/integrations/repositories/integration-mapping-repository-interface.js +15 -0
- package/integrations/repositories/integration-mapping-repository-mongo.js +29 -0
- package/integrations/repositories/integration-mapping-repository-postgres.js +30 -0
- package/integrations/repositories/integration-mapping-repository.js +29 -0
- package/integrations/repositories/integration-repository-documentdb.js +35 -35
- package/integrations/repositories/integration-repository-interface.js +31 -17
- package/integrations/repositories/integration-repository-mongo.js +129 -60
- package/integrations/repositories/integration-repository-postgres.js +134 -62
- package/integrations/repositories/process-repository-mongo.js +1 -1
- package/integrations/tests/doubles/test-integration-repository.js +1 -12
- package/integrations/use-cases/add-entities-to-integration.js +92 -0
- package/integrations/use-cases/create-integration.js +75 -72
- package/integrations/use-cases/get-integrations-for-user.js +40 -80
- package/integrations/use-cases/index.js +1 -7
- package/integrations/use-cases/remove-entities-from-integration.js +70 -0
- package/integrations/utils/assert-entity-ids-input.js +42 -0
- package/integrations/utils/map-integration-dto.js +14 -43
- package/jest-setup.js +13 -2
- package/jest-teardown.js +5 -2
- package/jest.config.js +4 -0
- package/jest.db.config.js +23 -0
- package/modules/index.js +0 -14
- package/modules/module-factory.js +0 -12
- package/modules/module.js +2 -53
- package/modules/repositories/module-repository-documentdb.js +6 -51
- package/modules/repositories/module-repository-interface.js +2 -19
- package/modules/repositories/module-repository-mongo.js +74 -67
- package/modules/repositories/module-repository-postgres.js +74 -66
- package/modules/repositories/module-repository.js +1 -2
- package/modules/requester/requester.js +2 -27
- package/modules/use-cases/get-entities-for-user.js +13 -26
- package/modules/use-cases/get-entity-options-by-id.js +4 -10
- 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 +4 -10
- package/modules/use-cases/process-authorization-callback.js +19 -53
- package/modules/use-cases/refresh-entity-options.js +4 -10
- package/modules/use-cases/test-module-auth.js +4 -10
- package/modules/utils/map-module-dto.js +5 -3
- package/modules/utils/to-credential-id.js +14 -0
- package/package.json +7 -6
- package/prisma-mongodb/schema.prisma +0 -92
- package/prisma-postgresql/schema.prisma +0 -87
- package/queues/index.js +0 -25
- package/queues/queuer-util.js +6 -5
- package/types/index.d.ts +0 -2
- package/user/repositories/user-repository-documentdb.js +2 -27
- package/user/repositories/user-repository-interface.js +0 -18
- package/user/repositories/user-repository-mongo.js +0 -24
- package/user/repositories/user-repository-postgres.js +0 -18
- package/user/tests/doubles/test-user-repository.js +1 -1
- package/user/use-cases/authenticate-user.js +1 -7
- package/user/use-cases/get-user-from-adopter-jwt.js +124 -95
- package/user/use-cases/get-user-from-bearer-token.js +7 -2
- package/user/use-cases/get-user-from-x-frigg-headers.js +80 -83
- package/user/user.js +1 -64
- package/credential/use-cases/credential-operations-for-user.js +0 -202
- package/credential/use-cases/reauthorize-credential-for-user.js +0 -73
- package/database/encryption/encryption-config.js +0 -219
- package/database/encryption/test-support/fake-ciphertext.js +0 -15
- package/database/organization-where.js +0 -18
- package/database/unique-violation.js +0 -25
- package/extensions/bootstrap-runner.js +0 -43
- package/extensions/extension-loader.js +0 -187
- package/extensions/index.js +0 -41
- package/extensions/initialize-app.js +0 -123
- package/extensions/route-mounter.js +0 -103
- package/extensions/schema-composer.js +0 -188
- package/infrastructure/scheduler/netlify-scheduler-adapter.js +0 -220
- package/infrastructure/scheduler/scheduled-job-repository.js +0 -118
- package/integrations/repositories/integration-not-found.js +0 -14
- package/integrations/schedule-config.js +0 -80
- package/integrations/use-cases/dispatch-ongoing-syncs.js +0 -100
- package/integrations/use-cases/integration-operations-for-user.js +0 -256
- package/integrations/use-cases/resolve-integration-ownership.js +0 -177
- package/management-api/create-management-api-router.js +0 -49
- package/management-api/error-response.js +0 -116
- package/management-api/management-api-config.js +0 -151
- package/management-api/meta-router.js +0 -76
- package/management-api/openapi/build-openapi.js +0 -144
- package/management-api/openapi/components.js +0 -408
- package/management-api/openapi/v1-operations.js +0 -83
- package/management-api/openapi/v2-operations.js +0 -264
- package/management-api/route-registry.js +0 -411
- package/management-api/test-support/http-client.js +0 -39
- package/management-api/use-cases/get-management-api-meta.js +0 -57
- package/management-api/v1-deprecation.js +0 -120
- package/management-api/v2/create-v2-router.js +0 -85
- package/management-api/v2/dependencies.js +0 -281
- package/management-api/v2/dto.js +0 -143
- package/management-api/v2/handlers/authorization.js +0 -94
- package/management-api/v2/handlers/credentials.js +0 -33
- package/management-api/v2/handlers/entities.js +0 -77
- package/management-api/v2/handlers/integrations.js +0 -128
- package/management-api/v2/handlers/proxy.js +0 -34
- package/management-api/v2/request.js +0 -85
- package/management-api/v2/test-support/fake-dependencies.js +0 -103
- package/modules/domain/authorization-session.js +0 -144
- package/modules/entity-scope.js +0 -15
- package/modules/entity-type-registry.js +0 -311
- package/modules/proxy/proxy-errors.js +0 -20
- package/modules/proxy/proxy-path.js +0 -179
- package/modules/proxy/proxy-policy.js +0 -248
- package/modules/repositories/authorization-session-repository-documentdb.js +0 -112
- package/modules/repositories/authorization-session-repository-factory.js +0 -31
- package/modules/repositories/authorization-session-repository-interface.js +0 -47
- package/modules/repositories/authorization-session-repository-prisma.js +0 -87
- package/modules/repositories/entity-not-found.js +0 -18
- package/modules/requester/proxy-request.js +0 -244
- package/modules/scope-config.js +0 -192
- package/modules/scope-policy.js +0 -112
- package/modules/scope-resolution.js +0 -288
- package/modules/scope-visibility.js +0 -247
- package/modules/tests/doubles/in-memory-authorization-session-repository.js +0 -45
- package/modules/tests/doubles/ownership-matching.js +0 -18
- package/modules/use-cases/authorize-entity-for-user.js +0 -61
- package/modules/use-cases/create-entity-from-credential.js +0 -113
- package/modules/use-cases/entity-operations-for-user.js +0 -162
- package/modules/use-cases/entity-types.js +0 -139
- package/modules/use-cases/execute-entity-proxy-request.js +0 -286
- package/modules/use-cases/get-authorization-requirements.js +0 -100
- package/modules/use-cases/get-entity-module-for-user.js +0 -113
- package/modules/use-cases/manage-access.js +0 -21
- package/modules/use-cases/multi-step-authorization.js +0 -329
- package/prisma-postgresql/migrations/20261003000000_add_authorization_session/migration.sql +0 -28
- package/prisma-postgresql/migrations/20261015000000_add_organization_membership/migration.sql +0 -26
- package/prisma-postgresql/migrations/20261016000000_add_entity_integration_organization/migration.sql +0 -25
- package/prisma-postgresql/migrations/20261017000000_add_scheduled_job/migration.sql +0 -28
- package/providers/index.js +0 -23
- package/providers/resolve-provider.js +0 -93
- package/providers/runtime-detection.js +0 -124
- package/queues/providers/index.js +0 -11
- package/queues/providers/netlify-background-provider.js +0 -187
- package/queues/providers/qstash-queue-provider.js +0 -219
- package/queues/providers/sqs-queue-provider.js +0 -70
- package/queues/queue-client-interface.js +0 -55
- package/queues/queue-message-auth.js +0 -193
- package/queues/queue-provider-factory.js +0 -101
- package/queues/queue-provider.js +0 -65
- package/queues/sqs-queue-client.js +0 -81
- package/types/modules/index.d.ts +0 -197
- package/types/user/index.d.ts +0 -134
- package/user/adopter-jwt-config.js +0 -177
- package/user/adopter-jwt-errors.js +0 -59
- package/user/adopter-token-verifier.js +0 -122
- package/user/app-user-helpers.js +0 -97
- package/user/create-user-authentication.js +0 -64
- package/user/membership.js +0 -51
- package/user/repositories/organization-membership-record.js +0 -19
- package/user/repositories/organization-membership-repository-documentdb.js +0 -106
- package/user/repositories/organization-membership-repository-factory.js +0 -42
- package/user/repositories/organization-membership-repository-interface.js +0 -57
- package/user/repositories/organization-membership-repository-mongo.js +0 -13
- package/user/repositories/organization-membership-repository-postgres.js +0 -29
- package/user/repositories/organization-membership-repository-prisma.js +0 -81
- package/user/tests/doubles/adopter-jwt-fixtures.js +0 -220
- package/user/use-cases/ensure-organization-membership.js +0 -92
- package/user/use-cases/find-individual-users-by-organization.js +0 -54
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
const { NetlifyBackgroundProvider } = require('./netlify-background-provider');
|
|
2
|
-
const { QStashQueueProvider } = require('./qstash-queue-provider');
|
|
3
|
-
|
|
4
|
-
module.exports = {
|
|
5
|
-
NetlifyBackgroundProvider,
|
|
6
|
-
QStashQueueProvider,
|
|
7
|
-
// Lazy: requiring the barrel must not construct anything AWS-specific.
|
|
8
|
-
get SqsQueueProvider() {
|
|
9
|
-
return require('./sqs-queue-provider').SqsQueueProvider;
|
|
10
|
-
},
|
|
11
|
-
};
|
|
@@ -1,187 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Netlify Background Functions Queue Provider (Adapter)
|
|
3
|
-
*
|
|
4
|
-
* Uses Netlify Background Functions for async job processing.
|
|
5
|
-
* Background functions have a 15-minute execution limit and are triggered via HTTP POST.
|
|
6
|
-
* They return 202 immediately while processing continues.
|
|
7
|
-
*
|
|
8
|
-
* Queue ID format: The function name or URL path for the background function.
|
|
9
|
-
* Convention: function files named with '-background' suffix (e.g., 'worker-background.js')
|
|
10
|
-
*
|
|
11
|
-
* Authentication: a background function is a public URL and Netlify does
|
|
12
|
-
* not authenticate its caller, so every message is signed with
|
|
13
|
-
* FRIGG_QUEUE_SIGNING_SECRET (HMAC-SHA256 over timestamp + raw body, see
|
|
14
|
-
* ../queue-message-auth.js) and verifyEvent() checks that signature before
|
|
15
|
-
* the worker parses or dispatches anything. Without the secret, send() and
|
|
16
|
-
* verifyEvent() refuse, except outside production with
|
|
17
|
-
* FRIGG_QUEUE_ALLOW_UNSIGNED=true.
|
|
18
|
-
*
|
|
19
|
-
* Limitations:
|
|
20
|
-
* - No built-in retry (must implement manually)
|
|
21
|
-
* - No dead-letter queue
|
|
22
|
-
* - No FIFO ordering guarantees
|
|
23
|
-
* - 15-minute max execution time
|
|
24
|
-
*/
|
|
25
|
-
const { QueueProvider } = require('../queue-provider');
|
|
26
|
-
const {
|
|
27
|
-
SIGNING_SECRET_ENV,
|
|
28
|
-
ALLOW_UNSIGNED_ENV,
|
|
29
|
-
SIGNATURE_HEADER,
|
|
30
|
-
TIMESTAMP_HEADER,
|
|
31
|
-
allowsUnsignedMessages,
|
|
32
|
-
readSigningSecret,
|
|
33
|
-
signQueueMessage,
|
|
34
|
-
verifyQueueMessage,
|
|
35
|
-
readHeader,
|
|
36
|
-
readRawBody,
|
|
37
|
-
} = require('../queue-message-auth');
|
|
38
|
-
|
|
39
|
-
// A queue target is a background function on this site, named by path and
|
|
40
|
-
// nothing else: no scheme, no host, no '//', no '..', no query. Anything
|
|
41
|
-
// else would let a stored or configured queue id send a signed message to
|
|
42
|
-
// another host (a protocol-relative '//host/x' when the base URL is empty)
|
|
43
|
-
// or to a function that is not a queue worker.
|
|
44
|
-
const BACKGROUND_FUNCTION_PATH = /^\/\.netlify\/functions\/[A-Za-z0-9][A-Za-z0-9_-]*-background$/;
|
|
45
|
-
|
|
46
|
-
function isBackgroundFunctionPath(queueId) {
|
|
47
|
-
return typeof queueId === 'string' && BACKGROUND_FUNCTION_PATH.test(queueId);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* The URL a message for `queueId` is POSTed to, or a thrown error.
|
|
52
|
-
* The base must be an absolute http(s) origin; the path must be a
|
|
53
|
-
* background function (see BACKGROUND_FUNCTION_PATH).
|
|
54
|
-
*/
|
|
55
|
-
function backgroundFunctionUrl(baseUrl, queueId) {
|
|
56
|
-
if (!isBackgroundFunctionPath(queueId)) {
|
|
57
|
-
throw new Error(
|
|
58
|
-
`Refusing to send to queue id ${JSON.stringify(String(queueId))}: ` +
|
|
59
|
-
"a Netlify queue id must be '/.netlify/functions/<name>-background'"
|
|
60
|
-
);
|
|
61
|
-
}
|
|
62
|
-
let base;
|
|
63
|
-
try {
|
|
64
|
-
base = new URL(baseUrl);
|
|
65
|
-
} catch {
|
|
66
|
-
throw new Error(
|
|
67
|
-
'NetlifyBackgroundProvider needs an absolute site URL (options.baseUrl or the URL environment variable)'
|
|
68
|
-
);
|
|
69
|
-
}
|
|
70
|
-
if (!['http:', 'https:'].includes(base.protocol) || base.username || base.password) {
|
|
71
|
-
throw new Error('NetlifyBackgroundProvider site URL must be http(s) with no credentials');
|
|
72
|
-
}
|
|
73
|
-
return `${base.origin}${queueId}`;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
class NetlifyBackgroundProvider extends QueueProvider {
|
|
77
|
-
/**
|
|
78
|
-
* @param {Object} [options]
|
|
79
|
-
* @param {string} [options.baseUrl] - site URL (default process.env.URL)
|
|
80
|
-
* @param {string} [options.signingSecret] - default FRIGG_QUEUE_SIGNING_SECRET, read per call
|
|
81
|
-
* @param {Object} [options.env] - environment to read (default process.env)
|
|
82
|
-
* @param {() => number} [options.now] - clock in milliseconds (tests)
|
|
83
|
-
*/
|
|
84
|
-
constructor(options = {}) {
|
|
85
|
-
super();
|
|
86
|
-
// Base URL for triggering background functions (e.g., https://mysite.netlify.app)
|
|
87
|
-
this.baseUrl = options.baseUrl || process.env.URL || '';
|
|
88
|
-
this.signingSecret = options.signingSecret;
|
|
89
|
-
this.env = options.env || process.env;
|
|
90
|
-
this.now = options.now || Date.now;
|
|
91
|
-
// verifyEvent() proves an HTTP delivery came from Frigg.
|
|
92
|
-
this.verifiesHttpDelivery = true;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
// Read on every call, not in the constructor: secrets may be loaded into
|
|
96
|
-
// the environment after the provider is built (loadSecrets).
|
|
97
|
-
_secret() {
|
|
98
|
-
return this.signingSecret || readSigningSecret(this.env);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Send a message by invoking a Netlify Background Function via HTTP POST.
|
|
103
|
-
*
|
|
104
|
-
* @param {Object} message - Message payload
|
|
105
|
-
* @param {string} queueId - Background function path (e.g., '/.netlify/functions/worker-background')
|
|
106
|
-
*/
|
|
107
|
-
async send(message, queueId) {
|
|
108
|
-
const url = backgroundFunctionUrl(this.baseUrl, queueId);
|
|
109
|
-
const body = JSON.stringify(message);
|
|
110
|
-
const headers = { 'Content-Type': 'application/json' };
|
|
111
|
-
|
|
112
|
-
const secret = this._secret();
|
|
113
|
-
if (secret) {
|
|
114
|
-
Object.assign(headers, signQueueMessage({ body, secret, now: this.now() }));
|
|
115
|
-
} else if (!allowsUnsignedMessages(this.env)) {
|
|
116
|
-
// The worker would refuse the message, and a background function
|
|
117
|
-
// answers 202 either way, so the loss would be silent. Fail here.
|
|
118
|
-
throw new Error(
|
|
119
|
-
`${SIGNING_SECRET_ENV} is required to send to a Netlify background function ` +
|
|
120
|
-
`(${ALLOW_UNSIGNED_ENV}=true allows unsigned messages outside production only)`
|
|
121
|
-
);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
const response = await fetch(url, {
|
|
125
|
-
method: 'POST',
|
|
126
|
-
headers,
|
|
127
|
-
body,
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
if (!response.ok && response.status !== 202) {
|
|
131
|
-
throw new Error(
|
|
132
|
-
`Failed to invoke background function at ${queueId}: ${response.status} ${response.statusText}`
|
|
133
|
-
);
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
return { status: response.status, queueId };
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* Send batch of messages sequentially (background functions are HTTP-triggered).
|
|
141
|
-
*/
|
|
142
|
-
async batchSend(batch, queueId) {
|
|
143
|
-
const entries = batch ?? [];
|
|
144
|
-
const results = [];
|
|
145
|
-
for (const entry of entries) {
|
|
146
|
-
const result = await this.send(entry, queueId);
|
|
147
|
-
results.push(result);
|
|
148
|
-
}
|
|
149
|
-
return { results };
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Check the Frigg signature on an incoming background-function event.
|
|
154
|
-
*
|
|
155
|
-
* @param {Object} event - Netlify v1 (Lambda-shaped) event
|
|
156
|
-
* @returns {Promise<{ ok: boolean, reason?: string, unsigned?: boolean }>}
|
|
157
|
-
*/
|
|
158
|
-
async verifyEvent(event) {
|
|
159
|
-
const secret = this._secret();
|
|
160
|
-
if (!secret) {
|
|
161
|
-
return allowsUnsignedMessages(this.env)
|
|
162
|
-
? { ok: true, unsigned: true }
|
|
163
|
-
: { ok: false, reason: 'signing_secret_not_configured' };
|
|
164
|
-
}
|
|
165
|
-
return verifyQueueMessage({
|
|
166
|
-
body: readRawBody(event),
|
|
167
|
-
timestamp: readHeader(event, TIMESTAMP_HEADER),
|
|
168
|
-
signature: readHeader(event, SIGNATURE_HEADER),
|
|
169
|
-
secret,
|
|
170
|
-
now: this.now(),
|
|
171
|
-
});
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* Parse Netlify Background Function event.
|
|
176
|
-
* Background functions receive the raw HTTP event body as the event.
|
|
177
|
-
*/
|
|
178
|
-
parseEvent(event) {
|
|
179
|
-
const raw = readRawBody(event);
|
|
180
|
-
const body = raw !== null ? JSON.parse(raw) : event.body;
|
|
181
|
-
|
|
182
|
-
// Background functions receive a single message per invocation
|
|
183
|
-
return [body];
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
module.exports = { NetlifyBackgroundProvider, isBackgroundFunctionPath, backgroundFunctionUrl };
|
|
@@ -1,219 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* QStash Queue Provider (Adapter)
|
|
3
|
-
*
|
|
4
|
-
* Upstash QStash implementation - platform-agnostic HTTP-based message queue.
|
|
5
|
-
* Works on any platform (Netlify, Vercel, AWS, bare metal, etc.)
|
|
6
|
-
*
|
|
7
|
-
* Features:
|
|
8
|
-
* - Configurable retry with exponential backoff
|
|
9
|
-
* - Dead-letter queue via callbacks
|
|
10
|
-
* - At-least-once delivery
|
|
11
|
-
* - Scheduling support (delay, cron)
|
|
12
|
-
* - Message deduplication
|
|
13
|
-
*
|
|
14
|
-
* Requires:
|
|
15
|
-
* - QSTASH_TOKEN env var (from Upstash console)
|
|
16
|
-
* - QSTASH_CURRENT_SIGNING_KEY and QSTASH_NEXT_SIGNING_KEY for verification
|
|
17
|
-
*
|
|
18
|
-
* Queue ID format: The destination URL that QStash will POST to when delivering the message.
|
|
19
|
-
*
|
|
20
|
-
* Verification (verifyEvent), per Upstash's documented scheme and its
|
|
21
|
-
* @upstash/qstash Receiver: every delivery carries an `Upstash-Signature`
|
|
22
|
-
* header, a JWT signed HS256 with the current signing key (the next key
|
|
23
|
-
* during rotation). Its claims are iss "Upstash", sub the destination URL,
|
|
24
|
-
* exp / nbf / iat, and body = base64url(SHA-256(raw body)). A delivery is
|
|
25
|
-
* accepted when the JWT verifies with either key, carries exp, nbf, sub and
|
|
26
|
-
* body, is inside its exp / nbf window, names this request's URL as its sub
|
|
27
|
-
* (the configured destinationUrl, else the event's rawUrl: a delivery
|
|
28
|
-
* published to another endpoint cannot be replayed here), and its body claim
|
|
29
|
-
* matches the body received (trailing '=' padding ignored, as the Receiver
|
|
30
|
-
* does). A request whose URL cannot be determined is refused. With neither
|
|
31
|
-
* key configured it is refused, except outside production with
|
|
32
|
-
* FRIGG_QUEUE_ALLOW_UNSIGNED=true.
|
|
33
|
-
*/
|
|
34
|
-
const crypto = require('crypto');
|
|
35
|
-
const { QueueProvider } = require('../queue-provider');
|
|
36
|
-
const {
|
|
37
|
-
allowsUnsignedMessages,
|
|
38
|
-
readHeader,
|
|
39
|
-
readRawBody,
|
|
40
|
-
} = require('../queue-message-auth');
|
|
41
|
-
|
|
42
|
-
const QSTASH_API_BASE = 'https://qstash.upstash.io/v2';
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* @param {*} value
|
|
46
|
-
* @returns {string|null} the URL in canonical form, or null when it is not one
|
|
47
|
-
*/
|
|
48
|
-
function normalizeUrl(value) {
|
|
49
|
-
if (typeof value !== 'string' || value.length === 0) return null;
|
|
50
|
-
try {
|
|
51
|
-
return new URL(value).href;
|
|
52
|
-
} catch (_) {
|
|
53
|
-
return null;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
class QStashQueueProvider extends QueueProvider {
|
|
58
|
-
constructor(options = {}) {
|
|
59
|
-
super();
|
|
60
|
-
this.token =
|
|
61
|
-
options.token || process.env.QSTASH_TOKEN;
|
|
62
|
-
this.retries =
|
|
63
|
-
options.retries !== undefined ? options.retries : 3;
|
|
64
|
-
this.currentSigningKey = options.currentSigningKey;
|
|
65
|
-
this.nextSigningKey = options.nextSigningKey;
|
|
66
|
-
// The URL QStash delivers to. When unset, the JWT's sub is compared
|
|
67
|
-
// with the request's own URL (event.rawUrl, set by Netlify).
|
|
68
|
-
this.destinationUrl = options.destinationUrl;
|
|
69
|
-
this.env = options.env || process.env;
|
|
70
|
-
this.clockToleranceSeconds = options.clockToleranceSeconds || 0;
|
|
71
|
-
// verifyEvent() proves an HTTP delivery came from QStash.
|
|
72
|
-
this.verifiesHttpDelivery = true;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// Read per call: secrets may reach the environment after construction.
|
|
76
|
-
_signingKeys() {
|
|
77
|
-
return [
|
|
78
|
-
this.currentSigningKey || this.env.QSTASH_CURRENT_SIGNING_KEY,
|
|
79
|
-
this.nextSigningKey || this.env.QSTASH_NEXT_SIGNING_KEY,
|
|
80
|
-
].filter((key) => typeof key === 'string' && key.length > 0);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Verify the Upstash-Signature JWT of a QStash delivery.
|
|
85
|
-
*
|
|
86
|
-
* @param {Object} event - Lambda-shaped HTTP event
|
|
87
|
-
* @returns {Promise<{ ok: boolean, reason?: string, unsigned?: boolean }>}
|
|
88
|
-
*/
|
|
89
|
-
async verifyEvent(event) {
|
|
90
|
-
const keys = this._signingKeys();
|
|
91
|
-
if (keys.length === 0) {
|
|
92
|
-
return allowsUnsignedMessages(this.env)
|
|
93
|
-
? { ok: true, unsigned: true }
|
|
94
|
-
: { ok: false, reason: 'signing_keys_not_configured' };
|
|
95
|
-
}
|
|
96
|
-
const token = readHeader(event, 'upstash-signature');
|
|
97
|
-
if (!token) return { ok: false, reason: 'missing_signature' };
|
|
98
|
-
const body = readRawBody(event);
|
|
99
|
-
if (body === null) return { ok: false, reason: 'missing_body' };
|
|
100
|
-
|
|
101
|
-
const jose = require('jose');
|
|
102
|
-
let payload;
|
|
103
|
-
for (const key of keys) {
|
|
104
|
-
try {
|
|
105
|
-
({ payload } = await jose.jwtVerify(token, new TextEncoder().encode(key), {
|
|
106
|
-
issuer: 'Upstash',
|
|
107
|
-
algorithms: ['HS256'],
|
|
108
|
-
requiredClaims: ['exp', 'nbf', 'sub', 'body'],
|
|
109
|
-
clockTolerance: this.clockToleranceSeconds,
|
|
110
|
-
}));
|
|
111
|
-
break;
|
|
112
|
-
} catch (_) {
|
|
113
|
-
// try the next key
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
if (!payload) return { ok: false, reason: 'invalid_signature' };
|
|
117
|
-
|
|
118
|
-
const requestUrl = normalizeUrl(this.destinationUrl || event?.rawUrl);
|
|
119
|
-
if (!requestUrl) return { ok: false, reason: 'missing_request_url' };
|
|
120
|
-
if (normalizeUrl(payload.sub) !== requestUrl) {
|
|
121
|
-
return { ok: false, reason: 'invalid_subject' };
|
|
122
|
-
}
|
|
123
|
-
const bodyHash = crypto.createHash('sha256').update(body, 'utf8').digest('base64url');
|
|
124
|
-
const claimed = typeof payload.body === 'string' ? payload.body.replace(/=+$/, '') : '';
|
|
125
|
-
if (claimed !== bodyHash.replace(/=+$/, '')) {
|
|
126
|
-
return { ok: false, reason: 'body_mismatch' };
|
|
127
|
-
}
|
|
128
|
-
return { ok: true };
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Send a message via QStash.
|
|
133
|
-
*
|
|
134
|
-
* @param {Object} message - Message payload
|
|
135
|
-
* @param {string} queueId - Destination URL that QStash will POST to
|
|
136
|
-
*/
|
|
137
|
-
async send(message, queueId) {
|
|
138
|
-
if (!this.token) {
|
|
139
|
-
throw new Error(
|
|
140
|
-
'QSTASH_TOKEN is required. Get one at https://console.upstash.com'
|
|
141
|
-
);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
const headers = {
|
|
145
|
-
Authorization: `Bearer ${this.token}`,
|
|
146
|
-
'Content-Type': 'application/json',
|
|
147
|
-
'Upstash-Retries': String(this.retries),
|
|
148
|
-
};
|
|
149
|
-
|
|
150
|
-
const response = await fetch(
|
|
151
|
-
`${QSTASH_API_BASE}/publish/${queueId}`,
|
|
152
|
-
{
|
|
153
|
-
method: 'POST',
|
|
154
|
-
headers,
|
|
155
|
-
body: JSON.stringify(message),
|
|
156
|
-
}
|
|
157
|
-
);
|
|
158
|
-
|
|
159
|
-
if (!response.ok) {
|
|
160
|
-
const errorBody = await response.text().catch(() => 'unknown');
|
|
161
|
-
throw new Error(
|
|
162
|
-
`QStash publish failed: ${response.status} ${response.statusText} - ${errorBody}`
|
|
163
|
-
);
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
return response.json();
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* Send batch of messages via QStash batch API.
|
|
171
|
-
*/
|
|
172
|
-
async batchSend(batch, queueId) {
|
|
173
|
-
const entries = batch ?? [];
|
|
174
|
-
if (!this.token) {
|
|
175
|
-
throw new Error(
|
|
176
|
-
'QSTASH_TOKEN is required. Get one at https://console.upstash.com'
|
|
177
|
-
);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
const messages = entries.map((entry) => ({
|
|
181
|
-
destination: queueId,
|
|
182
|
-
body: JSON.stringify(entry),
|
|
183
|
-
headers: { 'Content-Type': 'application/json' },
|
|
184
|
-
retries: this.retries,
|
|
185
|
-
}));
|
|
186
|
-
|
|
187
|
-
const response = await fetch(`${QSTASH_API_BASE}/batch`, {
|
|
188
|
-
method: 'POST',
|
|
189
|
-
headers: {
|
|
190
|
-
Authorization: `Bearer ${this.token}`,
|
|
191
|
-
'Content-Type': 'application/json',
|
|
192
|
-
},
|
|
193
|
-
body: JSON.stringify(messages),
|
|
194
|
-
});
|
|
195
|
-
|
|
196
|
-
if (!response.ok) {
|
|
197
|
-
const errorBody = await response.text().catch(() => 'unknown');
|
|
198
|
-
throw new Error(
|
|
199
|
-
`QStash batch publish failed: ${response.status} - ${errorBody}`
|
|
200
|
-
);
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
return response.json();
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* Parse QStash webhook delivery event.
|
|
208
|
-
* QStash POSTs the message body directly to the destination URL.
|
|
209
|
-
*/
|
|
210
|
-
parseEvent(event) {
|
|
211
|
-
const raw = readRawBody(event);
|
|
212
|
-
const body = raw !== null ? JSON.parse(raw) : event.body;
|
|
213
|
-
|
|
214
|
-
// QStash delivers a single message per invocation
|
|
215
|
-
return [body];
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
module.exports = { QStashQueueProvider };
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* SQS Queue Provider (Adapter)
|
|
3
|
-
*
|
|
4
|
-
* AWS SQS implementation of the QueueProvider port, from
|
|
5
|
-
* friggframework/frigg#545 (where it lives in @friggframework/provider-aws).
|
|
6
|
-
* Kept in core here because the AWS SDK is still a core dependency; the SDK
|
|
7
|
-
* is required on first send, not at module load.
|
|
8
|
-
*/
|
|
9
|
-
const { v4: uuid } = require('uuid');
|
|
10
|
-
const { QueueProvider } = require('../queue-provider');
|
|
11
|
-
const {
|
|
12
|
-
SqsQueueClient,
|
|
13
|
-
defaultSqsClientConfig,
|
|
14
|
-
} = require('../sqs-queue-client');
|
|
15
|
-
|
|
16
|
-
const SQS_BATCH_SIZE = 10;
|
|
17
|
-
|
|
18
|
-
class SqsQueueProvider extends QueueProvider {
|
|
19
|
-
/**
|
|
20
|
-
* @param {Object} [options]
|
|
21
|
-
* @param {QueueClientInterface} [options.queueClient] - defaults to an SqsQueueClient.
|
|
22
|
-
*/
|
|
23
|
-
constructor({ queueClient } = {}) {
|
|
24
|
-
super();
|
|
25
|
-
this.queueClient =
|
|
26
|
-
queueClient ||
|
|
27
|
-
new SqsQueueClient({ clientConfig: defaultSqsClientConfig() });
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
async send(message, queueUrl) {
|
|
31
|
-
return this.queueClient.sendMessage({
|
|
32
|
-
MessageBody: JSON.stringify(message),
|
|
33
|
-
QueueUrl: queueUrl,
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
async batchSend(batch, queueUrl) {
|
|
38
|
-
const entries = batch ?? [];
|
|
39
|
-
let last = {};
|
|
40
|
-
for (let i = 0; i < entries.length; i += SQS_BATCH_SIZE) {
|
|
41
|
-
const Entries = entries.slice(i, i + SQS_BATCH_SIZE).map((entry) => ({
|
|
42
|
-
Id: uuid(),
|
|
43
|
-
MessageBody: JSON.stringify(entry),
|
|
44
|
-
}));
|
|
45
|
-
last = await this.queueClient.sendMessageBatch({
|
|
46
|
-
Entries,
|
|
47
|
-
QueueUrl: queueUrl,
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
return last;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Parse an SQS event: event.Records[].body (JSON string).
|
|
55
|
-
*/
|
|
56
|
-
parseEvent(event) {
|
|
57
|
-
const records = event?.Records || [];
|
|
58
|
-
return records.map((record) => JSON.parse(record.body));
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* SQS events reach a Lambda only through its event source mapping, which
|
|
63
|
-
* AWS authenticates; there is no public URL to post to.
|
|
64
|
-
*/
|
|
65
|
-
async verifyEvent() {
|
|
66
|
-
return { ok: true };
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
module.exports = { SqsQueueProvider };
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Queue Client Interface (Port)
|
|
3
|
-
*
|
|
4
|
-
* Defines the contract for low-level queue message operations.
|
|
5
|
-
* Used by Worker class and QueuerUtil for sending/receiving queue messages.
|
|
6
|
-
*
|
|
7
|
-
* Following Frigg's hexagonal architecture pattern:
|
|
8
|
-
* - Port defines WHAT the service does (contract)
|
|
9
|
-
* - Adapters implement HOW (AWS SQS, Netlify, QStash, etc.)
|
|
10
|
-
*
|
|
11
|
-
* Distinct from QueueProvider which is the higher-level provider for
|
|
12
|
-
* integration-level queue operations (send/batchSend/parseEvent).
|
|
13
|
-
* QueueClientInterface is the lower-level "how do I talk to the queue service" port.
|
|
14
|
-
*/
|
|
15
|
-
class QueueClientInterface {
|
|
16
|
-
/**
|
|
17
|
-
* Send a single message to a queue
|
|
18
|
-
*
|
|
19
|
-
* @param {Object} params
|
|
20
|
-
* @param {string} params.QueueUrl - Queue endpoint/identifier
|
|
21
|
-
* @param {string} params.MessageBody - Serialized message body
|
|
22
|
-
* @param {number} [params.DelaySeconds] - Delay before message becomes visible
|
|
23
|
-
* @returns {Promise<{MessageId: string}>} Result with message identifier
|
|
24
|
-
*/
|
|
25
|
-
async sendMessage(params) {
|
|
26
|
-
throw new Error('Method sendMessage must be implemented by subclass');
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Send a batch of messages to a queue
|
|
31
|
-
*
|
|
32
|
-
* @param {Object} params
|
|
33
|
-
* @param {string} params.QueueUrl - Queue endpoint/identifier
|
|
34
|
-
* @param {Array<{Id: string, MessageBody: string}>} params.Entries - Messages to send
|
|
35
|
-
* @returns {Promise<Object>} Batch send result
|
|
36
|
-
*/
|
|
37
|
-
async sendMessageBatch(params) {
|
|
38
|
-
throw new Error(
|
|
39
|
-
'Method sendMessageBatch must be implemented by subclass'
|
|
40
|
-
);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Resolve a queue name to a URL/endpoint
|
|
45
|
-
*
|
|
46
|
-
* @param {Object} params
|
|
47
|
-
* @param {string} params.QueueName - Queue name to resolve
|
|
48
|
-
* @returns {Promise<string>} Resolved queue URL/endpoint
|
|
49
|
-
*/
|
|
50
|
-
async getQueueUrl(params) {
|
|
51
|
-
throw new Error('Method getQueueUrl must be implemented by subclass');
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
module.exports = { QueueClientInterface };
|