• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3const assert = require('assert');
4const net = require('net');
5
6const message = Buffer.from('hello world');
7
8// Test typical usage
9net.createServer(common.mustCall(function(socket) {
10  this.close();
11  socket.end(message);
12})).listen(0, function() {
13  let received = 0;
14  const buffers = [];
15  const sockBuf = Buffer.alloc(8);
16  net.connect({
17    port: this.address().port,
18    onread: {
19      buffer: sockBuf,
20      callback: function(nread, buf) {
21        assert.strictEqual(buf, sockBuf);
22        received += nread;
23        buffers.push(Buffer.from(buf.slice(0, nread)));
24      }
25    }
26  }).on('data', common.mustNotCall()).on('end', common.mustCall(() => {
27    assert.strictEqual(received, message.length);
28    assert.deepStrictEqual(Buffer.concat(buffers), message);
29  }));
30});
31
32// Test Uint8Array support
33net.createServer(common.mustCall(function(socket) {
34  this.close();
35  socket.end(message);
36})).listen(0, function() {
37  let received = 0;
38  let incoming = new Uint8Array(0);
39  const sockBuf = new Uint8Array(8);
40  net.connect({
41    port: this.address().port,
42    onread: {
43      buffer: sockBuf,
44      callback: function(nread, buf) {
45        assert.strictEqual(buf, sockBuf);
46        received += nread;
47        const newIncoming = new Uint8Array(incoming.length + nread);
48        newIncoming.set(incoming);
49        newIncoming.set(buf.slice(0, nread), incoming.length);
50        incoming = newIncoming;
51      }
52    }
53  }).on('data', common.mustNotCall()).on('end', common.mustCall(() => {
54    assert.strictEqual(received, message.length);
55    assert.deepStrictEqual(incoming, new Uint8Array(message));
56  }));
57});
58
59// Test Buffer callback usage
60net.createServer(common.mustCall(function(socket) {
61  this.close();
62  socket.end(message);
63})).listen(0, function() {
64  let received = 0;
65  const incoming = [];
66  const bufPool = [ Buffer.alloc(2), Buffer.alloc(2), Buffer.alloc(2) ];
67  let bufPoolIdx = -1;
68  let bufPoolUsage = 0;
69  net.connect({
70    port: this.address().port,
71    onread: {
72      buffer: () => {
73        ++bufPoolUsage;
74        bufPoolIdx = (bufPoolIdx + 1) % bufPool.length;
75        return bufPool[bufPoolIdx];
76      },
77      callback: function(nread, buf) {
78        assert.strictEqual(buf, bufPool[bufPoolIdx]);
79        received += nread;
80        incoming.push(Buffer.from(buf.slice(0, nread)));
81      }
82    }
83  }).on('data', common.mustNotCall()).on('end', common.mustCall(() => {
84    assert.strictEqual(received, message.length);
85    assert.deepStrictEqual(Buffer.concat(incoming), message);
86    assert.strictEqual(bufPoolUsage, 7);
87  }));
88});
89
90// Test Uint8Array callback support
91net.createServer(common.mustCall(function(socket) {
92  this.close();
93  socket.end(message);
94})).listen(0, function() {
95  let received = 0;
96  let incoming = new Uint8Array(0);
97  const bufPool = [ new Uint8Array(2), new Uint8Array(2), new Uint8Array(2) ];
98  let bufPoolIdx = -1;
99  let bufPoolUsage = 0;
100  net.connect({
101    port: this.address().port,
102    onread: {
103      buffer: () => {
104        ++bufPoolUsage;
105        bufPoolIdx = (bufPoolIdx + 1) % bufPool.length;
106        return bufPool[bufPoolIdx];
107      },
108      callback: function(nread, buf) {
109        assert.strictEqual(buf, bufPool[bufPoolIdx]);
110        received += nread;
111        const newIncoming = new Uint8Array(incoming.length + nread);
112        newIncoming.set(incoming);
113        newIncoming.set(buf.slice(0, nread), incoming.length);
114        incoming = newIncoming;
115      }
116    }
117  }).on('data', common.mustNotCall()).on('end', common.mustCall(() => {
118    assert.strictEqual(received, message.length);
119    assert.deepStrictEqual(incoming, new Uint8Array(message));
120    assert.strictEqual(bufPoolUsage, 7);
121  }));
122});
123
124// Test explicit socket pause
125net.createServer(common.mustCall(function(socket) {
126  this.close();
127  socket.end(message);
128})).listen(0, function() {
129  let received = 0;
130  const buffers = [];
131  const sockBuf = Buffer.alloc(8);
132  let paused = false;
133  net.connect({
134    port: this.address().port,
135    onread: {
136      buffer: sockBuf,
137      callback: function(nread, buf) {
138        assert.strictEqual(paused, false);
139        assert.strictEqual(buf, sockBuf);
140        received += nread;
141        buffers.push(Buffer.from(buf.slice(0, nread)));
142        paused = true;
143        this.pause();
144        setTimeout(() => {
145          paused = false;
146          this.resume();
147        }, 100);
148      }
149    }
150  }).on('data', common.mustNotCall()).on('end', common.mustCall(() => {
151    assert.strictEqual(received, message.length);
152    assert.deepStrictEqual(Buffer.concat(buffers), message);
153  }));
154});
155
156// Test implicit socket pause
157net.createServer(common.mustCall(function(socket) {
158  this.close();
159  socket.end(message);
160})).listen(0, function() {
161  let received = 0;
162  const buffers = [];
163  const sockBuf = Buffer.alloc(8);
164  let paused = false;
165  net.connect({
166    port: this.address().port,
167    onread: {
168      buffer: sockBuf,
169      callback: function(nread, buf) {
170        assert.strictEqual(paused, false);
171        assert.strictEqual(buf, sockBuf);
172        received += nread;
173        buffers.push(Buffer.from(buf.slice(0, nread)));
174        paused = true;
175        setTimeout(() => {
176          paused = false;
177          this.resume();
178        }, 100);
179        return false;
180      }
181    }
182  }).on('data', common.mustNotCall()).on('end', common.mustCall(() => {
183    assert.strictEqual(received, message.length);
184    assert.deepStrictEqual(Buffer.concat(buffers), message);
185  }));
186});
187