1// Writes app/crafted-link.js with an arewehuman replay link for recipe.md.
2// The link carries the whole recording in its URL fragment (gzip + base64url),
3// which the viewer accepts as-is, so nothing needs to be hosted.
4// Until recipe.md has been recorded, the link is null and stays hidden.
5import { existsSync, readdirSync, readFileSync, writeFileSync } from 'node:fs';
6import { gzipSync } from 'node:zlib';
8const dir = '.arewehuman/recipe.md';
9const logs = existsSync(dir) ? readdirSync(dir).filter((f) => f.endsWith('.awh.jsonl')) : [];
10if (logs.length > 1) {
11 throw new Error(`Expected one recording in ${dir}, found ${logs.length}`);
12}
14let url = null;
15if (logs.length === 1) {
16 const recording = readFileSync(`${dir}/${logs[0]}`);
17 const z = gzipSync(recording, { level: 9 }).toString('base64url');
18 url = `https://magland.github.io/arewehuman/#/view?z=${z}`;
19}
21writeFileSync('app/crafted-link.js', `// Generated by scripts/crafted-link.mjs; do not edit.\nwindow.CRAFTED_LINK = ${JSON.stringify(url)};\n`);
22console.log(url ? `app/crafted-link.js: replay link for ${logs[0]} (${url.length} characters)` : `app/crafted-link.js: no recording in ${dir} yet`);