1'use strict';
2const common = require('../common');
3const assert = require('assert');
4if (!common.hasCrypto) common.skip('missing crypto');
5const tls = require('tls');
6const net = require('net');
7
8const HEAD = Buffer.alloc(1024 * 1024, 0);
9
10const server = net.createServer((serverSock) => {
11  let recvLen = 0;
12  const recv = [];
13  serverSock.on('data', common.mustCallAtLeast((chunk) => {
14    recv.push(chunk);
15    recvLen += chunk.length;
16
17    // Check that HEAD is followed by a client hello
18    if (recvLen > HEAD.length) {
19      const clientHelloFstByte = Buffer.concat(recv).subarray(HEAD.length, HEAD.length + 1);
20      assert.strictEqual(clientHelloFstByte.toString('hex'), '16');
21      process.exit(0);
22    }
23  }, 1));
24})
25  .listen(client);
26
27function client() {
28  const socket = net.createConnection({
29    host: '127.0.0.1',
30    port: server.address().port,
31  });
32  socket.write(HEAD.subarray(0, HEAD.length / 2), common.mustSucceed());
33
34  // This write will be queued by streams.Writable, the super class of net.Socket,
35  // which will dequeue this write when it gets notified about the finish of the first write.
36  // We had a bug that it wouldn't get notified. This test verifies the bug is fixed.
37  socket.write(HEAD.subarray(HEAD.length / 2), common.mustSucceed());
38
39  tls.connect({
40    socket,
41  });
42}
43