Skip to content
Merged
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
12 changes: 11 additions & 1 deletion convex/siteActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ function safeLabel(value: string): string {
.replace(/-+/g, "-");
}

function normalizeBaseDomain(baseDomain: string): string {
return baseDomain
.replace(/^https?:\/\//i, "")
.split("/")[0]
.split(":")[0]
.trim()
.toLowerCase();
}

function randomChoice(values: string[]): string {
return values[randomBytes(1)[0] % values.length];
}
Expand All @@ -118,9 +127,10 @@ function randomDigits(): string {
}

function buildHostname(baseDomain: string): string {
const normalizedBaseDomain = normalizeBaseDomain(baseDomain);
return `${safeLabel(randomChoice(ADJECTIVES))}-${safeLabel(
randomChoice(NOUNS)
)}-${randomDigits()}.${baseDomain}`;
)}-${randomDigits()}.${normalizedBaseDomain}`;
}

function encryptionKey(secret: string): Buffer {
Expand Down
5 changes: 5 additions & 0 deletions scripts/sites.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ const sites = await loadSitesModule();
assert.equal(hostname, "brisk-paper-07.boop.ad");
}

{
const hostname = sites.buildBoopHostname("tiny", "paper", 0, "https://trypoo.app");
assert.equal(hostname, "tiny-paper-00.trypoo.app");
}

{
let calls = 0;
const hostname = await sites.generateMemorableHostname({
Expand Down
15 changes: 13 additions & 2 deletions server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,24 @@ const port = Number(process.env.PORT || 3000);
const distDir = join(process.cwd(), "dist");
const siteCacheControl = "public, max-age=300, s-maxage=86400";

function normalizeBaseDomain(baseDomain: string): string {
return baseDomain
.replace(/^https?:\/\//i, "")
.split("/")[0]
.split(":")[0]
.trim()
.toLowerCase();
}

function configuredAppHostnames(): Set<string> {
const explicit = process.env.APP_HOSTNAMES;
const baseDomain = process.env.SITE_BASE_DOMAIN || process.env.WEBVH_DOMAIN || "boop.ad";
const baseDomain = normalizeBaseDomain(
process.env.SITE_BASE_DOMAIN || process.env.WEBVH_DOMAIN || "boop.ad"
);
const values = explicit
? explicit.split(",")
: [baseDomain, `www.${baseDomain}`, "localhost", "127.0.0.1", "0.0.0.0"];
return new Set(values.map((value) => value.trim().toLowerCase()).filter(Boolean));
return new Set(values.map((value) => normalizeBaseDomain(value)).filter(Boolean));
}

const appHostnames = configuredAppHostnames();
Expand Down
14 changes: 12 additions & 2 deletions src/lib/sites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,23 @@ function safeLabel(value: string): string {
.replace(/-+/g, "-");
}

export function normalizeBaseDomain(baseDomain: string): string {
return baseDomain
.replace(/^https?:\/\//i, "")
.split("/")[0]
.split(":")[0]
.trim()
.toLowerCase();
}

export function buildBoopHostname(
adjective: string,
noun: string,
digits: number,
baseDomain: string
): string {
const suffix = String(Math.abs(digits) % 100).padStart(2, "0");
return `${safeLabel(adjective)}-${safeLabel(noun)}-${suffix}.${baseDomain}`;
return `${safeLabel(adjective)}-${safeLabel(noun)}-${suffix}.${normalizeBaseDomain(baseDomain)}`;
}

export async function generateMemorableHostname(
Expand Down Expand Up @@ -101,5 +110,6 @@ export async function sha256Hex(input: string): Promise<string> {
}

export function appHostnameSet(baseDomain: string): Set<string> {
return new Set([baseDomain, `www.${baseDomain}`]);
const normalized = normalizeBaseDomain(baseDomain);
return new Set([normalized, `www.${normalized}`]);
}
Loading