workflow_launcher/node_modules/resolve-alpn/index.js

44 lines
859 B
JavaScript
Raw Normal View History

2024-03-11 11:01:07 -04:00
'use strict';
const tls = require('tls');
module.exports = (options = {}, connect = tls.connect) => new Promise((resolve, reject) => {
let timeout = false;
let socket;
const callback = async () => {
await socketPromise;
socket.off('timeout', onTimeout);
socket.off('error', reject);
if (options.resolveSocket) {
resolve({alpnProtocol: socket.alpnProtocol, socket, timeout});
if (timeout) {
await Promise.resolve();
socket.emit('timeout');
}
} else {
socket.destroy();
resolve({alpnProtocol: socket.alpnProtocol, timeout});
}
};
const onTimeout = async () => {
timeout = true;
callback();
};
const socketPromise = (async () => {
try {
socket = await connect(options, callback);
socket.on('error', reject);
socket.once('timeout', onTimeout);
} catch (error) {
reject(error);
}
})();
});