Add resolver, server, PDF rendering and web app
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
6a7fcdc4b8
commit
bdfdf581f3
59 files changed
+4088
No files matched your search
@@ -0,0 +1,151 @@
|
||||
import type { BookMeta, Candidate, ResolveInput, Resolution, SearchResult } from '../../shared/types.ts';
|
||||
import { normalizeIsbn } from '../../shared/isbn.ts';
|
||||
import { chooseCallNumber } from './choose.ts';
|
||||
import { createLoc } from '../sources/loc.ts';
|
||||
import { createOpenLibrary } from '../sources/openlibrary.ts';
|
||||
import type { OlInfo } from '../sources/openlibrary.ts';
|
||||
import type { MarcFields } from '../sources/marc.ts';
|
||||
import { TemporaryError } from '../sources/http.ts';
|
||||
import type { SourceCache } from '../sources/http.ts';
|
||||
|
||||
export { TemporaryError };
|
||||
export type { SourceCache };
|
||||
|
||||
export interface Resolver {
|
||||
resolve(input: ResolveInput, opts?: { onMeta?: (meta: BookMeta) => void }): Promise<Resolution>;
|
||||
search(title: string, author: string): Promise<SearchResult[]>;
|
||||
}
|
||||
|
||||
type Partials = (Partial<BookMeta> | null | undefined)[];
|
||||
|
||||
function mergeMeta(parts: Partials): BookMeta | null {
|
||||
const first = <K extends keyof BookMeta>(k: K): BookMeta[K] | undefined => {
|
||||
for (const p of parts) {
|
||||
const v = p?.[k];
|
||||
if (Array.isArray(v) ? v.length > 0 : v !== undefined && v !== null && v !== '') return v as BookMeta[K];
|
||||
}
|
||||
return undefined;
|
||||
};
|
||||
const title = first('title');
|
||||
if (!title) return null;
|
||||
const meta: BookMeta = { title, authors: first('authors') ?? [], subjects: first('subjects') ?? [] };
|
||||
const subtitle = first('subtitle'); if (subtitle) meta.subtitle = subtitle;
|
||||
const publisher = first('publisher'); if (publisher) meta.publisher = publisher;
|
||||
const year = first('year'); if (year) meta.year = year;
|
||||
const lccn = first('lccn'); if (lccn) meta.lccn = lccn;
|
||||
return meta;
|
||||
}
|
||||
|
||||
const lccOf = (recs: MarcFields[]): Candidate[] => recs.flatMap((r) => r.lcc);
|
||||
|
||||
export function createResolver(deps: {
|
||||
fetch: typeof fetch; cache: SourceCache;
|
||||
locIntervalMs?: number;
|
||||
sleep?: (ms: number) => Promise<void>;
|
||||
}): Resolver {
|
||||
const ol = createOpenLibrary({ fetch: deps.fetch, cache: deps.cache });
|
||||
const loc = createLoc({
|
||||
fetch: deps.fetch, cache: deps.cache,
|
||||
intervalMs: deps.locIntervalMs ?? 6000,
|
||||
sleep: deps.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms))),
|
||||
});
|
||||
|
||||
async function resolve(input: ResolveInput, opts?: { onMeta?: (meta: BookMeta) => void }): Promise<Resolution> {
|
||||
const isbn = input.isbn13 ? (normalizeIsbn(input.isbn13) ?? input.isbn13) : undefined;
|
||||
let notified = false;
|
||||
const notify = (m: BookMeta | null) => {
|
||||
if (m && !notified) { notified = true; opts?.onMeta?.(m); }
|
||||
};
|
||||
|
||||
// Open Library: edition first; the work search runs alongside and is awaited only when needed.
|
||||
let edition: OlInfo | null = null;
|
||||
let work: OlInfo | null = null;
|
||||
let workP: Promise<{ v: OlInfo | null } | { e: unknown }> | null = null;
|
||||
const getWork = async (required: boolean): Promise<OlInfo | null> => {
|
||||
if (!workP) return null;
|
||||
const r = await workP;
|
||||
if ('e' in r) {
|
||||
if (required) throw r.e;
|
||||
return null;
|
||||
}
|
||||
return r.v;
|
||||
};
|
||||
if (isbn) {
|
||||
workP = ol.work(isbn).then((v) => ({ v }), (e: unknown) => ({ e }));
|
||||
edition = await ol.edition(isbn);
|
||||
if (edition) notify(mergeMeta([edition.meta]));
|
||||
else { work = await getWork(true); notify(mergeMeta([work?.meta])); }
|
||||
}
|
||||
|
||||
// Library of Congress: by LCCN when known, else by ISBN.
|
||||
const lccn = input.lccn ?? edition?.meta.lccn;
|
||||
let locRecs: MarcFields[] = [];
|
||||
let exactSource: 'loc-isbn' | 'loc-lccn' = 'loc-isbn';
|
||||
if (lccn) {
|
||||
locRecs = await loc.byLccn(lccn);
|
||||
if (locRecs.length > 0) exactSource = 'loc-lccn';
|
||||
}
|
||||
if (isbn && lccOf(locRecs).length === 0) {
|
||||
const recs = await loc.byIsbn(isbn);
|
||||
if (recs.length > 0) { locRecs = recs; exactSource = 'loc-isbn'; }
|
||||
}
|
||||
|
||||
const olEditionCands: Candidate[] = edition?.lcc ?? [];
|
||||
const metaOf = (extra: Partials = []): BookMeta | null =>
|
||||
mergeMeta([edition?.meta, locRecs[0]?.meta, ...extra, input.title ? { title: input.title } : null]);
|
||||
const forChoose = (m: BookMeta | null) => ({
|
||||
authors: m?.authors.length ? m.authors : input.author ? [input.author] : [],
|
||||
title: m?.title ?? input.title ?? '',
|
||||
year: m?.year ?? input.year,
|
||||
});
|
||||
notify(metaOf());
|
||||
|
||||
// Tiers 1 and 2.
|
||||
let chosen = chooseCallNumber(
|
||||
{ locExact: lccOf(locRecs), olEdition: olEditionCands, locTitle: [], olWork: [] },
|
||||
forChoose(metaOf()), exactSource,
|
||||
);
|
||||
|
||||
// Tier 3: LoC by title and author.
|
||||
let locTitleRecs: MarcFields[] = [];
|
||||
if (!chosen) {
|
||||
work ??= await getWork(false);
|
||||
const m = metaOf([work?.meta]);
|
||||
const title = m?.title ?? input.title;
|
||||
if (title) {
|
||||
locTitleRecs = await loc.byTitle(title, m?.authors[0] ?? input.author ?? '');
|
||||
chosen = chooseCallNumber(
|
||||
{ locExact: [], olEdition: [], locTitle: lccOf(locTitleRecs), olWork: [] },
|
||||
forChoose(m), exactSource,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Tier 4, and the work-level part of the metadata merge.
|
||||
work ??= await getWork(!chosen);
|
||||
// Without an ISBN or LCCN, the best title hit also supplies metadata.
|
||||
const hitMeta = !isbn && !input.lccn ? locTitleRecs[0]?.meta : undefined;
|
||||
const meta = metaOf([hitMeta, work?.meta]);
|
||||
notify(meta);
|
||||
chosen ??= chooseCallNumber(
|
||||
{ locExact: [], olEdition: [], locTitle: [], olWork: work?.lcc ?? [] },
|
||||
forChoose(meta), exactSource,
|
||||
);
|
||||
|
||||
const dewey = locRecs.map((r) => r.dewey).find(Boolean) ?? edition?.dewey ?? work?.dewey
|
||||
?? locTitleRecs.map((r) => r.dewey).find(Boolean) ?? null;
|
||||
|
||||
if (chosen) {
|
||||
return {
|
||||
meta, callNumber: chosen.callNumber, sortKey: chosen.sortKey,
|
||||
confidence: chosen.confidence, source: chosen.source, dewey, reason: null,
|
||||
};
|
||||
}
|
||||
return {
|
||||
meta, callNumber: null, sortKey: null, confidence: null, source: null, dewey,
|
||||
reason: meta ? 'No Library of Congress number found' : 'Not found in Open Library or the Library of Congress',
|
||||
};
|
||||
}
|
||||
|
||||
return { resolve, search: (title, author) => ol.search(title, author) };
|
||||
}
|
||||
Reference in new issue
Block a user