@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,700 @@
|
|
|
1
|
+
import { getStealthProfile } from "../../stealth/profiles.js";
|
|
2
|
+
import type { ChallengeSolution, ProviderChallenge } from "../../types.js";
|
|
3
|
+
import { redactSensitiveText } from "../request-options.js";
|
|
4
|
+
import { DEFAULT_PROFILE } from "../stealth.js";
|
|
5
|
+
import type { TraceRecorder } from "../trace.js";
|
|
6
|
+
import { assertResolverHostAllowed } from "./hosts.js";
|
|
7
|
+
import {
|
|
8
|
+
ResolverChallengeVerdictError,
|
|
9
|
+
type ResolverIdentity,
|
|
10
|
+
type ResolverVendorAdapter,
|
|
11
|
+
ResolverVendorUnavailableError,
|
|
12
|
+
type ResolverVendorUnavailableReason,
|
|
13
|
+
resolverVendorSupports,
|
|
14
|
+
} from "./types.js";
|
|
15
|
+
|
|
16
|
+
const CAPSOLVER_VENDOR_ID = "capsolver" as const;
|
|
17
|
+
const DEFAULT_CAPSOLVER_BASE_URL = "https://api.capsolver.com";
|
|
18
|
+
const DEFAULT_POLL_INTERVAL_MS = 2_000;
|
|
19
|
+
const DEFAULT_TIMEOUT_MS = 120_000;
|
|
20
|
+
|
|
21
|
+
type Delay = (ms: number, signal: AbortSignal) => Promise<void>;
|
|
22
|
+
type CapsolverOperationPhase = "create_task" | "poll_result";
|
|
23
|
+
|
|
24
|
+
export interface CapsolverResolverVendorOptions {
|
|
25
|
+
readonly apiKey?: string;
|
|
26
|
+
readonly timeoutMs?: number;
|
|
27
|
+
readonly pollIntervalMs?: number;
|
|
28
|
+
readonly allowedHosts: readonly string[];
|
|
29
|
+
readonly fetchImpl?: typeof fetch;
|
|
30
|
+
readonly baseUrl?: string;
|
|
31
|
+
/** Test-only clock override; supplying it disables the real-time deadline timer. */
|
|
32
|
+
readonly now?: () => number;
|
|
33
|
+
/** Test-only delay override used with `now` to exercise polling without sleeping. */
|
|
34
|
+
readonly delay?: Delay;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface CapsolverResolverVendorAdapter extends ResolverVendorAdapter {
|
|
38
|
+
readonly id: "capsolver";
|
|
39
|
+
solve(
|
|
40
|
+
challenge: ProviderChallenge,
|
|
41
|
+
identity: ResolverIdentity | undefined,
|
|
42
|
+
signal: AbortSignal,
|
|
43
|
+
traceRecorder?: TraceRecorder,
|
|
44
|
+
): Promise<ChallengeSolution>;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
class CapsolverSolveTimeoutError extends Error {
|
|
48
|
+
constructor() {
|
|
49
|
+
super("Capsolver resolver solve budget elapsed");
|
|
50
|
+
this.name = "CapsolverSolveTimeoutError";
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
class CapsolverUnavailableError extends ResolverVendorUnavailableError {
|
|
55
|
+
readonly errorCode: string | undefined;
|
|
56
|
+
readonly errorDescription: string | undefined;
|
|
57
|
+
readonly responseStatus: number | undefined;
|
|
58
|
+
readonly responseContentType: string | undefined;
|
|
59
|
+
readonly responseLength: number | undefined;
|
|
60
|
+
|
|
61
|
+
constructor(
|
|
62
|
+
reason: ResolverVendorUnavailableReason,
|
|
63
|
+
options: {
|
|
64
|
+
readonly phase: CapsolverOperationPhase;
|
|
65
|
+
readonly cause?: unknown;
|
|
66
|
+
readonly errorCode?: string;
|
|
67
|
+
readonly errorDescription?: string;
|
|
68
|
+
readonly responseStatus?: number;
|
|
69
|
+
readonly responseContentType?: string;
|
|
70
|
+
readonly responseLength?: number;
|
|
71
|
+
},
|
|
72
|
+
) {
|
|
73
|
+
super(CAPSOLVER_VENDOR_ID, reason, options);
|
|
74
|
+
this.errorCode = options.errorCode;
|
|
75
|
+
this.errorDescription = options.errorDescription;
|
|
76
|
+
this.responseStatus = options.responseStatus;
|
|
77
|
+
this.responseContentType = options.responseContentType;
|
|
78
|
+
this.responseLength = options.responseLength;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
type JsonRecord = Record<string, unknown>;
|
|
83
|
+
|
|
84
|
+
interface CapsolverResponseErrorFields {
|
|
85
|
+
readonly errorId: number | undefined;
|
|
86
|
+
readonly errorCode: string | undefined;
|
|
87
|
+
readonly errorDescription: string | undefined;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
interface CapsolverCreateTaskResponse extends CapsolverResponseErrorFields {
|
|
91
|
+
readonly taskId: string | number | undefined;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
interface CapsolverPollResultResponse extends CapsolverResponseErrorFields {
|
|
95
|
+
readonly status: string | undefined;
|
|
96
|
+
readonly solution:
|
|
97
|
+
| {
|
|
98
|
+
readonly token: string | undefined;
|
|
99
|
+
readonly cookie: string | undefined;
|
|
100
|
+
readonly gRecaptchaResponse: string | undefined;
|
|
101
|
+
readonly cookies: Readonly<Record<string, string>> | undefined;
|
|
102
|
+
readonly userAgent: string | undefined;
|
|
103
|
+
}
|
|
104
|
+
| undefined;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
class CapsolverVerdictError extends ResolverChallengeVerdictError {
|
|
108
|
+
readonly phase: CapsolverOperationPhase;
|
|
109
|
+
readonly errorCode: string | undefined;
|
|
110
|
+
readonly errorDescription: string | undefined;
|
|
111
|
+
|
|
112
|
+
constructor(options: {
|
|
113
|
+
readonly phase: CapsolverOperationPhase;
|
|
114
|
+
readonly errorCode?: string;
|
|
115
|
+
readonly errorDescription?: string;
|
|
116
|
+
}) {
|
|
117
|
+
super(CAPSOLVER_VENDOR_ID, "solve_failed", options);
|
|
118
|
+
this.phase = options.phase;
|
|
119
|
+
this.errorCode = options.errorCode;
|
|
120
|
+
this.errorDescription = options.errorDescription;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function isJsonRecord(value: unknown): value is JsonRecord {
|
|
125
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function abortReason(signal: AbortSignal): unknown {
|
|
129
|
+
return signal.reason ?? new DOMException("The operation was aborted", "AbortError");
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function containsSensitiveValue(value: unknown, sensitiveValues: readonly string[]): boolean {
|
|
133
|
+
const secrets = sensitiveValues.filter((secret) => secret.length > 0);
|
|
134
|
+
if (secrets.length === 0) return false;
|
|
135
|
+
const seen = new Set<object>();
|
|
136
|
+
|
|
137
|
+
const inspect = (candidate: unknown): boolean => {
|
|
138
|
+
if (typeof candidate === "string") {
|
|
139
|
+
return secrets.some((secret) => candidate.includes(secret));
|
|
140
|
+
}
|
|
141
|
+
if (candidate === null || (typeof candidate !== "object" && typeof candidate !== "function")) {
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
if (seen.has(candidate)) return false;
|
|
145
|
+
seen.add(candidate);
|
|
146
|
+
|
|
147
|
+
try {
|
|
148
|
+
for (const property of Reflect.ownKeys(candidate)) {
|
|
149
|
+
if (typeof property === "string" && inspect(property)) return true;
|
|
150
|
+
const descriptor = Object.getOwnPropertyDescriptor(candidate, property);
|
|
151
|
+
if (!descriptor) return true;
|
|
152
|
+
if ("value" in descriptor && inspect(descriptor.value)) return true;
|
|
153
|
+
if (descriptor.get !== undefined || descriptor.set !== undefined) return true;
|
|
154
|
+
}
|
|
155
|
+
} catch {
|
|
156
|
+
return true;
|
|
157
|
+
}
|
|
158
|
+
return false;
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
return inspect(value);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function safeCauseOptions(
|
|
165
|
+
error: unknown,
|
|
166
|
+
sensitiveValues: readonly string[],
|
|
167
|
+
): { readonly cause?: unknown } {
|
|
168
|
+
return containsSensitiveValue(error, sensitiveValues) ? {} : { cause: error };
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function sanitizedJsonParseCause(): SyntaxError {
|
|
172
|
+
return new SyntaxError("Upstream response failed");
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function raceWithAbort<T>(
|
|
176
|
+
operation: () => Promise<T>,
|
|
177
|
+
signal: AbortSignal,
|
|
178
|
+
phase?: CapsolverOperationPhase,
|
|
179
|
+
sensitiveValues: readonly string[] = [],
|
|
180
|
+
): Promise<T> {
|
|
181
|
+
if (signal.aborted) return Promise.reject(abortReason(signal));
|
|
182
|
+
|
|
183
|
+
return new Promise<T>((resolve, reject) => {
|
|
184
|
+
const cleanup = () => signal.removeEventListener("abort", onAbort);
|
|
185
|
+
const onAbort = () => {
|
|
186
|
+
cleanup();
|
|
187
|
+
reject(abortReason(signal));
|
|
188
|
+
};
|
|
189
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
190
|
+
operation().then(
|
|
191
|
+
(value) => {
|
|
192
|
+
cleanup();
|
|
193
|
+
resolve(value);
|
|
194
|
+
},
|
|
195
|
+
(error: unknown) => {
|
|
196
|
+
cleanup();
|
|
197
|
+
if (phase === undefined) {
|
|
198
|
+
reject(error);
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
reject(
|
|
202
|
+
new ResolverVendorUnavailableError(CAPSOLVER_VENDOR_ID, "transport_failure", {
|
|
203
|
+
...safeCauseOptions(error, sensitiveValues),
|
|
204
|
+
phase,
|
|
205
|
+
}),
|
|
206
|
+
);
|
|
207
|
+
},
|
|
208
|
+
);
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
async function abortableDelay(ms: number, signal: AbortSignal): Promise<void> {
|
|
213
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
214
|
+
try {
|
|
215
|
+
await raceWithAbort(
|
|
216
|
+
() =>
|
|
217
|
+
new Promise<void>((resolve) => {
|
|
218
|
+
timer = setTimeout(resolve, ms);
|
|
219
|
+
}),
|
|
220
|
+
signal,
|
|
221
|
+
);
|
|
222
|
+
} finally {
|
|
223
|
+
if (timer !== undefined) clearTimeout(timer);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function responseErrorFields(payload: JsonRecord): CapsolverResponseErrorFields {
|
|
228
|
+
return {
|
|
229
|
+
errorId: typeof payload.errorId === "number" ? payload.errorId : undefined,
|
|
230
|
+
errorCode: typeof payload.errorCode === "string" ? payload.errorCode : undefined,
|
|
231
|
+
errorDescription:
|
|
232
|
+
typeof payload.errorDescription === "string" ? payload.errorDescription : undefined,
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function parseCreateTaskResponse(payload: JsonRecord): CapsolverCreateTaskResponse {
|
|
237
|
+
const taskId = payload.taskId;
|
|
238
|
+
return {
|
|
239
|
+
...responseErrorFields(payload),
|
|
240
|
+
taskId: typeof taskId === "string" || typeof taskId === "number" ? taskId : undefined,
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function parsePollResultResponse(payload: JsonRecord): CapsolverPollResultResponse {
|
|
245
|
+
const solution = isJsonRecord(payload.solution) ? payload.solution : undefined;
|
|
246
|
+
const cookies = solution && isJsonRecord(solution.cookies)
|
|
247
|
+
? Object.fromEntries(
|
|
248
|
+
Object.entries(solution.cookies).filter(
|
|
249
|
+
(entry): entry is [string, string] => typeof entry[1] === "string",
|
|
250
|
+
),
|
|
251
|
+
)
|
|
252
|
+
: undefined;
|
|
253
|
+
return {
|
|
254
|
+
...responseErrorFields(payload),
|
|
255
|
+
status: typeof payload.status === "string" ? payload.status : undefined,
|
|
256
|
+
solution: solution
|
|
257
|
+
? {
|
|
258
|
+
token: typeof solution.token === "string" ? solution.token : undefined,
|
|
259
|
+
cookie: typeof solution.cookie === "string" ? solution.cookie : undefined,
|
|
260
|
+
gRecaptchaResponse:
|
|
261
|
+
typeof solution.gRecaptchaResponse === "string"
|
|
262
|
+
? solution.gRecaptchaResponse
|
|
263
|
+
: undefined,
|
|
264
|
+
cookies,
|
|
265
|
+
userAgent: typeof solution.userAgent === "string" ? solution.userAgent : undefined,
|
|
266
|
+
}
|
|
267
|
+
: undefined,
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function proxyForCapsolver(
|
|
272
|
+
proxyUrl: string,
|
|
273
|
+
): { readonly value: string; readonly sensitive: readonly string[] } | undefined {
|
|
274
|
+
try {
|
|
275
|
+
const url = new URL(proxyUrl);
|
|
276
|
+
const protocol = url.protocol.slice(0, -1).toLowerCase();
|
|
277
|
+
const scheme = protocol === "http" || protocol === "socks5" ? protocol : undefined;
|
|
278
|
+
const port = Number(url.port || (scheme === "socks5" ? 1080 : 80));
|
|
279
|
+
if (!scheme || !url.hostname || !Number.isInteger(port) || port <= 0 || port > 65_535) {
|
|
280
|
+
return undefined;
|
|
281
|
+
}
|
|
282
|
+
const username = url.username ? decodeURIComponent(url.username) : "";
|
|
283
|
+
const password = url.password ? decodeURIComponent(url.password) : "";
|
|
284
|
+
if (username.includes(":") || password.includes(":")) return undefined;
|
|
285
|
+
const value = `${scheme}:${url.hostname}:${port}${username || password ? `:${username}:${password}` : ""}`;
|
|
286
|
+
return { value, sensitive: [proxyUrl, value, username, password].filter(Boolean) };
|
|
287
|
+
} catch {
|
|
288
|
+
return undefined;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function isAllocationExhausted(payload: CapsolverResponseErrorFields): boolean {
|
|
293
|
+
const code = payload.errorCode?.toLowerCase() ?? "";
|
|
294
|
+
const description = payload.errorDescription?.toLowerCase() ?? "";
|
|
295
|
+
return (
|
|
296
|
+
code === "error_zero_balance" ||
|
|
297
|
+
/(?:insufficient|zero|no|not enough)\s+(?:balance|funds|credit)/u.test(`${code} ${description}`)
|
|
298
|
+
);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
function isNegativeVerdict(payload: CapsolverResponseErrorFields): boolean {
|
|
302
|
+
return payload.errorCode?.toLowerCase() === "error_captcha_unsolvable";
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function safeVendorDetail(value: string, sensitiveValues: readonly string[]): string {
|
|
306
|
+
const redacted = redactSensitiveText(value, sensitiveValues);
|
|
307
|
+
return containsSensitiveValue(redacted, sensitiveValues) ? "[REDACTED]" : redacted;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
function vendorErrorDetails(
|
|
311
|
+
payload: CapsolverResponseErrorFields,
|
|
312
|
+
sensitiveValues: readonly string[],
|
|
313
|
+
): { readonly errorCode?: string; readonly errorDescription?: string } {
|
|
314
|
+
const errorCode = payload.errorCode?.trim();
|
|
315
|
+
const errorDescription = payload.errorDescription?.trim();
|
|
316
|
+
return {
|
|
317
|
+
...(errorCode ? { errorCode: safeVendorDetail(errorCode, sensitiveValues) } : {}),
|
|
318
|
+
...(errorDescription
|
|
319
|
+
? { errorDescription: safeVendorDetail(payload.errorDescription ?? "", sensitiveValues) }
|
|
320
|
+
: {}),
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
function unavailableForPayload(
|
|
325
|
+
payload: CapsolverResponseErrorFields,
|
|
326
|
+
phase: CapsolverOperationPhase,
|
|
327
|
+
sensitiveValues: readonly string[],
|
|
328
|
+
): ResolverVendorUnavailableError | ResolverChallengeVerdictError {
|
|
329
|
+
const details = vendorErrorDetails(payload, sensitiveValues);
|
|
330
|
+
if (isNegativeVerdict(payload)) {
|
|
331
|
+
return new CapsolverVerdictError({ phase, ...details });
|
|
332
|
+
}
|
|
333
|
+
return new CapsolverUnavailableError(
|
|
334
|
+
isAllocationExhausted(payload) ? "allocation_exhausted" : "transport_failure",
|
|
335
|
+
{ phase, ...details },
|
|
336
|
+
);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
async function postJson<TResponse>(
|
|
340
|
+
fetchImpl: typeof fetch,
|
|
341
|
+
url: string,
|
|
342
|
+
body: JsonRecord,
|
|
343
|
+
signal: AbortSignal,
|
|
344
|
+
phase: CapsolverOperationPhase,
|
|
345
|
+
sensitiveValues: readonly string[],
|
|
346
|
+
parseResponse: (payload: JsonRecord) => TResponse,
|
|
347
|
+
): Promise<{ readonly ok: boolean; readonly payload: TResponse }> {
|
|
348
|
+
const response = await raceWithAbort(
|
|
349
|
+
() =>
|
|
350
|
+
fetchImpl(url, {
|
|
351
|
+
method: "POST",
|
|
352
|
+
headers: { "content-type": "application/json" },
|
|
353
|
+
body: JSON.stringify(body),
|
|
354
|
+
signal,
|
|
355
|
+
redirect: "error",
|
|
356
|
+
}),
|
|
357
|
+
signal,
|
|
358
|
+
phase,
|
|
359
|
+
sensitiveValues,
|
|
360
|
+
);
|
|
361
|
+
|
|
362
|
+
let responseText: string;
|
|
363
|
+
try {
|
|
364
|
+
responseText = await raceWithAbort(() => response.text(), signal, phase, sensitiveValues);
|
|
365
|
+
} catch (error) {
|
|
366
|
+
if (error instanceof ResolverVendorUnavailableError) throw error;
|
|
367
|
+
throw new ResolverVendorUnavailableError(CAPSOLVER_VENDOR_ID, "transport_failure", {
|
|
368
|
+
cause: error,
|
|
369
|
+
phase,
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
let payload: unknown;
|
|
374
|
+
try {
|
|
375
|
+
payload = JSON.parse(responseText);
|
|
376
|
+
} catch {
|
|
377
|
+
const contentType = response.headers.get("content-type");
|
|
378
|
+
throw new CapsolverUnavailableError("transport_failure", {
|
|
379
|
+
cause: sanitizedJsonParseCause(),
|
|
380
|
+
phase,
|
|
381
|
+
responseStatus: response.status,
|
|
382
|
+
responseContentType: contentType
|
|
383
|
+
? safeVendorDetail(contentType.slice(0, 128), sensitiveValues)
|
|
384
|
+
: undefined,
|
|
385
|
+
responseLength: responseText.length,
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
if (!isJsonRecord(payload)) {
|
|
389
|
+
throw new ResolverVendorUnavailableError(CAPSOLVER_VENDOR_ID, "transport_failure", {
|
|
390
|
+
phase,
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
return { ok: response.ok, payload: parseResponse(payload) };
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
function endpoint(baseUrl: string, path: string): string {
|
|
397
|
+
return `${baseUrl.replace(/\/+$/u, "")}/${path}`;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
function spanErrorAttributes(
|
|
401
|
+
error: unknown,
|
|
402
|
+
phase: CapsolverOperationPhase,
|
|
403
|
+
): Record<string, unknown> | undefined {
|
|
404
|
+
if (error instanceof ResolverVendorUnavailableError) {
|
|
405
|
+
return {
|
|
406
|
+
unavailability_reason: error.reason,
|
|
407
|
+
transport_phase: error.phase,
|
|
408
|
+
...(error instanceof CapsolverUnavailableError
|
|
409
|
+
? {
|
|
410
|
+
vendor_error_code: error.errorCode,
|
|
411
|
+
vendor_error_description: error.errorDescription,
|
|
412
|
+
response_status: error.responseStatus,
|
|
413
|
+
response_content_type: error.responseContentType,
|
|
414
|
+
response_length: error.responseLength,
|
|
415
|
+
}
|
|
416
|
+
: {}),
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
if (error instanceof CapsolverVerdictError) {
|
|
420
|
+
return {
|
|
421
|
+
verdict_reason: error.reason,
|
|
422
|
+
transport_phase: error.phase,
|
|
423
|
+
vendor_error_code: error.errorCode,
|
|
424
|
+
vendor_error_description: error.errorDescription,
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
if (error instanceof CapsolverSolveTimeoutError) {
|
|
428
|
+
return {
|
|
429
|
+
unavailability_reason: "timeout",
|
|
430
|
+
transport_phase: phase,
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
return undefined;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
export function createCapsolverResolverVendorAdapter(
|
|
437
|
+
options: CapsolverResolverVendorOptions,
|
|
438
|
+
): CapsolverResolverVendorAdapter {
|
|
439
|
+
const fetchImpl = options.fetchImpl ?? globalThis.fetch;
|
|
440
|
+
const baseUrl = options.baseUrl ?? DEFAULT_CAPSOLVER_BASE_URL;
|
|
441
|
+
const pollIntervalMs = options.pollIntervalMs ?? DEFAULT_POLL_INTERVAL_MS;
|
|
442
|
+
const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
443
|
+
const now = options.now ?? Date.now;
|
|
444
|
+
const delay = options.delay ?? abortableDelay;
|
|
445
|
+
|
|
446
|
+
return {
|
|
447
|
+
id: CAPSOLVER_VENDOR_ID,
|
|
448
|
+
|
|
449
|
+
supports(kind) {
|
|
450
|
+
return resolverVendorSupports(CAPSOLVER_VENDOR_ID, kind);
|
|
451
|
+
},
|
|
452
|
+
|
|
453
|
+
async solve(challenge, identity, callerSignal, traceRecorder) {
|
|
454
|
+
const apiKey = options.apiKey?.trim();
|
|
455
|
+
if (!apiKey) {
|
|
456
|
+
throw new ResolverVendorUnavailableError(CAPSOLVER_VENDOR_ID, "missing_credentials", {
|
|
457
|
+
phase: "create_task",
|
|
458
|
+
});
|
|
459
|
+
}
|
|
460
|
+
if (!resolverVendorSupports(CAPSOLVER_VENDOR_ID, challenge.kind)) {
|
|
461
|
+
throw new TypeError(`Capsolver resolver does not support ${challenge.kind}`);
|
|
462
|
+
}
|
|
463
|
+
const proxy = identity ? proxyForCapsolver(identity.proxyUrl) : undefined;
|
|
464
|
+
if (challenge.kind === "aws_waf" && identity && !proxy) {
|
|
465
|
+
throw new ResolverVendorUnavailableError(CAPSOLVER_VENDOR_ID, "transport_failure", {
|
|
466
|
+
phase: "create_task",
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
if (challenge.kind === "cloudflare_interstitial" && !identity) {
|
|
470
|
+
throw new ResolverVendorUnavailableError(CAPSOLVER_VENDOR_ID, "missing_proxy_identity", {
|
|
471
|
+
phase: "create_task",
|
|
472
|
+
});
|
|
473
|
+
}
|
|
474
|
+
if (identity && !proxy) {
|
|
475
|
+
throw new ResolverVendorUnavailableError(CAPSOLVER_VENDOR_ID, "transport_failure", {
|
|
476
|
+
phase: "create_task",
|
|
477
|
+
});
|
|
478
|
+
}
|
|
479
|
+
const challengeFields = challenge as unknown as Record<string, unknown>;
|
|
480
|
+
const sensitiveValues = [
|
|
481
|
+
apiKey,
|
|
482
|
+
challenge.pageUrl,
|
|
483
|
+
...(typeof challengeFields.siteKey === "string" ? [challengeFields.siteKey] : []),
|
|
484
|
+
...(typeof challengeFields.action === "string" ? [challengeFields.action] : []),
|
|
485
|
+
...(typeof challengeFields.cdata === "string" ? [challengeFields.cdata] : []),
|
|
486
|
+
...(typeof challengeFields.blockedHtml === "string" ? [challengeFields.blockedHtml] : []),
|
|
487
|
+
...(typeof challengeFields.captchaScript === "string" ? [challengeFields.captchaScript] : []),
|
|
488
|
+
...(typeof challengeFields.context === "string" ? [challengeFields.context] : []),
|
|
489
|
+
...(typeof challengeFields.iv === "string" ? [challengeFields.iv] : []),
|
|
490
|
+
...(proxy?.sensitive ?? []),
|
|
491
|
+
];
|
|
492
|
+
|
|
493
|
+
assertResolverHostAllowed(challenge.pageUrl, options.allowedHosts);
|
|
494
|
+
callerSignal.throwIfAborted();
|
|
495
|
+
|
|
496
|
+
const solveController = new AbortController();
|
|
497
|
+
const onCallerAbort = () => solveController.abort(abortReason(callerSignal));
|
|
498
|
+
callerSignal.addEventListener("abort", onCallerAbort, { once: true });
|
|
499
|
+
const timeout = options.now
|
|
500
|
+
? undefined
|
|
501
|
+
: setTimeout(() => solveController.abort(new CapsolverSolveTimeoutError()), timeoutMs);
|
|
502
|
+
const startedAt = now();
|
|
503
|
+
let phase: CapsolverOperationPhase = "create_task";
|
|
504
|
+
|
|
505
|
+
try {
|
|
506
|
+
const createTask = async () => {
|
|
507
|
+
const task =
|
|
508
|
+
challenge.kind === "aws_waf"
|
|
509
|
+
? {
|
|
510
|
+
type: proxy ? "AntiAwsWafTask" : "AntiAwsWafTaskProxyLess",
|
|
511
|
+
websiteURL: challenge.pageUrl,
|
|
512
|
+
...(challenge.siteKey !== undefined ? { awsKey: challenge.siteKey } : {}),
|
|
513
|
+
...(challenge.iv !== undefined ? { awsIv: challenge.iv } : {}),
|
|
514
|
+
...(challenge.context !== undefined ? { awsContext: challenge.context } : {}),
|
|
515
|
+
...(challenge.captchaScript !== undefined
|
|
516
|
+
? { awsChallengeJS: challenge.captchaScript }
|
|
517
|
+
: {}),
|
|
518
|
+
...(proxy ? { proxy: proxy.value } : {}),
|
|
519
|
+
}
|
|
520
|
+
: challenge.kind === "turnstile"
|
|
521
|
+
? {
|
|
522
|
+
type: "AntiTurnstileTaskProxyLess",
|
|
523
|
+
websiteURL: challenge.pageUrl,
|
|
524
|
+
websiteKey: challenge.siteKey,
|
|
525
|
+
...(challenge.action !== undefined || challenge.cdata !== undefined
|
|
526
|
+
? {
|
|
527
|
+
metadata: {
|
|
528
|
+
...(challenge.action !== undefined ? { action: challenge.action } : {}),
|
|
529
|
+
...(challenge.cdata !== undefined ? { cdata: challenge.cdata } : {}),
|
|
530
|
+
},
|
|
531
|
+
}
|
|
532
|
+
: {}),
|
|
533
|
+
}
|
|
534
|
+
: challenge.kind === "recaptcha_v2"
|
|
535
|
+
? {
|
|
536
|
+
type: proxy ? "ReCaptchaV2Task" : "ReCaptchaV2TaskProxyLess",
|
|
537
|
+
websiteURL: challenge.pageUrl,
|
|
538
|
+
websiteKey: challenge.siteKey,
|
|
539
|
+
...(proxy ? { proxy: proxy.value } : {}),
|
|
540
|
+
}
|
|
541
|
+
: challenge.kind === "recaptcha_v3"
|
|
542
|
+
? {
|
|
543
|
+
type: proxy ? "ReCaptchaV3Task" : "ReCaptchaV3TaskProxyLess",
|
|
544
|
+
websiteURL: challenge.pageUrl,
|
|
545
|
+
websiteKey: challenge.siteKey,
|
|
546
|
+
pageAction: challenge.action,
|
|
547
|
+
...(challenge.minScore !== undefined ? { minScore: challenge.minScore } : {}),
|
|
548
|
+
...(proxy ? { proxy: proxy.value } : {}),
|
|
549
|
+
}
|
|
550
|
+
: challenge.kind === "hcaptcha"
|
|
551
|
+
? {
|
|
552
|
+
type: proxy ? "HCaptchaTask" : "HCaptchaTaskProxyLess",
|
|
553
|
+
websiteURL: challenge.pageUrl,
|
|
554
|
+
websiteKey: challenge.siteKey,
|
|
555
|
+
...(proxy ? { proxy: proxy.value } : {}),
|
|
556
|
+
}
|
|
557
|
+
: challenge.kind === "cloudflare_interstitial"
|
|
558
|
+
? {
|
|
559
|
+
type: "AntiCloudflareTask",
|
|
560
|
+
websiteURL: challenge.pageUrl,
|
|
561
|
+
proxy: proxy?.value,
|
|
562
|
+
...(identity?.userAgent ? { userAgent: identity.userAgent } : {}),
|
|
563
|
+
...(challenge.kind === "cloudflare_interstitial" && challenge.blockedHtml !== undefined
|
|
564
|
+
? { html: challenge.blockedHtml }
|
|
565
|
+
: {}),
|
|
566
|
+
}
|
|
567
|
+
: (() => {
|
|
568
|
+
throw new TypeError(`Capsolver resolver does not support ${challenge.kind}`);
|
|
569
|
+
})();
|
|
570
|
+
const createResult = await postJson(
|
|
571
|
+
fetchImpl,
|
|
572
|
+
endpoint(baseUrl, "createTask"),
|
|
573
|
+
{ clientKey: apiKey, task },
|
|
574
|
+
solveController.signal,
|
|
575
|
+
phase,
|
|
576
|
+
sensitiveValues,
|
|
577
|
+
parseCreateTaskResponse,
|
|
578
|
+
);
|
|
579
|
+
const taskId = createResult.payload.taskId;
|
|
580
|
+
if (!createResult.ok || createResult.payload.errorId !== 0 || taskId === undefined) {
|
|
581
|
+
throw unavailableForPayload(createResult.payload, phase, sensitiveValues);
|
|
582
|
+
}
|
|
583
|
+
return taskId;
|
|
584
|
+
};
|
|
585
|
+
const taskId = traceRecorder
|
|
586
|
+
? await traceRecorder.runSpan("resolver.vendor.create_task", createTask, {
|
|
587
|
+
attributes: {
|
|
588
|
+
vendor: CAPSOLVER_VENDOR_ID,
|
|
589
|
+
challenge_kind: challenge.kind,
|
|
590
|
+
},
|
|
591
|
+
onError: (error) => spanErrorAttributes(error, "create_task"),
|
|
592
|
+
})
|
|
593
|
+
: await createTask();
|
|
594
|
+
|
|
595
|
+
phase = "poll_result";
|
|
596
|
+
const pollResult = async () => {
|
|
597
|
+
while (true) {
|
|
598
|
+
callerSignal.throwIfAborted();
|
|
599
|
+
const remainingMs = timeoutMs - (now() - startedAt);
|
|
600
|
+
if (remainingMs <= 0) throw new CapsolverSolveTimeoutError();
|
|
601
|
+
await delay(Math.min(pollIntervalMs, remainingMs), solveController.signal);
|
|
602
|
+
callerSignal.throwIfAborted();
|
|
603
|
+
if (now() - startedAt >= timeoutMs) throw new CapsolverSolveTimeoutError();
|
|
604
|
+
|
|
605
|
+
const result = await postJson(
|
|
606
|
+
fetchImpl,
|
|
607
|
+
endpoint(baseUrl, "getTaskResult"),
|
|
608
|
+
{ clientKey: apiKey, taskId },
|
|
609
|
+
solveController.signal,
|
|
610
|
+
phase,
|
|
611
|
+
sensitiveValues,
|
|
612
|
+
parsePollResultResponse,
|
|
613
|
+
);
|
|
614
|
+
if (!result.ok || result.payload.errorId !== 0) {
|
|
615
|
+
throw unavailableForPayload(result.payload, phase, sensitiveValues);
|
|
616
|
+
}
|
|
617
|
+
switch (result.payload.status) {
|
|
618
|
+
case "idle":
|
|
619
|
+
case "processing":
|
|
620
|
+
continue;
|
|
621
|
+
case "ready":
|
|
622
|
+
break;
|
|
623
|
+
default:
|
|
624
|
+
throw new ResolverVendorUnavailableError(CAPSOLVER_VENDOR_ID, "transport_failure", {
|
|
625
|
+
phase,
|
|
626
|
+
});
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
const solutionValue =
|
|
630
|
+
challenge.kind === "aws_waf"
|
|
631
|
+
? result.payload.solution?.cookie
|
|
632
|
+
: result.payload.solution?.token ?? result.payload.solution?.gRecaptchaResponse;
|
|
633
|
+
if (challenge.kind === "cloudflare_interstitial") {
|
|
634
|
+
const cookies = result.payload.solution?.cookies;
|
|
635
|
+
const clearance = cookies?.cf_clearance ?? solutionValue;
|
|
636
|
+
if (!clearance?.trim()) {
|
|
637
|
+
throw new ResolverVendorUnavailableError(CAPSOLVER_VENDOR_ID, "transport_failure", {
|
|
638
|
+
phase,
|
|
639
|
+
});
|
|
640
|
+
}
|
|
641
|
+
return {
|
|
642
|
+
form: "cookies" as const,
|
|
643
|
+
cookies: cookies && Object.keys(cookies).length > 0 ? cookies : { cf_clearance: clearance },
|
|
644
|
+
userAgent:
|
|
645
|
+
result.payload.solution?.userAgent ??
|
|
646
|
+
identity?.userAgent ??
|
|
647
|
+
getStealthProfile(DEFAULT_PROFILE).userAgent,
|
|
648
|
+
};
|
|
649
|
+
}
|
|
650
|
+
if (!solutionValue?.trim()) {
|
|
651
|
+
throw new ResolverVendorUnavailableError(CAPSOLVER_VENDOR_ID, "transport_failure", {
|
|
652
|
+
phase,
|
|
653
|
+
});
|
|
654
|
+
}
|
|
655
|
+
return challenge.kind === "aws_waf"
|
|
656
|
+
? {
|
|
657
|
+
form: "cookies" as const,
|
|
658
|
+
cookies: { "aws-waf-token": solutionValue },
|
|
659
|
+
userAgent: identity?.userAgent ?? getStealthProfile(DEFAULT_PROFILE).userAgent,
|
|
660
|
+
}
|
|
661
|
+
: { form: "token" as const, token: solutionValue };
|
|
662
|
+
}
|
|
663
|
+
};
|
|
664
|
+
return traceRecorder
|
|
665
|
+
? await traceRecorder.runSpan("resolver.vendor.poll_result", pollResult, {
|
|
666
|
+
attributes: {
|
|
667
|
+
vendor: CAPSOLVER_VENDOR_ID,
|
|
668
|
+
challenge_kind: challenge.kind,
|
|
669
|
+
},
|
|
670
|
+
onError: (error) => spanErrorAttributes(error, "poll_result"),
|
|
671
|
+
})
|
|
672
|
+
: await pollResult();
|
|
673
|
+
} catch (error) {
|
|
674
|
+
if (callerSignal.aborted) throw abortReason(callerSignal);
|
|
675
|
+
if (
|
|
676
|
+
error instanceof CapsolverSolveTimeoutError ||
|
|
677
|
+
solveController.signal.reason instanceof CapsolverSolveTimeoutError
|
|
678
|
+
) {
|
|
679
|
+
throw new ResolverVendorUnavailableError(CAPSOLVER_VENDOR_ID, "timeout", {
|
|
680
|
+
cause: error,
|
|
681
|
+
phase,
|
|
682
|
+
});
|
|
683
|
+
}
|
|
684
|
+
if (
|
|
685
|
+
error instanceof ResolverVendorUnavailableError ||
|
|
686
|
+
error instanceof ResolverChallengeVerdictError
|
|
687
|
+
) {
|
|
688
|
+
throw error;
|
|
689
|
+
}
|
|
690
|
+
throw new ResolverVendorUnavailableError(CAPSOLVER_VENDOR_ID, "transport_failure", {
|
|
691
|
+
...safeCauseOptions(error, sensitiveValues),
|
|
692
|
+
phase,
|
|
693
|
+
});
|
|
694
|
+
} finally {
|
|
695
|
+
if (timeout !== undefined) clearTimeout(timeout);
|
|
696
|
+
callerSignal.removeEventListener("abort", onCallerAbort);
|
|
697
|
+
}
|
|
698
|
+
},
|
|
699
|
+
};
|
|
700
|
+
}
|