Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add cancelReprovide function to routing #672

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
})
}

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 @@
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 @@
}
}

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 @@
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 @@
)
}

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
Loading