service-core/core/http.mjs
Jonatan Nilsson 17830d7f8d
All checks were successful
continuous-integration/appveyor/branch AppVeyor build succeeded
Many test, many refactoring, only core, lib and runner left to test and fix
2022-01-13 17:02:55 +00:00

49 lines
1 KiB
JavaScript

import http from 'http'
import https from 'https'
export default class HttpServer {
constructor(config) {
this.ishttps = false
this.active = null
this.sockets = new Set()
this.creator = http
if (config && config.https) {
this.creator = https
this.ishttps = true
}
}
createServer(opts, listener) {
let server = this.creator.createServer(opts, listener)
server.on('connection', (socket) => {
this.sockets.add(socket)
socket.once('close', () => {
this.sockets.delete(socket)
})
})
this.active = server
return server
}
closeServer() {
if (!this.active) return Promise.resolve()
return new Promise((res, rej) => {
this.sockets.forEach(function(socket) {
socket.destroy()
})
this.sockets.clear()
this.active.close(err => {
if (err) return rej(err)
this.active = null
// Waiting 1 second for it to close down
setTimeout(function() {res() }, 1000)
})
})
}
}