mirror of
https://github.com/SukkaW/Surge.git
synced 2026-01-29 01:51:52 +08:00
Fix corrupt local cache
This commit is contained in:
@@ -350,13 +350,15 @@ export class Cache<S = string> {
|
||||
this.set(getETagKey(url), serverETag, TTL.ONE_WEEK_STATIC);
|
||||
}
|
||||
// If we do not have a cached value, we ignore 304
|
||||
if (res.statusCode === 304 && typeof previouslyCached === 'string') {
|
||||
controller.abort();
|
||||
throw new Custom304NotModifiedError(url, previouslyCached);
|
||||
if (res.statusCode === 304 && typeof previouslyCached === 'string' && previouslyCached.length > 1) {
|
||||
const err = new Custom304NotModifiedError(url, previouslyCached);
|
||||
controller.abort(err);
|
||||
throw err;
|
||||
}
|
||||
if (!serverETag && !this.get(getETagKey(primaryUrl)) && typeof previouslyCached === 'string') {
|
||||
controller.abort();
|
||||
throw new CustomNoETagFallbackError(previouslyCached);
|
||||
const err = new CustomNoETagFallbackError(previouslyCached);
|
||||
controller.abort(err);
|
||||
throw err;
|
||||
}
|
||||
|
||||
// either no etag and not cached
|
||||
@@ -386,18 +388,26 @@ export class Cache<S = string> {
|
||||
|
||||
return value;
|
||||
} catch (e) {
|
||||
if (e instanceof AggregateError) {
|
||||
if (e && typeof e === 'object' && 'errors' in e && Array.isArray(e.errors)) {
|
||||
const deserializer = 'deserializer' in opt ? opt.deserializer : identity as any;
|
||||
|
||||
for (const error of e.errors) {
|
||||
if (error instanceof Custom304NotModifiedError) {
|
||||
console.log(picocolors.green('[cache] http 304'), picocolors.gray(primaryUrl));
|
||||
this.updateTtl(cachedKey, TTL.ONE_WEEK_STATIC);
|
||||
return deserializer(error.data);
|
||||
console.log(e.errors);
|
||||
|
||||
for (let i = 0, len = e.errors.length; i < len; i++) {
|
||||
const error = e.errors[i];
|
||||
if ('name' in error && (error.name === 'CustomAbortError' || error.name === 'AbortError')) {
|
||||
continue;
|
||||
}
|
||||
if (error instanceof CustomNoETagFallbackError) {
|
||||
console.log(picocolors.green('[cache] hit'), picocolors.gray(primaryUrl));
|
||||
return deserializer(error.data);
|
||||
if ('digest' in error) {
|
||||
if (error.digest === 'Custom304NotModifiedError') {
|
||||
console.log(picocolors.green('[cache] http 304'), picocolors.gray(primaryUrl));
|
||||
this.updateTtl(cachedKey, TTL.ONE_WEEK_STATIC);
|
||||
return deserializer(error.data);
|
||||
}
|
||||
if (error.digest === 'CustomNoETagFallbackError') {
|
||||
console.log(picocolors.green('[cache] hit'), picocolors.gray(primaryUrl));
|
||||
return deserializer(error.data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,8 +22,9 @@ export const readFileByLine: ((file: string | FileHandle) => AsyncIterable<strin
|
||||
.pipeThrough(new TextDecoderStream())
|
||||
.pipeThrough(new TextLineStream());
|
||||
|
||||
function ensureResponseBody<T extends Response | NodeFetchResponse | UndiciResponseData>(resp: T): NonNullable<T['body']> {
|
||||
if (!resp.body) {
|
||||
function ensureResponseBody<T extends NodeFetchResponse | UndiciResponseData>(resp: T): NonNullable<T['body']> {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- NodeFetchResponse['body'] is nullable
|
||||
if (resp.body == null) {
|
||||
throw new Error('Failed to fetch remote text');
|
||||
}
|
||||
if ('bodyUsed' in resp && resp.bodyUsed) {
|
||||
@@ -32,7 +33,7 @@ function ensureResponseBody<T extends Response | NodeFetchResponse | UndiciRespo
|
||||
return resp.body;
|
||||
}
|
||||
|
||||
export const createReadlineInterfaceFromResponse: ((resp: Response | NodeFetchResponse | UndiciResponseData) => AsyncIterable<string>) = (resp) => {
|
||||
export const createReadlineInterfaceFromResponse: ((resp: NodeFetchResponse | UndiciResponseData) => AsyncIterable<string>) = (resp) => {
|
||||
const stream = ensureResponseBody(resp);
|
||||
|
||||
const webStream: ReadableStream<Uint8Array> = 'getReader' in stream
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { createReadlineInterfaceFromResponse } from './fetch-text-by-line';
|
||||
import { parse as tldtsParse } from 'tldts';
|
||||
import { $fetch } from './make-fetch-happen';
|
||||
import type { NodeFetchResponse } from './make-fetch-happen';
|
||||
import type { UndiciResponseData } from './fetch-retry';
|
||||
|
||||
@@ -28,8 +27,3 @@ export async function parseFelixDnsmasqFromResp(resp: NodeFetchResponse | Undici
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
export async function parseFelixDnsmasq(url: string): Promise<string[]> {
|
||||
const resp = await $fetch(url);
|
||||
return parseFelixDnsmasqFromResp(resp);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user