Node red : insert js code on node-red function

Hello,

I have a small project that I should to split the reading value from a sensor.
The supplier of the sensor give a formula in order to decode the value of the actual reading
I paste the deocoder function (.js) on node function, but this is not work, I tried many times to arrange the formula but without success.
Please if someone can help me to solve it.

error message : "Function tried to send a message of type function"

flow input: "05040030060400300965000C0B6700FC0B683900BA710C6700F3"

the decode program on js format on the following link :

the Node-red flow simulation :

the simulation input MQTT:
{"applicationID":"1","applicationName":"T0005987","data":"BQQAMAYEADAJZQAMC2cA/AtoOQC6cQxnAPM=","devEUI":"647fda0000007fa9","deviceName":"T0005987","fCnt":3864,"fPort":10,"rxInfo":[{"altitude":0,"latitude":0,"loRaSNR":14,"longitude":0,"mac":"24e124fffef128ea","name":"Local Gateway","rssi":-58,"time":"2021-09-06T13:34:11.770693Z"}],"time":"2021-09-06T13:34:11.770693Z","txInfo":{"adr":true,"codeRate":"4/5","dataRate":{"bandwidth":125,"modulation":"LORA","spreadFactor":7},"frequency":868100000}}

Function node must ALWAYS return an object (normally that is the incoming msg)

Here is what I imagine you are supposed to have...

Demo flow (import using CTRL+I)...

