feat: serve the BYOS device API

The panel now pairs, fetches its image and files its logs against this
application rather than against the TRMNL cloud.

Four endpoints: /api/setup issues a token on first contact,
/api/display hands back an image and a wake interval, /api/log stores
firmware diagnostics, and /api/device/image/<hash> serves the bytes.

The wake interval is where freshness and battery are traded off. In BYOS
nothing can be pushed: the device sleeps, wakes, asks and sleeps again.
So the interval is short while the shop trades and long overnight, and
it is shortened further whenever a change of state falls inside it —
the door opening in twenty minutes means waking in twenty-one,
whatever the base interval says.

The image filename is the hash of its own bytes. The firmware skips the
redraw when the name is unchanged, which is the whole battery strategy,
and the URL is immutable, unguessable and safe to cache forever. Two
integration tests pin this: unchanged data must yield the same filename
and store one row, changed hours must yield a different one.

MAC addresses are normalised before use. They are a primary key here,
and firmwares are inconsistent about case and separators; without this a
panel could register twice by capitalising itself differently. Header
names are read in both the hyphen and underscore spellings for the same
reason — the TRMNL docs and the Seeed sources disagree, and being
liberal costs nothing while being wrong costs a blank shop window.

Pairing is deliberately made to survive a rendering failure. The token
is issued once and only its digest is kept, so a device stranded by a
failed response would be registered yet hold no credential, and unable
to register again. The welcome image is worth far less than that. This
was found by running the flow, not by reading it.

satori, yoga and harfbuzz are marked external: bundling rewrites the
relative path satori uses to load its WebAssembly, and the renderer dies
on a missing hb.wasm.

The integration tests run against a real Postgres, in CI too. Mocking
Prisma here would only prove the mock works.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012cSY9pVhZmJUKNN7wf1Myd
This commit is contained in:
2026-09-20 18:00:50 +02:00
co-authored by Claude Opus 5
parent fccccbd118
commit dd4d1b97c8
25 changed files with 1500 additions and 4 deletions
+84
View File
@@ -0,0 +1,84 @@
import { describe, expect, it } from 'vitest';
import {
generateDeviceToken,
generateFriendlyId,
hashToken,
normaliseMac,
tokenMatches,
} from './auth';
describe('generateDeviceToken', () => {
it('is URL-safe and long enough to be worth nothing to a guesser', () => {
const token = generateDeviceToken();
expect(token).toMatch(/^[A-Za-z0-9_-]+$/);
expect(token.length).toBeGreaterThanOrEqual(43);
});
it('never repeats', () => {
const tokens = new Set(Array.from({ length: 50 }, generateDeviceToken));
expect(tokens.size).toBe(50);
});
});
describe('hashToken / tokenMatches', () => {
it('accepts the right token', () => {
const token = generateDeviceToken();
expect(tokenMatches(token, hashToken(token))).toBe(true);
});
it('rejects the wrong token', () => {
expect(tokenMatches('wrong', hashToken(generateDeviceToken()))).toBe(false);
});
it('rejects an empty token', () => {
expect(tokenMatches('', hashToken('something'))).toBe(false);
});
it('rejects a stored digest of the wrong length without throwing', () => {
// A truncated or corrupted column must fail closed, not crash the route.
expect(tokenMatches('token', 'abcd')).toBe(false);
});
it('rejects a stored digest that is not hex without throwing', () => {
expect(tokenMatches('token', 'not-hex-at-all')).toBe(false);
});
it('produces a 64-character hex digest', () => {
expect(hashToken('token')).toMatch(/^[0-9a-f]{64}$/);
});
});
describe('generateFriendlyId', () => {
it('uses only characters that survive being read aloud', () => {
for (let attempt = 0; attempt < 50; attempt += 1) {
expect(generateFriendlyId()).toMatch(/^[ABCDEFGHJKLMNPQRSTUVWXYZ23456789]{6}$/);
}
});
it('honours a requested length', () => {
expect(generateFriendlyId(10)).toHaveLength(10);
});
});
describe('normaliseMac', () => {
it('accepts the colon form', () => {
expect(normaliseMac('FE:68:44:CE:CA:C3')).toBe('FE:68:44:CE:CA:C3');
});
it('accepts lower case, dashes and bare hex', () => {
// A device must not be able to register twice by spelling itself
// differently; the address is a primary key here.
expect(normaliseMac('fe:68:44:ce:ca:c3')).toBe('FE:68:44:CE:CA:C3');
expect(normaliseMac('fe-68-44-ce-ca-c3')).toBe('FE:68:44:CE:CA:C3');
expect(normaliseMac('fe6844ceCAc3')).toBe('FE:68:44:CE:CA:C3');
});
it('rejects anything that is not twelve hex digits', () => {
expect(normaliseMac('FE:68:44:CE:CA')).toBeNull();
expect(normaliseMac('not a mac')).toBeNull();
expect(normaliseMac('')).toBeNull();
expect(normaliseMac(null)).toBeNull();
expect(normaliseMac(undefined)).toBeNull();
});
});