@@ -27,7 +27,7 @@ export interface DynamicBufferOptions {
2727 fill ?: string | Buffer | number ;
2828
2929 /**
30- * The initial size of the buffer, default 16.
30+ * The initial size of the buffer, and it must greater than 0, default 16.
3131 */
3232 size ?: number ;
3333}
@@ -153,31 +153,34 @@ export class DynamicBuffer {
153153 data ?: string | DynamicBufferOptions ,
154154 options ?: DynamicBufferOptions ,
155155 ) {
156- if ( typeof data !== 'string' ) {
157- // eslint-disable-next-line no-param-reassign
158- options = data ;
159- // eslint-disable-next-line no-param-reassign
160- data = undefined ;
156+ let initData : string | undefined ;
157+ let initOptions : DynamicBufferOptions | undefined ;
158+
159+ if ( typeof data === 'string' ) {
160+ initData = data ;
161+ initOptions = options ;
162+ } else {
163+ initOptions = data ;
161164 }
162165
163- if ( options ?. size !== undefined ) {
164- this . size = options . size ;
166+ if ( initOptions ?. size !== undefined ) {
167+ this . size = initOptions . size ;
165168 } else {
166169 this . size = this . DefaultInitialSize ;
167170 }
168171
169- if ( data && data . length > this . size ) {
170- this . size = data . length ;
172+ if ( initData && initData ? .length > this . size ) {
173+ this . size = initData . length ;
171174 }
172175
173176 if ( this . size < 0 || this . size > constants . MAX_LENGTH ) {
174177 throw new Error ( 'Invalid buffer size' ) ;
175178 }
176179
177180 this . used = 0 ;
178- this . fillVal = options ?. fill || 0 ;
179- this . encoding = options ?. encoding || 'utf8' ;
180- this . factor = options ?. factor || this . DefaultResizeFactor ;
181+ this . fillVal = initOptions ?. fill || 0 ;
182+ this . encoding = initOptions ?. encoding || 'utf8' ;
183+ this . factor = initOptions ?. factor || this . DefaultResizeFactor ;
181184
182185 if ( this . factor <= 0 || Number . isNaN ( this . factor ) ) {
183186 throw new Error ( 'Invalid factor' ) ;
@@ -187,8 +190,8 @@ export class DynamicBuffer {
187190 this . buffer = Buffer . alloc ( this . size , this . fillVal , this . encoding ) ;
188191 }
189192
190- if ( data ) {
191- this . append ( data ) ;
193+ if ( initData ) {
194+ this . append ( initData ) ;
192195 }
193196
194197 // eslint-disable-next-line no-constructor-return
0 commit comments