You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 859B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. 'use strict';
  2. const tls = require('tls');
  3. module.exports = (options = {}, connect = tls.connect) => new Promise((resolve, reject) => {
  4. let timeout = false;
  5. let socket;
  6. const callback = async () => {
  7. await socketPromise;
  8. socket.off('timeout', onTimeout);
  9. socket.off('error', reject);
  10. if (options.resolveSocket) {
  11. resolve({alpnProtocol: socket.alpnProtocol, socket, timeout});
  12. if (timeout) {
  13. await Promise.resolve();
  14. socket.emit('timeout');
  15. }
  16. } else {
  17. socket.destroy();
  18. resolve({alpnProtocol: socket.alpnProtocol, timeout});
  19. }
  20. };
  21. const onTimeout = async () => {
  22. timeout = true;
  23. callback();
  24. };
  25. const socketPromise = (async () => {
  26. try {
  27. socket = await connect(options, callback);
  28. socket.on('error', reject);
  29. socket.once('timeout', onTimeout);
  30. } catch (error) {
  31. reject(error);
  32. }
  33. })();
  34. });