@mocks-server/core
Advanced tools
Comparing version 3.9.0 to 3.10.0
{ | ||
"name": "@mocks-server/core", | ||
"version": "3.9.0", | ||
"version": "3.10.0", | ||
"description": "Pluggable mock server supporting multiple route variants and mocks", | ||
@@ -41,3 +41,3 @@ "keywords": [ | ||
"@hapi/boom": "9.1.4", | ||
"@mocks-server/config": "1.2.0", | ||
"@mocks-server/config": "1.2.1", | ||
"@mocks-server/logger": "1.1.0", | ||
@@ -44,0 +44,0 @@ "@mocks-server/nested-collections": "2.0.0", |
@@ -11,2 +11,5 @@ /* | ||
const path = require("path"); | ||
const fsExtra = require("fs-extra"); | ||
function arrayMerge(_destinationArray, sourceArray) { | ||
@@ -40,2 +43,6 @@ return sourceArray; | ||
function readFileSync(filePath) { | ||
return fsExtra.readFileSync(path.resolve(process.cwd(), filePath), "utf-8"); | ||
} | ||
module.exports = { | ||
@@ -46,2 +53,3 @@ arrayMerge, | ||
resolveWhenConditionPass, | ||
readFileSync, | ||
}; |
@@ -24,2 +24,3 @@ /* | ||
} = require("./middlewares"); | ||
const { readFileSync } = require("../common/helpers"); | ||
@@ -29,2 +30,3 @@ const ALL_HOSTS = "0.0.0.0"; | ||
const HTTPS_ALERT_ID = "https"; | ||
const START_ALERT_ID = "start"; | ||
@@ -49,2 +51,23 @@ const START_ERROR_MESSAGE = "Error starting server"; | ||
const HTTPS_NAMESPACE = "https"; | ||
const HTTPS_OPTIONS = [ | ||
{ | ||
description: "Use https protocol or not", | ||
name: "enabled", | ||
type: "boolean", | ||
default: false, | ||
}, | ||
{ | ||
description: "Path to a TLS/SSL certificate", | ||
name: "cert", | ||
type: "string", | ||
}, | ||
{ | ||
description: "Path to the certificate private key", | ||
name: "key", | ||
type: "string", | ||
}, | ||
]; | ||
const CORS_NAMESPACE = "cors"; | ||
@@ -119,2 +142,3 @@ | ||
); | ||
const httpsConfigNamespace = this._config.addNamespace(HTTPS_NAMESPACE); | ||
@@ -132,3 +156,7 @@ [this._portOption, this._hostOption] = this._config.addOptions(OPTIONS); | ||
[this._httpsEnabledOption, this._httpsCertOption, this._httpsKeyOption] = | ||
httpsConfigNamespace.addOptions(HTTPS_OPTIONS); | ||
this.restart = this.restart.bind(this); | ||
this._reinitServer = this._reinitServer.bind(this); | ||
@@ -143,2 +171,5 @@ this._hostOption.onChange(this.restart); | ||
this._urlEncodedBodyParserOptionsOption.onChange(this.restart); | ||
this._httpsEnabledOption.onChange(this._reinitServer); | ||
this._httpsCertOption.onChange(this._reinitServer); | ||
this._httpsKeyOption.onChange(this._reinitServer); | ||
@@ -196,12 +227,44 @@ this._routesRouter = routesRouter; | ||
// Create server | ||
this._server = http.createServer(this._express); | ||
this._alerts.remove(SERVER_ALERT_ID); | ||
this._server.on("error", (error) => { | ||
this._alerts.set(SERVER_ALERT_ID, "Server error", error); | ||
this._error = error; | ||
throw error; | ||
}); | ||
this._serverInitted = true; | ||
this._server = this._createServer(); | ||
if (this._server) { | ||
this._alerts.remove(SERVER_ALERT_ID); | ||
this._server.on("error", (error) => { | ||
this._alerts.set(SERVER_ALERT_ID, "Server error", error); | ||
this._error = error; | ||
throw error; | ||
}); | ||
this._serverInitted = true; | ||
} else { | ||
this._serverInitted = false; | ||
} | ||
} | ||
_createHttpsServer() { | ||
this._logger.verbose("Creating HTTPS server"); | ||
this._alerts.remove(HTTPS_ALERT_ID); | ||
try { | ||
const https = require("https"); | ||
return https.createServer( | ||
{ | ||
cert: readFileSync(this._httpsCertOption.value), | ||
key: readFileSync(this._httpsKeyOption.value), | ||
}, | ||
this._express | ||
); | ||
} catch (error) { | ||
this._alerts.set(HTTPS_ALERT_ID, "Error creating HTTPS server", error); | ||
this._serverInitError = error; | ||
} | ||
} | ||
_createHttpServer() { | ||
this._logger.verbose("Creating HTTP server"); | ||
return http.createServer(this._express); | ||
} | ||
_createServer() { | ||
this._server = null; | ||
return !!this._httpsEnabledOption.value ? this._createHttpsServer() : this._createHttpServer(); | ||
} | ||
_reinitServer() { | ||
@@ -225,3 +288,2 @@ if (this._serverInitted) { | ||
const port = this._portOption.value; | ||
const hostName = host === ALL_HOSTS ? LOCALHOST : host; | ||
@@ -242,3 +304,3 @@ try { | ||
} else { | ||
this._logger.info(`Server started and listening at http://${hostName}:${port}`); | ||
this._logger.info(`Server started and listening at ${this.url}`); | ||
this._error = null; | ||
@@ -329,2 +391,12 @@ this._serverStarting = false; | ||
get protocol() { | ||
return !!this._httpsEnabledOption.value ? "https" : "http"; | ||
} | ||
get url() { | ||
const host = this._hostOption.value; | ||
const hostName = host === ALL_HOSTS ? LOCALHOST : host; | ||
return `${this.protocol}://${hostName}:${this._portOption.value}`; | ||
} | ||
// LEGACY, to be removed | ||
@@ -331,0 +403,0 @@ get error() { |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
160808
4529
5
2
+ Added@mocks-server/config@1.2.1(transitive)
- Removed@mocks-server/config@1.2.0(transitive)
Updated@mocks-server/config@1.2.1