From bcde1d9ba6ece2abc2ee76cc67a8392cd57ef62f Mon Sep 17 00:00:00 2001 From: Aaron Parker Date: Sun, 26 Oct 2025 12:20:31 +1100 Subject: [PATCH 1/4] Log only valid API requests to R2 Updated R2 logging to only log requests to main API endpoints, excluding health checks and invalid paths. Added logic to determine valid endpoints and a flag to control logging behavior. --- src/index.js | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index 3998985..99907b8 100644 --- a/src/index.js +++ b/src/index.js @@ -128,9 +128,9 @@ async function getCachedDataSimple(request, key, kvKey) { } } -// R2 logging function -async function storeLogToR2(request, startTime) { - if (!ensureLogsBucketBinding()) { +// R2 logging function - only logs requests to valid endpoints +async function storeLogToR2(request, startTime, shouldLog = true) { + if (!ensureLogsBucketBinding() || !shouldLog) { return } @@ -406,12 +406,25 @@ app.get('/', async (req, res) => { // Event listener with R2 logging addEventListener('fetch', event => { const startTime = Date.now() + const url = new URL(event.request.url) + const path = url.pathname + console.log('Event received:', event.request.method, event.request.url) + // Determine if this request should be logged to R2 + // Only log requests to main API endpoints, exclude health checks and invalid paths + const validEndpoints = ['/apps', '/app/', '/endpoints/versions', '/endpoints/downloads'] + const shouldLog = validEndpoints.some(endpoint => + path === endpoint || path.startsWith(endpoint) + ) && path !== '/health' + event.respondWith( app.handleRequest(event.request).then(response => { // Store log to R2 asynchronously (don't await to avoid delaying response) - event.waitUntil(storeLogToR2(event.request, startTime)) + // Only log if it's a valid endpoint + if (shouldLog) { + event.waitUntil(storeLogToR2(event.request, startTime, true)) + } return response }) ) From a59043a835369237a890a90c891a394b6ceca099 Mon Sep 17 00:00:00 2001 From: Aaron Parker Date: Sun, 26 Oct 2025 12:51:03 +1100 Subject: [PATCH 2/4] Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 99907b8..7e8b886 100644 --- a/src/index.js +++ b/src/index.js @@ -129,8 +129,8 @@ async function getCachedDataSimple(request, key, kvKey) { } // R2 logging function - only logs requests to valid endpoints -async function storeLogToR2(request, startTime, shouldLog = true) { - if (!ensureLogsBucketBinding() || !shouldLog) { +async function storeLogToR2(request, startTime) { + if (!ensureLogsBucketBinding()) { return } From 356961ad021a0ee4d845479287b73bd78691e1f9 Mon Sep 17 00:00:00 2001 From: Aaron Parker Date: Sun, 26 Oct 2025 12:51:40 +1100 Subject: [PATCH 3/4] Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 7e8b886..c6c8272 100644 --- a/src/index.js +++ b/src/index.js @@ -423,7 +423,7 @@ addEventListener('fetch', event => { // Store log to R2 asynchronously (don't await to avoid delaying response) // Only log if it's a valid endpoint if (shouldLog) { - event.waitUntil(storeLogToR2(event.request, startTime, true)) + event.waitUntil(storeLogToR2(event.request, startTime)) } return response }) From ae6b052b63d86602be9e86f18714ca23c01fbad7 Mon Sep 17 00:00:00 2001 From: Aaron Parker Date: Sun, 26 Oct 2025 13:17:11 +1100 Subject: [PATCH 4/4] Update src/index.js Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/index.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index c6c8272..4b9a743 100644 --- a/src/index.js +++ b/src/index.js @@ -413,10 +413,13 @@ addEventListener('fetch', event => { // Determine if this request should be logged to R2 // Only log requests to main API endpoints, exclude health checks and invalid paths - const validEndpoints = ['/apps', '/app/', '/endpoints/versions', '/endpoints/downloads'] - const shouldLog = validEndpoints.some(endpoint => - path === endpoint || path.startsWith(endpoint) - ) && path !== '/health' + // Define exact match endpoints and prefix match endpoints separately + const exactEndpoints = ['/apps', '/app', '/endpoints/versions', '/endpoints/downloads']; + const prefixEndpoints = ['/app/', '/apps/', '/endpoints/versions/', '/endpoints/downloads/']; + const shouldLog = ( + exactEndpoints.includes(path) || + prefixEndpoints.some(endpoint => path.startsWith(endpoint)) + ) && path !== '/health'; event.respondWith( app.handleRequest(event.request).then(response => {