fix(runtime): guard conversations and serviceworker path

This commit is contained in:
svemagie
2026-03-08 01:03:23 +01:00
parent 17b730eb04
commit 65b26dec3d
5 changed files with 156 additions and 4 deletions

View File

@@ -63,6 +63,8 @@
- `start.sh` is intentionally ignored by Git (`.gitignore`) so server secrets are not committed.
- Use `start.example.sh` as the tracked template and keep real credentials in environment variables (or `.env` on the server).
- Startup scripts parse `.env` with the `dotenv` parser (not shell `source`), so values containing spaces are handled safely.
- Startup scripts run patch helpers before boot (`scripts/patch-lightningcss.mjs`, `scripts/patch-endpoint-media-scope.mjs`, `scripts/patch-endpoint-files-upload-route.mjs`).
- Startup scripts run patch helpers before boot (`scripts/patch-lightningcss.mjs`, `scripts/patch-endpoint-media-scope.mjs`, `scripts/patch-endpoint-files-upload-route.mjs`, `scripts/patch-frontend-serviceworker-file.mjs`, `scripts/patch-conversations-collection-guards.mjs`).
- The media scope patch fixes a known upstream issue where file uploads can fail if the token scope is `create update delete` without explicit `media`.
- The files upload route patch fixes browser multi-upload by posting to `/files/upload` (session-authenticated) instead of direct `/media` calls without bearer token.
- The frontend serviceworker patch ensures `@indiekit/frontend/lib/serviceworker.js` exists at runtime to avoid ENOENT in the offline/service worker route.
- The conversations guard patch prevents `Cannot read properties of undefined (reading 'find')` when the `conversation_items` collection is temporarily unavailable.

View File

@@ -4,8 +4,8 @@
"description": "",
"main": "index.js",
"scripts": {
"postinstall": "node scripts/patch-lightningcss.mjs && node scripts/patch-endpoint-media-scope.mjs && node scripts/patch-endpoint-files-upload-route.mjs",
"serve": "node scripts/patch-lightningcss.mjs && node scripts/patch-endpoint-media-scope.mjs && node scripts/patch-endpoint-files-upload-route.mjs && node node_modules/@indiekit/indiekit/bin/cli.js serve --config indiekit.config.mjs",
"postinstall": "node scripts/patch-lightningcss.mjs && node scripts/patch-endpoint-media-scope.mjs && node scripts/patch-endpoint-files-upload-route.mjs && node scripts/patch-frontend-serviceworker-file.mjs && node scripts/patch-conversations-collection-guards.mjs",
"serve": "node scripts/patch-lightningcss.mjs && node scripts/patch-endpoint-media-scope.mjs && node scripts/patch-endpoint-files-upload-route.mjs && node scripts/patch-frontend-serviceworker-file.mjs && node scripts/patch-conversations-collection-guards.mjs && node node_modules/@indiekit/indiekit/bin/cli.js serve --config indiekit.config.mjs",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],

View File

@@ -0,0 +1,91 @@
import { access, readFile, writeFile } from "node:fs/promises";
const candidates = [
"node_modules/@rmdes/indiekit-endpoint-conversations/lib/storage/conversation-items.js",
"node_modules/@indiekit/indiekit/node_modules/@rmdes/indiekit-endpoint-conversations/lib/storage/conversation-items.js",
];
const oldBlock = `function getCollection(application) {
return application.collections.get("conversation_items");
}`;
const newBlock = `const emptyCursor = {
sort() {
return this;
},
skip() {
return this;
},
limit() {
return this;
},
async toArray() {
return [];
},
};
const emptyCollection = {
find() {
return emptyCursor;
},
aggregate() {
return { toArray: async () => [] };
},
async countDocuments() {
return 0;
},
async findOneAndUpdate() {
return null;
},
async deleteMany() {
return { deletedCount: 0 };
},
async createIndex() {
return null;
},
};
function getCollection(application) {
return application?.collections?.get?.("conversation_items") || emptyCollection;
}`;
async function exists(path) {
try {
await access(path);
return true;
} catch {
return false;
}
}
let checked = 0;
let patched = 0;
for (const filePath of candidates) {
if (!(await exists(filePath))) {
continue;
}
checked += 1;
const source = await readFile(filePath, "utf8");
if (source.includes("const emptyCollection = {")) {
continue;
}
if (!source.includes(oldBlock)) {
continue;
}
const updated = source.replace(oldBlock, newBlock);
await writeFile(filePath, updated, "utf8");
patched += 1;
}
if (checked === 0) {
console.log("[postinstall] No conversations storage files found");
} else if (patched === 0) {
console.log("[postinstall] conversations storage guards already patched");
} else {
console.log(`[postinstall] Patched conversations storage guards in ${patched} file(s)`);
}

View File

@@ -0,0 +1,57 @@
import { access, mkdir, readFile, writeFile } from "node:fs/promises";
import path from "node:path";
const expected = "node_modules/@indiekit/frontend/lib/serviceworker.js";
const candidates = [
"node_modules/@rmdes/indiekit-frontend/lib/serviceworker.js",
"node_modules/@indiekit/indiekit/node_modules/@indiekit/frontend/lib/serviceworker.js",
"node_modules/@indiekit/endpoint-posts/node_modules/@indiekit/frontend/lib/serviceworker.js",
"node_modules/@rmdes/indiekit-endpoint-conversations/node_modules/@indiekit/frontend/lib/serviceworker.js",
"node_modules/@rmdes/indiekit-endpoint-webmention-io/node_modules/@indiekit/frontend/lib/serviceworker.js",
];
const fallback = `const APP_VERSION = "APP_VERSION";
self.addEventListener("install", (event) => {
event.waitUntil(self.skipWaiting());
});
self.addEventListener("activate", (event) => {
event.waitUntil(self.clients.claim());
});
self.addEventListener("fetch", () => {});
`;
async function exists(filePath) {
try {
await access(filePath);
return true;
} catch {
return false;
}
}
if (await exists(expected)) {
console.log("[postinstall] frontend serviceworker already present");
process.exit(0);
}
let sourcePath = null;
for (const candidate of candidates) {
if (await exists(candidate)) {
sourcePath = candidate;
break;
}
}
await mkdir(path.dirname(expected), { recursive: true });
if (sourcePath) {
const content = await readFile(sourcePath, "utf8");
await writeFile(expected, content, "utf8");
console.log(`[postinstall] Restored frontend serviceworker from ${sourcePath}`);
} else {
await writeFile(expected, fallback, "utf8");
console.log("[postinstall] Created fallback frontend serviceworker");
}

View File

@@ -38,5 +38,7 @@ export NODE_ENV="${NODE_ENV:-production}"
/usr/local/bin/node scripts/patch-lightningcss.mjs
/usr/local/bin/node scripts/patch-endpoint-media-scope.mjs
/usr/local/bin/node scripts/patch-endpoint-files-upload-route.mjs
/usr/local/bin/node scripts/patch-frontend-serviceworker-file.mjs
/usr/local/bin/node scripts/patch-conversations-collection-guards.mjs
exec /usr/local/bin/node node_modules/@indiekit/indiekit/bin/cli.js serve --config indiekit.config.mjs