-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
95 lines (76 loc) · 2.46 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
'use strict'
const crypto = require('crypto')
const litesocket = require('litesocket')
const http = require('http')
const bl = require('bl')
const events = new (require('events').EventEmitter)()
http.ServerResponse.prototype.status = function(code) {
this.statusCode = code
return this
}
const routes = {
// an endpoint that just gives some info- and causes that info to be emitted
// (just for testing)
'GET/info': (req, res) => {
const info = JSON.stringify({ listeners: app.listenerCount('sse') })
app.emit('sse', info)
res.writeHead(200, { 'content-type': 'application/json' })
res.end(info)
},
// Add an endpoint that will send all events via SSE.
'GET/': (req, res) => {
// do not allow too many listeners!
if (app.listenerCount('sse') >= app.getMaxListeners()) {
return res.status(502).end()
}
litesocket(req, res, () => {
const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress
const fn = (data) => {
if(data === 'ping') {
return litesocket.sendComment(res, 'ping')
}
litesocket.send(res, data)
}
const removeListener = (timeout) => {
console.log('removing listener @ ' + ip + (timeout||''))
app.removeListener('sse', fn)
}
const timeout = () => {
removeListener(' (timeout)')
res.end()
}
console.log('new listener @ ' + ip)
app.on('sse', fn)
res.on('close', removeListener)
// force disconnect after 20 min
setTimeout(timeout, 1200000)
})
},
'POST/': (req, res) => {
const event = req.headers['x-github-event']
if(!event) return res.end()
req.pipe(bl(function (err, data) {
if (err) return res.status(400).end()
try {
data = JSON.parse(data.toString())
} catch (e) {
return res.status(400).end()
}
const source = data.repository ? data.repository.full_name : data.organization.login
data.action = data.action ? event + '.' + data.action : event
console.log('event@%s: %s', source, data.action)
app.emit('sse', JSON.stringify(data, null, 2))
res.end()
}))
}
}
const port = process.env.PORT || 3000
var app = http.createServer((req, res) => {
const handler = routes[req.method+req.url]
if(!handler) return res.status(404).end()
handler(req, res)
})
.listen(port, () => {
console.log('hook-relay listening on port', port)
})
setInterval(() => app.emit('sse', 'ping'), 57000);