This repository was archived by the owner on Jul 21, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +44
-0
lines changed
Expand file tree Collapse file tree 2 files changed +44
-0
lines changed Original file line number Diff line number Diff 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 /**
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments