Skip to content
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

util: fix isBuffer #1988

Closed
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
5 changes: 2 additions & 3 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

const binding = process.binding('buffer');
const smalloc = process.binding('smalloc');
const util = require('util');
const internalUtil = require('internal/util');
const alloc = smalloc.alloc;
const truncate = smalloc.truncate;
Expand Down Expand Up @@ -457,7 +456,7 @@ Buffer.prototype.fill = function fill(val, start, end) {


// XXX remove in v0.13
Buffer.prototype.get = util.deprecate(function get(offset) {
Buffer.prototype.get = internalUtil.deprecate(function get(offset) {
offset = ~~offset;
if (offset < 0 || offset >= this.length)
throw new RangeError('index out of range');
Expand All @@ -466,7 +465,7 @@ Buffer.prototype.get = util.deprecate(function get(offset) {


// XXX remove in v0.13
Buffer.prototype.set = util.deprecate(function set(offset, v) {
Buffer.prototype.set = internalUtil.deprecate(function set(offset, v) {
offset = ~~offset;
if (offset < 0 || offset >= this.length)
throw new RangeError('index out of range');
Expand Down
24 changes: 24 additions & 0 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,27 @@ exports.printDeprecationMessage = function(msg, warned) {

return true;
};

// Mark that a method should not be used.
// Returns a modified function which warns once by default.
// If --no-deprecation is set, then it is a no-op.
exports.deprecate = function(fn, msg) {
// Allow for deprecating things in the process of starting up.
if (global.process === undefined) {
return function() {
return exports.deprecate(fn, msg).apply(this, arguments);
};
}

if (process.noDeprecation === true) {
return fn;
}

var warned = false;
function deprecated() {
warned = exports.printDeprecationMessage(msg, warned);
return fn.apply(this, arguments);
}

return deprecated;
};
29 changes: 2 additions & 27 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,29 +48,7 @@ exports.format = function(f) {
};


// Mark that a method should not be used.
// Returns a modified function which warns once by default.
// If --no-deprecation is set, then it is a no-op.
exports.deprecate = function(fn, msg) {
// Allow for deprecating things in the process of starting up.
if (global.process === undefined) {
return function() {
return exports.deprecate(fn, msg).apply(this, arguments);
};
}

if (process.noDeprecation === true) {
return fn;
}

var warned = false;
function deprecated() {
warned = internalUtil.printDeprecationMessage(msg, warned);
return fn.apply(this, arguments);
}

return deprecated;
};
exports.deprecate = internalUtil.deprecate;


var debugs = {};
Expand Down Expand Up @@ -662,10 +640,7 @@ function isPrimitive(arg) {
}
exports.isPrimitive = isPrimitive;

function isBuffer(arg) {
return arg instanceof Buffer;
}
exports.isBuffer = isBuffer;
exports.isBuffer = Buffer.isBuffer;

function objectToString(o) {
return Object.prototype.toString.call(o);
Expand Down
4 changes: 4 additions & 0 deletions test/parallel/test-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ assert.equal(true, util.isPrimitive(Infinity));
assert.equal(true, util.isPrimitive(NaN));
assert.equal(true, util.isPrimitive(Symbol('symbol')));

// isBuffer
assert.equal(false, util.isBuffer('foo'));
assert.equal(true, util.isBuffer(new Buffer('foo')));

// _extend
assert.deepEqual(util._extend({a:1}), {a:1});
assert.deepEqual(util._extend({a:1}, []), {a:1});
Expand Down