mirror of
https://github.com/multipleof4/4ev.link.git
synced 2026-01-13 15:57:53 +00:00
Refactor: Use incremental slug length on collision
This commit is contained in:
@@ -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})}
|
||||
|
||||
Reference in New Issue
Block a user