Skip to content
Open
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
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ cocod history --watch
cocod logs
cocod logs --follow
cocod logs --path

# Debug a stuck init/unlock in another terminal
cocod logs --follow
```

## NPC (Lightning Address)
Expand Down Expand Up @@ -98,15 +101,16 @@ cocod x-cashu handle "<encoded-x-cashu-request>"

Defaults:

- Socket: `~/.cocod/cocod.sock` (or `COCOD_SOCKET`)
- PID file: `~/.cocod/cocod.pid` (or `COCOD_PID`)
- Daemon log: `~/.cocod/daemon.log` (or `COCOD_LOG_FILE`)
- Config: `~/.cocod/config.json`
- Database: `~/.cocod/coco.db`
- Base directory: `~/.cocod` (or `COCOD_DIR`)
- Socket: `<base>/cocod.sock` (or `COCOD_SOCKET`)
- PID file: `<base>/cocod.pid` (or `COCOD_PID`)
- Daemon log: `<base>/daemon.log` (or `COCOD_LOG_FILE`)
- Config: `<base>/config.json`
- Database: `<base>/coco.db`

Logging defaults:

- Structured JSON logs are written to `~/.cocod/daemon.log`
- Structured JSON logs are written to `<base>/daemon.log`
- Rotation keeps 5 files at 5 MiB each by default
- Override with `COCOD_LOG_LEVEL`, `COCOD_LOG_MAX_BYTES`, and `COCOD_LOG_MAX_FILES`

Expand Down
40 changes: 23 additions & 17 deletions bun.lock
100644 → 100755

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ All commands are available under `cocod`.

The CLI talks to the daemon over HTTP on a UNIX socket.

- Socket path env var: `COCOD_SOCKET`
- Default socket: `~/.cocod/cocod.sock`
- Base directory env var: `COCOD_DIR` (default `~/.cocod`)
- Socket path env var: `COCOD_SOCKET` (default `<COCOD_DIR>/cocod.sock`)

### Response shape

Expand Down
4 changes: 3 additions & 1 deletion docs/daemon-api.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
"version": "0.0.12",
"transport": {
"protocol": "http",
"baseDirEnv": "COCOD_DIR",
"defaultBaseDir": "~/.cocod",
"unixSocketEnv": "COCOD_SOCKET",
"defaultUnixSocket": "~/.cocod/cocod.sock"
"defaultUnixSocket": "<COCOD_DIR>/cocod.sock"
},
"responseContract": {
"success": {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cocod",
"version": "0.0.16",
"name": "@routstr/cocod",
"version": "0.0.20",
"module": "src/index.ts",
"type": "module",
"private": false,
Expand Down
86 changes: 80 additions & 6 deletions src/cli-shared.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { program } from "commander";

const CONFIG_DIR = `${process.env.HOME || process.env.USERPROFILE}/.cocod`;
const SOCKET_PATH = process.env.COCOD_SOCKET || `${CONFIG_DIR}/cocod.sock`;
import { LOG_FILE, SOCKET_PATH } from "./utils/config";

export interface CommandResponse {
output?: unknown;
Expand Down Expand Up @@ -42,6 +41,64 @@ export async function isDaemonRunning(): Promise<boolean> {
}
}

const DAEMON_POLL_INTERVAL_MS = 1_000;
const DAEMON_SLOW_START_WARNING_MS = 30_000;
const DAEMON_START_TIMEOUT_MS = 60_000;
const DAEMON_START_LOG_LINES = 40;

function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}

async function waitForDaemonReady(startedAt: number, warningShown: { value: boolean }): Promise<void> {
for (;;) {
try {
const result = await callDaemon("/status");
if (typeof result.output === "string") {
return;
}
} catch {
// Daemon may not be accepting requests yet
}

const elapsedMs = Date.now() - startedAt;

if (!warningShown.value && elapsedMs >= DAEMON_SLOW_START_WARNING_MS) {
warningShown.value = true;
console.log("Daemon is taking longer than expected, please wait...");
}

if (elapsedMs >= DAEMON_START_TIMEOUT_MS) {
throw new Error("Daemon failed to start after 1 minute");
}

await sleep(DAEMON_POLL_INTERVAL_MS);
}
}

function printProgressStep(message: string): void {
console.log(`• ${message}`);
}

function maybePrintFriendlyProgress(path: string, body?: object): void {
if (path === "/init") {
const mintUrl =
body && "mintUrl" in body && typeof body.mintUrl === "string"
? body.mintUrl
: "https://mint.minibits.cash/Bitcoin";

printProgressStep("Preparing wallet...");
printProgressStep(`Connecting to mint: ${mintUrl}`);
printProgressStep("This can take a few seconds on first run.");
return;
}

if (path === "/unlock") {
printProgressStep("Unlocking wallet...");
printProgressStep("Reconnecting wallet services...");
}
}

export async function startDaemonProcess(): Promise<void> {
const proc = Bun.spawn({
cmd: ["bun", "run", `${import.meta.dir}/index.ts`, "daemon"],
Expand All @@ -51,14 +108,30 @@ export async function startDaemonProcess(): Promise<void> {
});
proc.unref();

for (let i = 0; i < 50; i++) {
await new Promise((resolve) => setTimeout(resolve, 100));
const startedAt = Date.now();
const warningShown = { value: false };

for (;;) {
await sleep(DAEMON_POLL_INTERVAL_MS);
if (await isDaemonRunning()) {
await waitForDaemonReady(startedAt, warningShown);
return;
}
}

throw new Error("Daemon failed to start within 5 seconds");
const elapsedMs = Date.now() - startedAt;

if (!warningShown.value && elapsedMs >= DAEMON_SLOW_START_WARNING_MS) {
warningShown.value = true;
console.log("Daemon is taking longer than expected, please wait...");
console.log(
`Tip: run 'cocod logs --follow' or 'tail -n ${DAEMON_START_LOG_LINES} ${LOG_FILE}' in another terminal.`,
);
}

if (elapsedMs >= DAEMON_START_TIMEOUT_MS) {
throw new Error("Daemon failed to start after 1 minute");
}
}
}

export async function ensureDaemonRunning(): Promise<void> {
Expand All @@ -76,6 +149,7 @@ export async function handleDaemonCommand(
): Promise<CommandResponse> {
try {
await ensureDaemonRunning();
maybePrintFriendlyProgress(path, options.body);
const result = await callDaemon(path, options);

if (result.error) {
Expand Down
Loading