| … | … | @@ -3,7 +3,7 @@ import FramedStream from 'framed-stream' |
| 3 | 3 | import b4a from 'b4a' |
| 4 | 4 | import path from 'path' |
| 5 | 5 | import os from 'os' |
| 6 | | import { getGenericEventName } from './lib/format.js' |
| 6 | import { getGenericEventName } from 'keet-daemon/lib/format.js' |
| 7 | 7 | |
| 8 | 8 | // --- Parse Args --- |
| 9 | 9 | const rawArgs = Pear.config.args || [] |
| … | … | @@ -31,7 +31,7 @@ for (let i = 0; i < rawArgs.length; i++) { |
| 31 | 31 | } |
| 32 | 32 | } |
| 33 | 33 | |
| 34 | | const STORAGE = customStorage || path.join(os.homedir(), '.config', 'keet-cli', 'default') |
| 34 | const STORAGE = customStorage || path.join(os.homedir(), '.config', 'keet-cli') |
| 35 | 35 | const SOCKET = customSocket || '/tmp/keet-daemon.sock' |
| 36 | 36 | const command = args[0] |
| 37 | 37 | |
| … | … | @@ -163,6 +163,52 @@ if (command === 'daemon') { |
| 163 | 163 | await new Promise(() => {}) |
| 164 | 164 | } |
| 165 | 165 | |
| 166 | // ============================================================ |
| 167 | // UDS client helpers (framed-stream JSON-RPC over Unix socket) |
| 168 | // ============================================================ |
| 169 | // NOTE: Must be declared BEFORE the client switch to avoid TDZ errors |
| 170 | |
| 171 | let _nextId = 1 |
| 172 | const _pending = new Map() |
| 173 | |
| 174 | function rpc (method, params) { |
| 175 | return new Promise((resolve, reject) => { |
| 176 | const id = _nextId++ |
| 177 | const msg = JSON.stringify({ id, method, params: params || {} }) |
| 178 | |
| 179 | const pipe = new Pipe(SOCKET) |
| 180 | const framed = new FramedStream(pipe) |
| 181 | |
| 182 | const timeout = setTimeout(() => { |
| 183 | framed.destroy() |
| 184 | reject(new Error('RPC timeout (5s)')) |
| 185 | }, 5000) |
| 186 | |
| 187 | pipe.on('error', (err) => { |
| 188 | clearTimeout(timeout) |
| 189 | reject(err) |
| 190 | }) |
| 191 | |
| 192 | framed.on('data', (buf) => { |
| 193 | try { |
| 194 | const response = JSON.parse(b4a.toString(buf, 'utf8')) |
| 195 | if (response.id === id) { |
| 196 | clearTimeout(timeout) |
| 197 | framed.destroy() |
| 198 | resolve(response) |
| 199 | } |
| 200 | } catch {} |
| 201 | }) |
| 202 | |
| 203 | framed.on('error', (err) => { |
| 204 | clearTimeout(timeout) |
| 205 | reject(err) |
| 206 | }) |
| 207 | |
| 208 | framed.write(b4a.from(msg, 'utf8')) |
| 209 | }) |
| 210 | } |
| 211 | |
| 166 | 212 | // --- Client Mode --- |
| 167 | 213 | try { |
| 168 | 214 | switch (command) { |
| … | … | @@ -687,47 +733,4 @@ async function clientIntrospect (api) { |
| 687 | 733 | } |
| 688 | 734 | } |
| 689 | 735 | |
| 690 | | // ============================================================ |
| 691 | | // UDS client helpers (framed-stream JSON-RPC over Unix socket) |
| 692 | | // ============================================================ |
| 693 | | |
| 694 | | let _nextId = 1 |
| 695 | | const _pending = new Map() |
| 696 | | |
| 697 | | function rpc (method, params) { |
| 698 | | return new Promise((resolve, reject) => { |
| 699 | | const id = _nextId++ |
| 700 | | const msg = JSON.stringify({ id, method, params: params || {} }) |
| 701 | | |
| 702 | | const pipe = Pipe.connect(SOCKET) |
| 703 | | const framed = new FramedStream(pipe) |
| 704 | | |
| 705 | | const timeout = setTimeout(() => { |
| 706 | | framed.destroy() |
| 707 | | reject(new Error('RPC timeout (5s)')) |
| 708 | | }, 5000) |
| 709 | | |
| 710 | | pipe.on('error', (err) => { |
| 711 | | clearTimeout(timeout) |
| 712 | | reject(err) |
| 713 | | }) |
| 714 | | |
| 715 | | framed.on('data', (buf) => { |
| 716 | | try { |
| 717 | | const response = JSON.parse(b4a.toString(buf, 'utf8')) |
| 718 | | if (response.id === id) { |
| 719 | | clearTimeout(timeout) |
| 720 | | framed.destroy() |
| 721 | | resolve(response) |
| 722 | | } |
| 723 | | } catch {} |
| 724 | | }) |
| 725 | | |
| 726 | | framed.on('error', (err) => { |
| 727 | | clearTimeout(timeout) |
| 728 | | reject(err) |
| 729 | | }) |
| 730 | | |
| 731 | | framed.write(b4a.from(msg, 'utf8')) |
| 732 | | }) |
| 733 | | } |
| 736 | // (UDS client helpers moved above client switch to avoid TDZ) |