buffer: refactor checks for SlowBuffer creation

PR-URL: https://github.com/nodejs/node/pull/25266
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
P. Mike 2018-12-29 16:17:40 +03:00 committed by Rich Trott
parent 0a549aaeac
commit 728505acd5

View File

@ -102,7 +102,7 @@ const constants = Object.defineProperties({}, {
}); });
Buffer.poolSize = 8 * 1024; Buffer.poolSize = 8 * 1024;
var poolSize, poolOffset, allocPool; let poolSize, poolOffset, allocPool;
setupBufferJS(Buffer.prototype, bindingObj); setupBufferJS(Buffer.prototype, bindingObj);
@ -221,7 +221,7 @@ Buffer.from = function from(value, encodingOrOffset, length) {
if (valueOf !== null && valueOf !== undefined && valueOf !== value) if (valueOf !== null && valueOf !== undefined && valueOf !== value)
return Buffer.from(valueOf, encodingOrOffset, length); return Buffer.from(valueOf, encodingOrOffset, length);
var b = fromObject(value); const b = fromObject(value);
if (b) if (b)
return b; return b;
@ -307,13 +307,10 @@ Buffer.allocUnsafeSlow = function allocUnsafeSlow(size) {
// If --zero-fill-buffers command line argument is set, a zero-filled // If --zero-fill-buffers command line argument is set, a zero-filled
// buffer is returned. // buffer is returned.
function SlowBuffer(length) { function SlowBuffer(length) {
const len = +length; if (typeof length !== 'number')
// eslint-disable-next-line eqeqeq length = +length;
if (len != length) assertSize(length);
length = 0; return createUnsafeBuffer(length);
else
assertSize(len);
return createUnsafeBuffer(len);
} }
Object.setPrototypeOf(SlowBuffer.prototype, Uint8Array.prototype); Object.setPrototypeOf(SlowBuffer.prototype, Uint8Array.prototype);
@ -326,7 +323,7 @@ function allocate(size) {
if (size < (Buffer.poolSize >>> 1)) { if (size < (Buffer.poolSize >>> 1)) {
if (size > (poolSize - poolOffset)) if (size > (poolSize - poolOffset))
createPool(); createPool();
var b = new FastBuffer(allocPool, poolOffset, size); const b = new FastBuffer(allocPool, poolOffset, size);
poolOffset += size; poolOffset += size;
alignPool(); alignPool();
return b; return b;
@ -335,7 +332,7 @@ function allocate(size) {
} }
function fromString(string, encoding) { function fromString(string, encoding) {
var length; let length;
if (typeof encoding !== 'string' || encoding.length === 0) { if (typeof encoding !== 'string' || encoding.length === 0) {
if (string.length === 0) if (string.length === 0)
return new FastBuffer(); return new FastBuffer();
@ -354,7 +351,7 @@ function fromString(string, encoding) {
if (length > (poolSize - poolOffset)) if (length > (poolSize - poolOffset))
createPool(); createPool();
var b = new FastBuffer(allocPool, poolOffset, length); let b = new FastBuffer(allocPool, poolOffset, length);
const actual = b.write(string, encoding); const actual = b.write(string, encoding);
if (actual !== length) { if (actual !== length) {
// byteLength() may overestimate. That's a rare case, though. // byteLength() may overestimate. That's a rare case, though.
@ -456,7 +453,7 @@ Buffer.isEncoding = function isEncoding(encoding) {
Buffer[kIsEncodingSymbol] = Buffer.isEncoding; Buffer[kIsEncodingSymbol] = Buffer.isEncoding;
Buffer.concat = function concat(list, length) { Buffer.concat = function concat(list, length) {
var i; let i;
if (!Array.isArray(list)) { if (!Array.isArray(list)) {
throw new ERR_INVALID_ARG_TYPE( throw new ERR_INVALID_ARG_TYPE(
'list', ['Array', 'Buffer', 'Uint8Array'], list); 'list', ['Array', 'Buffer', 'Uint8Array'], list);
@ -473,10 +470,10 @@ Buffer.concat = function concat(list, length) {
length = length >>> 0; length = length >>> 0;
} }
var buffer = Buffer.allocUnsafe(length); const buffer = Buffer.allocUnsafe(length);
var pos = 0; let pos = 0;
for (i = 0; i < list.length; i++) { for (i = 0; i < list.length; i++) {
var buf = list[i]; const buf = list[i];
if (!isUint8Array(buf)) { if (!isUint8Array(buf)) {
// TODO(BridgeAR): This should not be of type ERR_INVALID_ARG_TYPE. // TODO(BridgeAR): This should not be of type ERR_INVALID_ARG_TYPE.
// Instead, find the proper error code for this. // Instead, find the proper error code for this.
@ -791,7 +788,7 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
} }
function slowIndexOf(buffer, val, byteOffset, encoding, dir) { function slowIndexOf(buffer, val, byteOffset, encoding, dir) {
var loweredCase = false; let loweredCase = false;
for (;;) { for (;;) {
switch (encoding) { switch (encoding) {
case 'utf8': case 'utf8':
@ -926,7 +923,7 @@ Buffer.prototype.write = function write(string, offset, length, encoding) {
length = undefined; length = undefined;
} }
var remaining = this.length - offset; const remaining = this.length - offset;
if (length === undefined || length > remaining) if (length === undefined || length > remaining)
length = remaining; length = remaining;