-
Notifications
You must be signed in to change notification settings - Fork 29.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
buffer: allow Uint8Array input to methods #10236
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,8 @@ | |
'use strict'; | ||
|
||
const binding = process.binding('buffer'); | ||
const { isArrayBuffer, isSharedArrayBuffer } = process.binding('util'); | ||
const { isArrayBuffer, isSharedArrayBuffer, isUint8Array } = | ||
process.binding('util'); | ||
const bindingObj = {}; | ||
const internalUtil = require('internal/util'); | ||
|
||
|
@@ -247,13 +248,13 @@ function fromArrayBuffer(obj, byteOffset, length) { | |
} | ||
|
||
function fromObject(obj) { | ||
if (obj instanceof Buffer) { | ||
if (isUint8Array(obj)) { | ||
const b = allocate(obj.length); | ||
|
||
if (b.length === 0) | ||
return b; | ||
|
||
obj.copy(b, 0, 0, obj.length); | ||
Buffer.prototype.copy.call(obj, b, 0, 0, obj.length); | ||
return b; | ||
} | ||
|
||
|
@@ -283,8 +284,7 @@ Buffer.isBuffer = function isBuffer(b) { | |
|
||
|
||
Buffer.compare = function compare(a, b) { | ||
if (!(a instanceof Buffer) || | ||
!(b instanceof Buffer)) { | ||
if (!isUint8Array(a) || !isUint8Array(b)) { | ||
throw new TypeError('Arguments must be Buffers'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this error message will need to be changed |
||
} | ||
|
||
|
@@ -322,9 +322,9 @@ Buffer.concat = function(list, length) { | |
var pos = 0; | ||
for (i = 0; i < list.length; i++) { | ||
var buf = list[i]; | ||
if (!Buffer.isBuffer(buf)) | ||
if (!isUint8Array(buf)) | ||
throw new TypeError('"list" argument must be an Array of Buffers'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto here and elsewhere about error messages |
||
buf.copy(buffer, pos); | ||
Buffer.prototype.copy.call(buf, buffer, pos); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto on using |
||
pos += buf.length; | ||
} | ||
|
||
|
@@ -506,7 +506,7 @@ Buffer.prototype.toString = function() { | |
|
||
|
||
Buffer.prototype.equals = function equals(b) { | ||
if (!(b instanceof Buffer)) | ||
if (!isUint8Array(b)) | ||
throw new TypeError('Argument must be a Buffer'); | ||
|
||
if (this === b) | ||
|
@@ -535,7 +535,7 @@ Buffer.prototype.compare = function compare(target, | |
thisStart, | ||
thisEnd) { | ||
|
||
if (!(target instanceof Buffer)) | ||
if (!isUint8Array(target)) | ||
throw new TypeError('Argument must be a Buffer'); | ||
|
||
if (start === undefined) | ||
|
@@ -600,7 +600,7 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) { | |
return binding.indexOfString(buffer, val, byteOffset, encoding, dir); | ||
} | ||
return slowIndexOf(buffer, val, byteOffset, encoding, dir); | ||
} else if (val instanceof Buffer) { | ||
} else if (isUint8Array(val)) { | ||
return binding.indexOfBuffer(buffer, val, byteOffset, encoding, dir); | ||
} else if (typeof val === 'number') { | ||
return binding.indexOfNumber(buffer, val, byteOffset, dir); | ||
|
@@ -1033,7 +1033,7 @@ Buffer.prototype.readDoubleBE = function readDoubleBE(offset, noAssert) { | |
|
||
|
||
function checkInt(buffer, value, offset, ext, max, min) { | ||
if (!(buffer instanceof Buffer)) | ||
if (!isUint8Array(buffer)) | ||
throw new TypeError('"buffer" argument must be a Buffer instance'); | ||
if (value > max || value < min) | ||
throw new TypeError('"value" argument is out of bounds'); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't need to reference it this way.
copy()
is native method. Can simply callbinding.copy(...)
.