feat: programmatically set agentVersion for use in identify (#1296)

If no `agentVersion` is provided for the Identify protocol, the default `AGENT_VERSION` will now be set to
* `js-libp2p/<libp2p.version> UserAgent=<process.version>` when running in Node.js
* `js-libp2p/<libp2p.version> UserAgent=<navigator.userAgent>` when running in the browser (also when running in a webworker)

Fixes #686
Supersedes #1240

Co-authored-by: Kevin Westphal <westphal@consider-it.de>
Co-authored-by: Kevin <56823591+6d7a@users.noreply.github.com>
This commit is contained in:
Alex Potsides
2022-07-15 16:35:52 +00:00
committed by GitHub
parent 6eaab2e3ee
commit 0bb1b802c8
5 changed files with 49 additions and 1 deletions

View File

@ -14,6 +14,7 @@ import { peerIdFromString } from '@libp2p/peer-id'
import type { PeerId } from '@libp2p/interface-peer-id'
import type { Libp2pNode } from '../../src/libp2p.js'
import { pEvent } from 'p-event'
import { AGENT_VERSION } from '../../src/identify/consts.js'
describe('libp2p.dialer.identifyService', () => {
let peerId: PeerId
@ -147,6 +148,29 @@ describe('libp2p.dialer.identifyService', () => {
await pWaitFor(() => connection.streams.length === 0)
})
it('should append UserAgent information to default AGENT_VERSION', async () => {
// Stub environment version for testing dynamic AGENT_VERSION
sinon.stub(process, 'version').value('vTEST')
if (typeof globalThis.navigator !== 'undefined') {
sinon.stub(navigator, 'userAgent').value('vTEST')
}
libp2p = await createLibp2pNode(createBaseOptions({
peerId
}))
await libp2p.start()
if (libp2p.identifyService == null) {
throw new Error('Identity service was not configured')
}
const storedAgentVersion = await libp2p.peerStore.metadataBook.getValue(peerId, 'AgentVersion')
expect(AGENT_VERSION + ' UserAgent=vTEST').to.equal(uint8ArrayToString(storedAgentVersion ?? new Uint8Array()))
})
it('should store host data and protocol version into metadataBook', async () => {
const agentVersion = 'js-project/1.0.0'