@friggframework/core 2.0.0--canary.658.130b4ec.0 → 2.0.0--canary.656.602fc50.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/CLAUDE.md +1 -47
- package/errors/rate-limit-error.js +1 -0
- package/handlers/backend-utils.js +1 -2
- package/integrations/integration-base.js +0 -16
- package/integrations/repositories/integration-repository-documentdb.js +10 -23
- package/integrations/repositories/integration-repository-interface.js +7 -21
- package/integrations/repositories/integration-repository-mongo.js +16 -29
- package/integrations/repositories/integration-repository-postgres.js +16 -30
- package/integrations/tests/doubles/test-integration-repository.js +2 -15
- package/integrations/use-cases/update-integration-messages.js +9 -28
- package/modules/module.js +0 -27
- package/modules/requester/rate-limit/policy.js +3 -3
- package/modules/requester/requester.js +0 -24
- package/package.json +5 -5
- package/types/integrations/index.d.ts +4 -20
- package/types/module-plugin/index.d.ts +1 -5
- package/integrations/repositories/message-item-shared.js +0 -4
- package/integrations/tests/doubles/prisma-message-row.js +0 -24
- package/integrations/use-cases/record-rate-limit-message.js +0 -110
package/CLAUDE.md
CHANGED
|
@@ -487,8 +487,7 @@ handlers (`{ req, res, next }`) and `this.on` events do not.
|
|
|
487
487
|
`isLastAttempt` is `false` when either count is unknown: a local or non-SQS
|
|
488
488
|
invocation, or a queue whose redrive policy the stack does not own
|
|
489
489
|
(`ownership.queue: 'external'`). The value is information only: core still
|
|
490
|
-
rethrows retryable errors and discards halt errors (4xx except 408
|
|
491
|
-
errors with `isRateLimited`).
|
|
490
|
+
rethrows retryable errors and discards halt errors (4xx except 408 and 429).
|
|
492
491
|
|
|
493
492
|
Use it to end a run or count lost work on the final try: when a retryable
|
|
494
493
|
error (429, 5xx, network) is about to be rethrown and `isLastAttempt` is
|
|
@@ -533,51 +532,6 @@ if (!userId) {
|
|
|
533
532
|
}
|
|
534
533
|
```
|
|
535
534
|
|
|
536
|
-
**Rate-limit message** (ADR-049): when a wait is longer than the in-process cap
|
|
537
|
-
of the caller (`maxInProcessWaitMs`: 10 s HTTP, 60 s queue worker, 5 min outside
|
|
538
|
-
an invocation), the Requester tells its delegate
|
|
539
|
-
(`RATE_LIMITED`, with the `RateLimitError`) and then throws. A shorter wait
|
|
540
|
-
that throws sends no notification. The notification takes the path of
|
|
541
|
-
`CREDENTIAL_INVALIDATED`: Requester, `Module`, `IntegrationBase`.
|
|
542
|
-
|
|
543
|
-
- The `Module` sends `{ moduleName, reason, retryAt, policy, statusCode, links }`.
|
|
544
|
-
`links` is `static rateLimit.userHints[reason].links`, or `[]`. The message,
|
|
545
|
-
url, body and headers of the error are never sent: the message reaches end
|
|
546
|
-
users.
|
|
547
|
-
- `IntegrationBase` writes one `WARN` (`integration.<name>.rate_limited`), and
|
|
548
|
-
`RecordRateLimitMessage` appends one item to the stored `warnings`. It
|
|
549
|
-
changes no status. A rate limit is not an `ERROR`, and an `ERROR` status
|
|
550
|
-
makes the queue worker drop every webhook.
|
|
551
|
-
- A report for the same module whose reset time is within 60 s of a stored one
|
|
552
|
-
is skipped. The check reads the stored `warnings` with
|
|
553
|
-
`findIntegrationMessages`, not `this.messages`, which is per instance and
|
|
554
|
-
empty on Postgres. It is a read then a write, so workers that report at the
|
|
555
|
-
same instant can still store a few copies.
|
|
556
|
-
- Every hop is best effort. A failure is logged, and the caller still gets the
|
|
557
|
-
`RateLimitError`.
|
|
558
|
-
|
|
559
|
-
```javascript
|
|
560
|
-
{
|
|
561
|
-
title: 'Rate limit reached',
|
|
562
|
-
message: 'The hubspot API rate limit was reached and resets at 2026-09-28 14:31 UTC.',
|
|
563
|
-
timestamp: 1790000000000,
|
|
564
|
-
code: 'RATE_LIMITED',
|
|
565
|
-
module: 'hubspot',
|
|
566
|
-
reason: 'daily',
|
|
567
|
-
retryAt: '2026-09-28T14:30:15.000Z',
|
|
568
|
-
actions: [
|
|
569
|
-
{ type: 'RETRY_WHEN_READY' },
|
|
570
|
-
{ type: 'LINK', label: 'API usage limits', url: 'https://example.com/limits' },
|
|
571
|
-
],
|
|
572
|
-
}
|
|
573
|
-
```
|
|
574
|
-
|
|
575
|
-
The text is fixed. It holds the module name and the reset time in UTC, rounded
|
|
576
|
-
up to the minute, and nothing from the request.
|
|
577
|
-
|
|
578
|
-
Integration messages (the item form, `findIntegrationMessages`, the newest 50
|
|
579
|
-
per type): see "Integration Messages" in `docs/guides/INTEGRATION-PATTERNS.md`.
|
|
580
|
-
|
|
581
535
|
### 9. Logging System (`/logs`)
|
|
582
536
|
|
|
583
537
|
**Purpose**: One redacted JSON record per line to stdout (ADR-048). See `docs/guides/LOGGING.md`.
|
|
@@ -31,6 +31,7 @@ class RateLimitError extends FetchError {
|
|
|
31
31
|
super(fetchErrorArgs);
|
|
32
32
|
|
|
33
33
|
const timing = resolveTiming({ hint, waitMs, now });
|
|
34
|
+
this.statusCode ??= fetchErrorArgs.cause?.statusCode;
|
|
34
35
|
this.isRateLimited = true;
|
|
35
36
|
this.waitMs = timing.waitMs;
|
|
36
37
|
this.retryAt = timing.retryAt;
|
|
@@ -17,9 +17,6 @@ const {
|
|
|
17
17
|
const {
|
|
18
18
|
UpdateIntegrationConfig,
|
|
19
19
|
} = require('./use-cases/update-integration-config');
|
|
20
|
-
const {
|
|
21
|
-
RecordRateLimitMessage,
|
|
22
|
-
} = require('./use-cases/record-rate-limit-message');
|
|
23
20
|
const { validateExtensionBinding } = require('./extension');
|
|
24
21
|
const { getTelemetry } = require('../telemetry/telemetry-runtime');
|
|
25
22
|
const { instrumentHandler } = require('../telemetry/instrument-handler');
|
|
@@ -62,9 +59,6 @@ class IntegrationBase {
|
|
|
62
59
|
updateIntegrationConfig = new UpdateIntegrationConfig({
|
|
63
60
|
integrationRepository: this.integrationRepository,
|
|
64
61
|
});
|
|
65
|
-
recordRateLimitMessage = new RecordRateLimitMessage({
|
|
66
|
-
integrationRepository: this.integrationRepository,
|
|
67
|
-
});
|
|
68
62
|
|
|
69
63
|
// this.telemetry.count('records.synced', n, { entity: 'contact' })
|
|
70
64
|
telemetry = bindTelemetryContext(getTelemetry(), () =>
|
|
@@ -914,16 +908,6 @@ class IntegrationBase {
|
|
|
914
908
|
});
|
|
915
909
|
await this.persistStatus('ENABLED');
|
|
916
910
|
}
|
|
917
|
-
|
|
918
|
-
if (delegateString === 'RATE_LIMITED') {
|
|
919
|
-
this.logger.warn('Module reported a rate limit', {
|
|
920
|
-
eventName: `${this.logger.name}.rate_limited`,
|
|
921
|
-
moduleName: object?.moduleName,
|
|
922
|
-
reason: object?.reason,
|
|
923
|
-
retryAt: object?.retryAt,
|
|
924
|
-
});
|
|
925
|
-
await this.recordRateLimitMessage.execute(this.id, object);
|
|
926
|
-
}
|
|
927
911
|
}
|
|
928
912
|
|
|
929
913
|
/**
|
|
@@ -14,7 +14,6 @@ const {
|
|
|
14
14
|
IntegrationRepositoryInterface,
|
|
15
15
|
} = require('./integration-repository-interface');
|
|
16
16
|
const { validateConfigPatch } = require('./config-patch-shared');
|
|
17
|
-
const { messagesOfType } = require('./message-item-shared');
|
|
18
17
|
|
|
19
18
|
class IntegrationRepositoryDocumentDB extends IntegrationRepositoryInterface {
|
|
20
19
|
constructor() {
|
|
@@ -95,8 +94,9 @@ class IntegrationRepositoryDocumentDB extends IntegrationRepositoryInterface {
|
|
|
95
94
|
async updateIntegrationMessages(
|
|
96
95
|
integrationId,
|
|
97
96
|
messageType,
|
|
98
|
-
|
|
99
|
-
|
|
97
|
+
messageTitle,
|
|
98
|
+
messageBody,
|
|
99
|
+
messageTimestamp
|
|
100
100
|
) {
|
|
101
101
|
const objectId = toObjectId(integrationId);
|
|
102
102
|
if (!objectId) {
|
|
@@ -107,12 +107,13 @@ class IntegrationRepositoryDocumentDB extends IntegrationRepositoryInterface {
|
|
|
107
107
|
throw new Error(`Integration ${integrationId} not found`);
|
|
108
108
|
}
|
|
109
109
|
const messages = this._extractMessages(existing);
|
|
110
|
-
const list = [...
|
|
111
|
-
list.push({
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
};
|
|
110
|
+
const list = Array.isArray(messages[messageType]) ? [...messages[messageType]] : [];
|
|
111
|
+
list.push({
|
|
112
|
+
title: messageTitle ?? null,
|
|
113
|
+
message: messageBody,
|
|
114
|
+
timestamp: messageTimestamp,
|
|
115
|
+
});
|
|
116
|
+
const updatedMessages = { ...messages, [messageType]: list };
|
|
116
117
|
await updateOne(
|
|
117
118
|
this.prisma,
|
|
118
119
|
'Integration',
|
|
@@ -131,20 +132,6 @@ class IntegrationRepositoryDocumentDB extends IntegrationRepositoryInterface {
|
|
|
131
132
|
return true;
|
|
132
133
|
}
|
|
133
134
|
|
|
134
|
-
async findIntegrationMessages(integrationId, messageType) {
|
|
135
|
-
const objectId = toObjectId(integrationId);
|
|
136
|
-
if (!objectId) {
|
|
137
|
-
throw new Error(`Integration ${integrationId} not found`);
|
|
138
|
-
}
|
|
139
|
-
const existing = await findOne(this.prisma, 'Integration', {
|
|
140
|
-
_id: objectId,
|
|
141
|
-
});
|
|
142
|
-
if (!existing) {
|
|
143
|
-
throw new Error(`Integration ${integrationId} not found`);
|
|
144
|
-
}
|
|
145
|
-
return messagesOfType(this._extractMessages(existing), messageType);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
135
|
async createIntegration(entities, userId, config) {
|
|
149
136
|
const now = new Date();
|
|
150
137
|
const document = {
|
|
@@ -97,40 +97,26 @@ class IntegrationRepositoryInterface {
|
|
|
97
97
|
}
|
|
98
98
|
|
|
99
99
|
/**
|
|
100
|
-
*
|
|
100
|
+
* Update integration messages
|
|
101
101
|
*
|
|
102
102
|
* @param {string|number} integrationId - Integration ID
|
|
103
103
|
* @param {string} messageType - Type of message (errors, warnings, info, logs)
|
|
104
|
-
* @param {
|
|
105
|
-
* @param {
|
|
106
|
-
* @param {
|
|
104
|
+
* @param {string} messageTitle - Message title
|
|
105
|
+
* @param {string} messageBody - Message body
|
|
106
|
+
* @param {Date} messageTimestamp - Message timestamp
|
|
107
107
|
* @returns {Promise<boolean>} Success indicator
|
|
108
108
|
* @abstract
|
|
109
109
|
*/
|
|
110
110
|
async updateIntegrationMessages(
|
|
111
111
|
integrationId,
|
|
112
112
|
messageType,
|
|
113
|
-
|
|
114
|
-
|
|
113
|
+
messageTitle,
|
|
114
|
+
messageBody,
|
|
115
|
+
messageTimestamp
|
|
115
116
|
) {
|
|
116
117
|
throw new Error('Method updateIntegrationMessages must be implemented by subclass');
|
|
117
118
|
}
|
|
118
119
|
|
|
119
|
-
/**
|
|
120
|
-
* Find the stored messages of one type. Reads the stored items, not the
|
|
121
|
-
* `messages` of an integration record, which some adapters do not fill.
|
|
122
|
-
*
|
|
123
|
-
* @param {string|number} integrationId - Integration ID
|
|
124
|
-
* @param {string} messageType - Type of message (errors, warnings, info, logs)
|
|
125
|
-
* @returns {Promise<Array>} The stored message items of that type, oldest first
|
|
126
|
-
* @abstract
|
|
127
|
-
*/
|
|
128
|
-
async findIntegrationMessages(integrationId, messageType) {
|
|
129
|
-
throw new Error(
|
|
130
|
-
'Method findIntegrationMessages must be implemented by subclass'
|
|
131
|
-
);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
120
|
/**
|
|
135
121
|
* Create a new integration
|
|
136
122
|
*
|
|
@@ -3,7 +3,6 @@ const {
|
|
|
3
3
|
IntegrationRepositoryInterface,
|
|
4
4
|
} = require('./integration-repository-interface');
|
|
5
5
|
const { validateConfigPatch } = require('./config-patch-shared');
|
|
6
|
-
const { messagesOfType } = require('./message-item-shared');
|
|
7
6
|
|
|
8
7
|
/**
|
|
9
8
|
* MongoDB Integration Repository Adapter
|
|
@@ -230,16 +229,17 @@ class IntegrationRepositoryMongo extends IntegrationRepositoryInterface {
|
|
|
230
229
|
*
|
|
231
230
|
* @param {string} integrationId - Integration ID
|
|
232
231
|
* @param {string} messageType - Type of message (errors, warnings, info, logs)
|
|
233
|
-
* @param {
|
|
234
|
-
* @param {
|
|
235
|
-
* @param {
|
|
232
|
+
* @param {string} messageTitle - Message title
|
|
233
|
+
* @param {string} messageBody - Message body
|
|
234
|
+
* @param {Date} messageTimestamp - Message timestamp
|
|
236
235
|
* @returns {Promise<boolean>} Success indicator
|
|
237
236
|
*/
|
|
238
237
|
async updateIntegrationMessages(
|
|
239
238
|
integrationId,
|
|
240
239
|
messageType,
|
|
241
|
-
|
|
242
|
-
|
|
240
|
+
messageTitle,
|
|
241
|
+
messageBody,
|
|
242
|
+
messageTimestamp
|
|
243
243
|
) {
|
|
244
244
|
// Get current integration
|
|
245
245
|
const integration = await this.prisma.integration.findUnique({
|
|
@@ -251,42 +251,29 @@ class IntegrationRepositoryMongo extends IntegrationRepositoryInterface {
|
|
|
251
251
|
}
|
|
252
252
|
|
|
253
253
|
// Parse existing messages (JSON field)
|
|
254
|
-
const
|
|
254
|
+
const messages = integration.messages || {};
|
|
255
|
+
const messageArray = Array.isArray(messages[messageType])
|
|
256
|
+
? messages[messageType]
|
|
257
|
+
: [];
|
|
255
258
|
|
|
256
259
|
// Add new message
|
|
257
|
-
messageArray.push(
|
|
260
|
+
messageArray.push({
|
|
261
|
+
title: messageTitle,
|
|
262
|
+
message: messageBody,
|
|
263
|
+
timestamp: messageTimestamp,
|
|
264
|
+
});
|
|
258
265
|
|
|
259
266
|
// Update messages
|
|
260
267
|
await this.prisma.integration.update({
|
|
261
268
|
where: { id: integrationId },
|
|
262
269
|
data: {
|
|
263
|
-
[messageType]: messageArray
|
|
270
|
+
[messageType]: messageArray,
|
|
264
271
|
},
|
|
265
272
|
});
|
|
266
273
|
|
|
267
274
|
return true; // Mongoose compatibility
|
|
268
275
|
}
|
|
269
276
|
|
|
270
|
-
/**
|
|
271
|
-
* Find the stored messages of one type
|
|
272
|
-
*
|
|
273
|
-
* @param {string} integrationId - Integration ID
|
|
274
|
-
* @param {string} messageType - Type of message (errors, warnings, info, logs)
|
|
275
|
-
* @returns {Promise<Array>} The stored message items of that type, oldest first
|
|
276
|
-
*/
|
|
277
|
-
async findIntegrationMessages(integrationId, messageType) {
|
|
278
|
-
const integration = await this.prisma.integration.findUnique({
|
|
279
|
-
where: { id: integrationId },
|
|
280
|
-
select: { [messageType]: true },
|
|
281
|
-
});
|
|
282
|
-
|
|
283
|
-
if (!integration) {
|
|
284
|
-
throw new Error(`Integration ${integrationId} not found`);
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
return messagesOfType(integration, messageType);
|
|
288
|
-
}
|
|
289
|
-
|
|
290
277
|
/**
|
|
291
278
|
* Find all integrations whose entity set includes the given entity ID.
|
|
292
279
|
*
|
|
@@ -3,7 +3,6 @@ const {
|
|
|
3
3
|
IntegrationRepositoryInterface,
|
|
4
4
|
} = require('./integration-repository-interface');
|
|
5
5
|
const { validateConfigPatch } = require('./config-patch-shared');
|
|
6
|
-
const { messagesOfType } = require('./message-item-shared');
|
|
7
6
|
const { strictIntId } = require('./report-id');
|
|
8
7
|
|
|
9
8
|
/**
|
|
@@ -274,16 +273,17 @@ class IntegrationRepositoryPostgres extends IntegrationRepositoryInterface {
|
|
|
274
273
|
*
|
|
275
274
|
* @param {string} integrationId - Integration ID (string from application layer)
|
|
276
275
|
* @param {string} messageType - Type of message (errors, warnings, info, logs)
|
|
277
|
-
* @param {
|
|
278
|
-
* @param {
|
|
279
|
-
* @param {
|
|
276
|
+
* @param {string} messageTitle - Message title
|
|
277
|
+
* @param {string} messageBody - Message body
|
|
278
|
+
* @param {Date} messageTimestamp - Message timestamp
|
|
280
279
|
* @returns {Promise<boolean>} Success indicator
|
|
281
280
|
*/
|
|
282
281
|
async updateIntegrationMessages(
|
|
283
282
|
integrationId,
|
|
284
283
|
messageType,
|
|
285
|
-
|
|
286
|
-
|
|
284
|
+
messageTitle,
|
|
285
|
+
messageBody,
|
|
286
|
+
messageTimestamp
|
|
287
287
|
) {
|
|
288
288
|
const intId = this._convertId(integrationId);
|
|
289
289
|
|
|
@@ -297,43 +297,29 @@ class IntegrationRepositoryPostgres extends IntegrationRepositoryInterface {
|
|
|
297
297
|
}
|
|
298
298
|
|
|
299
299
|
// Parse existing messages (JSON field)
|
|
300
|
-
const
|
|
300
|
+
const messages = integration.messages || {};
|
|
301
|
+
const messageArray = Array.isArray(messages[messageType])
|
|
302
|
+
? messages[messageType]
|
|
303
|
+
: [];
|
|
301
304
|
|
|
302
305
|
// Add new message
|
|
303
|
-
messageArray.push(
|
|
306
|
+
messageArray.push({
|
|
307
|
+
title: messageTitle,
|
|
308
|
+
message: messageBody,
|
|
309
|
+
timestamp: messageTimestamp,
|
|
310
|
+
});
|
|
304
311
|
|
|
305
312
|
// Update messages
|
|
306
313
|
await this.prisma.integration.update({
|
|
307
314
|
where: { id: intId },
|
|
308
315
|
data: {
|
|
309
|
-
[messageType]: messageArray
|
|
316
|
+
[messageType]: messageArray,
|
|
310
317
|
},
|
|
311
318
|
});
|
|
312
319
|
|
|
313
320
|
return true; // Mongoose compatibility
|
|
314
321
|
}
|
|
315
322
|
|
|
316
|
-
/**
|
|
317
|
-
* Find the stored messages of one type
|
|
318
|
-
*
|
|
319
|
-
* @param {string} integrationId - Integration ID (string from application layer)
|
|
320
|
-
* @param {string} messageType - Type of message (errors, warnings, info, logs)
|
|
321
|
-
* @returns {Promise<Array>} The stored message items of that type, oldest first
|
|
322
|
-
*/
|
|
323
|
-
async findIntegrationMessages(integrationId, messageType) {
|
|
324
|
-
const intId = this._convertId(integrationId);
|
|
325
|
-
const integration = await this.prisma.integration.findUnique({
|
|
326
|
-
where: { id: intId },
|
|
327
|
-
select: { [messageType]: true },
|
|
328
|
-
});
|
|
329
|
-
|
|
330
|
-
if (!integration) {
|
|
331
|
-
throw new Error(`Integration ${integrationId} not found`);
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
return messagesOfType(integration, messageType);
|
|
335
|
-
}
|
|
336
|
-
|
|
337
323
|
/**
|
|
338
324
|
* Create a new integration
|
|
339
325
|
*
|
|
@@ -72,31 +72,18 @@ class TestIntegrationRepository {
|
|
|
72
72
|
return results;
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
-
async updateIntegrationMessages(id, type,
|
|
75
|
+
async updateIntegrationMessages(id, type, title, body, timestamp) {
|
|
76
76
|
const rec = this.store.get(id);
|
|
77
77
|
if (!rec) {
|
|
78
78
|
this.operationHistory.push({ operation: 'updateMessages', id, success: false });
|
|
79
79
|
return false;
|
|
80
80
|
}
|
|
81
81
|
if (!rec.messages[type]) rec.messages[type] = [];
|
|
82
|
-
rec.messages[type].push(
|
|
83
|
-
rec.messages[type] = rec.messages[type].slice(-keepLast);
|
|
82
|
+
rec.messages[type].push({ title, message: body, timestamp });
|
|
84
83
|
this.operationHistory.push({ operation: 'updateMessages', id, type, success: true });
|
|
85
84
|
return true;
|
|
86
85
|
}
|
|
87
86
|
|
|
88
|
-
async findIntegrationMessages(id, type) {
|
|
89
|
-
const rec = this.store.get(id);
|
|
90
|
-
this.operationHistory.push({
|
|
91
|
-
operation: 'findMessages',
|
|
92
|
-
id,
|
|
93
|
-
type,
|
|
94
|
-
found: !!rec,
|
|
95
|
-
});
|
|
96
|
-
if (!rec) throw new Error(`Integration ${id} not found`);
|
|
97
|
-
return [...(rec.messages[type] ?? [])];
|
|
98
|
-
}
|
|
99
|
-
|
|
100
87
|
async updateIntegrationConfig(id, config) {
|
|
101
88
|
if (config === null || config === undefined) {
|
|
102
89
|
throw new Error('Config parameter is required');
|
|
@@ -1,17 +1,3 @@
|
|
|
1
|
-
const MAX_MESSAGES_PER_TYPE = 50;
|
|
2
|
-
|
|
3
|
-
const isPlainObject = (value) =>
|
|
4
|
-
value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
5
|
-
|
|
6
|
-
function toMessageItem(titleOrItem, messageBody, messageTimestamp) {
|
|
7
|
-
if (isPlainObject(titleOrItem)) return { ...titleOrItem };
|
|
8
|
-
return {
|
|
9
|
-
title: titleOrItem,
|
|
10
|
-
message: messageBody,
|
|
11
|
-
timestamp: messageTimestamp,
|
|
12
|
-
};
|
|
13
|
-
}
|
|
14
|
-
|
|
15
1
|
/**
|
|
16
2
|
* Use case for updating messages associated with an integration.
|
|
17
3
|
* @class UpdateIntegrationMessages
|
|
@@ -27,21 +13,19 @@ class UpdateIntegrationMessages {
|
|
|
27
13
|
}
|
|
28
14
|
|
|
29
15
|
/**
|
|
30
|
-
* Executes the integration messages update.
|
|
31
|
-
* (title, body, timestamp) or one message item object; the keys of an item
|
|
32
|
-
* object are stored as they are.
|
|
16
|
+
* Executes the integration messages update.
|
|
33
17
|
* @async
|
|
34
18
|
* @param {string} integrationId - ID of the integration to update.
|
|
35
19
|
* @param {string} messageType - Type of message: 'errors', 'warnings', 'info', or 'logs'.
|
|
36
|
-
* @param {string
|
|
37
|
-
* @param {string}
|
|
38
|
-
* @param {string}
|
|
20
|
+
* @param {string} messageTitle - Title of the message.
|
|
21
|
+
* @param {string} messageBody - Body content of the message.
|
|
22
|
+
* @param {string} messageTimestamp - Timestamp when the message was created.
|
|
39
23
|
* @returns {Promise<Object>} The updated integration record.
|
|
40
24
|
*/
|
|
41
25
|
async execute(
|
|
42
26
|
integrationId,
|
|
43
27
|
messageType,
|
|
44
|
-
|
|
28
|
+
messageTitle,
|
|
45
29
|
messageBody,
|
|
46
30
|
messageTimestamp
|
|
47
31
|
) {
|
|
@@ -49,15 +33,12 @@ class UpdateIntegrationMessages {
|
|
|
49
33
|
await this.integrationRepository.updateIntegrationMessages(
|
|
50
34
|
integrationId,
|
|
51
35
|
messageType,
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
messageTimestamp
|
|
56
|
-
),
|
|
57
|
-
{ keepLast: MAX_MESSAGES_PER_TYPE }
|
|
36
|
+
messageTitle,
|
|
37
|
+
messageBody,
|
|
38
|
+
messageTimestamp
|
|
58
39
|
);
|
|
59
40
|
return integration;
|
|
60
41
|
}
|
|
61
42
|
}
|
|
62
43
|
|
|
63
|
-
module.exports = {
|
|
44
|
+
module.exports = { UpdateIntegrationMessages };
|
package/modules/module.js
CHANGED
|
@@ -8,7 +8,6 @@ const {
|
|
|
8
8
|
const {
|
|
9
9
|
createModuleRepository,
|
|
10
10
|
} = require('./repositories/module-repository-factory');
|
|
11
|
-
const { readRateLimitPolicy } = require('./requester/rate-limit');
|
|
12
11
|
|
|
13
12
|
// todo: this class should be a Domain class, and the Delegate function is preventing us from
|
|
14
13
|
// doing that, we probably have to get rid of the Delegate class as well as the event based
|
|
@@ -52,8 +51,6 @@ class Module extends Delegate {
|
|
|
52
51
|
this.delegateTypes.push(this.DLGT_CREDENTIAL_INVALIDATED);
|
|
53
52
|
this.DLGT_CREDENTIAL_VALIDATED = 'CREDENTIAL_VALIDATED';
|
|
54
53
|
this.delegateTypes.push(this.DLGT_CREDENTIAL_VALIDATED);
|
|
55
|
-
this.DLGT_RATE_LIMITED = 'RATE_LIMITED';
|
|
56
|
-
this.delegateTypes.push(this.DLGT_RATE_LIMITED);
|
|
57
54
|
|
|
58
55
|
Object.assign(this, this.definition.requiredAuthMethods);
|
|
59
56
|
|
|
@@ -163,8 +160,6 @@ class Module extends Delegate {
|
|
|
163
160
|
await this.deauthorize();
|
|
164
161
|
} else if (delegateString === this.api.DLGT_INVALID_AUTH) {
|
|
165
162
|
await this.markCredentialsInvalid(object);
|
|
166
|
-
} else if (delegateString === this.api.DLGT_RATE_LIMITED) {
|
|
167
|
-
await this.reportRateLimit(object);
|
|
168
163
|
} else if (delegateString === this.api.DLGT_CREDENTIAL_RELOAD) {
|
|
169
164
|
return this.reloadCredential();
|
|
170
165
|
}
|
|
@@ -239,28 +234,6 @@ class Module extends Delegate {
|
|
|
239
234
|
}
|
|
240
235
|
}
|
|
241
236
|
|
|
242
|
-
async reportRateLimit(rateLimitError) {
|
|
243
|
-
const { reason, retryAt, policy, statusCode } = rateLimitError;
|
|
244
|
-
const links =
|
|
245
|
-
readRateLimitPolicy(this.apiClass)?.userHints?.[reason]?.links ??
|
|
246
|
-
[];
|
|
247
|
-
try {
|
|
248
|
-
await this.notify(this.DLGT_RATE_LIMITED, {
|
|
249
|
-
moduleName: this.name,
|
|
250
|
-
reason,
|
|
251
|
-
retryAt,
|
|
252
|
-
policy,
|
|
253
|
-
statusCode,
|
|
254
|
-
links,
|
|
255
|
-
});
|
|
256
|
-
} catch (err) {
|
|
257
|
-
this.logger.warn('Failed to propagate RATE_LIMITED', {
|
|
258
|
-
eventName: `${this.logger.name}.rate_limit_propagation_failed`,
|
|
259
|
-
error: err,
|
|
260
|
-
});
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
|
|
264
237
|
async deauthorize() {
|
|
265
238
|
//todo: Check if this is correct, we're instantiating a new api without params (credentials, tokens, etc...)
|
|
266
239
|
this.api = new this.apiClass();
|
|
@@ -110,9 +110,9 @@ function computeScopeKey(policy, requester) {
|
|
|
110
110
|
if (typeof scope === 'function') {
|
|
111
111
|
try {
|
|
112
112
|
const value = scope(requester);
|
|
113
|
-
return value ===
|
|
114
|
-
?
|
|
115
|
-
:
|
|
113
|
+
return typeof value === 'string' || Number.isFinite(value)
|
|
114
|
+
? `${label}:custom:${value}`
|
|
115
|
+
: undefined;
|
|
116
116
|
} catch {
|
|
117
117
|
return undefined;
|
|
118
118
|
}
|
|
@@ -61,8 +61,6 @@ class Requester extends Delegate {
|
|
|
61
61
|
this._authGeneration = 0;
|
|
62
62
|
this.DLGT_INVALID_AUTH = 'INVALID_AUTH';
|
|
63
63
|
this.delegateTypes.push(this.DLGT_INVALID_AUTH);
|
|
64
|
-
this.DLGT_RATE_LIMITED = 'RATE_LIMITED';
|
|
65
|
-
this.delegateTypes.push(this.DLGT_RATE_LIMITED);
|
|
66
64
|
this.agent = get(params, 'agent', null);
|
|
67
65
|
|
|
68
66
|
// Per-attempt HTTP timeout. Without this the framework called fetch()
|
|
@@ -534,7 +532,6 @@ class Requester extends Delegate {
|
|
|
534
532
|
module: this._telemetryModuleLabel(),
|
|
535
533
|
scopeKey: computeScopeKey(this._rateLimitPolicy, this),
|
|
536
534
|
});
|
|
537
|
-
await this._notifyRateLimited(rateLimitError);
|
|
538
535
|
throw this._maybeFlagTimeoutDuringBodyRead(rateLimitError, timeoutMs);
|
|
539
536
|
}
|
|
540
537
|
|
|
@@ -602,27 +599,6 @@ class Requester extends Delegate {
|
|
|
602
599
|
});
|
|
603
600
|
}
|
|
604
601
|
|
|
605
|
-
async _notifyRateLimited(rateLimitError) {
|
|
606
|
-
if (!this._waitsLongerThanInProcessCap(rateLimitError)) return;
|
|
607
|
-
try {
|
|
608
|
-
await this.notify(this.DLGT_RATE_LIMITED, rateLimitError);
|
|
609
|
-
} catch (error) {
|
|
610
|
-
const logger = this.logger;
|
|
611
|
-
logger.warn('Rate limit notification failed', {
|
|
612
|
-
eventName: `${logger.name}.rate_limit_notify_failed`,
|
|
613
|
-
statusCode: rateLimitError.statusCode,
|
|
614
|
-
error,
|
|
615
|
-
});
|
|
616
|
-
}
|
|
617
|
-
}
|
|
618
|
-
|
|
619
|
-
_waitsLongerThanInProcessCap(rateLimitError) {
|
|
620
|
-
const waitMs = Number.isFinite(rateLimitError?.waitMs)
|
|
621
|
-
? rateLimitError.waitMs
|
|
622
|
-
: new Date(rateLimitError?.retryAt).getTime() - Date.now();
|
|
623
|
-
return waitMs > invocationMaxInProcessWaitMs();
|
|
624
|
-
}
|
|
625
|
-
|
|
626
602
|
_logRequestFailed(encodedUrl, options, status) {
|
|
627
603
|
const logger = this.logger;
|
|
628
604
|
if (!logger.isLevelEnabled('DEBUG')) return;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@friggframework/core",
|
|
3
3
|
"prettier": "@friggframework/prettier-config",
|
|
4
|
-
"version": "2.0.0--canary.
|
|
4
|
+
"version": "2.0.0--canary.656.602fc50.0",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"@aws-sdk/client-apigatewaymanagementapi": "^3.588.0",
|
|
7
7
|
"@aws-sdk/client-kms": "^3.588.0",
|
|
@@ -47,9 +47,9 @@
|
|
|
47
47
|
}
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
|
-
"@friggframework/eslint-config": "2.0.0--canary.
|
|
51
|
-
"@friggframework/prettier-config": "2.0.0--canary.
|
|
52
|
-
"@friggframework/test": "2.0.0--canary.
|
|
50
|
+
"@friggframework/eslint-config": "2.0.0--canary.656.602fc50.0",
|
|
51
|
+
"@friggframework/prettier-config": "2.0.0--canary.656.602fc50.0",
|
|
52
|
+
"@friggframework/test": "2.0.0--canary.656.602fc50.0",
|
|
53
53
|
"@prisma/client": "^6.19.3",
|
|
54
54
|
"@types/lodash": "4.17.15",
|
|
55
55
|
"@typescript-eslint/eslint-plugin": "^8.0.0",
|
|
@@ -89,5 +89,5 @@
|
|
|
89
89
|
"publishConfig": {
|
|
90
90
|
"access": "public"
|
|
91
91
|
},
|
|
92
|
-
"gitHead": "
|
|
92
|
+
"gitHead": "602fc501b2b433a6e220d651c369939ad0ec6245"
|
|
93
93
|
}
|
|
@@ -1,22 +1,6 @@
|
|
|
1
1
|
declare module "@friggframework/integrations" {
|
|
2
2
|
import { Delegate, IFriggDelegate } from "@friggframework/core";
|
|
3
3
|
|
|
4
|
-
export type IntegrationMessageAction =
|
|
5
|
-
| { type: "RETRY_WHEN_READY" }
|
|
6
|
-
| { type: "LINK"; label: string; url: string };
|
|
7
|
-
|
|
8
|
-
export interface IntegrationMessage {
|
|
9
|
-
title: string;
|
|
10
|
-
message: string;
|
|
11
|
-
timestamp: number;
|
|
12
|
-
code?: string;
|
|
13
|
-
module?: string;
|
|
14
|
-
reason?: string;
|
|
15
|
-
retryAt?: string;
|
|
16
|
-
actions?: IntegrationMessageAction[];
|
|
17
|
-
[key: string]: unknown;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
4
|
export interface Integration {
|
|
21
5
|
entities: any[];
|
|
22
6
|
userId: string;
|
|
@@ -24,10 +8,10 @@ declare module "@friggframework/integrations" {
|
|
|
24
8
|
config: any;
|
|
25
9
|
version: string;
|
|
26
10
|
messages: {
|
|
27
|
-
errors:
|
|
28
|
-
warnings:
|
|
29
|
-
info:
|
|
30
|
-
logs:
|
|
11
|
+
errors: [];
|
|
12
|
+
warnings: [];
|
|
13
|
+
info: [];
|
|
14
|
+
logs: [];
|
|
31
15
|
};
|
|
32
16
|
}
|
|
33
17
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
declare module "@friggframework/module-plugin" {
|
|
2
2
|
import { Delegate, IFriggDelegate } from "@friggframework/core";
|
|
3
|
-
import type {
|
|
3
|
+
import type { RateLimitHint } from "@friggframework/errors";
|
|
4
4
|
|
|
5
5
|
export interface Credential {
|
|
6
6
|
id?: string;
|
|
@@ -65,7 +65,6 @@ declare module "@friggframework/module-plugin" {
|
|
|
65
65
|
static requestTimeoutMs?: number;
|
|
66
66
|
|
|
67
67
|
DLGT_INVALID_AUTH: string;
|
|
68
|
-
DLGT_RATE_LIMITED: string;
|
|
69
68
|
requestTimeoutMs: number;
|
|
70
69
|
backOff: number[];
|
|
71
70
|
fetch: any;
|
|
@@ -85,7 +84,6 @@ declare module "@friggframework/module-plugin" {
|
|
|
85
84
|
parseBody(response: any): Promise<any>;
|
|
86
85
|
refreshAuth(): Promise<any>;
|
|
87
86
|
_adoptNewerCredential(): Promise<boolean>;
|
|
88
|
-
_notifyRateLimited(rateLimitError: RateLimitError): Promise<void>;
|
|
89
87
|
|
|
90
88
|
delegate: any;
|
|
91
89
|
delegateTypes: any[];
|
|
@@ -103,7 +101,6 @@ declare module "@friggframework/module-plugin" {
|
|
|
103
101
|
isRefreshable: boolean;
|
|
104
102
|
refreshCount: number;
|
|
105
103
|
DLGT_INVALID_AUTH: string;
|
|
106
|
-
DLGT_RATE_LIMITED: string;
|
|
107
104
|
fetch: any;
|
|
108
105
|
|
|
109
106
|
parseBody(response: any): Promise<any>;
|
|
@@ -119,7 +116,6 @@ declare module "@friggframework/module-plugin" {
|
|
|
119
116
|
_delete(options: RequestOptions): Promise<any>;
|
|
120
117
|
refreshAuth(): Promise<any>;
|
|
121
118
|
_adoptNewerCredential(): Promise<boolean>;
|
|
122
|
-
_notifyRateLimited(rateLimitError: RateLimitError): Promise<void>;
|
|
123
119
|
}
|
|
124
120
|
|
|
125
121
|
type RequestOptions = {
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
const copy = (value) => JSON.parse(JSON.stringify(value));
|
|
2
|
-
|
|
3
|
-
function withMessageRow(repo, id, stored = {}) {
|
|
4
|
-
const row = {
|
|
5
|
-
id,
|
|
6
|
-
errors: [],
|
|
7
|
-
warnings: [],
|
|
8
|
-
info: [],
|
|
9
|
-
logs: [],
|
|
10
|
-
...stored,
|
|
11
|
-
};
|
|
12
|
-
repo.prisma = {
|
|
13
|
-
integration: {
|
|
14
|
-
findUnique: jest.fn(async () => copy(row)),
|
|
15
|
-
update: jest.fn(async ({ data }) => {
|
|
16
|
-
Object.assign(row, copy(data));
|
|
17
|
-
return copy(row);
|
|
18
|
-
}),
|
|
19
|
-
},
|
|
20
|
-
};
|
|
21
|
-
return { repo, row };
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
module.exports = { withMessageRow };
|
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
const { getLogger } = require('../../logs');
|
|
2
|
-
const { MAX_MESSAGES_PER_TYPE } = require('./update-integration-messages');
|
|
3
|
-
|
|
4
|
-
const log = getLogger('frigg.integrations');
|
|
5
|
-
|
|
6
|
-
const RATE_LIMITED_MESSAGE_CODE = 'RATE_LIMITED';
|
|
7
|
-
const SAME_LIMIT_WINDOW_MS = 60_000;
|
|
8
|
-
const MINUTE_MS = 60_000;
|
|
9
|
-
|
|
10
|
-
const isNonEmptyString = (value) => typeof value === 'string' && value !== '';
|
|
11
|
-
|
|
12
|
-
const isValidTime = (value) =>
|
|
13
|
-
value !== null &&
|
|
14
|
-
value !== undefined &&
|
|
15
|
-
!Number.isNaN(new Date(value).getTime());
|
|
16
|
-
|
|
17
|
-
const isLink = (link) =>
|
|
18
|
-
typeof link?.label === 'string' && typeof link?.url === 'string';
|
|
19
|
-
|
|
20
|
-
const linkActions = (links) =>
|
|
21
|
-
(Array.isArray(links) ? links : [])
|
|
22
|
-
.filter(isLink)
|
|
23
|
-
.map(({ label, url }) => ({ type: 'LINK', label, url }));
|
|
24
|
-
|
|
25
|
-
function formatResetTime(date) {
|
|
26
|
-
const minute = new Date(Math.ceil(date.getTime() / MINUTE_MS) * MINUTE_MS);
|
|
27
|
-
return `${minute.toISOString().slice(0, 16).replace('T', ' ')} UTC`;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* @class RecordRateLimitMessage
|
|
32
|
-
*/
|
|
33
|
-
class RecordRateLimitMessage {
|
|
34
|
-
/**
|
|
35
|
-
* @param {Object} params - Configuration parameters.
|
|
36
|
-
* @param {import('../repositories/integration-repository-interface').IntegrationRepositoryInterface} params.integrationRepository - Repository for integration data operations.
|
|
37
|
-
*/
|
|
38
|
-
constructor({ integrationRepository }) {
|
|
39
|
-
this.integrationRepository = integrationRepository;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* @async
|
|
44
|
-
* @param {string} integrationId - ID of the integration to warn on.
|
|
45
|
-
* @param {Object} payload - The RATE_LIMITED payload of the Module.
|
|
46
|
-
* @param {string} payload.moduleName
|
|
47
|
-
* @param {string} payload.reason
|
|
48
|
-
* @param {Date|string|number} payload.retryAt
|
|
49
|
-
* @param {Array<{label: string, url: string}>} [payload.links]
|
|
50
|
-
*/
|
|
51
|
-
async execute(integrationId, payload) {
|
|
52
|
-
const { moduleName, reason, retryAt, links } = payload ?? {};
|
|
53
|
-
if (!isNonEmptyString(moduleName) || !isValidTime(retryAt)) return;
|
|
54
|
-
|
|
55
|
-
try {
|
|
56
|
-
await this._recordUnlessStored(integrationId, {
|
|
57
|
-
moduleName,
|
|
58
|
-
reason,
|
|
59
|
-
resetAt: new Date(retryAt),
|
|
60
|
-
links,
|
|
61
|
-
});
|
|
62
|
-
} catch (error) {
|
|
63
|
-
log.warn('Failed to record rate limit message', {
|
|
64
|
-
eventName:
|
|
65
|
-
'frigg.integrations.rate_limit_message_record_failed',
|
|
66
|
-
integrationId,
|
|
67
|
-
moduleName,
|
|
68
|
-
error,
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
async _recordUnlessStored(
|
|
74
|
-
integrationId,
|
|
75
|
-
{ moduleName, reason, resetAt, links }
|
|
76
|
-
) {
|
|
77
|
-
const warnings =
|
|
78
|
-
await this.integrationRepository.findIntegrationMessages(
|
|
79
|
-
integrationId,
|
|
80
|
-
'warnings'
|
|
81
|
-
);
|
|
82
|
-
const alreadyRecorded = warnings.some(
|
|
83
|
-
(warning) =>
|
|
84
|
-
warning?.code === RATE_LIMITED_MESSAGE_CODE &&
|
|
85
|
-
warning.module === moduleName &&
|
|
86
|
-
Math.abs(Date.parse(warning.retryAt) - resetAt.getTime()) <=
|
|
87
|
-
SAME_LIMIT_WINDOW_MS
|
|
88
|
-
);
|
|
89
|
-
if (alreadyRecorded) return;
|
|
90
|
-
|
|
91
|
-
const resetsAt = formatResetTime(resetAt);
|
|
92
|
-
await this.integrationRepository.updateIntegrationMessages(
|
|
93
|
-
integrationId,
|
|
94
|
-
'warnings',
|
|
95
|
-
{
|
|
96
|
-
title: 'Rate limit reached',
|
|
97
|
-
message: `The ${moduleName} API rate limit was reached and resets at ${resetsAt}.`,
|
|
98
|
-
timestamp: Date.now(),
|
|
99
|
-
code: RATE_LIMITED_MESSAGE_CODE,
|
|
100
|
-
module: moduleName,
|
|
101
|
-
reason,
|
|
102
|
-
retryAt: resetAt.toISOString(),
|
|
103
|
-
actions: [{ type: 'RETRY_WHEN_READY' }, ...linkActions(links)],
|
|
104
|
-
},
|
|
105
|
-
{ keepLast: MAX_MESSAGES_PER_TYPE }
|
|
106
|
-
);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
module.exports = { RecordRateLimitMessage };
|