-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Expand file tree
/
Copy pathserver.js
More file actions
181 lines (156 loc) · 5.5 KB
/
server.js
File metadata and controls
181 lines (156 loc) · 5.5 KB
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
'use strict'
const assert = require('assert')
const http = require('http')
const https = require('https')
const { kState, kOptions } = require('./symbols')
const { FST_ERR_HTTP2_INVALID_VERSION, FST_ERR_REOPENED_CLOSE_SERVER, FST_ERR_REOPENED_SERVER } = require('./errors')
function createServer (options, httpHandler) {
assert(options, 'Missing options')
assert(httpHandler, 'Missing http handler')
let server = null
if (options.serverFactory) {
server = options.serverFactory(httpHandler, options)
} else if (options.http2) {
if (options.https) {
server = http2().createSecureServer(options.https, httpHandler)
} else {
server = http2().createServer(httpHandler)
}
server.on('session', sessionTimeout(options.http2SessionTimeout))
} else {
// this is http1
if (options.https) {
server = https.createServer(options.https, httpHandler)
} else {
server = http.createServer(httpHandler)
}
server.keepAliveTimeout = options.keepAliveTimeout
server.requestTimeout = options.requestTimeout
// we treat zero as null
// and null is the default setting from nodejs
// so we do not pass the option to server
if (options.maxRequestsPerSocket > 0) {
server.maxRequestsPerSocket = options.maxRequestsPerSocket
}
}
if (!options.serverFactory) {
server.setTimeout(options.connectionTimeout)
}
return { server, listen }
// `this` is the Fastify object
function listen () {
const normalizeListenArgs = (args) => {
if (args.length === 0) {
return { port: 0, host: 'localhost' }
}
const cb = typeof args[args.length - 1] === 'function' ? args.pop() : undefined
const options = { cb: cb }
const firstArg = args[0]
const argsLength = args.length
const lastArg = args[argsLength - 1]
/* Deal with listen (options) || (handle[, backlog]) */
if (typeof firstArg === 'object' && firstArg !== null) {
options.backlog = argsLength > 1 ? lastArg : undefined
Object.assign(options, firstArg)
} else if (typeof firstArg === 'string' && isNaN(firstArg)) {
/* Deal with listen (pipe[, backlog]) */
options.path = firstArg
options.backlog = argsLength > 1 ? lastArg : undefined
} else {
/* Deal with listen ([port[, host[, backlog]]]) */
options.port = argsLength >= 1 && firstArg ? firstArg : 0
// This will listen to what localhost is.
// It can be 127.0.0.1 or ::1, depending on the operating system.
// Fixes https://github.com/fastify/fastify/issues/1022.
options.host = argsLength >= 2 && args[1] ? args[1] : 'localhost'
options.backlog = argsLength >= 3 ? args[2] : undefined
}
return options
}
const listenOptions = normalizeListenArgs(Array.from(arguments))
const cb = listenOptions.cb
const wrap = err => {
server.removeListener('error', wrap)
if (!err) {
const address = logServerAddress()
cb(null, address)
} else {
this[kState].listening = false
cb(err, null)
}
}
const listenPromise = (listenOptions) => {
if (this[kState].listening && this[kState].closing) {
return Promise.reject(new FST_ERR_REOPENED_CLOSE_SERVER())
} else if (this[kState].listening) {
return Promise.reject(new FST_ERR_REOPENED_SERVER())
}
return this.ready().then(() => {
let errEventHandler
const errEvent = new Promise((resolve, reject) => {
errEventHandler = (err) => {
this[kState].listening = false
reject(err)
}
server.once('error', errEventHandler)
})
const listen = new Promise((resolve, reject) => {
server.listen(listenOptions, () => {
server.removeListener('error', errEventHandler)
resolve(logServerAddress())
})
// we set it afterwards because listen can throw
this[kState].listening = true
})
return Promise.race([
errEvent, // e.g invalid port range error is always emitted before the server listening
listen
])
})
}
const logServerAddress = () => {
let address = server.address()
const isUnixSocket = typeof address === 'string'
/* istanbul ignore next */
if (!isUnixSocket) {
if (address.address.indexOf(':') === -1) {
address = address.address + ':' + address.port
} else {
address = '[' + address.address + ']:' + address.port
}
}
/* istanbul ignore next */
address = (isUnixSocket ? '' : ('http' + (this[kOptions].https ? 's' : '') + '://')) + address
this.log.info('Server listening at ' + address)
return address
}
if (cb === undefined) return listenPromise(listenOptions)
this.ready(err => {
if (err != null) return cb(err)
if (this[kState].listening && this[kState].closing) {
return cb(new FST_ERR_REOPENED_CLOSE_SERVER(), null)
} else if (this[kState].listening) {
return cb(new FST_ERR_REOPENED_SERVER(), null)
}
server.once('error', wrap)
server.listen(listenOptions, wrap)
this[kState].listening = true
})
}
}
function http2 () {
try {
return require('http2')
} catch (err) {
throw new FST_ERR_HTTP2_INVALID_VERSION()
}
}
function sessionTimeout (timeout) {
return function (session) {
session.setTimeout(timeout, close)
}
}
function close () {
this.close()
}
module.exports = { createServer }