Skip to content

Commit

Permalink
feat: Use libp2p for logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
haydenyoung committed May 9, 2024
1 parent c803708 commit 94a319a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
13 changes: 9 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
import Orbiter from './lib/orbiter.js'
import { Access } from './lib/authorization.js'

const main = async () => {
const argv = yargs(hideBin(process.argv)).option('directory', {
Expand All @@ -10,6 +11,10 @@ const main = async () => {
}).option('verbose', {
alias: 'v',
description: 'Be more verbose. Outputs errors and other connection messages.'
}).option('allow', {
alias: 'a',
type: 'boolean',
description: 'Allow anyone to pin a database. Defaults to false.'
}).parse()

const options = {}
Expand All @@ -18,16 +23,16 @@ const main = async () => {
options.directory = argv.directory
}

if (argv.allow) {
options.defaultAccess = Access.ALLOW
}

if (argv.verbose) {
options.verbose = Array.isArray(argv.verbose) ? argv.verbose.length : 1
}

const orbiter = await Orbiter(options)

if (options.verbose >= 2) {
console.log('Peer Addresses', orbiter.orbitdb.ipfs.libp2p.getMultiaddrs())
}

process.on('SIGINT', async () => {
await orbiter.stop()
process.exit(0)
Expand Down
18 changes: 15 additions & 3 deletions src/lib/orbiter.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,23 @@ import libp2pConfig from './libp2p/config.js'
import Authorization, { Access } from './authorization.js'
import { handleRequest } from './handlers/index.js'
import { voyagerProtocol } from './protocol.js'
import { logger, enable } from '@libp2p/logger'


export default async ({ directory, verbose, defaultAccess } = {}) => {
const log = logger('orbitdb:voyager:orbiter')

directory = directory || join('./', 'orbiter')

defaultAccess = defaultAccess || Access.DENY

verbose = verbose || 0
if (verbose && verbose >= 1 && verbose <= 2) {
enable('orbitdb:voyager:orbiter' + (verbose > 1 ? '*' : ':error'))
}

log('directory:', directory)

log('default access:', defaultAccess === Access.ALLOW ? 'allow all' : 'deny all')

const path = join(directory, '/', 'keystore')

Expand All @@ -24,6 +34,8 @@ export default async ({ directory, verbose, defaultAccess } = {}) => {
const libp2p = await createLibp2p(libp2pConfig)
const ipfs = await createHelia({ libp2p, datastore, blockstore })

log('listening on', libp2p.getMultiaddrs())

const keystore = await KeyStore({ path })
const identities = await Identities({ keystore })
const id = 'voyager'
Expand All @@ -44,10 +56,10 @@ export default async ({ directory, verbose, defaultAccess } = {}) => {

for await (const db of pins.iterator()) {
dbs[db.value] = await orbitdb.open(db.value)
if (verbose === 3) console.log('db opened', db.value)
log('db opened', db.value)
}

if (verbose === 3) console.log(dbs.length, 'dbs loaded')
log(dbs.length, 'dbs loaded')

const stop = async () => {
await orbitdb.ipfs.libp2p.unhandle(voyagerProtocol)
Expand Down

0 comments on commit 94a319a

Please sign in to comment.