OverviewFilesCommitsIssuesDocsReleasesPeers

keet-daemon / test / probe-real-storage.js

76 lines · javascript ref:
// probe-real-storage.js — reproduces the stortron Hypercore.destroyRequests
// TypeError on tabertron by exercising swapCore against the REAL populated
// ~/.config/keet-cli storage (not a fresh /tmp dir).
//
// CAUTION: writes a new identity-N to your real storage. SAFE because:
//   - identity-manager.create just makes a registry entry + empty dir
//   - We DON'T call vault.setup (no mnemonic, no encrypted vault)
//   - We DON'T call identity-manager.switch's full path — we directly invoke
//     daemon.router._swapCore() so any error throws synchronously in this
//     same JS context, giving us the full stack trace inline.
//
// To clean up after: remove the new identity-N entry from
// ~/.config/keet-cli/identities.json + rm -rf ~/.config/keet-cli/identity-N
//
// Run:  bare test/probe-real-storage.js

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

const STORAGE = path.join(os.homedir(), '.config', 'keet-cli')
const SOCKET = '/tmp/keet-daemon-probe.sock'

async function main () {
  console.log('[probe] storage:', STORAGE)
  try { fs.unlinkSync(SOCKET) } catch {}

  console.log('[probe] starting daemon...')
  const daemon = await startDaemon({ storage: STORAGE, socketPath: SOCKET })
  console.log('[probe] daemon ready')

  const router = daemon.router
  const list = await router._imList()
  console.log('[probe] identities before:', list.identities.length, 'active:', list.activeId)

  console.log('[probe] creating probe identity...')
  const entry = await router._imCreate({ name: 'probe-' + Date.now().toString(36) })
  console.log('[probe] created:', entry.id, 'at', entry.dir)

  console.log('[probe] calling daemon.swapCore directly (so errors throw with full stack)...')
  try {
    // _swapCore is the closure from index.js bound onto the router
    await router._swapCore(entry.dir)
    console.log('[probe] swapCore COMPLETED — no destroyRequests error here')
  } catch (err) {
    console.error('[probe] swapCore THREW:')
    console.error('  err.name:    ', err && err.name)
    console.error('  err.message: ', err && err.message)
    console.error('  err.constructor:', err && err.constructor && err.constructor.name)
    if (err && err.stack) {
      console.error('  err.stack:')
      for (const line of err.stack.split('\n')) console.error('    ' + line)
    }
    if (err && err.cause) {
      console.error('  err.cause:   ', err.cause.message || err.cause)
      if (err.cause.stack) {
        console.error('  cause.stack:')
        for (const line of err.cause.stack.split('\n')) console.error('    ' + line)
      }
    }
  }

  console.log('[probe] cleanup: stop daemon')
  try { await daemon.uds.stop() } catch {}
  try { await daemon.router.core.close() } catch {}
  try { await daemon.core.close() } catch {}
  Bare.exit(0)
}

main().catch(err => {
  console.error('[probe] FAILED uncaught:', err && err.message)
  if (err && err.stack) console.error(err.stack)
  Bare.exit(1)
})