-
-
Notifications
You must be signed in to change notification settings - Fork 95
/
server.js
129 lines (108 loc) · 3.56 KB
/
server.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
var http = require('http')
var https = require('https')
var corsify = require('corsify')
var collect = require('stream-collector')
var pump = require('pump')
var iterate = require('random-iterate')
var limiter = require('size-limit-stream')
var eos = require('end-of-stream')
var flushHeaders = function (res) {
if (res.flushHeaders) {
res.flushHeaders()
} else {
if (!res._header) res._implicitHeader()
res._send('')
}
}
module.exports = function (opts) {
var channels = {}
var maxBroadcasts = (opts && opts.maxBroadcasts) || Infinity
var subs = 0
var get = function (channel) {
if (channels[channel]) return channels[channel]
var sub = {name: channel, subscribers: [], heartbeat: null}
sub.heartbeat = setInterval(heartbeater(sub), 30 * 1000)
channels[channel] = sub
return channels[channel]
}
var cors = corsify({
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE',
'Access-Control-Allow-Headers': 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept, Authorization'
})
var onRequest = cors(function (req, res) {
if (req.url === '/') {
res.setHeader('Content-Type', 'application/json; charset=utf-8')
flushHeaders(res)
res.end(JSON.stringify({name: 'signalhub', version: require('./package').version, subscribers: subs}, null, 2) + '\n')
return
}
if (req.url.slice(0, 4) !== '/v1/') {
res.statusCode = 404
res.end()
return
}
var name = req.url.slice(4).split('?')[0]
if (req.method === 'POST') {
collect(pump(req, limiter(64 * 1024)), function (err, data) {
if (err) return res.end()
if (!channels[name]) return res.end()
var channel = get(name)
server.emit('publish', channel.name, data)
data = Buffer.concat(data).toString()
var ite = iterate(channel.subscribers)
var next
var cnt = 0
while ((next = ite()) && cnt++ < maxBroadcasts) {
next.write('data: ' + data + '\n\n')
}
res.end()
})
return
}
if (req.method === 'GET') {
res.setHeader('Content-Type', 'text/event-stream; charset=utf-8')
res.setHeader('Cache-Control', 'no-cache')
// Disable NGINX request buffering
res.setHeader('X-Accel-Buffering', 'no')
var app = name.split('/')[0]
var channelNames = name.slice(app.length + 1)
channelNames.split(',').forEach(function (channelName) {
var channel = get(app + '/' + channelName)
server.emit('subscribe', channel.name)
channel.subscribers.push(res)
subs++
eos(res, function () {
subs--
var i = channel.subscribers.indexOf(res)
if (i > -1) channel.subscribers.splice(i, 1)
if (!channel.subscribers.length && channel === channels[channel.name]) {
clearInterval(channel.heartbeat)
delete channels[channel.name]
}
})
})
flushHeaders(res)
return
}
res.statusCode = 404
res.end()
})
var useHttps = !!(opts && opts.key && opts.cert)
var server = useHttps ? https.createServer(opts) : http.createServer()
server.on('request', onRequest)
server.on('close', function () {
var names = Object.keys(channels)
for (var i = 0; i < names.length; i++) {
clearInterval(channels[names[i]].heartbeat)
}
})
return server
}
function heartbeater (sub) {
return function () {
for (var i = 0; i < sub.subscribers.length; i++) {
sub.subscribers[i].write(':heartbeat signal\n\n')
}
}
}