OverviewFilesCommitsIssuesDocsReleasesPeers

hyper-vdrive / example / seed.mjs

66 lines · javascript ref:
// T5 seed — serve the VIRTUAL drive on the DHT. Every byte a peer receives is
// materialised on demand from the source files; the store holds no blob bytes.
import { createRequire } from 'module'
const require = createRequire(import.meta.url)

const fs = require('fs')
const path = require('path')
const process = require('process')
const Corestore = require('corestore')
const Hyperdrive = require('hyperdrive')
const Hyperswarm = require('hyperswarm')
const { ContentStore, build, stats } = require('../lib/storage.cjs')
const { chunk } = await import('../../hyper-overlay/lib/chunker.js')

const ROOT = process.argv[2]
const dir = ROOT + '/virtual'
const srcDir = ROOT + '/src'

// Rebuild the content index from the source files, exactly as the overlay would.
const content = new ContentStore()
for (const name of fs.readdirSync(srcDir)) {
  const file = path.join(srcDir, name)
  const buf = fs.readFileSync(file)
  for (const c of chunk(buf)) content.add(c.hash, file, c.offset, c.length)
}

const Storage = build(content)
const store = new Corestore(new Storage(dir), { primaryKey: Buffer.alloc(32, 7), unsafe: true })
const drive = new Hyperdrive(store)
await drive.ready()
await drive.getBlobs()

const swarm = new Hyperswarm()
swarm.on('connection', (conn) => store.replicate(conn))
const discovery = swarm.join(drive.discoveryKey, { server: true, client: false })
await discovery.flushed()

console.log(
  JSON.stringify({ seeding: drive.key.toString('hex'), storeBytes: dirSize(dir) })
)

function dirSize (p) {
  let total = 0
  for (const e of fs.readdirSync(p, { withFileTypes: true })) {
    const full = path.join(p, e.name)
    total += e.isDirectory() ? dirSize(full) : fs.statSync(full).size
  }
  return total
}

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

async function shutdown () {
  console.log(JSON.stringify({ served: stats }))
  await swarm.destroy()
  await drive.close()
  await store.close()
  content.close()
  process.exit(0)
}

setInterval(() => {
  console.log(JSON.stringify({ peers: swarm.connections.size, bytesServed: stats.bytesServed }))
}, 10000)