|
| 1 | +var fs = require('fs'), |
| 2 | + childProcess = require('child_process'), |
| 3 | + os = require('os'), |
| 4 | + ZipBinary = require('./ZipBinary'), |
| 5 | + log = require('./helper').log; |
| 6 | + |
| 7 | +function BrowserStackTunnel(options) { |
| 8 | + var params = [], |
| 9 | + startCallback = null, |
| 10 | + stopCallback = null, |
| 11 | + doneStart = function(params) { |
| 12 | + if(startCallback !== null) { |
| 13 | + startCallback(params); |
| 14 | + } |
| 15 | + }, |
| 16 | + doneStop = function(params) { |
| 17 | + if(stopCallback !== null) { |
| 18 | + stopCallback(params); |
| 19 | + } |
| 20 | + }; |
| 21 | + |
| 22 | + var binary; |
| 23 | + switch (os.platform()) { |
| 24 | + case 'linux': |
| 25 | + switch (os.arch()) { |
| 26 | + case 'x64': |
| 27 | + binary = new ZipBinary('linux', 'x64', options.path); |
| 28 | + break; |
| 29 | + case 'ia32': |
| 30 | + binary = new ZipBinary('linux', 'ia32', options.path); |
| 31 | + break; |
| 32 | + } |
| 33 | + break; |
| 34 | + case 'darwin': |
| 35 | + binary = new ZipBinary('darwin', 'x64', options.path); |
| 36 | + break; |
| 37 | + default: |
| 38 | + binary = new ZipBinary('win32', null, options.path, 'exe'); |
| 39 | + break; |
| 40 | + } |
| 41 | + |
| 42 | + this.stdoutData = ''; |
| 43 | + this.tunnel = null; |
| 44 | + |
| 45 | + if (options.hosts) { |
| 46 | + params.push(options.hosts); |
| 47 | + } |
| 48 | + |
| 49 | + if (options.localIdentifier) { |
| 50 | + params.push('-localIdentifier', options.localIdentifier); |
| 51 | + } |
| 52 | + |
| 53 | + if (options.verbose) { |
| 54 | + params.push('-v'); |
| 55 | + } |
| 56 | + |
| 57 | + if (options.force) { |
| 58 | + params.push('-force'); |
| 59 | + } |
| 60 | + |
| 61 | + if (options.forcelocal) { |
| 62 | + params.push('-forcelocal'); |
| 63 | + } |
| 64 | + |
| 65 | + if (options.onlyAutomate) { |
| 66 | + params.push('-onlyAutomate'); |
| 67 | + } |
| 68 | + |
| 69 | + if (options.proxyHost) { |
| 70 | + params.push('-proxyHost', options.proxyHost); |
| 71 | + } |
| 72 | + |
| 73 | + if (options.proxyPort) { |
| 74 | + params.push('-proxyPort', options.proxyPort); |
| 75 | + } |
| 76 | + |
| 77 | + if (options.proxyUser) { |
| 78 | + params.push('-proxyUser', options.proxyUser); |
| 79 | + } |
| 80 | + |
| 81 | + if (options.proxyPass) { |
| 82 | + params.push('-proxyPass', options.proxyPass); |
| 83 | + } |
| 84 | + |
| 85 | + if (options.key == null || !options.key.trim()) { |
| 86 | + options.key = process.env.BROWSERSTACK_ACCESS_KEY; |
| 87 | + } |
| 88 | + |
| 89 | + this.state = 'stop'; |
| 90 | + this.stateMatchers = { |
| 91 | + 'already_running': new RegExp('\\*\\*Error: There is another JAR already running'), |
| 92 | + 'invalid_key': new RegExp('\\*\\*Error: You provided an invalid key'), |
| 93 | + 'connection_failure': new RegExp('\\*\\*Error: Could not connect to server'), |
| 94 | + 'newer_available': new RegExp('There is a new version of BrowserStackTunnel.jar available on server'), |
| 95 | + 'started': new RegExp('Press Ctrl-C to exit') |
| 96 | + }; |
| 97 | + |
| 98 | + this.updateState = function (data) { |
| 99 | + var state; |
| 100 | + this.stdoutData += data.toString(); |
| 101 | + for (state in this.stateMatchers) { |
| 102 | + if (this.stateMatchers.hasOwnProperty(state) && this.stateMatchers[state].test(this.stdoutData) && this.state !== state) { |
| 103 | + this.state = state; |
| 104 | + switch(state) { |
| 105 | + case('newer_available'): |
| 106 | + log.warn('BrowserStackTunnel: binary out of date'); |
| 107 | + this.killTunnel(); |
| 108 | + var self = this; |
| 109 | + binary.update(function () { |
| 110 | + self.startTunnel(); |
| 111 | + }); |
| 112 | + break; |
| 113 | + case('invalid_key'): |
| 114 | + doneStart(new Error('Invalid key')); |
| 115 | + break; |
| 116 | + case('connection_failure'): |
| 117 | + doneStart(new Error('Could not connect to server')); |
| 118 | + break; |
| 119 | + case('already_running'): |
| 120 | + doneStart(new Error('child already started')); |
| 121 | + break; |
| 122 | + default: |
| 123 | + doneStart(); |
| 124 | + break; |
| 125 | + } |
| 126 | + break; |
| 127 | + } |
| 128 | + } |
| 129 | + }; |
| 130 | + |
| 131 | + this.killTunnel = function () { |
| 132 | + if (this.tunnel) { |
| 133 | + this.tunnel.stdout.removeAllListeners('data'); |
| 134 | + this.tunnel.stderr.removeAllListeners('data'); |
| 135 | + this.tunnel.removeAllListeners('error'); |
| 136 | + this.tunnel.kill(); |
| 137 | + this.tunnel = null; |
| 138 | + } |
| 139 | + }; |
| 140 | + |
| 141 | + this.exit = function () { |
| 142 | + if (this.state !== 'started' && this.state !== 'newer_available') { |
| 143 | + doneStart(new Error('child failed to start:\n' + this.stdoutData)); |
| 144 | + } else if (this.state !== 'newer_available') { |
| 145 | + this.state = 'stop'; |
| 146 | + doneStop(); |
| 147 | + } |
| 148 | + }; |
| 149 | + |
| 150 | + this.cleanUp = function () { |
| 151 | + this.stdoutData = ''; |
| 152 | + }; |
| 153 | + |
| 154 | + this._startTunnel = function () { |
| 155 | + this.cleanUp(); |
| 156 | + log.info('Local started with args: ' + JSON.stringify(params)); |
| 157 | + this.tunnel = childProcess.spawn(binary.command, binary.args.concat([options.key]).concat(params)); |
| 158 | + this.tunnel.stdout.on('data', this.updateState.bind(this)); |
| 159 | + this.tunnel.stderr.on('data', this.updateState.bind(this)); |
| 160 | + this.tunnel.on('error', this.killTunnel.bind(this)); |
| 161 | + this.tunnel.on('exit', this.exit.bind(this)); |
| 162 | + }; |
| 163 | + |
| 164 | + this.startTunnel = function () { |
| 165 | + var self = this; |
| 166 | + if (!fs.existsSync(binary.path)) { |
| 167 | + log.warn('Binary not present'); |
| 168 | + binary.update(function () { |
| 169 | + self._startTunnel(); |
| 170 | + }); |
| 171 | + } else { |
| 172 | + try { |
| 173 | + this._startTunnel(); |
| 174 | + }catch(e) { |
| 175 | + log.warn('The downloaded binary might be corrupt. Retrying download'); |
| 176 | + binary.update(function () { |
| 177 | + self._startTunnel(); |
| 178 | + }); |
| 179 | + } |
| 180 | + } |
| 181 | + }; |
| 182 | + |
| 183 | + this.start = function (callback) { |
| 184 | + startCallback = callback; |
| 185 | + if (this.state === 'started') { |
| 186 | + doneStart(new Error('child already started')); |
| 187 | + } else { |
| 188 | + this.startTunnel(); |
| 189 | + } |
| 190 | + }; |
| 191 | + |
| 192 | + this.stop = function (callback) { |
| 193 | + if (this.state !== 'stop') { |
| 194 | + stopCallback = callback; |
| 195 | + } else if (this.state !== 'started') { |
| 196 | + var err = new Error('child not started'); |
| 197 | + callback(err); |
| 198 | + } |
| 199 | + |
| 200 | + this.killTunnel(); |
| 201 | + }; |
| 202 | +} |
| 203 | + |
| 204 | +module.exports = BrowserStackTunnel; |
0 commit comments