Skip to content

Commit a3cb074

Browse files
Sarat AddepalliSirR4T
authored andcommitted
partially done, blocked on stream
1 parent 38fd0fc commit a3cb074

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

lib/net.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const {
4141
UV_EOF
4242
} = internalBinding('uv');
4343

44-
const { Buffer } = require('buffer');
44+
const { byteLength } = require('buffer').Buffer;
4545
const TTYWrap = process.binding('tty_wrap');
4646
const { ShutdownWrap } = internalBinding('stream_wrap');
4747
const {
@@ -78,6 +78,7 @@ const {
7878
} = errors.codes;
7979
const { validateInt32, validateString } = require('internal/validators');
8080
const kLastWriteQueueSize = Symbol('lastWriteQueueSize');
81+
const { isArrayBufferView } = require('internal/util/types');
8182

8283
// Lazy loaded to improve startup performance.
8384
let cluster;
@@ -791,28 +792,28 @@ protoGetter('bytesWritten', function bytesWritten() {
791792
return undefined;
792793

793794
this.writableBuffer.forEach(function(el) {
794-
if (el.chunk instanceof Buffer)
795-
bytes += el.chunk.length;
795+
if (isArrayBufferView(el.chunk))
796+
bytes += el.chunk.byteLength;
796797
else
797-
bytes += Buffer.byteLength(el.chunk, el.encoding);
798+
bytes += byteLength(el.chunk, el.encoding);
798799
});
799800

800801
if (Array.isArray(data)) {
801802
// was a writev, iterate over chunks to get total length
802803
for (var i = 0; i < data.length; i++) {
803804
const chunk = data[i];
804805

805-
if (data.allBuffers || chunk instanceof Buffer)
806-
bytes += chunk.length;
806+
if (data.allBuffers || isArrayBufferView(chunk))
807+
bytes += chunk.byteLength;
807808
else
808-
bytes += Buffer.byteLength(chunk.chunk, chunk.encoding);
809+
bytes += byteLength(chunk.chunk, chunk.encoding);
809810
}
810811
} else if (data) {
811812
// Writes are either a string or a Buffer.
812813
if (typeof data !== 'string')
813-
bytes += data.length;
814+
bytes += data.byteLength;
814815
else
815-
bytes += Buffer.byteLength(data, encoding);
816+
bytes += byteLength(data, encoding);
816817
}
817818

818819
return bytes;

test/parallel/test-net-bytes-stats.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,20 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23-
require('../common');
23+
const common = require('../common');
2424
const assert = require('assert');
2525
const net = require('net');
2626

2727
let bytesRead = 0;
2828
let bytesWritten = 0;
2929
let count = 0;
3030

31+
const writeFoo = ['foo'];
32+
writeFoo.push(...common.getArrayBufferViews(Buffer.from(writeFoo[0])));
33+
const writeBar = ['bar'];
34+
writeBar.push(...common.getArrayBufferViews(Buffer.from(writeBar[0])));
35+
const maxReconnects = writeFoo.length - 1;
36+
3137
const tcp = net.Server(function(s) {
3238
console.log('tcp server connection');
3339

@@ -48,9 +54,9 @@ tcp.listen(0, function doTest() {
4854
count++;
4955
console.error('CLIENT connect #%d', count);
5056

51-
socket.write('foo', function() {
57+
socket.write(writeFoo[count - 1], function() {
5258
console.error('CLIENT: write cb');
53-
socket.end('bar');
59+
socket.end(writeBar[count - 1]);
5460
});
5561
});
5662

@@ -63,7 +69,7 @@ tcp.listen(0, function doTest() {
6369
console.error('CLIENT close event #%d', count);
6470
console.log(`Bytes read: ${bytesRead}`);
6571
console.log(`Bytes written: ${bytesWritten}`);
66-
if (count < 2) {
72+
if (count < maxReconnects) {
6773
console.error('RECONNECTING');
6874
socket.connect(tcp.address().port);
6975
} else {
@@ -73,6 +79,6 @@ tcp.listen(0, function doTest() {
7379
});
7480

7581
process.on('exit', function() {
76-
assert.strictEqual(bytesRead, 12);
77-
assert.strictEqual(bytesWritten, 12);
82+
assert.strictEqual(bytesRead, 24);
83+
assert.strictEqual(bytesWritten, 24);
7884
});

0 commit comments

Comments
 (0)