@friggframework/core 2.0.0-next.127 → 2.0.0-next.129
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 +166 -7
- package/core/CLAUDE.md +57 -1
- package/core/Worker.js +420 -9
- package/core/create-handler.js +20 -0
- package/core/index.js +6 -0
- package/core/invocation-deadline.js +91 -0
- package/core/invocation-scope.js +30 -24
- package/core/rate-limited-response.js +27 -0
- package/database/encryption/field-encryption-service.js +42 -0
- package/database/encryption/prisma-encryption-extension.js +6 -9
- package/encrypt/Cryptor.js +90 -38
- package/errors/fetch-error.js +10 -6
- package/errors/index.js +2 -0
- package/errors/rate-limit-error.js +46 -0
- package/generated/prisma-mongodb/edge.js +6 -2
- package/generated/prisma-mongodb/index.js +10 -2
- package/generated/prisma-mongodb/libquery_engine-linux-arm64-openssl-3.0.x.so.node +0 -0
- package/generated/prisma-mongodb/package.json +1 -1
- package/generated/prisma-mongodb/schema.prisma +1 -1
- package/generated/prisma-mongodb/wasm.js +6 -2
- package/generated/prisma-postgresql/edge.js +6 -2
- package/generated/prisma-postgresql/index.js +10 -2
- package/generated/prisma-postgresql/libquery_engine-linux-arm64-openssl-3.0.x.so.node +0 -0
- package/generated/prisma-postgresql/package.json +1 -1
- package/generated/prisma-postgresql/schema.prisma +1 -1
- package/generated/prisma-postgresql/wasm.js +6 -2
- package/handlers/app-handler-helpers.js +15 -0
- package/handlers/backend-utils.js +75 -1
- package/handlers/workers/integration-defined-workers.js +2 -0
- package/index.js +18 -0
- package/infrastructure/scheduler/eventbridge-scheduler-adapter.js +2 -0
- package/integrations/repositories/integration-repository-documentdb.js +59 -10
- package/integrations/repositories/integration-repository-interface.js +50 -7
- package/integrations/repositories/integration-repository-mongo.js +88 -16
- package/integrations/repositories/integration-repository-postgres.js +89 -16
- package/integrations/repositories/message-item-shared.js +17 -0
- package/integrations/tests/doubles/prisma-message-row.js +24 -0
- package/integrations/tests/doubles/test-integration-repository.js +32 -2
- package/integrations/use-cases/clear-rate-limit-wait.js +28 -0
- package/integrations/use-cases/index.js +7 -1
- package/integrations/use-cases/record-rate-limit-message.js +104 -0
- package/integrations/use-cases/record-rate-limit-wait.js +36 -0
- package/integrations/use-cases/update-integration-messages.js +28 -9
- package/logs/serialize.js +15 -0
- package/modules/requester/oauth-2.js +8 -0
- package/modules/requester/rate-limit/index.js +4 -0
- package/modules/requester/rate-limit/parsers.js +249 -0
- package/modules/requester/rate-limit/policy.js +321 -0
- package/modules/requester/requester.js +187 -9
- package/package.json +7 -6
- package/prisma-mongodb/schema.prisma +1 -1
- package/prisma-postgresql/schema.prisma +1 -1
- package/queues/queue-deferral.js +80 -0
- package/queues/queue-delivery.js +1 -0
- package/queues/queuer-util.js +1 -1
- package/telemetry/otel-telemetry.js +76 -3
- package/telemetry/telemetry-config.js +16 -5
- package/telemetry/telemetry-runtime.js +14 -1
- package/types/core/consumer.fixture.ts +12 -0
- package/types/core/index.d.ts +84 -0
- package/types/errors/index.d.ts +47 -0
- package/types/integrations/index.d.ts +25 -4
- package/types/module-plugin/index.d.ts +45 -0
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
const { toPositiveInteger } = require('./queue-delivery');
|
|
2
|
+
|
|
3
|
+
const MAX_DEFERRALS = 30;
|
|
4
|
+
const MAX_DEFERRED_MS = 26 * 60 * 60 * 1000;
|
|
5
|
+
const RATE_LIMIT_PROBE_MS = 60 * 60 * 1000;
|
|
6
|
+
const MAX_DELAY_SECONDS = 900;
|
|
7
|
+
const MAX_VISIBILITY_TIMEOUT_SECONDS = 43_200;
|
|
8
|
+
const MAX_LAMBDA_RUN_SECONDS = 900;
|
|
9
|
+
const MAX_BATCHING_WINDOW_SECONDS = 300;
|
|
10
|
+
const MAX_DEFERRED_VISIBILITY_SECONDS =
|
|
11
|
+
MAX_VISIBILITY_TIMEOUT_SECONDS -
|
|
12
|
+
MAX_LAMBDA_RUN_SECONDS -
|
|
13
|
+
MAX_BATCHING_WINDOW_SECONDS;
|
|
14
|
+
const MAX_DEFERRALS_ENV = 'FRIGG_QUEUE_MAX_DEFERRALS';
|
|
15
|
+
const MAX_DEFERRED_MS_ENV = 'FRIGG_QUEUE_MAX_DEFERRED_MS';
|
|
16
|
+
const RATE_LIMIT_PROBE_MS_ENV = 'FRIGG_QUEUE_RATE_LIMIT_PROBE_MS';
|
|
17
|
+
|
|
18
|
+
const readDeferralLimits = (env = process.env) => ({
|
|
19
|
+
maxDeferrals: toPositiveInteger(env[MAX_DEFERRALS_ENV]) ?? MAX_DEFERRALS,
|
|
20
|
+
maxDeferredMs:
|
|
21
|
+
toPositiveInteger(env[MAX_DEFERRED_MS_ENV]) ?? MAX_DEFERRED_MS,
|
|
22
|
+
probeMs:
|
|
23
|
+
toPositiveInteger(env[RATE_LIMIT_PROBE_MS_ENV]) ?? RATE_LIMIT_PROBE_MS,
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
const readDeferral = (body) => {
|
|
27
|
+
const frigg = body?._frigg;
|
|
28
|
+
const deferrals =
|
|
29
|
+
Number.isInteger(frigg?.deferrals) && frigg.deferrals > 0
|
|
30
|
+
? frigg.deferrals
|
|
31
|
+
: 0;
|
|
32
|
+
const firstDeferredAt =
|
|
33
|
+
typeof frigg?.firstDeferredAt === 'string' &&
|
|
34
|
+
!Number.isNaN(Date.parse(frigg.firstDeferredAt))
|
|
35
|
+
? frigg.firstDeferredAt
|
|
36
|
+
: undefined;
|
|
37
|
+
const deferralId =
|
|
38
|
+
typeof frigg?.deferralId === 'string' && frigg.deferralId !== ''
|
|
39
|
+
? frigg.deferralId
|
|
40
|
+
: undefined;
|
|
41
|
+
return { deferrals, firstDeferredAt, deferralId };
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const nextDeferral = (body, now = Date.now(), messageId) => {
|
|
45
|
+
const current = readDeferral(body);
|
|
46
|
+
return {
|
|
47
|
+
deferrals: current.deferrals + 1,
|
|
48
|
+
firstDeferredAt: current.firstDeferredAt ?? new Date(now).toISOString(),
|
|
49
|
+
deferralId: current.deferralId ?? messageId,
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const withDeferral = (body, deferral) => ({
|
|
54
|
+
...body,
|
|
55
|
+
_frigg: { ...body._frigg, ...deferral },
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
const deferredMs = ({ firstDeferredAt, retryAt }) =>
|
|
59
|
+
retryAt.getTime() - Date.parse(firstDeferredAt);
|
|
60
|
+
|
|
61
|
+
const isDeferralCapped = (deferral, { maxDeferrals, maxDeferredMs }) =>
|
|
62
|
+
deferral.deferrals > maxDeferrals || deferredMs(deferral) > maxDeferredMs;
|
|
63
|
+
|
|
64
|
+
module.exports = {
|
|
65
|
+
MAX_DEFERRALS,
|
|
66
|
+
MAX_DEFERRALS_ENV,
|
|
67
|
+
MAX_DEFERRED_MS,
|
|
68
|
+
MAX_DEFERRED_MS_ENV,
|
|
69
|
+
MAX_DEFERRED_VISIBILITY_SECONDS,
|
|
70
|
+
MAX_DELAY_SECONDS,
|
|
71
|
+
MAX_VISIBILITY_TIMEOUT_SECONDS,
|
|
72
|
+
RATE_LIMIT_PROBE_MS,
|
|
73
|
+
RATE_LIMIT_PROBE_MS_ENV,
|
|
74
|
+
deferredMs,
|
|
75
|
+
isDeferralCapped,
|
|
76
|
+
nextDeferral,
|
|
77
|
+
readDeferral,
|
|
78
|
+
readDeferralLimits,
|
|
79
|
+
withDeferral,
|
|
80
|
+
};
|
package/queues/queue-delivery.js
CHANGED
package/queues/queuer-util.js
CHANGED
|
@@ -16,6 +16,7 @@ const { createTelemetryEventBus } = require('./telemetry-event-bus');
|
|
|
16
16
|
const {
|
|
17
17
|
runWithTelemetryContext,
|
|
18
18
|
mergeTelemetryContext,
|
|
19
|
+
getTelemetryContextStore,
|
|
19
20
|
} = require('./telemetry-context');
|
|
20
21
|
const { serializeError } = require('../logs/serialize');
|
|
21
22
|
const { LOGGER_SCOPE_KEY } = require('../logs/context');
|
|
@@ -27,6 +28,74 @@ const TRACER_NAME = 'frigg';
|
|
|
27
28
|
const METRIC_EXPORT_INTERVAL_MS =
|
|
28
29
|
Number(process.env.OTEL_METRIC_EXPORT_INTERVAL_MS) || 60000;
|
|
29
30
|
|
|
31
|
+
// ADR-011 §2: high-cardinality ids ride baggage/ALS; copy onto SPANS only
|
|
32
|
+
// (never metric labels). Keys match withContext baggage entry names.
|
|
33
|
+
const BAGGAGE_TO_SPAN_ATTR = Object.freeze({
|
|
34
|
+
integrationId: 'frigg.integration_id',
|
|
35
|
+
userId: 'frigg.user_id',
|
|
36
|
+
integrationType: 'integration_type',
|
|
37
|
+
version: 'frigg.version',
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
function nonEmptyString(value) {
|
|
41
|
+
if (value === undefined || value === null) return null;
|
|
42
|
+
const s = String(value).trim();
|
|
43
|
+
return s.length > 0 ? s : null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Built-in processor: stamp active baggage (fallback ALS) onto each new span
|
|
48
|
+
* as Frigg span attributes. Runs ahead of BatchSpanProcessor so exported OTLP
|
|
49
|
+
* spans carry attribution Watchtower/backends can index. Never touches metrics.
|
|
50
|
+
*/
|
|
51
|
+
class BaggageAttributeSpanProcessor {
|
|
52
|
+
onStart(span, parentContext) {
|
|
53
|
+
try {
|
|
54
|
+
const baggage = otelApi.propagation.getBaggage(parentContext);
|
|
55
|
+
const als = getTelemetryContextStore();
|
|
56
|
+
const existing =
|
|
57
|
+
span && span.attributes && typeof span.attributes === 'object'
|
|
58
|
+
? span.attributes
|
|
59
|
+
: null;
|
|
60
|
+
|
|
61
|
+
for (const [baggageKey, attrName] of Object.entries(
|
|
62
|
+
BAGGAGE_TO_SPAN_ATTR
|
|
63
|
+
)) {
|
|
64
|
+
if (
|
|
65
|
+
existing &&
|
|
66
|
+
existing[attrName] !== undefined &&
|
|
67
|
+
existing[attrName] !== null
|
|
68
|
+
) {
|
|
69
|
+
continue; // explicit span attribute wins
|
|
70
|
+
}
|
|
71
|
+
let value = null;
|
|
72
|
+
if (baggage) {
|
|
73
|
+
const entry = baggage.getEntry(baggageKey);
|
|
74
|
+
value = nonEmptyString(entry && entry.value);
|
|
75
|
+
}
|
|
76
|
+
if (!value && als) {
|
|
77
|
+
value = nonEmptyString(als[baggageKey]);
|
|
78
|
+
}
|
|
79
|
+
if (value) {
|
|
80
|
+
span.setAttribute(attrName, value);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
} catch (_) {
|
|
84
|
+
// Telemetry must never break the wrapped path.
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
onEnd() {}
|
|
89
|
+
|
|
90
|
+
shutdown() {
|
|
91
|
+
return Promise.resolve();
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
forceFlush() {
|
|
95
|
+
return Promise.resolve();
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
30
99
|
/**
|
|
31
100
|
* Register an AsyncLocalStorage-backed OTel context manager once per process.
|
|
32
101
|
* Without it `BasicTracerProvider` uses the Noop context manager, so
|
|
@@ -83,12 +152,16 @@ class OtelTelemetry {
|
|
|
83
152
|
})
|
|
84
153
|
: new AlwaysOnSampler();
|
|
85
154
|
|
|
155
|
+
// Attribution processor ahead of the batch exporter so every exported
|
|
156
|
+
// span (handler, apimodule, adopter) carries frigg.* ids from baggage.
|
|
157
|
+
const spanProcessors = [new BaggageAttributeSpanProcessor()];
|
|
158
|
+
if (traceExporter) {
|
|
159
|
+
spanProcessors.push(new BatchSpanProcessor(traceExporter));
|
|
160
|
+
}
|
|
86
161
|
this._tracerProvider = new BasicTracerProvider({
|
|
87
162
|
resource: res,
|
|
88
163
|
sampler,
|
|
89
|
-
spanProcessors
|
|
90
|
-
? [new BatchSpanProcessor(traceExporter)]
|
|
91
|
-
: [],
|
|
164
|
+
spanProcessors,
|
|
92
165
|
});
|
|
93
166
|
|
|
94
167
|
const metricReader = metricExporter
|
|
@@ -3,11 +3,14 @@
|
|
|
3
3
|
* into a normalized config the TelemetryService and
|
|
4
4
|
* usage rollup consume: `{ exporter, northStar, sampleRatio }`.
|
|
5
5
|
*
|
|
6
|
-
* Exporter default
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
6
|
+
* Exporter default when `Definition.telemetry.exporter` is omitted:
|
|
7
|
+
* 1. `OTEL_EXPORTER_OTLP_ENDPOINT` set (non-empty after trim) → `{ type: 'otlp' }`
|
|
8
|
+
* (zero-code Watchtower / collector activation on any stage, including local)
|
|
9
|
+
* 2. else STAGE=local → `{ type: 'console' }` (visible, free)
|
|
10
|
+
* 3. else → `{ type: 'none' }` (deployed stages must not surprise-write to
|
|
11
|
+
* CloudWatch; `dev` is a deployed AWS stage)
|
|
12
|
+
* An explicit `Definition.telemetry.exporter` always wins over the env var, so
|
|
13
|
+
* `{ type: 'none' }` still disables export even when the endpoint is set.
|
|
11
14
|
*/
|
|
12
15
|
const VALID_EXPORTER_TYPES = [
|
|
13
16
|
'none',
|
|
@@ -18,8 +21,16 @@ const VALID_EXPORTER_TYPES = [
|
|
|
18
21
|
'datadog',
|
|
19
22
|
];
|
|
20
23
|
|
|
24
|
+
function hasOtlpEndpointEnv() {
|
|
25
|
+
const endpoint = process.env.OTEL_EXPORTER_OTLP_ENDPOINT;
|
|
26
|
+
return typeof endpoint === 'string' && endpoint.trim().length > 0;
|
|
27
|
+
}
|
|
28
|
+
|
|
21
29
|
function normalizeExporterConfig(exporter, stage) {
|
|
22
30
|
if (!exporter) {
|
|
31
|
+
if (hasOtlpEndpointEnv()) {
|
|
32
|
+
return { type: 'otlp' };
|
|
33
|
+
}
|
|
23
34
|
return stage === 'local' ? { type: 'console' } : { type: 'none' };
|
|
24
35
|
}
|
|
25
36
|
|
|
@@ -47,8 +47,21 @@ function getTelemetry() {
|
|
|
47
47
|
try {
|
|
48
48
|
const { telemetry = {} } = loadTelemetryConfig();
|
|
49
49
|
const stage = process.env.STAGE || process.env.NODE_ENV || 'production';
|
|
50
|
+
// loadAppDefinition normally resolves via resolveTelemetryConfig. When the
|
|
51
|
+
// loader returns an unresolved block (exporter unset) — e.g. tests, or a
|
|
52
|
+
// Definition with no telemetry key — apply the same env-aware default so
|
|
53
|
+
// OTEL_EXPORTER_OTLP_ENDPOINT still auto-activates `{ type: 'otlp' }`.
|
|
54
|
+
// Explicit exporter (incl. `{ type: 'none' }`) always wins.
|
|
55
|
+
let exporter = telemetry.exporter;
|
|
56
|
+
if (exporter === undefined || exporter === null) {
|
|
57
|
+
const { resolveTelemetryConfig } = require('./telemetry-config');
|
|
58
|
+
exporter = resolveTelemetryConfig(
|
|
59
|
+
{ telemetry: {} },
|
|
60
|
+
{ stage }
|
|
61
|
+
).exporter;
|
|
62
|
+
}
|
|
50
63
|
state.telemetry = createTelemetry({
|
|
51
|
-
exporter
|
|
64
|
+
exporter,
|
|
52
65
|
sampleRatio: telemetry.sampleRatio,
|
|
53
66
|
resource: { service: process.env.FRIGG_STACK || 'frigg', stage },
|
|
54
67
|
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/// <reference path="../index.d.ts" />
|
|
2
|
+
import { RateLimitError, parseResetHeaders } from "@friggframework/core";
|
|
3
|
+
import type { RateLimitPolicy } from "@friggframework/module-plugin";
|
|
4
|
+
|
|
5
|
+
const error = new RateLimitError({ waitMs: 1_000 });
|
|
6
|
+
const isRateLimited: true = error.isRateLimited;
|
|
7
|
+
const retryAt: Date | undefined = error.retryAt;
|
|
8
|
+
|
|
9
|
+
const policy: RateLimitPolicy = { resetDeltaUnit: "ms" };
|
|
10
|
+
const hint = parseResetHeaders({}, { now: 0, resetDeltaUnit: "ms" });
|
|
11
|
+
|
|
12
|
+
export { isRateLimited, retryAt, policy, hint };
|
package/types/core/index.d.ts
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
declare module "@friggframework/core" {
|
|
2
2
|
import type { SendMessageCommandInput } from "@aws-sdk/client-sqs";
|
|
3
|
+
import type { RateLimitHint } from "@friggframework/errors";
|
|
4
|
+
import type {
|
|
5
|
+
RateLimitPolicy,
|
|
6
|
+
RateLimitSignal,
|
|
7
|
+
} from "@friggframework/module-plugin";
|
|
8
|
+
|
|
9
|
+
export { RateLimitError } from "@friggframework/errors";
|
|
3
10
|
|
|
4
11
|
export class Delegate implements IFriggDelegate {
|
|
5
12
|
delegate: any;
|
|
@@ -42,8 +49,57 @@ declare module "@friggframework/core" {
|
|
|
42
49
|
send(params: object & { QueueUrl: any }, delay?: number): Promise<string>;
|
|
43
50
|
|
|
44
51
|
sendAsyncSQSMessage(params: SendSQSMessageParams): Promise<string>;
|
|
52
|
+
|
|
53
|
+
recordRateLimitWait(
|
|
54
|
+
body: object,
|
|
55
|
+
error: Error,
|
|
56
|
+
state: RateLimitWaitState
|
|
57
|
+
): Promise<void>;
|
|
58
|
+
|
|
59
|
+
clearRateLimitWait(
|
|
60
|
+
body: object,
|
|
61
|
+
options?: { deferralId?: string }
|
|
62
|
+
): Promise<void>;
|
|
63
|
+
|
|
64
|
+
decideRateLimited(
|
|
65
|
+
event: RateLimitedEvent
|
|
66
|
+
): Promise<RateLimitedDecision | void>;
|
|
67
|
+
|
|
68
|
+
/** Runs past the delay tier, for an unknown retryAt, or at a cap. */
|
|
69
|
+
recordRateLimitMessage(
|
|
70
|
+
body: object,
|
|
71
|
+
error: Error,
|
|
72
|
+
limit: { retryAt: Date; capped: boolean }
|
|
73
|
+
): Promise<void>;
|
|
45
74
|
}
|
|
46
75
|
|
|
76
|
+
/** ADR-049 section 3. The input of the RATE_LIMITED event handler. */
|
|
77
|
+
export type RateLimitedEvent = {
|
|
78
|
+
retryAt?: Date;
|
|
79
|
+
waitMs?: number;
|
|
80
|
+
reason?: string;
|
|
81
|
+
module?: string;
|
|
82
|
+
scopeKey?: string;
|
|
83
|
+
message: object;
|
|
84
|
+
delivery: {
|
|
85
|
+
messageId: string;
|
|
86
|
+
receiveCount?: number;
|
|
87
|
+
deferrals: number;
|
|
88
|
+
deferredMs: number;
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
/** `defer` or no value runs the default handler. A throw counts as `retry`. */
|
|
93
|
+
export type RateLimitedDecision = "defer" | "drop" | "retry";
|
|
94
|
+
|
|
95
|
+
export type RateLimitWaitState = {
|
|
96
|
+
status: "WAITING" | "EXHAUSTED";
|
|
97
|
+
mechanism: "delay" | "schedule" | "visibility" | "none";
|
|
98
|
+
deferrals: number;
|
|
99
|
+
deferralId?: string;
|
|
100
|
+
retryAt: Date;
|
|
101
|
+
};
|
|
102
|
+
|
|
47
103
|
interface IWorker {
|
|
48
104
|
getQueueURL(params: GetQueueURLParams): Promise<string | undefined>;
|
|
49
105
|
run(params: { Records: any }, context?: object): Promise<BatchItemFailuresResponse>;
|
|
@@ -53,6 +109,34 @@ declare module "@friggframework/core" {
|
|
|
53
109
|
|
|
54
110
|
export function loadInstalledModules(): any[];
|
|
55
111
|
|
|
112
|
+
export function runWithInvocationDeadline<T>(
|
|
113
|
+
limits: { deadlineAt?: number; maxInProcessWaitMs?: number },
|
|
114
|
+
fn: () => T
|
|
115
|
+
): T;
|
|
116
|
+
|
|
117
|
+
export function remainingInvocationMs(now?: number): number;
|
|
118
|
+
|
|
119
|
+
export function classifyRateLimit(
|
|
120
|
+
policy: RateLimitPolicy | undefined,
|
|
121
|
+
signal: RateLimitSignal,
|
|
122
|
+
options?: { now?: number; onClassifyError?: (error: Error) => void }
|
|
123
|
+
): RateLimitHint | null;
|
|
124
|
+
|
|
125
|
+
export function parseRetryAfter(
|
|
126
|
+
value: string | null | undefined,
|
|
127
|
+
options?: { now?: number }
|
|
128
|
+
): RateLimitHint | null;
|
|
129
|
+
|
|
130
|
+
export function parseResetHeaders(
|
|
131
|
+
headers: object | undefined,
|
|
132
|
+
options?: { now?: number; resetDeltaUnit?: "s" | "ms" }
|
|
133
|
+
): RateLimitHint | null;
|
|
134
|
+
|
|
135
|
+
export function parseIetfRateLimit(
|
|
136
|
+
headers: object | undefined,
|
|
137
|
+
options?: { now?: number }
|
|
138
|
+
): RateLimitHint | null;
|
|
139
|
+
|
|
56
140
|
type GetQueueURLParams = {
|
|
57
141
|
QueueName: string;
|
|
58
142
|
QueueOwnerAWSAccountId?: string;
|
package/types/errors/index.d.ts
CHANGED
|
@@ -16,10 +16,57 @@ declare module "@friggframework/errors" {
|
|
|
16
16
|
readonly body: any;
|
|
17
17
|
isTimeout?: boolean;
|
|
18
18
|
timeoutMs?: number;
|
|
19
|
+
isRateLimited?: boolean;
|
|
20
|
+
reason?: RateLimitReason;
|
|
19
21
|
|
|
20
22
|
static create(options?: CreateFetchErrorParams): Promise<FetchError>;
|
|
21
23
|
}
|
|
22
24
|
|
|
25
|
+
export type RateLimitReason =
|
|
26
|
+
| "burst"
|
|
27
|
+
| "daily"
|
|
28
|
+
| "monthly"
|
|
29
|
+
| "concurrency"
|
|
30
|
+
| "unknown";
|
|
31
|
+
|
|
32
|
+
export type RateLimitSource = "header" | "body" | "static" | "backoff";
|
|
33
|
+
|
|
34
|
+
export type RateLimitHint = {
|
|
35
|
+
retryAt: Date;
|
|
36
|
+
waitMs: number;
|
|
37
|
+
reason: RateLimitReason;
|
|
38
|
+
policy?: string;
|
|
39
|
+
remaining?: number;
|
|
40
|
+
source: RateLimitSource;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export class RateLimitError extends FetchError {
|
|
44
|
+
constructor(options?: RateLimitErrorConstructor);
|
|
45
|
+
|
|
46
|
+
isRateLimited: true;
|
|
47
|
+
retryAt?: Date;
|
|
48
|
+
waitMs?: number;
|
|
49
|
+
reason: RateLimitReason;
|
|
50
|
+
policy?: string;
|
|
51
|
+
source: RateLimitSource | "unknown";
|
|
52
|
+
module?: string;
|
|
53
|
+
scopeKey?: string;
|
|
54
|
+
|
|
55
|
+
static create(
|
|
56
|
+
options?: CreateRateLimitErrorParams
|
|
57
|
+
): Promise<RateLimitError>;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
type RateLimitErrorConstructor = FetchErrorConstructor & {
|
|
61
|
+
hint?: Partial<RateLimitHint>;
|
|
62
|
+
waitMs?: number;
|
|
63
|
+
module?: string;
|
|
64
|
+
scopeKey?: string;
|
|
65
|
+
now?: number;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
type CreateRateLimitErrorParams = RateLimitErrorConstructor;
|
|
69
|
+
|
|
23
70
|
type FetchErrorResponse = {
|
|
24
71
|
headers?: object;
|
|
25
72
|
status?: number;
|
|
@@ -1,6 +1,27 @@
|
|
|
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
|
+
/** A stored integration message. Each type keeps its newest 50 items. */
|
|
9
|
+
export interface IntegrationMessage {
|
|
10
|
+
title: string | null;
|
|
11
|
+
message: string;
|
|
12
|
+
timestamp: number;
|
|
13
|
+
[key: string]: unknown;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** ADR-049 section 5. Stored in `warnings`; `retryAt` is ISO UTC. */
|
|
17
|
+
export interface RateLimitedIntegrationMessage extends IntegrationMessage {
|
|
18
|
+
code: "RATE_LIMITED";
|
|
19
|
+
module: string;
|
|
20
|
+
reason: string;
|
|
21
|
+
retryAt: string;
|
|
22
|
+
actions: IntegrationMessageAction[];
|
|
23
|
+
}
|
|
24
|
+
|
|
4
25
|
export interface Integration {
|
|
5
26
|
entities: any[];
|
|
6
27
|
userId: string;
|
|
@@ -8,10 +29,10 @@ declare module "@friggframework/integrations" {
|
|
|
8
29
|
config: any;
|
|
9
30
|
version: string;
|
|
10
31
|
messages: {
|
|
11
|
-
errors: [];
|
|
12
|
-
warnings: [];
|
|
13
|
-
info: [];
|
|
14
|
-
logs: [];
|
|
32
|
+
errors: IntegrationMessage[];
|
|
33
|
+
warnings: IntegrationMessage[];
|
|
34
|
+
info: IntegrationMessage[];
|
|
35
|
+
logs: IntegrationMessage[];
|
|
15
36
|
};
|
|
16
37
|
}
|
|
17
38
|
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
declare module "@friggframework/module-plugin" {
|
|
2
2
|
import { Delegate, IFriggDelegate } from "@friggframework/core";
|
|
3
|
+
import type { RateLimitHint } from "@friggframework/errors";
|
|
3
4
|
|
|
4
5
|
export interface Credential {
|
|
5
6
|
id?: string;
|
|
@@ -22,8 +23,51 @@ declare module "@friggframework/module-plugin" {
|
|
|
22
23
|
export type MappedEntity = Entity & { id: string; type: any };
|
|
23
24
|
|
|
24
25
|
|
|
26
|
+
export type RateLimitScope =
|
|
27
|
+
| "credential"
|
|
28
|
+
| "entity"
|
|
29
|
+
| "app"
|
|
30
|
+
| ((requester: Requester) => string | number | null | undefined);
|
|
31
|
+
|
|
32
|
+
export type RateLimitSignal = {
|
|
33
|
+
status?: number;
|
|
34
|
+
headers?: { get(name: string): string | null } | object;
|
|
35
|
+
body?: unknown;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export type RateLimitWindow = {
|
|
39
|
+
name: string;
|
|
40
|
+
limit?: number;
|
|
41
|
+
perMs?: number;
|
|
42
|
+
rollingMs?: number;
|
|
43
|
+
resets?: { at: string; tz: string };
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export type RateLimitPolicy = {
|
|
47
|
+
scope?: RateLimitScope;
|
|
48
|
+
windows?: RateLimitWindow[];
|
|
49
|
+
maxConcurrency?: number;
|
|
50
|
+
minRetryAfterMs?: number;
|
|
51
|
+
parsers?: Array<"retryAfter" | "resetHeaders" | "ietf">;
|
|
52
|
+
resetDeltaUnit?: "s" | "ms";
|
|
53
|
+
classify?(
|
|
54
|
+
signal: RateLimitSignal
|
|
55
|
+
): Partial<Omit<RateLimitHint, "source">> & {
|
|
56
|
+
source?: "header" | "body" | "static";
|
|
57
|
+
} | null | undefined;
|
|
58
|
+
/** Keyed by reason. Client-safe text and links for the RATE_LIMITED message. */
|
|
59
|
+
userHints?: Record<
|
|
60
|
+
string,
|
|
61
|
+
{ message?: string; links?: Array<{ label: string; url: string }> }
|
|
62
|
+
>;
|
|
63
|
+
};
|
|
64
|
+
|
|
25
65
|
export class Requester implements IFriggRequester {
|
|
66
|
+
static rateLimit?: RateLimitPolicy;
|
|
67
|
+
static requestTimeoutMs?: number;
|
|
68
|
+
|
|
26
69
|
DLGT_INVALID_AUTH: string;
|
|
70
|
+
requestTimeoutMs: number;
|
|
27
71
|
backOff: number[];
|
|
28
72
|
fetch: any;
|
|
29
73
|
isRefreshable: boolean;
|
|
@@ -87,6 +131,7 @@ declare module "@friggframework/module-plugin" {
|
|
|
87
131
|
type RequesterConstructor = {
|
|
88
132
|
backOff?: number[];
|
|
89
133
|
fetch?: any;
|
|
134
|
+
random?: () => number;
|
|
90
135
|
};
|
|
91
136
|
|
|
92
137
|
export class ApiKeyRequester
|