@@ -90,9 +90,11 @@ export class ByteBuffer extends Uint8Array {
9090 return Buffer . from ( this ) ;
9191 }
9292
93- public get ( type : 'string' | 'STRING' ) : string ;
94- public get ( type : DataType , signed ?: Signedness , endian ?: Endianness ) : number ;
95- public get ( type : DataType = 'byte' , signed : Signedness = 'signed' , endian : Endianness = 'be' ) : number | string {
93+ public get ( ) : number ;
94+ public get ( type : Extract < DataType , 'string' | 'STRING' > ) : string ;
95+ public get ( type : Extract < DataType , 'long' | 'LONG' > , signed ?: Signedness , endian ?: Endianness ) : bigint ;
96+ public get ( type : Exclude < DataType , 'string' | 'STRING' | 'long' | 'LONG' > , signed ?: Signedness , endian ?: Endianness ) : number ;
97+ public get ( type : DataType = 'byte' , signed : Signedness = 'signed' , endian : Endianness = 'be' ) : number | bigint | string {
9698 type = ByteBuffer . getType ( type ) ;
9799 signed = ByteBuffer . getSignage ( signed ) ;
98100 endian = ByteBuffer . getEndianness ( endian ) ;
@@ -111,18 +113,24 @@ export class ByteBuffer extends Uint8Array {
111113 const smol = type === 'long' ? 'Big' : '' ;
112114
113115 this . _readerIndex += size ;
114- const methodName = `read${ signedChar } ${ smol } Int${ bitLength } ${ suffix } ` ;
116+ const methodName = `read${ smol } ${ signedChar } Int${ bitLength } ${ suffix } ` ;
115117
116118 try {
117- return this [ methodName ] ( readerIndex ) as number ;
119+ if ( type === 'long' ) {
120+ return this [ methodName ] ( readerIndex ) as bigint ;
121+ } else {
122+ return this [ methodName ] ( readerIndex ) as number ;
123+ }
118124 } catch ( error ) {
119125 logger . error ( `Error reading ${ methodName } :` , error ) ;
120126 return null ;
121127 }
122128 }
123129 }
124130
125- public put ( value : string , type : 'string' | 'STRING' ) : ByteBuffer ;
131+ public put ( value : number ) : ByteBuffer ;
132+ public put ( value : string , type : Extract < DataType , 'string' | 'STRING' > ) : ByteBuffer ;
133+ public put ( value : bigint , type : Extract < DataType , 'long' | 'LONG' > ) : ByteBuffer ;
126134 public put ( value : number | bigint , type ?: DataType , endian ?: Endianness ) : ByteBuffer
127135 public put ( value : number | bigint | string , type : DataType = 'byte' , endian : Endianness = 'be' ) : ByteBuffer {
128136 const writerIndex = this . _writerIndex ;
@@ -132,10 +140,8 @@ export class ByteBuffer extends Uint8Array {
132140
133141 if ( type === 'smart' ) {
134142 return this . putSmart ( value as number ) ;
135- } else if ( type === 'string' ) {
136- if ( typeof value === 'string' ) {
137- return this . putString ( value ) ;
138- }
143+ } else if ( type === 'string' || typeof value === 'string' ) {
144+ return this . putString ( typeof value !== 'string' ? String ( value ) : value ) ;
139145 } else {
140146 const maxSignedLength = MAX_SIGNED_LENGTHS [ type ] ;
141147 const size = BYTE_LENGTH [ type ] ;
@@ -253,7 +259,7 @@ export class ByteBuffer extends Uint8Array {
253259 return this ;
254260 }
255261
256- public getSmart ( offset : number , signed : Signedness = 'SIGNED ' ) : number {
262+ public getSmart ( offset : number , signed : Signedness = 'signed ' ) : number {
257263 const peek = this [ offset ] ;
258264
259265 const signedString = ByteBuffer . getSignage ( signed ) ;
@@ -265,6 +271,13 @@ export class ByteBuffer extends Uint8Array {
265271 }
266272 }
267273
274+ public clone ( ) : ByteBuffer {
275+ const dataCopy = new ByteBuffer ( this . length ) ;
276+ this . copy ( dataCopy , 0 , 0 ) ;
277+ dataCopy . readerIndex = this . readerIndex ;
278+ return dataCopy ;
279+ }
280+
268281 public readUInt24BE ( offset : number ) : number {
269282 return ( ( this [ offset ] & 0xff ) << 16 ) + ( ( this [ offset + 1 ] & 0xff ) << 8 ) + ( this [ offset + 2 ] & 0xff ) ;
270283 }
0 commit comments