-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
206 lines (164 loc) · 5.43 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
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
'use strict'
const defaultFastify = require('fastify')
const getServerInstance = require('./lib/server')
const closingServer = Symbol('closingServer')
async function restartable (factory, opts, fastify = defaultFastify) {
const proxy = { then: undefined }
let app = await factory((opts) => createApplication(opts, false), opts)
const server = wrapServer(app.server)
let newHandler = null
const preRestartHooks = []
const onRestartHooks = []
async function restart (restartOptions) {
const requestListeners = server.listeners('request')
const clientErrorListeners = server.listeners('clientError')
await executeHooks(preRestartHooks, app, restartOptions)
let newApp = null
try {
newApp = await factory(createApplication, opts, restartOptions)
if (server.listening) {
const { port, address } = server.address()
await newApp.listen({ port, host: address })
} else {
await newApp.ready()
}
} catch (error) {
restoreClientErrorListeners(server, clientErrorListeners)
// In case if fastify.listen() would throw an error
/* c8 ignore next 3 */
if (newApp !== null) {
await closeApplication(newApp)
}
throw error
}
server.on('request', newHandler)
removeRequestListeners(server, requestListeners)
removeClientErrorListeners(server, clientErrorListeners)
Object.setPrototypeOf(proxy, newApp)
await closeApplication(app)
app = newApp
executeHooks(onRestartHooks, newApp, restartOptions)
}
let debounce = null
// TODO: think about queueing restarts with different options
async function debounceRestart (...args) {
if (debounce === null) {
debounce = restart(...args).finally(() => { debounce = null })
}
return debounce
}
let serverCloseCounter = 0
let closingRestartable = false
function createApplication (newOpts, isRestarted = true) {
opts = newOpts
let createServerCounter = 0
function serverFactory (handler, options) {
// this cause an uncaughtException because of the bug in Fastify
// see: https://github.com/fastify/fastify/issues/4730
/* c8 ignore next 6 */
if (++createServerCounter > 1) {
throw new Error(
'Cannot create multiple server bindings for a restartable application. ' +
'Please specify an IP address as a host parameter to the fastify.listen()'
)
}
if (isRestarted) {
newHandler = handler
return server
}
return getServerInstance(options, handler)
}
const app = fastify({ ...newOpts, serverFactory })
if (!isRestarted) {
Object.setPrototypeOf(proxy, app)
}
app.decorate('restart', debounceRestart)
app.decorate('addPreRestartHook', (hook) => {
if (typeof hook !== 'function') {
throw new TypeError('The hook must be a function')
}
preRestartHooks.push(hook)
})
app.decorate('addOnRestartHook', (hook) => {
if (typeof hook !== 'function') {
throw new TypeError('The hook must be a function')
}
onRestartHooks.push(hook)
})
app.decorate('restarted', {
getter: () => isRestarted
})
app.decorate('persistentRef', {
getter: () => proxy
})
app.decorate('closingRestartable', {
getter: () => closingRestartable
})
app.addHook('preClose', async () => {
if (++serverCloseCounter > 0) {
closingRestartable = true
server[closingServer] = true
}
})
return app
}
async function closeApplication (app) {
serverCloseCounter--
await app.close()
}
return proxy
}
function wrapServer (server) {
const _listen = server.listen.bind(server)
server.listen = (...args) => {
if (server.listening) {
server.emit('listening')
} else {
return _listen(...args)
}
}
server[closingServer] = false
const _close = server.close.bind(server)
server.close = (cb) => server[closingServer] ? _close(cb) : cb()
/* c8 ignore next 5 */
// closeAllConnections was added in Nodejs v18.2.0
if (server.closeAllConnections) {
const _closeAllConnections = server.closeAllConnections.bind(server)
server.closeAllConnections = () => server[closingServer] && _closeAllConnections()
}
/* c8 ignore next 5 */
// closeIdleConnections was added in Nodejs v18.2.0
if (server.closeIdleConnections) {
const _closeIdleConnections = server.closeIdleConnections.bind(server)
server.closeIdleConnections = () => server[closingServer] && _closeIdleConnections()
}
return server
}
function removeRequestListeners (server, listeners) {
for (const listener of listeners) {
server.removeListener('request', listener)
}
}
function removeClientErrorListeners (server, listeners) {
for (const listener of listeners) {
server.removeListener('clientError', listener)
}
}
function restoreClientErrorListeners (server, oldListeners) {
// Creating a new Fastify apps adds one clientError listener
// Let's remove all the new ones
const listeners = server.listeners('clientError')
for (const listener of listeners) {
if (!oldListeners.includes(listener)) {
server.removeListener('clientError', listener)
}
}
}
async function executeHooks (hooks, app, opts) {
for (const hook of hooks) {
await hook(app, opts)?.catch((error) => app.log.error(error))
}
}
module.exports = restartable
module.exports.default = restartable
module.exports.restartable = restartable