import { getAssetFromKV } from '@cloudflare/kv-asset-handler'

const STATIC_HEADERS = [
    [
        'Content-Security-Policy',
        "default-src 'none'; connect-src 'self' https://fonts.googleapis.com https://fonts.gstatic.com; font-src 'self' https://fonts.gstatic.com; img-src 'self' data:; manifest-src 'self'; script-src 'self' 'unsafe-inline' https://static.cloudflareinsights.com; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com",
    ],
    [
        'Link',
        "<https://fonts.googleapis.com/css?family=IBM+Plex+Sans+Condensed:400,600|IBM+Plex+Serif:300> rel='preload' as='style' crossorigin='anonymous'",
    ],
    ['Referrer-Policy', 'strict-origin-when-cross-origin'],
    ['Strict-Transport-Security', 'max-age=63072000; includeSubdomains; preload'],
    ['X-Content-Type-Options', 'nosniff'],
    ['X-Frame-Options', 'DENY'],
    ['X-XSS-Protection', '1; mode=block'],
]

const withResponseHeaders = async (response) => {
    if (!response.ok) return response

    for (const [key, value] of STATIC_HEADERS) {
        response.headers.set(key, value)
    }

    return response
}

const getResponse = async (event) => {
    try {
        const response = await getAssetFromKV(event)
        return withResponseHeaders(response)
    } catch (error) {
        try {
            const notFoundResponse = await getAssetFromKV(event, {
                mapRequestToAsset: (req) => new Request(`${new URL(req.url).origin}/404.html`, req),
            })

            return withResponseHeaders(
                new Response(notFoundResponse.body, {
                    ...notFoundResponse,
                    status: 404,
                })
            )
        } catch {}

        return new Response(error.message || error.toString(), { status: 500 })
    }
}

addEventListener('fetch', async (event) => {
    try {
        const response = getResponse(event)
        return event.respondWith(response)
    } catch {
        event.respondWith(new Response('Internal Error', { status: 500 }))
    }
})

@@ -56,16 +56,1054 @@
 ndler'%0A%0A
+const isPlaintextRequest = (request) =%3E %7B%0A    const acceptHeader = request.headers.get('Accept')%0A    const isAcceptPlaintext = !!acceptHeader && acceptHeader.includes('text/plain')%0A%0A    if (isAcceptPlaintext) %7B%0A        return true%0A    %7D%0A%0A    const userAgentHeader = request.headers.get('User-Agent')%0A    const isCurlUserAgent = userAgentHeader && userAgentHeader.startsWith('curl')%0A    const isWgetUserAgent = userAgentHeader && userAgentHeader.startsWith('Wget')%0A%0A    if (isCurlUserAgent %7C%7C isWgetUserAgent) %7B%0A        return true%0A    %7D%0A%0A    return false%0A%7D%0A%0Aconst PLAINTEXT_BODY = %60Iiro J%C3%A4ppinen%0A- https://github.com/iiroj%0A- https://fi.linkedin.com/in/iiroj%0A%60%0A%0Aconst PLAINTEXT_OPTIONS = %7B headers: %7B 'Content-Type': 'text/plain' %7D %7D%0A%0Aconst getPlaintextResponse = async (event) =%3E %7B%0A    const url = new URL(event.request.url)%0A%0A    if (url.pathname === '/') %7B%0A        return new Response(PLAINTEXT_BODY, %7B ...PLAINTEXT_OPTIONS, status: 200 %7D)%0A    %7D%0A%0A    return new Response('404 %E2%80%94 Page Not Found', %7B ...PLAINTEXT_OPTIONS, status: 404 %7D)%0A%7D%0A%0A
 const ST
@@ -2874,32 +2874,214 @@
  =%3E %7B%0A    try %7B%0A
+        if (isPlaintextRequest(event.request)) %7B%0A            const plaintextResponse = getPlaintextResponse(event)%0A            return event.respondWith(plaintextResponse)%0A        %7D%0A%0A
         const re

add support for plaintext response