all files / lib/ Utils.js

41.03% Statements 16/39
37.93% Branches 11/29
0% Functions 0/1
42.86% Lines 15/35
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117                                                                                                                                                                                                           
'use strict';
 
//region 1. Platform Libraries
const ipaddr = require('ipaddr.js');
//endregion
 
const isValidStreamIndex = index => Number.isInteger(Number(index)) && Number(index) >= 0;
 
const isWithinValidPublicIpRange = publicIp => {
        const range = ipaddr.parse(publicIp).range();
        return !(range === 'broadcast' || range === 'linkLocal' || range === 'loopback' || range === 'multicast' || range === 'private' || range === 'reserved' || range === 'unspecified');
};
 
const isValidPublicIP = ip => {
        if (!ip) {
                return new Error('No Public IP');
        }
        if (!ipaddr.isValid(ip)) {
                return new Error(`Malformed Public IP ${ ip }`);
        }
        if (!isWithinValidPublicIpRange(ip)) {
                return new Error(`Public IP in Invalid Range ${ ip }`);
        }
        return true;
};
 
const stripUndefined = obj => {
        const result = obj;
        Object.keys(result).forEach(key => {
                if (result[key] && typeof result[key] === 'object') {
                        stripUndefined(result[key]);
                } else if (result[key] === undefined) {
                        delete result[key];
                }
        });
        return result;
};
 
const valueOf = (obj, ...args) => {
        Iif (!obj) return null;
        const context = { obj };
        for (let i = 0; i < args.length; i++) {
                Iif (!{}.hasOwnProperty.call(context.obj, args[i])) return undefined;
                context.obj = context.obj[args[i]];
        }
        return context.obj;
};
 
module.exports = {
        GetNested(obj) {
                // TODO: Deprecated. Remove when possible.
                if (!obj) return null;
                for (var i = 1; i < arguments.length; i++) {
                        if (!obj.hasOwnProperty(arguments[i])) {
                                return undefined;
                        }
                        obj = obj[arguments[i]];
                }
                return obj;
        },
        isValidStreamIndex,
        isValidPublicIP,
        isWithinValidPublicIpRange,
        stripUndefined,
        valueOf
};
 
/**
 * @typedef  {object} Router
 * @property {*}      use
 * @property {*}      post
 * @property {*}      get
 * @property {*}      put
 * @property {*}      delete
 */
 
/**
 * @typedef  {object} RateControl
 * @property {number} bitrate_limit
 * @property {number} frame_rate_limit
 */
 
/**
 * @typedef  {object} H264
 * @property {string} h264_profile
 * @property {number} gov_length
 */
 
/**
 * @typedef  {object} VideoInfo
 * @property {number} index
 * @property {*}      start_time
 * @property {*}      end_time
 * @property {string} bucket_name
 * @property {string} object_name
 */
 
/**
 * @typedef  {object}      Stream
 * @property {string}      rtsp_url
 * @property {number}      trigger_idle_duration
 * @property {number}      trigger_prealarm_duration
 * @property {number}      trigger_video_duration
 * @property {RateControl} rate_control
 * @property {H264}        h264
 * @property {VideoInfo}   video_info
 * @property {string[]}    triggered_streams
 */
 
/**
 * @typedef  {object} Camera
 * @property {string} username
 * @property {string} password
 * @property {string} public_address
 * @property {number} web_port
 */
//# sourceMappingURL=Utils.js.map