Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add helia version to agent version #128

Merged
merged 1 commit into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Comment on lines +156 to +157
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not saying we need to open a new PR, but I think it would be nice in future for these kinds of things if we linked to the source of the information (presumably somewhere in js-libp2p).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, but it’s buried fairly deeply in the identify service so would need a change there - it’s not something that’s needed to be accessed before.

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'
Comment on lines +1 to +2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not have this file be a json file

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Node 18 still shows a warning if you import json from js:

(node:25810) ExperimentalWarning: Importing JSON modules is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)

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)
Comment on lines +46 to +47
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it make sense to maintain and expose a method for obtaining a decoded agent version ? I can imagine having to decode this agent version being a minor pain point for users.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The AgentVersion is used by the identify protocol and it's not something people generally interact with directly.


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