milieu-cli 0.3.2 → 0.4.0
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/README.md +177 -76
- package/dist/bridges/context/agents-json.d.ts +0 -1
- package/dist/bridges/context/agents-json.js +21 -12
- package/dist/bridges/context/auth-clarity.d.ts +0 -1
- package/dist/bridges/context/auth-clarity.js +11 -9
- package/dist/bridges/context/auth-legibility.d.ts +2 -2
- package/dist/bridges/context/auth-legibility.js +67 -60
- package/dist/bridges/context/index.d.ts +1 -2
- package/dist/bridges/context/index.js +19 -40
- package/dist/bridges/context/oauth-metadata.d.ts +14 -0
- package/dist/bridges/context/oauth-metadata.js +127 -0
- package/dist/bridges/context/probe.d.ts +33 -0
- package/dist/bridges/context/probe.js +157 -0
- package/dist/bridges/context/rate-limits.d.ts +5 -2
- package/dist/bridges/context/rate-limits.js +27 -51
- package/dist/bridges/context/tos-contact.d.ts +13 -4
- package/dist/bridges/context/tos-contact.js +108 -46
- package/dist/bridges/context/versioning.d.ts +1 -2
- package/dist/bridges/context/versioning.js +42 -7
- package/dist/bridges/index.d.ts +0 -1
- package/dist/bridges/index.js +0 -1
- package/dist/bridges/reachability/crawler-policy.d.ts +30 -10
- package/dist/bridges/reachability/crawler-policy.js +41 -56
- package/dist/bridges/reachability/http-status.d.ts +3 -2
- package/dist/bridges/reachability/http-status.js +18 -3
- package/dist/bridges/reachability/https-check.d.ts +2 -2
- package/dist/bridges/reachability/https-check.js +6 -3
- package/dist/bridges/reachability/index.d.ts +1 -2
- package/dist/bridges/reachability/index.js +34 -43
- package/dist/bridges/reachability/meta-robots.d.ts +0 -13
- package/dist/bridges/reachability/meta-robots.js +89 -101
- package/dist/bridges/reachability/robots-parser.d.ts +7 -7
- package/dist/bridges/reachability/robots-parser.js +65 -15
- package/dist/bridges/reachability/robots-txt.d.ts +4 -2
- package/dist/bridges/reachability/robots-txt.js +33 -60
- package/dist/bridges/schema/descriptions.d.ts +2 -2
- package/dist/bridges/schema/descriptions.js +79 -44
- package/dist/bridges/schema/error-schemas.d.ts +2 -2
- package/dist/bridges/schema/error-schemas.js +31 -18
- package/dist/bridges/schema/index.d.ts +2 -2
- package/dist/bridges/schema/index.js +48 -30
- package/dist/bridges/schema/oas-types.d.ts +65 -26
- package/dist/bridges/schema/oas-types.js +37 -7
- package/dist/bridges/schema/operation-ids.d.ts +2 -2
- package/dist/bridges/schema/operation-ids.js +2 -3
- package/dist/bridges/schema/required-fields.d.ts +2 -2
- package/dist/bridges/schema/required-fields.js +12 -10
- package/dist/bridges/schema/schema-sources.d.ts +19 -0
- package/dist/bridges/schema/schema-sources.js +182 -0
- package/dist/bridges/schema/schema-types.d.ts +2 -2
- package/dist/bridges/schema/schema-types.js +20 -21
- package/dist/bridges/separation/api-presence.d.ts +3 -15
- package/dist/bridges/separation/api-presence.js +34 -85
- package/dist/bridges/separation/developer-docs.d.ts +6 -18
- package/dist/bridges/separation/developer-docs.js +100 -99
- package/dist/bridges/separation/docs-discovery.d.ts +28 -0
- package/dist/bridges/separation/docs-discovery.js +95 -0
- package/dist/bridges/separation/evidence-summary.d.ts +2 -0
- package/dist/bridges/separation/evidence-summary.js +67 -0
- package/dist/bridges/separation/index.d.ts +2 -19
- package/dist/bridges/separation/index.js +35 -39
- package/dist/bridges/separation/published-routes.d.ts +69 -0
- package/dist/bridges/separation/published-routes.js +271 -0
- package/dist/bridges/separation/sdk-references.d.ts +8 -3
- package/dist/bridges/separation/sdk-references.js +86 -61
- package/dist/bridges/separation/webhook-support.d.ts +3 -23
- package/dist/bridges/separation/webhook-support.js +61 -162
- package/dist/bridges/standards/a2a-agent-card.d.ts +0 -1
- package/dist/bridges/standards/a2a-agent-card.js +24 -9
- package/dist/bridges/standards/graphql.d.ts +3 -4
- package/dist/bridges/standards/graphql.js +70 -66
- package/dist/bridges/standards/index.d.ts +1 -2
- package/dist/bridges/standards/index.js +57 -47
- package/dist/bridges/standards/json-ld.d.ts +25 -11
- package/dist/bridges/standards/json-ld.js +114 -56
- package/dist/bridges/standards/llms-txt.d.ts +4 -2
- package/dist/bridges/standards/llms-txt.js +46 -32
- package/dist/bridges/standards/markdown-negotiation.d.ts +4 -1
- package/dist/bridges/standards/markdown-negotiation.js +16 -11
- package/dist/bridges/standards/mcp.d.ts +3 -12
- package/dist/bridges/standards/mcp.js +89 -132
- package/dist/bridges/standards/openapi.d.ts +7 -15
- package/dist/bridges/standards/openapi.js +657 -207
- package/dist/bridges/standards/probe-evidence.d.ts +30 -0
- package/dist/bridges/standards/probe-evidence.js +90 -0
- package/dist/bridges/standards/schema-org.d.ts +15 -9
- package/dist/bridges/standards/schema-org.js +38 -64
- package/dist/bridges/standards/sitemap.d.ts +2 -3
- package/dist/bridges/standards/sitemap.js +161 -169
- package/dist/bridges/standards/well-known.d.ts +3 -7
- package/dist/bridges/standards/well-known.js +28 -12
- package/dist/cli/index.d.ts +1 -1
- package/dist/cli/index.js +107 -22
- package/dist/cli/journey-commands.d.ts +3 -0
- package/dist/cli/journey-commands.js +35 -0
- package/dist/cli/read-report.d.ts +2 -0
- package/dist/cli/read-report.js +11 -0
- package/dist/core/bridge-output.d.ts +11 -0
- package/dist/core/bridge-output.js +86 -0
- package/dist/core/bridge-session.d.ts +28 -0
- package/dist/core/bridge-session.js +58 -0
- package/dist/core/bridge-worker.d.ts +1 -0
- package/dist/core/bridge-worker.js +66 -0
- package/dist/core/diff.d.ts +79 -0
- package/dist/core/diff.js +499 -0
- package/dist/core/explanations.d.ts +1 -3
- package/dist/core/explanations.js +36 -59
- package/dist/core/index.d.ts +4 -2
- package/dist/core/index.js +2 -3
- package/dist/core/scan-options.d.ts +0 -1
- package/dist/core/scan-options.js +14 -6
- package/dist/core/scan.d.ts +0 -1
- package/dist/core/scan.js +91 -41
- package/dist/core/scoring.d.ts +6 -0
- package/dist/core/scoring.js +27 -0
- package/dist/core/types.d.ts +97 -61
- package/dist/core/types.js +9 -3
- package/dist/core/validate-report.d.ts +5 -0
- package/dist/core/validate-report.js +95 -0
- package/dist/core/version.d.ts +0 -1
- package/dist/core/version.js +0 -1
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/dist/journey/body-worker.d.ts +27 -0
- package/dist/journey/body-worker.js +140 -0
- package/dist/journey/case.d.ts +74 -0
- package/dist/journey/case.js +187 -0
- package/dist/journey/cli.d.ts +14 -0
- package/dist/journey/cli.js +226 -0
- package/dist/journey/compare.d.ts +43 -0
- package/dist/journey/compare.js +83 -0
- package/dist/journey/contracts.d.ts +143 -0
- package/dist/journey/contracts.js +37 -0
- package/dist/journey/decision.d.ts +72 -0
- package/dist/journey/decision.js +121 -0
- package/dist/journey/demo.d.ts +36 -0
- package/dist/journey/demo.js +137 -0
- package/dist/journey/document.d.ts +8 -0
- package/dist/journey/document.js +113 -0
- package/dist/journey/excerpt.d.ts +6 -0
- package/dist/journey/excerpt.js +54 -0
- package/dist/journey/name-guidance.d.ts +2 -0
- package/dist/journey/name-guidance.js +41 -0
- package/dist/journey/providers.d.ts +34 -0
- package/dist/journey/providers.js +197 -0
- package/dist/journey/report-validation.d.ts +3 -0
- package/dist/journey/report-validation.js +457 -0
- package/dist/journey/runner.d.ts +12 -0
- package/dist/journey/runner.js +321 -0
- package/dist/journey/scope.d.ts +8 -0
- package/dist/journey/scope.js +50 -0
- package/dist/journey/transport.d.ts +3 -0
- package/dist/journey/transport.js +45 -0
- package/dist/journey/verify.d.ts +21 -0
- package/dist/journey/verify.js +213 -0
- package/dist/render/colors.d.ts +6 -7
- package/dist/render/colors.js +16 -25
- package/dist/render/discovery-guidance.d.ts +3 -0
- package/dist/render/discovery-guidance.js +66 -0
- package/dist/render/format-bridge.d.ts +1 -2
- package/dist/render/format-bridge.js +22 -17
- package/dist/render/format-diff.d.ts +2 -0
- package/dist/render/format-diff.js +169 -0
- package/dist/render/format-scan.d.ts +0 -1
- package/dist/render/format-scan.js +6 -4
- package/dist/render/format-verbose.d.ts +0 -1
- package/dist/render/format-verbose.js +7 -4
- package/dist/render/index.d.ts +0 -1
- package/dist/render/index.js +1 -2
- package/dist/render/oauth-observations.d.ts +4 -0
- package/dist/render/oauth-observations.js +27 -0
- package/dist/render/progress-bar.d.ts +1 -2
- package/dist/render/progress-bar.js +8 -6
- package/dist/render/robots-inventory.d.ts +3 -0
- package/dist/render/robots-inventory.js +38 -0
- package/dist/render/symbols.d.ts +4 -10
- package/dist/render/symbols.js +15 -17
- package/dist/render/terminal-text.d.ts +1 -3
- package/dist/render/terminal-text.js +1 -5
- package/dist/utils/analysis-worker-entry.d.ts +3 -0
- package/dist/utils/analysis-worker-entry.js +12 -0
- package/dist/utils/analysis-worker.d.ts +12 -0
- package/dist/utils/analysis-worker.js +54 -0
- package/dist/utils/html.d.ts +39 -0
- package/dist/utils/html.js +261 -0
- package/dist/utils/http-client.d.ts +24 -17
- package/dist/utils/http-client.js +166 -200
- package/dist/utils/http-types.d.ts +43 -0
- package/dist/utils/http-types.js +11 -0
- package/dist/utils/index.d.ts +0 -1
- package/dist/utils/index.js +0 -1
- package/dist/utils/package-registry.d.ts +11 -0
- package/dist/utils/package-registry.js +69 -0
- package/dist/utils/read-json-file.d.ts +3 -0
- package/dist/utils/read-json-file.js +41 -0
- package/dist/utils/record.d.ts +1 -0
- package/dist/utils/record.js +3 -0
- package/dist/utils/response-body.d.ts +7 -0
- package/dist/utils/response-body.js +71 -0
- package/dist/utils/ssrf.d.ts +11 -15
- package/dist/utils/ssrf.js +80 -44
- package/dist/utils/terminal-text.d.ts +2 -0
- package/dist/utils/terminal-text.js +4 -0
- package/dist/utils/url.d.ts +29 -1
- package/dist/utils/url.js +147 -17
- package/docs/examples/onboarding/account-claim.json +25 -0
- package/docs/examples/onboarding/pages/account-claim.html +3 -0
- package/docs/examples/onboarding/pages/production-activation.html +3 -0
- package/docs/examples/onboarding/pages/test-credential.html +3 -0
- package/docs/examples/onboarding/production-activation.json +25 -0
- package/docs/examples/onboarding/test-credential.json +25 -0
- package/docs/journey-qualification.md +46 -0
- package/docs/journeys.md +83 -0
- package/package.json +28 -8
- package/dist/bridges/context/agents-json.d.ts.map +0 -1
- package/dist/bridges/context/agents-json.js.map +0 -1
- package/dist/bridges/context/auth-clarity.d.ts.map +0 -1
- package/dist/bridges/context/auth-clarity.js.map +0 -1
- package/dist/bridges/context/auth-legibility.d.ts.map +0 -1
- package/dist/bridges/context/auth-legibility.js.map +0 -1
- package/dist/bridges/context/index.d.ts.map +0 -1
- package/dist/bridges/context/index.js.map +0 -1
- package/dist/bridges/context/rate-limits.d.ts.map +0 -1
- package/dist/bridges/context/rate-limits.js.map +0 -1
- package/dist/bridges/context/tos-contact.d.ts.map +0 -1
- package/dist/bridges/context/tos-contact.js.map +0 -1
- package/dist/bridges/context/versioning.d.ts.map +0 -1
- package/dist/bridges/context/versioning.js.map +0 -1
- package/dist/bridges/index.d.ts.map +0 -1
- package/dist/bridges/index.js.map +0 -1
- package/dist/bridges/reachability/crawler-policy.d.ts.map +0 -1
- package/dist/bridges/reachability/crawler-policy.js.map +0 -1
- package/dist/bridges/reachability/http-status.d.ts.map +0 -1
- package/dist/bridges/reachability/http-status.js.map +0 -1
- package/dist/bridges/reachability/https-check.d.ts.map +0 -1
- package/dist/bridges/reachability/https-check.js.map +0 -1
- package/dist/bridges/reachability/index.d.ts.map +0 -1
- package/dist/bridges/reachability/index.js.map +0 -1
- package/dist/bridges/reachability/meta-robots.d.ts.map +0 -1
- package/dist/bridges/reachability/meta-robots.js.map +0 -1
- package/dist/bridges/reachability/robots-parser.d.ts.map +0 -1
- package/dist/bridges/reachability/robots-parser.js.map +0 -1
- package/dist/bridges/reachability/robots-txt.d.ts.map +0 -1
- package/dist/bridges/reachability/robots-txt.js.map +0 -1
- package/dist/bridges/schema/descriptions.d.ts.map +0 -1
- package/dist/bridges/schema/descriptions.js.map +0 -1
- package/dist/bridges/schema/error-schemas.d.ts.map +0 -1
- package/dist/bridges/schema/error-schemas.js.map +0 -1
- package/dist/bridges/schema/index.d.ts.map +0 -1
- package/dist/bridges/schema/index.js.map +0 -1
- package/dist/bridges/schema/oas-types.d.ts.map +0 -1
- package/dist/bridges/schema/oas-types.js.map +0 -1
- package/dist/bridges/schema/operation-ids.d.ts.map +0 -1
- package/dist/bridges/schema/operation-ids.js.map +0 -1
- package/dist/bridges/schema/required-fields.d.ts.map +0 -1
- package/dist/bridges/schema/required-fields.js.map +0 -1
- package/dist/bridges/schema/schema-types.d.ts.map +0 -1
- package/dist/bridges/schema/schema-types.js.map +0 -1
- package/dist/bridges/separation/api-presence.d.ts.map +0 -1
- package/dist/bridges/separation/api-presence.js.map +0 -1
- package/dist/bridges/separation/developer-docs.d.ts.map +0 -1
- package/dist/bridges/separation/developer-docs.js.map +0 -1
- package/dist/bridges/separation/index.d.ts.map +0 -1
- package/dist/bridges/separation/index.js.map +0 -1
- package/dist/bridges/separation/sdk-references.d.ts.map +0 -1
- package/dist/bridges/separation/sdk-references.js.map +0 -1
- package/dist/bridges/separation/webhook-support.d.ts.map +0 -1
- package/dist/bridges/separation/webhook-support.js.map +0 -1
- package/dist/bridges/standards/a2a-agent-card.d.ts.map +0 -1
- package/dist/bridges/standards/a2a-agent-card.js.map +0 -1
- package/dist/bridges/standards/graphql.d.ts.map +0 -1
- package/dist/bridges/standards/graphql.js.map +0 -1
- package/dist/bridges/standards/index.d.ts.map +0 -1
- package/dist/bridges/standards/index.js.map +0 -1
- package/dist/bridges/standards/json-ld.d.ts.map +0 -1
- package/dist/bridges/standards/json-ld.js.map +0 -1
- package/dist/bridges/standards/llms-txt.d.ts.map +0 -1
- package/dist/bridges/standards/llms-txt.js.map +0 -1
- package/dist/bridges/standards/markdown-negotiation.d.ts.map +0 -1
- package/dist/bridges/standards/markdown-negotiation.js.map +0 -1
- package/dist/bridges/standards/mcp.d.ts.map +0 -1
- package/dist/bridges/standards/mcp.js.map +0 -1
- package/dist/bridges/standards/openapi.d.ts.map +0 -1
- package/dist/bridges/standards/openapi.js.map +0 -1
- package/dist/bridges/standards/schema-org.d.ts.map +0 -1
- package/dist/bridges/standards/schema-org.js.map +0 -1
- package/dist/bridges/standards/sitemap.d.ts.map +0 -1
- package/dist/bridges/standards/sitemap.js.map +0 -1
- package/dist/bridges/standards/well-known.d.ts.map +0 -1
- package/dist/bridges/standards/well-known.js.map +0 -1
- package/dist/cli/index.d.ts.map +0 -1
- package/dist/cli/index.js.map +0 -1
- package/dist/core/explanations.d.ts.map +0 -1
- package/dist/core/explanations.js.map +0 -1
- package/dist/core/index.d.ts.map +0 -1
- package/dist/core/index.js.map +0 -1
- package/dist/core/scan-options.d.ts.map +0 -1
- package/dist/core/scan-options.js.map +0 -1
- package/dist/core/scan.d.ts.map +0 -1
- package/dist/core/scan.js.map +0 -1
- package/dist/core/types.d.ts.map +0 -1
- package/dist/core/types.js.map +0 -1
- package/dist/core/version.d.ts.map +0 -1
- package/dist/core/version.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/render/colors.d.ts.map +0 -1
- package/dist/render/colors.js.map +0 -1
- package/dist/render/format-bridge.d.ts.map +0 -1
- package/dist/render/format-bridge.js.map +0 -1
- package/dist/render/format-scan.d.ts.map +0 -1
- package/dist/render/format-scan.js.map +0 -1
- package/dist/render/format-verbose.d.ts.map +0 -1
- package/dist/render/format-verbose.js.map +0 -1
- package/dist/render/index.d.ts.map +0 -1
- package/dist/render/index.js.map +0 -1
- package/dist/render/progress-bar.d.ts.map +0 -1
- package/dist/render/progress-bar.js.map +0 -1
- package/dist/render/symbols.d.ts.map +0 -1
- package/dist/render/symbols.js.map +0 -1
- package/dist/render/terminal-text.d.ts.map +0 -1
- package/dist/render/terminal-text.js.map +0 -1
- package/dist/utils/http-client.d.ts.map +0 -1
- package/dist/utils/http-client.js.map +0 -1
- package/dist/utils/index.d.ts.map +0 -1
- package/dist/utils/index.js.map +0 -1
- package/dist/utils/ssrf.d.ts.map +0 -1
- package/dist/utils/ssrf.js.map +0 -1
- package/dist/utils/url.d.ts.map +0 -1
- package/dist/utils/url.js.map +0 -1
|
@@ -1,40 +1,19 @@
|
|
|
1
1
|
import { validateDns } from "./ssrf.js";
|
|
2
|
-
import {
|
|
2
|
+
import { readBodyBytes, readBodyStream } from "./response-body.js";
|
|
3
|
+
import { MAX_URL_LENGTH, parseHttpUrl, resolveRedirectUrl } from "./url.js";
|
|
3
4
|
import { getVersion } from "../core/version.js";
|
|
4
5
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
5
6
|
import { Agent, fetch as undiciFetch } from "undici";
|
|
6
|
-
/**
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
*/
|
|
10
|
-
export function pinnedLookup(ip) {
|
|
11
|
-
const family = ip.includes(":") ? 6 : 4;
|
|
7
|
+
/** Return only addresses from the validated DNS answer, including family fallback. */
|
|
8
|
+
export function pinnedLookup(addresses) {
|
|
9
|
+
const records = addresses.map(address => ({ address, family: address.includes(":") ? 6 : 4 }));
|
|
12
10
|
return (_hostname, options, callback) => {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
if (options.all) {
|
|
18
|
-
callback(null, [{ address: ip, family }]);
|
|
19
|
-
}
|
|
20
|
-
else {
|
|
21
|
-
callback(null, ip, family);
|
|
22
|
-
}
|
|
11
|
+
if (options.all)
|
|
12
|
+
callback(null, records);
|
|
13
|
+
else
|
|
14
|
+
callback(null, records[0].address, records[0].family);
|
|
23
15
|
};
|
|
24
16
|
}
|
|
25
|
-
/**
|
|
26
|
-
* Build an undici dispatcher that connects ONLY to the pre-validated IP.
|
|
27
|
-
*
|
|
28
|
-
* This closes the DNS-rebinding TOCTOU: without it, `fetch` performs its own
|
|
29
|
-
* DNS resolution at connect time, so a low-TTL hostname could return a public
|
|
30
|
-
* IP to `validateDns` and a private one (127.0.0.1, 169.254.169.254, …) to the
|
|
31
|
-
* actual socket. Overriding `connect.lookup` pins the socket to the validated
|
|
32
|
-
* address while undici keeps the original hostname for the Host header and TLS
|
|
33
|
-
* SNI/certificate validation.
|
|
34
|
-
*/
|
|
35
|
-
function pinnedDispatcher(ip) {
|
|
36
|
-
return new Agent({ connect: { lookup: pinnedLookup(ip) } });
|
|
37
|
-
}
|
|
38
17
|
/** Scan-wide FIFO limiter. A slot represents one physical fetch attempt. */
|
|
39
18
|
export class RequestCoordinator {
|
|
40
19
|
maxConcurrency;
|
|
@@ -59,9 +38,22 @@ export class RequestCoordinator {
|
|
|
59
38
|
this.maxConcurrency = maxConcurrency;
|
|
60
39
|
this.maxRequests = maxRequests;
|
|
61
40
|
}
|
|
62
|
-
acquire() {
|
|
41
|
+
acquire(signal) {
|
|
63
42
|
return new Promise((resolve) => {
|
|
64
|
-
|
|
43
|
+
if (signal?.aborted)
|
|
44
|
+
return resolve(false);
|
|
45
|
+
const complete = (granted) => {
|
|
46
|
+
signal?.removeEventListener("abort", abort);
|
|
47
|
+
resolve(granted);
|
|
48
|
+
};
|
|
49
|
+
const abort = () => {
|
|
50
|
+
const at = this.waiting.indexOf(complete);
|
|
51
|
+
if (at >= 0)
|
|
52
|
+
this.waiting.splice(at, 1);
|
|
53
|
+
complete(false);
|
|
54
|
+
};
|
|
55
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
56
|
+
this.waiting.push(complete);
|
|
65
57
|
this.drain();
|
|
66
58
|
});
|
|
67
59
|
}
|
|
@@ -83,6 +75,10 @@ export class RequestCoordinator {
|
|
|
83
75
|
}
|
|
84
76
|
}
|
|
85
77
|
}
|
|
78
|
+
const bridgeTransport = new AsyncLocalStorage();
|
|
79
|
+
export function runWithBridgeTransport(transport, callback) {
|
|
80
|
+
return bridgeTransport.run(transport, callback);
|
|
81
|
+
}
|
|
86
82
|
const scanRequestContext = new AsyncLocalStorage();
|
|
87
83
|
/** Clamp a configured limit to a positive integer, or fall back to the default. */
|
|
88
84
|
function sanitizeLimit(value, fallback) {
|
|
@@ -91,17 +87,35 @@ function sanitizeLimit(value, fallback) {
|
|
|
91
87
|
: fallback;
|
|
92
88
|
}
|
|
93
89
|
export function runWithScanRequestContext(options, callback) {
|
|
90
|
+
const agents = new Map();
|
|
91
|
+
const lifetime = new AbortController();
|
|
92
|
+
const signal = options.signal ? AbortSignal.any([lifetime.signal, options.signal]) : lifetime.signal;
|
|
94
93
|
return scanRequestContext.run({
|
|
94
|
+
agents,
|
|
95
|
+
signal,
|
|
96
|
+
deadlineMs: options.deadlineMs,
|
|
97
|
+
registryLookup: options.registryLookup ?? true,
|
|
95
98
|
dnsCache: new Map(),
|
|
99
|
+
timeoutMs: sanitizeLimit(options.timeout, DEFAULT_OPTIONS.timeout),
|
|
96
100
|
coordinator: new RequestCoordinator(sanitizeLimit(options.maxConcurrency, 8), sanitizeLimit(options.maxRequests, 150)),
|
|
97
|
-
},
|
|
101
|
+
}, async () => {
|
|
102
|
+
try {
|
|
103
|
+
return await callback();
|
|
104
|
+
}
|
|
105
|
+
finally {
|
|
106
|
+
lifetime.abort();
|
|
107
|
+
await Promise.all(Array.from(agents.values(), agent => agent.destroy()));
|
|
108
|
+
}
|
|
109
|
+
});
|
|
98
110
|
}
|
|
99
111
|
/** Request stats for the current scan context, or null outside one. */
|
|
100
112
|
export function getScanRequestStats() {
|
|
101
113
|
const context = scanRequestContext.getStore();
|
|
102
114
|
if (!context)
|
|
103
115
|
return null;
|
|
104
|
-
|
|
116
|
+
const { timeoutMs, coordinator } = context;
|
|
117
|
+
return { timeoutMs, ...(context.deadlineMs && { deadlineMs: context.deadlineMs }), registryLookup: context.registryLookup, maxConcurrency: coordinator.maxConcurrency, maxRequests: coordinator.maxRequests,
|
|
118
|
+
started: coordinator.startedCount, denied: coordinator.deniedCount };
|
|
105
119
|
}
|
|
106
120
|
const DEFAULT_OPTIONS = {
|
|
107
121
|
method: "GET",
|
|
@@ -109,144 +123,44 @@ const DEFAULT_OPTIONS = {
|
|
|
109
123
|
maxRedirects: 5,
|
|
110
124
|
maxBodyBytes: 5 * 1024 * 1024,
|
|
111
125
|
};
|
|
112
|
-
const USER_AGENT = `milieu-cli/${getVersion()}`;
|
|
126
|
+
const USER_AGENT = `milieu-cli/${getVersion()} (+https://github.com/simplyarun/milieu-cli)`;
|
|
113
127
|
// ---------------------------------------------------------------------------
|
|
114
128
|
// Error classification
|
|
115
129
|
// ---------------------------------------------------------------------------
|
|
116
130
|
function classifyFetchError(error, url) {
|
|
131
|
+
if (error instanceof DOMException && error.name === "AbortError") {
|
|
132
|
+
return { ok: false, error: { kind: "cancelled", message: "Request cancelled", url } };
|
|
133
|
+
}
|
|
117
134
|
// AbortSignal.timeout produces DOMException with name "TimeoutError"
|
|
118
135
|
if (error instanceof DOMException && error.name === "TimeoutError") {
|
|
119
136
|
return { ok: false, error: { kind: "timeout", message: "Request timed out", url } };
|
|
120
137
|
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
case "ECONNREFUSED":
|
|
127
|
-
return { ok: false, error: { kind: "connection_refused", message: "Connection refused", url } };
|
|
128
|
-
case "CERT_HAS_EXPIRED":
|
|
129
|
-
case "DEPTH_ZERO_SELF_SIGNED_CERT":
|
|
130
|
-
case "UNABLE_TO_VERIFY_LEAF_SIGNATURE":
|
|
131
|
-
case "ERR_TLS_CERT_ALTNAME_INVALID":
|
|
132
|
-
return { ok: false, error: { kind: "ssl_error", message: `SSL error: ${cause.code}`, url } };
|
|
133
|
-
default:
|
|
134
|
-
break;
|
|
138
|
+
const cause = error instanceof Error && error.cause ? error.cause : error;
|
|
139
|
+
const code = typeof cause === "object" && cause !== null && "code" in cause ? cause.code : undefined;
|
|
140
|
+
if (typeof code === "string") {
|
|
141
|
+
if (code === "ENOTFOUND" || code === "EAI_AGAIN") {
|
|
142
|
+
return { ok: false, error: { kind: "dns", message: `DNS resolution failed (${code})`, url } };
|
|
135
143
|
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
// Streaming body reader
|
|
141
|
-
// ---------------------------------------------------------------------------
|
|
142
|
-
/**
|
|
143
|
-
* Read response body as a string, stopping early when maxBytes is reached
|
|
144
|
-
* or timeoutMs elapses.
|
|
145
|
-
*
|
|
146
|
-
* Uses the response body ReadableStream for chunk-by-chunk reading,
|
|
147
|
-
* cancelling the stream once the byte limit is exceeded. This prevents
|
|
148
|
-
* downloading multi-megabyte responses when only the first portion is needed.
|
|
149
|
-
*
|
|
150
|
-
* The timeoutMs parameter provides a hard deadline for body reading,
|
|
151
|
-
* independent of the fetch AbortSignal (which may not propagate to
|
|
152
|
-
* body stream reads in all Node.js versions). When the timeout fires,
|
|
153
|
-
* the reader is cancelled and whatever data was collected is returned.
|
|
154
|
-
*/
|
|
155
|
-
async function readBodyStream(response, maxBytes, timeoutMs) {
|
|
156
|
-
const stream = response.body;
|
|
157
|
-
if (!stream)
|
|
158
|
-
return "";
|
|
159
|
-
const reader = stream.getReader();
|
|
160
|
-
const chunks = [];
|
|
161
|
-
let totalBytes = 0;
|
|
162
|
-
// Hard timeout: cancel the reader directly if body reading stalls
|
|
163
|
-
const timeoutId = timeoutMs
|
|
164
|
-
? setTimeout(() => { reader.cancel().catch(() => { }); }, timeoutMs)
|
|
165
|
-
: undefined;
|
|
166
|
-
try {
|
|
167
|
-
while (true) {
|
|
168
|
-
const { done, value } = await reader.read();
|
|
169
|
-
if (done)
|
|
170
|
-
break;
|
|
171
|
-
totalBytes += value.length;
|
|
172
|
-
if (totalBytes > maxBytes) {
|
|
173
|
-
// Keep only what fits within the limit
|
|
174
|
-
const excess = totalBytes - maxBytes;
|
|
175
|
-
chunks.push(value.subarray(0, value.length - excess));
|
|
176
|
-
break;
|
|
177
|
-
}
|
|
178
|
-
chunks.push(value);
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
catch {
|
|
182
|
-
// reader.read() throws when stream is cancelled (timeout or network error).
|
|
183
|
-
// Return whatever data was collected so far.
|
|
184
|
-
}
|
|
185
|
-
finally {
|
|
186
|
-
if (timeoutId)
|
|
187
|
-
clearTimeout(timeoutId);
|
|
188
|
-
try {
|
|
189
|
-
await reader.cancel();
|
|
144
|
+
if (code === "ECONNREFUSED")
|
|
145
|
+
return { ok: false, error: { kind: "connection_refused", message: "Connection refused", url } };
|
|
146
|
+
if (/^(ETIMEDOUT|UND_ERR_(CONNECT|HEADERS|BODY)_TIMEOUT)$/.test(code)) {
|
|
147
|
+
return { ok: false, error: { kind: "timeout", message: `Request timed out (${code})`, url } };
|
|
190
148
|
}
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
const decoder = new TextDecoder();
|
|
194
|
-
let body = "";
|
|
195
|
-
for (const chunk of chunks) {
|
|
196
|
-
body += decoder.decode(chunk, { stream: true });
|
|
197
|
-
}
|
|
198
|
-
body += decoder.decode(); // flush remaining multi-byte chars
|
|
199
|
-
return body;
|
|
200
|
-
}
|
|
201
|
-
async function readBodyBytes(response, maxBytes, timeoutMs) {
|
|
202
|
-
if (!response.body)
|
|
203
|
-
return new Uint8Array();
|
|
204
|
-
const reader = response.body.getReader();
|
|
205
|
-
const chunks = [];
|
|
206
|
-
let totalBytes = 0;
|
|
207
|
-
let timedOut = false;
|
|
208
|
-
const timeoutId = setTimeout(() => {
|
|
209
|
-
timedOut = true;
|
|
210
|
-
void reader.cancel().catch(() => { });
|
|
211
|
-
}, timeoutMs);
|
|
212
|
-
try {
|
|
213
|
-
while (true) {
|
|
214
|
-
const { done, value } = await reader.read();
|
|
215
|
-
if (timedOut)
|
|
216
|
-
throw new DOMException("Body read timed out", "TimeoutError");
|
|
217
|
-
if (done)
|
|
218
|
-
break;
|
|
219
|
-
totalBytes += value.length;
|
|
220
|
-
if (totalBytes > maxBytes)
|
|
221
|
-
return null;
|
|
222
|
-
chunks.push(value);
|
|
149
|
+
if (/^(ECONNRESET|ECONNABORTED|EHOSTUNREACH|EHOSTDOWN|ENETUNREACH|ENETDOWN|EADDRNOTAVAIL|EPIPE|UND_ERR_SOCKET)$/.test(code)) {
|
|
150
|
+
return { ok: false, error: { kind: "network_error", message: `Connection failed (${code})`, url } };
|
|
223
151
|
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
clearTimeout(timeoutId);
|
|
227
|
-
try {
|
|
228
|
-
await reader.cancel();
|
|
152
|
+
if (/^(CERT_|UNABLE_TO_|ERR_TLS_|ERR_SSL_)|SELF_SIGNED_CERT|^EPROTO$/.test(code)) {
|
|
153
|
+
return { ok: false, error: { kind: "ssl_error", message: `TLS error: ${code}`, url } };
|
|
229
154
|
}
|
|
230
|
-
|
|
155
|
+
return { ok: false, error: { kind: "unknown", message: `${String(error)} (${code})`, url } };
|
|
231
156
|
}
|
|
232
|
-
return
|
|
157
|
+
return { ok: false, error: { kind: "unknown", message: String(error), url } };
|
|
233
158
|
}
|
|
234
159
|
// ---------------------------------------------------------------------------
|
|
235
160
|
// Bot protection detection
|
|
236
161
|
// ---------------------------------------------------------------------------
|
|
237
162
|
function isBotProtected(status, headers) {
|
|
238
|
-
|
|
239
|
-
// Cloudflare 403 with server header or cf-ray
|
|
240
|
-
if (status === 403 && (server.includes("cloudflare") || headers["cf-ray"] !== undefined)) {
|
|
241
|
-
return true;
|
|
242
|
-
}
|
|
243
|
-
// 429 rate limit from any server
|
|
244
|
-
if (status === 429)
|
|
245
|
-
return true;
|
|
246
|
-
// Cloudflare 503 challenge
|
|
247
|
-
if (status === 503 && server.includes("cloudflare"))
|
|
248
|
-
return true;
|
|
249
|
-
return false;
|
|
163
|
+
return status === 429 || headers["cf-mitigated"]?.toLowerCase() === "challenge";
|
|
250
164
|
}
|
|
251
165
|
// ---------------------------------------------------------------------------
|
|
252
166
|
// Convert Response headers to plain object
|
|
@@ -272,26 +186,38 @@ async function fetchOnce(url, options, binary = false) {
|
|
|
272
186
|
for (let hop = 0; hop <= options.maxRedirects; hop++) {
|
|
273
187
|
// SSRF pre-flight at every hop
|
|
274
188
|
const hostname = new URL(currentUrl).hostname;
|
|
275
|
-
const ssrfResult = await validateDns(hostname, options.dnsCache
|
|
189
|
+
const ssrfResult = await validateDns(hostname, options.dnsCache, {
|
|
190
|
+
signal: options.signal, publicOnly: options.publicProductRead,
|
|
191
|
+
});
|
|
192
|
+
if (options.signal?.aborted)
|
|
193
|
+
return classifyFetchError(options.signal.reason, currentUrl);
|
|
276
194
|
if (!ssrfResult.safe) {
|
|
277
|
-
|
|
278
|
-
const kind = ssrfResult.error.startsWith("DNS resolution failed")
|
|
279
|
-
? "dns"
|
|
280
|
-
: "ssrf_blocked";
|
|
281
|
-
return { ok: false, error: { kind, message: ssrfResult.error, url: currentUrl } };
|
|
195
|
+
return { ok: false, error: { kind: ssrfResult.reason, message: ssrfResult.error, url: currentUrl } };
|
|
282
196
|
}
|
|
283
|
-
const granted = await options.coordinator.acquire();
|
|
197
|
+
const granted = await options.coordinator.acquire(options.signal);
|
|
284
198
|
if (!granted) {
|
|
199
|
+
if (options.signal?.aborted)
|
|
200
|
+
return classifyFetchError(options.signal.reason, currentUrl);
|
|
285
201
|
return { ok: false, error: { kind: "request_budget_exhausted", message: "Scan request budget exhausted", url: currentUrl } };
|
|
286
202
|
}
|
|
287
|
-
|
|
288
|
-
const
|
|
203
|
+
const pool = scanRequestContext.getStore()?.agents;
|
|
204
|
+
const poolKey = JSON.stringify([new URL(currentUrl).origin, ssrfResult.addresses, options.timeout]);
|
|
205
|
+
let dispatcher = pool?.get(poolKey);
|
|
206
|
+
let response;
|
|
289
207
|
try {
|
|
208
|
+
if (options.signal?.aborted)
|
|
209
|
+
return classifyFetchError(options.signal.reason, currentUrl);
|
|
210
|
+
if (!dispatcher) {
|
|
211
|
+
dispatcher = new Agent({ connect: { lookup: pinnedLookup(ssrfResult.addresses), timeout: options.timeout, autoSelectFamily: true } });
|
|
212
|
+
pool?.set(poolKey, dispatcher);
|
|
213
|
+
}
|
|
214
|
+
const hopTimeout = AbortSignal.timeout(options.timeout);
|
|
215
|
+
const signal = options.signal ? AbortSignal.any([options.signal, hopTimeout]) : hopTimeout;
|
|
290
216
|
const init = {
|
|
291
217
|
method: options.method,
|
|
292
218
|
// Manual redirects: required for SSRF re-validation at each hop and redirect chain tracking
|
|
293
219
|
redirect: "manual",
|
|
294
|
-
signal
|
|
220
|
+
signal,
|
|
295
221
|
headers,
|
|
296
222
|
dispatcher,
|
|
297
223
|
// POST body (only sent on first request, not on redirects)
|
|
@@ -300,7 +226,17 @@ async function fetchOnce(url, options, binary = false) {
|
|
|
300
226
|
: {}),
|
|
301
227
|
};
|
|
302
228
|
// The fetch implementation and dispatcher must share Undici's handler contract.
|
|
303
|
-
|
|
229
|
+
response = await undiciFetch(currentUrl, init);
|
|
230
|
+
// A Journey needs only the observed status of a rejection. Never
|
|
231
|
+
// follow its Location or consume its error body. Generic CDN headers
|
|
232
|
+
// alone do not establish that a challenge blocked the operation.
|
|
233
|
+
if (options.publicProductRead) {
|
|
234
|
+
const blocked = response.status === 429 || response.headers.get("cf-mitigated")?.toLowerCase() === "challenge";
|
|
235
|
+
if (blocked || response.status < 200 || response.status >= 300) {
|
|
236
|
+
return { ok: false, error: { kind: blocked ? "bot_protected" : "http_error",
|
|
237
|
+
message: `HTTP ${response.status}`, statusCode: response.status, url: currentUrl } };
|
|
238
|
+
}
|
|
239
|
+
}
|
|
304
240
|
// Only follow redirect statuses, and only for GET and HEAD requests.
|
|
305
241
|
if (response.status >= 300 && response.status < 400) {
|
|
306
242
|
const location = response.headers.get("location");
|
|
@@ -311,16 +247,21 @@ async function fetchOnce(url, options, binary = false) {
|
|
|
311
247
|
};
|
|
312
248
|
}
|
|
313
249
|
const resolved = resolveRedirectUrl(location, currentUrl);
|
|
250
|
+
// A direct-only read was answered by this redirect. Where the scan declines to follow a redirect it would
|
|
251
|
+
// otherwise follow, past a chain's limit or to a target too long to request, the answer is unknown.
|
|
252
|
+
const answered = options.maxRedirects === 0 && { statusCode: response.status };
|
|
314
253
|
if (!resolved.ok) {
|
|
315
254
|
return {
|
|
316
255
|
ok: false,
|
|
317
|
-
error:
|
|
256
|
+
error: resolved.tooLong
|
|
257
|
+
? { kind: "http_error", message: `Redirect URL too long (max ${MAX_URL_LENGTH})`, ...answered, url: currentUrl }
|
|
258
|
+
: { kind: "http_error", message: `Invalid redirect: ${resolved.error}`, statusCode: response.status, url: currentUrl },
|
|
318
259
|
};
|
|
319
260
|
}
|
|
320
261
|
if (redirects.length >= options.maxRedirects) {
|
|
321
262
|
return {
|
|
322
263
|
ok: false,
|
|
323
|
-
error: { kind: "http_error", message: `Too many redirects (max ${options.maxRedirects})`, url: currentUrl },
|
|
264
|
+
error: { kind: "http_error", message: `Too many redirects (max ${options.maxRedirects})`, ...answered, url: currentUrl },
|
|
324
265
|
};
|
|
325
266
|
}
|
|
326
267
|
if (new URL(resolved.url).origin !== new URL(currentUrl).origin) {
|
|
@@ -332,27 +273,18 @@ async function fetchOnce(url, options, binary = false) {
|
|
|
332
273
|
}
|
|
333
274
|
// Convert headers
|
|
334
275
|
const headerRecord = headersToRecord(response.headers);
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
headers: headerRecord,
|
|
340
|
-
body: options.method === "HEAD" ? "" : await readBodyStream(response, options.maxBodyBytes, options.timeout),
|
|
341
|
-
});
|
|
342
|
-
// Bot protection detection
|
|
343
|
-
if (isBotProtected(response.status, headerRecord)) {
|
|
344
|
-
return {
|
|
345
|
-
ok: false,
|
|
346
|
-
error: { kind: "bot_protected", message: "Bot protection detected", statusCode: response.status, url: currentUrl },
|
|
347
|
-
response: await captureResponse(),
|
|
348
|
-
};
|
|
349
|
-
}
|
|
350
|
-
// 4xx/5xx errors (non-bot)
|
|
351
|
-
if (response.status >= 400) {
|
|
276
|
+
const blocked = isBotProtected(response.status, headerRecord);
|
|
277
|
+
if (blocked || response.status >= 400) {
|
|
278
|
+
const limit = options.captureFullErrorBody ? options.maxBodyBytes : Math.min(options.maxBodyBytes, 64 * 1024);
|
|
279
|
+
const captured = options.method === "HEAD" ? { body: "" } : await readBodyStream(response, limit, signal);
|
|
352
280
|
return {
|
|
353
281
|
ok: false,
|
|
354
|
-
error: {
|
|
355
|
-
|
|
282
|
+
error: {
|
|
283
|
+
kind: blocked ? "bot_protected" : "http_error",
|
|
284
|
+
message: blocked ? "Bot protection detected" : `HTTP ${response.status} ${response.statusText}`,
|
|
285
|
+
statusCode: response.status, url: currentUrl,
|
|
286
|
+
},
|
|
287
|
+
response: { status: response.status, headers: headerRecord, ...captured },
|
|
356
288
|
};
|
|
357
289
|
}
|
|
358
290
|
// Success (2xx) -- read body
|
|
@@ -372,27 +304,39 @@ async function fetchOnce(url, options, binary = false) {
|
|
|
372
304
|
if (contentLength && parseInt(contentLength, 10) > options.maxBodyBytes) {
|
|
373
305
|
return {
|
|
374
306
|
ok: false,
|
|
375
|
-
error: { kind: "body_too_large", message: `Response body exceeds ${options.maxBodyBytes} bytes`, url: currentUrl
|
|
307
|
+
error: { kind: "body_too_large", message: `Response body exceeds ${options.maxBodyBytes} bytes`, url: currentUrl,
|
|
308
|
+
statusCode: response.status },
|
|
376
309
|
};
|
|
377
310
|
}
|
|
378
311
|
// Stream body with early cancellation at maxBodyBytes and hard timeout
|
|
379
312
|
if (binary) {
|
|
380
|
-
|
|
313
|
+
let bytes;
|
|
314
|
+
try {
|
|
315
|
+
bytes = await readBodyBytes(response, options.maxBodyBytes, signal);
|
|
316
|
+
}
|
|
317
|
+
catch (error) {
|
|
318
|
+
if (!options.publicProductRead)
|
|
319
|
+
throw error;
|
|
320
|
+
return { ok: false, error: { kind: signal.aborted ? "timeout" : "unknown",
|
|
321
|
+
statusCode: response.status, message: "Body read interrupted", url: currentUrl } };
|
|
322
|
+
}
|
|
381
323
|
if (bytes === null) {
|
|
382
|
-
return { ok: false, error: { kind: "body_too_large", message: `Response body exceeds ${options.maxBodyBytes} bytes`, url: currentUrl
|
|
324
|
+
return { ok: false, error: { kind: "body_too_large", message: `Response body exceeds ${options.maxBodyBytes} bytes`, url: currentUrl,
|
|
325
|
+
statusCode: response.status } };
|
|
383
326
|
}
|
|
384
327
|
return { ...base, body: bytes };
|
|
385
328
|
}
|
|
386
|
-
return { ...base,
|
|
329
|
+
return { ...base, ...await readBodyStream(response, options.maxBodyBytes, signal) };
|
|
387
330
|
}
|
|
388
331
|
catch (err) {
|
|
389
332
|
return classifyFetchError(err, currentUrl);
|
|
390
333
|
}
|
|
391
334
|
finally {
|
|
392
|
-
// Redirects and size checks can exit with a streaming response still in
|
|
393
|
-
// flight. Tear down this per-hop dispatcher before granting another slot.
|
|
394
335
|
try {
|
|
395
|
-
|
|
336
|
+
if (response?.body && !response.body.locked)
|
|
337
|
+
await response.body.cancel().catch(() => { });
|
|
338
|
+
if (!pool)
|
|
339
|
+
await dispatcher?.destroy();
|
|
396
340
|
}
|
|
397
341
|
finally {
|
|
398
342
|
options.coordinator.release();
|
|
@@ -413,7 +357,7 @@ function isRetriable(result) {
|
|
|
413
357
|
return false;
|
|
414
358
|
const { kind } = result.error;
|
|
415
359
|
// Retry on timeout or connection_refused
|
|
416
|
-
if (kind === "timeout" || kind === "connection_refused")
|
|
360
|
+
if (kind === "timeout" || kind === "connection_refused" || kind === "network_error")
|
|
417
361
|
return true;
|
|
418
362
|
// Retry on 5xx server errors
|
|
419
363
|
if (kind === "http_error" && result.error.statusCode !== undefined && result.error.statusCode >= 500) {
|
|
@@ -423,7 +367,7 @@ function isRetriable(result) {
|
|
|
423
367
|
}
|
|
424
368
|
async function fetchWithRetry(url, options, binary = false) {
|
|
425
369
|
const result = await fetchOnce(url, options, binary);
|
|
426
|
-
if (isRetriable(result)) {
|
|
370
|
+
if (options.retry !== false && isRetriable(result)) {
|
|
427
371
|
// Skip the retry when the budget cannot grant it — and never let a
|
|
428
372
|
// denied retry mask the genuine first-attempt failure, which may drive
|
|
429
373
|
// scan-abort decisions (e.g. connection_refused).
|
|
@@ -431,7 +375,19 @@ async function fetchWithRetry(url, options, binary = false) {
|
|
|
431
375
|
return result;
|
|
432
376
|
}
|
|
433
377
|
// Wait 2 seconds before retry
|
|
434
|
-
|
|
378
|
+
if (options.signal?.aborted)
|
|
379
|
+
return result;
|
|
380
|
+
await new Promise(resolve => {
|
|
381
|
+
const done = () => {
|
|
382
|
+
clearTimeout(timer);
|
|
383
|
+
options.signal?.removeEventListener("abort", done);
|
|
384
|
+
resolve();
|
|
385
|
+
};
|
|
386
|
+
const timer = setTimeout(done, 2000);
|
|
387
|
+
options.signal?.addEventListener("abort", done, { once: true });
|
|
388
|
+
});
|
|
389
|
+
if (options.signal?.aborted)
|
|
390
|
+
return classifyFetchError(options.signal.reason, url);
|
|
435
391
|
const retry = await fetchOnce(url, options, binary);
|
|
436
392
|
if (!retry.ok && retry.error.kind === "request_budget_exhausted") {
|
|
437
393
|
return result;
|
|
@@ -453,11 +409,14 @@ async function fetchWithRetry(url, options, binary = false) {
|
|
|
453
409
|
* NEVER throws -- all errors are returned as HttpFailure values.
|
|
454
410
|
*/
|
|
455
411
|
export async function httpGet(url, options) {
|
|
412
|
+
const remote = bridgeTransport.getStore();
|
|
413
|
+
if (remote)
|
|
414
|
+
return remote.get(url, options);
|
|
415
|
+
const context = scanRequestContext.getStore();
|
|
456
416
|
const method = options?.method ?? DEFAULT_OPTIONS.method;
|
|
457
|
-
const timeout = options?.timeout ?? DEFAULT_OPTIONS.timeout;
|
|
417
|
+
const timeout = options?.timeout ?? context?.timeoutMs ?? DEFAULT_OPTIONS.timeout;
|
|
458
418
|
const maxRedirects = options?.maxRedirects ?? DEFAULT_OPTIONS.maxRedirects;
|
|
459
419
|
const maxBodyBytes = options?.maxBodyBytes ?? DEFAULT_OPTIONS.maxBodyBytes;
|
|
460
|
-
const context = scanRequestContext.getStore();
|
|
461
420
|
const dnsCache = options?.dnsCache ?? context?.dnsCache ?? new Map();
|
|
462
421
|
const coordinator = options?.coordinator ?? context?.coordinator ?? new RequestCoordinator(Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
|
|
463
422
|
const headers = {
|
|
@@ -474,9 +433,12 @@ export async function httpGet(url, options) {
|
|
|
474
433
|
timeout,
|
|
475
434
|
maxRedirects,
|
|
476
435
|
maxBodyBytes,
|
|
436
|
+
captureFullErrorBody: options?.captureFullErrorBody,
|
|
477
437
|
dnsCache,
|
|
478
438
|
headers,
|
|
479
439
|
body: options?.body,
|
|
440
|
+
retry: options?.retry ?? method !== "POST",
|
|
441
|
+
signal: options?.signal && context?.signal ? AbortSignal.any([options.signal, context.signal]) : options?.signal ?? context?.signal,
|
|
480
442
|
coordinator,
|
|
481
443
|
});
|
|
482
444
|
const durationMs = Math.round(performance.now() - start);
|
|
@@ -488,8 +450,11 @@ export async function httpGet(url, options) {
|
|
|
488
450
|
}
|
|
489
451
|
/** Binary counterpart for internal consumers such as compressed sitemaps. */
|
|
490
452
|
export async function httpGetBytes(url, options) {
|
|
453
|
+
const remote = bridgeTransport.getStore();
|
|
454
|
+
if (remote)
|
|
455
|
+
return remote.bytes(url, options);
|
|
491
456
|
const context = scanRequestContext.getStore();
|
|
492
|
-
const timeout = options?.timeout ?? DEFAULT_OPTIONS.timeout;
|
|
457
|
+
const timeout = options?.timeout ?? context?.timeoutMs ?? DEFAULT_OPTIONS.timeout;
|
|
493
458
|
const maxRedirects = options?.maxRedirects ?? DEFAULT_OPTIONS.maxRedirects;
|
|
494
459
|
const maxBodyBytes = options?.maxBodyBytes ?? DEFAULT_OPTIONS.maxBodyBytes;
|
|
495
460
|
const dnsCache = options?.dnsCache ?? context?.dnsCache ?? new Map();
|
|
@@ -498,9 +463,10 @@ export async function httpGetBytes(url, options) {
|
|
|
498
463
|
const start = performance.now();
|
|
499
464
|
const result = await fetchWithRetry(url, {
|
|
500
465
|
method: "GET", timeout, maxRedirects, maxBodyBytes, dnsCache, coordinator, headers,
|
|
466
|
+
retry: options?.retry, publicProductRead: options?.publicProductRead,
|
|
467
|
+
signal: options?.signal && context?.signal ? AbortSignal.any([options.signal, context.signal]) : options?.signal ?? context?.signal,
|
|
501
468
|
}, true);
|
|
502
469
|
if (result.ok)
|
|
503
470
|
return { ...result, durationMs: Math.round(performance.now() - start) };
|
|
504
471
|
return result;
|
|
505
472
|
}
|
|
506
|
-
//# sourceMappingURL=http-client.js.map
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/** Transport and HTTP failure classifications */
|
|
2
|
+
export type HttpErrorKind = "dns" | "cancelled" | "timeout" | "ssrf_blocked" | "http_error" | "bot_protected" | "connection_refused" | "network_error" | "ssl_error" | "body_too_large" | "request_budget_exhausted" | "unknown";
|
|
3
|
+
/** HTTP error with discriminated kind field */
|
|
4
|
+
export interface HttpError {
|
|
5
|
+
kind: HttpErrorKind;
|
|
6
|
+
message: string;
|
|
7
|
+
statusCode?: number;
|
|
8
|
+
url: string;
|
|
9
|
+
}
|
|
10
|
+
/** HTTP success response */
|
|
11
|
+
export interface HttpSuccess {
|
|
12
|
+
/** The text body was truncated or interrupted before its end. */
|
|
13
|
+
bodyIncomplete?: true;
|
|
14
|
+
ok: true;
|
|
15
|
+
url: string;
|
|
16
|
+
status: number;
|
|
17
|
+
headers: Record<string, string>;
|
|
18
|
+
body: string;
|
|
19
|
+
redirects: string[];
|
|
20
|
+
durationMs: number;
|
|
21
|
+
}
|
|
22
|
+
/** HTTP failure response */
|
|
23
|
+
export interface HttpFailure {
|
|
24
|
+
ok: false;
|
|
25
|
+
error: HttpError;
|
|
26
|
+
/**
|
|
27
|
+
* The HTTP response, present only when the failure IS one — a 4xx/5xx
|
|
28
|
+
* (`http_error`) or bot-protection (`bot_protected`). Absent for
|
|
29
|
+
* network-level failures (dns, timeout, ssrf_blocked, connection_refused,
|
|
30
|
+
* ssl_error, body_too_large, request_budget_exhausted).
|
|
31
|
+
*
|
|
32
|
+
* Checks that grade a *rejection* rather than an absence — auth legibility
|
|
33
|
+
* (401/403 quality) and rate limits (headers on any response) — read this
|
|
34
|
+
* instead of treating every non-2xx as "unreachable".
|
|
35
|
+
*/
|
|
36
|
+
response?: Pick<HttpSuccess, "status" | "headers" | "body" | "bodyIncomplete">;
|
|
37
|
+
}
|
|
38
|
+
/** Discriminated union for HTTP responses */
|
|
39
|
+
export type HttpResponse = HttpSuccess | HttpFailure;
|
|
40
|
+
/** Lowercase media type without optional parameters. Header names from the client are lowercase. */
|
|
41
|
+
export declare function mediaType(headers?: Readonly<Record<string, string>>): string;
|
|
42
|
+
export declare function isHtmlMediaType(type: string): boolean;
|
|
43
|
+
export declare function isMarkdownMediaType(type: string): boolean;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// === HTTP Error Types ===
|
|
2
|
+
/** Lowercase media type without optional parameters. Header names from the client are lowercase. */
|
|
3
|
+
export function mediaType(headers) {
|
|
4
|
+
return headers?.["content-type"]?.split(";", 1)[0].trim().toLowerCase() ?? "";
|
|
5
|
+
}
|
|
6
|
+
export function isHtmlMediaType(type) {
|
|
7
|
+
return type === "text/html" || type === "application/xhtml+xml";
|
|
8
|
+
}
|
|
9
|
+
export function isMarkdownMediaType(type) {
|
|
10
|
+
return type === "text/markdown" || type === "text/x-markdown";
|
|
11
|
+
}
|
package/dist/utils/index.d.ts
CHANGED
package/dist/utils/index.js
CHANGED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** Registry detection pattern with URL and optional install command regex */
|
|
2
|
+
interface RegistryPattern {
|
|
3
|
+
name: string;
|
|
4
|
+
urlPattern: RegExp;
|
|
5
|
+
installPattern?: RegExp;
|
|
6
|
+
}
|
|
7
|
+
/** Package registry patterns for SDK/package reference detection */
|
|
8
|
+
export declare const REGISTRY_PATTERNS: RegistryPattern[];
|
|
9
|
+
/** A target path on the actual registry host. Ordinary mentions keep their weaker, historical meaning. */
|
|
10
|
+
export declare function linkedRegistry(url: URL): string | undefined;
|
|
11
|
+
export {};
|