How to use byPortAndAddress method in Cypress

Best JavaScript code snippet using cypress

Run Cypress automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

connect.js

Source: connect.js Github

copy
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3    return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.createRetryingSocket = exports.getDelayForRetry = exports.getAddress = exports.byPortAndAddress = void 0;
7const bluebird_1 = __importDefault(require("bluebird"));
8const debug_1 = __importDefault(require("debug"));
9const dns_1 = __importDefault(require("dns"));
10const lodash_1 = __importDefault(require("lodash"));
11const net_1 = __importDefault(require("net"));
12const tls_1 = __importDefault(require("tls"));
13const debug = (0, debug_1.default)('cypress:network:connect');
14function byPortAndAddress(port, address) {
15    // https://nodejs.org/api/net.html#net_net_connect_port_host_connectlistener
16    return new bluebird_1.default((resolve, reject) => {
17        const onConnect = () => {
18            client.end();
19            resolve(address);
20        };
21        const client = net_1.default.connect(port, address.address, onConnect);
22        client.on('error', reject);
23    });
24}
25exports.byPortAndAddress = byPortAndAddress;
26function getAddress(port, hostname) {
27    debug('beginning getAddress %o', { hostname, port });
28    const fn = byPortAndAddress.bind({}, port);
29    // promisify at the very last second which enables us to
30    // modify dns lookup function (via hosts overrides)
31    const lookupAsync = bluebird_1.default.promisify(dns_1.default.lookup, { context: dns_1.default });
32    // this does not go out to the network to figure
33    // out the addresess. in fact it respects the /etc/hosts file
34    // https://github.com/nodejs/node/blob/dbdbdd4998e163deecefbb1d34cda84f749844a4/lib/dns.js#L108
35    // https://nodejs.org/api/dns.html#dns_dns_lookup_hostname_options_callback
36    // @ts-ignore
37    return lookupAsync(hostname, { all: true })
38        .then((addresses) => {
39        debug('got addresses %o', { hostname, port, addresses });
40        // convert to an array if string
41        return Array.prototype.concat.call(addresses).map(fn);
42    })
43        .tapCatch((err) => {
44        debug('error getting address %o', { hostname, port, err });
45    })
46        .any();
47}
48exports.getAddress = getAddress;
49function getDelayForRetry(iteration) {
50    return [0, 100, 200, 200][iteration];
51}
52exports.getDelayForRetry = getDelayForRetry;
53function createSocket(opts, onConnect) {
54    const netOpts = lodash_1.default.pick(opts, 'host', 'port');
55    if (opts.useTls) {
56        return tls_1.default.connect(netOpts, onConnect);
57    }
58    return net_1.default.connect(netOpts, onConnect);
59}
60function createRetryingSocket(opts, cb) {
61    if (typeof opts.getDelayMsForRetry === 'undefined') {
62        opts.getDelayMsForRetry = getDelayForRetry;
63    }
64    function tryConnect(iteration = 0) {
65        const retry = (err) => {
66            const delay = opts.getDelayMsForRetry(iteration, err);
67            if (typeof delay === 'undefined') {
68                debug('retries exhausted, bubbling up error %o', { iteration, err });
69                return cb(err);
70            }
71            debug('received error on connect, retrying %o', { iteration, delay, err });
72            setTimeout(() => {
73                tryConnect(iteration + 1);
74            }, delay);
75        };
76        function onError(err) {
77            sock.on('error', (err) => {
78                debug('second error received on retried socket %o', { opts, iteration, err });
79            });
80            retry(err);
81        }
82        function onConnect() {
83            debug('successfully connected %o', { opts, iteration });
84            // connection successfully established, pass control of errors/retries to consuming function
85            sock.removeListener('error', onError);
86            cb(undefined, sock, retry);
87        }
88        const sock = createSocket(opts, onConnect);
89        sock.once('error', onError);
90    }
91    tryConnect();
92}
93exports.createRetryingSocket = createRetryingSocket;
94
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Cypress on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)