Skip to content

Commit

Permalink
feat: programmatically set agentVersion for use in identify (#1296)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
3 people committed Jul 15, 2022
1 parent 6eaab2e commit 0bb1b80
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
"clean": "aegir clean",
"lint": "aegir lint",
"dep-check": "aegir dep-check",
"prepublishOnly": "node scripts/update-version.js",
"build": "aegir build",
"generate": "run-s generate:proto:*",
"generate:proto:circuit": "protons ./src/circuit/pb/index.proto",
Expand Down
14 changes: 14 additions & 0 deletions scripts/update-version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { readFile, writeFile } from 'fs/promises'

const pkg = JSON.parse(
await readFile(
new URL('../package.json', import.meta.url)
)
)

await writeFile(
new URL('../src/version.ts', import.meta.url),
`export const version = '${pkg.version}'
export const name = '${pkg.name}'
`
)
10 changes: 10 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { Libp2pInit } from './index.js'
import { codes, messages } from './errors.js'
import errCode from 'err-code'
import type { RecursivePartial } from '@libp2p/interfaces'
import { isNode, isBrowser, isWebWorker, isElectronMain, isElectronRenderer, isReactNative } from 'wherearewe'

const DefaultConfig: Partial<Libp2pInit> = {
addresses: {
Expand Down Expand Up @@ -116,5 +117,14 @@ export function validateConfig (opts: RecursivePartial<Libp2pInit>): Libp2pInit
throw errCode(new Error(messages.ERR_PROTECTOR_REQUIRED), codes.ERR_PROTECTOR_REQUIRED)
}

// Append user agent version to default AGENT_VERSION depending on the environment
if (resultingOptions.identify.host.agentVersion === AGENT_VERSION) {
if (isNode || isElectronMain) {
resultingOptions.identify.host.agentVersion += ` UserAgent=${globalThis.process.version}`
} else if (isBrowser || isWebWorker || isElectronRenderer || isReactNative) {
resultingOptions.identify.host.agentVersion += ` UserAgent=${globalThis.navigator.userAgent}`
}
}

return resultingOptions
}
1 change: 0 additions & 1 deletion src/version.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@

export const version = '0.0.0'
export const name = 'libp2p'
24 changes: 24 additions & 0 deletions test/identify/service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'

Expand Down

0 comments on commit 0bb1b80

Please sign in to comment.