fs: extract out validateBuffer function
PR-URL: https://github.com/nodejs/node/pull/17682 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Anatoli Papirovski <apapirovski@mac.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
This commit is contained in:
parent
fc8c1b1ded
commit
8983405508
17
lib/fs.js
17
lib/fs.js
@ -153,6 +153,15 @@ function makeCallback(cb) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function validateBuffer(buffer) {
|
||||||
|
if (!isUint8Array(buffer)) {
|
||||||
|
const err = new errors.TypeError('ERR_INVALID_ARG_TYPE', 'buffer',
|
||||||
|
['Buffer', 'Uint8Array']);
|
||||||
|
Error.captureStackTrace(err, validateBuffer);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function validateFd(fd) {
|
function validateFd(fd) {
|
||||||
let err;
|
let err;
|
||||||
|
|
||||||
@ -745,9 +754,7 @@ fs.openSync = function(path, flags, mode) {
|
|||||||
|
|
||||||
fs.read = function(fd, buffer, offset, length, position, callback) {
|
fs.read = function(fd, buffer, offset, length, position, callback) {
|
||||||
validateFd(fd);
|
validateFd(fd);
|
||||||
if (!isUint8Array(buffer))
|
validateBuffer(buffer);
|
||||||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'buffer',
|
|
||||||
['Buffer', 'Uint8Array']);
|
|
||||||
|
|
||||||
offset |= 0;
|
offset |= 0;
|
||||||
length |= 0;
|
length |= 0;
|
||||||
@ -779,9 +786,7 @@ Object.defineProperty(fs.read, internalUtil.customPromisifyArgs,
|
|||||||
|
|
||||||
fs.readSync = function(fd, buffer, offset, length, position) {
|
fs.readSync = function(fd, buffer, offset, length, position) {
|
||||||
validateFd(fd);
|
validateFd(fd);
|
||||||
if (!isUint8Array(buffer))
|
validateBuffer(buffer);
|
||||||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'buffer',
|
|
||||||
['Buffer', 'Uint8Array']);
|
|
||||||
|
|
||||||
offset |= 0;
|
offset |= 0;
|
||||||
length |= 0;
|
length |= 0;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user