Return `{ length, byteLength }` object from `core.append()`
MODIFY
README.md
README.md
@@ -75,10 +75,10 @@ You can also set valueEncoding to any [abstract-encoding](https://github.com/maf
valueEncodings will be applied to individually blocks, even if you append batches. If you want to control encoding at the batch-level, you can use the `encodeBatch` option, which is a function that takes a batch and returns a binary-encoded batch. If you provide a custom valueEncoding, it will not be applied prior to `encodeBatch`.
-#### `const seq = await core.append(block)`
+#### `const { length, byteLength } = await core.append(block)`
Append a block of data (or an array of blocks) to the core.
-Returns the seq the first block was stored at.
+Returns the new length and byte length of the core.
#### `const block = await core.get(index, [options])`

MODIFY
lib/block-store.js
lib/block-store.js
@@ -51,7 +51,7 @@ module.exports = class BlockStore {
return new Promise((resolve, reject) => {
this.storage.write(offset, data, (err) => {
if (err) reject(err)
- else resolve()
+ else resolve(offset + data.byteLength)
})
})
}

MODIFY
lib/core.js
lib/core.js
@@ -295,7 +295,9 @@ module.exports = class Core {
try {
if (hooks.preappend) await hooks.preappend(values)
- if (!values.length) return this.tree.length
+ if (!values.length) {
+ return { length: this.tree.length, byteLength: this.tree.byteLength }
+ }
const batch = this.tree.batch()
for (const val of values) batch.append(val)
@@ -314,7 +316,8 @@ module.exports = class Core {
}
}
- await this._appendBlocks(values)
+ const byteLength = await this._appendBlocks(values)
+
await this.oplog.append([entry], false)
this.bitfield.setRange(batch.ancestors, batch.length - batch.ancestors, true)
@@ -329,7 +332,7 @@ module.exports = class Core {
if (this._shouldFlush()) await this._flushOplog()
- return batch.ancestors
+ return { length: batch.length, byteLength }
} finally {
this._mutex.unlock()
}

MODIFY
test/core.js
test/core.js
@@ -6,12 +6,12 @@ test('core - append', async function (t) {
const { core } = await create()
{
- const seq = await core.append([
+ const info = await core.append([
Buffer.from('hello'),
Buffer.from('world')
])
- t.is(seq, 0)
+ t.alike(info, { length: 2, byteLength: 10 })
t.is(core.tree.length, 2)
t.is(core.tree.byteLength, 10)
t.alike([
@@ -24,11 +24,11 @@ test('core - append', async function (t) {
}
{
- const seq = await core.append([
+ const info = await core.append([
Buffer.from('hej')
])
- t.is(seq, 2)
+ t.alike(info, { length: 3, byteLength: 13 })
t.is(core.tree.length, 3)
t.is(core.tree.byteLength, 13)
t.alike([

Return `{ length, byteLength }` object from `core.append()` (#127)