Skip to content
Permalink
Browse files

buffer: refactor checks for SlowBuffer creation

PR-URL: #25266
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information...
Mnwa authored and addaleax committed Dec 29, 2018
1 parent dd8795f commit 1d4940888d5f24dfade7dcbdbdab8e8598c306fe
Showing with 15 additions and 18 deletions.
  1. +15 −18 lib/buffer.js
@@ -93,7 +93,7 @@ const constants = Object.defineProperties({}, {
});

Buffer.poolSize = 8 * 1024;
var poolSize, poolOffset, allocPool;
let poolSize, poolOffset, allocPool;

setupBufferJS(Buffer.prototype, bindingObj);

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

var b = fromObject(value);
const b = fromObject(value);
if (b)
return b;

@@ -298,13 +298,10 @@ Buffer.allocUnsafeSlow = function allocUnsafeSlow(size) {
// If --zero-fill-buffers command line argument is set, a zero-filled
// buffer is returned.
function SlowBuffer(length) {
const len = +length;
// eslint-disable-next-line eqeqeq
if (len != length)
length = 0;
else
assertSize(len);
return createUnsafeBuffer(len);
if (typeof length !== 'number')
length = +length;
assertSize(length);
return createUnsafeBuffer(length);
}

Object.setPrototypeOf(SlowBuffer.prototype, Uint8Array.prototype);
@@ -317,7 +314,7 @@ function allocate(size) {
if (size < (Buffer.poolSize >>> 1)) {
if (size > (poolSize - poolOffset))
createPool();
var b = new FastBuffer(allocPool, poolOffset, size);
const b = new FastBuffer(allocPool, poolOffset, size);
poolOffset += size;
alignPool();
return b;
@@ -326,7 +323,7 @@ function allocate(size) {
}

function fromString(string, encoding) {
var length;
let length;
if (typeof encoding !== 'string' || encoding.length === 0) {
if (string.length === 0)
return new FastBuffer();
@@ -345,7 +342,7 @@ function fromString(string, encoding) {

if (length > (poolSize - poolOffset))
createPool();
var b = new FastBuffer(allocPool, poolOffset, length);
let b = new FastBuffer(allocPool, poolOffset, length);
const actual = b.write(string, encoding);
if (actual !== length) {
// byteLength() may overestimate. That's a rare case, though.
@@ -447,7 +444,7 @@ Buffer.isEncoding = function isEncoding(encoding) {
Buffer[kIsEncodingSymbol] = Buffer.isEncoding;

Buffer.concat = function concat(list, length) {
var i;
let i;
if (!Array.isArray(list)) {
throw new ERR_INVALID_ARG_TYPE(
'list', ['Array', 'Buffer', 'Uint8Array'], list);
@@ -464,10 +461,10 @@ Buffer.concat = function concat(list, length) {
length = length >>> 0;
}

var buffer = Buffer.allocUnsafe(length);
var pos = 0;
const buffer = Buffer.allocUnsafe(length);
let pos = 0;
for (i = 0; i < list.length; i++) {
var buf = list[i];
const buf = list[i];
if (!isUint8Array(buf)) {
// TODO(BridgeAR): This should not be of type ERR_INVALID_ARG_TYPE.
// Instead, find the proper error code for this.
@@ -772,7 +769,7 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
}

function slowIndexOf(buffer, val, byteOffset, encoding, dir) {
var loweredCase = false;
let loweredCase = false;
for (;;) {
switch (encoding) {
case 'utf8':
@@ -907,7 +904,7 @@ Buffer.prototype.write = function write(string, offset, length, encoding) {
length = undefined;
}

var remaining = this.length - offset;
const remaining = this.length - offset;
if (length === undefined || length > remaining)
length = remaining;

0 comments on commit 1d49408

Please sign in to comment.
You can’t perform that action at this time.