@apifuse/provider-sdk 2.2.0-beta.4 → 2.2.0-beta.40
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 +501 -13
- package/CHANGELOG.md +165 -1
- package/README.md +72 -14
- package/SUBMISSION.md +1 -1
- package/bin/apifuse-check.ts +88 -4
- package/bin/apifuse-dev.ts +38 -5
- package/bin/apifuse-pack-check.ts +22 -2
- package/bin/apifuse-pack-smoke.ts +57 -2
- package/bin/apifuse-pack-types.ts +356 -38
- package/bin/apifuse-perf.ts +14 -13
- package/bin/apifuse-record.ts +691 -68
- package/bin/apifuse-submit-check.ts +518 -37
- 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 +8 -0
- package/dist/cli/create.d.ts +3 -0
- package/dist/cli/create.js +34 -35
- 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 +4 -4
- package/dist/cli/templates/provider/index.ts.tpl +6 -3
- package/dist/cli/templates/provider/operations/ping.ts.tpl +2 -1
- package/dist/config/loader.d.ts +177 -16
- package/dist/config/loader.js +424 -127
- package/dist/contract-serialization.js +4 -8
- package/dist/contract-types.d.ts +1 -0
- package/dist/contract.js +2 -0
- package/dist/declaration-validation.d.ts +32 -0
- package/dist/declaration-validation.js +207 -0
- package/dist/define.d.ts +51 -25
- package/dist/define.js +752 -38
- package/dist/error-resolution.d.ts +4 -0
- package/dist/error-resolution.js +122 -0
- package/dist/errors.d.ts +18 -0
- package/dist/errors.js +40 -0
- package/dist/fixture-sanitization.d.ts +28 -0
- package/dist/fixture-sanitization.js +217 -0
- package/dist/health-scenario.d.ts +1842 -0
- package/dist/health-scenario.js +624 -0
- package/dist/index.d.ts +19 -9
- package/dist/index.js +10 -6
- package/dist/lint.d.ts +6 -1
- package/dist/lint.js +362 -3
- 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 +8 -2
- package/dist/provider.js +3 -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/executor.d.ts +2 -2
- package/dist/runtime/executor.js +26 -2
- package/dist/runtime/http.d.ts +1 -0
- package/dist/runtime/http.js +515 -53
- 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/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 +3 -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 +526 -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 +407 -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 +60 -0
- package/dist/runtime/resolver.js +737 -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 +28 -3
- package/dist/runtime/stealth.js +519 -255
- 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 +61 -0
- package/dist/serve.d.ts +1 -1
- package/dist/serve.js +1 -1
- package/dist/server/index.d.ts +5 -3
- package/dist/server/index.js +3 -3
- 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 +114 -0
- package/dist/server/self-test.js +784 -148
- package/dist/server/serve-implementation.d.ts +213 -0
- package/dist/server/serve-implementation.js +2173 -0
- package/dist/server/serve.d.ts +1 -70
- package/dist/server/serve.js +1 -1130
- package/dist/server/trace-output.d.ts +4 -0
- package/dist/server/trace-output.js +20 -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.js +16 -7
- 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 +545 -23
- package/dist/types.js +1 -0
- package/package.json +44 -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/commands.ts +10 -0
- package/src/cli/create.ts +42 -35
- 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 +4 -4
- package/src/cli/templates/provider/index.ts.tpl +6 -3
- package/src/cli/templates/provider/operations/ping.ts.tpl +2 -1
- package/src/config/loader.ts +665 -163
- package/src/contract-serialization.ts +5 -7
- package/src/contract-types.ts +1 -0
- package/src/contract.ts +2 -0
- package/src/declaration-validation.ts +266 -0
- package/src/define.ts +970 -87
- package/src/error-resolution.ts +127 -0
- package/src/errors.ts +52 -0
- package/src/fixture-sanitization.ts +248 -0
- package/src/health-scenario.ts +875 -0
- package/src/index.ts +204 -8
- package/src/lint.ts +408 -4
- 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 +133 -0
- 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/executor.ts +40 -7
- package/src/runtime/http.ts +641 -61
- 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/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 +20 -0
- package/src/runtime/resolver-shared.ts +17 -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 +700 -0
- package/src/runtime/resolver-vendors/hosts.ts +38 -0
- package/src/runtime/resolver-vendors/twocaptcha.ts +539 -0
- package/src/runtime/resolver-vendors/types.ts +212 -0
- package/src/runtime/resolver.ts +1103 -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 +675 -289
- package/src/runtime/stt.ts +1 -19
- package/src/runtime/timeout.ts +18 -0
- package/src/runtime/trace-config.ts +77 -0
- package/src/serve.ts +6 -1
- package/src/server/index.ts +37 -2
- package/src/server/self-test-input-tokens.ts +29 -14
- package/src/server/self-test.ts +1025 -175
- package/src/server/serve-implementation.ts +3250 -0
- package/src/server/serve.ts +1 -1626
- package/src/server/trace-output.ts +32 -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 +17 -7
- 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 +660 -38
- 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,403 @@
|
|
|
1
|
+
import type { SessionKey } from "./session-key.js";
|
|
2
|
+
|
|
3
|
+
/** @deprecated Use SessionKey from session-key.ts. */
|
|
4
|
+
export type StatefulProviderSessionKey = SessionKey;
|
|
5
|
+
export type SessionOwnerStatus = "acquiring" | "connected" | "draining" | "expired";
|
|
6
|
+
|
|
7
|
+
export interface SessionOwnerRecord {
|
|
8
|
+
/** Runtime-decoded key; registry inputs use the opaque SessionKey type. */
|
|
9
|
+
readonly sessionKey: string;
|
|
10
|
+
readonly ownerPodId: string;
|
|
11
|
+
readonly ownerEndpoint: string;
|
|
12
|
+
readonly generation: number;
|
|
13
|
+
readonly leaseExpiresAt: string;
|
|
14
|
+
readonly status: SessionOwnerStatus;
|
|
15
|
+
readonly lastUsedAt: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface AcquireSessionOwnerInput {
|
|
19
|
+
readonly sessionKey: SessionKey;
|
|
20
|
+
readonly ownerPodId: string;
|
|
21
|
+
readonly ownerEndpoint: string;
|
|
22
|
+
readonly leaseDurationMs: number;
|
|
23
|
+
readonly status?: Exclude<SessionOwnerStatus, "expired">;
|
|
24
|
+
readonly now?: Date;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface AcquireSessionOwnerResult {
|
|
28
|
+
readonly record: SessionOwnerRecord;
|
|
29
|
+
readonly acquired: boolean;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface RenewSessionOwnerInput {
|
|
33
|
+
readonly sessionKey: SessionKey;
|
|
34
|
+
readonly ownerPodId: string;
|
|
35
|
+
readonly generation: number;
|
|
36
|
+
readonly leaseDurationMs: number;
|
|
37
|
+
readonly status?: Exclude<SessionOwnerStatus, "expired">;
|
|
38
|
+
readonly now?: Date;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface ReleaseSessionOwnerInput {
|
|
42
|
+
readonly sessionKey: SessionKey;
|
|
43
|
+
readonly ownerPodId: string;
|
|
44
|
+
readonly generation: number;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* A session-owner registry is a fencing authority. Generations MUST be positive integers and
|
|
49
|
+
* strictly increase for each successful takeover of a session key, including after expiry or
|
|
50
|
+
* release. Implementations must retain a high-water mark (a tombstone) for at least the registry's
|
|
51
|
+
* lifetime; generation values must never be reused.
|
|
52
|
+
*/
|
|
53
|
+
export interface SessionOwnerRegistry {
|
|
54
|
+
resolve(
|
|
55
|
+
sessionKey: SessionKey,
|
|
56
|
+
now?: Date,
|
|
57
|
+
signal?: AbortSignal,
|
|
58
|
+
): Promise<SessionOwnerRecord | null>;
|
|
59
|
+
acquire(
|
|
60
|
+
input: AcquireSessionOwnerInput,
|
|
61
|
+
signal?: AbortSignal,
|
|
62
|
+
): Promise<AcquireSessionOwnerResult>;
|
|
63
|
+
renew(input: RenewSessionOwnerInput, signal?: AbortSignal): Promise<SessionOwnerRecord | null>;
|
|
64
|
+
release(input: ReleaseSessionOwnerInput, signal?: AbortSignal): Promise<boolean>;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface SessionPoolPolicy {
|
|
68
|
+
readonly maxSessions: number;
|
|
69
|
+
/** Disable idle eviction for connection-owned listeners with `"unlimited"`. */
|
|
70
|
+
readonly idleTimeoutMs: number | "unlimited";
|
|
71
|
+
/** Disable age-based recycling for expensive healthy sessions with `"unlimited"`. */
|
|
72
|
+
readonly maxLifetimeMs: number | "unlimited";
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface ManagedSessionIdentity {
|
|
76
|
+
readonly connectionId: string;
|
|
77
|
+
readonly serviceAccountId: string;
|
|
78
|
+
readonly ownerPodId: string;
|
|
79
|
+
readonly ownerEndpoint: string;
|
|
80
|
+
readonly ownerStatus: SessionOwnerStatus;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export interface ManagedSession<T> {
|
|
84
|
+
readonly sessionKey: string;
|
|
85
|
+
readonly generation: number;
|
|
86
|
+
readonly value: T;
|
|
87
|
+
readonly createdAt: string;
|
|
88
|
+
readonly lastUsedAt: string;
|
|
89
|
+
readonly identity?: ManagedSessionIdentity;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
type SessionFactory<T> = () => T | Promise<T>;
|
|
93
|
+
type SessionCloseHook<T> = (session: ManagedSession<T>, reason: string) => void | Promise<void>;
|
|
94
|
+
export class InMemorySessionOwnerRegistry implements SessionOwnerRegistry {
|
|
95
|
+
readonly #owners = new Map<SessionKey, SessionOwnerRecord>();
|
|
96
|
+
readonly #generationHighWater = new Map<SessionKey, number>();
|
|
97
|
+
|
|
98
|
+
async resolve(
|
|
99
|
+
sessionKey: SessionKey,
|
|
100
|
+
now: Date = new Date(),
|
|
101
|
+
signal?: AbortSignal,
|
|
102
|
+
): Promise<SessionOwnerRecord | null> {
|
|
103
|
+
signal?.throwIfAborted();
|
|
104
|
+
const current = this.#owners.get(sessionKey);
|
|
105
|
+
if (!current || isLeaseExpired(current, now)) return null;
|
|
106
|
+
validateGeneration(current.generation);
|
|
107
|
+
return current;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async acquire(
|
|
111
|
+
input: AcquireSessionOwnerInput,
|
|
112
|
+
signal?: AbortSignal,
|
|
113
|
+
): Promise<AcquireSessionOwnerResult> {
|
|
114
|
+
signal?.throwIfAborted();
|
|
115
|
+
validateLeaseDuration(input.leaseDurationMs);
|
|
116
|
+
const now = input.now ?? new Date();
|
|
117
|
+
const current = this.#owners.get(input.sessionKey);
|
|
118
|
+
if (current && !isLeaseExpired(current, now)) {
|
|
119
|
+
validateGeneration(current.generation);
|
|
120
|
+
if (current.ownerPodId !== input.ownerPodId) {
|
|
121
|
+
return { record: current, acquired: false };
|
|
122
|
+
}
|
|
123
|
+
const record = makeOwnerRecord(input, current.generation, now);
|
|
124
|
+
this.#owners.set(input.sessionKey, record);
|
|
125
|
+
return { record, acquired: true };
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const generation =
|
|
129
|
+
Math.max(current?.generation ?? 0, this.#generationHighWater.get(input.sessionKey) ?? 0) + 1;
|
|
130
|
+
const record = makeOwnerRecord(input, generation, now);
|
|
131
|
+
this.#owners.set(input.sessionKey, record);
|
|
132
|
+
this.#generationHighWater.set(input.sessionKey, generation);
|
|
133
|
+
return { record, acquired: true };
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
async renew(
|
|
137
|
+
input: RenewSessionOwnerInput,
|
|
138
|
+
signal?: AbortSignal,
|
|
139
|
+
): Promise<SessionOwnerRecord | null> {
|
|
140
|
+
signal?.throwIfAborted();
|
|
141
|
+
validateGeneration(input.generation);
|
|
142
|
+
validateLeaseDuration(input.leaseDurationMs);
|
|
143
|
+
const now = input.now ?? new Date();
|
|
144
|
+
const current = this.#owners.get(input.sessionKey);
|
|
145
|
+
if (
|
|
146
|
+
!current ||
|
|
147
|
+
current.ownerPodId !== input.ownerPodId ||
|
|
148
|
+
current.generation !== input.generation ||
|
|
149
|
+
isLeaseExpired(current, now)
|
|
150
|
+
) {
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const record: SessionOwnerRecord = {
|
|
155
|
+
...current,
|
|
156
|
+
leaseExpiresAt: addMs(now, input.leaseDurationMs).toISOString(),
|
|
157
|
+
status: input.status ?? current.status,
|
|
158
|
+
lastUsedAt: now.toISOString(),
|
|
159
|
+
};
|
|
160
|
+
this.#owners.set(input.sessionKey, record);
|
|
161
|
+
return record;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
async release(input: ReleaseSessionOwnerInput, signal?: AbortSignal): Promise<boolean> {
|
|
165
|
+
signal?.throwIfAborted();
|
|
166
|
+
validateGeneration(input.generation);
|
|
167
|
+
const current = this.#owners.get(input.sessionKey);
|
|
168
|
+
if (
|
|
169
|
+
!current ||
|
|
170
|
+
current.ownerPodId !== input.ownerPodId ||
|
|
171
|
+
current.generation !== input.generation
|
|
172
|
+
) {
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
this.#generationHighWater.set(
|
|
176
|
+
input.sessionKey,
|
|
177
|
+
Math.max(this.#generationHighWater.get(input.sessionKey) ?? 0, current.generation),
|
|
178
|
+
);
|
|
179
|
+
this.#owners.delete(input.sessionKey);
|
|
180
|
+
return true;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export class PodLocalSessionPool<T> {
|
|
185
|
+
readonly #sessions = new Map<string, ManagedSession<T>>();
|
|
186
|
+
readonly #queues = new Map<string, Promise<void>>();
|
|
187
|
+
readonly #creates = new Map<string, Promise<ManagedSession<T>>>();
|
|
188
|
+
#closed = false;
|
|
189
|
+
|
|
190
|
+
constructor(
|
|
191
|
+
private readonly policy: SessionPoolPolicy,
|
|
192
|
+
private readonly closeSession: SessionCloseHook<T>,
|
|
193
|
+
) {
|
|
194
|
+
validatePoolPolicy(policy);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
async getOrCreate(
|
|
198
|
+
sessionKey: string,
|
|
199
|
+
generation: number,
|
|
200
|
+
factory: SessionFactory<T>,
|
|
201
|
+
now: Date = new Date(),
|
|
202
|
+
identity?: ManagedSessionIdentity,
|
|
203
|
+
): Promise<ManagedSession<T>> {
|
|
204
|
+
validateGeneration(generation);
|
|
205
|
+
this.assertOpen(sessionKey);
|
|
206
|
+
const existingCreate = this.#creates.get(sessionKey);
|
|
207
|
+
if (existingCreate) {
|
|
208
|
+
try {
|
|
209
|
+
await existingCreate;
|
|
210
|
+
} catch {}
|
|
211
|
+
this.assertOpen(sessionKey);
|
|
212
|
+
}
|
|
213
|
+
const create = this.getOrCreateUnlocked(sessionKey, generation, factory, now, identity);
|
|
214
|
+
this.#creates.set(sessionKey, create);
|
|
215
|
+
try {
|
|
216
|
+
const session = await create;
|
|
217
|
+
this.assertOpen(sessionKey);
|
|
218
|
+
return session;
|
|
219
|
+
} finally {
|
|
220
|
+
if (this.#creates.get(sessionKey) === create) this.#creates.delete(sessionKey);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
private async getOrCreateUnlocked(
|
|
225
|
+
sessionKey: string,
|
|
226
|
+
generation: number,
|
|
227
|
+
factory: SessionFactory<T>,
|
|
228
|
+
now: Date,
|
|
229
|
+
identity?: ManagedSessionIdentity,
|
|
230
|
+
): Promise<ManagedSession<T>> {
|
|
231
|
+
await this.evictExpired(now);
|
|
232
|
+
|
|
233
|
+
const current = this.#sessions.get(sessionKey);
|
|
234
|
+
if (current && current.generation === generation) {
|
|
235
|
+
const touched = { ...current, lastUsedAt: now.toISOString() };
|
|
236
|
+
this.#sessions.delete(sessionKey);
|
|
237
|
+
this.#sessions.set(sessionKey, touched);
|
|
238
|
+
return touched;
|
|
239
|
+
}
|
|
240
|
+
if (current) await this.closeOne(sessionKey, "generation-changed");
|
|
241
|
+
|
|
242
|
+
const session: ManagedSession<T> = {
|
|
243
|
+
sessionKey,
|
|
244
|
+
generation,
|
|
245
|
+
value: await factory(),
|
|
246
|
+
createdAt: now.toISOString(),
|
|
247
|
+
lastUsedAt: now.toISOString(),
|
|
248
|
+
...(identity ? { identity } : {}),
|
|
249
|
+
};
|
|
250
|
+
this.#sessions.set(sessionKey, session);
|
|
251
|
+
await this.evictOverCapacity();
|
|
252
|
+
return session;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
async closeAll(reason: string): Promise<void> {
|
|
256
|
+
this.#closed = true;
|
|
257
|
+
const errors: unknown[] = [];
|
|
258
|
+
await Promise.all(
|
|
259
|
+
[...this.#creates.values()].map(async (create) => {
|
|
260
|
+
let session: ManagedSession<T>;
|
|
261
|
+
try {
|
|
262
|
+
session = await create;
|
|
263
|
+
} catch {
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
try {
|
|
267
|
+
await this.closeOne(session.sessionKey, reason);
|
|
268
|
+
} catch (error) {
|
|
269
|
+
errors.push(error);
|
|
270
|
+
}
|
|
271
|
+
}),
|
|
272
|
+
);
|
|
273
|
+
for (const sessionKey of [...this.#sessions.keys()]) {
|
|
274
|
+
try {
|
|
275
|
+
await this.closeOne(sessionKey, reason);
|
|
276
|
+
} catch (error) {
|
|
277
|
+
errors.push(error);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
if (errors.length > 0) {
|
|
281
|
+
throw new AggregateError(errors, `Failed to close ${errors.length} stateful session(s).`);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
async invalidate(sessionKey: string, reason: string): Promise<void> {
|
|
286
|
+
await this.closeOne(sessionKey, reason);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
async runExclusive<R>(sessionKey: string, task: () => R | Promise<R>): Promise<R> {
|
|
290
|
+
const previous = this.#queues.get(sessionKey) ?? Promise.resolve();
|
|
291
|
+
const next = previous.then(task, task);
|
|
292
|
+
const settled = next.then(
|
|
293
|
+
() => undefined,
|
|
294
|
+
() => undefined,
|
|
295
|
+
);
|
|
296
|
+
this.#queues.set(sessionKey, settled);
|
|
297
|
+
await settled.finally(() => {
|
|
298
|
+
if (this.#queues.get(sessionKey) === settled) {
|
|
299
|
+
this.#queues.delete(sessionKey);
|
|
300
|
+
}
|
|
301
|
+
});
|
|
302
|
+
return next;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
private async evictExpired(now: Date): Promise<void> {
|
|
306
|
+
for (const [sessionKey, session] of this.#sessions) {
|
|
307
|
+
if (isSessionExpired(session, this.policy, now)) {
|
|
308
|
+
await this.closeOne(sessionKey, "expired");
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
private async evictOverCapacity(): Promise<void> {
|
|
314
|
+
while (this.#sessions.size > this.policy.maxSessions) {
|
|
315
|
+
const lruKey = this.#sessions.keys().next().value;
|
|
316
|
+
if (lruKey === undefined) return;
|
|
317
|
+
await this.closeOne(lruKey, "capacity");
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
private async closeOne(sessionKey: string, reason: string): Promise<void> {
|
|
322
|
+
const session = this.#sessions.get(sessionKey);
|
|
323
|
+
if (!session) return;
|
|
324
|
+
this.#sessions.delete(sessionKey);
|
|
325
|
+
await this.closeSession(session, reason);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
private assertOpen(sessionKey: string): void {
|
|
329
|
+
if (!this.#closed) return;
|
|
330
|
+
throw new Error(
|
|
331
|
+
`Pod-local session pool is closed; cannot get or create session "${sessionKey}".`,
|
|
332
|
+
);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
function makeOwnerRecord(
|
|
337
|
+
input: AcquireSessionOwnerInput,
|
|
338
|
+
generation: number,
|
|
339
|
+
now: Date,
|
|
340
|
+
): SessionOwnerRecord {
|
|
341
|
+
validateGeneration(generation);
|
|
342
|
+
return {
|
|
343
|
+
sessionKey: input.sessionKey,
|
|
344
|
+
ownerPodId: input.ownerPodId,
|
|
345
|
+
ownerEndpoint: input.ownerEndpoint,
|
|
346
|
+
generation,
|
|
347
|
+
leaseExpiresAt: addMs(now, input.leaseDurationMs).toISOString(),
|
|
348
|
+
status: input.status ?? "acquiring",
|
|
349
|
+
lastUsedAt: now.toISOString(),
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
function isLeaseExpired(record: SessionOwnerRecord, now: Date): boolean {
|
|
354
|
+
return Date.parse(record.leaseExpiresAt) <= now.getTime();
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
function isSessionExpired<T>(
|
|
358
|
+
session: ManagedSession<T>,
|
|
359
|
+
policy: SessionPoolPolicy,
|
|
360
|
+
now: Date,
|
|
361
|
+
): boolean {
|
|
362
|
+
const nowMs = now.getTime();
|
|
363
|
+
return (
|
|
364
|
+
(policy.idleTimeoutMs !== "unlimited" &&
|
|
365
|
+
nowMs - Date.parse(session.lastUsedAt) >= policy.idleTimeoutMs) ||
|
|
366
|
+
(policy.maxLifetimeMs !== "unlimited" &&
|
|
367
|
+
nowMs - Date.parse(session.createdAt) >= policy.maxLifetimeMs)
|
|
368
|
+
);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
function validateLeaseDuration(leaseDurationMs: number): void {
|
|
372
|
+
if (!Number.isFinite(leaseDurationMs) || leaseDurationMs <= 0) {
|
|
373
|
+
throw new Error("Session owner leaseDurationMs must be a positive finite number.");
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
function validateGeneration(generation: number): void {
|
|
378
|
+
if (!Number.isInteger(generation) || generation <= 0) {
|
|
379
|
+
throw new Error("Session owner generation must be a positive integer.");
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
function validatePoolPolicy(policy: SessionPoolPolicy): void {
|
|
384
|
+
if (!Number.isInteger(policy.maxSessions) || policy.maxSessions <= 0) {
|
|
385
|
+
throw new Error("Session pool maxSessions must be a positive integer.");
|
|
386
|
+
}
|
|
387
|
+
if (
|
|
388
|
+
policy.idleTimeoutMs !== "unlimited" &&
|
|
389
|
+
(!Number.isFinite(policy.idleTimeoutMs) || policy.idleTimeoutMs <= 0)
|
|
390
|
+
) {
|
|
391
|
+
throw new Error('Session pool idleTimeoutMs must be a positive finite number or "unlimited".');
|
|
392
|
+
}
|
|
393
|
+
if (
|
|
394
|
+
policy.maxLifetimeMs !== "unlimited" &&
|
|
395
|
+
(!Number.isFinite(policy.maxLifetimeMs) || policy.maxLifetimeMs <= 0)
|
|
396
|
+
) {
|
|
397
|
+
throw new Error('Session pool maxLifetimeMs must be a positive finite number or "unlimited".');
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
function addMs(date: Date, ms: number): Date {
|
|
402
|
+
return new Date(date.getTime() + ms);
|
|
403
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { createHmac, randomUUID, timingSafeEqual } from "node:crypto";
|
|
2
|
+
|
|
3
|
+
export const STATEFUL_SIGNATURE_HEADER = "x-apifuse-stateful-signature";
|
|
4
|
+
export const STATEFUL_TIMESTAMP_HEADER = "x-apifuse-stateful-timestamp";
|
|
5
|
+
export const STATEFUL_NONCE_HEADER = "x-apifuse-stateful-nonce";
|
|
6
|
+
|
|
7
|
+
export type StatefulSigningInput = {
|
|
8
|
+
readonly secret: string;
|
|
9
|
+
readonly timestamp: string;
|
|
10
|
+
readonly rawBody: string;
|
|
11
|
+
readonly method: string;
|
|
12
|
+
readonly path: string;
|
|
13
|
+
readonly nonce: string;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export function signStatefulRequestBody(input: StatefulSigningInput): string {
|
|
17
|
+
return `v1=${createHmac("sha256", input.secret)
|
|
18
|
+
.update(
|
|
19
|
+
`v1:${input.method.toUpperCase()}:${input.path}:${input.timestamp}:${input.nonce}.${input.rawBody}`,
|
|
20
|
+
)
|
|
21
|
+
.digest("hex")}`;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function verifyStatefulRequestSignature(
|
|
25
|
+
input: StatefulSigningInput & { readonly signature: string },
|
|
26
|
+
): boolean {
|
|
27
|
+
return safeEqualAscii(input.signature, signStatefulRequestBody(input));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function statefulSignedHeaders(
|
|
31
|
+
input: Omit<StatefulSigningInput, "nonce"> & { readonly nonce?: string },
|
|
32
|
+
): Record<string, string> {
|
|
33
|
+
const nonce = input.nonce ?? randomUUID();
|
|
34
|
+
return {
|
|
35
|
+
[STATEFUL_SIGNATURE_HEADER]: signStatefulRequestBody({ ...input, nonce }),
|
|
36
|
+
[STATEFUL_TIMESTAMP_HEADER]: input.timestamp,
|
|
37
|
+
[STATEFUL_NONCE_HEADER]: nonce,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function safeEqualAscii(actual: string, expected: string): boolean {
|
|
42
|
+
const actualBytes = Buffer.from(actual);
|
|
43
|
+
const expectedBytes = Buffer.from(expected);
|
|
44
|
+
if (actualBytes.byteLength !== expectedBytes.byteLength) return false;
|
|
45
|
+
return timingSafeEqual(actualBytes, expectedBytes);
|
|
46
|
+
}
|
package/src/stealth/profiles.ts
CHANGED
|
@@ -68,7 +68,7 @@ const FIREFOX_H2_SETTINGS = {
|
|
|
68
68
|
};
|
|
69
69
|
|
|
70
70
|
const SAFARI_H2_SETTINGS = {
|
|
71
|
-
|
|
71
|
+
// Safari 17 also sends a connection-level WINDOW_UPDATE increment of 10485760.
|
|
72
72
|
ENABLE_PUSH: 0,
|
|
73
73
|
INITIAL_WINDOW_SIZE: 4194304,
|
|
74
74
|
MAX_CONCURRENT_STREAMS: 100,
|
|
@@ -192,9 +192,9 @@ const STEALTH_PROFILES: Record<string, StealthProfile> = {
|
|
|
192
192
|
}),
|
|
193
193
|
"firefox-132": createProfile("firefox-132", {
|
|
194
194
|
platform: "macos",
|
|
195
|
-
version: "
|
|
195
|
+
version: "133.0",
|
|
196
196
|
userAgent:
|
|
197
|
-
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:
|
|
197
|
+
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:133.0) Gecko/20100101 Firefox/133.0",
|
|
198
198
|
tlsClientIdentifier: "firefox_132",
|
|
199
199
|
ja3: FIREFOX_JA3,
|
|
200
200
|
h2Settings: FIREFOX_H2_SETTINGS,
|
|
@@ -210,6 +210,16 @@ const STEALTH_PROFILES: Record<string, StealthProfile> = {
|
|
|
210
210
|
h2Settings: SAFARI_H2_SETTINGS,
|
|
211
211
|
headerOrder: SAFARI_HEADER_ORDER,
|
|
212
212
|
}),
|
|
213
|
+
"safari-17": createProfile("safari-17", {
|
|
214
|
+
platform: "macos",
|
|
215
|
+
version: "17.0",
|
|
216
|
+
userAgent:
|
|
217
|
+
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Safari/605.1.15",
|
|
218
|
+
tlsClientIdentifier: "safari_17_0",
|
|
219
|
+
ja3: SAFARI_JA3,
|
|
220
|
+
h2Settings: SAFARI_H2_SETTINGS,
|
|
221
|
+
headerOrder: SAFARI_HEADER_ORDER,
|
|
222
|
+
}),
|
|
213
223
|
"safari-15": createProfile("safari-15", {
|
|
214
224
|
platform: "macos",
|
|
215
225
|
version: "15.6.1",
|
|
@@ -232,9 +242,9 @@ const STEALTH_PROFILES: Record<string, StealthProfile> = {
|
|
|
232
242
|
}),
|
|
233
243
|
"ios-safari-18": createProfile("ios-safari-18", {
|
|
234
244
|
platform: "ios",
|
|
235
|
-
version: "18.
|
|
245
|
+
version: "18.1.1",
|
|
236
246
|
userAgent:
|
|
237
|
-
"Mozilla/5.0 (iPhone; CPU iPhone OS
|
|
247
|
+
"Mozilla/5.0 (iPhone; CPU iPhone OS 18_1_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.1.1 Mobile/15E148 Safari/604.1",
|
|
238
248
|
tlsClientIdentifier: "safari_ios_18_0",
|
|
239
249
|
ja3: SAFARI_JA3,
|
|
240
250
|
h2Settings: SAFARI_H2_SETTINGS,
|
|
@@ -242,9 +252,9 @@ const STEALTH_PROFILES: Record<string, StealthProfile> = {
|
|
|
242
252
|
}),
|
|
243
253
|
"ios-safari-17": createProfile("ios-safari-17", {
|
|
244
254
|
platform: "ios",
|
|
245
|
-
version: "17.
|
|
255
|
+
version: "17.2",
|
|
246
256
|
userAgent:
|
|
247
|
-
"Mozilla/5.0 (iPhone; CPU iPhone OS
|
|
257
|
+
"Mozilla/5.0 (iPhone; CPU iPhone OS 17_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2 Mobile/15E148 Safari/604.1",
|
|
248
258
|
tlsClientIdentifier: "safari_ios_17_0",
|
|
249
259
|
ja3: SAFARI_JA3,
|
|
250
260
|
h2Settings: SAFARI_H2_SETTINGS,
|