Skip to content

Commit

Permalink
feat: Add getPeerMeta method
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoPolo committed Jun 13, 2024
1 parent 9407f5d commit 303410e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* ```
*/

import { WHATWGFetch } from './whatwg-fetch-service.js'
import { WHATWGFetch, type ProtosMap } from './whatwg-fetch-service.js'
import type { ComponentLogger } from '@libp2p/interface'
import type { ConnectionManager, Registrar } from '@libp2p/interface-internal'
import type { Multiaddr } from '@multiformats/multiaddr'
Expand All @@ -49,10 +49,14 @@ export { WELL_KNOWN_PROTOCOLS } from './constants.js'
*/
export interface HTTP {
fetch(request: string | Request, requestInit?: RequestInit): Promise<Response>

// Uses the peer's .well-known endpoint to find where it hosts a given protocol.
// Throws an error if the peer does not serve the protocol.
prefixForProtocol (peer: Multiaddr, protocol: string): Promise<string>

// Get the .well-known protocols for a peer.
getPeerMeta (peer: Multiaddr): Promise<ProtosMap>

// handleHTTPProtocol registers a handler for the given protocol on the given path. This is incompatible with a customHTTPHandler.
handleHTTPProtocol (protocol: string, path: string, handler: (req: Request) => Promise<Response>): void
}
Expand Down
14 changes: 8 additions & 6 deletions src/whatwg-fetch-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { IncomingStreamData } from '@libp2p/interface-internal'
const multiaddrURIPrefix = 'multiaddr:'

type ProtocolID = string
type ProtosMap = Record<ProtocolID, { path: string }>
export type ProtosMap = Record<ProtocolID, { path: string }>
type ProtoHandlers = Record<ProtocolID, HTTPHandler>

export class WHATWGFetch implements Startable, WHATWGFetchInterface {
Expand Down Expand Up @@ -192,13 +192,10 @@ export class WHATWGFetch implements Startable, WHATWGFetchInterface {
this.protoHandlers[protocol] = handler
}

async prefixForProtocol (peer: Multiaddr, protocol: string): Promise<string> {
async getPeerMeta (peer: Multiaddr): Promise<ProtosMap> {
const cached = this.wellKnownProtosCache.get(peer)
if (cached !== undefined) {
if (cached[protocol] == null) {
throw new Error(`Peer does not serve protocol: ${protocol}`)
}
return cached[protocol].path
return cached

Check warning on line 198 in src/whatwg-fetch-service.ts

View check run for this annotation

Codecov / codecov/patch

src/whatwg-fetch-service.ts#L198

Added line #L198 was not covered by tests
}

let fetch
Expand All @@ -223,6 +220,11 @@ export class WHATWGFetch implements Startable, WHATWGFetchInterface {
}
const peerMeta = await resp.json()
this.wellKnownProtosCache.set(peer, peerMeta)
return peerMeta
}

async prefixForProtocol (peer: Multiaddr, protocol: string): Promise<string> {
const peerMeta = await this.getPeerMeta(peer)
if (peerMeta[protocol] == null) {
throw new Error(`Peer does not serve protocol: ${protocol}`)
}
Expand Down

0 comments on commit 303410e

Please sign in to comment.