Skip to content

Commit

Permalink
Merge branch 'main' into deadred
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit Riessen committed Jan 2, 2024
2 parents b71b36c + 85b6d26 commit 6a23c96
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 274 deletions.
33 changes: 20 additions & 13 deletions red/deadred.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ var DEADRED = (function() {
return new TextDecoder().decode(base64ToBytes(content));
};

NodeRedBackendCode.setMessageProperty(
RED.utils.setMessageProperty(
msg,
nde.property,
atobUtf8(NodeRedBackendCode.getMessageProperty(
atobUtf8(RED.utils.getMessageProperty(
msg, nde.property
))
)
Expand All @@ -147,10 +147,10 @@ var DEADRED = (function() {
return bytesToBase64(new TextEncoder().encode(content))
}

NodeRedBackendCode.setMessageProperty(
RED.utils.setMessageProperty(
msg,
nde.property,
btoaUtf8(NodeRedBackendCode.getMessageProperty(
btoaUtf8(RED.utils.getMessageProperty(
msg, nde.property
))
)
Expand All @@ -160,12 +160,12 @@ var DEADRED = (function() {
}

if ( nde.action == "" ) {
var val = NodeRedBackendCode.getMessageProperty(
var val = RED.utils.getMessageProperty(
msg, nde.property
)

if ( typeof val == "string" ) {
NodeRedBackendCode.setMessageProperty(
RED.utils.setMessageProperty(
msg,
nde.property,
base64ToBytes(val)
Expand All @@ -175,7 +175,7 @@ var DEADRED = (function() {
return

} else if ( typeof val == "object") {
NodeRedBackendCode.setMessageProperty(
RED.utils.setMessageProperty(
msg,
nde.property,
bytesToBase64(val)
Expand Down Expand Up @@ -228,8 +228,10 @@ var DEADRED = (function() {
msg[rle.p] = jsonata(rle.to).evaluate({msg:msg})
}
if ( rle.pt == "msg" && rle.tot == "msg" ) {
msg[rle.p] = NodeRedBackendCode.getMessageProperty(
msg, rle.to
RED.utils.setMessageProperty(
msg,
rle.p,
RED.utils.getMessageProperty(msg, rle.to)
)
}
}
Expand Down Expand Up @@ -306,12 +308,17 @@ var DEADRED = (function() {
}

if ( nde.complete != "true" ) {
if (nde.complete == "false"){ nde.complete = "payload" }
debugData["property"] = nde.complete
var val = NodeRedBackendCode.getMessageProperty(

var val = RED.utils.getMessageProperty(
msg, nde.complete
)

if ( typeof val === "string" ) {
if ( val == undefined ) {
debugData["msg"] = val
debugData["format"] = "string[0]"
} else if ( typeof val === "string" ) {
debugData["msg"] = val
debugData["format"] = "string[" + val.length + "]"
} else if ( Array.isArray(val) ) {
Expand Down Expand Up @@ -457,7 +464,7 @@ var DEADRED = (function() {
if ( nde.ret == "txt" ) {
msg.payload = data.toString("utf8")
} else if ( nde.ret == "bin" ) {
msg.payload = new ArrayBuffer(data)
msg.payload = data
} else {
msg.payload = data
}
Expand Down Expand Up @@ -635,7 +642,7 @@ var DEADRED = (function() {
case 'switch':

if ( nde.property && nde.propertyType == "msg" ) {
let val = NodeRedBackendCode.getMessageProperty(
let val = RED.utils.getMessageProperty(
msg,nde.property
)

Expand Down
261 changes: 0 additions & 261 deletions red/taken_from_nodered_backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,265 +16,6 @@
**/

var NodeRedBackendCode = (function(){

function normalisePropertyExpression(str, msg, toString) {
// This must be kept in sync with validatePropertyExpression
// in editor/js/ui/utils.js

var length = str.length;
if (length === 0) {
throw createError("INVALID_EXPR","Invalid property expression: zero-length");
}
var parts = [];
var start = 0;
var inString = false;
var inBox = false;
var boxExpression = false;
var quoteChar;
var v;
for (var i=0;i<length;i++) {
var c = str[i];
if (!inString) {
if (c === "'" || c === '"') {
if (i != start) {
throw createError("INVALID_EXPR","Invalid property expression: unexpected "+c+" at position "+i);
}
inString = true;
quoteChar = c;
start = i+1;
} else if (c === '.') {
if (i===0) {
throw createError("INVALID_EXPR","Invalid property expression: unexpected . at position 0");
}
if (start != i) {
v = str.substring(start,i);
if (/^\d+$/.test(v)) {
parts.push(parseInt(v));
} else {
parts.push(v);
}
}
if (i===length-1) {
throw createError("INVALID_EXPR","Invalid property expression: unterminated expression");
}
// Next char is first char of an identifier: a-z 0-9 $ _
if (!/[a-z0-9\$\_]/i.test(str[i+1])) {
throw createError("INVALID_EXPR","Invalid property expression: unexpected "+str[i+1]+" at position "+(i+1));
}
start = i+1;
} else if (c === '[') {
if (i === 0) {
throw createError("INVALID_EXPR","Invalid property expression: unexpected "+c+" at position "+i);
}
if (start != i) {
parts.push(str.substring(start,i));
}
if (i===length-1) {
throw createError("INVALID_EXPR","Invalid property expression: unterminated expression");
}
// Start of a new expression. If it starts with msg it is a nested expression
// Need to scan ahead to find the closing bracket
if (/^msg[.\[]/.test(str.substring(i+1))) {
var depth = 1;
var inLocalString = false;
var localStringQuote;
for (var j=i+1;j<length;j++) {
if (/["']/.test(str[j])) {
if (inLocalString) {
if (str[j] === localStringQuote) {
inLocalString = false
}
} else {
inLocalString = true;
localStringQuote = str[j]
}
}
if (str[j] === '[') {
depth++;
} else if (str[j] === ']') {
depth--;
}
if (depth === 0) {
try {
if (msg) {
var crossRefProp = getMessageProperty(msg, str.substring(i+1,j));
if (crossRefProp === undefined) {
throw createError("INVALID_EXPR","Invalid expression: undefined reference at position "+(i+1)+" : "+str.substring(i+1,j))
}
parts.push(crossRefProp)
} else {
parts.push(normalisePropertyExpression(str.substring(i+1,j), msg));
}
inBox = false;
i = j;
start = j+1;
break;
} catch(err) {
throw createError("INVALID_EXPR","Invalid expression started at position "+(i+1))
}
}
}
if (depth > 0) {
throw createError("INVALID_EXPR","Invalid property expression: unmatched '[' at position "+i);
}
continue;
} else if (!/["'\d]/.test(str[i+1])) {
// Next char is either a quote or a number
throw createError("INVALID_EXPR","Invalid property expression: unexpected "+str[i+1]+" at position "+(i+1));
}
start = i+1;
inBox = true;
} else if (c === ']') {
if (!inBox) {
throw createError("INVALID_EXPR","Invalid property expression: unexpected "+c+" at position "+i);
}
if (start != i) {
v = str.substring(start,i);
if (/^\d+$/.test(v)) {
parts.push(parseInt(v));
} else {
throw createError("INVALID_EXPR","Invalid property expression: unexpected array expression at position "+start);
}
}
start = i+1;
inBox = false;
} else if (c === ' ') {
throw createError("INVALID_EXPR","Invalid property expression: unexpected ' ' at position "+i);
}
} else {
if (c === quoteChar) {
if (i-start === 0) {
throw createError("INVALID_EXPR","Invalid property expression: zero-length string at position "+start);
}
parts.push(str.substring(start,i));
// If inBox, next char must be a ]. Otherwise it may be [ or .
if (inBox && !/\]/.test(str[i+1])) {
throw createError("INVALID_EXPR","Invalid property expression: unexpected array expression at position "+start);
} else if (!inBox && i+1!==length && !/[\[\.]/.test(str[i+1])) {
throw createError("INVALID_EXPR","Invalid property expression: unexpected "+str[i+1]+" expression at position "+(i+1));
}
start = i+1;
inString = false;
}
}

}
if (inBox || inString) {
throw new createError("INVALID_EXPR","Invalid property expression: unterminated expression");
}
if (start < length) {
parts.push(str.substring(start));
}

if (toString) {
var result = parts.shift();
while(parts.length > 0) {
var p = parts.shift();
if (typeof p === 'string') {
if (/"/.test(p)) {
p = "'"+p+"'";
} else {
p = '"'+p+'"';
}
}
result = result+"["+p+"]";
}
return result;
}

return parts;
}


function getObjectProperty(msg,expr) {
var result = null;
var msgPropParts = normalisePropertyExpression(expr,msg);
msgPropParts.reduce(function(obj, key) {
result = (typeof obj[key] !== "undefined" ? obj[key] : undefined);
return result;
}, msg);
return result;
}

function getMessageProperty(msg,expr) {
if (expr.indexOf('msg.')===0) {
expr = expr.substring(4);
}
return getObjectProperty(msg,expr);
}

function setObjectProperty(msg,prop,value,createMissing) {
if (typeof createMissing === 'undefined') {
createMissing = (typeof value !== 'undefined');
}
var msgPropParts = normalisePropertyExpression(prop, msg);
var depth = 0;
var length = msgPropParts.length;
var obj = msg;
var key;
for (var i=0;i<length-1;i++) {
key = msgPropParts[i];
if (typeof key === 'string' || (typeof key === 'number' && !Array.isArray(obj))) {
if (hasOwnProperty.call(obj, key)) {
if (length > 1 && ((typeof obj[key] !== "object" && typeof obj[key] !== "function") || obj[key] === null)) {
// Break out early as we cannot create a property beneath
// this type of value
return false;
}
obj = obj[key];
} else if (createMissing) {
if (typeof msgPropParts[i+1] === 'string') {
obj[key] = {};
} else {
obj[key] = [];
}
obj = obj[key];
} else {
return false;
}
} else if (typeof key === 'number') {
// obj is an array
if (obj[key] === undefined) {
if (createMissing) {
if (typeof msgPropParts[i+1] === 'string') {
obj[key] = {};
} else {
obj[key] = [];
}
obj = obj[key];
} else {
return false;
}
} else {
obj = obj[key];
}
}
}
key = msgPropParts[length-1];
if (typeof value === "undefined") {
if (typeof key === 'number' && Array.isArray(obj)) {
obj.splice(key,1);
} else {
delete obj[key]
}
} else {
if (typeof obj === "object" && obj !== null) {
obj[key] = value;
} else {
// Cannot set a property of a non-object/array
return false;
}
}
return true;
}

function setMessageProperty(msg,prop,value,createMissing) {
if (prop.indexOf('msg.')===0) {
prop = prop.substring(4);
}
return setObjectProperty(msg,prop,value,createMissing);
}

const switchOperators = {
'eq': function(a, b) { return a == b; },
'neq': function(a, b) { return a != b; },
Expand Down Expand Up @@ -342,7 +83,5 @@ var NodeRedBackendCode = (function(){

return {
switchOperators: switchOperators,
getMessageProperty: getMessageProperty,
setMessageProperty: setMessageProperty
}
})();

0 comments on commit 6a23c96

Please sign in to comment.