-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.js
163 lines (133 loc) · 4.78 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
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
var jsonToObject = require('json-stream-to-object')
var stringify = require('fast-safe-stringify')
var serverRouter = require('server-router')
var fromString = require('from2-string')
var loghttp = require('log-http')
var envobj = require('envobj')
var assert = require('assert')
var http = require('http')
var pino = require('pino')
var pump = require('pump')
module.exports = Merry
function Merry (opts) {
if (!(this instanceof Merry)) return new Merry(opts)
this.opts = opts || {}
assert.equal(typeof this.opts, 'object', 'Merry: opts should be type object')
this.log = pino({ level: this.opts.logLevel || 'info', name: 'merry' }, this.opts.logStream || process.stdout)
this.router = serverRouter({ default: '/notFoundHandlerRoute' })
this.env = envobj(this.opts.env || {})
this.middleware = []
this._port = null
}
Merry.prototype.route = function (method, route, handler) {
// if a default method is provided, second param is handler
if (method === 'default') return this.defaultRoute(route)
assert.ok(Array.isArray(method) || typeof method === 'string', 'Merry.route: method should be type string or an array of strings')
assert.equal(typeof handler, 'function', 'Merry.route: handler should be type function')
assert.equal(typeof route, 'string', 'Merry.route: route should be type string')
var self = this
self.router.route(method, route, routeHandler)
function routeHandler (req, res, params) {
var ctx = new Ctx(req, res, self)
ctx.params = params.params
self.middleware.forEach(function (item) {
item(req, res, ctx)
})
handler(req, res, ctx)
}
}
Merry.prototype.defaultRoute = function (handler) {
var self = this
var method = 'GET'
var route = '/notFoundHandlerRoute'
self.router.route(method, route, routeHandler)
function routeHandler (req, res, params) {
var ctx = new Ctx(req, res, self)
ctx.params = params.params
self.middleware.forEach(function (item) {
item(req, res, ctx)
})
handler(req, res, ctx)
}
}
Merry.prototype.start = function () {
assert.ok(this.router, 'merry.start: router was not found. Did you run app.route() ?')
this._onerror()
return this.router.start()
}
Merry.prototype.use = function (handler) {
assert.equal(typeof handler, 'function', 'merry.use: handler should be type function')
this.middleware.push(handler)
}
Merry.prototype.listen = function (port) {
this._port = port || this.env.PORT
assert.equal(typeof this._port, 'number', 'Merry.listen: port should be type number')
var server
if (this.opts.key || this.opts.cert) {
assert.ok(this.opts.key, 'merry.listen: for http2 or https connection please provide a secure key')
assert.ok(this.opts.cert, 'merry.listen: for http2 or https connection please provide a cert')
var serverOpts = { key: this.opts.key, cert: this.opts.cert, allowHTTP1: true }
var http2 = http2Server()
server = http2.createSecureServer(serverOpts, this.router.start())
} else {
server = http.createServer(this.router.start())
}
var self = this
var stats = loghttp(server)
stats.on('data', function (level, data) {
self.log[level](data)
})
server.listen(this._port, this.onlisten.bind(this))
function http2Server () {
try {
return require('http2')
} catch (e) {
throw new Error('HTTP2 is not available, please update your node.js version to 8.4.0 or greater')
}
}
}
Merry.prototype.onlisten = function () {
this.log.info({
message: 'listening on port:' + this._port,
env: process.env.NODE_ENV || 'undefined'
})
}
Merry.prototype._onerror = function () {
var self = this
if (process.listenerCount('uncaughtException') === 0) {
process.once('uncaughtException', function (err) {
self.log.fatal(err)
console.error(err.stack)
process.exit(1)
})
}
if (process.listenerCount('unhandledRejection') === 0) {
process.once('unhandledRejection', function (err) {
self.log.fatal(err)
console.error(err.stack)
process.exit(1)
})
}
}
Ctx.prototype.parse = jsonToObject
function Ctx (req, res, ctx) {
this.log = ctx.log.child({ parent: 'merry:ctx' })
this.env = ctx.env
this.req = req
this.res = res
}
Ctx.prototype.send = function (statusCode, body, headers) {
headers = headers || {}
assert.equal(typeof statusCode, 'number', 'Merry.Ctx.send: statusCode should be type number')
assert.equal(typeof headers, 'object', 'Merry.Ctx.send: headers should be type object')
assert.ok(body, 'Merry.Ctx.send: body should exist')
if (typeof body === 'object') {
body = stringify(body)
headers['content-type'] = 'application/json'
}
this.res.writeHead(statusCode, headers)
pump(fromString(body), this.res, this.ondone.bind(this)) // need context in ondone
}
Ctx.prototype.ondone = function (err) {
if (err) this.log.error(err)
}