Skip to content

Commit 6dc7d55

Browse files
authored
fix: update attempt to add helia to identify agent version (#268)
The identify service does not pull the agent string from the peer store any more so attempt to update it on the service directly.
1 parent d5e9c3c commit 6dc7d55

File tree

2 files changed

+16
-47
lines changed

2 files changed

+16
-47
lines changed

packages/helia/src/index.ts

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,7 @@ export async function createHelia (init: HeliaInit = {}): Promise<Helia<unknown>
143143
}
144144

145145
// add helia to agent version
146-
if (helia.libp2p.isStarted()) {
147-
await addHeliaToAgentVersion(helia)
148-
} else {
149-
helia.libp2p.addEventListener('start', () => {
150-
addHeliaToAgentVersion(helia)
151-
.catch(err => {
152-
log.error('could not add Helia to agent version', err)
153-
})
154-
})
155-
}
146+
addHeliaToAgentVersion(helia)
156147

157148
return helia
158149
}
@@ -169,34 +160,18 @@ function isLibp2p (obj: any): obj is Libp2p {
169160
return funcs.every(m => typeof obj[m] === 'function')
170161
}
171162

172-
async function addHeliaToAgentVersion (helia: Helia): Promise<void> {
163+
function addHeliaToAgentVersion (helia: Helia<any>): void {
173164
// add helia to agent version
174-
const peer = await helia.libp2p.peerStore.get(helia.libp2p.peerId)
175-
const versionBuf = peer.metadata.get('AgentVersion')
176-
177-
if (versionBuf == null) {
178-
// identify was not configured
179-
return
180-
}
181-
182-
let versionStr = new TextDecoder().decode(versionBuf)
165+
try {
166+
const existingAgentVersion = helia.libp2p.services.identify.host.agentVersion
183167

184-
if (versionStr.match(/js-libp2p\/\d+\.\d+\.\d+\sUserAgent=/) == null) {
185-
// the user changed the agent version
186-
return
187-
}
168+
if (existingAgentVersion.match(/js-libp2p\/\d+\.\d+\.\d+\sUserAgent=/) == null) {
169+
// the user changed the agent version
170+
return
171+
}
188172

189-
if (versionStr.includes(name)) {
190-
// update version name
191-
versionStr = `${name}/${version} ${versionStr.split(' ').slice(1).join(' ')}`
192-
} else {
193-
// just prepend version name
194-
versionStr = `${name}/${version} ${versionStr}`
173+
helia.libp2p.services.identify.host.agentVersion = `${name}/${version} ${helia.libp2p.services.identify.host.agentVersion}`
174+
} catch (err) {
175+
log.error('could not add Helia to agent version', err)
195176
}
196-
197-
await helia.libp2p.peerStore.merge(helia.libp2p.peerId, {
198-
metadata: {
199-
AgentVersion: new TextEncoder().encode(versionStr)
200-
}
201-
})
202177
}

packages/helia/test/factory.spec.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,8 @@ describe('helia factory', () => {
4242
it('adds helia details to the AgentVersion', async () => {
4343
helia = await createHelia()
4444

45-
const peer = await helia.libp2p.peerStore.get(helia.libp2p.peerId)
46-
const agentVersionBuf = peer.metadata.get('AgentVersion')
47-
const agentVersion = new TextDecoder().decode(agentVersionBuf)
48-
49-
expect(agentVersion).to.include('helia/')
45+
expect(helia).to.have.nested.property('libp2p.services.identify.host.agentVersion')
46+
.that.includes('helia/')
5047
})
5148

5249
it('does not add helia details to the AgentVersion when it has been overridden', async () => {
@@ -56,18 +53,15 @@ describe('helia factory', () => {
5653
webSockets()
5754
],
5855
services: {
59-
identity: identifyService({
56+
identify: identifyService({
6057
agentVersion: 'my custom agent version'
6158
})
6259
}
6360
})
6461
})
6562

66-
const peer = await helia.libp2p.peerStore.get(helia.libp2p.peerId)
67-
const agentVersionBuf = peer.metadata.get('AgentVersion')
68-
const agentVersion = new TextDecoder().decode(agentVersionBuf)
69-
70-
expect(agentVersion).to.not.include('helia/')
63+
expect(helia).to.have.nested.property('libp2p.services.identify.host.agentVersion')
64+
.that.does.not.include('helia/')
7165
})
7266

7367
it('does not add helia details to the AgentVersion when identify is not configured', async () => {

0 commit comments

Comments
 (0)