Skip to content

Commit

Permalink
Add new logger to discovery + dnssd
Browse files Browse the repository at this point in the history
  • Loading branch information
gmaclennan committed Nov 10, 2023
1 parent 8c6a081 commit 64bd405
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
12 changes: 7 additions & 5 deletions src/discovery/dns-sd.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { TypedEmitter } from 'tiny-typed-emitter'
import { Bonjour } from 'bonjour-service'
// @ts-ignore
import debug from 'debug'
import pTimeout from 'p-timeout'
import { randomBytes } from 'node:crypto'
import { once } from 'node:events'
import { Logger } from '../logger.js'

const SERVICE_NAME = 'mapeo'

Expand Down Expand Up @@ -48,7 +47,7 @@ export class DnsSd extends TypedEmitter {
}
/* c8 ignore stop */
const { name, port } = service
this.#log(`service up`, [name, address, port])
this.#log('serviceUp', name.slice(0, 7), address, port)
this.emit('up', { port, name, address })
}
/** @param {import('bonjour-service').Service} service */
Expand All @@ -74,18 +73,21 @@ export class DnsSd extends TypedEmitter {
/** @type {Promise<any> | null} */
#advertisingStopping = null
#log
#l

/**
*
* @param {object} [opts]
* @param {string} [opts.name]
* @param {boolean} [opts.disableIpv6]
* @param {Logger} [opts.logger]
*/
constructor({ name, disableIpv6 = true } = {}) {
constructor({ name, disableIpv6 = true, logger } = {}) {
super()
this.#l = Logger.create('dnssd', logger)
this.#name = name || randomBytes(8).toString('hex')
this.#disableIpv6 = disableIpv6
this.#log = debug('mapeo:dnssd:' + this.#name)
this.#log = this.#l.log.bind(this.#l)
}

get name() {
Expand Down
24 changes: 10 additions & 14 deletions src/discovery/local-discovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import net from 'node:net'
import NoiseSecretStream from '@hyperswarm/secret-stream'
import { once } from 'node:events'
import { DnsSd } from './dns-sd.js'
import debug from 'debug'
import { isPrivate } from 'bogon'
import StartStopStateMachine from 'start-stop-state-machine'
import pTimeout from 'p-timeout'
import { keyToPublicId } from '@mapeo/crypto'
import { Logger } from '../logger.js'

/** @typedef {{ publicKey: Buffer, secretKey: Buffer }} Keypair */
/** @typedef {import('../utils.js').OpenedNoiseStream<net.Socket>} OpenedNoiseStream */
Expand All @@ -32,21 +32,25 @@ export class LocalDiscovery extends TypedEmitter {
#log
/** @type {(e: Error) => void} */
#handleSocketError
#l

/**
* @param {Object} opts
* @param {Keypair} opts.identityKeypair
* @param {DnsSd} [opts.dnssd] Optional DnsSd instance, used for testing
* @param {Logger} [opts.logger]
*/
constructor({ identityKeypair, dnssd }) {
constructor({ identityKeypair, dnssd, logger }) {
super()
this.#l = Logger.create('mdns', logger)
this.#log = this.#l.log.bind(this.#l)
this.#dnssd =
dnssd ||
new DnsSd({
name: keyToPublicId(identityKeypair.publicKey),
logger: this.#l,
})
this.#dnssd.on('up', this.#handleServiceUp.bind(this))
this.#log = debug('mapeo:mdns:' + keyShortname(identityKeypair.publicKey))
this.#sm = new StartStopStateMachine({
start: this.#start.bind(this),
stop: this.#stop.bind(this),
Expand Down Expand Up @@ -195,7 +199,8 @@ export class LocalDiscovery extends TypedEmitter {
this.#log(
`${isInitiator ? 'outgoing' : 'incoming'} secretSteam connection ${
isInitiator ? 'to' : 'from'
} ${keyShortname(remotePublicKey)}`
} %h`,
remotePublicKey
)

const existing = this.#noiseConnections.get(remoteId)
Expand Down Expand Up @@ -231,7 +236,7 @@ export class LocalDiscovery extends TypedEmitter {
this.#noiseConnections.set(remoteId, conn)

conn.on('close', () => {
this.#log(`closed connection with ${keyShortname(remotePublicKey)}`)
this.#log('closed connection with %h', remotePublicKey)
this.#noiseConnections.delete(remoteId)
})

Expand Down Expand Up @@ -295,13 +300,4 @@ function getAddress(server) {
return addr
}

/**
*
* @param {Buffer} key
* @returns
*/
function keyShortname(key) {
return keyToPublicId(key).slice(0, 7)
}

function noop() {}
2 changes: 1 addition & 1 deletion src/local-peers.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ export class LocalPeers extends TypedEmitter {
{ protocol: 'hypercore/alpha' },
/** @param {Buffer} discoveryKey */ async (discoveryKey) => {
this.#l.log(
'Received dk %h from %h',
'Received discovery key %h from %h',
discoveryKey,
stream.noiseStream.remotePublicKey
)
Expand Down
3 changes: 2 additions & 1 deletion src/mapeo-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class MapeoManager extends TypedEmitter {
super()
this.#keyManager = new KeyManager(rootKey)
this.#deviceId = getDeviceId(this.#keyManager)
this.#l = new Logger({ deviceId: this.#deviceId })
this.#l = new Logger({ deviceId: this.#deviceId, ns: 'manager' })
this.#dbFolder = dbFolder
const sqlite = new Database(
dbFolder === ':memory:'
Expand Down Expand Up @@ -142,6 +142,7 @@ export class MapeoManager extends TypedEmitter {

this.#localDiscovery = new LocalDiscovery({
identityKeypair: this.#keyManager.getIdentityKeypair(),
logger: this.#l,
})
this.#localDiscovery.on('connection', this[kManagerReplicate].bind(this))
}
Expand Down

0 comments on commit 64bd405

Please sign in to comment.