diff --git a/README.md b/README.md index ab5638ec..dcd90548 100644 --- a/README.md +++ b/README.md @@ -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. \ No newline at end of file +- 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. \ No newline at end of file diff --git a/package.json b/package.json index 91c4af2a..676b6364 100644 --- a/package.json +++ b/package.json @@ -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": [], diff --git a/scripts/patch-conversations-collection-guards.mjs b/scripts/patch-conversations-collection-guards.mjs new file mode 100644 index 00000000..9c4a3697 --- /dev/null +++ b/scripts/patch-conversations-collection-guards.mjs @@ -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)`); +} diff --git a/scripts/patch-frontend-serviceworker-file.mjs b/scripts/patch-frontend-serviceworker-file.mjs new file mode 100644 index 00000000..f22c28d0 --- /dev/null +++ b/scripts/patch-frontend-serviceworker-file.mjs @@ -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"); +} diff --git a/start.example.sh b/start.example.sh index ad713a81..c609f797 100644 --- a/start.example.sh +++ b/start.example.sh @@ -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