@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,40 @@
|
|
|
1
|
+
// An IP-bound artifact minted without any recorded egress identity is unsafe to
|
|
2
|
+
// share. The Akamai kinds therefore reject direct caching, while Cloudflare
|
|
3
|
+
// keeps its pre-existing direct-cache behavior pending measurement.
|
|
4
|
+
export const RESOLVER_CHALLENGE_BINDINGS = {
|
|
5
|
+
turnstile: { cacheable: false, identityBinding: "none", directCacheable: false },
|
|
6
|
+
recaptcha_v2: { cacheable: false, identityBinding: "none", directCacheable: false },
|
|
7
|
+
recaptcha_v3: { cacheable: false, identityBinding: "none", directCacheable: false },
|
|
8
|
+
hcaptcha: { cacheable: false, identityBinding: "none", directCacheable: false },
|
|
9
|
+
cloudflare_interstitial: {
|
|
10
|
+
cacheable: true,
|
|
11
|
+
identityBinding: "identity_scoped",
|
|
12
|
+
directCacheable: true,
|
|
13
|
+
},
|
|
14
|
+
aws_waf: { cacheable: true, identityBinding: "portable", directCacheable: true },
|
|
15
|
+
akamai_sec_cpt: {
|
|
16
|
+
cacheable: true,
|
|
17
|
+
identityBinding: "identity_scoped",
|
|
18
|
+
directCacheable: false,
|
|
19
|
+
},
|
|
20
|
+
akamai_sensor: {
|
|
21
|
+
cacheable: true,
|
|
22
|
+
identityBinding: "identity_scoped",
|
|
23
|
+
directCacheable: false,
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
export function resolverChallengeIsCacheable(challenge) {
|
|
27
|
+
return RESOLVER_CHALLENGE_BINDINGS[challenge.kind].cacheable;
|
|
28
|
+
}
|
|
29
|
+
export function resolverChallengeAllowsDirectCache(challenge) {
|
|
30
|
+
return RESOLVER_CHALLENGE_BINDINGS[challenge.kind].directCacheable;
|
|
31
|
+
}
|
|
32
|
+
export function resolverChallengeIsIdentityScoped(challenge) {
|
|
33
|
+
return RESOLVER_CHALLENGE_BINDINGS[challenge.kind].identityBinding === "identity_scoped";
|
|
34
|
+
}
|
|
35
|
+
export function resolverChallengeIssuingIdentity(challenge, identity) {
|
|
36
|
+
if (RESOLVER_CHALLENGE_BINDINGS[challenge.kind].identityBinding === "portable") {
|
|
37
|
+
return { userAgent: identity.userAgent };
|
|
38
|
+
}
|
|
39
|
+
return identity;
|
|
40
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { BrowserClient, ChallengeSolution, ProviderChallenge } from "../../types.js";
|
|
2
|
+
import { type BrowserClientOptions } from "../browser.js";
|
|
3
|
+
import type { TraceRecorder } from "../trace.js";
|
|
4
|
+
import { type ResolverIdentity, type ResolverVendorAdapter } from "./types.js";
|
|
5
|
+
type BrowserClientFactory = (options: BrowserClientOptions) => BrowserClient;
|
|
6
|
+
/** Internal test seam; deliberately not re-exported from the package root. */
|
|
7
|
+
export declare function swapBrowserResolverClientFactoryForTests(factory: BrowserClientFactory | undefined): () => void;
|
|
8
|
+
export interface BrowserResolverVendorOptions {
|
|
9
|
+
readonly cdpUrl?: string;
|
|
10
|
+
readonly timeoutMs: number;
|
|
11
|
+
readonly pollIntervalMs?: number;
|
|
12
|
+
readonly allowedHosts: readonly string[];
|
|
13
|
+
readonly createClient?: BrowserClientFactory;
|
|
14
|
+
}
|
|
15
|
+
export interface BrowserResolverVendorAdapter extends ResolverVendorAdapter {
|
|
16
|
+
readonly id: "browser";
|
|
17
|
+
solve(challenge: ProviderChallenge, identity: ResolverIdentity | undefined, signal: AbortSignal, traceRecorder?: TraceRecorder): Promise<Extract<ChallengeSolution, {
|
|
18
|
+
readonly form: "cookies";
|
|
19
|
+
}>>;
|
|
20
|
+
}
|
|
21
|
+
export declare function createBrowserResolverVendorAdapter(options: BrowserResolverVendorOptions): BrowserResolverVendorAdapter;
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
import { isProviderError } from "../../errors.js";
|
|
2
|
+
import { createBrowserClient } from "../browser.js";
|
|
3
|
+
import { resolverChallengeIssuingIdentity } from "./bindings.js";
|
|
4
|
+
import { assertResolverHostAllowed, normalizedResolverHostname } from "./hosts.js";
|
|
5
|
+
import { ResolverVendorUnavailableError, } from "./types.js";
|
|
6
|
+
const BROWSER_VENDOR_ID = "browser";
|
|
7
|
+
const DEFAULT_COOKIE_POLL_INTERVAL_MS = 100;
|
|
8
|
+
const NAVIGATION_BLOCKED_ERROR_TEXT = "net::ERR_BLOCKED_BY_CLIENT";
|
|
9
|
+
const AWS_WAF_CHALLENGE_INFRASTRUCTURE_HOST_SUFFIX = ".awswaf.com";
|
|
10
|
+
const RESOLVER_DOCUMENT_CONTENT_SECURITY_POLICY = "connect-src http: https:; worker-src 'none'";
|
|
11
|
+
const SUCCESS_COOKIE_NAMES = {
|
|
12
|
+
aws_waf: "aws-waf-token",
|
|
13
|
+
cloudflare_interstitial: "cf_clearance",
|
|
14
|
+
};
|
|
15
|
+
let createResolverBrowserClient = createBrowserClient;
|
|
16
|
+
/** Internal test seam; deliberately not re-exported from the package root. */
|
|
17
|
+
export function swapBrowserResolverClientFactoryForTests(factory) {
|
|
18
|
+
const original = createResolverBrowserClient;
|
|
19
|
+
createResolverBrowserClient = factory ?? createBrowserClient;
|
|
20
|
+
let restored = false;
|
|
21
|
+
return () => {
|
|
22
|
+
if (restored)
|
|
23
|
+
return;
|
|
24
|
+
restored = true;
|
|
25
|
+
createResolverBrowserClient = original;
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
class BrowserSolveTimeoutError extends Error {
|
|
29
|
+
constructor(blockedRequests) {
|
|
30
|
+
super(`Browser resolver solve budget elapsed${formatBlockedRequests(blockedRequests)}`);
|
|
31
|
+
this.name = "BrowserSolveTimeoutError";
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
class BrowserNavigationBlockedError extends Error {
|
|
35
|
+
navigationUrl;
|
|
36
|
+
blockedUrls;
|
|
37
|
+
code = "RESOLVER_BROWSER_NAVIGATION_BLOCKED";
|
|
38
|
+
constructor(navigationUrl, blockedUrls, options) {
|
|
39
|
+
super(`Browser resolver navigation was blocked for ${navigationUrl}${formatBlockedRequests(blockedUrls)}`, options);
|
|
40
|
+
this.navigationUrl = navigationUrl;
|
|
41
|
+
this.blockedUrls = blockedUrls;
|
|
42
|
+
this.name = "BrowserNavigationBlockedError";
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function formatBlockedRequests(blockedRequests) {
|
|
46
|
+
if (blockedRequests.length === 0)
|
|
47
|
+
return "";
|
|
48
|
+
const displayed = blockedRequests.slice(0, 5);
|
|
49
|
+
const remainder = blockedRequests.length - displayed.length;
|
|
50
|
+
return `; blocked ${blockedRequests.length} requests: [${displayed.join(", ")}]${remainder > 0 ? ` (+${remainder} more)` : ""}`;
|
|
51
|
+
}
|
|
52
|
+
function isNavigationBlockedError(error) {
|
|
53
|
+
return error instanceof Error && error.message.includes(NAVIGATION_BLOCKED_ERROR_TEXT);
|
|
54
|
+
}
|
|
55
|
+
class BrowserCleanupTimeoutError extends Error {
|
|
56
|
+
constructor(timeoutMs) {
|
|
57
|
+
super(`Browser resolver cleanup exceeded ${timeoutMs}ms`);
|
|
58
|
+
this.name = "BrowserCleanupTimeoutError";
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function abortReason(signal) {
|
|
62
|
+
return signal.reason ?? new DOMException("The operation was aborted", "AbortError");
|
|
63
|
+
}
|
|
64
|
+
function raceWithAbort(operation, signal) {
|
|
65
|
+
if (signal.aborted) {
|
|
66
|
+
return Promise.reject(abortReason(signal));
|
|
67
|
+
}
|
|
68
|
+
return new Promise((resolve, reject) => {
|
|
69
|
+
const cleanup = () => signal.removeEventListener("abort", onAbort);
|
|
70
|
+
const onAbort = () => {
|
|
71
|
+
cleanup();
|
|
72
|
+
reject(abortReason(signal));
|
|
73
|
+
};
|
|
74
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
75
|
+
operation().then((value) => {
|
|
76
|
+
cleanup();
|
|
77
|
+
resolve(value);
|
|
78
|
+
}, (error) => {
|
|
79
|
+
cleanup();
|
|
80
|
+
reject(error);
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
async function runBoundedCleanup(cleanup, timeoutMs, challengeKind, operation, traceRecorder) {
|
|
85
|
+
let timeout;
|
|
86
|
+
const boundedCleanup = async () => {
|
|
87
|
+
try {
|
|
88
|
+
await Promise.race([
|
|
89
|
+
cleanup(),
|
|
90
|
+
new Promise((_resolve, reject) => {
|
|
91
|
+
timeout = setTimeout(() => reject(new BrowserCleanupTimeoutError(timeoutMs)), timeoutMs);
|
|
92
|
+
}),
|
|
93
|
+
]);
|
|
94
|
+
}
|
|
95
|
+
finally {
|
|
96
|
+
if (timeout !== undefined)
|
|
97
|
+
clearTimeout(timeout);
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
if (!traceRecorder) {
|
|
101
|
+
await boundedCleanup().catch(() => undefined);
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
await traceRecorder
|
|
105
|
+
.runSpan("resolver.vendor.cleanup", boundedCleanup, {
|
|
106
|
+
attributes: {
|
|
107
|
+
vendor: BROWSER_VENDOR_ID,
|
|
108
|
+
challenge_kind: challengeKind,
|
|
109
|
+
operation,
|
|
110
|
+
},
|
|
111
|
+
onError(error) {
|
|
112
|
+
return {
|
|
113
|
+
error_message: error instanceof Error ? error.message : String(error),
|
|
114
|
+
...(error instanceof Error && error.stack ? { error_stack: error.stack } : {}),
|
|
115
|
+
};
|
|
116
|
+
},
|
|
117
|
+
})
|
|
118
|
+
.catch(() => undefined);
|
|
119
|
+
}
|
|
120
|
+
async function abortableDelay(ms, signal) {
|
|
121
|
+
let timer;
|
|
122
|
+
try {
|
|
123
|
+
await raceWithAbort(() => new Promise((resolve) => {
|
|
124
|
+
timer = setTimeout(resolve, ms);
|
|
125
|
+
}), signal);
|
|
126
|
+
}
|
|
127
|
+
finally {
|
|
128
|
+
if (timer !== undefined)
|
|
129
|
+
clearTimeout(timer);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
function isSupportedKind(kind) {
|
|
133
|
+
return Object.hasOwn(SUCCESS_COOKIE_NAMES, kind);
|
|
134
|
+
}
|
|
135
|
+
function cookieDomainSpecificity(cookie) {
|
|
136
|
+
return normalizedResolverHostname(cookie.domain.replace(/^\./, "")).length;
|
|
137
|
+
}
|
|
138
|
+
function isHostOnlyCookieFor(cookie, hostname) {
|
|
139
|
+
return (!cookie.domain.startsWith(".") &&
|
|
140
|
+
normalizedResolverHostname(cookie.domain) === normalizedResolverHostname(hostname));
|
|
141
|
+
}
|
|
142
|
+
function cookieAppliesToUrl(cookie, url) {
|
|
143
|
+
const cookieDomain = normalizedResolverHostname(cookie.domain.replace(/^\./, ""));
|
|
144
|
+
const requestHostname = normalizedResolverHostname(url.hostname);
|
|
145
|
+
const domainMatches = cookieDomain.length > 0 &&
|
|
146
|
+
(requestHostname === cookieDomain ||
|
|
147
|
+
(cookie.domain.startsWith(".") && requestHostname.endsWith(`.${cookieDomain}`)));
|
|
148
|
+
if (!domainMatches || (cookie.secure && url.protocol !== "https:"))
|
|
149
|
+
return false;
|
|
150
|
+
const requestPath = url.pathname || "/";
|
|
151
|
+
const cookiePath = cookie.path;
|
|
152
|
+
return (cookiePath.startsWith("/") &&
|
|
153
|
+
(requestPath === cookiePath ||
|
|
154
|
+
(requestPath.startsWith(cookiePath) &&
|
|
155
|
+
(cookiePath.endsWith("/") || requestPath[cookiePath.length] === "/"))));
|
|
156
|
+
}
|
|
157
|
+
function selectSuccessCookie(cookies, successCookieName, pageUrl) {
|
|
158
|
+
const url = new URL(pageUrl);
|
|
159
|
+
return cookies
|
|
160
|
+
.filter((cookie) => cookie.name === successCookieName && cookieAppliesToUrl(cookie, url))
|
|
161
|
+
.sort((left, right) => Number(isHostOnlyCookieFor(right, url.hostname)) -
|
|
162
|
+
Number(isHostOnlyCookieFor(left, url.hostname)) ||
|
|
163
|
+
cookieDomainSpecificity(right) - cookieDomainSpecificity(left) ||
|
|
164
|
+
right.path.length - left.path.length)[0];
|
|
165
|
+
}
|
|
166
|
+
async function solveInPage(page, challengeKind, pageUrl, allowedHosts, successCookieName, pollIntervalMs, gotoTimeoutMs, blockedRequests, signal) {
|
|
167
|
+
return await page.withResourcePolicy({
|
|
168
|
+
allowedMethods: ["GET", "HEAD", "POST"],
|
|
169
|
+
documentContentSecurityPolicy: RESOLVER_DOCUMENT_CONTENT_SECURITY_POLICY,
|
|
170
|
+
routes: [
|
|
171
|
+
{
|
|
172
|
+
match: () => true,
|
|
173
|
+
handle: (request) => {
|
|
174
|
+
if (isResolverBrowserRequestAllowed(request.url, challengeKind, allowedHosts)) {
|
|
175
|
+
return { action: "continue" };
|
|
176
|
+
}
|
|
177
|
+
blockedRequests.add(request.url);
|
|
178
|
+
return { action: "block" };
|
|
179
|
+
},
|
|
180
|
+
},
|
|
181
|
+
],
|
|
182
|
+
}, async () => {
|
|
183
|
+
const userAgent = await raceWithAbort(() => page.userAgent(), signal);
|
|
184
|
+
try {
|
|
185
|
+
await raceWithAbort(() => page.goto(pageUrl, {
|
|
186
|
+
timeout: gotoTimeoutMs,
|
|
187
|
+
waitUntil: "domcontentloaded",
|
|
188
|
+
}), signal);
|
|
189
|
+
}
|
|
190
|
+
catch (error) {
|
|
191
|
+
if (!isNavigationBlockedError(error))
|
|
192
|
+
throw error;
|
|
193
|
+
throw new BrowserNavigationBlockedError(pageUrl, [...blockedRequests], { cause: error });
|
|
194
|
+
}
|
|
195
|
+
while (true) {
|
|
196
|
+
const cookies = await raceWithAbort(() => page.cookies(), signal);
|
|
197
|
+
const successCookie = selectSuccessCookie(cookies, successCookieName, pageUrl);
|
|
198
|
+
if (successCookie) {
|
|
199
|
+
return {
|
|
200
|
+
form: "cookies",
|
|
201
|
+
cookies: { [successCookieName]: successCookie.value },
|
|
202
|
+
userAgent,
|
|
203
|
+
...(successCookie.expires === undefined ? {} : { expires: successCookie.expires }),
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
await abortableDelay(pollIntervalMs, signal);
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
function isResolverBrowserRequestAllowed(targetUrl, challengeKind, allowedHosts) {
|
|
211
|
+
try {
|
|
212
|
+
assertResolverHostAllowed(targetUrl, allowedHosts);
|
|
213
|
+
return true;
|
|
214
|
+
}
|
|
215
|
+
catch {
|
|
216
|
+
if (challengeKind !== "aws_waf")
|
|
217
|
+
return false;
|
|
218
|
+
}
|
|
219
|
+
let target;
|
|
220
|
+
try {
|
|
221
|
+
target = new URL(targetUrl);
|
|
222
|
+
}
|
|
223
|
+
catch {
|
|
224
|
+
return false;
|
|
225
|
+
}
|
|
226
|
+
const hostname = normalizedResolverHostname(target.hostname);
|
|
227
|
+
return (target.protocol === "https:" &&
|
|
228
|
+
hostname.length > AWS_WAF_CHALLENGE_INFRASTRUCTURE_HOST_SUFFIX.length &&
|
|
229
|
+
hostname.endsWith(AWS_WAF_CHALLENGE_INFRASTRUCTURE_HOST_SUFFIX));
|
|
230
|
+
}
|
|
231
|
+
const POOL_ALLOCATION_EXHAUSTED_CODES = new Set([
|
|
232
|
+
-32_001, // queue full
|
|
233
|
+
-32_002, // acquire timed out
|
|
234
|
+
-32_003, // shutting down
|
|
235
|
+
]);
|
|
236
|
+
function poolErrorCode(error) {
|
|
237
|
+
const code = error.code;
|
|
238
|
+
return typeof code === "number" ? code : undefined;
|
|
239
|
+
}
|
|
240
|
+
function knownUnavailableReason(error, beforePageSolve = false) {
|
|
241
|
+
// Source-grounded mappings:
|
|
242
|
+
// - apps/cdp-pool/src/index.ts: the JSON-RPC codes and messages below.
|
|
243
|
+
// - src/runtime/browser.ts: BROWSER_CDP_POOL_REQUIRED and the two WebSocket messages.
|
|
244
|
+
// The pool's numeric JSON-RPC code is authoritative and is preferred whenever present.
|
|
245
|
+
// The message substrings remain as a fallback for pool builds predating code
|
|
246
|
+
// propagation; they are exact strings verified against the pool source. -32004 (unknown
|
|
247
|
+
// lease) and -32006 (missing allowedHosts) are deliberately unmapped: both are caller
|
|
248
|
+
// bugs, and the next vendor would fail identically, so they propagate unchanged.
|
|
249
|
+
if (isProviderError(error)) {
|
|
250
|
+
return error.code === "BROWSER_CDP_POOL_REQUIRED" ? "missing_credentials" : undefined;
|
|
251
|
+
}
|
|
252
|
+
if (!(error instanceof Error))
|
|
253
|
+
return undefined;
|
|
254
|
+
const code = poolErrorCode(error);
|
|
255
|
+
if (code !== undefined) {
|
|
256
|
+
return POOL_ALLOCATION_EXHAUSTED_CODES.has(code) ? "allocation_exhausted" : undefined;
|
|
257
|
+
}
|
|
258
|
+
if (error.message.includes("CDP pool acquire queue is full") ||
|
|
259
|
+
error.message.includes("CDP pool acquire timed out") ||
|
|
260
|
+
error.message.includes("CDP pool is shutting down")) {
|
|
261
|
+
return "allocation_exhausted";
|
|
262
|
+
}
|
|
263
|
+
if (error.message.includes("Unable to connect to WebSocket endpoint") ||
|
|
264
|
+
error.message.includes("WebSocket closed")) {
|
|
265
|
+
return "transport_failure";
|
|
266
|
+
}
|
|
267
|
+
// Browser creation, Playwright launch, and CDP connection all happen before the
|
|
268
|
+
// isolated-page handler is entered. Errors after that boundary belong to the
|
|
269
|
+
// challenge solve and must retain their existing classification.
|
|
270
|
+
return beforePageSolve ? "transport_failure" : undefined;
|
|
271
|
+
}
|
|
272
|
+
async function closeBrowserClient(client, timeoutMs, challengeKind, traceRecorder) {
|
|
273
|
+
const close = client?.close;
|
|
274
|
+
if (!close)
|
|
275
|
+
return;
|
|
276
|
+
await runBoundedCleanup(() => close.call(client), timeoutMs, challengeKind, "client.close", traceRecorder);
|
|
277
|
+
}
|
|
278
|
+
export function createBrowserResolverVendorAdapter(options) {
|
|
279
|
+
const createClient = options.createClient ?? createResolverBrowserClient;
|
|
280
|
+
const pollIntervalMs = options.pollIntervalMs ?? DEFAULT_COOKIE_POLL_INTERVAL_MS;
|
|
281
|
+
return {
|
|
282
|
+
id: BROWSER_VENDOR_ID,
|
|
283
|
+
supports(kind) {
|
|
284
|
+
return isSupportedKind(kind);
|
|
285
|
+
},
|
|
286
|
+
getIssuingIdentity(solution, requestedIdentity, challenge) {
|
|
287
|
+
if (solution.form !== "cookies" || !isSupportedKind(challenge.kind))
|
|
288
|
+
return undefined;
|
|
289
|
+
return resolverChallengeIssuingIdentity(challenge, {
|
|
290
|
+
...(requestedIdentity ? { proxyUrl: requestedIdentity.proxyUrl } : {}),
|
|
291
|
+
userAgent: solution.userAgent,
|
|
292
|
+
});
|
|
293
|
+
},
|
|
294
|
+
async solve(challenge, identity, callerSignal, traceRecorder) {
|
|
295
|
+
const cdpUrl = options.cdpUrl?.trim();
|
|
296
|
+
const proxyUrl = identity?.proxyUrl;
|
|
297
|
+
if (!cdpUrl && proxyUrl === undefined) {
|
|
298
|
+
throw new ResolverVendorUnavailableError(BROWSER_VENDOR_ID, "missing_credentials");
|
|
299
|
+
}
|
|
300
|
+
if (!isSupportedKind(challenge.kind)) {
|
|
301
|
+
throw new TypeError(`Browser resolver does not support ${challenge.kind}`);
|
|
302
|
+
}
|
|
303
|
+
assertResolverHostAllowed(challenge.pageUrl, options.allowedHosts);
|
|
304
|
+
const challengeKind = challenge.kind;
|
|
305
|
+
callerSignal.throwIfAborted();
|
|
306
|
+
if (proxyUrl !== undefined && proxyUrl.length === 0) {
|
|
307
|
+
throw new ResolverVendorUnavailableError(BROWSER_VENDOR_ID, "missing_proxy_identity");
|
|
308
|
+
}
|
|
309
|
+
if (cdpUrl && proxyUrl !== undefined) {
|
|
310
|
+
// The current pool acquire protocol cannot bind a proxy to its browser context.
|
|
311
|
+
// Refuse so the chain can choose a vendor that can honor the resolved identity.
|
|
312
|
+
throw new ResolverVendorUnavailableError(BROWSER_VENDOR_ID, "not_implemented");
|
|
313
|
+
}
|
|
314
|
+
const blockedRequests = new Set();
|
|
315
|
+
const solveController = new AbortController();
|
|
316
|
+
const onCallerAbort = () => solveController.abort(abortReason(callerSignal));
|
|
317
|
+
callerSignal.addEventListener("abort", onCallerAbort, { once: true });
|
|
318
|
+
const timeout = setTimeout(() => solveController.abort(new BrowserSolveTimeoutError([...blockedRequests])), options.timeoutMs);
|
|
319
|
+
let client;
|
|
320
|
+
let handlerEntered = false;
|
|
321
|
+
try {
|
|
322
|
+
client = createClient({
|
|
323
|
+
allowedHosts: [...options.allowedHosts],
|
|
324
|
+
cdpUrl: cdpUrl ?? "",
|
|
325
|
+
...(proxyUrl === undefined ? {} : { proxy: proxyUrl }),
|
|
326
|
+
requireCdpPool: cdpUrl !== undefined,
|
|
327
|
+
serviceWorkers: "block",
|
|
328
|
+
});
|
|
329
|
+
const contextOperation = client.withIsolatedContext(async (page) => {
|
|
330
|
+
handlerEntered = true;
|
|
331
|
+
return await solveInPage(page, challengeKind, challenge.pageUrl, options.allowedHosts, SUCCESS_COOKIE_NAMES[challengeKind], pollIntervalMs, options.timeoutMs, blockedRequests, solveController.signal);
|
|
332
|
+
});
|
|
333
|
+
try {
|
|
334
|
+
return await raceWithAbort(() => contextOperation, solveController.signal);
|
|
335
|
+
}
|
|
336
|
+
catch (error) {
|
|
337
|
+
if (solveController.signal.aborted) {
|
|
338
|
+
if (handlerEntered) {
|
|
339
|
+
await runBoundedCleanup(() => contextOperation, options.timeoutMs, challengeKind, "context.close", traceRecorder);
|
|
340
|
+
throw error;
|
|
341
|
+
}
|
|
342
|
+
await closeBrowserClient(client, options.timeoutMs, challengeKind, traceRecorder);
|
|
343
|
+
void contextOperation.catch(() => undefined);
|
|
344
|
+
}
|
|
345
|
+
throw error;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
catch (error) {
|
|
349
|
+
if (callerSignal.aborted)
|
|
350
|
+
throw abortReason(callerSignal);
|
|
351
|
+
if (error instanceof BrowserSolveTimeoutError) {
|
|
352
|
+
throw new ResolverVendorUnavailableError(BROWSER_VENDOR_ID, "timeout", {
|
|
353
|
+
cause: error,
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
if (error instanceof BrowserNavigationBlockedError) {
|
|
357
|
+
throw new ResolverVendorUnavailableError(BROWSER_VENDOR_ID, "transport_failure", {
|
|
358
|
+
cause: error,
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
if (error instanceof ResolverVendorUnavailableError) {
|
|
362
|
+
throw error;
|
|
363
|
+
}
|
|
364
|
+
const reason = knownUnavailableReason(error, !handlerEntered);
|
|
365
|
+
if (reason) {
|
|
366
|
+
throw new ResolverVendorUnavailableError(BROWSER_VENDOR_ID, reason, { cause: error });
|
|
367
|
+
}
|
|
368
|
+
throw error;
|
|
369
|
+
}
|
|
370
|
+
finally {
|
|
371
|
+
clearTimeout(timeout);
|
|
372
|
+
callerSignal.removeEventListener("abort", onCallerAbort);
|
|
373
|
+
await closeBrowserClient(client, options.timeoutMs, challengeKind, traceRecorder);
|
|
374
|
+
}
|
|
375
|
+
},
|
|
376
|
+
};
|
|
377
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { ChallengeSolution, ProviderChallenge } from "../../types.js";
|
|
2
|
+
import type { TraceRecorder } from "../trace.js";
|
|
3
|
+
import { type ResolverIdentity, type ResolverVendorAdapter } from "./types.js";
|
|
4
|
+
type Delay = (ms: number, signal: AbortSignal) => Promise<void>;
|
|
5
|
+
export interface CapsolverResolverVendorOptions {
|
|
6
|
+
readonly apiKey?: string;
|
|
7
|
+
readonly timeoutMs?: number;
|
|
8
|
+
readonly pollIntervalMs?: number;
|
|
9
|
+
readonly allowedHosts: readonly string[];
|
|
10
|
+
readonly fetchImpl?: typeof fetch;
|
|
11
|
+
readonly baseUrl?: string;
|
|
12
|
+
/** Test-only clock override; supplying it disables the real-time deadline timer. */
|
|
13
|
+
readonly now?: () => number;
|
|
14
|
+
/** Test-only delay override used with `now` to exercise polling without sleeping. */
|
|
15
|
+
readonly delay?: Delay;
|
|
16
|
+
}
|
|
17
|
+
export interface CapsolverResolverVendorAdapter extends ResolverVendorAdapter {
|
|
18
|
+
readonly id: "capsolver";
|
|
19
|
+
solve(challenge: ProviderChallenge, identity: ResolverIdentity | undefined, signal: AbortSignal, traceRecorder?: TraceRecorder): Promise<ChallengeSolution>;
|
|
20
|
+
}
|
|
21
|
+
export declare function createCapsolverResolverVendorAdapter(options: CapsolverResolverVendorOptions): CapsolverResolverVendorAdapter;
|
|
22
|
+
export {};
|