@friggframework/core 2.0.0--canary.743.265a5cd.0 → 2.0.0--canary.744.95fdcc7.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 +0 -7
- package/core/Worker.js +5 -43
- package/index.js +0 -8
- package/modules/repositories/module-repository-documentdb.js +4 -29
- package/modules/repositories/module-repository-mongo.js +11 -55
- package/modules/repositories/module-repository-postgres.js +12 -41
- package/modules/requester/oauth-2.js +37 -0
- package/modules/requester/rate-limit/index.js +1 -3
- package/modules/requester/rate-limit/policy.js +0 -26
- package/modules/requester/requester.js +66 -180
- package/package.json +6 -6
- package/queues/queue-deferral.js +3 -78
- package/types/module-plugin/index.d.ts +4 -8
- package/modules/repositories/entity-data.js +0 -71
- package/modules/requester/rate-limit/pacer.js +0 -584
- package/modules/requester/rate-limit/state/rate-limit-state-repository-factory.js +0 -42
- package/modules/requester/rate-limit/state/rate-limit-state-repository-interface.js +0 -75
- package/modules/requester/rate-limit/state/rate-limit-state-repository-memory.js +0 -64
- package/modules/requester/rate-limit/state/rate-limit-state-repository-mongo.js +0 -168
- package/modules/requester/rate-limit/state/rate-limit-state-repository-postgres.js +0 -111
package/CLAUDE.md
CHANGED
|
@@ -949,13 +949,6 @@ Use test doubles from `@friggframework/test` package for consistent mocking.
|
|
|
949
949
|
- `FRIGG_QUEUE_MAX_DEFERRALS` - Most rate-limit deferrals of one message (default 30)
|
|
950
950
|
- `FRIGG_QUEUE_MAX_DEFERRED_MS` - Most time in ms from a message's first rate-limit deferral to its retry time (default 26 h)
|
|
951
951
|
- `FRIGG_QUEUE_RATE_LIMIT_PROBE_MS` - Time in ms until the retry of a rate-limited message whose `retryAt` is unknown (default 1 h)
|
|
952
|
-
- `FRIGG_QUEUE_DEFERRAL_JITTER_MAX_MS` - Most random spread in ms added to a rate-limit deferral (default 5 min; `0` turns it off). A wait in the 900 s SQS delay range stays in it
|
|
953
|
-
- `FRIGG_QUEUE_MAX_PACED_DEFERRALS` - Pacer deferrals (no call sent) of one message that do not count against `FRIGG_QUEUE_MAX_DEFERRALS` (default 100)
|
|
954
|
-
- `FRIGG_RATE_LIMIT_PACING` - `off` turns off pacing before a call (ADR-049 section 6) in all modules
|
|
955
|
-
- `FRIGG_RATE_LIMIT_MAX_PACING_WAIT_MS` - Longest pacing sleep in ms before the call defers instead (default 2 s)
|
|
956
|
-
- `FRIGG_RATE_LIMIT_MAX_BLOCK_MS` - Longest block of a scope key in ms (default 15 min)
|
|
957
|
-
- `FRIGG_RATE_LIMIT_SHARED_CACHE_MS` - How long in ms a worker keeps the shared block it read (default 1 s)
|
|
958
|
-
- `FRIGG_RATE_LIMIT_SHARED_TIMEOUT_MS` - Longest wait in ms for the shared rate-limit store before pacing goes on in process (default 250 ms)
|
|
959
952
|
|
|
960
953
|
## Version Information
|
|
961
954
|
|
package/core/Worker.js
CHANGED
|
@@ -14,7 +14,6 @@ const {
|
|
|
14
14
|
MAX_DELAY_SECONDS,
|
|
15
15
|
deferredMs,
|
|
16
16
|
isDeferralCapped,
|
|
17
|
-
jitteredRetryAt,
|
|
18
17
|
nextDeferral,
|
|
19
18
|
readDeferral,
|
|
20
19
|
readDeferralLimits,
|
|
@@ -168,8 +167,7 @@ class Worker {
|
|
|
168
167
|
// The RATE_LIMITED event of ADR-049 section 3. The same object is meant to
|
|
169
168
|
// become the input of the ADR-047 orchestrator's reducer.
|
|
170
169
|
_rateLimitedEvent(record, body, error, delivery) {
|
|
171
|
-
const { deferrals,
|
|
172
|
-
readDeferral(body);
|
|
170
|
+
const { deferrals, firstDeferredAt } = readDeferral(body);
|
|
173
171
|
return {
|
|
174
172
|
retryAt: validDate(error.retryAt) ?? undefined,
|
|
175
173
|
waitMs: error.waitMs,
|
|
@@ -181,7 +179,6 @@ class Worker {
|
|
|
181
179
|
messageId: record.messageId,
|
|
182
180
|
receiveCount: delivery.receiveCount,
|
|
183
181
|
deferrals,
|
|
184
|
-
pacedDeferrals: pacedDeferrals ?? 0,
|
|
185
182
|
deferredMs: firstDeferredAt
|
|
186
183
|
? Date.now() - Date.parse(firstDeferredAt)
|
|
187
184
|
: 0,
|
|
@@ -217,21 +214,6 @@ class Worker {
|
|
|
217
214
|
});
|
|
218
215
|
}
|
|
219
216
|
|
|
220
|
-
// A paced error carries the key's next slot. A message that was paced
|
|
221
|
-
// before and is paced again comes back at a random point between the
|
|
222
|
-
// earliest call time and that slot, so the messages that came in after
|
|
223
|
-
// it do not keep it at the back of the backlog.
|
|
224
|
-
_pacedRetryAt(error, slotAt, body) {
|
|
225
|
-
const notBefore = validDate(error.notBefore);
|
|
226
|
-
if (!notBefore || !(readDeferral(body).pacedDeferrals > 0)) {
|
|
227
|
-
return slotAt;
|
|
228
|
-
}
|
|
229
|
-
const span = Math.max(0, slotAt.getTime() - notBefore.getTime());
|
|
230
|
-
return new Date(
|
|
231
|
-
notBefore.getTime() + Math.floor((this._random ?? Math.random)() * span)
|
|
232
|
-
);
|
|
233
|
-
}
|
|
234
|
-
|
|
235
217
|
async defer(record, body, error, delivery) {
|
|
236
218
|
const queueName = queueNameOf(record.eventSourceARN);
|
|
237
219
|
if (!queueName || queueName.endsWith('.fifo')) {
|
|
@@ -241,24 +223,8 @@ class Worker {
|
|
|
241
223
|
const now = Date.now();
|
|
242
224
|
const limits = readDeferralLimits();
|
|
243
225
|
const knownRetryAt = validDate(error.retryAt);
|
|
244
|
-
const
|
|
245
|
-
|
|
246
|
-
// An error from a module with no rate-limit policy keeps its exact
|
|
247
|
-
// retryAt: no policy, no change (ADR-049 A.4).
|
|
248
|
-
const retryAt = error.paced
|
|
249
|
-
? this._pacedRetryAt(error, earliest, body)
|
|
250
|
-
: error.declaredPolicy !== true
|
|
251
|
-
? earliest
|
|
252
|
-
: jitteredRetryAt(
|
|
253
|
-
earliest,
|
|
254
|
-
Math.max(0, earliest.getTime() - now),
|
|
255
|
-
{ ...limits, now },
|
|
256
|
-
this._random
|
|
257
|
-
);
|
|
258
|
-
const counters = nextDeferral(body, now, record.messageId, {
|
|
259
|
-
preempted: error.preempted === true,
|
|
260
|
-
maxPacedDeferrals: limits.maxPacedDeferrals,
|
|
261
|
-
});
|
|
226
|
+
const retryAt = knownRetryAt ?? new Date(now + limits.probeMs);
|
|
227
|
+
const counters = nextDeferral(body, now, record.messageId);
|
|
262
228
|
const deferral = {
|
|
263
229
|
record,
|
|
264
230
|
queueName,
|
|
@@ -461,8 +427,8 @@ class Worker {
|
|
|
461
427
|
|
|
462
428
|
async _clearDeferredState(record, body, delivery) {
|
|
463
429
|
const redelivered = delivery.receiveCount > 1;
|
|
464
|
-
const { deferrals,
|
|
465
|
-
if (deferrals === 0 && !
|
|
430
|
+
const { deferrals, deferralId } = readDeferral(body);
|
|
431
|
+
if (deferrals === 0 && !redelivered) return;
|
|
466
432
|
await this._clearWait(body, deferralId ?? record.messageId);
|
|
467
433
|
}
|
|
468
434
|
|
|
@@ -489,11 +455,7 @@ class Worker {
|
|
|
489
455
|
waitMs,
|
|
490
456
|
retryAt: retryAt.toISOString(),
|
|
491
457
|
...(probe && { probe }),
|
|
492
|
-
...(error.preempted && { preempted: true }),
|
|
493
458
|
deferrals: counters.deferrals,
|
|
494
|
-
...(counters.pacedDeferrals && {
|
|
495
|
-
pacedDeferrals: counters.pacedDeferrals,
|
|
496
|
-
}),
|
|
497
459
|
firstDeferredAt: counters.firstDeferredAt,
|
|
498
460
|
reason: error.reason,
|
|
499
461
|
module: error.module,
|
package/index.js
CHANGED
|
@@ -109,10 +109,6 @@ const {
|
|
|
109
109
|
parseRetryAfter,
|
|
110
110
|
parseResetHeaders,
|
|
111
111
|
parseIetfRateLimit,
|
|
112
|
-
RateLimitPacer,
|
|
113
|
-
getRateLimitPacer,
|
|
114
|
-
hashRateLimitScope,
|
|
115
|
-
setRateLimitPacer,
|
|
116
112
|
} = require('./modules/requester/rate-limit');
|
|
117
113
|
const application = require('./application');
|
|
118
114
|
const utils = require('./utils');
|
|
@@ -168,10 +164,6 @@ module.exports = {
|
|
|
168
164
|
parseRetryAfter,
|
|
169
165
|
parseResetHeaders,
|
|
170
166
|
parseIetfRateLimit,
|
|
171
|
-
RateLimitPacer,
|
|
172
|
-
getRateLimitPacer,
|
|
173
|
-
hashRateLimitScope,
|
|
174
|
-
setRateLimitPacer,
|
|
175
167
|
|
|
176
168
|
// integrations
|
|
177
169
|
IntegrationBase,
|
|
@@ -1,9 +1,4 @@
|
|
|
1
1
|
const { prisma } = require('../../database/prisma');
|
|
2
|
-
const {
|
|
3
|
-
dataSetPaths,
|
|
4
|
-
entityDataFields,
|
|
5
|
-
RATE_LIMIT_STATE_FIELD,
|
|
6
|
-
} = require('./entity-data');
|
|
7
2
|
const {
|
|
8
3
|
toObjectId,
|
|
9
4
|
fromObjectId,
|
|
@@ -138,7 +133,7 @@ class ModuleRepositoryDocumentDB extends ModuleRepositoryInterface {
|
|
|
138
133
|
name: name ?? null,
|
|
139
134
|
moduleName: moduleName ?? null,
|
|
140
135
|
externalId: externalId ?? null,
|
|
141
|
-
data:
|
|
136
|
+
data: dynamicData,
|
|
142
137
|
};
|
|
143
138
|
const insertedId = await insertOne(this.prisma, 'Entity', document);
|
|
144
139
|
const created = await findOne(this.prisma, 'Entity', { _id: insertedId });
|
|
@@ -175,28 +170,8 @@ class ModuleRepositoryDocumentDB extends ModuleRepositoryInterface {
|
|
|
175
170
|
if (moduleName !== undefined) updatePayload.moduleName = moduleName;
|
|
176
171
|
if (externalId !== undefined) updatePayload.externalId = externalId;
|
|
177
172
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
// back. A key that cannot be a path segment falls back to a whole
|
|
181
|
-
// `data` write, which keeps the state as it was read.
|
|
182
|
-
const fields = entityDataFields(dynamicData);
|
|
183
|
-
if (Object.keys(fields).length > 0) {
|
|
184
|
-
const paths =
|
|
185
|
-
existing.data && typeof existing.data === 'object'
|
|
186
|
-
? dataSetPaths(fields)
|
|
187
|
-
: null;
|
|
188
|
-
if (paths) {
|
|
189
|
-
Object.assign(updatePayload, paths);
|
|
190
|
-
} else {
|
|
191
|
-
const state = existing.data?.[RATE_LIMIT_STATE_FIELD];
|
|
192
|
-
updatePayload.data = {
|
|
193
|
-
...entityDataFields(existing.data),
|
|
194
|
-
...fields,
|
|
195
|
-
...(state !== undefined && {
|
|
196
|
-
[RATE_LIMIT_STATE_FIELD]: state,
|
|
197
|
-
}),
|
|
198
|
-
};
|
|
199
|
-
}
|
|
173
|
+
if (Object.keys(dynamicData).length > 0) {
|
|
174
|
+
updatePayload.data = { ...(existing.data || {}), ...dynamicData };
|
|
200
175
|
}
|
|
201
176
|
|
|
202
177
|
await updateOne(
|
|
@@ -358,7 +333,7 @@ class ModuleRepositoryDocumentDB extends ModuleRepositoryInterface {
|
|
|
358
333
|
}
|
|
359
334
|
|
|
360
335
|
_mapEntity(doc, credential) {
|
|
361
|
-
const dynamicData =
|
|
336
|
+
const dynamicData = doc?.data || {};
|
|
362
337
|
return {
|
|
363
338
|
id: fromObjectId(doc?._id),
|
|
364
339
|
credential,
|
|
@@ -1,10 +1,4 @@
|
|
|
1
1
|
const { prisma } = require('../../database/prisma');
|
|
2
|
-
const {
|
|
3
|
-
canMergeEntityDataRaw,
|
|
4
|
-
dataSetPaths,
|
|
5
|
-
entityDataFields,
|
|
6
|
-
RATE_LIMIT_STATE_FIELD,
|
|
7
|
-
} = require('./entity-data');
|
|
8
2
|
const { ModuleRepositoryInterface } = require('./module-repository-interface');
|
|
9
3
|
|
|
10
4
|
/**
|
|
@@ -108,7 +102,7 @@ class ModuleRepositoryMongo extends ModuleRepositoryInterface {
|
|
|
108
102
|
name: entity.name,
|
|
109
103
|
externalId: entity.externalId,
|
|
110
104
|
moduleName: entity.moduleName,
|
|
111
|
-
...
|
|
105
|
+
...(entity.data || {}),
|
|
112
106
|
};
|
|
113
107
|
}
|
|
114
108
|
|
|
@@ -134,7 +128,7 @@ class ModuleRepositoryMongo extends ModuleRepositoryInterface {
|
|
|
134
128
|
name: e.name,
|
|
135
129
|
externalId: e.externalId,
|
|
136
130
|
moduleName: e.moduleName,
|
|
137
|
-
...
|
|
131
|
+
...(e.data || {}),
|
|
138
132
|
}));
|
|
139
133
|
}
|
|
140
134
|
|
|
@@ -160,7 +154,7 @@ class ModuleRepositoryMongo extends ModuleRepositoryInterface {
|
|
|
160
154
|
name: e.name,
|
|
161
155
|
externalId: e.externalId,
|
|
162
156
|
moduleName: e.moduleName,
|
|
163
|
-
...
|
|
157
|
+
...(e.data || {}),
|
|
164
158
|
}));
|
|
165
159
|
}
|
|
166
160
|
|
|
@@ -190,7 +184,7 @@ class ModuleRepositoryMongo extends ModuleRepositoryInterface {
|
|
|
190
184
|
name: e.name,
|
|
191
185
|
externalId: e.externalId,
|
|
192
186
|
moduleName: e.moduleName,
|
|
193
|
-
...
|
|
187
|
+
...(e.data || {}),
|
|
194
188
|
}));
|
|
195
189
|
}
|
|
196
190
|
|
|
@@ -236,7 +230,7 @@ class ModuleRepositoryMongo extends ModuleRepositoryInterface {
|
|
|
236
230
|
name: entity.name,
|
|
237
231
|
externalId: entity.externalId,
|
|
238
232
|
moduleName: entity.moduleName,
|
|
239
|
-
...
|
|
233
|
+
...(entity.data || {}),
|
|
240
234
|
};
|
|
241
235
|
}
|
|
242
236
|
|
|
@@ -264,7 +258,7 @@ class ModuleRepositoryMongo extends ModuleRepositoryInterface {
|
|
|
264
258
|
name: e.name,
|
|
265
259
|
externalId: e.externalId,
|
|
266
260
|
moduleName: e.moduleName,
|
|
267
|
-
...
|
|
261
|
+
...(e.data || {}),
|
|
268
262
|
}));
|
|
269
263
|
}
|
|
270
264
|
|
|
@@ -293,7 +287,7 @@ class ModuleRepositoryMongo extends ModuleRepositoryInterface {
|
|
|
293
287
|
name,
|
|
294
288
|
moduleName,
|
|
295
289
|
externalId,
|
|
296
|
-
data:
|
|
290
|
+
data: dynamicData,
|
|
297
291
|
};
|
|
298
292
|
|
|
299
293
|
const entity = await this.prisma.entity.create({
|
|
@@ -309,7 +303,7 @@ class ModuleRepositoryMongo extends ModuleRepositoryInterface {
|
|
|
309
303
|
name: entity.name,
|
|
310
304
|
externalId: entity.externalId,
|
|
311
305
|
moduleName: entity.moduleName,
|
|
312
|
-
...
|
|
306
|
+
...(entity.data || {}),
|
|
313
307
|
};
|
|
314
308
|
}
|
|
315
309
|
|
|
@@ -352,46 +346,8 @@ class ModuleRepositoryMongo extends ModuleRepositoryInterface {
|
|
|
352
346
|
if (moduleName !== undefined) schemaUpdates.moduleName = moduleName;
|
|
353
347
|
if (externalId !== undefined) schemaUpdates.externalId = externalId;
|
|
354
348
|
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
// back. A key that cannot be a path segment, or an app that encrypts
|
|
358
|
-
// an Entity field (a raw command skips encryption), falls back to a
|
|
359
|
-
// whole `data` write through the Prisma model, which keeps the state
|
|
360
|
-
// as it was read.
|
|
361
|
-
const fields = entityDataFields(dynamicData);
|
|
362
|
-
if (Object.keys(fields).length > 0) {
|
|
363
|
-
const paths =
|
|
364
|
-
existing.data &&
|
|
365
|
-
typeof existing.data === 'object' &&
|
|
366
|
-
canMergeEntityDataRaw()
|
|
367
|
-
? dataSetPaths(fields)
|
|
368
|
-
: null;
|
|
369
|
-
if (paths) {
|
|
370
|
-
const result = await this.prisma.$runCommandRaw({
|
|
371
|
-
update: 'Entity',
|
|
372
|
-
updates: [
|
|
373
|
-
{ q: { _id: { $oid: entityId } }, u: { $set: paths } },
|
|
374
|
-
],
|
|
375
|
-
});
|
|
376
|
-
const writeError =
|
|
377
|
-
result?.writeErrors?.[0] || result?.writeConcernError;
|
|
378
|
-
if (writeError || (result && result.ok !== undefined && !result.ok)) {
|
|
379
|
-
throw new Error(
|
|
380
|
-
`Entity data update failed: ${
|
|
381
|
-
writeError?.errmsg || 'command not acknowledged'
|
|
382
|
-
}`
|
|
383
|
-
);
|
|
384
|
-
}
|
|
385
|
-
} else {
|
|
386
|
-
const state = existing.data?.[RATE_LIMIT_STATE_FIELD];
|
|
387
|
-
schemaUpdates.data = {
|
|
388
|
-
...entityDataFields(existing.data),
|
|
389
|
-
...fields,
|
|
390
|
-
...(state !== undefined && {
|
|
391
|
-
[RATE_LIMIT_STATE_FIELD]: state,
|
|
392
|
-
}),
|
|
393
|
-
};
|
|
394
|
-
}
|
|
349
|
+
if (Object.keys(dynamicData).length > 0) {
|
|
350
|
+
schemaUpdates.data = { ...(existing.data || {}), ...dynamicData };
|
|
395
351
|
}
|
|
396
352
|
|
|
397
353
|
try {
|
|
@@ -409,7 +365,7 @@ class ModuleRepositoryMongo extends ModuleRepositoryInterface {
|
|
|
409
365
|
name: entity.name,
|
|
410
366
|
externalId: entity.externalId,
|
|
411
367
|
moduleName: entity.moduleName,
|
|
412
|
-
...
|
|
368
|
+
...(entity.data || {}),
|
|
413
369
|
};
|
|
414
370
|
} catch (error) {
|
|
415
371
|
if (error.code === 'P2025') {
|
|
@@ -1,9 +1,4 @@
|
|
|
1
1
|
const { prisma } = require('../../database/prisma');
|
|
2
|
-
const {
|
|
3
|
-
canMergeEntityDataRaw,
|
|
4
|
-
entityDataFields,
|
|
5
|
-
RATE_LIMIT_STATE_FIELD,
|
|
6
|
-
} = require('./entity-data');
|
|
7
2
|
const { ModuleRepositoryInterface } = require('./module-repository-interface');
|
|
8
3
|
|
|
9
4
|
/**
|
|
@@ -141,7 +136,7 @@ class ModuleRepositoryPostgres extends ModuleRepositoryInterface {
|
|
|
141
136
|
name: entity.name,
|
|
142
137
|
externalId: entity.externalId,
|
|
143
138
|
moduleName: entity.moduleName,
|
|
144
|
-
...
|
|
139
|
+
...(entity.data || {}),
|
|
145
140
|
};
|
|
146
141
|
}
|
|
147
142
|
|
|
@@ -169,7 +164,7 @@ class ModuleRepositoryPostgres extends ModuleRepositoryInterface {
|
|
|
169
164
|
name: e.name,
|
|
170
165
|
externalId: e.externalId,
|
|
171
166
|
moduleName: e.moduleName,
|
|
172
|
-
...
|
|
167
|
+
...(e.data || {}),
|
|
173
168
|
}));
|
|
174
169
|
}
|
|
175
170
|
|
|
@@ -197,7 +192,7 @@ class ModuleRepositoryPostgres extends ModuleRepositoryInterface {
|
|
|
197
192
|
name: e.name,
|
|
198
193
|
externalId: e.externalId,
|
|
199
194
|
moduleName: e.moduleName,
|
|
200
|
-
...
|
|
195
|
+
...(e.data || {}),
|
|
201
196
|
}));
|
|
202
197
|
}
|
|
203
198
|
|
|
@@ -229,7 +224,7 @@ class ModuleRepositoryPostgres extends ModuleRepositoryInterface {
|
|
|
229
224
|
name: e.name,
|
|
230
225
|
externalId: e.externalId,
|
|
231
226
|
moduleName: e.moduleName,
|
|
232
|
-
...
|
|
227
|
+
...(e.data || {}),
|
|
233
228
|
}));
|
|
234
229
|
}
|
|
235
230
|
|
|
@@ -276,7 +271,7 @@ class ModuleRepositoryPostgres extends ModuleRepositoryInterface {
|
|
|
276
271
|
name: entity.name,
|
|
277
272
|
externalId: entity.externalId,
|
|
278
273
|
moduleName: entity.moduleName,
|
|
279
|
-
...
|
|
274
|
+
...(entity.data || {}),
|
|
280
275
|
};
|
|
281
276
|
}
|
|
282
277
|
|
|
@@ -304,7 +299,7 @@ class ModuleRepositoryPostgres extends ModuleRepositoryInterface {
|
|
|
304
299
|
name: e.name,
|
|
305
300
|
externalId: e.externalId,
|
|
306
301
|
moduleName: e.moduleName,
|
|
307
|
-
...
|
|
302
|
+
...(e.data || {}),
|
|
308
303
|
}));
|
|
309
304
|
}
|
|
310
305
|
|
|
@@ -333,7 +328,7 @@ class ModuleRepositoryPostgres extends ModuleRepositoryInterface {
|
|
|
333
328
|
name,
|
|
334
329
|
moduleName,
|
|
335
330
|
externalId,
|
|
336
|
-
data:
|
|
331
|
+
data: dynamicData,
|
|
337
332
|
};
|
|
338
333
|
|
|
339
334
|
const entity = await this.prisma.entity.create({
|
|
@@ -349,7 +344,7 @@ class ModuleRepositoryPostgres extends ModuleRepositoryInterface {
|
|
|
349
344
|
name: entity.name,
|
|
350
345
|
externalId: entity.externalId,
|
|
351
346
|
moduleName: entity.moduleName,
|
|
352
|
-
...
|
|
347
|
+
...(entity.data || {}),
|
|
353
348
|
};
|
|
354
349
|
}
|
|
355
350
|
|
|
@@ -394,39 +389,15 @@ class ModuleRepositoryPostgres extends ModuleRepositoryInterface {
|
|
|
394
389
|
if (moduleName !== undefined) schemaUpdates.moduleName = moduleName;
|
|
395
390
|
if (externalId !== undefined) schemaUpdates.externalId = externalId;
|
|
396
391
|
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
// and the update of the other columns commit together. When the app
|
|
400
|
-
// encrypts an Entity field, a raw statement would skip encryption,
|
|
401
|
-
// so the whole `data` goes through the Prisma model instead.
|
|
402
|
-
const fields = entityDataFields(dynamicData);
|
|
403
|
-
const hasFields = Object.keys(fields).length > 0;
|
|
404
|
-
const rawMerge = hasFields && canMergeEntityDataRaw();
|
|
405
|
-
if (hasFields && !rawMerge) {
|
|
406
|
-
const state = existing.data?.[RATE_LIMIT_STATE_FIELD];
|
|
407
|
-
schemaUpdates.data = {
|
|
408
|
-
...entityDataFields(existing.data),
|
|
409
|
-
...fields,
|
|
410
|
-
...(state !== undefined && { [RATE_LIMIT_STATE_FIELD]: state }),
|
|
411
|
-
};
|
|
392
|
+
if (Object.keys(dynamicData).length > 0) {
|
|
393
|
+
schemaUpdates.data = { ...(existing.data || {}), ...dynamicData };
|
|
412
394
|
}
|
|
413
395
|
|
|
414
396
|
try {
|
|
415
|
-
const
|
|
397
|
+
const entity = await this.prisma.entity.update({
|
|
416
398
|
where: { id: intId },
|
|
417
399
|
data: schemaUpdates,
|
|
418
400
|
});
|
|
419
|
-
const entity = rawMerge
|
|
420
|
-
? (
|
|
421
|
-
await this.prisma.$transaction([
|
|
422
|
-
this.prisma.$executeRaw`
|
|
423
|
-
UPDATE "Entity"
|
|
424
|
-
SET "data" = COALESCE("data", '{}'::jsonb) || ${JSON.stringify(fields)}::jsonb
|
|
425
|
-
WHERE "id" = ${intId}`,
|
|
426
|
-
update,
|
|
427
|
-
])
|
|
428
|
-
)[1]
|
|
429
|
-
: await update;
|
|
430
401
|
|
|
431
402
|
const credentialObj = await this._fetchCredential(entity.credentialId);
|
|
432
403
|
|
|
@@ -437,7 +408,7 @@ class ModuleRepositoryPostgres extends ModuleRepositoryInterface {
|
|
|
437
408
|
name: entity.name,
|
|
438
409
|
externalId: entity.externalId,
|
|
439
410
|
moduleName: entity.moduleName,
|
|
440
|
-
...
|
|
411
|
+
...(entity.data || {}),
|
|
441
412
|
};
|
|
442
413
|
} catch (error) {
|
|
443
414
|
if (error.code === 'P2025') {
|
|
@@ -116,6 +116,43 @@ class OAuth2Requester extends Requester {
|
|
|
116
116
|
|
|
117
117
|
/** @type {boolean} Whether this requester supports token refresh */
|
|
118
118
|
this.isRefreshable = true;
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* @type {number} Refresh the access token before a request when it
|
|
122
|
+
* expires within this many ms. 0 turns it off (the default). A
|
|
123
|
+
* module opts in with `static refreshBeforeExpiryMs`; the
|
|
124
|
+
* constructor param overrides the class value. Opt in only when the
|
|
125
|
+
* provider's refresh token survives a refresh, and persist
|
|
126
|
+
* `accessTokenExpire` in `apiPropertiesToPersist.credential` so a
|
|
127
|
+
* new invocation knows the expiry.
|
|
128
|
+
*/
|
|
129
|
+
const refreshBeforeExpiryMs =
|
|
130
|
+
params?.refreshBeforeExpiryMs ??
|
|
131
|
+
this.constructor.refreshBeforeExpiryMs ??
|
|
132
|
+
0;
|
|
133
|
+
this.refreshBeforeExpiryMs =
|
|
134
|
+
Number.isFinite(refreshBeforeExpiryMs) && refreshBeforeExpiryMs > 0
|
|
135
|
+
? refreshBeforeExpiryMs
|
|
136
|
+
: 0;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* True when proactive refresh is on and the access token expires within
|
|
141
|
+
* `refreshBeforeExpiryMs`. `accessTokenExpire` can be a Date, or an ISO
|
|
142
|
+
* string from the credential store. An unknown or unreadable expiry
|
|
143
|
+
* returns false: the 401 path handles that token.
|
|
144
|
+
*
|
|
145
|
+
* @returns {boolean}
|
|
146
|
+
*/
|
|
147
|
+
_isTokenExpiringSoon() {
|
|
148
|
+
if (!this.refreshBeforeExpiryMs) return false;
|
|
149
|
+
if (!this.accessTokenExpire) return false;
|
|
150
|
+
if (this.grant_type !== 'client_credentials' && !this.refresh_token) {
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
153
|
+
const expiresAt = new Date(this.accessTokenExpire).getTime();
|
|
154
|
+
if (Number.isNaN(expiresAt)) return false;
|
|
155
|
+
return expiresAt - Date.now() <= this.refreshBeforeExpiryMs;
|
|
119
156
|
}
|
|
120
157
|
|
|
121
158
|
/**
|
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
const parsers = require('./parsers');
|
|
2
2
|
const policy = require('./policy');
|
|
3
|
-
const pacer = require('./pacer');
|
|
4
|
-
const state = require('./state/rate-limit-state-repository-factory');
|
|
5
3
|
|
|
6
|
-
module.exports = { ...parsers, ...policy
|
|
4
|
+
module.exports = { ...parsers, ...policy };
|
|
@@ -79,11 +79,6 @@ function normalizePolicy(raw, owner) {
|
|
|
79
79
|
policy.maxConcurrency = raw.maxConcurrency;
|
|
80
80
|
}
|
|
81
81
|
if (Array.isArray(raw.windows)) policy.windows = raw.windows;
|
|
82
|
-
// Pacing (ADR-049 section 6) is on for every module that declares a
|
|
83
|
-
// policy. `pacing: false` turns it off. `shared: true` keeps the block
|
|
84
|
-
// and the retry slots in the app database, for parallel workers.
|
|
85
|
-
if (raw.pacing === false) policy.pacing = false;
|
|
86
|
-
if (raw.shared === true) policy.shared = true;
|
|
87
82
|
if (isPlainObject(raw.userHints)) policy.userHints = raw.userHints;
|
|
88
83
|
|
|
89
84
|
normalizedPolicies.add(policy);
|
|
@@ -216,26 +211,6 @@ function staticHint(policy, reason, now) {
|
|
|
216
211
|
return waitMs > 0 ? hintFromWait(waitMs, now, { source: 'static' }) : null;
|
|
217
212
|
}
|
|
218
213
|
|
|
219
|
-
/**
|
|
220
|
-
* How long a throttle blocks the whole scope key: the provider's time only.
|
|
221
|
-
* A parsed or classified hint gives it directly. A static hint is
|
|
222
|
-
* `max(window.perMs, minRetryAfterMs)`; only the named window is the
|
|
223
|
-
* provider's, so `minRetryAfterMs` delays the one message, not the key.
|
|
224
|
-
* Returns 0 when the provider gave no time.
|
|
225
|
-
*/
|
|
226
|
-
function scopeBlockMs(hint, policy) {
|
|
227
|
-
if (!hint || !Number.isFinite(hint.waitMs)) return 0;
|
|
228
|
-
if (hint.source !== 'static') return Math.max(0, hint.waitMs);
|
|
229
|
-
const window = policy?.windows?.find(
|
|
230
|
-
(candidate) =>
|
|
231
|
-
candidate &&
|
|
232
|
-
candidate.name === hint.reason &&
|
|
233
|
-
Number.isFinite(candidate.perMs) &&
|
|
234
|
-
candidate.perMs > 0
|
|
235
|
-
);
|
|
236
|
-
return window?.perMs ?? 0;
|
|
237
|
-
}
|
|
238
|
-
|
|
239
214
|
function withClassified(hint, classified) {
|
|
240
215
|
return {
|
|
241
216
|
...hint,
|
|
@@ -334,7 +309,6 @@ function inProcessBudgetMs({
|
|
|
334
309
|
}
|
|
335
310
|
|
|
336
311
|
module.exports = {
|
|
337
|
-
scopeBlockMs,
|
|
338
312
|
DEFAULT_PARSERS,
|
|
339
313
|
JITTER_MAX_MS,
|
|
340
314
|
MIN_HINTED_WAIT_MS,
|