import { encrypt, decrypt, CAPTURE_MAGIC } from './crypto.js'; import { isImageDataUrl } from './images.js'; // Capture bundles (issue #28): walkthroughs captured in the separate, online capture.html // and carried to the offline main app. A bundle is JSON sealed like the main document // (PBKDF2 -> AES-GCM) but with the "FC" magic bytes, so the two can never be mixed up. // // { format: 'fw-capture', version: 1, created: 'YYYY-MM-DD', // walkthroughs: [{ title, captured: 'YYYY-MM-DD', frames: [{ image: }] }] } // // Pure functions, no DOM. build.js strips `export` / `import` lines. export const BUNDLE_FORMAT = 'fw-capture'; export const BUNDLE_VERSION = 1; export const MAX_WALKTHROUGHS = 100; export const MAX_FRAMES = 200; const BUNDLE_DATE_RE = /^\d{4}-\d{2}-\d{2}$/; export function newWalkthrough(title = '', date = new Date().toISOString().slice(0, 10)) { return { title, captured: date, frames: [] }; } // Problems with a bundle's contents, as strings; [] when it is fine to import. export function validateBundle(b) { const errs = []; if (!b || typeof b !== 'object' || b.format !== BUNDLE_FORMAT) return ['This is not a capture bundle.']; if (b.version !== BUNDLE_VERSION) return ['This bundle is from a different version of the capture tool (' + b.version + ').']; if (!Array.isArray(b.walkthroughs) || b.walkthroughs.length > MAX_WALKTHROUGHS) return ['The bundle has no walkthroughs list.']; b.walkthroughs.forEach((w, i) => { const where = 'Walkthrough ' + (i + 1); if (!w || typeof w !== 'object') return errs.push(where + ' is not valid.'); if (typeof w.title !== 'string') errs.push(where + ' has no title.'); if (typeof w.captured !== 'string' || !BUNDLE_DATE_RE.test(w.captured)) errs.push(where + ' has no capture date.'); if (!Array.isArray(w.frames) || w.frames.length > MAX_FRAMES) return errs.push(where + ' has no steps list.'); w.frames.forEach((f, j) => { if (!f || !isImageDataUrl(f.image)) errs.push(where + ', step ' + (j + 1) + ' has no valid picture.'); }); }); return errs; } // Walkthroughs with no steps are left out: there is nothing to carry over. export async function sealBundle(walkthroughs, passphrase, iterations, date = new Date().toISOString().slice(0, 10)) { const bundle = { format: BUNDLE_FORMAT, version: BUNDLE_VERSION, created: date, walkthroughs: walkthroughs.filter((w) => w.frames.length) }; const errs = validateBundle(bundle); if (errs.length) throw new Error(errs[0]); return encrypt(JSON.stringify(bundle), passphrase, iterations, CAPTURE_MAGIC); } // Returns the bundle; throws DecryptError (wrong passphrase / damaged / not a bundle) or Error (bad contents). // Nothing is returned unless the whole bundle is valid, so a merge is all or nothing. export async function openBundle(blob, passphrase) { const bundle = JSON.parse(await decrypt(blob, passphrase, CAPTURE_MAGIC)); const errs = validateBundle(bundle); if (errs.length) throw new Error(errs[0]); return bundle; } // ---- where a walkthrough can go in the document ---- // [{ key, sid, tab, label }]: every section a sequence can live in, with one entry per sub-tab. export function destinations(doc) { const out = []; for (const s of doc.sections) { out.push({ key: s.id + '|', sid: s.id, tab: '', label: s.title }); if (s.kind === 'records') { for (const t of s.subsections || []) out.push({ key: s.id + '|' + t.id, sid: s.id, tab: t.id, label: s.title + ' \u203a ' + t.title }); } } return out; }