Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

fix(docker): serve JavaScript modules with the correct MIME type (#544)

Thorufurcd7da40686c3
diff --git a/scripts/self-hosted-server.mjs b/scripts/self-hosted-server.mjs
index 8ae5747c..76d3a71d 100644
--- a/scripts/self-hosted-server.mjs
+++ b/scripts/self-hosted-server.mjs
@@ -113,6 +113,7 @@ const contentTypes = {
".css": "text/css; charset=utf-8",
".html": "text/html; charset=utf-8",
".js": "text/javascript; charset=utf-8",
+ ".mjs": "text/javascript; charset=utf-8",
".json": "application/json; charset=utf-8",
".png": "image/png",
".svg": "image/svg+xml",
diff --git a/scripts/self-hosted-static.test.mjs b/scripts/self-hosted-static.test.mjs
new file mode 100644
index 00000000..7af7e9c6
--- /dev/null
+++ b/scripts/self-hosted-static.test.mjs
@@ -0,0 +1,37 @@
+import { test } from "node:test";
+import assert from "node:assert/strict";
+import { readFileSync } from "node:fs";
+import { extname, join, normalize, relative, resolve } from "node:path";
+import { runInNewContext } from "node:vm";
+
+// Exercise the production static handler without starting the database or API.
+const source = readFileSync(new URL("./self-hosted-server.mjs", import.meta.url), "utf8");
+const handlerSource = source.slice(source.indexOf("const contentTypes ="), source.indexOf("const server = Bun.serve"));
+const webDirectory = resolve("/test/web");
+const contents = new Map([
+ [join(webDirectory, "assets/pdf.worker.mjs"), "export const WorkerMessageHandler = {};"],
+ [join(webDirectory, "assets/app.js"), "export {};"],
+ [join(webDirectory, "assets/unknown.bin"), "binary"],
+]);
+const serveStatic = runInNewContext(`${handlerSource}\nserveStatic;`, {
+ extname, join, normalize, relative, resolve, webDirectory, URL, Response,
+ Bun: { file(path) {
+ const file = new Blob([contents.get(path) ?? ""]);
+ file.exists = async () => contents.has(path);
+ return file;
+ } },
+});
+
+test("serves the PDF module worker with a JavaScript MIME type and its original bytes", async () => {
+ const response = await serveStatic(new Request("http://localhost/assets/pdf.worker.mjs"));
+ assert.equal(response.status, 200);
+ assert.equal(response.headers.get("Content-Type"), "text/javascript; charset=utf-8");
+ assert.equal(await response.text(), "export const WorkerMessageHandler = {};");
+});
+
+test("preserves JavaScript and unknown-file content types", async () => {
+ for (const [name, expected] of [["app.js", "text/javascript; charset=utf-8"], ["unknown.bin", "application/octet-stream"]]) {
+ const response = await serveStatic(new Request(`http://localhost/assets/${name}`));
+ assert.equal(response.headers.get("Content-Type"), expected);
+ }
+});