Files
indiekit-server/scripts/patch-ap-key-freshness-nonblocking.mjs
Sven 0ae3d835cd
Deploy Indiekit Server / deploy (push) Has been cancelled
Add patch: touchKeyFreshness() fire-and-forget in inbox-listeners.js
11x 'await touchKeyFreshness()' blocks AP inbox processing on a non-critical
tracking write. Under burst traffic this caused 8s delays (seen in profiling).
Removing await + .catch(() => {}) mirrors the function's own error handling and
lets inbox listeners complete without waiting for the DB write.
2026-05-03 12:39:29 +02:00

56 lines
2.0 KiB
JavaScript

/**
* Patch: make touchKeyFreshness() fire-and-forget in inbox-listeners.js.
*
* touchKeyFreshness() is non-critical tracking (records lastSeenAt on
* ap_key_freshness). Under burst AP traffic, 11 parallel awaited upserts
* compete for MongoDB write locks — profiling shows 8-second delays on
* this single call while holding up inbox processing.
*
* Removing the await means inbox listeners don't block on the tracking
* write. The .catch(() => {}) mirrors the existing error handling inside
* the function itself.
*/
import { access, readFile, writeFile } from "node:fs/promises";
const candidates = [
"node_modules/@rmdes/indiekit-endpoint-activitypub/lib/inbox-listeners.js",
"node_modules/@indiekit/indiekit/node_modules/@rmdes/indiekit-endpoint-activitypub/lib/inbox-listeners.js",
];
const MARKER = "// [patch] key-freshness-nonblocking";
async function exists(p) {
try { await access(p); return true; } catch { return false; }
}
let patched = false;
for (const filePath of candidates) {
if (!(await exists(filePath))) continue;
let src = await readFile(filePath, "utf8");
if (src.includes(MARKER)) {
console.log(`[postinstall] patch-ap-key-freshness-nonblocking: already applied in ${filePath}`);
patched = true;
break;
}
const before = src;
// Replace all: await touchKeyFreshness(...) → touchKeyFreshness(...).catch(() => {})
src = src.replace(
/await touchKeyFreshness\(([^)]+)\);/g,
`touchKeyFreshness($1).catch(() => {}); ${MARKER}`,
);
const count = (src.match(/key-freshness-nonblocking/g) || []).length;
if (count === 0) {
console.log(`[postinstall] patch-ap-key-freshness-nonblocking: target not found in ${filePath}`);
continue;
}
await writeFile(filePath, src, "utf8");
console.log(`[postinstall] patch-ap-key-freshness-nonblocking: applied to ${count} calls in ${filePath}`);
patched = true;
break;
}
if (!patched) {
console.log("[postinstall] patch-ap-key-freshness-nonblocking: no target file found");
}