OverviewFilesCommitsIssuesDocsReleasesPeers

hyperswarm-peer / integration / layer10_daemon.js

53 lines · javascript ref:
// Layer 10 — Daemon lifecycle helper
// Starts keet-daemon with isolated storage for testing.
// Writes ready signal to /tmp/layer10_daemon_ready.txt.
// Cleans up on SIGTERM.
// Runs under bare runtime (not Node.js).

import { startDaemon } from './index.js'
import fs from 'fs'
import path from 'path'
import os from 'os'

const SOCKET_PATH = os.getEnv('SOCKET_PATH') || '/tmp/keet-daemon.sock'
const STORAGE_DIR = os.getEnv('STORAGE_DIR') || '/tmp/layer10_storage'
const READY_FILE = '/tmp/layer10_daemon_ready.txt'
const DEBUG = os.getEnv('DEBUG')

// Ensure storage directory exists
if (!fs.existsSync(STORAGE_DIR)) {
  fs.mkdirSync(STORAGE_DIR, { recursive: true })
}

console.log('[layer10-daemon] starting with storage:', STORAGE_DIR)
console.log('[layer10-daemon] socket:', SOCKET_PATH)

let daemon
try {
  daemon = await startDaemon({
    socketPath: SOCKET_PATH,
    storage: STORAGE_DIR,
    debug: !!DEBUG
  })
  console.log('[layer10-daemon] ready')
  fs.writeFileSync(READY_FILE, 'ready')
} catch (err) {
  console.error('[layer10-daemon] start failed:', err.message)
  Bare.exit(1)
}

const shutdown = async () => {
  console.log('[layer10-daemon] shutting down...')
  try { fs.unlinkSync(READY_FILE) } catch {}
  try {
    if (daemon && daemon.uds) await daemon.uds.close()
    if (daemon && daemon.core) await daemon.core.close()
  } catch (err) {
    console.error('[layer10-daemon] cleanup error:', err.message)
  }
  Bare.exit(0)
}

Bare.on('SIGTERM', shutdown)
Bare.on('SIGINT', shutdown)