From 85b6d26841d12b626163f076df18add625f46a50 Mon Sep 17 00:00:00 2001 From: Gerrit Riessen Date: Tue, 2 Jan 2024 09:44:51 +0100 Subject: [PATCH] use builtin functionality --- red/deadred.js | 33 ++-- red/taken_from_nodered_backend.js | 261 ------------------------------ 2 files changed, 20 insertions(+), 274 deletions(-) diff --git a/red/deadred.js b/red/deadred.js index 1446798..1be9fc0 100644 --- a/red/deadred.js +++ b/red/deadred.js @@ -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 )) ) @@ -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 )) ) @@ -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) @@ -175,7 +175,7 @@ var DEADRED = (function() { return } else if ( typeof val == "object") { - NodeRedBackendCode.setMessageProperty( + RED.utils.setMessageProperty( msg, nde.property, bytesToBase64(val) @@ -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) ) } } @@ -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) ) { @@ -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 } @@ -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 ) diff --git a/red/taken_from_nodered_backend.js b/red/taken_from_nodered_backend.js index e873f29..05a4937 100644 --- a/red/taken_from_nodered_backend.js +++ b/red/taken_from_nodered_backend.js @@ -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 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 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; }, @@ -342,7 +83,5 @@ var NodeRedBackendCode = (function(){ return { switchOperators: switchOperators, - getMessageProperty: getMessageProperty, - setMessageProperty: setMessageProperty } })();