UNPKG

hyperns-service

Version:
109 lines (86 loc) 3.74 kB
#!/usr/bin/env node const Corestore = require('corestore') const idEnc = require('hypercore-id-encoding') const Hyperswarm = require('hyperswarm') const goodbye = require('graceful-goodbye') const { command, flag } = require('paparam') const instrument = require('hyper-instrument') const pino = require('pino') const HyperNsService = require('.') const cmd = command('hyperns', flag('--storage|-s [path]', 'storage path, defaults to ./hyperns'), flag('--blind-peer|-b [blindPeer]', 'Public key of a blind-peer that can propose new records. Can be more than 1.').multiple(), flag('--scraper-public-key [scraper-public-key]', 'Public key of a dht-prometheus scraper'), flag('--scraper-secret [scraper-secret]', 'Secret of the dht-prometheus scraper'), flag('--scraper-alias [scraper-alias]', '(optional) Alias with which to register to the scraper'), async function ({ flags }) { const storage = flags.storage || 'hyperns' const blindPeers = flags.blindPeer || [] const logger = pino() const store = new Corestore(storage) await store.ready() const swarm = new Hyperswarm( { keyPair: await store.createKeyPair('public-key') } ) swarm.on('connection', (conn, peerInfo) => { store.replicate(conn) const key = idEnc.normalize(peerInfo.publicKey) logger.info(`Opened connection to ${key}`) conn.on('close', () => logger.info(`Closed connection to ${key}`)) }) let dhtPromClient = null if (flags.scraperPublicKey) { logger.info('Setting up instrumentation') const scraperPublicKey = idEnc.decode(flags.scraperPublicKey) const scraperSecret = idEnc.decode(flags.scraperSecret) const prometheusServiceName = 'hyperns-service' let prometheusAlias = flags.scraperAlias if (prometheusAlias && prometheusAlias.length > 99) throw new Error('The Prometheus alias must have length less than 100') if (!prometheusAlias) { prometheusAlias = `hyperns-service-${idEnc.normalize(swarm.keyPair.publicKey)}`.slice(0, 99) } dhtPromClient = instrument({ swarm, corestore: store, scraperPublicKey, prometheusAlias, scraperSecret, prometheusServiceName }) dhtPromClient.registerLogger(logger) } const service = new HyperNsService( store.namespace('hyperns'), swarm, { blindPeers } ) service.on('add-mailbox', req => { logger.info(`Added mailbox for autobase ${idEnc.normalize(req.autobase)}`) }) service.on('blind-peer-connection', peerInfo => { logger.info(`Opened blind peer connection to ${idEnc.normalize(peerInfo.publicKey)}`) }) service.on('new-record', (record) => { logger.info(`New record: ${record.name}->${idEnc.normalize(record.publicKey)} (blind peers: [${record.blindPeers.map(p => idEnc.normalize(p))}])`) }) goodbye(async () => { logger.info('Shutting down HyperNS service') if (dhtPromClient) await dhtPromClient.close() await swarm.destroy() await service.close() }) if (dhtPromClient) await dhtPromClient.ready() logger.info('Starting HyperNS service') await service.ready() swarm.join(service.base.discoveryKey) swarm.join(service.base.view.db.engine.core.discoveryKey) if (blindPeers.length > 0) { logger.info('Blind peers: ') for (const p of blindPeers) { logger.info(` - ${idEnc.normalize(p)}`) } } logger.info(`Autobase key: ${idEnc.normalize(service.base.key)}`) logger.info(`Name-service database key: ${idEnc.normalize(service.dbKey)}`) logger.info(`Database block-encryption key: ${idEnc.normalize(service.viewBlockEncryptionKey)}`) } ) cmd.parse()