← Public packages
@kentcdodds/pie-night
Pie night sign-ups: hosts start an event, guests add the pies they are bringing and watch the list fill in live. Installable, with optional push notifications.
Files
import assert from 'node:assert/strict'
import { registerHooks } from 'node:module'
import { test } from 'node:test'
const STUB = `
const store = globalThis.__pieStore ??= new Map();
export const packageStorage = () => ({ async get(k) { return store.get(k) ?? null }, async set(k, v) { store.set(k, v) } });
export const packageContext = { appBasePath: '/apps/pie-night/' };
export async function credential(key) { throw new Error('credential not mapped: ' + key) }
export const kody = {};
export async function broadcast() {}
`
registerHooks({
resolve(spec, ctx, next) {
if (spec === 'kody:runtime') return { url: `data:text/javascript,${encodeURIComponent(STUB)}`, shortCircuit: true }
return next(spec, ctx)
},
})
const { encryptPushPayload, b64urlEncode, b64urlDecode, hkdf, concat } = await import('../src/push-crypto.ts')
const { isAllowedEndpoint, parseSubscription } = await import('../src/push.ts')
const { renderOgPng, ogText } = await import('../src/og.ts')
const { decodePng } = await import('../src/raster.ts')
const { eventPageHtml, homePageHtml } = await import('../src/page.ts')
const { serviceWorkerJs } = await import('../src/sw.ts')
const { LIMITS, formatEventWhen } = await import('../src/pies.ts')
async function browserKeys() {
const pair = (await crypto.subtle.generateKey({ name: 'ECDH', namedCurve: 'P-256' }, true, ['deriveBits'])) as CryptoKeyPair
const pub = new Uint8Array(await crypto.subtle.exportKey('raw', pair.publicKey))
return { pair, p256dh: b64urlEncode(pub), auth: b64urlEncode(crypto.getRandomValues(new Uint8Array(16))) }
}
test('web push payloads decrypt in the browser (RFC 8291)', async () => {
const keys = await browserKeys()
const body = await encryptPushPayload(keys, new TextEncoder().encode('{"title":"pie"}'))
const salt = body.slice(0, 16)
const idlen = body[20]!
const asPublic = body.slice(21, 21 + idlen)
const asKey = await crypto.subtle.importKey('raw', asPublic, { name: 'ECDH', namedCurve: 'P-256' }, false, [])
const ecdh = new Uint8Array(await crypto.subtle.deriveBits({ name: 'ECDH', public: asKey }, keys.pair.privateKey, 256))
const enc = new TextEncoder()
const ikm = await hkdf(b64urlDecode(keys.auth), ecdh, concat(enc.encode('WebPush: info\0'), b64urlDecode(keys.p256dh), asPublic), 32)
const cek = await hkdf(salt, ikm, enc.encode('Content-Encoding: aes128gcm\0'), 16)
const nonce = await hkdf(salt, ikm, enc.encode('Content-Encoding: nonce\0'), 12)
const k = await crypto.subtle.importKey('raw', cek, 'AES-GCM', false, ['decrypt'])
const plain = new Uint8Array(await crypto.subtle.decrypt({ name: 'AES-GCM', iv: nonce }, k, body.slice(21 + idlen)))
assert.equal(new TextDecoder().decode(plain.slice(0, -1)), '{"title":"pie"}')
})
test('push subscriptions only go to known push services', async () => {
const keys = await browserKeys()
assert.equal(isAllowedEndpoint('https://fcm.googleapis.com/fcm/send/abc'), true)
assert.equal(isAllowedEndpoint('https://web.push.apple.com/abc'), true)
assert.equal(isAllowedEndpoint('https://wns2-by3p.notify.windows.com/w/?token=x'), true)
assert.equal(isAllowedEndpoint('https://evil.example.com/push'), false)
assert.equal(isAllowedEndpoint('http://fcm.googleapis.com/x'), false)
assert.equal(parseSubscription({ endpoint: 'https://evil.example.com/x', keys }).ok, false)
assert.equal(parseSubscription({ endpoint: 'https://fcm.googleapis.com/fcm/send/abc', keys: { p256dh: keys.p256dh, auth: keys.auth } }).ok, true)
})
test('share image renders a 1200x630 PNG with Satori and the pure JS rasterizer', async () => {
const png = await renderOgPng('test-1', { title: 'Smith Pie Night', day: 'Friday, Nov 27', time: '6 PM', place: 'At our house', theme: 'berry' })
assert.deepEqual([...png.slice(1, 4)], [80, 78, 71])
const img = await decodePng(png)
assert.equal(img.width, 1200)
assert.equal(img.height, 630)
assert.equal(ogText('Pies 🥧 tonight', 40), 'Pies tonight')
})
const shared = { appBase: '', assetBase: '/assets', site: 'https://pie.example.com', assetVersion: '7', limits: LIMITS as unknown as Record<string, number> }
const event = { slug: 'smith-pies', code: 'Smith Pies', title: 'Smith <Pies>', date: '2026-11-27', time: '18:00', place: '', blurb: 'Bring pie', theme: 'evergreen', version: 3, when: formatEventWhen({ date: '2026-11-27', time: '18:00' }) }
test('event page has per-event share tags, theme, manifest, and escapes text', () => {
const html = eventPageHtml({ ...shared, event, pies: [], mode: 'guest', visitor: null, isHost: false, loginUrl: '/-/auth/login', pushKey: '' })
assert.match(html, /og:image" content="https:\/\/pie\.example\.com\/e\/smith-pies\/og\.png\?v=3"/)
assert.match(html, /og:url" content="https:\/\/pie\.example\.com\/e\/smith-pies"/)
assert.match(html, /\/e\/smith-pies\/manifest\.webmanifest/)
assert.match(html, /data-theme="evergreen"/)
assert.match(html, /id="event-where-chip" hidden/)
assert.doesNotMatch(html, /<Pies>/)
assert.doesNotMatch(html, /undefined/)
const home = homePageHtml({ ...shared, visitor: null, mine: [], loginUrl: '/x' })
assert.match(home, /Start a pie night/)
})
test('service worker supports the Refresh flow and push clicks', () => {
const js = serviceWorkerJs({ appBase: '', assetBase: '/assets', version: '7' })
assert.match(js, /SKIP_WAITING/)
assert.match(js, /notificationclick/)
assert.match(js, /badge-96\.png/)
new Function(js)
})