yq-pdf
Version:
High-performance PDF manipulation library with native processing capabilities. Supports encryption, decryption, merging, splitting, watermarking, optimization, and comprehensive PDF operations with both file and buffer support.
8 lines (7 loc) • 5.35 kB
JavaScript
import T from"https";import W from"fs";import Z from"path";import{pipeline as V}from"stream";import{promisify as J}from"util";import{fileURLToPath as M}from"url";var N=M(import.meta.url),$=Z.dirname(N),R=J(V);class E{total;current;width;startTime;constructor(C,D=40){this.total=C,this.current=0,this.width=D,this.startTime=Date.now()}update(C){this.current=C;let D=Math.round(C/this.total*100),F=Math.round(C/this.total*this.width),K=this.width-F,O=(Date.now()-this.startTime)/1000,Q=C/O,X=Q>0?(this.total-C)/Q:0,A="█".repeat(F)+"░".repeat(K),Y=this.formatBytes(C)+"/"+this.formatBytes(this.total),L=this.formatBytes(Q)+"/s";process.stdout.write(`\r\x1B[36m[\x1B[0m${A}\x1B[36m]\x1B[0m ${D}% ${Y} ${L} ETA: ${this.formatTime(X)}`)}complete(){let C=(Date.now()-this.startTime)/1000;process.stdout.write(`\r\x1B[32m[\x1B[0m${"█".repeat(this.width)}\x1B[32m]\x1B[0m 100% ${this.formatBytes(this.total)} Complete in ${this.formatTime(C)}
`)}formatBytes(C){if(C===0)return"0 B";let D=1024,F=["B","KB","MB","GB"],K=Math.floor(Math.log(C)/Math.log(D));return parseFloat((C/Math.pow(D,K)).toFixed(1))+" "+F[K]}formatTime(C){if(C<60)return Math.round(C)+"s";let D=Math.floor(C/60),F=Math.round(C%60);return`${D}m ${F}s`}}function q(){let{platform:C,arch:D}=process,F={win32:"windows",darwin:"darwin",linux:"linux",freebsd:"freebsd",openbsd:"openbsd"},K={x64:"amd64",arm64:"arm64",arm:"arm",ia32:"386"},O=F[C],Q=K[D];if(!O||!Q)throw new Error(`Unsupported platform: ${C}-${D}`);let X=C==="win32"?".exe":"",A=`yq-pdf-${O}-${Q}${X}`,Y=`yq-pdf-${O}-${Q}${X}`;return{platform:O,arch:Q,binaryName:A,localBinaryName:Y}}function x(){try{let C=Z.join($,"..","package.json");if(!W.existsSync(C))C=Z.join($,"..","..","package.json");if(!W.existsSync(C))C=Z.join($,"..","package.json");return JSON.parse(W.readFileSync(C,"utf8")).version}catch(C){throw new Error(`Failed to read package.json: ${C.message}`)}}function G(){try{let C=Z.join($,"..","package.json");if(!W.existsSync(C))C=Z.join($,"..","..","package.json");if(!W.existsSync(C))C=Z.join($,"..","package.json");let D;if(W.existsSync(C)){let K=JSON.parse(W.readFileSync(C,"utf8"));D=K.repository?.url||K.homepage}if(!D)return console.log("\x1B[33m⚠ No repository URL found in package.json, using default\x1B[0m"),{owner:"yuniqsolutions",repo:"yq-pdf"};let F=D.match(/github\.com[\/:]([^\/]+)\/([^\/\.]+)/);if(!F)return console.log("\x1B[33m⚠ Invalid GitHub repository URL, using default\x1B[0m"),{owner:"yuniqsolutions",repo:"yq-pdf"};return{owner:F[1],repo:F[2]}}catch(C){return console.log("\x1B[33m⚠ Failed to parse repository info, using default\x1B[0m"),{owner:"yuniqsolutions",repo:"yq-pdf"}}}function H(C){return new Promise((D,F)=>{let K=T.get(C,{headers:{"User-Agent":"yq-pdf-postinstall"}},(O)=>{if(O.statusCode===302||O.statusCode===301){let Q=O.headers.location;if(!Q){F(new Error("Redirect response missing location header"));return}return H(Q).then(D).catch(F)}if(O.statusCode!==200){F(new Error(`HTTP ${O.statusCode}: ${O.statusMessage}`));return}D(O)});K.on("error",F),K.setTimeout(30000,()=>{K.destroy(),F(new Error("Request timeout"))})})}async function I(C,D){console.log(`\x1B[34mDownloading binary from:\x1B[0m ${C}`);try{let F=await H(C),K=parseInt(F.headers["content-length"],10);if(!K)throw new Error("Unable to determine file size");let O=new E(K),Q=0,X=Z.dirname(D);try{if(!W.existsSync(X))W.mkdirSync(X,{recursive:!0})}catch(Y){throw new Error(`Failed to create directory ${X}: ${Y.message}`)}let A=W.createWriteStream(D);if(F.on("data",(Y)=>{Q+=Y.length,O.update(Q)}),await R(F,A),O.complete(),process.platform!=="win32")try{W.chmodSync(D,493)}catch(Y){console.warn(`\x1B[33m⚠ Warning: Could not make binary executable: ${Y.message}\x1B[0m`)}console.log(`\x1B[32m✓ Binary downloaded successfully to:\x1B[0m ${D}`)}catch(F){throw new Error(`Download failed: ${F.message}`)}}async function S(){console.log(`\x1B[36m\uD83D\uDE80 yq-pdf postinstall: Setting up binary...\x1B[0m
`);try{let C=q();console.log(`\x1B[33mDetected platform:\x1B[0m ${C.platform}-${C.arch}`);let D=x(),{owner:F,repo:K}=G();console.log(`\x1B[33mPackage version:\x1B[0m v${D}`),console.log(`\x1B[33mRepository:\x1B[0m ${F}/${K}
`);let O=`https://github.com/${F}/${K}/releases/download/v${D}/${C.binaryName}`,Q=Z.join($,C.localBinaryName);if(console.log(`\x1B[34mTarget path:\x1B[0m ${Q}`),await I(O,Q),!W.existsSync(Q))throw new Error("Binary was not created successfully");let X=W.statSync(Q);if(X.size===0)throw new Error("Downloaded binary is empty");console.log(`\x1B[32m✓ Binary setup complete! Size: ${(X.size/1024/1024).toFixed(2)} MB\x1B[0m`),console.log(`\x1B[36m\uD83C\uDF89 yq-pdf is ready to use!\x1B[0m
`)}catch(C){console.error(`\x1B[31m❌ Postinstall failed: ${C.message}\x1B[0m`),console.error("\x1B[31m\uD83D\uDCA1 This will prevent the package installation from completing.\x1B[0m"),console.error(`\x1B[31m\uD83D\uDD27 Please check your internet connection and try again.\x1B[0m
`),process.exit(1)}}process.on("unhandledRejection",(C)=>{console.error(`\x1B[31m❌ Unhandled error: ${C.message}\x1B[0m`),process.exit(1)});process.on("SIGINT",()=>{console.log(`
\x1B[31m❌ Installation cancelled by user\x1B[0m`),process.exit(1)});if(import.meta.url===`file://${process.argv[1]}`||process.argv[1]?.endsWith("postinstall.ts")||process.argv[1]?.endsWith("postinstall.js"))S();