mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
Add support of subflow env var
This commit is contained in:
38
packages/node_modules/@node-red/util/lib/util.js
vendored
38
packages/node_modules/@node-red/util/lib/util.js
vendored
@@ -413,6 +413,20 @@ function setObjectProperty(msg,prop,value,createMissing) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get value of environment variable.
|
||||
* @param {Node} node - accessing node
|
||||
* @param {String} name - name of variable
|
||||
* @return {String} value of env var
|
||||
*/
|
||||
function getenv(node, name) {
|
||||
if (node && node.getenv) {
|
||||
return node.getenv(name);
|
||||
}
|
||||
return process.env[name];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Checks if a String contains any Environment Variable specifiers and returns
|
||||
* it with their values substituted in place.
|
||||
@@ -420,24 +434,27 @@ function setObjectProperty(msg,prop,value,createMissing) {
|
||||
* For example, if the env var `WHO` is set to `Joe`, the string `Hello ${WHO}!`
|
||||
* will return `Hello Joe!`.
|
||||
* @param {String} value - the string to parse
|
||||
* @param {Node} node - the node evaluating the property
|
||||
* @return {String} The parsed string
|
||||
* @memberof @node-red/util_util
|
||||
*/
|
||||
function evaluateEnvProperty(value) {
|
||||
function evaluateEnvProperty(value, node) {
|
||||
if (/^\${[^}]+}$/.test(value)) {
|
||||
// ${ENV_VAR}
|
||||
value = value.substring(2,value.length-1);
|
||||
value = process.env.hasOwnProperty(value)?process.env[value]:""
|
||||
var name = value.substring(2,value.length-1);
|
||||
var val = getenv(node, name);
|
||||
return val ? val : "";
|
||||
} else if (!/\${\S+}/.test(value)) {
|
||||
// ENV_VAR
|
||||
value = process.env.hasOwnProperty(value)?process.env[value]:""
|
||||
var val = getenv(node, value);
|
||||
return val ? val : "";
|
||||
} else {
|
||||
// FOO${ENV_VAR}BAR
|
||||
value = value.replace(/\${([^}]+)}/g, function(match, v) {
|
||||
return process.env.hasOwnProperty(v)?process.env[v]:""
|
||||
return value.replace(/\${([^}]+)}/g, function(match, name) {
|
||||
var val = getenv(node, name);
|
||||
return (val ? val : "");
|
||||
});
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
@@ -513,7 +530,7 @@ function evaluateNodeProperty(value, type, node, msg, callback) {
|
||||
var expr = prepareJSONataExpression(value,node);
|
||||
result = evaluateJSONataExpression(expr,msg);
|
||||
} else if (type === 'env') {
|
||||
result = evaluateEnvProperty(value);
|
||||
result = evaluateEnvProperty(value, node);
|
||||
}
|
||||
if (callback) {
|
||||
callback(null,result);
|
||||
@@ -540,8 +557,9 @@ function prepareJSONataExpression(value,node) {
|
||||
expr.assign('globalContext',function(val) {
|
||||
return node.context().global.get(val);
|
||||
});
|
||||
expr.assign('env', function(val) {
|
||||
return process.env[val];
|
||||
expr.assign('env', function(name) {
|
||||
var val = getenv(node, name);
|
||||
return (val ? val : "");
|
||||
})
|
||||
expr.registerFunction('clone', cloneMessage, '<(oa)-:o>');
|
||||
expr._legacyMode = /(^|[^a-zA-Z0-9_'"])msg([^a-zA-Z0-9_'"]|$)/.test(value);
|
||||
|
Reference in New Issue
Block a user