All checks were successful
CD / Build and push images (push) Successful in 3m4s
CI / Lint, typecheck, test (push) Successful in 2m27s
CI / Auth e2e pack (push) Successful in 3m6s
CI / Build container images (push) Has been skipped
CD / Deploy to Test (push) Successful in 9s
CD / Smoke tests against Test (push) Successful in 1m16s
CD / Promote to Int (push) Successful in 11s
Live editing now obeys the same rules as REST: the collab-token mode comes from the shared grant resolution, anonymous visitors can join public pages, and revoking write access flips a running session to read-only within seconds. - Anonymous public tokens: `GET /pages/:id/collab-token` is `@Public()` but still permission-guarded, so a logged-out visitor gets an `ro` token where a `public` grant makes the page readable (404 otherwise). The token's `userId` is nullable (shared schema + collab context) for anonymous subjects. - Prompt revocation: the pond-level NOTIFY (#39) now also fires on label tree/assignment changes (LabelsService move/remove/assign/unassign), and the collab server closes the *actual* WebSocket instead of only sending an application-level close message. Hocuspocus' `closeConnections` leaves the socket open so the client only re-checks on its ~30s message timeout; `closeDocumentConnections` drops the socket so the client reconnects and re-authenticates with a freshly-resolved token at once — the "within seconds" downgrade the milestone promises. - Tests: the #52 fixture matrix gains anonymous cases (public grant → `ro`, none → 404); a collab db test proves an editor downgraded to reader goes read-only on reconnect (its post-downgrade edits no longer reach a peer); a new browser `collab-permissions` pack covers the read-only participant and the live downgrade end to end (new plain `fixture-editor` account). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EwZ4jR4KFAPvpjWevfUGX1
81 lines
3.2 KiB
TypeScript
81 lines
3.2 KiB
TypeScript
import { collabEnvSchema, parseEnv } from '@dorfteich/shared';
|
|
import { Client } from 'pg';
|
|
|
|
import { createAccessListener } from './access-listener.js';
|
|
import { createPool, pingDatabase } from './db.js';
|
|
import { createLogger } from './logger.js';
|
|
import { PostgresPagePersistence } from './persistence.js';
|
|
import { createRestoreListener } from './restore-listener.js';
|
|
import { closeDocumentConnections, createCollabServer } from './server.js';
|
|
import { PostgresSessionRegistry } from './session-registry.js';
|
|
import { PostgresVersionStore } from './version-store.js';
|
|
|
|
/**
|
|
* Entry point of the collaboration server (ADR 0003). Validates the
|
|
* environment, opens a database pool for the health probe, and starts the
|
|
* Hocuspocus server. A clean shutdown closes both so the container stops fast.
|
|
*/
|
|
async function bootstrap(): Promise<void> {
|
|
// Validate configuration up front; crash with a readable list on problems.
|
|
const env = parseEnv(collabEnvSchema, process.env);
|
|
const logger = createLogger(env);
|
|
const pool = createPool(env.DATABASE_URL);
|
|
|
|
// Created before the server (the server's hooks call markOpen/markClosed);
|
|
// its heartbeat gets the server's open-document accessor at start() below,
|
|
// which sidesteps the mutual reference between the two.
|
|
const sessionRegistry = new PostgresSessionRegistry({ pool, logger });
|
|
const versionStore = new PostgresVersionStore({ pool, logger });
|
|
|
|
const server = createCollabServer({
|
|
version: env.APP_VERSION,
|
|
logger,
|
|
tokenSecret: env.COLLAB_TOKEN_SECRET,
|
|
pingDatabase: () => pingDatabase(pool),
|
|
persistence: new PostgresPagePersistence(pool),
|
|
sessionRegistry,
|
|
versionStore,
|
|
});
|
|
|
|
// Terminate live sessions when access to a pond is revoked (issue #39). The
|
|
// listener owns a dedicated connection because `LISTEN` is connection-bound
|
|
// and cannot be served from the pool.
|
|
const accessListener = createAccessListener({
|
|
createClient: () => new Client({ connectionString: env.DATABASE_URL }),
|
|
pool,
|
|
openDocumentNames: () => [...server.hocuspocus.documents.keys()],
|
|
closeConnections: (documentName) => closeDocumentConnections(server.hocuspocus, documentName),
|
|
logger,
|
|
});
|
|
|
|
// Applies api-requested restores to the live document (issue #42).
|
|
const restoreListener = createRestoreListener({
|
|
createClient: () => new Client({ connectionString: env.DATABASE_URL }),
|
|
pool,
|
|
openDirectConnection: (documentName) =>
|
|
server.hocuspocus.openDirectConnection(documentName, { userId: 'restore', mode: 'rw' }),
|
|
logger,
|
|
});
|
|
|
|
await server.listen(env.PORT);
|
|
await accessListener.start();
|
|
await restoreListener.start();
|
|
sessionRegistry.start(() => [...server.hocuspocus.documents.keys()]);
|
|
logger.info({ event: 'listen', port: env.PORT }, 'collaboration server listening');
|
|
|
|
const shutdown = (signal: NodeJS.Signals): void => {
|
|
logger.info({ event: 'shutdown', signal }, 'shutting down');
|
|
sessionRegistry.stop();
|
|
void Promise.allSettled([
|
|
accessListener.stop(),
|
|
restoreListener.stop(),
|
|
server.destroy(),
|
|
pool.end(),
|
|
]).then(() => process.exit(0));
|
|
};
|
|
process.on('SIGTERM', () => shutdown('SIGTERM'));
|
|
process.on('SIGINT', () => shutdown('SIGINT'));
|
|
}
|
|
|
|
void bootstrap();
|