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:
parent
000821887f
commit
f694bc768b
31 changed files with 1048 additions and 190 deletions
|
|
@ -36,7 +36,7 @@ if command -v wasm-opt >/dev/null 2>&1; then
|
|||
"$out/chesspenguin_web_bg.wasm" -o "$out/chesspenguin_web_bg.wasm"
|
||||
fi
|
||||
|
||||
cp "$here/page/device.js" "$here/page/page.js" "$out/"
|
||||
cp "$here/page/device.js" "$here/page/page.js" "$here/page/import-worker.js" "$out/"
|
||||
|
||||
# The engine: Stockfish.js 19.0.1 (npm `stockfish`, GPL-3.0), its single-threaded build,
|
||||
# checked against the registry's integrity (THIRD_PARTY_NOTICES.md). STOCKFISH_TGZ= points
|
||||
|
|
|
|||
|
|
@ -57,9 +57,7 @@ try {
|
|||
// Open a PGN by the button's shortcut: the browser's file chooser gets the file.
|
||||
const pgn = join(s.b.profile, 'Test games.pgn');
|
||||
copyFileSync(join(root, 'examples/alekhine.pgn'), pgn);
|
||||
await t.key('o', ['ctrl']);
|
||||
await t.waitFor(`!!document.getElementById('cp-open-files')`, 5000);
|
||||
await t.setFiles('cp-open-files', [pgn]);
|
||||
await t.choose([pgn], () => t.key('o', ['ctrl']));
|
||||
await sleep(3000);
|
||||
await shot('web-2-opened.png');
|
||||
|
||||
|
|
@ -109,11 +107,31 @@ try {
|
|||
await sleep(1000);
|
||||
await shot('web-8-e4.png');
|
||||
|
||||
// The settings and the shelf after a reload (the browser's storage).
|
||||
// An unsaved game: the browser asks before the page is left, and its draft is kept.
|
||||
check(await t.evaluate(`typeof window.onbeforeunload === 'function'`), 'unsaved: the browser asks before leaving');
|
||||
await sleep(1500);
|
||||
const draft = () => t.evaluate(`new Promise((ok) => {
|
||||
const q = indexedDB.open('chesspenguin');
|
||||
q.onsuccess = () => {
|
||||
const r = q.result.transaction('files').objectStore('files').get('/data/draft.json');
|
||||
r.onsuccess = () => ok(r.result ? JSON.parse(new TextDecoder().decode(r.result)).pgn : '');
|
||||
};
|
||||
q.onerror = () => ok('');
|
||||
})`);
|
||||
check(/1\. e4/.test(await draft()), 'the draft of the unsaved game is kept');
|
||||
|
||||
// The settings and the shelf after a reload (the browser's storage). The question on
|
||||
// leaving is answered «Leave».
|
||||
await t.evaluate(`(window.onbeforeunload = null, true)`);
|
||||
await t.navigate(`${s.base}?lang=en`);
|
||||
await started();
|
||||
await sleep(2500);
|
||||
await shot('web-9-reloaded.png');
|
||||
check(/1\. e4/.test(await draft()), 'the draft is there after the reload (offered over the library)');
|
||||
if (await t.evaluate(`typeof window.showOpenFilePicker === 'function'`)) {
|
||||
// Chrome, Edge: the file chosen before the reload is read again by itself.
|
||||
check(s.logs.some((l) => /remembered file read again: .*Test games\.pgn/.test(l)), 'the chosen file is remembered (File System Access)');
|
||||
}
|
||||
} finally {
|
||||
await s.stop();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
|
|
|
|||
|
|
@ -33,9 +33,7 @@ try {
|
|||
await s.run({ url: '?lang=en' }, outDir);
|
||||
await sleep(2000);
|
||||
// The games in one file, reviewed one by one.
|
||||
await s.run({ key: 'o', mods: ['ctrl'] }, outDir);
|
||||
await t.waitFor(`!!document.getElementById('cp-open-files')`, 5000);
|
||||
await t.setFiles('cp-open-files', [all]);
|
||||
await t.choose([all], () => s.run({ key: 'o', mods: ['ctrl'] }, outDir));
|
||||
await sleep(3000);
|
||||
await s.run({ shot: 'review-0-opened.png' }, outDir);
|
||||
for (let i = 0; i < games.length; i++) {
|
||||
|
|
|
|||
|
|
@ -57,9 +57,7 @@ try {
|
|||
copyFileSync(join(root, 'examples/alekhine.pgn'), a);
|
||||
copyFileSync(join(root, 'examples/classics.pgn'), b);
|
||||
for (const file of [b, a]) {
|
||||
await step({ key: 'o', mods: ['ctrl'] });
|
||||
await t.waitFor(`!!document.getElementById('cp-open-files')`, 5000);
|
||||
await t.setFiles('cp-open-files', [file]);
|
||||
await t.choose([file], () => step({ key: 'o', mods: ['ctrl'] }));
|
||||
await sleep(2500);
|
||||
}
|
||||
await step({ shot: 's2-5-opened.png' });
|
||||
|
|
|
|||
|
|
@ -19,7 +19,9 @@ export async function session(appDir, port = Number(process.env.CP_CHECK_PORT ||
|
|||
const server = spawn('python3', ['-I', join(root, 'apps/web/check/serve.py'), resolve(appDir), join(root, 'website/static'), String(port)], { stdio: 'ignore' });
|
||||
const b = await open(port + 457);
|
||||
const errors = [];
|
||||
const logs = [];
|
||||
const t = await b.tab(1400, 860, (line) => {
|
||||
logs.push(line);
|
||||
if (/panicked|RuntimeError|\[error\]/.test(line) && !/favicon|404/.test(line)) errors.push(line);
|
||||
if (process.env.CONSOLE) console.log(line);
|
||||
});
|
||||
|
|
@ -43,7 +45,7 @@ export async function session(appDir, port = Number(process.env.CP_CHECK_PORT ||
|
|||
if (step.shot) writeFileSync(join(outDir, step.shot), await t.screenshot());
|
||||
};
|
||||
return {
|
||||
t, b, base, errors, run,
|
||||
t, b, base, errors, logs, run,
|
||||
stop: async () => { await b.stop(); server.kill(); },
|
||||
};
|
||||
}
|
||||
|
|
|
|||
14
apps/web/page/import-worker.js
Normal file
14
apps/web/page/import-worker.js
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
// The page's reader: a second instance of the program that reads the databases the user
|
||||
// opens, so that the page does not stop while a large one is read
|
||||
// (apps/desktop/src/platform/web/import.rs). One file at a time, in order.
|
||||
import init, { read_database } from './chesspenguin_web.js';
|
||||
|
||||
const ready = init();
|
||||
|
||||
self.onmessage = async (e) => {
|
||||
await ready;
|
||||
const { id, name, bytes, count } = e.data;
|
||||
const answer = read_database(name, bytes, count);
|
||||
answer.id = id;
|
||||
self.postMessage(answer, answer.text ? [answer.text.buffer] : []);
|
||||
};
|
||||
|
|
@ -6,6 +6,13 @@
|
|||
use wasm_bindgen::JsCast;
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
/// The reader worker's work (`page/import-worker.js`): the database in the file `name`,
|
||||
/// read and indexed away from the page (or only its games counted).
|
||||
#[wasm_bindgen]
|
||||
pub fn read_database(name: String, bytes: Vec<u8>, count: bool) -> JsValue {
|
||||
chesspenguin_app::read_database(&name, bytes, count)
|
||||
}
|
||||
|
||||
/// Starts ChessPenguin in the page's canvas `canvas_id`. Resolves once the interface
|
||||
/// runs; an error says why it could not start (no WebGL, …).
|
||||
#[wasm_bindgen]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue