Add async to all paths that JSONata env var calls

This commit is contained in:
Steve-Mcl
2023-06-17 21:14:56 +01:00
parent fb5b470966
commit 4808cac89d
4 changed files with 184 additions and 69 deletions

View File

@@ -18,6 +18,7 @@
/**
* @mixin @node-red/util_util
*/
/** @typedef {import('../../runtime/lib/flows/Flow.js').Flow} RuntimeLibFlowsFlow */
const clonedeep = require("lodash.clonedeep");
const jsonata = require("jsonata");
@@ -526,37 +527,67 @@ function setObjectProperty(msg,prop,value,createMissing) {
return true;
}
/*!
/**
* Get value of environment variable.
* @param {Node} node - accessing node
* @param {String} name - name of variable
* @param {RuntimeLibFlowsFlow} flow_ - (optional) flow to check for setting
* @param {(err: Error, result: Any) => void} callback - (optional) called when the property is evaluated
* @return {String} value of env var
*/
function getSetting(node, name, flow_) {
function getSetting(node, name, flow_, callback) {
const returnOrCallback = (err, result) => {
if (callback) {
callback(err, result);
return
}
return result;
}
if (node) {
if (name === "NR_NODE_NAME") {
return node.name;
return returnOrCallback(null, node.name);
}
if (name === "NR_NODE_ID") {
return node.id;
return returnOrCallback(null, node.id);
}
if (name === "NR_NODE_PATH") {
return node._path;
return returnOrCallback(null, node._path);
}
}
/** @type {RuntimeLibFlowsFlow} */
var flow = (flow_ ? flow_ : (node ? node._flow : null));
if (flow) {
if (node && node.g) {
const group = flow.getGroupNode(node.g);
const [result, newName] = flow.getGroupEnvSetting(node, group, name);
if (result) {
return result.val;
if (callback) {
flow.getGroupEnvSetting(node, group, name, (e, [result, newName]) => {
if (e) {
callback(e);
return
}
if (result) {
callback(null, result.val);
}
name = newName;
flow.getSetting(name, callback);
});
return
} else {
const [result, newName] = flow.getGroupEnvSetting(node, group, name);
if (result) {
return result.val;
}
name = newName;
}
name = newName;
}
return flow.getSetting(name);
const fVal = flow.getSetting(name, callback)
if (callback) {
return
}
return fVal;
}
return process.env[name];
return returnOrCallback(null, process.env[name]);
}
@@ -568,19 +599,34 @@ function getSetting(node, name, flow_) {
* will return `Hello Joe!`.
* @param {String} value - the string to parse
* @param {Node} node - the node evaluating the property
* @param {(err: Error, result: Any) => void} callback - (optional) called when the property is evaluated
* @return {String} The parsed string
* @memberof @node-red/util_util
*/
function evaluateEnvProperty(value, node) {
function evaluateEnvProperty(value, node, callback) {
const returnOrCallback = (err, result) => {
if (callback) {
callback(err, result);
return
}
return result;
}
/** @type {RuntimeLibFlowsFlow} */
var flow = (node && hasOwnProperty.call(node, "_flow")) ? node._flow : null;
var result;
if (/^\${[^}]+}$/.test(value)) {
// ${ENV_VAR}
var name = value.substring(2,value.length-1);
result = getSetting(node, name, flow);
result = getSetting(node, name, flow, callback);
if (callback) {
return
}
} else if (!/\${\S+}/.test(value)) {
// ENV_VAR
result = getSetting(node, value, flow);
result = getSetting(node, value, flow, callback);
if (callback) {
return
}
} else {
// FOO${ENV_VAR}BAR
return value.replace(/\${([^}]+)}/g, function(match, name) {
@@ -588,8 +634,7 @@ function evaluateEnvProperty(value, node) {
return (val === undefined)?"":val;
});
}
return (result === undefined)?"":result;
return returnOrCallback(null, (result === undefined)?"":result);
}
@@ -677,7 +722,10 @@ function evaluateNodeProperty(value, type, node, msg, callback) {
return
}
} else if (type === 'env') {
result = evaluateEnvProperty(value, node);
result = evaluateEnvProperty(value, node, callback);
if (callback) {
return
}
}
if (callback) {
callback(null,result);