A minimal web editor for managing standard.site records in your atproto PDS
1#!/usr/bin/env bun
2/**
3 * Database cleanup script
4 * Removes expired OAuth states and optionally old sessions
5 * Run via cron: 0 * * * * /home/exedev/.bun/bin/bun /home/exedev/std.pub/scripts/cleanup.ts
6 */
7
8import { Database } from "bun:sqlite";
9import * as path from "path";
10
11const DATA_DIR = process.env.DATA_DIR || "./data";
12const DB_PATH = path.join(DATA_DIR, "oauth.db");
13
14try {
15 const db = new Database(DB_PATH);
16
17 // Clean up OAuth states older than 1 hour
18 const statesResult = db.run(
19 `DELETE FROM oauth_states WHERE created_at < strftime('%s', 'now') - 3600`,
20 );
21
22 // Clean up sessions older than 30 days (optional - sessions may still be valid)
23 const sessionsResult = db.run(
24 `DELETE FROM oauth_sessions WHERE updated_at < strftime('%s', 'now') - 2592000`,
25 );
26
27 // Vacuum the database to reclaim space
28 db.run("VACUUM");
29
30 const timestamp = new Date().toISOString();
31 console.log(
32 `[${timestamp}] Cleanup complete: removed old states and sessions, vacuumed database`,
33 );
34
35 db.close();
36} catch (error) {
37 console.error("Cleanup failed:", error);
38 process.exit(1);
39}