mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
55 lines
2.6 KiB
TypeScript
55 lines
2.6 KiB
TypeScript
// hex.kitlangton.com is the one Hex site. hex.kitlangton.dev, plain
|
|
// HTTP, and workers.dev all 301 to the same path and query there. Hashed build
|
|
// assets get a long cache. `wrangler dev --var ANY_HOST:1` turns the redirect
|
|
// off for local testing.
|
|
type Env = { ASSETS: { fetch(request: Request): Promise<Response> }; ANY_HOST?: string }
|
|
|
|
const HOST = "hex.kitlangton.com"
|
|
|
|
export default {
|
|
async fetch(request: Request, env: Env): Promise<Response> {
|
|
const url = new URL(request.url)
|
|
if (!env.ANY_HOST && (url.hostname !== HOST || url.protocol !== "https:")) {
|
|
url.protocol = "https:"
|
|
url.hostname = HOST
|
|
url.port = ""
|
|
return Response.redirect(url.toString(), 301)
|
|
}
|
|
|
|
const response = await env.ASSETS.fetch(request)
|
|
const headers = new Headers(response.headers)
|
|
if (url.pathname.startsWith("/assets/")) {
|
|
headers.set("Cache-Control", "public, max-age=31536000, immutable")
|
|
}
|
|
headers.set("Strict-Transport-Security", "max-age=31536000")
|
|
headers.set("X-Content-Type-Options", "nosniff")
|
|
headers.set("Referrer-Policy", "strict-origin-when-cross-origin")
|
|
if (url.pathname.endsWith(".mp4")) return ranged(request, request.method === "HEAD" ? await env.ASSETS.fetch(new Request(request.url, { headers: request.headers })) : response, headers)
|
|
return new Response(response.body, {
|
|
status: response.status,
|
|
statusText: response.statusText,
|
|
headers,
|
|
})
|
|
},
|
|
}
|
|
|
|
// Static assets ignore Range, but Safari and AVFoundation need byte ranges to play video.
|
|
// A HEAD is answered from the full GET so its headers match.
|
|
async function ranged(request: Request, response: Response, headers: Headers): Promise<Response> {
|
|
headers.set("Accept-Ranges", "bytes")
|
|
const match = /^bytes=(\d*)-(\d*)$/.exec(request.headers.get("Range") ?? "")
|
|
if (response.status !== 200 || !match || (!match[1] && !match[2])) {
|
|
return new Response(response.body, { status: response.status, statusText: response.statusText, headers })
|
|
}
|
|
const body = await response.arrayBuffer()
|
|
const size = body.byteLength
|
|
const start = match[1] ? Number(match[1]) : Math.max(0, size - Number(match[2]))
|
|
const end = match[1] && match[2] ? Math.min(Number(match[2]), size - 1) : size - 1
|
|
if (start > end) {
|
|
headers.set("Content-Range", `bytes */${size}`)
|
|
return new Response(null, { status: 416, headers })
|
|
}
|
|
headers.set("Content-Range", `bytes ${start}-${end}/${size}`)
|
|
headers.set("Content-Length", String(end - start + 1))
|
|
return new Response(request.method === "HEAD" ? null : body.slice(start, end + 1), { status: 206, headers })
|
|
}
|