Rework scan screen, make lookups non-blocking, fix review findings
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
468cafc6db
commit
b5defb4e4f
46 files changed
+1957
-406
No files matched your search
@@ -2,16 +2,27 @@ export class TemporaryError extends Error {}
|
||||
|
||||
export interface SourceCache { get(key: string): string | null; set(key: string, body: string): void }
|
||||
|
||||
/** The database cache can also forget entries, so "Look up again" really asks again. */
|
||||
export interface ForgettableCache extends SourceCache {
|
||||
delete(key: string): void;
|
||||
deletePrefix(prefix: string): void;
|
||||
}
|
||||
|
||||
export const REQUEST_TIMEOUT_MS = 15_000;
|
||||
|
||||
export const USER_AGENT = 'booksorter/0.1 (personal library tool)';
|
||||
|
||||
export interface HttpResult { status: number; body: string }
|
||||
|
||||
/** One GET. Network errors and 5xx/429 become TemporaryError; other statuses are returned. */
|
||||
export async function getText(f: typeof fetch, url: string): Promise<HttpResult> {
|
||||
export async function getText(f: typeof fetch, url: string, timeoutMs = REQUEST_TIMEOUT_MS): Promise<HttpResult> {
|
||||
const signal = AbortSignal.timeout(timeoutMs);
|
||||
const timedOut = (e: unknown) => signal.aborted || (e as Error)?.name === 'TimeoutError';
|
||||
let res: Response;
|
||||
try {
|
||||
res = await f(url, { headers: { 'User-Agent': USER_AGENT } });
|
||||
res = await f(url, { headers: { 'User-Agent': USER_AGENT }, signal });
|
||||
} catch (e) {
|
||||
if (timedOut(e)) throw new TemporaryError(`Timed out after ${Math.round(timeoutMs / 1000)} s waiting for ${url}`);
|
||||
throw new TemporaryError(`Network error for ${url}: ${(e as Error).message}`);
|
||||
}
|
||||
if (res.status >= 500 || res.status === 429) throw new TemporaryError(`HTTP ${res.status} from ${url}`);
|
||||
@@ -19,6 +30,7 @@ export async function getText(f: typeof fetch, url: string): Promise<HttpResult>
|
||||
try {
|
||||
body = await res.text();
|
||||
} catch (e) {
|
||||
if (timedOut(e)) throw new TemporaryError(`Timed out after ${Math.round(timeoutMs / 1000)} s waiting for ${url}`);
|
||||
throw new TemporaryError(`Network error reading ${url}: ${(e as Error).message}`);
|
||||
}
|
||||
return { status: res.status, body };
|
||||
|
||||
Reference in new issue
Block a user