@@ -5,7 +5,6 @@ type BaseOptions = {
55}
66
77type BaseStringOptions = {
8- wrap ?: ( str : Buffer ) => Buffer
98 encoding ?: BufferEncoding
109}
1110
@@ -386,20 +385,19 @@ export class Decoder {
386385 /**
387386 * Reads a string from the buffer. Defaults to `utf8`.
388387 *
389- * @param options - Requires either `zeroed` or `length`.
390- * @param options.zeroed - Read until a null terminator (`0x00`) is reached.
391- * @param options.length - Bytes to read.
392- * @param options.encoding - Defaults to `utf8`.
393- * @param options.wrap - ?
388+ * @param opts - Requires either `zeroed` or `length`.
389+ * @param opts.zeroed - Read until a null terminator (`0x00`) is reached.
390+ * @param opts.length - Bytes to read.
391+ * @param opts.encoding - Defaults to `utf8`.
394392 */
395- readString ( options : StringOptions ) : string {
396- let strBuffer : Buffer
393+ readString ( opts : StringOptions ) : string {
394+ let strBuffer : Buffer | null = null
397395
398- if ( "length" in options ) {
399- strBuffer = this . readBuffer ( options )
396+ if ( "length" in opts && opts . length != null ) {
397+ strBuffer = this . readBuffer ( opts )
400398 }
401399
402- if ( "zeroed" in options ) {
400+ if ( "zeroed" in opts && opts . zeroed ) {
403401 const view = new DataView (
404402 this . #buffer. buffer ,
405403 this . #buffer. byteOffset + this . #currentOffset,
@@ -414,6 +412,10 @@ export class Decoder {
414412 this . #currentOffset += size + 1
415413 }
416414
417- return strBuffer ! . toString ( options . encoding ?? "utf8" )
415+ if ( strBuffer == null ) {
416+ throw new Error ( "You need to specify either `zeroed` or `length`." )
417+ }
418+
419+ return strBuffer . toString ( opts . encoding ?? "utf8" )
418420 }
419421}
0 commit comments