mirror of
https://github.com/nodejs/node.git
synced 2025-04-29 06:19:07 +00:00

`socket.destroy()` can destory the stream before the chunk to write with `socket.end()` is actually sent. Furthermore `socket.destroy()` destroys `p` and not the actual raw socket. As a result it is possible that the connection is left open. Remove `socket.destroy()` to ensure that the chunk is sent. Also use `common.mustCall()` to ensure that the `'secureConnection'` and `'secureConnect'` events are emitted exactly once. PR-URL: https://github.com/nodejs/node/pull/27478 Fixes: https://github.com/nodejs/node/issues/26938 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Sam Roberts <vieuxtech@gmail.com> Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com>
67 lines
1.4 KiB
JavaScript
67 lines
1.4 KiB
JavaScript
'use strict';
|
|
const common = require('../common');
|
|
|
|
if (!common.hasCrypto)
|
|
common.skip('missing crypto');
|
|
|
|
const fixtures = require('../common/fixtures');
|
|
|
|
const net = require('net');
|
|
const stream = require('stream');
|
|
const tls = require('tls');
|
|
|
|
const server = tls.createServer({
|
|
key: fixtures.readKey('agent1-key.pem'),
|
|
cert: fixtures.readKey('agent1-cert.pem')
|
|
}, common.mustCall(function(c) {
|
|
console.log('new client');
|
|
|
|
c.resume();
|
|
c.end('ohai');
|
|
})).listen(0, common.mustCall(function() {
|
|
const raw = net.connect(this.address().port);
|
|
|
|
let pending = false;
|
|
raw.on('readable', function() {
|
|
if (pending)
|
|
p._read();
|
|
});
|
|
|
|
raw.on('end', function() {
|
|
p.push(null);
|
|
});
|
|
|
|
const p = new stream.Duplex({
|
|
read: function read() {
|
|
pending = false;
|
|
|
|
const chunk = raw.read();
|
|
if (chunk) {
|
|
console.log('read', chunk);
|
|
this.push(chunk);
|
|
} else {
|
|
pending = true;
|
|
}
|
|
},
|
|
write: function write(data, enc, cb) {
|
|
console.log('write', data, enc);
|
|
raw.write(data, enc, cb);
|
|
}
|
|
});
|
|
|
|
const socket = tls.connect({
|
|
socket: p,
|
|
rejectUnauthorized: false
|
|
}, common.mustCall(function() {
|
|
console.log('client secure');
|
|
|
|
socket.resume();
|
|
socket.end('hello');
|
|
}));
|
|
|
|
socket.once('close', function() {
|
|
console.log('client close');
|
|
server.close();
|
|
});
|
|
}));
|