diff --git a/.gitignore b/.gitignore index eabb971..f3869e8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.idea node_modules npm-debug.log package-lock.json diff --git a/readme.md b/readme.md index e431638..89a22c9 100644 --- a/readme.md +++ b/readme.md @@ -13,6 +13,8 @@ Ultralight http server with live reload. ### Light and modern +### With secure protocol + ### No dependencies
@@ -58,7 +60,12 @@ import serve from 'create-serve'; serve.start({ port: 7000, root: '.', - live: true + live: true, + isHttps: true, + tlsOptions: { + key: 'absolute path to private key', + cert: 'absolute path to cert', + }, }); ``` diff --git a/src/index.js b/src/index.js index b29add5..8369c55 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,7 @@ -import http from 'http'; import fs from 'fs'; import path from 'path'; +import http from 'http'; +import https from 'https'; import listen from './listen.js'; import { addClient, @@ -12,13 +13,16 @@ import { setRoot, show404, showError, - showFile + showFile, + checkTLSOptions } from './utils/index.js'; export let options = { port: 7000, root: '.', - live: true + live: true, + isHttps: false, + tlsOptions: null }; export const defaultRoot = './public'; @@ -30,8 +34,15 @@ export const start = (startOptions = {}) => { Object.assign(options, startOptions); options.root = setRoot(); - const { live } = options; - const server = http.createServer((request, response) => { + const { live, tlsOptions, isHttps } = options; + const tlsConfig = isHttps ? checkTLSOptions(tlsOptions) : undefined; + const protocol = { + module: tlsConfig ? https : http, + alias: tlsConfig ? 'https' : 'http', + }; + options.protocol = protocol.alias; + + const server = protocol.module.createServer(tlsConfig, (request, response) => { if (live && request.url == eventSource) { const client = addClient(response); diff --git a/src/logMessage.js b/src/logMessage.js index b697d51..41b99d8 100644 --- a/src/logMessage.js +++ b/src/logMessage.js @@ -1,12 +1,12 @@ import { log, error, getIp } from './utils/index.js'; import { options } from './index.js'; -const logMessage = currentPort => { - const { port } = options; +const logMessage = (currentPort) => { + const { port, protocol } = options; log('\nServing 🍛\n'); - log(`Local → http://localhost:${currentPort}\n`); - log(`Network → http://${getIp()}:${currentPort}\n`); + log(`Local → ${protocol}://localhost:${currentPort}\n`); + log(`Network → ${protocol}://${getIp()}:${currentPort}\n`); if (currentPort != port) error(`Port ${port} was in use.\n`); }; diff --git a/src/utils/checkTLSOptions.js b/src/utils/checkTLSOptions.js new file mode 100644 index 0000000..4aa9b0f --- /dev/null +++ b/src/utils/checkTLSOptions.js @@ -0,0 +1,26 @@ +import { readFileSync } from 'fs'; +import { error } from './error.js'; +import { isEmptyObject } from './fn.js'; + +export const checkTLSOptions = (object) => { + const validTLSOptions = + !isEmptyObject(object) + && typeof object.key === 'string' + && typeof object.cert === 'string'; + + if (validTLSOptions) { + try { + const tlsConfig = {}; + tlsConfig.key = readFileSync(object.key); + tlsConfig.cert = readFileSync(object.cert); + return tlsConfig; + } catch (e) { + error('\nUnable to start HTTPS server \n'); + error(`Reason: ${e} \n`); + return undefined; + } + } else { + error('\nUnable to start HTTPS server. Reason: INVALID TLS Options \n'); + return undefined; + } +}; diff --git a/src/utils/fn.js b/src/utils/fn.js new file mode 100644 index 0000000..7bf4812 --- /dev/null +++ b/src/utils/fn.js @@ -0,0 +1,13 @@ +/** + * Checks, if value is an empty object + * @param {*} value - the value to check + * @returns {boolean} - returns true, if value is empty object, else false + */ +export const isEmptyObject = (value) => ( + !value + || JSON.stringify(value) === '{}' + || ( + Object.prototype.toString.call(value) === '[object Object]' + && Object.keys(value).length < 1 + ) +); diff --git a/src/utils/index.js b/src/utils/index.js index 6be28d9..5c80498 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -10,3 +10,5 @@ export { show404 } from './show404.js'; export { showError } from './showError.js'; export { showFile } from './showFile.js'; export { styles } from './styles.js'; +export * from './fn.js'; +export { checkTLSOptions } from './checkTLSOptions.js';