Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"bcryptjs": "^3.0.3",
"better-sqlite3": "^12.10.0",
"fastify": "^5.8.5",
"gitsheets": "^2.2.0",
"gitsheets": "^2.7.0",
"jose": "^6.2.3",
"postmark": "^5.1.0",
"samlify": "^2.13.0",
Expand Down
25 changes: 6 additions & 19 deletions apps/api/scripts/import-laddr/importer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ import {
type TranslateCtx,
type Warnings,
} from './translators.js';
import type { BlobHandle } from 'gitsheets';

// ---------------------------------------------------------------------------
// Public types
Expand Down Expand Up @@ -186,9 +185,6 @@ export async function importLaddrFromJson(opts: ImportOptions): Promise<ImportRe
// against the last even when nothing changed upstream.
// -------------------------------------------------------------------------
let store: PublicStore | null = null;
// Gitsheets Repository — needed to write attachment blobs via
// repo.writeBlob into the underlying git object DB.
let publicRepo: Awaited<ReturnType<typeof openPublicStore>>['repo'] | null = null;
let existingIds: ExistingIds;

if (opts.dryRun) {
Expand All @@ -198,7 +194,6 @@ export async function importLaddrFromJson(opts: ImportOptions): Promise<ImportRe
await ensureBranchCheckedOut(repo, branch, initialParent);
const opened = await openPublicStore(repo);
store = opened.store;
publicRepo = opened.repo;
existingIds = await collectExistingIds(store, log);
}

