{"size": 2172, "ext": "js", "max_stars_repo_path": "workers-site/handler.js", "max_stars_repo_name": "natsupon/matome", "max_stars_repo_stars_event_min_datetime": "2021-07-20T18:36:23.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-08T21:51:17.000Z", "max_issues_repo_path": "workers-site/handler.js", "max_issues_repo_name": "natsupon/matome", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "workers-site/handler.js", "max_forks_repo_name": "natsupon/matome", "max_forks_repo_forks_event_min_datetime": "2021-07-30T12:42:57.000Z", "max_forks_repo_forks_event_max_datetime": "2022-01-08T11:03:00.000Z"}
{"max_stars_count": 39, "max_issues_count": null, "max_forks_count": 2, "avg_line_length": 32.4179104478, "max_line_length": 76, "alphanum_fraction": 0.5069060773}
import { getAssetFromKV } from '@cloudflare/kv-asset-handler';
import { redirects } from './data.js';

const GH_URL = `https://github.com/VisheshBansal`;

export async function handleRequest(event) {
    return performRedirect(event);
}

async function getPageFromKV(event) {
    const url = new URL(event.request.url);
    let options = {};
    try {
        return await getAssetFromKV(event, options);
        //on error try appending html
    } catch (e) {
        try {
            const options = {
                mapRequestToAsset: (req) =>
                    new Request(`${new URL(req.url)}.html`, req),
            };
            const page = await getAssetFromKV(event, options);
            return new Response(page.body, page);
            //on error return 404
        } catch (e) {
            let notFoundResponse = await getAssetFromKV(event, {
                mapRequestToAsset: (req) =>
                    new Request(`${new URL(req.url).origin}/404.html`, req),
            });
            return new Response(notFoundResponse.body, {
                ...notFoundResponse,
                status: 404,
            });
        }
        // return new Response(e.message || e.toString(), { status: 500 });
    }
}

async function performRedirect(event) {
    const urlParts = event.request.url
        .split('?')[0]
        .replace(BASE_URL, '')
        .split('/')
        .map((s) => s.toLowerCase());
    if (redirects[urlParts[0]]) {
        return Response.redirect(redirects[urlParts[0]]['url'], 301);
    }
    if (urlParts[0] == 'gh') {
        switch (urlParts.length) {
            case 1:
                return Response.redirect(GH_URL, 301);
            case 2:
                return Response.redirect(`${GH_URL}/${urlParts[1]}`, 301);
            case 3:
                return Response.redirect(
                    `${GH_URL}/${urlParts[1]}/commit/${urlParts[2]}`,
                    301
                );
            case 4:
                return Response.redirect(
                    `${GH_URL}/${urlParts[1]}/issues/${urlParts[3]}`,
                    301
                );
        }
    }
    return getPageFromKV(event);
}