All files / common mqtt-utils.js

100% Statements 25/25
100% Branches 12/12
100% Functions 3/3
100% Lines 24/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42    7x 7x 7x   787x 2x     7x   97x 5x     7x   471x 471x 471x 527513x 527513x 527503x   10x 2x   8x 2x   6x 4x     2x     471x   7x  
"use strict";
/*! Copyright (c) 2021 Siemens AG. Licensed under the MIT License. */
Object.defineProperty(exports, "__esModule", { value: true });
exports.getUtf8BytesCount = exports.assertMqttTopicUtf8Count = exports.assertMqttTopicLength = void 0;
const MAX_MQTT_TOPIC_UTF8_COUNT = 65535;
function assertMqttTopicLength(mqttTopic) {
    if (mqttTopic.length * 4 > MAX_MQTT_TOPIC_UTF8_COUNT) {
        assertMqttTopicUtf8Count(getUtf8BytesCount(mqttTopic));
    }
}
exports.assertMqttTopicLength = assertMqttTopicLength;
function assertMqttTopicUtf8Count(utf8Count) {
    if (utf8Count > MAX_MQTT_TOPIC_UTF8_COUNT) {
        throw new Error(`MQTT topic exceeds maximum allowed UTF-8 byte length`);
    }
}
exports.assertMqttTopicUtf8Count = assertMqttTopicUtf8Count;
function getUtf8BytesCount(str) {
    let count = 0;
    const strLen = str.length;
    for (let i = 0; i < strLen; i++) {
        const code = str.charCodeAt(i);
        if (code <= 0x007F) {
            count++;
        }
        else if (code <= 0x07FF) {
            count += 2;
        }
        else if (code <= 0xD7FF) {
            count += 3;
        }
        else if (code <= 0xDFFF) {
            count += 2;
        }
        else {
            count += 3;
        }
    }
    return count;
}
exports.getUtf8BytesCount = getUtf8BytesCount;