Expand Down Expand Up @@ -497,10 +492,6 @@ export async function importLaddrFromJson(opts: ImportOptions): Promise<ImportRe
},
},
async (tx) => {
if (publicRepo === null) {
throw new Error('[import-laddr] internal: publicRepo not opened');
}

log(`[import] clear + upsert tags (${tags.length})`);
await tx.tags.clear();
for (const t of tags) await tx.tags.upsert(t);
Expand All @@ -512,11 +503,9 @@ export async function importLaddrFromJson(opts: ImportOptions): Promise<ImportRe
if (avatar) {
// Mirror POST /api/people/:slug/avatar: store original + 128 thumb
// as attachments and point avatarKey at the conventional path.
const originalBlob = await publicRepo.writeBlob(avatar.original);
const thumbnailBlob = await publicRepo.writeBlob(avatar.thumbnail);
await tx.people.setAttachments(p, {
'avatar.jpg': originalBlob,
'avatar-128.jpg': thumbnailBlob,
'avatar.jpg': avatar.original,
'avatar-128.jpg': avatar.thumbnail,
});
await tx.people.upsert({ ...p, avatarKey: `people/${p.slug}/avatar.jpg` });
} else {
Expand Down Expand Up @@ -579,12 +568,10 @@ export async function importLaddrFromJson(opts: ImportOptions): Promise<ImportRe
for (const { record } of blogTranslations) {
const artifacts = mediaArtifactsBySlug.get(record.slug) ?? [];
if (artifacts.length > 0) {
const blobs: Record<string, BlobHandle> = {};
for (const a of artifacts) {
// repo.writeBlob hashes the Buffer into the git object DB.
blobs[a.filename] = await publicRepo.writeBlob(a.bytes);
}
await tx['blog-posts'].setAttachments(record, blobs);
await tx['blog-posts'].setAttachments(
record,
Object.fromEntries(artifacts.map((a) => [a.filename, a.bytes])),
);
}
await tx['blog-posts'].upsert(record);
}
Expand Down
7 changes: 1 addition & 6 deletions apps/api/src/auth/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,7 @@ async function sessionMiddlewarePlugin(fastify: FastifyInstance): Promise<void>

// Load revocation state from gitsheets + session metadata from private store at boot
fastify.addHook('onReady', async () => {
const allRevocations: import('@cfp/shared/schemas').Revocation[] = [];
const revocationsSheet = fastify.store.public.revocations;
for await (const record of revocationsSheet.query()) {
allRevocations.push(record);
}
revocations.load(allRevocations);
await revocations.loadFromSheet(fastify.store.public);

await sessionMetadata.load(fastify.store.private);
});
Expand Down
10 changes: 10 additions & 0 deletions apps/api/src/auth/revocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ export class InMemoryRevocationStore implements RevocationStore {
}
}

/**
* Re-populate from the `revocations` sheet's current read snapshot — at
* boot and after a hot reload moves the data branch.
*/
async loadFromSheet(store: PublicStore): Promise<void> {
const records: Revocation[] = [];
for await (const record of store.revocations.query()) records.push(record);
this.load(records);
}

isRevoked(jti: string): boolean {
return this.#byJti.has(jti);
}
Expand Down
54 changes: 0 additions & 54 deletions apps/api/src/lib/data-repo-lock.ts

This file was deleted.

103 changes: 53 additions & 50 deletions apps/api/src/plugins/reconcile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,69 +20,82 @@
* and crashes the boot. k8s will restart the pod and the entrypoint will
* re-clone if needed.
*
* Decorates Fastify with:
* - `dataRepoLock` — a single-slot async lock callers use to serialize
* non-`store.transact` git operations (boot reconcile, future webhook).
* - `reconcileDataRepo({ branch })` — a thin wrapper that acquires the
* lock and invokes the state-machine function with the current
* environment. Provided so the future hot-reload webhook (#65) has a
* single call to make.
* Decorates Fastify with `reconcileDataRepo({ onMoved })`: fetch outside the
* lock, then reconcile under the repository's write lock — the same mutex
* `transact` holds (gitsheets `repo.withLock`), so a reconcile can never race
* a write. When the reconcile moves the branch, the public store is
* refreshed and `onMoved` runs inside the same lock, so the hot-reload
* rebuild can't interleave with a write either.
*
* Pushes of the working branch go through the push daemon. At boot the
* daemon isn't registered yet; its startup backlog pushes anything ahead.
*/
import type { FastifyInstance } from 'fastify';
import fp from 'fastify-plugin';

import { createDataRepoLock, type DataRepoLock } from '../lib/data-repo-lock.js';
import { reconcileDataRepo, type ReconcileResult } from '../store/reconcile.js';
import {
fetchRemoteBranch,
reconcileDataRepo,
type ReconcileResult,
} from '../store/reconcile.js';

export interface ReconcileCallOptions {
/**
* Runs under the write lock after a reconcile that moved the branch, once
* the public store has been refreshed to the new HEAD.
*/
readonly onMoved?: (result: ReconcileResult) => Promise<void>;
}

declare module 'fastify' {
interface FastifyInstance {
/**
* Acquire the data-repo lock. Returns a release function; release is
* idempotent. See `lib/data-repo-lock.ts` for the contract.
* Reconcile the data repo against `CFP_DATA_REMOTE` for
* `CFP_DATA_BRANCH`. Returns the outcome envelope. Throws on
* unrecoverable filesystem / git errors; soft failures (fetch blip,
* conflict-escape) return a non-throwing result.
*/
dataRepoLock: DataRepoLock;
/**
* Reconcile the local working tree against `CFP_DATA_REMOTE` for the
* given branch under the data-repo lock. Defaults to the configured
* `CFP_DATA_BRANCH`.
*
* Returns the outcome envelope. Throws on unrecoverable filesystem /
* git errors; soft failures (fetch blip, conflict-escape) return a
* non-throwing result.
*/
reconcileDataRepo: (opts?: { branch?: string }) => Promise<ReconcileResult>;
reconcileDataRepo: (opts?: ReconcileCallOptions) => Promise<ReconcileResult>;
}
}

async function reconcilePlugin(fastify: FastifyInstance): Promise<void> {
const lock = createDataRepoLock();
fastify.decorate('dataRepoLock', lock);

const repoPath = fastify.config.CFP_DATA_REPO_PATH;
const configuredBranch = fastify.config.CFP_DATA_BRANCH;
const remote = fastify.config.CFP_DATA_REMOTE;

// Expose a Fastify-bound wrapper so the future webhook handler (#65) has
// a single call to make. Always under the lock.
fastify.decorate(
'reconcileDataRepo',
async (opts?: { branch?: string }): Promise<ReconcileResult> => {
const branch = opts?.branch ?? configuredBranch;
async (opts?: ReconcileCallOptions): Promise<ReconcileResult> => {
const branch = configuredBranch;
if (!branch) {
throw new Error(
'reconcileDataRepo: no branch specified and CFP_DATA_BRANCH is unset',
);
throw new Error('reconcileDataRepo: CFP_DATA_BRANCH is unset');
}
const release = await lock();
try {
return await reconcileDataRepo({
const logger = fastify.log;

// Network outside the lock: writes keep flowing during a slow fetch.
// The ref moves below are CAS (`update-ref <ref> <new> <old>`).
if (!(await fetchRemoteBranch({ repoPath, branch, logger }))) {
const head = (await fastify.publicRepo.resolveRef('HEAD')) ?? '';
return { outcome: 'fetch-failed', oldCommit: head, newCommit: head };
}

return fastify.publicRepo.withLock(async () => {
const result = await reconcileDataRepo({
repoPath,
branch,
logger: fastify.log,
logger,
skipFetch: true,
push: (commit) => {
fastify.pushDaemon?.notifyCommit(commit);
},
});
} finally {
release();
}
if (result.newCommit !== result.oldCommit) {
await fastify.store.public.refresh();
await opts?.onMoved?.(result);
}
return result;
});
},
);

Expand All @@ -103,17 +116,7 @@ async function reconcilePlugin(fastify: FastifyInstance): Promise<void> {
);
}

const release = await lock();
let result: ReconcileResult;
try {
result = await reconcileDataRepo({
repoPath,
branch: configuredBranch,
logger: fastify.log,
});
} finally {
release();
}
const result = await fastify.reconcileDataRepo();

// Outcome-specific logging so operators get an at-a-glance line in prod.
switch (result.outcome) {
Expand Down
Loading
Loading