Repository Public
1#!/usr/bin/env node2// Author: Simon-Pierre Boucher — contact@spboucher.ai3// File: Scripts/dev-fake-apns.mjs4// Description: A fake APNs provider endpoint for LOCAL development (cleartext HTTP/2 on 127.0.0.1), same idea as the one in5// sp-mail/tests/integration/next-push.test.ts: it answers 200 to every push, appends each request (device token, apns-* headers, payload) to6// Build/fake-apns.jsonl, and creates a throwaway P-256 key (Build/fake-apns-key.p8) for the backend's provider token. Nothing ever leaves the Mac.7//8// Usage: node Scripts/dev-fake-apns.mjs (SPMAIL_FAKE_APNS_PORT, default 8857)9// The dev backend is pointed at it with SPMAIL_APNS_HOST_OVERRIDE=http://127.0.0.1:<port> (see Scripts/dev-backend.sh).10import crypto from "node:crypto"11import fs from "node:fs"12import http2 from "node:http2"13import path from "node:path"14import { fileURLToPath } from "node:url"1516const OUT = path.join(path.dirname(fileURLToPath(import.meta.url)), "..", "Build")17const PORT = Number(process.env.SPMAIL_FAKE_APNS_PORT ?? 8857)18const LOG = path.join(OUT, "fake-apns.jsonl"), KEY = path.join(OUT, "fake-apns-key.p8")19fs.mkdirSync(OUT, { recursive: true })20if (!fs.existsSync(KEY)) {21 const { privateKey } = crypto.generateKeyPairSync("ec", { namedCurve: "P-256" })22 fs.writeFileSync(KEY, privateKey.export({ type: "pkcs8", format: "pem" }), { mode: 0o600 })23}24const srv = http2.createServer()25srv.on("stream", (stream, headers) => {26 let body = ""27 stream.on("data", (d) => (body += d))28 stream.on("end", () => {29 let payload = null30 try { payload = JSON.parse(body) } catch { /* recorded as null */ }31 const apns = Object.fromEntries(Object.entries(headers).filter(([k]) => k.startsWith("apns-")))32 fs.appendFileSync(LOG, JSON.stringify({ at: new Date().toISOString(), token: String(headers[":path"]).replace("/3/device/", ""), headers: apns, payload }) + "\n")33 stream.respond({ ":status": 200, "apns-id": crypto.randomUUID() })34 stream.end("")35 })36})37srv.listen(PORT, "127.0.0.1", () => console.log(`fake APNs on http://127.0.0.1:${PORT} — log ${LOG}`))38