Skip to content

Commit e3277a9

Browse files
authored
add max suggested block size (#481)
* add max suggested block size * fix tests
1 parent 7733e7a commit e3277a9

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

index.js

+11
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ const {
2929
const promises = Symbol.for('hypercore.promises')
3030
const inspect = Symbol.for('nodejs.util.inspect.custom')
3131

32+
// Hypercore actually does not have any notion of max/min block sizes
33+
// but we enforce 15mb to ensure smooth replication (each block is transmitted atomically)
34+
const MAX_SUGGESTED_BLOCK_SIZE = 15 * 1024 * 1024
35+
3236
module.exports = class Hypercore extends EventEmitter {
3337
constructor (storage, key, opts) {
3438
super()
@@ -134,6 +138,8 @@ module.exports = class Hypercore extends EventEmitter {
134138
indent + ')'
135139
}
136140

141+
static MAX_SUGGESTED_BLOCK_SIZE = MAX_SUGGESTED_BLOCK_SIZE
142+
137143
static key (manifest, { compat } = {}) {
138144
return compat ? manifest.signers[0].publicKey : manifestHash(createManifest(manifest))
139145
}
@@ -1024,6 +1030,11 @@ module.exports = class Hypercore extends EventEmitter {
10241030
buffers[i] = this._encode(this.valueEncoding, blocks[i])
10251031
}
10261032
}
1033+
for (const b of buffers) {
1034+
if (b.byteLength > MAX_SUGGESTED_BLOCK_SIZE) {
1035+
throw BAD_ARGUMENT('Appended block exceeds the maximum suggested block size')
1036+
}
1037+
}
10271038

10281039
return this.core.append(buffers, { keyPair, signature, preappend })
10291040
}

test/basic.js

+18
Original file line numberDiff line numberDiff line change
@@ -410,3 +410,21 @@ test('writable session on a readable only core', async function (t) {
410410
t.pass(err.code, 'SESSION_NOT_WRITABLE')
411411
}
412412
})
413+
414+
test('append above the max suggested block size', async function (t) {
415+
t.plan(1)
416+
417+
const core = new Hypercore(RAM)
418+
419+
try {
420+
await core.append(Buffer.alloc(Hypercore.MAX_SUGGESTED_BLOCK_SIZE))
421+
} catch (e) {
422+
t.fail('should not throw')
423+
}
424+
425+
try {
426+
await core.append(Buffer.alloc(Hypercore.MAX_SUGGESTED_BLOCK_SIZE + 1))
427+
} catch {
428+
t.pass('should throw')
429+
}
430+
})

0 commit comments

Comments
 (0)