Skip to content

Commit 73786c8

Browse files
cjihrigBridgeAR
authored andcommitted
buffer: remove checkNumberType()
checkNumberType() was a very thin wrapper around validateNumber(). This commit removes checkNumberType() and used validateNumber() directly instead. PR-URL: #24815 Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 1ea01c5 commit 73786c8

File tree

1 file changed

+29
-33
lines changed

1 file changed

+29
-33
lines changed

lib/internal/buffer.js

+29-33
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ float32Array[0] = -1; // 0xBF800000
2626
const bigEndian = uInt8Float32Array[3] === 0;
2727

2828
function checkBounds(buf, offset, byteLength) {
29-
checkNumberType(offset);
29+
validateNumber(offset, 'offset');
3030
if (buf[offset] === undefined || buf[offset + byteLength] === undefined)
3131
boundsError(offset, buf.length - (byteLength + 1));
3232
}
@@ -38,13 +38,9 @@ function checkInt(value, min, max, buf, offset, byteLength) {
3838
checkBounds(buf, offset, byteLength);
3939
}
4040

41-
function checkNumberType(value, type) {
42-
validateNumber(value, type || 'offset');
43-
}
44-
4541
function boundsError(value, length, type) {
4642
if (Math.floor(value) !== value) {
47-
checkNumberType(value, type);
43+
validateNumber(value, type);
4844
throw new ERR_OUT_OF_RANGE(type || 'offset', 'an integer', value);
4945
}
5046

@@ -77,7 +73,7 @@ function readUIntLE(offset, byteLength) {
7773
}
7874

7975
function readUInt48LE(buf, offset = 0) {
80-
checkNumberType(offset);
76+
validateNumber(offset, 'offset');
8177
const first = buf[offset];
8278
const last = buf[offset + 5];
8379
if (first === undefined || last === undefined)
@@ -91,7 +87,7 @@ function readUInt48LE(buf, offset = 0) {
9187
}
9288

9389
function readUInt40LE(buf, offset = 0) {
94-
checkNumberType(offset);
90+
validateNumber(offset, 'offset');
9591
const first = buf[offset];
9692
const last = buf[offset + 4];
9793
if (first === undefined || last === undefined)
@@ -105,7 +101,7 @@ function readUInt40LE(buf, offset = 0) {
105101
}
106102

107103
function readUInt32LE(offset = 0) {
108-
checkNumberType(offset);
104+
validateNumber(offset, 'offset');
109105
const first = this[offset];
110106
const last = this[offset + 3];
111107
if (first === undefined || last === undefined)
@@ -118,7 +114,7 @@ function readUInt32LE(offset = 0) {
118114
}
119115

120116
function readUInt24LE(buf, offset = 0) {
121-
checkNumberType(offset);
117+
validateNumber(offset, 'offset');
122118
const first = buf[offset];
123119
const last = buf[offset + 2];
124120
if (first === undefined || last === undefined)
@@ -128,7 +124,7 @@ function readUInt24LE(buf, offset = 0) {
128124
}
129125

130126
function readUInt16LE(offset = 0) {
131-
checkNumberType(offset);
127+
validateNumber(offset, 'offset');
132128
const first = this[offset];
133129
const last = this[offset + 1];
134130
if (first === undefined || last === undefined)
@@ -138,7 +134,7 @@ function readUInt16LE(offset = 0) {
138134
}
139135

140136
function readUInt8(offset = 0) {
141-
checkNumberType(offset);
137+
validateNumber(offset, 'offset');
142138
const val = this[offset];
143139
if (val === undefined)
144140
boundsError(offset, this.length - 1);
@@ -166,7 +162,7 @@ function readUIntBE(offset, byteLength) {
166162
}
167163

168164
function readUInt48BE(buf, offset = 0) {
169-
checkNumberType(offset);
165+
validateNumber(offset, 'offset');
170166
const first = buf[offset];
171167
const last = buf[offset + 5];
172168
if (first === undefined || last === undefined)
@@ -180,7 +176,7 @@ function readUInt48BE(buf, offset = 0) {
180176
}
181177

182178
function readUInt40BE(buf, offset = 0) {
183-
checkNumberType(offset);
179+
validateNumber(offset, 'offset');
184180
const first = buf[offset];
185181
const last = buf[offset + 4];
186182
if (first === undefined || last === undefined)
@@ -194,7 +190,7 @@ function readUInt40BE(buf, offset = 0) {
194190
}
195191

196192
function readUInt32BE(offset = 0) {
197-
checkNumberType(offset);
193+
validateNumber(offset, 'offset');
198194
const first = this[offset];
199195
const last = this[offset + 3];
200196
if (first === undefined || last === undefined)
@@ -207,7 +203,7 @@ function readUInt32BE(offset = 0) {
207203
}
208204

209205
function readUInt24BE(buf, offset = 0) {
210-
checkNumberType(offset);
206+
validateNumber(offset, 'offset');
211207
const first = buf[offset];
212208
const last = buf[offset + 2];
213209
if (first === undefined || last === undefined)
@@ -217,7 +213,7 @@ function readUInt24BE(buf, offset = 0) {
217213
}
218214

219215
function readUInt16BE(offset = 0) {
220-
checkNumberType(offset);
216+
validateNumber(offset, 'offset');
221217
const first = this[offset];
222218
const last = this[offset + 1];
223219
if (first === undefined || last === undefined)
@@ -246,7 +242,7 @@ function readIntLE(offset, byteLength) {
246242
}
247243

248244
function readInt48LE(buf, offset = 0) {
249-
checkNumberType(offset);
245+
validateNumber(offset, 'offset');
250246
const first = buf[offset];
251247
const last = buf[offset + 5];
252248
if (first === undefined || last === undefined)
@@ -261,7 +257,7 @@ function readInt48LE(buf, offset = 0) {
261257
}
262258

263259
function readInt40LE(buf, offset = 0) {
264-
checkNumberType(offset);
260+
validateNumber(offset, 'offset');
265261
const first = buf[offset];
266262
const last = buf[offset + 4];
267263
if (first === undefined || last === undefined)
@@ -275,7 +271,7 @@ function readInt40LE(buf, offset = 0) {
275271
}
276272

277273
function readInt32LE(offset = 0) {
278-
checkNumberType(offset);
274+
validateNumber(offset, 'offset');
279275
const first = this[offset];
280276
const last = this[offset + 3];
281277
if (first === undefined || last === undefined)
@@ -288,7 +284,7 @@ function readInt32LE(offset = 0) {
288284
}
289285

290286
function readInt24LE(buf, offset = 0) {
291-
checkNumberType(offset);
287+
validateNumber(offset, 'offset');
292288
const first = buf[offset];
293289
const last = buf[offset + 2];
294290
if (first === undefined || last === undefined)
@@ -299,7 +295,7 @@ function readInt24LE(buf, offset = 0) {
299295
}
300296

301297
function readInt16LE(offset = 0) {
302-
checkNumberType(offset);
298+
validateNumber(offset, 'offset');
303299
const first = this[offset];
304300
const last = this[offset + 1];
305301
if (first === undefined || last === undefined)
@@ -310,7 +306,7 @@ function readInt16LE(offset = 0) {
310306
}
311307

312308
function readInt8(offset = 0) {
313-
checkNumberType(offset);
309+
validateNumber(offset, 'offset');
314310
const val = this[offset];
315311
if (val === undefined)
316312
boundsError(offset, this.length - 1);
@@ -338,7 +334,7 @@ function readIntBE(offset, byteLength) {
338334
}
339335

340336
function readInt48BE(buf, offset = 0) {
341-
checkNumberType(offset);
337+
validateNumber(offset, 'offset');
342338
const first = buf[offset];
343339
const last = buf[offset + 5];
344340
if (first === undefined || last === undefined)
@@ -353,7 +349,7 @@ function readInt48BE(buf, offset = 0) {
353349
}
354350

355351
function readInt40BE(buf, offset = 0) {
356-
checkNumberType(offset);
352+
validateNumber(offset, 'offset');
357353
const first = buf[offset];
358354
const last = buf[offset + 4];
359355
if (first === undefined || last === undefined)
@@ -367,7 +363,7 @@ function readInt40BE(buf, offset = 0) {
367363
}
368364

369365
function readInt32BE(offset = 0) {
370-
checkNumberType(offset);
366+
validateNumber(offset, 'offset');
371367
const first = this[offset];
372368
const last = this[offset + 3];
373369
if (first === undefined || last === undefined)
@@ -380,7 +376,7 @@ function readInt32BE(offset = 0) {
380376
}
381377

382378
function readInt24BE(buf, offset = 0) {
383-
checkNumberType(offset);
379+
validateNumber(offset, 'offset');
384380
const first = buf[offset];
385381
const last = buf[offset + 2];
386382
if (first === undefined || last === undefined)
@@ -391,7 +387,7 @@ function readInt24BE(buf, offset = 0) {
391387
}
392388

393389
function readInt16BE(offset = 0) {
394-
checkNumberType(offset);
390+
validateNumber(offset, 'offset');
395391
const first = this[offset];
396392
const last = this[offset + 1];
397393
if (first === undefined || last === undefined)
@@ -403,7 +399,7 @@ function readInt16BE(offset = 0) {
403399

404400
// Read floats
405401
function readFloatBackwards(offset = 0) {
406-
checkNumberType(offset);
402+
validateNumber(offset, 'offset');
407403
const first = this[offset];
408404
const last = this[offset + 3];
409405
if (first === undefined || last === undefined)
@@ -417,7 +413,7 @@ function readFloatBackwards(offset = 0) {
417413
}
418414

419415
function readFloatForwards(offset = 0) {
420-
checkNumberType(offset);
416+
validateNumber(offset, 'offset');
421417
const first = this[offset];
422418
const last = this[offset + 3];
423419
if (first === undefined || last === undefined)
@@ -431,7 +427,7 @@ function readFloatForwards(offset = 0) {
431427
}
432428

433429
function readDoubleBackwards(offset = 0) {
434-
checkNumberType(offset);
430+
validateNumber(offset, 'offset');
435431
const first = this[offset];
436432
const last = this[offset + 7];
437433
if (first === undefined || last === undefined)
@@ -449,7 +445,7 @@ function readDoubleBackwards(offset = 0) {
449445
}
450446

451447
function readDoubleForwards(offset = 0) {
452-
checkNumberType(offset);
448+
validateNumber(offset, 'offset');
453449
const first = this[offset];
454450
const last = this[offset + 7];
455451
if (first === undefined || last === undefined)
@@ -563,7 +559,7 @@ function writeUInt16LE(value, offset = 0) {
563559
function writeU_Int8(buf, value, offset, min, max) {
564560
value = +value;
565561
// `checkInt()` can not be used here because it checks two entries.
566-
checkNumberType(offset);
562+
validateNumber(offset, 'offset');
567563
if (value > max || value < min) {
568564
throw new ERR_OUT_OF_RANGE('value', `>= ${min} and <= ${max}`, value);
569565
}

0 commit comments

Comments
 (0)