@searchos/bot-proxy 1.0.4 → 1.0.5
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-YO3LY7GV.js +1 -0
- package/dist/cloudflare.cjs +1 -1
- package/dist/cloudflare.d.cts +0 -7
- package/dist/cloudflare.d.ts +0 -7
- package/dist/cloudflare.js +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/package.json +1 -1
- package/dist/chunk-HLBLRAGQ.js +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{a as x,b as V,c as g}from"./chunk-62QI7OUQ.js";var S=/^\/(?:robots\.txt|llms\.txt|sitemap[^\/]*\.(?:xml|xsl))$/i,L=/mobile|android|iphone|phone|tablet|webos|ipad/i,U=604800;async function D(s,t,e,a){let{projectId:n,siteDomain:i,prerenderDomain:o,botLogEndpoint:c,aiRender:m=!0,ocr:y=!0,aiPathPrefixes:C=[]}=a,r=new URL(s.url).pathname,h=i.replace(/^https?:\/\//,"").replace(/\/+$/,"");if(s.method==="POST"){if(r==="/__update-edge-cache")return I(s,t);if(r==="/__purge-edge-cache")return v(s,t);if(r==="/__read-edge-cache")return B(s,t)}if(s.headers.get("x-prerender-host"))return null;if(r===`/${n}.txt`)try{let u=await fetch(`${o}${r}`,{redirect:"manual"});return new Response(u.body,{status:u.status,headers:{"Content-Type":"text/plain","Cache-Control":"public, max-age=3600"}})}catch{return new Response("",{status:404})}if(S.test(r))try{let u=await fetch(`${o}/static${r}`,{redirect:"manual",headers:{"X-Signature":n,"X-Original-Host":h}});return new Response(u.body,{status:u.status,headers:{"Content-Type":u.headers.get("Content-Type")||"text/plain","Cache-Control":"public, max-age=3600"}})}catch{return null}let d=s.headers.get("user-agent")||"",l=V(d);if(!l||!t.V2_CACHE)return null;let O=L.test(d)?"mobile":"desktop",f=`https://${h}${r}`,w=x.has(l.name)?"ai-user":l.isAi?"ai-crawl":"standard",_=u=>e.waitUntil(u().catch(T=>console.error("[bot-proxy]",T)));try{if(l.isAi&&m){let P=await g(r,"ai"),A=await t.V2_CACHE.get(P);if(A)return _(async()=>{console.log(`[BOT] ${l.name} | ${w} | hit_ai | ${r}`),await E(c,n,h,r,d,"hit_ai"),await b(o,n,f,y,C)}),$(A,"HIT-AI",l.name);let j=await g(r,"desktop"),H=await t.V2_CACHE.get(j);return H?(_(async()=>{console.log(`[BOT] ${l.name} | ${w} | hit_desktop_fallback | ${r}`),await E(c,n,h,r,d,"hit_desktop_fallback"),await b(o,n,f,y,C)}),$(H,"HIT-DESKTOP",l.name)):(_(async()=>{console.log(`[BOT] ${l.name} | ${w} | miss_ai | ${r}`),await E(c,n,h,r,d,"miss_ai"),await Promise.all([b(o,n,f,y,C),R(o,n,f)])}),null)}let u=await g(r,O),T=await t.V2_CACHE.get(u);return T?(_(async()=>{console.log(`[BOT] ${l.name} | ${w} | hit | ${r}`),await E(c,n,h,r,d,"hit"),await R(o,n,f)}),$(T,"HIT",l.name)):(_(async()=>{console.log(`[BOT] ${l.name} | ${w} | miss | ${r}`),await E(c,n,h,r,d,"miss"),await R(o,n,f)}),null)}catch(u){return console.error("[bot-proxy]",u),null}}function $(s,t,e){return new Response(s,{headers:{"Content-Type":"text/html; charset=utf-8","X-Cache":t,"X-Bot":e}})}async function E(s,t,e,a,n,i){await fetch(s,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({project_id:t,url:`https://${e}${a}`,domain:e,path:a,user_agent:n,phase:i})})}async function R(s,t,e){await fetch(`${s}/v2/cache/kv`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({items:[{url:e,project_id:t}]})})}function p(s,t=200){return new Response(JSON.stringify(s),{status:t,headers:{"Content-Type":"application/json"}})}async function I(s,t){try{let{entries:e=[]}=await s.json();if(!t.V2_CACHE)return p({success:!1,error:"V2_CACHE \uBC14\uC778\uB529 \uC5C6\uC74C"},500);if(!e.length)return p({success:!1,error:"entries \uD544\uC218"},400);let a=0;for(let n of e){let{url:i,html:o,variant:c="",device:m="desktop"}=n||{};if(!i||!o)continue;let y=c.startsWith("v2_")?c.substring(3):m,C=new URL(i).pathname||"/",k=await g(C,y);await t.V2_CACHE.put(k,o,{expirationTtl:U}),a++}return p({success:!0,updated:a})}catch(e){return p({success:!1,error:e.message},500)}}async function v(s,t){try{let{urls:e=[],variant:a=""}=await s.json();if(!t.V2_CACHE)return p({success:!1,error:"V2_CACHE \uBC14\uC778\uB529 \uC5C6\uC74C"},500);if(!e.length)return p({success:!1,error:"urls \uD544\uC218"},400);let n=a.startsWith("v2_")?a.substring(3):"desktop",i=0;for(let o of e){let c=new URL(o).pathname||"/",m=await g(c,n);await t.V2_CACHE.delete(m),i++}return p({success:!0,deleted:i})}catch(e){return p({success:!1,error:e.message},500)}}async function B(s,t){try{let{url:e,variant:a=""}=await s.json();if(!t.V2_CACHE)return p({success:!1,error:"V2_CACHE \uBC14\uC778\uB529 \uC5C6\uC74C"},500);if(!e)return p({success:!1,error:"url \uD544\uC218"},400);let n=a.startsWith("v2_")?a.substring(3):"desktop",i=new URL(e).pathname||"/",o=await g(i,n),c=await t.V2_CACHE.get(o);return p({success:!0,html:c??null})}catch(e){return p({success:!1,error:e.message},500)}}async function b(s,t,e,a,n){let i={items:[{url:e,project_id:t}],ocr:a};n.length>0&&(i.path_prefixes=n),await fetch(`${s}/v2/cache/kv/ai`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)})}export{D as a};
|
package/dist/cloudflare.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var E=Object.defineProperty;var D=Object.getOwnPropertyDescriptor;var W=Object.getOwnPropertyNames;var J=Object.prototype.hasOwnProperty;var M=(t,n)=>{for(var e in n)E(t,e,{get:n[e],enumerable:!0})},X=(t,n,e,r)=>{if(n&&typeof n=="object"||typeof n=="function")for(let o of W(n))!J.call(t,o)&&o!==e&&E(t,o,{get:()=>n[o],enumerable:!(r=D(n,o))||r.enumerable});return t};var z=t=>X(E({},"__esModule",{value:!0}),t);var ot={};M(ot,{cloudflareProxy:()=>j});module.exports=z(ot);var T=["chatgpt-user","claude-user","perplexity-user","gemini-user","gemini-deep-research","mistralai-user","grok-user"],B=["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"],P=["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"],N=["ahrefsbot","semrushbot","mj12bot","dotbot","dataforseobot"],G=["uptimerobot","lighthouse","chrome-lighthouse"],I=new Set([...T,...B]),O=new Set(T),F=[...T,...B,...P,...V,...N,...G];function U(t){let n=t.toLowerCase();for(let e of F)if(n.includes(e)){let r;return O.has(e)?r="ai-user":I.has(e)?r="ai-crawl":P.includes(e)?r="search":V.includes(e)?r="sns":N.includes(e)?r="seo":r="monitoring",{name:e,isAi:I.has(e),category:r}}return null}async function q(t){if(typeof process<"u"&&process.versions?.node){let{createHash:r}=require("crypto");return r("md5").update(t,"utf8").digest("hex")}let n=new TextEncoder().encode(t),e=await crypto.subtle.digest("MD5",n);return Array.from(new Uint8Array(e),r=>r.toString(16).padStart(2,"0")).join("")}function Q(t){if(!t)return"/";if(t.startsWith("http://")||t.startsWith("https://"))try{t=new URL(t).pathname}catch{}let n=t.indexOf("?");return n!==-1&&(t=t.substring(0,n)),t?t.split("/").map(e=>{if(!e)return e;try{return encodeURIComponent(decodeURIComponent(e))}catch{return encodeURIComponent(e)}}).join("/"):"/"}async function h(t,n){let e=Q(t);return`${await q(e)}_${n}`}var Y=/^\/(?:robots\.txt|llms\.txt|sitemap[^\/]*\.(?:xml|xsl))$/i,Z=/mobile|android|iphone|phone|tablet|webos|ipad/i,tt=604800;async function j(t,n,e,r){let{projectId:o,siteDomain:i,prerenderDomain:a,botLogEndpoint:c,aiRender:m=!0,ocr:y=!0,aiPathPrefixes:b=[]}=r,s=new URL(t.url).pathname,d=i.replace(/^https?:\/\//,"").replace(/\/+$/,"");if(t.method==="POST"){if(s==="/__update-edge-cache")return et(t,n);if(s==="/__purge-edge-cache")return nt(t,n);if(s==="/__read-edge-cache")return rt(t,n)}if(t.headers.get("x-prerender-host"))return null;if(s===`/${o}.txt`)try{let u=await fetch(`${a}${s}`,{redirect:"manual"});return new Response(u.body,{status:u.status,headers:{"Content-Type":"text/plain","Cache-Control":"public, max-age=3600"}})}catch{return new Response("",{status:404})}if(Y.test(s))try{let u=await fetch(`${a}/static${s}`,{redirect:"manual",headers:{"X-Signature":o,"X-Original-Host":d}});return new Response(u.body,{status:u.status,headers:{"Content-Type":u.headers.get("Content-Type")||"text/plain","Cache-Control":"public, max-age=3600"}})}catch{return null}let g=t.headers.get("user-agent")||"",l=U(g);if(!l||!n.V2_CACHE)return null;let K=Z.test(g)?"mobile":"desktop",f=`https://${d}${s}`,w=O.has(l.name)?"ai-user":l.isAi?"ai-crawl":"standard",C=u=>e.waitUntil(u().catch(x=>console.error("[bot-proxy]",x)));try{if(l.isAi&&m){let L=await h(s,"ai"),$=await n.V2_CACHE.get(L);if($)return C(async()=>{console.log(`[BOT] ${l.name} | ${w} | hit_ai | ${s}`),await _(c,o,d,s,g,"hit_ai"),await A(a,o,f,y,b)}),R($,"HIT-AI",l.name);let v=await h(s,"desktop"),H=await n.V2_CACHE.get(v);return H?(C(async()=>{console.log(`[BOT] ${l.name} | ${w} | hit_desktop_fallback | ${s}`),await _(c,o,d,s,g,"hit_desktop_fallback"),await A(a,o,f,y,b)}),R(H,"HIT-DESKTOP",l.name)):(C(async()=>{console.log(`[BOT] ${l.name} | ${w} | miss_ai | ${s}`),await _(c,o,d,s,g,"miss_ai"),await Promise.all([A(a,o,f,y,b),k(a,o,f)])}),null)}let u=await h(s,K),x=await n.V2_CACHE.get(u);return x?(C(async()=>{console.log(`[BOT] ${l.name} | ${w} | hit | ${s}`),await _(c,o,d,s,g,"hit"),await k(a,o,f)}),R(x,"HIT",l.name)):(C(async()=>{console.log(`[BOT] ${l.name} | ${w} | miss | ${s}`),await _(c,o,d,s,g,"miss"),await k(a,o,f)}),null)}catch(u){return console.error("[bot-proxy]",u),null}}function R(t,n,e){return new Response(t,{headers:{"Content-Type":"text/html; charset=utf-8","X-Cache":n,"X-Bot":e}})}async function _(t,n,e,r,o,i){await fetch(t,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({project_id:n,url:`https://${e}${r}`,domain:e,path:r,user_agent:o,phase:i})})}async function k(t,n,e){await fetch(`${t}/v2/cache/kv`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({items:[{url:e,project_id:n}]})})}function p(t,n=200){return new Response(JSON.stringify(t),{status:n,headers:{"Content-Type":"application/json"}})}async function et(t,n){try{let{entries:e=[]}=await t.json();if(!n.V2_CACHE)return p({success:!1,error:"V2_CACHE \uBC14\uC778\uB529 \uC5C6\uC74C"},500);if(!e.length)return p({success:!1,error:"entries \uD544\uC218"},400);let r=0;for(let o of e){let{url:i,html:a,variant:c="",device:m="desktop"}=o||{};if(!i||!a)continue;let y=c.startsWith("v2_")?c.substring(3):m,b=new URL(i).pathname||"/",S=await h(b,y);await n.V2_CACHE.put(S,a,{expirationTtl:tt}),r++}return p({success:!0,updated:r})}catch(e){return p({success:!1,error:e.message},500)}}async function nt(t,n){try{let{urls:e=[],variant:r=""}=await t.json();if(!n.V2_CACHE)return p({success:!1,error:"V2_CACHE \uBC14\uC778\uB529 \uC5C6\uC74C"},500);if(!e.length)return p({success:!1,error:"urls \uD544\uC218"},400);let o=r.startsWith("v2_")?r.substring(3):"desktop",i=0;for(let a of e){let c=new URL(a).pathname||"/",m=await h(c,o);await n.V2_CACHE.delete(m),i++}return p({success:!0,deleted:i})}catch(e){return p({success:!1,error:e.message},500)}}async function rt(t,n){try{let{url:e,variant:r=""}=await t.json();if(!n.V2_CACHE)return p({success:!1,error:"V2_CACHE \uBC14\uC778\uB529 \uC5C6\uC74C"},500);if(!e)return p({success:!1,error:"url \uD544\uC218"},400);let o=r.startsWith("v2_")?r.substring(3):"desktop",i=new URL(e).pathname||"/",a=await h(i,o),c=await n.V2_CACHE.get(a);return p({success:!0,html:c??null})}catch(e){return p({success:!1,error:e.message},500)}}async function A(t,n,e,r,o){let i={items:[{url:e,project_id:n}],ocr:r};o.length>0&&(i.path_prefixes=o),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.d.cts
CHANGED
|
@@ -11,13 +11,6 @@ interface CloudflareProxyOptions {
|
|
|
11
11
|
/** AI 렌더 경로 prefix (빈 배열 = 전체 경로) */
|
|
12
12
|
aiPathPrefixes?: string[];
|
|
13
13
|
}
|
|
14
|
-
/**
|
|
15
|
-
* Cloudflare Worker에서 봇 요청을 KV 캐시로 프록시.
|
|
16
|
-
* 봇이 아니거나 캐시 미스면 null 반환.
|
|
17
|
-
*
|
|
18
|
-
* AI 봇: ai 캐시 → desktop 폴백 → null (origin)
|
|
19
|
-
* 일반 봇: desktop/mobile 캐시 → null (origin)
|
|
20
|
-
*/
|
|
21
14
|
declare function cloudflareProxy(request: Request, env: {
|
|
22
15
|
V2_CACHE?: KVNamespace;
|
|
23
16
|
[key: string]: unknown;
|
package/dist/cloudflare.d.ts
CHANGED
|
@@ -11,13 +11,6 @@ interface CloudflareProxyOptions {
|
|
|
11
11
|
/** AI 렌더 경로 prefix (빈 배열 = 전체 경로) */
|
|
12
12
|
aiPathPrefixes?: string[];
|
|
13
13
|
}
|
|
14
|
-
/**
|
|
15
|
-
* Cloudflare Worker에서 봇 요청을 KV 캐시로 프록시.
|
|
16
|
-
* 봇이 아니거나 캐시 미스면 null 반환.
|
|
17
|
-
*
|
|
18
|
-
* AI 봇: ai 캐시 → desktop 폴백 → null (origin)
|
|
19
|
-
* 일반 봇: desktop/mobile 캐시 → null (origin)
|
|
20
|
-
*/
|
|
21
14
|
declare function cloudflareProxy(request: Request, env: {
|
|
22
15
|
V2_CACHE?: KVNamespace;
|
|
23
16
|
[key: string]: unknown;
|
package/dist/cloudflare.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{a as o}from"./chunk-
|
|
1
|
+
import{a as o}from"./chunk-YO3LY7GV.js";import"./chunk-62QI7OUQ.js";export{o as cloudflareProxy};
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var F=Object.create;var
|
|
1
|
+
"use strict";var F=Object.create;var k=Object.defineProperty;var Q=Object.getOwnPropertyDescriptor;var Y=Object.getOwnPropertyNames;var Z=Object.getPrototypeOf,q=Object.prototype.hasOwnProperty;var tt=(e,n)=>{for(var t in n)k(e,t,{get:n[t],enumerable:!0})},K=(e,n,t,s)=>{if(n&&typeof n=="object"||typeof n=="function")for(let r of Y(n))!q.call(e,r)&&r!==t&&k(e,r,{get:()=>n[r],enumerable:!(s=Q(n,r))||s.enumerable});return e};var et=(e,n,t)=>(t=e!=null?F(Z(e)):{},K(n||!e||!e.__esModule?k(t,"default",{value:e,enumerable:!0}):t,e)),nt=e=>K(k({},"__esModule",{value:!0}),e);var gt={};tt(gt,{cloudflareProxy:()=>z,nextjsProxy:()=>M});module.exports=nt(gt);var S=["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"],D=["googlebot","googlebot-image","googlebot-video","googlebot-news","googleother","mediapartners-google","adsbot-google","google-inspectiontool","bingbot","bingpreview","naverbot","yeti","daumoa","baiduspider","yandexbot","duckduckbot","applebot"],X=["facebookexternalhit","linkedinbot","twitterbot","slackbot","discordbot","telegrambot","whatsapp","pinterestbot","redditbot"],J=["ahrefsbot","semrushbot","mj12bot","dotbot","dataforseobot"],rt=["uptimerobot","lighthouse","chrome-lighthouse"],v=new Set([...S,...U]),P=new Set(S),st=[...S,...U,...D,...X,...J,...rt];function A(e){let n=e.toLowerCase();for(let t of st)if(n.includes(t)){let s;return P.has(t)?s="ai-user":v.has(t)?s="ai-crawl":D.includes(t)?s="search":X.includes(t)?s="sns":J.includes(t)?s="seo":s="monitoring",{name:t,isAi:v.has(t),category:s}}return null}async function ot(e){if(typeof process<"u"&&process.versions?.node){let{createHash:s}=require("crypto");return s("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),s=>s.toString(16).padStart(2,"0")).join("")}function at(e){if(!e)return"/";if(e.startsWith("http://")||e.startsWith("https://"))try{e=new URL(e).pathname}catch{}let n=e.indexOf("?");return n!==-1&&(e=e.substring(0,n)),e?e.split("/").map(t=>{if(!t)return t;try{return encodeURIComponent(decodeURIComponent(t))}catch{return encodeURIComponent(t)}}).join("/"):"/"}async function y(e,n){let t=at(e);return`${await ot(t)}_${n}`}var W=/^\/(?:robots\.txt|llms\.txt|sitemap[^\/]*\.(?:xml|xsl))$/i,it=/mobile|android|iphone|phone|tablet|webos|ipad/i;async function M(e,n){let{projectId:t,cloudfrontDomain:s,prerenderDomain:r,botLogEndpoint:a,aiRender:i=!0,ocr:l=!0,aiPathPrefixes:b=[]}=n,p=n.siteDomain.replace(/^https?:\/\//,"").replace(/\/+$/,""),w=s.replace(/\/$/,""),C=new URL(e.url),o=C.pathname;if(e.headers.get("x-prerender-host")){if(W.test(o)||o===`/${t}.txt`){let{NextResponse:c}=await import("next/server");return c.next()}return null}if(o===`/${t}.txt`)try{let c=await fetch(`${r}${o}`,{redirect:"manual"});return new Response(c.body,{status:c.status,headers:{"Content-Type":"text/plain","Cache-Control":"public, max-age=3600"}})}catch{return null}if(W.test(o))try{let c=await fetch(`${r}/static${o}`,{headers:{"X-Signature":t,"X-Original-Host":p}});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=e.headers.get("user-agent")||"",g=A(u);if(!g)return null;let h=it.test(u)?"mobile":"desktop",_=`https://${p}${o}`;try{if(g.isAi&&i){let m=await y(o,"ai"),d=await fetch(`${w}/v2/${p}/${m}.html`,{headers:{Accept:"text/html"}});if(d.ok){let x=await d.text();if(x&&x.length>100)return $(a,t,p,o,C.search,u,"hit_ai"),H(r,t,_,l,b),j(x,"HIT-AI",g.name)}let E=await y(o,"desktop"),O=await fetch(`${w}/v2/${p}/${E}.html`,{headers:{Accept:"text/html"}});if(O.ok){let x=await O.text();if(x&&x.length>100)return $(a,t,p,o,C.search,u,"hit_desktop_fallback"),H(r,t,_,l,b),j(x,"HIT-DESKTOP",g.name)}return $(a,t,p,o,C.search,u,"miss_ai"),H(r,t,_,l,b),I(r,t,_),null}let c=await y(o,h),T=await fetch(`${w}/v2/${p}/${c}.html`,{headers:{Accept:"text/html"}});if(T.ok){let m=await T.text();if(m&&m.length>100)return $(a,t,p,o,C.search,u,"hit"),I(r,t,_),j(m,"HIT",g.name)}}catch{}return $(a,t,p,o,C.search,u,"miss"),I(r,t,_),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 $(e,n,t,s,r,a,i){fetch(e,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({project_id:n,url:`https://${t}${s}${r}`,domain:t,path:s,user_agent:a,phase:i})}).catch(()=>{})}function I(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,s,r){let a={items:[{url:t,project_id:n}],ocr:s};r.length>0&&(a.path_prefixes=r),fetch(`${e}/v2/cache/ai`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(a)}).catch(()=>{})}var ct=/^\/(?:robots\.txt|llms\.txt|sitemap[^\/]*\.(?:xml|xsl))$/i,lt=/mobile|android|iphone|phone|tablet|webos|ipad/i,pt=604800;async function z(e,n,t,s){let{projectId:r,siteDomain:a,prerenderDomain:i,botLogEndpoint:l,aiRender:b=!0,ocr:p=!0,aiPathPrefixes:w=[]}=s,o=new URL(e.url).pathname,u=a.replace(/^https?:\/\//,"").replace(/\/+$/,"");if(e.method==="POST"){if(o==="/__update-edge-cache")return ut(e,n);if(o==="/__purge-edge-cache")return dt(e,n);if(o==="/__read-edge-cache")return ht(e,n)}if(e.headers.get("x-prerender-host"))return null;if(o===`/${r}.txt`)try{let d=await fetch(`${i}${o}`,{redirect:"manual"});return new Response(d.body,{status:d.status,headers:{"Content-Type":"text/plain","Cache-Control":"public, max-age=3600"}})}catch{return new Response("",{status:404})}if(ct.test(o))try{let d=await fetch(`${i}/static${o}`,{redirect:"manual",headers:{"X-Signature":r,"X-Original-Host":u}});return new Response(d.body,{status:d.status,headers:{"Content-Type":d.headers.get("Content-Type")||"text/plain","Cache-Control":"public, max-age=3600"}})}catch{return null}let g=e.headers.get("user-agent")||"",h=A(g);if(!h||!n.V2_CACHE)return null;let _=lt.test(g)?"mobile":"desktop",c=`https://${u}${o}`,T=P.has(h.name)?"ai-user":h.isAi?"ai-crawl":"standard",m=d=>t.waitUntil(d().catch(E=>console.error("[bot-proxy]",E)));try{if(h.isAi&&b){let O=await y(o,"ai"),x=await n.V2_CACHE.get(O);if(x)return m(async()=>{console.log(`[BOT] ${h.name} | ${T} | hit_ai | ${o}`),await R(l,r,u,o,g,"hit_ai"),await L(i,r,c,p,w)}),N(x,"HIT-AI",h.name);let G=await y(o,"desktop"),V=await n.V2_CACHE.get(G);return V?(m(async()=>{console.log(`[BOT] ${h.name} | ${T} | hit_desktop_fallback | ${o}`),await R(l,r,u,o,g,"hit_desktop_fallback"),await L(i,r,c,p,w)}),N(V,"HIT-DESKTOP",h.name)):(m(async()=>{console.log(`[BOT] ${h.name} | ${T} | miss_ai | ${o}`),await R(l,r,u,o,g,"miss_ai"),await Promise.all([L(i,r,c,p,w),B(i,r,c)])}),null)}let d=await y(o,_),E=await n.V2_CACHE.get(d);return E?(m(async()=>{console.log(`[BOT] ${h.name} | ${T} | hit | ${o}`),await R(l,r,u,o,g,"hit"),await B(i,r,c)}),N(E,"HIT",h.name)):(m(async()=>{console.log(`[BOT] ${h.name} | ${T} | miss | ${o}`),await R(l,r,u,o,g,"miss"),await B(i,r,c)}),null)}catch(d){return console.error("[bot-proxy]",d),null}}function N(e,n,t){return new Response(e,{headers:{"Content-Type":"text/html; charset=utf-8","X-Cache":n,"X-Bot":t}})}async function R(e,n,t,s,r,a){await fetch(e,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({project_id:n,url:`https://${t}${s}`,domain:t,path:s,user_agent:r,phase:a})})}async function B(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 f(e,n=200){return new Response(JSON.stringify(e),{status:n,headers:{"Content-Type":"application/json"}})}async function ut(e,n){try{let{entries:t=[]}=await e.json();if(!n.V2_CACHE)return f({success:!1,error:"V2_CACHE \uBC14\uC778\uB529 \uC5C6\uC74C"},500);if(!t.length)return f({success:!1,error:"entries \uD544\uC218"},400);let s=0;for(let r of t){let{url:a,html:i,variant:l="",device:b="desktop"}=r||{};if(!a||!i)continue;let p=l.startsWith("v2_")?l.substring(3):b,w=new URL(a).pathname||"/",C=await y(w,p);await n.V2_CACHE.put(C,i,{expirationTtl:pt}),s++}return f({success:!0,updated:s})}catch(t){return f({success:!1,error:t.message},500)}}async function dt(e,n){try{let{urls:t=[],variant:s=""}=await e.json();if(!n.V2_CACHE)return f({success:!1,error:"V2_CACHE \uBC14\uC778\uB529 \uC5C6\uC74C"},500);if(!t.length)return f({success:!1,error:"urls \uD544\uC218"},400);let r=s.startsWith("v2_")?s.substring(3):"desktop",a=0;for(let i of t){let l=new URL(i).pathname||"/",b=await y(l,r);await n.V2_CACHE.delete(b),a++}return f({success:!0,deleted:a})}catch(t){return f({success:!1,error:t.message},500)}}async function ht(e,n){try{let{url:t,variant:s=""}=await e.json();if(!n.V2_CACHE)return f({success:!1,error:"V2_CACHE \uBC14\uC778\uB529 \uC5C6\uC74C"},500);if(!t)return f({success:!1,error:"url \uD544\uC218"},400);let r=s.startsWith("v2_")?s.substring(3):"desktop",a=new URL(t).pathname||"/",i=await y(a,r),l=await n.V2_CACHE.get(i);return f({success:!0,html:l??null})}catch(t){return f({success:!1,error:t.message},500)}}async function L(e,n,t,s,r){let a={items:[{url:t,project_id:n}],ocr:s};r.length>0&&(a.path_prefixes=r),await fetch(`${e}/v2/cache/kv/ai`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(a)})}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-YO3LY7GV.js";import{a as r}from"./chunk-3GWBNF6R.js";import"./chunk-62QI7OUQ.js";export{o as cloudflareProxy,r as nextjsProxy};
|
package/package.json
CHANGED
package/dist/chunk-HLBLRAGQ.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{a as E,b as R,c as w}from"./chunk-62QI7OUQ.js";var S=/^\/(?:robots\.txt|llms\.txt|sitemap[^\/]*\.(?:xml|xsl))$/i,I=/mobile|android|iphone|phone|tablet|webos|ipad/i;async function V(s,n,r,p){let{projectId:t,siteDomain:u,prerenderDomain:i,botLogEndpoint:h,aiRender:k=!0,ocr:$=!0,aiPathPrefixes:C=[]}=p,e=new URL(s.url).pathname,c=u.replace(/^https?:\/\//,"").replace(/\/+$/,"");if(s.headers.get("x-prerender-host"))return null;if(e===`/${t}.txt`)try{let o=await fetch(`${i}${e}`,{redirect:"manual"});return new Response(o.body,{status:o.status,headers:{"Content-Type":"text/plain","Cache-Control":"public, max-age=3600"}})}catch{return new Response("",{status:404})}if(S.test(e))try{let o=await fetch(`${i}/static${e}`,{redirect:"manual",headers:{"X-Signature":t,"X-Original-Host":c}});return new Response(o.body,{status:o.status,headers:{"Content-Type":o.headers.get("Content-Type")||"text/plain","Cache-Control":"public, max-age=3600"}})}catch{return null}let l=s.headers.get("user-agent")||"",a=R(l);if(!a||!n.V2_CACHE)return null;let P=I.test(l)?"mobile":"desktop",d=`https://${c}${e}`,g=E.has(a.name)?"ai-user":a.isAi?"ai-crawl":"standard",m=o=>r.waitUntil(o().catch(f=>console.error("[bot-proxy]",f)));try{if(a.isAi&&k){let A=await w(e,"ai"),x=await n.V2_CACHE.get(A);if(x)return m(async()=>{console.log(`[BOT] ${a.name} | ${g} | hit_ai | ${e}`),await y(h,t,c,e,l,"hit_ai"),await _(i,t,d,$,C)}),b(x,"HIT-AI",a.name);let H=await w(e,"desktop"),O=await n.V2_CACHE.get(H);return O?(m(async()=>{console.log(`[BOT] ${a.name} | ${g} | hit_desktop_fallback | ${e}`),await y(h,t,c,e,l,"hit_desktop_fallback"),await _(i,t,d,$,C)}),b(O,"HIT-DESKTOP",a.name)):(m(async()=>{console.log(`[BOT] ${a.name} | ${g} | miss_ai | ${e}`),await y(h,t,c,e,l,"miss_ai"),await Promise.all([_(i,t,d,$,C),T(i,t,d)])}),null)}let o=await w(e,P),f=await n.V2_CACHE.get(o);return f?(m(async()=>{console.log(`[BOT] ${a.name} | ${g} | hit | ${e}`),await y(h,t,c,e,l,"hit"),await T(i,t,d)}),b(f,"HIT",a.name)):(m(async()=>{console.log(`[BOT] ${a.name} | ${g} | miss | ${e}`),await y(h,t,c,e,l,"miss"),await T(i,t,d)}),null)}catch(o){return console.error("[bot-proxy]",o),null}}function b(s,n,r){return new Response(s,{headers:{"Content-Type":"text/html; charset=utf-8","X-Cache":n,"X-Bot":r}})}async function y(s,n,r,p,t,u){await fetch(s,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({project_id:n,url:`https://${r}${p}`,domain:r,path:p,user_agent:t,phase:u})})}async function T(s,n,r){await fetch(`${s}/v2/cache/kv`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({items:[{url:r,project_id:n}]})})}async function _(s,n,r,p,t){let u={items:[{url:r,project_id:n}],ocr:p};t.length>0&&(u.path_prefixes=t),await fetch(`${s}/v2/cache/kv/ai`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(u)})}export{V as a};
|