Two root causes for seeding not working on production: 1. Origin mismatch: The seeder writes content IDs as http://localhost:7777/content/... but the app was using window.location.origin (the user's actual browser URL) to query the relay. Introduced VITE_CONTENT_ORIGIN env var baked into the Docker build so both sides use the same origin. 2. Dockerfile.seed fragility: Replaced --omit=dev + global tsx with a cleaner approach that strips sharp from package.json (the only native dep that fails on Alpine) then does a full npm install, ensuring tsx/esbuild and all applesauce deps resolve correctly. Also improved wait-for-relay to accept any HTTP response (some relays return 4xx for plain GET) and increased max attempts. Co-authored-by: Cursor <cursoragent@cursor.com>
46 lines
1.3 KiB
JavaScript
46 lines
1.3 KiB
JavaScript
/**
|
|
* 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 = 45
|
|
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
|
|
// Accept ANY HTTP response as "alive" — some relays return 4xx
|
|
// for plain GET because they expect a WebSocket upgrade
|
|
resolve(true)
|
|
})
|
|
req.on('error', (err) => {
|
|
if (i === 1) console.log(` (${err.code || err.message})`)
|
|
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)
|