Download server/push.js from Cnass/Claude: direct link, hf CLI and curl.
- Browser
- Download file 3.93 kB
-
https://huggingface.co/spaces/Cnass/Claude/resolve/main/server/push.js
- Command line
-
hf download hf://spaces/Cnass/Claude/server/push.js
-
curl -L -o push.js https://huggingface.co/spaces/Cnass/Claude/resolve/main/server/push.js
3.93 kB
| ; | |
| // Web Push notifications ("Claude finished", "permission needed") for phones | |
| // where the page is in the background or closed. VAPID keys are generated | |
| // once and kept in ~/.cc-web/push.json with the browsers' subscriptions. | |
| const fs = require('fs'); | |
| const path = require('path'); | |
| const MAX_SUBSCRIPTIONS = 20; | |
| const THROTTLE_MS = 15000; | |
| let webpush = null; | |
| try { | |
| webpush = require('web-push'); | |
| } catch { | |
| webpush = null; | |
| } | |
| class PushManager { | |
| constructor({ home, subject }) { | |
| this.file = path.join(home, '.cc-web', 'push.json'); | |
| this.subject = subject || 'mailto:agentweb@example.invalid'; | |
| this.last = new Map(); | |
| this.data = this.load(); | |
| } | |
| get available() { | |
| return !!webpush; | |
| } | |
| load() { | |
| let data = {}; | |
| try { | |
| data = JSON.parse(fs.readFileSync(this.file, 'utf8')) || {}; | |
| } catch { | |
| data = {}; | |
| } | |
| if (!Array.isArray(data.subs)) data.subs = []; | |
| if (webpush && (!data.vapid || !data.vapid.publicKey || !data.vapid.privateKey)) { | |
| data.vapid = webpush.generateVAPIDKeys(); | |
| this.save(data); | |
| } | |
| return data; | |
| } | |
| save(data = this.data) { | |
| try { | |
| fs.mkdirSync(path.dirname(this.file), { recursive: true }); | |
| fs.writeFileSync(this.file, JSON.stringify(data, null, 1), { mode: 0o600 }); | |
| } catch (err) { | |
| console.error('[push] kaydedilemedi:', err.message); | |
| } | |
| } | |
| publicKey() { | |
| return this.data.vapid ? this.data.vapid.publicKey : null; | |
| } | |
| subscribe(sub, userAgent) { | |
| if (!webpush) throw Object.assign(new Error('Bildirimler bu kurulumda yok.'), { status: 501 }); | |
| if (!sub || typeof sub.endpoint !== 'string' || !/^https:\/\//.test(sub.endpoint) || !sub.keys || !sub.keys.p256dh || !sub.keys.auth) { | |
| throw Object.assign(new Error('Geçersiz abonelik.'), { status: 400 }); | |
| } | |
| this.data.subs = this.data.subs.filter((s) => s.endpoint !== sub.endpoint); | |
| this.data.subs.push({ endpoint: sub.endpoint, keys: { p256dh: String(sub.keys.p256dh), auth: String(sub.keys.auth) }, ua: String(userAgent || '').slice(0, 160), addedAt: Date.now() }); | |
| if (this.data.subs.length > MAX_SUBSCRIPTIONS) this.data.subs.splice(0, this.data.subs.length - MAX_SUBSCRIPTIONS); | |
| this.save(); | |
| return { ok: true, count: this.data.subs.length }; | |
| } | |
| unsubscribe(endpoint) { | |
| const before = this.data.subs.length; | |
| this.data.subs = this.data.subs.filter((s) => s.endpoint !== endpoint); | |
| if (this.data.subs.length !== before) this.save(); | |
| return { ok: true }; | |
| } | |
| has(endpoint) { | |
| return this.data.subs.some((s) => s.endpoint === endpoint); | |
| } | |
| async send({ session, kind, body, title }) { | |
| if (!webpush || !this.data.subs.length || !this.data.vapid) return; | |
| const key = `${session}:${kind}`; | |
| const now = Date.now(); | |
| if (now - (this.last.get(key) || 0) < THROTTLE_MS) return; | |
| this.last.set(key, now); | |
| const payload = JSON.stringify({ | |
| title: title || `Agent Web · ${session}`, | |
| body: String(body || '').slice(0, 300), | |
| session, | |
| kind, | |
| url: `/?s=${encodeURIComponent(session)}`, | |
| }); | |
| const options = { | |
| TTL: 3600, | |
| urgency: kind === 'done' ? 'normal' : 'high', | |
| vapidDetails: { subject: this.subject, publicKey: this.data.vapid.publicKey, privateKey: this.data.vapid.privateKey }, | |
| }; | |
| let changed = false; | |
| await Promise.all( | |
| this.data.subs.map(async (sub) => { | |
| try { | |
| await webpush.sendNotification({ endpoint: sub.endpoint, keys: sub.keys }, payload, options); | |
| } catch (err) { | |
| // Gone (unsubscribed, uninstalled): forget it. | |
| if (err && (err.statusCode === 404 || err.statusCode === 410)) { | |
| sub.gone = true; | |
| changed = true; | |
| } | |
| } | |
| }) | |
| ); | |
| if (changed) { | |
| this.data.subs = this.data.subs.filter((s) => !s.gone); | |
| this.save(); | |
| } | |
| } | |
| } | |
| module.exports = { PushManager }; | |