From 826e0cd2dd68fce3739f6b16ae56a181f9d8c8e9 Mon Sep 17 00:00:00 2001 From: multipleof4 Date: Tue, 6 Oct 2026 16:29:49 -0700 Subject: [PATCH] Refactor: Move daily search limits into rate-limit.js Co-Authored-By: Claude Opus 5.5 --- functions/[[path]].js | 3 ++- functions/_utils/rate-limit.js | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 functions/_utils/rate-limit.js diff --git a/functions/[[path]].js b/functions/[[path]].js index 451299c..c678db0 100644 --- a/functions/[[path]].js +++ b/functions/[[path]].js @@ -2,6 +2,7 @@ import { braveImageSearch } from "./_utils/brave.js"; import { bingImageSearchFallback } from "./_utils/bing.js"; import { freeImageUrls, parseFreeSource, FREE_UA } from "./_utils/free.js"; import { isFlickrPlaceholder } from "./_utils/flickr.js"; +import { DAILY_LIMIT, FREE_DAILY_LIMIT } from "./_utils/rate-limit.js"; const TTL_SECONDS = 90 * 24 * 60 * 60; const MAX_INDEX = 20; @@ -56,7 +57,7 @@ export async function onRequest(context) { const today = new Date().toISOString().slice(0, 10); // free.* gets its own counter (prefixed so main-site ids stay unchanged) since it never spends Brave credits const rateId = await sha256(`${free ? "free:" : ""}${ip}:${today}`); - const limit = free ? 100 : 35; + const limit = free ? FREE_DAILY_LIMIT : DAILY_LIMIT; let count = 1; if (env.SURREAL_URL && env.SURREAL_USER && env.SURREAL_PASS) { diff --git a/functions/_utils/rate-limit.js b/functions/_utils/rate-limit.js new file mode 100644 index 0000000..587a57d --- /dev/null +++ b/functions/_utils/rate-limit.js @@ -0,0 +1,5 @@ +// New searches (cache misses) allowed per IP per day, reset at 00:00 UTC. Cache hits never count. +// Safe to edit by hand; also update the numbers in README.md and index.html. +// The burst limit (~10 req/10s) is a Cloudflare WAF rule, not set here. +export const DAILY_LIMIT = 35; +export const FREE_DAILY_LIMIT = 100;