Skip to content
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
25 changes: 12 additions & 13 deletions src/whatwg-fetch-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ export class WHATWGFetch implements Startable, WHATWGFetchInterface {
const peerWithoutHTTPPath = ma.decapsulateCode(protocols('http-path').code)

if (this.isHTTPTransportMultiaddr(peerWithoutHTTPPath)) {
if (peerWithoutHTTPPath.getPeerId() !== null) {
throw new Error('HTTP Transport does not yet support peer IDs. Use a stream based transport instead.')
}
const [, httpPathVal] = ma.stringTuples().find(([code]) =>
code === protocols('http-path').code
) ?? ['', '']
Expand Down Expand Up @@ -166,7 +169,13 @@ export class WHATWGFetch implements Startable, WHATWGFetchInterface {
throw new Error('peer multiaddr must have at least one part')
}

return parts[parts.length - 1].name === 'http' || parts[parts.length - 1].name === 'https'
// Reverse order for faster common case (/http is near the end)
for (let i = parts.length - 1; i >= 0; i--) {
if (parts[i].name === 'http' || parts[i].name === 'https') {
return true
}
}
return false
}

// Register a protocol with a path and remember it so we can tell our peers
Expand Down Expand Up @@ -209,18 +218,8 @@ export class WHATWGFetch implements Startable, WHATWGFetchInterface {
return cached
}

let fetch
let reqUrl = ''
if (this.isHTTPTransportMultiaddr(peerAddr)) {
fetch = this._fetch
reqUrl = `${multiaddrToUri(peerAddr)}${WELL_KNOWN_PROTOCOLS}`
} else {
const conn = await this.components.connectionManager.openConnection(peerAddr)
const s = await conn.newStream(PROTOCOL_NAME)
fetch = fetchViaDuplex(s)
reqUrl = multiaddrURIPrefix + peerAddr.encapsulate(`/http-path/${encodeURIComponent(WELL_KNOWN_PROTOCOLS)}`).toString()
}
const resp = await fetch(new Request(reqUrl, {
const reqUrl = multiaddrURIPrefix + peerAddr.encapsulate(`/http-path/${encodeURIComponent(WELL_KNOWN_PROTOCOLS)}`).toString()
const resp = await this.fetch(new Request(reqUrl, {
method: 'GET',
headers: {
Accept: 'application/json'
Expand Down
7 changes: 7 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { streamPair } from '@libp2p/interface-compliance-tests/mocks'
import { defaultLogger } from '@libp2p/logger'
import { peerIdFromString } from '@libp2p/peer-id'
import { multiaddr, type Multiaddr, type Protocol } from '@multiformats/multiaddr'
import { expect } from 'aegir/chai'
import { duplexPair } from 'it-pair/duplex'
import { type Libp2p } from 'libp2p'
import pDefer from 'p-defer'
Expand Down Expand Up @@ -95,4 +96,10 @@ describe('whatwg-fetch', () => {

await ping.sendPing(clientNode, serverPeerID)
})

it('Throws an error if using an http transport with a peer id component', async () => {
const clientNode = stubInterface<Libp2p<{ http: HTTP }>>({ services: { http: client } })

await expect(ping.sendPing(clientNode, multiaddr('/ip4/127.0.0.1/http/p2p/' + serverPeerID.toString()))).to.be.rejectedWith('HTTP Transport does not yet support peer IDs')
})
})