| 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 | |
| j | 5e67d8e | 2022-11-08 22:55:04 -0800 | [diff] [blame] | 6 | import UAParser from "ua-parser-js"; |
| 7 | |
| j | 5e67d8e | 2022-11-08 22:55:04 -0800 | [diff] [blame] | 8 | async function logAnalyticsEvent(url, request, env, ctx) { |
| 9 | //Collect statistics about this event |
| 10 | const entrypoint = url.pathname.split("/").pop(); |
| 11 | const colo = request.cf.colo; |
| 12 | const canvasInstance = new URL(request.headers.get("Referer")).hostname; |
| j | 9e24de4 | 2022-11-10 14:26:05 -0800 | [diff] [blame] | 13 | const uh = url.searchParams.get("uh").substring(0, 32); |
| j | 5e67d8e | 2022-11-08 22:55:04 -0800 | [diff] [blame] | 14 | const country = request.cf.country; |
| 15 | const region = request.cf.regionCode; |
| j | f1af28d | 2022-11-08 23:24:52 -0800 | [diff] [blame] | 16 | const deviceOS = new UAParser(request.headers.get("User-Agent")).getOS().name; |
| j | 5e67d8e | 2022-11-08 22:55:04 -0800 | [diff] [blame] | 17 | const isp = request.cf.asOrganization; |
| 18 | |
| 19 | //Record statistics |
| j | 9e24de4 | 2022-11-10 14:26:05 -0800 | [diff] [blame] | 20 | env.STATS.writeDataPoint({ |
| j | 5e67d8e | 2022-11-08 22:55:04 -0800 | [diff] [blame] | 21 | blobs: [ |
| 22 | canvasInstance, |
| 23 | entrypoint, |
| 24 | country, |
| 25 | region, |
| 26 | colo, |
| 27 | deviceOS, |
| 28 | isp |
| 29 | ], |
| 30 | indexes: [uh] |
| 31 | }); |
| 32 | } |
| 33 | |
| j | 3043ce7 | 2026-10-13 21:28:45 +0000 | [diff] [blame^] | 34 | async function serveStaticSite(request, env, ctx) { |
| j | 5e67d8e | 2022-11-08 22:55:04 -0800 | [diff] [blame] | 35 | const url = new URL(request.url); |
| 36 | |
| j | 3043ce7 | 2026-10-13 21:28:45 +0000 | [diff] [blame^] | 37 | const assetResponse = await env.ASSETS.fetch(request); |
| j | 5e67d8e | 2022-11-08 22:55:04 -0800 | [diff] [blame] | 38 | |
| j | 3043ce7 | 2026-10-13 21:28:45 +0000 | [diff] [blame^] | 39 | if (assetResponse.status === 404) { |
| 40 | return env.HTTP_STATUS_PAGES.fetch("https://http-status-pages.jottocraft.com/?status=404&brand=dtps"); |
| 41 | } else if (assetResponse.status === 405) { |
| 42 | return env.HTTP_STATUS_PAGES.fetch("https://http-status-pages.jottocraft.com/?status=405&brand=dtps"); |
| 43 | } else if (assetResponse.status >= 500) { |
| 44 | return env.HTTP_STATUS_PAGES.fetch("https://http-status-pages.jottocraft.com/?status=" + assetResponse.status + "&brand=dtps"); |
| 45 | } else { |
| j | 93d65ac | 2022-11-08 23:07:30 -0800 | [diff] [blame] | 46 | ctx.waitUntil((async function () { |
| j | 5e67d8e | 2022-11-08 22:55:04 -0800 | [diff] [blame] | 47 | //Log analytics event |
| 48 | if ( |
| 49 | url.pathname.startsWith("/scripts/lms/") |
| 50 | && url.pathname.endsWith(".js") |
| 51 | && (url.searchParams.get("upstream") !== "true") |
| 52 | && url.searchParams.get("uh") |
| j | 9e24de4 | 2022-11-10 14:26:05 -0800 | [diff] [blame] | 53 | && (url.searchParams.get("uh") !== "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855") //filter empty/garbage data (crx <3.1.2) |
| j | 5e67d8e | 2022-11-08 22:55:04 -0800 | [diff] [blame] | 54 | ) { |
| 55 | logAnalyticsEvent(url, request, env, ctx); |
| 56 | } |
| j | 93d65ac | 2022-11-08 23:07:30 -0800 | [diff] [blame] | 57 | })()); |
| j | 5e67d8e | 2022-11-08 22:55:04 -0800 | [diff] [blame] | 58 | |
| j | 3043ce7 | 2026-10-13 21:28:45 +0000 | [diff] [blame^] | 59 | return assetResponse; |
| j | 5e67d8e | 2022-11-08 22:55:04 -0800 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
| 63 | export default { |
| 64 | async fetch(request, env, ctx) { |
| j | 3043ce7 | 2026-10-13 21:28:45 +0000 | [diff] [blame^] | 65 | return await serveStaticSite(request, env, ctx); |
| j | 5e67d8e | 2022-11-08 22:55:04 -0800 | [diff] [blame] | 66 | } |
| 67 | }; |