BetterDiscordApp-v2/common/modules/utils.js

555 lines
17 KiB
JavaScript
Raw Normal View History

2018-01-30 14:38:34 +01:00
/**
* BetterDiscord Utils Module
* Copyright (c) 2015-present Jiiks/JsSucks - https://github.com/Jiiks / https://github.com/JsSucks
* All rights reserved.
* https://betterdiscord.net
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import fs from 'fs';
import _ from 'lodash';
import filetype from 'file-type';
2018-08-21 10:09:38 +02:00
import path from 'path';
2018-01-30 14:48:25 +01:00
2018-01-30 14:38:34 +01:00
export class Utils {
2018-02-02 13:55:57 +01:00
static overload(fn, cb) {
2018-01-30 14:38:34 +01:00
const orig = fn;
return function (...args) {
2018-02-02 13:55:57 +01:00
orig(...args);
cb(...args);
2018-01-30 14:38:34 +01:00
}
}
/**
* Attempts to parse a string as JSON.
* @param {String} json The string to parse
* @return {Any}
*/
2018-01-30 14:38:34 +01:00
static async tryParseJson(jsonString) {
try {
return JSON.parse(jsonString);
} catch (err) {
throw ({
2018-04-04 22:53:02 +02:00
message: 'Failed to parse json',
2018-01-30 14:38:34 +01:00
err
});
}
}
2018-02-02 13:45:06 +01:00
/**
* Returns a new object with normalised keys.
* @param {Object} object
* @return {Object}
*/
2018-02-02 13:45:06 +01:00
static toCamelCase(o) {
const camelCased = {};
_.forEach(o, (value, key) => {
if (_.isPlainObject(value) || _.isArray(value)) {
value = this.toCamelCase(value);
}
camelCased[_.camelCase(key)] = value;
});
return camelCased;
}
2018-08-13 22:38:13 +02:00
/**
* Finds a value, subobject, or array from a tree that matches a specific filter. Great for patching render functions.
* @param {object} tree React tree to look through. Can be a rendered object or an internal instance.
* @param {callable} searchFilter Filter function to check subobjects against.
*/
static findInReactTree(tree, searchFilter) {
return this.findInTree(tree, searchFilter, {walkable: ['props', 'children', 'child', 'sibling']});
}
/**
* Finds a value, subobject, or array from a tree that matches a specific filter.
* @param {object} tree Tree that should be walked
* @param {callable} searchFilter Filter to check against each object and subobject
* @param {object} options Additional options to customize the search
* @param {Array<string>|null} [options.walkable=null] Array of strings to use as keys that are allowed to be walked on. Null value indicates all keys are walkable
* @param {Array<string>} [options.ignore=[]] Array of strings to use as keys to exclude from the search, most helpful when `walkable = null`.
*/
static findInTree(tree, searchFilter, { walkable = null, ignore = [] }) {
if (typeof searchFilter === 'string') {
if (tree.hasOwnProperty(searchFilter)) return tree[searchFilter];
} else if (searchFilter(tree)) return tree;
2018-08-15 08:01:47 +02:00
if (typeof tree !== 'object' || tree == null) return undefined;
2018-08-13 22:38:13 +02:00
let tempReturn = undefined;
if (tree instanceof Array) {
2018-08-15 08:01:47 +02:00
for (const value of tree) {
2018-08-13 22:38:13 +02:00
tempReturn = this.findInTree(value, searchFilter, {walkable, ignore});
2018-08-15 08:01:47 +02:00
if (typeof tempReturn != 'undefined') return tempReturn;
2018-08-13 22:38:13 +02:00
}
}
else {
const toWalk = walkable == null ? Object.keys(tree) : walkable;
2018-08-15 08:01:47 +02:00
for (const key of toWalk) {
2018-08-13 22:38:13 +02:00
if (!tree.hasOwnProperty(key) || ignore.includes(key)) continue;
tempReturn = this.findInTree(tree[key], searchFilter, {walkable, ignore});
2018-08-15 08:01:47 +02:00
if (typeof tempReturn != 'undefined') return tempReturn;
2018-08-13 22:38:13 +02:00
}
}
return tempReturn;
}
/**
* Checks if two or more values contain the same data.
* @param {Any} ...value The value to compare
* @return {Boolean}
*/
static compare(value1, value2, ...values) {
2018-02-15 18:09:06 +01:00
// Check to see if value1 and value2 contain the same data
if (typeof value1 !== typeof value2) return false;
if (value1 === null && value2 === null) return true;
if (value1 === null || value2 === null) return false;
if (typeof value1 === 'object') {
2018-02-15 18:09:06 +01:00
// Loop through the object and check if everything's the same
if (Object.keys(value1).length !== Object.keys(value2).length) return false;
2018-08-15 08:01:47 +02:00
for (const key in value1) {
2018-02-15 18:09:06 +01:00
if (!this.compare(value1[key], value2[key])) return false;
}
} else if (value1 !== value2) return false;
2018-02-15 18:09:06 +01:00
// value1 and value2 contain the same data
// Check any more values
2018-08-15 08:01:47 +02:00
for (const value3 of values) {
if (!this.compare(value1, value3))
return false;
}
2018-02-15 18:09:06 +01:00
return true;
}
2018-03-01 19:42:53 +01:00
/**
* Clones an object and all it's properties.
* @param {Any} value The value to clone
2018-06-27 00:25:43 +02:00
* @param {Function} exclude A function to filter objects that shouldn't be cloned
* @return {Any} The cloned value
*/
static deepclone(value, exclude, cloned) {
2018-07-18 21:57:05 +02:00
if (exclude && exclude(value)) return value;
2018-06-27 00:25:43 +02:00
if (!cloned) cloned = new WeakMap();
if (typeof value === 'object' && value !== null) {
if (value instanceof Array) return value.map(i => this.deepclone(i, exclude, cloned));
if (cloned.has(value)) return cloned.get(value);
2018-03-01 19:42:53 +01:00
const clone = Object.assign({}, value);
cloned.set(value, clone);
2018-03-01 19:42:53 +01:00
2018-08-15 08:01:47 +02:00
for (const key in clone) {
clone[key] = this.deepclone(clone[key], exclude, cloned);
2018-03-01 19:42:53 +01:00
}
return clone;
}
return value;
}
/**
* Freezes an object and all it's properties.
* @param {Any} object The object to freeze
2018-06-27 00:25:43 +02:00
* @param {Function} exclude A function to filter objects that shouldn't be frozen
*/
static deepfreeze(object, exclude) {
if (exclude && exclude(object)) return;
if (typeof object === 'object' && object !== null) {
if (Object.isFrozen(object)) return object;
const properties = Object.getOwnPropertyNames(object);
2018-08-15 08:01:47 +02:00
for (const property of properties) {
this.deepfreeze(object[property], exclude);
}
Object.freeze(object);
}
return object;
}
/**
* Removes an item from an array. This differs from Array.prototype.filter as it mutates the original array instead of creating a new one.
* @param {Array} array The array to filter
* @param {Any} item The item to remove from the array
* @return {Array}
*/
static removeFromArray(array, item, filter) {
let index;
while ((index = filter ? array.findIndex(item) : array.indexOf(item)) > -1)
array.splice(index, 1);
return array;
}
/**
* Defines a property with a getter that can be changed like a normal property.
* @param {Object} object The object to define a property on
* @param {String} property The property to define
* @param {Function} getter The property's getter
* @return {Object}
*/
static defineSoftGetter(object, property, get) {
return Object.defineProperty(object, property, {
get,
set: value => Object.defineProperty(object, property, {
value,
writable: true,
configurable: true,
enumerable: true
}),
configurable: true,
enumerable: true
});
}
2018-04-01 23:44:01 +02:00
2018-04-04 22:53:02 +02:00
static wait(time = 0) {
return new Promise(resolve => setTimeout(resolve, time));
}
2018-04-01 23:44:01 +02:00
static async until(check, time = 0) {
let value, i;
do {
// Wait for the next tick
await new Promise(resolve => setTimeout(resolve, time));
value = check(i);
i++;
} while (!value);
return value;
}
2018-08-11 14:29:30 +02:00
/**
* Finds the index of array of bytes in another array
* @param {Array} haystack The array to find aob in
* @param {Array} needle The aob to find
* @return {Number} aob index, -1 if not found
*/
static aobscan(haystack, needle) {
for (let h = 0; h < haystack.length - needle.length + 1; ++h) {
let found = true;
for (let n = 0; n < needle.length; ++n) {
if (needle[n] === null ||
needle[n] === '??' ||
haystack[h + n] === needle[n]) continue;
found = false;
break;
}
if (found) return h;
}
return -1;
}
/**
* Convert buffer to base64 encoded string
* @param {any} buffer buffer to convert
* @returns {String} base64 encoded string from buffer
*/
static arrayBufferToBase64(buffer) {
let binary = '';
const bytes = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
for (let i = 0; i < bytes.byteLength; i++) {
binary += String.fromCharCode(bytes[i]);
}
return window.btoa(binary);
}
static async getImageFromBuffer(buffer) {
if (!(buffer instanceof Blob)) buffer = new Blob([buffer]);
const reader = new FileReader();
reader.readAsDataURL(buffer);
await new Promise(r => {
reader.onload = r
});
const img = new Image();
img.src = reader.result;
2018-08-15 08:01:47 +02:00
return new Promise(resolve => {
img.onload = () => {
resolve(img);
}
});
}
static async canvasToArrayBuffer(canvas, mime = 'image/png') {
const reader = new FileReader();
return new Promise(resolve => {
canvas.toBlob(blob => {
reader.addEventListener('loadend', () => {
resolve(reader.result);
});
reader.readAsArrayBuffer(blob);
}, mime);
});
}
2018-01-30 14:38:34 +01:00
}
export class FileUtils {
/**
2018-06-27 00:25:43 +02:00
* Gets information about a file.
* @param {String} path The file's path
* @return {Promise}
*/
2018-06-27 00:25:43 +02:00
static async stat(path) {
2018-01-30 14:38:34 +01:00
return new Promise((resolve, reject) => {
2018-06-27 00:25:43 +02:00
fs.stat(path, (err, stat) => {
2018-01-30 14:38:34 +01:00
if (err) return reject({
message: `No such file or directory: ${err.path}`,
2018-01-30 14:38:34 +01:00
err
});
2018-06-27 00:25:43 +02:00
resolve(stat);
2018-01-30 14:38:34 +01:00
});
});
}
2018-06-27 00:25:43 +02:00
/**
* Checks if a file exists and is a file.
* @param {String} path The file's path
* @return {Promise}
*/
static async fileExists(path) {
const stats = await this.stat(path);
2018-06-27 00:25:43 +02:00
if (!stats.isFile()) throw {
2018-06-27 00:25:43 +02:00
message: `Not a file: ${path}`,
stats
};
}
/**
* Checks if a directory exists and is a directory.
* @param {String} path The directory's path
* @return {Promise}
*/
2018-01-30 14:38:34 +01:00
static async directoryExists(path) {
const stats = await this.stat(path);
2018-01-30 14:38:34 +01:00
if (!stats.isDirectory()) throw {
2018-06-27 00:25:43 +02:00
message: `Not a directory: ${path}`,
stats
};
2018-01-30 14:38:34 +01:00
}
/**
* Creates a directory.
* @param {String} path The directory's path
* @return {Promise}
*/
2018-01-30 16:59:27 +01:00
static async createDirectory(path) {
return new Promise((resolve, reject) => {
fs.mkdir(path, err => {
if (err) reject(err);
else resolve();
2018-01-30 16:59:27 +01:00
});
});
}
/**
* Checks if a directory exists and creates it if it doesn't.
* @param {String} path The directory's path
* @return {Promise}
*/
2018-01-30 16:59:27 +01:00
static async ensureDirectory(path) {
try {
await this.directoryExists(path);
return true;
} catch (err) {
2018-06-27 00:25:43 +02:00
await this.createDirectory(path);
return true;
2018-01-30 16:59:27 +01:00
}
}
/**
* Returns the contents of a file.
* @param {String} path The file's path
* @return {Promise}
*/
2018-01-30 14:38:34 +01:00
static async readFile(path) {
2018-06-27 00:25:43 +02:00
await this.fileExists(path);
2018-01-30 14:38:34 +01:00
return new Promise((resolve, reject) => {
fs.readFile(path, 'utf-8', (err, data) => {
if (err) return reject({
message: `Could not read file: ${path}`,
2018-01-30 14:38:34 +01:00
err
});
resolve(data);
});
});
}
/**
* Returns the contents of a file.
* @param {String} path The file's path
* @param {Object} options Additional options to pass to fs.readFile
* @return {Promise}
*/
static async readFileBuffer(path, options) {
2018-06-27 00:25:43 +02:00
await this.fileExists(path);
return new Promise((resolve, reject) => {
fs.readFile(path, options || {}, (err, data) => {
if (err) reject(err);
else resolve(data);
});
});
}
/**
* Writes to a file.
* @param {String} path The file's path
* @param {String} data The file's new contents
* @return {Promise}
*/
2018-01-30 14:38:34 +01:00
static async writeFile(path, data) {
return new Promise((resolve, reject) => {
fs.writeFile(path, data, err => {
if (err) reject(err);
else resolve();
2018-01-30 14:38:34 +01:00
});
2018-03-21 16:57:10 +01:00
});
}
/**
* Writes to the end of a file.
* @param {String} path The file's path
* @param {String} data The data to append to the file
* @return {Promise}
*/
static async appendToFile(path, data) {
return new Promise((resolve, reject) => {
fs.appendFile(path, data, err => {
if (err) reject(err);
else resolve();
});
2018-01-30 14:38:34 +01:00
});
}
/**
* Returns the contents of a file parsed as JSON.
* @param {String} path The file's path
* @return {Promise}
*/
2018-01-30 14:38:34 +01:00
static async readJsonFromFile(path) {
2018-06-27 00:25:43 +02:00
const readFile = await this.readFile(path);
2018-01-30 14:38:34 +01:00
try {
return await Utils.tryParseJson(readFile);
2018-01-30 14:38:34 +01:00
} catch (err) {
throw Object.assign(err, { path });
2018-01-30 14:38:34 +01:00
}
}
/**
* Writes to a file as JSON.
* @param {String} path The file's path
* @param {Any} data The file's new contents
* @param {Boolean} pretty Whether to pretty print the JSON object
* @return {Promise}
*/
static async writeJsonToFile(path, json, pretty) {
2018-08-15 08:01:47 +02:00
return this.writeFile(path, `${JSON.stringify(json, null, pretty ? 4 : 0)}\n`);
2018-01-30 14:38:34 +01:00
}
/**
* Returns an array of items in a directory.
* @param {String} path The directory's path
* @return {Promise}
*/
2018-01-30 16:59:27 +01:00
static async listDirectory(path) {
await this.directoryExists(path);
2018-06-27 00:25:43 +02:00
return new Promise((resolve, reject) => {
fs.readdir(path, (err, files) => {
if (err) reject(err);
else resolve(files);
2018-01-30 14:38:34 +01:00
});
});
2018-01-30 14:38:34 +01:00
}
2018-01-30 16:59:27 +01:00
static async readDir(path) {
return this.listDirectory(path);
}
/**
* Returns a file or buffer's MIME type and typical file extension.
* @param {String|Buffer} buffer A buffer or the path of a file
* @return {Promise}
*/
static async getFileType(buffer) {
if (typeof buffer === 'string') buffer = await this.readFileBuffer(buffer);
return filetype(buffer);
}
/**
* Returns a file's contents as a data URI.
* @param {String} path The directory's path
* @return {Promise}
*/
static async toDataURI(buffer, type) {
if (typeof buffer === 'string') buffer = await this.readFileBuffer(buffer);
2018-08-06 03:45:59 +02:00
if (!type) type = (await this.getFileType(buffer)).mime;
return `data:${type};base64,${buffer.toString('base64')}`;
}
/**
* Deletes a file.
* @param {String} path The file's path
* @return {Promise}
*/
static async deleteFile(path) {
await this.fileExists(path);
return new Promise((resolve, reject) => {
fs.unlink(path, (err, files) => {
if (err) reject(err);
else resolve(files);
});
});
}
/**
* Deletes a directory.
* @param {String} path The directory's path
* @return {Promise}
*/
static async deleteDirectory(path) {
await this.directoryExists(path);
return new Promise((resolve, reject) => {
fs.rmdir(path, (err, files) => {
if (err) reject(err);
else resolve(files);
});
});
}
/**
* Deletes a directory and it's contents.
* @param {String} path The directory's path
* @return {Promise}
*/
static async recursiveDeleteDirectory(pathToDir) {
for (const file of await this.listDirectory(pathToDir)) {
const pathToFile = path.join(pathToDir, file);
try {
await this.recursiveDeleteDirectory(pathToFile);
} catch (err) {
await this.deleteFile(pathToFile);
}
}
await this.deleteDirectory(pathToDir);
}
2018-01-30 14:38:34 +01:00
}