Skip to content

Commit

Permalink
chore: convert everything to peer records
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Oct 24, 2023
1 parent b14ff94 commit ab5e8fd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
38 changes: 24 additions & 14 deletions packages/client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { ipnsValidator } from 'ipns/validator'
import ndjson from 'iterable-ndjson'
import defer from 'p-defer'
import PQueue from 'p-queue'
import type { DelegatedRoutingV1HttpApiClient, DelegatedRoutingV1HttpApiClientInit, Record, PeerRecord } from './index.js'
import type { DelegatedRoutingV1HttpApiClient, DelegatedRoutingV1HttpApiClientInit, PeerRecord } from './index.js'
import type { AbortOptions } from '@libp2p/interface'
import type { PeerId } from '@libp2p/interface/peer-id'
import type { CID } from 'multiformats'
Expand Down Expand Up @@ -56,7 +56,7 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV
this.started = false
}

async * getProviders (cid: CID, options: AbortOptions = {}): AsyncGenerator<Record, any, unknown> {
async * getProviders (cid: CID, options: AbortOptions = {}): AsyncGenerator<PeerRecord, any, unknown> {
log('getProviders starts: %c', cid)

const signal = anySignal([this.shutDownController.signal, options.signal, AbortSignal.timeout(this.timeout)])
Expand Down Expand Up @@ -86,14 +86,14 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV

for (const provider of body.Providers) {
const record = this.#handleProviderRecords(provider)
if (record !== null) {
if (record != null) {
yield record
}
}
} else {
for await (const provider of ndjson(toIt(res.body))) {
const record = this.#handleProviderRecords(provider)
if (record !== null) {
if (record != null) {
yield record
}
}
Expand Down Expand Up @@ -137,14 +137,14 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV

for (const peer of body.Peers) {
const record = this.#handlePeerRecords(peerId, peer)
if (record !== null) {
if (record != null) {
yield record
}
}
} else {
for await (const peer of ndjson(toIt(res.body))) {
const record = this.#handlePeerRecords(peerId, peer)
if (record !== null) {
if (record != null) {
yield record
}
}
Expand Down Expand Up @@ -223,25 +223,37 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV
}
}

#handleProviderRecords (record: any): Record | null {
#handleProviderRecords (record: any): PeerRecord | undefined {
if (record.Schema === 'peer') {
// Peer schema can have additional, user-defined, fields.
record.ID = peerIdFromString(record.ID)
record.Addrs = record.Addrs.map(multiaddr)
return record
} else if (record.Schema === 'bitswap') {
// Bitswap schema is deprecated, was incorrectly used when server had no information about actual protocols, so we convert it to peer result without protocol information.
}

if (record.Schema === 'bitswap') {
// Bitswap schema is deprecated, was incorrectly used when server had no
// information about actual protocols, so we convert it to peer result
// without protocol information
return {
Schema: 'peer',
ID: peerIdFromString(record.ID),
Addrs: record.Addrs.map(multiaddr)
Addrs: record.Addrs.map(multiaddr),
Protocols: record.Protocol != null ? [record.Protocol] : []
}
}

return null
if (record.ID != null && Array.isArray(record.Addrs)) {
return {
Schema: 'peer',
ID: peerIdFromString(record.ID),
Addrs: record.Addrs.map(multiaddr),
Protocols: Array.isArray(record.Protocols) ? record.Protocols : []
}
}
}

#handlePeerRecords (peerId: PeerId, record: any): PeerRecord | null {
#handlePeerRecords (peerId: PeerId, record: any): PeerRecord | undefined {
if (record.Schema === 'peer') {
// Peer schema can have additional, user-defined, fields.
record.ID = peerIdFromString(record.ID)
Expand All @@ -250,7 +262,5 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV
return record
}
}

return null
}
}
11 changes: 1 addition & 10 deletions packages/client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,6 @@ export interface PeerRecord {
Protocols?: string[]
}

export interface BitswapRecord {
Schema: 'bitswap'
Protocol: string
ID: PeerId
Addrs: Multiaddr[]
}

export type Record = PeerRecord | BitswapRecord

export interface DelegatedRoutingV1HttpApiClientInit {
/**
* A concurrency limit to avoid request flood in web browser (default: 4)
Expand All @@ -59,7 +50,7 @@ export interface DelegatedRoutingV1HttpApiClient {
* Returns an async generator of PeerInfos that can provide the content
* for the passed CID
*/
getProviders(cid: CID, options?: AbortOptions): AsyncGenerator<Record>
getProviders(cid: CID, options?: AbortOptions): AsyncGenerator<PeerRecord>

/**
* Returns an async generator of PeerInfos for the provided PeerId
Expand Down
3 changes: 3 additions & 0 deletions packages/client/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ describe('routing-v1-http-api-client', () => {
Metadata: 'gBI=',
ID: (await createEd25519PeerId()).toString(),
Addrs: ['/ip4/42.42.42.42/tcp/1234']
}, {
ID: (await createEd25519PeerId()).toString(),
Addrs: ['/ip4/43.43.43.43/tcp/1234']
}]

const cid = CID.parse('QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn')
Expand Down

0 comments on commit ab5e8fd

Please sign in to comment.