CartoDB-SQL-API/app/utils/logs.js

46 lines
1.2 KiB
JavaScript
Raw Normal View History

2019-02-27 17:40:39 +08:00
'use strict';
module.exports = {
/**
* Remove problematic nested characters
* from object for logs RegEx
*
* @param {Object} object
* @param {Number} max_string_length
*/
stringifyForLogs(object, max_string_length = 1024) {
return JSON.stringify(cloneAndFilter(object, max_string_length));
2019-02-27 17:40:39 +08:00
}
2019-02-27 18:59:04 +08:00
};
2019-02-27 17:40:39 +08:00
function cloneAndFilter(object, max_string_length) {
if (!object || !(object instanceof Object)) {
return null;
}
const newObject = {};
2019-02-27 17:40:39 +08:00
Object.keys(object).map(key => {
if (typeof object[key] === 'string') {
2019-02-27 18:59:04 +08:00
newObject[key] = filterString(object[key], max_string_length);
2019-02-27 17:40:39 +08:00
} else if (typeof object[key] === 'object') {
newObject[key] = cloneAndFilter(object[key], max_string_length);
2019-02-27 17:40:39 +08:00
} else if (object[key] instanceof Array) {
newObject[key] = [];
2019-02-27 17:40:39 +08:00
for (let element of object[key]) {
newObject[key].push(cloneAndFilter(element, max_string_length));
2019-02-27 17:40:39 +08:00
}
} else {
newObject[key] = object[key];
2019-02-27 17:40:39 +08:00
}
});
return newObject;
2019-02-27 17:40:39 +08:00
}
function filterString(s, max_string_length) {
return s
.substring(0, max_string_length)
.replace(/[^a-zA-Z0-9]/g, ' ');
}