@@ -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 ( / j s - l i b p 2 p \/ \d + \. \d + \. \d + \s U s e r A g e n t = / ) == null ) {
185- // the user changed the agent version
186- return
187- }
168+ if ( existingAgentVersion . match ( / j s - l i b p 2 p \/ \d + \. \d + \. \d + \s U s e r A g e n t = / ) == 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}
0 commit comments