forked from logux/server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreporter.js
207 lines (177 loc) · 5.13 KB
/
reporter.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
var yyyymmdd = require('yyyy-mm-dd')
var stripAnsi = require('strip-ansi')
var chalk = require('chalk')
var path = require('path')
var os = require('os')
var pkg = require('./package.json')
var PADDING = ' '
var SEPARATOR = os.EOL + os.EOL
var NEXT_LINE = os.EOL === '\n' ? '\r\v' : os.EOL
function rightPag (str, length) {
var add = length - stripAnsi(str).length
for (var i = 0; i < add; i++) str += ' '
return str
}
function time (c) {
return c.dim('at ' + yyyymmdd.withTime(module.exports.now()))
}
function line (c, label, color, message) {
var labelFormat = c.bold[color].bgBlack.inverse
var messageFormat = c.bold[color]
return rightPag(labelFormat(label), 8) +
messageFormat(message) + ' ' +
time(c)
}
function info (c, str) {
return line(c, ' INFO ', 'green', str)
}
function warn (c, str) {
return line(c, ' WARN ', 'yellow', str)
}
function error (c, str) {
return line(c, ' ERROR ', 'red', str)
}
function params (c, type, fields) {
var max = 0
var current
for (var i = 0; i < fields.length; i++) {
current = fields[i][0].length + 2
if (current > max) max = current
}
return fields.map(function (field) {
return PADDING + rightPag(field[0] + ': ', max) + c.bold(field[1])
}).join(NEXT_LINE)
}
function errorParams (c, type, client) {
if (!client) {
return ''
} else {
var user = client.user ? client.user.id : 'unauthenticated'
return params(c, 'error', [
['User ID', user],
['Node ID', client.nodeId || 'unknown'],
['Subprotocol', client.sync.otherSubprotocol || 'unknown'],
['IP address', client.remoteAddress]
])
}
}
function note (c, str) {
return PADDING + c.grey(str)
}
function prettyStackTrace (c, err, root) {
if (root.slice(-1) !== path.sep) root += path.sep
return err.stack.split('\n').slice(1).map(function (i) {
i = i.replace(/^\s*/, PADDING)
var match = i.match(/(\s+at [^(]+ \()([^)]+)\)/)
if (!match || match[2].indexOf(root) !== 0) {
return c.red(i)
} else {
match[2] = match[2].slice(root.length)
if (match[2].indexOf('node_modules') !== -1) {
return c.red(match[1] + match[2] + ')')
} else {
return c.yellow(match[1] + match[2] + ')')
}
}
}).join(NEXT_LINE)
}
var reporters = {
listen: function listen (c, app) {
var url
if (app.listenOptions.server) {
url = 'Custom HTTP server'
} else {
url = (app.listenOptions.cert ? 'wss://' : 'ws://') +
app.listenOptions.host + ':' + app.listenOptions.port
}
var dev = app.env === 'development'
return [
info(c, 'Logux server is listening'),
params(c, 'info', [
['Logux server', pkg.version],
['PID', app.options.pid],
['Node ID', app.options.nodeId],
['Environment', app.env],
['Subprotocol', app.options.subprotocol],
['Supports', app.options.supports],
['Listen', url]
]),
(dev ? note(c, 'Press Ctrl-C to shutdown server') : '')
]
},
connect: function connect (c, app, ip) {
return [
info(c, 'Client was connected'),
params(c, 'info', [['IP address', ip]])
]
},
authenticated: function authenticated (c, app, client) {
return [
info(c, 'User was authenticated'),
params(c, 'info', [
['User ID', client.user.id],
['Node ID', client.nodeId || 'unknown'],
['Subprotocol', client.sync.otherSubprotocol],
['Logux protocol', client.sync.otherProtocol.join('.')],
['IP address', client.remoteAddress]
])
]
},
disconnect: function disconnect (c, app, client) {
var user = client.user ? client.user.id : 'unauthenticated'
return [
info(c, 'Client was disconnected'),
params(c, 'info', [
['User ID', user],
['Node ID', client.nodeId || 'unknown'],
['IP address', client.remoteAddress]
])
]
},
destroy: function destroy (c) {
return [
info(c, 'Shutting down Logux server')
]
},
runtimeError: function runtimeError (c, app, client, err) {
var prefix = err.name + ': ' + err.message
if (err.name === 'Error') prefix = err.message
return [
error(c, prefix),
prettyStackTrace(c, err, app.options.root),
errorParams(c, 'error', client)
]
},
syncError: function syncError (c, app, client, err) {
var prefix
if (err.received) {
prefix = 'SyncError from client: ' + err.description
} else {
prefix = 'SyncError: ' + err.description
}
return [
error(c, prefix),
errorParams(c, 'error', client)
]
},
clientError: function clientError (c, app, client, err) {
return [
warn(c, 'Client error: ' + err.description),
errorParams(c, 'warn', client)
]
}
}
module.exports = function (type, app) {
var c = chalk
if (app.env !== 'development') {
c = new chalk.constructor({ enabled: false })
}
var reporter = reporters[type]
var args = [c].concat(Array.prototype.slice.call(arguments, 1))
return reporter.apply({ }, args).filter(function (i) {
return i !== ''
}).join(NEXT_LINE) + SEPARATOR
}
module.exports.now = function () {
return new Date()
}