Web version: databases read in a worker, the shelf's counts, Chrome and Edge remember chosen files

The page's reader: a Web Worker with a second instance of the program reads
a database (PGN, CBV, ZIP, 7Z) and gives back its text and index; the page
puts it together without reading it again (chess_core::PgnIndex,
PgnDatabase::into_index / from_index, serde behind a feature). A 13 MB CBV:
the longest frame 17 ms in Firefox, 67 ms in Chromium (1000 ms before).
platform::open_database is the same on both sides: away from the screen,
done when read. The shelf's cards count the games the same way (one file
at a time); the counts are kept through platform::fs.

Chrome, Edge (File System Access): a chosen file's handle is kept in the
browser's storage; at the next visit the file is read again by itself when
the browser allows it, else the card offers «Allow reading» (the browser's
own question). Files of different visits get folders of their own.

Checks: t.choose(files, open) for both browsers (in Chromium the handles of
the site's private file system stand for the dialog's); app.mjs checks the
draft, the question on leaving and the remembered file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
vrubelroman 2026-10-30 17:50:54 +03:00
parent 000821887f
commit f694bc768b
31 changed files with 1048 additions and 190 deletions

View file

@ -6,10 +6,11 @@
// const b = await open(port); const t = await b.tab(1400, 860, log);
// t.navigate(url) · t.evaluate(js) · t.waitFor(js, ms) · t.screenshot()
// t.click(x, y, {double, right}) · t.key(key, mods, times) · t.setFiles(inputId, paths)
// t.choose(paths, open): the user chooses the files in the dialog open() brings up
import { spawn } from 'node:child_process';
import { mkdirSync, mkdtempSync, rmSync } from 'node:fs';
import { mkdirSync, mkdtempSync, readFileSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { basename, join } from 'node:path';
import { launch as launchFirefox } from './firefox.mjs';
import { sleep, tab as bidiTab } from './bidi.mjs';
@ -85,6 +86,12 @@ async function firefox(port) {
});
await ff.browser.send('input.setFiles', { context: t.context, element: { sharedId: node.result.sharedId }, files });
},
// The user chooses `files` in the dialog `open()` brings up (the page's file input).
async choose(files, open) {
await open();
await this.waitFor(`!!document.getElementById('cp-open-files')`, 5000);
await this.setFiles('cp-open-files', files);
},
};
},
};
@ -207,6 +214,36 @@ async function chromium(port) {
const { result } = await s('Runtime.evaluate', { expression: `document.getElementById(${JSON.stringify(id)})` });
await s('DOM.setFileInputFiles', { objectId: result.objectId, files });
},
// The user chooses `files` in the dialog `open()` brings up. Chrome's own dialog
// (showOpenFilePicker, the files remembered) cannot be driven headless: the files
// are put into the site's private file system and its handles are what the
// dialog gives — the same kind of handle, kept and read again the same way.
async choose(files, open) {
const picker = await this.evaluate(`typeof window.showOpenFilePicker === 'function'`);
if (!picker) {
await open();
await this.waitFor(`!!document.getElementById('cp-open-files')`, 5000);
return this.setFiles('cp-open-files', files);
}
const given = files.map((f) => ({ name: basename(f), b64: readFileSync(f).toString('base64') }));
await this.evaluate(`(() => {
const given = ${JSON.stringify(given)};
window.showOpenFilePicker = async () => {
const root = await navigator.storage.getDirectory();
const handles = [];
for (const g of given) {
const h = await root.getFileHandle(g.name, { create: true });
const w = await h.createWritable();
await w.write(Uint8Array.from(atob(g.b64), (c) => c.charCodeAt(0)));
await w.close();
handles.push(h);
}
return handles;
};
return true;
})()`);
await open();
},
};
},
};