@friggframework/core 2.0.0--canary.654.ebad223.0 → 2.0.0--canary.651.dadf4d6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.json +1 -18
- package/CLAUDE.md +62 -14
- package/README.md +22 -19
- package/core/CLAUDE.md +12 -25
- package/core/Worker.js +37 -31
- package/core/create-handler.js +197 -96
- package/core/index.js +1 -8
- package/core/secrets-to-env.js +6 -7
- package/database/config.js +7 -0
- package/database/documentdb-encryption-service.js +6 -2
- package/database/documentdb-utils.js +2 -1
- package/database/encryption/README.md +21 -5
- package/database/encryption/encryption-schema-registry.js +0 -3
- package/database/encryption/integration-mapping-encryption.js +97 -0
- package/database/encryption/prisma-encryption-extension.js +20 -9
- package/errors/fetch-error.js +65 -48
- package/handlers/app-definition-loader.js +2 -53
- package/handlers/app-handler-helpers.js +4 -18
- package/handlers/backend-utils.js +5 -0
- package/handlers/database-migration-handler.js +1 -10
- package/handlers/routers/websocket.js +6 -26
- package/handlers/workers/db-migration.js +1 -12
- package/handlers/workers/dlq-processor.js +38 -64
- package/index.js +1 -19
- package/integrations/integration-base.js +38 -56
- package/integrations/integration-router.js +20 -21
- package/integrations/repositories/integration-mapping-query-pipeline.js +152 -0
- package/integrations/repositories/integration-mapping-query.js +193 -0
- package/integrations/repositories/integration-mapping-repository-documentdb.js +53 -0
- package/integrations/repositories/integration-mapping-repository-interface.js +48 -0
- package/integrations/repositories/integration-mapping-repository-mongo.js +72 -0
- package/integrations/repositories/integration-mapping-repository-postgres.js +118 -0
- package/integrations/use-cases/delete-integration-for-user.js +13 -24
- package/jest.config.js +0 -2
- package/logs/.eslintrc.json +1 -26
- package/logs/index.js +3 -14
- package/logs/logger.js +42 -132
- package/modules/module.js +19 -32
- package/modules/requester/oauth-2.js +22 -28
- package/modules/requester/requester.js +6 -69
- package/package.json +6 -5
- package/queues/queuer-util.js +15 -1
- package/telemetry/bind-telemetry-context.js +0 -4
- package/telemetry/instrument-handler.js +2 -19
- package/telemetry/no-op-telemetry.js +0 -4
- package/telemetry/otel-telemetry.js +6 -25
- package/telemetry/telemetry-context.js +8 -19
- package/telemetry/telemetry-runtime.js +0 -17
- package/telemetry/telemetry-service.js +0 -1
- package/types/errors/index.d.ts +10 -24
- package/types/logs/index.d.ts +2 -61
- package/user/repositories/user-repository-documentdb.js +40 -8
- package/core/invocation-scope.js +0 -224
- package/logs/__fixtures__/events.js +0 -82
- package/logs/__fixtures__/matchers.js +0 -45
- package/logs/__fixtures__/secrets.js +0 -22
- package/logs/__fixtures__/vectors.js +0 -329
- package/logs/__fixtures__/with-env.js +0 -45
- package/logs/context.js +0 -92
- package/logs/debug-shims.js +0 -46
- package/logs/jest-logger-setup.js +0 -25
- package/logs/levels.js +0 -71
- package/logs/logger-runtime.js +0 -142
- package/logs/record.js +0 -231
- package/logs/redact.js +0 -333
- package/logs/serialize.js +0 -322
- package/logs/sinks.js +0 -99
- package/logs/summarize-event.js +0 -162
package/errors/fetch-error.js
CHANGED
|
@@ -1,65 +1,82 @@
|
|
|
1
1
|
const { BaseError } = require('./base-error');
|
|
2
|
-
const {
|
|
2
|
+
const { stripIndent } = require('common-tags');
|
|
3
|
+
|
|
4
|
+
// TODO hide header values
|
|
3
5
|
|
|
4
6
|
// Parameters names here are based on fetch. See:
|
|
5
7
|
// https://developer.mozilla.org/en-US/docs/Web/API/fetch
|
|
6
8
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
if (typeof resource === 'object' && typeof resource.url === 'string') {
|
|
10
|
-
return redactUrl(resource.url);
|
|
11
|
-
}
|
|
12
|
-
return redactUrl(resource);
|
|
13
|
-
}
|
|
9
|
+
class FetchError extends BaseError {
|
|
10
|
+
response = null;
|
|
14
11
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
12
|
+
constructor(options = {}) {
|
|
13
|
+
const { resource, init, response, responseBody } = options;
|
|
14
|
+
const method = init?.method ?? 'GET';
|
|
15
|
+
let initText = init
|
|
16
|
+
? init.body instanceof URLSearchParams
|
|
17
|
+
? (() => {
|
|
18
|
+
init.body = init.body.toString();
|
|
19
|
+
return JSON.stringify({ init }, null, 2);
|
|
20
|
+
})()
|
|
21
|
+
: JSON.stringify({ init }, null, 2)
|
|
22
|
+
: '';
|
|
20
23
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
return [method, url, outcome].filter(Boolean).join(' ');
|
|
28
|
-
}
|
|
24
|
+
let responseBodyText = '<response body is unavailable>';
|
|
25
|
+
if (typeof responseBody === 'string') {
|
|
26
|
+
responseBodyText = responseBody;
|
|
27
|
+
} else if (responseBody) {
|
|
28
|
+
responseBodyText = JSON.stringify(responseBody, null, 2);
|
|
29
|
+
}
|
|
29
30
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
31
|
+
const responseHeaders = {};
|
|
32
|
+
if (response?.headers) {
|
|
33
|
+
for (const [key, value] of response.headers) {
|
|
34
|
+
responseHeaders[key] = value;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
let responseHeaderText = response
|
|
39
|
+
? JSON.stringify({ headers: responseHeaders }, null, 2)
|
|
40
|
+
: '';
|
|
41
|
+
|
|
42
|
+
// sanitize error reporting
|
|
43
|
+
if (process.env.STAGE !== 'dev') {
|
|
44
|
+
initText = false;
|
|
45
|
+
responseHeaderText = false;
|
|
46
|
+
responseBodyText = false;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const messageParts = [
|
|
50
|
+
stripIndent`
|
|
51
|
+
-----------------------------------------------------
|
|
52
|
+
An error ocurred while fetching an external resource.
|
|
53
|
+
-----------------------------------------------------
|
|
54
|
+
>>> Request Details >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
55
|
+
${method} ${resource}
|
|
56
|
+
`,
|
|
57
|
+
initText,
|
|
58
|
+
stripIndent`
|
|
59
|
+
<<< Response Details <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
|
|
60
|
+
${response?.status} ${response?.statusText}
|
|
61
|
+
`,
|
|
62
|
+
responseHeaderText,
|
|
63
|
+
responseBodyText,
|
|
64
|
+
stripIndent`
|
|
65
|
+
-----------------------------------------------------
|
|
66
|
+
Stack Trace:
|
|
67
|
+
`,
|
|
68
|
+
];
|
|
69
|
+
|
|
70
|
+
super(messageParts.filter(Boolean).join('\n'));
|
|
38
71
|
|
|
72
|
+
this.response = response;
|
|
39
73
|
this.statusCode = response?.status;
|
|
40
|
-
this.method = method;
|
|
41
|
-
this.url = url;
|
|
42
|
-
Object.defineProperty(this, 'response', {
|
|
43
|
-
value: response ?? null,
|
|
44
|
-
enumerable: false,
|
|
45
|
-
writable: true,
|
|
46
|
-
configurable: true,
|
|
47
|
-
});
|
|
48
|
-
Object.defineProperty(this, 'body', {
|
|
49
|
-
value: responseBody ?? body,
|
|
50
|
-
enumerable: false,
|
|
51
|
-
writable: true,
|
|
52
|
-
configurable: true,
|
|
53
|
-
});
|
|
54
74
|
}
|
|
55
75
|
|
|
56
76
|
static async create(options = {}) {
|
|
57
77
|
const { response } = options;
|
|
58
|
-
let responseBody =
|
|
59
|
-
|
|
60
|
-
? await response.text()
|
|
61
|
-
: null;
|
|
62
|
-
if (!responseBody) responseBody = options.responseBody ?? options.body;
|
|
78
|
+
let responseBody = response?.bodyUsed ? null : await response?.text();
|
|
79
|
+
if (!responseBody && options.body) responseBody = options.body;
|
|
63
80
|
return new FetchError({ ...options, responseBody });
|
|
64
81
|
}
|
|
65
82
|
}
|
|
@@ -2,53 +2,13 @@ const { findNearestBackendPackageJson } = require('@friggframework/core/utils');
|
|
|
2
2
|
const path = require('node:path');
|
|
3
3
|
const fs = require('fs-extra');
|
|
4
4
|
const { resolveTelemetryConfig } = require('../telemetry/telemetry-config');
|
|
5
|
-
const { addDeniedKeys } = require('../logs/redact');
|
|
6
|
-
const { getLogger } = require('../logs');
|
|
7
|
-
const {
|
|
8
|
-
extractCredentialFieldsFromModules,
|
|
9
|
-
} = require('../database/encryption/encryption-schema-registry');
|
|
10
|
-
const {
|
|
11
|
-
getModulesDefinitionFromIntegrationClasses,
|
|
12
|
-
} = require('../integrations/utils/map-integration-dto');
|
|
13
|
-
|
|
14
|
-
// Handlers without a database never load the encryption registry, so the
|
|
15
|
-
// credential leaf keys are registered for redaction here too.
|
|
16
|
-
const warnedIgnoredKeys = new Set();
|
|
17
|
-
|
|
18
|
-
function warnIgnoredKeys(ignored) {
|
|
19
|
-
for (const key of ignored) {
|
|
20
|
-
if (warnedIgnoredKeys.has(key)) continue;
|
|
21
|
-
warnedIgnoredKeys.add(key);
|
|
22
|
-
getLogger('frigg.logger').warn(
|
|
23
|
-
'Credential field name is a record field; it stays visible in logs',
|
|
24
|
-
{ eventName: 'frigg.logger.denied_key_ignored', key }
|
|
25
|
-
);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function registerCredentialLogKeys(appDefinition, integrations) {
|
|
30
|
-
try {
|
|
31
|
-
const fields = extractCredentialFieldsFromModules(
|
|
32
|
-
getModulesDefinitionFromIntegrationClasses(integrations)
|
|
33
|
-
);
|
|
34
|
-
warnIgnoredKeys(addDeniedKeys(fields));
|
|
35
|
-
} catch {
|
|
36
|
-
// An odd integration shape must not stop the app from loading.
|
|
37
|
-
}
|
|
38
|
-
const schema = appDefinition.encryption?.schema;
|
|
39
|
-
if (schema && typeof schema === 'object') {
|
|
40
|
-
for (const config of Object.values(schema)) {
|
|
41
|
-
warnIgnoredKeys(addDeniedKeys(config?.fields));
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
5
|
|
|
46
6
|
/**
|
|
47
7
|
* Loads the App definition from the nearest backend package
|
|
48
8
|
* @function loadAppDefinition
|
|
49
9
|
* @description Searches for the nearest backend package.json, loads the corresponding index.js file,
|
|
50
10
|
* and extracts the application definition containing integrations and user configuration.
|
|
51
|
-
* @returns {{integrations: Array<object>, userConfig: object | null, adminScripts: Array<object>, reports: Array<object>, admin: object, telemetry: object
|
|
11
|
+
* @returns {{integrations: Array<object>, userConfig: object | null, adminScripts: Array<object>, reports: Array<object>, admin: object, telemetry: object}} An object containing the application definition.
|
|
52
12
|
* @throws {Error} Throws error if backend package.json cannot be found.
|
|
53
13
|
* @throws {Error} Throws error if index.js file cannot be found in the backend directory.
|
|
54
14
|
* @example
|
|
@@ -76,11 +36,8 @@ function loadAppDefinition() {
|
|
|
76
36
|
adminScripts = [],
|
|
77
37
|
reports = [],
|
|
78
38
|
admin = {},
|
|
79
|
-
logging = null,
|
|
80
39
|
} = appDefinition;
|
|
81
40
|
|
|
82
|
-
registerCredentialLogKeys(appDefinition, integrations);
|
|
83
|
-
|
|
84
41
|
// Degrade consistently: an invalid telemetry block must never take down a
|
|
85
42
|
// router bundle that loads the app definition at module scope (telemetry is
|
|
86
43
|
// never allowed to break a handler). The singletons apply the same
|
|
@@ -102,15 +59,7 @@ function loadAppDefinition() {
|
|
|
102
59
|
};
|
|
103
60
|
}
|
|
104
61
|
|
|
105
|
-
return {
|
|
106
|
-
integrations,
|
|
107
|
-
userConfig,
|
|
108
|
-
adminScripts,
|
|
109
|
-
reports,
|
|
110
|
-
admin,
|
|
111
|
-
telemetry,
|
|
112
|
-
logging,
|
|
113
|
-
};
|
|
62
|
+
return { integrations, userConfig, adminScripts, reports, admin, telemetry };
|
|
114
63
|
}
|
|
115
64
|
|
|
116
65
|
module.exports = {
|
|
@@ -1,14 +1,10 @@
|
|
|
1
|
-
const { createHandler } = require('@friggframework/core');
|
|
2
|
-
const { getLogger } = require('../logs');
|
|
3
|
-
const { summarizeExpressRequest } = require('../logs/summarize-event');
|
|
1
|
+
const { createHandler, flushDebugLog } = require('@friggframework/core');
|
|
4
2
|
const express = require('express');
|
|
5
3
|
const bodyParser = require('body-parser');
|
|
6
4
|
const cors = require('cors');
|
|
7
5
|
const Boom = require('@hapi/boom');
|
|
8
6
|
const serverlessHttp = require('serverless-http');
|
|
9
7
|
|
|
10
|
-
const log = getLogger('frigg.http');
|
|
11
|
-
|
|
12
8
|
const createApp = (applyMiddleware) => {
|
|
13
9
|
const app = express();
|
|
14
10
|
|
|
@@ -25,7 +21,7 @@ const createApp = (applyMiddleware) => {
|
|
|
25
21
|
|
|
26
22
|
if (applyMiddleware) applyMiddleware(app);
|
|
27
23
|
|
|
28
|
-
//
|
|
24
|
+
// Handle sending error response and logging server errors to console
|
|
29
25
|
app.use((err, req, res, next) => {
|
|
30
26
|
const boomError = err.isBoom ? err : Boom.boomify(err);
|
|
31
27
|
const {
|
|
@@ -33,20 +29,10 @@ const createApp = (applyMiddleware) => {
|
|
|
33
29
|
} = boomError;
|
|
34
30
|
|
|
35
31
|
if (statusCode >= 500) {
|
|
36
|
-
|
|
37
|
-
eventName: 'frigg.http.request_failed',
|
|
38
|
-
statusCode,
|
|
39
|
-
invocation: summarizeExpressRequest(req),
|
|
40
|
-
error: boomError,
|
|
41
|
-
});
|
|
32
|
+
flushDebugLog(boomError);
|
|
42
33
|
res.status(statusCode).json({ error: 'Internal Server Error' });
|
|
43
34
|
} else {
|
|
44
|
-
|
|
45
|
-
log.warn('Request rejected', {
|
|
46
|
-
eventName: 'frigg.http.request_rejected',
|
|
47
|
-
statusCode,
|
|
48
|
-
reason: boomError.message,
|
|
49
|
-
});
|
|
35
|
+
console.warn(`[Frigg] ${req.method} ${req.path} -> ${statusCode}: ${err.message}`);
|
|
50
36
|
res.status(statusCode).json({ error: err.message });
|
|
51
37
|
}
|
|
52
38
|
});
|
|
@@ -273,6 +273,11 @@ const createQueueWorker = (integrationClass) => {
|
|
|
273
273
|
return;
|
|
274
274
|
}
|
|
275
275
|
|
|
276
|
+
console.error(
|
|
277
|
+
`Error in ${params.event} for ${integrationName}:`,
|
|
278
|
+
error
|
|
279
|
+
);
|
|
280
|
+
|
|
276
281
|
// 4xx HTTP errors are permanent — the requester already
|
|
277
282
|
// attempted token refresh (401) and backoff (429/5xx).
|
|
278
283
|
// By the time a 4xx reaches here, retrying won't help.
|
|
@@ -24,9 +24,6 @@
|
|
|
24
24
|
|
|
25
25
|
const { execFile } = require('child_process');
|
|
26
26
|
const path = require('path');
|
|
27
|
-
const { getLogger } = require('../logs');
|
|
28
|
-
|
|
29
|
-
const log = getLogger('frigg.database.migration');
|
|
30
27
|
|
|
31
28
|
/**
|
|
32
29
|
* Execute Prisma migration command
|
|
@@ -156,13 +153,7 @@ exports.handler = async (event, context) => {
|
|
|
156
153
|
console.log('='.repeat(60));
|
|
157
154
|
console.log('Database Migration Handler');
|
|
158
155
|
console.log('='.repeat(60));
|
|
159
|
-
log
|
|
160
|
-
eventName: 'frigg.database.migration.invoked',
|
|
161
|
-
migrationId: event.migrationId,
|
|
162
|
-
dbType: process.env.DB_TYPE,
|
|
163
|
-
targetStage: process.env.STAGE,
|
|
164
|
-
action: event.command || 'deploy',
|
|
165
|
-
});
|
|
156
|
+
console.log('Event:', JSON.stringify(event, null, 2));
|
|
166
157
|
console.log('Context:', JSON.stringify({
|
|
167
158
|
functionName: context.functionName,
|
|
168
159
|
functionVersion: context.functionVersion,
|
|
@@ -1,8 +1,5 @@
|
|
|
1
1
|
const { createHandler } = require('@friggframework/core');
|
|
2
2
|
const { createWebsocketConnectionRepository } = require('../../database/websocket-connection-repository-factory');
|
|
3
|
-
const { getLogger } = require('../../logs');
|
|
4
|
-
|
|
5
|
-
const log = getLogger('frigg.websocket');
|
|
6
3
|
|
|
7
4
|
const websocketConnectionRepository = createWebsocketConnectionRepository();
|
|
8
5
|
|
|
@@ -14,16 +11,10 @@ const handleWebSocketConnection = async (event, context) => {
|
|
|
14
11
|
try {
|
|
15
12
|
const connectionId = event.requestContext.connectionId;
|
|
16
13
|
await websocketConnectionRepository.createConnection(connectionId);
|
|
17
|
-
log
|
|
18
|
-
eventName: 'frigg.websocket.connected',
|
|
19
|
-
connectionId,
|
|
20
|
-
});
|
|
14
|
+
console.log(`Stored new connection: ${connectionId}`);
|
|
21
15
|
return { statusCode: 200, body: 'Connected.' };
|
|
22
16
|
} catch (error) {
|
|
23
|
-
|
|
24
|
-
eventName: 'frigg.websocket.connect_failed',
|
|
25
|
-
error,
|
|
26
|
-
});
|
|
17
|
+
console.error('Error storing connection:', error);
|
|
27
18
|
return { statusCode: 500, body: 'Error connecting.' };
|
|
28
19
|
}
|
|
29
20
|
|
|
@@ -32,28 +23,17 @@ const handleWebSocketConnection = async (event, context) => {
|
|
|
32
23
|
try {
|
|
33
24
|
const connectionId = event.requestContext.connectionId;
|
|
34
25
|
await websocketConnectionRepository.deleteConnection(connectionId);
|
|
35
|
-
log
|
|
36
|
-
eventName: 'frigg.websocket.disconnected',
|
|
37
|
-
connectionId,
|
|
38
|
-
});
|
|
26
|
+
console.log(`Removed connection: ${connectionId}`);
|
|
39
27
|
return { statusCode: 200, body: 'Disconnected.' };
|
|
40
28
|
} catch (error) {
|
|
41
|
-
|
|
42
|
-
eventName: 'frigg.websocket.disconnect_failed',
|
|
43
|
-
error,
|
|
44
|
-
});
|
|
29
|
+
console.error('Error removing connection:', error);
|
|
45
30
|
return { statusCode: 500, body: 'Error disconnecting.' };
|
|
46
31
|
}
|
|
47
32
|
|
|
48
33
|
case 'MESSAGE':
|
|
49
34
|
// Handle incoming message
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
log.info('Received message', {
|
|
53
|
-
eventName: 'frigg.websocket.message_received',
|
|
54
|
-
connectionId: event.requestContext.connectionId,
|
|
55
|
-
bodyLength: event.body?.length ?? 0,
|
|
56
|
-
});
|
|
35
|
+
const message = JSON.parse(event.body);
|
|
36
|
+
console.log('Received message:', message);
|
|
57
37
|
|
|
58
38
|
// Process the message and send a response
|
|
59
39
|
const responseMessage = { message: 'Message received' };
|
|
@@ -55,9 +55,6 @@ const {
|
|
|
55
55
|
|
|
56
56
|
// Inject prisma-runner as dependency
|
|
57
57
|
const prismaRunner = require('../../database/utils/prisma-runner');
|
|
58
|
-
const { getLogger } = require('../../logs');
|
|
59
|
-
|
|
60
|
-
const log = getLogger('frigg.database.migration');
|
|
61
58
|
|
|
62
59
|
// Use S3 repository for migration status tracking (no User table dependency)
|
|
63
60
|
const bucketName = process.env.S3_BUCKET_NAME || process.env.MIGRATION_STATUS_BUCKET;
|
|
@@ -147,6 +144,7 @@ exports.handler = async (event, context) => {
|
|
|
147
144
|
console.log('========================================');
|
|
148
145
|
console.log('Database Migration Lambda Started');
|
|
149
146
|
console.log('========================================');
|
|
147
|
+
console.log('Event:', JSON.stringify(event, null, 2));
|
|
150
148
|
console.log('Context:', JSON.stringify({
|
|
151
149
|
requestId: context.requestId,
|
|
152
150
|
functionName: context.functionName,
|
|
@@ -159,15 +157,6 @@ exports.handler = async (event, context) => {
|
|
|
159
157
|
// Check for action parameter (direct invocation for status checks)
|
|
160
158
|
const action = event.action || 'migrate'; // Default to migration
|
|
161
159
|
|
|
162
|
-
// targetStage, not stage: the record's own stage field would win.
|
|
163
|
-
log.info('Database migration invoked', {
|
|
164
|
-
eventName: 'frigg.database.migration.invoked',
|
|
165
|
-
migrationId,
|
|
166
|
-
dbType,
|
|
167
|
-
targetStage: stage,
|
|
168
|
-
action,
|
|
169
|
-
});
|
|
170
|
-
|
|
171
160
|
// Handle checkStatus action
|
|
172
161
|
if (action === 'checkStatus') {
|
|
173
162
|
console.log(`\n========================================`);
|
|
@@ -5,18 +5,6 @@
|
|
|
5
5
|
* This handler MUST NOT throw. If it throws, the message goes back to
|
|
6
6
|
* the DLQ and creates an infinite loop. All errors are caught and logged.
|
|
7
7
|
*/
|
|
8
|
-
const crypto = require('node:crypto');
|
|
9
|
-
const { getLogger } = require('../../logs');
|
|
10
|
-
const {
|
|
11
|
-
summarizeLambdaEvent,
|
|
12
|
-
toScopeInvocation,
|
|
13
|
-
} = require('../../logs/summarize-event');
|
|
14
|
-
const {
|
|
15
|
-
runInvocationScope,
|
|
16
|
-
runMessageScope,
|
|
17
|
-
} = require('../../core/invocation-scope');
|
|
18
|
-
|
|
19
|
-
const log = getLogger('frigg.queue.dlq');
|
|
20
8
|
|
|
21
9
|
function extractQueueName(eventSourceARN) {
|
|
22
10
|
if (!eventSourceARN) return 'UNKNOWN';
|
|
@@ -24,64 +12,50 @@ function extractQueueName(eventSourceARN) {
|
|
|
24
12
|
return parts[parts.length - 1] || 'UNKNOWN';
|
|
25
13
|
}
|
|
26
14
|
|
|
27
|
-
|
|
28
|
-
function describeBody(body) {
|
|
29
|
-
const text = typeof body === 'string' ? body : String(body ?? '');
|
|
30
|
-
let parsed = true;
|
|
15
|
+
function parseMessageBody(body) {
|
|
31
16
|
try {
|
|
32
|
-
JSON.parse(
|
|
33
|
-
|
|
34
|
-
|
|
17
|
+
const parsed = JSON.parse(body);
|
|
18
|
+
return {
|
|
19
|
+
event: parsed.event || 'UNKNOWN',
|
|
20
|
+
integrationId: parsed.data?.integrationId || null,
|
|
21
|
+
processId: parsed.data?.processId || null,
|
|
22
|
+
data: parsed.data,
|
|
23
|
+
};
|
|
24
|
+
} catch (error) {
|
|
25
|
+
console.warn('[DLQ] Failed to parse message body', { error: error.message, body });
|
|
26
|
+
return {
|
|
27
|
+
event: 'UNKNOWN',
|
|
28
|
+
integrationId: null,
|
|
29
|
+
processId: null,
|
|
30
|
+
rawBody: body,
|
|
31
|
+
};
|
|
35
32
|
}
|
|
36
|
-
return {
|
|
37
|
-
parsed,
|
|
38
|
-
bodyLength: Buffer.byteLength(text),
|
|
39
|
-
bodySha256: crypto.createHash('sha256').update(text).digest('hex'),
|
|
40
|
-
};
|
|
41
33
|
}
|
|
42
34
|
|
|
43
|
-
function
|
|
44
|
-
const { parsed, ...body } = describeBody(record.body);
|
|
45
|
-
if (!parsed) {
|
|
46
|
-
log.warn('DLQ message body is not JSON', {
|
|
47
|
-
eventName: 'frigg.queue.dlq.body_unparsed',
|
|
48
|
-
...body,
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
// messageId, receiveCount and the body ids come from the message scope.
|
|
52
|
-
log.error('Message reached the DLQ', {
|
|
53
|
-
eventName: 'frigg.queue.dlq.message_failed',
|
|
54
|
-
sentTimestamp: record.attributes?.SentTimestamp,
|
|
55
|
-
sourceQueue: extractQueueName(record.eventSourceARN),
|
|
56
|
-
...body,
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
async function dlqProcessor(event, context) {
|
|
35
|
+
async function dlqProcessor(event) {
|
|
61
36
|
if (!event?.Records?.length) return { batchItemFailures: [] };
|
|
62
37
|
|
|
63
|
-
|
|
64
|
-
{
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
);
|
|
38
|
+
for (const record of event.Records) {
|
|
39
|
+
try {
|
|
40
|
+
const parsed = parseMessageBody(record.body);
|
|
41
|
+
|
|
42
|
+
console.error('[DLQ] Failed message', {
|
|
43
|
+
messageId: record.messageId,
|
|
44
|
+
event: parsed.event,
|
|
45
|
+
integrationId: parsed.integrationId,
|
|
46
|
+
processId: parsed.processId,
|
|
47
|
+
receiveCount: record.attributes?.ApproximateReceiveCount,
|
|
48
|
+
sentTimestamp: record.attributes?.SentTimestamp,
|
|
49
|
+
sourceQueue: extractQueueName(record.eventSourceARN),
|
|
50
|
+
...(parsed.rawBody !== undefined && { rawBody: parsed.rawBody }),
|
|
51
|
+
});
|
|
52
|
+
} catch (error) {
|
|
53
|
+
console.error('[DLQ] Error processing DLQ record', {
|
|
54
|
+
messageId: record.messageId,
|
|
55
|
+
error: error.message,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
85
59
|
|
|
86
60
|
return { batchItemFailures: [] };
|
|
87
61
|
}
|
package/index.js
CHANGED
|
@@ -11,7 +11,6 @@ const {
|
|
|
11
11
|
Worker,
|
|
12
12
|
loadInstalledModules,
|
|
13
13
|
createHandler,
|
|
14
|
-
runInvocationScope,
|
|
15
14
|
} = require('./core/index');
|
|
16
15
|
const {
|
|
17
16
|
prisma,
|
|
@@ -79,17 +78,7 @@ const {
|
|
|
79
78
|
} = require('./telemetry/index');
|
|
80
79
|
const { createUsageRepository } = require('./usage/index');
|
|
81
80
|
const { TimeoutCatcher } = require('./lambda/index');
|
|
82
|
-
const {
|
|
83
|
-
getLogger,
|
|
84
|
-
createMemorySink,
|
|
85
|
-
resetLoggerForTests,
|
|
86
|
-
serializeError,
|
|
87
|
-
redactValue,
|
|
88
|
-
toSanitizedSurrogate,
|
|
89
|
-
debug,
|
|
90
|
-
initDebugLog,
|
|
91
|
-
flushDebugLog,
|
|
92
|
-
} = require('./logs/index');
|
|
81
|
+
const { debug, initDebugLog, flushDebugLog } = require('./logs/index');
|
|
93
82
|
const {
|
|
94
83
|
Credential,
|
|
95
84
|
Entity,
|
|
@@ -119,7 +108,6 @@ module.exports = {
|
|
|
119
108
|
Worker,
|
|
120
109
|
loadInstalledModules,
|
|
121
110
|
createHandler,
|
|
122
|
-
runInvocationScope,
|
|
123
111
|
|
|
124
112
|
// database
|
|
125
113
|
prisma,
|
|
@@ -187,12 +175,6 @@ module.exports = {
|
|
|
187
175
|
TimeoutCatcher,
|
|
188
176
|
|
|
189
177
|
// logs
|
|
190
|
-
getLogger,
|
|
191
|
-
createMemorySink,
|
|
192
|
-
resetLoggerForTests,
|
|
193
|
-
serializeError,
|
|
194
|
-
redactValue,
|
|
195
|
-
toSanitizedSurrogate,
|
|
196
178
|
debug,
|
|
197
179
|
initDebugLog,
|
|
198
180
|
flushDebugLog,
|