@@ -20,6 +20,7 @@ const Batch = require('./lib/batch')
20
20
const { manifestHash, defaultSignerManifest, createManifest, isCompat, sign } = require ( './lib/verifier' )
21
21
const { ReadStream, WriteStream, ByteStream } = require ( './lib/streams' )
22
22
const {
23
+ ASSERTION ,
23
24
BAD_ARGUMENT ,
24
25
SESSION_CLOSED ,
25
26
SESSION_NOT_WRITABLE ,
@@ -825,6 +826,7 @@ module.exports = class Hypercore extends EventEmitter {
825
826
826
827
async seek ( bytes , opts ) {
827
828
if ( this . opened === false ) await this . opening
829
+ if ( ! isValidIndex ( bytes ) ) throw ASSERTION ( 'seek is invalid' )
828
830
829
831
const tree = ( opts && opts . tree ) || this . core . tree
830
832
const s = tree . seek ( bytes , this . padding )
@@ -847,17 +849,17 @@ module.exports = class Hypercore extends EventEmitter {
847
849
848
850
async has ( start , end = start + 1 ) {
849
851
if ( this . opened === false ) await this . opening
852
+ if ( ! isValidIndex ( start ) || ! isValidIndex ( end ) ) throw ASSERTION ( 'has range is invalid' )
850
853
851
- const length = end - start
852
- if ( length <= 0 ) return false
853
- if ( length === 1 ) return this . core . bitfield . get ( start )
854
+ if ( end === start + 1 ) return this . core . bitfield . get ( start )
854
855
855
856
const i = this . core . bitfield . firstUnset ( start )
856
857
return i === - 1 || i >= end
857
858
}
858
859
859
860
async get ( index , opts ) {
860
861
if ( this . opened === false ) await this . opening
862
+ if ( ! isValidIndex ( index ) ) throw ASSERTION ( 'block index is invalid' )
861
863
862
864
this . tracer . trace ( 'get' , { index } )
863
865
@@ -891,6 +893,8 @@ module.exports = class Hypercore extends EventEmitter {
891
893
end = start + 1
892
894
}
893
895
896
+ if ( ! isValidIndex ( start ) || ! isValidIndex ( end ) ) throw ASSERTION ( 'clear range is invalid' )
897
+
894
898
const cleared = ( opts && opts . diff ) ? { blocks : 0 } : null
895
899
896
900
if ( start >= end ) return cleared
@@ -1156,3 +1160,7 @@ function ensureEncryption (core, opts) {
1156
1160
function createCache ( cache ) {
1157
1161
return cache === true ? new Xache ( { maxSize : 65536 , maxAge : 0 } ) : ( cache || null )
1158
1162
}
1163
+
1164
+ function isValidIndex ( index ) {
1165
+ return index === 0 || index > 0
1166
+ }
0 commit comments