UNPKG

@roots/bud-server

Version:

Development server for @roots/bud

84 lines (83 loc) 1.96 kB
import { __decorate } from "tslib"; import { bind } from '@roots/bud-support/decorators/bind'; import logger from '@roots/bud-support/logger'; /** * Node server * * @remarks * Base class. Extended by either `http` or `https` class. */ export class BaseServer { app; /** * Server instance */ instance; /** * Constructor * * @param app - Bud */ constructor(app) { this.app = app; } /** * Server error event */ onError(error) { this.logger.error(error); } /** * Server listen event */ onListening(...param) { this.logger.info(...param); } /** * Server request event */ async onRequest(request, response) { this.logger.log(`[${response.statusCode}]`, request.url, response.statusMessage ?? ``); return response; } /** * Listen */ async listen() { if (!this.app.server) { throw new Error(`No server instance found.`); } this.instance .listen(this.app.hooks.filter(`dev.listenOptions`, { host: this.app.server?.url.hostname, port: parseInt(this.app.server?.url.port), })) .on(`listening`, this.app.hooks.filter(`dev.onListening`, this.onListening)) .on(`request`, this.app.hooks.filter(`dev.onRequest`, this.onRequest)) .on(`error`, this.app.hooks.filter(`dev.onError`, this.onError)); } /** * Logger */ get logger() { return logger.scope(`server`); } /** * Options */ get options() { return this.app.hooks.filter(`dev.options`, {}); } } __decorate([ bind ], BaseServer.prototype, "onError", null); __decorate([ bind ], BaseServer.prototype, "onListening", null); __decorate([ bind ], BaseServer.prototype, "onRequest", null); __decorate([ bind ], BaseServer.prototype, "listen", null);