Skip to content

Commit 90c19cd

Browse files
HDegrootemafintosh
authored andcommitted
Clear storage info (#52)
* Add storageInfo opt to drive.clear() * (failing test) Add storageInfo test * Rm test * Add readme note on storageInfo opt
1 parent cc81fb8 commit 90c19cd

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,12 @@ Returns the entry at `path` in the drive. It looks like this:
135135

136136
Deletes the file at `path` from the drive.
137137

138-
#### `await drive.clear(path)`
138+
#### `await drive.clear(path, [options])`
139139

140140
Deletes the blob from storage to free up space, but the file structure reference is kept.
141141

142+
If `storageInfo = true` is set, an estimate of the amount of bytes freed will be returned (default `false`).
143+
142144
#### `await drive.symlink(path, linkname)`
143145

144146
Creates an entry in drive at `path` that points to the entry at `linkname`.

index.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,22 @@ module.exports = class Hyperdrive extends ReadyResource {
190190
return this.files.del(normalizePath(name))
191191
}
192192

193-
async clear (name) {
193+
async clear (name, { storageInfo = false } = {}) {
194194
const node = await this.entry(name)
195195
if (node === null) return
196196

197+
const infoBefore = storageInfo
198+
? await this.blobs.core.info({ storage: true })
199+
: null
200+
197201
await this.getBlobs()
198202
await this.blobs.clear(node.value.blob)
203+
204+
if (infoBefore) {
205+
const infoAfter = await this.blobs.core.info({ storage: true })
206+
const bytesCleared = infoBefore.storage.blocks - infoAfter.storage.blocks
207+
return bytesCleared
208+
}
199209
}
200210

201211
async symlink (name, dst, { metadata = null } = {}) {

test.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,8 @@ test('drive.clear(path)', async (t) => {
749749
const initContent = await drive.blobs.get(entry.value.blob, { wait: false })
750750
t.alike(initContent, b4a.from('hello world'))
751751

752-
await drive.clear('/loc')
752+
const res = await drive.clear('/loc')
753+
t.is(res, undefined)
753754

754755
// Entry still exists (so file not deleted)
755756
const nowEntry = await drive.entry('/loc')

0 commit comments

Comments
 (0)