Promisify proxy close.

This commit is contained in:
John Livingston 2022-08-24 11:03:29 +02:00
parent 1bb202d9d3
commit de179e90d3
No known key found for this signature in database
GPG Key ID: B17B5640CE66CDBC
2 changed files with 25 additions and 9 deletions

View File

@ -188,7 +188,7 @@ async function ensureProsodyRunning (options: RegisterServerOptions): Promise<vo
}) })
// Set the http-bind route. // Set the http-bind route.
enableProxyRoute(options, { await enableProxyRoute(options, {
host: config.host, host: config.host,
port: config.port port: config.port
}) })
@ -241,7 +241,7 @@ async function ensureProsodyNotRunning (options: RegisterServerOptions): Promise
logger.info(`ProsodyCtl command returned: ${status.message}`) logger.info(`ProsodyCtl command returned: ${status.message}`)
logger.debug('Removing proxy route') logger.debug('Removing proxy route')
disableProxyRoute(options) await disableProxyRoute(options)
} }
export { export {

View File

@ -206,7 +206,7 @@ async function initWebchatRouter (options: RegisterServerOptions): Promise<Route
} }
)) ))
disableProxyRoute(options) await disableProxyRoute(options)
router.all('/http-bind', router.all('/http-bind',
(req: Request, res: Response, next: NextFunction) => { (req: Request, res: Response, next: NextFunction) => {
try { try {
@ -312,22 +312,38 @@ async function initWebchatRouter (options: RegisterServerOptions): Promise<Route
// } // }
// } // }
function disableProxyRoute (_options: RegisterServerOptions): void { async function disableProxyRoute ({ peertubeHelpers }: RegisterServerOptions): Promise<void> {
currentHttpBindProxy?.close() return new Promise((resolve) => {
currentHttpBindProxy = null try {
currentProsodyProxyInfo = null currentProsodyProxyInfo = null
if (!currentHttpBindProxy) {
resolve()
return
}
peertubeHelpers.logger.debug('Closing the proxy...')
currentHttpBindProxy.close(() => {
peertubeHelpers.logger.debug('The proxy is closed.')
resolve()
})
currentHttpBindProxy = null
} catch (err) {
peertubeHelpers.logger.error('Seems that the http bind proxy close has failed: ' + (err as string))
resolve()
}
})
} }
function enableProxyRoute ( async function enableProxyRoute (
{ peertubeHelpers }: RegisterServerOptions, { peertubeHelpers }: RegisterServerOptions,
prosodyProxyInfo: ProsodyProxyInfo prosodyProxyInfo: ProsodyProxyInfo
): void { ): Promise<void> {
const logger = peertubeHelpers.logger const logger = peertubeHelpers.logger
if (!/^\d+$/.test(prosodyProxyInfo.port)) { if (!/^\d+$/.test(prosodyProxyInfo.port)) {
logger.error(`Port '${prosodyProxyInfo.port}' is not valid. Aborting.`) logger.error(`Port '${prosodyProxyInfo.port}' is not valid. Aborting.`)
return return
} }
currentProsodyProxyInfo = prosodyProxyInfo currentProsodyProxyInfo = prosodyProxyInfo
logger.debug('Creating a new http bind proxy')
currentHttpBindProxy = createProxyServer({ currentHttpBindProxy = createProxyServer({
target: 'http://localhost:' + prosodyProxyInfo.port + '/http-bind', target: 'http://localhost:' + prosodyProxyInfo.port + '/http-bind',
ignorePath: true ignorePath: true