Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: add the options to create https server #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
node_modules
npm-debug.log
package-lock.json
Expand Down
9 changes: 8 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Ultralight http server with live reload.

### Light and modern

### With secure protocol

### No dependencies

<br>
Expand Down Expand Up @@ -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',
},
});
```

Expand Down
21 changes: 16 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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';
Expand All @@ -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);

Expand Down
8 changes: 4 additions & 4 deletions src/logMessage.js
Original file line number Diff line number Diff line change
@@ -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`);
};

Expand Down
26 changes: 26 additions & 0 deletions src/utils/checkTLSOptions.js
Original file line number Diff line number Diff line change
@@ -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;
}
};
13 changes: 13 additions & 0 deletions src/utils/fn.js
Original file line number Diff line number Diff line change
@@ -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
)
);
2 changes: 2 additions & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';