flowdock
Version:
Flowdock client library for node.js
151 lines (132 loc) • 4.4 kB
JavaScript
// Generated by CoffeeScript 1.9.3
(function() {
var JSONStream, Stream, backoff, baseURL, events, request, url,
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
hasProp = {}.hasOwnProperty;
url = require('url');
events = require('events');
request = require('request');
JSONStream = require('./json_stream');
baseURL = function() {
return url.parse(process.env.FLOWDOCK_STREAM_URL || 'https://stream.flowdock.com/flows');
};
backoff = function(backoff, errors, operator) {
if (operator == null) {
operator = '*';
}
return Math.min(backoff.max, (operator === '+' ? errors : Math.pow(2, errors - 1)) * backoff.delay);
};
Stream = (function(superClass) {
extend(Stream, superClass);
function Stream(auth1, flows1, params1) {
this.auth = auth1;
this.flows = flows1;
this.params = params1 != null ? params1 : {};
this.networkErrors = 0;
this.responseErrors = 0;
this.on('reconnecting', (function(_this) {
return function(timeout) {
return setTimeout(function() {
return _this.connect();
}, timeout);
};
})(this));
}
Stream.prototype.connect = function() {
var errorHandler;
if (this.disconnecting) {
return;
}
errorHandler = (function(_this) {
return function(error) {
_this.networkErrors += 1;
_this.emit('clientError', 0, 'Network error');
return _this.emit('reconnecting', backoff(Stream.backoff.network, _this.networkErrors, '+'));
};
})(this);
this.request = request(this.options()).on('response', (function(_this) {
return function(response) {
var parser;
_this.request.removeListener('error', errorHandler);
_this.networkErrors = 0;
if (response.statusCode >= 400) {
_this.responseErrors += 1;
_this.emit('clientError', response.statusCode);
return _this.emit('reconnecting', backoff(Stream.backoff.error, _this.responseErrors, '*'));
} else {
_this.responseErrors = 0;
parser = new JSONStream();
parser.on('data', function(message) {
return _this.emit('message', message);
});
_this.request.on('abort', function() {
parser.removeAllListeners();
_this.emit('disconnected');
return _this.emit('end');
});
parser.on('end', function() {
parser.removeAllListeners();
_this.emit('disconnected');
_this.emit('clientError', 0, 'Disconnected');
return _this.emit('reconnecting', 0);
});
_this.request.pipe(parser);
return _this.emit('connected');
}
};
})(this));
this.request.once('error', errorHandler);
return this.request;
};
Stream.prototype.options = function() {
var key, options, ref, value;
options = {
uri: baseURL(),
qs: {
filter: this.flows.join(',')
},
method: 'GET',
headers: {
'Authorization': this.auth,
'Accept': 'application/json'
}
};
ref = this.params;
for (key in ref) {
value = ref[key];
options.qs[key] = value;
}
return options;
};
Stream.prototype.end = function() {
this.disconnecting = true;
if (this.request) {
this.request.abort();
this.request.removeAllListeners();
return this.request = void 0;
}
};
Stream.prototype.close = function() {
console.warn('DEPRECATED, use Stream#end() instead');
return this.end();
};
return Stream;
})(events.EventEmitter);
Stream.connect = function(auth, flows, params) {
var stream;
stream = new Stream(auth, flows, params);
stream.connect();
return stream;
};
Stream.backoff = {
network: {
delay: 200,
max: 10000
},
error: {
delay: 2000,
max: 120000
}
};
module.exports = Stream;
}).call(this);