feat: refactored server to use ts and improved folder structure

This commit is contained in:
Roberto Tonino
2021-04-03 19:46:54 +02:00
parent f98abb384c
commit 8e4e2ff5eb
43 changed files with 4125 additions and 452 deletions

View File

@@ -0,0 +1,4 @@
import path from 'path'
export const ROOT_DIR = path.resolve('../')
export const WEBUI_DIR = path.join(ROOT_DIR, 'webui', 'public')

View File

@@ -0,0 +1,22 @@
import { Port } from '../types'
/**
* Normalize a port into a number, string, or false.
*
* @since 0.0.0
*/
export function normalizePort(portString: string): Port {
const port = parseInt(portString, 10)
if (isNaN(port)) {
// named pipe
return portString
}
if (port >= 0) {
// port number
return port
}
return false
}

View File

@@ -0,0 +1,47 @@
import http from 'http'
import type { Debugger } from 'debug'
/**
* Event listener for HTTP server "error" event.
*
* @since 0.0.0
*/
export function getErrorCb(port: number | string | boolean) {
return (error: any) => {
if (error.syscall !== 'listen') {
throw error
}
const bind = typeof port === 'string' ? 'Pipe ' + port : 'Port ' + port
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES': {
console.error(bind + ' requires elevated privileges')
process.exit(1)
}
case 'EADDRINUSE': {
console.error(bind + ' is already in use')
process.exit(1)
}
default:
throw error
}
}
}
/**
* Event listener for HTTP server "listening" event.
*
* @since 0.0.0
*/
export function getListeningCb(server: http.Server, debug: Debugger) {
return () => {
const addr = server.address()
if (addr) {
const bind = typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port
debug('Listening on ' + bind)
}
}
}