UNPKG

@appsensorlike/appsensorlike

Version:

A port of OWASP AppSensor reference implementation

128 lines (127 loc) 4.7 kB
import http from 'http'; import https from 'https'; import http2 from 'http2'; import fs from 'fs'; import { Logger } from '../logging/logging.js'; var HTTP_PROTOCOLS; (function (HTTP_PROTOCOLS) { HTTP_PROTOCOLS["HTTP"] = "http"; HTTP_PROTOCOLS["HTTPS"] = "https"; HTTP_PROTOCOLS["HTTP2"] = "http2"; })(HTTP_PROTOCOLS || (HTTP_PROTOCOLS = {})); /** * Http/s/2 server and listen configuration */ class HttpS2ServerConfig { constructor() { this.protocol = HTTP_PROTOCOLS.HTTP; } } /** * Creates Http/s/2 server and listen for connection according to * provided HttpS2ServerConfig configuration */ class HttpS2Server { constructor() { this.server = null; } getConfiguration() { return new HttpS2ServerConfig(); } async startServer() { const config = this.getConfiguration(); switch (config.protocol) { case HTTP_PROTOCOLS.HTTP: { const options = config.http ? config.http : {}; this.server = http.createServer(options); break; } case HTTP_PROTOCOLS.HTTPS: { const options = config.https ? config.https : {}; if (!options.key) { throw new Error("For https server: You have to set the name of the key file under config https.key!"); } if (!options.cert) { throw new Error("For https server: You have to set the name of the cert file under config https.cert!"); } const serviceKey = fs.readFileSync(options.key); const certificate = fs.readFileSync(options.cert); options.key = serviceKey; options.cert = certificate; this.server = https.createServer(options); break; } case HTTP_PROTOCOLS.HTTP2: { const options = config.http2 ? config.http2 : {}; this.server = http2.createServer(options); break; } } if (this.server) { this.server.on('error', (err) => { Logger.getServerLogger().error('HttpS2Server.startServer:', 'error:', err); }).on('tlsClientError', (err, tlsSocket) => { Logger.getServerLogger().error('HttpS2Server.startServer:', 'tlsClientError:', err); }); await this.attachToServer(); const listenOptions = config.listenOptions ? config.listenOptions : {}; const self = this; const listenPromise = new Promise((resolve, reject) => { if (this.server) { this.server.listen(listenOptions, () => { Logger.getServerLogger().info('HttpS2Server.startServer: ', `Listening on: ${self.getAddress()}`); resolve(null); }); } }); const timeOutInMillis = 10000; const timeoutPromise = new Promise((resolve, reject) => { setTimeout(() => { reject(new Error(`Server didn't start in ${timeOutInMillis} milliseconds!`)); }, timeOutInMillis); }); await Promise.race([listenPromise, timeoutPromise]); } } getAddress() { let address = ''; if (this.server) { address = this.server.address(); if (address && this.instanceofAddressInfo(address)) { address = address.address + ':' + address.port; } if (!address) { address = ''; } } return address; } async attachToServer() { //allow sharing of the same http server } instanceofAddressInfo(obj) { return 'address' in obj && 'family' in obj && 'port' in obj; } async stopServer() { await new Promise((resolve, reject) => { if (this.server) { const address = this.getAddress(); this.server.close((err) => { if (err) { Logger.getServerLogger().error('HttpS2Server.stopServer: ', err); reject(err); return; } else { Logger.getServerLogger().info('HttpS2Server.stopServer: ', `Server listening on ${address} stopped.`); } resolve(0); }); } else { resolve(0); } }); } } export { HTTP_PROTOCOLS, HttpS2ServerConfig, HttpS2Server };