Fix: ignore empty response

This commit is contained in:
SukkaW
2024-10-15 18:44:41 +08:00
parent 2bbc122b85
commit ac470d4af9
2 changed files with 24 additions and 13 deletions

View File

@@ -1,5 +1,5 @@
import picocolors from 'picocolors';
import { defaultRequestInit, fetchWithLog } from './fetch-retry';
import { defaultRequestInit, fetchWithLog, ResponseError } from './fetch-retry';
import { setTimeout } from 'node:timers/promises';
// eslint-disable-next-line sukka/unicorn/custom-error-definition -- typescript is better
@@ -42,7 +42,7 @@ export function sleepWithAbort(ms: number, signal: AbortSignal) {
});
}
export async function fetchAssets(url: string, fallbackUrls: string[] | readonly string[]) {
export async function fetchAssetsWith304(url: string, fallbackUrls: string[] | readonly string[]) {
const controller = new AbortController();
const createFetchFallbackPromise = async (url: string, index: number) => {
@@ -61,6 +61,11 @@ export async function fetchAssets(url: string, fallbackUrls: string[] | readonly
}
const res = await fetchWithLog(url, { signal: controller.signal, ...defaultRequestInit });
const text = await res.text();
if (text.length < 2) {
throw new ResponseError(res);
}
controller.abort();
return text;
};