From 86ab1d894d9a41434b051afbd53f9a272ea39d90 Mon Sep 17 00:00:00 2001 From: Cory Dransfeldt Date: Sun, 24 Mar 2024 12:40:42 -0700 Subject: [PATCH] chore: debug --- api/event.js | 29 ++++++++++++++++++++++++++++- package-lock.json | 14 ++++++++++++-- package.json | 1 + src/_includes/base.liquid | 5 +++++ src/assets/scripts/index.js | 15 +++++++++++++++ 5 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 src/assets/scripts/index.js diff --git a/api/event.js b/api/event.js index 0472d12c..151e4160 100644 --- a/api/event.js +++ b/api/event.js @@ -1,5 +1,32 @@ +import crypto from 'node:crypto' +import { getStore } from '@netlify/blobs'; + export default async (request, context) => { - fetch(`https://cdn.usefathom.com/?h=${encodeURIComponent(request['referer'] || 'https://coryd.dev/unknown')}&sid=CWSVCDJC&cid=${context['requestId']}`) + const ns = new URL(request['url']).searchParams.get('ns') + const id = crypto.createHash('md5').update(`${context['ip']}${context['geo']['city']}`).digest('hex'); + const page = new URL(request['url']).searchParams.get('page') + const num = new URL(request['url']).searchParams.get('num') + const lang = new URL(request['url']).searchParams.get('lang') + const nav = new URL(request['url']).searchParams.get('nav') + const i = new URL(request['url']).searchParams.get('i') + const headers = {} + if (lang) headers['Accept-Language'] = lang; + if (nav) headers['User-Agent'] = nav; + let url; + + if (i) return; + + if (ns) { + const ids = getStore('ids') + if (!ids.get(id)) ids.set(id, '') + const id = ids.get(id) + url = `https://cdn.usefathom.com/?h=${encodeURIComponent(page)}&sid=CWSVCDJC&cid=${id}` + } else { + url = `https://cdn.usefathom.com/?h=${encodeURIComponent(page)}&sid=CWSVCDJC&cid=${num}` + } + + fetch(`https://cdn.usefathom.com/?h=${encodeURIComponent(page)}&sid=CWSVCDJC&cid=${num}`, + { headers }) .then((data) => { console.log(data) return {} diff --git a/package-lock.json b/package-lock.json index 1313774a..4be769f1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "coryd.dev", - "version": "8.4.4", + "version": "8.6.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "coryd.dev", - "version": "8.4.4", + "version": "8.6.2", "license": "MIT", "dependencies": { "@cdransf/api-text": "^1.2.2", @@ -27,6 +27,7 @@ "@11ty/eleventy-plugin-syntaxhighlight": "^5.0.0", "@aws-sdk/client-s3": "^3.537.0", "@cdransf/eleventy-plugin-tabler-icons": "^1.1.0", + "@netlify/blobs": "^7.0.1", "@rknightuk/eleventy-plugin-post-graph": "^1.0.6", "child_process": "^1.0.2", "dotenv-flow": "^4.1.0", @@ -1746,6 +1747,15 @@ "@jridgewell/sourcemap-codec": "^1.4.14" } }, + "node_modules/@netlify/blobs": { + "version": "7.0.1", + "resolved": "https://registry.npmjs.org/@netlify/blobs/-/blobs-7.0.1.tgz", + "integrity": "sha512-TPx1QzdPcrtBLFXDsv+BOJg5ip/g4trKaot1MZIUV/uI6H0Islg3gU4AGuH5hAMZOr6VlRLWicFhGX7ihLz4eg==", + "dev": true, + "engines": { + "node": "^14.16.0 || >=16.0.0" + } + }, "node_modules/@nodelib/fs.scandir": { "version": "2.1.5", "resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz", diff --git a/package.json b/package.json index f2342d32..dcacf4a7 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ "@11ty/eleventy-plugin-syntaxhighlight": "^5.0.0", "@aws-sdk/client-s3": "^3.537.0", "@cdransf/eleventy-plugin-tabler-icons": "^1.1.0", + "@netlify/blobs": "^7.0.1", "@rknightuk/eleventy-plugin-post-graph": "^1.0.6", "child_process": "^1.0.2", "dotenv-flow": "^4.1.0", diff --git a/src/_includes/base.liquid b/src/_includes/base.liquid index e35dcd37..9d37b0f0 100644 --- a/src/_includes/base.liquid +++ b/src/_includes/base.liquid @@ -15,6 +15,10 @@ {% render "../assets/styles/index.css" %} {% endcapture %} + {% capture js %} + {% render "../assets/scripts/index.js" %} + {% endcapture %} + @@ -86,6 +90,7 @@ {% endif %} diff --git a/src/assets/scripts/index.js b/src/assets/scripts/index.js new file mode 100644 index 00000000..6dd838cc --- /dev/null +++ b/src/assets/scripts/index.js @@ -0,0 +1,15 @@ +const id = Math.floor(Math.random() * Date.now()) +let i; +if (window.sessionStorage && !window.sessionStorage?.getItem('id')) sessionStorage.setItem('id', id) +if (window.localStorage && window.localStorage.getItem('i')) i = localStorage.getItem('i') +if (!window.fathom) { + fetch(`https://coryd.dev/api/event/?page=${encodeURIComponent(window.location.href)}&num=${sessionStorage.getItem('id' || id)}&lang=${navigator.language}&nav=${navigator.userAgent}&i=${i}`) + .then((data) => { + console.log(data) + return {} + }) + .catch(err => { + console.log(err); + return {} + }); +} \ No newline at end of file