/** * Waits for the Nostr relay to be reachable before seeding. * Used by the Docker seeder container. * * Usage: node scripts/wait-for-relay.mjs * Env: RELAY_URL (default ws://localhost:7777) */ import http from 'node:http' const wsUrl = process.env.RELAY_URL || 'ws://localhost:7777' const httpUrl = wsUrl.replace('ws://', 'http://').replace('wss://', 'https://') const maxAttempts = 30 const intervalMs = 2000 console.log(`Waiting for relay at ${httpUrl} ...`) for (let i = 1; i <= maxAttempts; i++) { const ok = await new Promise((resolve) => { const req = http.get(httpUrl, (res) => { res.resume() // drain response resolve(res.statusCode >= 200 && res.statusCode < 400) }) req.on('error', () => resolve(false)) req.setTimeout(3000, () => { req.destroy() resolve(false) }) }) if (ok) { console.log(`Relay is ready! (attempt ${i}/${maxAttempts})`) process.exit(0) } console.log(` attempt ${i}/${maxAttempts} — not ready yet`) await new Promise((r) => setTimeout(r, intervalMs)) } console.error(`Relay did not become ready after ${maxAttempts} attempts`) process.exit(1)