|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * LeanSpec HTTP Server Binary Wrapper |
| 4 | + * |
| 5 | + * This script detects the current platform and architecture, |
| 6 | + * then spawns the appropriate Rust HTTP server binary. |
| 7 | + * |
| 8 | + * The wrapper looks for binaries in the following locations: |
| 9 | + * 1. Platform-specific npm package (@leanspec/http-darwin-x64, etc.) |
| 10 | + * 2. Local binaries directory (for development) |
| 11 | + * 3. Rust target directory (for local development) |
| 12 | + */ |
| 13 | + |
| 14 | +import { spawn } from 'child_process'; |
| 15 | +import { createRequire } from 'module'; |
| 16 | +import { fileURLToPath } from 'url'; |
| 17 | +import { dirname, join } from 'path'; |
| 18 | +import { accessSync } from 'fs'; |
| 19 | + |
| 20 | +const require = createRequire(import.meta.url); |
| 21 | +const __filename = fileURLToPath(import.meta.url); |
| 22 | +const __dirname = dirname(__filename); |
| 23 | + |
| 24 | +// Debug mode - enable with LEANSPEC_DEBUG=1 |
| 25 | +const DEBUG = process.env.LEANSPEC_DEBUG === '1'; |
| 26 | +const debug = (...args) => DEBUG && console.error('[leanspec-http debug]', ...args); |
| 27 | + |
| 28 | +// Platform detection mapping |
| 29 | +const PLATFORM_MAP = { |
| 30 | + darwin: { x64: 'darwin-x64', arm64: 'darwin-arm64' }, |
| 31 | + linux: { x64: 'linux-x64', arm64: 'linux-arm64' }, |
| 32 | + win32: { x64: 'windows-x64', arm64: 'windows-arm64' } |
| 33 | +}; |
| 34 | + |
| 35 | +function getBinaryPath() { |
| 36 | + const platform = process.platform; |
| 37 | + const arch = process.arch; |
| 38 | + |
| 39 | + debug('Platform detection:', { platform, arch }); |
| 40 | + |
| 41 | + const platformKey = PLATFORM_MAP[platform]?.[arch]; |
| 42 | + if (!platformKey) { |
| 43 | + console.error(`Unsupported platform: ${platform}-${arch}`); |
| 44 | + console.error('Supported: macOS (x64/arm64), Linux (x64/arm64), Windows (x64)'); |
| 45 | + process.exit(1); |
| 46 | + } |
| 47 | + |
| 48 | + const isWindows = platform === 'win32'; |
| 49 | + const binaryName = isWindows ? 'leanspec-http.exe' : 'leanspec-http'; |
| 50 | + const packageName = `@leanspec/http-${platformKey}`; |
| 51 | + |
| 52 | + debug('Binary info:', { platformKey, binaryName, packageName }); |
| 53 | + |
| 54 | + // Try to resolve platform package |
| 55 | + try { |
| 56 | + const resolvedPath = require.resolve(`${packageName}/${binaryName}`); |
| 57 | + debug('Found platform package binary:', resolvedPath); |
| 58 | + return resolvedPath; |
| 59 | + } catch (e) { |
| 60 | + debug('Platform package not found:', packageName, '-', e.message); |
| 61 | + } |
| 62 | + |
| 63 | + // Try local binaries directory (for development/testing) |
| 64 | + try { |
| 65 | + const localPath = join(__dirname, '..', 'binaries', platformKey, binaryName); |
| 66 | + debug('Trying local binary:', localPath); |
| 67 | + accessSync(localPath); |
| 68 | + debug('Found local binary:', localPath); |
| 69 | + return localPath; |
| 70 | + } catch (e) { |
| 71 | + debug('Local binary not found:', e.message); |
| 72 | + } |
| 73 | + |
| 74 | + // Try rust/target/release directory (for local development) |
| 75 | + try { |
| 76 | + const rustTargetPath = join(__dirname, '..', '..', '..', 'rust', 'target', 'release', binaryName); |
| 77 | + debug('Trying rust target binary:', rustTargetPath); |
| 78 | + accessSync(rustTargetPath); |
| 79 | + debug('Found rust target binary:', rustTargetPath); |
| 80 | + return rustTargetPath; |
| 81 | + } catch (e) { |
| 82 | + debug('Rust target binary not found:', e.message); |
| 83 | + } |
| 84 | + |
| 85 | + console.error(`Binary not found for ${platform}-${arch}`); |
| 86 | + console.error(`Expected package: ${packageName}`); |
| 87 | + console.error(''); |
| 88 | + console.error('To install:'); |
| 89 | + console.error(' npm install @leanspec/http-server'); |
| 90 | + console.error(''); |
| 91 | + process.exit(1); |
| 92 | +} |
| 93 | + |
| 94 | +// Execute binary |
| 95 | +const binaryPath = getBinaryPath(); |
| 96 | +const args = process.argv.slice(2); |
| 97 | + |
| 98 | +debug('Spawning binary:', binaryPath); |
| 99 | +debug('Arguments:', args); |
| 100 | + |
| 101 | +const child = spawn(binaryPath, args, { |
| 102 | + stdio: 'inherit', |
| 103 | + windowsHide: true, |
| 104 | +}); |
| 105 | + |
| 106 | +child.on('exit', (code) => { |
| 107 | + debug('Binary exited with code:', code); |
| 108 | + process.exit(code ?? 1); |
| 109 | +}); |
| 110 | + |
| 111 | +child.on('error', (err) => { |
| 112 | + console.error('Failed to start leanspec-http:', err.message); |
| 113 | + debug('Spawn error:', err); |
| 114 | + process.exit(1); |
| 115 | +}); |
0 commit comments