On WebAssembly the engine is a Web Worker: a line of UCI is one message each way, the protocol machine is driven by the worker's messages and by timers set for its deadlines; the handshake does not block (Engine::start_worker gives a Starting, polled until readyok). EngineHandle has the same methods as the process one, minus those that block or name a process. Option and search checks are shared by both transports. tests/worker.rs runs the worker transport against a fake engine in JavaScript in headless Firefox (apps/web/check/uci-worker.mjs: wasm-bindgen-test-runner serves, a WebDriver BiDi client reads the outcome): handshake, analysis and stop, coalescing of rapid requests without mixing output, an engine hung on stop, a crash, and every handshake failure. 5 of 5 pass. game-review: the job in engine processes (run and its workers) moves to job/engines.rs, built where there are processes. .cargo/config.toml: the getrandom backend of WebAssembly. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
78 lines
2.8 KiB
JavaScript
78 lines
2.8 KiB
JavaScript
// A minimal WebDriver BiDi client for headless Firefox (Node 22+: global WebSocket).
|
||
// Used by the web checks (docs/dev/web.md, «Проверки»); no windows are opened.
|
||
|
||
export async function connect(port) {
|
||
const ws = new WebSocket(`ws://127.0.0.1:${port}/session`);
|
||
let id = 0;
|
||
const pending = new Map();
|
||
const listeners = [];
|
||
ws.onmessage = (m) => {
|
||
const d = JSON.parse(m.data);
|
||
if (d.id && pending.has(d.id)) {
|
||
const p = pending.get(d.id);
|
||
pending.delete(d.id);
|
||
if (d.type === 'error') p.rej(new Error(`${d.error}: ${d.message}`));
|
||
else p.res(d.result);
|
||
} else if (d.method) {
|
||
for (const l of listeners) l(d.method, d.params);
|
||
}
|
||
};
|
||
await new Promise((res, rej) => {
|
||
ws.onopen = res;
|
||
ws.onerror = () => rej(new Error('cannot connect to Firefox'));
|
||
});
|
||
const send = (method, params) =>
|
||
new Promise((res, rej) => {
|
||
const i = ++id;
|
||
pending.set(i, { res, rej });
|
||
ws.send(JSON.stringify({ id: i, method, params }));
|
||
});
|
||
await send('session.new', { capabilities: {} });
|
||
return {
|
||
send,
|
||
on: (f) => listeners.push(f),
|
||
close: async () => {
|
||
await send('session.end', {}).catch(() => {});
|
||
ws.close();
|
||
},
|
||
};
|
||
}
|
||
|
||
export const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
||
|
||
// A tab of `width`×`height` CSS pixels with the console forwarded to `log`.
|
||
export async function tab(b, width, height, log = console.log) {
|
||
await b.send('session.subscribe', { events: ['log.entryAdded'] });
|
||
b.on((method, p) => {
|
||
if (method === 'log.entryAdded') log(`[${p.level}] ${p.text}`);
|
||
});
|
||
const { context } = await b.send('browsingContext.create', { type: 'tab' });
|
||
await b.send('browsingContext.setViewport', { context, viewport: { width, height } });
|
||
const evaluate = async (expression) => {
|
||
const r = await b.send('script.evaluate', {
|
||
expression,
|
||
target: { context },
|
||
awaitPromise: true,
|
||
});
|
||
if (r.type === 'exception') throw new Error(r.exceptionDetails?.text || 'script failed');
|
||
return r.result?.value;
|
||
};
|
||
return {
|
||
context,
|
||
evaluate,
|
||
navigate: (url) => b.send('browsingContext.navigate', { context, url, wait: 'complete' }),
|
||
screenshot: async () =>
|
||
Buffer.from((await b.send('browsingContext.captureScreenshot', { context })).data, 'base64'),
|
||
actions: (actions) => b.send('input.performActions', { context, actions }),
|
||
// Waits until `expression` is truthy (polled), or throws after `ms`.
|
||
waitFor: async (expression, ms = 10000) => {
|
||
const end = Date.now() + ms;
|
||
for (;;) {
|
||
const v = await evaluate(expression).catch(() => undefined);
|
||
if (v) return v;
|
||
if (Date.now() > end) throw new Error(`timed out waiting for ${expression}`);
|
||
await sleep(100);
|
||
}
|
||
},
|
||
};
|
||
}
|