From a40443a733f79000c27da3feef079c8d2276351f Mon Sep 17 00:00:00 2001 From: multipleof4 Date: Fri, 26 Sep 2025 21:23:31 -0700 Subject: [PATCH] Refactor: Use incremental slug length on collision --- functions/[[path]].js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/functions/[[path]].js b/functions/[[path]].js index 600bf62..499a262 100644 --- a/functions/[[path]].js +++ b/functions/[[path]].js @@ -1,4 +1,4 @@ -const C='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789',L=4,H={'Access-Control-Allow-Origin':'*'},r=_=>[...Array(L)].map(_=>C[Math.random()*C.length|0]).join(''),n=u=>{try{return new URL(u).href}catch(_){try{return new URL('https://'+u).href}catch(_){}}} +const C='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789',H={'Access-Control-Allow-Origin':'*'},r=l=>[...Array(l)].map(_=>C[Math.random()*C.length|0]).join(''),n=u=>{try{return new URL(u).href}catch(_){try{return new URL('https://'+u).href}catch(_){}}} export async function onRequest({request,env,next}){ if(request.method==='OPTIONS')return new Response(null,{headers:{...H,'Access-Control-Allow-Methods':'POST','Access-Control-Allow-Headers':'Content-Type'}}) @@ -9,7 +9,7 @@ export async function onRequest({request,env,next}){ if(!k||!(await(await fetch('https://www.google.com/recaptcha/api/siteverify',{method:'POST',headers:{'Content-Type':'application/x-www-form-urlencoded'},body:`secret=${env.RECAPCHA_KEY}&response=${k}`})).json()).success) return new Response('CAPTCHA failed',{status:403,headers:H}) t=n(t);if(!t)throw 0 - let s;do s=r();while(await env.EV.get(s)) + let l=4,s;do s=r(l++);while(await env.EV.get(s)) await env.EV.put(s,t) return new Response(JSON.stringify({slug:s,target:t,shortUrl:`${u.origin}/${s}`}),{headers:{'Content-Type':'application/json',...H}}) }catch(_){return new Response('Invalid request',{status:400,headers:H})}