@libp2p/devtools-metrics 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.min.js +1 -1
- package/dist/src/index.d.ts +24 -95
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +66 -131
- package/dist/src/index.js.map +1 -1
- package/dist/src/rpc/codecs/cid.d.ts +4 -0
- package/dist/src/rpc/codecs/cid.d.ts.map +1 -0
- package/dist/src/rpc/codecs/cid.js +8 -0
- package/dist/src/rpc/codecs/cid.js.map +1 -0
- package/dist/src/rpc/codecs/custom-progress-event.d.ts +4 -0
- package/dist/src/rpc/codecs/custom-progress-event.d.ts.map +1 -0
- package/dist/src/rpc/codecs/custom-progress-event.js +15 -0
- package/dist/src/rpc/codecs/custom-progress-event.js.map +1 -0
- package/dist/src/rpc/codecs/multiaddr.d.ts +4 -0
- package/dist/src/rpc/codecs/multiaddr.d.ts.map +1 -0
- package/dist/src/rpc/codecs/multiaddr.js +8 -0
- package/dist/src/rpc/codecs/multiaddr.js.map +1 -0
- package/dist/src/rpc/codecs/peer-id.d.ts +4 -0
- package/dist/src/rpc/codecs/peer-id.d.ts.map +1 -0
- package/dist/src/rpc/codecs/peer-id.js +9 -0
- package/dist/src/rpc/codecs/peer-id.js.map +1 -0
- package/dist/src/rpc/index.d.ts +99 -0
- package/dist/src/rpc/index.d.ts.map +1 -0
- package/dist/src/rpc/index.js +11 -0
- package/dist/src/rpc/index.js.map +1 -0
- package/dist/src/rpc/rpc.d.ts +4 -0
- package/dist/src/rpc/rpc.d.ts.map +1 -0
- package/dist/src/rpc/rpc.js +51 -0
- package/dist/src/rpc/rpc.js.map +1 -0
- package/dist/src/utils/debounce.d.ts +2 -0
- package/dist/src/utils/debounce.d.ts.map +1 -0
- package/dist/src/utils/debounce.js +21 -0
- package/dist/src/utils/debounce.js.map +1 -0
- package/dist/src/utils/get-peers.d.ts +9 -0
- package/dist/src/utils/get-peers.d.ts.map +1 -0
- package/dist/src/utils/get-peers.js +42 -0
- package/dist/src/utils/get-peers.js.map +1 -0
- package/dist/src/utils/get-self.d.ts +8 -0
- package/dist/src/utils/get-self.d.ts.map +1 -0
- package/dist/src/utils/get-self.js +13 -0
- package/dist/src/utils/get-self.js.map +1 -0
- package/dist/src/utils/to-object.d.ts +2 -0
- package/dist/src/utils/to-object.d.ts.map +1 -0
- package/dist/src/utils/to-object.js +8 -0
- package/dist/src/utils/to-object.js.map +1 -0
- package/dist/typedoc-urls.json +17 -13
- package/package.json +34 -8
- package/src/index.ts +101 -257
- package/src/rpc/codecs/cid.ts +9 -0
- package/src/rpc/codecs/custom-progress-event.ts +17 -0
- package/src/rpc/codecs/multiaddr.ts +10 -0
- package/src/rpc/codecs/peer-id.ts +11 -0
- package/src/rpc/index.ts +122 -0
- package/src/rpc/rpc.ts +59 -0
- package/src/utils/debounce.ts +23 -0
- package/src/utils/get-peers.ts +53 -0
- package/src/utils/get-self.ts +21 -0
- package/src/utils/to-object.ts +9 -0
package/src/rpc/rpc.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { enable, disable } from '@libp2p/logger'
|
|
2
|
+
import { peerIdFromString } from '@libp2p/peer-id'
|
|
3
|
+
import { multiaddr } from '@multiformats/multiaddr'
|
|
4
|
+
import { getPeers } from '../utils/get-peers.js'
|
|
5
|
+
import { getSelf } from '../utils/get-self.js'
|
|
6
|
+
import type { MetricsRPC } from './index.js'
|
|
7
|
+
import type { DevToolsMetricsComponents } from '../index.js'
|
|
8
|
+
import type { PeerId } from '@libp2p/interface'
|
|
9
|
+
import type { OpenConnectionOptions } from '@libp2p/interface-internal'
|
|
10
|
+
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
11
|
+
import type { AbortOptions } from 'it-pushable'
|
|
12
|
+
|
|
13
|
+
export function metricsRpc (components: DevToolsMetricsComponents): MetricsRPC {
|
|
14
|
+
const log = components.logger.forComponent('libp2p:devtools-metrics:metrics-rpc')
|
|
15
|
+
|
|
16
|
+
return {
|
|
17
|
+
init: async () => {
|
|
18
|
+
return {
|
|
19
|
+
self: await getSelf(components),
|
|
20
|
+
peers: await getPeers(components, log),
|
|
21
|
+
debug: localStorage.getItem('debug') ?? ''
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
setDebug: async (namespace?: string) => {
|
|
25
|
+
if (namespace?.length != null && namespace?.length > 0) {
|
|
26
|
+
enable(namespace)
|
|
27
|
+
localStorage.setItem('debug', namespace)
|
|
28
|
+
} else {
|
|
29
|
+
disable()
|
|
30
|
+
localStorage.removeItem('debug')
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
openConnection: async (peerIdOrMultiaddr: string, options?: OpenConnectionOptions) => {
|
|
34
|
+
let peer: PeerId | Multiaddr
|
|
35
|
+
|
|
36
|
+
try {
|
|
37
|
+
peer = peerIdFromString(peerIdOrMultiaddr)
|
|
38
|
+
} catch {
|
|
39
|
+
peer = multiaddr(peerIdOrMultiaddr)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
await components.connectionManager.openConnection(peer, options)
|
|
43
|
+
},
|
|
44
|
+
closeConnection: async (peerId: PeerId, options?: AbortOptions) => {
|
|
45
|
+
await Promise.all(
|
|
46
|
+
components.connectionManager.getConnections(peerId)
|
|
47
|
+
.map(async connection => {
|
|
48
|
+
try {
|
|
49
|
+
await connection.close(options)
|
|
50
|
+
} catch (err: any) {
|
|
51
|
+
connection.abort(err)
|
|
52
|
+
}
|
|
53
|
+
})
|
|
54
|
+
)
|
|
55
|
+
},
|
|
56
|
+
contentRouting: components.contentRouting,
|
|
57
|
+
peerRouting: components.peerRouting
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export function debounce (callback: () => void, wait: number = 100): () => void {
|
|
2
|
+
let timeout: ReturnType<typeof setTimeout>
|
|
3
|
+
let start: number | undefined
|
|
4
|
+
|
|
5
|
+
return (): void => {
|
|
6
|
+
if (start == null) {
|
|
7
|
+
start = Date.now()
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
if (timeout != null && Date.now() - start > wait) {
|
|
11
|
+
clearTimeout(timeout)
|
|
12
|
+
start = undefined
|
|
13
|
+
callback()
|
|
14
|
+
return
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
clearTimeout(timeout)
|
|
18
|
+
timeout = setTimeout(() => {
|
|
19
|
+
start = undefined
|
|
20
|
+
callback()
|
|
21
|
+
}, wait)
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { base64 } from 'multiformats/bases/base64'
|
|
2
|
+
import { toObject } from './to-object.js'
|
|
3
|
+
import type { Peer } from '../rpc/index.js'
|
|
4
|
+
import type { Logger, PeerStore } from '@libp2p/interface'
|
|
5
|
+
import type { ConnectionManager } from '@libp2p/interface-internal'
|
|
6
|
+
|
|
7
|
+
export interface Components {
|
|
8
|
+
connectionManager: ConnectionManager
|
|
9
|
+
peerStore: PeerStore
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export async function getPeers (components: Components, log: Logger): Promise<Peer[]> {
|
|
13
|
+
const peers: Peer[] = []
|
|
14
|
+
const connections = components.connectionManager.getConnectionsMap()
|
|
15
|
+
const connectedAddresses = [...connections.values()].flatMap(conn => conn).map(conn => conn.remoteAddr.toString())
|
|
16
|
+
|
|
17
|
+
for (const [peerId, conns] of connections.entries()) {
|
|
18
|
+
try {
|
|
19
|
+
const peer = await components.peerStore.get(peerId)
|
|
20
|
+
|
|
21
|
+
peers.push({
|
|
22
|
+
id: peerId,
|
|
23
|
+
addresses: peer.addresses.map(({ isCertified, multiaddr }) => {
|
|
24
|
+
return {
|
|
25
|
+
multiaddr,
|
|
26
|
+
isCertified,
|
|
27
|
+
isConnected: connectedAddresses.includes(multiaddr.toString())
|
|
28
|
+
}
|
|
29
|
+
}),
|
|
30
|
+
protocols: [...peer.protocols],
|
|
31
|
+
tags: toObject(peer.tags, (t) => t.value),
|
|
32
|
+
metadata: toObject(peer.metadata, (buf) => base64.encode(buf))
|
|
33
|
+
})
|
|
34
|
+
} catch (err) {
|
|
35
|
+
log.error('could not load peer data from peer store', err)
|
|
36
|
+
|
|
37
|
+
peers.push({
|
|
38
|
+
id: peerId,
|
|
39
|
+
addresses: conns.map(conn => {
|
|
40
|
+
return {
|
|
41
|
+
multiaddr: conn.remoteAddr,
|
|
42
|
+
isConnected: connectedAddresses.includes(conn.remoteAddr.toString())
|
|
43
|
+
}
|
|
44
|
+
}),
|
|
45
|
+
protocols: [],
|
|
46
|
+
tags: {},
|
|
47
|
+
metadata: {}
|
|
48
|
+
})
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return peers
|
|
53
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { base64 } from 'multiformats/bases/base64'
|
|
2
|
+
import { toObject } from './to-object.js'
|
|
3
|
+
import type { Peer } from '../rpc/index.js'
|
|
4
|
+
import type { PeerId, PeerStore } from '@libp2p/interface'
|
|
5
|
+
|
|
6
|
+
export interface Components {
|
|
7
|
+
peerId: PeerId
|
|
8
|
+
peerStore: PeerStore
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export async function getSelf (components: Components): Promise<Peer> {
|
|
12
|
+
const peer = await components.peerStore.get(components.peerId)
|
|
13
|
+
|
|
14
|
+
return {
|
|
15
|
+
id: peer.id,
|
|
16
|
+
addresses: peer.addresses,
|
|
17
|
+
protocols: [...peer.protocols],
|
|
18
|
+
tags: toObject(peer.tags, (t) => t.value),
|
|
19
|
+
metadata: toObject(peer.metadata, (buf) => base64.encode(buf))
|
|
20
|
+
}
|
|
21
|
+
}
|