-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathradar.js
172 lines (155 loc) · 4.29 KB
/
radar.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const http = require('http')
const Radar = require('../../index.js')
const Middleware = require('../../src/middleware')
const { QuotaManager, LegacyAuthManager } = Middleware
const Persistence = require('persistence')
const { Type } = require('../../src/core')
const Minilog = require('minilog')
const logger = Minilog('lib_radar')
const formatter = require('./formatter.js')
const assertHelper = require('./assert_helper.js')
let serverStarted = false
let radar
let httpServer
if (process.env.verbose) {
let minilogPipe = Minilog
// Configure log output
if (process.env.radar_log) {
minilogPipe = minilogPipe.pipe(Minilog.suggest.deny(/.*/, process.env.radar_log))
}
minilogPipe.pipe(formatter)
.pipe(Minilog.backends.nodeConsole.formatColor)
.pipe(process.stdout)
}
function p404 (req, res) {
res.statusCode = 404
res.end('404 Not Found')
}
Type.add([
{ // For client.auth.test
name: 'client_auth',
expression: /^message:\/client_auth\/disabled$/,
type: 'MessageList',
policy: { cache: true, maxAgeSeconds: 30 },
authProvider: {
authorize: function () { return false }
}
},
{
name: 'client_auth',
expression: /^message:\/client_auth\/enabled$/,
type: 'MessageList',
authProvider: {
authorize: function () { return true }
}
},
{ // For client.message.test
name: 'cached_chat',
expression: /^message:\/dev\/cached_chat\/(.+)/,
type: 'MessageList',
policy: { cache: true, maxAgeSeconds: 30 }
},
{ // For client.presence.test
name: 'short_expiry',
expression: /^presence:\/dev\/test/,
type: 'Presence',
policy: { userExpirySeconds: 1 }
},
{
name: 'short_stream',
expression: /^stream:\/dev\/short_stream\/(.+)/,
type: 'Stream',
policy: { maxLength: 2 }
},
{
name: 'uncached_stream',
expression: /^stream:\/dev\/uncached_stream\/(.+)/,
type: 'Stream',
policy: { maxLength: 0 }
},
{
name: 'general control',
type: 'Control',
expression: /^control:/
},
{ // For client.presence.test
name: 'limited',
expression: /^presence:\/dev\/limited/,
type: 'Presence',
policy: {
limit: 1
}
}
])
const Service = {}
Service.start = function (configuration, callbackFn) {
logger.debug('creating radar', configuration)
httpServer = http.createServer(p404)
// Add sentry defaults for testing.
configuration.sentry = assertHelper.SentryDefaults
const RadarServer = Radar.server
radar = new RadarServer()
radar.use(new QuotaManager())
radar.use(new LegacyAuthManager())
radar.ready.then(function () {
httpServer.listen(configuration.port, function () {
logger.debug('httpServer listening on', configuration.port)
serverStarted = true
Persistence.delWildCard('*', function () {
logger.info('Persistence cleared')
callbackFn()
})
})
})
radar.attach(httpServer, configuration)
}
Service.stop = function (arg, callbackFn) {
let serverTimeout
logger.info('stop')
httpServer.on('close', function () {
logger.info('httpServer closed')
if (serverStarted) {
clearTimeout(serverTimeout)
logger.info('Calling callbackFn, close event')
serverStarted = false
callbackFn()
}
})
Persistence.delWildCard('*', function () {
radar.terminate(function () {
logger.info('radar terminated')
if (!serverStarted) {
logger.info('httpServer terminated')
callbackFn()
} else {
logger.info('closing httpServer')
logger.info('connections left', httpServer._connections)
httpServer.close()
serverTimeout = setTimeout(function () {
// Failsafe, because server.close does not always
// throw the close event within time.
if (serverStarted) {
serverStarted = false
logger.info('Calling callbackFn, timeout')
callbackFn()
}
}, 200)
}
})
})
}
process.on('message', function (message) {
const command = JSON.parse(message)
const complete = function (error) {
logger.debug('complete: ', error, command.action)
process.send(JSON.stringify({
action: command.action,
error: error
}))
}
if (Service[command.action]) {
Service[command.action](command.arg, complete)
} else {
complete('NotFound')
}
})