clone

push a directory of html from the terminal and get a four-character url back. a deno cli on cloudflare

feat/source-stable-push
nzip / worker / src / env.ts
env.ts 1.7K
1// Generated from wrangler.jsonc by `npm run types`.2export type Env = Cloudflare.Env;34export function json<T = unknown>(5 body: T,6 status = 200,7 headers: HeadersInit = {},8): Response {9 return new Response(JSON.stringify(body), {10 status,11 headers: { "content-type": "application/json; charset=utf-8", ...headers },12 });13}1415export function err(message: string, status: number): Response {16 return json({ error: message }, status, { "cache-control": "no-store" });17}1819function configuredSiteDomain(env: Env): string {20 const domain = env.SITE_DOMAIN.toLowerCase();21 if (22 domain.length > 253 || domain.includes("..") ||23 !/^[a-z0-9](?:[a-z0-9.-]*[a-z0-9])?$/.test(domain)24 ) {25 throw new Error("invalid SITE_DOMAIN");26 }27 return domain;28}2930export function isControlOrigin(env: Env, url: URL): boolean {31 const publicUrl = new URL(env.PUBLIC_BASE);32 return url.hostname === publicUrl.hostname &&33 (url.port === "" || url.port === publicUrl.port);34}3536export function siteAddressFromUrl(env: Env, url: URL): string | null {37 const publicUrl = new URL(env.PUBLIC_BASE);38 if (url.port !== "" && url.port !== publicUrl.port) {39 return null;40 }41 const suffix = `.${configuredSiteDomain(env)}`;42 if (!url.hostname.endsWith(suffix)) return null;43 const address = url.hostname.slice(0, -suffix.length);44 return /^[0-9a-f]{4}$/.test(address) ? address : null;45}4647export function siteUrl(48 env: Env,49 address: string,50 pathname = "/",51 search = "",52): string {53 const url = new URL(env.PUBLIC_BASE);54 url.hostname = `${address}.${configuredSiteDomain(env)}`;55 url.pathname = pathname;56 url.search = search;57 url.hash = "";58 return url.toString();59}