Sha256: dba282c3ff703f878b3b9232247168468a4b1f982288038940b4a87692b235bd

Contents?: true

Size: 859 Bytes

Versions: 25

Compression:

Stored size: 859 Bytes

Contents

'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);
		}
	})();
});

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
immosquare-cleaner-0.1.27 node_modules/resolve-alpn/index.js
immosquare-cleaner-0.1.26 node_modules/resolve-alpn/index.js
immosquare-cleaner-0.1.25 node_modules/resolve-alpn/index.js
immosquare-cleaner-0.1.24 node_modules/resolve-alpn/index.js
immosquare-cleaner-0.1.23 node_modules/resolve-alpn/index.js