Skip to content

Commit ec62768

Browse files
authored
fix: PeerRecord Addrs and Protocols do not need to be optional (#43)
We ensure that the `.Addrs` and `.Protocols` properties are present so they don't need to be marked optional in the interface.
1 parent 3fcc332 commit ec62768

File tree

5 files changed

+15
-14
lines changed

5 files changed

+15
-14
lines changed

packages/client/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@
138138
"browser-readablestream-to-it": "^2.0.3",
139139
"ipns": "^7.0.1",
140140
"it-all": "^3.0.2",
141-
"iterable-ndjson": "^1.1.0",
141+
"it-ndjson": "^1.0.4",
142142
"multiformats": "^12.1.1",
143143
"p-defer": "^4.0.0",
144144
"p-queue": "^7.3.4"

packages/client/src/client.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import { anySignal } from 'any-signal'
66
import toIt from 'browser-readablestream-to-it'
77
import { unmarshal, type IPNSRecord, marshal, peerIdToRoutingKey } from 'ipns'
88
import { ipnsValidator } from 'ipns/validator'
9-
// @ts-expect-error no types
10-
import ndjson from 'iterable-ndjson'
9+
import { parse as ndjson } from 'it-ndjson'
1110
import defer from 'p-defer'
1211
import PQueue from 'p-queue'
1312
import type { DelegatedRoutingV1HttpApiClient, DelegatedRoutingV1HttpApiClientInit, PeerRecord } from './index.js'
@@ -107,7 +106,7 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV
107106
}
108107
}
109108

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

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

packages/client/src/index.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ import type { CID } from 'multiformats/cid'
2727
export interface PeerRecord {
2828
Schema: 'peer'
2929
ID: PeerId
30-
Addrs?: Multiaddr[]
31-
Protocols?: string[]
30+
Addrs: Multiaddr[]
31+
Protocols: string[]
3232
}
3333

3434
export interface DelegatedRoutingV1HttpApiClientInit {
@@ -47,23 +47,24 @@ export interface DelegatedRoutingV1HttpApiClientInit {
4747

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

5555
/**
56-
* Returns an async generator of PeerInfos for the provided PeerId
56+
* Returns an async generator of {@link PeerRecord}s for the provided
57+
* {@link PeerId}
5758
*/
58-
getPeerInfo(peerId: PeerId, options?: AbortOptions): AsyncGenerator<PeerRecord>
59+
getPeers(peerId: PeerId, options?: AbortOptions): AsyncGenerator<PeerRecord>
5960

6061
/**
61-
* Returns a promise of a IPNSRecord for the given PeerId
62+
* Returns a promise of a {@link IPNSRecord} for the given {@link PeerId}
6263
*/
6364
getIPNS(peerId: PeerId, options?: AbortOptions): Promise<IPNSRecord>
6465

6566
/**
66-
* Publishes the given IPNSRecord for the provided PeerId
67+
* Publishes the given {@link IPNSRecord} for the provided {@link PeerId}
6768
*/
6869
putIPNS(peerId: PeerId, record: IPNSRecord, options?: AbortOptions): Promise<void>
6970

packages/client/test/index.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ describe('delegated-routing-v1-http-api-client', () => {
143143
body: records.map(prov => JSON.stringify(prov)).join('\n')
144144
})
145145

146-
const peerRecords = await all(client.getPeerInfo(peerId))
146+
const peerRecords = await all(client.getPeers(peerId))
147147
expect(peerRecords.map(peerRecord => ({
148148
...peerRecord,
149149
ID: peerRecord.ID.toString(),

packages/interop/test/index.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ describe('delegated-routing-v1-http-api interop', () => {
7979
})
8080

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

8484
if (result == null) {
8585
throw new Error('PeerInfo not found')

0 commit comments

Comments
 (0)