Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Remove duplicated code
  • Loading branch information
jahirfiquitiva authored May 30, 2019
commit baf6ed7b8aaba49bf69eaed0f639dd5e0fc12995
92 changes: 27 additions & 65 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,14 @@ function alignPool() {
let bufferWarningAlreadyEmitted = false;
let nodeModulesCheckCounter = 0;
const bufferWarning = 'Buffer() is deprecated due to security and usability ' +
'issues. Please use the Buffer.alloc(), ' +
'Buffer.allocUnsafe(), or Buffer.from() methods instead.';
'issues. Please use the Buffer.alloc(), ' +
'Buffer.allocUnsafe(), or Buffer.from() methods instead.';

function showFlaggedDeprecation() {
if (bufferWarningAlreadyEmitted ||
++nodeModulesCheckCounter > 10000 ||
(!require('internal/options').getOptionValue('--pending-deprecation') &&
isInsideNodeModules())) {
++nodeModulesCheckCounter > 10000 ||
(!require('internal/options').getOptionValue('--pending-deprecation') &&
isInsideNodeModules())) {
// We don't emit a warning, because we either:
// - Already did so, or
// - Already checked too many times whether a call is coming
Expand Down Expand Up @@ -211,8 +211,8 @@ Buffer.from = function from(value, encodingOrOffset, length) {

if (typeof value[Symbol.toPrimitive] === 'function') {
return Buffer.from(value[Symbol.toPrimitive]('string'),
encodingOrOffset,
length);
encodingOrOffset,
length);
}
}

Expand Down Expand Up @@ -425,7 +425,7 @@ Buffer.compare = function compare(buf1, buf2) {

Buffer.isEncoding = function isEncoding(encoding) {
return typeof encoding === 'string' && encoding.length !== 0 &&
normalizeEncoding(encoding) !== undefined;
normalizeEncoding(encoding) !== undefined;
};
Buffer[kIsEncodingSymbol] = Buffer.isEncoding;

Expand Down Expand Up @@ -505,41 +505,29 @@ function byteLength(string, encoding) {
return (mustMatch ? -1 : byteLengthUtf8(string));

encoding += '';
encoding = encoding.toLowerCase();
switch (encoding.length) {
case 4:
if (encoding === 'utf8') return byteLengthUtf8(string);
if (encoding === 'ucs2') return len * 2;
encoding = encoding.toLowerCase();
if (encoding === 'utf8') return byteLengthUtf8(string);
if (encoding === 'ucs2') return len * 2;
break;
case 5:
if (encoding === 'utf-8') return byteLengthUtf8(string);
if (encoding === 'ascii') return len;
if (encoding === 'ucs-2') return len * 2;
encoding = encoding.toLowerCase();
if (encoding === 'utf-8') return byteLengthUtf8(string);
if (encoding === 'ascii') return len;
if (encoding === 'ucs-2') return len * 2;
break;
case 7:
if (encoding === 'utf16le' || encoding.toLowerCase() === 'utf16le')
return len * 2;
if (encoding === 'utf16le') return len * 2;
break;
case 8:
if (encoding === 'utf-16le' || encoding.toLowerCase() === 'utf-16le')
return len * 2;
if (encoding === 'utf-16le') return len * 2;
break;
case 6:
if (encoding === 'latin1' || encoding === 'binary') return len;
if (encoding === 'base64') return base64ByteLength(string, len);
encoding = encoding.toLowerCase();
if (encoding === 'latin1' || encoding === 'binary') return len;
if (encoding === 'base64') return base64ByteLength(string, len);
break;
case 3:
if (encoding === 'hex' || encoding.toLowerCase() === 'hex')
return len >>> 1;
if (encoding === 'hex') return len >>> 1;
break;
}
return (mustMatch ? -1 : byteLengthUtf8(string));
Expand Down Expand Up @@ -568,43 +556,30 @@ Object.defineProperty(Buffer.prototype, 'offset', {
function stringSlice(buf, encoding, start, end) {
if (encoding === undefined) return buf.utf8Slice(start, end);
encoding += '';
encoding = encoding.toLowerCase();
switch (encoding.length) {
case 4:
if (encoding === 'utf8') return buf.utf8Slice(start, end);
if (encoding === 'ucs2') return buf.ucs2Slice(start, end);
encoding = encoding.toLowerCase();
if (encoding === 'utf8') return buf.utf8Slice(start, end);
if (encoding === 'ucs2') return buf.ucs2Slice(start, end);
break;
case 5:
if (encoding === 'utf-8') return buf.utf8Slice(start, end);
if (encoding === 'ascii') return buf.asciiSlice(start, end);
if (encoding === 'ucs-2') return buf.ucs2Slice(start, end);
encoding = encoding.toLowerCase();
if (encoding === 'utf-8') return buf.utf8Slice(start, end);
if (encoding === 'ascii') return buf.asciiSlice(start, end);
if (encoding === 'ucs-2') return buf.ucs2Slice(start, end);
break;
case 6:
if (encoding === 'latin1' || encoding === 'binary')
return buf.latin1Slice(start, end);
if (encoding === 'base64') return buf.base64Slice(start, end);
encoding = encoding.toLowerCase();
if (encoding === 'latin1' || encoding === 'binary')
return buf.latin1Slice(start, end);
if (encoding === 'base64') return buf.base64Slice(start, end);
break;
case 3:
if (encoding === 'hex' || encoding.toLowerCase() === 'hex')
return buf.hexSlice(start, end);
if (encoding === 'hex') return buf.hexSlice(start, end);
break;
case 7:
if (encoding === 'utf16le' || encoding.toLowerCase() === 'utf16le')
return buf.ucs2Slice(start, end);
if (encoding === 'utf16le') return buf.ucs2Slice(start, end);
break;
case 8:
if (encoding === 'utf-16le' || encoding.toLowerCase() === 'utf-16le')
return buf.ucs2Slice(start, end);
if (encoding === 'utf-16le') return buf.ucs2Slice(start, end);
break;
}
throw new ERR_UNKNOWN_ENCODING(encoding);
Expand Down Expand Up @@ -689,10 +664,10 @@ Buffer.prototype[customInspectSymbol] = function inspect(recurseTimes, ctx) {
Buffer.prototype.inspect = Buffer.prototype[customInspectSymbol];

Buffer.prototype.compare = function compare(target,
targetStart,
targetEnd,
sourceStart,
sourceEnd) {
targetStart,
targetEnd,
sourceStart,
sourceEnd) {
if (!isUint8Array(target)) {
throw new ERR_INVALID_ARG_TYPE('target', ['Buffer', 'Uint8Array'], target);
}
Expand Down Expand Up @@ -725,7 +700,7 @@ Buffer.prototype.compare = function compare(target,
return 1;

return compareOffset(this, target, targetStart, sourceStart, targetEnd,
sourceEnd);
sourceEnd);
};

// Finds either the first index of `val` in `buffer` at offset >= `byteOffset`,
Expand Down Expand Up @@ -892,7 +867,7 @@ Buffer.prototype.write = function write(string, offset, length, encoding) {
length = this.length;
offset = 0;

// Buffer#write(string, offset[, length][, encoding])
// Buffer#write(string, offset[, length][, encoding])
} else {
validateInt32(offset, 'offset', 0, this.length);

Expand All @@ -914,45 +889,32 @@ Buffer.prototype.write = function write(string, offset, length, encoding) {
return this.utf8Write(string, offset, length);

encoding += '';
encoding = encoding.toLowerCase();
switch (encoding.length) {
case 4:
if (encoding === 'utf8') return this.utf8Write(string, offset, length);
if (encoding === 'ucs2') return this.ucs2Write(string, offset, length);
encoding = encoding.toLowerCase();
if (encoding === 'utf8') return this.utf8Write(string, offset, length);
if (encoding === 'ucs2') return this.ucs2Write(string, offset, length);
break;
case 5:
if (encoding === 'utf-8') return this.utf8Write(string, offset, length);
if (encoding === 'ascii') return this.asciiWrite(string, offset, length);
if (encoding === 'ucs-2') return this.ucs2Write(string, offset, length);
encoding = encoding.toLowerCase();
if (encoding === 'utf-8') return this.utf8Write(string, offset, length);
if (encoding === 'ascii') return this.asciiWrite(string, offset, length);
if (encoding === 'ucs-2') return this.ucs2Write(string, offset, length);
break;
case 7:
if (encoding === 'utf16le' || encoding.toLowerCase() === 'utf16le')
return this.ucs2Write(string, offset, length);
if (encoding === 'utf16le') return this.ucs2Write(string, offset, length);
break;
case 8:
if (encoding === 'utf-16le' || encoding.toLowerCase() === 'utf-16le')
if (encoding === 'utf-16le')
return this.ucs2Write(string, offset, length);
break;
case 6:
if (encoding === 'latin1' || encoding === 'binary')
return this.latin1Write(string, offset, length);
if (encoding === 'base64')
return this.base64Write(string, offset, length);
encoding = encoding.toLowerCase();
if (encoding === 'latin1' || encoding === 'binary')
return this.latin1Write(string, offset, length);
if (encoding === 'base64')
return this.base64Write(string, offset, length);
break;
case 3:
if (encoding === 'hex' || encoding.toLowerCase() === 'hex')
return this.hexWrite(string, offset, length);
if (encoding === 'hex') return this.hexWrite(string, offset, length);
break;
}
throw new ERR_UNKNOWN_ENCODING(encoding);
Expand Down Expand Up @@ -1064,7 +1026,7 @@ if (internalBinding('config').hasIntl) {
transcode = function transcode(source, fromEncoding, toEncoding) {
if (!isUint8Array(source)) {
throw new ERR_INVALID_ARG_TYPE('source',
['Buffer', 'Uint8Array'], source);
['Buffer', 'Uint8Array'], source);
}
if (source.length === 0) return Buffer.alloc(0);

Expand Down