Chapter 2
Getting started
Prerequisites#
- Node.js ≥ 22.6. The server and tests run TypeScript directly with
--experimental-strip-types. - A modern browser: Chrome/Edge 110+, Firefox 115+, Safari 16.4+ (Canvas 2D and Web Crypto; no WebGL needed).
Install and run#
npm install
npm run dev # http://localhost:8787 (Vite with hot reload + API)
Scripts#
| Script | What it does |
|---|---|
npm run dev |
Dev server: Vite middleware + API on one port, hot module reload |
npm run build |
Typecheck, then production build into dist/ |
npm run start |
Serve dist/ + API in production mode |
npm run serve |
build + start |
npm test |
Unit tests for the security core (Node's built-in test runner) |
npm run test:pfe |
Cross-language encryption conformance test |
npm run typecheck |
tsc --noEmit across app, library, server, tests |
npm run build:lib |
Library bundles for other apps (dist-lib/, see §23) |
Environment variables#
| Variable | Default | Purpose |
|---|---|---|
PORT |
8787 |
Listen port |
HOST |
127.0.0.1 |
Bind address. Use 0.0.0.0 only for device testing on a trusted network. |
EXTRA_ALLOWED_HOSTS |
unset | Extra Host header values to accept, comma-separated (tunnels, LAN IPs) |
SESSION_SECRET |
random per start | HMAC key for CSRF tokens. Set it in production so tokens survive restarts and match across instances. |
HTTPS |
unset | 1 adds Secure to the session cookie (when behind a TLS proxy) |
PFE_KEY_FILE |
.keys/pfe-current.pem |
P-256 private key for encrypted password fields |
PFE_RETIRED_KEY_FILES |
unset | Old keys still accepted during rotation |
PFE_REQUIRE_KEY |
unset | 1 refuses to start without PFE_KEY_FILE |
PASSWORD_PEPPER |
.keys/pepper |
Base64 HMAC key mixed into passwords before hashing |
OTP_DEV_LOG |
on in dev | Print one-time codes to the console (never enable in real production) |
NODE_ENV |
unset | production serves dist/ and tightens the CSP |
In development, .keys/ is created automatically with owner-only permissions and is git-ignored.
Project layout#
index.html Page shell (CSP is added by the server and by the build)
public/style.css Minimal page CSS (full-bleed canvas, no text selection)
shared/rules.ts Field rules + validators shared by browser and server
server/
server.ts HTTP server: API, sessions, CSRF, CSP, static files
pfe.ts PFE1 decryption + key ring (Node)
decrypt/<language>/ PFE1 decryptors for other back ends
src/
lib/ The library (import from 'src/lib')
index.ts Public API
theme.ts Tokens, light/dark themes, createTheme, themeFromSeed
core/ FormUI, FocusManager, KeyboardProxy, Control base, icons
controls/ All controls
security/ SecureBuffer, Secret, sanitize, formatters, envelope, ...
Form.ts layout.ts widgets.ts rules.ts
demo/main.ts Sample registration form
examples/StarRating.ts Reference custom control
examples/SearchInput.ts Reference subclass of a built-in control
scripts/pfe-test.ts Cross-language conformance runner
tests/ Unit tests
docs/ This guide and the encryption spec