const { spawn } = require('node:child_process'); const path = require('node:path'); class WindowsBridge { constructor(root) { this.next = 1; this.pending = new Map(); this.process = spawn(path.join(root, 'vendor/windows/FileBow.Windows.exe'), [], { windowsHide: true, stdio: ['pipe','pipe','pipe'] }); let buffer = ''; this.ready = new Promise((resolve, reject) => { this.process.on('error', reject); this.process.stdout.setEncoding('utf8'); this.process.stdout.on('data', chunk => { buffer += chunk; const lines = buffer.split(/\r?\n/); buffer = lines.pop(); for (const line of lines) { let message; try { message = JSON.parse(line); } catch { continue; } if (message.type === 'ready') resolve(); else if (this.pending.has(message.id)) { const pending = this.pending.get(message.id); this.pending.delete(message.id); clearTimeout(pending.timer); if (message.error) pending.reject(new Error(message.error)); else pending.resolve(message.result); } } }); this.process.stderr.resume(); this.process.on('exit', () => { reject(new Error('The Windows helper process has stopped.')); for (const pending of this.pending.values()) { clearTimeout(pending.timer); pending.reject(new Error('The Windows helper process has stopped.')); } this.pending.clear(); }); }); } async request(command, extra = {}) { await this.ready; return new Promise((resolve,reject) => { const id = this.next++; const timer = setTimeout(() => { this.pending.delete(id); reject(new Error('Windows is not responding.')); }, 10000); this.pending.set(id, { resolve,reject,timer }); this.process.stdin.write(JSON.stringify({ id, command, ...extra }) + '\n'); }); } getExplorerSelection() { return this.request('selection'); } close() { this.process.stdin.end(); } } module.exports = { WindowsBridge };