diff --git a/lib/net.js b/lib/net.js index 3c58de2837..b5a1a62328 100644 --- a/lib/net.js +++ b/lib/net.js @@ -667,11 +667,17 @@ Socket.prototype.__defineGetter__('bytesWritten', function() { encoding = this._pendingEncoding; state.buffer.forEach(function(el) { - bytes += Buffer.byteLength(el.chunk, el.encoding); + if (Buffer.isBuffer(el.chunk)) + bytes += el.chunk.length; + else + bytes += Buffer.byteLength(el.chunk, el.encoding); }); if (data) - bytes += Buffer.byteLength(data, encoding); + if (Buffer.isBuffer(data)) + bytes += data.length; + else + bytes += Buffer.byteLength(data, encoding); return bytes; }); diff --git a/test/simple/test-http-byteswritten.js b/test/simple/test-http-byteswritten.js index 160ea4d76e..ff842414cf 100644 --- a/test/simple/test-http-byteswritten.js +++ b/test/simple/test-http-byteswritten.js @@ -38,8 +38,10 @@ var httpServer = http.createServer(function(req, res) { // Write 1.5mb to cause some requests to buffer // Also, mix up the encodings a bit. var chunk = new Array(1024 + 1).join('7'); + var bchunk = new Buffer(chunk); for (var i = 0; i < 1024; i++) { res.write(chunk); + res.write(bchunk); res.write(chunk, 'hex'); } // Get .bytesWritten while buffer is not empty