mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-04-24 18:12:14 +00:00
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:
parent
6eaab2e3ee
commit
0bb1b802c8
@ -80,6 +80,7 @@
|
|||||||
"clean": "aegir clean",
|
"clean": "aegir clean",
|
||||||
"lint": "aegir lint",
|
"lint": "aegir lint",
|
||||||
"dep-check": "aegir dep-check",
|
"dep-check": "aegir dep-check",
|
||||||
|
"prepublishOnly": "node scripts/update-version.js",
|
||||||
"build": "aegir build",
|
"build": "aegir build",
|
||||||
"generate": "run-s generate:proto:*",
|
"generate": "run-s generate:proto:*",
|
||||||
"generate:proto:circuit": "protons ./src/circuit/pb/index.proto",
|
"generate:proto:circuit": "protons ./src/circuit/pb/index.proto",
|
||||||
|
14
scripts/update-version.js
Normal file
14
scripts/update-version.js
Normal file
@ -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,6 +10,7 @@ import type { Libp2pInit } from './index.js'
|
|||||||
import { codes, messages } from './errors.js'
|
import { codes, messages } from './errors.js'
|
||||||
import errCode from 'err-code'
|
import errCode from 'err-code'
|
||||||
import type { RecursivePartial } from '@libp2p/interfaces'
|
import type { RecursivePartial } from '@libp2p/interfaces'
|
||||||
|
import { isNode, isBrowser, isWebWorker, isElectronMain, isElectronRenderer, isReactNative } from 'wherearewe'
|
||||||
|
|
||||||
const DefaultConfig: Partial<Libp2pInit> = {
|
const DefaultConfig: Partial<Libp2pInit> = {
|
||||||
addresses: {
|
addresses: {
|
||||||
@ -116,5 +117,14 @@ export function validateConfig (opts: RecursivePartial<Libp2pInit>): Libp2pInit
|
|||||||
throw errCode(new Error(messages.ERR_PROTECTOR_REQUIRED), codes.ERR_PROTECTOR_REQUIRED)
|
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
|
return resultingOptions
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,2 @@
|
|||||||
|
|
||||||
export const version = '0.0.0'
|
export const version = '0.0.0'
|
||||||
export const name = 'libp2p'
|
export const name = 'libp2p'
|
||||||
|
@ -14,6 +14,7 @@ import { peerIdFromString } from '@libp2p/peer-id'
|
|||||||
import type { PeerId } from '@libp2p/interface-peer-id'
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
||||||
import type { Libp2pNode } from '../../src/libp2p.js'
|
import type { Libp2pNode } from '../../src/libp2p.js'
|
||||||
import { pEvent } from 'p-event'
|
import { pEvent } from 'p-event'
|
||||||
|
import { AGENT_VERSION } from '../../src/identify/consts.js'
|
||||||
|
|
||||||
describe('libp2p.dialer.identifyService', () => {
|
describe('libp2p.dialer.identifyService', () => {
|
||||||
let peerId: PeerId
|
let peerId: PeerId
|
||||||
@ -147,6 +148,29 @@ describe('libp2p.dialer.identifyService', () => {
|
|||||||
await pWaitFor(() => connection.streams.length === 0)
|
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 () => {
|
it('should store host data and protocol version into metadataBook', async () => {
|
||||||
const agentVersion = 'js-project/1.0.0'
|
const agentVersion = 'js-project/1.0.0'
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user