PeerTube/server/tests/shared/mock-servers/shared.ts

34 lines
624 B
TypeScript
Raw Normal View History

2021-10-22 14:31:38 +02:00
import { Express } from 'express'
2021-09-06 08:13:11 +02:00
import { Server } from 'http'
2021-10-22 14:31:38 +02:00
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
}
2021-09-06 08:13:11 +02:00
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 {
2021-10-22 14:31:38 +02:00
randomListen,
getPort,
2021-09-06 08:13:11 +02:00
terminateServer
}