Skip to content

Commit ddc5cbc

Browse files
committed
buffer: cleanup handling different types buffers
1 parent e4f61de commit ddc5cbc

File tree

1 file changed

+59
-42
lines changed

1 file changed

+59
-42
lines changed

lib/buffer.js

+59-42
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,15 @@ FastBuffer.prototype.constructor = Buffer;
133133
Buffer.prototype = FastBuffer.prototype;
134134
addBufferPrototypeMethods(Buffer.prototype);
135135

136+
const BUFFER_NAMES = [
137+
'ArrayBuffer',
138+
'Buffer',
139+
'TypedArray',
140+
'DataView',
141+
'ArrayBuffer',
142+
'SharedArrayBuffer',
143+
]
144+
136145
const constants = ObjectDefineProperties({}, {
137146
MAX_LENGTH: {
138147
__proto__: null,
@@ -203,10 +212,13 @@ function toInteger(n, defaultVal) {
203212
}
204213

205214
function copyImpl(source, target, targetStart, sourceStart, sourceEnd) {
206-
if (!ArrayBufferIsView(source))
207-
throw new ERR_INVALID_ARG_TYPE('source', ['Buffer', 'Uint8Array'], source);
208-
if (!ArrayBufferIsView(target))
209-
throw new ERR_INVALID_ARG_TYPE('target', ['Buffer', 'Uint8Array'], target);
215+
if (isArrayBufferView(target)) {
216+
// Do nothing..
217+
} else if (isAnyArrayBuffer(target)) {
218+
target = new Uint8Array(target);
219+
} else {
220+
throw new ERR_INVALID_ARG_TYPE('target', BUFFER_NAMES, target);
221+
}
210222

211223
if (targetStart === undefined) {
212224
targetStart = 0;
@@ -321,25 +333,29 @@ Buffer.from = function from(value, encodingOrOffset, length) {
321333

322334
throw new ERR_INVALID_ARG_TYPE(
323335
'first argument',
324-
['string', 'Buffer', 'ArrayBuffer', 'Array', 'Array-like Object'],
336+
['string', 'Array', 'Array-like Object', ...BUFFER_NAMES],
325337
value,
326338
);
327339
};
328340

329341
/**
330342
* Creates the Buffer as a copy of the underlying ArrayBuffer of the view
331343
* rather than the contents of the view.
332-
* @param {TypedArray} view
344+
* @param {Buffer|TypedArray|DataView} view
333345
* @param {number} [offset]
334346
* @param {number} [length]
335347
* @returns {Buffer}
336348
*/
337349
Buffer.copyBytesFrom = function copyBytesFrom(view, offset, length) {
338-
if (!isTypedArray(view)) {
339-
throw new ERR_INVALID_ARG_TYPE('view', [ 'TypedArray' ], view);
350+
if (isArrayBufferView(view)) {
351+
// Do nothing..
352+
} else if (isAnyArrayBuffer(view)) {
353+
view = new Uint8Array(view);
354+
} else {
355+
throw new ERR_INVALID_ARG_TYPE('view', BUFFER_NAMES, view);
340356
}
341357

342-
const viewLength = TypedArrayPrototypeGetLength(view);
358+
const viewLength = view.byteLength;
343359
if (viewLength === 0) {
344360
return Buffer.alloc(0);
345361
}
@@ -351,21 +367,17 @@ Buffer.copyBytesFrom = function copyBytesFrom(view, offset, length) {
351367
} else {
352368
offset = 0;
353369
}
354-
let end;
355370
if (length !== undefined) {
356371
validateInteger(length, 'length', 0);
357-
end = offset + length;
358372
} else {
359-
end = viewLength;
373+
length = viewLength;
360374
}
361-
362-
view = TypedArrayPrototypeSlice(view, offset, end);
363375
}
364376

365377
return fromArrayLike(new Uint8Array(
366-
TypedArrayPrototypeGetBuffer(view),
367-
TypedArrayPrototypeGetByteOffset(view),
368-
TypedArrayPrototypeGetByteLength(view)));
378+
view.buffer,
379+
view.byteOffset,
380+
length));
369381
};
370382

371383
// Identical to the built-in %TypedArray%.of(), but avoids using the deprecated
@@ -583,8 +595,8 @@ Buffer.concat = function concat(list, length) {
583595
if (length === undefined) {
584596
length = 0;
585597
for (let i = 0; i < list.length; i++) {
586-
if (list[i].length) {
587-
length += list[i].length;
598+
if (list[i].byteLength) {
599+
length += list[i].byteLength;
588600
}
589601
}
590602
} else {
@@ -595,13 +607,13 @@ Buffer.concat = function concat(list, length) {
595607
let pos = 0;
596608
for (let i = 0; i < list.length; i++) {
597609
const buf = list[i];
598-
if (!isUint8Array(buf)) {
610+
if (!isArrayBufferView(buf) && !isAnyArrayBuffer(buf)) {
599611
// TODO(BridgeAR): This should not be of type ERR_INVALID_ARG_TYPE.
600612
// Instead, find the proper error code for this.
601613
throw new ERR_INVALID_ARG_TYPE(
602-
`list[${i}]`, ['Buffer', 'Uint8Array'], list[i]);
614+
`list[${i}]`, BUFFER_NAMES, list[i]);
603615
}
604-
pos += _copyActual(buf, buffer, pos, 0, buf.length);
616+
pos += _copyActual(buf, buffer, pos, 0, buf.byteLength);
605617
}
606618

607619
// Note: `length` is always equal to `buffer.length` at this point
@@ -773,9 +785,7 @@ function byteLength(string, encoding) {
773785
return string.byteLength;
774786
}
775787

776-
throw new ERR_INVALID_ARG_TYPE(
777-
'string', ['string', 'Buffer', 'ArrayBuffer'], string,
778-
);
788+
throw new ERR_INVALID_ARG_TYPE( 'string', ['string', ...BUFFER_NAMES], string);
779789
}
780790

781791
const len = string.length;
@@ -864,15 +874,18 @@ Buffer.prototype.toString = function toString(encoding, start, end) {
864874
};
865875

866876
Buffer.prototype.equals = function equals(otherBuffer) {
867-
if (!isUint8Array(otherBuffer)) {
868-
throw new ERR_INVALID_ARG_TYPE(
869-
'otherBuffer', ['Buffer', 'Uint8Array'], otherBuffer);
877+
if (isArrayBufferView(otherBuffer)) {
878+
// Do nothing..
879+
} else if (isAnyArrayBuffer(otherBuffer)) {
880+
otherBuffer = new Uint8Array(otherBuffer);
881+
} else {
882+
throw new ERR_INVALID_ARG_TYPE('otherBuffer', BUFFER_NAMES, otherBuffer);
870883
}
871884

872885
if (this === otherBuffer)
873886
return true;
874-
const len = TypedArrayPrototypeGetByteLength(this);
875-
if (len !== TypedArrayPrototypeGetByteLength(otherBuffer))
887+
const len = this.byteLength;
888+
if (len !== otherBuffer.byteLength)
876889
return false;
877890

878891
return len === 0 || _compare(this, otherBuffer) === 0;
@@ -919,9 +932,14 @@ Buffer.prototype.compare = function compare(target,
919932
targetEnd,
920933
sourceStart,
921934
sourceEnd) {
922-
if (!isUint8Array(target)) {
923-
throw new ERR_INVALID_ARG_TYPE('target', ['Buffer', 'Uint8Array'], target);
935+
if (isArrayBufferView(target)) {
936+
// Do nothing..
937+
} else if (isAnyArrayBuffer(target)) {
938+
target = new Uint8Array(target);
939+
} else {
940+
throw new ERR_INVALID_ARG_TYPE('target', BUFFER_NAMES, target);
924941
}
942+
925943
if (arguments.length === 1)
926944
return _compare(this, target);
927945

@@ -997,14 +1015,14 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
9971015
return ops.indexOf(buffer, val, byteOffset, dir);
9981016
}
9991017

1000-
if (isUint8Array(val)) {
1018+
if (isArrayBufferView(val) || isAnyArrayBuffer(val)) {
10011019
const encodingVal =
10021020
(ops === undefined ? encodingsMap.utf8 : ops.encodingVal);
10031021
return indexOfBuffer(buffer, val, byteOffset, encodingVal, dir);
10041022
}
10051023

10061024
throw new ERR_INVALID_ARG_TYPE(
1007-
'value', ['number', 'string', 'Buffer', 'Uint8Array'], val,
1025+
'value', ['number', 'string', ...BUFFER_NAMES], val,
10081026
);
10091027
}
10101028

@@ -1081,7 +1099,7 @@ function _fill(buf, value, offset, end, encoding) {
10811099

10821100
if (typeof value === 'number') {
10831101
// OOB check
1084-
const byteLen = TypedArrayPrototypeGetByteLength(buf);
1102+
const byteLen = buf.byteLength;
10851103
const fillLength = end - offset;
10861104
if (offset > end || fillLength + offset > byteLen)
10871105
throw new ERR_BUFFER_OUT_OF_BOUNDS();
@@ -1247,9 +1265,8 @@ if (internalBinding('config').hasIntl) {
12471265
// Transcodes the Buffer from one encoding to another, returning a new
12481266
// Buffer instance.
12491267
transcode = function transcode(source, fromEncoding, toEncoding) {
1250-
if (!isUint8Array(source)) {
1251-
throw new ERR_INVALID_ARG_TYPE('source',
1252-
['Buffer', 'Uint8Array'], source);
1268+
if (!ArrayBufferIsView(source) && !isAnyArrayBuffer(source)) {
1269+
throw new ERR_INVALID_ARG_TYPE('source', BUFFER_NAMES, source);
12531270
}
12541271
if (source.length === 0) return Buffer.alloc(0);
12551272

@@ -1305,19 +1322,19 @@ function atob(input) {
13051322
}
13061323

13071324
function isUtf8(input) {
1308-
if (isTypedArray(input) || isAnyArrayBuffer(input)) {
1325+
if (isArrayBufferView(input) || isAnyArrayBuffer(input)) {
13091326
return bindingIsUtf8(input);
13101327
}
13111328

1312-
throw new ERR_INVALID_ARG_TYPE('input', ['ArrayBuffer', 'Buffer', 'TypedArray'], input);
1329+
throw new ERR_INVALID_ARG_TYPE('input', BUFFER_NAMES, input);
13131330
}
13141331

13151332
function isAscii(input) {
1316-
if (isTypedArray(input) || isAnyArrayBuffer(input)) {
1333+
if (isArrayBufferView(input) || isAnyArrayBuffer(input)) {
13171334
return bindingIsAscii(input);
13181335
}
13191336

1320-
throw new ERR_INVALID_ARG_TYPE('input', ['ArrayBuffer', 'Buffer', 'TypedArray'], input);
1337+
throw new ERR_INVALID_ARG_TYPE('input', BUFFER_NAMES, input);
13211338
}
13221339

13231340
module.exports = {

0 commit comments

Comments
 (0)