buffer: deprecate legacy code
authorTrevor Norris <trev.norris@gmail.com>
Fri, 26 Apr 2013 23:58:18 +0000 (16:58 -0700)
committerTrevor Norris <trev.norris@gmail.com>
Tue, 18 Jun 2013 22:39:32 +0000 (15:39 -0700)
Several things are now no longer necessary. These have been deprecated,
and will be removed in v0.13.

lib/buffer.js

index a21fb79..81ecf79 100644 (file)
@@ -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) {
+  if (typeof length === 'undefined' || length > remaining)
     length = remaining;
-  } else {
-    length = +length;
-    if (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');