UNPKG

@kodeglot/node-python-eid-reader

Version:

A cross-platform Node.js package to read Belgian eID card public data using Python eidreader with automatic dependency checking

303 lines • 12.7 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.RequirementsChecker = void 0; const child_process_1 = require("child_process"); const util_1 = require("util"); const sudoPrompt = __importStar(require("sudo-prompt")); const execAsync = (0, util_1.promisify)(child_process_1.exec); function getPlatformRequirements() { const platform = process.platform; if (platform === 'darwin') { return [ { name: 'Python 3.x', checkCommand: 'python3 --version', installCommand: 'brew install python3', description: 'Python 3.x is required to run the eidreader package' }, { name: 'eidreader Python package', checkCommand: 'python3 -c "import eidreader; print(eidreader.__version__)"', installCommand: 'python3 -m pip install eidreader', description: 'The eidreader package is required to read Belgian eID cards' }, { name: 'Belgian eID Middleware', checkCommand: 'ls "/Library/Belgium Identity Card/Pkcs11/libbeidpkcs11.dylib" 2>/dev/null || ' + 'ls "/Library/Belgium Identity Card/Pkcs11/beid-pkcs11.bundle/Contents/MacOS/libbeidpkcs11.dylib" 2>/dev/null || ' + 'ls "/usr/local/lib/libbeidpkcs11.dylib" 2>/dev/null', installCommand: 'echo "Please install Belgian eID middleware from https://eid.belgium.be/en"', description: 'Belgian eID middleware is required for card communication', isOptional: false } ]; } else if (platform === 'win32') { return [ { name: 'Python 3.x', checkCommand: 'python --version || python3 --version', installCommand: 'Download from https://www.python.org/downloads/', description: 'Python 3.x is required to run the eidreader package' }, { name: 'eidreader Python package', checkCommand: 'python -c "import eidreader; print(eidreader.__version__)" || python3 -c "import eidreader; print(eidreader.__version__)"', installCommand: 'python -m pip install eidreader', description: 'The eidreader package is required to read Belgian eID cards' }, { name: 'Belgian eID Middleware', checkCommand: 'if exist "C:\\Program Files\\Belgium Identity Card\\pkcs11\\beidpkcs11.dll" (exit 0) else if exist "C:\\Program Files (x86)\\Belgium Identity Card\\pkcs11\\beidpkcs11.dll" (exit 0) else (exit 1)', installCommand: 'echo "Please install Belgian eID middleware from https://eid.belgium.be/en"', description: 'Belgian eID middleware is required for card communication', isOptional: false } ]; } else if (platform === 'linux') { return [ { name: 'Python 3.x', checkCommand: 'python3 --version', installCommand: 'sudo apt-get install python3 python3-pip', description: 'Python 3.x is required to run the eidreader package' }, { name: 'eidreader Python package', checkCommand: 'python3 -c "import eidreader; print(eidreader.__version__)"', installCommand: 'python3 -m pip install eidreader', description: 'The eidreader package is required to read Belgian eID cards' }, { name: 'Belgian eID Middleware', checkCommand: 'which beidpkcs11.so.0 2>/dev/null || ls /usr/lib/libbeidpkcs11.so.0 2>/dev/null', installCommand: 'sudo apt-get install beidpkcs11', description: 'Belgian eID middleware is required for card communication', isOptional: false } ]; } else { // Fallback for unknown platforms return [ { name: 'Python 3.x', checkCommand: 'python3 --version', installCommand: 'See your OS documentation', description: 'Python 3.x is required to run the eidreader package' }, { name: 'eidreader Python package', checkCommand: 'python3 -c "import eidreader; print(eidreader.__version__)"', installCommand: 'python3 -m pip install eidreader', description: 'The eidreader package is required to read Belgian eID cards' }, { name: 'Belgian eID Middleware', checkCommand: 'echo "Please manually check for Belgian eID middleware"', installCommand: 'See https://eid.belgium.be/en', description: 'Belgian eID middleware is required for card communication', isOptional: false } ]; } } class RequirementsChecker { constructor() { this.requirements = getPlatformRequirements(); } async checkAllRequirements() { console.log('šŸ” Checking system requirements...\n'); const results = []; let allPassed = true; for (const req of this.requirements) { const result = await this.checkRequirement(req); results.push(result); if (!result.available && !req.isOptional) { allPassed = false; } } return { passed: allPassed, results }; } async checkRequirement(req) { try { await execAsync(req.checkCommand); console.log(`āœ… ${req.name} - Available`); return { name: req.name, available: true, error: null }; } catch (error) { console.log(`āŒ ${req.name} - Not found`); console.log(` ${req.description}`); if (!req.isOptional) { console.log(` šŸ’” To install: ${req.installCommand}\n`); } return { name: req.name, available: false, error: error instanceof Error ? error.message : String(error), installCommand: req.installCommand }; } } async runWithSudo(command) { return new Promise((resolve, reject) => { sudoPrompt.exec(command, { name: 'Node-Python-EID-Reader' }, (error, stdout, stderr) => { if (error) { reject(error); } else { resolve(); } }); }); } async installMissingRequirements(results) { const missing = results.filter(r => !r.available && !r.isOptional); if (missing.length === 0) { console.log('āœ… All required dependencies are available!'); return true; } console.log(`\nšŸ”§ Found ${missing.length} missing required dependency(ies):`); missing.forEach(r => console.log(` - ${r.name}`)); console.log('\nšŸ“‹ Installation instructions:'); console.log('1. Belgian eID Middleware: Download from https://eid.belgium.be/en'); console.log('2. Python 3.x: Install from https://python.org/downloads/'); console.log('3. eidreader package: Run "python3 -m pip install eidreader"'); // Try to auto-install Python packages const pythonPackageMissing = missing.find(r => r.name === 'eidreader Python package'); if (pythonPackageMissing) { console.log('\nšŸš€ Attempting to install eidreader package automatically...'); try { await this.installEidreader(); console.log('āœ… eidreader package installed successfully!'); return true; } catch (error) { console.log('āŒ Failed to install eidreader automatically. Please install manually.'); return false; } } // Try to auto-install system-level requirements if possible for (const req of missing) { if (req.installCommand.startsWith('sudo')) { console.log(`\nšŸ”‘ ${req.name} requires elevated privileges. Prompting for password...`); try { await this.runWithSudo(req.installCommand); console.log(`āœ… ${req.name} installed successfully!`); } catch (error) { console.log(`āŒ Failed to install ${req.name} with sudo. Please install manually.`); return false; } } } return false; } async installEidreader() { return new Promise((resolve, reject) => { const child = (0, child_process_1.spawn)('python3', ['-m', 'pip', 'install', 'eidreader'], { stdio: 'inherit' }); child.on('close', (code) => { if (code === 0) { resolve(); } else { reject(new Error(`Installation failed with code ${code}`)); } }); child.on('error', (error) => { reject(error); }); }); } async checkPythonVersion() { try { const { stdout } = await execAsync('python3 --version'); const match = stdout.match(/Python (\d+)\.(\d+)\.(\d+)/); if (match) { const major = parseInt(match[1]); const minor = parseInt(match[2]); return { version: stdout.trim(), major, minor }; } return null; } catch (error) { return null; } } async checkEidreaderVersion() { try { const { stdout } = await execAsync('python3 -c "import eidreader; print(eidreader.__version__)"'); return stdout.trim(); } catch (error) { return null; } } getPlatformSpecificInstallCommands() { const platform = process.platform; if (platform === 'darwin') { return { python: 'brew install python3', eidreader: 'python3 -m pip install eidreader', middleware: 'Download from https://eid.belgium.be/en' }; } else if (platform === 'win32') { return { python: 'Download from https://python.org/downloads/', eidreader: 'python -m pip install eidreader', middleware: 'Download from https://eid.belgium.be/en' }; } else if (platform === 'linux') { return { python: 'sudo apt-get install python3 python3-pip', eidreader: 'python3 -m pip install eidreader', middleware: 'sudo apt-get install beidpkcs11' }; } return {}; } } exports.RequirementsChecker = RequirementsChecker; //# sourceMappingURL=requirements-checker.js.map