Browse Source

buffer: deprecate legacy code

Several things are now no longer necessary. These have been deprecated,
and will be removed in v0.13.
v0.11.3-release
Trevor Norris 12 years ago
parent
commit
f489649159
  1. 63
      lib/buffer.js

63
lib/buffer.js

@ -22,6 +22,7 @@
var smalloc = process.binding('smalloc');
var buffer = process.binding('buffer');
var assert = require('assert');
var util = require('util');
var alloc = smalloc.alloc;
var sliceOnto = smalloc.sliceOnto;
var kMaxLength = smalloc.kMaxLength;
@ -245,57 +246,69 @@ Buffer.prototype.inspect = function inspect() {
};
// TODO(trevnorris): DEPRECATE
Buffer.prototype.get = function get(offset) {
// XXX remove in v0.13
Buffer.prototype.get = util.deprecate(function get(offset) {
offset = ~~offset;
if (offset < 0 || offset >= this.length)
throw new RangeError('index out of range');
return this[offset];
};
}, '.get() is deprecated. Access using array indexes instead.');
// TODO(trevnorris): DEPRECATE
Buffer.prototype.set = function set(offset, v) {
// XXX remove in v0.13
Buffer.prototype.set = util.deprecate(function set(offset, v) {
offset = ~~offset;
if (offset < 0 || offset >= this.length)
throw new RangeError('index out of range');
return this[offset] = v;
};
}, '.set() is deprecated. Set using array indexes instead.');
// TODO(trevnorris): fix these checks to follow new standard
// write(string, offset = 0, length = buffer.length, encoding = 'utf8')
var writeWarned = false;
var writeMsg = '.write(string, encoding, offset, length) is deprecated.' +
' Use write(string, offset, length, encoding) instead.';
Buffer.prototype.write = function(string, offset, length, encoding) {
// Support both (string, offset, length, encoding)
// and the legacy (string, encoding, offset, length)
if (isFinite(offset)) {
if (!isFinite(length)) {
// allow write(string, encoding)
if (typeof offset === 'string' && typeof length === 'undefined') {
encoding = offset;
offset = 0;
length = undefined;
// allow write(string, offset[, length], encoding)
} else if (isFinite(offset)) {
offset = ~~offset;
if (isFinite(length)) {
length = ~~length;
} else {
encoding = length;
length = undefined;
}
// TODO(trevnorris): DEPRECATE
} else { // legacy
// XXX legacy write(string, encoding, offset, length) - remove in v0.13
} else {
if (!writeWarned) {
if (process.throwDeprecation)
throw new Error(writeMsg);
else if (process.traceDeprecation)
console.trace(writeMsg);
else
console.error(writeMsg);
writeWarned = true;
}
var swap = encoding;
encoding = offset;
offset = length;
offset = ~~length;
length = swap;
}
offset = +offset || 0;
var remaining = this.length - offset;
if (!length) {
length = remaining;
} else {
length = +length;
if (length > remaining) {
if (typeof length === 'undefined' || length > remaining)
length = remaining;
}
}
if (typeof encoding === 'undefined')
encoding = 'utf8';
else
encoding = (encoding + '').toLowerCase();
encoding = !!encoding ? (encoding + '').toLowerCase() : 'utf8';
if (string.length > 0 && (length < 0 || offset < 0))
throw new RangeError('attempt to write beyond buffer bounds');

Loading…
Cancel
Save