@lightpanda/browser
Version:
Lightpanda for Node.js
1 lines • 1.68 kB
JavaScript
import{execSync as t}from"child_process";import r from"os";var o=`${r.homedir()}/.cache/lightpanda-node`,e=process.env.LIGHTPANDA_EXECUTABLE_PATH,i=`${o}/lightpanda`,n=t=>{if(!t||"string"!=typeof t)throw new Error(`URL is required and must be a string ${t}`);try{new URL(t)}catch{throw new Error(`Invalid URL format ${t}`)}if(!t.startsWith("http://")&&!t.startsWith("https://"))throw new Error(`URL must use http or https protocol ${t}`)},a=()=>e??i,s={dump:!0};import{spawn as l}from"child_process";var p={host:"127.0.0.1",port:9222},h={fetch:(r,o=s)=>{const{dump:e,disableHostVerification:i,httpProxy:l}=o;return n(r),l&&n(l),new Promise((o,n)=>{try{const n=a(),s=[{flag:"--dump",condition:e},{flag:"--insecure_disable_tls_host_verification",condition:i},{flag:`--http_proxy ${l}`,condition:l}].map(t=>t.condition?t.flag:"").join(" "),p=t(`${n} fetch ${s} ${r}`);e&&o(p.toString()),o(p)}catch(t){n(t)}})},serve:(t=p)=>{const{host:r,port:o,timeout:e,disableHostVerification:i,httpProxy:s}=t;return o&&(t=>{if(!t||"number"!=typeof t)throw new Error(`Port is required and must be a number ${t}`);if(t<=0)throw new Error(`Port should be a positive number ${t}`)})(o),s&&n(s),new Promise((t,n)=>{const p=a(),h=[{flag:"--host",value:r},{flag:"--port",value:o},{flag:"--timeout",value:e},{flag:"--insecure_disable_tls_host_verification",value:i,flagOnly:!0},{flag:"--http_proxy",value:s}].flatMap(t=>t.value?[t.flag,t.flagOnly?"":t.value.toString()]:"").filter(t=>""!==t),f=l(p,["serve",...h]);f.on("spawn",async()=>{console.info("🐼 Running Lightpanda's CDP server…",{pid:f.pid}),await new Promise(t=>setTimeout(t,250)),t(f)}),f.on("error",t=>n(t))})}};export{h as lightpanda};