1
0
Fork 0
peertube/server/tests/shared/mock-servers/shared.ts
2021-12-17 12:24:03 +01:00

33 lines
624 B
TypeScript

import { Express } from 'express'
import { Server } from 'http'
import { AddressInfo } from 'net'
function randomListen (app: Express) {
return new Promise<Server>(res => {
const server = app.listen(0, () => res(server))
})
}
function getPort (server: Server) {
const address = server.address() as AddressInfo
return address.port
}
function terminateServer (server: Server) {
if (!server) return Promise.resolve()
return new Promise<void>((res, rej) => {
server.close(err => {
if (err) return rej(err)
return res()
})
})
}
export {
randomListen,
getPort,
terminateServer
}