Only emit block length in `download` event
MODIFY
index.js
index.js
@@ -322,14 +322,10 @@ module.exports = class Hypercore extends EventEmitter {
}
if (value) {
- if (this.encryption) {
- this.encryption.decrypt(bitfield.start, value)
- }
-
- value = value.subarray(this.padding)
+ const byteLength = value.byteLength - this.padding
for (let i = 0; i < this.sessions.length; i++) {
- this.sessions[i].emit('download', bitfield.start, value, from)
+ this.sessions[i].emit('download', bitfield.start, byteLength, from)
}
}
}
@@ -394,13 +390,12 @@ module.exports = class Hypercore extends EventEmitter {
if (this.core.bitfield.get(index)) {
block = await this.core.blocks.get(index)
- if (this.encryption) this.encryption.decrypt(index, block)
} else {
if (opts && opts.onwait) opts.onwait(index)
- // Note that the _oncoreupdate handler decrypts inplace so we should not decrypt here
block = await this.replicator.requestBlock(index)
}
+ if (this.encryption) this.encryption.decrypt(index, block)
return this._decode(encoding, block)
}

MODIFY
test/encryption.js
test/encryption.js
@@ -43,14 +43,8 @@ test('encrypted replication', async function (t) {
await a.append(['a', 'b', 'c', 'd', 'e'])
t.test('with encryption key', async function (t) {
- t.plan(10)
-
const b = await create(a.key, { encryptionKey })
- b.on('download', (i, block) => {
- t.alike(block, Buffer.from([i + /* a */ 0x61]))
- })
-
replicate(a, b, t)
const r = b.download({ start: 0, end: a.length })

Only emit block length in `download` event (#58)