@friggframework/core 2.0.0--canary.737.83b4e17.0 → 2.0.0--canary.743.c55fe7b.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.
Files changed (67) hide show
  1. package/CLAUDE.md +173 -7
  2. package/core/CLAUDE.md +57 -1
  3. package/core/Worker.js +440 -11
  4. package/core/create-handler.js +20 -0
  5. package/core/index.js +6 -0
  6. package/core/invocation-deadline.js +91 -0
  7. package/core/invocation-scope.js +30 -24
  8. package/core/rate-limited-response.js +27 -0
  9. package/errors/fetch-error.js +10 -6
  10. package/errors/index.js +2 -0
  11. package/errors/rate-limit-error.js +46 -0
  12. package/generated/prisma-mongodb/edge.js +13 -4
  13. package/generated/prisma-mongodb/index-browser.js +10 -1
  14. package/generated/prisma-mongodb/index.d.ts +1394 -118
  15. package/generated/prisma-mongodb/index.js +13 -4
  16. package/generated/prisma-mongodb/package.json +1 -1
  17. package/generated/prisma-mongodb/schema.prisma +15 -0
  18. package/generated/prisma-mongodb/wasm.js +13 -4
  19. package/generated/prisma-postgresql/edge.js +13 -4
  20. package/generated/prisma-postgresql/index-browser.js +10 -1
  21. package/generated/prisma-postgresql/index.d.ts +1438 -100
  22. package/generated/prisma-postgresql/index.js +13 -4
  23. package/generated/prisma-postgresql/package.json +1 -1
  24. package/generated/prisma-postgresql/schema.prisma +13 -0
  25. package/generated/prisma-postgresql/wasm.js +13 -4
  26. package/handlers/app-handler-helpers.js +15 -0
  27. package/handlers/backend-utils.js +75 -1
  28. package/handlers/workers/integration-defined-workers.js +2 -0
  29. package/index.js +26 -0
  30. package/infrastructure/scheduler/eventbridge-scheduler-adapter.js +2 -0
  31. package/integrations/repositories/integration-repository-documentdb.js +59 -10
  32. package/integrations/repositories/integration-repository-interface.js +50 -7
  33. package/integrations/repositories/integration-repository-mongo.js +88 -16
  34. package/integrations/repositories/integration-repository-postgres.js +89 -16
  35. package/integrations/repositories/message-item-shared.js +17 -0
  36. package/integrations/tests/doubles/prisma-message-row.js +24 -0
  37. package/integrations/tests/doubles/test-integration-repository.js +32 -2
  38. package/integrations/use-cases/clear-rate-limit-wait.js +28 -0
  39. package/integrations/use-cases/index.js +7 -1
  40. package/integrations/use-cases/record-rate-limit-message.js +104 -0
  41. package/integrations/use-cases/record-rate-limit-wait.js +36 -0
  42. package/integrations/use-cases/update-integration-messages.js +28 -9
  43. package/logs/serialize.js +15 -0
  44. package/modules/requester/oauth-2.js +8 -0
  45. package/modules/requester/rate-limit/index.js +6 -0
  46. package/modules/requester/rate-limit/pacer.js +541 -0
  47. package/modules/requester/rate-limit/parsers.js +249 -0
  48. package/modules/requester/rate-limit/policy.js +326 -0
  49. package/modules/requester/rate-limit/state/rate-limit-state-repository-factory.js +35 -0
  50. package/modules/requester/rate-limit/state/rate-limit-state-repository-interface.js +48 -0
  51. package/modules/requester/rate-limit/state/rate-limit-state-repository-memory.js +57 -0
  52. package/modules/requester/rate-limit/state/rate-limit-state-repository-mongo.js +160 -0
  53. package/modules/requester/rate-limit/state/rate-limit-state-repository-postgres.js +59 -0
  54. package/modules/requester/requester.js +342 -11
  55. package/package.json +7 -6
  56. package/prisma-mongodb/schema.prisma +15 -0
  57. package/prisma-postgresql/migrations/20261202000000_create_rate_limit_scope/migration.sql +14 -0
  58. package/prisma-postgresql/schema.prisma +13 -0
  59. package/queues/queue-deferral.js +155 -0
  60. package/queues/queue-delivery.js +1 -0
  61. package/queues/queuer-util.js +3 -4
  62. package/types/core/consumer.fixture.ts +12 -0
  63. package/types/core/index.d.ts +84 -0
  64. package/types/errors/index.d.ts +47 -0
  65. package/types/integrations/index.d.ts +25 -4
  66. package/types/module-plugin/index.d.ts +52 -0
  67. package/queues/sqs-message-body.js +0 -17
