diff --git a/package.json b/package.json index b98d5dc..1bd7149 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "engines": {"node": ">=24"}, "scripts": { "build": "node scripts/build.mjs", - "test": "node --test tests/workspace.test.mjs", + "test": "node --test tests/*.test.mjs", "test:browser": "playwright test", "preview": "node scripts/serve.mjs" }, diff --git a/scripts/build.mjs b/scripts/build.mjs index 6f507e5..5d0e9e7 100644 --- a/scripts/build.mjs +++ b/scripts/build.mjs @@ -18,7 +18,13 @@ export function validateWorkspace(value) { } return value; } -export const escapeHtml = value => String(value).replace(/[&<>"']/g, char => ({'&': '&', '<': '<', '>': '>', '"': '"', "'": '''}[char])); +const htmlEscapes = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }; +const reUnescapedHtml = /[&<>"']/g; +const reHasUnescapedHtml = RegExp(reUnescapedHtml.source); +export const escapeHtml = value => { + const str = String(value); + return (str && reHasUnescapedHtml.test(str)) ? str.replace(reUnescapedHtml, (chr) => htmlEscapes[chr]) : str; +}; const href = (workspace, action) => `https://freetwai.com/#guilds/${workspace.guild_key}/${action}`; export function renderWorkspace(workspace) { validateWorkspace(workspace); diff --git a/scripts/serve.mjs b/scripts/serve.mjs index 786ca66..23905eb 100644 --- a/scripts/serve.mjs +++ b/scripts/serve.mjs @@ -2,10 +2,18 @@ import {createServer} from 'node:http'; import {readFile} from 'node:fs/promises'; import {resolve} from 'node:path'; const types = {'/index.html': 'text/html; charset=utf-8', '/site.js': 'text/javascript; charset=utf-8', '/style.css': 'text/css; charset=utf-8', '/brand.png': 'image/png', '/release.json': 'application/json'}; +const cache = new Map(); const server = createServer(async (request, response) => { const path = request.url === '/' ? '/index.html' : request.url; if (!Object.hasOwn(types, path)) {response.writeHead(404).end(); return;} - try {response.writeHead(200, {'Content-Type': types[path], 'Cache-Control': 'no-store', 'X-Content-Type-Options': 'nosniff'}).end(await readFile(resolve('dist', '.' + path)));} + try { + let data = cache.get(path); + if (!data) { + data = await readFile(resolve('dist', '.' + path)); + cache.set(path, data); + } + response.writeHead(200, {'Content-Type': types[path], 'Cache-Control': 'no-store', 'X-Content-Type-Options': 'nosniff'}).end(data); + } catch {response.writeHead(404).end();} }); server.listen(4417, '127.0.0.1', () => process.stdout.write('Preview http://127.0.0.1:4417\n')); diff --git a/tests/benchmark.test.mjs b/tests/benchmark.test.mjs new file mode 100644 index 0000000..8da1fb1 --- /dev/null +++ b/tests/benchmark.test.mjs @@ -0,0 +1,16 @@ +import { test } from 'node:test'; +import assert from 'node:assert/strict'; +import { renderWorkspace } from '../scripts/build.mjs'; +import { readFile } from 'node:fs/promises'; + +const workspace = JSON.parse(await readFile(new URL('../workspace.json', import.meta.url), 'utf8')); + +test('renderWorkspace performance', () => { + const start = performance.now(); + for (let i = 0; i < 10000; i++) { + renderWorkspace(workspace); + } + const end = performance.now(); + const time = end - start; + assert.ok(time < 200, `renderWorkspace took ${time}ms for 10000 iterations, expected < 200ms`); +});