@searchos/bot-proxy 1.0.9 → 1.0.12
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.
- package/dist/chunk-VPK7GIYJ.js +2 -0
- package/dist/chunk-WERE2ZTC.js +1 -0
- package/dist/cloudflare.cjs +1 -1
- package/dist/cloudflare.js +1 -1
- package/dist/index.cjs +2 -1
- package/dist/index.js +1 -1
- package/dist/nextjs.cjs +2 -1
- package/dist/nextjs.js +1 -1
- package/package.json +1 -1
- package/dist/chunk-JXQQEZ5V.js +0 -1
- package/dist/chunk-NYRQ66HM.js +0 -1
|
@@ -0,0 +1,2 @@
|
|
|
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};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{a as P,b as j,c as S,d as h}from"./chunk-BVZHYMX6.js";var K=/^\/(?:robots\.txt|llms\.txt|sitemap[^\/]*\.(?:xml|xsl))$/i,L=/mobile|android|iphone|phone|tablet|webos|ipad/i,N=604800;async function G(e,t,s,i){let{projectId:n,siteDomain:o,renderDomain:a,botLogEndpoint:p,aiRender:k=!0,ocr:m=!0,aiPathPrefixes:y=[]}=i,A=new URL(e.url),r=A.pathname,g=o.replace(/^https?:\/\//,"").replace(/\/+$/,"");if(e.method==="POST"){if(r==="/__update-edge-cache")return D(e,t);if(r==="/__purge-edge-cache")return J(e,t);if(r==="/__read-edge-cache")return W(e,t)}if(e.headers.get("x-prerender-host"))return null;if(r===`/${n}.txt`)try{let l=await fetch(`${a}${r}`,{redirect:"manual"});return new Response(l.body,{status:l.status,headers:{"Content-Type":"text/plain","Cache-Control":"public, max-age=3600"}})}catch{return new Response("",{status:404})}if(K.test(r))try{let l=await fetch(`${a}/static${r}`,{redirect:"manual",headers:{"X-Signature":n,"X-Original-Host":g}});return new Response(l.body,{status:l.status,headers:{"Content-Type":l.headers.get("Content-Type")||"text/plain","Cache-Control":"public, max-age=3600"}})}catch{return null}let d=e.headers.get("user-agent")||"",c=j(d);if(!c||!t.V2_CACHE)return null;let I=L.test(d)?"mobile":"desktop",H=S(A.search),$=H?`${r}?${H}`:r,f=`https://${g}${$}`,C=P.has(c.name)?"ai-user":c.isAi?"ai-crawl":"standard",w=U(e),_=l=>s.waitUntil(l().catch(T=>console.error("[bot-proxy]",T)));try{if(c.isAi&&k){let v=await h($,"ai"),V=await t.V2_CACHE.get(v);if(V)return _(async()=>{console.log(`[BOT] ${c.name} | ${C} | hit_ai | ${r}`),await E(p,w,n,g,r,d,"hit_ai"),await R(a,n,f,m,y)}),b(V,"HIT-AI",c.name);let B=await h($,"desktop"),O=await t.V2_CACHE.get(B);return O?(_(async()=>{console.log(`[BOT] ${c.name} | ${C} | hit_desktop_fallback | ${r}`),await E(p,w,n,g,r,d,"hit_desktop_fallback"),await R(a,n,f,m,y)}),b(O,"HIT-DESKTOP",c.name)):(_(async()=>{console.log(`[BOT] ${c.name} | ${C} | miss_ai | ${r}`),await E(p,w,n,g,r,d,"miss_ai"),await Promise.all([R(a,n,f,m,y),x(a,n,f)])}),null)}let l=await h($,I),T=await t.V2_CACHE.get(l);return T?(_(async()=>{console.log(`[BOT] ${c.name} | ${C} | hit | ${r}`),await E(p,w,n,g,r,d,"hit"),await x(a,n,f)}),b(T,"HIT",c.name)):(_(async()=>{console.log(`[BOT] ${c.name} | ${C} | miss | ${r}`),await E(p,w,n,g,r,d,"miss"),await x(a,n,f)}),null)}catch(l){return console.error("[bot-proxy]",l),null}}function b(e,t,s){return new Response(e,{headers:{"Content-Type":"text/html; charset=utf-8","X-Cache":t,"X-Bot":s}})}function U(e){let t=e.headers;return t.get("cf-connecting-ip")||t.get("x-real-ip")||(t.get("x-forwarded-for")||"").split(",")[0].trim()||""}async function E(e,t,s,i,n,o,a){await fetch(e,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({project_id:s,url:`https://${i}${n}`,domain:i,path:n,user_agent:o,phase:a,client_ip:t})})}async function x(e,t,s){await fetch(`${e}/v2/cache/kv`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({items:[{url:s,project_id:t}]})})}function u(e,t=200){return new Response(JSON.stringify(e),{status:t,headers:{"Content-Type":"application/json"}})}async function D(e,t){try{let{entries:s=[]}=await e.json();if(!t.V2_CACHE)return u({success:!1,error:"V2_CACHE \uBC14\uC778\uB529 \uC5C6\uC74C"},500);if(!s.length)return u({success:!1,error:"entries \uD544\uC218"},400);let i=0;for(let n of s){let{url:o,html:a,variant:p="",device:k="desktop"}=n||{};if(!o||!a)continue;let m=p.startsWith("v2_")?p.substring(3):k,y=await h(o,m);await t.V2_CACHE.put(y,a,{expirationTtl:N}),i++}return u({success:!0,updated:i})}catch(s){return u({success:!1,error:s.message},500)}}async function J(e,t){try{let{urls:s=[],variant:i=""}=await e.json();if(!t.V2_CACHE)return u({success:!1,error:"V2_CACHE \uBC14\uC778\uB529 \uC5C6\uC74C"},500);if(!s.length)return u({success:!1,error:"urls \uD544\uC218"},400);let n=i.startsWith("v2_")?i.substring(3):"desktop",o=0;for(let a of s){let p=await h(a,n);await t.V2_CACHE.delete(p),o++}return u({success:!0,deleted:o})}catch(s){return u({success:!1,error:s.message},500)}}async function W(e,t){try{let{url:s,variant:i=""}=await e.json();if(!t.V2_CACHE)return u({success:!1,error:"V2_CACHE \uBC14\uC778\uB529 \uC5C6\uC74C"},500);if(!s)return u({success:!1,error:"url \uD544\uC218"},400);let n=i.startsWith("v2_")?i.substring(3):"desktop",o=await h(s,n),a=await t.V2_CACHE.get(o);return u({success:!0,html:a??null})}catch(s){return u({success:!1,error:s.message},500)}}async function R(e,t,s,i,n){let o={items:[{url:s,project_id:t}],ocr:i};n.length>0&&(o.path_prefixes=n),await fetch(`${e}/v2/cache/kv/ai`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(o)})}export{G as a};
|
package/dist/cloudflare.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var X=Object.create;var E=Object.defineProperty;var Q=Object.getOwnPropertyDescriptor;var F=Object.getOwnPropertyNames;var Y=Object.getPrototypeOf,Z=Object.prototype.hasOwnProperty;var q=(t,e)=>{for(var n in e)E(t,n,{get:e[n],enumerable:!0})},P=(t,e,n,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of F(e))!Z.call(t,r)&&r!==n&&E(t,r,{get:()=>e[r],enumerable:!(s=Q(e,r))||s.enumerable});return t};var tt=(t,e,n)=>(n=t!=null?X(Y(t)):{},P(e||!t||!t.__esModule?E(n,"default",{value:t,enumerable:!0}):n,t)),et=t=>P(E({},"__esModule",{value:!0}),t);var ht={};q(ht,{cloudflareProxy:()=>M});module.exports=et(ht);var R=["chatgpt-user","claude-user","perplexity-user","gemini-user","gemini-deep-research","mistralai-user","grok-user"],K=["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"],U=["googlebot","googlebot-image","googlebot-video","googlebot-news","googleother","mediapartners-google","adsbot-google","google-inspectiontool","bingbot","bingpreview","naverbot","yeti","daumoa","baiduspider","yandexbot","duckduckbot","applebot"],v=["facebookexternalhit","linkedinbot","twitterbot","slackbot","discordbot","telegrambot","whatsapp","pinterestbot","redditbot"],L=["ahrefsbot","semrushbot","mj12bot","dotbot","dataforseobot"],nt=["uptimerobot","lighthouse","chrome-lighthouse"],j=new Set([...R,...K]),S=new Set(R),st=[...R,...K,...U,...v,...L,...nt];function W(t){let e=t.toLowerCase();for(let n of st)if(e.includes(n)){let s;return S.has(n)?s="ai-user":j.has(n)?s="ai-crawl":U.includes(n)?s="search":v.includes(n)?s="sns":L.includes(n)?s="seo":s="monitoring",{name:n,isAi:j.has(n),category:s}}return null}var rt=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 e=[];for(let n of t.split("&")){if(!n)continue;let s=n.indexOf("="),r=s===-1?n:n.substring(0,s);rt.has(r.toLowerCase())||e.push(n)}return e.join("&")}function D(t){return encodeURIComponent(t).replace(/[!'()*]/g,e=>"%"+e.charCodeAt(0).toString(16).toUpperCase())}function ot(t){if(!t)return"/";let e,n,s=t.slice(0,8).toLowerCase();if(s.startsWith("http://")||s.startsWith("https://"))try{let o=new URL(t);e=o.pathname,n=o.search.startsWith("?")?o.search.substring(1):o.search}catch{e=t,n=""}else{let o=t.indexOf("?");o===-1?(e=t,n=""):(e=t.substring(0,o),n=t.substring(o+1))}e||(e="/");let r=e.split("/").map(o=>{if(!o)return o;try{return D(decodeURIComponent(o))}catch{return D(o)}}).join("/"),i=k(n);return i?`${r}?${i}`:r}async function at(t){if(typeof process<"u"&&process.versions?.node){let{createHash:s}=await import("crypto");return s("md5").update(t,"utf8").digest("hex")}let e=new TextEncoder().encode(t),n=await crypto.subtle.digest("MD5",e);return Array.from(new Uint8Array(n),s=>s.toString(16).padStart(2,"0")).join("")}async function h(t,e){let n=ot(t);return`${await at(n)}_${e}`}var it=/^\/(?:robots\.txt|llms\.txt|sitemap[^\/]*\.(?:xml|xsl))$/i,ct=/mobile|android|iphone|phone|tablet|webos|ipad/i,lt=604800;async function M(t,e,n,s){let{projectId:r,siteDomain:i,renderDomain:o,botLogEndpoint:p,aiRender:A=!0,ocr:m=!0,aiPathPrefixes:_=[]}=s,H=new URL(t.url),a=H.pathname,g=i.replace(/^https?:\/\//,"").replace(/\/+$/,"");if(t.method==="POST"){if(a==="/__update-edge-cache")return pt(t,e);if(a==="/__purge-edge-cache")return gt(t,e);if(a==="/__read-edge-cache")return dt(t,e)}if(t.headers.get("x-prerender-host"))return null;if(a===`/${r}.txt`)try{let l=await fetch(`${o}${a}`,{redirect:"manual"});return new Response(l.body,{status:l.status,headers:{"Content-Type":"text/plain","Cache-Control":"public, max-age=3600"}})}catch{return new Response("",{status:404})}if(it.test(a))try{let l=await fetch(`${o}/static${a}`,{redirect:"manual",headers:{"X-Signature":r,"X-Original-Host":g}});return new Response(l.body,{status:l.status,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")||"",c=W(d);if(!c||!e.V2_CACHE)return null;let z=ct.test(d)?"mobile":"desktop",B=k(H.search),x=B?`${a}?${B}`:a,f=`https://${g}${x}`,y=S.has(c.name)?"ai-user":c.isAi?"ai-crawl":"standard",b=ut(t),C=l=>n.waitUntil(l().catch(T=>console.error("[bot-proxy]",T)));try{if(c.isAi&&A){let G=await h(x,"ai"),V=await e.V2_CACHE.get(G);if(V)return C(async()=>{console.log(`[BOT] ${c.name} | ${y} | hit_ai | ${a}`),await w(p,b,r,g,a,d,"hit_ai"),await I(o,r,f,m,_)}),$(V,"HIT-AI",c.name);let J=await h(x,"desktop"),N=await e.V2_CACHE.get(J);return N?(C(async()=>{console.log(`[BOT] ${c.name} | ${y} | hit_desktop_fallback | ${a}`),await w(p,b,r,g,a,d,"hit_desktop_fallback"),await I(o,r,f,m,_)}),$(N,"HIT-DESKTOP",c.name)):(C(async()=>{console.log(`[BOT] ${c.name} | ${y} | miss_ai | ${a}`),await w(p,b,r,g,a,d,"miss_ai"),await Promise.all([I(o,r,f,m,_),O(o,r,f)])}),null)}let l=await h(x,z),T=await e.V2_CACHE.get(l);return T?(C(async()=>{console.log(`[BOT] ${c.name} | ${y} | hit | ${a}`),await w(p,b,r,g,a,d,"hit"),await O(o,r,f)}),$(T,"HIT",c.name)):(C(async()=>{console.log(`[BOT] ${c.name} | ${y} | miss | ${a}`),await w(p,b,r,g,a,d,"miss"),await O(o,r,f)}),null)}catch(l){return console.error("[bot-proxy]",l),null}}function $(t,e,n){return new Response(t,{headers:{"Content-Type":"text/html; charset=utf-8","X-Cache":e,"X-Bot":n}})}function ut(t){let e=t.headers;return e.get("cf-connecting-ip")||e.get("x-real-ip")||(e.get("x-forwarded-for")||"").split(",")[0].trim()||""}async function w(t,e,n,s,r,i,o){await fetch(t,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({project_id:n,url:`https://${s}${r}`,domain:s,path:r,user_agent:i,phase:o,client_ip:e})})}async function O(t,e,n){await fetch(`${t}/v2/cache/kv`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({items:[{url:n,project_id:e}]})})}function u(t,e=200){return new Response(JSON.stringify(t),{status:e,headers:{"Content-Type":"application/json"}})}async function pt(t,e){try{let{entries:n=[]}=await t.json();if(!e.V2_CACHE)return u({success:!1,error:"V2_CACHE \uBC14\uC778\uB529 \uC5C6\uC74C"},500);if(!n.length)return u({success:!1,error:"entries \uD544\uC218"},400);let s=0;for(let r of n){let{url:i,html:o,variant:p="",device:A="desktop"}=r||{};if(!i||!o)continue;let m=p.startsWith("v2_")?p.substring(3):A,_=await h(i,m);await e.V2_CACHE.put(_,o,{expirationTtl:lt}),s++}return u({success:!0,updated:s})}catch(n){return u({success:!1,error:n.message},500)}}async function gt(t,e){try{let{urls:n=[],variant:s=""}=await t.json();if(!e.V2_CACHE)return u({success:!1,error:"V2_CACHE \uBC14\uC778\uB529 \uC5C6\uC74C"},500);if(!n.length)return u({success:!1,error:"urls \uD544\uC218"},400);let r=s.startsWith("v2_")?s.substring(3):"desktop",i=0;for(let o of n){let p=await h(o,r);await e.V2_CACHE.delete(p),i++}return u({success:!0,deleted:i})}catch(n){return u({success:!1,error:n.message},500)}}async function dt(t,e){try{let{url:n,variant:s=""}=await t.json();if(!e.V2_CACHE)return u({success:!1,error:"V2_CACHE \uBC14\uC778\uB529 \uC5C6\uC74C"},500);if(!n)return u({success:!1,error:"url \uD544\uC218"},400);let r=s.startsWith("v2_")?s.substring(3):"desktop",i=await h(n,r),o=await e.V2_CACHE.get(i);return u({success:!0,html:o??null})}catch(n){return u({success:!1,error:n.message},500)}}async function I(t,e,n,s,r){let i={items:[{url:n,project_id:e}],ocr:s};r.length>0&&(i.path_prefixes=r),await fetch(`${t}/v2/cache/kv/ai`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)})}0&&(module.exports={cloudflareProxy});
|
package/dist/cloudflare.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{a as o}from"./chunk-
|
|
1
|
+
import{a as o}from"./chunk-WERE2ZTC.js";import"./chunk-BVZHYMX6.js";export{o as cloudflareProxy};
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
"use strict";var tt=Object.create;var I=Object.defineProperty;var et=Object.getOwnPropertyDescriptor;var nt=Object.getOwnPropertyNames;var st=Object.getPrototypeOf,rt=Object.prototype.hasOwnProperty;var ot=(e,n)=>{for(var t in n)I(e,t,{get:n[t],enumerable:!0})},W=(e,n,t,r)=>{if(n&&typeof n=="object"||typeof n=="function")for(let s of nt(n))!rt.call(e,s)&&s!==t&&I(e,s,{get:()=>n[s],enumerable:!(r=et(n,s))||r.enumerable});return e};var X=(e,n,t)=>(t=e!=null?tt(st(e)):{},W(n||!e||!e.__esModule?I(t,"default",{value:e,enumerable:!0}):t,e)),at=e=>W(I({},"__esModule",{value:!0}),e);var xt={};ot(xt,{cloudflareProxy:()=>q,nextjsProxy:()=>Z});module.exports=at(xt);var H=["chatgpt-user","claude-user","perplexity-user","gemini-user","gemini-deep-research","mistralai-user","grok-user"],M=["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"],Q=["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"],G=["ahrefsbot","semrushbot","mj12bot","dotbot","dataforseobot"],it=["uptimerobot","lighthouse","chrome-lighthouse"],J=new Set([...H,...M]),N=new Set(H),ct=[...H,...M,...Q,...z,...G,...it];function P(e){let n=e.toLowerCase();for(let t of ct)if(n.includes(t)){let r;return N.has(t)?r="ai-user":J.has(t)?r="ai-crawl":Q.includes(t)?r="search":z.includes(t)?r="sns":G.includes(t)?r="seo":r="monitoring",{name:t,isAi:J.has(t),category:r}}return null}var lt=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(e){if(!e||(e.startsWith("?")&&(e=e.substring(1)),!e))return"";let n=[];for(let t of e.split("&")){if(!t)continue;let r=t.indexOf("="),s=r===-1?t:t.substring(0,r);lt.has(s.toLowerCase())||n.push(t)}return n.join("&")}function F(e){return encodeURIComponent(e).replace(/[!'()*]/g,n=>"%"+n.charCodeAt(0).toString(16).toUpperCase())}function pt(e){if(!e)return"/";let n,t,r=e.slice(0,8).toLowerCase();if(r.startsWith("http://")||r.startsWith("https://"))try{let o=new URL(e);n=o.pathname,t=o.search.startsWith("?")?o.search.substring(1):o.search}catch{n=e,t=""}else{let o=e.indexOf("?");o===-1?(n=e,t=""):(n=e.substring(0,o),t=e.substring(o+1))}n||(n="/");let s=n.split("/").map(o=>{if(!o)return o;try{return F(decodeURIComponent(o))}catch{return F(o)}}).join("/"),i=k(t);return i?`${s}?${i}`:s}async function ut(e){if(typeof process<"u"&&process.versions?.node){let{createHash:r}=await import("crypto");return r("md5").update(e,"utf8").digest("hex")}let n=new TextEncoder().encode(e),t=await crypto.subtle.digest("MD5",n);return Array.from(new Uint8Array(t),r=>r.toString(16).padStart(2,"0")).join("")}async function _(e,n){let t=pt(e);return`${await ut(t)}_${n}`}var Y=/^\/(?:robots\.txt|llms\.txt|sitemap[^\/]*\.(?:xml|xsl))$/i,dt=/mobile|android|iphone|phone|tablet|webos|ipad/i;async function Z(e,n){let{projectId:t,cloudfrontDomain:r,renderDomain:s,botLogEndpoint:i,aiRender:o=!0,ocr:l=!0,aiPathPrefixes:T=[]}=n,p=n.siteDomain.replace(/^https?:\/\//,"").replace(/\/+$/,""),x=r.replace(/\/$/,""),b=new URL(e.url),a=b.pathname;if(e.headers.get("x-prerender-host")){if(Y.test(a)||a===`/${t}.txt`){let{NextResponse:h}=await import("next/server");return h.next()}return null}if(a===`/${t}.txt`)return new Response(t,{status:200,headers:{"Content-Type":"text/plain","Cache-Control":"public, max-age=3600"}});if(Y.test(a)){try{let h=await fetch(`${s}/static${a}`,{headers:{"X-Signature":t,"X-Original-Host":p}});if(h.ok)return new Response(h.body,{status:200,headers:{"Content-Type":h.headers.get("Content-Type")||"text/plain","Cache-Control":"public, max-age=3600"}})}catch{}return null}let u=e.headers.get("user-agent")||"",g=P(u);if(!g)return null;let d=dt.test(u)?"mobile":"desktop",O=k(b.search),$=O?`${a}?${O}`:a,y=`https://${p}${$}`,f=gt(e);try{if(g.isAi&&o){let c=await _($,"ai"),E=await fetch(`${x}/v2/${p}/${c}.html`,{headers:{Accept:"text/html"}});if(E.ok){let w=await E.text();if(w&&w.length>100)return A(i,f,t,p,a,b.search,u,"hit_ai"),L(s,t,y,l,T),B(w,"HIT-AI",g.name)}let j=await _($,"desktop"),R=await fetch(`${x}/v2/${p}/${j}.html`,{headers:{Accept:"text/html"}});if(R.ok){let w=await R.text();if(w&&w.length>100)return A(i,f,t,p,a,b.search,u,"hit_desktop_fallback"),L(s,t,y,l,T),B(w,"HIT-DESKTOP",g.name)}return A(i,f,t,p,a,b.search,u,"miss_ai"),L(s,t,y,l,T),K(s,t,y),null}let h=await _($,d),C=await fetch(`${x}/v2/${p}/${h}.html`,{headers:{Accept:"text/html"}});if(C.ok){let c=await C.text();if(c&&c.length>100)return A(i,f,t,p,a,b.search,u,"hit"),K(s,t,y),B(c,"HIT",g.name)}}catch{}return A(i,f,t,p,a,b.search,u,"miss"),K(s,t,y),null}function B(e,n,t){return new Response(e,{headers:{"Content-Type":"text/html; charset=utf-8","X-Cache":n,"X-Bot":t}})}function gt(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 A(e,n,t,r,s,i,o,l){fetch(e,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({project_id:t,url:`https://${r}${s}${i}`,domain:r,path:s,user_agent:o,phase:l,client_ip:n})}).catch(()=>{})}function K(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 L(e,n,t,r,s){let i={items:[{url:t,project_id:n}],ocr:r};s.length>0&&(i.path_prefixes=s),fetch(`${e}/v2/cache/ai`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)}).catch(()=>{})}var ht=/^\/(?:robots\.txt|llms\.txt|sitemap[^\/]*\.(?:xml|xsl))$/i,ft=/mobile|android|iphone|phone|tablet|webos|ipad/i,mt=604800;async function q(e,n,t,r){let{projectId:s,siteDomain:i,renderDomain:o,botLogEndpoint:l,aiRender:T=!0,ocr:p=!0,aiPathPrefixes:x=[]}=r,b=new URL(e.url),a=b.pathname,u=i.replace(/^https?:\/\//,"").replace(/\/+$/,"");if(e.method==="POST"){if(a==="/__update-edge-cache")return yt(e,n);if(a==="/__purge-edge-cache")return _t(e,n);if(a==="/__read-edge-cache")return bt(e,n)}if(e.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(ht.test(a))try{let c=await fetch(`${o}/static${a}`,{redirect:"manual",headers:{"X-Signature":s,"X-Original-Host":u}});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 g=e.headers.get("user-agent")||"",d=P(g);if(!d||!n.V2_CACHE)return null;let O=ft.test(g)?"mobile":"desktop",$=k(b.search),y=$?`${a}?${$}`:a,f=`https://${u}${y}`,h=N.has(d.name)?"ai-user":d.isAi?"ai-crawl":"standard",C=c=>t.waitUntil(c().catch(E=>console.error("[bot-proxy]",E)));try{if(d.isAi&&T){let j=await _(y,"ai"),R=await n.V2_CACHE.get(j);if(R)return C(async()=>{console.log(`[BOT] ${d.name} | ${h} | hit_ai | ${a}`),await S(l,s,u,a,g,"hit_ai"),await U(o,s,f,p,x)}),V(R,"HIT-AI",d.name);let w=await _(y,"desktop"),D=await n.V2_CACHE.get(w);return D?(C(async()=>{console.log(`[BOT] ${d.name} | ${h} | hit_desktop_fallback | ${a}`),await S(l,s,u,a,g,"hit_desktop_fallback"),await U(o,s,f,p,x)}),V(D,"HIT-DESKTOP",d.name)):(C(async()=>{console.log(`[BOT] ${d.name} | ${h} | miss_ai | ${a}`),await S(l,s,u,a,g,"miss_ai"),await Promise.all([U(o,s,f,p,x),v(o,s,f)])}),null)}let c=await _(y,O),E=await n.V2_CACHE.get(c);return E?(C(async()=>{console.log(`[BOT] ${d.name} | ${h} | hit | ${a}`),await S(l,s,u,a,g,"hit"),await v(o,s,f)}),V(E,"HIT",d.name)):(C(async()=>{console.log(`[BOT] ${d.name} | ${h} | miss | ${a}`),await S(l,s,u,a,g,"miss"),await v(o,s,f)}),null)}catch(c){return console.error("[bot-proxy]",c),null}}function V(e,n,t){return new Response(e,{headers:{"Content-Type":"text/html; charset=utf-8","X-Cache":n,"X-Bot":t}})}async function S(e,n,t,r,s,i){await fetch(e,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({project_id:n,url:`https://${t}${r}`,domain:t,path:r,user_agent:s,phase:i})})}async function v(e,n,t){await fetch(`${e}/v2/cache/kv`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({items:[{url:t,project_id:n}]})})}function m(e,n=200){return new Response(JSON.stringify(e),{status:n,headers:{"Content-Type":"application/json"}})}async function yt(e,n){try{let{entries:t=[]}=await e.json();if(!n.V2_CACHE)return m({success:!1,error:"V2_CACHE \uBC14\uC778\uB529 \uC5C6\uC74C"},500);if(!t.length)return m({success:!1,error:"entries \uD544\uC218"},400);let r=0;for(let s of t){let{url:i,html:o,variant:l="",device:T="desktop"}=s||{};if(!i||!o)continue;let p=l.startsWith("v2_")?l.substring(3):T,x=await _(i,p);await n.V2_CACHE.put(x,o,{expirationTtl:mt}),r++}return m({success:!0,updated:r})}catch(t){return m({success:!1,error:t.message},500)}}async function _t(e,n){try{let{urls:t=[],variant:r=""}=await e.json();if(!n.V2_CACHE)return m({success:!1,error:"V2_CACHE \uBC14\uC778\uB529 \uC5C6\uC74C"},500);if(!t.length)return m({success:!1,error:"urls \uD544\uC218"},400);let s=r.startsWith("v2_")?r.substring(3):"desktop",i=0;for(let o of t){let l=await _(o,s);await n.V2_CACHE.delete(l),i++}return m({success:!0,deleted:i})}catch(t){return m({success:!1,error:t.message},500)}}async function bt(e,n){try{let{url:t,variant:r=""}=await e.json();if(!n.V2_CACHE)return m({success:!1,error:"V2_CACHE \uBC14\uC778\uB529 \uC5C6\uC74C"},500);if(!t)return m({success:!1,error:"url \uD544\uC218"},400);let s=r.startsWith("v2_")?r.substring(3):"desktop",i=await _(t,s),o=await n.V2_CACHE.get(i);return m({success:!0,html:o??null})}catch(t){return m({success:!1,error:t.message},500)}}async function U(e,n,t,r,s){let i={items:[{url:t,project_id:n}],ocr:r};s.length>0&&(i.path_prefixes=s),await fetch(`${e}/v2/cache/kv/ai`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)})}0&&(module.exports={cloudflareProxy,nextjsProxy});
|
|
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});
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{a as o}from"./chunk-
|
|
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};
|
package/dist/nextjs.cjs
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
"use strict";var
|
|
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});
|
package/dist/nextjs.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{a as o}from"./chunk-
|
|
1
|
+
import{a as o}from"./chunk-VPK7GIYJ.js";import"./chunk-BVZHYMX6.js";export{o as nextjsProxy};
|
package/package.json
CHANGED
package/dist/chunk-JXQQEZ5V.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{a as O,b as P,c as j,d as h}from"./chunk-BVZHYMX6.js";var B=/^\/(?:robots\.txt|llms\.txt|sitemap[^\/]*\.(?:xml|xsl))$/i,K=/mobile|android|iphone|phone|tablet|webos|ipad/i,L=604800;async function Q(n,t,e,a){let{projectId:s,siteDomain:i,renderDomain:o,botLogEndpoint:p,aiRender:T=!0,ocr:m=!0,aiPathPrefixes:y=[]}=a,H=new URL(n.url),r=H.pathname,d=i.replace(/^https?:\/\//,"").replace(/\/+$/,"");if(n.method==="POST"){if(r==="/__update-edge-cache")return N(n,t);if(r==="/__purge-edge-cache")return U(n,t);if(r==="/__read-edge-cache")return D(n,t)}if(n.headers.get("x-prerender-host"))return null;if(r===`/${s}.txt`)try{let l=await fetch(`${o}${r}`,{redirect:"manual"});return new Response(l.body,{status:l.status,headers:{"Content-Type":"text/plain","Cache-Control":"public, max-age=3600"}})}catch{return new Response("",{status:404})}if(B.test(r))try{let l=await fetch(`${o}/static${r}`,{redirect:"manual",headers:{"X-Signature":s,"X-Original-Host":d}});return new Response(l.body,{status:l.status,headers:{"Content-Type":l.headers.get("Content-Type")||"text/plain","Cache-Control":"public, max-age=3600"}})}catch{return null}let g=n.headers.get("user-agent")||"",c=P(g);if(!c||!t.V2_CACHE)return null;let S=K.test(g)?"mobile":"desktop",R=j(H.search),E=R?`${r}?${R}`:r,f=`https://${d}${E}`,C=O.has(c.name)?"ai-user":c.isAi?"ai-crawl":"standard",w=l=>e.waitUntil(l().catch($=>console.error("[bot-proxy]",$)));try{if(c.isAi&&T){let I=await h(E,"ai"),x=await t.V2_CACHE.get(I);if(x)return w(async()=>{console.log(`[BOT] ${c.name} | ${C} | hit_ai | ${r}`),await _(p,s,d,r,g,"hit_ai"),await A(o,s,f,m,y)}),k(x,"HIT-AI",c.name);let v=await h(E,"desktop"),V=await t.V2_CACHE.get(v);return V?(w(async()=>{console.log(`[BOT] ${c.name} | ${C} | hit_desktop_fallback | ${r}`),await _(p,s,d,r,g,"hit_desktop_fallback"),await A(o,s,f,m,y)}),k(V,"HIT-DESKTOP",c.name)):(w(async()=>{console.log(`[BOT] ${c.name} | ${C} | miss_ai | ${r}`),await _(p,s,d,r,g,"miss_ai"),await Promise.all([A(o,s,f,m,y),b(o,s,f)])}),null)}let l=await h(E,S),$=await t.V2_CACHE.get(l);return $?(w(async()=>{console.log(`[BOT] ${c.name} | ${C} | hit | ${r}`),await _(p,s,d,r,g,"hit"),await b(o,s,f)}),k($,"HIT",c.name)):(w(async()=>{console.log(`[BOT] ${c.name} | ${C} | miss | ${r}`),await _(p,s,d,r,g,"miss"),await b(o,s,f)}),null)}catch(l){return console.error("[bot-proxy]",l),null}}function k(n,t,e){return new Response(n,{headers:{"Content-Type":"text/html; charset=utf-8","X-Cache":t,"X-Bot":e}})}async function _(n,t,e,a,s,i){await fetch(n,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({project_id:t,url:`https://${e}${a}`,domain:e,path:a,user_agent:s,phase:i})})}async function b(n,t,e){await fetch(`${n}/v2/cache/kv`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({items:[{url:e,project_id:t}]})})}function u(n,t=200){return new Response(JSON.stringify(n),{status:t,headers:{"Content-Type":"application/json"}})}async function N(n,t){try{let{entries:e=[]}=await n.json();if(!t.V2_CACHE)return u({success:!1,error:"V2_CACHE \uBC14\uC778\uB529 \uC5C6\uC74C"},500);if(!e.length)return u({success:!1,error:"entries \uD544\uC218"},400);let a=0;for(let s of e){let{url:i,html:o,variant:p="",device:T="desktop"}=s||{};if(!i||!o)continue;let m=p.startsWith("v2_")?p.substring(3):T,y=await h(i,m);await t.V2_CACHE.put(y,o,{expirationTtl:L}),a++}return u({success:!0,updated:a})}catch(e){return u({success:!1,error:e.message},500)}}async function U(n,t){try{let{urls:e=[],variant:a=""}=await n.json();if(!t.V2_CACHE)return u({success:!1,error:"V2_CACHE \uBC14\uC778\uB529 \uC5C6\uC74C"},500);if(!e.length)return u({success:!1,error:"urls \uD544\uC218"},400);let s=a.startsWith("v2_")?a.substring(3):"desktop",i=0;for(let o of e){let p=await h(o,s);await t.V2_CACHE.delete(p),i++}return u({success:!0,deleted:i})}catch(e){return u({success:!1,error:e.message},500)}}async function D(n,t){try{let{url:e,variant:a=""}=await n.json();if(!t.V2_CACHE)return u({success:!1,error:"V2_CACHE \uBC14\uC778\uB529 \uC5C6\uC74C"},500);if(!e)return u({success:!1,error:"url \uD544\uC218"},400);let s=a.startsWith("v2_")?a.substring(3):"desktop",i=await h(e,s),o=await t.V2_CACHE.get(i);return u({success:!0,html:o??null})}catch(e){return u({success:!1,error:e.message},500)}}async function A(n,t,e,a,s){let i={items:[{url:e,project_id:t}],ocr:a};s.length>0&&(i.path_prefixes=s),await fetch(`${n}/v2/cache/kv/ai`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)})}export{Q as a};
|
package/dist/chunk-NYRQ66HM.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{b as P,c as S,d as $}from"./chunk-BVZHYMX6.js";var A=/^\/(?:robots\.txt|llms\.txt|sitemap[^\/]*\.(?:xml|xsl))$/i,L=/mobile|android|iphone|phone|tablet|webos|ipad/i;async function H(e,n){let{projectId:t,cloudfrontDomain:l,renderDomain:s,botLogEndpoint:i,aiRender:w=!0,ocr:g=!0,aiPathPrefixes:b=[]}=n,o=n.siteDomain.replace(/^https?:\/\//,"").replace(/\/+$/,""),T=l.replace(/\/$/,""),a=new URL(e.url),r=a.pathname;if(e.headers.get("x-prerender-host")){if(A.test(r)||r===`/${t}.txt`){let{NextResponse:h}=await import("next/server");return h.next()}return null}if(r===`/${t}.txt`)return new Response(t,{status:200,headers:{"Content-Type":"text/plain","Cache-Control":"public, max-age=3600"}});if(A.test(r)){try{let h=await fetch(`${s}/static${r}`,{headers:{"X-Signature":t,"X-Original-Host":o}});if(h.ok)return new Response(h.body,{status:200,headers:{"Content-Type":h.headers.get("Content-Type")||"text/plain","Cache-Control":"public, max-age=3600"}})}catch{}return null}let c=e.headers.get("user-agent")||"",m=P(c);if(!m)return null;let D=L.test(c)?"mobile":"desktop",j=S(a.search),y=j?`${r}?${j}`:r,d=`https://${o}${y}`,u=v(e);try{if(m.isAi&&w){let f=await $(y,"ai"),I=await fetch(`${T}/v2/${o}/${f}.html`,{headers:{Accept:"text/html"}});if(I.ok){let p=await I.text();if(p&&p.length>100)return x(i,u,t,o,r,a.search,c,"hit_ai"),_(s,t,d,g,b),R(p,"HIT-AI",m.name)}let E=await $(y,"desktop"),O=await fetch(`${T}/v2/${o}/${E}.html`,{headers:{Accept:"text/html"}});if(O.ok){let p=await O.text();if(p&&p.length>100)return x(i,u,t,o,r,a.search,c,"hit_desktop_fallback"),_(s,t,d,g,b),R(p,"HIT-DESKTOP",m.name)}return x(i,u,t,o,r,a.search,c,"miss_ai"),_(s,t,d,g,b),C(s,t,d),null}let h=await $(y,D),k=await fetch(`${T}/v2/${o}/${h}.html`,{headers:{Accept:"text/html"}});if(k.ok){let f=await k.text();if(f&&f.length>100)return x(i,u,t,o,r,a.search,c,"hit"),C(s,t,d),R(f,"HIT",m.name)}}catch{}return x(i,u,t,o,r,a.search,c,"miss"),C(s,t,d),null}function R(e,n,t){return new Response(e,{headers:{"Content-Type":"text/html; charset=utf-8","X-Cache":n,"X-Bot":t}})}function v(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 x(e,n,t,l,s,i,w,g){fetch(e,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({project_id:t,url:`https://${l}${s}${i}`,domain:l,path:s,user_agent:w,phase:g,client_ip:n})}).catch(()=>{})}function C(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 _(e,n,t,l,s){let i={items:[{url:t,project_id:n}],ocr:l};s.length>0&&(i.path_prefixes=s),fetch(`${e}/v2/cache/ai`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)}).catch(()=>{})}export{H as a};
|