add support for comments in config files.
This commit is contained in:
@@ -14,13 +14,14 @@ class WTVShared {
|
||||
iconv = require('iconv-lite');
|
||||
parentDirectory = process.cwd()
|
||||
extend = require('util')._extend;
|
||||
debug = require('debug')('WTVShared')
|
||||
|
||||
minisrv_config = [];
|
||||
|
||||
constructor(minisrv_config, quiet = false) {
|
||||
if (minisrv_config == null) this.minisrv_config = this.readMiniSrvConfig(true, !quiet);
|
||||
else this.minisrv_config = minisrv_config;
|
||||
|
||||
|
||||
if (!String.prototype.reverse) {
|
||||
String.prototype.reverse = function () {
|
||||
var splitString = this.split("");
|
||||
@@ -59,6 +60,72 @@ class WTVShared {
|
||||
return src;
|
||||
}
|
||||
|
||||
parseJSON(json) {
|
||||
if (!json) return null;
|
||||
if (typeof json !== 'string') json = json.toString();
|
||||
|
||||
// from https://github.com/getify/JSON.minify/blob/javascript/minify.json.js
|
||||
var tokenizer = /"|(\/\*)|(\*\/)|(\/\/)|\n|\r/g,
|
||||
in_string = false,
|
||||
in_multiline_comment = false,
|
||||
in_singleline_comment = false,
|
||||
tmp, tmp2, new_str = [], ns = 0, from = 0, lc, rc,
|
||||
prevFrom
|
||||
;
|
||||
|
||||
tokenizer.lastIndex = 0;
|
||||
|
||||
while (tmp = tokenizer.exec(json)) {
|
||||
lc = RegExp.leftContext;
|
||||
rc = RegExp.rightContext;
|
||||
if (!in_multiline_comment && !in_singleline_comment) {
|
||||
tmp2 = lc.substring(from);
|
||||
if (!in_string) {
|
||||
tmp2 = tmp2.replace(/(\n|\r|\s)+/g, "");
|
||||
}
|
||||
new_str[ns++] = tmp2;
|
||||
}
|
||||
prevFrom = from;
|
||||
from = tokenizer.lastIndex;
|
||||
|
||||
// found a " character, and we're not currently in
|
||||
// a comment? check for previous `\` escaping immediately
|
||||
// leftward adjacent to this match
|
||||
if (tmp[0] == "\"" && !in_multiline_comment && !in_singleline_comment) {
|
||||
// perform look-behind escaping match, but
|
||||
// limit left-context matching to only go back
|
||||
// to the position of the last token match
|
||||
//
|
||||
// see: https://github.com/getify/JSON.minify/issues/64
|
||||
tmp2 = lc.substring(prevFrom).match(/\\+$/);
|
||||
|
||||
// start of string with ", or unescaped " character found to end string?
|
||||
if (!in_string || !tmp2 || (tmp2[0].length % 2) == 0) {
|
||||
in_string = !in_string;
|
||||
}
|
||||
from--; // include " character in next catch
|
||||
rc = json.substring(from);
|
||||
}
|
||||
else if (tmp[0] == "/*" && !in_string && !in_multiline_comment && !in_singleline_comment) {
|
||||
in_multiline_comment = true;
|
||||
}
|
||||
else if (tmp[0] == "*/" && !in_string && in_multiline_comment && !in_singleline_comment) {
|
||||
in_multiline_comment = false;
|
||||
}
|
||||
else if (tmp[0] == "//" && !in_string && !in_multiline_comment && !in_singleline_comment) {
|
||||
in_singleline_comment = true;
|
||||
}
|
||||
else if ((tmp[0] == "\n" || tmp[0] == "\r") && !in_string && !in_multiline_comment && in_singleline_comment) {
|
||||
in_singleline_comment = false;
|
||||
}
|
||||
else if (!in_multiline_comment && !in_singleline_comment && !(/\n|\r|\s/.test(tmp[0]))) {
|
||||
new_str[ns++] = tmp[0];
|
||||
}
|
||||
}
|
||||
new_str[ns++] = rc;
|
||||
return JSON.parse(new_str.join(""));
|
||||
}
|
||||
|
||||
isConfiguredService(service) {
|
||||
if (this.minisrv_config.services[service]) {
|
||||
if (!this.minisrv_config.services[service].disabled) return true;
|
||||
@@ -236,12 +303,12 @@ class WTVShared {
|
||||
getUserConfig() {
|
||||
try {
|
||||
var user_config_filename = this.getAbsolutePath("user_config.json", this.parentDirectory);
|
||||
|
||||
if (this.fs.lstatSync(user_config_filename)) {
|
||||
try {
|
||||
var minisrv_user_config = JSON.parse(this.fs.readFileSync(user_config_filename));
|
||||
var minisrv_user_config = this.parseJSON(this.fs.readFileSync(user_config_filename));
|
||||
} catch (e) {
|
||||
console.error("ERROR: Could not read user_config.json", e);
|
||||
var throw_me = true;
|
||||
throw ("ERROR: Could not read user_config.json", e);
|
||||
}
|
||||
} else {
|
||||
var minisrv_user_config = {}
|
||||
@@ -288,7 +355,7 @@ class WTVShared {
|
||||
readMiniSrvConfig(user_config = true, notices = true, reload_notice = false) {
|
||||
if (notices || reload_notice) console.log(" *** Reading global configuration...");
|
||||
try {
|
||||
var minisrv_config = JSON.parse(this.fs.readFileSync(this.getAbsolutePath("config.json", __dirname)));
|
||||
var minisrv_config = this.parseJSON(this.fs.readFileSync(this.getAbsolutePath("config.json", __dirname)));
|
||||
} catch (e) {
|
||||
throw ("ERROR: Could not read config.json", e);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user