Code-Cleanup

- Migrated to Webstorm and cleaned the code
pull/4/head
Trivernis 6 years ago
parent cf9f51013c
commit 2b7c88596a

1
.gitignore vendored

@ -31,3 +31,4 @@ node_modules
.log .log
.cache .cache
package-lock.json package-lock.json
.idea

@ -2,27 +2,27 @@ const fs = require("fs"),
path = require("path"), path = require("path"),
config_path = "./config/caching_dump.json", config_path = "./config/caching_dump.json",
cache_dump = JSON.parse(fs.readFileSync(config_path)), cache_dump = JSON.parse(fs.readFileSync(config_path)),
cache_dir = "./.cache", cache_dir = "./.cache";
cache = {}; let cache = {};
var logger = require("winston"); let logger = require("winston");
if (cache_dump != null && cache_dump["last"] != null) cache = cache_dump["last"]; // read the data from the file dump if (cache_dump != null && cache_dump["last"] != null) cache = cache_dump["last"]; // read the data from the file dump
/** /**
* Sets the logger for logging * Sets the logger for logging
* @param {Winston Logger} newLogger * @param {Object} newLogger
*/ */
exports.setLogger = function(newLogger) { exports.setLogger = function (newLogger) {
logger = newLogger; logger = newLogger;
} };
/** /**
* Returns the data from files that were cached * Returns the data from files that were cached
* @param {String} filename The name of the file that has been cached * @param {String} filename The name of the file that has been cached
* @return {String} The data stored in the file * @return {String} The data stored in the file
*/ */
exports.getCached = function(filename) { exports.getCached = function (filename) {
let cf = cache[filename]; let cf = cache[filename];
let call_passed = (Date.now()-cf.last_call) / 1000; // calculate the time since the last call of the file let call_passed = (Date.now() - cf.last_call) / 1000; // calculate the time since the last call of the file
if (cf.call_count > 10 && call_passed < 60) { if (cf.call_count > 10 && call_passed < 60) {
cf.data = fs.readFileSync(cf.path); // store the file's data into the json cf.data = fs.readFileSync(cf.path); // store the file's data into the json
} else if (call_passed > 3600) { } else if (call_passed > 3600) {
@ -41,15 +41,17 @@ exports.getCached = function(filename) {
* @param {String} filename The name of the file * @param {String} filename The name of the file
* @param {String} data The data form the file * @param {String} data The data form the file
*/ */
exports.cache = function(filename, data) { exports.cache = function (filename, data) {
logger.verbose("Creating cache entry for %s", filename); logger.verbose("Creating cache entry for %s", filename);
if (!fs.existsSync("./.cache")) fs.mkdirSync("./.cache"); // if the cache folder doesn't exist, create it if (!fs.existsSync("./.cache")) fs.mkdirSync("./.cache"); // if the cache folder doesn't exist, create it
let cache_fn = filename.replace(/[^\w\.]/g, "__"); // remove invalid path characters let cache_fn = filename.replace(/[^\w.]/g, "__"); // remove invalid path characters
let count = 0; let count = 0;
while (fs.existsSync(filename + count + ".cache")) count++; // check if a file with the same name already exists and increase count while (fs.existsSync(filename + count + ".cache")) count++; // check if a file with the same name already exists and increase count
let cache_path = path.join(cache_dir, cache_fn+count+".cache"); // create the final file path. Cachedir + cached filename (without invalid) + count + .cache let cache_path = path.join(cache_dir, cache_fn + count + ".cache"); // create the final file path. Cachedir + cached filename (without invalid) + count + .cache
logger.debug("Creating file %s", cache_path); logger.debug("Creating file %s", cache_path);
fs.writeFile(cache_path, data, (error) => { fs.writeFile(cache_path, data, (err) => {
if (err !== null) logger.error(err);
else {
logger.debug("Created file cache entry for %s", filename); logger.debug("Created file cache entry for %s", filename);
cache[filename] = { // create a cache-entry with the file's path when the file is written (so it won't be accessed before) cache[filename] = { // create a cache-entry with the file's path when the file is written (so it won't be accessed before)
"path": cache_path, // the last call to the file, the count of calls and an "path": cache_path, // the last call to the file, the count of calls and an
@ -58,24 +60,21 @@ exports.cache = function(filename, data) {
"data": null, "data": null,
"creation_time": Date.now(), "creation_time": Date.now(),
"changed": false "changed": false
}; }
}
fs.watch(filename, (eventType) => { // watch the file for changes fs.watch(filename, (eventType) => { // watch the file for changes
logger.debug("Change detected on %s", filename); logger.debug("Change detected on %s", filename);
if (eventType == 'change') cache[filename].changed = true; // if the event change is detected set the change attribute to true if (eventType === 'change') cache[filename].changed = true; // if the event change is detected set the change attribute to true
}); });
}); // write the data asynchronously to the file }); // write the data asynchronously to the file
}; };
/** /**
* Returns if the file is already cached * Returns if the file is already cached. It returns false if changes were detected in the file
* @param {String} filename The filename to check * @param {String} filename The filename to check
* @return {Boolean} Is it cached or not * @return {Boolean} Is it cached or not
* TODO: Use last access or use creation_time property to check if the file might
* be too old. If the function returns false, a new cache-file will be created which
* has a different name from the old. On each startup a function could check if
* there are cache-files that are not listet in the cache_dump and delete them.
*/ */
exports.isCached = function(filename) { exports.isCached = function (filename) {
let cached_entry = cache[filename]; let cached_entry = cache[filename];
if (cached_entry) { // check if the cache entry exists if (cached_entry) { // check if the cache entry exists
logger.debug("Found cache entry for %s", filename); logger.debug("Found cache entry for %s", filename);
@ -92,8 +91,8 @@ exports.isCached = function(filename) {
/** /**
* A function that dumps the config into the config file after appending the cache to it. * A function that dumps the config into the config file after appending the cache to it.
*/ */
exports.cleanup = function() { exports.cleanup = function () {
logger.verbose("Dumping cache into cache_dump file"); logger.verbose("Dumping cache into cache_dump file");
cache_dump["last"] = cache; // append the cache to the dump object cache_dump["last"] = cache; // append the cache to the dump object
fs.writeFileSync(config_path, JSON.stringify(cache_dump)); // write the dump data to the file fs.writeFileSync(config_path, JSON.stringify(cache_dump)); // write the dump data to the file
} };

@ -2,9 +2,7 @@
* Preprocesses html-files * Preprocesses html-files
*/ */
const fs = require("fs"), const fs = require("fs"),
{ { JSDOM } = require('jsdom'),
JSDOM
} = require('jsdom')
// ressources // ressources
defaultCss = "/glob/style.sass", // the default style that is embedded in every html defaultCss = "/glob/style.sass", // the default style that is embedded in every html
defaultJs = "/glob/script.js"; // the default script that is embedded in every html defaultJs = "/glob/script.js"; // the default script that is embedded in every html
@ -38,11 +36,11 @@ function createScriptLinkElement(document, src) {
/** /**
* Formats the html string by adding a link to the standard css and to the standard javascript file. * Formats the html string by adding a link to the standard css and to the standard javascript file.
* @param {String} htmlstring A string read from an html file or a html document string itself.
* @return {String} A html-string that represents a document. * @return {String} A html-string that represents a document.
* @param filename
*/ */
exports.formatHtml = function(filename) { exports.formatHtml = function (filename) {
var htmlstring = fs.readFileSync(filename); let htmlstring = fs.readFileSync(filename);
try { try {
let dom = new JSDOM(htmlstring); // creates a dom from the html string let dom = new JSDOM(htmlstring); // creates a dom from the html string
let document = dom.window.document; let document = dom.window.document;
@ -56,4 +54,4 @@ exports.formatHtml = function(filename) {
console.error(error); console.error(error);
return htmlstring; return htmlstring;
} }
} };

@ -7,31 +7,31 @@ const fs = require("fs"),
pp_sass = require("node-sass"), pp_sass = require("node-sass"),
pp_config = { pp_config = {
".sass" : "sass", ".sass": "sass",
".html": "html", ".html": "html",
".htm": "hmtl" ".htm": "hmtl"
}; };
var logger = require("winston"); let logger = require("winston");
/** /**
* Sets the logger for logging * Sets the logger for logging
* @param {Winston Logger} newLogger * @param {Object} newLogger
*/ */
exports.setLogger = function(newLogger) { exports.setLogger = function (newLogger) {
logger = newLogger; logger = newLogger;
caching.setLogger(logger); caching.setLogger(logger);
} };
/** /**
* Returns the data for the file. In some cases the data is processed or loaded from cache. * Returns the data for the file. In some cases the data is processed or loaded from cache.
* @param {String} filename The file containing the data * @param {String} filename The file containing the data
* @return {String} The data that should be send * @return {String} The data that should be send
*/ */
exports.getProcessed = function(filename) { exports.getProcessed = function (filename) {
try { try {
logger.debug("Processing File %s", filename); logger.debug("Processing File %s", filename);
var extension = utils.getExtension(filename); // use the utils function to get the files extension let extension = utils.getExtension(filename); // use the utils function to get the files extension
var data = null; let data = null;
if (caching.isCached(filename)) return caching.getCached(filename) // return the cached file if it exists if (caching.isCached(filename)) return caching.getCached(filename) // return the cached file if it exists
logger.debug("File is not cached. Processing..."); logger.debug("File is not cached. Processing...");
switch (pp_config[extension]) { switch (pp_config[extension]) {
@ -56,4 +56,11 @@ exports.getProcessed = function(filename) {
logger.error(error); logger.error(error);
return "Processing Error"; return "Processing Error";
} }
} };
/**
* Cleanup function that calls the cleanup for the caching module
*/
exports.cleanup = function() {
caching.cleanup();
};

@ -7,14 +7,14 @@
* @param {String} filename The name of the file. * @param {String} filename The name of the file.
* @return {String} A string that represents the file-extension. * @return {String} A string that represents the file-extension.
*/ */
exports.getExtension = function(filename) { exports.getExtension = function (filename) {
if (!filename) return null; if (!filename) return null;
try { try {
let exts = filename.match(/\.[a-z]+/g); // get the extension by using regex let exts = filename.match(/\.[a-z]+/g); // get the extension by using regex
if (exts) return exts[exts.length - 1]; // return the found extension if (exts) return exts[exts.length - 1]; // return the found extension
else return null; // return null if no extension could be found else return null; // return null if no extension could be found
} catch (error) { } catch (error) {
logger.warn(error); console.error(error);
return null; return null;
} }
} }

@ -86,13 +86,13 @@ const https = require('https'),
function main() { function main() {
try { try {
prepro.setLogger(logger); prepro.setLogger(logger);
https.createServer(options, function(req, res) { https.createServer(options, function (req, res) {
logger.verbose({'msg': 'Received request', 'method': req.method, 'url': req.url}); logger.verbose({'msg': 'Received request', 'method': req.method, 'url': req.url});
perfy.start('response-calculation'); // caluclate the response time perfy.start('response-calculation'); // caluclate the response time
let url = urlparse.parse(req.url); // parse the url let url = urlparse.parse(req.url); // parse the url
let uri = url.pathname; // set uri to the urls uriame let uri = url.pathname; // set uri to the urls uriame
logger.debug({"msg": 'Got URL by using url package','url': url, 'path': uri}); logger.debug({"msg": 'Got URL by using url package', 'url': url, 'path': uri});
let [response, mime] = getResponse(uri); // get a response for the url path let [response, mime] = getResponse(uri); // get a response for the url path
logger.debug({'response-length': response.length, 'mime-type': mime}); logger.debug({'response-length': response.length, 'mime-type': mime});
@ -106,7 +106,7 @@ function main() {
} catch (error) { } catch (error) {
logger.error(error); logger.error(error);
logger.info("Shutting Down..."); logger.info("Shutting Down...");
caching.cleanup(); prepro.cleanup();
winston.end(); winston.end();
return false; return false;
} }
@ -114,28 +114,28 @@ function main() {
/** /**
* Returns a string that depends on the uri It gets the data from the routes variable. * Returns a string that depends on the uri It gets the data from the routes variable.
* @param {String} uriNormally a file-name. Depending on the extension, an other root-uriis choosen. * @param uri
* @return {String} An Array containing (Either the files content or an error message) and the mime-type. * @return {string[]} An Array containing (Either the files content or an error message) and the mime-type.
*/ */
function getResponse(uri) { function getResponse(uri) {
if (!uri || uri == "/") uri = "/index.html"; // uri redirects to the index.html if it is not set or if it is root if (!uri || uri === "/") uri = "/index.html"; // uri redirects to the index.html if it is not set or if it is root
logger.verbose({'msg':'calculating response', 'path': uri}); logger.verbose({'msg': 'calculating response', 'path': uri});
let gp = prepro.getProcessed; let gp = prepro.getProcessed;
try { try {
// get the file extension // get the file extension
let extension = utils.getExtension(uri); let extension = utils.getExtension(uri);
// returns the global script or css if the extension is css or js and the root-uriis glob. // returns the global script or css if the extension is css or js and the root-uriis glob.
if (uri.includes("/glob") && (extension == ".sass" || extension == ".js")) { if (uri.includes("/glob") && (extension === ".sass" || extension === ".js")) {
logger.verbose("Using global uri"); logger.verbose("Using global uri");
if (extension == ".sass") return [gp("." + uri), "text/css"]; if (extension === ".sass") return [gp("." + uri), "text/css"];
else return [gp("." + uri), "text/javascript"]; else return [gp("." + uri), "text/javascript"];
} }
let mount = getMount(uri); // get mount for uri it will be uses as path later instead of route let mount = getMount(uri); // get mount for uri it will be uses as path later instead of route
logger.verbose("Mount for uri is "+ mount); logger.verbose("Mount for uri is " + mount);
let route = routes[extension]; // get the route from the extension json let route = routes[extension]; // get the route from the extension json
logger.verbose("Found route: "+JSON.stringify(route)); logger.verbose("Found route: " + JSON.stringify(route));
if (!route) return ["Not Allowed", "text/plain"]; // return not allowed if no route was found if (!route) return ["Not Allowed", "text/plain"]; // return not allowed if no route was found
return [gp(mount || path.join(route["path"],uri)), route["mime"]]; // get processed output (done by preprocessor) return [gp(mount || path.join(route["path"], uri)), route["mime"]]; // get processed output (done by preprocessor)
} catch (error) { } catch (error) {
logger.error(error); logger.error(error);
return ["Error", "text/plain"]; return ["Error", "text/plain"];
@ -148,8 +148,8 @@ function getResponse(uri) {
* @return {String} The uri that points to the mounted path * @return {String} The uri that points to the mounted path
*/ */
function getMount(uri) { function getMount(uri) {
if (mounts){ // if mounts are set if (mounts) { // if mounts are set
for (var mount of mounts){ // for each set mount for (let mount of mounts) { // for each set mount
if (mount.mount && mount.path) { // if the mount has the mount parameter and the path parameter if (mount.mount && mount.path) { // if the mount has the mount parameter and the path parameter
let regEx = RegExp(mount.mount); // create a regex from the mount let regEx = RegExp(mount.mount); // create a regex from the mount
if (uri.match(regEx)) { // if there is a match if (uri.match(regEx)) { // if there is a match
@ -162,7 +162,7 @@ function getMount(uri) {
} }
// Executing the main function // Executing the main function
if (typeof require != 'undefined' && require.main == module) { if (typeof require !== 'undefined' && require.main === module) {
logger.exceptions.handle( logger.exceptions.handle(
new winston.transports.File({ new winston.transports.File({
filename: './.log/frontserver-exceptions.log' filename: './.log/frontserver-exceptions.log'

Loading…
Cancel
Save