Shinobi/libs/common.js

103 lines
3.0 KiB
JavaScript
Raw Normal View History

2020-08-27 05:37:44 +00:00
const async = require("async");
2021-12-19 20:32:44 +00:00
const fetch = require("node-fetch");
const mergeDeep = function(...objects) {
const isObject = obj => obj && typeof obj === 'object';
return objects.reduce((prev, obj) => {
Object.keys(obj).forEach(key => {
const pVal = prev[key];
const oVal = obj[key];
if (Array.isArray(pVal) && Array.isArray(oVal)) {
prev[key] = pVal.concat(...oVal);
}
else if (isObject(pVal) && isObject(oVal)) {
prev[key] = mergeDeep(pVal, oVal);
}
else {
prev[key] = oVal;
}
});
return prev;
}, {});
}
2021-12-19 20:32:44 +00:00
const getBuffer = async (url) => {
try {
const response = await fetch(url);
const arrayBuffer = await response.arrayBuffer();
return Buffer.from(arrayBuffer);
2021-12-19 20:32:44 +00:00
} catch (error) {
return { error };
}
};
function addCredentialsToUrl(options){
const streamUrl = options.url
const username = options.username
const password = options.password
const urlParts = streamUrl.split('://')
return [urlParts[0],'://',`${username}:${password}@`,urlParts[1]].join('')
}
module.exports = {
addCredentialsToUrl,
2021-12-19 20:32:44 +00:00
getBuffer: getBuffer,
mergeDeep: mergeDeep,
validateIntValue: (value) => {
const newValue = !isNaN(parseInt(value)) ? parseInt(value) : null
return newValue
},
arrayContains: (query,theArray) => {
var foundQuery = false
theArray.forEach((value) => {
if(value.indexOf(query) > -1)foundQuery = true
})
return foundQuery
},
createQueue: (timeoutInSeconds, queueItemsRunningInParallel) => {
return async.queue(function(action, callback) {
setTimeout(function(){
action(callback)
},timeoutInSeconds * 1000 || 1000)
},queueItemsRunningInParallel || 3)
2023-01-21 00:49:37 +00:00
},
createQueueAwaited: (timeoutInSeconds, queueItemsRunningInParallel) => {
return async.queue(function(action, callback) {
setTimeout(function(){
action().then(callback)
},timeoutInSeconds * 1000 || 1000)
},queueItemsRunningInParallel || 3)
},
copyObject: (obj) => {
return Object.assign({},obj)
},
stringContains: (find,string,toLowerCase) => {
var newString = string + ''
if(toLowerCase)newString = newString.toLowerCase()
return newString.indexOf(find) > -1
2020-11-18 19:26:38 +00:00
},
stringToSqlTime: (value) => {
newValue = new Date(value.replace('T',' '))
return newValue
},
queryStringToObject: (string) => {
const newObject = {}
string.split('&').forEach((piece) => {
const parts = piece.split('=')
const key = parts[0]
const value = parts[1]
newObject[key] = value
})
return newObject
},
createQueryStringFromObject: (theObject) => {
const string = []
const keys = Object.keys(theObject)
keys.forEach((key) => {
const value = theObject[key]
if(value)string.push(`${key}=${value}`)
})
return string.join('&')
}
}