| j | 5e67d8e | 2022-11-08 22:55:04 -0800 | [diff] [blame] | 1 | /** |
| 2 | * Power+ Production Server v2 |
| j | 6e1a7bf | 2026-10-13 03:37:04 +0000 | [diff] [blame] | 3 | * Copyright (c) jottocraft 2022 |
| j | 5e67d8e | 2022-11-08 22:55:04 -0800 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | import { getAssetFromKV, NotFoundError, MethodNotAllowedError } from '@cloudflare/kv-asset-handler'; |
| 7 | import manifestJSON from '__STATIC_CONTENT_MANIFEST'; |
| 8 | import UAParser from "ua-parser-js"; |
| 9 | |
| 10 | const assetManifest = JSON.parse(manifestJSON); |
| 11 | |
| 12 | async function logAnalyticsEvent(url, request, env, ctx) { |
| 13 | //Collect statistics about this event |
| 14 | const entrypoint = url.pathname.split("/").pop(); |
| 15 | const colo = request.cf.colo; |
| 16 | const canvasInstance = new URL(request.headers.get("Referer")).hostname; |
| j | 9e24de4 | 2022-11-10 14:26:05 -0800 | [diff] [blame] | 17 | const uh = url.searchParams.get("uh").substring(0, 32); |
| j | 5e67d8e | 2022-11-08 22:55:04 -0800 | [diff] [blame] | 18 | const country = request.cf.country; |
| 19 | const region = request.cf.regionCode; |
| j | f1af28d | 2022-11-08 23:24:52 -0800 | [diff] [blame] | 20 | const deviceOS = new UAParser(request.headers.get("User-Agent")).getOS().name; |
| j | 5e67d8e | 2022-11-08 22:55:04 -0800 | [diff] [blame] | 21 | const isp = request.cf.asOrganization; |
| 22 | |
| 23 | //Record statistics |
| j | 9e24de4 | 2022-11-10 14:26:05 -0800 | [diff] [blame] | 24 | env.STATS.writeDataPoint({ |
| j | 5e67d8e | 2022-11-08 22:55:04 -0800 | [diff] [blame] | 25 | blobs: [ |
| 26 | canvasInstance, |
| 27 | entrypoint, |
| 28 | country, |
| 29 | region, |
| 30 | colo, |
| 31 | deviceOS, |
| 32 | isp |
| 33 | ], |
| 34 | indexes: [uh] |
| 35 | }); |
| 36 | } |
| 37 | |
| 38 | async function serveKV(request, env, ctx) { |
| 39 | const url = new URL(request.url); |
| 40 | |
| 41 | try { |
| 42 | const res = await getAssetFromKV( |
| 43 | { |
| 44 | request, |
| 45 | waitUntil: (promise) => ctx.waitUntil(promise) |
| 46 | }, |
| 47 | { |
| 48 | ASSET_NAMESPACE: env.__STATIC_CONTENT, |
| 49 | ASSET_MANIFEST: assetManifest |
| 50 | } |
| 51 | ); |
| 52 | |
| j | 93d65ac | 2022-11-08 23:07:30 -0800 | [diff] [blame] | 53 | ctx.waitUntil((async function () { |
| j | 5e67d8e | 2022-11-08 22:55:04 -0800 | [diff] [blame] | 54 | //Log analytics event |
| 55 | if ( |
| 56 | url.pathname.startsWith("/scripts/lms/") |
| 57 | && url.pathname.endsWith(".js") |
| 58 | && (url.searchParams.get("upstream") !== "true") |
| 59 | && url.searchParams.get("uh") |
| j | 9e24de4 | 2022-11-10 14:26:05 -0800 | [diff] [blame] | 60 | && (url.searchParams.get("uh") !== "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855") //filter empty/garbage data (crx <3.1.2) |
| j | 5e67d8e | 2022-11-08 22:55:04 -0800 | [diff] [blame] | 61 | ) { |
| 62 | logAnalyticsEvent(url, request, env, ctx); |
| 63 | } |
| j | 93d65ac | 2022-11-08 23:07:30 -0800 | [diff] [blame] | 64 | })()); |
| j | 5e67d8e | 2022-11-08 22:55:04 -0800 | [diff] [blame] | 65 | |
| 66 | return res; |
| 67 | } catch (e) { |
| 68 | if (e instanceof NotFoundError) { |
| j | fe19037 | 2023-02-26 09:24:46 -0800 | [diff] [blame] | 69 | return env.HTTP_STATUS_PAGES.fetch("https://http-status-pages.jottocraft.com/?status=404&brand=dtps"); |
| j | 5e67d8e | 2022-11-08 22:55:04 -0800 | [diff] [blame] | 70 | } else if (e instanceof MethodNotAllowedError) { |
| j | fe19037 | 2023-02-26 09:24:46 -0800 | [diff] [blame] | 71 | return env.HTTP_STATUS_PAGES.fetch("https://http-status-pages.jottocraft.com/?status=405&brand=dtps"); |
| j | 5e67d8e | 2022-11-08 22:55:04 -0800 | [diff] [blame] | 72 | } else { |
| j | fe19037 | 2023-02-26 09:24:46 -0800 | [diff] [blame] | 73 | return env.HTTP_STATUS_PAGES.fetch("https://http-status-pages.jottocraft.com/?status=500&brand=dtps"); |
| j | 5e67d8e | 2022-11-08 22:55:04 -0800 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | export default { |
| 79 | async fetch(request, env, ctx) { |
| 80 | return await serveKV(request, env, ctx); |
| 81 | } |
| 82 | }; |