blob: db20a67718fa31c5a14a68a084c5434171b0df5b [file] [log] [blame]
j5e67d8e2022-11-08 22:55:04 -08001/**
2 * Power+ Production Server v2
j6e1a7bf2026-10-13 03:37:04 +00003 * Copyright (c) jottocraft 2022
j5e67d8e2022-11-08 22:55:04 -08004 */
5
6import { getAssetFromKV, NotFoundError, MethodNotAllowedError } from '@cloudflare/kv-asset-handler';
7import manifestJSON from '__STATIC_CONTENT_MANIFEST';
8import UAParser from "ua-parser-js";
9
10const assetManifest = JSON.parse(manifestJSON);
11
12async 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;
j9e24de42022-11-10 14:26:05 -080017 const uh = url.searchParams.get("uh").substring(0, 32);
j5e67d8e2022-11-08 22:55:04 -080018 const country = request.cf.country;
19 const region = request.cf.regionCode;
jf1af28d2022-11-08 23:24:52 -080020 const deviceOS = new UAParser(request.headers.get("User-Agent")).getOS().name;
j5e67d8e2022-11-08 22:55:04 -080021 const isp = request.cf.asOrganization;
22
23 //Record statistics
j9e24de42022-11-10 14:26:05 -080024 env.STATS.writeDataPoint({
j5e67d8e2022-11-08 22:55:04 -080025 blobs: [
26 canvasInstance,
27 entrypoint,
28 country,
29 region,
30 colo,
31 deviceOS,
32 isp
33 ],
34 indexes: [uh]
35 });
36}
37
38async 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
j93d65ac2022-11-08 23:07:30 -080053 ctx.waitUntil((async function () {
j5e67d8e2022-11-08 22:55:04 -080054 //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")
j9e24de42022-11-10 14:26:05 -080060 && (url.searchParams.get("uh") !== "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855") //filter empty/garbage data (crx <3.1.2)
j5e67d8e2022-11-08 22:55:04 -080061 ) {
62 logAnalyticsEvent(url, request, env, ctx);
63 }
j93d65ac2022-11-08 23:07:30 -080064 })());
j5e67d8e2022-11-08 22:55:04 -080065
66 return res;
67 } catch (e) {
68 if (e instanceof NotFoundError) {
jfe190372023-02-26 09:24:46 -080069 return env.HTTP_STATUS_PAGES.fetch("https://http-status-pages.jottocraft.com/?status=404&brand=dtps");
j5e67d8e2022-11-08 22:55:04 -080070 } else if (e instanceof MethodNotAllowedError) {
jfe190372023-02-26 09:24:46 -080071 return env.HTTP_STATUS_PAGES.fetch("https://http-status-pages.jottocraft.com/?status=405&brand=dtps");
j5e67d8e2022-11-08 22:55:04 -080072 } else {
jfe190372023-02-26 09:24:46 -080073 return env.HTTP_STATUS_PAGES.fetch("https://http-status-pages.jottocraft.com/?status=500&brand=dtps");
j5e67d8e2022-11-08 22:55:04 -080074 }
75 }
76}
77
78export default {
79 async fetch(request, env, ctx) {
80 return await serveKV(request, env, ctx);
81 }
82};