Skip to content

Commit

Permalink
feat: debugging logs
Browse files Browse the repository at this point in the history
  • Loading branch information
deblanco committed Apr 19, 2023
1 parent 870a142 commit 00ecc68
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 3 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ ev.emit('test', 'test-1')

For further information, check it's [EventEmitter interface](src/IEventEmitter.ts)

For printing the debugging logs, the `DEBUG=p2p:*` env flag is required.

```sh
$ DEBUG=p2p:* node index.js
$ DEBUG=p2p:* yarn test # to see an example of the outputs
```

## License

This library is licensed under the MIT License. See the [LICENSE](LICENSE) file for more information.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@commitlint/config-conventional": "^17.4.4",
"@esbuild-plugins/node-globals-polyfill": "^0.2.3",
"@esbuild-plugins/node-modules-polyfill": "^0.2.2",
"@types/debug": "^4.1.7",
"@types/events": "^3.0.0",
"@types/jest": "^29.5.0",
"@types/node": "^18.15.11",
Expand All @@ -38,10 +39,11 @@
"@libp2p/mplex": "^7.1.3",
"@libp2p/pubsub-peer-discovery": "^8.0.2",
"@libp2p/tcp": "^6.2.2",
"debug": "^4.3.4",
"libp2p": "^0.44.0",
"uint8arrays": "^4.0.3"
},
"publishConfig": {
"registry": "https://npm.pkg.github.com"
}
}
}
10 changes: 8 additions & 2 deletions src/EventEmitterP2P.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import LibP2P, { Libp2p } from 'libp2p'
import { Disposable, IEventEmitter, Listener } from './IEventEmitter'
import { getNode } from './node'
import { fromString as uint8ArrayFromString } from 'uint8arrays'
import { error, info } from './logger'

type P2POptions = { overridedOptions: LibP2P.Libp2pOptions; bootstrapList?: string[] }

Expand All @@ -20,6 +21,7 @@ export class EventEmitterP2P<T extends string> implements IEventEmitter<T> {
async initialize(p2pOpt?: P2POptions) {
this.p2pnode = await getNode(p2pOpt?.overridedOptions, p2pOpt?.bootstrapList).then((node) => {
node.pubsub.addEventListener('message', this._onMessage.bind(this))
info('P2P node initialized')
return node
})
}
Expand Down Expand Up @@ -51,10 +53,12 @@ export class EventEmitterP2P<T extends string> implements IEventEmitter<T> {

emit = async (topic: T, event: any) => {
if (!this.p2pnode) {
error('P2P node not initialized')
throw new Error('P2P node not initialized')
}
const data = uint8ArrayFromString(JSON.stringify(event))
this.p2pnode.pubsub.publish(topic, data)
info('Publishing message on topic %s: %s', topic, JSON.stringify(event))
return this.p2pnode.pubsub.publish(topic, data)
}

pipe = (topic: T, te: IEventEmitter<T>): Disposable => {
Expand All @@ -63,6 +67,7 @@ export class EventEmitterP2P<T extends string> implements IEventEmitter<T> {

_subscribe = (topic: string) => {
if (!this.p2pnode) {
error('P2P node not initialized')
throw new Error('P2P node not initialized')
}
const currentTopics = this.p2pnode.pubsub.getTopics()
Expand All @@ -75,6 +80,7 @@ export class EventEmitterP2P<T extends string> implements IEventEmitter<T> {
try {
const topic = msg.detail.topic as T
const data = msg.detail.data.toString()
info('Received message on topic %s: %s', topic, data)
if (this.listeners[topic]) {
;(this.listeners[topic] ?? []).forEach((listener) => listener(topic, data))
}
Expand All @@ -85,7 +91,7 @@ export class EventEmitterP2P<T extends string> implements IEventEmitter<T> {
toCall.forEach((listener) => listener(topic, data))
}
} catch (e) {
console.error('🚀 ~ file: EventEmitterP2P.ts:87 ~ EventEmitterP2P<T ~ e:', e)
error('%O', e)
}
}
}
14 changes: 14 additions & 0 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import debug from 'debug'

const _log = debug('p2p')

export const error = _log.extend('error')

export const warn = _log.extend('warn')
warn.log = console.warn.bind(console)

export const info = _log.extend('info')
info.log = console.info.bind(console)

export const log = _log.extend('log')
log.log = console.log.bind(console)
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1897,6 +1897,13 @@
dependencies:
"@babel/types" "^7.3.0"

"@types/debug@^4.1.7":
version "4.1.7"
resolved "https://registry.yarnpkg.com/@types/debug/-/debug-4.1.7.tgz#7cc0ea761509124709b8b2d1090d8f6c17aadb82"
integrity sha512-9AonUzyTjXXhEOa0DnqpzZi6VHlqKMswga9EXjpXnnqxwLtdvPPtlO8evrI5D9S6asFRCQ6v+wpiUKbw+vKqyg==
dependencies:
"@types/ms" "*"

"@types/estree@^1.0.0":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-1.0.0.tgz#5fb2e536c1ae9bf35366eed879e827fa59ca41c2"
Expand Down Expand Up @@ -1951,6 +1958,11 @@
resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.2.tgz#ee771e2ba4b3dc5b372935d549fd9617bf345b8c"
integrity sha512-jhuKLIRrhvCPLqwPcx6INqmKeiA5EWrsCOPhrlFSrbrmU4ZMPjj5Ul/oLCMDO98XRUIwVm78xICz4EPCektzeQ==

"@types/ms@*":
version "0.7.31"
resolved "https://registry.yarnpkg.com/@types/ms/-/ms-0.7.31.tgz#31b7ca6407128a3d2bbc27fe2d21b345397f6197"
integrity sha512-iiUgKzV9AuaEkZqkOLDIvlQiL6ltuZd9tGcW3gwpnX8JbuiuhFlEGmmFXEXkN50Cvq7Os88IY2v0dkDqXYWVgA==

"@types/node@*", "@types/node@>=13.7.0", "@types/node@^18.15.11":
version "18.15.11"
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.15.11.tgz#b3b790f09cb1696cffcec605de025b088fa4225f"
Expand Down

0 comments on commit 00ecc68

Please sign in to comment.