diff --git a/.gitignore b/.gitignore index ff4601c..194c116 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ *.swo node_modules npm-debug.log +.env diff --git a/lib/client.js b/lib/client.js index 8ef90e3..ba46ca0 100644 --- a/lib/client.js +++ b/lib/client.js @@ -1,11 +1,11 @@ /* jshint node:true, unused:true */ -var https = require('https'); -var http = require('http'); -var qs = require('qs'); -var Q = require('q'); -var url = require('url'); -var debug = require('debug')('node-gitter'); +var https = require('https'); +var http = require('http'); +var qs = require('qs'); +var Promise = require('bluebird'); +var url = require('url'); +var debug = require('debug')('node-gitter'); var Client = function(token, opts) { opts = opts || {}; @@ -44,7 +44,6 @@ var Client = function(token, opts) { Client.prototype.request = function(method, path, opts) { opts = opts || {}; var self = this; - var defer = Q.defer(); var headers = { 'Authorization': 'Bearer ' + this.token, @@ -67,47 +66,47 @@ Client.prototype.request = function(method, path, opts) { debug('%s %s', req_opts.method, req_opts.path); - var req = scheme.request(req_opts, function(res) { - // Accommodate webpack/browserify - if(res.setEncoding) { - res.setEncoding('utf-8'); - } - - self.rateLimit = res.headers['x-ratelimit-limit']; - self.remaining = res.headers['x-ratelimit-remaining']; - - var data = ''; - res.on('data' , function(chunk) { - data += chunk; - }); - - res.on('end', function() { - var body; - try { - body = JSON.parse(data); - } catch(err) { - defer.reject(new Error(res.statusCode + ' ' + data)); + return new Promise(function(resolve, reject) { + var req = scheme.request(req_opts, function(res) { + // Accommodate webpack/browserify + if(res.setEncoding) { + res.setEncoding('utf-8'); } - if (res.statusCode !== 200) { - defer.reject(new Error(res.statusCode + ' ' + data)); - } else { - defer.resolve(body); - } + self.rateLimit = res.headers['x-ratelimit-limit']; + self.remaining = res.headers['x-ratelimit-remaining']; + + var data = ''; + res.on('data' , function(chunk) { + data += chunk; + }); + + res.on('end', function() { + var body; + try { + body = JSON.parse(data); + } catch(err) { + reject(new Error(res.statusCode + ' ' + data)); + } + + if (res.statusCode !== 200) { + reject(new Error(res.statusCode + ' ' + data)); + } else { + resolve(body); + } + }); }); - }); - req.on('error', function(err) { - defer.reject(err); - }); - - if (opts.body) { - req.write(JSON.stringify(opts.body)); - } + req.on('error', function(err) { + reject(err); + }); - req.end(); + if (opts.body) { + req.write(JSON.stringify(opts.body)); + } - return defer.promise; + req.end(); + }); }; Client.prototype.stream = function(path, cb) { diff --git a/lib/faye.js b/lib/faye.js index 82df014..becb78c 100644 --- a/lib/faye.js +++ b/lib/faye.js @@ -1,7 +1,9 @@ /* jshint node:true, unused:true */ +'use strict'; -var Faye = require('gitter-faye'); +var Halley = require('halley'); var EventEmitter = require('eventemitter3'); +var Promise = require('bluebird'); // Authentication extension for Faye var ClientAuthExt = function(opts) { @@ -41,7 +43,7 @@ var FayeClient = function(token, opts) { this.subscriptions = {}; - this.client = new Faye.Client(host, {timeout: 60, retry: 5, interval: 1}); + this.client = new Halley.Client(host); this.client.addExtension(new ClientAuthExt({token: token, clientType: opts.clientType})); this.client.addExtension(new SnapshotExt({subscriptions: this.subscriptions})); }; @@ -52,6 +54,8 @@ FayeClient.prototype.subscribeTo = function(resource, eventName) { var emitter = new EventEmitter(); var subscription = this.client.subscribe(resource, function(msg) { emitter.emit(eventName, msg); + }).catch(function(err) { + emitter.emit('error', err); }); this.subscriptions[resource] = { @@ -66,10 +70,15 @@ FayeClient.prototype.subscribeTo = function(resource, eventName) { FayeClient.prototype.disconnect = function() { var self = this; - Object.keys(this.subscriptions).forEach(function(sub) { - self.subscriptions[sub].subscription.cancel(); - self.subscriptions[sub].emitter.removeAllListeners(); - }); + Object.keys(this.subscriptions) + .forEach(function(sub) { + var subscriptionsInfo = self.subscriptions[sub]; + subscriptionsInfo.emitter.removeAllListeners(); + }); + + return this.client.disconnect(); + + }; module.exports = FayeClient; diff --git a/lib/gitter.js b/lib/gitter.js index 3e562e5..fa7230b 100644 --- a/lib/gitter.js +++ b/lib/gitter.js @@ -1,4 +1,5 @@ /* jshint node:true, unused:true */ +'use strict'; var Client = require('./client.js'); var Users = require('./users.js'); diff --git a/lib/rooms.js b/lib/rooms.js index 3b8aea6..c5ac382 100644 --- a/lib/rooms.js +++ b/lib/rooms.js @@ -1,8 +1,9 @@ /* jshint node:true, unused:true */ +'use strict'; var util = require('util'); var EventEmitter = require('eventemitter3'); -var Q = require('q'); +var Promise = require('bluebird'); var Room = function(attrs, client, faye) { EventEmitter.call(this); @@ -18,8 +19,8 @@ var Room = function(attrs, client, faye) { if (typeof this.users !== 'function') { var users = this.users; this.users = function() { - return Q.resolve(users); - } + return Promise.resolve(users); + }; } this.client = client; @@ -81,17 +82,23 @@ Room.prototype.subscribe = function() { var resourcePath = '/api/v1/rooms/' + this.id + '/' + resource; var events = this.faye.subscribeTo(resourcePath, resourcePath) events.on(resourcePath, function(msg) { - this.emit(resource, msg); + this.emit(resource, msg); }.bind(this)); }.bind(this)); }; Room.prototype.unsubscribe = function() { - ['chatMessages', 'events', 'users'].forEach(function(resource) { + var promises = Promise.all(['chatMessages', 'events', 'users'].forEach(function(resource) { var resourcePath = '/api/v1/rooms/' + this.id + '/' + resource; var meta = this.faye.subscriptions[resourcePath]; - if (meta) meta.subscription.cancel(); - }.bind(this)); + if (meta) { + meta.subscription.then(function(subscription) { + return subscription.unsubscribe(); + }); + } + }.bind(this))); + + return Promise.all(promises) }; // DEPRECATED Rooms is now an event emitter and all you need is to diff --git a/lib/users.js b/lib/users.js index 960e9fb..b311fc8 100644 --- a/lib/users.js +++ b/lib/users.js @@ -1,7 +1,7 @@ /* jshint node:true, unused:true */ var User = function(attrs, client, faye) { - // API path to Users + // API path to Users this.path = '/user'; if (attrs) @@ -32,7 +32,7 @@ User.prototype.findById = function(id, cb) { .then(function(userData) { return new User(userData, this.client, this.faye); }.bind(this)); - + return cb ? user.nodeify(cb) : user; }; @@ -42,7 +42,7 @@ User.prototype.findByUsername = function(username, cb) { var userData = results.results[0]; return new User(userData, this.client, this.faye); }.bind(this)); - + return cb ? user.nodeify(cb) : user; }; @@ -55,7 +55,7 @@ User.prototype.findByUsername = function(username, cb) { }); User.prototype.markAsRead = function(roomId, chatIds, cb) { - var resourcePath = this.path + '/' + this.id + '/troupes/' + roomId + '/unreadItems'; + var resourcePath = this.path + '/' + this.id + '/rooms/' + roomId + '/unreadItems'; var resource = this.client.post(resourcePath, {body: {chat: chatIds}}); return cb ? resource.nodeify(cb) : resource; }; diff --git a/package.json b/package.json index 75bb352..dcef0f3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-gitter", - "version": "1.2.8", + "version": "2.0.3", "description": "Gitter client", "main": "lib/gitter.js", "scripts": { @@ -20,15 +20,16 @@ }, "homepage": "https://github.com/gitterHQ/node-gitter", "dependencies": { - "debug": "^0.8.1", + "bluebird": "^3.1.1", + "debug": "^2.2.0", "eventemitter3": "^0.1.6", - "faye": "~1.0.1", "gitter-faye": "^1.1.0-e", + "halley": "^0.4.2", "q": "~1.0.1", "qs": "~1.2.1" }, "devDependencies": { - "mocha": "~1.18.2", - "retire": "latest" + "mocha": "^2.3.4", + "retire": "^1.1.2" } } diff --git a/test/rooms-test.js b/test/rooms-test.js index bf33aab..3ed93f6 100644 --- a/test/rooms-test.js +++ b/test/rooms-test.js @@ -69,7 +69,7 @@ describe('Gitter Rooms', function() { }).then(function(rooms) { var check = rooms.some(function(room) { return room.name === 'node-gitter/yacht'; }); assert.equal(false, check); - }).fin(function() { + }).finally(function() { // Join the room again for the rest of the tests gitter.rooms.join('node-gitter/yacht'); }).nodeify(done); @@ -77,10 +77,10 @@ describe('Gitter Rooms', function() { it('should not be able to join an invalid room', function(done) { gitter.rooms.join('some-invalid-room').then(function() { - }).fail(function(err) { + }).catch(function(err) { assert(err); done(); - }).fail(done); + }).catch(done); }); it('should be able to send a message', function(done) { @@ -136,7 +136,7 @@ describe('Gitter Rooms', function() { }); setTimeout(function() { room.send(msg); }, 500); - }).fail(done); + }).catch(done); }); it('should be able to subscribe to a room', function(done) { @@ -164,7 +164,7 @@ describe('Gitter Rooms', function() { }); setTimeout(function() { room.send(msg); }, 750); - }).fail(done); + }).catch(done); }); diff --git a/test/users-test.js b/test/users-test.js index 9b62182..a4d89d7 100644 --- a/test/users-test.js +++ b/test/users-test.js @@ -72,7 +72,7 @@ describe('Gitter Users', function() { it('should fail when fidning an invalid user', function(done) { gitter.users.find('invalid').then(function() { assert(false); - }).fail(function() { + }).catch(function() { done(); }); });