diff --git a/index.js b/index.js index a31fa9d..ab8a6c4 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,7 @@ 'use strict'; var inherit = require('inherit'), - q = require('q'), + Promise = require('bluebird'), childProcess = require('child_process'), util = require('util'); @@ -11,6 +11,20 @@ var DEFAULTS = { SSH_PORT: 22 }; +function defer() { + var resolve, reject; + var promise = new Promise(function () { + resolve = arguments[0]; + reject = arguments[1]; + }); + + return { + resolve: resolve, + reject: reject, + promise: promise + }; +} + var Tunnel = inherit({ /** * Constuctor @@ -33,8 +47,8 @@ var Tunnel = inherit({ this._localPort = opts.localport; this._connectTimeout = opts.connectTimeout || DEFAULTS.CONNECT_TIMEOUT; this._tunnel = null; - this._tunnelDeferred = q.defer(); - this._closeDeferred = q.defer(); + this._tunnelDeferred = defer(); + this._closeDeferred = defer(); }, /** @@ -85,13 +99,13 @@ var Tunnel = inherit({ */ close: function () { if (!this._tunnel) { - return q(); + return Promise.resolve(); } var _this = this; this._tunnel.kill('SIGTERM'); - return this._closeDeferred.promise.timeout(3000).fail(function () { + return this._closeDeferred.promise.timeout(3000).catch(function () { _this._tunnel.kill('SIGKILL'); return _this._closeTunnel(-1); }); @@ -144,16 +158,16 @@ Tunnel.openWithRetries = function (opts, retries) { function retry_(retriesLeft) { if (!retriesLeft) { - return q.reject(util.format('ERROR: failed to create tunnel after %d attempts', retries)); + return Promise.reject(util.format('ERROR: failed to create tunnel after %d attempts', retries)); } var tunnel = new Tunnel(opts); return tunnel.open() .then(function () { - return q.resolve(tunnel); + return Promise.resolve(tunnel); }) - .fail(function () { + .catch(function () { return tunnel.close() .then(retry_.bind(null, retriesLeft - 1)); }); diff --git a/package.json b/package.json index 7f2c004..d954a1d 100644 --- a/package.json +++ b/package.json @@ -39,8 +39,8 @@ "unit-test": "istanbul test _mocha --recursive test" }, "dependencies": { - "inherit": "^2.2.2", - "q": "^1.4.1" + "bluebird": "^3.5.0", + "inherit": "^2.2.2" }, "devDependencies": { "chai": "^3.3.0", @@ -50,7 +50,6 @@ "jscs": "^2.1.1", "jshint": "^2.8.0", "mocha": "^2.3.3", - "qemitter": "^1.0.0", "sinon": "^1.16.1", "sinon-chai": "^2.8.0", "lodash": "^3.10.1" diff --git a/test/index.js b/test/index.js index 3ca874a..3377aef 100644 --- a/test/index.js +++ b/test/index.js @@ -1,6 +1,6 @@ var Tunnel = require('../'), _ = require('lodash'), - q = require('q'), + Promise = require('bluebird'), childProcess = require('child_process'), events = require('events'), util = require('util'); @@ -44,7 +44,7 @@ describe('Tunnel', function () { it('should set default timeout as 10 seconds', function () { var tunnel = createTunnel(), - timeout = sandbox.stub(q.makePromise.prototype, 'timeout'); // promise constructor available like this?! + timeout = sandbox.stub(Promise.prototype, 'timeout'); tunnel.open(); @@ -59,7 +59,7 @@ describe('Tunnel', function () { var result = tunnel.open(); - expect(q.isPromise(result)).to.be.true; + expect(result).to.be.an.instanceof(Promise); }); describe('tunnel spawn and events', function () { @@ -215,7 +215,7 @@ describe('Tunnel', function () { tunnel = createTunnel(); tunnel.open(); - expect(q.isPromise(tunnel.close())).to.be.true; + expect(tunnel.close()).to.be.an.instanceof(Promise); }); it('should try to kill tunnel using SIGTERM', function () { @@ -252,14 +252,14 @@ describe('Tunnel', function () { describe('openWithRetries', function () { beforeEach(function () { sandbox.stub(Tunnel.prototype); - Tunnel.prototype.open.returns(q()); - Tunnel.prototype.close.returns(q()); + Tunnel.prototype.open.returns(Promise.resolve()); + Tunnel.prototype.close.returns(Promise.resolve()); }); it('should return promise', function () { var result = Tunnel.openWithRetries(defaultOpts()); - expect(q.isPromise(result)).to.be.true; + expect(result).to.be.an.instanceof(Promise); }); it('should try to open tunnel with passed opts', function () { @@ -271,13 +271,13 @@ describe('Tunnel', function () { }); it('should resolve promise if tunnel opened successfully', function () { - Tunnel.prototype.open.returns(q()); + Tunnel.prototype.open.returns(Promise.resolve()); return expect(Tunnel.openWithRetries(defaultOpts())).to.be.eventually.resolved; }); it('should resolve promise with tunnel instance', function () { - Tunnel.prototype.open.returns(q()); + Tunnel.prototype.open.returns(Promise.resolve()); return Tunnel.openWithRetries(defaultOpts()).then(function (tunnel) { expect(tunnel).to.be.instanceOf(Tunnel); @@ -285,23 +285,23 @@ describe('Tunnel', function () { }); it('should reject tunnel if failed to open tunnel after retries', function () { - Tunnel.prototype.open.returns(q.reject()); + Tunnel.prototype.open.returns(Promise.reject()); return expect(Tunnel.openWithRetries(defaultOpts)).to.be.eventually.rejected; }); it('should retry to create tunnel 5 times by default', function () { - Tunnel.prototype.open.returns(q.reject()); + Tunnel.prototype.open.returns(Promise.reject()); - return Tunnel.openWithRetries(defaultOpts()).fail(function () { + return Tunnel.openWithRetries(defaultOpts()).catch(function () { expect(Tunnel.prototype.open.callCount).to.be.equal(5); }); }); it('should retry create tunnel retries times', function () { - Tunnel.prototype.open.returns(q.reject()); + Tunnel.prototype.open.returns(Promise.reject()); - return Tunnel.openWithRetries(defaultOpts(), 10).fail(function () { + return Tunnel.openWithRetries(defaultOpts(), 10).catch(function () { expect(Tunnel.prototype.open.callCount).to.be.equal(10); }); });