import { execFile } from 'child_process'; import { ExecResult } from '../types'; // Wraps the `goat` CLI (ATProto swiss-army tool) — used for DID resolution and // reading/listing records. execFile (not shell) so args are never shell-interpolated. export function executeGoatCommand(args: string[], timeout = 30000): Promise { return new Promise((resolve) => { execFile('goat', args, { timeout, maxBuffer: 5 * 1024 * 1024 }, (error, stdout, stderr) => { resolve({ success: !error, stdout: stdout.toString(), stderr: stderr.toString(), code: error ? error.code as number : 0 }); }); }); } export async function resolveDid(identifier: string): Promise { const result = await executeGoatCommand(['resolve', identifier]); if (!result.success) { throw new Error(result.stderr || 'Failed to resolve DID'); } try { return JSON.parse(result.stdout); } catch { return { raw: result.stdout.trim() }; } } export async function getRecord(uri: string): Promise { const result = await executeGoatCommand(['get', uri]); if (!result.success) { throw new Error(result.stderr || 'Failed to get record'); } try { return JSON.parse(result.stdout); } catch { return { raw: result.stdout.trim() }; } } export async function listCollections(did: string, listCollectionsFlag: boolean): Promise<{ items: string[], count: number }> { const args = listCollectionsFlag ? ['ls', '-c', did] : ['ls', did]; const result = await executeGoatCommand(args); if (!result.success) { throw new Error(result.stderr || 'Failed to list collections'); } const items = result.stdout.trim().split('\n').filter(Boolean); return { items, count: items.length }; }