sse.js
1.52 KB
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
var Options = require('options')
, util = require('util')
, url = require('url')
, querystring = require('querystring')
, events = require('events')
, SSEClient = require('./sseclient');
RegExp.quote = function(str) {
return (str+'').replace(/[.?*+^$[\]\\(){}|-]/g, "\\$&");
};
module.exports = SSE;
module.exports.Client = SSEClient;
function SSE(httpServer, options) {
options = new Options({
path : '/sse',
verifyRequest : null
}).merge(options);
this.server = httpServer;
var oldListeners = this.server.listeners('request');
this.server.removeAllListeners('request');
var self = this;
this.server.on('request', function(req, res) {
var u = url.parse(req.url);
var pathname = u.pathname.replace(/^\/{2,}/, '/');
if (self.matchesPath(pathname, options.value.path) && (options.value.verifyRequest == null || options.value.verifyRequest(req))) {
self.handleRequest(req, res, u.query);
}
else {
for (var i = 0, l = oldListeners.length; i < l; ++i) {
oldListeners[i].call(self.server, req, res);
}
}
});
}
util.inherits(SSE, events.EventEmitter);
SSE.prototype.handleRequest = function(req, res, query) {
var client = new SSEClient(req, res);
client.initialize();
this.emit('connection', client, querystring.parse(query));
}
SSE.prototype.matchesPath = function(queryPath, matchPath) {
var match = RegExp.quote(matchPath).replace('\\*', '.*');
return RegExp('^' + match + '$').test(queryPath);
}