-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscriber.js
152 lines (138 loc) · 4.22 KB
/
subscriber.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
var http = require('http');
var url = require('url');
var hRequest = require('./hRequest');
var querystring = require('querystring');
var subscriptions = require('./subscriptions');
ClientSub.prototype.subscribe = function(subOptions, clientOptions, acceptHeaders){
if(subOptions.name !== undefined && !this._subscriptions.subExists(subOptions.name)){
if(subOptions.path !== undefined && subOptions.hub !== undefined && subOptions.topic !== undefined){
acceptHeaders = acceptHeaders !== undefined ? acceptHeaders : { };
clientOptions = clientOptions !== undefined ? clientOptions : { };
var sub = this._subscriptions.add(subOptions, clientOptions, acceptHeaders);
handleRequest.call(this, sub, 'subscribe');
}
}
}
ClientSub.prototype.unsubscribe = function(subName){
if(this._subscriptions.subExists(subName)){
var sub = this._subscriptions.getSubByName(subName);
handleRequest.call(this, sub, 'unsubscribe');
}
}
function handleRequest(sub, mode){
var subOptions = sub.subOptions;
var clientOptions = sub.clientOptions;
var postData = {
'hub.verify' : 'sync',
'hub.topic' : subOptions.topic,
'hub.callback' : 'http://' + this._host + subOptions.path
}
postData['hub.mode'] = mode;
if(mode == 'unsubscribing'){
sub.markUnsubscribing();
}
if(subOptions.secret !== undefined){
postData['hub.secret'] = subOptions.secret;
}
if(clientOptions.extraData !== undefined && typeof clientOptions.extraData === 'object'){
for(param in clientOptions.extraData){
postData[param] = clientOptions.extraData[param];
}
}
var httpRequest;
if(clientOptions.extraHeaders !== undefined && typeof clientOptions.extraHeaders === 'object') {
httpRequest = hRequest.post(subOptions.hub, postData, clientOptions.extraHeaders);
} else {
httpRequest = hRequest.post(subOptions.hub, postData);
}
httpRequest.then(function(data){
if(data.code === 204 || data.code === 202){
if(mode == 'subscribe'){
sub.markSubscribed();
} else {
sub.markUnsubscribed();
}
} else {
console.log('[Unexpected answer]');
console.log(data);
}
});
}
function trigger(event){
var args = [].slice.call(arguments, 1);
if(this._events[event]){
this._events[event].forEach(function(fn){
fn.apply(null, args);
});
}
}
ClientSub.prototype.on = function(event, fn){
if(this._events[event] === undefined){
this._events[event] = [];
}
this._events[event].push(fn);
}
function ClientSub(host, dir, httpServer){
if(httpServer !== undefined){
this._server = httpServer;
} else {
//Init server
this._server = http.createServer();
}
var that = this;
this._server.on('request', function(req, res){
req.setEncoding('utf8');
var params = url.parse(req.url, true);
var msg = '';
req.on('data', function(data){
msg += data;
});
req.on('end', function(){
var subscription = that._subscriptions.getSubByPath(params.pathname);
if(subscription){
if((subscription.isValidating() || subscription.isUnsubscribing()) && Object.keys(params.query).length > 0){
res.write(params.query['hub.challenge']);
} else {
res.writeHead(200, subscription.acceptHeaders);
if(req.headers['X-Hub-Signature'] === undefined || subscription.auth(req.headers['X-Hub-Signature'])) {
if(req.headers['content-type']){
if(req.headers['content-type'] == 'application/json'){
try {
var msgObj = JSON.parse(msg);
trigger.call(that, 'update', subscription, 'json', msgObj, req.headers);
} catch (e){
console.log('Malformed JSON data');
}
} else if(req.headers['content-type'] == 'application/x-www-form-urlencoded'){
try {
var msgObj = querystring.parse(msg);
trigger.call(that, 'update', subscription, 'post', msgObj, req.headers);
}catch(e){
console.log('Malformed POST data');
}
}
}
} else {
console.log('Auth Failed');
}
}
} else {
res.writeHead(404);
}
res.end();
});
});
this._subscriptions = subscriptions.load(dir);
//Hostname
this._host = host;
this._events = { };
}
ClientSub.prototype.listen = function(port){
//Listen on port
this._server.listen(port);
}
module.exports = {
load : function(host, dir){
return new ClientSub(host, dir);
}
}