Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
120 changes: 77 additions & 43 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ const {
isAnyArrayBuffer,
isArrayBufferView,
isUint8Array,
isTypedArray,
} = require('internal/util/types');
const {
inspect: utilInspect,
Expand Down Expand Up @@ -154,6 +153,14 @@ FastBuffer.prototype.constructor = Buffer;
Buffer.prototype = FastBuffer.prototype;
addBufferPrototypeMethods(Buffer.prototype);

const BUFFER_NAMES = [
'ArrayBuffer',
'Buffer',
'TypedArray',
'DataView',
'SharedArrayBuffer',
];

const constants = ObjectDefineProperties({}, {
MAX_LENGTH: {
__proto__: null,
Expand Down Expand Up @@ -232,10 +239,18 @@ function toInteger(n, defaultVal) {
}

function copyImpl(source, target, targetStart, sourceStart, sourceEnd) {
if (!ArrayBufferIsView(source))
throw new ERR_INVALID_ARG_TYPE('source', ['Buffer', 'Uint8Array'], source);
if (!ArrayBufferIsView(target))
throw new ERR_INVALID_ARG_TYPE('target', ['Buffer', 'Uint8Array'], target);
if (source?.constructor === DataView || isAnyArrayBuffer(source)) {
source = new Uint8Array(source.buffer ?? source, source.byteOffset ?? 0,
source.byteLength);
} else if (!isArrayBufferView(source)) {
throw new ERR_INVALID_ARG_TYPE('source', BUFFER_NAMES, source);
}
if (target?.constructor === DataView || isAnyArrayBuffer(target)) {
target = new Uint8Array(target.buffer ?? target, target.byteOffset ?? 0,
target.byteLength);
} else if (!isArrayBufferView(target)) {
throw new ERR_INVALID_ARG_TYPE('target', BUFFER_NAMES, target);
}

if (targetStart === undefined) {
targetStart = 0;
Expand Down Expand Up @@ -359,22 +374,29 @@ Buffer.from = function from(value, encodingOrOffset, length) {

throw new ERR_INVALID_ARG_TYPE(
'first argument',
['string', 'Buffer', 'ArrayBuffer', 'Array', 'Array-like Object'],
['string', 'Array', 'Array-like Object', ...BUFFER_NAMES],
value,
);
};

/**
* Creates the Buffer as a copy of the underlying ArrayBuffer of the view
* rather than the contents of the view.
* @param {TypedArray} view
* @param {Buffer|TypedArray|DataView} view
* @param {number} [offset]
* @param {number} [length]
* @returns {Buffer}
*/
Buffer.copyBytesFrom = function copyBytesFrom(view, offset, length) {
if (!isTypedArray(view)) {
throw new ERR_INVALID_ARG_TYPE('view', [ 'TypedArray' ], view);
if (view?.constructor === DataView) {
// Normalize DataView to Uint8Array so we can use TypedArray primordials.
view = new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
} else if (isArrayBufferView(view)) {
// Do nothing..
} else if (isAnyArrayBuffer(view)) {
view = new Uint8Array(view);
} else {
throw new ERR_INVALID_ARG_TYPE('view', BUFFER_NAMES, view);
}

const viewLength = TypedArrayPrototypeGetLength(view);
Expand All @@ -399,16 +421,11 @@ Buffer.copyBytesFrom = function copyBytesFrom(view, offset, length) {

if (end <= start) return new FastBuffer();

const viewByteLength = TypedArrayPrototypeGetByteLength(view);
const elementSize = viewByteLength / viewLength;
const srcByteOffset = TypedArrayPrototypeGetByteOffset(view) +
start * elementSize;
const srcByteLength = (end - start) * elementSize;

const srcView = TypedArrayPrototypeSubarray(view, start, end);
return fromArrayLike(new Uint8Array(
TypedArrayPrototypeGetBuffer(view),
srcByteOffset,
srcByteLength));
TypedArrayPrototypeGetBuffer(srcView),
TypedArrayPrototypeGetByteOffset(srcView),
TypedArrayPrototypeGetByteLength(srcView)));
};

// Identical to the built-in %TypedArray%.of(), but avoids using the deprecated
Expand Down Expand Up @@ -620,12 +637,17 @@ Buffer.concat = function concat(list, length) {
if (length === undefined) {
length = 0;
for (let i = 0; i < list.length; i++) {
const buf = list[i];
if (!isUint8Array(buf)) {
let buf = list[i];
if (buf?.constructor === DataView || isAnyArrayBuffer(buf)) {
buf = list[i] = new Uint8Array(buf.buffer ?? buf, buf.byteOffset ?? 0,
buf.byteLength);
} else if (isArrayBufferView(buf)) {
// Do nothing..
} else {
// TODO(BridgeAR): This should not be of type ERR_INVALID_ARG_TYPE.
// Instead, find the proper error code for this.
throw new ERR_INVALID_ARG_TYPE(
`list[${i}]`, ['Buffer', 'Uint8Array'], buf);
`list[${i}]`, BUFFER_NAMES, buf);
}
length += TypedArrayPrototypeGetByteLength(buf);
}
Expand All @@ -647,11 +669,17 @@ Buffer.concat = function concat(list, length) {

validateOffset(length, 'length');
for (let i = 0; i < list.length; i++) {
if (!isUint8Array(list[i])) {
if (list[i]?.constructor === DataView || isAnyArrayBuffer(list[i])) {
const buf = list[i];
list[i] = new Uint8Array(buf.buffer ?? buf, buf.byteOffset ?? 0,
buf.byteLength);
} else if (isArrayBufferView(list[i])) {
// Do nothing..
} else {
// TODO(BridgeAR): This should not be of type ERR_INVALID_ARG_TYPE.
// Instead, find the proper error code for this.
throw new ERR_INVALID_ARG_TYPE(
`list[${i}]`, ['Buffer', 'Uint8Array'], list[i]);
`list[${i}]`, BUFFER_NAMES, list[i]);
}
}

Expand Down Expand Up @@ -834,10 +862,7 @@ function byteLength(string, encoding) {
if (isArrayBufferView(string) || isAnyArrayBuffer(string)) {
return string.byteLength;
}

throw new ERR_INVALID_ARG_TYPE(
'string', ['string', 'Buffer', 'ArrayBuffer'], string,
);
throw new ERR_INVALID_ARG_TYPE('string', ['string', ...BUFFER_NAMES], string);
}

const len = string.length;
Expand Down Expand Up @@ -926,15 +951,18 @@ Buffer.prototype.toString = function toString(encoding, start, end) {
};

Buffer.prototype.equals = function equals(otherBuffer) {
if (!isUint8Array(otherBuffer)) {
throw new ERR_INVALID_ARG_TYPE(
'otherBuffer', ['Buffer', 'Uint8Array'], otherBuffer);
if (isArrayBufferView(otherBuffer)) {
// Do nothing..
} else if (isAnyArrayBuffer(otherBuffer)) {
otherBuffer = new Uint8Array(otherBuffer);
} else {
throw new ERR_INVALID_ARG_TYPE('otherBuffer', BUFFER_NAMES, otherBuffer);
}

if (this === otherBuffer)
return true;
const len = TypedArrayPrototypeGetByteLength(this);
if (len !== TypedArrayPrototypeGetByteLength(otherBuffer))
if (len !== otherBuffer.byteLength)
return false;

return len === 0 || _compare(this, otherBuffer) === 0;
Expand Down Expand Up @@ -988,9 +1016,14 @@ Buffer.prototype.compare = function compare(target,
targetEnd,
sourceStart,
sourceEnd) {
if (!isUint8Array(target)) {
throw new ERR_INVALID_ARG_TYPE('target', ['Buffer', 'Uint8Array'], target);
if (isArrayBufferView(target)) {
// Do nothing..
} else if (isAnyArrayBuffer(target)) {
target = new Uint8Array(target);
} else {
throw new ERR_INVALID_ARG_TYPE('target', BUFFER_NAMES, target);
}

if (arguments.length === 1)
return _compare(this, target);

Expand Down Expand Up @@ -1066,14 +1099,14 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
return ops.indexOf(buffer, val, byteOffset, dir);
}

if (isUint8Array(val)) {
if (isArrayBufferView(val) || isAnyArrayBuffer(val)) {
const encodingVal =
(ops === undefined ? encodingsMap.utf8 : ops.encodingVal);
return indexOfBuffer(buffer, val, byteOffset, encodingVal, dir);
}

throw new ERR_INVALID_ARG_TYPE(
'value', ['number', 'string', 'Buffer', 'Uint8Array'], val,
'value', ['number', 'string', ...BUFFER_NAMES], val,
);
}

Expand Down Expand Up @@ -1244,7 +1277,9 @@ Buffer.prototype.subarray = function subarray(start, end) {
start = adjustOffset(start, srcLength);
end = end !== undefined ? adjustOffset(end, srcLength) : srcLength;
const newLength = end > start ? end - start : 0;
return new FastBuffer(this.buffer, this.byteOffset + start, newLength);
return new FastBuffer(TypedArrayPrototypeGetBuffer(this),
TypedArrayPrototypeGetByteOffset(this) + start,
newLength);
};

Buffer.prototype.slice = function slice(start, end) {
Expand Down Expand Up @@ -1328,9 +1363,8 @@ if (internalBinding('config').hasIntl) {
// Transcodes the Buffer from one encoding to another, returning a new
// Buffer instance.
transcode = function transcode(source, fromEncoding, toEncoding) {
if (!isUint8Array(source)) {
throw new ERR_INVALID_ARG_TYPE('source',
['Buffer', 'Uint8Array'], source);
if (!ArrayBufferIsView(source) && !isAnyArrayBuffer(source)) {
throw new ERR_INVALID_ARG_TYPE('source', BUFFER_NAMES, source);
}
if (source.length === 0) return new FastBuffer();

Expand Down Expand Up @@ -1386,19 +1420,19 @@ function atob(input) {
}

function isUtf8(input) {
if (isTypedArray(input) || isAnyArrayBuffer(input)) {
if (isArrayBufferView(input) || isAnyArrayBuffer(input)) {
return bindingIsUtf8(input);
}

throw new ERR_INVALID_ARG_TYPE('input', ['ArrayBuffer', 'Buffer', 'TypedArray'], input);
throw new ERR_INVALID_ARG_TYPE('input', BUFFER_NAMES, input);
}

function isAscii(input) {
if (isTypedArray(input) || isAnyArrayBuffer(input)) {
if (isArrayBufferView(input) || isAnyArrayBuffer(input)) {
return bindingIsAscii(input);
}

throw new ERR_INVALID_ARG_TYPE('input', ['ArrayBuffer', 'Buffer', 'TypedArray'], input);
throw new ERR_INVALID_ARG_TYPE('input', BUFFER_NAMES, input);
}

module.exports = {
Expand Down
50 changes: 43 additions & 7 deletions test/parallel/test-buffer-compare.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,54 @@ assert.strictEqual(Buffer.compare(Buffer.alloc(1), Buffer.alloc(0)), 1);

assert.throws(() => Buffer.compare(Buffer.alloc(1), 'abc'), {
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "buf2" argument must be an instance of Buffer or Uint8Array. ' +
"Received type string ('abc')"
});
assert.throws(() => Buffer.compare('abc', Buffer.alloc(1)), {
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "buf1" argument must be an instance of Buffer or Uint8Array. ' +
"Received type string ('abc')"
});

assert.throws(() => Buffer.alloc(1).compare('abc'), {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: 'The "target" argument must be an instance of ' +
"Buffer or Uint8Array. Received type string ('abc')"
});

// compare works with ArrayBuffer
{
const buf = Buffer.from([1, 2, 3]);
const ab = new ArrayBuffer(3);
new Uint8Array(ab).set([1, 2, 3]);
assert.strictEqual(buf.compare(ab), 0);

const ab2 = new ArrayBuffer(3);
new Uint8Array(ab2).set([4, 5, 6]);
assert.strictEqual(buf.compare(ab2), -1);

const ab3 = new ArrayBuffer(3);
new Uint8Array(ab3).set([0, 0, 0]);
assert.strictEqual(buf.compare(ab3), 1);
}

// compare works with DataView
{
const buf = Buffer.from([1, 2, 3]);
const dv = new DataView(new ArrayBuffer(3));
new Uint8Array(dv.buffer).set([1, 2, 3]);
assert.strictEqual(buf.compare(dv), 0);

const dv2 = new DataView(new ArrayBuffer(3));
new Uint8Array(dv2.buffer).set([4, 5, 6]);
assert.strictEqual(buf.compare(dv2), -1);
}

// compare works with SharedArrayBuffer
{
const buf = Buffer.from([1, 2, 3]);
const sab = new SharedArrayBuffer(3);
new Uint8Array(sab).set([1, 2, 3]);
assert.strictEqual(buf.compare(sab), 0);
}

// compare works with other TypedArrays
{
const buf = Buffer.from([0x01, 0x00, 0x02, 0x00]);
const u16 = new Uint16Array([1, 2]);
assert.strictEqual(buf.compare(u16), 0);
}
Loading