Skip to content

Commit 1d49408

Browse files
Mnwaaddaleax
authored andcommitted
buffer: refactor checks for SlowBuffer creation
PR-URL: #25266 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent dd8795f commit 1d49408

1 file changed

Lines changed: 15 additions & 18 deletions

File tree

‎lib/buffer.js‎

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ const constants = Object.defineProperties({}, {
9393
});
9494

9595
Buffer.poolSize=8*1024;
96-
varpoolSize,poolOffset,allocPool;
96+
letpoolSize,poolOffset,allocPool;
9797

9898
setupBufferJS(Buffer.prototype,bindingObj);
9999

@@ -212,7 +212,7 @@ Buffer.from = function from(value, encodingOrOffset, length) {
212212
if(valueOf!==null&&valueOf!==undefined&&valueOf!==value)
213213
returnBuffer.from(valueOf,encodingOrOffset,length);
214214

215-
varb=fromObject(value);
215+
constb=fromObject(value);
216216
if(b)
217217
returnb;
218218

@@ -298,13 +298,10 @@ Buffer.allocUnsafeSlow = function allocUnsafeSlow(size) {
298298
// If --zero-fill-buffers command line argument is set, a zero-filled
299299
// buffer is returned.
300300
functionSlowBuffer(length){
301-
constlen=+length;
302-
// eslint-disable-next-line eqeqeq
303-
if(len!=length)
304-
length=0;
305-
else
306-
assertSize(len);
307-
returncreateUnsafeBuffer(len);
301+
if(typeoflength!=='number')
302+
length=+length;
303+
assertSize(length);
304+
returncreateUnsafeBuffer(length);
308305
}
309306

310307
Object.setPrototypeOf(SlowBuffer.prototype,Uint8Array.prototype);
@@ -317,7 +314,7 @@ function allocate(size) {
317314
if(size<(Buffer.poolSize>>>1)){
318315
if(size>(poolSize-poolOffset))
319316
createPool();
320-
varb=newFastBuffer(allocPool,poolOffset,size);
317+
constb=newFastBuffer(allocPool,poolOffset,size);
321318
poolOffset+=size;
322319
alignPool();
323320
returnb;
@@ -326,7 +323,7 @@ function allocate(size) {
326323
}
327324

328325
functionfromString(string,encoding){
329-
varlength;
326+
letlength;
330327
if(typeofencoding!=='string'||encoding.length===0){
331328
if(string.length===0)
332329
returnnewFastBuffer();
@@ -345,7 +342,7 @@ function fromString(string, encoding) {
345342

346343
if(length>(poolSize-poolOffset))
347344
createPool();
348-
varb=newFastBuffer(allocPool,poolOffset,length);
345+
letb=newFastBuffer(allocPool,poolOffset,length);
349346
constactual=b.write(string,encoding);
350347
if(actual!==length){
351348
// byteLength() may overestimate. That's a rare case, though.
@@ -447,7 +444,7 @@ Buffer.isEncoding = function isEncoding(encoding) {
447444
Buffer[kIsEncodingSymbol]=Buffer.isEncoding;
448445

449446
Buffer.concat=functionconcat(list,length){
450-
vari;
447+
leti;
451448
if(!Array.isArray(list)){
452449
thrownewERR_INVALID_ARG_TYPE(
453450
'list',['Array','Buffer','Uint8Array'],list);
@@ -464,10 +461,10 @@ Buffer.concat = function concat(list, length) {
464461
length=length>>>0;
465462
}
466463

467-
varbuffer=Buffer.allocUnsafe(length);
468-
varpos=0;
464+
constbuffer=Buffer.allocUnsafe(length);
465+
letpos=0;
469466
for(i=0;i<list.length;i++){
470-
varbuf=list[i];
467+
constbuf=list[i];
471468
if(!isUint8Array(buf)){
472469
// TODO(BridgeAR): This should not be of type ERR_INVALID_ARG_TYPE.
473470
// Instead, find the proper error code for this.
@@ -772,7 +769,7 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
772769
}
773770

774771
functionslowIndexOf(buffer,val,byteOffset,encoding,dir){
775-
varloweredCase=false;
772+
letloweredCase=false;
776773
for(;;){
777774
switch(encoding){
778775
case'utf8':
@@ -907,7 +904,7 @@ Buffer.prototype.write = function write(string, offset, length, encoding) {
907904
length=undefined;
908905
}
909906

910-
varremaining=this.length-offset;
907+
constremaining=this.length-offset;
911908
if(length===undefined||length>remaining)
912909
length=remaining;
913910

0 commit comments

Comments
 (0)