[{"id":"072041207853ad1e","type":"function","z":"0489bdf91e1981f0","name":"Decode","func":"var dataBytes = msg.payload;\nvar port = msg.mqttPayload.fPort;\n\nmsg.payload = Decoder(dataBytes, port)\n\nreturn msg;\n\nfunction Decoder(bytes, port) { //bytes - Array of bytes (signed)\n\n    function slice(a, f, t) {\n        var res = [];\n        for (var i = 0; i < t - f; i++) {\n            res[i] = a[f + i];\n        }\n        return res;\n    }\n\n    function extract_bytes(chunk, start_bit, end_bit) {\n        var total_bits = end_bit - start_bit + 1;\n        var total_bytes = total_bits % 8 === 0 ? to_uint(total_bits / 8) : to_uint(total_bits / 8) + 1;\n        var offset_in_byte = start_bit % 8;\n        var end_bit_chunk = total_bits % 8;\n        var arr = new Array(total_bytes);\n        for (byte = 0; byte < total_bytes; ++byte) {\n            var chunk_idx = to_uint(start_bit / 8) + byte;\n            var lo = chunk[chunk_idx] >> offset_in_byte;\n            var hi = 0;\n            if (byte < total_bytes - 1) {\n                hi = (chunk[chunk_idx + 1] & ((1 << offset_in_byte) - 1)) << (8 - offset_in_byte);\n            } else if (end_bit_chunk !== 0) {\n                // Truncate last bits\n                lo = lo & ((1 << end_bit_chunk) - 1);\n            }\n            arr[byte] = hi | lo;\n        }\n        return arr;\n    }\n\n    function apply_data_type(bytes, data_type) {\n        var output = 0;\n        if (data_type === \"unsigned\") {\n            for (var i = 0; i < bytes.length; ++i) {\n                output = (to_uint(output << 8)) | bytes[i];\n            }\n            return output;\n        }\n        if (data_type === \"signed\") {\n            for (var i = 0; i < bytes.length; ++i) {\n                output = (output << 8) | bytes[i];\n            }\n            // Convert to signed, based on value size\n            if (output > Math.pow(2, 8 * bytes.length - 1)) {\n                output -= Math.pow(2, 8 * bytes.length);\n            }\n            return output;\n        }\n        if (data_type === \"bool\") {\n            return !(bytes[0] === 0);\n        }\n        if (data_type === \"hexstring\") {\n            return toHexString(bytes);\n        }\n        // Incorrect data type\n        return null;\n    }\n\n    function decode_field(chunk, start_bit, end_bit, data_type) {\n        var chunk_size = chunk.length;\n        if (end_bit >= chunk_size * 8) {\n            return null; // Error: exceeding boundaries of the chunk\n        }\n        if (end_bit < start_bit) {\n            return null; // Error: invalid input\n        }\n        var arr = extract_bytes(chunk, start_bit, end_bit);\n        return apply_data_type(arr, data_type);\n    }\n\n    var decoded_data = {};\n    var decoder = [];\n\n    if (port === 10) {\n        decoder = [\n            {\n                key: [0x00, 0xFF],\n                fn: function (arg) {\n                    decoded_data.battery_life = decode_field(arg, 0, 15, \"unsigned\") * 0.01;\n                    return 2;\n                }\n            },\n            {\n                key: [0x00, 0xBA],\n                fn: function (arg) {\n                    decoded_data.battery_life = decode_field(arg, 0, 6, \"unsigned\") * 0.01 + 2.5;\n                    return 1;\n                }\n            },\n            {\n                key: [0x01, 0x04],\n                fn: function (arg) {\n                    decoded_data.input1_frequency = decode_field(arg, 0, 15, \"unsigned\") * 1000;\n                    return 2;\n                }\n            },\n            {\n                key: [0x02, 0x02],\n                fn: function (arg) {\n                    decoded_data.input2_voltage = decode_field(arg, 0, 15, \"unsigned\") * 0.001;\n                    return 2;\n                }\n            },\n            {\n                key: [0x03, 0x02],\n                fn: function (arg) {\n                    decoded_data.input3_voltage = decode_field(arg, 0, 15, \"unsigned\") * 0.001;\n                    return 2;\n                }\n            },\n            {\n                key: [0x04, 0x02],\n                fn: function (arg) {\n                    decoded_data.input4_voltage = decode_field(arg, 0, 15, \"unsigned\") * 0.001;\n                    return 2;\n                }\n            },\n            {\n                key: [0x05, 0x04],\n                fn: function (arg) {\n                    decoded_data.watermark1_raw = decode_field(arg, 0, 15, \"unsigned\");\n                    // convertion done as per the communication from the Tektelic sensor team on Jan 13, 2020 - contact: Mark Oevering - details absent in the TRM\n                    if (decoded_data.watermark1_raw > 6430) {\n                        decoded_data.watermark1 = 0;\n                    } else if (decoded_data.watermark1_raw > 4330) {\n                        decoded_data.watermark1 = (9 - (decoded_data.watermark1_raw - 4600) * 0.004286) * 1000;\n                    } else if (decoded_data.watermark1_raw > 2820) {\n                        decoded_data.watermark1 = (15 - (decoded_data.watermark1_raw - 2820) * 0.003974) * 1000;\n                    } else if (decoded_data.watermark1_raw > 1110) {\n                        decoded_data.watermark1 = (35 - (decoded_data.watermark1_raw - 1110) * 0.01170) * 1000;\n                    } else if (decoded_data.watermark1_raw > 770) {\n                        decoded_data.watermark1 = (55 - (decoded_data.watermark1_raw - 770) * 0.05884) * 1000;\n                    } else if (decoded_data.watermark1_raw > 600) {\n                        decoded_data.watermark1 = (75 - (decoded_data.watermark1_raw - 600) * 0.1176) * 1000;\n                    } else if (decoded_data.watermark1_raw > 485) {\n                        decoded_data.watermark1 = (100 - (decoded_data.watermark1_raw - 485) * 0.2174) * 1000;\n                    } else if (decoded_data.watermark1_raw > 293) {\n                        decoded_data.watermark1 = (200 - (decoded_data.watermark1_raw - 293) * 0.5208) * 1000;\n                    } else {\n                        decoded_data.watermark1 = 200 * 1000;\n                    }\n                    return 2;\n                }\n            },\n            {\n                key: [0x06, 0x04],\n                fn: function (arg) {\n                    decoded_data.watermark2_raw = decode_field(arg, 0, 15, \"unsigned\");\n                    // convertion done as per the communication from the Tektelic sensor team on Jan 13, 2020 - contact: Mark Oevering - details absent in the TRM\n                    if (decoded_data.watermark2 > 6430) {\n                        decoded_data.watermark2 = 0;\n                    } else if (decoded_data.watermark2_raw > 4330) {\n                        decoded_data.watermark2 = (9 - (decoded_data.watermark2_raw - 4600) * 0.004286) * 1000;\n                    } else if (decoded_data.watermark2_raw > 2820) {\n                        decoded_data.watermark2 = (15 - (decoded_data.watermark2_raw - 2820) * 0.003974) * 1000;\n                    } else if (decoded_data.watermark2_raw > 1110) {\n                        decoded_data.watermark2 = (35 - (decoded_data.watermark2_raw - 1110) * 0.01170) * 1000;\n                    } else if (decoded_data.watermark2_raw > 770) {\n                        decoded_data.watermark2 = (55 - (decoded_data.watermark2_raw - 770) * 0.05884) * 1000;\n                    } else if (decoded_data.watermark2_raw > 600) {\n                        decoded_data.watermark2 = (75 - (decoded_data.watermark2_raw - 600) * 0.1176) * 1000;\n                    } else if (decoded_data.watermark2_raw > 485) {\n                        decoded_data.watermark2 = (100 - (decoded_data.watermark2_raw - 485) * 0.2174) * 1000;\n                    } else if (decoded_data.watermark2_raw > 293) {\n                        decoded_data.watermark2 = (200 - (decoded_data.watermark2_raw - 293) * 0.5208) * 1000;\n                    } else {\n                        decoded_data.watermark2 = 200 * 1000;\n                    }\n                    return 2;\n                }\n            },\n            {\n                key: [0x09, 0x65],\n                fn: function (arg) {\n                    decoded_data.light_intensity = decode_field(arg, 0, 15, \"unsigned\");\n                    return 2;\n                }\n            },\n            {\n                key: [0x09, 0x00],\n                fn: function (arg) {\n                    decoded_data.light_detected = decode_field(arg, 0, 7, \"unsigned\");\n                    return 1;\n                }\n            },\n            {\n                key: [0x0A, 0x71],\n                fn: function (arg) {\n                    decoded_data['acceleration.xaxis'] = decode_field(arg, 32, 47, \"signed\") * 0.001;\n                    decoded_data['acceleration.yaxis'] = decode_field(arg, 16, 31, \"signed\") * 0.001;\n                    decoded_data['acceleration.zaxis'] = decode_field(arg, 0, 15, \"signed\") * 0.001;\n                    return 6;\n                }\n            },\n            {\n                key: [0x0A, 0x02],\n                fn: function (arg) {\n                    decoded_data.impact_magnitude = decode_field(arg, 0, 15, \"unsigned\") * 0.001;\n                    return 2;\n                }\n            },\n            {\n                key: [0x0A, 0x00],\n                fn: function (arg) {\n                    decoded_data.impact_alarm = decode_field(arg, 0, 7, \"unsigned\");\n                    return 1;\n                }\n            },\n            {\n                key: [0x0B, 0x67],\n                fn: function (arg) {\n                    decoded_data.ambient_temperature = decode_field(arg, 0, 15, \"signed\") * 0.1;\n                    return 2;\n                }\n            },\n            {\n                key: [0x0B, 0x68],\n                fn: function (arg) {\n                    // Ambient RH\n                    decoded_data.relative_humidity = decode_field(arg, 0, 7, \"unsigned\") * 0.5;\n                    return 1;\n                }\n            },\n            {\n                key: [0x0C, 0x67],\n                fn: function (arg) {\n                    decoded_data.mcu_temperature = decode_field(arg, 0, 15, \"signed\") * 0.1;\n                    return 2;\n                }\n            },\n        ]\n    }\n    if (port === 100) {\n        decoder = [\n            {\n                key: [0x00],\n                fn: function (arg) {\n                    decoded_data.device_eui = decode_field(arg, 0, 63, \"hexstring\");\n                    return 8;\n                }\n            },\n            {\n                key: [0x01],\n                fn: function (arg) {\n                    decoded_data.app_eui = decode_field(arg, 0, 63, \"hexstring\");\n                    return 8;\n                }\n            },\n            {\n                key: [0x02],\n                fn: function (arg) {\n                    decoded_data.app_key = decode_field(arg, 0, 127, \"hexstring\");\n                    return 16;\n                }\n            },\n            {\n                key: [0x03],\n                fn: function (arg) {\n                    decoded_data.device_address = decode_field(arg, 0, 31, \"hexstring\");\n                    return 4;\n                }\n            },\n            {\n                key: [0x04],\n                fn: function (arg) {\n                    decoded_data.network_session_key = decode_field(arg, 0, 127, \"hexstring\");\n                    return 16;\n                }\n            },\n            {\n                key: [0x05],\n                fn: function (arg) {\n                    decoded_data.app_session_key = decode_field(arg, 0, 127, \"hexstring\");\n                    return 16;\n                }\n            },\n            {\n                key: [0x10],\n                fn: function (arg) {\n                    decoded_data.loramac_join_mode = decode_field(arg, 7, 7, \"unsigned\");\n                    return 2;\n                }\n            },\n            {\n                key: [0x11],\n                fn: function (arg) {\n                    decoded_data['loramac_opts.confirm_mode'] = decode_field(arg, 8, 8, \"unsigned\");\n                    decoded_data['loramac_opts.sync_word'] = decode_field(arg, 9, 9, \"unsigned\");\n                    decoded_data['loramac_opts.duty_cycle'] = decode_field(arg, 10, 10, \"unsigned\");\n                    decoded_data['loramac_opts.adr'] = decode_field(arg, 11, 11, \"unsigned\");\n                    return 2;\n                }\n            },\n            {\n                key: [0x12],\n                fn: function (arg) {\n                    decoded_data['loramac_dr_tx.dr_number'] = decode_field(arg, 0, 3, \"unsigned\");\n                    decoded_data['loramac_dr_tx.tx_power_number'] = decode_field(arg, 8, 11, \"unsigned\");\n                    return 2;\n                }\n            },\n            {\n                key: [0x13],\n                fn: function (arg) {\n                    decoded_data['loramac_rx2.frequency'] = decode_field(arg, 0, 31, \"unsigned\");\n                    decoded_data['loramac_rx2.dr_number'] = decode_field(arg, 32, 39, \"unsigned\");\n                    return 5;\n                }\n            },\n            {\n                key: [0x20],\n                fn: function (arg) {\n                    decoded_data.seconds_per_core_tick = decode_field(arg, 0, 31, \"unsigned\");\n                    return 4;\n                }\n            },\n            {\n                key: [0x21],\n                fn: function (arg) {\n                    decoded_data.tick_per_battery = decode_field(arg, 0, 15, \"unsigned\");\n                    return 2;\n                }\n            },\n            {\n                key: [0x22],\n                fn: function (arg) {\n                    decoded_data.tick_per_ambient_temperature = decode_field(arg, 0, 15, \"unsigned\");\n                    return 2;\n                }\n            },\n            {\n                key: [0x23],\n                fn: function (arg) {\n                    decoded_data.tick_per_relative_humidity = decode_field(arg, 0, 15, \"unsigned\");\n                    return 2;\n                }\n            },\n            {\n                key: [0x24],\n                fn: function (arg) {\n                    decoded_data.tick_per_light = decode_field(arg, 0, 15, \"unsigned\");\n                    return 2;\n                }\n            },\n            {\n                key: [0x25],\n                fn: function (arg) {\n                    decoded_data.tick_per_input1 = decode_field(arg, 0, 15, \"unsigned\");\n                    return 2;\n                }\n            },\n            {\n                key: [0x26],\n                fn: function (arg) {\n                    decoded_data.tick_per_input2 = decode_field(arg, 0, 15, \"unsigned\");\n                    return 2;\n                }\n            },\n            {\n                key: [0x29],\n                fn: function (arg) {\n                    decoded_data.tick_per_watermark1 = decode_field(arg, 0, 15, \"unsigned\");\n                    return 2;\n                }\n            },\n            {\n                key: [0x2A],\n                fn: function (arg) {\n                    decoded_data.tick_per_watermark2 = decode_field(arg, 0, 15, \"unsigned\");\n                    return 2;\n                }\n            },\n            {\n                key: [0x2C],\n                fn: function (arg) {\n                    decoded_data.tick_per_accelerometer = decode_field(arg, 0, 15, \"unsigned\");\n                    return 2;\n                }\n            },\n            {\n                key: [0x2D],\n                fn: function (arg) {\n                    decoded_data.tick_per_orientation_alarm = decode_field(arg, 0, 15, \"unsigned\");\n                    return 2;\n                }\n            },\n            {\n                key: [0x2E],\n                fn: function (arg) {\n                    decoded_data.tick_per_mcu_tempearture = decode_field(arg, 0, 15, \"unsigned\");\n                    return 2;\n                }\n            },\n            {\n                key: [0x30],\n                fn: function (arg) {\n                    decoded_data.temperature_relative_humidity_idle = decode_field(arg, 0, 31, \"unsigned\");\n                    return 4;\n                }\n            },\n            {\n                key: [0x31],\n                fn: function (arg) {\n                    decoded_data.temperature_relative_humidity_active = decode_field(arg, 0, 31, \"unsigned\");\n                    return 4;\n                }\n            },\n            {\n                key: [0x32],\n                fn: function (arg) {\n                    decoded_data['ambient_temperature_threshold.high'] = decode_field(arg, 0, 7, \"unsigned\");\n                    decoded_data['ambient_temperature_threshold.low'] = decode_field(arg, 8, 15, \"unsigned\");\n                    return 2;\n                }\n            },\n            {\n                key: [0x33],\n                fn: function (arg) {\n                    decoded_data.ambient_temperature_threshold_enabled = decode_field(arg, 0, 0, \"unsigned\");\n                    return 1;\n                }\n            },\n            {\n                key: [0x34],\n                fn: function (arg) {\n                    decoded_data['relative_humidity_threshold.high'] = decode_field(arg, 0, 7, \"unsigned\");\n                    decoded_data['relative_humidity_threshold.low'] = decode_field(arg, 8, 15, \"unsigned\");\n                    return 2;\n                }\n            },\n            {\n                key: [0x35],\n                fn: function (arg) {\n                    decoded_data.relative_humidity_threshold_enabled = decode_field(arg, 0, 0, \"unsigned\");\n                    return 1;\n                }\n            },\n            {\n                key: [0x36],\n                fn: function (arg) {\n                    decoded_data.input_sample_period_idle = decode_field(arg, 0, 31, \"unsigned\");\n                    return 4;\n                }\n            },\n            {\n                key: [0x37],\n                fn: function (arg) {\n                    decoded_data.input_sample_period_active = decode_field(arg, 0, 31, \"unsigned\");\n                    return 4;\n                }\n            },\n            {\n                key: [0x38],\n                fn: function (arg) {\n                    decoded_data['input1_threshold.high'] = decode_field(arg, 0, 15, \"unsigned\") * 1000;\n                    decoded_data['input1_threshold.low'] = decode_field(arg, 16, 31, \"unsigned\") * 1000;\n                    return 4;\n                }\n            },\n            {\n                key: [0x39],\n                fn: function (arg) {\n                    decoded_data['input2_threshold.high'] = decode_field(arg, 0, 15, \"unsigned\") * 0.001;\n                    decoded_data['input2_threshold.low'] = decode_field(arg, 16, 31, \"unsigned\") * 0.001;\n                    return 4;\n                }\n            },\n            {\n                key: [0x3C],\n                fn: function (arg) {\n                    decoded_data['moisture1_threshold.high'] = decode_field(arg, 0, 15, \"unsigned\") * 1000;\n                    decoded_data['moisture1_threshold.low'] = decode_field(arg, 16, 31, \"unsigned\") * 1000;\n                    return 4;\n                }\n            },\n            {\n                key: [0x3D],\n                fn: function (arg) {\n                    decoded_data['moisture2_threshold.high'] = decode_field(arg, 0, 15, \"unsigned\") * 1000;\n                    decoded_data['moisture2_threshold.low'] = decode_field(arg, 16, 31, \"unsigned\") * 1000;\n                    return 4;\n                }\n            },\n            {\n                key: [0x3F],\n                fn: function (arg) {\n                    decoded_data['threshold_enabled.input1'] = decode_field(arg, 0, 0, \"unsigned\");\n                    decoded_data['threshold_enabled.input2'] = decode_field(arg, 1, 1, \"unsigned\");\n                    decoded_data['threshold_enabled.input5'] = decode_field(arg, 4, 4, \"unsigned\");\n                    decoded_data['threshold_enabled.input6'] = decode_field(arg, 5, 5, \"unsigned\");\n                    return 1;\n                }\n            },\n            {\n                key: [0x40],\n                fn: function (arg) {\n                    decoded_data.mcu_temperature_sample_period_idle = decode_field(arg, 0, 31, \"unsigned\");\n                    return 4;\n                }\n            },\n            {\n                key: [0x41],\n                fn: function (arg) {\n                    decoded_data.mcu_temperature_sample_period_active = decode_field(arg, 0, 31, \"unsigned\");\n                    return 4;\n                }\n            },\n            {\n                key: [0x42],\n                fn: function (arg) {\n                    decoded_data['mcu_temperature_threshold.high'] = decode_field(arg, 0, 7, \"unsigned\");\n                    decoded_data['mcu_temperature_threshold.low'] = decode_field(arg, 8, 15, \"unsigned\");\n                    return 2;\n                }\n            },\n            {\n                key: [0x43],\n                fn: function (arg) {\n                    decoded_data.mcu_temperature_threshold_enabled = decode_field(arg, 0, 0, \"unsigned\");\n                    return 1;\n                }\n            },\n            {\n                key: [0x48],\n                fn: function (arg) {\n                    decoded_data.interrupt_enabled = decode_field(arg, 0, 0, \"unsigned\");\n                    return 1;\n                }\n            },\n            {\n                key: [0x49],\n                fn: function (arg) {\n                    decoded_data.upper_threshold = decode_field(arg, 0, 31, \"unsigned\");\n                    return 4;\n                }\n            },\n            {\n                key: [0x4A],\n                fn: function (arg) {\n                    decoded_data.lower_threshold = decode_field(arg, 0, 31, \"unsigned\");\n                    return 4;\n                }\n            },\n            {\n                key: [0x4B],\n                fn: function (arg) {\n                    decoded_data.threshold_timer = decode_field(arg, 0, 7, \"unsigned\") * 0.1;\n                    return 1;\n                }\n            },\n            {\n                key: [0x4C],\n                fn: function (arg) {\n                    decoded_data.light_sample_period_active = decode_field(arg, 0, 31, \"unsigned\");\n                    return 4;\n                }\n            },\n            {\n                key: [0x4D],\n                fn: function (arg) {\n                    decoded_data['als_tx.light_alarm_reported'] = decode_field(arg, 0, 0, \"unsigned\");\n                    decoded_data['als_tx.light_intensity_reported'] = decode_field(arg, 1, 1, \"unsigned\");\n                    return 1;\n                }\n            },\n            {\n                key: [0x50],\n                fn: function (arg) {\n                    decoded_data.orientation_alarm_threshold = decode_field(arg, 0, 0, \"unsigned\");\n                    return 2;\n                }\n            },\n            {\n                key: [0x51],\n                fn: function (arg) {\n                    decoded_data['accelerometer_tx.orientation_alarm_reported'] = decode_field(arg, 0, 0, \"unsigned\");\n                    decoded_data['accelerometer_tx.orientation_vector_reported'] = decode_field(arg, 5, 5, \"unsigned\");\n                    return 1;\n                }\n            },\n            {\n                key: [0x52],\n                fn: function (arg) {\n                    decoded_data['mode.orientation_alarm_enabled'] = decode_field(arg, 0, 0, \"unsigned\");\n                    decoded_data['mode.accelerometer_power_on'] = decode_field(arg, 7, 7, \"unsigned\");\n                    return 1;\n                }\n            },\n            {\n                key: [0x71],\n                fn: function (arg) {\n                    decoded_data['firmware_version.app_major_version'] = decode_field(arg, 0, 7, \"unsigned\");\n                    decoded_data['firmware_version.app_minor_version'] = decode_field(arg, 8, 15, \"unsigned\");\n                    decoded_data['firmware_version.app_revision'] = decode_field(arg, 16, 23, \"unsigned\");\n                    decoded_data['firmware_version.loramac_major_version'] = decode_field(arg, 24, 31, \"unsigned\");\n                    decoded_data['firmware_version.loramac_minor_version'] = decode_field(arg, 32, 39, \"unsigned\");\n                    decoded_data['firmware_version.loramac_revision'] = decode_field(arg, 40, 47, \"unsigned\");\n                    decoded_data['firmware_version.region'] = decode_field(arg, 48, 55, \"unsigned\");\n                    return 7;\n                }\n            }\n        ]\n    }\n\n    bytes = convertToUint8Array(bytes);\n    decoded_data['raw'] = JSON.stringify(byteToArray(bytes));\n    decoded_data['port'] = port;\n\n    for (var bytes_left = bytes.length; bytes_left > 0;) {\n        var found = false;\n        for (var i = 0; i < decoder.length; i++) {\n            var item = decoder[i];\n            var key = item.key;\n            var keylen = key.length;\n            var header = slice(bytes, 0, keylen);\n            // Header in the data matches to what we expect\n            if (is_equal(header, key)) {\n                var f = item.fn;\n                var consumed = f(slice(bytes, keylen, bytes.length)) + keylen;\n                bytes_left -= consumed;\n                bytes = slice(bytes, consumed, bytes.length);\n                found = true;\n                break;\n            }\n        }\n        if (found) {\n            continue;\n        }\n        // Unable to decode -- headers are not as expected, send raw payload to the application!\n        decoded_data = {};\n        decoded_data['raw'] = JSON.stringify(byteToArray(bytes));\n        decoded_data['port'] = port;\n        return decoded_data;\n    }\n\n    // Converts value to unsigned\n    function to_uint(x) {\n        return x >>> 0;\n    }\n\n    // Checks if two arrays are equal\n    function is_equal(arr1, arr2) {\n        if (arr1.length != arr2.length) {\n            return false;\n        }\n        for (var i = 0; i != arr1.length; i++) {\n            if (arr1[i] != arr2[i]) {\n                return false;\n            }\n        }\n        return true;\n    }\n\n    function byteToArray(byteArray) {\n        var arr = [];\n        for (var i = 0; i < byteArray.length; i++) {\n            arr.push(byteArray[i]);\n        }\n        return arr;\n    }\n\n    function convertToUint8Array(byteArray) {\n        var arr = [];\n        for (var i = 0; i < byteArray.length; i++) {\n            arr.push(to_uint(byteArray[i]) & 0xff);\n        }\n        return arr;\n    }\n\n    function toHexString(byteArray) {\n        var arr = [];\n        for (var i = 0; i < byteArray.length; ++i) {\n            arr.push(('0' + (byteArray[i] & 0xFF).toString(16)).slice(-2));\n        }\n        return arr.join('');\n    }\n\n    return decoded_data;\n}\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1980,"y":144,"wires":[["3999f2d038f2db46"]]},{"id":"ed962b862a3ab253","type":"function","z":"0489bdf91e1981f0","name":"base64-->bytes","func":"msg.mqttPayload = msg.payload;\nmsg.payload = Buffer.from(msg.payload.data, \"base64\")\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1792,"y":144,"wires":[["072041207853ad1e"]]},{"id":"3522e7fc1349e6aa","type":"inject","z":"0489bdf91e1981f0","name":"fake MQTT","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"applicationID\":\"1\",\"applicationName\":\"T0005987\",\"data\":\"BQQAMAYEADAJZQAMC2cA/AtoOQC6cQxnAPM=\",\"devEUI\":\"647fda0000007fa9\",\"deviceName\":\"T0005987\",\"fCnt\":3864,\"fPort\":10,\"rxInfo\":[{\"altitude\":0,\"latitude\":0,\"loRaSNR\":14,\"longitude\":0,\"mac\":\"24e124fffef128ea\",\"name\":\"Local Gateway\",\"rssi\":-58,\"time\":\"2021-09-06T13:34:11.770693Z\"}],\"time\":\"2021-09-06T13:34:11.770693Z\",\"txInfo\":{\"adr\":true,\"codeRate\":\"4/5\",\"dataRate\":{\"bandwidth\":125,\"modulation\":\"LORA\",\"spreadFactor\":7},\"frequency\":868100000}}","payloadType":"json","x":1788,"y":96,"wires":[["ed962b862a3ab253"]]},{"id":"3999f2d038f2db46","type":"debug","z":"0489bdf91e1981f0","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1986,"y":192,"wires":[]}]



PS: In order to make code more readable and importable it is important to surround your code with three backticks
```
like this
```
See this post for more details - How to share code or flow json

Thank you so much Dear Steve,
I appreciate your support and your contribution to Node-red community helper.
you are the best.
Thanks

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.