@apifuse/provider-sdk 2.2.0-beta.5 → 2.2.0-beta.50
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/AUTHORING.md +537 -33
- package/CHANGELOG.md +201 -1
- package/README.md +83 -22
- package/SUBMISSION.md +2 -2
- package/bin/apifuse-check.ts +165 -11
- package/bin/apifuse-dev.ts +57 -13
- package/bin/apifuse-migrate-operation-declaration.ts +55 -0
- package/bin/apifuse-migrate-shape.ts +202 -0
- package/bin/apifuse-pack-check.ts +22 -2
- package/bin/apifuse-pack-smoke.ts +57 -2
- package/bin/apifuse-pack-types.ts +357 -38
- package/bin/apifuse-perf.ts +14 -13
- package/bin/apifuse-record.ts +709 -72
- package/bin/apifuse-submit-check.ts +2344 -324
- package/bin/apifuse-sync-assets.ts +117 -0
- package/bin/submit-check-delimited-text.ts +50 -0
- package/dist/auth-turn/index.d.ts +3 -3
- package/dist/auth-turn/index.js +1 -1
- package/dist/auth.d.ts +14 -0
- package/dist/auth.js +67 -0
- package/dist/ceremonies/index.d.ts +16 -0
- package/dist/ceremonies/index.js +141 -36
- package/dist/cli/commands.d.ts +1 -1
- package/dist/cli/commands.js +27 -0
- package/dist/cli/create.d.ts +4 -1
- package/dist/cli/create.js +42 -37
- package/dist/cli/migrate-operation-declaration.d.ts +59 -0
- package/dist/cli/migrate-operation-declaration.js +1178 -0
- package/dist/cli/migrate-operation-shape.d.ts +44 -0
- package/dist/cli/migrate-operation-shape.js +113 -0
- package/dist/cli/migrate-provider-shape.d.ts +52 -0
- package/dist/cli/migrate-provider-shape.js +578 -0
- package/dist/cli/prompt-assets.d.ts +80 -0
- package/dist/cli/prompt-assets.js +743 -0
- package/dist/cli/templates/provider/AGENTS.md.tpl +17 -8
- package/dist/cli/templates/provider/Dockerfile.tpl +1 -1
- package/dist/cli/templates/provider/README.md.tpl +5 -5
- package/dist/cli/templates/provider/index.ts.tpl +6 -3
- package/dist/cli/templates/provider/operations/ping.ts.tpl +4 -1
- package/dist/cli/templates/provider/provider.json.tpl +6 -0
- package/dist/config/loader.d.ts +179 -16
- package/dist/config/loader.js +435 -127
- package/dist/contract-serialization.js +4 -8
- package/dist/contract-types.d.ts +12 -5
- package/dist/contract.js +24 -10
- package/dist/declaration-validation.d.ts +33 -0
- package/dist/declaration-validation.js +266 -0
- package/dist/define.d.ts +66 -29
- package/dist/define.js +778 -69
- package/dist/dev.d.ts +3 -0
- package/dist/dev.js +1 -1
- package/dist/engine.d.ts +78 -0
- package/dist/engine.js +133 -0
- package/dist/error-observability.d.ts +7 -0
- package/dist/error-observability.js +61 -0
- package/dist/error-resolution.d.ts +4 -0
- package/dist/error-resolution.js +122 -0
- package/dist/errors.d.ts +33 -0
- package/dist/errors.js +40 -0
- package/dist/fixture-sanitization.d.ts +28 -0
- package/dist/fixture-sanitization.js +227 -0
- package/dist/health-scenario.d.ts +1842 -0
- package/dist/health-scenario.js +624 -0
- package/dist/index.d.ts +21 -10
- package/dist/index.js +12 -7
- package/dist/lint.d.ts +23 -11
- package/dist/lint.js +572 -69
- package/dist/native-address.d.ts +43 -0
- package/dist/native-address.js +281 -0
- package/dist/native-egress-policy.d.ts +31 -0
- package/dist/native-egress-policy.js +288 -0
- package/dist/observability.d.ts +5 -2
- package/dist/observability.js +48 -1
- package/dist/provider.d.ts +10 -2
- package/dist/provider.js +4 -1
- package/dist/runtime/auth-flow.d.ts +5 -1
- package/dist/runtime/auth-flow.js +6 -0
- package/dist/runtime/browser.d.ts +1 -0
- package/dist/runtime/browser.js +492 -49
- package/dist/runtime/cache.d.ts +1 -0
- package/dist/runtime/cache.js +169 -15
- package/dist/runtime/choice-wordlist.d.ts +9 -0
- package/dist/runtime/choice-wordlist.js +138 -0
- package/dist/runtime/choice.d.ts +13 -1
- package/dist/runtime/choice.js +490 -102
- package/dist/runtime/chrome149-header-order.d.ts +58 -0
- package/dist/runtime/chrome149-header-order.js +289 -0
- package/dist/runtime/env.js +12 -0
- package/dist/runtime/executor.d.ts +3 -2
- package/dist/runtime/executor.js +26 -25
- package/dist/runtime/http.d.ts +1 -0
- package/dist/runtime/http.js +515 -53
- package/dist/runtime/insights.js +2 -2
- package/dist/runtime/instrumentation.d.ts +2 -2
- package/dist/runtime/instrumentation.js +366 -8
- package/dist/runtime/native-network-errors.d.ts +33 -0
- package/dist/runtime/native-network-errors.js +69 -0
- package/dist/runtime/native-network.d.ts +96 -0
- package/dist/runtime/native-network.js +1232 -0
- package/dist/runtime/ocr.d.ts +29 -0
- package/dist/runtime/ocr.js +440 -0
- package/dist/runtime/otlp.d.ts +71 -2
- package/dist/runtime/otlp.js +397 -16
- package/dist/runtime/proxy-errors.js +6 -2
- package/dist/runtime/proxy-nodemaven.d.ts +56 -0
- package/dist/runtime/proxy-nodemaven.js +146 -0
- package/dist/runtime/proxy-telemetry.d.ts +80 -1
- package/dist/runtime/proxy-telemetry.js +154 -47
- package/dist/runtime/redirects.d.ts +29 -0
- package/dist/runtime/redirects.js +36 -0
- package/dist/runtime/redis.d.ts +1 -1
- package/dist/runtime/redis.js +4 -2
- package/dist/runtime/request-options.d.ts +68 -1
- package/dist/runtime/request-options.js +548 -0
- package/dist/runtime/resolver-config.d.ts +6 -0
- package/dist/runtime/resolver-config.js +6 -0
- package/dist/runtime/resolver-public.d.ts +1 -0
- package/dist/runtime/resolver-public.js +1 -0
- package/dist/runtime/resolver-shared.d.ts +14 -0
- package/dist/runtime/resolver-shared.js +12 -0
- package/dist/runtime/resolver-vendors/bindings.d.ts +48 -0
- package/dist/runtime/resolver-vendors/bindings.js +40 -0
- package/dist/runtime/resolver-vendors/browser.d.ts +22 -0
- package/dist/runtime/resolver-vendors/browser.js +377 -0
- package/dist/runtime/resolver-vendors/capsolver.d.ts +22 -0
- package/dist/runtime/resolver-vendors/capsolver.js +531 -0
- package/dist/runtime/resolver-vendors/hosts.d.ts +2 -0
- package/dist/runtime/resolver-vendors/hosts.js +33 -0
- package/dist/runtime/resolver-vendors/twocaptcha.d.ts +24 -0
- package/dist/runtime/resolver-vendors/twocaptcha.js +408 -0
- package/dist/runtime/resolver-vendors/types.d.ts +94 -0
- package/dist/runtime/resolver-vendors/types.js +96 -0
- package/dist/runtime/resolver.d.ts +72 -0
- package/dist/runtime/resolver.js +771 -0
- package/dist/runtime/secrets.d.ts +27 -0
- package/dist/runtime/secrets.js +51 -0
- package/dist/runtime/state.d.ts +3 -0
- package/dist/runtime/state.js +277 -71
- package/dist/runtime/stealth-cookies.d.ts +20 -0
- package/dist/runtime/stealth-cookies.js +111 -0
- package/dist/runtime/stealth.d.ts +41 -5
- package/dist/runtime/stealth.js +997 -291
- package/dist/runtime/stt.js +1 -12
- package/dist/runtime/timeout.d.ts +5 -0
- package/dist/runtime/timeout.js +12 -0
- package/dist/runtime/trace-config.d.ts +12 -0
- package/dist/runtime/trace-config.js +62 -0
- package/dist/runtime/trace.d.ts +5 -0
- package/dist/runtime/trace.js +43 -10
- package/dist/serve.d.ts +1 -1
- package/dist/serve.js +1 -1
- package/dist/server/error-observability.d.ts +1 -0
- package/dist/server/error-observability.js +1 -0
- package/dist/server/index.d.ts +5 -2
- package/dist/server/index.js +2 -2
- package/dist/server/self-test-input-tokens.d.ts +2 -1
- package/dist/server/self-test-input-tokens.js +18 -14
- package/dist/server/self-test.d.ts +14 -3
- package/dist/server/self-test.js +129 -58
- package/dist/server/serve-implementation.d.ts +230 -0
- package/dist/server/serve-implementation.js +2265 -0
- package/dist/server/serve.d.ts +1 -70
- package/dist/server/serve.js +1 -1131
- package/dist/server/trace-output.d.ts +6 -0
- package/dist/server/trace-output.js +79 -0
- package/dist/server/types.d.ts +30 -5
- package/dist/server/types.js +13 -1
- package/dist/stateful/errors.d.ts +19 -0
- package/dist/stateful/errors.js +24 -0
- package/dist/stateful/http-provider-event-emitter.d.ts +40 -0
- package/dist/stateful/http-provider-event-emitter.js +237 -0
- package/dist/stateful/http-session-owner-registry.d.ts +44 -0
- package/dist/stateful/http-session-owner-registry.js +210 -0
- package/dist/stateful/index.d.ts +18 -0
- package/dist/stateful/index.js +18 -0
- package/dist/stateful/provider-event-delivery-failures.d.ts +32 -0
- package/dist/stateful/provider-event-delivery-failures.js +43 -0
- package/dist/stateful/provider-event-pipeline-metrics.d.ts +46 -0
- package/dist/stateful/provider-event-pipeline-metrics.js +48 -0
- package/dist/stateful/provider-event-pipeline.d.ts +50 -0
- package/dist/stateful/provider-event-pipeline.js +1 -0
- package/dist/stateful/provider-events.d.ts +101 -0
- package/dist/stateful/provider-events.js +289 -0
- package/dist/stateful/session-key.d.ts +15 -0
- package/dist/stateful/session-key.js +86 -0
- package/dist/stateful/stateful-provider-adapter-context.d.ts +5 -0
- package/dist/stateful/stateful-provider-adapter-context.js +42 -0
- package/dist/stateful/stateful-provider-adapter-metrics.d.ts +15 -0
- package/dist/stateful/stateful-provider-adapter-metrics.js +21 -0
- package/dist/stateful/stateful-provider-adapter.d.ts +98 -0
- package/dist/stateful/stateful-provider-adapter.js +287 -0
- package/dist/stateful/stateful-provider-observability.d.ts +62 -0
- package/dist/stateful/stateful-provider-observability.js +161 -0
- package/dist/stateful/stateful-provider-owner-forwarder.d.ts +41 -0
- package/dist/stateful/stateful-provider-owner-forwarder.js +215 -0
- package/dist/stateful/stateful-provider-runtime-context.d.ts +32 -0
- package/dist/stateful/stateful-provider-runtime-context.js +60 -0
- package/dist/stateful/stateful-provider-runtime-executor.d.ts +34 -0
- package/dist/stateful/stateful-provider-runtime-executor.js +52 -0
- package/dist/stateful/stateful-provider-session-routing.d.ts +67 -0
- package/dist/stateful/stateful-provider-session-routing.js +345 -0
- package/dist/stateful/stateful-provider-session-runtime.d.ts +98 -0
- package/dist/stateful/stateful-provider-session-runtime.js +245 -0
- package/dist/stateful-signing.d.ts +18 -0
- package/dist/stateful-signing.js +27 -0
- package/dist/stealth/profiles.d.ts +9 -4
- package/dist/stealth/profiles.js +160 -211
- package/dist/stream-evidence.d.ts +74 -0
- package/dist/stream-evidence.js +785 -0
- package/dist/stream.js +7 -1
- package/dist/testing/index.d.ts +2 -1
- package/dist/testing/index.js +2 -1
- package/dist/testing/run.d.ts +32 -2
- package/dist/testing/run.js +489 -21
- package/dist/trace-sanitization.d.ts +5 -0
- package/dist/trace-sanitization.js +45 -0
- package/dist/types.d.ts +673 -131
- package/dist/types.js +1 -0
- package/package.json +45 -5
- package/src/auth-turn/index.ts +1 -1
- package/src/auth.ts +118 -0
- package/src/ceremonies/index.ts +189 -46
- package/src/cli/__tests__/fixtures/migrate-operation-declaration/approval-override.ts.txt +6 -0
- package/src/cli/__tests__/fixtures/migrate-operation-declaration/codemod-syntax.ts.txt +3 -0
- package/src/cli/__tests__/fixtures/migrate-operation-declaration/connection-precedence.ts.txt +10 -0
- package/src/cli/__tests__/fixtures/migrate-operation-declaration/docs-conflict.ts.txt +8 -0
- package/src/cli/__tests__/fixtures/migrate-operation-declaration/examples-map.ts.txt +5 -0
- package/src/cli/__tests__/fixtures/migrate-operation-declaration/examples-operation.ts.txt +16 -0
- package/src/cli/__tests__/fixtures/migrate-operation-declaration/factory-map.ts.txt +3 -0
- package/src/cli/__tests__/fixtures/migrate-operation-declaration/hoist-all.ts.txt +31 -0
- package/src/cli/__tests__/fixtures/migrate-operation-declaration/hoisted-const.ts.txt +11 -0
- package/src/cli/__tests__/fixtures/migrate-operation-declaration/imported-spread.ts.txt +11 -0
- package/src/cli/__tests__/fixtures/migrate-operation-declaration/inline-map.ts.txt +11 -0
- package/src/cli/__tests__/fixtures/migrate-operation-declaration/inline-spread-cast-tail.ts.txt +21 -0
- package/src/cli/__tests__/fixtures/migrate-operation-declaration/inline-spread-ekitan.ts.txt +11 -0
- package/src/cli/__tests__/fixtures/migrate-operation-declaration/inline-spread-override.ts.txt +14 -0
- package/src/cli/__tests__/fixtures/migrate-operation-declaration/missing-english-locale.ts.txt +7 -0
- package/src/cli/__tests__/fixtures/migrate-operation-declaration/no-safety.ts.txt +6 -0
- package/src/cli/__tests__/fixtures/migrate-operation-declaration/non-literal.ts.txt +7 -0
- package/src/cli/__tests__/fixtures/migrate-operation-declaration/redundant-approval.ts.txt +6 -0
- package/src/cli/__tests__/fixtures/migrate-operation-declaration/safety-conflict.ts.txt +7 -0
- package/src/cli/__tests__/fixtures/migrate-operation-declaration/stream.ts.txt +7 -0
- package/src/cli/__tests__/fixtures/migrate-operation-declaration/tool-router-spread.ts.txt +15 -0
- package/src/cli/__tests__/fixtures/migrate-operation-declaration/unparseable.ts.txt +4 -0
- package/src/cli/__tests__/fixtures/migrate-operation-declaration/verbatim-template.ts.txt +12 -0
- package/src/cli/commands.ts +33 -0
- package/src/cli/create.ts +50 -37
- package/src/cli/migrate-operation-declaration.ts +1654 -0
- package/src/cli/migrate-operation-shape.ts +184 -0
- package/src/cli/migrate-provider-shape.ts +772 -0
- package/src/cli/prompt-assets.ts +865 -0
- package/src/cli/templates/provider/AGENTS.md.tpl +17 -8
- package/src/cli/templates/provider/Dockerfile.tpl +1 -1
- package/src/cli/templates/provider/README.md.tpl +5 -5
- package/src/cli/templates/provider/index.ts.tpl +6 -3
- package/src/cli/templates/provider/operations/ping.ts.tpl +4 -1
- package/src/cli/templates/provider/provider.json.tpl +6 -0
- package/src/config/loader.ts +690 -163
- package/src/contract-serialization.ts +5 -7
- package/src/contract-types.ts +12 -5
- package/src/contract.ts +24 -10
- package/src/declaration-validation.ts +330 -0
- package/src/define.ts +1048 -131
- package/src/dev.ts +4 -1
- package/src/engine.ts +279 -0
- package/src/error-observability.ts +64 -0
- package/src/error-resolution.ts +127 -0
- package/src/errors.ts +68 -0
- package/src/fixture-sanitization.ts +264 -0
- package/src/health-scenario.ts +875 -0
- package/src/index.ts +220 -13
- package/src/lint.ts +698 -90
- package/src/native-address.ts +340 -0
- package/src/native-egress-policy.ts +358 -0
- package/src/observability.ts +51 -1
- package/src/provider.ts +161 -3
- package/src/runtime/auth-flow.ts +12 -0
- package/src/runtime/browser.ts +661 -63
- package/src/runtime/cache.ts +189 -14
- package/src/runtime/choice-wordlist.ts +145 -0
- package/src/runtime/choice.ts +631 -120
- package/src/runtime/chrome149-header-order.ts +330 -0
- package/src/runtime/env.ts +13 -0
- package/src/runtime/executor.ts +43 -31
- package/src/runtime/http.ts +641 -61
- package/src/runtime/insights.ts +2 -2
- package/src/runtime/instrumentation.ts +520 -15
- package/src/runtime/native-network-errors.ts +99 -0
- package/src/runtime/native-network.ts +1605 -0
- package/src/runtime/ocr.ts +523 -0
- package/src/runtime/otlp.ts +467 -21
- package/src/runtime/proxy-errors.ts +12 -4
- package/src/runtime/proxy-nodemaven.ts +221 -0
- package/src/runtime/proxy-telemetry.ts +244 -75
- package/src/runtime/redirects.ts +66 -0
- package/src/runtime/redis.ts +7 -2
- package/src/runtime/request-options.ts +680 -1
- package/src/runtime/resolver-config.ts +6 -0
- package/src/runtime/resolver-public.ts +23 -0
- package/src/runtime/resolver-shared.ts +33 -0
- package/src/runtime/resolver-vendors/bindings.ts +56 -0
- package/src/runtime/resolver-vendors/browser.ts +533 -0
- package/src/runtime/resolver-vendors/capsolver.ts +707 -0
- package/src/runtime/resolver-vendors/hosts.ts +38 -0
- package/src/runtime/resolver-vendors/twocaptcha.ts +540 -0
- package/src/runtime/resolver-vendors/types.ts +212 -0
- package/src/runtime/resolver.ts +1157 -0
- package/src/runtime/secrets.ts +64 -0
- package/src/runtime/state.ts +394 -77
- package/src/runtime/stealth-cookies.ts +132 -0
- package/src/runtime/stealth.ts +1280 -336
- package/src/runtime/stt.ts +1 -19
- package/src/runtime/timeout.ts +18 -0
- package/src/runtime/trace-config.ts +78 -0
- package/src/runtime/trace.ts +57 -17
- package/src/serve.ts +6 -1
- package/src/server/error-observability.ts +1 -0
- package/src/server/index.ts +34 -2
- package/src/server/self-test-input-tokens.ts +29 -14
- package/src/server/self-test.ts +191 -68
- package/src/server/serve-implementation.ts +3378 -0
- package/src/server/serve.ts +1 -1632
- package/src/server/trace-output.ts +129 -0
- package/src/server/types.ts +13 -1
- package/src/stateful/README.md +146 -0
- package/src/stateful/errors.ts +35 -0
- package/src/stateful/http-provider-event-emitter.ts +314 -0
- package/src/stateful/http-session-owner-registry.ts +306 -0
- package/src/stateful/index.ts +18 -0
- package/src/stateful/provider-event-delivery-failures.ts +80 -0
- package/src/stateful/provider-event-pipeline-metrics.ts +95 -0
- package/src/stateful/provider-event-pipeline.ts +61 -0
- package/src/stateful/provider-events.ts +462 -0
- package/src/stateful/session-key.ts +111 -0
- package/src/stateful/stateful-provider-adapter-context.ts +59 -0
- package/src/stateful/stateful-provider-adapter-metrics.ts +48 -0
- package/src/stateful/stateful-provider-adapter.ts +562 -0
- package/src/stateful/stateful-provider-observability.ts +261 -0
- package/src/stateful/stateful-provider-owner-forwarder.ts +287 -0
- package/src/stateful/stateful-provider-runtime-context.ts +92 -0
- package/src/stateful/stateful-provider-runtime-executor.ts +96 -0
- package/src/stateful/stateful-provider-session-routing.ts +546 -0
- package/src/stateful/stateful-provider-session-runtime.ts +403 -0
- package/src/stateful-signing.ts +46 -0
- package/src/stealth/profiles.ts +202 -231
- package/src/stream-evidence.ts +988 -0
- package/src/stream.ts +8 -1
- package/src/testing/index.ts +10 -1
- package/src/testing/run.ts +658 -15
- package/src/trace-sanitization.ts +63 -0
- package/src/types.ts +788 -174
- package/dist/cli/templates/provider/CLAUDE.md.tpl +0 -1
- package/src/cli/templates/provider/CLAUDE.md.tpl +0 -1
- /package/dist/cli/templates/provider/{skills → .agents/skills}/fixtures-and-recording/SKILL.md.tpl +0 -0
- /package/dist/cli/templates/provider/{skills → .agents/skills}/health-checks-and-fail-closed/SKILL.md.tpl +0 -0
- /package/dist/cli/templates/provider/{skills → .agents/skills}/normalization-standards/SKILL.md.tpl +0 -0
- /package/dist/cli/templates/provider/{skills → .agents/skills}/pagination-and-counts/SKILL.md.tpl +0 -0
- /package/dist/cli/templates/provider/{skills → .agents/skills}/upstream-contract-verification/SKILL.md.tpl +0 -0
- /package/dist/cli/templates/provider/{skills → .agents/skills}/upstream-notes/README.md.tpl +0 -0
- /package/src/cli/templates/provider/{skills → .agents/skills}/fixtures-and-recording/SKILL.md.tpl +0 -0
- /package/src/cli/templates/provider/{skills → .agents/skills}/health-checks-and-fail-closed/SKILL.md.tpl +0 -0
- /package/src/cli/templates/provider/{skills → .agents/skills}/normalization-standards/SKILL.md.tpl +0 -0
- /package/src/cli/templates/provider/{skills → .agents/skills}/pagination-and-counts/SKILL.md.tpl +0 -0
- /package/src/cli/templates/provider/{skills → .agents/skills}/upstream-contract-verification/SKILL.md.tpl +0 -0
- /package/src/cli/templates/provider/{skills → .agents/skills}/upstream-notes/README.md.tpl +0 -0
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
const DEFAULT_MAX_PAYLOAD_BYTES = 256 * 1024;
|
|
3
|
+
const REDACTED_VALUE = "[REDACTED]";
|
|
4
|
+
const CREDENTIAL_KEY_PATTERN = /(?:token|password|secret|credential|cookie|authorization)/i;
|
|
5
|
+
export class ProviderEventValidationError extends Error {
|
|
6
|
+
constructor(message) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.name = "ProviderEventValidationError";
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export function buildProviderEvent(input) {
|
|
12
|
+
validatePayload(input.payload, input.maxPayloadBytes ?? DEFAULT_MAX_PAYLOAD_BYTES);
|
|
13
|
+
const observedAt = input.observedAt ?? input.clock?.().toISOString() ?? new Date().toISOString();
|
|
14
|
+
const payload = input.shouldRedactPayload === false
|
|
15
|
+
? input.payload
|
|
16
|
+
: redactProviderEventPayload(input.payload, input.redactionPatterns);
|
|
17
|
+
const event = {
|
|
18
|
+
eventId: input.eventId ?? buildEventId({ ...input, observedAt }),
|
|
19
|
+
providerId: input.providerId,
|
|
20
|
+
eventType: input.eventType,
|
|
21
|
+
observedAt,
|
|
22
|
+
payload,
|
|
23
|
+
session: input.session,
|
|
24
|
+
...(input.connectionId ? { connectionId: input.connectionId } : {}),
|
|
25
|
+
...(input.serviceAccountId ? { serviceAccountId: input.serviceAccountId } : {}),
|
|
26
|
+
...(input.subject ? { subject: input.subject } : {}),
|
|
27
|
+
...(input.occurredAt ? { occurredAt: input.occurredAt } : {}),
|
|
28
|
+
...(input.providerCursor ? { providerCursor: input.providerCursor } : {}),
|
|
29
|
+
...(input.dedupeKey ? { dedupeKey: input.dedupeKey } : {}),
|
|
30
|
+
...(input.rawRef ? { rawRef: input.rawRef } : {}),
|
|
31
|
+
};
|
|
32
|
+
validateProviderEvent(event, input.maxPayloadBytes);
|
|
33
|
+
return event;
|
|
34
|
+
}
|
|
35
|
+
export function redactProviderEventPayload(payload, extraPatterns = []) {
|
|
36
|
+
const cloned = structuredClone(payload);
|
|
37
|
+
redactValueInPlace(cloned, [CREDENTIAL_KEY_PATTERN, ...extraPatterns]);
|
|
38
|
+
return cloned;
|
|
39
|
+
}
|
|
40
|
+
export function isStaleProviderEvent(event, currentGeneration) {
|
|
41
|
+
return event.session.generation < currentGeneration;
|
|
42
|
+
}
|
|
43
|
+
export class InMemoryProviderEventLog {
|
|
44
|
+
#events = [];
|
|
45
|
+
#eventIds = new Set();
|
|
46
|
+
#dedupeKeys = new Set();
|
|
47
|
+
async append(event) {
|
|
48
|
+
validateProviderEvent(event);
|
|
49
|
+
if (this.#eventIds.has(event.eventId)) {
|
|
50
|
+
return { event, appended: false };
|
|
51
|
+
}
|
|
52
|
+
const dedupeTuple = event.dedupeKey ? buildDedupeTuple(event) : undefined;
|
|
53
|
+
if (dedupeTuple && this.#dedupeKeys.has(dedupeTuple)) {
|
|
54
|
+
return { event, appended: false };
|
|
55
|
+
}
|
|
56
|
+
this.#events.push(event);
|
|
57
|
+
this.#eventIds.add(event.eventId);
|
|
58
|
+
if (dedupeTuple)
|
|
59
|
+
this.#dedupeKeys.add(dedupeTuple);
|
|
60
|
+
return { event, appended: true };
|
|
61
|
+
}
|
|
62
|
+
async get(eventId) {
|
|
63
|
+
validateRequiredString("eventId", eventId);
|
|
64
|
+
return this.#events.find((event) => event.eventId === eventId) ?? null;
|
|
65
|
+
}
|
|
66
|
+
async list(options = {}) {
|
|
67
|
+
validateListOptions(options);
|
|
68
|
+
let events = this.#events;
|
|
69
|
+
if (options.providerId) {
|
|
70
|
+
events = events.filter((event) => event.providerId === options.providerId);
|
|
71
|
+
}
|
|
72
|
+
if (options.connectionId) {
|
|
73
|
+
events = events.filter((event) => event.connectionId === options.connectionId);
|
|
74
|
+
}
|
|
75
|
+
if (options.eventType) {
|
|
76
|
+
events = events.filter((event) => event.eventType === options.eventType);
|
|
77
|
+
}
|
|
78
|
+
if (options.subjectKind) {
|
|
79
|
+
events = events.filter((event) => event.subject?.kind === options.subjectKind);
|
|
80
|
+
}
|
|
81
|
+
if (options.subjectId) {
|
|
82
|
+
events = events.filter((event) => event.subject?.id === options.subjectId);
|
|
83
|
+
}
|
|
84
|
+
if (options.afterObservedAt) {
|
|
85
|
+
const afterMs = Date.parse(options.afterObservedAt);
|
|
86
|
+
events = events.filter((event) => Date.parse(event.observedAt) > afterMs);
|
|
87
|
+
}
|
|
88
|
+
return typeof options.limit === "number" ? events.slice(0, options.limit) : [...events];
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
export class ProviderEventEmitter {
|
|
92
|
+
#log;
|
|
93
|
+
#defaults;
|
|
94
|
+
constructor(log, defaults) {
|
|
95
|
+
validateRequiredString("providerId", defaults.providerId);
|
|
96
|
+
if (defaults.connectionId !== undefined) {
|
|
97
|
+
validateRequiredString("connectionId", defaults.connectionId);
|
|
98
|
+
}
|
|
99
|
+
if (defaults.serviceAccountId !== undefined) {
|
|
100
|
+
validateRequiredString("serviceAccountId", defaults.serviceAccountId);
|
|
101
|
+
}
|
|
102
|
+
validateSession(defaults.session);
|
|
103
|
+
this.#log = log;
|
|
104
|
+
this.#defaults = defaults;
|
|
105
|
+
}
|
|
106
|
+
emit(input) {
|
|
107
|
+
const event = buildProviderEvent({
|
|
108
|
+
...input,
|
|
109
|
+
providerId: input.providerId ?? this.#defaults.providerId,
|
|
110
|
+
connectionId: input.connectionId ?? this.#defaults.connectionId,
|
|
111
|
+
serviceAccountId: input.serviceAccountId ?? this.#defaults.serviceAccountId,
|
|
112
|
+
session: input.session ?? this.#defaults.session,
|
|
113
|
+
clock: input.clock ?? this.#defaults.clock,
|
|
114
|
+
idFactory: input.idFactory ?? this.#defaults.idFactory,
|
|
115
|
+
maxPayloadBytes: input.maxPayloadBytes ?? this.#defaults.maxPayloadBytes,
|
|
116
|
+
shouldRedactPayload: input.shouldRedactPayload ?? this.#defaults.shouldRedactPayload,
|
|
117
|
+
redactionPatterns: input.redactionPatterns ?? this.#defaults.redactionPatterns,
|
|
118
|
+
});
|
|
119
|
+
return this.#log.append(event);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
function buildEventId(input) {
|
|
123
|
+
if (input.dedupeKey) {
|
|
124
|
+
return deterministicProviderEventId([
|
|
125
|
+
input.providerId,
|
|
126
|
+
input.connectionId ?? "",
|
|
127
|
+
input.eventType,
|
|
128
|
+
input.dedupeKey,
|
|
129
|
+
]);
|
|
130
|
+
}
|
|
131
|
+
return (input.idFactory?.() ??
|
|
132
|
+
deterministicProviderEventId([
|
|
133
|
+
input.providerId,
|
|
134
|
+
input.connectionId ?? "",
|
|
135
|
+
input.eventType,
|
|
136
|
+
input.subject?.kind ?? "",
|
|
137
|
+
input.subject?.id ?? "",
|
|
138
|
+
input.observedAt,
|
|
139
|
+
]));
|
|
140
|
+
}
|
|
141
|
+
function deterministicProviderEventId(parts) {
|
|
142
|
+
return `pevt_${createHash("sha256").update(parts.join("\u001f")).digest("hex")}`;
|
|
143
|
+
}
|
|
144
|
+
function validateProviderEvent(event, maxPayloadBytes = DEFAULT_MAX_PAYLOAD_BYTES) {
|
|
145
|
+
validateRequiredString("eventId", event.eventId);
|
|
146
|
+
validateRequiredString("providerId", event.providerId);
|
|
147
|
+
if (event.connectionId !== undefined)
|
|
148
|
+
validateRequiredString("connectionId", event.connectionId);
|
|
149
|
+
if (event.serviceAccountId !== undefined) {
|
|
150
|
+
validateRequiredString("serviceAccountId", event.serviceAccountId);
|
|
151
|
+
}
|
|
152
|
+
validateRequiredString("eventType", event.eventType);
|
|
153
|
+
if (event.subject) {
|
|
154
|
+
validateRequiredString("subject.kind", event.subject.kind);
|
|
155
|
+
validateRequiredString("subject.id", event.subject.id);
|
|
156
|
+
}
|
|
157
|
+
if (event.occurredAt !== undefined) {
|
|
158
|
+
validateRequiredString("occurredAt", event.occurredAt);
|
|
159
|
+
validateTimestamp("occurredAt", event.occurredAt);
|
|
160
|
+
}
|
|
161
|
+
validateRequiredString("observedAt", event.observedAt);
|
|
162
|
+
validateTimestamp("observedAt", event.observedAt);
|
|
163
|
+
validateSession(event.session);
|
|
164
|
+
if (event.providerCursor !== undefined) {
|
|
165
|
+
validateRequiredString("providerCursor", event.providerCursor);
|
|
166
|
+
}
|
|
167
|
+
if (event.dedupeKey !== undefined) {
|
|
168
|
+
validateRequiredString("dedupeKey", event.dedupeKey);
|
|
169
|
+
}
|
|
170
|
+
if (event.rawRef !== undefined) {
|
|
171
|
+
validateRequiredString("rawRef", event.rawRef);
|
|
172
|
+
}
|
|
173
|
+
validatePayload(event.payload, maxPayloadBytes);
|
|
174
|
+
}
|
|
175
|
+
function validateSession(session) {
|
|
176
|
+
validateRequiredString("session.sessionKey", session?.sessionKey);
|
|
177
|
+
if (!Number.isInteger(session?.generation) || session.generation < 0) {
|
|
178
|
+
throw new ProviderEventValidationError("ProviderEvent session.generation must be a non-negative integer.");
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
function validateRequiredString(name, value) {
|
|
182
|
+
if (typeof value !== "string" || value.trim().length === 0) {
|
|
183
|
+
throw new ProviderEventValidationError(`ProviderEvent ${name} must be a non-empty string.`);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
function validateTimestamp(name, value) {
|
|
187
|
+
if (!Number.isFinite(Date.parse(value))) {
|
|
188
|
+
throw new ProviderEventValidationError(`ProviderEvent ${name} must be a parseable ISO timestamp string.`);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
function validatePayload(payload, maxPayloadBytes) {
|
|
192
|
+
if (typeof maxPayloadBytes !== "number" ||
|
|
193
|
+
!Number.isFinite(maxPayloadBytes) ||
|
|
194
|
+
maxPayloadBytes <= 0) {
|
|
195
|
+
throw new ProviderEventValidationError("ProviderEvent maxPayloadBytes must be a positive finite number.");
|
|
196
|
+
}
|
|
197
|
+
if (!isPlainRecord(payload)) {
|
|
198
|
+
throw new ProviderEventValidationError("ProviderEvent payload must be a JSON-serializable object.");
|
|
199
|
+
}
|
|
200
|
+
assertJsonValue(payload, "payload");
|
|
201
|
+
const encoded = JSON.stringify(payload);
|
|
202
|
+
if (encoded === undefined) {
|
|
203
|
+
throw new ProviderEventValidationError("ProviderEvent payload must be JSON-serializable.");
|
|
204
|
+
}
|
|
205
|
+
const payloadBytes = Buffer.byteLength(encoded, "utf8");
|
|
206
|
+
if (payloadBytes > maxPayloadBytes) {
|
|
207
|
+
throw new ProviderEventValidationError(`ProviderEvent payload exceeds max size of ${maxPayloadBytes} bytes.`);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
function assertJsonValue(value, path, seen = new WeakSet()) {
|
|
211
|
+
if (value === null)
|
|
212
|
+
return;
|
|
213
|
+
if (typeof value === "string" || typeof value === "boolean")
|
|
214
|
+
return;
|
|
215
|
+
if (typeof value === "number") {
|
|
216
|
+
if (Number.isFinite(value))
|
|
217
|
+
return;
|
|
218
|
+
throw new ProviderEventValidationError(`ProviderEvent ${path} must not contain non-finite numbers.`);
|
|
219
|
+
}
|
|
220
|
+
if (Array.isArray(value)) {
|
|
221
|
+
if (seen.has(value)) {
|
|
222
|
+
throw new ProviderEventValidationError(`ProviderEvent ${path} must not contain circular references.`);
|
|
223
|
+
}
|
|
224
|
+
seen.add(value);
|
|
225
|
+
for (const [index, entry] of value.entries()) {
|
|
226
|
+
assertJsonValue(entry, `${path}[${index}]`, seen);
|
|
227
|
+
}
|
|
228
|
+
seen.delete(value);
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
if (isPlainRecord(value)) {
|
|
232
|
+
if (seen.has(value)) {
|
|
233
|
+
throw new ProviderEventValidationError(`ProviderEvent ${path} must not contain circular references.`);
|
|
234
|
+
}
|
|
235
|
+
seen.add(value);
|
|
236
|
+
for (const [key, entry] of Object.entries(value)) {
|
|
237
|
+
assertJsonValue(entry, `${path}.${key}`, seen);
|
|
238
|
+
}
|
|
239
|
+
seen.delete(value);
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
throw new ProviderEventValidationError(`ProviderEvent ${path} must be JSON-serializable.`);
|
|
243
|
+
}
|
|
244
|
+
function redactValueInPlace(value, patterns) {
|
|
245
|
+
if (Array.isArray(value)) {
|
|
246
|
+
for (const entry of value)
|
|
247
|
+
redactValueInPlace(entry, patterns);
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
if (!isPlainRecord(value))
|
|
251
|
+
return;
|
|
252
|
+
for (const [key, entry] of Object.entries(value)) {
|
|
253
|
+
if (patterns.some((pattern) => matchesPattern(pattern, key)))
|
|
254
|
+
value[key] = REDACTED_VALUE;
|
|
255
|
+
else
|
|
256
|
+
redactValueInPlace(entry, patterns);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
function isPlainRecord(value) {
|
|
260
|
+
return (typeof value === "object" &&
|
|
261
|
+
value !== null &&
|
|
262
|
+
!Array.isArray(value) &&
|
|
263
|
+
Object.getPrototypeOf(value) === Object.prototype);
|
|
264
|
+
}
|
|
265
|
+
function buildDedupeTuple(event) {
|
|
266
|
+
return [
|
|
267
|
+
event.serviceAccountId ?? "",
|
|
268
|
+
event.providerId,
|
|
269
|
+
event.connectionId ?? "",
|
|
270
|
+
event.dedupeKey,
|
|
271
|
+
].join("\u001f");
|
|
272
|
+
}
|
|
273
|
+
function matchesPattern(pattern, value) {
|
|
274
|
+
pattern.lastIndex = 0;
|
|
275
|
+
const matched = pattern.test(value);
|
|
276
|
+
pattern.lastIndex = 0;
|
|
277
|
+
return matched;
|
|
278
|
+
}
|
|
279
|
+
function validateListOptions(options) {
|
|
280
|
+
if (options.afterObservedAt) {
|
|
281
|
+
validateTimestamp("afterObservedAt", options.afterObservedAt);
|
|
282
|
+
}
|
|
283
|
+
if (options.limit !== undefined && (!Number.isInteger(options.limit) || options.limit <= 0)) {
|
|
284
|
+
throw new ProviderEventValidationError("ProviderEvent list limit must be a positive integer.");
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
export function randomProviderEventId() {
|
|
288
|
+
return `pevt_${randomUUID().replaceAll("-", "")}`;
|
|
289
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
declare const SESSION_KEY_BRAND: unique symbol;
|
|
2
|
+
/** Canonical, opaque identity for a stateful provider session. */
|
|
3
|
+
export type SessionKey = string & {
|
|
4
|
+
readonly [SESSION_KEY_BRAND]: "SessionKey";
|
|
5
|
+
};
|
|
6
|
+
export interface SessionKeyParts {
|
|
7
|
+
readonly providerId: string;
|
|
8
|
+
readonly serviceAccountId: string;
|
|
9
|
+
readonly connectionId: string;
|
|
10
|
+
/** Optional extra session axes (e.g. device, mailbox, persona). Defaults to none. */
|
|
11
|
+
readonly dimensions?: Readonly<Record<string, string>>;
|
|
12
|
+
}
|
|
13
|
+
export declare function buildSessionKey(parts: SessionKeyParts): SessionKey;
|
|
14
|
+
export declare function parseSessionKey(key: string): SessionKeyParts;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
const PREFIX = "stateful:v1";
|
|
2
|
+
const REQUIRED_DIMENSIONS = ["providerId", "serviceAccountId", "connectionId"];
|
|
3
|
+
const REQUIRED_DIMENSION_SET = new Set(REQUIRED_DIMENSIONS);
|
|
4
|
+
export function buildSessionKey(parts) {
|
|
5
|
+
validateRequiredPart(parts.providerId, "providerId");
|
|
6
|
+
validateRequiredPart(parts.serviceAccountId, "serviceAccountId");
|
|
7
|
+
validateRequiredPart(parts.connectionId, "connectionId");
|
|
8
|
+
const dimensions = Object.entries(parts.dimensions ?? {}).sort(([left], [right]) => left < right ? -1 : left > right ? 1 : 0);
|
|
9
|
+
for (const [name, value] of dimensions) {
|
|
10
|
+
validateDimension(name, value);
|
|
11
|
+
}
|
|
12
|
+
const segments = [
|
|
13
|
+
["providerId", parts.providerId],
|
|
14
|
+
["serviceAccountId", parts.serviceAccountId],
|
|
15
|
+
["connectionId", parts.connectionId],
|
|
16
|
+
...dimensions,
|
|
17
|
+
];
|
|
18
|
+
return `${PREFIX}/${segments
|
|
19
|
+
.map(([name, value]) => `${encodeURIComponent(name)}=${encodeURIComponent(value)}`)
|
|
20
|
+
.join("/")}`;
|
|
21
|
+
}
|
|
22
|
+
export function parseSessionKey(key) {
|
|
23
|
+
const prefix = `${PREFIX}/`;
|
|
24
|
+
if (!key.startsWith(prefix)) {
|
|
25
|
+
throw new Error(`Invalid session key: expected canonical prefix "${PREFIX}".`);
|
|
26
|
+
}
|
|
27
|
+
const parsed = new Map();
|
|
28
|
+
for (const segment of key.slice(prefix.length).split("/")) {
|
|
29
|
+
const separator = segment.indexOf("=");
|
|
30
|
+
if (separator <= 0) {
|
|
31
|
+
throw new Error(`Invalid session key segment "${segment}"; expected name=value.`);
|
|
32
|
+
}
|
|
33
|
+
const name = decodeSegment(segment.slice(0, separator), "dimension name");
|
|
34
|
+
const value = decodeSegment(segment.slice(separator + 1), `dimension "${name}"`);
|
|
35
|
+
if (parsed.has(name)) {
|
|
36
|
+
throw new Error(`Invalid session key: duplicate dimension "${name}".`);
|
|
37
|
+
}
|
|
38
|
+
if (name.trim().length === 0) {
|
|
39
|
+
throw new Error("Invalid session key: dimension name must not be empty.");
|
|
40
|
+
}
|
|
41
|
+
if (value.trim().length === 0) {
|
|
42
|
+
throw new Error(`Invalid session key: dimension "${name}" must not be empty.`);
|
|
43
|
+
}
|
|
44
|
+
parsed.set(name, value);
|
|
45
|
+
}
|
|
46
|
+
for (const required of REQUIRED_DIMENSIONS) {
|
|
47
|
+
if (!parsed.has(required)) {
|
|
48
|
+
throw new Error(`Invalid session key: missing required dimension "${required}".`);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
const dimensions = {};
|
|
52
|
+
for (const [name, value] of parsed) {
|
|
53
|
+
if (!REQUIRED_DIMENSION_SET.has(name))
|
|
54
|
+
dimensions[name] = value;
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
providerId: parsed.get("providerId"),
|
|
58
|
+
serviceAccountId: parsed.get("serviceAccountId"),
|
|
59
|
+
connectionId: parsed.get("connectionId"),
|
|
60
|
+
dimensions,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
function validateRequiredPart(value, name) {
|
|
64
|
+
if (typeof value !== "string" || value.trim().length === 0) {
|
|
65
|
+
throw new Error(`Cannot build session key: required dimension "${name}" is missing or empty.`);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
function validateDimension(name, value) {
|
|
69
|
+
if (name.trim().length === 0) {
|
|
70
|
+
throw new Error("Cannot build session key: extra dimension name must not be empty.");
|
|
71
|
+
}
|
|
72
|
+
if (REQUIRED_DIMENSION_SET.has(name)) {
|
|
73
|
+
throw new Error(`Cannot build session key: extra dimension "${name}" duplicates a required dimension.`);
|
|
74
|
+
}
|
|
75
|
+
if (typeof value !== "string" || value.trim().length === 0) {
|
|
76
|
+
throw new Error(`Cannot build session key: extra dimension "${name}" is missing or empty.`);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
function decodeSegment(value, label) {
|
|
80
|
+
try {
|
|
81
|
+
return decodeURIComponent(value);
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
throw new Error(`Invalid session key: ${label} is not valid URI encoding.`);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { StatefulProviderSessionContext } from "./stateful-provider-adapter.js";
|
|
2
|
+
import type { StatefulOperationRequest } from "./stateful-provider-session-routing.js";
|
|
3
|
+
import type { ManagedSession, SessionOwnerRecord } from "./stateful-provider-session-runtime.js";
|
|
4
|
+
export declare function makeStatefulProviderSessionContext(providerId: string, owner: SessionOwnerRecord, request: StatefulOperationRequest, signal: AbortSignal): StatefulProviderSessionContext;
|
|
5
|
+
export declare function makeStatefulProviderCloseContext<TSession>(providerId: string, session: ManagedSession<TSession>): StatefulProviderSessionContext;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export function makeStatefulProviderSessionContext(providerId, owner, request, signal) {
|
|
2
|
+
const ctx = {
|
|
3
|
+
sessionKey: owner.sessionKey,
|
|
4
|
+
providerId,
|
|
5
|
+
connectionId: request.connectionId,
|
|
6
|
+
serviceAccountId: request.serviceAccountId,
|
|
7
|
+
ownerPodId: owner.ownerPodId,
|
|
8
|
+
ownerEndpoint: owner.ownerEndpoint,
|
|
9
|
+
ownerStatus: owner.status,
|
|
10
|
+
generation: owner.generation,
|
|
11
|
+
signal,
|
|
12
|
+
requestId: request.requestId,
|
|
13
|
+
operationId: request.operationId,
|
|
14
|
+
...(request.runtimeContext !== undefined ? { runtimeContext: request.runtimeContext } : {}),
|
|
15
|
+
};
|
|
16
|
+
return withOptionalRequestMetadata(ctx, request);
|
|
17
|
+
}
|
|
18
|
+
export function makeStatefulProviderCloseContext(providerId, session) {
|
|
19
|
+
const identity = session.identity;
|
|
20
|
+
return {
|
|
21
|
+
sessionKey: session.sessionKey,
|
|
22
|
+
providerId,
|
|
23
|
+
...(identity
|
|
24
|
+
? {
|
|
25
|
+
connectionId: identity.connectionId,
|
|
26
|
+
serviceAccountId: identity.serviceAccountId,
|
|
27
|
+
ownerPodId: identity.ownerPodId,
|
|
28
|
+
ownerEndpoint: identity.ownerEndpoint,
|
|
29
|
+
ownerStatus: identity.ownerStatus,
|
|
30
|
+
}
|
|
31
|
+
: {}),
|
|
32
|
+
generation: session.generation,
|
|
33
|
+
signal: new AbortController().signal,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
function withOptionalRequestMetadata(ctx, request) {
|
|
37
|
+
return {
|
|
38
|
+
...ctx,
|
|
39
|
+
...(request.idempotencyKey ? { idempotencyKey: request.idempotencyKey } : {}),
|
|
40
|
+
...(request.deadlineAt ? { deadlineAt: request.deadlineAt } : {}),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { StatefulProviderMetricEmitter } from "./stateful-provider-observability.js";
|
|
2
|
+
import type { StatefulOperationRequest } from "./stateful-provider-session-routing.js";
|
|
3
|
+
import type { SessionOwnerRecord } from "./stateful-provider-session-runtime.js";
|
|
4
|
+
export declare function observeStatefulSessionOperationDuration(input: {
|
|
5
|
+
readonly metricEmitter: StatefulProviderMetricEmitter;
|
|
6
|
+
readonly request: StatefulOperationRequest;
|
|
7
|
+
readonly owner: SessionOwnerRecord;
|
|
8
|
+
readonly durationMs: number;
|
|
9
|
+
}): void;
|
|
10
|
+
export declare function emitStatefulSessionInvalidatedMetric(input: {
|
|
11
|
+
readonly metricEmitter: StatefulProviderMetricEmitter;
|
|
12
|
+
readonly request: StatefulOperationRequest;
|
|
13
|
+
readonly owner: SessionOwnerRecord;
|
|
14
|
+
readonly reason: string;
|
|
15
|
+
}): void;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export function observeStatefulSessionOperationDuration(input) {
|
|
2
|
+
input.metricEmitter.observe("apifuse_stateful_provider_session_operation_duration_ms", statefulSessionMetricLabels(input.request, input.owner), Math.max(0, input.durationMs));
|
|
3
|
+
}
|
|
4
|
+
export function emitStatefulSessionInvalidatedMetric(input) {
|
|
5
|
+
input.metricEmitter.increment("apifuse_stateful_provider_session_invalidations_total", statefulSessionMetricLabels(input.request, input.owner, {
|
|
6
|
+
redactedErrorCode: input.reason,
|
|
7
|
+
}));
|
|
8
|
+
}
|
|
9
|
+
function statefulSessionMetricLabels(request, owner, extra = {}) {
|
|
10
|
+
return {
|
|
11
|
+
requestId: request.requestId,
|
|
12
|
+
providerId: request.providerId,
|
|
13
|
+
connectionId: request.connectionId,
|
|
14
|
+
serviceAccountId: request.serviceAccountId,
|
|
15
|
+
operationId: request.operationId,
|
|
16
|
+
sessionKey: request.sessionKey,
|
|
17
|
+
ownerPodId: owner.ownerPodId,
|
|
18
|
+
generation: owner.generation,
|
|
19
|
+
...extra,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import type { ProviderEventPublishOptions } from "./provider-event-pipeline.js";
|
|
2
|
+
import { type StatefulProviderMetricEmitter } from "./stateful-provider-observability.js";
|
|
3
|
+
import type { StatefulOperationExecutor, StatefulOperationRequest, StatefulOperationResult, StatefulOwnershipValidator } from "./stateful-provider-session-routing.js";
|
|
4
|
+
import type { SessionOwnerRecord, SessionPoolPolicy } from "./stateful-provider-session-runtime.js";
|
|
5
|
+
export type StatefulProviderConcurrencyPolicy = {
|
|
6
|
+
readonly mode: "serialize";
|
|
7
|
+
} | {
|
|
8
|
+
readonly mode: "parallel";
|
|
9
|
+
readonly maxInFlight?: number;
|
|
10
|
+
};
|
|
11
|
+
export type StatefulProviderReconnectPolicy = "resume" | "recreate" | "unsupported";
|
|
12
|
+
export type StatefulProviderAdapterPolicy = {
|
|
13
|
+
readonly concurrency: StatefulProviderConcurrencyPolicy;
|
|
14
|
+
readonly reconnect: StatefulProviderReconnectPolicy;
|
|
15
|
+
};
|
|
16
|
+
export type StatefulSessionHealth = {
|
|
17
|
+
readonly status: "ready" | "connecting" | "reconnecting" | "degraded" | "closed";
|
|
18
|
+
readonly reason?: string;
|
|
19
|
+
readonly lastError?: string;
|
|
20
|
+
};
|
|
21
|
+
export declare class StatefulSessionInvalidatedError extends Error {
|
|
22
|
+
readonly name = "StatefulSessionInvalidatedError";
|
|
23
|
+
readonly reason: string;
|
|
24
|
+
readonly retryable: boolean;
|
|
25
|
+
constructor(message: string, options: {
|
|
26
|
+
readonly reason: string;
|
|
27
|
+
readonly retryable?: boolean;
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
export interface StatefulProviderSessionContext {
|
|
31
|
+
readonly sessionKey: string;
|
|
32
|
+
readonly providerId: string;
|
|
33
|
+
/** Present for operation-created sessions; optional for direct pool users. */
|
|
34
|
+
readonly connectionId?: string;
|
|
35
|
+
/** Present for operation-created sessions; optional for direct pool users. */
|
|
36
|
+
readonly serviceAccountId?: string;
|
|
37
|
+
readonly ownerPodId?: string;
|
|
38
|
+
readonly ownerEndpoint?: string;
|
|
39
|
+
readonly ownerStatus?: string;
|
|
40
|
+
readonly generation: number;
|
|
41
|
+
readonly signal: AbortSignal;
|
|
42
|
+
readonly requestId?: string;
|
|
43
|
+
readonly operationId?: string;
|
|
44
|
+
readonly idempotencyKey?: string;
|
|
45
|
+
readonly deadlineAt?: string;
|
|
46
|
+
readonly runtimeContext?: unknown;
|
|
47
|
+
}
|
|
48
|
+
export type StatefulProviderEventPublish<TEvent = unknown> = (event: TEvent) => void | Promise<void>;
|
|
49
|
+
export type StatefulProviderEventDisposer = () => void | Promise<void>;
|
|
50
|
+
export interface StatefulProviderEventPublisher<TEvent = unknown> {
|
|
51
|
+
publish(event: TEvent, options: ProviderEventPublishOptions): unknown;
|
|
52
|
+
}
|
|
53
|
+
export type StatefulWriteReconciliationResult<TSharedState = unknown> = {
|
|
54
|
+
readonly result: StatefulOperationResult;
|
|
55
|
+
readonly sharedState?: TSharedState;
|
|
56
|
+
};
|
|
57
|
+
export interface StatefulProviderAdapter<TSession, TSharedState = unknown, TEvent = unknown> {
|
|
58
|
+
readonly providerId: string;
|
|
59
|
+
readonly policy: StatefulProviderAdapterPolicy;
|
|
60
|
+
/** Optional until durable cursor/checkpoint state exists for a provider. */
|
|
61
|
+
restore?(ctx: StatefulProviderSessionContext): Promise<TSharedState | undefined>;
|
|
62
|
+
connect(ctx: StatefulProviderSessionContext, sharedState: TSharedState | undefined): Promise<TSession>;
|
|
63
|
+
invoke(ctx: StatefulProviderSessionContext, session: TSession, request: StatefulOperationRequest): Promise<StatefulOperationResult>;
|
|
64
|
+
/** Requires a matching session-manager checkpointStore. */
|
|
65
|
+
snapshot?(ctx: StatefulProviderSessionContext, session: TSession): Promise<TSharedState | undefined>;
|
|
66
|
+
/** Caller-driven through StatefulProviderSessionManager.health(). */
|
|
67
|
+
health?(ctx: StatefulProviderSessionContext, session: TSession): Promise<StatefulSessionHealth>;
|
|
68
|
+
close(ctx: StatefulProviderSessionContext, session: TSession, reason: string): Promise<void>;
|
|
69
|
+
/** Caller-driven through StatefulProviderSessionManager.reconcileWrite(). */
|
|
70
|
+
reconcileWrite?(ctx: StatefulProviderSessionContext, ledgerEntry: unknown, sharedState: TSharedState | undefined): Promise<StatefulWriteReconciliationResult<TSharedState>>;
|
|
71
|
+
/** Registers a connection-owned event source. Its disposer runs before checkpoint and close. */
|
|
72
|
+
subscribe?(ctx: StatefulProviderSessionContext, session: TSession, publish: StatefulProviderEventPublish<TEvent>): StatefulProviderEventDisposer | Promise<StatefulProviderEventDisposer>;
|
|
73
|
+
}
|
|
74
|
+
export type StatefulProviderSessionManagerOptions<TSession, TSharedState, TEvent = unknown> = {
|
|
75
|
+
readonly adapter: StatefulProviderAdapter<TSession, TSharedState, TEvent>;
|
|
76
|
+
readonly poolPolicy: SessionPoolPolicy;
|
|
77
|
+
readonly checkpointStore?: (ctx: StatefulProviderSessionContext, state: TSharedState) => Promise<void>;
|
|
78
|
+
readonly eventPublisher?: StatefulProviderEventPublisher<TEvent>;
|
|
79
|
+
readonly clock?: () => Date;
|
|
80
|
+
readonly metricEmitter?: StatefulProviderMetricEmitter;
|
|
81
|
+
};
|
|
82
|
+
export declare class StatefulProviderSessionManager<TSession, TSharedState = unknown, TEvent = unknown> implements StatefulOperationExecutor<TSession> {
|
|
83
|
+
#private;
|
|
84
|
+
constructor(options: StatefulProviderSessionManagerOptions<TSession, TSharedState, TEvent>);
|
|
85
|
+
invoke(owner: SessionOwnerRecord, request: StatefulOperationRequest, signal: AbortSignal, validateOwnership?: StatefulOwnershipValidator): Promise<StatefulOperationResult>;
|
|
86
|
+
executeLocal(request: StatefulOperationRequest, owner: SessionOwnerRecord, signal: AbortSignal, validateOwnership?: StatefulOwnershipValidator): Promise<StatefulOperationResult>;
|
|
87
|
+
health(owner: SessionOwnerRecord, request: StatefulOperationRequest, signal: AbortSignal, validateOwnership?: StatefulOwnershipValidator): Promise<StatefulSessionHealth>;
|
|
88
|
+
reconcileWrite(owner: SessionOwnerRecord, request: StatefulOperationRequest, ledgerEntry: unknown, signal: AbortSignal, validateOwnership?: StatefulOwnershipValidator): Promise<StatefulWriteReconciliationResult<TSharedState>>;
|
|
89
|
+
closeAll(reason: string): Promise<void>;
|
|
90
|
+
private getOrCreate;
|
|
91
|
+
private connect;
|
|
92
|
+
private validatePooledOwnership;
|
|
93
|
+
private refreshEventOwnerFence;
|
|
94
|
+
private closeManagedSession;
|
|
95
|
+
private observeDuration;
|
|
96
|
+
private runLimited;
|
|
97
|
+
private schedule;
|
|
98
|
+
}
|