mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-09-30 12:39:42 +02:00
Detach a session tab into its own browser window and back. Detach/undock: - GET /session/:id serves the SPA in "solo mode", reusing the existing client (terminal, local-echo overlay, reconnect) so no terminal code is duplicated. One PTY already fans out to N SSE/WS clients, so a detached window is just another live client — no server fan-out work was needed. - A pop-out icon per tab; detached tabs show a badge and focus the popup on click; closing the popup re-docks. Cross-window state via BroadcastChannel plus a WindowProxy poll, and survives a dashboard reload (roll-call). app.detachSession(id) is a single idempotent entry point (future gesture hook). <base href="/"> so relative assets resolve under /session/:id. Beta-branch isolation (so it can run alongside a prod Codeman): - Default port 3000 -> 5000. - New src/config/instance.ts derives the data dir and tmux socket from CODEMAN_INSTANCE (default "beta"): ~/.codeman-beta + tmux -L codeman-beta. Every ~/.codeman path now goes through dataPath()/getDataDir() (state, mux-sessions, settings, push keys, lifecycle log, screenshots, certs, linked-cases, subagent window state). Overridable via CODEMAN_INSTANCE / CODEMAN_DATA_DIR / CODEMAN_TMUX_SOCKET. Prevents a second instance from discovering and attaching PTYs to the first instance's live tmux sessions. Verified: tsc / eslint / prettier / lockfile clean; Playwright E2E (27 checks) for detach/solo/redock; default isolation confirmed to see zero real sessions. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
173 lines
4.9 KiB
TypeScript
173 lines
4.9 KiB
TypeScript
/**
|
|
* @fileoverview VAPID key auto-generation and push subscription CRUD.
|
|
*
|
|
* Persists VAPID keys to ~/.codeman/push-keys.json and subscriptions
|
|
* to ~/.codeman/push-subscriptions.json. Debounced saves prevent
|
|
* excessive disk I/O during rapid subscription updates.
|
|
*/
|
|
|
|
import { existsSync, readFileSync, writeFileSync, mkdirSync } from 'node:fs';
|
|
import { join } from 'node:path';
|
|
import webpush from 'web-push';
|
|
import type { VapidKeys, PushSubscriptionRecord } from './types.js';
|
|
import { Debouncer } from './utils/index.js';
|
|
import { getDataDir } from './config/instance.js';
|
|
|
|
const DATA_DIR = getDataDir();
|
|
const KEYS_FILE = join(DATA_DIR, 'push-keys.json');
|
|
const SUBS_FILE = join(DATA_DIR, 'push-subscriptions.json');
|
|
const SAVE_DEBOUNCE_MS = 500;
|
|
|
|
export class PushSubscriptionStore {
|
|
private vapidKeys: VapidKeys | null = null;
|
|
private subscriptions: Map<string, PushSubscriptionRecord> = new Map();
|
|
private saveDeb = new Debouncer(SAVE_DEBOUNCE_MS);
|
|
private _disposed = false;
|
|
|
|
constructor() {
|
|
this.loadSubscriptions();
|
|
}
|
|
|
|
get isDisposed(): boolean {
|
|
return this._disposed;
|
|
}
|
|
|
|
/** Get or generate VAPID keys */
|
|
getVapidKeys(): VapidKeys {
|
|
if (this.vapidKeys) return this.vapidKeys;
|
|
|
|
// Try loading from disk
|
|
if (existsSync(KEYS_FILE)) {
|
|
try {
|
|
const raw = readFileSync(KEYS_FILE, 'utf-8');
|
|
this.vapidKeys = JSON.parse(raw) as VapidKeys;
|
|
return this.vapidKeys;
|
|
} catch {
|
|
// Regenerate on parse error
|
|
}
|
|
}
|
|
|
|
// Generate new keys
|
|
const keys = webpush.generateVAPIDKeys();
|
|
this.vapidKeys = {
|
|
publicKey: keys.publicKey,
|
|
privateKey: keys.privateKey,
|
|
generatedAt: Date.now(),
|
|
};
|
|
|
|
// Persist
|
|
mkdirSync(DATA_DIR, { recursive: true });
|
|
writeFileSync(KEYS_FILE, JSON.stringify(this.vapidKeys, null, 2));
|
|
return this.vapidKeys;
|
|
}
|
|
|
|
/** Get the public VAPID key for client subscription */
|
|
getPublicKey(): string {
|
|
return this.getVapidKeys().publicKey;
|
|
}
|
|
|
|
/** Register or update a push subscription (deduplicates by endpoint) */
|
|
addSubscription(sub: Omit<PushSubscriptionRecord, 'lastUsedAt'>): PushSubscriptionRecord {
|
|
// Check for existing subscription with same endpoint
|
|
for (const [existingId, existing] of this.subscriptions) {
|
|
if (existing.endpoint === sub.endpoint) {
|
|
// Update existing
|
|
const updated: PushSubscriptionRecord = {
|
|
...existing,
|
|
keys: sub.keys,
|
|
userAgent: sub.userAgent,
|
|
lastUsedAt: Date.now(),
|
|
pushPreferences: sub.pushPreferences,
|
|
};
|
|
this.subscriptions.set(existingId, updated);
|
|
this.scheduleSave();
|
|
return updated;
|
|
}
|
|
}
|
|
|
|
// New subscription
|
|
const record: PushSubscriptionRecord = {
|
|
...sub,
|
|
lastUsedAt: Date.now(),
|
|
};
|
|
this.subscriptions.set(record.id, record);
|
|
this.scheduleSave();
|
|
return record;
|
|
}
|
|
|
|
/** Update push preferences for a subscription */
|
|
updatePreferences(id: string, preferences: Record<string, boolean>): PushSubscriptionRecord | null {
|
|
const sub = this.subscriptions.get(id);
|
|
if (!sub) return null;
|
|
sub.pushPreferences = preferences;
|
|
sub.lastUsedAt = Date.now();
|
|
this.scheduleSave();
|
|
return sub;
|
|
}
|
|
|
|
/** Remove a subscription */
|
|
removeSubscription(id: string): boolean {
|
|
const deleted = this.subscriptions.delete(id);
|
|
if (deleted) this.scheduleSave();
|
|
return deleted;
|
|
}
|
|
|
|
/** Remove subscription by endpoint (used for auto-cleanup of expired subs) */
|
|
removeByEndpoint(endpoint: string): void {
|
|
for (const [id, sub] of this.subscriptions) {
|
|
if (sub.endpoint === endpoint) {
|
|
this.subscriptions.delete(id);
|
|
this.scheduleSave();
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
/** Get all subscriptions */
|
|
getAll(): PushSubscriptionRecord[] {
|
|
return Array.from(this.subscriptions.values());
|
|
}
|
|
|
|
/** Get a single subscription by ID */
|
|
get(id: string): PushSubscriptionRecord | null {
|
|
return this.subscriptions.get(id) ?? null;
|
|
}
|
|
|
|
/** Load subscriptions from disk */
|
|
private loadSubscriptions(): void {
|
|
if (!existsSync(SUBS_FILE)) return;
|
|
try {
|
|
const raw = readFileSync(SUBS_FILE, 'utf-8');
|
|
const arr = JSON.parse(raw) as PushSubscriptionRecord[];
|
|
for (const sub of arr) {
|
|
this.subscriptions.set(sub.id, sub);
|
|
}
|
|
} catch {
|
|
// Start fresh on parse error
|
|
}
|
|
}
|
|
|
|
/** Schedule a debounced save */
|
|
private scheduleSave(): void {
|
|
if (this._disposed) return;
|
|
this.saveDeb.schedule(() => this.flushSave());
|
|
}
|
|
|
|
/** Immediately persist subscriptions to disk */
|
|
private flushSave(): void {
|
|
try {
|
|
mkdirSync(DATA_DIR, { recursive: true });
|
|
writeFileSync(SUBS_FILE, JSON.stringify(Array.from(this.subscriptions.values()), null, 2));
|
|
} catch {
|
|
// Ignore write errors
|
|
}
|
|
}
|
|
|
|
/** Clean shutdown */
|
|
dispose(): void {
|
|
if (this._disposed) return;
|
|
this._disposed = true;
|
|
this.saveDeb.flush(() => this.flushSave());
|
|
}
|
|
}
|