diff --git a/.travis.yml b/.travis.yml index c7739b0..40df843 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,3 +4,4 @@ node_js: - "8" - "10" - "12" + - "14" \ No newline at end of file diff --git a/README.md b/README.md index 5b1cf98..ca10264 100644 --- a/README.md +++ b/README.md @@ -322,6 +322,13 @@ tunnelArguments = { logFile: '/lambdatest/logs.txt' }; ``` +- egressOnly: Uses proxy settings only for outbound requests. +- ingressOnly: Uses proxy settings only for inbound requests. +- dns: Comma separated list of dns servers +- sshConnType: Specify type of ssh connection (over_22, over_443, over_ws) +- mode: Specifies in which mode tunnel should run [ssh,ws] +- nows: Force tunnel to run in non websocket mode +- mitm: MITM mode, used for testing websites with private certificates ## Contribute diff --git a/index.d.ts b/index.d.ts index 9ef83cb..27b7549 100644 --- a/index.d.ts +++ b/index.d.ts @@ -26,7 +26,7 @@ declare module "@lambdatest/node-tunnel" { class Tunnel { start(options: Partial, callback: (error?: Error) => void): void; isRunning(): boolean; - stop(callback: () => void): void; + stop(callback: (error?: Error, status: boolean) => void): Promise; } } \ No newline at end of file diff --git a/lib/cfg/node-tunnel-config-v3-latest.json b/lib/cfg/node-tunnel-config-v3-latest.json new file mode 100644 index 0000000..e06277a --- /dev/null +++ b/lib/cfg/node-tunnel-config-v3-latest.json @@ -0,0 +1,142 @@ +{ + "binaryLinks": { + "mac": { + "v2": { + "32bit": { + "httpPath": "https://downloads.lambdatest.com/tunnel/mac/32bit/ltcomponent.zip", + "binaryName": "ltcomponent.zip", + "hash": "f802ce3166bb25c1a7508d530d514b19" + }, + "64bit": { + "httpPath": "https://downloads.lambdatest.com/tunnel/mac/64bit/ltcomponent.zip", + "binaryName": "ltcomponent.zip", + "hash": "50508831995353deb4983d11ff4b0861" + } + }, + "v3": { + "32bit": { + "httpPath": "https://downloads.lambdatest.com/tunnel/v3/mac/32bit/LT_Mac.zip", + "binaryName": "LT_Mac.zip", + "hash": "5fdb3e99e8c20fa9de64c73a3d68bac3" + }, + "64bit": { + "httpPath": "https://downloads.lambdatest.com/tunnel/v3/mac/64bit/LT_Mac.zip", + "binaryName": "LT_Mac.zip", + "hash": "8bb2c01ca2eef94764523a7c40e72a39" + } + } + }, + "win": { + "v2": { + "32bit": { + "httpPath": "https://downloads.lambdatest.com/tunnel/windows/32bit/ltcomponent.zip", + "binaryName": "ltcomponent.zip", + "hash": "a8eb108eb6fc60178ae28196242a17ff" + }, + "64bit": { + "httpPath": "https://downloads.lambdatest.com/tunnel/windows/64bit/ltcomponent.zip", + "binaryName": "ltcomponent.zip", + "hash": "69f59ccd89aae6adeccf69a8a829bdea" + } + }, + "v3": { + "32bit": { + "httpPath": "https://downloads.lambdatest.com/tunnel/v3/windows/32bit/LT_Windows.zip", + "binaryName": "LT_Windows.zip", + "hash": "251cedbb4aeaf4f65d0e772073327832" + }, + "64bit": { + "httpPath": "https://downloads.lambdatest.com/tunnel/v3/windows/64bit/LT_Windows.zip", + "binaryName": "LT_Windows.zip", + "hash": "815260a1d983e79f2a35a732ab178c28" + } + } + }, + "linux": { + "v2": { + "32bit": { + "httpPath": "https://downloads.lambdatest.com/tunnel/linux/32bit/ltcomponent.zip", + "binaryName": "ltcomponent.zip", + "hash": "f3eae3b5b32918e6ab53b694965fbdd2" + }, + "64bit": { + "httpPath": "https://downloads.lambdatest.com/tunnel/linux/64bit/ltcomponent.zip", + "binaryName": "ltcomponent.zip", + "hash": "107d7d5020d181923fe2f207c2ec76cb" + } + }, + "v3": { + "32bit": { + "httpPath": "https://downloads.lambdatest.com/tunnel/v3/linux/32bit/LT_Linux.zip", + "binaryName": "LT_Linux.zip", + "hash": "dfed824375d2e4326034ba8720fb88fd" + }, + "64bit": { + "httpPath": "https://downloads.lambdatest.com/tunnel/v3/linux/64bit/LT_Linux.zip", + "binaryName": "LT_Linux.zip", + "hash": "7ce7a92c80e5968bbc81e610ad12ed04" + } + } + }, + "freebsd": { + "v2": { + "32bit": { + "httpPath": "https://downloads.lambdatest.com/tunnel/freebsd/32bit/ltcomponent.zip", + "binaryName": "ltcomponent.zip", + "hash": "874da2557a6e7d4ee047830092b36f10" + }, + "64bit": { + "httpPath": "https://downloads.lambdatest.com/tunnel/freebsd/64bit/ltcomponent.zip", + "binaryName": "ltcomponent.zip", + "hash": "cd6d2cdca428fa38c94922b9853afe06" + } + }, + "v3": { + "32bit": { + "httpPath": "https://downloads.lambdatest.com/tunnel/v3/freebsd/32bit/LT_Freebsd.zip", + "binaryName": "LT_Freebsd.zip", + "hash": "03389252446fa9c10afd3784cc8c550e" + }, + "64bit": { + "httpPath": "https://downloads.lambdatest.com/tunnel/v3/freebsd/64bit/LT_Freebsd.zip", + "binaryName": "LT_Freebsd.zip", + "hash": "36b42ef31100b8602596ad2eb8b9d0be" + } + } + } + }, + "AuthUrl": "https://accounts.lambdatest.com/api/user/token/auth", + "logEnable": true, + "latest": "3.0.0", + "supportedVersions": [ + "1.0.0", + "1.0.1", + "1.0.2", + "1.0.3", + "1.0.4", + "1.0.5", + "1.0.6", + "1.0.7", + "1.0.8", + "1.0.9", + "1.1.0", + "1.1.1", + "1.1.2", + "1.1.3", + "1.1.4", + "2.0.0", + "2.0.1", + "2.0.2", + "3.0.0-beta.1", + "3.0.0", + "3.0.1", + "3.0.2", + "3.0.3", + "3.0.4", + "3.0.5", + "3.0.6", + "3.0.7", + "3.0.8", + "3.0.9" + ] +} \ No newline at end of file diff --git a/lib/conf/node-tunnel-config.json b/lib/conf/node-tunnel-config.json deleted file mode 100644 index 5ba7269..0000000 --- a/lib/conf/node-tunnel-config.json +++ /dev/null @@ -1 +0,0 @@ -{"tunnelBinary":{"mac":{"32bit":{"httpPath":"https://downloads.lambdatest.com/tunnel/mac/32bit/LT_Mac.zip","binaryName":"LT_Mac.zip","newHttpPath":"https://downloads.lambdatest.com/tunnel/mac/32bit/ltcomponent.zip","newBinaryName":"ltcomponent.zip","hash":"7e0421e977ed8d46b93180c3f840a5ca"},"64bit":{"httpPath":"https://downloads.lambdatest.com/tunnel/mac/64bit/LT_Mac.zip","binaryName":"LT_Mac.zip","newHttpPath":"https://downloads.lambdatest.com/tunnel/mac/64bit/ltcomponent.zip","newBinaryName":"ltcomponent.zip","hash":"8f273e905e9e372f049931f210d3afd0"}},"win":{"32bit":{"httpPath":"https://downloads.lambdatest.com/tunnel/windows/32bit/LT_Windows.zip","binaryName":"LT_Windows.zip","newHttpPath":"https://downloads.lambdatest.com/tunnel/windows/32bit/ltcomponent.zip","newBinaryName":"ltcomponent.zip","hash":"e9b4b5095c708ce241c443434038a888"},"64bit":{"httpPath":"https://downloads.lambdatest.com/tunnel/windows/64bit/LT_Windows.zip","binaryName":"LT_Windows.zip","newHttpPath":"https://downloads.lambdatest.com/tunnel/windows/64bit/ltcomponent.zip","newBinaryName":"ltcomponent.zip","hash":"9a8012732dbea3bda25d20fec4d4f1c8"}},"linux":{"32bit":{"httpPath":"https://downloads.lambdatest.com/tunnel/linux/32bit/LT_Linux.zip","binaryName":"LT_Linux.zip","newHttpPath":"https://downloads.lambdatest.com/tunnel/linux/32bit/ltcomponent.zip","newBinaryName":"ltcomponent.zip","hash":"78a31c2c5d632863f9d7383933e6627a"},"64bit":{"httpPath":"https://downloads.lambdatest.com/tunnel/linux/64bit/LT_Linux.zip","binaryName":"LT_Linux.zip","newHttpPath":"https://downloads.lambdatest.com/tunnel/linux/64bit/ltcomponent.zip","newBinaryName":"ltcomponent.zip","hash":"394ac8c32383b8c42842bc4eeb74f901"}},"freebsd":{"32bit":{"newHttpPath":"https://downloads.lambdatest.com/tunnel/freebsd/32bit/ltcomponent.zip","newBinaryName":"ltcomponent.zip","hash":"fec5a4ff0bc682d9e874fc28c1e5ce96"},"64bit":{"newHttpPath":"https://downloads.lambdatest.com/tunnel/freebsd/64bit/ltcomponent.zip","newBinaryName":"ltcomponent.zip","hash":"7ed86881cdb37c9059443789d6e0f982"}}},"AuthUrl":"https://accounts.lambdatest.com/api/user/token/auth","logEnable":true,"latest":"2.0.2","supportedVersions":["1.0.0","1.0.1","1.0.2","1.0.3","1.0.4","1.0.5","1.0.6","1.0.7","1.0.8","1.0.9","1.1.0","1.1.1","1.1.2","1.1.3","1.1.4","2.0.0","2.0.1","2.0.2","2.0.3","2.0.4","2.0.5","2.0.6","2.0.7","2.0.8","2.0.9","2.0.10","2.0.11","2.0.12","2.0.13","2.0.14","2.0.15","2.0.16","2.0.17","2.0.18","2.0.19","2.1.0","2.1.1","2.1.2","2.1.3","2.1.4","2.1.5","2.1.6","2.1.7","2.1.8","2.1.9","2.1.10","2.1.11","2.1.12","2.1.13","2.1.14","2.1.15","2.1.16","2.1.17","2.1.18","2.1.19","2.2.0","2.2.1","2.2.2","2.2.3","2.2.4","2.2.5","2.2.6","2.2.7","2.2.8","2.2.9","2.2.10","2.2.11","2.2.12","2.2.13","2.2.14","2.2.15","2.2.16","2.2.17","2.2.18","2.2.19","2.3.0","2.3.1","2.3.2","2.3.3","2.3.4","2.3.5","2.3.6","2.3.7","2.3.8","2.3.9","2.3.10","2.3.11","2.3.12","2.3.13","2.3.14","2.3.15","2.3.16","2.3.17","2.3.18","2.3.19","2.4.0","2.4.1","2.4.2","2.4.3","2.4.4","2.4.5","2.4.6","2.4.7","2.4.8","2.4.9","2.4.10","2.4.11","2.4.12","2.4.13","2.4.14","2.4.15","2.4.16","2.4.17","2.4.18","2.4.19","2.5.0","2.5.1","2.5.2","2.5.3","2.5.4","2.5.5","2.5.6","2.5.7","2.5.8","2.5.9","2.5.10","2.5.11","2.5.12","2.5.13","2.5.14","2.5.15","2.5.16","2.5.17","2.5.18","2.5.19","2.6.0","2.6.1","2.6.2","2.6.3","2.6.4","2.6.5","2.6.6","2.6.7","2.6.8","2.6.9","2.6.10","2.6.11","2.6.12","2.6.13","2.6.14","2.6.15","2.6.16","2.6.17","2.6.18","2.6.19","2.7.0","2.7.1","2.7.2","2.7.3","2.7.4","2.7.5","2.7.6","2.7.7","2.7.8","2.7.9","2.7.10","2.7.11","2.7.12","2.7.13","2.7.14","2.7.15","2.7.16","2.7.17","2.7.18","2.7.19","2.8.0","2.8.1","2.8.2","2.8.3","2.8.4","2.8.5","2.8.6","2.8.7","2.8.8","2.8.9","2.8.10","2.8.11","2.8.12","2.8.13","2.8.14","2.8.15","2.8.16","2.8.17","2.8.18","2.8.19","2.9.0","2.9.1","2.9.2","2.9.3","2.9.4","2.9.5","2.9.6","2.9.7","2.9.8","2.9.9","2.9.10","2.9.11","2.9.12","2.9.13","2.9.14","2.9.15","2.9.16","2.9.17","2.9.18","2.9.19"]} \ No newline at end of file diff --git a/lib/config.js b/lib/config.js index b1f332e..2f82d9b 100644 --- a/lib/config.js +++ b/lib/config.js @@ -1,5 +1,4 @@ -var httpTunnelConfigPath = - 'https://s3.amazonaws.com/downloads.lambdatest.com/tunnel/node-tunnel-config.json', +var httpTunnelConfigPath = 'https://downloads.lambdatest.com/tunnel/node-tunnel-config-v3-latest.json', httpTunnelLogUrl = 'https://oinwgsy681.execute-api.us-east-1.amazonaws.com/prod/addLog', https = require('https'), urlParse = require('url'), @@ -82,9 +81,7 @@ function setupLogger_(options, jsonResponse) { reqOptions.agent = new HttpsProxyAgent(proxyOpts); } var req = https.request(reqOptions, res => { - res.on('data', d => { - process.stdout.write(d); - }); + res.on('data', _ => {}); }); req.on('error', _ => {}); diff --git a/lib/tunnel.js b/lib/tunnel.js index dc74440..6797f36 100644 --- a/lib/tunnel.js +++ b/lib/tunnel.js @@ -14,7 +14,8 @@ var childProcess = require('child_process'), package_ = require('../package.json'), packageVersion = package_.version, packageName = package_.name, - usingPorts = []; + usingPorts = [], + globalForceRetries = 0; /** * Tunnel is a function based Class. */ @@ -317,6 +318,9 @@ function runBinary_(self, retries, fnCallback) { self.isProcessRunning = true; isCallback = true; self.getTunnelName(function(tunnelName) { + if (tunnelName === null) { + return fnCallback(true, false); + } console.log(`Tunnel successfully initiated. You can start testing now`); return fnCallback(null, true); }); @@ -348,8 +352,10 @@ function runBinary_(self, retries, fnCallback) { // On exit if process is exit due to unable to start local Server then // Retry else log and exit self.proc.on('exit', function(code) { - if (code && code === 10) { - runBinary_(self, retries - 1, fnCallback); + if (code && code === 3) { + globalForceRetries = retries - 1; + sleep(500); + return runBinary_(self, retries - 1, fnCallback); } console.log(`Tunnel successfully stopped`); logger.log( @@ -477,7 +483,7 @@ function killRunningProcess_(that, fnCallback) { }; var req = http.request(reqOptions, resp => { - resp.on('data', () => {}) + resp.on('data', () => {}); resp.on('end', () => { if (resp.statusCode != 200) { killProcess_(that, fnCallback); @@ -534,6 +540,9 @@ function addArguments_(self, fnCallback) { case 'user': case 'key': case 'port': + case 'dns': + case 'pidfile': + case 'controller': if (value) { binaryArgs.push('--' + key); binaryArgs.push(value); @@ -541,11 +550,13 @@ function addArguments_(self, fnCallback) { break; case 'tunnelname': + case 'n': if (value) { binaryArgs.push('--tunnelName'); binaryArgs.push(value); } break; + case 'proxyhost': if (value) { binaryArgs.push('--proxy-host'); @@ -577,6 +588,7 @@ function addArguments_(self, fnCallback) { case 'localdirectory': case 'localdir': case 'dir': + case 'd': if (value) { binaryArgs.push('--dir'); binaryArgs.push(value); @@ -585,84 +597,96 @@ function addArguments_(self, fnCallback) { case 'env': case 'environment': + case 'e': if (value) { binaryArgs.push('--env'); binaryArgs.push(value); } break; - case 'verbose': - case 'v': + case 'sharedtunnel': + case 'shared-tunnel': if (value) { - binaryArgs.push('--v'); + binaryArgs.push('--shared-tunnel'); } break; - case 'configurationfile': - case 'conffile': - case 'configfile': + case 'logfile': if (value) { - binaryArgs.push('--config'); + binaryArgs.push('--logFile'); binaryArgs.push(value); } break; - - case 'sharedtunnel': - case 'shared-tunnel': + case 'nows': if (value) { - binaryArgs.push('--shared-tunnel'); + binaryArgs.push('--nows'); } break; - - case 'localdomains': - case 'local-domains': + case 'verbose': + case 'v': if (value) { - binaryArgs.push('--local-domains'); + binaryArgs.push('-v'); + } + break; + case 'mode': + if (value && !options['legacy']) { + binaryArgs.push('--' + key); binaryArgs.push(value); } break; - case 'outputconfiguration': - case 'outputconf': - case 'outputconfig': - case 'output-config': - if (value) { - binaryArgs.push('--output-config'); + case 'bypasshosts': + if (value && !options['legacy']) { + binaryArgs.push('--bypassHosts'); binaryArgs.push(value); } break; - case 'dns': - if (value) { - binaryArgs.push('--dns'); + case 'callbackurl': + if (value && !options['legacy']) { + binaryArgs.push('--callbackURL'); binaryArgs.push(value); } break; - case 'pidfile': - if (value) { - binaryArgs.push('--pidfile'); + case 'localdomains': + case 'local-domains': + if (value && !options['legacy']) { + binaryArgs.push('--local-domains'); binaryArgs.push(value); } break; + case 'mitm': + case 'm': + if (value && !options['legacy']) { + binaryArgs.push('--mitm'); + } + break; - case 'pac': - if (value) { - binaryArgs.push('--pac'); + case 'noproxy': + case 'no-proxy': + if (value && !options['legacy']) { + binaryArgs.push('--no-proxy'); binaryArgs.push(value); } break; - case 'logfile': - if (value) { - binaryArgs.push('--logFile'); - binaryArgs.push(value); + case 'ingressonly': + if (value && !options['legacy']) { + binaryArgs.push('--ingress-only'); } break; - case 'controller': - if (value) { - binaryArgs.push('--controller'); - binaryArgs.push(value); + + case 'egressonly': + if (value && !options['legacy']) { + binaryArgs.push('--egress-only'); + } + break; + + case 'sshconntype': + if (value && !options['legacy']) { + binaryArgs.push('--sshConnType'); + binaryArgs.push(value); // will work only if mode === "ssh", allowed values (over_22, over_443, over_ws), can give error if any other value specified } break; } @@ -764,7 +788,7 @@ function getFreePort_(options, retries, fnCallback) { function getBinaryPath_(that, options, fnCallback) { // if path is there then Return path else get from Server if (typeof that.binaryPath == 'undefined') { - that.binary = new TunnelBinary(httpTunnelConfig); + that.binary = new TunnelBinary(httpTunnelConfig, options); var conf = { user: options['user'], key: options['key'] }; // Do this for getting binary using Proxy. if ( @@ -798,7 +822,6 @@ function getBinaryPath_(that, options, fnCallback) { */ function retryTunnelName_(self, infoAPIPort, retries, fnCallback) { try { - var timeoutId = null; // Check whether max retries is reached ?. if (retries >= 0) { // local Server path for getting tunnelName @@ -813,14 +836,11 @@ function retryTunnelName_(self, infoAPIPort, retries, fnCallback) { response.on('end', () => { // After successfully getting name clear the timeout and return name if (response.statusCode === 200) { - clearTimeout_(timeoutId); if (typeof json === 'string') json = JSON.parse(json); return fnCallback((json.data && json.data.tunnelName) || null); } else { - clearTimeout_(timeoutId); - timeoutId = setTimeout(function() { - retryTunnelName_(self, infoAPIPort, retries - 1, fnCallback); - }, 500); + sleep(500); + return retryTunnelName_(self, infoAPIPort, retries - 1, fnCallback); } }); }) @@ -833,14 +853,12 @@ function retryTunnelName_(self, infoAPIPort, retries, fnCallback) { 'Getting error while trying to get the tunnel name from running local server. Error : ' + e ); - clearTimeout_(timeoutId); // wait .5s for next retries - timeoutId = setTimeout(function() { - retryTunnelName_(self, infoAPIPort, retries - 1, fnCallback); - }, 500); + sleep(500); + return retryTunnelName_(self, infoAPIPort, retries - 1, fnCallback); }); } else { - clearTimeout_(timeoutId); + sleep(500); logger.log( self.options['user'], self.options['key'], @@ -848,6 +866,13 @@ function retryTunnelName_(self, infoAPIPort, retries, fnCallback) { self.options, 'Number of retries to to get tunnel name exceeded.' ); + if (globalForceRetries <= 0) { + console.log(` + Failed to run tunnel binary may be due to + Tunnel server ts.lambdatest.com not reachable. Please run curl ts.lambdatest.com/health" to check + OR + Tunnel Binary not compatible to the platform. Please delete .lambdatest folder and run the test again. `); + } return fnCallback(null); } } catch (e) { @@ -881,12 +906,11 @@ function clearTimeout_(timeoutId) { function killProcess_(that, fnCallback) { that.proc.on('exit', function() { if (that.proc.pid) { - setTimeout(function() { - try { - process.kill(that.proc.pid); - } catch (err) {} - return fnCallback(); - }, 500); + sleep(500); + try { + process.kill(that.proc.pid); + } catch (err) {} + return fnCallback(); } return fnCallback(); }); @@ -897,5 +921,11 @@ function killProcess_(that, fnCallback) { that.proc.kill('SIGINT'); } } +function sleep(sleepDuration) { + var now = new Date().getTime(); + while (new Date().getTime() < now + sleepDuration) { + /* do nothing */ + } +} module.exports = Tunnel; module.exports.Tunnel = Tunnel; diff --git a/lib/tunnel_binary.js b/lib/tunnel_binary.js index 1c5ef33..5899014 100644 --- a/lib/tunnel_binary.js +++ b/lib/tunnel_binary.js @@ -1,4 +1,4 @@ -var localTunnelConfig_ = require('./conf/node-tunnel-config.json'), +var localTunnelConfig_ = require('./cfg/node-tunnel-config-v3-latest.json'), logger, https = require('https'), urlParse = require('url'), @@ -7,14 +7,20 @@ var localTunnelConfig_ = require('./conf/node-tunnel-config.json'), unzip = require('unzipper'), fs = require('fs'), path = require('path'), - os = require('os'); + os = require('os'), + executableName = 'LT'; /** * TunnelBinary is a function based Class. */ -function TunnelBinary(httpTunnelConfig) { +function TunnelBinary(httpTunnelConfig, options) { /** * Try to find out binary based on platform arch. */ + this.binaryVersion = 'v3'; + if (options['legacy']) { + this.binaryVersion = 'v2'; + executableName = "ltcomponent"; + } this.httpTunnelConfig = httpTunnelConfig; this.hostOS = process.platform; this.bits = process.arch === 'x64' ? '64bit' : '32bit'; @@ -28,16 +34,16 @@ function TunnelBinary(httpTunnelConfig) { } else { this.platform = 'linux'; } - this.httpPath = this.httpTunnelConfig.jsonResponse.tunnelBinary[this.platform][ + this.httpPath = this.httpTunnelConfig.jsonResponse.binaryLinks[this.platform][this.binaryVersion][ this.bits - ].newHttpPath; - this.binaryName = this.httpTunnelConfig.jsonResponse.tunnelBinary[this.platform][ + ].httpPath; + this.binaryName = this.httpTunnelConfig.jsonResponse.binaryLinks[this.platform][this.binaryVersion][ this.bits - ].newBinaryName; - this.httpHashContents = this.httpTunnelConfig.jsonResponse.tunnelBinary[this.platform][ + ].binaryName; + this.httpHashContents = this.httpTunnelConfig.jsonResponse.binaryLinks[this.platform][this.binaryVersion][ this.bits ].hash; - this.localHashContents = localTunnelConfig_.tunnelBinary[this.platform][this.bits].hash; + this.localHashContents = localTunnelConfig_.binaryLinks[this.platform][this.binaryVersion][this.bits].hash; logger = this.httpTunnelConfig.logger; /** @@ -85,7 +91,7 @@ function TunnelBinary(httpTunnelConfig) { if (this.checkPath_(destParentDir)) { this.rmDir(destParentDir); } - fs.mkdirSync(destParentDir); + fs.mkdirSync(destParentDir, { recursive : true }); // Generate binary path. var binaryPath = path.join(destParentDir, this.binaryName); var fileStream = fs.createWriteStream(binaryPath); @@ -123,7 +129,6 @@ function TunnelBinary(httpTunnelConfig) { }); fileStream.on('close', function() { if (self.checkPath_(binaryPath)) { - var executableName = self.binaryName.split('.')[0]; var unzipBinaryPath = path.join(destParentDir, executableName); var destBinaryName = executableName; if (self.windows) { @@ -197,7 +202,6 @@ function TunnelBinary(httpTunnelConfig) { this.binaryPath_ = function(conf, fnCallback) { try { var destParentDir = this.availableDirs_(); - var executableName = this.binaryName.split('.')[0]; var destBinaryName = executableName; if (this.windows) { destBinaryName += '.exe'; @@ -214,7 +218,7 @@ function TunnelBinary(httpTunnelConfig) { } else { console.log(`Binary is deprecated`); fs.writeFileSync( - __dirname + '/conf/node-tunnel-config.json', + __dirname + '/cfg/node-tunnel-config-v3-latest.json', JSON.stringify(this.httpTunnelConfig.jsonResponse) ); localTunnelConfig_ = this.httpTunnelConfig.jsonResponse; @@ -259,12 +263,17 @@ function TunnelBinary(httpTunnelConfig) { */ this.availableDirs_ = function() { var orderedPathLength = this.orderedPaths.length; + var _path, iCounter = 0; for (var i = 0; i < orderedPathLength; i++) { - var path = this.orderedPaths[i]; - if (this.makePath_(path)) { - return path; + iCounter++; + _path = this.orderedPaths[i]; + if (this.makePath_(_path)) { + break; } } + if (iCounter < orderedPathLength) { + return _path; + } throw Error('Error trying to download LambdaTest Tunnel binary'); }; @@ -275,7 +284,7 @@ function TunnelBinary(httpTunnelConfig) { this.makePath_ = function(path) { try { if (!this.checkPath_(path)) { - fs.mkdirSync(path); + fs.mkdirSync(path, { recursive : true }); } return true; } catch (e) { @@ -324,7 +333,7 @@ function TunnelBinary(httpTunnelConfig) { fs.rmdirSync(dir); } }; - this.orderedPaths = [path.join(this.homeDir_(), '.lambdatest'), process.cwd(), os.tmpdir()]; + this.orderedPaths = [path.join(this.homeDir_(), '.lambdatest', this.binaryVersion), path.join(process.cwd(), this.binaryVersion), path.join(os.tmpdir(), this.binaryVersion )]; } module.exports = TunnelBinary; diff --git a/package.json b/package.json index 282be86..3e3f5be 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@lambdatest/node-tunnel", - "version": "2.0.2", + "version": "3.0.0", "description": "Nodejs bindings for LambdaTest Tunnel", "main": "index.js", "repository": {