Skip to content

Commit ad9eb80

Browse files
add raw flag to batch and hypercore get
1 parent eb63ee2 commit ad9eb80

File tree

3 files changed

+10
-17
lines changed

3 files changed

+10
-17
lines changed

index.js

+2
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,8 @@ module.exports = class Hypercore extends EventEmitter {
875875
let block = await req
876876
if (!block) return null
877877

878+
if (opts && opts.raw) return block
879+
878880
if (this.encryption && (!opts || opts.decrypt !== false)) {
879881
// Copy the block as it might be shared with other sessions.
880882
block = b4a.from(block)

lib/batch.js

+7-16
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,17 @@ module.exports = class HypercoreBatch extends EventEmitter {
170170
throw BLOCK_NOT_AVAILABLE()
171171
}
172172

173-
async get (index, opts) {
173+
async get (index, opts = {}) {
174174
if (this.opened === false) await this.opening
175175
if (this.closing) throw SESSION_CLOSED()
176176

177177
const length = this._sessionLength
178-
if (index < length) return this.session.get(index, { ...opts, tree: this._sessionBatch })
178+
179+
if (index < length) {
180+
return this.session.get(index, { ...opts, tree: this._sessionBatch })
181+
} else if (opts && opts.raw) {
182+
return this._appendsActual[index - length] || null
183+
}
179184

180185
const buffer = this._appends[index - length] || null
181186
if (!buffer) throw BLOCK_NOT_AVAILABLE()
@@ -186,20 +191,6 @@ module.exports = class HypercoreBatch extends EventEmitter {
186191
return c.decode(encoding, buffer)
187192
}
188193

189-
async getRaw (index, opts) {
190-
if (this.opened === false) await this.opening
191-
if (this.closing) throw SESSION_CLOSED()
192-
193-
const length = this._sessionLength
194-
if (index < length) {
195-
const block = await this.session.get(index, { ...opts, tree: this._sessionBatch })
196-
if (!this.session.encryption) return block
197-
return this._encrypt(index, block)
198-
}
199-
200-
return this._appendsActual[index - length] || null
201-
}
202-
203194
async _waitForFlush () {
204195
// wait for any pending flush...
205196
while (this._flushing) {

test/batch.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ test('copy from with encrypted batch', async function (t) {
678678
const tree = clone.core.tree.batch()
679679

680680
for (let i = 0; i < blocks; i++) {
681-
await tree.append(await b.getRaw(i))
681+
await tree.append(await b.get(i, { raw: true }))
682682
}
683683

684684
t.alike(tree.hash(), manifest.prologue.hash)

0 commit comments

Comments
 (0)