Browse Source

net: fix socket.bytesWritten Buffers support

Buffer.byteLength() works only for string inputs. Thus, when connection
has pending Buffer to write, it should just use it's length instead of
throwing exception.
v0.10.4-release
Fedor Indutny 12 years ago
parent
commit
c665b8e9ba
  1. 6
      lib/net.js
  2. 2
      test/simple/test-http-byteswritten.js

6
lib/net.js

@ -667,10 +667,16 @@ Socket.prototype.__defineGetter__('bytesWritten', function() {
encoding = this._pendingEncoding;
state.buffer.forEach(function(el) {
if (Buffer.isBuffer(el.chunk))
bytes += el.chunk.length;
else
bytes += Buffer.byteLength(el.chunk, el.encoding);
});
if (data)
if (Buffer.isBuffer(data))
bytes += data.length;
else
bytes += Buffer.byteLength(data, encoding);
return bytes;

2
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

Loading…
Cancel
Save