diff --git a/src/lock-ops.mjs b/src/lock-ops.mjs index e3fbcad..d0ac6b1 100644 --- a/src/lock-ops.mjs +++ b/src/lock-ops.mjs @@ -8,7 +8,7 @@ import { KIND_APP_DATA, MS_PER_SECOND, } from './constants.mjs'; -import { nowUnix, relayListLabel } from './utils.mjs'; +import { nowUnix } from './utils.mjs'; import { defaultHealthManager, buildRelayHealthConfig, RelayHealthManager } from './relay-health-manager.mjs'; import { publishLock, LockPublisher } from './lock-publisher.mjs'; import { @@ -61,20 +61,6 @@ function filterActiveLocks(locks) { return locks.filter((lock) => !lock.expiresAt || lock.expiresAt > now); } -function withTimeout(promise, timeoutMs, timeoutMessage) { - let timeoutHandle; - const timeoutPromise = new Promise((_, reject) => { - timeoutHandle = setTimeout(() => reject(new Error(timeoutMessage)), timeoutMs); - }); - return Promise.race([promise, timeoutPromise]).finally(() => { - if (timeoutHandle) clearTimeout(timeoutHandle); - }); -} - -function mergeRelayList(primaryRelays, fallbackRelays) { - return [...new Set([...primaryRelays, ...fallbackRelays])]; -} - /** * Queries relays for active lock events matching the criteria. * Uses a tiered approach: tries primary relays first, then falls back to others if needed. diff --git a/src/lock-publisher.mjs b/src/lock-publisher.mjs index 61fba13..cdc8ecd 100644 --- a/src/lock-publisher.mjs +++ b/src/lock-publisher.mjs @@ -1,4 +1,4 @@ -import { randomUUID } from 'node:crypto'; +import { randomUUID, randomInt } from 'node:crypto'; import { SimplePool } from 'nostr-tools/pool'; import { getPublishTimeoutMs, @@ -6,7 +6,7 @@ import { getRelayFallbacks, getMinActiveRelayPool, } from './torch-config.mjs'; -import { mergeRelayList } from './utils.mjs'; +import { mergeRelayList } from './lock-utils.mjs'; import { defaultHealthManager, buildRelayHealthConfig } from './relay-health-manager.mjs'; import { relayListLabel } from './utils.mjs'; @@ -20,10 +20,6 @@ function withTimeout(promise, timeoutMs, timeoutMessage) { }); } -function mergeRelayList(primaryRelays, fallbackRelays) { - return [...new Set([...primaryRelays, ...fallbackRelays])]; -} - const PUBLISH_ERROR_CODES = { TIMEOUT: 'publish_timeout', DNS: 'dns_resolution', diff --git a/src/lock-utils.mjs b/src/lock-utils.mjs index dbe1820..c287fa5 100644 --- a/src/lock-utils.mjs +++ b/src/lock-utils.mjs @@ -14,6 +14,24 @@ export { relayListLabel } from './utils.mjs'; * @param {Object} deps - Dependency injection for testing * @returns {Promise>} - Set of agent names that have completed their task */ +/** + * Promisified timeout wrapper. + * + * @param {Promise} promise - The promise to wrap. + * @param {number} timeoutMs - Timeout in milliseconds. + * @param {string} timeoutMessage - Error message on timeout. + * @returns {Promise} - The result of the promise or a timeout error. + */ +export function withTimeout(promise, timeoutMs, timeoutMessage) { + let timeoutHandle; + const timeoutPromise = new Promise((_, reject) => { + timeoutHandle = setTimeout(() => reject(new Error(timeoutMessage)), timeoutMs); + }); + return Promise.race([promise, timeoutPromise]).finally(() => { + if (timeoutHandle) clearTimeout(timeoutHandle); + }); +} + export async function getCompletedAgents(cadence, logDir, deps) { const { readdir = fs.readdir, getDateStr = todayDateStr, getIsoWeek = getIsoWeekStr } = deps; const completed = new Set(); @@ -51,8 +69,6 @@ export async function getCompletedAgents(cadence, logDir, deps) { return completed; } -export { withTimeout }; - export function mergeRelayList(primaryRelays, fallbackRelays) { return [...new Set([...primaryRelays, ...fallbackRelays])]; } diff --git a/test/utils-async.test.mjs b/test/utils-async.test.mjs index 66b602e..c5d8749 100644 --- a/test/utils-async.test.mjs +++ b/test/utils-async.test.mjs @@ -1,6 +1,6 @@ import { describe, it } from 'node:test'; import assert from 'node:assert'; -import { withTimeout } from '../src/utils.mjs'; +import { withTimeout } from '../src/lock-utils.mjs'; describe('Async Utilities', () => { describe('withTimeout', () => { diff --git a/test/utils.test.mjs b/test/utils.test.mjs index 7051687..da7213a 100644 --- a/test/utils.test.mjs +++ b/test/utils.test.mjs @@ -3,6 +3,7 @@ import assert from 'node:assert'; import fs from 'node:fs'; import path from 'node:path'; import { getIsoWeekStr, todayDateStr, nowUnix, ensureDir, detectPlatform, relayListLabel } from '../src/utils.mjs'; +import { mergeRelayList } from '../src/lock-utils.mjs'; describe('Date Utilities', () => { describe('todayDateStr', () => {