Permalink
Browse files

net: fix buffer iteration in bytesWritten

  • Loading branch information...
1 parent 77715ed commit d6c5719ac1038c482c6ed8ed84769dd56599f09d @indutny committed Apr 7, 2013
Showing with 10 additions and 2 deletions.
  1. +1 −2 lib/net.js
  2. +9 −0 test/simple/test-http-byteswritten.js
View
@@ -667,8 +667,7 @@ Socket.prototype.__defineGetter__('bytesWritten', function() {
encoding = this._pendingEncoding;
state.buffer.forEach(function(el) {
- el = el[0];
- bytes += Buffer.byteLength(el[0], el[1]);
+ bytes += Buffer.byteLength(el.chunk);
@isaacs

isaacs Apr 7, 2013

This is only valid for utf8, buffer, and ascii chunks. What if el.encoding === 'hex'?

@isaacs

isaacs Apr 7, 2013

Should be Buffer.byteLength(el.chunk, el.encoding)

@indutny

indutny Apr 8, 2013

Owner

You're right, fixed in ff32ecd.

});
if (data)
@@ -34,6 +34,15 @@ var httpServer = http.createServer(function(req, res) {
console.log('ok');
});
res.writeHead(200, { 'Content-Type': 'text/plain' });
+
+ // Write 1mb to cause some requests to buffer
+ var chunk = new Array(1024).join('A');
+ for (var i = 0; i < 1024; i++) {
+ res.write(chunk);
+ }
+ // Get .bytesWritten while buffer is not empty
+ assert(res.connection.bytesWritten > 0);
+
res.end(body);
});

0 comments on commit d6c5719

Please sign in to comment.