Skip to content

Commit

Permalink
feat: add cancelReprovide function to routing
Browse files Browse the repository at this point in the history
Exposes the cancel reprovide functionality of the libp2p routers.
  • Loading branch information
achingbrain committed Nov 4, 2024
1 parent dff82ec commit fd92cfb
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
13 changes: 13 additions & 0 deletions packages/interface/src/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,19 @@ export interface Routing {
*/
provide(cid: CID, options?: RoutingOptions): Promise<void>

/**
* Helia will periodically re-provide every previously provided CID. Use this
* method to no longer re-provide the passed CID.
*
* @example
*
* ```js
* // ...
* await contentRouting.cancelReprovide(cid)
* ```
*/
cancelReprovide(key: CID, options?: AbortOptions): Promise<void>

/**
* Find the providers of the passed CID.
*
Expand Down
16 changes: 10 additions & 6 deletions packages/routers/src/delegated-http-routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@ class DelegatedHTTPRouter implements Routing {
this.client = createDelegatedRoutingV1HttpApiClient(url, init)
}

async provide (cid: CID, options?: RoutingOptions | undefined): Promise<void> {
async provide (cid: CID, options?: RoutingOptions): Promise<void> {
// noop
}

async * findProviders (cid: CID<unknown, number, number, Version>, options?: RoutingOptions | undefined): AsyncIterable<Provider> {
async cancelReprovide (cid?: CID, options?: RoutingOptions): Promise<void> {
// noop
}

Check warning on line 34 in packages/routers/src/delegated-http-routing.ts

View check run for this annotation

Codecov / codecov/patch

packages/routers/src/delegated-http-routing.ts#L33-L34

Added lines #L33 - L34 were not covered by tests

async * findProviders (cid: CID<unknown, number, number, Version>, options?: RoutingOptions): AsyncIterable<Provider> {
yield * map(this.client.getProviders(cid, options), (record) => {
return {
id: record.ID,
Expand All @@ -39,7 +43,7 @@ class DelegatedHTTPRouter implements Routing {
})
}

async put (key: Uint8Array, value: Uint8Array, options?: RoutingOptions | undefined): Promise<void> {
async put (key: Uint8Array, value: Uint8Array, options?: RoutingOptions): Promise<void> {
if (!isIPNSKey(key)) {
return
}
Expand All @@ -51,7 +55,7 @@ class DelegatedHTTPRouter implements Routing {
await this.client.putIPNS(cid, record, options)
}

async get (key: Uint8Array, options?: RoutingOptions | undefined): Promise<Uint8Array> {
async get (key: Uint8Array, options?: RoutingOptions): Promise<Uint8Array> {
if (!isIPNSKey(key)) {
throw new NotFoundError('Not found')
}
Expand All @@ -74,7 +78,7 @@ class DelegatedHTTPRouter implements Routing {
}
}

async findPeer (peerId: PeerId, options?: RoutingOptions | undefined): Promise<PeerInfo> {
async findPeer (peerId: PeerId, options?: RoutingOptions): Promise<PeerInfo> {
const peer = await first(this.client.getPeers(peerId, options))

if (peer != null) {
Expand All @@ -87,7 +91,7 @@ class DelegatedHTTPRouter implements Routing {
throw new NotFoundError('Not found')
}

async * getClosestPeers (key: Uint8Array, options?: RoutingOptions | undefined): AsyncIterable<PeerInfo> {
async * getClosestPeers (key: Uint8Array, options?: RoutingOptions): AsyncIterable<PeerInfo> {
// noop
}
}
Expand Down
4 changes: 4 additions & 0 deletions packages/routers/src/libp2p-routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class Libp2pRouter implements Routing {
await this.libp2p.contentRouting.provide(cid, options)
}

async cancelReprovide (key: CID, options?: RoutingOptions): Promise<void> {
await this.libp2p.contentRouting.cancelReprovide(key, options)
}

async * findProviders (cid: CID, options?: RoutingOptions): AsyncIterable<Provider> {
yield * this.libp2p.contentRouting.findProviders(cid, options)
}
Expand Down
9 changes: 9 additions & 0 deletions packages/routers/test/libp2p-routing.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ describe('libp2p-routing', () => {
expect(contentRouting.provide.calledWith(cid, options)).to.be.true()
})

it('should call through to contentRouting.cancelReprovide', async () => {
const cid = CID.parse('bafyreidykglsfhoixmivffc5uwhcgshx4j465xwqntbmu43nb2dzqwfvae')
const options = {}

await router.cancelReprovide(cid, options)

expect(contentRouting.cancelReprovide.calledWith(cid, options)).to.be.true()
})

it('should call through to contentRouting.findProviders', async () => {
contentRouting.findProviders.returns(async function * () {}())

Expand Down
9 changes: 9 additions & 0 deletions packages/utils/src/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ export class Routing implements RoutingInterface, Startable {
)
}

async cancelReprovide (key: CID, options: AbortOptions = {}): Promise<void> {
await Promise.all(
supports(this.routers, 'cancelReprovide')
.map(async (router) => {
await router.cancelReprovide(key, options)
})
)
}

Check warning on line 125 in packages/utils/src/routing.ts

View check run for this annotation

Codecov / codecov/patch

packages/utils/src/routing.ts#L119-L125

Added lines #L119 - L125 were not covered by tests

/**
* Store the given key/value pair in the available content routings
*/
Expand Down

0 comments on commit fd92cfb

Please sign in to comment.