forked from jmreidy/sauce-tunnel
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
158 lines (139 loc) · 3.93 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* npm */
var chalk = require('chalk');
var request = require('request').defaults({jar:false});
var split = require('split');
/* 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',
'linux32': '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 == null) ? true : tunneled;
this.baseUrl = ["https://", this.user, ':', this.key, '@saucelabs.com', '/rest/v1/', this.user].join("");
this.extraFlags = extraFlags;
this.id = null;
}
util.inherits(SauceTunnel, EventEmitter);
SauceTunnel.prototype.openTunnel = function(callback) {
var me = this;
// win32, darwin or linux
var platform = os.platform();
// Special case: 32bit linux?
platform += (platform === 'linux' && os.arch() === 'ia32') ? '32' : '';
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;
this.proc.stdout.pipe(split()).on('data', function(data) {
if (!data.match(/^\[-u,/g)) {
me.emit('verbose:debug', data);
}
if (data.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);
}
}
var match = data.match(/Tunnel ID\: ([a-z0-9]{32})/);
if (match) {
me.id = match[1];
}
});
this.proc.stderr.pipe(split()).on('data', function(data) {
me.emit('log:error', data);
});
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.id,
json: true
}, function (err, resp, body) {
if (!err && resp.statusCode === 200) {
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 self = this,
callbackArg;
this.proc.on('exit', function () {
callback(callbackArg);
});
this.killTunnel(function (err) {
// When deleting the tunnel via the REST API succeeds, then Sauce Connect exits automatically.
// Otherwise kill the process. Don't care with the tunnel, it will time out.
if (err) {
callbackArg = err;
self.proc.kill();
}
});
};
SauceTunnel.prototype.kill = function(callback) {
if (this.proc) {
this.proc.on('exit', function () {
callback();
});
this.proc.kill();
}
else {
callback();
}
};