instanceof Buffer to Buffer.isBuffer()
This commit is contained in:
parent
6961bc568f
commit
02729d4af7
@ -7,6 +7,10 @@ function toHex (n) {
|
|||||||
return n.toString(16);
|
return n.toString(16);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Buffer.isBuffer = function (b) {
|
||||||
|
return b instanceof Buffer;
|
||||||
|
};
|
||||||
|
|
||||||
Buffer.prototype.inspect = function () {
|
Buffer.prototype.inspect = function () {
|
||||||
var s = "<Buffer ";
|
var s = "<Buffer ";
|
||||||
for (var i = 0; i < this.length; i++) {
|
for (var i = 0; i < this.length; i++) {
|
||||||
|
12
lib/fs.js
12
lib/fs.js
@ -150,7 +150,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) {
|
||||||
if (!(buffer instanceof Buffer)) {
|
if (!Buffer.isBuffer(buffer)) {
|
||||||
// legacy string interface (fd, length, position, encoding, callback)
|
// legacy string interface (fd, length, position, encoding, callback)
|
||||||
var cb = arguments[4],
|
var cb = arguments[4],
|
||||||
encoding = arguments[3];
|
encoding = arguments[3];
|
||||||
@ -175,7 +175,7 @@ fs.read = function (fd, buffer, offset, length, position, callback) {
|
|||||||
|
|
||||||
fs.readSync = function (fd, buffer, offset, length, position) {
|
fs.readSync = function (fd, buffer, offset, length, position) {
|
||||||
var legacy = false;
|
var legacy = false;
|
||||||
if (!(buffer instanceof Buffer)) {
|
if (!Buffer.isBuffer(buffer)) {
|
||||||
// legacy string interface (fd, length, position, encoding, callback)
|
// legacy string interface (fd, length, position, encoding, callback)
|
||||||
legacy = true;
|
legacy = true;
|
||||||
encoding = arguments[3];
|
encoding = arguments[3];
|
||||||
@ -198,7 +198,7 @@ fs.readSync = function (fd, buffer, offset, length, position) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
fs.write = function (fd, buffer, offset, length, position, callback) {
|
fs.write = function (fd, buffer, offset, length, position, callback) {
|
||||||
if (!(buffer instanceof Buffer)) {
|
if (!Buffer.isBuffer(buffer)) {
|
||||||
// legacy string interface (fd, data, position, encoding, callback)
|
// legacy string interface (fd, data, position, encoding, callback)
|
||||||
callback = arguments[4];
|
callback = arguments[4];
|
||||||
position = arguments[2];
|
position = arguments[2];
|
||||||
@ -212,7 +212,7 @@ fs.write = function (fd, buffer, offset, length, position, callback) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
fs.writeSync = function (fd, buffer, offset, length, position) {
|
fs.writeSync = function (fd, buffer, offset, length, position) {
|
||||||
if (!(buffer instanceof Buffer)) {
|
if (!Buffer.isBuffer(buffer)) {
|
||||||
// legacy string interface (fd, data, position, encoding)
|
// legacy string interface (fd, data, position, encoding)
|
||||||
position = arguments[2];
|
position = arguments[2];
|
||||||
|
|
||||||
@ -384,7 +384,7 @@ fs.writeFile = function (path, data, encoding_, callback) {
|
|||||||
if (openErr) {
|
if (openErr) {
|
||||||
if (callback) callback(openErr);
|
if (callback) callback(openErr);
|
||||||
} else {
|
} else {
|
||||||
var buffer = data instanceof Buffer ? data : new Buffer(data, encoding);
|
var buffer = Buffer.isBuffer(data) ? data : new Buffer(data, encoding);
|
||||||
writeAll(fd, buffer, callback);
|
writeAll(fd, buffer, callback);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -864,7 +864,7 @@ WriteStream.prototype.write = function (data) {
|
|||||||
cb = arguments[arguments.length-1];
|
cb = arguments[arguments.length-1];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data instanceof Buffer) {
|
if (Buffer.isBuffer(data)) {
|
||||||
this._queue.push([fs.write, data, 0, data.length, null, cb]);
|
this._queue.push([fs.write, data, 0, data.length, null, cb]);
|
||||||
} else {
|
} else {
|
||||||
var encoding = 'utf8';
|
var encoding = 'utf8';
|
||||||
|
@ -433,7 +433,7 @@ OutgoingMessage.prototype.write = function (chunk, encoding) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (typeof chunk !== "string"
|
if (typeof chunk !== "string"
|
||||||
&& !(chunk instanceof Buffer)
|
&& !Buffer.isBuffer(chunk)
|
||||||
&& !Array.isArray(chunk)) {
|
&& !Array.isArray(chunk)) {
|
||||||
throw new TypeError("first argument must be a string, Array, or Buffer");
|
throw new TypeError("first argument must be a string, Array, or Buffer");
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user