Skip to content

Commit d53a9c8

Browse files
committed
feat: use bluebird instead q and qemitter
1 parent e76ea95 commit d53a9c8

File tree

3 files changed

+38
-25
lines changed

3 files changed

+38
-25
lines changed

index.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
var inherit = require('inherit'),
4-
q = require('q'),
4+
Promise = require('bluebird'),
55
childProcess = require('child_process'),
66
util = require('util');
77

@@ -11,6 +11,20 @@ var DEFAULTS = {
1111
SSH_PORT: 22
1212
};
1313

14+
function defer() {
15+
var resolve, reject;
16+
var promise = new Promise(function() {
17+
resolve = arguments[0];
18+
reject = arguments[1];
19+
});
20+
21+
return {
22+
resolve: resolve,
23+
reject: reject,
24+
promise: promise
25+
};
26+
}
27+
1428
var Tunnel = inherit({
1529
/**
1630
* Constuctor
@@ -33,8 +47,8 @@ var Tunnel = inherit({
3347
this._localPort = opts.localport;
3448
this._connectTimeout = opts.connectTimeout || DEFAULTS.CONNECT_TIMEOUT;
3549
this._tunnel = null;
36-
this._tunnelDeferred = q.defer();
37-
this._closeDeferred = q.defer();
50+
this._tunnelDeferred = defer();
51+
this._closeDeferred = defer();
3852
},
3953

4054
/**
@@ -85,13 +99,13 @@ var Tunnel = inherit({
8599
*/
86100
close: function () {
87101
if (!this._tunnel) {
88-
return q();
102+
return Promise.resolve();
89103
}
90104

91105
var _this = this;
92106

93107
this._tunnel.kill('SIGTERM');
94-
return this._closeDeferred.promise.timeout(3000).fail(function () {
108+
return this._closeDeferred.promise.timeout(3000).catch(function () {
95109
_this._tunnel.kill('SIGKILL');
96110
return _this._closeTunnel(-1);
97111
});
@@ -144,16 +158,16 @@ Tunnel.openWithRetries = function (opts, retries) {
144158

145159
function retry_(retriesLeft) {
146160
if (!retriesLeft) {
147-
return q.reject(util.format('ERROR: failed to create tunnel after %d attempts', retries));
161+
return Promise.reject(util.format('ERROR: failed to create tunnel after %d attempts', retries));
148162
}
149163

150164
var tunnel = new Tunnel(opts);
151165

152166
return tunnel.open()
153167
.then(function () {
154-
return q.resolve(tunnel);
168+
return Promise.resolve(tunnel);
155169
})
156-
.fail(function () {
170+
.catch(function () {
157171
return tunnel.close()
158172
.then(retry_.bind(null, retriesLeft - 1));
159173
});

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
"unit-test": "istanbul test _mocha --recursive test"
4040
},
4141
"dependencies": {
42-
"inherit": "^2.2.2",
43-
"q": "^1.4.1"
42+
"bluebird": "^3.5.0",
43+
"inherit": "^2.2.2"
4444
},
4545
"devDependencies": {
4646
"chai": "^3.3.0",
@@ -50,7 +50,6 @@
5050
"jscs": "^2.1.1",
5151
"jshint": "^2.8.0",
5252
"mocha": "^2.3.3",
53-
"qemitter": "^1.0.0",
5453
"sinon": "^1.16.1",
5554
"sinon-chai": "^2.8.0",
5655
"lodash": "^3.10.1"

test/index.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var Tunnel = require('../'),
22
_ = require('lodash'),
3-
q = require('q'),
3+
Promise = require('bluebird'),
44
childProcess = require('child_process'),
55
events = require('events'),
66
util = require('util');
@@ -44,7 +44,7 @@ describe('Tunnel', function () {
4444

4545
it('should set default timeout as 10 seconds', function () {
4646
var tunnel = createTunnel(),
47-
timeout = sandbox.stub(q.makePromise.prototype, 'timeout'); // promise constructor available like this?!
47+
timeout = sandbox.stub(Promise.prototype, 'timeout');
4848

4949
tunnel.open();
5050

@@ -59,7 +59,7 @@ describe('Tunnel', function () {
5959

6060
var result = tunnel.open();
6161

62-
expect(q.isPromise(result)).to.be.true;
62+
expect(result).to.be.an.instanceof(Promise);
6363
});
6464

6565
describe('tunnel spawn and events', function () {
@@ -215,7 +215,7 @@ describe('Tunnel', function () {
215215
tunnel = createTunnel();
216216
tunnel.open();
217217

218-
expect(q.isPromise(tunnel.close())).to.be.true;
218+
expect(tunnel.close()).to.be.an.instanceof(Promise);
219219
});
220220

221221
it('should try to kill tunnel using SIGTERM', function () {
@@ -252,14 +252,14 @@ describe('Tunnel', function () {
252252
describe('openWithRetries', function () {
253253
beforeEach(function () {
254254
sandbox.stub(Tunnel.prototype);
255-
Tunnel.prototype.open.returns(q());
256-
Tunnel.prototype.close.returns(q());
255+
Tunnel.prototype.open.returns(Promise.resolve());
256+
Tunnel.prototype.close.returns(Promise.resolve());
257257
});
258258

259259
it('should return promise', function () {
260260
var result = Tunnel.openWithRetries(defaultOpts());
261261

262-
expect(q.isPromise(result)).to.be.true;
262+
expect(result).to.be.an.instanceof(Promise);
263263
});
264264

265265
it('should try to open tunnel with passed opts', function () {
@@ -271,37 +271,37 @@ describe('Tunnel', function () {
271271
});
272272

273273
it('should resolve promise if tunnel opened successfully', function () {
274-
Tunnel.prototype.open.returns(q());
274+
Tunnel.prototype.open.returns(Promise.resolve());
275275

276276
return expect(Tunnel.openWithRetries(defaultOpts())).to.be.eventually.resolved;
277277
});
278278

279279
it('should resolve promise with tunnel instance', function () {
280-
Tunnel.prototype.open.returns(q());
280+
Tunnel.prototype.open.returns(Promise.resolve());
281281

282282
return Tunnel.openWithRetries(defaultOpts()).then(function (tunnel) {
283283
expect(tunnel).to.be.instanceOf(Tunnel);
284284
});
285285
});
286286

287287
it('should reject tunnel if failed to open tunnel after retries', function () {
288-
Tunnel.prototype.open.returns(q.reject());
288+
Tunnel.prototype.open.returns(Promise.reject());
289289

290290
return expect(Tunnel.openWithRetries(defaultOpts)).to.be.eventually.rejected;
291291
});
292292

293293
it('should retry to create tunnel 5 times by default', function () {
294-
Tunnel.prototype.open.returns(q.reject());
294+
Tunnel.prototype.open.returns(Promise.reject());
295295

296-
return Tunnel.openWithRetries(defaultOpts()).fail(function () {
296+
return Tunnel.openWithRetries(defaultOpts()).catch(function () {
297297
expect(Tunnel.prototype.open.callCount).to.be.equal(5);
298298
});
299299
});
300300

301301
it('should retry create tunnel retries times', function () {
302-
Tunnel.prototype.open.returns(q.reject());
302+
Tunnel.prototype.open.returns(Promise.reject());
303303

304-
return Tunnel.openWithRetries(defaultOpts(), 10).fail(function () {
304+
return Tunnel.openWithRetries(defaultOpts(), 10).catch(function () {
305305
expect(Tunnel.prototype.open.callCount).to.be.equal(10);
306306
});
307307
});

0 commit comments

Comments
 (0)