11#!/usr/bin/env node
22/**
3- * Postinstall script - downloads the correct thoth-core binary
3+ * Postinstall script - downloads and decompresses the correct thoth-core binary
44 * 𓅝
55 */
66
77import { platform , arch } from 'os' ;
88import { join , dirname } from 'path' ;
9- import { existsSync , mkdirSync , createWriteStream , chmodSync , statSync } from 'fs' ;
9+ import { existsSync , mkdirSync , createWriteStream , chmodSync , statSync , unlinkSync } from 'fs' ;
10+ import { createGunzip } from 'zlib' ;
11+ import { pipeline } from 'stream/promises' ;
1012import { fileURLToPath } from 'url' ;
1113import https from 'https' ;
1214
1315const __dirname = dirname ( fileURLToPath ( import . meta. url ) ) ;
1416
15- const VERSION = '0.2.7 ' ;
17+ const VERSION = '0.2.9 ' ;
1618const REPO = 'aklo360/thoth-cli' ;
17- const BASE_URL = `https://github.com/${ REPO } /releases/download/v${ VERSION } ` ;
19+
20+ // Use jsDelivr CDN for faster downloads (mirrors GitHub releases)
21+ const CDN_URL = `https://cdn.jsdelivr.net/gh/${ REPO } @v${ VERSION } /releases` ;
22+ // Fallback to GitHub releases directly
23+ const GITHUB_URL = `https://github.com/${ REPO } /releases/download/v${ VERSION } ` ;
1824
1925function getPlatformKey ( ) {
2026 const p = platform ( ) ;
@@ -38,14 +44,12 @@ function getBinaryName() {
3844 return platform ( ) === 'win32' ? 'thoth-core.exe' : 'thoth-core' ;
3945}
4046
41- async function download ( url , dest ) {
47+ function download ( url ) {
4248 return new Promise ( ( resolve , reject ) => {
43- const file = createWriteStream ( dest ) ;
44-
4549 https . get ( url , ( response ) => {
4650 // Handle redirects
4751 if ( response . statusCode === 302 || response . statusCode === 301 ) {
48- download ( response . headers . location , dest ) . then ( resolve ) . catch ( reject ) ;
52+ download ( response . headers . location ) . then ( resolve ) . catch ( reject ) ;
4953 return ;
5054 }
5155
@@ -54,15 +58,19 @@ async function download(url, dest) {
5458 return ;
5559 }
5660
57- response . pipe ( file ) ;
58- file . on ( 'finish' , ( ) => {
59- file . close ( ) ;
60- resolve ( ) ;
61- } ) ;
61+ resolve ( response ) ;
6262 } ) . on ( 'error' , reject ) ;
6363 } ) ;
6464}
6565
66+ async function downloadAndDecompress ( url , dest ) {
67+ const response = await download ( url ) ;
68+ const gunzip = createGunzip ( ) ;
69+ const fileStream = createWriteStream ( dest ) ;
70+
71+ await pipeline ( response , gunzip , fileStream ) ;
72+ }
73+
6674async function main ( ) {
6775 const platformKey = getPlatformKey ( ) ;
6876 const binaryName = getBinaryName ( ) ;
@@ -72,34 +80,43 @@ async function main() {
7280 // Skip if binary already exists AND has content
7381 if ( existsSync ( binaryPath ) ) {
7482 const stats = statSync ( binaryPath ) ;
75- if ( stats . size > 0 ) {
76- console . log ( `✓ thoth-core binary already exists` ) ;
83+ if ( stats . size > 1000 ) { // Real binary is ~18MB, placeholder is 0
84+ console . log ( `✓ thoth-core binary already exists ( ${ ( stats . size / 1024 / 1024 ) . toFixed ( 1 ) } MB) ` ) ;
7785 return ;
7886 }
79- console . log ( `Found empty placeholder, downloading actual binary...` ) ;
87+ console . log ( `Found placeholder, downloading actual binary...` ) ;
88+ unlinkSync ( binaryPath ) ; // Remove placeholder
8089 }
8190
82- console . log ( `Downloading thoth-core for ${ platformKey } ...` ) ;
83-
8491 // Create bin directory
8592 mkdirSync ( binDir , { recursive : true } ) ;
8693
87- // Download binary
88- const url = `${ BASE_URL } /thoth-core-${ platformKey } ${ platform ( ) === 'win32' ? '.exe' : '' } ` ;
94+ // Compressed filename
95+ const compressedName = `thoth-core-${ platformKey } ${ platform ( ) === 'win32' ? '.exe' : '' } .gz` ;
96+
97+ // Try GitHub releases (jsDelivr doesn't serve release assets directly)
98+ const url = `${ GITHUB_URL } /${ compressedName } ` ;
99+
100+ console . log ( `Downloading thoth-core for ${ platformKey } ...` ) ;
101+ console . log ( ` From: ${ url } ` ) ;
102+
103+ const startTime = Date . now ( ) ;
89104
90105 try {
91- await download ( url , binaryPath ) ;
106+ await downloadAndDecompress ( url , binaryPath ) ;
92107
93108 // Make executable on Unix
94109 if ( platform ( ) !== 'win32' ) {
95110 chmodSync ( binaryPath , 0o755 ) ;
96111 }
97112
98- console . log ( `✓ Downloaded thoth-core to ${ binaryPath } ` ) ;
113+ const stats = statSync ( binaryPath ) ;
114+ const elapsed = ( ( Date . now ( ) - startTime ) / 1000 ) . toFixed ( 1 ) ;
115+ console . log ( `✓ Downloaded and decompressed (${ ( stats . size / 1024 / 1024 ) . toFixed ( 1 ) } MB) in ${ elapsed } s` ) ;
99116 } catch ( error ) {
100117 console . warn ( `⚠ Could not download binary: ${ error . message } ` ) ;
101- console . warn ( ` You can run in development mode with Python installed .` ) ;
102- console . warn ( ` Install thoth-core : pip install thoth-core` ) ;
118+ console . warn ( ` You may need to install thoth-core manually .` ) ;
119+ console . warn ( ` Or install via : pip install thoth-core` ) ;
103120 }
104121}
105122
0 commit comments