onvif-nvt-ts
Version:
Wrapper for ONVIF spec to control NVT (Network Video Transitter) devices. Forked and added TypeScript support from onvif-nvt.
158 lines • 5.19 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.isInvalidValue = exports.isXml = exports.execCallback = exports.isValidCallback = exports.getTypeOfValue = exports.createUuidV4 = exports.URLWithEnforcedPort = void 0;
const crypto_1 = __importDefault(require("crypto"));
const xml2js_1 = __importDefault(require("xml2js"));
function URLWithEnforcedPort(xaddr, port) {
const badURL = new URL(xaddr);
if (badURL.origin.endsWith(`:${port}`))
return badURL;
const idx = badURL.origin.indexOf(':', 6);
const originWithoutPort = badURL.origin.slice(0, idx < 0 ? badURL.origin.length : idx);
return new URL(xaddr.replace(badURL.origin, `${originWithoutPort}:${port}`));
}
exports.URLWithEnforcedPort = URLWithEnforcedPort;
function createUuidV4() {
const clist = crypto_1.default.randomBytes(16).toString('hex').toLowerCase().split('');
clist[12] = '4';
clist[16] = ((parseInt(clist[16], 16) & 3) | 8).toString(16);
const m = clist.join('').match(/^(.{8})(.{4})(.{4})(.{4})(.{12})/);
const uuid = [m[1], m[2], m[3], m[4], m[5]].join('-');
return uuid;
}
exports.createUuidV4 = createUuidV4;
function getTypeOfValue(value) {
if (value === undefined) {
return 'undefined';
}
else if (value === null) {
return 'null';
}
else if (Array.isArray(value)) {
return 'array';
}
const t = typeof value;
if (t === 'boolean') {
return 'boolean';
}
else if (t === 'string') {
return 'string';
}
else if (t === 'number') {
if (value % 1 === 0) {
return 'integer';
}
else {
return 'float';
}
}
else if (t === 'object') {
if (Object.prototype.toString.call(value) === '[object Object]') {
return 'object';
}
else {
return 'unknown';
}
}
else if (t === 'function') {
return 'function';
}
else {
return 'unknown';
}
}
exports.getTypeOfValue = getTypeOfValue;
function isValidCallback(callback) {
return !!(callback && typeof callback === 'function');
}
exports.isValidCallback = isValidCallback;
function execCallback(callback, arg1, arg2) {
if (isValidCallback(callback)) {
callback(arg1, arg2);
}
}
exports.execCallback = execCallback;
function isXml(xml) {
if (!xml) {
return false;
}
const opts = {
explicitRoot: false,
explicitArray: false,
// 'ignoreAttrs' : true,
ignoreAttrs: false,
tagNameProcessors: [
function (name) {
// strip namespaces
/* eslint-disable no-useless-escape */
const m = name.match(/^([^\:]+)\:([^\:]+)$/);
/* eslint-enable no-useless-escape */
return m ? m[2] : name;
}
]
};
let retVal = false;
xml2js_1.default.parseString(xml, opts, (error, results) => {
if (error) {
console.log(`isXml: ${xml} is not xml`, error);
retVal = false;
}
else {
retVal = true;
}
});
return retVal;
}
exports.isXml = isXml;
function isInvalidValue(value, type, allowEmpty) {
const vt = getTypeOfValue(value);
// special handling for type 'xml' as typeof will be 'string'
if (type === 'xml') {
if (!isXml(value)) {
return ' The type of the value must be a"' + type + '".';
}
}
if (type === 'float') {
/* eslint-disable no-useless-escape */
if (!vt.match(/^(float|integer)$/)) {
/* eslint-enable no-useless-escape */
return ' The type of the value must be a "' + type + '".';
}
}
else {
if (vt !== type && type !== 'xml') {
return ' The type of the value must be a "' + type + '".';
}
}
if (!allowEmpty) {
if (vt === 'array' && value.length === 0) {
return ' The value must not be an empty array.';
}
else if (vt === 'string' && value === '') {
return ' The value must not be an empty string.';
}
}
if (typeof value === 'string') {
/* eslint-disable no-useless-escape */
if (value.match(/[^\x20-\x7e]/)) {
/* eslint-enable no-useless-escape */
return ' The value must consist of ascii characters.';
}
else if (type !== 'xml' && value.match(/[\<\>]/)) {
/* eslint-disable no-useless-escape */
/* eslint-enable no-useless-escape */
return ' Invalid characters were found in the value ("<", ">")';
}
else if (type === 'xml' && !value.match(/[\<\>]/)) {
/* eslint-disable no-useless-escape */
/* eslint-enable no-useless-escape */
return ' Valid characters were found in the value for xml ("<", ">")';
}
}
return '';
}
exports.isInvalidValue = isInvalidValue;
//# sourceMappingURL=util.js.map