@sap/ams-dev
Version:
NodesJS AMS development environment
57 lines (52 loc) • 1.39 kB
JavaScript
const fs = require('node:fs');
function debouncePromise(fn, ms = 0) {
let timeoutId;
const pending = [];
return (...args) =>
new Promise((res, rej) => {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
const currentPending = [...pending];
pending.length = 0;
Promise.resolve(fn.call(null, ...args)).then(
data => {
currentPending.forEach(({ resolve }) => resolve(data));
},
error => {
currentPending.forEach(({ reject }) => reject(error));
}
);
}, ms);
pending.push({ resolve: res, reject: rej });
});
}
/**
* Creates the given folders if they do not exist.
* @param {Array<string>} folders array of folder paths
*/
function prepareFolders(folders) {
return Promise.all(
folders.map(f => {
if (!fs.existsSync(f)) {
return fs.mkdirSync(f, { recursive: true });
}
})
);
}
/**
* Removes CRLF characters from a value so that untrusted (e.g. server-provided)
* text cannot be used to forge additional log entries (log injection).
* @param {*} value the value to sanitize; non-string values are returned unchanged
* @returns {*} the sanitized value with carriage returns and line feeds stripped
*/
function sanitizeForLog(value) {
if (typeof value !== 'string') {
return value;
}
return value.replace(/[\r\n]+/g, ' ');
}
module.exports = {
debouncePromise,
prepareFolders,
sanitizeForLog
}