import { Readable } from "stream" ; import { bufToStream } from "./bufToStream" ; /**
 * Wraps a readable stream and provides utilities to assist with reading values
 * off the stream. If there is an attempt to read past the stream, an exception
 * will thrown.
 */ export class StreamReader { param hex public static fromHex ( hex ) { return StreamReader . fromBuffer ( Buffer . from ( hex , "hex" ) ) ; } param buf public static fromBuffer ( buf ) { return new StreamReader ( bufToStream ( buf ) ) ; } constructor ( readonly stream ) { } /**
     * Reads a VarInt from Bitcoin which is an unsigned integer from
     * 0 to 2^64-1 occupying 1 to 9 bytes. The first byte indicates the variable
     * length and numbers read in little-endian.
     *  < 0xfd   8-bit number
     *    0xfd:  16-bit LE number (3 bytes consumed)
     *    0xfe:  32-bit LE number (5 bytes consumed)
     *    0xff:  64-bit LE number (9 bytes consumed)
     */ public readVarInt ( ) { const byte = this . readUInt8 ( ) ; if ( byte < 0xfd ) return BigInt ( byte ) ; if ( byte === 0xfd ) return BigInt ( this . readUInt16LE ( ) ) ; if ( byte === 0xfe ) return BigInt ( this . readUInt32LE ( ) ) ; if ( byte === 0xff ) return this . readBigUInt64LE ( ) ; } /**
     * Reads a BigSize from BOLT specs which is an unsigned integer from
     * 0 to 2^64-1 occupying 1 to 9 bytes. The first byte indicates the variable
     * length and numbers read in big-endian.
     *  < 0xfd   8-bit number
     *    0xfd:  16-bit BE number (3 bytes consumed)
     *    0xfe:  32-bit BE number (5 bytes consumed)
     *    0xff:  64-bit BE number (9 bytes consumed)
     */ public readBigSize ( ) { const byte = this . readUInt8 ( ) ; if ( byte < 0xfd ) return BigInt ( byte ) ; if ( byte === 0xfd ) return BigInt ( this . readUInt16BE ( ) ) ; if ( byte === 0xfe ) return BigInt ( this . readUInt32BE ( ) ) ; if ( byte === 0xff ) return BigInt ( this . readBigUInt64BE ( ) ) ; } remarks param n public readBytes ( n ? ) { const bytes = this . stream . read ( n ) as Buffer ; if ( n === 0 ) return Buffer . alloc ( 0 ) ; if ( n !== undefined ) this . _assertLen ( bytes , n ) ; if ( n === undefined && bytes === null ) return Buffer . alloc ( 0 ) ; return bytes ; } param n public readUIntLE ( n ) { const byte = this . readBytes ( n ) ; return byte . readUIntLE ( 0 , n ) ; } param n public readUIntBE ( n ) { const byte = this . readBytes ( n ) ; return byte . readUIntBE ( 0 , n ) ; } /**
     * Reads a UInt8 from the stream
     */ public readUInt8 ( ) { const byte = this . readBytes ( 1 ) ; return byte [ 0 ] ; } /**
     * Reads a UInt16 in little-endian from the stream
     */ public readUInt16LE ( ) { const bytes = this . readBytes ( 2 ) ; return bytes . readUInt16LE ( 0 ) ; } /**
     * Reads a UInt16 in big-endian from the stream
     */ public readUInt16BE ( ) { const bytes = this . readBytes ( 2 ) ; return bytes . readUInt16BE ( 0 ) ; } /**
     * Reads a UInt32 in little-endian from the stream
     */ public readUInt32LE ( ) { const bytes = this . readBytes ( 4 ) ; return bytes . readUInt32LE ( 0 ) ; } /**
     * Reads a UInt32 in big-endian from the stream
     */ public readUInt32BE ( ) { const bytes = this . readBytes ( 4 ) ; return bytes . readUInt32BE ( 0 ) ; } /**
     * Reads a UInt64 in little-endian from the stream
     */ public readBigUInt64LE ( ) { const bytes = this . readBytes ( 8 ) ; return bytes . readBigUInt64LE ( 0 ) ; } /**
     * Reads a UInt64 in big-endian from the stream
     */ public readBigUInt64BE ( ) { const bytes = this . readBytes ( 8 ) ; return bytes . readBigUInt64BE ( 0 ) ; } param chunk public unshift ( chunk ) { this . stream . unshift ( chunk ) ; } private _assertLen ( val , n ) { if ( val === null || val . length !== n ) throw new RangeError ( "Out of range" ) ; } }