hypercore-storage 2.6.1 → 2.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +26 -3
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -242,7 +242,7 @@ class HypercoreStorage {
242
242
  const existingSessions = await existingSessionsPromise
243
243
 
244
244
  const sessions = existingSessions || []
245
- const session = getBatch(sessions, name, false)
245
+ const session = getSession(sessions, name, false)
246
246
 
247
247
  if (session === null) return null
248
248
 
@@ -288,7 +288,7 @@ class HypercoreStorage {
288
288
  }
289
289
 
290
290
  const sessions = existingSessions || []
291
- const session = getBatch(sessions, name, true)
291
+ const session = getSession(sessions, name, true)
292
292
  const fresh = session.dataPointer === -1
293
293
 
294
294
  if (fresh) {
@@ -374,6 +374,29 @@ class HypercoreStorage {
374
374
  return deps
375
375
  }
376
376
 
377
+ async deleteSessions() {
378
+ const rx = this.read()
379
+ const existingSessionsPromise = rx.getSessions()
380
+ rx.tryFlush()
381
+
382
+ const existingSessions = await existingSessionsPromise
383
+
384
+ // Remove batches
385
+ const coreTx = this.write()
386
+ coreTx.setSessions([]) // Clear sessions record
387
+ await coreTx.flush()
388
+
389
+ const tx = this.db.write({ autoDestroy: true })
390
+
391
+ for (const { dataPointer } of existingSessions) {
392
+ const start = core.data(dataPointer)
393
+ const end = core.data(dataPointer + 1)
394
+ tx.tryDeleteRange(start, end)
395
+ }
396
+
397
+ await tx.flush()
398
+ }
399
+
377
400
  read() {
378
401
  return new CoreRX(this.core, this.db, this.view)
379
402
  }
@@ -1096,7 +1119,7 @@ function initStoreHead() {
1096
1119
  }
1097
1120
  }
1098
1121
 
1099
- function getBatch(sessions, name, alloc) {
1122
+ function getSession(sessions, name, alloc) {
1100
1123
  for (let i = 0; i < sessions.length; i++) {
1101
1124
  if (sessions[i].name === name) return sessions[i]
1102
1125
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore-storage",
3
- "version": "2.6.1",
3
+ "version": "2.7.0",
4
4
  "main": "index.js",
5
5
  "files": [
6
6
  "index.js",