diff --git a/build.mjs b/build.mjs index 26ae01c..6cb79e6 100644 --- a/build.mjs +++ b/build.mjs @@ -1,4 +1,4 @@ -import { rm, mkdir, readdir } from "node:fs/promises"; +import { rm, mkdir, cp } from "node:fs/promises"; import { join } from "node:path"; const DIST = "dist"; @@ -21,7 +21,7 @@ async function build() { minify: true, sourcemap: "external", target: "browser", - external: ["/lib/*"], + external: ["/lib/*", "*/zerocms_tagger.js", "*/zerocms_tagger_bg.wasm"], }); if (!result.success) { @@ -31,12 +31,7 @@ async function build() { // 3. Copy Static Libraries console.log("📂 Copying libraries..."); - const libs = await readdir("lib"); - for (const lib of libs) { - const src = join("lib", lib); - const dest = join(DIST, "lib", lib); - await Bun.write(dest, Bun.file(src)); - } + await cp("lib", join(DIST, "lib"), { recursive: true }); // Extract the generated hashed file name from Bun's output const jsOutput = result.outputs.find(out => out.path.endsWith('.js')); diff --git a/serve.js b/serve.js index 0fe3dcc..a294666 100644 --- a/serve.js +++ b/serve.js @@ -334,16 +334,22 @@ const server = http.createServer((req, res) => { } }); -server.listen(PORT, () => { - console.log(`\x1b[32m[ZeroCMS] Server running at http://localhost:${PORT}\x1b[0m`); - console.log(`\x1b[33mOpen http://localhost:${PORT} to start the CMS.\x1b[0m`); - if (CLIENT_ID === 'YOUR_CLIENT_ID') { - console.log(`\x1b[31m[!] GITHUB_CLIENT_ID is not configured. OAuth will not work.\x1b[0m`); - } - if (!APP_ID) { - console.log(`\x1b[33m[!] GITHUB_APP_ID not found. Organization support disabled.\x1b[0m`); - } - if (!PRIVATE_KEY) { - console.log(`\x1b[33m[!] GITHUB_PRIVATE_KEY not found. Organization support disabled.\x1b[0m`); - } -}); +if (require.main === module) { + server.listen(PORT, () => { + console.log(`\x1b[32m[ZeroCMS] Server running at http://localhost:${PORT}\x1b[0m`); + console.log(`\x1b[33mOpen http://localhost:${PORT} to start the CMS.\x1b[0m`); + if (CLIENT_ID === 'YOUR_CLIENT_ID') { + console.log(`\x1b[31m[!] GITHUB_CLIENT_ID is not configured. OAuth will not work.\x1b[0m`); + } + if (!APP_ID) { + console.log(`\x1b[33m[!] GITHUB_APP_ID not found. Organization support disabled.\x1b[0m`); + } + if (!PRIVATE_KEY) { + console.log(`\x1b[33m[!] GITHUB_PRIVATE_KEY not found. Organization support disabled.\x1b[0m`); + } + }); +} + +if (typeof module !== 'undefined') { + module.exports.base64Url = base64Url; +} diff --git a/tests/serve.test.js b/tests/serve.test.js new file mode 100644 index 0000000..748a0d9 --- /dev/null +++ b/tests/serve.test.js @@ -0,0 +1,30 @@ +const { test, expect } = require("bun:test"); +const { base64Url } = require("../serve.js"); + +test("base64Url handles standard string", () => { + expect(base64Url("hello world")).toBe("aGVsbG8gd29ybGQ"); +}); + +test("base64Url handles empty string", () => { + expect(base64Url("")).toBe(""); +}); + +test("base64Url replaces + with -", () => { + // String.fromCharCode(0, 190) creates a string whose standard base64 encoding contains '+' + // The standard base64 is AMK+ + const input = String.fromCharCode(0, 190); + expect(base64Url(input)).toBe("AMK-"); +}); + +test("base64Url replaces / with _", () => { + // String.fromCharCode(128, 63) creates a string whose standard base64 encoding contains '/' + // The standard base64 is woA/ + const input = String.fromCharCode(128, 63); + expect(base64Url(input)).toBe("woA_"); +}); + +test("base64Url removes =", () => { + // 'a' has standard base64 encoding 'YQ==' + // The two '=' should be removed + expect(base64Url("a")).toBe("YQ"); +});