Download server/index.js from abr00kx/taper: direct link, hf CLI and curl.
- Browser
- Download file 4.3 kB
-
https://huggingface.co/abr00kx/taper/resolve/main/server/index.js
- Command line
-
hf download hf://abr00kx/taper/server/index.js
-
curl -L -o index.js https://huggingface.co/abr00kx/taper/resolve/main/server/index.js
4.3 kB
| // Barebones Taper homeserver: startup and wiring only. | |
| // | |
| // One WebSocket gateway, allowlisted (experimental) federation, encrypted | |
| // media, optional encrypted-at-rest storage. Protocol summary is in | |
| // README.md. Run with `npm start`. | |
| // | |
| // Every module is built by create(app) from one shared context, `app`: | |
| // app.cfg configuration (config.js) | |
| // app.disk the sealed store (store.js) | |
| // app.state the maps several modules share: users, tokens, rooms, | |
| // sessions, txnIds, remoteInvites | |
| // app.<name> each module's exported functions; modules call each other | |
| // only through these, never into each other's private state. | |
| import { Store } from './store.js'; | |
| import { isDecommissioned } from './dms.js'; | |
| import { serverOfUser } from './state-res.js'; | |
| import { loadConfig } from './config.js'; | |
| import * as sessions from './sessions.js'; | |
| import * as rooms from './rooms.js'; | |
| import * as keys from './keys.js'; | |
| import * as media from './media.js'; | |
| import * as quota from './quota.js'; | |
| import * as auth from './auth.js'; | |
| import * as account from './account.js'; | |
| import * as federation from './federation.js'; | |
| import * as operator from './operator.js'; | |
| import * as gateway from './gateway.js'; | |
| import { now } from './util.js'; | |
| // A server whose dead man's switch went off stays down: it must not come back | |
| // as an empty service that looks like the old one. | |
| if (isDecommissioned(process.env.TAPER_DATA_DIR)) { | |
| console.error(`This server was decommissioned (${process.env.TAPER_DATA_DIR}/DECOMMISSIONED). Not starting.`); | |
| process.exit(0); | |
| } | |
| const cfg = loadConfig(); | |
| const app = { | |
| cfg, | |
| // Persistence is on when TAPER_DATA_DIR is set; keys go in TAPER_KEY_DIR, | |
| // which must be somewhere else. | |
| disk: new Store({ dataDir: cfg.DATA_DIR, keyDir: cfg.KEY_DIR, retentionMs: cfg.RETENTION_MS, periodMs: cfg.KEY_PERIOD_MS }), | |
| state: { | |
| users: new Map(), // user_id -> { user_id, opaque_record, created_ts } | |
| tokens: new Map(), // sha256(access_token) -> { user_id, device_id, issued_ts, expires_ts } | |
| rooms: new Map(), // room_id -> Room | |
| sessions: new Map(), // session_id -> Session | |
| txnIds: new Map(), // `${user_id}|${device_id}|${txn_id}` -> event_id | |
| remoteInvites: new Map(), // room_id -> Map(local user -> { inviter, via, ts }): rooms we don't have yet | |
| }, | |
| serverOf: serverOfUser, | |
| isLocalUser: (user_id) => serverOfUser(user_id) === cfg.SERVER_NAME, | |
| }; | |
| app.sessions = sessions.create(app); | |
| app.rooms = rooms.create(app); | |
| app.keys = keys.create(app); | |
| app.media = media.create(app); | |
| app.quota = quota.create(app); | |
| app.auth = auth.create(app); | |
| app.account = account.create(app); | |
| app.fed = federation.create(app); | |
| app.fed.addMethods(app.keys.fedMethods); | |
| app.operator = operator.create(app); | |
| // The one client method table; the gateway enforces what each entry declares. | |
| app.gateway = gateway.create(app, { | |
| ...app.auth.methods, ...app.account.methods, ...app.rooms.methods, ...app.media.methods, ...app.keys.methods, | |
| }); | |
| // Retention: everything past its time goes, then expired keys are shredded. | |
| app.purgeExpired = () => { | |
| const t = now(); | |
| const { disk, state } = app; | |
| app.rooms.purge(t); | |
| app.sessions.purge(t); | |
| let purgedPlain = app.quota.purge(t); | |
| app.keys.purge(t); | |
| app.fed.purge(t); | |
| app.media.purge(t); | |
| if (app.auth.purge(t)) purgedPlain = true; | |
| app.rooms.rekeyState(); | |
| disk.shred(); | |
| if (purgedPlain) disk.checkpoint(); | |
| for (const [k, event_id] of state.txnIds) if (![...state.rooms.values()].some((room) => room.byId.has(event_id))) state.txnIds.delete(k); | |
| }; | |
| setInterval(app.purgeExpired, Math.min(60_000, cfg.RETENTION_MS)).unref(); | |
| setInterval(app.account.purgeBackups, Math.min(3_600_000, cfg.RETENTION_MS)).unref(); | |
| // Restore persisted state into memory. Events whose keys were destroyed are | |
| // simply gone: that is crypto-shredding working. | |
| if (app.disk.enabled) { | |
| app.disk.shred(); | |
| for (const m of [app.auth, app.account, app.quota, app.fed, app.keys, app.media, app.rooms]) m.restore(); | |
| app.keys.forgetOrphans(app.auth.hasToken); | |
| console.log(`Restored ${app.state.users.size} users and ${app.state.rooms.size} rooms from ${cfg.DATA_DIR}`); | |
| } | |
| app.operator.start(); | |
| app.gateway.listen(); | |
| export const { httpServer } = app.gateway; | |