@@ -1,6 +1,11 @@
1
1
  const { runInContext, LOGGER_SCOPE_KEY } = require('../logs/context');
2
2
  const { summarizeMessageBody } = require('../logs/summarize-event');
3
3
  const { flushSinks, hasFlushableSinks } = require('../logs/logger-runtime');
4
+ const {
5
+ deadlineFromContext,
6
+ remainingTimeMs,
7
+ runWithInvocationDeadline,
8
+ } = require('./invocation-deadline');
4
9
 
5
10
  // Bounds the tail latency telemetry adds to every warm invocation. Kept low so
6
11
  // an unreachable OTLP endpoint (e.g. a VPC Lambda with no NAT/egress) costs at
@@ -111,15 +116,6 @@ function withDeadline(ms, fn) {
111
116
  });
112
117
  }
113
118
 
114
- function remainingTimeMs(context) {
115
- try {
116
- const remaining = context?.getRemainingTimeInMillis?.();
117
- return typeof remaining === 'number' ? remaining : Infinity;
118
- } catch (_) {
119
- return Infinity;
120
- }
121
- }
122
-
123
119
  async function flushInvocation({
124
120
  telemetry,
125
121
  usageRollup,
@@ -167,22 +163,32 @@ async function runInvocationScope(fields, fn, opts = {}) {
167
163
  shouldUseDatabase = true,
168
164
  flushTimeoutMs = DEFAULT_FLUSH_TIMEOUT_MS,
169
165
  context,
166
+ maxInProcessWaitMs,
170
167
  } = opts;
171
- return runInContext({ [LOGGER_SCOPE_KEY]: { ...(fields || {}) } }, async () => {
172
- try {
173
- return await fn();
174
- } finally {
175
- // Every step is guarded, so a flush never changes the result.
176
- await flushInvocation({
177
- telemetry,
178
- usageRollup,
179
- eventSummary,
180
- shouldUseDatabase,
181
- flushTimeoutMs,
182
- context,
183
- });
184
- }
185
- });
168
+ const deadline = {
169
+ deadlineAt: deadlineFromContext(context),
170
+ maxInProcessWaitMs,
171
+ };
172
+ return runWithInvocationDeadline(deadline, () =>
173
+ runInContext(
174
+ { [LOGGER_SCOPE_KEY]: { ...(fields || {}) } },
175
+ async () => {
176
+ try {
177
+ return await fn();
178
+ } finally {
179
+ // Every step is guarded, so a flush never changes the result.
180
+ await flushInvocation({
181
+ telemetry,
182
+ usageRollup,
183
+ eventSummary,
184
+ shouldUseDatabase,
185
+ flushTimeoutMs,
186
+ context,
187
+ });
188
+ }
189
+ }
190
+ )
191
+ );
186
192
  }
187
193
 
188
194
  function toCount(value) {
@@ -0,0 +1,27 @@
1
+ const isValidDate = (value) =>
2
+ value instanceof Date && !Number.isNaN(value.getTime());
3
+
4
+ function rateLimitedResponse(error, now = Date.now()) {
5
+ if (error?.isRateLimited !== true) return null;
6
+ if (!isValidDate(error.retryAt)) {
7
+ return {
8
+ statusCode: 429,
9
+ headers: {},
10
+ body: { error: 'Too Many Requests' },
11
+ };
12
+ }
13
+ const retryAfterSeconds = Math.max(
14
+ 0,
15
+ Math.ceil((error.retryAt.getTime() - now) / 1000)
16
+ );
17
+ return {
18
+ statusCode: 429,
19
+ headers: { 'Retry-After': String(retryAfterSeconds) },
20
+ body: {
21
+ error: 'Too Many Requests',
22
+ retryAt: error.retryAt.toISOString(),
23
+ },
24
+ };
25
+ }
26
+
27
+ module.exports = { rateLimitedResponse };
@@ -66,12 +66,16 @@ class FetchError extends BaseError {
66
66
 
67
67
  static async create(options = {}) {
68
68
  const { response } = options;
69
- let responseBody =
70
- response && !response.bodyUsed && typeof response.text === 'function'
71
- ? await response.text()
72
- : null;
73
- if (!responseBody) responseBody = options.responseBody ?? options.body;
74
- return new FetchError({ ...options, responseBody });
69
+ let { responseBody } = options;
70
+ if (responseBody === undefined || responseBody === null) {
71
+ const canRead =
72
+ response &&
73
+ !response.bodyUsed &&
74
+ typeof response.text === 'function';
75
+ responseBody = (canRead && (await response.text())) || options.body;
76
+ }
77
+ const ErrorClass = typeof this === 'function' ? this : FetchError;
78
+ return new ErrorClass({ ...options, responseBody });
75
79
  }
76
80
  }
77
81
 
package/errors/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  const { BaseError } = require('./base-error');
2
2
  const { FetchError } = require('./fetch-error');
3
3
  const { HaltError } = require('./halt-error');
4
+ const { RateLimitError } = require('./rate-limit-error');
4
5
  const {
5
6
  RequiredPropertyError,
6
7
  ParameterTypeError,
@@ -14,6 +15,7 @@ module.exports = {
14
15
  BaseError,
15
16
  FetchError,
16
17
  HaltError,
18
+ RateLimitError,
17
19
  RequiredPropertyError,
18
20
  ParameterTypeError,
19
21
  ClientSafeError,
@@ -0,0 +1,46 @@
1
+ const { FetchError } = require('./fetch-error');
2
+
3
+ function resolveTiming({ hint, waitMs, now }) {
4
+ if (waitMs !== undefined) {
5
+ return { waitMs, retryAt: new Date(now + waitMs) };
6
+ }
7
+ if (hint?.retryAt) {
8
+ const retryAt = new Date(hint.retryAt);
9
+ if (!Number.isNaN(retryAt.getTime())) {
10
+ return { waitMs: Math.max(0, retryAt.getTime() - now), retryAt };
11
+ }
12
+ }
13
+ if (hint?.waitMs !== undefined) {
14
+ return {
15
+ waitMs: hint.waitMs,
16
+ retryAt: new Date(now + hint.waitMs),
17
+ };
18
+ }
19
+ return { waitMs: undefined, retryAt: undefined };
20
+ }
21
+
22
+ class RateLimitError extends FetchError {
23
+ constructor({
24
+ hint,
25
+ waitMs,
26
+ module,
27
+ scopeKey,
28
+ now = Date.now(),
29
+ ...fetchErrorArgs
30
+ } = {}) {
31
+ super(fetchErrorArgs);
32
+
33
+ const timing = resolveTiming({ hint, waitMs, now });
34
+ this.statusCode ??= fetchErrorArgs.cause?.statusCode;
35
+ this.isRateLimited = true;
36
+ this.waitMs = timing.waitMs;
37
+ this.retryAt = timing.retryAt;
38
+ this.reason = hint?.reason ?? 'unknown';
39
+ this.policy = hint?.policy;
40
+ this.source = hint?.source ?? 'unknown';
41
+ this.module = module;
42
+ this.scopeKey = scopeKey;
43
+ }
44
+ }
45
+
46
+ module.exports = { RateLimitError };