@zeldrisho/pi-web-fetch 0.16.3 → 0.16.4
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/CHANGELOG.md +10 -1
- package/package.json +1 -1
- package/src/acquisition/admission.ts +61 -0
- package/src/acquisition/fetch.ts +12 -6
- package/src/challenges/anubis.ts +1 -1
- package/src/network/network-redirects.ts +15 -9
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [0.16.4] - 2026-11-09
|
|
11
|
+
|
|
12
|
+
### Fixed
|
|
13
|
+
|
|
14
|
+
- Close unused redirect and error responses promptly, including stalled responses.
|
|
15
|
+
- Avoid retrying before a server's longer requested delay.
|
|
16
|
+
- Bound concurrent page acquisitions, including slow downloads, and reject requests when the waiting queue is full.
|
|
17
|
+
|
|
10
18
|
## [0.16.3] - 2026-11-01
|
|
11
19
|
|
|
12
20
|
### Fixed
|
|
@@ -316,7 +324,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
316
324
|
|
|
317
325
|
- Add bounded public page fetching ([b979496](https://github.com/zeldrisho/pi-packages/commit/b979496b32de1cead172ba570307e4a4a7b3421d))
|
|
318
326
|
|
|
319
|
-
[Unreleased]: https://github.com/zeldrisho/pi-packages/compare/pi-web-fetch-v0.16.
|
|
327
|
+
[Unreleased]: https://github.com/zeldrisho/pi-packages/compare/pi-web-fetch-v0.16.4...HEAD
|
|
328
|
+
[0.16.4]: https://github.com/zeldrisho/pi-packages/compare/pi-web-fetch-v0.16.3...pi-web-fetch-v0.16.4
|
|
320
329
|
[0.16.3]: https://github.com/zeldrisho/pi-packages/compare/pi-web-fetch-v0.16.2...pi-web-fetch-v0.16.3
|
|
321
330
|
[0.16.2]: https://github.com/zeldrisho/pi-packages/compare/pi-web-fetch-v0.16.1...pi-web-fetch-v0.16.2
|
|
322
331
|
[0.16.1]: https://github.com/zeldrisho/pi-packages/compare/pi-web-fetch-v0.16.0...pi-web-fetch-v0.16.1
|
package/package.json
CHANGED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/** Limits complete acquisitions, including slow response bodies and extraction. */
|
|
2
|
+
export class AcquisitionAdmission {
|
|
3
|
+
#active = 0;
|
|
4
|
+
readonly #waiting: Array<() => void> = [];
|
|
5
|
+
|
|
6
|
+
constructor(
|
|
7
|
+
readonly maxActive: number,
|
|
8
|
+
readonly maxWaiting: number,
|
|
9
|
+
) {}
|
|
10
|
+
|
|
11
|
+
async acquire(signal: AbortSignal): Promise<() => void> {
|
|
12
|
+
signal.throwIfAborted();
|
|
13
|
+
|
|
14
|
+
if (this.#active >= this.maxActive) {
|
|
15
|
+
if (this.#waiting.length >= this.maxWaiting) {
|
|
16
|
+
throw new Error("web_fetch is busy; too many requests are waiting.");
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
await new Promise<void>((resolve, reject) => {
|
|
20
|
+
const enter = () => {
|
|
21
|
+
signal.removeEventListener("abort", cancel);
|
|
22
|
+
this.#active += 1;
|
|
23
|
+
resolve();
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const cancel = () => {
|
|
27
|
+
const index = this.#waiting.indexOf(enter);
|
|
28
|
+
|
|
29
|
+
if (index >= 0) this.#waiting.splice(index, 1);
|
|
30
|
+
reject(signal.reason);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
this.#waiting.push(enter);
|
|
34
|
+
signal.addEventListener("abort", cancel, { once: true });
|
|
35
|
+
|
|
36
|
+
if (signal.aborted) cancel();
|
|
37
|
+
});
|
|
38
|
+
} else {
|
|
39
|
+
this.#active += 1;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
let released = false;
|
|
43
|
+
|
|
44
|
+
const release = () => {
|
|
45
|
+
if (released) return;
|
|
46
|
+
released = true;
|
|
47
|
+
this.#active -= 1;
|
|
48
|
+
this.#waiting.shift()?.();
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
if (signal.aborted) {
|
|
52
|
+
release();
|
|
53
|
+
signal.throwIfAborted();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return release;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Bound worst-case concurrent downloads without serializing normal subagent use. */
|
|
61
|
+
export const acquisitionAdmission = new AcquisitionAdmission(16, 128);
|
package/src/acquisition/fetch.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { IncomingMessage } from "node:http";
|
|
2
|
+
import { acquisitionAdmission } from "./admission.ts";
|
|
2
3
|
import {
|
|
3
4
|
lineFragmentOffsets,
|
|
4
5
|
markdownFragmentOffsets,
|
|
@@ -202,7 +203,7 @@ function assertSuccessfulResponse(response: IncomingMessage): void {
|
|
|
202
203
|
const status = response.statusCode ?? 0;
|
|
203
204
|
|
|
204
205
|
if (isAwsWafChallengeResponse(response)) {
|
|
205
|
-
response.
|
|
206
|
+
response.destroy();
|
|
206
207
|
throw new ChallengeBlockedError(
|
|
207
208
|
`web_fetch was blocked by an AWS WAF anti-bot challenge (HTTP ${status}).`,
|
|
208
209
|
);
|
|
@@ -213,7 +214,7 @@ function assertSuccessfulResponse(response: IncomingMessage): void {
|
|
|
213
214
|
const cloudflareChallenge =
|
|
214
215
|
responseHeader(response, "cf-mitigated")?.trim().toLowerCase() === "challenge";
|
|
215
216
|
|
|
216
|
-
response.
|
|
217
|
+
response.destroy();
|
|
217
218
|
|
|
218
219
|
if (cloudflareChallenge) {
|
|
219
220
|
throw new ChallengeBlockedError(
|
|
@@ -569,6 +570,9 @@ async function fetchDocument(
|
|
|
569
570
|
|
|
570
571
|
const cancel = () => controller.abort();
|
|
571
572
|
signal?.addEventListener("abort", cancel, { once: true });
|
|
573
|
+
|
|
574
|
+
if (signal?.aborted) cancel();
|
|
575
|
+
let release: (() => void) | undefined;
|
|
572
576
|
const conditionalHeaders: Record<string, string> = {};
|
|
573
577
|
|
|
574
578
|
if (cached?.validators?.etag) conditionalHeaders["If-None-Match"] = cached.validators.etag;
|
|
@@ -584,6 +588,7 @@ async function fetchDocument(
|
|
|
584
588
|
};
|
|
585
589
|
|
|
586
590
|
try {
|
|
591
|
+
release = await acquisitionAdmission.acquire(controller.signal);
|
|
587
592
|
const requestUrl = normalizeGitHubRawUrl(rawUrl);
|
|
588
593
|
|
|
589
594
|
let { target, response } = await requestFollowingRedirects(
|
|
@@ -600,7 +605,7 @@ async function fetchDocument(
|
|
|
600
605
|
// for one exact representation opt out: HTML from that URL is never the
|
|
601
606
|
// representation they are looking for.
|
|
602
607
|
if (retryRepresentationMiss && [404, 406].includes(response.statusCode ?? 0)) {
|
|
603
|
-
response.
|
|
608
|
+
response.destroy();
|
|
604
609
|
({ target, response } = await requestFollowingRedirects(
|
|
605
610
|
target.url,
|
|
606
611
|
controller.signal,
|
|
@@ -616,7 +621,7 @@ async function fetchDocument(
|
|
|
616
621
|
const fallbackUrl = npmRegistryFallbackUrl(rawUrl);
|
|
617
622
|
|
|
618
623
|
if (fallbackUrl) {
|
|
619
|
-
response.
|
|
624
|
+
response.destroy();
|
|
620
625
|
({ target, response } = await requestFollowingRedirects(
|
|
621
626
|
fallbackUrl,
|
|
622
627
|
controller.signal,
|
|
@@ -632,7 +637,7 @@ async function fetchDocument(
|
|
|
632
637
|
const resolver = requestDependencies.awsWafResolver ?? nativeAwsWafResolver;
|
|
633
638
|
|
|
634
639
|
if (resolver) {
|
|
635
|
-
response.
|
|
640
|
+
response.destroy();
|
|
636
641
|
|
|
637
642
|
const solution = await resolveAwsWafToken(
|
|
638
643
|
resolver,
|
|
@@ -668,7 +673,7 @@ async function fetchDocument(
|
|
|
668
673
|
}
|
|
669
674
|
|
|
670
675
|
if (response.statusCode === 304 && cached) {
|
|
671
|
-
response.
|
|
676
|
+
response.destroy();
|
|
672
677
|
const etag = responseHeader(response, "etag") ?? cached.validators?.etag;
|
|
673
678
|
|
|
674
679
|
const lastModified =
|
|
@@ -702,6 +707,7 @@ async function fetchDocument(
|
|
|
702
707
|
if (signal?.aborted) throw new Error("web_fetch was cancelled.");
|
|
703
708
|
throw error;
|
|
704
709
|
} finally {
|
|
710
|
+
release?.();
|
|
705
711
|
clearTimeout(timeout);
|
|
706
712
|
signal?.removeEventListener("abort", cancel);
|
|
707
713
|
}
|
package/src/challenges/anubis.ts
CHANGED
|
@@ -394,7 +394,7 @@ async function fetchWasmModule(
|
|
|
394
394
|
const status = response.statusCode ?? 0;
|
|
395
395
|
|
|
396
396
|
if (status < 200 || status >= 300) {
|
|
397
|
-
response.
|
|
397
|
+
response.destroy();
|
|
398
398
|
throw new Error(`web_fetch could not download the Anubis challenge module (HTTP ${status}).`);
|
|
399
399
|
}
|
|
400
400
|
|
|
@@ -116,18 +116,17 @@ async function defaultSleep(milliseconds: number, signal: AbortSignal): Promise<
|
|
|
116
116
|
});
|
|
117
117
|
}
|
|
118
118
|
|
|
119
|
-
/** Parses a Retry-After header into a
|
|
119
|
+
/** Parses a Retry-After header into a delay in milliseconds. */
|
|
120
120
|
function retryAfterMilliseconds(value: string | undefined, now = Date.now()): number | undefined {
|
|
121
121
|
if (!value) return undefined;
|
|
122
|
-
const
|
|
122
|
+
const trimmed = value.trim();
|
|
123
123
|
|
|
124
|
-
if (
|
|
125
|
-
return Math.min(seconds * 1_000, MAX_RETRY_DELAY_MS);
|
|
124
|
+
if (/^\d+(?:\.\d+)?$/u.test(trimmed)) return Number(trimmed) * 1_000;
|
|
126
125
|
const date = Date.parse(value);
|
|
127
126
|
|
|
128
127
|
if (!Number.isFinite(date)) return undefined;
|
|
129
128
|
|
|
130
|
-
return Math.
|
|
129
|
+
return Math.max(0, date - now);
|
|
131
130
|
}
|
|
132
131
|
|
|
133
132
|
/** Sends one origin-limited request and retries transient rate-limit responses. */
|
|
@@ -170,10 +169,14 @@ async function coordinatedRequest(
|
|
|
170
169
|
const status = response.statusCode ?? 0;
|
|
171
170
|
|
|
172
171
|
if (![429, 503].includes(status) || attempt >= MAX_RETRIES) return response;
|
|
173
|
-
// The retry decision uses headers only. Drop an untrusted error body so it cannot
|
|
174
|
-
// consume unbounded bandwidth while the caller is backing off.
|
|
175
|
-
response.destroy();
|
|
176
172
|
const advertised = retryAfterMilliseconds(responseHeader(response, "retry-after"));
|
|
173
|
+
|
|
174
|
+
// Do not retry earlier than the server requested. Return the response so the
|
|
175
|
+
// caller can report its status rather than waiting beyond our retry budget.
|
|
176
|
+
if (advertised !== undefined && advertised > MAX_RETRY_DELAY_MS) return response;
|
|
177
|
+
|
|
178
|
+
// The retry decision uses headers only. Drop the untrusted error body.
|
|
179
|
+
response.destroy();
|
|
177
180
|
const exponential = BASE_RETRY_DELAY_MS * 2 ** attempt;
|
|
178
181
|
const jittered = Math.round(exponential * (0.75 + Math.min(1, Math.max(0, random())) * 0.5));
|
|
179
182
|
await sleep(advertised ?? Math.min(jittered, MAX_RETRY_DELAY_MS), signal);
|
|
@@ -202,11 +205,14 @@ export async function requestFollowingRedirects(
|
|
|
202
205
|
|
|
203
206
|
const location = responseHeader(response, "location");
|
|
204
207
|
|
|
208
|
+
// Redirect bodies are unused and may never finish. Close before validation
|
|
209
|
+
// or error handling so no discarded response survives the acquisition.
|
|
210
|
+
response.destroy();
|
|
211
|
+
|
|
205
212
|
if (!location) throw new Error("web_fetch received a redirect without a Location header.");
|
|
206
213
|
|
|
207
214
|
if (redirects === FETCH_MAX_REDIRECTS)
|
|
208
215
|
throw new Error("web_fetch followed too many redirects.");
|
|
209
|
-
response.resume();
|
|
210
216
|
target = await awaitWithAbort(validateUrl(new URL(location, target.url)), signal);
|
|
211
217
|
// Validators describe the originally cached resource and must not leak to a redirect target.
|
|
212
218
|
// Challenge handshakes opt into a fixed browser identity instead.
|