Skip to content

Commit

Permalink
Clean up truncate (#369)
Browse files Browse the repository at this point in the history
  • Loading branch information
HDegroote authored Mar 5, 2024
1 parent c5bc3e2 commit e4639bd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
11 changes: 9 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,16 @@ module.exports = class Hyperdrive extends ReadyResource {
}

async truncate (version, { blobs = -1 } = {}) {
if (!this.opened) await this.ready()

if (version > this.core.length) {
throw BAD_ARGUMENT('Bad truncation length')
}

const blobsVersion = blobs === -1 ? await this.getBlobsLength(version) : blobs
const bl = await this.getBlobs()

if (version > this.core.length || blobsVersion > bl.core.length) {
if (blobsVersion > bl.core.length) {
throw BAD_ARGUMENT('Bad truncation length')
}

Expand All @@ -107,7 +113,8 @@ module.exports = class Hyperdrive extends ReadyResource {
}

async getBlobsLength (checkout) {
await this.ready()
if (!this.opened) await this.ready()

if (!checkout) checkout = this.version

const c = this.db.checkout(checkout)
Expand Down
12 changes: 12 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1473,6 +1473,18 @@ test('truncate happy path', async t => {
t.is(drive.blobs.core.fork, 1, 'sanity check on blobs fork')
})

test('truncate throws when truncating future version)', async t => {
const corestore = new Corestore(RAM.reusable())
const drive = new Hyperdrive(corestore)

await drive.put('./file', 'here')
await t.exception(
() => drive.truncate(10),
/Bad truncation length/,
'throws when truncating the future'
)
})

async function testenv (teardown) {
const corestore = new Corestore(RAM)
await corestore.ready()
Expand Down

0 comments on commit e4639bd

Please sign in to comment.