|
| 1 | +'use strict'; |
| 2 | +var __importDefault = |
| 3 | + (this && this.__importDefault) || |
| 4 | + function (mod) { |
| 5 | + return mod && mod.__esModule ? mod : { default: mod }; |
| 6 | + }; |
| 7 | +Object.defineProperty(exports, '__esModule', { value: true }); |
| 8 | +exports.startNotifierApiServer = startNotifierApiServer; |
| 9 | +const http_1 = __importDefault(require('http')); |
| 10 | +const store_1 = require('./store'); |
| 11 | +function parseBody(body) { |
| 12 | + try { |
| 13 | + return JSON.parse(body); |
| 14 | + } catch { |
| 15 | + return {}; |
| 16 | + } |
| 17 | +} |
| 18 | +function startNotifierApiServer(config) { |
| 19 | + const server = http_1.default.createServer((req, res) => { |
| 20 | + const url = req.url ?? ''; |
| 21 | + if (req.method === 'GET' && url === '/health') { |
| 22 | + res.writeHead(200, { 'Content-Type': 'application/json' }); |
| 23 | + res.end(JSON.stringify({ ok: true })); |
| 24 | + return; |
| 25 | + } |
| 26 | + if (req.method === 'GET' && url === '/subscriptions') { |
| 27 | + void (0, store_1.readStore)().then((store) => { |
| 28 | + res.writeHead(200, { 'Content-Type': 'application/json' }); |
| 29 | + res.end(JSON.stringify(store.subscriptions)); |
| 30 | + }); |
| 31 | + return; |
| 32 | + } |
| 33 | + if (req.method === 'POST' && url === '/subscriptions') { |
| 34 | + const chunks = []; |
| 35 | + req.on('data', (chunk) => chunks.push(chunk)); |
| 36 | + req.on('end', () => { |
| 37 | + const payload = parseBody(Buffer.concat(chunks).toString('utf8')); |
| 38 | + if (!payload.endpoint || !payload.p256dh || !payload.auth || !payload.userId) { |
| 39 | + res.writeHead(400, { 'Content-Type': 'application/json' }); |
| 40 | + res.end(JSON.stringify({ error: 'Missing endpoint, p256dh, auth, or userId' })); |
| 41 | + return; |
| 42 | + } |
| 43 | + void (0, store_1.upsertSubscription)({ |
| 44 | + userId: payload.userId, |
| 45 | + endpoint: payload.endpoint, |
| 46 | + p256dh: payload.p256dh, |
| 47 | + auth: payload.auth, |
| 48 | + }).then(() => { |
| 49 | + res.writeHead(201, { 'Content-Type': 'application/json' }); |
| 50 | + res.end(JSON.stringify({ ok: true })); |
| 51 | + }); |
| 52 | + }); |
| 53 | + return; |
| 54 | + } |
| 55 | + if (req.method === 'DELETE' && url === '/subscriptions') { |
| 56 | + const chunks = []; |
| 57 | + req.on('data', (chunk) => chunks.push(chunk)); |
| 58 | + req.on('end', () => { |
| 59 | + const payload = parseBody(Buffer.concat(chunks).toString('utf8')); |
| 60 | + if (!payload.endpoint) { |
| 61 | + res.writeHead(400, { 'Content-Type': 'application/json' }); |
| 62 | + res.end(JSON.stringify({ error: 'Missing endpoint' })); |
| 63 | + return; |
| 64 | + } |
| 65 | + void (0, store_1.deleteSubscription)(payload.endpoint).then(() => { |
| 66 | + res.writeHead(200, { 'Content-Type': 'application/json' }); |
| 67 | + res.end(JSON.stringify({ ok: true })); |
| 68 | + }); |
| 69 | + }); |
| 70 | + return; |
| 71 | + } |
| 72 | + res.writeHead(404, { 'Content-Type': 'application/json' }); |
| 73 | + res.end(JSON.stringify({ error: 'Not found' })); |
| 74 | + }); |
| 75 | + server.listen(config.apiPort, () => { |
| 76 | + console.log(`[push-notifier] API listening on :${config.apiPort}`); |
| 77 | + }); |
| 78 | + return server; |
| 79 | +} |
0 commit comments