@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,526 @@
|
|
|
1
|
+
import { getStealthProfile } from "../../stealth/profiles.js";
|
|
2
|
+
import { redactSensitiveText } from "../request-options.js";
|
|
3
|
+
import { DEFAULT_PROFILE } from "../stealth.js";
|
|
4
|
+
import { assertResolverHostAllowed } from "./hosts.js";
|
|
5
|
+
import { ResolverChallengeVerdictError, ResolverVendorUnavailableError, resolverVendorSupports, } from "./types.js";
|
|
6
|
+
const CAPSOLVER_VENDOR_ID = "capsolver";
|
|
7
|
+
const DEFAULT_CAPSOLVER_BASE_URL = "https://api.capsolver.com";
|
|
8
|
+
const DEFAULT_POLL_INTERVAL_MS = 2_000;
|
|
9
|
+
const DEFAULT_TIMEOUT_MS = 120_000;
|
|
10
|
+
class CapsolverSolveTimeoutError extends Error {
|
|
11
|
+
constructor() {
|
|
12
|
+
super("Capsolver resolver solve budget elapsed");
|
|
13
|
+
this.name = "CapsolverSolveTimeoutError";
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
class CapsolverUnavailableError extends ResolverVendorUnavailableError {
|
|
17
|
+
errorCode;
|
|
18
|
+
errorDescription;
|
|
19
|
+
responseStatus;
|
|
20
|
+
responseContentType;
|
|
21
|
+
responseLength;
|
|
22
|
+
constructor(reason, options) {
|
|
23
|
+
super(CAPSOLVER_VENDOR_ID, reason, options);
|
|
24
|
+
this.errorCode = options.errorCode;
|
|
25
|
+
this.errorDescription = options.errorDescription;
|
|
26
|
+
this.responseStatus = options.responseStatus;
|
|
27
|
+
this.responseContentType = options.responseContentType;
|
|
28
|
+
this.responseLength = options.responseLength;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
class CapsolverVerdictError extends ResolverChallengeVerdictError {
|
|
32
|
+
phase;
|
|
33
|
+
errorCode;
|
|
34
|
+
errorDescription;
|
|
35
|
+
constructor(options) {
|
|
36
|
+
super(CAPSOLVER_VENDOR_ID, "solve_failed", options);
|
|
37
|
+
this.phase = options.phase;
|
|
38
|
+
this.errorCode = options.errorCode;
|
|
39
|
+
this.errorDescription = options.errorDescription;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
function isJsonRecord(value) {
|
|
43
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
44
|
+
}
|
|
45
|
+
function abortReason(signal) {
|
|
46
|
+
return signal.reason ?? new DOMException("The operation was aborted", "AbortError");
|
|
47
|
+
}
|
|
48
|
+
function containsSensitiveValue(value, sensitiveValues) {
|
|
49
|
+
const secrets = sensitiveValues.filter((secret) => secret.length > 0);
|
|
50
|
+
if (secrets.length === 0)
|
|
51
|
+
return false;
|
|
52
|
+
const seen = new Set();
|
|
53
|
+
const inspect = (candidate) => {
|
|
54
|
+
if (typeof candidate === "string") {
|
|
55
|
+
return secrets.some((secret) => candidate.includes(secret));
|
|
56
|
+
}
|
|
57
|
+
if (candidate === null || (typeof candidate !== "object" && typeof candidate !== "function")) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
if (seen.has(candidate))
|
|
61
|
+
return false;
|
|
62
|
+
seen.add(candidate);
|
|
63
|
+
try {
|
|
64
|
+
for (const property of Reflect.ownKeys(candidate)) {
|
|
65
|
+
if (typeof property === "string" && inspect(property))
|
|
66
|
+
return true;
|
|
67
|
+
const descriptor = Object.getOwnPropertyDescriptor(candidate, property);
|
|
68
|
+
if (!descriptor)
|
|
69
|
+
return true;
|
|
70
|
+
if ("value" in descriptor && inspect(descriptor.value))
|
|
71
|
+
return true;
|
|
72
|
+
if (descriptor.get !== undefined || descriptor.set !== undefined)
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
return false;
|
|
80
|
+
};
|
|
81
|
+
return inspect(value);
|
|
82
|
+
}
|
|
83
|
+
function safeCauseOptions(error, sensitiveValues) {
|
|
84
|
+
return containsSensitiveValue(error, sensitiveValues) ? {} : { cause: error };
|
|
85
|
+
}
|
|
86
|
+
function sanitizedJsonParseCause() {
|
|
87
|
+
return new SyntaxError("Upstream response failed");
|
|
88
|
+
}
|
|
89
|
+
function raceWithAbort(operation, signal, phase, sensitiveValues = []) {
|
|
90
|
+
if (signal.aborted)
|
|
91
|
+
return Promise.reject(abortReason(signal));
|
|
92
|
+
return new Promise((resolve, reject) => {
|
|
93
|
+
const cleanup = () => signal.removeEventListener("abort", onAbort);
|
|
94
|
+
const onAbort = () => {
|
|
95
|
+
cleanup();
|
|
96
|
+
reject(abortReason(signal));
|
|
97
|
+
};
|
|
98
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
99
|
+
operation().then((value) => {
|
|
100
|
+
cleanup();
|
|
101
|
+
resolve(value);
|
|
102
|
+
}, (error) => {
|
|
103
|
+
cleanup();
|
|
104
|
+
if (phase === undefined) {
|
|
105
|
+
reject(error);
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
reject(new ResolverVendorUnavailableError(CAPSOLVER_VENDOR_ID, "transport_failure", {
|
|
109
|
+
...safeCauseOptions(error, sensitiveValues),
|
|
110
|
+
phase,
|
|
111
|
+
}));
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
async function abortableDelay(ms, signal) {
|
|
116
|
+
let timer;
|
|
117
|
+
try {
|
|
118
|
+
await raceWithAbort(() => new Promise((resolve) => {
|
|
119
|
+
timer = setTimeout(resolve, ms);
|
|
120
|
+
}), signal);
|
|
121
|
+
}
|
|
122
|
+
finally {
|
|
123
|
+
if (timer !== undefined)
|
|
124
|
+
clearTimeout(timer);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
function responseErrorFields(payload) {
|
|
128
|
+
return {
|
|
129
|
+
errorId: typeof payload.errorId === "number" ? payload.errorId : undefined,
|
|
130
|
+
errorCode: typeof payload.errorCode === "string" ? payload.errorCode : undefined,
|
|
131
|
+
errorDescription: typeof payload.errorDescription === "string" ? payload.errorDescription : undefined,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
function parseCreateTaskResponse(payload) {
|
|
135
|
+
const taskId = payload.taskId;
|
|
136
|
+
return {
|
|
137
|
+
...responseErrorFields(payload),
|
|
138
|
+
taskId: typeof taskId === "string" || typeof taskId === "number" ? taskId : undefined,
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
function parsePollResultResponse(payload) {
|
|
142
|
+
const solution = isJsonRecord(payload.solution) ? payload.solution : undefined;
|
|
143
|
+
const cookies = solution && isJsonRecord(solution.cookies)
|
|
144
|
+
? Object.fromEntries(Object.entries(solution.cookies).filter((entry) => typeof entry[1] === "string"))
|
|
145
|
+
: undefined;
|
|
146
|
+
return {
|
|
147
|
+
...responseErrorFields(payload),
|
|
148
|
+
status: typeof payload.status === "string" ? payload.status : undefined,
|
|
149
|
+
solution: solution
|
|
150
|
+
? {
|
|
151
|
+
token: typeof solution.token === "string" ? solution.token : undefined,
|
|
152
|
+
cookie: typeof solution.cookie === "string" ? solution.cookie : undefined,
|
|
153
|
+
gRecaptchaResponse: typeof solution.gRecaptchaResponse === "string"
|
|
154
|
+
? solution.gRecaptchaResponse
|
|
155
|
+
: undefined,
|
|
156
|
+
cookies,
|
|
157
|
+
userAgent: typeof solution.userAgent === "string" ? solution.userAgent : undefined,
|
|
158
|
+
}
|
|
159
|
+
: undefined,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
function proxyForCapsolver(proxyUrl) {
|
|
163
|
+
try {
|
|
164
|
+
const url = new URL(proxyUrl);
|
|
165
|
+
const protocol = url.protocol.slice(0, -1).toLowerCase();
|
|
166
|
+
const scheme = protocol === "http" || protocol === "socks5" ? protocol : undefined;
|
|
167
|
+
const port = Number(url.port || (scheme === "socks5" ? 1080 : 80));
|
|
168
|
+
if (!scheme || !url.hostname || !Number.isInteger(port) || port <= 0 || port > 65_535) {
|
|
169
|
+
return undefined;
|
|
170
|
+
}
|
|
171
|
+
const username = url.username ? decodeURIComponent(url.username) : "";
|
|
172
|
+
const password = url.password ? decodeURIComponent(url.password) : "";
|
|
173
|
+
if (username.includes(":") || password.includes(":"))
|
|
174
|
+
return undefined;
|
|
175
|
+
const value = `${scheme}:${url.hostname}:${port}${username || password ? `:${username}:${password}` : ""}`;
|
|
176
|
+
return { value, sensitive: [proxyUrl, value, username, password].filter(Boolean) };
|
|
177
|
+
}
|
|
178
|
+
catch {
|
|
179
|
+
return undefined;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
function isAllocationExhausted(payload) {
|
|
183
|
+
const code = payload.errorCode?.toLowerCase() ?? "";
|
|
184
|
+
const description = payload.errorDescription?.toLowerCase() ?? "";
|
|
185
|
+
return (code === "error_zero_balance" ||
|
|
186
|
+
/(?:insufficient|zero|no|not enough)\s+(?:balance|funds|credit)/u.test(`${code} ${description}`));
|
|
187
|
+
}
|
|
188
|
+
function isNegativeVerdict(payload) {
|
|
189
|
+
return payload.errorCode?.toLowerCase() === "error_captcha_unsolvable";
|
|
190
|
+
}
|
|
191
|
+
function safeVendorDetail(value, sensitiveValues) {
|
|
192
|
+
const redacted = redactSensitiveText(value, sensitiveValues);
|
|
193
|
+
return containsSensitiveValue(redacted, sensitiveValues) ? "[REDACTED]" : redacted;
|
|
194
|
+
}
|
|
195
|
+
function vendorErrorDetails(payload, sensitiveValues) {
|
|
196
|
+
const errorCode = payload.errorCode?.trim();
|
|
197
|
+
const errorDescription = payload.errorDescription?.trim();
|
|
198
|
+
return {
|
|
199
|
+
...(errorCode ? { errorCode: safeVendorDetail(errorCode, sensitiveValues) } : {}),
|
|
200
|
+
...(errorDescription
|
|
201
|
+
? { errorDescription: safeVendorDetail(payload.errorDescription ?? "", sensitiveValues) }
|
|
202
|
+
: {}),
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
function unavailableForPayload(payload, phase, sensitiveValues) {
|
|
206
|
+
const details = vendorErrorDetails(payload, sensitiveValues);
|
|
207
|
+
if (isNegativeVerdict(payload)) {
|
|
208
|
+
return new CapsolverVerdictError({ phase, ...details });
|
|
209
|
+
}
|
|
210
|
+
return new CapsolverUnavailableError(isAllocationExhausted(payload) ? "allocation_exhausted" : "transport_failure", { phase, ...details });
|
|
211
|
+
}
|
|
212
|
+
async function postJson(fetchImpl, url, body, signal, phase, sensitiveValues, parseResponse) {
|
|
213
|
+
const response = await raceWithAbort(() => fetchImpl(url, {
|
|
214
|
+
method: "POST",
|
|
215
|
+
headers: { "content-type": "application/json" },
|
|
216
|
+
body: JSON.stringify(body),
|
|
217
|
+
signal,
|
|
218
|
+
redirect: "error",
|
|
219
|
+
}), signal, phase, sensitiveValues);
|
|
220
|
+
let responseText;
|
|
221
|
+
try {
|
|
222
|
+
responseText = await raceWithAbort(() => response.text(), signal, phase, sensitiveValues);
|
|
223
|
+
}
|
|
224
|
+
catch (error) {
|
|
225
|
+
if (error instanceof ResolverVendorUnavailableError)
|
|
226
|
+
throw error;
|
|
227
|
+
throw new ResolverVendorUnavailableError(CAPSOLVER_VENDOR_ID, "transport_failure", {
|
|
228
|
+
cause: error,
|
|
229
|
+
phase,
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
let payload;
|
|
233
|
+
try {
|
|
234
|
+
payload = JSON.parse(responseText);
|
|
235
|
+
}
|
|
236
|
+
catch {
|
|
237
|
+
const contentType = response.headers.get("content-type");
|
|
238
|
+
throw new CapsolverUnavailableError("transport_failure", {
|
|
239
|
+
cause: sanitizedJsonParseCause(),
|
|
240
|
+
phase,
|
|
241
|
+
responseStatus: response.status,
|
|
242
|
+
responseContentType: contentType
|
|
243
|
+
? safeVendorDetail(contentType.slice(0, 128), sensitiveValues)
|
|
244
|
+
: undefined,
|
|
245
|
+
responseLength: responseText.length,
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
if (!isJsonRecord(payload)) {
|
|
249
|
+
throw new ResolverVendorUnavailableError(CAPSOLVER_VENDOR_ID, "transport_failure", {
|
|
250
|
+
phase,
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
return { ok: response.ok, payload: parseResponse(payload) };
|
|
254
|
+
}
|
|
255
|
+
function endpoint(baseUrl, path) {
|
|
256
|
+
return `${baseUrl.replace(/\/+$/u, "")}/${path}`;
|
|
257
|
+
}
|
|
258
|
+
function spanErrorAttributes(error, phase) {
|
|
259
|
+
if (error instanceof ResolverVendorUnavailableError) {
|
|
260
|
+
return {
|
|
261
|
+
unavailability_reason: error.reason,
|
|
262
|
+
transport_phase: error.phase,
|
|
263
|
+
...(error instanceof CapsolverUnavailableError
|
|
264
|
+
? {
|
|
265
|
+
vendor_error_code: error.errorCode,
|
|
266
|
+
vendor_error_description: error.errorDescription,
|
|
267
|
+
response_status: error.responseStatus,
|
|
268
|
+
response_content_type: error.responseContentType,
|
|
269
|
+
response_length: error.responseLength,
|
|
270
|
+
}
|
|
271
|
+
: {}),
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
if (error instanceof CapsolverVerdictError) {
|
|
275
|
+
return {
|
|
276
|
+
verdict_reason: error.reason,
|
|
277
|
+
transport_phase: error.phase,
|
|
278
|
+
vendor_error_code: error.errorCode,
|
|
279
|
+
vendor_error_description: error.errorDescription,
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
if (error instanceof CapsolverSolveTimeoutError) {
|
|
283
|
+
return {
|
|
284
|
+
unavailability_reason: "timeout",
|
|
285
|
+
transport_phase: phase,
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
return undefined;
|
|
289
|
+
}
|
|
290
|
+
export function createCapsolverResolverVendorAdapter(options) {
|
|
291
|
+
const fetchImpl = options.fetchImpl ?? globalThis.fetch;
|
|
292
|
+
const baseUrl = options.baseUrl ?? DEFAULT_CAPSOLVER_BASE_URL;
|
|
293
|
+
const pollIntervalMs = options.pollIntervalMs ?? DEFAULT_POLL_INTERVAL_MS;
|
|
294
|
+
const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
295
|
+
const now = options.now ?? Date.now;
|
|
296
|
+
const delay = options.delay ?? abortableDelay;
|
|
297
|
+
return {
|
|
298
|
+
id: CAPSOLVER_VENDOR_ID,
|
|
299
|
+
supports(kind) {
|
|
300
|
+
return resolverVendorSupports(CAPSOLVER_VENDOR_ID, kind);
|
|
301
|
+
},
|
|
302
|
+
async solve(challenge, identity, callerSignal, traceRecorder) {
|
|
303
|
+
const apiKey = options.apiKey?.trim();
|
|
304
|
+
if (!apiKey) {
|
|
305
|
+
throw new ResolverVendorUnavailableError(CAPSOLVER_VENDOR_ID, "missing_credentials", {
|
|
306
|
+
phase: "create_task",
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
if (!resolverVendorSupports(CAPSOLVER_VENDOR_ID, challenge.kind)) {
|
|
310
|
+
throw new TypeError(`Capsolver resolver does not support ${challenge.kind}`);
|
|
311
|
+
}
|
|
312
|
+
const proxy = identity ? proxyForCapsolver(identity.proxyUrl) : undefined;
|
|
313
|
+
if (challenge.kind === "aws_waf" && identity && !proxy) {
|
|
314
|
+
throw new ResolverVendorUnavailableError(CAPSOLVER_VENDOR_ID, "transport_failure", {
|
|
315
|
+
phase: "create_task",
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
if (challenge.kind === "cloudflare_interstitial" && !identity) {
|
|
319
|
+
throw new ResolverVendorUnavailableError(CAPSOLVER_VENDOR_ID, "missing_proxy_identity", {
|
|
320
|
+
phase: "create_task",
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
if (identity && !proxy) {
|
|
324
|
+
throw new ResolverVendorUnavailableError(CAPSOLVER_VENDOR_ID, "transport_failure", {
|
|
325
|
+
phase: "create_task",
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
const challengeFields = challenge;
|
|
329
|
+
const sensitiveValues = [
|
|
330
|
+
apiKey,
|
|
331
|
+
challenge.pageUrl,
|
|
332
|
+
...(typeof challengeFields.siteKey === "string" ? [challengeFields.siteKey] : []),
|
|
333
|
+
...(typeof challengeFields.action === "string" ? [challengeFields.action] : []),
|
|
334
|
+
...(typeof challengeFields.cdata === "string" ? [challengeFields.cdata] : []),
|
|
335
|
+
...(typeof challengeFields.blockedHtml === "string" ? [challengeFields.blockedHtml] : []),
|
|
336
|
+
...(typeof challengeFields.captchaScript === "string" ? [challengeFields.captchaScript] : []),
|
|
337
|
+
...(typeof challengeFields.context === "string" ? [challengeFields.context] : []),
|
|
338
|
+
...(typeof challengeFields.iv === "string" ? [challengeFields.iv] : []),
|
|
339
|
+
...(proxy?.sensitive ?? []),
|
|
340
|
+
];
|
|
341
|
+
assertResolverHostAllowed(challenge.pageUrl, options.allowedHosts);
|
|
342
|
+
callerSignal.throwIfAborted();
|
|
343
|
+
const solveController = new AbortController();
|
|
344
|
+
const onCallerAbort = () => solveController.abort(abortReason(callerSignal));
|
|
345
|
+
callerSignal.addEventListener("abort", onCallerAbort, { once: true });
|
|
346
|
+
const timeout = options.now
|
|
347
|
+
? undefined
|
|
348
|
+
: setTimeout(() => solveController.abort(new CapsolverSolveTimeoutError()), timeoutMs);
|
|
349
|
+
const startedAt = now();
|
|
350
|
+
let phase = "create_task";
|
|
351
|
+
try {
|
|
352
|
+
const createTask = async () => {
|
|
353
|
+
const task = challenge.kind === "aws_waf"
|
|
354
|
+
? {
|
|
355
|
+
type: proxy ? "AntiAwsWafTask" : "AntiAwsWafTaskProxyLess",
|
|
356
|
+
websiteURL: challenge.pageUrl,
|
|
357
|
+
...(challenge.siteKey !== undefined ? { awsKey: challenge.siteKey } : {}),
|
|
358
|
+
...(challenge.iv !== undefined ? { awsIv: challenge.iv } : {}),
|
|
359
|
+
...(challenge.context !== undefined ? { awsContext: challenge.context } : {}),
|
|
360
|
+
...(challenge.captchaScript !== undefined
|
|
361
|
+
? { awsChallengeJS: challenge.captchaScript }
|
|
362
|
+
: {}),
|
|
363
|
+
...(proxy ? { proxy: proxy.value } : {}),
|
|
364
|
+
}
|
|
365
|
+
: challenge.kind === "turnstile"
|
|
366
|
+
? {
|
|
367
|
+
type: "AntiTurnstileTaskProxyLess",
|
|
368
|
+
websiteURL: challenge.pageUrl,
|
|
369
|
+
websiteKey: challenge.siteKey,
|
|
370
|
+
...(challenge.action !== undefined || challenge.cdata !== undefined
|
|
371
|
+
? {
|
|
372
|
+
metadata: {
|
|
373
|
+
...(challenge.action !== undefined ? { action: challenge.action } : {}),
|
|
374
|
+
...(challenge.cdata !== undefined ? { cdata: challenge.cdata } : {}),
|
|
375
|
+
},
|
|
376
|
+
}
|
|
377
|
+
: {}),
|
|
378
|
+
}
|
|
379
|
+
: challenge.kind === "recaptcha_v2"
|
|
380
|
+
? {
|
|
381
|
+
type: proxy ? "ReCaptchaV2Task" : "ReCaptchaV2TaskProxyLess",
|
|
382
|
+
websiteURL: challenge.pageUrl,
|
|
383
|
+
websiteKey: challenge.siteKey,
|
|
384
|
+
...(proxy ? { proxy: proxy.value } : {}),
|
|
385
|
+
}
|
|
386
|
+
: challenge.kind === "recaptcha_v3"
|
|
387
|
+
? {
|
|
388
|
+
type: proxy ? "ReCaptchaV3Task" : "ReCaptchaV3TaskProxyLess",
|
|
389
|
+
websiteURL: challenge.pageUrl,
|
|
390
|
+
websiteKey: challenge.siteKey,
|
|
391
|
+
pageAction: challenge.action,
|
|
392
|
+
...(challenge.minScore !== undefined ? { minScore: challenge.minScore } : {}),
|
|
393
|
+
...(proxy ? { proxy: proxy.value } : {}),
|
|
394
|
+
}
|
|
395
|
+
: challenge.kind === "hcaptcha"
|
|
396
|
+
? {
|
|
397
|
+
type: proxy ? "HCaptchaTask" : "HCaptchaTaskProxyLess",
|
|
398
|
+
websiteURL: challenge.pageUrl,
|
|
399
|
+
websiteKey: challenge.siteKey,
|
|
400
|
+
...(proxy ? { proxy: proxy.value } : {}),
|
|
401
|
+
}
|
|
402
|
+
: challenge.kind === "cloudflare_interstitial"
|
|
403
|
+
? {
|
|
404
|
+
type: "AntiCloudflareTask",
|
|
405
|
+
websiteURL: challenge.pageUrl,
|
|
406
|
+
proxy: proxy?.value,
|
|
407
|
+
...(identity?.userAgent ? { userAgent: identity.userAgent } : {}),
|
|
408
|
+
...(challenge.kind === "cloudflare_interstitial" && challenge.blockedHtml !== undefined
|
|
409
|
+
? { html: challenge.blockedHtml }
|
|
410
|
+
: {}),
|
|
411
|
+
}
|
|
412
|
+
: (() => {
|
|
413
|
+
throw new TypeError(`Capsolver resolver does not support ${challenge.kind}`);
|
|
414
|
+
})();
|
|
415
|
+
const createResult = await postJson(fetchImpl, endpoint(baseUrl, "createTask"), { clientKey: apiKey, task }, solveController.signal, phase, sensitiveValues, parseCreateTaskResponse);
|
|
416
|
+
const taskId = createResult.payload.taskId;
|
|
417
|
+
if (!createResult.ok || createResult.payload.errorId !== 0 || taskId === undefined) {
|
|
418
|
+
throw unavailableForPayload(createResult.payload, phase, sensitiveValues);
|
|
419
|
+
}
|
|
420
|
+
return taskId;
|
|
421
|
+
};
|
|
422
|
+
const taskId = traceRecorder
|
|
423
|
+
? await traceRecorder.runSpan("resolver.vendor.create_task", createTask, {
|
|
424
|
+
attributes: {
|
|
425
|
+
vendor: CAPSOLVER_VENDOR_ID,
|
|
426
|
+
challenge_kind: challenge.kind,
|
|
427
|
+
},
|
|
428
|
+
onError: (error) => spanErrorAttributes(error, "create_task"),
|
|
429
|
+
})
|
|
430
|
+
: await createTask();
|
|
431
|
+
phase = "poll_result";
|
|
432
|
+
const pollResult = async () => {
|
|
433
|
+
while (true) {
|
|
434
|
+
callerSignal.throwIfAborted();
|
|
435
|
+
const remainingMs = timeoutMs - (now() - startedAt);
|
|
436
|
+
if (remainingMs <= 0)
|
|
437
|
+
throw new CapsolverSolveTimeoutError();
|
|
438
|
+
await delay(Math.min(pollIntervalMs, remainingMs), solveController.signal);
|
|
439
|
+
callerSignal.throwIfAborted();
|
|
440
|
+
if (now() - startedAt >= timeoutMs)
|
|
441
|
+
throw new CapsolverSolveTimeoutError();
|
|
442
|
+
const result = await postJson(fetchImpl, endpoint(baseUrl, "getTaskResult"), { clientKey: apiKey, taskId }, solveController.signal, phase, sensitiveValues, parsePollResultResponse);
|
|
443
|
+
if (!result.ok || result.payload.errorId !== 0) {
|
|
444
|
+
throw unavailableForPayload(result.payload, phase, sensitiveValues);
|
|
445
|
+
}
|
|
446
|
+
switch (result.payload.status) {
|
|
447
|
+
case "idle":
|
|
448
|
+
case "processing":
|
|
449
|
+
continue;
|
|
450
|
+
case "ready":
|
|
451
|
+
break;
|
|
452
|
+
default:
|
|
453
|
+
throw new ResolverVendorUnavailableError(CAPSOLVER_VENDOR_ID, "transport_failure", {
|
|
454
|
+
phase,
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
const solutionValue = challenge.kind === "aws_waf"
|
|
458
|
+
? result.payload.solution?.cookie
|
|
459
|
+
: result.payload.solution?.token ?? result.payload.solution?.gRecaptchaResponse;
|
|
460
|
+
if (challenge.kind === "cloudflare_interstitial") {
|
|
461
|
+
const cookies = result.payload.solution?.cookies;
|
|
462
|
+
const clearance = cookies?.cf_clearance ?? solutionValue;
|
|
463
|
+
if (!clearance?.trim()) {
|
|
464
|
+
throw new ResolverVendorUnavailableError(CAPSOLVER_VENDOR_ID, "transport_failure", {
|
|
465
|
+
phase,
|
|
466
|
+
});
|
|
467
|
+
}
|
|
468
|
+
return {
|
|
469
|
+
form: "cookies",
|
|
470
|
+
cookies: cookies && Object.keys(cookies).length > 0 ? cookies : { cf_clearance: clearance },
|
|
471
|
+
userAgent: result.payload.solution?.userAgent ??
|
|
472
|
+
identity?.userAgent ??
|
|
473
|
+
getStealthProfile(DEFAULT_PROFILE).userAgent,
|
|
474
|
+
};
|
|
475
|
+
}
|
|
476
|
+
if (!solutionValue?.trim()) {
|
|
477
|
+
throw new ResolverVendorUnavailableError(CAPSOLVER_VENDOR_ID, "transport_failure", {
|
|
478
|
+
phase,
|
|
479
|
+
});
|
|
480
|
+
}
|
|
481
|
+
return challenge.kind === "aws_waf"
|
|
482
|
+
? {
|
|
483
|
+
form: "cookies",
|
|
484
|
+
cookies: { "aws-waf-token": solutionValue },
|
|
485
|
+
userAgent: identity?.userAgent ?? getStealthProfile(DEFAULT_PROFILE).userAgent,
|
|
486
|
+
}
|
|
487
|
+
: { form: "token", token: solutionValue };
|
|
488
|
+
}
|
|
489
|
+
};
|
|
490
|
+
return traceRecorder
|
|
491
|
+
? await traceRecorder.runSpan("resolver.vendor.poll_result", pollResult, {
|
|
492
|
+
attributes: {
|
|
493
|
+
vendor: CAPSOLVER_VENDOR_ID,
|
|
494
|
+
challenge_kind: challenge.kind,
|
|
495
|
+
},
|
|
496
|
+
onError: (error) => spanErrorAttributes(error, "poll_result"),
|
|
497
|
+
})
|
|
498
|
+
: await pollResult();
|
|
499
|
+
}
|
|
500
|
+
catch (error) {
|
|
501
|
+
if (callerSignal.aborted)
|
|
502
|
+
throw abortReason(callerSignal);
|
|
503
|
+
if (error instanceof CapsolverSolveTimeoutError ||
|
|
504
|
+
solveController.signal.reason instanceof CapsolverSolveTimeoutError) {
|
|
505
|
+
throw new ResolverVendorUnavailableError(CAPSOLVER_VENDOR_ID, "timeout", {
|
|
506
|
+
cause: error,
|
|
507
|
+
phase,
|
|
508
|
+
});
|
|
509
|
+
}
|
|
510
|
+
if (error instanceof ResolverVendorUnavailableError ||
|
|
511
|
+
error instanceof ResolverChallengeVerdictError) {
|
|
512
|
+
throw error;
|
|
513
|
+
}
|
|
514
|
+
throw new ResolverVendorUnavailableError(CAPSOLVER_VENDOR_ID, "transport_failure", {
|
|
515
|
+
...safeCauseOptions(error, sensitiveValues),
|
|
516
|
+
phase,
|
|
517
|
+
});
|
|
518
|
+
}
|
|
519
|
+
finally {
|
|
520
|
+
if (timeout !== undefined)
|
|
521
|
+
clearTimeout(timeout);
|
|
522
|
+
callerSignal.removeEventListener("abort", onCallerAbort);
|
|
523
|
+
}
|
|
524
|
+
},
|
|
525
|
+
};
|
|
526
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { ProviderError } from "../../errors.js";
|
|
2
|
+
export function normalizedResolverHostname(hostname) {
|
|
3
|
+
return hostname.trim().toLowerCase().replace(/\.$/, "");
|
|
4
|
+
}
|
|
5
|
+
export function assertResolverHostAllowed(targetUrl, allowedHosts) {
|
|
6
|
+
let targetUrlObject;
|
|
7
|
+
try {
|
|
8
|
+
targetUrlObject = new URL(targetUrl);
|
|
9
|
+
}
|
|
10
|
+
catch {
|
|
11
|
+
throw new ProviderError("Resolver target URL is invalid", {
|
|
12
|
+
code: "RESOLVER_HOST_NOT_ALLOWED",
|
|
13
|
+
fix: "Use a valid URL whose exact hostname appears in the provider's allowedHosts declaration.",
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
if (targetUrlObject.protocol !== "http:" && targetUrlObject.protocol !== "https:") {
|
|
17
|
+
throw new ProviderError(`Resolver target URL scheme "${targetUrlObject.protocol}" is not allowed`, {
|
|
18
|
+
code: "RESOLVER_HOST_NOT_ALLOWED",
|
|
19
|
+
fix: "Use an http or https URL whose exact hostname appears in the provider's allowedHosts declaration.",
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
const targetHost = normalizedResolverHostname(targetUrlObject.hostname);
|
|
23
|
+
const isAllowed = allowedHosts.some((host) => {
|
|
24
|
+
const declaredHost = normalizedResolverHostname(host);
|
|
25
|
+
return declaredHost.length > 0 && !declaredHost.includes("*") && declaredHost === targetHost;
|
|
26
|
+
});
|
|
27
|
+
if (isAllowed)
|
|
28
|
+
return;
|
|
29
|
+
throw new ProviderError(`Resolver target host "${targetHost}" is not declared`, {
|
|
30
|
+
code: "RESOLVER_HOST_NOT_ALLOWED",
|
|
31
|
+
fix: "Add the exact target hostname to the provider's allowedHosts declaration.",
|
|
32
|
+
});
|
|
33
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
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 TwoCaptchaResolverVendorOptions {
|
|
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 TwoCaptchaResolverVendorAdapter extends ResolverVendorAdapter {
|
|
18
|
+
readonly id: "2captcha";
|
|
19
|
+
solve(challenge: ProviderChallenge, identity: ResolverIdentity | undefined, signal: AbortSignal, traceRecorder?: TraceRecorder): Promise<Extract<ChallengeSolution, {
|
|
20
|
+
readonly form: "token";
|
|
21
|
+
}>>;
|
|
22
|
+
}
|
|
23
|
+
export declare function createTwoCaptchaResolverVendorAdapter(options: TwoCaptchaResolverVendorOptions): TwoCaptchaResolverVendorAdapter;
|
|
24
|
+
export {};
|