From de33d39a939fee6d0f7be0635e9a43667348d339 Mon Sep 17 00:00:00 2001 From: zefie Date: Sat, 22 Oct 2022 19:07:11 -0400 Subject: [PATCH] implement proper nodejs debugging - will start to phase out using console.log for actual debugging - existing "debug" (verbose) messages will stay as is - future code debugging will use debug() to debug WIP, therefore not showing to most users if it is accidently left in --- zefie_wtvp_minisrv/app.js | 18 +++++++++++++++++- zefie_wtvp_minisrv/includes/WTVShared.js | 7 +++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/zefie_wtvp_minisrv/app.js b/zefie_wtvp_minisrv/app.js index 0dc9db12..7e21d215 100644 --- a/zefie_wtvp_minisrv/app.js +++ b/zefie_wtvp_minisrv/app.js @@ -22,6 +22,7 @@ const WTVClientSessionData = require(classPath + "/WTVClientSessionData.js"); const WTVMime = require(classPath + "/WTVMime.js"); const WTVFlashrom = require(classPath + "/WTVFlashrom.js"); const vm = require('vm'); +const debug = require('debug')('minisrv_main'); const express = require('express'); var wtvnewsserver = null; @@ -182,12 +183,26 @@ async function sendRawFile(socket, path) { }); } -var runScriptInVM = function (script_data, user_contextObj = {}, privileged = false, filename = null) { +var runScriptInVM = function (script_data, user_contextObj = {}, privileged = false, filename = null, debug_name = null) { // Here we define the ServiceVault Script Context Object // The ServiceVault scripts will only be allowed to access the following fcnutions/variables. // Furthermore, only modifications to variables in `updateFromVM` will be saved. // Example: an attempt to change "minisrv_config" from a ServiceVault script would be discarded + // try to build a name for the script's debug() calls + if (!debug_name) { + // try to make the debug name + var debug_name = (filename) ? filename.split(path.sep) : null; + if (debug_name) { + if (wtvshared.isConfiguredService(debug_name[debug_name.length - 2])) + // service:/filename + debug_name = debug_name[debug_name.length - 2] + ":/" + debug_name[debug_name.length - 1]; + else + // filename + debug_name = debug_name[debug_name.length - 1]; + } + } + // create global context object var contextObj = { // node core variables and functions @@ -210,6 +225,7 @@ var runScriptInVM = function (script_data, user_contextObj = {}, privileged = fa "path": path, // Our variables and functions + "debug": require('debug')((debug_name) ? debug_name : 'service_script'), "minisrv_config": minisrv_config, "socket": null, "headers": null, diff --git a/zefie_wtvp_minisrv/includes/WTVShared.js b/zefie_wtvp_minisrv/includes/WTVShared.js index 24f2f9cd..1b7c399d 100644 --- a/zefie_wtvp_minisrv/includes/WTVShared.js +++ b/zefie_wtvp_minisrv/includes/WTVShared.js @@ -59,6 +59,13 @@ class WTVShared { return src; } + isConfiguredService(service) { + if (this.minisrv_config.services[service]) { + if (!this.minisrv_config.services[service].disabled) return true; + } + return false; + } + getServiceString(service, overrides = {}) { // used externally by service scripts if (service === "all") {