Skip to content

Commit 9b74c43

Browse files
committed
feat: fix for failing tests due to dep changes
1 parent 48cf488 commit 9b74c43

File tree

3 files changed

+58
-14
lines changed

3 files changed

+58
-14
lines changed

packages/protocol-http/src/http-client.ts

+47-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44
import { AbortError, setMaxListeners } from '@libp2p/interface'
55
import { pbStream } from 'it-protobuf-stream'
6+
import { URL } from './common/url.js'
67
import { HttpConstants } from './constants.js'
78
import { type HttpComponents } from './http-components-interface.js'
89
import { http } from './http-proto-api.js'
@@ -97,29 +98,66 @@ export class HttpClient implements Startable {
9798
const result = JSON.parse(contentString) as ProtocolDiscoveryResult
9899
this.log.trace('discovered %d protocols from %p', result.protocols.length, peer)
99100
return result
100-
} catch (err) {
101-
throw new Error('Invalid protocol discovery response: ' + err.message)
101+
} catch (err: unknown) {
102+
const errMessage = err instanceof Error ? err.message : String(err)
103+
throw new Error('Invalid protocol discovery response: ' + errMessage)
102104
}
103-
} catch (err) {
104-
this.log.error('protocol discovery failed - %e', err)
105+
} catch (err: any) {
106+
this.log.error('protocol discovery failed - %e', err?.message)
105107
throw err
106108
}
107109
}
108110

109-
async fetch (peer: PeerId, request: http.HttpRequest, options: AbortOptions = {}): Promise<http.HttpResponse> {
111+
/**
112+
* Send an HTTP request to a peer or URL
113+
*/
114+
async fetch (peerOrUrl: PeerId | string | URL, request: http.HttpRequest, options: AbortOptions = {}): Promise<http.HttpResponse> {
110115
if (!this.isStarted()) {
111116
// Auto-start the client if it hasn't been started
112117
await this.start()
113118
}
114119

115-
this.log.trace('sending %s request to %p for %s', request.method, peer, request.targetUri)
120+
// Handle URL version of the overload
121+
if (typeof peerOrUrl === 'string' || peerOrUrl instanceof URL) {
122+
const urlString = typeof peerOrUrl === 'string' ? peerOrUrl : peerOrUrl.toString()
123+
const url = typeof peerOrUrl === 'string' ? new URL(peerOrUrl) : peerOrUrl
124+
const hostname = url.hostname
125+
126+
if (hostname === undefined || hostname === null || hostname === '') {
127+
throw new Error(`Invalid URL: ${urlString}, missing hostname`)
128+
}
129+
130+
try {
131+
// Import PeerId dynamically to avoid circular dependencies
132+
const { peerIdFromString } = await import('@libp2p/peer-id')
133+
const peer = peerIdFromString(hostname)
134+
135+
// Set the target URI to the path and query of the URL
136+
const targetUri = url.pathname + (url.search !== '' ? url.search : '')
137+
const modifiedRequest = {
138+
...request,
139+
targetUri
140+
}
141+
142+
this.log.trace('sending %s request to peer %s for %s', modifiedRequest.method, hostname, modifiedRequest.targetUri)
143+
return await this.fetch(peer, modifiedRequest, options)
144+
} catch (err) {
145+
// If we can't create a peer ID from the hostname, this is likely a regular web URL
146+
// In this case, we should handle it as a clear web request or throw an appropriate error
147+
this.log.error('not a valid peer ID in URL hostname: %s', hostname)
148+
throw new Error(`Cannot route to ${urlString}: hostname is not a valid peer ID`)
149+
}
150+
}
151+
152+
// Original implementation for PeerId
153+
this.log.trace('sending %s request to %p for %s', request.method, peerOrUrl, request.targetUri)
116154
let connection
117155
let stream: Stream | undefined
118156
let signal = options.signal
119157
let onAbort = (): void => {}
120158

121159
try {
122-
connection = await this.components.connectionManager.openConnection(peer, options)
160+
connection = await this.components.connectionManager.openConnection(peerOrUrl, options)
123161

124162
if (signal == null) {
125163
const timeout = this.init.timeout ?? DEFAULT_TIMEOUT
@@ -143,10 +181,10 @@ export class HttpClient implements Startable {
143181
const response = await pb.read(http.HttpResponse, options)
144182
await pb.unwrap().close(options)
145183

146-
this.log.trace('received response with status %d from %p', response.statusCode, peer)
184+
this.log.trace('received response with status %d from %p', response.statusCode, peerOrUrl)
147185
return response
148186
} catch (err: any) {
149-
this.log.error('error fetching from %p - %e', peer, err)
187+
this.log.error('error fetching from %p - %e', peerOrUrl, err)
150188
if (stream != null) {
151189
stream.abort(err)
152190
}

packages/protocol-http/src/interfaces/http-client-interface.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@ import type { AbortOptions, PeerId } from '@libp2p/interface'
1010
*/
1111
export interface HttpClientInterface {
1212
/**
13-
* Sends an HTTP request to a remote peer
13+
* Sends an HTTP request to a remote peer or URL
14+
*
15+
* @param peerOrUrl - The peer ID or URL to send the request to
16+
* @param request - The HTTP request to send
17+
* @param options - Optional abort options
18+
* @returns A promise that resolves to the HTTP response
1419
*/
15-
fetch(peer: PeerId, request: http.HttpRequest, options?: AbortOptions): Promise<http.HttpResponse>
20+
fetch(peerOrUrl: PeerId | string | URL, request: http.HttpRequest, options?: AbortOptions): Promise<http.HttpResponse>
1621

1722
/**
1823
* Discover protocols supported by a remote peer

packages/protocol-http/test/007-http-client.spec.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { webRTC, webRTCDirect } from '@libp2p/webrtc'
88
import { expect } from 'aegir/chai'
99
import { createLibp2p } from 'libp2p'
1010
import { createSandbox } from 'sinon'
11+
import { URL } from '../src/common/url.js'
1112
import { HttpClientFactory } from '../src/http-client-factory.js'
1213
import { getNodeConfig } from './common/node-config.js'
1314
import type { http } from '../src/http-proto-api.js'
@@ -82,8 +83,8 @@ describe('007-HTTP Client Implementation', () => {
8283
trailers: []
8384
}
8485
}
85-
await expect(client.fetch(new URL('libp2p://test'), request))
86-
.to.be.rejectedWith('Invalid URL: could not extract peer ID from libp2p://test')
86+
await expect(client.fetch(new URL('https://test'), request))
87+
.to.be.rejectedWith('Invalid URL: could not extract peer ID from https://test')
8788
})
8889

8990
it('should handle invalid address', async () => {
@@ -98,7 +99,7 @@ describe('007-HTTP Client Implementation', () => {
9899
}
99100
}
100101
const validPeerId = '12D3KooWGC6nRXh5ZXvpeqscJCEiYkGYvXZr5RfKJ6iBXqE3Xa7X'
101-
await expect(client.fetch(new URL(`libp2p://${validPeerId}`), request))
102+
await expect(client.fetch(new URL(`https://${validPeerId}`), request))
102103
.to.be.rejectedWith('The dial request has no valid addresses')
103104
})
104105
})

0 commit comments

Comments
 (0)