File tree 2 files changed +29
-0
lines changed
2 files changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -29,6 +29,10 @@ const {
29
29
const promises = Symbol . for ( 'hypercore.promises' )
30
30
const inspect = Symbol . for ( 'nodejs.util.inspect.custom' )
31
31
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
+
32
36
module . exports = class Hypercore extends EventEmitter {
33
37
constructor ( storage , key , opts ) {
34
38
super ( )
@@ -134,6 +138,8 @@ module.exports = class Hypercore extends EventEmitter {
134
138
indent + ')'
135
139
}
136
140
141
+ static MAX_SUGGESTED_BLOCK_SIZE = MAX_SUGGESTED_BLOCK_SIZE
142
+
137
143
static key ( manifest , { compat } = { } ) {
138
144
return compat ? manifest . signers [ 0 ] . publicKey : manifestHash ( createManifest ( manifest ) )
139
145
}
@@ -1024,6 +1030,11 @@ module.exports = class Hypercore extends EventEmitter {
1024
1030
buffers [ i ] = this . _encode ( this . valueEncoding , blocks [ i ] )
1025
1031
}
1026
1032
}
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
+ }
1027
1038
1028
1039
return this . core . append ( buffers , { keyPair, signature, preappend } )
1029
1040
}
Original file line number Diff line number Diff line change @@ -410,3 +410,21 @@ test('writable session on a readable only core', async function (t) {
410
410
t . pass ( err . code , 'SESSION_NOT_WRITABLE' )
411
411
}
412
412
} )
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
+ } )
You can’t perform that action at this time.
0 commit comments