forked from logux/server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
80 lines (72 loc) · 2.78 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
var BaseServer = require('./base-server')
var reporter = require('./reporter')
/**
* End-user API to create Logux server.
*
* @param {object} options Server options.
* @param {string} options.subprotocol Server current application
* subprotocol version in SemVer format.
* @param {string} options.supports npm’s version requirements for client
* subprotocol version.
* @param {string|number} [options.nodeId] Unique server ID. Be default,
* `server:` with compacted UUID.
* @param {string} [options.root=process.cwd()] Application root to load files
* and show errors.
* @param {number} [options.timeout=20000] Timeout in milliseconds
* to disconnect connection.
* @param {number} [options.ping=10000] Milliseconds since last message to test
* connection by sending ping.
* @param {function} [options.timer] Timer to use in log. Will be default
* timer with server `nodeId`, by default.
* @param {Store} [options.store] Store to save log. Will be `MemoryStore`,
* by default.
* @param {"production"|"development"} [options.env] Development or production
* server mode. By default,
* it will be taken from
* `NODE_ENV` environment
* variable. On empty
* `NODE_ENV` it will
* be `"development"`.
*
* @example
* import { Server } from 'logux-server'
* const app = new Server({
* subprotocol: '1.0.0',
* supports: '1.x || 0.x',
* root: __dirname
* })
* if (app.env === 'production') {
* app.listen({ cert: 'cert.pem', key: 'key.pem' })
* } else {
* app.listen()
* }
*
* @class
* @extends BaseServer
*/
function Server (options) {
options.pid = process.pid
BaseServer.call(this, options, function () {
process.stderr.write(reporter.apply(reporter, arguments))
})
var app = this
function onError (e) {
app.reporter('runtimeError', app, undefined, e)
app.destroy().then(function () {
process.exit(1)
})
}
process.on('uncaughtException', onError)
process.on('unhandledRejection', onError)
function onExit () {
app.destroy().then(function () {
process.exit(0)
})
}
process.on('SIGINT', onExit)
this.unbind.push(function () {
process.removeListener('SIGINT', onExit)
})
}
Server.prototype = BaseServer.prototype
module.exports = Server