buffer: deprecate legacy code

Several things are now no longer necessary. These have been deprecated,
and will be removed in v0.13.
This commit is contained in:
Trevor Norris 2013-04-26 16:58:18 -07:00
parent 56869d9ae7
commit f489649159

View File

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