Help rewriting TLD with a worker?

Wondering if someone has an example of using a worker to rewrite the top-level domain?

I’m trying to use a B2 bucket as the backend. I setup a cname for @ in my domain and pointed it to f002… I setup a page rule, a route, and this worker. This is successfully rewriting the URL to rewrite the default sub directory. But it doesn’t handle the top level. I am really trying to read and understand what’s going on but this is outside of my skillset and for some reason it’s not sinking in.

Does anyone have any example code that I could add to this to make it handle the TLD so that it rewrites to

async function handleRequest(request) {
    let url = new URL(request.url)
    // make sure you set B2_BUCKET_NAME as an environment variable
    url.pathname = `/file/${B2_BUCKET_NAME}${url.pathname}`
    let modified = new Request(url.toString(), request)
    let response = await fetch(modified, {
        cf: { cacheTtl: parseInt(CF_CACHE_TTL) }
    let respURL = new URL(response.url)

    // customize Content-Type headers for various extensions here if you like
    if (/\.(pub|boot|cfg)$/.test(respURL.pathname)) {
        response = new Response(response.body, response)
        response.headers.set('Content-Type', 'text/plain')
    return response

addEventListener('fetch', event => {

Just build the URL in its entirety.

let url = new URL(request.url)
let b2Url = new URL(`https://`${B2_HOST}/file/${B2_BUCKET_NAME}${url.pathname}`