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
@@ -31,6 +31,8 @@ export function createEventBus(): EventBus {
|
||||
}
|
||||
|
||||
export interface AppDeps {
|
||||
/** The port the page is served from; the only Origin (on localhost / 127.0.0.1) allowed to change state. */
|
||||
port?: number;
|
||||
db: Db; jobs: Jobs; resolver: ResolverLike;
|
||||
renderCardsPdf: RenderCardsPdf; renderCalibrationPdf: RenderCalibrationPdf;
|
||||
classNames: ClassNames;
|
||||
@@ -58,6 +60,13 @@ function callNumberFields(raw: string): Partial<Book> {
|
||||
manualOverride: true, confidence: 'manual', source: 'manual' };
|
||||
}
|
||||
|
||||
function cacheKeys(book: Book): string[] {
|
||||
const keys: string[] = [];
|
||||
if (book.isbn13) keys.push(`ol-data:${book.isbn13}`, `ol-search:${book.isbn13}`, `loc-isbn:${book.isbn13}`);
|
||||
if (book.lccn) keys.push(`loc-lccn:${book.lccn}`);
|
||||
return keys;
|
||||
}
|
||||
|
||||
export function createApp(deps: AppDeps): Hono {
|
||||
const { db, jobs, resolver, classNames } = deps;
|
||||
const events = deps.events ?? createEventBus();
|
||||
@@ -69,6 +78,20 @@ export function createApp(deps: AppDeps): Hono {
|
||||
return c.json({ error: 'Internal error' }, 500);
|
||||
});
|
||||
|
||||
// A web page on another origin must not be able to drive this local app. Requests without an
|
||||
// Origin header (curl, tests, same-origin GET-like navigations) are not browser cross-site requests.
|
||||
app.use('*', async (c, next) => {
|
||||
const method = c.req.method;
|
||||
const origin = c.req.header('origin');
|
||||
if (origin !== undefined && (method === 'POST' || method === 'PUT' || method === 'PATCH' || method === 'DELETE')) {
|
||||
const port = deps.port ?? (new URL(c.req.url).port || '80');
|
||||
if (origin !== `http://localhost:${port}` && origin !== `http://127.0.0.1:${port}`) {
|
||||
return c.json({ error: 'Cross-origin request refused' }, 403);
|
||||
}
|
||||
}
|
||||
await next();
|
||||
});
|
||||
|
||||
async function body(c: Context): Promise<Record<string, unknown>> {
|
||||
try {
|
||||
const v = await c.req.json();
|
||||
@@ -189,6 +212,9 @@ export function createApp(deps: AppDeps): Hono {
|
||||
app.post('/api/books/:id/retry', (c) => {
|
||||
const book = bookParam(c);
|
||||
if (book.manualOverride) throw new HttpError(400, 'This call number was entered by hand, so there is nothing to look up');
|
||||
// "Look up again" must really ask again, so forget what the sources told us for this book.
|
||||
for (const key of cacheKeys(book)) db.cache.delete(key);
|
||||
if (book.title) db.cache.deletePrefix(`loc-title:${book.title}|`);
|
||||
const updated = db.updateBook(book.id, { status: 'pending', attentionReason: null })!;
|
||||
events.emitBook(updated);
|
||||
jobs.enqueue(book.id);
|
||||
|
||||
Reference in new issue
Block a user