buffer: adhere to INSPECT_MAX_BYTES
authorTimothy J Fontaine <tjfontaine@gmail.com>
Thu, 22 Aug 2013 01:17:11 +0000 (18:17 -0700)
committerTimothy J Fontaine <tjfontaine@gmail.com>
Thu, 22 Aug 2013 03:50:28 +0000 (20:50 -0700)
lib/buffer.js
test/simple/test-buffer-inspect.js [new file with mode: 0644]

index b62e17d..f6eb984 100644 (file)
@@ -237,8 +237,12 @@ Buffer.prototype.toString = function(encoding, start, end) {
 // Inspect
 Buffer.prototype.inspect = function inspect() {
   var str = '';
-  if (this.length > 0)
-    str = this.hexSlice(0, this.length).match(/.{2}/g).join(' ');
+  var max = exports.INSPECT_MAX_BYTES;
+  if (this.length > 0) {
+    str = this.hexSlice(0, max).match(/.{2}/g).join(' ');
+    if (this.length > max)
+      str += ' ... ';
+  }
   return '<' + this.constructor.name + ' ' + str + '>';
 };
 
diff --git a/test/simple/test-buffer-inspect.js b/test/simple/test-buffer-inspect.js
new file mode 100644 (file)
index 0000000..7436fb8
--- /dev/null
@@ -0,0 +1,51 @@
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+var common = require('../common');
+var assert = require('assert');
+
+var util = require('util');
+
+var buffer = require('buffer');
+
+buffer.INSPECT_MAX_BYTES = 2;
+
+var b = new Buffer(4);
+b.fill('1234');
+
+var s = new buffer.SlowBuffer(4);
+s.fill('1234');
+
+var expected = '<Buffer 31 32 ... >';
+
+assert.strictEqual(util.inspect(b), expected);
+assert.strictEqual(util.inspect(s), expected);
+
+b = new Buffer(2);
+b.fill('12');
+
+s = new buffer.SlowBuffer(2);
+s.fill('12');
+
+expected = '<Buffer 31 32>';
+
+assert.strictEqual(util.inspect(b), expected);
+assert.strictEqual(util.inspect(s), expected);