Files
final-wishes/OVERVIEW.md
2026-11-23 21:41:02 -05:00

112 lines
11 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Final Wishes — Overview
> An encrypted, printable "in the event of my death or incapacity" guide that lives on a thumbdrive as a single self-contained HTML file, so a non-technical executor can open it on any PC or Mac with one passphrase and print it.
## Purpose
The owner keeps accounts, documents, contacts, wind-down steps and similar life-admin information in one document that is encrypted at rest. It began as an 8-page 2022 PDF with plaintext secrets (`abi-wishes.pdf`, never committed). The executor double-clicks `final-wishes.html`, enters a passphrase, reads the guide in the browser and prints it. The owner edits the same file and saves it back to the drive. There is no server, no install and no network access.
## Tech Stack
| Layer | Technology |
|-------|-----------|
| Language / runtime | Vanilla JavaScript (ES modules in source). Browser for the app, Node 20+ for build and tests. |
| UI | Hand-written DOM code (`h()` helper, `textContent` only, no `innerHTML`), plain CSS with light/dark themes |
| Crypto | WebCrypto: PBKDF2-SHA256 (600,000 iterations) into AES-256-GCM |
| Build | `build.js`: concatenates `src/*.js`, no bundler, no dependencies |
| Tests | `node --test` (built-in runner). Browser behavior was checked by hand with headless Chromium over CDP, which is not part of the repo. |
| Hosting / issues | Gitea (`gitea.ericthered.tech`), `gt` CLI. Issues #1–#19 track the work. |
There are no npm dependencies and no CDN or network calls at runtime.
## Directory Structure
```
build.js builds dist/final-wishes.html (optionally encrypting a JSON file into it)
bump-version.js `npm run bump`, and the pre-commit hook's engine: advances src/version.js to today's date + next letter
.githooks/pre-commit runs `bump-version.js --staged`; enable once per clone with `npm run hooks`
package.json scripts: build, seal, test
src/
crypto.js encrypt / decrypt / passphrase rules (blob format below)
model.js document schema, validate(), section/record/field/sequence kinds
ops.js pure edit helpers: add/move/remove, header, images, sequences, sub-tabs,
revert-to-saved (Cancel), delete-section message, clearSecrets
print.js pure print helpers: secret masking, header layout, frame rules, secret count
images.js image helpers (PNG/JPEG sniffing, sizing, data URLs) + browser import pipeline
template.js assemble(): the HTML skeleton shared by build.js and in-page Save
version.js APP_VERSION ("Ver YYMMDDX" in the top bar) and bump helpers
app.js the whole UI (start/unlock, view, edit, print, save)
style.css screen styles, visual-sequence styles, @media print rules
test/ one test file per src module, plus a build test
docs/
MAINTENANCE.md owner's guide (updating, images, sequences, upgrading, backups, security clean-up)
thumbdrive/README.txt executor's one-page instructions (copied to the drive)
PLAN.md original plan
```
Git-ignored on purpose: `*.pdf`, `data/` (plaintext working copies with real secrets), `dist/`, `node_modules/`.
## Architecture
**One file.** `build.js` concatenates the modules (`crypto, images, model, ops, print, template, version`), strips `^import ` lines and `^export ` keywords, appends `app.js`, and drops the result into the skeleton from `src/template.js`. All modules therefore share one global scope in the built file, so names must not collide across modules. The skeleton holds `<style id="fw-css">`, `<script id="payload" type="application/octet-stream">` (the encrypted blob), `<script id="fw-js">` and an empty `<div id="printdoc">`. The `</script>` inside the template literal is written `<\/script>`.
**Encrypted payload.** Blob layout: `"FW"` 0x01 | iterations (u32 big-endian) | salt (16) | IV (12) | ciphertext + GCM tag, with the header bound as additional authenticated data. The plaintext is the JSON document. The passphrase minimum is 12 characters. The passphrase is kept in memory only; the app never uses `localStorage`.
**Document model** (`src/model.js`, `schemaVersion 1`): `meta {owner, title, lastUpdated}`; optional `header {fields}` for the running print header; `sections`. Section kinds:
- `text` (a `body`, plus optional `sequences`)
- `records` (records with labeled `fields`; a field is text or an image). It can optionally have sub-tabs: `subsections: [{id, title}]`, a `generalTitle` for the catch-all first tab (default "General"), and a `subsection` id on each record or sequence. No `subsection`, or one that matches no tab, means General.
- `steps` and `checklist` (ordered `items`)
A **visual sequence** (`kind: 'sequence'`, numbered `frames` of image + text with a layout of right / under / none) is an entry in the same ordered list as a section's records or items, or in a text section's `sequences` array. It is not a record or an item: no step number and no tick box. Ids are unique across the whole document. `secret` flags exist on fields and frames. Validation is `validate()` / `assertValid()`; the save path refuses an invalid document.
**UI** (`src/app.js`, a single IIFE): state lives in one `state` object (including `collapsed`, the folded record ids). Edits mutate `state.doc` in place and then call `markDirty()` or `changed(record)` (which also stamps `updated` and `meta.lastUpdated`). `render()` rebuilds the page. `changed` clears `state.status`, so set the status afterwards.
- **Editing and Cancel:** edit mode is global. **Cancel** reverts the section (or the print header) on screen to a deep-copy baseline taken at open and at each save, then leaves edit mode.
- **Collapsing records:** in view mode each record card has a Collapse/Expand button, and records sections have "Collapse all" / "Expand all". Folded ids live in `state.collapsed` (a `Set`, view-only, never saved, cleared on reload). A collapsed card shows only its title and gets ↑/↓ buttons that reorder it via `move()` and `changed()`, so moving stamps `updated` and marks the document dirty.
- **Sub-tabs:** `ops.js` holds the pure logic (`tabsOf`, `entriesInTab`, `moveToTab`, `moveWithin`, `removeSubsection`, `printGroups`). The records stay in one array, and the tab bar filters them. The shown tab is in `state.tabs` (not saved). Up/down moves a record past its nearest neighbour in the same tab. Deleting a tab sends its records to General. Print writes one sub-heading per non-empty tab.
- **Images:** drag and drop, clipboard paste and file picker all feed `importFiles()`. Files are sniffed by their first bytes, downscaled to at most 1600 px and re-encoded, which strips metadata, and stored as data URLs with a limit of about 3 MB each.
- **Reset (`?reset=1`):** opening the file with `?reset=1` or `#reset=1` shows a banner after unlock offering to erase every secret-flagged field value and secret frame (`clearSecrets()` in `ops.js`, count from `countSecrets`). Labels, structure and flags stay. It is read live (`resetRequested()`), with a `hashchange` listener to re-offer, and `state.resetOffered` dismisses it. Nothing is written until Save, so make a Backup copy first. It's meant for working on layout on a less safe machine.
**Save.** Uses the File System Access API (`showSaveFilePicker`, handle reused) where available, otherwise a Blob download. `assemble()` rebuilds the full HTML from the page's own CSS and JS plus a fresh encrypted payload. "Backup copy" downloads `final-wishes-YYYYMMDD.html`.
**Upgrading app code, keeping data.** A file runs the code it was saved with. On the start screen, "Open your existing Final Wishes file" extracts the `#payload` from an older `.html`, unlocks it with the passphrase, and Save then writes it out with the newer code.
**Print.** Built only when Print is chosen and cleared on `afterprint`, so secrets don't linger in the DOM. It is a `<table>` whose `<thead>` holds the running header, which the browser repeats at the top of every page. Each section starts a new page (a section with sub-tabs gets a sub-heading per non-empty tab), records and sequence steps avoid page breaks inside them, and checklist items have tick boxes. The print dialog offers "withhold secrets" (default) or "include secrets". Images are decoded before `window.print()`.
## Integrations
None at runtime. Development uses the Gitea remote and the `gt` CLI for issues; `PLAN.md` and `docs/MAINTENANCE.md` describe the process.
## Database & Data Layer
No database. The document is a JSON object, encrypted and embedded in the HTML file. The plaintext working copy `data/seed.json` is git-ignored, was used only to seal the first build, and has been deleted.
## Connectivity & Configuration
| Item | Purpose |
|------|---------|
| `FW_PASSPHRASE` (env) | Passphrase for `build.js`; otherwise it is prompted for hidden and entered twice. It is never accepted as a command-line argument. |
| `node build.js --data FILE --out PATH` | Seal a JSON file into the built HTML. Without `--data` the app starts empty and can open an older file or a JSON file. |
| `npm run build` / `seal` / `test` / `bump` / `hooks` | Build the app only / build with `data/seed.json` / run the tests / advance the app version by hand / enable the pre-commit hook that does it automatically |
## Key Entry Points
- `build.js`: how the single file is assembled
- `src/template.js`: the output skeleton
- `src/app.js`: the start screen (`renderStart`), then `render()`, `save()`, `buildPrintDoc()`
- `src/model.js`: the data contract; read this first before changing stored data
- `src/crypto.js`: the encrypted format
- `docs/MAINTENANCE.md`: how the owner uses and updates it
## Notes & Gotchas
- **Bundle scope:** all modules share one scope after the build. Don't add a top-level name that already exists elsewhere, and don't use `import`/`export` forms other than line-start `import ` and `export `, because the stripper is regex-based.
- **`h()` children:** it flattens with `flat(Infinity)`, but `replaceChildren` needs an explicit `.flat()`. Use `textContent`, never `innerHTML`.
- **Dirty and status:** `markDirty()` clears the status text, so set `state.status` after calling `changed()`.
- **Older builds:** files saved with images, visual sequences or sub-tabs can't be opened by builds that predate them. The upgrade path only goes forward.
- **Blob size:** images are embedded in the encrypted payload and make the file bigger and saves slower.
- **Skipped test:** one test in `test/model.test.js` is skipped unless the gitignored `data/seed.json` exists.
- **Browser coverage:** features were verified in headless Chromium only; Firefox and Safari drop and paste are untested.
- **Open work:** issue #8 (security clean-up) remains open. It is the owner's own tasks: rotating exposed secrets, refreshing stale data and keeping a second encrypted copy.
- **Git history:** commit `9fa6096` contains a PIN that matched a real one from the source material. It was removed from the code in `7a81637` but not from history, so treat it as exposed.