From 08d291e7fa349b52078dc1e157f947d319e2853a Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Fri, 20 Feb 2026 21:53:59 +0000 Subject: [PATCH] refactor(lock): centralize lock utility functions - Moved `withTimeout` to `src/utils.mjs` and exported it. - Updated `src/lock-utils.mjs` to re-export `withTimeout` and `mergeRelayList` from `src/utils.mjs`. - Removed duplicate local definitions of `withTimeout` and `mergeRelayList` in `src/lock-ops.mjs` and `src/lock-publisher.mjs`. - Fixed `test/utils.test.mjs` to import `mergeRelayList`. - Verified all tests pass with `npm test`. Co-authored-by: PR0M3TH3AN <53631862+PR0M3TH3AN@users.noreply.github.com> --- src/lock-ops.mjs | 15 +-------------- src/lock-publisher.mjs | 17 +---------------- src/lock-utils.mjs | 7 +------ src/utils.mjs | 10 ++++++++++ test/utils.test.mjs | 2 +- 5 files changed, 14 insertions(+), 37 deletions(-) diff --git a/src/lock-ops.mjs b/src/lock-ops.mjs index e3fbcad..3010f5a 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,19 +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. diff --git a/src/lock-publisher.mjs b/src/lock-publisher.mjs index 61fba13..6120ed6 100644 --- a/src/lock-publisher.mjs +++ b/src/lock-publisher.mjs @@ -6,23 +6,8 @@ import { getRelayFallbacks, getMinActiveRelayPool, } from './torch-config.mjs'; -import { mergeRelayList } from './utils.mjs'; +import { mergeRelayList, relayListLabel, withTimeout } from './utils.mjs'; import { defaultHealthManager, buildRelayHealthConfig } from './relay-health-manager.mjs'; -import { relayListLabel } from './utils.mjs'; - -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])]; -} const PUBLISH_ERROR_CODES = { TIMEOUT: 'publish_timeout', diff --git a/src/lock-utils.mjs b/src/lock-utils.mjs index dbe1820..b44a11a 100644 --- a/src/lock-utils.mjs +++ b/src/lock-utils.mjs @@ -3,7 +3,7 @@ import path from 'node:path'; import { randomInt } from 'node:crypto'; import { todayDateStr, getIsoWeekStr } from './utils.mjs'; -export { relayListLabel } from './utils.mjs'; +export { relayListLabel, withTimeout, mergeRelayList } from './utils.mjs'; /** * Scans the local log directory to identify agents that have already completed their task @@ -51,11 +51,6 @@ export async function getCompletedAgents(cadence, logDir, deps) { return completed; } -export { withTimeout }; - -export function mergeRelayList(primaryRelays, fallbackRelays) { - return [...new Set([...primaryRelays, ...fallbackRelays])]; -} const MAX_RANDOM = 281474976710655; // 2**48 - 1 diff --git a/src/utils.mjs b/src/utils.mjs index 4397000..82445aa 100644 --- a/src/utils.mjs +++ b/src/utils.mjs @@ -55,3 +55,13 @@ export function ensureDir(dir) { export function relayListLabel(relays) { return relays.join(', '); } + +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); + }); +} diff --git a/test/utils.test.mjs b/test/utils.test.mjs index 7051687..40db583 100644 --- a/test/utils.test.mjs +++ b/test/utils.test.mjs @@ -2,7 +2,7 @@ import { describe, it, after } from 'node:test'; 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 { getIsoWeekStr, todayDateStr, nowUnix, ensureDir, detectPlatform, relayListLabel, mergeRelayList } from '../src/utils.mjs'; describe('Date Utilities', () => { describe('todayDateStr', () => {