fix(chat): downgrade media prefetch cache failures

This commit is contained in:
2026-07-01 16:53:11 +08:00
parent 1fbdd41da8
commit 5f9df58dbe
2 changed files with 135 additions and 57 deletions
@@ -46,58 +46,7 @@ export class ChatMediaCacheCoordinator {
async cacheRemoteMedia( async cacheRemoteMedia(
input: CacheRemoteChatMediaInput, input: CacheRemoteChatMediaInput,
): Promise<Result<LocalChatMediaRow>> { ): Promise<Result<LocalChatMediaRow>> {
return Result.wrap(async () => { return Result.wrap(() => this._cacheRemoteMediaOrThrow(input));
if (!isCacheableRemoteChatMediaUrl(input.remoteUrl)) {
throw new Error(`Unsupported media url: ${input.remoteUrl}`);
}
const ownerKeys = await this._resolveMediaOwnerKeys();
const ownerKey = ownerKeys[0] ?? "anonymous";
const cacheKey = this._buildMediaCacheKey(ownerKey, input);
for (const lookupOwnerKey of ownerKeys) {
const lookupCacheKey = this._buildMediaCacheKey(lookupOwnerKey, input);
const existingResult = await this.mediaStorage.getMedia(lookupCacheKey);
if (Result.isErr(existingResult)) throw existingResult.error;
if (existingResult.data) return existingResult.data;
}
const response = await fetch(input.remoteUrl, {
method: "GET",
cache: "force-cache",
});
if (!response.ok) {
throw new Error(
`Media download failed: ${response.status} ${response.statusText}`,
);
}
const blob = await response.blob();
if (blob.size <= 0) {
throw new Error("Media download returned an empty blob.");
}
const saveResult = await this.mediaStorage.saveMedia({
cacheKey,
ownerKey,
messageId: input.messageId,
kind: input.kind,
remoteUrl: input.remoteUrl,
blob,
mimeType:
blob.type ||
response.headers.get("content-type") ||
fallbackChatMediaMimeType(input.kind),
});
if (Result.isErr(saveResult)) throw saveResult.error;
void requestBrowserPersistentStorageOnce();
const savedResult = await this.mediaStorage.getMedia(cacheKey);
if (Result.isErr(savedResult)) throw savedResult.error;
if (!savedResult.data) {
throw new Error("Cached media could not be read after save.");
}
return savedResult.data;
});
} }
async prefetchMediaForMessages( async prefetchMediaForMessages(
@@ -119,19 +68,75 @@ export class ChatMediaCacheCoordinator {
): Promise<Result<void>> { ): Promise<Result<void>> {
return Result.wrap(async () => { return Result.wrap(async () => {
for (const target of uniqueMediaTargets(targets)) { for (const target of uniqueMediaTargets(targets)) {
const result = await this.cacheRemoteMedia(target); try {
if (Result.isErr(result)) { await this._cacheRemoteMediaOrThrow(target);
} catch (error) {
log.warn("[chat-media] prefetch failed", { log.warn("[chat-media] prefetch failed", {
messageId: target.messageId, messageId: target.messageId,
kind: target.kind, kind: target.kind,
remoteUrl: target.remoteUrl, remoteUrl: target.remoteUrl,
error: result.error, error,
}); });
} }
} }
}); });
} }
private async _cacheRemoteMediaOrThrow(
input: CacheRemoteChatMediaInput,
): Promise<LocalChatMediaRow> {
if (!isCacheableRemoteChatMediaUrl(input.remoteUrl)) {
throw new Error(`Unsupported media url: ${input.remoteUrl}`);
}
const ownerKeys = await this._resolveMediaOwnerKeys();
const ownerKey = ownerKeys[0] ?? "anonymous";
const cacheKey = this._buildMediaCacheKey(ownerKey, input);
for (const lookupOwnerKey of ownerKeys) {
const lookupCacheKey = this._buildMediaCacheKey(lookupOwnerKey, input);
const existingResult = await this.mediaStorage.getMedia(lookupCacheKey);
if (Result.isErr(existingResult)) throw existingResult.error;
if (existingResult.data) return existingResult.data;
}
const response = await fetch(input.remoteUrl, {
method: "GET",
cache: "force-cache",
});
if (!response.ok) {
throw new Error(
`Media download failed: ${response.status} ${response.statusText}`,
);
}
const blob = await response.blob();
if (blob.size <= 0) {
throw new Error("Media download returned an empty blob.");
}
const saveResult = await this.mediaStorage.saveMedia({
cacheKey,
ownerKey,
messageId: input.messageId,
kind: input.kind,
remoteUrl: input.remoteUrl,
blob,
mimeType:
blob.type ||
response.headers.get("content-type") ||
fallbackChatMediaMimeType(input.kind),
});
if (Result.isErr(saveResult)) throw saveResult.error;
void requestBrowserPersistentStorageOnce();
const savedResult = await this.mediaStorage.getMedia(cacheKey);
if (Result.isErr(savedResult)) throw savedResult.error;
if (!savedResult.data) {
throw new Error("Cached media could not be read after save.");
}
return savedResult.data;
}
private async _resolveMediaOwnerKeys(): Promise<string[]> { private async _resolveMediaOwnerKeys(): Promise<string[]> {
const ownerKeys: string[] = []; const ownerKeys: string[] = [];
const userIdResult = await UserStorage.getInstance().getUserId(); const userIdResult = await UserStorage.getInstance().getUserId();
+75 -2
View File
@@ -110,8 +110,17 @@ export class Logger {
} }
} }
const serializableValue = Logger.toSerializableLogValue(value);
if (serializableValue !== value) {
try {
return JSON.stringify(serializableValue, null, 2);
} catch {
return String(value);
}
}
try { try {
return JSON.stringify(value, null, 2); return JSON.stringify(serializableValue, null, 2);
} catch { } catch {
return String(value); return String(value);
} }
@@ -206,7 +215,10 @@ export class Logger {
return; return;
} }
console[method](`${prefix} ${message}`, ...data); console[method](
`${prefix} ${message}`,
...Logger.toBrowserConsoleData(data),
);
} }
private static toBrowserConsolePayload(args: LogArgs): { private static toBrowserConsolePayload(args: LogArgs): {
@@ -235,6 +247,67 @@ export class Logger {
}; };
} }
private static toBrowserConsoleData(data: unknown[]): unknown[] {
return data.map((item) => Logger.toSerializableLogValue(item));
}
private static toSerializableLogValue(
value: unknown,
seen = new WeakSet<object>(),
): unknown {
if (value instanceof Error) {
return Logger.serializeError(value, seen);
}
if (Array.isArray(value)) {
return value.map((item) => Logger.toSerializableLogValue(item, seen));
}
if (typeof value !== "object" || value === null) {
return value;
}
if (value instanceof Date) {
return value.toISOString();
}
if (seen.has(value)) {
return "[Circular]";
}
seen.add(value);
const output: Record<string, unknown> = {};
for (const [key, item] of Object.entries(value)) {
output[key] = Logger.toSerializableLogValue(item, seen);
}
return output;
}
private static serializeError(
error: Error,
seen: WeakSet<object>,
): Record<string, unknown> {
const output: Record<string, unknown> = {
name: error.name,
message: error.message,
stack: error.stack,
};
if (seen.has(error)) {
return output;
}
seen.add(error);
for (const key of Object.getOwnPropertyNames(error)) {
if (key in output) continue;
output[key] = Logger.toSerializableLogValue(
(error as unknown as Record<string, unknown>)[key],
seen,
);
}
return output;
}
/** 透传 raw pino logger(高级用法:自定义 binding / child / serializers */ /** 透传 raw pino logger(高级用法:自定义 binding / child / serializers */
get raw(): PinoLogger { get raw(): PinoLogger {
return this.logger; return this.logger;