Skip to content

Commit

Permalink
fix: PeerRecord Addrs and Protocols do not need to be optional (#43)
Browse files Browse the repository at this point in the history
We ensure that the `.Addrs` and `.Protocols` properties are present
so they don't need to be marked optional in the interface.
  • Loading branch information
achingbrain authored Oct 26, 2023
1 parent 3fcc332 commit ec62768
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 14 deletions.
2 changes: 1 addition & 1 deletion packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
"browser-readablestream-to-it": "^2.0.3",
"ipns": "^7.0.1",
"it-all": "^3.0.2",
"iterable-ndjson": "^1.1.0",
"it-ndjson": "^1.0.4",
"multiformats": "^12.1.1",
"p-defer": "^4.0.0",
"p-queue": "^7.3.4"
Expand Down
6 changes: 3 additions & 3 deletions packages/client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import { anySignal } from 'any-signal'
import toIt from 'browser-readablestream-to-it'
import { unmarshal, type IPNSRecord, marshal, peerIdToRoutingKey } from 'ipns'
import { ipnsValidator } from 'ipns/validator'
// @ts-expect-error no types
import ndjson from 'iterable-ndjson'
import { parse as ndjson } from 'it-ndjson'
import defer from 'p-defer'
import PQueue from 'p-queue'
import type { DelegatedRoutingV1HttpApiClient, DelegatedRoutingV1HttpApiClientInit, PeerRecord } from './index.js'
Expand Down Expand Up @@ -107,7 +106,7 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV
}
}

async * getPeerInfo (peerId: PeerId, options: AbortOptions | undefined = {}): AsyncGenerator<PeerRecord, any, unknown> {
async * getPeers (peerId: PeerId, options: AbortOptions | undefined = {}): AsyncGenerator<PeerRecord, any, unknown> {
log('getPeers starts: %c', peerId)

const signal = anySignal([this.shutDownController.signal, options.signal, AbortSignal.timeout(this.timeout)])
Expand Down Expand Up @@ -228,6 +227,7 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV
// Peer schema can have additional, user-defined, fields.
record.ID = peerIdFromString(record.ID)
record.Addrs = record.Addrs.map(multiaddr)
record.Protocols = record.Protocols ?? []
return record
}

Expand Down
17 changes: 9 additions & 8 deletions packages/client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import type { CID } from 'multiformats/cid'
export interface PeerRecord {
Schema: 'peer'
ID: PeerId
Addrs?: Multiaddr[]
Protocols?: string[]
Addrs: Multiaddr[]
Protocols: string[]
}

export interface DelegatedRoutingV1HttpApiClientInit {
Expand All @@ -47,23 +47,24 @@ export interface DelegatedRoutingV1HttpApiClientInit {

export interface DelegatedRoutingV1HttpApiClient {
/**
* Returns an async generator of PeerInfos that can provide the content
* for the passed CID
* Returns an async generator of {@link PeerRecord}s that can provide the
* content for the passed {@link CID}
*/
getProviders(cid: CID, options?: AbortOptions): AsyncGenerator<PeerRecord>

/**
* Returns an async generator of PeerInfos for the provided PeerId
* Returns an async generator of {@link PeerRecord}s for the provided
* {@link PeerId}
*/
getPeerInfo(peerId: PeerId, options?: AbortOptions): AsyncGenerator<PeerRecord>
getPeers(peerId: PeerId, options?: AbortOptions): AsyncGenerator<PeerRecord>

/**
* Returns a promise of a IPNSRecord for the given PeerId
* Returns a promise of a {@link IPNSRecord} for the given {@link PeerId}
*/
getIPNS(peerId: PeerId, options?: AbortOptions): Promise<IPNSRecord>

/**
* Publishes the given IPNSRecord for the provided PeerId
* Publishes the given {@link IPNSRecord} for the provided {@link PeerId}
*/
putIPNS(peerId: PeerId, record: IPNSRecord, options?: AbortOptions): Promise<void>

Expand Down
2 changes: 1 addition & 1 deletion packages/client/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ describe('delegated-routing-v1-http-api-client', () => {
body: records.map(prov => JSON.stringify(prov)).join('\n')
})

const peerRecords = await all(client.getPeerInfo(peerId))
const peerRecords = await all(client.getPeers(peerId))
expect(peerRecords.map(peerRecord => ({
...peerRecord,
ID: peerRecord.ID.toString(),
Expand Down
2 changes: 1 addition & 1 deletion packages/interop/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ describe('delegated-routing-v1-http-api interop', () => {
})

it('should find peer info', async () => {
const result = await first(client.getPeerInfo(network[2].libp2p.peerId))
const result = await first(client.getPeers(network[2].libp2p.peerId))

if (result == null) {
throw new Error('PeerInfo not found')
Expand Down

0 comments on commit ec62768

Please sign in to comment.