Move some functions to Util, Bug Fixes, new stuff

This commit is contained in:
Dragon Fire
2020-03-13 22:59:22 -04:00
parent bf7579c4ac
commit 0edac3e86d
14 changed files with 99 additions and 92 deletions
+29
View File
@@ -75,6 +75,35 @@ module.exports = class Util {
return crypto.createHash(algorithm).update(text).digest('hex');
}
static streamToArray(stream) {
if (!stream.readable) return Promise.resolve([]);
return new Promise((resolve, reject) => {
const array = [];
function onData(data) {
array.push(data);
}
function onEnd(error) {
if (error) reject(error);
else resolve(array);
cleanup();
}
function onClose() {
resolve(array);
cleanup();
}
function cleanup() {
stream.removeListener('data', onData);
stream.removeListener('end', onEnd);
stream.removeListener('error', onEnd);
stream.removeListener('close', onClose);
}
stream.on('data', onData);
stream.on('end', onEnd);
stream.on('error', onEnd);
stream.on('close', onClose);
});
}
static today(timeZone) {
const now = new Date();
if (timeZone) now.setUTCHours(timeZone);