-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrowsy_persistence.js
113 lines (98 loc) · 4.03 KB
/
drowsy_persistence.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
// Generated by CoffeeScript 1.7.1
(function() {
var DrowsyPersistence, events, http, httpRequest,
__hasProp = {}.hasOwnProperty,
__extends = 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; },
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
http = require('http');
httpRequest = require('request');
events = require('events');
DrowsyPersistence = (function(_super) {
var actionMethodMap;
__extends(DrowsyPersistence, _super);
function DrowsyPersistence(config) {
this.config = config;
}
DrowsyPersistence.prototype.added = function() {
return console.log("Drowsy persistence enabled. Broadcasts will be saved to " + (this.drowsyUrl()) + " ...");
};
DrowsyPersistence.prototype.incoming = function(message, callback) {
var cb2;
if (message.channel.match(/^\/meta\//)) {
return callback(message);
}
cb2 = function(message) {
console.log("Calling callback!");
return callback(message);
};
return this.persistInDrowsy(message, cb2);
};
actionMethodMap = {
create: 'POST',
update: 'PUT',
patch: 'PATCH',
"delete": 'DELETE'
};
DrowsyPersistence.prototype.persistInDrowsy = function(message, callback) {
var baseUri, channel, cid, hostname, path, payload, port, reqOpts, scheme, _ref, _ref1, _ref2;
cid = message.clientId;
channel = message.channel;
payload = message.data;
if (this.config.uri != null) {
baseUri = this.config.uri.replace(/\/$/, '');
} else {
scheme = (_ref = this.config.scheme) != null ? _ref : 'http';
hostname = (_ref1 = this.config.hostname) != null ? _ref1 : 'localhost';
port = (_ref2 = this.config.port) != null ? _ref2 : 9292;
baseUri = "" + scheme + "://" + hostname + ":" + port;
}
path = channel;
if ((this.config.username != null) && (this.config.password != null)) {
reqOpts = {
uri: "" + baseUri + "/" + path,
method: actionMethodMap[payload.action],
json: payload.data,
auth: {
user: this.config.username,
pass: this.config.password
}
};
} else {
reqOpts = {
uri: "" + baseUri + "/" + path,
method: actionMethodMap[payload.action],
json: payload.data
};
}
return httpRequest(reqOpts, (function(_this) {
return function(err, res, json) {
var errMsg, _i, _ref3, _results;
if (err) {
_this.emit('persist_failure', cid, channel, payload, err);
errMsg = "\n\n!!! Error while sending request to Drowsy! Is the Drowsy server up and running at " + (_this.drowsyUrl()) + "?\n " + (err.toString());
console.error(errMsg);
message.error = errMsg;
callback(message);
throw err;
}
if (_ref3 = res.statusCode, __indexOf.call((function() {
_results = [];
for (_i = 200; _i < 299; _i++){ _results.push(_i); }
return _results;
}).apply(this), _ref3) >= 0) {
_this.emit('persist_success', cid, channel, payload, res, json);
} else {
_this.emit('persist_failure', cid, channel, payload, res, json);
message.error = "Failed to persist data in Drowsy; Drowsy responded with a " + res.statusCode;
}
return callback(message);
};
})(this));
};
DrowsyPersistence.prototype.drowsyUrl = function() {
return "" + this.config.scheme + "://" + this.config.hostname + ":" + this.config.port;
};
return DrowsyPersistence;
})(events.EventEmitter);
exports.DrowsyPersistence = DrowsyPersistence;
}).call(this);