@apifuse/provider-sdk 2.2.0-beta.5 → 2.2.0-beta.51
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 +205 -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 +1249 -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-hoisted-array-spread.ts.txt +15 -0
- package/src/cli/__tests__/fixtures/migrate-operation-declaration/examples-imported-array-spread.ts.txt +9 -0
- package/src/cli/__tests__/fixtures/migrate-operation-declaration/examples-map.ts.txt +5 -0
- package/src/cli/__tests__/fixtures/migrate-operation-declaration/examples-mixed-array-spread.ts.txt +24 -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 +1751 -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,6 @@
|
|
|
1
|
+
import { type CreateTraceContextOptions } from "../runtime/trace.js";
|
|
2
|
+
import type { TraceConfig } from "../types.js";
|
|
3
|
+
type EnvLike = Record<string, string | undefined>;
|
|
4
|
+
/** Server-only trace output policy. Shared programmatic trace callers stay in-memory. */
|
|
5
|
+
export declare function resolveServerTraceContextOptions(config: TraceConfig, resourceAttributes: Record<string, string>, env?: EnvLike): CreateTraceContextOptions;
|
|
6
|
+
export {};
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { isSensitiveFixtureKey, REDACTED_FIXTURE_VALUE } from "../fixture-sanitization.js";
|
|
2
|
+
import { OTEL_EXPORTER_OTLP_ENDPOINT, OTEL_EXPORTER_OTLP_TRACES_ENDPOINT, resolveOTLPExportOptions, resolveOTLPResourceAttributes, } from "../runtime/otlp.js";
|
|
3
|
+
import { resolveTraceContextOptions, } from "../runtime/trace.js";
|
|
4
|
+
import { sanitizeSpanForOutput, sanitizeSpanNameForOutput, sanitizeTraceAttributes, } from "../trace-sanitization.js";
|
|
5
|
+
// Warn once per environment object: process.env in production, each injected env in tests.
|
|
6
|
+
const warnedEnvironments = new WeakSet();
|
|
7
|
+
const warnedResourceEnvironments = new WeakSet();
|
|
8
|
+
function warnDiscardedResourceAttributes(env, resolution) {
|
|
9
|
+
if (resolution.discarded.length === 0 || warnedResourceEnvironments.has(env))
|
|
10
|
+
return;
|
|
11
|
+
warnedResourceEnvironments.add(env);
|
|
12
|
+
console.warn(`[apifuse] ${resolution.discarded.join(", ")} could not be parsed as an OTel key=value list and was ignored.`);
|
|
13
|
+
}
|
|
14
|
+
function warnExportDisabled(env, resolution) {
|
|
15
|
+
if (warnedEnvironments.has(env))
|
|
16
|
+
return;
|
|
17
|
+
warnedEnvironments.add(env);
|
|
18
|
+
console.warn(resolution.status === "unconfigured"
|
|
19
|
+
? `[apifuse] OTLP trace export is enabled but no endpoint is configured; set ${OTEL_EXPORTER_OTLP_TRACES_ENDPOINT} or ${OTEL_EXPORTER_OTLP_ENDPOINT}. Trace export is disabled.`
|
|
20
|
+
: `[apifuse] OTLP trace export is enabled but ${resolution.source} ${resolution.reason}. Trace export is disabled.`);
|
|
21
|
+
}
|
|
22
|
+
function resolveServerOTLPExportOptions(config, env) {
|
|
23
|
+
const resolution = resolveOTLPExportOptions({
|
|
24
|
+
endpoint: config.otlp?.endpoint ?? config.endpoint,
|
|
25
|
+
headers: config.otlp?.headers,
|
|
26
|
+
timeout: config.otlp?.timeout,
|
|
27
|
+
}, env);
|
|
28
|
+
if (resolution.status === "resolved")
|
|
29
|
+
return resolution.options;
|
|
30
|
+
warnExportDisabled(env, resolution);
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Per-request attributes carry client-supplied values (request_id) and get the full attribute
|
|
35
|
+
* sanitizer, exactly as on the console path. Operator-configured attributes from the process
|
|
36
|
+
* environment are identifiers rather than request data: their values are kept, with control
|
|
37
|
+
* characters neutralized, length bounded, and secret-named keys redacted.
|
|
38
|
+
*/
|
|
39
|
+
function resolveExportResourceAttributes(requestAttributes, env) {
|
|
40
|
+
const resolution = resolveOTLPResourceAttributes({}, env);
|
|
41
|
+
warnDiscardedResourceAttributes(env, resolution);
|
|
42
|
+
const operatorAttributes = Object.fromEntries(Object.entries(resolution.attributes).map(([key, value]) => [
|
|
43
|
+
sanitizeSpanNameForOutput(key),
|
|
44
|
+
isSensitiveFixtureKey(key) ? REDACTED_FIXTURE_VALUE : sanitizeSpanNameForOutput(value),
|
|
45
|
+
]));
|
|
46
|
+
const sanitizedRequestAttributes = Object.fromEntries(Object.entries(sanitizeTraceAttributes(requestAttributes)).map(([key, value]) => [
|
|
47
|
+
key,
|
|
48
|
+
String(value),
|
|
49
|
+
]));
|
|
50
|
+
return { ...operatorAttributes, ...sanitizedRequestAttributes };
|
|
51
|
+
}
|
|
52
|
+
/** Server-only trace output policy. Shared programmatic trace callers stay in-memory. */
|
|
53
|
+
export function resolveServerTraceContextOptions(config, resourceAttributes, env = process.env) {
|
|
54
|
+
const resolved = resolveTraceContextOptions(config);
|
|
55
|
+
const outputEnabled = config.enabled !== false && config.exporter !== "none";
|
|
56
|
+
const consoleHook = outputEnabled && (config.exporter === "console" || config.exporter === "json")
|
|
57
|
+
? (span) => console.log(JSON.stringify(sanitizeSpanForOutput(span, resourceAttributes)))
|
|
58
|
+
: undefined;
|
|
59
|
+
const onSpan = consoleHook && resolved.onSpan
|
|
60
|
+
? (span) => {
|
|
61
|
+
consoleHook(span);
|
|
62
|
+
resolved.onSpan?.(span);
|
|
63
|
+
}
|
|
64
|
+
: (consoleHook ?? resolved.onSpan);
|
|
65
|
+
const exportOptions = outputEnabled && config.exporter === "otlp"
|
|
66
|
+
? resolveServerOTLPExportOptions(config, env)
|
|
67
|
+
: undefined;
|
|
68
|
+
return {
|
|
69
|
+
maxSpans: resolved.maxSpans,
|
|
70
|
+
onSpan,
|
|
71
|
+
...(exportOptions
|
|
72
|
+
? {
|
|
73
|
+
exportOptions,
|
|
74
|
+
resourceAttributes: resolveExportResourceAttributes(resourceAttributes, env),
|
|
75
|
+
sanitizeSpanForExport: (span) => sanitizeSpanForOutput(span),
|
|
76
|
+
}
|
|
77
|
+
: {}),
|
|
78
|
+
};
|
|
79
|
+
}
|
package/dist/server/types.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ export declare const ConnectionModeSchema: z.ZodEnum<{
|
|
|
4
4
|
none: "none";
|
|
5
5
|
oauth2: "oauth2";
|
|
6
6
|
"platform-managed": "platform-managed";
|
|
7
|
+
oauth2_proxied: "oauth2_proxied";
|
|
7
8
|
}>;
|
|
8
9
|
export declare const OperationConnectionSchema: z.ZodObject<{
|
|
9
10
|
id: z.ZodString;
|
|
@@ -12,6 +13,7 @@ export declare const OperationConnectionSchema: z.ZodObject<{
|
|
|
12
13
|
none: "none";
|
|
13
14
|
oauth2: "oauth2";
|
|
14
15
|
"platform-managed": "platform-managed";
|
|
16
|
+
oauth2_proxied: "oauth2_proxied";
|
|
15
17
|
}>;
|
|
16
18
|
secrets: z.ZodRecord<z.ZodString, z.ZodString>;
|
|
17
19
|
scopes: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
@@ -29,6 +31,7 @@ export declare const OperationRequestSchema: z.ZodObject<{
|
|
|
29
31
|
none: "none";
|
|
30
32
|
oauth2: "oauth2";
|
|
31
33
|
"platform-managed": "platform-managed";
|
|
34
|
+
oauth2_proxied: "oauth2_proxied";
|
|
32
35
|
}>;
|
|
33
36
|
secrets: z.ZodRecord<z.ZodString, z.ZodString>;
|
|
34
37
|
scopes: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
@@ -42,6 +45,13 @@ export declare const ErrorEnvelopeSchema: z.ZodObject<{
|
|
|
42
45
|
code: z.ZodString;
|
|
43
46
|
message: z.ZodString;
|
|
44
47
|
requestId: z.ZodOptional<z.ZodString>;
|
|
48
|
+
retryable: z.ZodBoolean;
|
|
49
|
+
source: z.ZodOptional<z.ZodEnum<{
|
|
50
|
+
client: "client";
|
|
51
|
+
upstream_rule: "upstream_rule";
|
|
52
|
+
upstream_failure: "upstream_failure";
|
|
53
|
+
apifuse: "apifuse";
|
|
54
|
+
}>>;
|
|
45
55
|
fix: z.ZodOptional<z.ZodString>;
|
|
46
56
|
details: z.ZodOptional<z.ZodUnknown>;
|
|
47
57
|
}, z.core.$strip>;
|
|
@@ -55,21 +65,21 @@ export declare const OperationSuccessResponseSchema: z.ZodObject<{
|
|
|
55
65
|
stale: z.ZodBoolean;
|
|
56
66
|
keys: z.ZodArray<z.ZodString>;
|
|
57
67
|
source: z.ZodOptional<z.ZodEnum<{
|
|
58
|
-
loader: "loader";
|
|
59
|
-
memory: "memory";
|
|
60
68
|
mixed: "mixed";
|
|
61
69
|
redis: "redis";
|
|
70
|
+
memory: "memory";
|
|
71
|
+
loader: "loader";
|
|
62
72
|
}>>;
|
|
63
73
|
}, z.core.$strip>>;
|
|
64
74
|
retry: z.ZodOptional<z.ZodObject<{
|
|
65
75
|
attempts: z.ZodNumber;
|
|
66
76
|
retries: z.ZodNumber;
|
|
67
77
|
preset: z.ZodOptional<z.ZodEnum<{
|
|
68
|
-
aggressive_read: "aggressive_read";
|
|
69
78
|
off: "off";
|
|
70
|
-
rate_limit_aware: "rate_limit_aware";
|
|
71
|
-
safe_read: "safe_read";
|
|
72
79
|
transport_transient: "transport_transient";
|
|
80
|
+
safe_read: "safe_read";
|
|
81
|
+
aggressive_read: "aggressive_read";
|
|
82
|
+
rate_limit_aware: "rate_limit_aware";
|
|
73
83
|
}>>;
|
|
74
84
|
transport: z.ZodEnum<{
|
|
75
85
|
native: "native";
|
|
@@ -84,6 +94,13 @@ export declare const OperationErrorResponseSchema: z.ZodObject<{
|
|
|
84
94
|
code: z.ZodString;
|
|
85
95
|
message: z.ZodString;
|
|
86
96
|
requestId: z.ZodOptional<z.ZodString>;
|
|
97
|
+
retryable: z.ZodBoolean;
|
|
98
|
+
source: z.ZodOptional<z.ZodEnum<{
|
|
99
|
+
client: "client";
|
|
100
|
+
upstream_rule: "upstream_rule";
|
|
101
|
+
upstream_failure: "upstream_failure";
|
|
102
|
+
apifuse: "apifuse";
|
|
103
|
+
}>>;
|
|
87
104
|
fix: z.ZodOptional<z.ZodString>;
|
|
88
105
|
details: z.ZodOptional<z.ZodUnknown>;
|
|
89
106
|
}, z.core.$strip>;
|
|
@@ -105,6 +122,7 @@ export declare const AuthFlowRequestSchema: z.ZodObject<{
|
|
|
105
122
|
none: "none";
|
|
106
123
|
oauth2: "oauth2";
|
|
107
124
|
"platform-managed": "platform-managed";
|
|
125
|
+
oauth2_proxied: "oauth2_proxied";
|
|
108
126
|
}>;
|
|
109
127
|
secrets: z.ZodRecord<z.ZodString, z.ZodString>;
|
|
110
128
|
scopes: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
@@ -121,6 +139,13 @@ export declare const AuthFlowErrorResponseSchema: z.ZodObject<{
|
|
|
121
139
|
code: z.ZodString;
|
|
122
140
|
message: z.ZodString;
|
|
123
141
|
requestId: z.ZodOptional<z.ZodString>;
|
|
142
|
+
retryable: z.ZodBoolean;
|
|
143
|
+
source: z.ZodOptional<z.ZodEnum<{
|
|
144
|
+
client: "client";
|
|
145
|
+
upstream_rule: "upstream_rule";
|
|
146
|
+
upstream_failure: "upstream_failure";
|
|
147
|
+
apifuse: "apifuse";
|
|
148
|
+
}>>;
|
|
124
149
|
fix: z.ZodOptional<z.ZodString>;
|
|
125
150
|
details: z.ZodOptional<z.ZodUnknown>;
|
|
126
151
|
}, z.core.$strip>;
|
package/dist/server/types.js
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
+
import { PROVIDER_ERROR_SOURCES } from "../observability.js";
|
|
2
3
|
import { HttpRetryPreset } from "../types.js";
|
|
3
|
-
export const ConnectionModeSchema = z.enum([
|
|
4
|
+
export const ConnectionModeSchema = z.enum([
|
|
5
|
+
"oauth2",
|
|
6
|
+
"oauth2_proxied",
|
|
7
|
+
"credentials",
|
|
8
|
+
"platform-managed",
|
|
9
|
+
"none",
|
|
10
|
+
]);
|
|
4
11
|
export const OperationConnectionSchema = z.object({
|
|
5
12
|
id: z.string(),
|
|
6
13
|
mode: ConnectionModeSchema,
|
|
@@ -21,6 +28,11 @@ export const ErrorEnvelopeSchema = z.object({
|
|
|
21
28
|
code: z.string(),
|
|
22
29
|
message: z.string(),
|
|
23
30
|
requestId: z.string().optional(),
|
|
31
|
+
retryable: z.boolean(),
|
|
32
|
+
// Who stopped the request (honest-provider-error-contract); optional so
|
|
33
|
+
// older peers (stateful forwarding owners) remain parseable. Derived from
|
|
34
|
+
// the PROVIDER_ERROR_SOURCES registry so the enum cannot drift.
|
|
35
|
+
source: z.enum(PROVIDER_ERROR_SOURCES).optional(),
|
|
24
36
|
fix: z.string().optional(),
|
|
25
37
|
details: z.unknown().optional(),
|
|
26
38
|
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export type StatefulControlPlaneOperation = "resolve" | "acquire" | "renew" | "release";
|
|
2
|
+
export declare class StatefulRoutingDeadlineError extends Error {
|
|
3
|
+
readonly requestId: string;
|
|
4
|
+
readonly deadlineAt: string;
|
|
5
|
+
constructor(requestId: string, deadlineAt: string);
|
|
6
|
+
}
|
|
7
|
+
export declare class StatefulControlPlaneError extends Error {
|
|
8
|
+
readonly code: string;
|
|
9
|
+
readonly operation: StatefulControlPlaneOperation;
|
|
10
|
+
readonly status?: number;
|
|
11
|
+
readonly cause?: unknown;
|
|
12
|
+
constructor(input: {
|
|
13
|
+
readonly code: string;
|
|
14
|
+
readonly message: string;
|
|
15
|
+
readonly operation: StatefulControlPlaneOperation;
|
|
16
|
+
readonly status?: number;
|
|
17
|
+
readonly cause?: unknown;
|
|
18
|
+
});
|
|
19
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export class StatefulRoutingDeadlineError extends Error {
|
|
2
|
+
requestId;
|
|
3
|
+
deadlineAt;
|
|
4
|
+
constructor(requestId, deadlineAt) {
|
|
5
|
+
super(`Stateful operation request ${requestId} deadline has expired.`);
|
|
6
|
+
this.name = "StatefulRoutingDeadlineError";
|
|
7
|
+
this.requestId = requestId;
|
|
8
|
+
this.deadlineAt = deadlineAt;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export class StatefulControlPlaneError extends Error {
|
|
12
|
+
code;
|
|
13
|
+
operation;
|
|
14
|
+
status;
|
|
15
|
+
cause;
|
|
16
|
+
constructor(input) {
|
|
17
|
+
super(input.message);
|
|
18
|
+
this.name = "StatefulControlPlaneError";
|
|
19
|
+
this.code = input.code;
|
|
20
|
+
this.operation = input.operation;
|
|
21
|
+
this.status = input.status;
|
|
22
|
+
this.cause = input.cause;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { ProviderEventDeliveryFailureRecorder } from "./provider-event-delivery-failures.js";
|
|
2
|
+
import type { ProviderEventPublisher, ProviderEventPublishOptions, PublishAck } from "./provider-event-pipeline.js";
|
|
3
|
+
import { type ProviderEventMetricEmitter } from "./provider-event-pipeline-metrics.js";
|
|
4
|
+
import type { ProviderEvent } from "./provider-events.js";
|
|
5
|
+
type FetchTransport = (url: string | URL | Request, init?: RequestInit) => Promise<Response>;
|
|
6
|
+
export type HttpProviderEventEmitterOptions = {
|
|
7
|
+
readonly baseUrl: string;
|
|
8
|
+
readonly secret: string;
|
|
9
|
+
readonly fetch?: FetchTransport;
|
|
10
|
+
readonly clock?: () => Date;
|
|
11
|
+
readonly random?: () => number;
|
|
12
|
+
readonly metricEmitter?: ProviderEventMetricEmitter;
|
|
13
|
+
readonly failureRecorder?: ProviderEventDeliveryFailureRecorder;
|
|
14
|
+
readonly maxBufferedEvents?: number;
|
|
15
|
+
readonly retryBaseMs?: number;
|
|
16
|
+
readonly retryMaxMs?: number;
|
|
17
|
+
readonly maxAttempts?: number;
|
|
18
|
+
readonly jitterRatio?: number;
|
|
19
|
+
};
|
|
20
|
+
export type ProviderEventFlushReport = {
|
|
21
|
+
/** Events delivered during this emitter's lifetime. */
|
|
22
|
+
readonly delivered: number;
|
|
23
|
+
/** Events permanently dropped during this emitter's lifetime. */
|
|
24
|
+
readonly failed: number;
|
|
25
|
+
/** Events still buffered when flush returned. */
|
|
26
|
+
readonly pending: number;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Provider-pod HTTP publication with a bounded best-effort delivery guarantee and at-least-once
|
|
30
|
+
* transport retries. This is not durable at-least-once delivery: buffered events can be lost if
|
|
31
|
+
* the process is killed. Graceful shutdown should drain the in-memory buffer with flush().
|
|
32
|
+
*/
|
|
33
|
+
export declare class HttpProviderEventEmitter implements ProviderEventPublisher {
|
|
34
|
+
#private;
|
|
35
|
+
constructor(options: HttpProviderEventEmitterOptions);
|
|
36
|
+
publish(event: ProviderEvent, options: ProviderEventPublishOptions): PublishAck;
|
|
37
|
+
pendingCount(): number;
|
|
38
|
+
flush(timeoutMs?: number): Promise<ProviderEventFlushReport>;
|
|
39
|
+
}
|
|
40
|
+
export {};
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
import { statefulSignedHeaders } from "../stateful-signing.js";
|
|
2
|
+
import { NOOP_PROVIDER_EVENT_METRIC_EMITTER, providerEventMetricLabels, } from "./provider-event-pipeline-metrics.js";
|
|
3
|
+
const DEFAULT_MAX_BUFFERED_EVENTS = 1_000;
|
|
4
|
+
const DEFAULT_RETRY_BASE_MS = 500;
|
|
5
|
+
const DEFAULT_RETRY_MAX_MS = 30_000;
|
|
6
|
+
const DEFAULT_MAX_ATTEMPTS = 10;
|
|
7
|
+
const DEFAULT_JITTER_RATIO = 0.2;
|
|
8
|
+
const CONSOLE_PROVIDER_EVENT_DELIVERY_FAILURE_RECORDER = {
|
|
9
|
+
record(failure) {
|
|
10
|
+
console.error(JSON.stringify({
|
|
11
|
+
event: "provider_event_delivery_failed",
|
|
12
|
+
eventId: failure.eventId,
|
|
13
|
+
reason: failure.reason,
|
|
14
|
+
attempts: failure.attempts,
|
|
15
|
+
}));
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Provider-pod HTTP publication with a bounded best-effort delivery guarantee and at-least-once
|
|
20
|
+
* transport retries. This is not durable at-least-once delivery: buffered events can be lost if
|
|
21
|
+
* the process is killed. Graceful shutdown should drain the in-memory buffer with flush().
|
|
22
|
+
*/
|
|
23
|
+
export class HttpProviderEventEmitter {
|
|
24
|
+
#baseUrl;
|
|
25
|
+
#secret;
|
|
26
|
+
#fetch;
|
|
27
|
+
#clock;
|
|
28
|
+
#random;
|
|
29
|
+
#metricEmitter;
|
|
30
|
+
#failureRecorder;
|
|
31
|
+
#maxBufferedEvents;
|
|
32
|
+
#retryBaseMs;
|
|
33
|
+
#retryMaxMs;
|
|
34
|
+
#maxAttempts;
|
|
35
|
+
#jitterRatio;
|
|
36
|
+
#queue = [];
|
|
37
|
+
#drainPromise;
|
|
38
|
+
#deliveredCount = 0;
|
|
39
|
+
#failedCount = 0;
|
|
40
|
+
constructor(options) {
|
|
41
|
+
if (options.baseUrl.trim().length === 0) {
|
|
42
|
+
throw new Error("Provider event ingest baseUrl is required.");
|
|
43
|
+
}
|
|
44
|
+
if (options.secret.trim().length === 0) {
|
|
45
|
+
throw new Error("Provider event ingest secret is required.");
|
|
46
|
+
}
|
|
47
|
+
this.#baseUrl = options.baseUrl.replace(/\/+$/, "");
|
|
48
|
+
this.#secret = options.secret;
|
|
49
|
+
this.#fetch = options.fetch ?? globalThis.fetch;
|
|
50
|
+
this.#clock = options.clock ?? (() => new Date());
|
|
51
|
+
this.#random = options.random ?? Math.random;
|
|
52
|
+
this.#metricEmitter = options.metricEmitter ?? NOOP_PROVIDER_EVENT_METRIC_EMITTER;
|
|
53
|
+
this.#failureRecorder =
|
|
54
|
+
options.failureRecorder ?? CONSOLE_PROVIDER_EVENT_DELIVERY_FAILURE_RECORDER;
|
|
55
|
+
this.#maxBufferedEvents = positiveInteger(options.maxBufferedEvents ?? DEFAULT_MAX_BUFFERED_EVENTS, "maxBufferedEvents");
|
|
56
|
+
this.#retryBaseMs = nonnegativeFinite(options.retryBaseMs ?? DEFAULT_RETRY_BASE_MS, "retryBaseMs");
|
|
57
|
+
this.#retryMaxMs = nonnegativeFinite(options.retryMaxMs ?? DEFAULT_RETRY_MAX_MS, "retryMaxMs");
|
|
58
|
+
this.#maxAttempts = positiveInteger(options.maxAttempts ?? DEFAULT_MAX_ATTEMPTS, "maxAttempts");
|
|
59
|
+
this.#jitterRatio = boundedRatio(options.jitterRatio ?? DEFAULT_JITTER_RATIO, "jitterRatio");
|
|
60
|
+
if (this.#retryMaxMs < this.#retryBaseMs) {
|
|
61
|
+
throw new Error("Provider event retryMaxMs must be greater than or equal to retryBaseMs.");
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
publish(event, options) {
|
|
65
|
+
const accepted = this.#enqueue(event, options);
|
|
66
|
+
return { accepted, queued: this.#queue.length };
|
|
67
|
+
}
|
|
68
|
+
pendingCount() {
|
|
69
|
+
return this.#queue.length;
|
|
70
|
+
}
|
|
71
|
+
async flush(timeoutMs = 30_000) {
|
|
72
|
+
nonnegativeFinite(timeoutMs, "flush timeout");
|
|
73
|
+
this.#ensureDrain();
|
|
74
|
+
if (this.#queue.length === 0 || timeoutMs === 0)
|
|
75
|
+
return this.#flushReport();
|
|
76
|
+
let timeout;
|
|
77
|
+
const timedOut = new Promise((resolve) => {
|
|
78
|
+
timeout = setTimeout(resolve, timeoutMs);
|
|
79
|
+
});
|
|
80
|
+
await Promise.race([this.#drainPromise ?? Promise.resolve(), timedOut]);
|
|
81
|
+
if (timeout)
|
|
82
|
+
clearTimeout(timeout);
|
|
83
|
+
return this.#flushReport();
|
|
84
|
+
}
|
|
85
|
+
#flushReport() {
|
|
86
|
+
return {
|
|
87
|
+
delivered: this.#deliveredCount,
|
|
88
|
+
failed: this.#failedCount,
|
|
89
|
+
pending: this.#queue.length,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
#enqueue(event, options) {
|
|
93
|
+
let rawBody;
|
|
94
|
+
try {
|
|
95
|
+
rawBody = JSON.stringify({
|
|
96
|
+
...event,
|
|
97
|
+
ownerFence: options.ownerFence,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
catch (error) {
|
|
101
|
+
this.#recordFailure(event, "attempts_exhausted", 0, error);
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
if (this.#queue.length >= this.#maxBufferedEvents) {
|
|
105
|
+
const oldest = this.#queue.shift();
|
|
106
|
+
if (oldest) {
|
|
107
|
+
oldest.dropped = true;
|
|
108
|
+
oldest.abortController?.abort();
|
|
109
|
+
this.#recordFailure(oldest.event, "buffer_overflow", oldest.attempts);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
this.#queue.push({
|
|
113
|
+
event,
|
|
114
|
+
rawBody,
|
|
115
|
+
idempotencyKey: options.idempotencyKey ?? event.eventId,
|
|
116
|
+
attempts: 0,
|
|
117
|
+
dropped: false,
|
|
118
|
+
});
|
|
119
|
+
this.#ensureDrain();
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
122
|
+
#ensureDrain() {
|
|
123
|
+
if (this.#drainPromise || this.#queue.length === 0)
|
|
124
|
+
return;
|
|
125
|
+
this.#drainPromise = Promise.resolve()
|
|
126
|
+
.then(() => this.#drain())
|
|
127
|
+
.finally(() => {
|
|
128
|
+
this.#drainPromise = undefined;
|
|
129
|
+
if (this.#queue.length > 0)
|
|
130
|
+
this.#ensureDrain();
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
async #drain() {
|
|
134
|
+
while (this.#queue.length > 0) {
|
|
135
|
+
const current = this.#queue[0];
|
|
136
|
+
if (!current)
|
|
137
|
+
return;
|
|
138
|
+
current.attempts += 1;
|
|
139
|
+
const delivered = await this.#send(current);
|
|
140
|
+
if (current.dropped)
|
|
141
|
+
continue;
|
|
142
|
+
if (delivered) {
|
|
143
|
+
if (this.#queue[0] === current)
|
|
144
|
+
this.#queue.shift();
|
|
145
|
+
this.#deliveredCount += 1;
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
if (current.attempts >= this.#maxAttempts) {
|
|
149
|
+
if (this.#queue[0] === current)
|
|
150
|
+
this.#queue.shift();
|
|
151
|
+
this.#recordFailure(current.event, "attempts_exhausted", current.attempts);
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
await delay(this.#retryDelay(current.attempts));
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
async #send(buffered) {
|
|
158
|
+
const timestamp = this.#clock().toISOString();
|
|
159
|
+
const path = "/v1/stateful/events";
|
|
160
|
+
const abortController = new AbortController();
|
|
161
|
+
buffered.abortController = abortController;
|
|
162
|
+
try {
|
|
163
|
+
const response = await this.#fetch(`${this.#baseUrl}${path}`, {
|
|
164
|
+
method: "POST",
|
|
165
|
+
headers: {
|
|
166
|
+
"content-type": "application/json",
|
|
167
|
+
"x-apifuse-event-id": buffered.idempotencyKey,
|
|
168
|
+
...statefulSignedHeaders({
|
|
169
|
+
secret: this.#secret,
|
|
170
|
+
timestamp,
|
|
171
|
+
rawBody: buffered.rawBody,
|
|
172
|
+
method: "POST",
|
|
173
|
+
path,
|
|
174
|
+
}),
|
|
175
|
+
},
|
|
176
|
+
body: buffered.rawBody,
|
|
177
|
+
signal: abortController.signal,
|
|
178
|
+
});
|
|
179
|
+
return response.ok;
|
|
180
|
+
}
|
|
181
|
+
catch {
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
finally {
|
|
185
|
+
if (buffered.abortController === abortController) {
|
|
186
|
+
buffered.abortController = undefined;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
#retryDelay(attempts) {
|
|
191
|
+
const exponential = Math.min(this.#retryMaxMs, this.#retryBaseMs * 2 ** Math.max(0, attempts - 1));
|
|
192
|
+
const jitter = 1 + (this.#random() * 2 - 1) * this.#jitterRatio;
|
|
193
|
+
return Math.max(0, Math.round(exponential * jitter));
|
|
194
|
+
}
|
|
195
|
+
#recordFailure(event, reason, attempts, error) {
|
|
196
|
+
this.#failedCount += 1;
|
|
197
|
+
this.#incrementDropMetric(event);
|
|
198
|
+
try {
|
|
199
|
+
const recorded = this.#failureRecorder.record({
|
|
200
|
+
eventId: event.eventId,
|
|
201
|
+
reason,
|
|
202
|
+
attempts,
|
|
203
|
+
failedAt: this.#clock().toISOString(),
|
|
204
|
+
...(error ? { error: "Provider event could not be serialized." } : {}),
|
|
205
|
+
});
|
|
206
|
+
void Promise.resolve(recorded).catch(() => { });
|
|
207
|
+
}
|
|
208
|
+
catch { }
|
|
209
|
+
}
|
|
210
|
+
#incrementDropMetric(event, emitter = this.#metricEmitter) {
|
|
211
|
+
try {
|
|
212
|
+
emitter.increment("apifuse_stateful_provider_event_drop_total", providerEventMetricLabels(event));
|
|
213
|
+
}
|
|
214
|
+
catch { }
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
function delay(ms) {
|
|
218
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
219
|
+
}
|
|
220
|
+
function positiveInteger(value, name) {
|
|
221
|
+
if (!Number.isInteger(value) || value <= 0) {
|
|
222
|
+
throw new Error(`Provider event ${name} must be a positive integer.`);
|
|
223
|
+
}
|
|
224
|
+
return value;
|
|
225
|
+
}
|
|
226
|
+
function nonnegativeFinite(value, name) {
|
|
227
|
+
if (!Number.isFinite(value) || value < 0) {
|
|
228
|
+
throw new Error(`Provider event ${name} must be a non-negative finite number.`);
|
|
229
|
+
}
|
|
230
|
+
return value;
|
|
231
|
+
}
|
|
232
|
+
function boundedRatio(value, name) {
|
|
233
|
+
if (!Number.isFinite(value) || value < 0 || value > 1) {
|
|
234
|
+
throw new Error(`Provider event ${name} must be between 0 and 1.`);
|
|
235
|
+
}
|
|
236
|
+
return value;
|
|
237
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { StatefulControlPlaneError, type StatefulControlPlaneOperation } from "./errors.js";
|
|
2
|
+
import { type SessionKey } from "./session-key.js";
|
|
3
|
+
import type { AcquireSessionOwnerInput, AcquireSessionOwnerResult, ReleaseSessionOwnerInput, RenewSessionOwnerInput, SessionOwnerRecord, SessionOwnerRegistry } from "./stateful-provider-session-runtime.js";
|
|
4
|
+
type FetchTransport = (url: string | URL | Request, init?: RequestInit) => Promise<Response>;
|
|
5
|
+
export type SessionOwnerScope = {
|
|
6
|
+
readonly serviceAccountId: string;
|
|
7
|
+
readonly providerId: string;
|
|
8
|
+
readonly connectionId?: string;
|
|
9
|
+
};
|
|
10
|
+
export type HttpSessionOwnerRegistryOptions = {
|
|
11
|
+
readonly baseUrl: string;
|
|
12
|
+
readonly secret: string;
|
|
13
|
+
readonly scope?: SessionOwnerScope;
|
|
14
|
+
readonly fetch?: FetchTransport;
|
|
15
|
+
readonly clock?: () => Date;
|
|
16
|
+
readonly requestTimeoutMs?: number;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* A mutating registry request failed after it may have reached the control plane. Callers must
|
|
20
|
+
* resolve the session owner and reconcile instead of blindly retrying the mutation.
|
|
21
|
+
*/
|
|
22
|
+
export declare class AmbiguousRegistryOperationError extends StatefulControlPlaneError {
|
|
23
|
+
readonly ambiguous = true;
|
|
24
|
+
constructor(input: {
|
|
25
|
+
readonly operation: Exclude<StatefulControlPlaneOperation, "resolve">;
|
|
26
|
+
readonly message: string;
|
|
27
|
+
readonly cause: unknown;
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
export type HttpSessionOwnerRecord = Omit<SessionOwnerRecord, "sessionKey"> & {
|
|
31
|
+
readonly sessionKey: SessionKey;
|
|
32
|
+
};
|
|
33
|
+
export type HttpAcquireSessionOwnerResult = Omit<AcquireSessionOwnerResult, "record"> & {
|
|
34
|
+
readonly record: HttpSessionOwnerRecord;
|
|
35
|
+
};
|
|
36
|
+
export declare class HttpSessionOwnerRegistry implements SessionOwnerRegistry {
|
|
37
|
+
#private;
|
|
38
|
+
constructor(options: HttpSessionOwnerRegistryOptions);
|
|
39
|
+
resolve(sessionKey: SessionKey, now?: Date, signal?: AbortSignal): Promise<HttpSessionOwnerRecord | null>;
|
|
40
|
+
acquire(input: AcquireSessionOwnerInput, signal?: AbortSignal): Promise<HttpAcquireSessionOwnerResult>;
|
|
41
|
+
renew(input: RenewSessionOwnerInput, signal?: AbortSignal): Promise<HttpSessionOwnerRecord | null>;
|
|
42
|
+
release(input: ReleaseSessionOwnerInput, signal?: AbortSignal): Promise<boolean>;
|
|
43
|
+
}
|
|
44
|
+
export {};
|