mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-18 20:30:27 +08:00
Refactor: 304 cache
This commit is contained in:
parent
d51f498579
commit
7c78d0483b
@ -363,68 +363,63 @@ export class Cache<S = string> {
|
|||||||
} catch (e) {
|
} catch (e) {
|
||||||
const deserializer = 'deserializer' in opt ? opt.deserializer : identity as any;
|
const deserializer = 'deserializer' in opt ? opt.deserializer : identity as any;
|
||||||
|
|
||||||
const on304 = (error: Custom304NotModifiedError) => {
|
const on304 = (error: Custom304NotModifiedError): NonNullable<T> => {
|
||||||
console.log(picocolors.green('[cache] http 304'), picocolors.gray(primaryUrl));
|
console.log(picocolors.green('[cache] http 304'), picocolors.gray(primaryUrl));
|
||||||
this.updateTtl(cachedKey, TTL.ONE_WEEK_STATIC);
|
this.updateTtl(cachedKey, TTL.ONE_WEEK_STATIC);
|
||||||
return deserializer(error.data);
|
return deserializer(error.data);
|
||||||
};
|
};
|
||||||
|
|
||||||
const onNoETagFallback = (error: CustomNoETagFallbackError) => {
|
const onNoETagFallback = (error: CustomNoETagFallbackError): NonNullable<T> => {
|
||||||
console.log(picocolors.green('[cache] hit'), picocolors.gray(primaryUrl));
|
console.log(picocolors.green('[cache] hit'), picocolors.gray(primaryUrl));
|
||||||
return deserializer(error.data);
|
return deserializer(error.data);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const onSingleError = (error: object & {}) => {
|
||||||
|
if ('name' in error) {
|
||||||
|
if (error.name === 'Custom304NotModifiedError') {
|
||||||
|
return on304(error as Custom304NotModifiedError);
|
||||||
|
}
|
||||||
|
if (error.name === 'CustomNoETagFallbackError') {
|
||||||
|
return onNoETagFallback(error as CustomNoETagFallbackError);
|
||||||
|
}
|
||||||
|
if (error.name === 'CustomAbortError' || error.name === 'AbortError') {
|
||||||
|
// noop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ('digest' in error) {
|
||||||
|
if (error.digest === 'Custom304NotModifiedError') {
|
||||||
|
return on304(error as Custom304NotModifiedError);
|
||||||
|
}
|
||||||
|
if (error.digest === 'CustomNoETagFallbackError') {
|
||||||
|
return onNoETagFallback(error as CustomNoETagFallbackError);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
if (e && typeof e === 'object') {
|
if (e && typeof e === 'object') {
|
||||||
if ('errors' in e && Array.isArray(e.errors)) {
|
if ('errors' in e && Array.isArray(e.errors)) {
|
||||||
for (let i = 0, len = e.errors.length; i < len; i++) {
|
for (let i = 0, len = e.errors.length; i < len; i++) {
|
||||||
const error = e.errors[i];
|
const error = e.errors[i];
|
||||||
if ('name' in error) {
|
|
||||||
if (error.name === 'CustomAbortError' || error.name === 'AbortError') {
|
const result = onSingleError(error);
|
||||||
continue;
|
if (result !== null) {
|
||||||
}
|
return result;
|
||||||
if (error.name === 'Custom304NotModifiedError') {
|
|
||||||
return on304(error);
|
|
||||||
}
|
|
||||||
if (error.name === 'CustomNoETagFallbackError') {
|
|
||||||
return onNoETagFallback(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ('digest' in error) {
|
|
||||||
if (error.digest === 'Custom304NotModifiedError') {
|
|
||||||
return on304(error);
|
|
||||||
}
|
|
||||||
if (error.digest === 'CustomNoETagFallbackError') {
|
|
||||||
return onNoETagFallback(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(picocolors.red('[fetch error]'), picocolors.gray(`[${primaryUrl}]`), error);
|
console.log(picocolors.red('[fetch error 1]'), picocolors.gray(`[${primaryUrl}]`), error);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if ('name' in e) {
|
const result = onSingleError(e);
|
||||||
if (e.name === 'Custom304NotModifiedError') {
|
if (result !== null) {
|
||||||
return on304(e as Custom304NotModifiedError);
|
return result;
|
||||||
}
|
}
|
||||||
if (e.name === 'CustomNoETagFallbackError') {
|
|
||||||
return onNoETagFallback(e as CustomNoETagFallbackError);
|
console.log(picocolors.red('[fetch error 2]'), picocolors.gray(`[${primaryUrl}]`), e);
|
||||||
}
|
|
||||||
}
|
|
||||||
if ('digest' in e) {
|
|
||||||
if (e.digest === 'Custom304NotModifiedError') {
|
|
||||||
return on304(e as Custom304NotModifiedError);
|
|
||||||
}
|
|
||||||
if (e.digest === 'CustomNoETagFallbackError') {
|
|
||||||
return onNoETagFallback(e as CustomNoETagFallbackError);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(picocolors.red('[fetch error]'), picocolors.gray(`[${primaryUrl}]`), e);
|
console.log(`Download Rule for [${primaryUrl}] failed`, { e, name: (e as any).name });
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log({ e, name: (e as any).name });
|
|
||||||
|
|
||||||
console.log(`Download Rule for [${primaryUrl}] failed`);
|
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user