forked from jmreidy/sauce-tunnel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
136 lines (121 loc) · 3.44 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* npm */
var chalk = require('chalk');
var request = require('request').defaults({jar:false});
/* core */
var util = require('util');
var os = require('os');
var path = require('path');
var proc = require('child_process');
var EventEmitter = require('events').EventEmitter;
var binaries = {
'darwin': 'sc',
'linux': 'sc',
'win32': 'sc.exe'
};
module.exports = SauceTunnel;
function SauceTunnel(user, key, identifier, tunneled, extraFlags) {
EventEmitter.call(this);
this.user = user;
this.key = key;
this.identifier = identifier || 'Tunnel'+new Date().getTime();
this.tunneled = tunneled;
this.baseUrl = ["https://", this.user, ':', this.key, '@saucelabs.com', '/rest/v1/', this.user].join("");
this.extraFlags = extraFlags;
}
util.inherits(SauceTunnel, EventEmitter);
SauceTunnel.prototype.openTunnel = function(callback) {
var me = this;
// win32, darwin or linux
var platform = os.platform();
var executable = binaries[platform];
if (!executable) {
throw new Error(platform + ' platform is not supported');
}
var args = ['-u', this.user, '-k', this.key];
if (this.identifier) {
args.push("-i", this.identifier);
}
if (this.extraFlags) {
args = args.concat(this.extraFlags);
}
var cmd = path.join(__dirname, 'vendor', platform, 'bin/', executable);
this.proc = proc.spawn(cmd, args);
callback.called = false;
var buf = '';
this.proc.stdout.on('data', function(d) {
var data = typeof d !== 'undefined' ? d.toString() : '';
buf += data.replace(/[\r\n]/g, '');
if (typeof data === 'string' && !data.match(/^\[-u,/g)) {
me.emit('verbose:debug', data.replace(/[\n\r]/g, ''));
}
if (typeof data === 'string' && buf.match(/Sauce Connect is up, you may start your tests/)) {
me.emit('verbose:ok', '=> Sauce Labs Tunnel established');
if (!callback.called) {
callback.called = true;
callback(true);
}
}
});
this.proc.stderr.on('data', function(data) {
me.emit('log:error', data.toString().replace(/[\n\r]/g, ''));
});
var self = this;
this.proc.on('exit', function(code) {
me.emit('verbose:ok', 'Sauce Labs Tunnel disconnected ', code);
if (!callback.called) {
callback.called = true;
callback(false);
}
});
};
SauceTunnel.prototype.getTunnels = function(callback) {
request({
url: this.baseUrl + '/tunnels',
json: true
}, function(err, resp, body) {
callback(body);
});
};
SauceTunnel.prototype.killTunnel = function(callback) {
if (!this.tunneled) {
return callback();
}
this.emit('verbose:debug', 'Trying to kill tunnel');
request({
method: "DELETE",
url: this.baseUrl + "/tunnels/" + this.identifier,
json: true
}, function (err, resp, body) {
if (!err) {
this.emit('verbose:debug', 'Tunnel Closed');
}
else {
this.emit('log:error', 'Error closing tunnel');
}
callback(err);
}.bind(this));
};
SauceTunnel.prototype.start = function(callback) {
var me = this;
if (!this.tunneled) {
return callback(true);
}
this.emit('verbose:writeln', chalk.inverse("=> Sauce Labs trying to open tunnel"));
this.openTunnel(function(status) {
callback(status);
});
};
SauceTunnel.prototype.stop = function(callback) {
var killProc = function (err) {
if (this.proc) {
this.proc.on('exit', function () {
callback(err);
});
this.proc.kill();
}
else {
callback(err);
}
}.bind(this);
this.killTunnel(killProc);
};