@searchos/bot-proxy 1.0.12 → 1.0.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2 @@
1
+ import{b as P,c as U,d as N}from"./chunk-BVZHYMX6.js";var D=/^\/(?:robots\.txt|llms\.txt|sitemap[^\/]*\.(?:xml|xsl))$/i,L=/mobile|android|iphone|phone|tablet|webos|ipad/i;async function z(e,n){let{projectId:t,cloudfrontDomain:m,renderDomain:a,botLogEndpoint:c,aiRender:u=!0,ocr:x=!0,aiPathPrefixes:C=[]}=n,l=n.siteDomain.replace(/^https?:\/\//,"").replace(/\/+$/,""),J=m.replace(/\/$/,""),g=new URL(e.url),i=g.pathname;if(e.headers.get("x-prerender-host")){if(D.test(i)||i===`/${t}.txt`){let{NextResponse:o}=await import("next/server");return o.next()}return null}if(i===`/${t}.txt`)return new Response(t,{status:200,headers:{"Content-Type":"text/plain","Cache-Control":"public, max-age=3600"}});if(D.test(i)){try{let o=await fetch(`${a}/static${i}`,{headers:{"X-Signature":t,"X-Original-Host":l}});if(o.ok)return new Response(o.body,{status:200,headers:{"Content-Type":o.headers.get("Content-Type")||"text/plain","Cache-Control":"public, max-age=3600"}})}catch{}return null}let h=e.headers.get("user-agent")||"",p=P(h);if(!p)return null;let T=L.test(h)?"mobile":"desktop",I=U(g.search),b=I?`${i}?${I}`:i,f=`https://${l}${b}`,y=W(e),w=await N(b,"ai"),k=await N(b,"desktop"),R=T==="mobile"?await N(b,"mobile"):k,$=`${J}/v2/${l}/${w}.html`,S=`${J}/v2/${l}/${k}.html`,E=`${J}/v2/${l}/${R}.html`,A=p.isAi&&u;console.log("[bot-proxy] req "+JSON.stringify({bot:p.name,isAi:p.isAi,aiRender:u,willTakeAiBranch:A,device:T,fullUrl:e.url,path:i,search:g.search,pathWithQuery:b,targetURL:f,clientIp:y,keys:{ai:w,desktop:k,[T]:R},cdnUrls:{ai:$,desktop:S,[T]:E},headers:{"user-agent":h,"x-forwarded-for":e.headers.get("x-forwarded-for"),"x-real-ip":e.headers.get("x-real-ip"),"x-forwarded-host":e.headers.get("x-forwarded-host"),"cf-connecting-ip":e.headers.get("cf-connecting-ip"),"cloudfront-viewer-address":e.headers.get("cloudfront-viewer-address"),"x-vercel-forwarded-for":e.headers.get("x-vercel-forwarded-for"),"x-amzn-trace-id":e.headers.get("x-amzn-trace-id"),"true-client-ip":e.headers.get("true-client-ip"),referer:e.headers.get("referer"),"accept-language":e.headers.get("accept-language"),host:e.headers.get("host")},headerKeys:Array.from(e.headers.keys())}));try{if(A){let s=null;try{s=await fetch($,{headers:{Accept:"text/html"}}),console.log("[bot-proxy] cf-ai "+JSON.stringify({key:w,url:$,status:s.status,ok:s.ok}))}catch(r){console.error("[bot-proxy] cf-ai THROW "+JSON.stringify({key:w,url:$,name:r?.name,msg:r?.message}))}if(s&&s.ok){let r="";try{r=await s.text()}catch(_){console.error("[bot-proxy] cf-ai TEXT-THROW "+JSON.stringify({msg:_?.message}))}if(console.log("[bot-proxy] cf-ai body "+JSON.stringify({length:r.length})),r&&r.length>100)return O(c,y,t,l,i,g.search,h,"hit_ai"),H(a,t,f,x,C),j(r,"HIT-AI",p.name)}let d=null;try{d=await fetch(S,{headers:{Accept:"text/html"}}),console.log("[bot-proxy] cf-desktop-fallback "+JSON.stringify({key:k,url:S,status:d.status,ok:d.ok}))}catch(r){console.error("[bot-proxy] cf-desktop-fallback THROW "+JSON.stringify({key:k,url:S,name:r?.name,msg:r?.message}))}if(d&&d.ok){let r="";try{r=await d.text()}catch(_){console.error("[bot-proxy] cf-desktop-fallback TEXT-THROW "+JSON.stringify({msg:_?.message}))}if(console.log("[bot-proxy] cf-desktop-fallback body "+JSON.stringify({length:r.length})),r&&r.length>100)return O(c,y,t,l,i,g.search,h,"hit_desktop_fallback"),H(a,t,f,x,C),j(r,"HIT-DESKTOP",p.name)}return O(c,y,t,l,i,g.search,h,"miss_ai"),H(a,t,f,x,C),v(a,t,f),null}let o=null;try{o=await fetch(E,{headers:{Accept:"text/html"}}),console.log("[bot-proxy] cf "+JSON.stringify({key:R,url:E,status:o.status,ok:o.ok}))}catch(s){console.error("[bot-proxy] cf THROW "+JSON.stringify({key:R,url:E,name:s?.name,msg:s?.message}))}if(o&&o.ok){let s="";try{s=await o.text()}catch(d){console.error("[bot-proxy] cf TEXT-THROW "+JSON.stringify({msg:d?.message}))}if(console.log("[bot-proxy] cf body "+JSON.stringify({length:s.length})),s&&s.length>100)return O(c,y,t,l,i,g.search,h,"hit"),v(a,t,f),j(s,"HIT",p.name)}}catch(o){console.error("[bot-proxy] OUTER-CATCH "+JSON.stringify({name:o?.name,msg:o?.message,stack:o?.stack?.split(`
2
+ `).slice(0,5).join(" | ")}))}return console.log("[bot-proxy] fallthrough-miss "+JSON.stringify({isAi:p.isAi,aiRender:u,branch:A?"ai":"normal"})),O(c,y,t,l,i,g.search,h,"miss"),v(a,t,f),null}function j(e,n,t){return new Response(e,{headers:{"Content-Type":"text/html; charset=utf-8","X-Cache":n,"X-Bot":t}})}function W(e){let n=e.headers,t=n.get("x-forwarded-for")||"";return n.get("cf-connecting-ip")||n.get("x-real-ip")||t.split(",")[0].trim()||""}function O(e,n,t,m,a,c,u,x){fetch(e,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({project_id:t,url:`https://${m}${a}${c}`,domain:m,path:a,user_agent:u,phase:x,client_ip:n})}).catch(()=>{})}function v(e,n,t){fetch(`${e}/v2/cache`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({items:[{url:t,project_id:n}]})}).catch(()=>{})}function H(e,n,t,m,a){let c={items:[{url:t,project_id:n}],ocr:m};a.length>0&&(c.path_prefixes=a),fetch(`${e}/v2/cache/ai`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(c)}).catch(()=>{})}export{z as a};
package/dist/index.cjs CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";var st=Object.create;var P=Object.defineProperty;var nt=Object.getOwnPropertyDescriptor;var ot=Object.getOwnPropertyNames;var at=Object.getPrototypeOf,it=Object.prototype.hasOwnProperty;var ct=(t,r)=>{for(var e in r)P(t,e,{get:r[e],enumerable:!0})},z=(t,r,e,n)=>{if(r&&typeof r=="object"||typeof r=="function")for(let s of ot(r))!it.call(t,s)&&s!==e&&P(t,s,{get:()=>r[s],enumerable:!(n=nt(r,s))||n.enumerable});return t};var M=(t,r,e)=>(e=t!=null?st(at(t)):{},z(r||!t||!t.__esModule?P(e,"default",{value:t,enumerable:!0}):e,t)),lt=t=>z(P({},"__esModule",{value:!0}),t);var kt={};ct(kt,{cloudflareProxy:()=>rt,nextjsProxy:()=>et});module.exports=lt(kt);var K=["chatgpt-user","claude-user","perplexity-user","gemini-user","gemini-deep-research","mistralai-user","grok-user"],G=["gptbot","oai-searchbot","claudebot","claude-searchbot","google-extended","google-cloudvertexbot","perplexitybot","meta-externalagent","applebot-extended","amazonbot","cohere-ai","bytespider","deepseekbot","grokbot","ccbot","diffbot","ai2bot","petalbot"],F=["googlebot","googlebot-image","googlebot-video","googlebot-news","googleother","mediapartners-google","adsbot-google","google-inspectiontool","bingbot","bingpreview","naverbot","yeti","daumoa","baiduspider","yandexbot","duckduckbot","applebot"],Y=["facebookexternalhit","linkedinbot","twitterbot","slackbot","discordbot","telegrambot","whatsapp","pinterestbot","redditbot"],Z=["ahrefsbot","semrushbot","mj12bot","dotbot","dataforseobot"],pt=["uptimerobot","lighthouse","chrome-lighthouse"],Q=new Set([...K,...G]),J=new Set(K),gt=[...K,...G,...F,...Y,...Z,...pt];function B(t){let r=t.toLowerCase();for(let e of gt)if(r.includes(e)){let n;return J.has(e)?n="ai-user":Q.has(e)?n="ai-crawl":F.includes(e)?n="search":Y.includes(e)?n="sns":Z.includes(e)?n="seo":n="monitoring",{name:e,isAi:Q.has(e),category:n}}return null}var dt=new Set(["utm_source","utm_medium","utm_campaign","utm_term","utm_content","fbclid","fb_action_ids","fb_action_types","fb_source","fb_ref","gclid","gclsrc","dclid","gbraid","wbraid","msclkid","hsa_cam","hsa_grp","hsa_mt","hsa_src","hsa_ad","hsa_acc","hsa_net","hsa_ver","hsa_la","hsa_ol","hsa_kw","_hsenc","_hsmi","_ga","_gl","_bhlid","mc_cid","mc_eid","oly_anon_id","oly_enc_id","ttclid","yclid","irclickid"]);function $(t){if(!t||(t.startsWith("?")&&(t=t.substring(1)),!t))return"";let r=[];for(let e of t.split("&")){if(!e)continue;let n=e.indexOf("="),s=n===-1?e:e.substring(0,n);dt.has(s.toLowerCase())||r.push(e)}return r.join("&")}function q(t){return encodeURIComponent(t).replace(/[!'()*]/g,r=>"%"+r.charCodeAt(0).toString(16).toUpperCase())}function ht(t){if(!t)return"/";let r,e,n=t.slice(0,8).toLowerCase();if(n.startsWith("http://")||n.startsWith("https://"))try{let o=new URL(t);r=o.pathname,e=o.search.startsWith("?")?o.search.substring(1):o.search}catch{r=t,e=""}else{let o=t.indexOf("?");o===-1?(r=t,e=""):(r=t.substring(0,o),e=t.substring(o+1))}r||(r="/");let s=r.split("/").map(o=>{if(!o)return o;try{return q(decodeURIComponent(o))}catch{return q(o)}}).join("/"),i=$(e);return i?`${s}?${i}`:s}async function ut(t){if(typeof process<"u"&&process.versions?.node){let{createHash:n}=await import("crypto");return n("md5").update(t,"utf8").digest("hex")}let r=new TextEncoder().encode(t),e=await crypto.subtle.digest("MD5",r);return Array.from(new Uint8Array(e),n=>n.toString(16).padStart(2,"0")).join("")}async function _(t,r){let e=ht(t);return`${await ut(e)}_${r}`}var tt=/^\/(?:robots\.txt|llms\.txt|sitemap[^\/]*\.(?:xml|xsl))$/i,ft=/mobile|android|iphone|phone|tablet|webos|ipad/i;async function et(t,r){let{projectId:e,cloudfrontDomain:n,renderDomain:s,botLogEndpoint:i,aiRender:o=!0,ocr:g=!0,aiPathPrefixes:R=[]}=r,d=r.siteDomain.replace(/^https?:\/\//,"").replace(/\/+$/,""),C=n.replace(/\/$/,""),w=new URL(t.url),a=w.pathname;if(t.headers.get("x-prerender-host")){if(tt.test(a)||a===`/${e}.txt`){let{NextResponse:l}=await import("next/server");return l.next()}return null}if(a===`/${e}.txt`)return new Response(e,{status:200,headers:{"Content-Type":"text/plain","Cache-Control":"public, max-age=3600"}});if(tt.test(a)){try{let l=await fetch(`${s}/static${a}`,{headers:{"X-Signature":e,"X-Original-Host":d}});if(l.ok)return new Response(l.body,{status:200,headers:{"Content-Type":l.headers.get("Content-Type")||"text/plain","Cache-Control":"public, max-age=3600"}})}catch{}return null}let h=t.headers.get("user-agent")||"",u=B(h);if(!u)return null;let f=ft.test(h)?"mobile":"desktop",I=$(w.search),S=I?`${a}?${I}`:a,b=`https://${d}${S}`,m=mt(t);console.log("[bot-proxy] req "+JSON.stringify({bot:u.name,isAi:u.isAi,path:a,clientIp:m,headers:{"user-agent":h,"x-forwarded-for":t.headers.get("x-forwarded-for"),"x-real-ip":t.headers.get("x-real-ip"),"x-forwarded-host":t.headers.get("x-forwarded-host"),"cf-connecting-ip":t.headers.get("cf-connecting-ip"),"cloudfront-viewer-address":t.headers.get("cloudfront-viewer-address"),"x-vercel-forwarded-for":t.headers.get("x-vercel-forwarded-for"),"x-amzn-trace-id":t.headers.get("x-amzn-trace-id"),"true-client-ip":t.headers.get("true-client-ip"),referer:t.headers.get("referer"),"accept-language":t.headers.get("accept-language"),host:t.headers.get("host")},headerKeys:Array.from(t.headers.keys())}));try{if(u.isAi&&o){let c=await _(S,"ai"),T=`${C}/v2/${d}/${c}.html`,O=null;try{O=await fetch(T,{headers:{Accept:"text/html"}}),console.log("[bot-proxy] cf-ai "+JSON.stringify({status:O.status,ok:O.ok}))}catch(p){console.error("[bot-proxy] cf-ai THROW "+JSON.stringify({url:T,name:p?.name,msg:p?.message}))}if(O&&O.ok){let p="";try{p=await O.text()}catch(v){console.error("[bot-proxy] cf-ai TEXT-THROW "+JSON.stringify({msg:v?.message}))}if(console.log("[bot-proxy] cf-ai body "+JSON.stringify({length:p.length})),p&&p.length>100)return A(i,m,e,d,a,w.search,h,"hit_ai"),V(s,e,b,g,R),L(p,"HIT-AI",u.name)}let H=await _(S,"desktop"),j=`${C}/v2/${d}/${H}.html`,k=null;try{k=await fetch(j,{headers:{Accept:"text/html"}}),console.log("[bot-proxy] cf-desktop-fallback "+JSON.stringify({status:k.status,ok:k.ok}))}catch(p){console.error("[bot-proxy] cf-desktop-fallback THROW "+JSON.stringify({url:j,name:p?.name,msg:p?.message}))}if(k&&k.ok){let p="";try{p=await k.text()}catch(v){console.error("[bot-proxy] cf-desktop-fallback TEXT-THROW "+JSON.stringify({msg:v?.message}))}if(console.log("[bot-proxy] cf-desktop-fallback body "+JSON.stringify({length:p.length})),p&&p.length>100)return A(i,m,e,d,a,w.search,h,"hit_desktop_fallback"),V(s,e,b,g,R),L(p,"HIT-DESKTOP",u.name)}return A(i,m,e,d,a,w.search,h,"miss_ai"),V(s,e,b,g,R),U(s,e,b),null}let l=await _(S,f),E=`${C}/v2/${d}/${l}.html`,y=null;try{y=await fetch(E,{headers:{Accept:"text/html"}}),console.log("[bot-proxy] cf "+JSON.stringify({status:y.status,ok:y.ok}))}catch(c){console.error("[bot-proxy] cf THROW "+JSON.stringify({url:E,name:c?.name,msg:c?.message}))}if(y&&y.ok){let c="";try{c=await y.text()}catch(T){console.error("[bot-proxy] cf TEXT-THROW "+JSON.stringify({msg:T?.message}))}if(console.log("[bot-proxy] cf body "+JSON.stringify({length:c.length})),c&&c.length>100)return A(i,m,e,d,a,w.search,h,"hit"),U(s,e,b),L(c,"HIT",u.name)}}catch(l){console.error("[bot-proxy] OUTER-CATCH "+JSON.stringify({name:l?.name,msg:l?.message,stack:l?.stack?.split(`
2
- `).slice(0,5).join(" | ")}))}return console.log("[bot-proxy] fallthrough-miss "+JSON.stringify({isAi:u.isAi})),A(i,m,e,d,a,w.search,h,"miss"),U(s,e,b),null}function L(t,r,e){return new Response(t,{headers:{"Content-Type":"text/html; charset=utf-8","X-Cache":r,"X-Bot":e}})}function mt(t){let r=t.headers,e=r.get("x-forwarded-for")||"";return r.get("cf-connecting-ip")||r.get("x-real-ip")||e.split(",")[0].trim()||""}function A(t,r,e,n,s,i,o,g){fetch(t,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({project_id:e,url:`https://${n}${s}${i}`,domain:n,path:s,user_agent:o,phase:g,client_ip:r})}).catch(()=>{})}function U(t,r,e){fetch(`${t}/v2/cache`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({items:[{url:e,project_id:r}]})}).catch(()=>{})}function V(t,r,e,n,s){let i={items:[{url:e,project_id:r}],ocr:n};s.length>0&&(i.path_prefixes=s),fetch(`${t}/v2/cache/ai`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)}).catch(()=>{})}var yt=/^\/(?:robots\.txt|llms\.txt|sitemap[^\/]*\.(?:xml|xsl))$/i,xt=/mobile|android|iphone|phone|tablet|webos|ipad/i,bt=604800;async function rt(t,r,e,n){let{projectId:s,siteDomain:i,renderDomain:o,botLogEndpoint:g,aiRender:R=!0,ocr:d=!0,aiPathPrefixes:C=[]}=n,w=new URL(t.url),a=w.pathname,h=i.replace(/^https?:\/\//,"").replace(/\/+$/,"");if(t.method==="POST"){if(a==="/__update-edge-cache")return wt(t,r);if(a==="/__purge-edge-cache")return Ct(t,r);if(a==="/__read-edge-cache")return Tt(t,r)}if(t.headers.get("x-prerender-host"))return null;if(a===`/${s}.txt`)try{let c=await fetch(`${o}${a}`,{redirect:"manual"});return new Response(c.body,{status:c.status,headers:{"Content-Type":"text/plain","Cache-Control":"public, max-age=3600"}})}catch{return new Response("",{status:404})}if(yt.test(a))try{let c=await fetch(`${o}/static${a}`,{redirect:"manual",headers:{"X-Signature":s,"X-Original-Host":h}});return new Response(c.body,{status:c.status,headers:{"Content-Type":c.headers.get("Content-Type")||"text/plain","Cache-Control":"public, max-age=3600"}})}catch{return null}let u=t.headers.get("user-agent")||"",f=B(u);if(!f||!r.V2_CACHE)return null;let I=xt.test(u)?"mobile":"desktop",S=$(w.search),b=S?`${a}?${S}`:a,m=`https://${h}${b}`,l=J.has(f.name)?"ai-user":f.isAi?"ai-crawl":"standard",E=_t(t),y=c=>e.waitUntil(c().catch(T=>console.error("[bot-proxy]",T)));try{if(f.isAi&&R){let O=await _(b,"ai"),H=await r.V2_CACHE.get(O);if(H)return y(async()=>{console.log(`[BOT] ${f.name} | ${l} | hit_ai | ${a}`),await N(g,E,s,h,a,u,"hit_ai"),await X(o,s,m,d,C)}),W(H,"HIT-AI",f.name);let j=await _(b,"desktop"),k=await r.V2_CACHE.get(j);return k?(y(async()=>{console.log(`[BOT] ${f.name} | ${l} | hit_desktop_fallback | ${a}`),await N(g,E,s,h,a,u,"hit_desktop_fallback"),await X(o,s,m,d,C)}),W(k,"HIT-DESKTOP",f.name)):(y(async()=>{console.log(`[BOT] ${f.name} | ${l} | miss_ai | ${a}`),await N(g,E,s,h,a,u,"miss_ai"),await Promise.all([X(o,s,m,d,C),D(o,s,m)])}),null)}let c=await _(b,I),T=await r.V2_CACHE.get(c);return T?(y(async()=>{console.log(`[BOT] ${f.name} | ${l} | hit | ${a}`),await N(g,E,s,h,a,u,"hit"),await D(o,s,m)}),W(T,"HIT",f.name)):(y(async()=>{console.log(`[BOT] ${f.name} | ${l} | miss | ${a}`),await N(g,E,s,h,a,u,"miss"),await D(o,s,m)}),null)}catch(c){return console.error("[bot-proxy]",c),null}}function W(t,r,e){return new Response(t,{headers:{"Content-Type":"text/html; charset=utf-8","X-Cache":r,"X-Bot":e}})}function _t(t){let r=t.headers;return r.get("cf-connecting-ip")||r.get("x-real-ip")||(r.get("x-forwarded-for")||"").split(",")[0].trim()||""}async function N(t,r,e,n,s,i,o){await fetch(t,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({project_id:e,url:`https://${n}${s}`,domain:n,path:s,user_agent:i,phase:o,client_ip:r})})}async function D(t,r,e){await fetch(`${t}/v2/cache/kv`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({items:[{url:e,project_id:r}]})})}function x(t,r=200){return new Response(JSON.stringify(t),{status:r,headers:{"Content-Type":"application/json"}})}async function wt(t,r){try{let{entries:e=[]}=await t.json();if(!r.V2_CACHE)return x({success:!1,error:"V2_CACHE \uBC14\uC778\uB529 \uC5C6\uC74C"},500);if(!e.length)return x({success:!1,error:"entries \uD544\uC218"},400);let n=0;for(let s of e){let{url:i,html:o,variant:g="",device:R="desktop"}=s||{};if(!i||!o)continue;let d=g.startsWith("v2_")?g.substring(3):R,C=await _(i,d);await r.V2_CACHE.put(C,o,{expirationTtl:bt}),n++}return x({success:!0,updated:n})}catch(e){return x({success:!1,error:e.message},500)}}async function Ct(t,r){try{let{urls:e=[],variant:n=""}=await t.json();if(!r.V2_CACHE)return x({success:!1,error:"V2_CACHE \uBC14\uC778\uB529 \uC5C6\uC74C"},500);if(!e.length)return x({success:!1,error:"urls \uD544\uC218"},400);let s=n.startsWith("v2_")?n.substring(3):"desktop",i=0;for(let o of e){let g=await _(o,s);await r.V2_CACHE.delete(g),i++}return x({success:!0,deleted:i})}catch(e){return x({success:!1,error:e.message},500)}}async function Tt(t,r){try{let{url:e,variant:n=""}=await t.json();if(!r.V2_CACHE)return x({success:!1,error:"V2_CACHE \uBC14\uC778\uB529 \uC5C6\uC74C"},500);if(!e)return x({success:!1,error:"url \uD544\uC218"},400);let s=n.startsWith("v2_")?n.substring(3):"desktop",i=await _(e,s),o=await r.V2_CACHE.get(i);return x({success:!0,html:o??null})}catch(e){return x({success:!1,error:e.message},500)}}async function X(t,r,e,n,s){let i={items:[{url:e,project_id:r}],ocr:n};s.length>0&&(i.path_prefixes=s),await fetch(`${t}/v2/cache/kv/ai`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)})}0&&(module.exports={cloudflareProxy,nextjsProxy});
1
+ "use strict";var nt=Object.create;var v=Object.defineProperty;var ot=Object.getOwnPropertyDescriptor;var at=Object.getOwnPropertyNames;var it=Object.getPrototypeOf,ct=Object.prototype.hasOwnProperty;var lt=(t,r)=>{for(var e in r)v(t,e,{get:r[e],enumerable:!0})},M=(t,r,e,n)=>{if(r&&typeof r=="object"||typeof r=="function")for(let s of at(r))!ct.call(t,s)&&s!==e&&v(t,s,{get:()=>r[s],enumerable:!(n=ot(r,s))||n.enumerable});return t};var Q=(t,r,e)=>(e=t!=null?nt(it(t)):{},M(r||!t||!t.__esModule?v(e,"default",{value:t,enumerable:!0}):e,t)),pt=t=>M(v({},"__esModule",{value:!0}),t);var Et={};lt(Et,{cloudflareProxy:()=>st,nextjsProxy:()=>rt});module.exports=pt(Et);var K=["chatgpt-user","claude-user","perplexity-user","gemini-user","gemini-deep-research","mistralai-user","grok-user"],F=["gptbot","oai-searchbot","claudebot","claude-searchbot","google-extended","google-cloudvertexbot","perplexitybot","meta-externalagent","applebot-extended","amazonbot","cohere-ai","bytespider","deepseekbot","grokbot","ccbot","diffbot","ai2bot","petalbot"],Y=["googlebot","googlebot-image","googlebot-video","googlebot-news","googleother","mediapartners-google","adsbot-google","google-inspectiontool","bingbot","bingpreview","naverbot","yeti","daumoa","baiduspider","yandexbot","duckduckbot","applebot"],Z=["facebookexternalhit","linkedinbot","twitterbot","slackbot","discordbot","telegrambot","whatsapp","pinterestbot","redditbot"],q=["ahrefsbot","semrushbot","mj12bot","dotbot","dataforseobot"],gt=["uptimerobot","lighthouse","chrome-lighthouse"],G=new Set([...K,...F]),J=new Set(K),dt=[...K,...F,...Y,...Z,...q,...gt];function B(t){let r=t.toLowerCase();for(let e of dt)if(r.includes(e)){let n;return J.has(e)?n="ai-user":G.has(e)?n="ai-crawl":Y.includes(e)?n="search":Z.includes(e)?n="sns":q.includes(e)?n="seo":n="monitoring",{name:e,isAi:G.has(e),category:n}}return null}var ht=new Set(["utm_source","utm_medium","utm_campaign","utm_term","utm_content","fbclid","fb_action_ids","fb_action_types","fb_source","fb_ref","gclid","gclsrc","dclid","gbraid","wbraid","msclkid","hsa_cam","hsa_grp","hsa_mt","hsa_src","hsa_ad","hsa_acc","hsa_net","hsa_ver","hsa_la","hsa_ol","hsa_kw","_hsenc","_hsmi","_ga","_gl","_bhlid","mc_cid","mc_eid","oly_anon_id","oly_enc_id","ttclid","yclid","irclickid"]);function I(t){if(!t||(t.startsWith("?")&&(t=t.substring(1)),!t))return"";let r=[];for(let e of t.split("&")){if(!e)continue;let n=e.indexOf("="),s=n===-1?e:e.substring(0,n);ht.has(s.toLowerCase())||r.push(e)}return r.join("&")}function tt(t){return encodeURIComponent(t).replace(/[!'()*]/g,r=>"%"+r.charCodeAt(0).toString(16).toUpperCase())}function ut(t){if(!t)return"/";let r,e,n=t.slice(0,8).toLowerCase();if(n.startsWith("http://")||n.startsWith("https://"))try{let o=new URL(t);r=o.pathname,e=o.search.startsWith("?")?o.search.substring(1):o.search}catch{r=t,e=""}else{let o=t.indexOf("?");o===-1?(r=t,e=""):(r=t.substring(0,o),e=t.substring(o+1))}r||(r="/");let s=r.split("/").map(o=>{if(!o)return o;try{return tt(decodeURIComponent(o))}catch{return tt(o)}}).join("/"),i=I(e);return i?`${s}?${i}`:s}async function ft(t){if(typeof process<"u"&&process.versions?.node){let{createHash:n}=await import("crypto");return n("md5").update(t,"utf8").digest("hex")}let r=new TextEncoder().encode(t),e=await crypto.subtle.digest("MD5",r);return Array.from(new Uint8Array(e),n=>n.toString(16).padStart(2,"0")).join("")}async function C(t,r){let e=ut(t);return`${await ft(e)}_${r}`}var et=/^\/(?:robots\.txt|llms\.txt|sitemap[^\/]*\.(?:xml|xsl))$/i,mt=/mobile|android|iphone|phone|tablet|webos|ipad/i;async function rt(t,r){let{projectId:e,cloudfrontDomain:n,renderDomain:s,botLogEndpoint:i,aiRender:o=!0,ocr:h=!0,aiPathPrefixes:$=[]}=r,u=r.siteDomain.replace(/^https?:\/\//,"").replace(/\/+$/,""),E=n.replace(/\/$/,""),_=new URL(t.url),a=_.pathname;if(t.headers.get("x-prerender-host")){if(et.test(a)||a===`/${e}.txt`){let{NextResponse:c}=await import("next/server");return c.next()}return null}if(a===`/${e}.txt`)return new Response(e,{status:200,headers:{"Content-Type":"text/plain","Cache-Control":"public, max-age=3600"}});if(et.test(a)){try{let c=await fetch(`${s}/static${a}`,{headers:{"X-Signature":e,"X-Original-Host":u}});if(c.ok)return new Response(c.body,{status:200,headers:{"Content-Type":c.headers.get("Content-Type")||"text/plain","Cache-Control":"public, max-age=3600"}})}catch{}return null}let f=t.headers.get("user-agent")||"",m=B(f);if(!m)return null;let p=mt.test(f)?"mobile":"desktop",P=I(_.search),R=P?`${a}?${P}`:a,b=`https://${u}${R}`,y=yt(t),k=await C(R,"ai"),w=await C(R,"desktop"),T=p==="mobile"?await C(R,"mobile"):w,g=`${E}/v2/${u}/${k}.html`,O=`${E}/v2/${u}/${w}.html`,A=`${E}/v2/${u}/${T}.html`,N=m.isAi&&o;console.log("[bot-proxy] req "+JSON.stringify({bot:m.name,isAi:m.isAi,aiRender:o,willTakeAiBranch:N,device:p,fullUrl:t.url,path:a,search:_.search,pathWithQuery:R,targetURL:b,clientIp:y,keys:{ai:k,desktop:w,[p]:T},cdnUrls:{ai:g,desktop:O,[p]:A},headers:{"user-agent":f,"x-forwarded-for":t.headers.get("x-forwarded-for"),"x-real-ip":t.headers.get("x-real-ip"),"x-forwarded-host":t.headers.get("x-forwarded-host"),"cf-connecting-ip":t.headers.get("cf-connecting-ip"),"cloudfront-viewer-address":t.headers.get("cloudfront-viewer-address"),"x-vercel-forwarded-for":t.headers.get("x-vercel-forwarded-for"),"x-amzn-trace-id":t.headers.get("x-amzn-trace-id"),"true-client-ip":t.headers.get("true-client-ip"),referer:t.headers.get("referer"),"accept-language":t.headers.get("accept-language"),host:t.headers.get("host")},headerKeys:Array.from(t.headers.keys())}));try{if(N){let l=null;try{l=await fetch(g,{headers:{Accept:"text/html"}}),console.log("[bot-proxy] cf-ai "+JSON.stringify({key:k,url:g,status:l.status,ok:l.ok}))}catch(d){console.error("[bot-proxy] cf-ai THROW "+JSON.stringify({key:k,url:g,name:d?.name,msg:d?.message}))}if(l&&l.ok){let d="";try{d=await l.text()}catch(U){console.error("[bot-proxy] cf-ai TEXT-THROW "+JSON.stringify({msg:U?.message}))}if(console.log("[bot-proxy] cf-ai body "+JSON.stringify({length:d.length})),d&&d.length>100)return H(i,y,e,u,a,_.search,f,"hit_ai"),W(s,e,b,h,$),L(d,"HIT-AI",m.name)}let S=null;try{S=await fetch(O,{headers:{Accept:"text/html"}}),console.log("[bot-proxy] cf-desktop-fallback "+JSON.stringify({key:w,url:O,status:S.status,ok:S.ok}))}catch(d){console.error("[bot-proxy] cf-desktop-fallback THROW "+JSON.stringify({key:w,url:O,name:d?.name,msg:d?.message}))}if(S&&S.ok){let d="";try{d=await S.text()}catch(U){console.error("[bot-proxy] cf-desktop-fallback TEXT-THROW "+JSON.stringify({msg:U?.message}))}if(console.log("[bot-proxy] cf-desktop-fallback body "+JSON.stringify({length:d.length})),d&&d.length>100)return H(i,y,e,u,a,_.search,f,"hit_desktop_fallback"),W(s,e,b,h,$),L(d,"HIT-DESKTOP",m.name)}return H(i,y,e,u,a,_.search,f,"miss_ai"),W(s,e,b,h,$),V(s,e,b),null}let c=null;try{c=await fetch(A,{headers:{Accept:"text/html"}}),console.log("[bot-proxy] cf "+JSON.stringify({key:T,url:A,status:c.status,ok:c.ok}))}catch(l){console.error("[bot-proxy] cf THROW "+JSON.stringify({key:T,url:A,name:l?.name,msg:l?.message}))}if(c&&c.ok){let l="";try{l=await c.text()}catch(S){console.error("[bot-proxy] cf TEXT-THROW "+JSON.stringify({msg:S?.message}))}if(console.log("[bot-proxy] cf body "+JSON.stringify({length:l.length})),l&&l.length>100)return H(i,y,e,u,a,_.search,f,"hit"),V(s,e,b),L(l,"HIT",m.name)}}catch(c){console.error("[bot-proxy] OUTER-CATCH "+JSON.stringify({name:c?.name,msg:c?.message,stack:c?.stack?.split(`
2
+ `).slice(0,5).join(" | ")}))}return console.log("[bot-proxy] fallthrough-miss "+JSON.stringify({isAi:m.isAi,aiRender:o,branch:N?"ai":"normal"})),H(i,y,e,u,a,_.search,f,"miss"),V(s,e,b),null}function L(t,r,e){return new Response(t,{headers:{"Content-Type":"text/html; charset=utf-8","X-Cache":r,"X-Bot":e}})}function yt(t){let r=t.headers,e=r.get("x-forwarded-for")||"";return r.get("cf-connecting-ip")||r.get("x-real-ip")||e.split(",")[0].trim()||""}function H(t,r,e,n,s,i,o,h){fetch(t,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({project_id:e,url:`https://${n}${s}${i}`,domain:n,path:s,user_agent:o,phase:h,client_ip:r})}).catch(()=>{})}function V(t,r,e){fetch(`${t}/v2/cache`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({items:[{url:e,project_id:r}]})}).catch(()=>{})}function W(t,r,e,n,s){let i={items:[{url:e,project_id:r}],ocr:n};s.length>0&&(i.path_prefixes=s),fetch(`${t}/v2/cache/ai`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)}).catch(()=>{})}var bt=/^\/(?:robots\.txt|llms\.txt|sitemap[^\/]*\.(?:xml|xsl))$/i,xt=/mobile|android|iphone|phone|tablet|webos|ipad/i,_t=604800;async function st(t,r,e,n){let{projectId:s,siteDomain:i,renderDomain:o,botLogEndpoint:h,aiRender:$=!0,ocr:u=!0,aiPathPrefixes:E=[]}=n,_=new URL(t.url),a=_.pathname,f=i.replace(/^https?:\/\//,"").replace(/\/+$/,"");if(t.method==="POST"){if(a==="/__update-edge-cache")return Ct(t,r);if(a==="/__purge-edge-cache")return kt(t,r);if(a==="/__read-edge-cache")return Tt(t,r)}if(t.headers.get("x-prerender-host"))return null;if(a===`/${s}.txt`)try{let g=await fetch(`${o}${a}`,{redirect:"manual"});return new Response(g.body,{status:g.status,headers:{"Content-Type":"text/plain","Cache-Control":"public, max-age=3600"}})}catch{return new Response("",{status:404})}if(bt.test(a))try{let g=await fetch(`${o}/static${a}`,{redirect:"manual",headers:{"X-Signature":s,"X-Original-Host":f}});return new Response(g.body,{status:g.status,headers:{"Content-Type":g.headers.get("Content-Type")||"text/plain","Cache-Control":"public, max-age=3600"}})}catch{return null}let m=t.headers.get("user-agent")||"",p=B(m);if(!p||!r.V2_CACHE)return null;let P=xt.test(m)?"mobile":"desktop",R=I(_.search),b=R?`${a}?${R}`:a,y=`https://${f}${b}`,k=J.has(p.name)?"ai-user":p.isAi?"ai-crawl":"standard",w=wt(t),T=g=>e.waitUntil(g().catch(O=>console.error("[bot-proxy]",O)));try{if(p.isAi&&$){let A=await C(b,"ai"),N=await r.V2_CACHE.get(A);if(N)return T(async()=>{console.log(`[BOT] ${p.name} | ${k} | hit_ai | ${a}`),await j(h,w,s,f,a,m,"hit_ai"),await z(o,s,y,u,E)}),D(N,"HIT-AI",p.name);let c=await C(b,"desktop"),l=await r.V2_CACHE.get(c);return l?(T(async()=>{console.log(`[BOT] ${p.name} | ${k} | hit_desktop_fallback | ${a}`),await j(h,w,s,f,a,m,"hit_desktop_fallback"),await z(o,s,y,u,E)}),D(l,"HIT-DESKTOP",p.name)):(T(async()=>{console.log(`[BOT] ${p.name} | ${k} | miss_ai | ${a}`),await j(h,w,s,f,a,m,"miss_ai"),await Promise.all([z(o,s,y,u,E),X(o,s,y)])}),null)}let g=await C(b,P),O=await r.V2_CACHE.get(g);return O?(T(async()=>{console.log(`[BOT] ${p.name} | ${k} | hit | ${a}`),await j(h,w,s,f,a,m,"hit"),await X(o,s,y)}),D(O,"HIT",p.name)):(T(async()=>{console.log(`[BOT] ${p.name} | ${k} | miss | ${a}`),await j(h,w,s,f,a,m,"miss"),await X(o,s,y)}),null)}catch(g){return console.error("[bot-proxy]",g),null}}function D(t,r,e){return new Response(t,{headers:{"Content-Type":"text/html; charset=utf-8","X-Cache":r,"X-Bot":e}})}function wt(t){let r=t.headers;return r.get("cf-connecting-ip")||r.get("x-real-ip")||(r.get("x-forwarded-for")||"").split(",")[0].trim()||""}async function j(t,r,e,n,s,i,o){await fetch(t,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({project_id:e,url:`https://${n}${s}`,domain:n,path:s,user_agent:i,phase:o,client_ip:r})})}async function X(t,r,e){await fetch(`${t}/v2/cache/kv`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({items:[{url:e,project_id:r}]})})}function x(t,r=200){return new Response(JSON.stringify(t),{status:r,headers:{"Content-Type":"application/json"}})}async function Ct(t,r){try{let{entries:e=[]}=await t.json();if(!r.V2_CACHE)return x({success:!1,error:"V2_CACHE \uBC14\uC778\uB529 \uC5C6\uC74C"},500);if(!e.length)return x({success:!1,error:"entries \uD544\uC218"},400);let n=0;for(let s of e){let{url:i,html:o,variant:h="",device:$="desktop"}=s||{};if(!i||!o)continue;let u=h.startsWith("v2_")?h.substring(3):$,E=await C(i,u);await r.V2_CACHE.put(E,o,{expirationTtl:_t}),n++}return x({success:!0,updated:n})}catch(e){return x({success:!1,error:e.message},500)}}async function kt(t,r){try{let{urls:e=[],variant:n=""}=await t.json();if(!r.V2_CACHE)return x({success:!1,error:"V2_CACHE \uBC14\uC778\uB529 \uC5C6\uC74C"},500);if(!e.length)return x({success:!1,error:"urls \uD544\uC218"},400);let s=n.startsWith("v2_")?n.substring(3):"desktop",i=0;for(let o of e){let h=await C(o,s);await r.V2_CACHE.delete(h),i++}return x({success:!0,deleted:i})}catch(e){return x({success:!1,error:e.message},500)}}async function Tt(t,r){try{let{url:e,variant:n=""}=await t.json();if(!r.V2_CACHE)return x({success:!1,error:"V2_CACHE \uBC14\uC778\uB529 \uC5C6\uC74C"},500);if(!e)return x({success:!1,error:"url \uD544\uC218"},400);let s=n.startsWith("v2_")?n.substring(3):"desktop",i=await C(e,s),o=await r.V2_CACHE.get(i);return x({success:!0,html:o??null})}catch(e){return x({success:!1,error:e.message},500)}}async function z(t,r,e,n,s){let i={items:[{url:e,project_id:r}],ocr:n};s.length>0&&(i.path_prefixes=s),await fetch(`${t}/v2/cache/kv/ai`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)})}0&&(module.exports={cloudflareProxy,nextjsProxy});
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- import{a as o}from"./chunk-WERE2ZTC.js";import{a as r}from"./chunk-VPK7GIYJ.js";import"./chunk-BVZHYMX6.js";export{o as cloudflareProxy,r as nextjsProxy};
1
+ import{a as o}from"./chunk-WERE2ZTC.js";import{a as r}from"./chunk-QYKFKEG5.js";import"./chunk-BVZHYMX6.js";export{o as cloudflareProxy,r as nextjsProxy};
package/dist/nextjs.cjs CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";var V=Object.create;var S=Object.defineProperty;var Y=Object.getOwnPropertyDescriptor;var Z=Object.getOwnPropertyNames;var q=Object.getPrototypeOf,tt=Object.prototype.hasOwnProperty;var et=(t,o)=>{for(var e in o)S(t,e,{get:o[e],enumerable:!0})},K=(t,o,e,r)=>{if(o&&typeof o=="object"||typeof o=="function")for(let s of Z(o))!tt.call(t,s)&&s!==e&&S(t,s,{get:()=>o[s],enumerable:!(r=Y(o,s))||r.enumerable});return t};var L=(t,o,e)=>(e=t!=null?V(q(t)):{},K(o||!t||!t.__esModule?S(e,"default",{value:t,enumerable:!0}):e,t)),ot=t=>K(S({},"__esModule",{value:!0}),t);var pt={};et(pt,{nextjsProxy:()=>G});module.exports=ot(pt);var $=["chatgpt-user","claude-user","perplexity-user","gemini-user","gemini-deep-research","mistralai-user","grok-user"],U=["gptbot","oai-searchbot","claudebot","claude-searchbot","google-extended","google-cloudvertexbot","perplexitybot","meta-externalagent","applebot-extended","amazonbot","cohere-ai","bytespider","deepseekbot","grokbot","ccbot","diffbot","ai2bot","petalbot"],H=["googlebot","googlebot-image","googlebot-video","googlebot-news","googleother","mediapartners-google","adsbot-google","google-inspectiontool","bingbot","bingpreview","naverbot","yeti","daumoa","baiduspider","yandexbot","duckduckbot","applebot"],W=["facebookexternalhit","linkedinbot","twitterbot","slackbot","discordbot","telegrambot","whatsapp","pinterestbot","redditbot"],D=["ahrefsbot","semrushbot","mj12bot","dotbot","dataforseobot"],rt=["uptimerobot","lighthouse","chrome-lighthouse"],P=new Set([...$,...U]),st=new Set($),nt=[...$,...U,...H,...W,...D,...rt];function X(t){let o=t.toLowerCase();for(let e of nt)if(o.includes(e)){let r;return st.has(e)?r="ai-user":P.has(e)?r="ai-crawl":H.includes(e)?r="search":W.includes(e)?r="sns":D.includes(e)?r="seo":r="monitoring",{name:e,isAi:P.has(e),category:r}}return null}var at=new Set(["utm_source","utm_medium","utm_campaign","utm_term","utm_content","fbclid","fb_action_ids","fb_action_types","fb_source","fb_ref","gclid","gclsrc","dclid","gbraid","wbraid","msclkid","hsa_cam","hsa_grp","hsa_mt","hsa_src","hsa_ad","hsa_acc","hsa_net","hsa_ver","hsa_la","hsa_ol","hsa_kw","_hsenc","_hsmi","_ga","_gl","_bhlid","mc_cid","mc_eid","oly_anon_id","oly_enc_id","ttclid","yclid","irclickid"]);function k(t){if(!t||(t.startsWith("?")&&(t=t.substring(1)),!t))return"";let o=[];for(let e of t.split("&")){if(!e)continue;let r=e.indexOf("="),s=r===-1?e:e.substring(0,r);at.has(s.toLowerCase())||o.push(e)}return o.join("&")}function z(t){return encodeURIComponent(t).replace(/[!'()*]/g,o=>"%"+o.charCodeAt(0).toString(16).toUpperCase())}function it(t){if(!t)return"/";let o,e,r=t.slice(0,8).toLowerCase();if(r.startsWith("http://")||r.startsWith("https://"))try{let n=new URL(t);o=n.pathname,e=n.search.startsWith("?")?n.search.substring(1):n.search}catch{o=t,e=""}else{let n=t.indexOf("?");n===-1?(o=t,e=""):(o=t.substring(0,n),e=t.substring(n+1))}o||(o="/");let s=o.split("/").map(n=>{if(!n)return n;try{return z(decodeURIComponent(n))}catch{return z(n)}}).join("/"),i=k(e);return i?`${s}?${i}`:s}async function ct(t){if(typeof process<"u"&&process.versions?.node){let{createHash:r}=await import("crypto");return r("md5").update(t,"utf8").digest("hex")}let o=new TextEncoder().encode(t),e=await crypto.subtle.digest("MD5",o);return Array.from(new Uint8Array(e),r=>r.toString(16).padStart(2,"0")).join("")}async function E(t,o){let e=it(t);return`${await ct(e)}_${o}`}var M=/^\/(?:robots\.txt|llms\.txt|sitemap[^\/]*\.(?:xml|xsl))$/i,lt=/mobile|android|iphone|phone|tablet|webos|ipad/i;async function G(t,o){let{projectId:e,cloudfrontDomain:r,renderDomain:s,botLogEndpoint:i,aiRender:n=!0,ocr:_=!0,aiPathPrefixes:R=[]}=o,g=o.siteDomain.replace(/^https?:\/\//,"").replace(/\/+$/,""),N=r.replace(/\/$/,""),f=new URL(t.url),c=f.pathname;if(t.headers.get("x-prerender-host")){if(M.test(c)||c===`/${e}.txt`){let{NextResponse:l}=await import("next/server");return l.next()}return null}if(c===`/${e}.txt`)return new Response(e,{status:200,headers:{"Content-Type":"text/plain","Cache-Control":"public, max-age=3600"}});if(M.test(c)){try{let l=await fetch(`${s}/static${c}`,{headers:{"X-Signature":e,"X-Original-Host":g}});if(l.ok)return new Response(l.body,{status:200,headers:{"Content-Type":l.headers.get("Content-Type")||"text/plain","Cache-Control":"public, max-age=3600"}})}catch{}return null}let d=t.headers.get("user-agent")||"",h=X(d);if(!h)return null;let Q=lt.test(d)?"mobile":"desktop",B=k(f.search),w=B?`${c}?${B}`:c,m=`https://${g}${w}`,u=gt(t);console.log("[bot-proxy] req "+JSON.stringify({bot:h.name,isAi:h.isAi,path:c,clientIp:u,headers:{"user-agent":d,"x-forwarded-for":t.headers.get("x-forwarded-for"),"x-real-ip":t.headers.get("x-real-ip"),"x-forwarded-host":t.headers.get("x-forwarded-host"),"cf-connecting-ip":t.headers.get("cf-connecting-ip"),"cloudfront-viewer-address":t.headers.get("cloudfront-viewer-address"),"x-vercel-forwarded-for":t.headers.get("x-vercel-forwarded-for"),"x-amzn-trace-id":t.headers.get("x-amzn-trace-id"),"true-client-ip":t.headers.get("true-client-ip"),referer:t.headers.get("referer"),"accept-language":t.headers.get("accept-language"),host:t.headers.get("host")},headerKeys:Array.from(t.headers.keys())}));try{if(h.isAi&&n){let p=await E(w,"ai"),O=`${N}/v2/${g}/${p}.html`,b=null;try{b=await fetch(O,{headers:{Accept:"text/html"}}),console.log("[bot-proxy] cf-ai "+JSON.stringify({status:b.status,ok:b.ok}))}catch(a){console.error("[bot-proxy] cf-ai THROW "+JSON.stringify({url:O,name:a?.name,msg:a?.message}))}if(b&&b.ok){let a="";try{a=await b.text()}catch(C){console.error("[bot-proxy] cf-ai TEXT-THROW "+JSON.stringify({msg:C?.message}))}if(console.log("[bot-proxy] cf-ai body "+JSON.stringify({length:a.length})),a&&a.length>100)return T(i,u,e,g,c,f.search,d,"hit_ai"),j(s,e,m,_,R),A(a,"HIT-AI",h.name)}let F=await E(w,"desktop"),v=`${N}/v2/${g}/${F}.html`,x=null;try{x=await fetch(v,{headers:{Accept:"text/html"}}),console.log("[bot-proxy] cf-desktop-fallback "+JSON.stringify({status:x.status,ok:x.ok}))}catch(a){console.error("[bot-proxy] cf-desktop-fallback THROW "+JSON.stringify({url:v,name:a?.name,msg:a?.message}))}if(x&&x.ok){let a="";try{a=await x.text()}catch(C){console.error("[bot-proxy] cf-desktop-fallback TEXT-THROW "+JSON.stringify({msg:C?.message}))}if(console.log("[bot-proxy] cf-desktop-fallback body "+JSON.stringify({length:a.length})),a&&a.length>100)return T(i,u,e,g,c,f.search,d,"hit_desktop_fallback"),j(s,e,m,_,R),A(a,"HIT-DESKTOP",h.name)}return T(i,u,e,g,c,f.search,d,"miss_ai"),j(s,e,m,_,R),I(s,e,m),null}let l=await E(w,Q),J=`${N}/v2/${g}/${l}.html`,y=null;try{y=await fetch(J,{headers:{Accept:"text/html"}}),console.log("[bot-proxy] cf "+JSON.stringify({status:y.status,ok:y.ok}))}catch(p){console.error("[bot-proxy] cf THROW "+JSON.stringify({url:J,name:p?.name,msg:p?.message}))}if(y&&y.ok){let p="";try{p=await y.text()}catch(O){console.error("[bot-proxy] cf TEXT-THROW "+JSON.stringify({msg:O?.message}))}if(console.log("[bot-proxy] cf body "+JSON.stringify({length:p.length})),p&&p.length>100)return T(i,u,e,g,c,f.search,d,"hit"),I(s,e,m),A(p,"HIT",h.name)}}catch(l){console.error("[bot-proxy] OUTER-CATCH "+JSON.stringify({name:l?.name,msg:l?.message,stack:l?.stack?.split(`
2
- `).slice(0,5).join(" | ")}))}return console.log("[bot-proxy] fallthrough-miss "+JSON.stringify({isAi:h.isAi})),T(i,u,e,g,c,f.search,d,"miss"),I(s,e,m),null}function A(t,o,e){return new Response(t,{headers:{"Content-Type":"text/html; charset=utf-8","X-Cache":o,"X-Bot":e}})}function gt(t){let o=t.headers,e=o.get("x-forwarded-for")||"";return o.get("cf-connecting-ip")||o.get("x-real-ip")||e.split(",")[0].trim()||""}function T(t,o,e,r,s,i,n,_){fetch(t,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({project_id:e,url:`https://${r}${s}${i}`,domain:r,path:s,user_agent:n,phase:_,client_ip:o})}).catch(()=>{})}function I(t,o,e){fetch(`${t}/v2/cache`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({items:[{url:e,project_id:o}]})}).catch(()=>{})}function j(t,o,e,r,s){let i={items:[{url:e,project_id:o}],ocr:r};s.length>0&&(i.path_prefixes=s),fetch(`${t}/v2/cache/ai`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)}).catch(()=>{})}0&&(module.exports={nextjsProxy});
1
+ "use strict";var Y=Object.create;var C=Object.defineProperty;var Z=Object.getOwnPropertyDescriptor;var q=Object.getOwnPropertyNames;var tt=Object.getPrototypeOf,et=Object.prototype.hasOwnProperty;var ot=(t,o)=>{for(var e in o)C(t,e,{get:o[e],enumerable:!0})},P=(t,o,e,r)=>{if(o&&typeof o=="object"||typeof o=="function")for(let s of q(o))!et.call(t,s)&&s!==e&&C(t,s,{get:()=>o[s],enumerable:!(r=Z(o,s))||r.enumerable});return t};var H=(t,o,e)=>(e=t!=null?Y(tt(t)):{},P(o||!t||!t.__esModule?C(e,"default",{value:t,enumerable:!0}):e,t)),rt=t=>P(C({},"__esModule",{value:!0}),t);var dt={};ot(dt,{nextjsProxy:()=>V});module.exports=rt(dt);var v=["chatgpt-user","claude-user","perplexity-user","gemini-user","gemini-deep-research","mistralai-user","grok-user"],D=["gptbot","oai-searchbot","claudebot","claude-searchbot","google-extended","google-cloudvertexbot","perplexitybot","meta-externalagent","applebot-extended","amazonbot","cohere-ai","bytespider","deepseekbot","grokbot","ccbot","diffbot","ai2bot","petalbot"],X=["googlebot","googlebot-image","googlebot-video","googlebot-news","googleother","mediapartners-google","adsbot-google","google-inspectiontool","bingbot","bingpreview","naverbot","yeti","daumoa","baiduspider","yandexbot","duckduckbot","applebot"],z=["facebookexternalhit","linkedinbot","twitterbot","slackbot","discordbot","telegrambot","whatsapp","pinterestbot","redditbot"],M=["ahrefsbot","semrushbot","mj12bot","dotbot","dataforseobot"],st=["uptimerobot","lighthouse","chrome-lighthouse"],W=new Set([...v,...D]),nt=new Set(v),at=[...v,...D,...X,...z,...M,...st];function G(t){let o=t.toLowerCase();for(let e of at)if(o.includes(e)){let r;return nt.has(e)?r="ai-user":W.has(e)?r="ai-crawl":X.includes(e)?r="search":z.includes(e)?r="sns":M.includes(e)?r="seo":r="monitoring",{name:e,isAi:W.has(e),category:r}}return null}var it=new Set(["utm_source","utm_medium","utm_campaign","utm_term","utm_content","fbclid","fb_action_ids","fb_action_types","fb_source","fb_ref","gclid","gclsrc","dclid","gbraid","wbraid","msclkid","hsa_cam","hsa_grp","hsa_mt","hsa_src","hsa_ad","hsa_acc","hsa_net","hsa_ver","hsa_la","hsa_ol","hsa_kw","_hsenc","_hsmi","_ga","_gl","_bhlid","mc_cid","mc_eid","oly_anon_id","oly_enc_id","ttclid","yclid","irclickid"]);function N(t){if(!t||(t.startsWith("?")&&(t=t.substring(1)),!t))return"";let o=[];for(let e of t.split("&")){if(!e)continue;let r=e.indexOf("="),s=r===-1?e:e.substring(0,r);it.has(s.toLowerCase())||o.push(e)}return o.join("&")}function Q(t){return encodeURIComponent(t).replace(/[!'()*]/g,o=>"%"+o.charCodeAt(0).toString(16).toUpperCase())}function ct(t){if(!t)return"/";let o,e,r=t.slice(0,8).toLowerCase();if(r.startsWith("http://")||r.startsWith("https://"))try{let n=new URL(t);o=n.pathname,e=n.search.startsWith("?")?n.search.substring(1):n.search}catch{o=t,e=""}else{let n=t.indexOf("?");n===-1?(o=t,e=""):(o=t.substring(0,n),e=t.substring(n+1))}o||(o="/");let s=o.split("/").map(n=>{if(!n)return n;try{return Q(decodeURIComponent(n))}catch{return Q(n)}}).join("/"),l=N(e);return l?`${s}?${l}`:s}async function lt(t){if(typeof process<"u"&&process.versions?.node){let{createHash:r}=await import("crypto");return r("md5").update(t,"utf8").digest("hex")}let o=new TextEncoder().encode(t),e=await crypto.subtle.digest("MD5",o);return Array.from(new Uint8Array(e),r=>r.toString(16).padStart(2,"0")).join("")}async function A(t,o){let e=ct(t);return`${await lt(e)}_${o}`}var F=/^\/(?:robots\.txt|llms\.txt|sitemap[^\/]*\.(?:xml|xsl))$/i,gt=/mobile|android|iphone|phone|tablet|webos|ipad/i;async function V(t,o){let{projectId:e,cloudfrontDomain:r,renderDomain:s,botLogEndpoint:l,aiRender:n=!0,ocr:b=!0,aiPathPrefixes:$=[]}=o,p=o.siteDomain.replace(/^https?:\/\//,"").replace(/\/+$/,""),I=r.replace(/\/$/,""),d=new URL(t.url),g=d.pathname;if(t.headers.get("x-prerender-host")){if(F.test(g)||g===`/${e}.txt`){let{NextResponse:i}=await import("next/server");return i.next()}return null}if(g===`/${e}.txt`)return new Response(e,{status:200,headers:{"Content-Type":"text/plain","Cache-Control":"public, max-age=3600"}});if(F.test(g)){try{let i=await fetch(`${s}/static${g}`,{headers:{"X-Signature":e,"X-Original-Host":p}});if(i.ok)return new Response(i.body,{status:200,headers:{"Content-Type":i.headers.get("Content-Type")||"text/plain","Cache-Control":"public, max-age=3600"}})}catch{}return null}let h=t.headers.get("user-agent")||"",f=G(h);if(!f)return null;let T=gt.test(h)?"mobile":"desktop",L=N(d.search),x=L?`${g}?${L}`:g,u=`https://${p}${x}`,y=pt(t),w=await A(x,"ai"),_=await A(x,"desktop"),O=T==="mobile"?await A(x,"mobile"):_,S=`${I}/v2/${p}/${w}.html`,E=`${I}/v2/${p}/${_}.html`,R=`${I}/v2/${p}/${O}.html`,j=f.isAi&&n;console.log("[bot-proxy] req "+JSON.stringify({bot:f.name,isAi:f.isAi,aiRender:n,willTakeAiBranch:j,device:T,fullUrl:t.url,path:g,search:d.search,pathWithQuery:x,targetURL:u,clientIp:y,keys:{ai:w,desktop:_,[T]:O},cdnUrls:{ai:S,desktop:E,[T]:R},headers:{"user-agent":h,"x-forwarded-for":t.headers.get("x-forwarded-for"),"x-real-ip":t.headers.get("x-real-ip"),"x-forwarded-host":t.headers.get("x-forwarded-host"),"cf-connecting-ip":t.headers.get("cf-connecting-ip"),"cloudfront-viewer-address":t.headers.get("cloudfront-viewer-address"),"x-vercel-forwarded-for":t.headers.get("x-vercel-forwarded-for"),"x-amzn-trace-id":t.headers.get("x-amzn-trace-id"),"true-client-ip":t.headers.get("true-client-ip"),referer:t.headers.get("referer"),"accept-language":t.headers.get("accept-language"),host:t.headers.get("host")},headerKeys:Array.from(t.headers.keys())}));try{if(j){let c=null;try{c=await fetch(S,{headers:{Accept:"text/html"}}),console.log("[bot-proxy] cf-ai "+JSON.stringify({key:w,url:S,status:c.status,ok:c.ok}))}catch(a){console.error("[bot-proxy] cf-ai THROW "+JSON.stringify({key:w,url:S,name:a?.name,msg:a?.message}))}if(c&&c.ok){let a="";try{a=await c.text()}catch(B){console.error("[bot-proxy] cf-ai TEXT-THROW "+JSON.stringify({msg:B?.message}))}if(console.log("[bot-proxy] cf-ai body "+JSON.stringify({length:a.length})),a&&a.length>100)return k(l,y,e,p,g,d.search,h,"hit_ai"),K(s,e,u,b,$),J(a,"HIT-AI",f.name)}let m=null;try{m=await fetch(E,{headers:{Accept:"text/html"}}),console.log("[bot-proxy] cf-desktop-fallback "+JSON.stringify({key:_,url:E,status:m.status,ok:m.ok}))}catch(a){console.error("[bot-proxy] cf-desktop-fallback THROW "+JSON.stringify({key:_,url:E,name:a?.name,msg:a?.message}))}if(m&&m.ok){let a="";try{a=await m.text()}catch(B){console.error("[bot-proxy] cf-desktop-fallback TEXT-THROW "+JSON.stringify({msg:B?.message}))}if(console.log("[bot-proxy] cf-desktop-fallback body "+JSON.stringify({length:a.length})),a&&a.length>100)return k(l,y,e,p,g,d.search,h,"hit_desktop_fallback"),K(s,e,u,b,$),J(a,"HIT-DESKTOP",f.name)}return k(l,y,e,p,g,d.search,h,"miss_ai"),K(s,e,u,b,$),U(s,e,u),null}let i=null;try{i=await fetch(R,{headers:{Accept:"text/html"}}),console.log("[bot-proxy] cf "+JSON.stringify({key:O,url:R,status:i.status,ok:i.ok}))}catch(c){console.error("[bot-proxy] cf THROW "+JSON.stringify({key:O,url:R,name:c?.name,msg:c?.message}))}if(i&&i.ok){let c="";try{c=await i.text()}catch(m){console.error("[bot-proxy] cf TEXT-THROW "+JSON.stringify({msg:m?.message}))}if(console.log("[bot-proxy] cf body "+JSON.stringify({length:c.length})),c&&c.length>100)return k(l,y,e,p,g,d.search,h,"hit"),U(s,e,u),J(c,"HIT",f.name)}}catch(i){console.error("[bot-proxy] OUTER-CATCH "+JSON.stringify({name:i?.name,msg:i?.message,stack:i?.stack?.split(`
2
+ `).slice(0,5).join(" | ")}))}return console.log("[bot-proxy] fallthrough-miss "+JSON.stringify({isAi:f.isAi,aiRender:n,branch:j?"ai":"normal"})),k(l,y,e,p,g,d.search,h,"miss"),U(s,e,u),null}function J(t,o,e){return new Response(t,{headers:{"Content-Type":"text/html; charset=utf-8","X-Cache":o,"X-Bot":e}})}function pt(t){let o=t.headers,e=o.get("x-forwarded-for")||"";return o.get("cf-connecting-ip")||o.get("x-real-ip")||e.split(",")[0].trim()||""}function k(t,o,e,r,s,l,n,b){fetch(t,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({project_id:e,url:`https://${r}${s}${l}`,domain:r,path:s,user_agent:n,phase:b,client_ip:o})}).catch(()=>{})}function U(t,o,e){fetch(`${t}/v2/cache`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({items:[{url:e,project_id:o}]})}).catch(()=>{})}function K(t,o,e,r,s){let l={items:[{url:e,project_id:o}],ocr:r};s.length>0&&(l.path_prefixes=s),fetch(`${t}/v2/cache/ai`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(l)}).catch(()=>{})}0&&(module.exports={nextjsProxy});
package/dist/nextjs.js CHANGED
@@ -1 +1 @@
1
- import{a as o}from"./chunk-VPK7GIYJ.js";import"./chunk-BVZHYMX6.js";export{o as nextjsProxy};
1
+ import{a as o}from"./chunk-QYKFKEG5.js";import"./chunk-BVZHYMX6.js";export{o as nextjsProxy};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@searchos/bot-proxy",
3
- "version": "1.0.12",
3
+ "version": "1.0.13",
4
4
  "description": "SearchOS 봇 프록시 — Next.js, Cloudflare Worker용 봇 감지 + 캐시 프록시",
5
5
  "type": "module",
6
6
  "exports": {
@@ -1,2 +0,0 @@
1
- import{b as H,c as I,d as w}from"./chunk-BVZHYMX6.js";var P=/^\/(?:robots\.txt|llms\.txt|sitemap[^\/]*\.(?:xml|xsl))$/i,L=/mobile|android|iphone|phone|tablet|webos|ipad/i;async function B(t,o){let{projectId:e,cloudfrontDomain:f,renderDomain:s,botLogEndpoint:i,aiRender:R=!0,ocr:b=!0,aiPathPrefixes:$=[]}=o,c=o.siteDomain.replace(/^https?:\/\//,"").replace(/\/+$/,""),S=f.replace(/\/$/,""),p=new URL(t.url),n=p.pathname;if(t.headers.get("x-prerender-host")){if(P.test(n)||n===`/${e}.txt`){let{NextResponse:a}=await import("next/server");return a.next()}return null}if(n===`/${e}.txt`)return new Response(e,{status:200,headers:{"Content-Type":"text/plain","Cache-Control":"public, max-age=3600"}});if(P.test(n)){try{let a=await fetch(`${s}/static${n}`,{headers:{"X-Signature":e,"X-Original-Host":c}});if(a.ok)return new Response(a.body,{status:200,headers:{"Content-Type":a.headers.get("Content-Type")||"text/plain","Cache-Control":"public, max-age=3600"}})}catch{}return null}let g=t.headers.get("user-agent")||"",h=H(g);if(!h)return null;let v=L.test(g)?"mobile":"desktop",A=I(p.search),T=A?`${n}?${A}`:n,d=`https://${c}${T}`,m=W(t);console.log("[bot-proxy] req "+JSON.stringify({bot:h.name,isAi:h.isAi,path:n,clientIp:m,headers:{"user-agent":g,"x-forwarded-for":t.headers.get("x-forwarded-for"),"x-real-ip":t.headers.get("x-real-ip"),"x-forwarded-host":t.headers.get("x-forwarded-host"),"cf-connecting-ip":t.headers.get("cf-connecting-ip"),"cloudfront-viewer-address":t.headers.get("cloudfront-viewer-address"),"x-vercel-forwarded-for":t.headers.get("x-vercel-forwarded-for"),"x-amzn-trace-id":t.headers.get("x-amzn-trace-id"),"true-client-ip":t.headers.get("true-client-ip"),referer:t.headers.get("referer"),"accept-language":t.headers.get("accept-language"),host:t.headers.get("host")},headerKeys:Array.from(t.headers.keys())}));try{if(h.isAi&&R){let l=await w(T,"ai"),k=`${S}/v2/${c}/${l}.html`,u=null;try{u=await fetch(k,{headers:{Accept:"text/html"}}),console.log("[bot-proxy] cf-ai "+JSON.stringify({status:u.status,ok:u.ok}))}catch(r){console.error("[bot-proxy] cf-ai THROW "+JSON.stringify({url:k,name:r?.name,msg:r?.message}))}if(u&&u.ok){let r="";try{r=await u.text()}catch(E){console.error("[bot-proxy] cf-ai TEXT-THROW "+JSON.stringify({msg:E?.message}))}if(console.log("[bot-proxy] cf-ai body "+JSON.stringify({length:r.length})),r&&r.length>100)return O(i,m,e,c,n,p.search,g,"hit_ai"),C(s,e,d,b,$),N(r,"HIT-AI",h.name)}let D=await w(T,"desktop"),j=`${S}/v2/${c}/${D}.html`,x=null;try{x=await fetch(j,{headers:{Accept:"text/html"}}),console.log("[bot-proxy] cf-desktop-fallback "+JSON.stringify({status:x.status,ok:x.ok}))}catch(r){console.error("[bot-proxy] cf-desktop-fallback THROW "+JSON.stringify({url:j,name:r?.name,msg:r?.message}))}if(x&&x.ok){let r="";try{r=await x.text()}catch(E){console.error("[bot-proxy] cf-desktop-fallback TEXT-THROW "+JSON.stringify({msg:E?.message}))}if(console.log("[bot-proxy] cf-desktop-fallback body "+JSON.stringify({length:r.length})),r&&r.length>100)return O(i,m,e,c,n,p.search,g,"hit_desktop_fallback"),C(s,e,d,b,$),N(r,"HIT-DESKTOP",h.name)}return O(i,m,e,c,n,p.search,g,"miss_ai"),C(s,e,d,b,$),J(s,e,d),null}let a=await w(T,v),_=`${S}/v2/${c}/${a}.html`,y=null;try{y=await fetch(_,{headers:{Accept:"text/html"}}),console.log("[bot-proxy] cf "+JSON.stringify({status:y.status,ok:y.ok}))}catch(l){console.error("[bot-proxy] cf THROW "+JSON.stringify({url:_,name:l?.name,msg:l?.message}))}if(y&&y.ok){let l="";try{l=await y.text()}catch(k){console.error("[bot-proxy] cf TEXT-THROW "+JSON.stringify({msg:k?.message}))}if(console.log("[bot-proxy] cf body "+JSON.stringify({length:l.length})),l&&l.length>100)return O(i,m,e,c,n,p.search,g,"hit"),J(s,e,d),N(l,"HIT",h.name)}}catch(a){console.error("[bot-proxy] OUTER-CATCH "+JSON.stringify({name:a?.name,msg:a?.message,stack:a?.stack?.split(`
2
- `).slice(0,5).join(" | ")}))}return console.log("[bot-proxy] fallthrough-miss "+JSON.stringify({isAi:h.isAi})),O(i,m,e,c,n,p.search,g,"miss"),J(s,e,d),null}function N(t,o,e){return new Response(t,{headers:{"Content-Type":"text/html; charset=utf-8","X-Cache":o,"X-Bot":e}})}function W(t){let o=t.headers,e=o.get("x-forwarded-for")||"";return o.get("cf-connecting-ip")||o.get("x-real-ip")||e.split(",")[0].trim()||""}function O(t,o,e,f,s,i,R,b){fetch(t,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({project_id:e,url:`https://${f}${s}${i}`,domain:f,path:s,user_agent:R,phase:b,client_ip:o})}).catch(()=>{})}function J(t,o,e){fetch(`${t}/v2/cache`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({items:[{url:e,project_id:o}]})}).catch(()=>{})}function C(t,o,e,f,s){let i={items:[{url:e,project_id:o}],ocr:f};s.length>0&&(i.path_prefixes=s),fetch(`${t}/v2/cache/ai`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)}).catch(()=>{})}export{B as a};