Skip to content
This repository was archived by the owner on Jul 21, 2023. It is now read-only.

Commit d0db16b

Browse files
authoredDec 16, 2020
feat: adds removeLocal function (#211)
1 parent ba029ca commit d0db16b

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed
 

‎src/index.js

+19
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,25 @@ class KadDHT extends EventEmitter {
273273
return this.contentFetching.getMany(key, nvals, options)
274274
}
275275

276+
/**
277+
* Remove the given key from the local datastore.
278+
* @param {Uint8Array} key
279+
* @returns {Promise<void>}
280+
*/
281+
async removeLocal (key) {
282+
this._log('removeLocal: %b', key)
283+
const dsKey = utils.bufferToKey(key)
284+
285+
try {
286+
await this.datastore.delete(dsKey)
287+
} catch (err) {
288+
if (err.code === 'ERR_NOT_FOUND') {
289+
return undefined
290+
}
291+
throw err
292+
}
293+
}
294+
276295
// ----------- Content Routing
277296

278297
/**

‎test/kad-dht.spec.js

+25
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,31 @@ describe('KadDHT', () => {
186186
return tdht.teardown()
187187
})
188188

189+
it('put - removeLocal', async function () {
190+
this.timeout(10 * 1000)
191+
192+
const tdht = new TestDHT()
193+
const key = uint8ArrayFromString('/v/hello')
194+
const value = uint8ArrayFromString('world')
195+
196+
const [dht] = await tdht.spawn(2)
197+
198+
await dht.put(key, value)
199+
200+
const res = await dht.get(uint8ArrayFromString('/v/hello'), { timeout: 1000 })
201+
expect(res).to.eql(value)
202+
203+
// remove from the local datastore
204+
await dht.removeLocal(key)
205+
try {
206+
await dht.datastore.get(key)
207+
} catch (err) {
208+
expect(err).to.exist()
209+
expect(err.code).to.be.eql('ERR_NOT_FOUND')
210+
return tdht.teardown()
211+
}
212+
})
213+
189214
it('put - get', async function () {
190215
this.timeout(10 * 1000)
191216

0 commit comments

Comments
 (0)
This repository has been archived.