Skip to content

Black Friday

Annual Pro is $90 for the first year, normally $120. Or start monthly at $6 for two months, then $12.

Ends Dec 4, 11:59 PM PT.

Create an account
← 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.

FollowForkOpen app

Files

setup-push.ts

17 lines · 1.0 KB · TypeScript
/**
 * Set the public half of the VAPID key. First save its private half as a Kody
 * signing credential (provider web-push), then map the webPush slot to it.
 * This export never takes or returns a private key.
 */
import { b64urlDecode } from './push-crypto.ts'
import { getPushPublicKey, setPushPublicKey } from './store.ts'

export default async function setupPush(params: { publicKey?: string } = {}) {
	if (!params.publicKey) return { ok: true, publicKey: await getPushPublicKey(), message: 'Save a VAPID signing credential outside package code, map webPush, then pass its publicKey here.' }
	const key = String(params.publicKey).trim()
	let bytes: Uint8Array
	try { bytes = b64urlDecode(key) } catch { bytes = new Uint8Array() }
	if (bytes.length !== 65 || bytes[0] !== 4) return { ok: false, publicKey: '', message: 'That is not a P-256 public key (65 bytes, base64url).' }
	await setPushPublicKey(key)
	return { ok: true, publicKey: key, message: 'Saved the public key. Notifications turn on once webPush is mapped.' }
}