Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@four-bytes/opencode-plugin-lib",
"version": "0.6.6",
"version": "0.7.0",
"description": "Shared utilities for opencode plugins — toast wrapper, helpers",
"license": "Apache-2.0",
"type": "module",
Expand Down
28 changes: 20 additions & 8 deletions src/bus-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,15 @@ export class BusClient {
/**
* Connect to the plugin bus. Auto-starts the bus binary if not running.
* Falls back to in-memory EventBus if no binary is available.
* @param timeoutMs — Max time to wait for bus to start (default 5000ms)
* @param opts.timeoutMs — Max time to wait for bus to start (default 5000ms)
* @param opts.onWarn — Optional callback for warning messages (default console.warn)
*/
static async connect(timeoutMs = 5000): Promise<BusClient> {
static async connect(
opts?: { timeoutMs?: number; onWarn?: (message: string, ...args: unknown[]) => void },
): Promise<BusClient> {
const timeoutMs = opts?.timeoutMs ?? 5000;
const warn = opts?.onWarn ?? console.warn;

// 1. Check if bus is already running
try {
const port = await discoverPort(500); // quick check — 500ms
Expand All @@ -49,11 +55,11 @@ export class BusClient {

// 2. Auto-start the bus binary
try {
const port = await BusClient.startBus(timeoutMs);
const port = await BusClient.startBus(timeoutMs, warn);
return new BusClient(port);
} catch (err) {
// 3. Fallback to in-memory bus (no binary available)
console.warn(
warn(
"[BusClient] Go bus not available, using in-memory fallback:",
(err as Error).message,
);
Expand All @@ -74,9 +80,12 @@ export class BusClient {
* Guarded by a module-level spawn lock so concurrent connect() calls share
* the same in-flight spawn instead of forking multiple bus processes.
*/
private static async startBus(timeoutMs: number): Promise<number> {
private static async startBus(
timeoutMs: number,
warn: (message: string, ...args: unknown[]) => void,
): Promise<number> {
if (spawnLock) return spawnLock;
spawnLock = BusClient.spawnBus(timeoutMs).finally(() => {
spawnLock = BusClient.spawnBus(timeoutMs, warn).finally(() => {
spawnLock = null;
});
return spawnLock;
Expand All @@ -86,7 +95,10 @@ export class BusClient {
* Inner spawn implementation — wraps the child process in a Promise.
* Kept separate from startBus() so the spawn lock can be released cleanly.
*/
private static spawnBus(timeoutMs: number): Promise<number> {
private static spawnBus(
timeoutMs: number,
warn: (message: string, ...args: unknown[]) => void,
): Promise<number> {
const binary = BusClient.findBusBinary();
return new Promise((resolve, reject) => {
const child = spawn(binary, [], {
Expand Down Expand Up @@ -118,7 +130,7 @@ export class BusClient {

child.stderr?.on("data", (data: Buffer) => {
// Log stderr but don't fail — Go may print warnings
console.warn("[BusClient] bus stderr:", data.toString().trim());
warn("[BusClient] bus stderr:", data.toString().trim());
});

child.on("error", (err) => {
Expand Down
Loading