Skip to content

Commit

Permalink
fix: add helia version to agent version (#128)
Browse files Browse the repository at this point in the history
If the user has configured identify but not overridden the agent version, add helia and its version number to the string.

Fixes #122
  • Loading branch information
achingbrain authored May 19, 2023
1 parent 996ca30 commit 48e19ec
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 5 deletions.
7 changes: 4 additions & 3 deletions packages/helia/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
"test:firefox-webworker": "aegir test -t webworker -- --browser firefox",
"test:node": "aegir test -t node --cov",
"test:electron-main": "aegir test -t electron-main",
"prepublishOnly": "node scripts/update-version.js && npm run build",
"release": "aegir release"
},
"dependencies": {
Expand All @@ -144,7 +145,7 @@
"@helia/interface": "^1.0.0",
"@ipld/dag-pb": "^4.0.3",
"@libp2p/bootstrap": "^8.0.0",
"@libp2p/interface-libp2p": "^3.1.0",
"@libp2p/interface-libp2p": "^3.2.0",
"@libp2p/interface-pubsub": "^4.0.1",
"@libp2p/interfaces": "^3.3.2",
"@libp2p/ipni-content-routing": "^1.0.0",
Expand All @@ -154,6 +155,7 @@
"@libp2p/mplex": "^8.0.3",
"@libp2p/tcp": "^7.0.1",
"@libp2p/webrtc": "^2.0.4",
"@libp2p/websockets": "^6.0.1",
"@libp2p/webtransport": "^2.0.1",
"blockstore-core": "^4.0.0",
"cborg": "^1.10.0",
Expand All @@ -166,6 +168,7 @@
"it-drain": "^3.0.1",
"it-filter": "^3.0.1",
"it-foreach": "^2.0.2",
"libp2p": "^0.45.2",
"mortice": "^3.0.1",
"multiformats": "^11.0.1",
"p-defer": "^4.0.0",
Expand All @@ -176,11 +179,9 @@
"devDependencies": {
"@ipld/dag-cbor": "^9.0.0",
"@ipld/dag-json": "^10.0.1",
"@libp2p/websockets": "^6.0.1",
"@types/sinon": "^10.0.14",
"aegir": "^39.0.4",
"delay": "^5.0.0",
"libp2p": "^0.45.1",
"sinon": "^15.0.2",
"sinon-ts": "^1.0.0"
},
Expand Down
14 changes: 14 additions & 0 deletions packages/helia/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}'
`
)
48 changes: 48 additions & 0 deletions packages/helia/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
* ```
*/

import { logger } from '@libp2p/logger'
import { MemoryBlockstore } from 'blockstore-core'
import { MemoryDatastore } from 'datastore-core'
import { HeliaImpl } from './helia.js'
import { createLibp2p } from './utils/libp2p.js'
import { name, version } from './version.js'
import type { Helia } from '@helia/interface'
import type { Libp2p } from '@libp2p/interface-libp2p'
import type { PubSub } from '@libp2p/interface-pubsub'
Expand All @@ -34,6 +36,8 @@ import type { Datastore } from 'interface-datastore'
import type { CID } from 'multiformats/cid'
import type { MultihashHasher } from 'multiformats/hashes/interface'

const log = logger('helia')

/**
* DAGWalkers take a block and yield CIDs encoded in that block
*/
Expand Down Expand Up @@ -122,5 +126,49 @@ export async function createHelia (init: HeliaInit = {}): Promise<Helia<unknown>
await helia.start()
}

// add helia to agent version
if (helia.libp2p.isStarted()) {
await addHeliaToAgentVersion(helia)
} else {
helia.libp2p.addEventListener('start', () => {
addHeliaToAgentVersion(helia)
.catch(err => {
log.error('could not add Helia to agent version', err)
})
})
}

return helia
}

async function addHeliaToAgentVersion (helia: Helia): Promise<void> {
// add helia to agent version
const peer = await helia.libp2p.peerStore.get(helia.libp2p.peerId)
const versionBuf = peer.metadata.get('AgentVersion')

if (versionBuf == null) {
// identify was not configured
return
}

let versionStr = new TextDecoder().decode(versionBuf)

if (versionStr.match(/js-libp2p\/\d+\.\d+\.\d+\sUserAgent=/) == null) {
// the user changed the agent version
return
}

if (versionStr.includes(name)) {
// update version name
versionStr = `${name}/${version} ${versionStr.split(' ').slice(1).join(' ')}`
} else {
// just prepend version name
versionStr = `${name}/${version} ${versionStr}`
}

await helia.libp2p.peerStore.merge(helia.libp2p.peerId, {
metadata: {
AgentVersion: new TextEncoder().encode(versionStr)
}
})
}
2 changes: 2 additions & 0 deletions packages/helia/src/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const version = '0.0.0'
export const name = 'helia'
49 changes: 49 additions & 0 deletions packages/helia/test/factory.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/* eslint-env mocha */

import { webSockets } from '@libp2p/websockets'
import { expect } from 'aegir/chai'
import { Key } from 'interface-datastore'
import { createLibp2p } from 'libp2p'
import { identifyService } from 'libp2p/identify'
import { CID } from 'multiformats/cid'
import { createHelia } from '../src/index.js'
import type { Helia } from '@helia/interface'
Expand Down Expand Up @@ -35,4 +38,50 @@ describe('helia factory', () => {
await helia.datastore.put(key, block)
expect(await helia.datastore.has(key)).to.be.true()
})

it('adds helia details to the AgentVersion', async () => {
helia = await createHelia()

const peer = await helia.libp2p.peerStore.get(helia.libp2p.peerId)
const agentVersionBuf = peer.metadata.get('AgentVersion')
const agentVersion = new TextDecoder().decode(agentVersionBuf)

expect(agentVersion).to.include('helia/')
})

it('does not add helia details to the AgentVersion when it has been overridden', async () => {
helia = await createHelia({
libp2p: await createLibp2p({
transports: [
webSockets()
],
services: {
identity: identifyService({
agentVersion: 'my custom agent version'
})
}
})
})

const peer = await helia.libp2p.peerStore.get(helia.libp2p.peerId)
const agentVersionBuf = peer.metadata.get('AgentVersion')
const agentVersion = new TextDecoder().decode(agentVersionBuf)

expect(agentVersion).to.not.include('helia/')
})

it('does not add helia details to the AgentVersion when identify is not configured', async () => {
helia = await createHelia({
libp2p: await createLibp2p({
transports: [
webSockets()
]
})
})

const peer = await helia.libp2p.peerStore.get(helia.libp2p.peerId)
const agentVersionBuf = peer.metadata.get('AgentVersion')

expect(agentVersionBuf).to.be.undefined()
})
})
2 changes: 1 addition & 1 deletion packages/interface/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
"release": "aegir release"
},
"dependencies": {
"@libp2p/interface-libp2p": "^3.1.0",
"@libp2p/interface-libp2p": "^3.2.0",
"@libp2p/interfaces": "^3.3.2",
"interface-blockstore": "^5.0.0",
"interface-datastore": "^8.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/interop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"ipfsd-ctl": "^13.0.0",
"it-to-buffer": "^4.0.1",
"kubo-rpc-client": "^3.0.0",
"libp2p": "^0.45.1",
"libp2p": "^0.45.2",
"multiformats": "^11.0.1"
},
"browser": {
Expand Down

0 comments on commit 48e19ec

Please sign in to comment.