-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
120 lines (93 loc) · 2.83 KB
/
index.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
var http = require('http')
var HttpHashRouter = require('http-hash-router')
var catNames = require('cat-names')
var concat = require('concat-stream')
var pumpify = require('pumpify')
var corsify = require('corsify')
var debug = require('debug')('cat-lobby')
var limitStream = require('./limit-stream.js')
module.exports = function create (lobbyOpts) {
if (!lobbyOpts) lobbyOpts = {}
var router = HttpHashRouter()
var state = {
pings: {},
pongs: {},
timeouts: []
}
var utils = {
makeName: makeName,
rnd: rnd,
uploadStream: uploadStream
}
// old alpha API
router.set('/ping', deprecated)
router.set('/pong/:name', deprecated)
router.set('/ping/:name', deprecated)
router.set('/pongs/:name', deprecated)
var v1API = require('./versions/v1.js')(state, utils)
router.set('/v1', v1API.create)
router.set('/v1/:name/ping', v1API.ping)
router.set('/v1/:name/pong', v1API.pong)
router.set('/v1/:name/pings', v1API.pings)
router.set('/v1/:name/pongs', v1API.pongs)
return createServer(router)
function deprecated (req, res, opts, cb) {
cb(new Error('This version of ScreenCat is unsupported, please upgrade.'))
}
function uploadStream (cb) {
var limiter = limitStream(1024 * 5) // 5kb max
var concatter = concat(function concatted (buff) {
cb(buff)
})
return pumpify(limiter, concatter)
}
function makeName () {
var n = [utils.rnd(), utils.rnd(), utils.rnd()].join('-')
if (state.pings[n]) return utils.makeName()
return n
}
function rnd () {
return catNames.random().toLowerCase().replace(/\s/g, '-')
}
function createServer (router) {
var cors = corsify({
'Access-Control-Allow-Methods': 'POST, GET'
})
var server = http.createServer(handler)
function handler (req, res) {
debug(req.url, 'request/response start')
// redirect https
if (req.headers['x-forwarded-proto'] === 'http') {
var httpsURL = 'https://' + req.headers.host + req.url
debug('https redirect', httpsURL)
res.writeHead(302, {'Location': httpsURL })
res.end()
return
}
req.on('end', function logReqEnd () {
debug(req.url, 'request end')
})
res.on('end', function logResEnd () {
debug(req.url, 'response end')
})
cors(route)(req, res)
function route (req, res) {
router(req, res, {}, onError)
}
function onError (err) {
if (err) {
debug('error', {path: req.url, message: err.message})
res.statusCode = err.statusCode || 500
res.end(JSON.stringify({name: err.message}))
}
}
}
server.on('close', function closed () {
// to prevent process from hanging open
state.timeouts.forEach(function each (t) {
clearTimeout(t)
})
})
return server
}
}