|
| 1 | +/** |
| 2 | + * Bootstrap loader for Socket CLI npm wrapper package. |
| 3 | + * |
| 4 | + * This script handles three scenarios: |
| 5 | + * 1. Brotli-compressed CLI exists -> decompress and execute |
| 6 | + * 2. Uncompressed CLI exists -> execute directly |
| 7 | + * 3. CLI not found -> download from npm using dlxPackage, then execute |
| 8 | + */ |
| 9 | + |
| 10 | +import { spawnSync } from 'node:child_process' |
| 11 | +import { existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from 'node:fs' |
| 12 | +import { homedir, tmpdir } from 'node:os' |
| 13 | +import path from 'node:path' |
| 14 | +import { brotliDecompressSync } from 'node:zlib' |
| 15 | + |
| 16 | +import type { DlxPackageResult } from '@socketsecurity/lib/dlx-package' |
| 17 | + |
| 18 | +import { dlxPackage } from '@socketsecurity/lib/dlx-package' |
| 19 | + |
| 20 | +const SOCKET_DLX_DIR = path.join(homedir(), '.socket', '_dlx') |
| 21 | + |
| 22 | +// Note: CLI_PACKAGE_DIR will be dynamically determined by dlxPackage. |
| 23 | +let cliPackageDir: string | undefined |
| 24 | +let cliEntry: string | undefined |
| 25 | +let cliEntryBz: string | undefined |
| 26 | + |
| 27 | +/** |
| 28 | + * Get paths for CLI package. |
| 29 | + */ |
| 30 | +function getCliPaths(): { cliEntry: string; cliEntryBz: string } { |
| 31 | + if (!cliPackageDir) { |
| 32 | + throw new Error('CLI package directory not initialized') |
| 33 | + } |
| 34 | + return { |
| 35 | + cliEntry: path.join(cliPackageDir, 'node_modules', '@socketsecurity', 'cli', 'dist', 'cli.js'), |
| 36 | + cliEntryBz: path.join(cliPackageDir, 'node_modules', '@socketsecurity', 'cli', 'dist', 'cli.js.bz'), |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Get command-line arguments. |
| 42 | + */ |
| 43 | +function getArgs(): string[] { |
| 44 | + return process.argv ? process.argv.slice(2) : [] |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * Execute the CLI with the given arguments. |
| 49 | + */ |
| 50 | +function executeCli(cliPath: string, args: string[]): never { |
| 51 | + const result = spawnSync(process.execPath, [cliPath, ...args], { |
| 52 | + env: { |
| 53 | + ...process.env, |
| 54 | + PKG_EXECPATH: process.env.PKG_EXECPATH || 'PKG_INVOKE_NODEJS', |
| 55 | + }, |
| 56 | + stdio: 'inherit', |
| 57 | + }) |
| 58 | + process.exit(result.status ?? 0) |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Execute brotli-compressed CLI. |
| 63 | + */ |
| 64 | +function executeCompressedCli(bzPath: string, args: string[]): never { |
| 65 | + // Read compressed file. |
| 66 | + const compressed = readFileSync(bzPath) |
| 67 | + |
| 68 | + // Decompress with brotli. |
| 69 | + const decompressed = brotliDecompressSync(compressed) |
| 70 | + |
| 71 | + // Write to temporary file and execute. |
| 72 | + // Using a temp file allows us to maintain spawn behavior for proper stdio handling. |
| 73 | + const tempCliPath = path.join(tmpdir(), `socket-cli-${process.pid}.js`) |
| 74 | + writeFileSync(tempCliPath, decompressed) |
| 75 | + |
| 76 | + try { |
| 77 | + executeCli(tempCliPath, args) |
| 78 | + } finally { |
| 79 | + // Clean up temp file. |
| 80 | + try { |
| 81 | + unlinkSync(tempCliPath) |
| 82 | + } catch { |
| 83 | + // Ignore cleanup errors. |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * Download and install CLI using dlxPackage. |
| 90 | + */ |
| 91 | +async function downloadCli(): Promise<DlxPackageResult> { |
| 92 | + process.stderr.write('📦 Socket CLI not found, downloading...\n') |
| 93 | + process.stderr.write('\n') |
| 94 | + |
| 95 | + // Create directories. |
| 96 | + mkdirSync(SOCKET_DLX_DIR, { recursive: true }) |
| 97 | + |
| 98 | + try { |
| 99 | + // Use dlxPackage to download and install @socketsecurity/cli. |
| 100 | + const result = await dlxPackage( |
| 101 | + [], // Empty args - we don't want to execute anything. |
| 102 | + { |
| 103 | + force: false, // Use cached version if available. |
| 104 | + package: '@socketsecurity/cli', |
| 105 | + spawnOptions: { |
| 106 | + stdio: 'pipe', // Suppress output from the package execution. |
| 107 | + }, |
| 108 | + }, |
| 109 | + ) |
| 110 | + |
| 111 | + // Save the package directory for later use. |
| 112 | + cliPackageDir = result.packageDir |
| 113 | + |
| 114 | + process.stderr.write(` Installed to: ${cliPackageDir}\n`) |
| 115 | + |
| 116 | + // Wait for installation to complete (but the spawn will fail since we don't have a command). |
| 117 | + // That's okay - we just need the package installed. |
| 118 | + try { |
| 119 | + await result.spawnPromise |
| 120 | + } catch { |
| 121 | + // Ignore execution errors - we only care that the package was installed. |
| 122 | + } |
| 123 | + |
| 124 | + process.stderr.write('✅ Socket CLI installed successfully\n') |
| 125 | + process.stderr.write('\n') |
| 126 | + |
| 127 | + return result |
| 128 | + } catch (e) { |
| 129 | + process.stderr.write('❌ Failed to download Socket CLI\n') |
| 130 | + process.stderr.write(` Error: ${e instanceof Error ? e.message : String(e)}\n`) |
| 131 | + process.exit(1) |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +/** |
| 136 | + * Main bootstrap logic. |
| 137 | + */ |
| 138 | +async function main(): Promise<void> { |
| 139 | + const args = getArgs() |
| 140 | + |
| 141 | + // Check if CLI is already installed by trying to locate it in dlx cache. |
| 142 | + // We need to download it first to find out where it is. |
| 143 | + await downloadCli() |
| 144 | + |
| 145 | + // Now get the paths. |
| 146 | + const { cliEntry, cliEntryBz } = getCliPaths() |
| 147 | + |
| 148 | + // Check for brotli-compressed CLI first. |
| 149 | + if (existsSync(cliEntryBz)) { |
| 150 | + executeCompressedCli(cliEntryBz, args) |
| 151 | + } |
| 152 | + |
| 153 | + // Fallback to uncompressed CLI. |
| 154 | + if (existsSync(cliEntry)) { |
| 155 | + executeCli(cliEntry, args) |
| 156 | + } |
| 157 | + |
| 158 | + // If we still can't find the CLI, exit with error. |
| 159 | + process.stderr.write('❌ Socket CLI installation failed\n') |
| 160 | + process.stderr.write(' CLI entry point not found after installation\n') |
| 161 | + process.stderr.write(` Looked in: ${cliEntry}\n`) |
| 162 | + process.exit(1) |
| 163 | +} |
| 164 | + |
| 165 | +// Run the bootstrap. |
| 166 | +main().catch((e) => { |
| 167 | + process.stderr.write(`❌ Bootstrap error: ${e instanceof Error ? e.message : String(e)}\n`) |
| 168 | + process.exit(1) |
| 169 | +}) |
0 commit comments