Revert "Add callback to getSetting to support async jsonata access"

This commit is contained in:
Nick O'Leary
2023-06-22 10:17:48 +01:00
committed by GitHub
parent 2448e137c8
commit 51a0b68d8e
9 changed files with 90 additions and 287 deletions

View File

@@ -416,50 +416,23 @@ class Flow {
return this.activeNodes;
}
/**
* Group callback signature
*
* @callback GroupEnvCallback
* @param {Error} err The error object (or null)
* @param {[result: {val:Any}, name: String]} result The result of the callback
* @returns {void}
/*!
* Get value of environment variable defined in group node.
* @param {String} group - group node
* @param {String} name - name of variable
* @return {Object} object containing the value in val property or null if not defined
*/
/**
* @function getGroupEnvSetting
* Get a group setting value synchronously.
* This currently automatically defers to the parent
* @overload
* @param {Object} node
* @param {Object} group
* @param {String} name
* @returns {Any}
*
* Get a group setting value asynchronously.
* @overload
* @param {Object} node
* @param {Object} group
* @param {String} name
* @param {GroupEnvCallback} callback
* @returns {void}
*/
getGroupEnvSetting(node, group, name, callback) {
/** @type {GroupEnvCallback} */
const returnOrCallback = (err, [result, newName]) => {
if (callback) {
callback(err, [result, newName]);
return
}
return [result, newName];
}
getGroupEnvSetting(node, group, name) {
if (group) {
if (name === "NR_GROUP_NAME") {
return returnOrCallback(null, [{ val: group.name }, null]);
return [{
val: group.name
}, null];
}
if (name === "NR_GROUP_ID") {
return returnOrCallback(null, [{ val: group.id }, null]);
return [{
val: group.id
}, null];
}
if (group.credentials === undefined) {
@@ -484,32 +457,33 @@ class Flow {
if (env) {
let value = env.value;
const type = env.type;
if ((type !== "env") || (value !== name)) {
if ((type !== "env") ||
(value !== name)) {
if (type === "env") {
value = value.replace(new RegExp("\\${"+name+"}","g"),"${$parent."+name+"}");
} else if (type === "bool") {
const val = ((value === "true") || (value === true));
return returnOrCallback(null, [{ val: val }, null])
}
if (type === "bool") {
const val
= ((value === "true") ||
(value === true));
return [{
val: val
}, null];
}
if (type === "cred") {
return returnOrCallback(null, [{ val: value }, null])
return [{
val: value
}, null];
}
try {
if (!callback) {
var val = redUtil.evaluateNodeProperty(value, type, node, null, null);
return [{ val: val }, null];
} else {
redUtil.evaluateNodeProperty(value, type, node, null, (err, value) => {
return returnOrCallback(err, [{ val: value }, null])
});
return
}
var val = redUtil.evaluateNodeProperty(value, type, node, null, null);
return [{
val: val
}, null];
}
catch (e) {
if (!callback) {
this.error(e);
}
return returnOrCallback(e, null);
this.error(e);
return [null, null];
}
}
}
@@ -520,47 +494,27 @@ class Flow {
}
if (group.g) {
const parent = this.getGroupNode(group.g);
const gVal = this.getGroupEnvSetting(node, parent, name, callback);
if (callback) {
return;
}
return gVal;
return this.getGroupEnvSetting(node, parent, name);
}
}
return returnOrCallback(null, [null, name]);
return [null, name];
}
/**
* Settings callback signature
*
* @callback SettingsCallback
* @param {Error} err The error object (or null)
* @param {Any} result The result of the callback
* @returns {void}
*/
/**
* Get a flow setting value. This currently automatically defers to the parent
* flow which, as defined in ./index.js returns `process.env[key]`.
* This lays the groundwork for Subflow to have instance-specific settings
* @param {String} key The settings key
* @param {SettingsCallback} callback Optional callback function
* @return {Any}
* @param {[type]} key [description]
* @return {[type]} [description]
*/
getSetting(key, callback) {
/** @type {SettingsCallback} */
const returnOrCallback = (err, result) => {
if (callback) {
callback(err, result);
return
}
return result;
}
getSetting(key) {
const flow = this.flow;
if (key === "NR_FLOW_NAME") {
return returnOrCallback(null, flow.label);
return flow.label;
}
if (key === "NR_FLOW_ID") {
return returnOrCallback(null, flow.id);
return flow.id;
}
if (flow.credentials === undefined) {
flow.credentials = credentials.get(flow.id) || {};
@@ -590,14 +544,15 @@ class Flow {
}
try {
if (type === "bool") {
const val = ((value === "true") || (value === true));
return returnOrCallback(null, val);
const val = ((value === "true") ||
(value === true));
return val;
}
if (type === "cred") {
return returnOrCallback(null, value);
return value;
}
var val = redUtil.evaluateNodeProperty(value, type, null, null, null);
return returnOrCallback(null, val);
return val;
}
catch (e) {
this.error(e);
@@ -609,11 +564,7 @@ class Flow {
key = key.substring(8);
}
}
const pVal = this.parent.getSetting(key, callback);
if (callback) {
return;
}
return pVal;
return this.parent.getSetting(key);
}
/**

View File

@@ -780,7 +780,7 @@ const flowAPI = {
getNode: getNode,
handleError: () => false,
handleStatus: () => false,
getSetting: (k, callback) => flowUtil.getEnvVar(k, callback),
getSetting: k => flowUtil.getEnvVar(k),
log: m => log.log(m)
}

View File

@@ -308,34 +308,16 @@ module.exports = {
runtime.settings.envVarExcludes.forEach(v => envVarExcludes[v] = true);
}
},
/**
* Get the value of an environment variable
* Call with a callback to get the value asynchronously
* or without to get the value synchronously
* @param {String} key The name of the environment variable
* @param {(err: Error, val: Any)} [callback] Optional callback for asynchronous call
* @returns {Any | void} The value of the environment variable or undefined if not found
*/
getEnvVar: function(key, callback) {
const returnOrCallback = function(err, val) {
if (callback) {
callback(err, val);
return;
}
return val;
}
if (!envVarExcludes[key]) {
const item = getGlobalEnv(key);
getEnvVar: function(k) {
if (!envVarExcludes[k]) {
const item = getGlobalEnv(k);
if (item) {
const val = redUtil.evaluateNodeProperty(item.value, item.type, null, null, callback);
if (callback) {
return;
}
const val = redUtil.evaluateNodeProperty(item.value, item.type, null, null, null);
return val;
}
return returnOrCallback(null, process.env[key]);
return process.env[k];
}
return returnOrCallback(undefined);
return undefined;
},
diffNodes: diffNodes,
mapEnvVarProperties: mapEnvVarProperties,