-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
Closed
Closed
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
binding.copy(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'); | ||
} | ||
|
||
|
@@ -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); | ||
binding.copy(buf, buffer, pos); | ||
pos += buf.length; | ||
} | ||
|
||
|
@@ -491,6 +491,9 @@ function slowToString(encoding, start, end) { | |
} | ||
} | ||
|
||
Buffer.prototype.copy = function(target, targetStart, sourceStart, sourceEnd) { | ||
return binding.copy(this, target, targetStart, sourceStart, sourceEnd); | ||
}; | ||
|
||
Buffer.prototype.toString = function() { | ||
let result; | ||
|
@@ -506,7 +509,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 +538,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 +603,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 +1036,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'); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think this error message will need to be changed