From 1051708697299a51bfb9dac77bdd14f644ac0fe2 Mon Sep 17 00:00:00 2001 From: Chad Nehemiah Date: Wed, 31 May 2023 11:59:21 -0500 Subject: [PATCH] feat: added multiaddr formatter to logging (#34) --- package.json | 13 ++++++---- src/index.ts | 6 +++++ test/index.spec.ts | 65 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 3fe6466356..2f3d771eef 100644 --- a/package.json +++ b/package.json @@ -139,13 +139,16 @@ "docs": "aegir docs" }, "dependencies": { - "@libp2p/interface-peer-id": "^2.0.0", - "debug": "^4.3.3", - "interface-datastore": "^8.0.0", - "multiformats": "^11.0.0" + "@libp2p/interface-peer-id": "^2.0.2", + "@multiformats/multiaddr": "^12.1.3", + "debug": "^4.3.4", + "interface-datastore": "^8.2.0", + "multiformats": "^11.0.2" }, "devDependencies": { + "@libp2p/peer-id": "^2.0.3", "@types/debug": "^4.1.7", - "aegir": "^38.1.7" + "aegir": "^38.1.7", + "uint8arrays": "^4.0.3" } } diff --git a/src/index.ts b/src/index.ts index 4a5e3f1cbc..9eca432f91 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,6 +4,7 @@ import { base32 } from 'multiformats/bases/base32' import { base64 } from 'multiformats/bases/base64' import type { PeerId } from '@libp2p/interface-peer-id' import type { CID } from 'multiformats/cid' +import type { Multiaddr } from '@multiformats/multiaddr' import type { Key } from 'interface-datastore' // Add a formatter for converting to a base58 string @@ -36,6 +37,11 @@ debug.formatters.k = (v: Key): string => { return v == null ? 'undefined' : v.toString() } +// Add a formatter for stringifying Multiaddrs +debug.formatters.ma = (v?: Multiaddr): string => { + return v == null ? 'undefined' : v.toString() +} + export interface Logger { (formatter: any, ...args: any[]): void error: (formatter: any, ...args: any[]) => void diff --git a/test/index.spec.ts b/test/index.spec.ts index 4c753bc10d..abd82be71f 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -1,6 +1,14 @@ import { expect } from 'aegir/chai' import { logger } from '../src/index.js' import debug from 'debug' +import { multiaddr } from '@multiformats/multiaddr' +import { peerIdFromString } from '@libp2p/peer-id' +import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' +import { toString as unint8ArrayToString } from 'uint8arrays/to-string' +import { base58btc } from 'multiformats/bases/base58' +import { base32 } from 'multiformats/bases/base32' +import { base64 } from 'multiformats/bases/base64' +import { Key } from 'interface-datastore' describe('logger', () => { it('creates a logger', () => { @@ -52,4 +60,61 @@ describe('logger', () => { expect(log).to.have.property('trace').that.is.a('function') expect(log).to.have.nested.property('trace.enabled').that.is.true() }) + + it('has all formatters', () => { + debug.enable('enabled-with-formatters') + + expect(debug.formatters).to.have.property('b').that.is.a('function') + expect(debug.formatters).to.have.property('t').that.is.a('function') + expect(debug.formatters).to.have.property('m').that.is.a('function') + expect(debug.formatters).to.have.property('p').that.is.a('function') + expect(debug.formatters).to.have.property('c').that.is.a('function') + expect(debug.formatters).to.have.property('k').that.is.a('function') + expect(debug.formatters).to.have.property('ma').that.is.a('function') + }) + + it('test ma formatter', () => { + const ma = multiaddr('/ip4/127.0.0.1/tcp/4001') + + expect(debug.formatters.ma(ma)).to.equal(ma.toString()) + }) + + it('test peerId formatter', () => { + const peerId = peerIdFromString('QmZ8eiDPqQqWR17EPxiwCDgrKPVhCHLcyn6xSCNpFAdAZb') + + expect(debug.formatters.p(peerId)).to.equal(peerId.toString()) + }) + + it('test cid formatter', () => { + const peerId = peerIdFromString('QmZ8eiDPqQqWR17EPxiwCDgrKPVhCHLcyn6xSCNpFAdAZb') + const cid = peerId.toCID() + + expect(debug.formatters.c(cid)).to.equal(cid.toString()) + }) + + it('test base58 formatter', () => { + const buf = uint8ArrayFromString('12D3KooWbtp1AcgweFSArD7dbKWYpAr8MZR1tofwNwLFLjeNGLWa', 'base58btc') + + expect(debug.formatters.b(buf)).to.equal(base58btc.baseEncode(buf)) + }) + + it('test base32 formatter', () => { + const buf = uint8ArrayFromString('jbswy3dpfqqho33snrscc===', 'base32') + + expect(debug.formatters.t(buf)).to.equal(base32.baseEncode(buf)) + }) + + it('test base64 formatter', () => { + const buf = uint8ArrayFromString('12D3KooWbtp1AcgweFSArD7dbKWYpAr8MZR1tofwNwLFLjeNGLWa', 'base64') + + expect(debug.formatters.m(buf)).to.equal(base64.baseEncode(buf)) + }) + + it('test datastore key formatter', () => { + const buf = uint8ArrayFromString('jbswy3dpfqqho33snrscc===', 'base32') + + const key = new Key('/' + unint8ArrayToString(buf, 'base32'), false) + + expect(debug.formatters.k(key)).to.equal(key.toString()) + }) })