axios+telemetry cleanup
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import axios, { type AxiosResponse } from 'axios'
|
||||
import { isHttpError, nativeRequest } from '../../utils/http.js'
|
||||
import { LRUCache } from 'lru-cache'
|
||||
import {
|
||||
type AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
||||
@@ -180,7 +180,7 @@ export async function checkDomainBlocklist(
|
||||
return { status: 'allowed' }
|
||||
}
|
||||
try {
|
||||
const response = await axios.get(
|
||||
const response = await nativeRequest(
|
||||
`https://api.anthropic.com/api/web/domain_info?domain=${encodeURIComponent(domain)}`,
|
||||
{ timeout: DOMAIN_CHECK_TIMEOUT_MS },
|
||||
)
|
||||
@@ -264,17 +264,16 @@ export async function getWithPermittedRedirects(
|
||||
signal: AbortSignal,
|
||||
redirectChecker: (originalUrl: string, redirectUrl: string) => boolean,
|
||||
depth = 0,
|
||||
): Promise<AxiosResponse<ArrayBuffer> | RedirectInfo> {
|
||||
): Promise<{ data: ArrayBuffer; status: number; headers: Record<string, string> } | RedirectInfo> {
|
||||
if (depth > MAX_REDIRECTS) {
|
||||
throw new Error(`Too many redirects (exceeded ${MAX_REDIRECTS})`)
|
||||
}
|
||||
try {
|
||||
return await axios.get(url, {
|
||||
return await nativeRequest<ArrayBuffer>(url, {
|
||||
method: 'GET',
|
||||
signal,
|
||||
timeout: FETCH_TIMEOUT_MS,
|
||||
maxRedirects: 0,
|
||||
responseType: 'arraybuffer',
|
||||
maxContentLength: MAX_HTTP_CONTENT_LENGTH,
|
||||
headers: {
|
||||
Accept: 'text/markdown, text/html, */*',
|
||||
'User-Agent': getWebFetchUserAgent(),
|
||||
@@ -282,11 +281,11 @@ export async function getWithPermittedRedirects(
|
||||
})
|
||||
} catch (error) {
|
||||
if (
|
||||
axios.isAxiosError(error) &&
|
||||
error.response &&
|
||||
[301, 302, 307, 308].includes(error.response.status)
|
||||
isHttpError(error) &&
|
||||
error.status &&
|
||||
[301, 302, 307, 308].includes(error.status)
|
||||
) {
|
||||
const redirectLocation = error.response.headers.location
|
||||
const redirectLocation = error.headers?.location
|
||||
if (!redirectLocation) {
|
||||
throw new Error('Redirect missing Location header')
|
||||
}
|
||||
@@ -302,23 +301,22 @@ export async function getWithPermittedRedirects(
|
||||
redirectChecker,
|
||||
depth + 1,
|
||||
)
|
||||
} else {
|
||||
// Return redirect information to the caller
|
||||
return {
|
||||
type: 'redirect',
|
||||
originalUrl: url,
|
||||
redirectUrl,
|
||||
statusCode: error.response.status,
|
||||
}
|
||||
}
|
||||
// Return redirect information to the caller
|
||||
return {
|
||||
type: 'redirect',
|
||||
originalUrl: url,
|
||||
redirectUrl,
|
||||
statusCode: error.status,
|
||||
}
|
||||
}
|
||||
|
||||
// Detect egress proxy blocks: the proxy returns 403 with
|
||||
// X-Proxy-Error: blocked-by-allowlist when egress is restricted
|
||||
if (
|
||||
axios.isAxiosError(error) &&
|
||||
error.response?.status === 403 &&
|
||||
error.response.headers['x-proxy-error'] === 'blocked-by-allowlist'
|
||||
isHttpError(error) &&
|
||||
error.status === 403 &&
|
||||
error.headers?.['x-proxy-error'] === 'blocked-by-allowlist'
|
||||
) {
|
||||
const hostname = new URL(url).hostname
|
||||
throw new EgressBlockedError(hostname)
|
||||
@@ -329,7 +327,7 @@ export async function getWithPermittedRedirects(
|
||||
}
|
||||
|
||||
function isRedirectInfo(
|
||||
response: AxiosResponse<ArrayBuffer> | RedirectInfo,
|
||||
response: { data: ArrayBuffer; status: number; headers: Record<string, string> } | RedirectInfo,
|
||||
): response is RedirectInfo {
|
||||
return 'type' in response && response.type === 'redirect'
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user