Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions scripts/build.mjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import { readFile, mkdir, writeFile } from 'node:fs/promises';
import { renderDirectory } from '../src/index.mjs';
import { renderPrivacyPage } from '../src/privacy.mjs';
const directory = JSON.parse(await readFile(new URL('../data/directory.json', import.meta.url), 'utf8'));
const privacy = JSON.parse(await readFile(new URL('../data/privacy-discord-bot.json', import.meta.url), 'utf8'));
const [directoryFile, privacyFile] = await Promise.all([
readFile(new URL('../data/directory.json', import.meta.url), 'utf8'),
readFile(new URL('../data/privacy-discord-bot.json', import.meta.url), 'utf8')
]);
const directory = JSON.parse(directoryFile);
const privacy = JSON.parse(privacyFile);
// Validate both inputs, including the privacy output path, before any writes.
const directoryHtml = renderDirectory(directory);
const privacyHtml = renderPrivacyPage(privacy);
await mkdir(new URL('../dist/', import.meta.url), { recursive: true });
await writeFile(new URL('../dist/index.html', import.meta.url), directoryHtml);
await mkdir(new URL(`../dist/${privacy.path}`, import.meta.url), { recursive: true });
await writeFile(new URL(`../dist/${privacy.path}index.html`, import.meta.url), privacyHtml);
await Promise.all([
mkdir(new URL('../dist/', import.meta.url), { recursive: true }).then(() =>
writeFile(new URL('../dist/index.html', import.meta.url), directoryHtml)
),
mkdir(new URL(`../dist/${privacy.path}`, import.meta.url), { recursive: true }).then(() =>
writeFile(new URL(`../dist/${privacy.path}index.html`, import.meta.url), privacyHtml)
)
]);
console.log(`Built source directory at dist/index.html and privacy page at dist/${privacy.path}index.html; deployment is done by .github/workflows/pages.yml on main.`);
8 changes: 5 additions & 3 deletions src/index.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
const escapeHtml = (value) => String(value).replace(/[&<>"']/g, (character) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' })[character]);
const ESCAPE_MAP = { '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' };
const escapeHtml = (value) => String(value).replace(/[&<>"']/g, (character) => ESCAPE_MAP[character]);

export function validateDirectory(input) {
if (input?.schema_version !== 'freedom.source-directory/v1' || input.organization !== 'FreeTWAI' || !Array.isArray(input.projects) || input.projects.length > 100) throw new TypeError('Expected the FreeTWAI public source directory');
const seen = new Set();
for (const project of input.projects) {
if (!project || !/^(?:\.github|[A-Za-z0-9][A-Za-z0-9._-]{0,99})$/.test(project.repository ?? '') || project.repository.includes('..')) throw new TypeError('Invalid repository name');
if (seen.has(project.repository.toLowerCase())) throw new TypeError('Duplicate repository');
seen.add(project.repository.toLowerCase());
const repoLower = project.repository.toLowerCase();
if (seen.has(repoLower)) throw new TypeError('Duplicate repository');
seen.add(repoLower);
for (const key of ['name', 'description', 'scope']) if (typeof project[key] !== 'string' || !project[key].trim() || project[key].length > 500) throw new TypeError(`Invalid ${key}`);
}
return input;
Expand Down
3 changes: 2 additions & 1 deletion src/privacy.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const escapeHtml = (value) => String(value).replace(/[&<>"']/g, (character) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' })[character]);
const ESCAPE_MAP = { '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' };
const escapeHtml = (value) => String(value).replace(/[&<>"']/g, (character) => ESCAPE_MAP[character]);

// Escape first, then link only https URLs and plain e-mail addresses (no raw HTML from data).
const linkify = (text) => escapeHtml(text)
Expand Down
27 changes: 27 additions & 0 deletions test/benchmark.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { readFile } from 'node:fs/promises';
import { renderDirectory } from '../src/index.mjs';
import { renderPrivacyPage } from '../src/privacy.mjs';

test('performance benchmark: renderDirectory executes quickly over thousands of iterations', async () => {
const directory = JSON.parse(await readFile(new URL('../data/directory.json', import.meta.url), 'utf8'));
const start = performance.now();
for (let i = 0; i < 5000; i++) {
renderDirectory(directory);
}
const end = performance.now();
const duration = end - start;
assert.ok(duration < 2000, `renderDirectory 5000 iterations took ${duration.toFixed(2)}ms, expected under 2000ms`);
});

test('performance benchmark: renderPrivacyPage executes quickly over thousands of iterations', async () => {
const privacy = JSON.parse(await readFile(new URL('../data/privacy-discord-bot.json', import.meta.url), 'utf8'));
const start = performance.now();
for (let i = 0; i < 5000; i++) {
renderPrivacyPage(privacy);
}
const end = performance.now();
const duration = end - start;
assert.ok(duration < 2000, `renderPrivacyPage 5000 iterations took ${duration.toFixed(2)}ms, expected under 2000ms`);
});
Loading