Files
wibosim/server/test/fakepush.js
T
db9979andClaude Opus 5.5 0c43810e42 WiBoSim als App: PWA mit Service Worker und Web Push (RFC 8291/8292)
- Service Worker (/sw.js, eigene Route mit no-cache und Service-Worker-Allowed), Offline-Seite,
  Manifest mit id, scope, start_url und Kurzbefehlen, iOS-Meta-Tags, Safe-Area im Standalone-Modus
- Web Push ohne neue Abhängigkeit: aes128gcm-Verschlüsselung und VAPID mit node:crypto,
  geprüft gegen die RFC-Testvektoren; VAPID-Schlüssel im Datenverzeichnis oder per Umgebung
- Abos je Spieler (bis 5 Geräte), Abo entfernen bei 404/410, Grenze je Stunde, Ruhezeiten,
  kein Push bei sichtbarem Tab, kurzer Inhalt mit Deep-Link; Versand über den Kanal „push“
- Einstellungen „Auf diesem Gerät“, iOS-Hinweis „Zum Home-Bildschirm“, Installationsknopf,
  App-Badge; Admin-Karte unter Betrieb; Datenschutz, Hilfe, Glossar und BETRIEB.md ergänzt
- Tests: RFC-Vektoren, Server-Modul, PWA-Auslieferung und Service Worker; lokaler Prüf-Push-Dienst

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018i4GUhQVoPUusUzxgxLWkK
2026-12-05 12:58:52 +00:00

51 lines
2.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Lokaler Prüf-Push-Dienst (Team App, nur Tests): nimmt Pushes wie Apple/Google an, prüft das VAPID-JWT und
// entschlüsselt die Nachricht mit den Schlüsseln eines simulierten Geräts – so ist die Verschlüsselung Ende zu Ende geprüft.
// const fp = await fakePushService(); const dev = fp.device(); // dev.subscription → POST /api/push/subscribe
// … fp.messages → [{id, json, claims, headers}] ; fp.status.set(id, 410) → Antwort für dieses Gerät
import http from 'node:http';
import crypto from 'node:crypto';
import { decrypt, verifyVapid, newKeyPair, b64u } from '../src/webpushcrypto.js';
export async function fakePushService({ port = 0 } = {}) {
const devices = new Map(); // id → {privateKey, publicKey, auth}
const messages = [];
const status = new Map(); // id → HTTP-Status (Vorgabe 201)
const waiters = [];
const server = http.createServer((req, res) => {
const chunks = [];
req.on('data', (c) => chunks.push(c));
req.on('end', () => {
const id = decodeURIComponent(req.url.replace(/^\/push\//, ''));
const code = status.get(id) ?? (devices.has(id) ? 201 : 404);
if (code >= 300) { res.writeHead(code).end(); return; }
try {
const v = verifyVapid(req.headers.authorization);
const plain = decrypt(Buffer.concat(chunks), devices.get(id));
const m = { id, json: JSON.parse(plain.toString('utf8')), claims: v.claims, k: v.k, headers: req.headers };
messages.push(m);
for (const w of waiters.splice(0)) w(m);
res.writeHead(201).end();
} catch (e) {
messages.push({ id, error: e.message });
res.writeHead(400).end(e.message);
}
});
});
await new Promise((r) => server.listen(port, '127.0.0.1', r));
const host = `127.0.0.1:${server.address().port}`;
return {
host,
messages,
status,
/** simuliertes Gerät (Browser) mit eigenem Schlüsselpaar und Geheimnis */
device(id = crypto.randomBytes(6).toString('hex')) {
const k = newKeyPair();
const auth = crypto.randomBytes(16);
devices.set(id, { ...k, auth });
return { id, endpoint: `http://${host}/push/${id}`, subscription: { endpoint: `http://${host}/push/${id}`, keys: { p256dh: b64u(k.publicKey), auth: b64u(auth) } } };
},
next: () => new Promise((r) => waiters.push(r)),
close: () => new Promise((r) => server.close(r)),
};
}