@friggframework/core 2.0.0--canary.737.83b4e17.0 → 2.0.0--canary.743.57dc4bf.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 +173 -7
- package/core/CLAUDE.md +57 -1
- package/core/Worker.js +440 -11
- 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/errors/fetch-error.js +10 -6
- package/errors/index.js +2 -0
- package/errors/rate-limit-error.js +46 -0
- 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 +26 -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 +6 -0
- package/modules/requester/rate-limit/pacer.js +551 -0
- package/modules/requester/rate-limit/parsers.js +249 -0
- package/modules/requester/rate-limit/policy.js +326 -0
- package/modules/requester/rate-limit/state/rate-limit-state-repository-factory.js +35 -0
- package/modules/requester/rate-limit/state/rate-limit-state-repository-interface.js +69 -0
- package/modules/requester/rate-limit/state/rate-limit-state-repository-memory.js +62 -0
- package/modules/requester/rate-limit/state/rate-limit-state-repository-mongo.js +148 -0
- package/modules/requester/rate-limit/state/rate-limit-state-repository-postgres.js +93 -0
- package/modules/requester/requester.js +357 -11
- package/package.json +7 -6
- package/queues/queue-deferral.js +155 -0
- package/queues/queue-delivery.js +1 -0
- package/queues/queuer-util.js +3 -4
- 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 +53 -0
- package/queues/sqs-message-body.js +0 -17
package/queues/queuer-util.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
const { v4: uuid } = require('uuid');
|
|
2
2
|
const { SQSClient, SendMessageCommand, SendMessageBatchCommand } = require('@aws-sdk/client-sqs');
|
|
3
3
|
const { summarizeMessageBody } = require('../logs/summarize-event');
|
|
4
|
-
const { toSqsMessageBody } = require('./sqs-message-body');
|
|
5
4
|
|
|
6
5
|
const awsConfigOptions = () => {
|
|
7
6
|
const config = {};
|
|
@@ -79,7 +78,7 @@ const inspectBatchResult = (result, queueUrl, buffer) => {
|
|
|
79
78
|
const QueuerUtil = {
|
|
80
79
|
send: async (message, queueUrl) => {
|
|
81
80
|
const command = new SendMessageCommand({
|
|
82
|
-
MessageBody:
|
|
81
|
+
MessageBody: JSON.stringify(message),
|
|
83
82
|
QueueUrl: queueUrl,
|
|
84
83
|
});
|
|
85
84
|
const result = await sqs.send(command);
|
|
@@ -96,7 +95,7 @@ const QueuerUtil = {
|
|
|
96
95
|
for (const entry of entries) {
|
|
97
96
|
buffer.push({
|
|
98
97
|
Id: uuid(),
|
|
99
|
-
MessageBody:
|
|
98
|
+
MessageBody: JSON.stringify(entry),
|
|
100
99
|
});
|
|
101
100
|
// Sends 10, then starts a fresh buffer. Each command gets its own
|
|
102
101
|
// array: clearing the shared buffer in place would also empty the
|
|
@@ -128,4 +127,4 @@ const QueuerUtil = {
|
|
|
128
127
|
},
|
|
129
128
|
};
|
|
130
129
|
|
|
131
|
-
module.exports = { QueuerUtil };
|
|
130
|
+
module.exports = { QueuerUtil, awsConfigOptions };
|
|
@@ -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,59 @@ 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
|
+
/** Pace calls on the scope key before they are sent (default true). */
|
|
59
|
+
pacing?: boolean;
|
|
60
|
+
/**
|
|
61
|
+
* Keep the block and the retry slots of the scope key in Entity.data of
|
|
62
|
+
* the calling entity, so that parallel workers on that entity share them
|
|
63
|
+
* (default false).
|
|
64
|
+
*/
|
|
65
|
+
shared?: boolean;
|
|
66
|
+
/** Keyed by reason. Client-safe text and links for the RATE_LIMITED message. */
|
|
67
|
+
userHints?: Record<
|
|
68
|
+
string,
|
|
69
|
+
{ message?: string; links?: Array<{ label: string; url: string }> }
|
|
70
|
+
>;
|
|
71
|
+
};
|
|
72
|
+
|
|
25
73
|
export class Requester implements IFriggRequester {
|
|
74
|
+
static rateLimit?: RateLimitPolicy;
|
|
75
|
+
static requestTimeoutMs?: number;
|
|
76
|
+
|
|
26
77
|
DLGT_INVALID_AUTH: string;
|
|
78
|
+
requestTimeoutMs: number;
|
|
27
79
|
backOff: number[];
|
|
28
80
|
fetch: any;
|
|
29
81
|
isRefreshable: boolean;
|
|
@@ -87,6 +139,7 @@ declare module "@friggframework/module-plugin" {
|
|
|
87
139
|
type RequesterConstructor = {
|
|
88
140
|
backOff?: number[];
|
|
89
141
|
fetch?: any;
|
|
142
|
+
random?: () => number;
|
|
90
143
|
};
|
|
91
144
|
|
|
92
145
|
export class ApiKeyRequester
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
// SQS accepts only #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] |
|
|
2
|
-
// [#x10000-#x10FFFF] and rejects the whole message otherwise
|
|
3
|
-
// (InvalidMessageContents). JSON.stringify already escapes control characters
|
|
4
|
-
// and lone surrogates, but leaves U+FFFE and U+FFFF raw. Those can only occur
|
|
5
|
-
// inside JSON strings, so a \uXXXX escape keeps the JSON valid and JSON.parse
|
|
6
|
-
// returns the original message exactly.
|
|
7
|
-
const SQS_DISALLOWED_CHARACTERS =
|
|
8
|
-
/[^\x09\x0A\x0D\x20-\uD7FF\uE000-\uFFFD\u{10000}-\u{10FFFF}]/gu;
|
|
9
|
-
|
|
10
|
-
const toSqsMessageBody = (message) =>
|
|
11
|
-
JSON.stringify(message).replace(
|
|
12
|
-
SQS_DISALLOWED_CHARACTERS,
|
|
13
|
-
(character) =>
|
|
14
|
-
`\\u${character.charCodeAt(0).toString(16).padStart(4, '0')}`
|
|
15
|
-
);
|
|
16
|
-
|
|
17
|
-
module.exports = { toSqsMessageBody };
|