/**
 * Modified from bcoin's bdb (https://github.com/bcoin-org/bdb) (MIT LICENSE).
 * Credit to the original author, Christopher Jeffrey (https://github.com/chjj).
 */ param check export const assertLen = ( check ) : void => { assert ( check , new RangeError ( 'Invalid length for database key.' ) ) ; } ; param check export const assertType = ( check ) : void => { assert ( check , new TypeError ( 'Invalid type for database key.' ) ) ; } ; param check param err export const assert = ( check , err ? ) : void => { err = err || new Error ( 'Assertion error.' ) ; if ( ! check ) { if ( Error . captureStackTrace ) { Error . captureStackTrace ( err , assert ) ; } throw err ; } } ; param id returns export const makeID = ( id ) : number => { let parsed ; if ( typeof id === 'string' ) { assert ( id . length === 1 ) ; parsed = id . charCodeAt ( 0 ) ; } else { parsed = id ; } assert ( ( parsed & 0xff ) === parsed ) ; assert ( parsed !== 0xff ) ; return parsed ; } ; param value param encoding returns export const sizeString = ( value , encoding ) : number => { return 1 + Buffer . byteLength ( value , encoding ) ; } ; param buf param offset param encoding returns export const readString = ( buf , offset , encoding ) : string => { assertLen ( offset + 1 <= buf . length ) ; assertLen ( offset + 1 + buf [ offset ] <= buf . length ) ; return buf . toString ( encoding , offset + 1 , offset + 1 + buf [ offset ] ) ; } ; param buf param value param offset param encoding returns export const writeString = ( buf , value , offset , encoding , length ? ) : number => { const size = Buffer . byteLength ( value , encoding ) ; assertType ( size <= 255 ) ; assertLen ( offset + 1 <= buf . length ) ; buf [ offset ] = size ; if ( size > 0 ) { assertType ( buf . write ( value , offset + 1 , length , encoding ) === size ) ; } return 1 + size ; } ; param buf returns export const sizeBuffer = ( buf ) : number => { return 1 + buf . length ; } ; param buf param offset returns export const readBuffer = ( buf , offset ) : Buffer => { assertLen ( offset + 1 <= buf . length ) ; assertLen ( offset + 1 + buf [ offset ] <= buf . length ) ; return buf . slice ( offset + 1 , offset + 1 + buf [ offset ] ) ; } ; param buf param value param offset returns export const writeBuffer = ( buf , value , offset ) : number => { assertLen ( value . length <= 255 ) ; assertLen ( offset + 1 <= buf . length ) ; buf [ offset ] = value . length ; assertLen ( value . copy ( buf , offset + 1 ) === value . length ) ; return 1 + value . length ; } ; param value returns export const sizeHex = ( value ) : number => { if ( Buffer . isBuffer ( value ) ) { return value . length ; } return value . length >>> 1 ; } ; param buf param value param offset returns export const writeHex = ( buf , value , offset , length ? ) : number => { if ( Buffer . isBuffer ( value ) ) { return value . copy ( buf , offset ) ; } return buf . write ( value , offset , length , 'hex' ) ; } ; export const BUFFER_MIN = Buffer . alloc ( 0 ) ; export const BUFFER_MAX = Buffer . alloc ( 255 , 0xff ) ;