




















@@ -7,9 +7,12 @@ import {
77} from "../infra/net/fetch-guard.js";
88import type { LookupFn, PinnedDispatcherPolicy, SsrFPolicy } from "../infra/net/ssrf.js";
99import { redactSensitiveText } from "../logging/redact.js";
10+import { MAX_DOCUMENT_BYTES } from "./constants.js";
1011import { detectMime, extensionForMime } from "./mime.js";
1112import { readResponseTextSnippet, readResponseWithLimit } from "./read-response-with-limit.js";
121314+export const DEFAULT_FETCH_MEDIA_MAX_BYTES = MAX_DOCUMENT_BYTES;
15+1316type FetchMediaResult = {
1417buffer: Buffer;
1518contentType?: string;
@@ -209,29 +212,28 @@ export async function fetchRemoteMedia(options: FetchMediaOptions): Promise<Fetc
209212);
210213}
211214215+const effectiveMaxBytes = maxBytes ?? DEFAULT_FETCH_MEDIA_MAX_BYTES;
212216const contentLength = res.headers.get("content-length");
213-if (maxBytes && contentLength) {
217+if (contentLength) {
214218const length = Number(contentLength);
215-if (Number.isFinite(length) && length > maxBytes) {
219+if (Number.isFinite(length) && length > effectiveMaxBytes) {
216220throw new MediaFetchError(
217221"max_bytes",
218-`Failed to fetch media from ${sourceUrl}: content length ${length} exceeds maxBytes ${maxBytes}`,
222+`Failed to fetch media from ${sourceUrl}: content length ${length} exceeds maxBytes ${effectiveMaxBytes}`,
219223);
220224}
221225}
222226223227let buffer: Buffer;
224228try {
225-buffer = maxBytes
226- ? await readResponseWithLimit(res, maxBytes, {
227-onOverflow: ({ maxBytes, res }) =>
228-new MediaFetchError(
229-"max_bytes",
230-`Failed to fetch media from ${redactMediaUrl(res.url || url)}: payload exceeds maxBytes ${maxBytes}`,
231-),
232-chunkTimeoutMs: readIdleTimeoutMs,
233-})
234- : Buffer.from(await res.arrayBuffer());
229+buffer = await readResponseWithLimit(res, effectiveMaxBytes, {
230+onOverflow: ({ maxBytes, res }) =>
231+new MediaFetchError(
232+"max_bytes",
233+`Failed to fetch media from ${redactMediaUrl(res.url || url)}: payload exceeds maxBytes ${maxBytes}`,
234+),
235+chunkTimeoutMs: readIdleTimeoutMs,
236+});
235237} catch (err) {
236238if (err instanceof MediaFetchError) {
237239throw err;
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。