Skip to content

Commit e0af1f9

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

File tree

1 file changed

+59
-49
lines changed

1 file changed

+59
-49
lines changed

lib/buffer.js

Lines changed: 59 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,7 @@ const {
4444
SymbolSpecies,
4545
SymbolToPrimitive,
4646
TypedArrayPrototypeFill,
47-
TypedArrayPrototypeGetBuffer,
48-
TypedArrayPrototypeGetByteLength,
49-
TypedArrayPrototypeGetByteOffset,
50-
TypedArrayPrototypeGetLength,
5147
TypedArrayPrototypeSet,
52-
TypedArrayPrototypeSlice,
5348
Uint8Array,
5449
Uint8ArrayPrototype,
5550
} = primordials;
@@ -93,7 +88,6 @@ const {
9388
isAnyArrayBuffer,
9489
isArrayBufferView,
9590
isUint8Array,
96-
isTypedArray,
9791
} = require('internal/util/types');
9892
const {
9993
inspect: utilInspect,
@@ -133,6 +127,15 @@ FastBuffer.prototype.constructor = Buffer;
133127
Buffer.prototype = FastBuffer.prototype;
134128
addBufferPrototypeMethods(Buffer.prototype);
135129

130+
const BUFFER_NAMES = [
131+
'ArrayBuffer',
132+
'Buffer',
133+
'TypedArray',
134+
'DataView',
135+
'ArrayBuffer',
136+
'SharedArrayBuffer',
137+
];
138+
136139
const constants = ObjectDefineProperties({}, {
137140
MAX_LENGTH: {
138141
__proto__: null,
@@ -203,10 +206,13 @@ function toInteger(n, defaultVal) {
203206
}
204207

205208
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);
209+
if (isArrayBufferView(target)) {
210+
// Do nothing..
211+
} else if (isAnyArrayBuffer(target)) {
212+
target = new Uint8Array(target);
213+
} else {
214+
throw new ERR_INVALID_ARG_TYPE('target', BUFFER_NAMES, target);
215+
}
210216

211217
if (targetStart === undefined) {
212218
targetStart = 0;
@@ -321,25 +327,29 @@ Buffer.from = function from(value, encodingOrOffset, length) {
321327

322328
throw new ERR_INVALID_ARG_TYPE(
323329
'first argument',
324-
['string', 'Buffer', 'ArrayBuffer', 'Array', 'Array-like Object'],
330+
['string', 'Array', 'Array-like Object', ...BUFFER_NAMES],
325331
value,
326332
);
327333
};
328334

329335
/**
330336
* Creates the Buffer as a copy of the underlying ArrayBuffer of the view
331337
* rather than the contents of the view.
332-
* @param {TypedArray} view
338+
* @param {Buffer|TypedArray|DataView} view
333339
* @param {number} [offset]
334340
* @param {number} [length]
335341
* @returns {Buffer}
336342
*/
337343
Buffer.copyBytesFrom = function copyBytesFrom(view, offset, length) {
338-
if (!isTypedArray(view)) {
339-
throw new ERR_INVALID_ARG_TYPE('view', [ 'TypedArray' ], view);
344+
if (isArrayBufferView(view)) {
345+
// Do nothing..
346+
} else if (isAnyArrayBuffer(view)) {
347+
view = new Uint8Array(view);
348+
} else {
349+
throw new ERR_INVALID_ARG_TYPE('view', BUFFER_NAMES, view);
340350
}
341351

342-
const viewLength = TypedArrayPrototypeGetLength(view);
352+
const viewLength = view.byteLength;
343353
if (viewLength === 0) {
344354
return Buffer.alloc(0);
345355
}
@@ -351,21 +361,17 @@ Buffer.copyBytesFrom = function copyBytesFrom(view, offset, length) {
351361
} else {
352362
offset = 0;
353363
}
354-
let end;
355364
if (length !== undefined) {
356365
validateInteger(length, 'length', 0);
357-
end = offset + length;
358366
} else {
359-
end = viewLength;
367+
length = viewLength - offset;
360368
}
361-
362-
view = TypedArrayPrototypeSlice(view, offset, end);
363369
}
364370

365371
return fromArrayLike(new Uint8Array(
366-
TypedArrayPrototypeGetBuffer(view),
367-
TypedArrayPrototypeGetByteOffset(view),
368-
TypedArrayPrototypeGetByteLength(view)));
372+
view.buffer,
373+
view.byteOffset + offset,
374+
length));
369375
};
370376

371377
// Identical to the built-in %TypedArray%.of(), but avoids using the deprecated
@@ -583,8 +589,8 @@ Buffer.concat = function concat(list, length) {
583589
if (length === undefined) {
584590
length = 0;
585591
for (let i = 0; i < list.length; i++) {
586-
if (list[i].length) {
587-
length += list[i].length;
592+
if (list[i].byteLength) {
593+
length += list[i].byteLength;
588594
}
589595
}
590596
} else {
@@ -595,13 +601,13 @@ Buffer.concat = function concat(list, length) {
595601
let pos = 0;
596602
for (let i = 0; i < list.length; i++) {
597603
const buf = list[i];
598-
if (!isUint8Array(buf)) {
604+
if (!isArrayBufferView(buf) && !isAnyArrayBuffer(buf)) {
599605
// TODO(BridgeAR): This should not be of type ERR_INVALID_ARG_TYPE.
600606
// Instead, find the proper error code for this.
601607
throw new ERR_INVALID_ARG_TYPE(
602-
`list[${i}]`, ['Buffer', 'Uint8Array'], list[i]);
608+
`list[${i}]`, BUFFER_NAMES, list[i]);
603609
}
604-
pos += _copyActual(buf, buffer, pos, 0, buf.length);
610+
pos += _copyActual(buf, buffer, pos, 0, buf.byteLength);
605611
}
606612

607613
// Note: `length` is always equal to `buffer.length` at this point
@@ -772,10 +778,7 @@ function byteLength(string, encoding) {
772778
if (isArrayBufferView(string) || isAnyArrayBuffer(string)) {
773779
return string.byteLength;
774780
}
775-
776-
throw new ERR_INVALID_ARG_TYPE(
777-
'string', ['string', 'Buffer', 'ArrayBuffer'], string,
778-
);
781+
throw new ERR_INVALID_ARG_TYPE('string', ['string', ...BUFFER_NAMES], string);
779782
}
780783

781784
const len = string.length;
@@ -864,15 +867,18 @@ Buffer.prototype.toString = function toString(encoding, start, end) {
864867
};
865868

866869
Buffer.prototype.equals = function equals(otherBuffer) {
867-
if (!isUint8Array(otherBuffer)) {
868-
throw new ERR_INVALID_ARG_TYPE(
869-
'otherBuffer', ['Buffer', 'Uint8Array'], otherBuffer);
870+
if (isArrayBufferView(otherBuffer)) {
871+
// Do nothing..
872+
} else if (isAnyArrayBuffer(otherBuffer)) {
873+
otherBuffer = new Uint8Array(otherBuffer);
874+
} else {
875+
throw new ERR_INVALID_ARG_TYPE('otherBuffer', BUFFER_NAMES, otherBuffer);
870876
}
871877

872878
if (this === otherBuffer)
873879
return true;
874-
const len = TypedArrayPrototypeGetByteLength(this);
875-
if (len !== TypedArrayPrototypeGetByteLength(otherBuffer))
880+
const len = this.byteLength;
881+
if (len !== otherBuffer.byteLength)
876882
return false;
877883

878884
return len === 0 || _compare(this, otherBuffer) === 0;
@@ -919,9 +925,14 @@ Buffer.prototype.compare = function compare(target,
919925
targetEnd,
920926
sourceStart,
921927
sourceEnd) {
922-
if (!isUint8Array(target)) {
923-
throw new ERR_INVALID_ARG_TYPE('target', ['Buffer', 'Uint8Array'], target);
928+
if (isArrayBufferView(target)) {
929+
// Do nothing..
930+
} else if (isAnyArrayBuffer(target)) {
931+
target = new Uint8Array(target);
932+
} else {
933+
throw new ERR_INVALID_ARG_TYPE('target', BUFFER_NAMES, target);
924934
}
935+
925936
if (arguments.length === 1)
926937
return _compare(this, target);
927938

@@ -997,14 +1008,14 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
9971008
return ops.indexOf(buffer, val, byteOffset, dir);
9981009
}
9991010

1000-
if (isUint8Array(val)) {
1011+
if (isArrayBufferView(val) || isAnyArrayBuffer(val)) {
10011012
const encodingVal =
10021013
(ops === undefined ? encodingsMap.utf8 : ops.encodingVal);
10031014
return indexOfBuffer(buffer, val, byteOffset, encodingVal, dir);
10041015
}
10051016

10061017
throw new ERR_INVALID_ARG_TYPE(
1007-
'value', ['number', 'string', 'Buffer', 'Uint8Array'], val,
1018+
'value', ['number', 'string', ...BUFFER_NAMES], val,
10081019
);
10091020
}
10101021

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

10821093
if (typeof value === 'number') {
10831094
// OOB check
1084-
const byteLen = TypedArrayPrototypeGetByteLength(buf);
1095+
const byteLen = buf.byteLength;
10851096
const fillLength = end - offset;
10861097
if (offset > end || fillLength + offset > byteLen)
10871098
throw new ERR_BUFFER_OUT_OF_BOUNDS();
@@ -1247,9 +1258,8 @@ if (internalBinding('config').hasIntl) {
12471258
// Transcodes the Buffer from one encoding to another, returning a new
12481259
// Buffer instance.
12491260
transcode = function transcode(source, fromEncoding, toEncoding) {
1250-
if (!isUint8Array(source)) {
1251-
throw new ERR_INVALID_ARG_TYPE('source',
1252-
['Buffer', 'Uint8Array'], source);
1261+
if (!ArrayBufferIsView(source) && !isAnyArrayBuffer(source)) {
1262+
throw new ERR_INVALID_ARG_TYPE('source', BUFFER_NAMES, source);
12531263
}
12541264
if (source.length === 0) return Buffer.alloc(0);
12551265

@@ -1305,19 +1315,19 @@ function atob(input) {
13051315
}
13061316

13071317
function isUtf8(input) {
1308-
if (isTypedArray(input) || isAnyArrayBuffer(input)) {
1318+
if (isArrayBufferView(input) || isAnyArrayBuffer(input)) {
13091319
return bindingIsUtf8(input);
13101320
}
13111321

1312-
throw new ERR_INVALID_ARG_TYPE('input', ['ArrayBuffer', 'Buffer', 'TypedArray'], input);
1322+
throw new ERR_INVALID_ARG_TYPE('input', BUFFER_NAMES, input);
13131323
}
13141324

13151325
function isAscii(input) {
1316-
if (isTypedArray(input) || isAnyArrayBuffer(input)) {
1326+
if (isArrayBufferView(input) || isAnyArrayBuffer(input)) {
13171327
return bindingIsAscii(input);
13181328
}
13191329

1320-
throw new ERR_INVALID_ARG_TYPE('input', ['ArrayBuffer', 'Buffer', 'TypedArray'], input);
1330+
throw new ERR_INVALID_ARG_TYPE('input', BUFFER_NAMES, input);
13211331
}
13221332

13231333
module.exports = {

0 commit comments

Comments
 (0)