From 99e5d46531395af1749e532bd35fda8d01e2bfb7 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Wed, 9 Oct 2024 21:10:25 +0800 Subject: [PATCH] Fix types --- Build/lib/cache-filesystem.ts | 18 +++++++++--------- Build/lib/fetch-assets.ts | 2 +- Build/lib/fetch-retry.ts | 4 +++- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/Build/lib/cache-filesystem.ts b/Build/lib/cache-filesystem.ts index bb608b26..cb8e90bd 100644 --- a/Build/lib/cache-filesystem.ts +++ b/Build/lib/cache-filesystem.ts @@ -214,7 +214,7 @@ export class Cache { fn: (resp: Response) => Promise, opt: Omit, 'incrementTtlWhenHit'>, requestInit?: RequestInit - ) { + ): Promise { if (opt.temporaryBypass) { return fn(await fetchWithRetry(url, requestInit ?? defaultRequestInit)); } @@ -323,12 +323,12 @@ export class Cache { this.set(getETagKey(primaryUrl), r.headers.get('etag')!, TTL.ONE_WEEK_STATIC); // If we do not have a cached value, we ignore 304 - if (r.status === 304 && previouslyCached != null) { + if (r.status === 304 && typeof previouslyCached === 'string') { controller.abort(); - throw new Custom304NotModifiedError(primaryUrl); + throw new Custom304NotModifiedError(primaryUrl, previouslyCached); } - } else if (!primaryETag && previouslyCached) { - throw new CustomNoETagFallbackError(previouslyCached as string); + } else if (!primaryETag && typeof previouslyCached === 'string') { + throw new CustomNoETagFallbackError(previouslyCached); } return r.text(); @@ -369,13 +369,13 @@ export class Cache { this.set(getETagKey(url), res.headers.get('etag')!, TTL.ONE_WEEK_STATIC); // If we do not have a cached value, we ignore 304 - if (res.status === 304 && previouslyCached != null) { + if (res.status === 304 && typeof previouslyCached === 'string') { controller.abort(); - throw new Custom304NotModifiedError(url); + throw new Custom304NotModifiedError(url, previouslyCached); } - } else if (!primaryETag && previouslyCached) { + } else if (!primaryETag && typeof previouslyCached === 'string') { controller.abort(); - throw new CustomNoETagFallbackError(previouslyCached as string); + throw new CustomNoETagFallbackError(previouslyCached); } const text = await res.text(); diff --git a/Build/lib/fetch-assets.ts b/Build/lib/fetch-assets.ts index 05e48181..bd3804aa 100644 --- a/Build/lib/fetch-assets.ts +++ b/Build/lib/fetch-assets.ts @@ -12,7 +12,7 @@ export class Custom304NotModifiedError extends Error { public readonly name = 'Custom304NotModifiedError'; public readonly digest = 'Custom304NotModifiedError'; - constructor(public readonly url: string) { + constructor(public readonly url: string, public readonly data: string) { super('304 Not Modified'); } } diff --git a/Build/lib/fetch-retry.ts b/Build/lib/fetch-retry.ts index 788fbecf..af73f343 100644 --- a/Build/lib/fetch-retry.ts +++ b/Build/lib/fetch-retry.ts @@ -107,7 +107,9 @@ function createFetchRetry($fetch: typeof fetch): FetchWithRetry { } console.log(picocolors.gray('[fetch fail]'), url, err); - throw err; + const newErr = new Error('Fetch failed'); + newErr.cause = err; + throw newErr; } }, retryOpts); } catch (err) {