2017-07-19 02:54:03 +00:00
|
|
|
// @flow
|
2017-11-18 22:47:54 +00:00
|
|
|
import * as electron from 'electron';
|
2018-06-25 17:42:50 +00:00
|
|
|
import { Readable, Writable } from 'stream';
|
2018-05-23 06:39:01 +00:00
|
|
|
import fuzzysort from 'fuzzysort';
|
2016-11-19 03:21:15 +00:00
|
|
|
import uuid from 'uuid';
|
2017-06-30 03:30:22 +00:00
|
|
|
import zlib from 'zlib';
|
2018-06-25 17:42:50 +00:00
|
|
|
import { join as pathJoin } from 'path';
|
|
|
|
import { DEBOUNCE_MILLIS } from './constants';
|
2016-09-21 00:03:26 +00:00
|
|
|
|
2017-08-04 16:54:11 +00:00
|
|
|
const ESCAPE_REGEX_MATCH = /[-[\]/{}()*+?.\\^$|]/g;
|
2017-02-16 17:38:59 +00:00
|
|
|
|
2017-07-19 02:54:03 +00:00
|
|
|
type Header = {
|
|
|
|
name: string,
|
2018-12-12 17:36:11 +00:00
|
|
|
value: string,
|
2017-07-19 02:54:03 +00:00
|
|
|
};
|
|
|
|
|
2018-03-28 20:48:58 +00:00
|
|
|
type Parameter = {
|
|
|
|
name: string,
|
2018-12-12 17:36:11 +00:00
|
|
|
value: string,
|
2018-03-28 20:48:58 +00:00
|
|
|
};
|
|
|
|
|
2018-09-08 19:01:34 +00:00
|
|
|
export function filterParameters<T: Parameter>(parameters: Array<T>, name: string): Array<T> {
|
2018-03-28 20:48:58 +00:00
|
|
|
if (!Array.isArray(parameters) || !name) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
return parameters.filter(h => (!h || !h.name ? false : h.name === name));
|
2018-03-28 20:48:58 +00:00
|
|
|
}
|
|
|
|
|
2018-09-08 19:01:34 +00:00
|
|
|
export function filterHeaders<T: Header>(headers: Array<T>, name: string): Array<T> {
|
2016-09-03 05:14:48 +00:00
|
|
|
if (!Array.isArray(headers) || !name) {
|
|
|
|
return [];
|
2016-09-03 04:32:45 +00:00
|
|
|
}
|
|
|
|
|
2016-11-10 17:33:28 +00:00
|
|
|
return headers.filter(h => {
|
|
|
|
if (!h || !h.name) {
|
|
|
|
return false;
|
|
|
|
} else {
|
2017-03-03 20:09:08 +00:00
|
|
|
return h.name.toLowerCase() === name.toLowerCase();
|
2016-11-10 17:33:28 +00:00
|
|
|
}
|
|
|
|
});
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-09-03 05:14:48 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function hasContentTypeHeader<T: Header>(headers: Array<T>): boolean {
|
2017-04-11 21:20:01 +00:00
|
|
|
return filterHeaders(headers, 'content-type').length > 0;
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function hasContentLengthHeader<T: Header>(headers: Array<T>): boolean {
|
2017-04-11 21:20:01 +00:00
|
|
|
return filterHeaders(headers, 'content-length').length > 0;
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function hasAuthHeader<T: Header>(headers: Array<T>): boolean {
|
2016-10-02 20:57:00 +00:00
|
|
|
return filterHeaders(headers, 'authorization').length > 0;
|
|
|
|
}
|
2016-09-03 04:32:45 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function hasAcceptHeader<T: Header>(headers: Array<T>): boolean {
|
2016-12-08 20:29:40 +00:00
|
|
|
return filterHeaders(headers, 'accept').length > 0;
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function hasUserAgentHeader<T: Header>(headers: Array<T>): boolean {
|
2016-12-08 20:29:40 +00:00
|
|
|
return filterHeaders(headers, 'user-agent').length > 0;
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function hasAcceptEncodingHeader<T: Header>(headers: Array<T>): boolean {
|
2017-11-12 18:35:01 +00:00
|
|
|
return filterHeaders(headers, 'accept-encoding').length > 0;
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function getSetCookieHeaders<T: Header>(headers: Array<T>): Array<T> {
|
2016-10-02 20:57:00 +00:00
|
|
|
return filterHeaders(headers, 'set-cookie');
|
|
|
|
}
|
2016-09-04 21:32:36 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function getLocationHeader<T: Header>(headers: Array<T>): T | null {
|
2017-11-13 23:10:53 +00:00
|
|
|
const matches = filterHeaders(headers, 'location');
|
|
|
|
return matches.length ? matches[0] : null;
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function getContentTypeHeader<T: Header>(headers: Array<T>): T | null {
|
2016-11-10 17:33:28 +00:00
|
|
|
const matches = filterHeaders(headers, 'content-type');
|
|
|
|
return matches.length ? matches[0] : null;
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function getHostHeader<T: Header>(headers: Array<T>): T | null {
|
2018-02-08 09:09:13 +00:00
|
|
|
const matches = filterHeaders(headers, 'host');
|
|
|
|
return matches.length ? matches[0] : null;
|
|
|
|
}
|
|
|
|
|
2018-09-08 19:01:34 +00:00
|
|
|
export function getContentDispositionHeader<T: Header>(headers: Array<T>): T | null {
|
2017-09-18 18:19:04 +00:00
|
|
|
const matches = filterHeaders(headers, 'content-disposition');
|
|
|
|
return matches.length ? matches[0] : null;
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function getContentLengthHeader<T: Header>(headers: Array<T>): T | null {
|
2016-11-22 19:42:10 +00:00
|
|
|
const matches = filterHeaders(headers, 'content-length');
|
|
|
|
return matches.length ? matches[0] : null;
|
|
|
|
}
|
|
|
|
|
2016-09-04 21:32:36 +00:00
|
|
|
/**
|
|
|
|
* Generate an ID of the format "<MODEL_NAME>_<TIMESTAMP><RANDOM>"
|
|
|
|
* @param prefix
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2018-06-25 17:42:50 +00:00
|
|
|
export function generateId(prefix: string): string {
|
2016-10-21 17:20:36 +00:00
|
|
|
const id = uuid.v4().replace(/-/g, '');
|
2016-09-04 21:32:36 +00:00
|
|
|
|
|
|
|
if (prefix) {
|
2016-10-21 17:20:36 +00:00
|
|
|
return `${prefix}_${id}`;
|
2016-09-04 21:32:36 +00:00
|
|
|
} else {
|
2016-10-21 17:20:36 +00:00
|
|
|
return id;
|
2016-09-04 21:32:36 +00:00
|
|
|
}
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-09-22 19:44:28 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function delay(milliseconds: number = DEBOUNCE_MILLIS): Promise<void> {
|
2017-03-03 20:09:08 +00:00
|
|
|
return new Promise(resolve => setTimeout(resolve, milliseconds));
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-10-26 17:49:49 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function removeVowels(str: string): string {
|
2016-10-26 17:49:49 +00:00
|
|
|
return str.replace(/[aeiouyAEIOUY]/g, '');
|
|
|
|
}
|
2016-11-09 03:18:25 +00:00
|
|
|
|
2018-09-08 19:01:34 +00:00
|
|
|
export function keyedDebounce(callback: Function, millis: number = DEBOUNCE_MILLIS): Function {
|
2018-03-29 17:59:32 +00:00
|
|
|
let timeout;
|
2016-11-10 21:03:12 +00:00
|
|
|
let results = {};
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
return function(key, ...args) {
|
2016-11-10 21:03:12 +00:00
|
|
|
results[key] = args;
|
|
|
|
|
2016-11-09 03:18:25 +00:00
|
|
|
clearTimeout(timeout);
|
|
|
|
timeout = setTimeout(() => {
|
2016-11-10 21:03:12 +00:00
|
|
|
if (!Object.keys(results).length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(results);
|
|
|
|
results = {};
|
2016-11-09 03:18:25 +00:00
|
|
|
}, millis);
|
2017-03-03 20:09:08 +00:00
|
|
|
};
|
2016-11-09 03:18:25 +00:00
|
|
|
}
|
2016-11-10 21:03:12 +00:00
|
|
|
|
2018-09-08 19:01:34 +00:00
|
|
|
export function debounce(callback: Function, millis: number = DEBOUNCE_MILLIS): Function {
|
2016-11-10 21:03:12 +00:00
|
|
|
// For regular debounce, just use a keyed debounce with a fixed key
|
|
|
|
return keyedDebounce(results => {
|
2017-03-03 20:09:08 +00:00
|
|
|
callback.apply(null, results['__key__']);
|
2016-11-10 21:03:12 +00:00
|
|
|
}, millis).bind(null, '__key__');
|
|
|
|
}
|
2016-11-22 22:26:52 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function describeByteSize(bytes: number, long: boolean = false): string {
|
2016-11-22 22:26:52 +00:00
|
|
|
bytes = Math.round(bytes * 10) / 10;
|
|
|
|
let size;
|
|
|
|
|
2016-11-30 23:11:36 +00:00
|
|
|
// NOTE: We multiply these by 2 so we don't end up with
|
|
|
|
// values like 0 GB
|
|
|
|
|
2017-06-09 21:42:19 +00:00
|
|
|
let unit = long ? 'bytes' : 'B';
|
2016-11-30 23:11:36 +00:00
|
|
|
if (bytes < 1024 * 2) {
|
2016-11-22 22:26:52 +00:00
|
|
|
size = bytes;
|
2017-06-09 21:42:19 +00:00
|
|
|
unit = long ? 'bytes' : 'B';
|
2016-11-30 23:11:36 +00:00
|
|
|
} else if (bytes < 1024 * 1024 * 2) {
|
2016-11-22 22:26:52 +00:00
|
|
|
size = bytes / 1024;
|
2017-06-09 21:42:19 +00:00
|
|
|
unit = long ? 'kilobytes' : 'KB';
|
2016-12-30 23:06:27 +00:00
|
|
|
} else if (bytes < 1024 * 1024 * 1024 * 2) {
|
2016-11-22 22:26:52 +00:00
|
|
|
size = bytes / 1024 / 1024;
|
2017-06-09 21:42:19 +00:00
|
|
|
unit = long ? 'megabytes' : 'MB';
|
2016-11-22 22:26:52 +00:00
|
|
|
} else {
|
|
|
|
size = bytes / 1024 / 1024 / 1024;
|
2017-06-09 21:42:19 +00:00
|
|
|
unit = long ? 'gigabytes' : 'GB';
|
2016-11-22 22:26:52 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
const rounded = Math.round(size * 10) / 10;
|
2017-03-03 20:09:08 +00:00
|
|
|
return `${rounded} ${unit}`;
|
2016-11-22 22:26:52 +00:00
|
|
|
}
|
2017-02-28 21:32:23 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function nullFn(): void {
|
2017-02-28 21:32:23 +00:00
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function preventDefault(e: Event): void {
|
2017-02-28 21:32:23 +00:00
|
|
|
e.preventDefault();
|
|
|
|
}
|
2017-03-28 22:45:23 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function clickLink(href: string): void {
|
2018-05-23 04:28:25 +00:00
|
|
|
electron.shell.openExternal(href);
|
2017-11-22 21:10:34 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function fnOrString(v: string | Function, ...args: Array<any>) {
|
2017-06-09 01:10:12 +00:00
|
|
|
if (typeof v === 'string') {
|
|
|
|
return v;
|
|
|
|
} else {
|
|
|
|
return v(...args);
|
|
|
|
}
|
|
|
|
}
|
2017-06-30 03:30:22 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function compressObject(obj: any): string {
|
2017-11-23 21:57:08 +00:00
|
|
|
const compressed = zlib.gzipSync(JSON.stringify(obj));
|
2017-06-30 03:30:22 +00:00
|
|
|
return compressed.toString('base64');
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function decompressObject(input: string): any {
|
2017-11-23 21:57:08 +00:00
|
|
|
const jsonBuffer = zlib.gunzipSync(Buffer.from(input, 'base64'));
|
2017-07-19 02:54:03 +00:00
|
|
|
return JSON.parse(jsonBuffer.toString('utf8'));
|
2017-06-30 03:30:22 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function resolveHomePath(p: string): string {
|
2017-07-20 01:55:40 +00:00
|
|
|
if (p.indexOf('~/') === 0) {
|
|
|
|
return pathJoin(process.env.HOME || '/', p.slice(1));
|
|
|
|
} else {
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
2017-08-03 21:44:55 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function jsonParseOr(str: string, fallback: any): any {
|
2017-08-03 21:44:55 +00:00
|
|
|
try {
|
|
|
|
return JSON.parse(str);
|
|
|
|
} catch (err) {
|
|
|
|
return fallback;
|
|
|
|
}
|
|
|
|
}
|
2017-08-04 16:54:11 +00:00
|
|
|
|
2018-09-08 19:01:34 +00:00
|
|
|
export function escapeHTML(unsafeText: string): string {
|
|
|
|
let div = document.createElement('div');
|
|
|
|
div.innerText = unsafeText;
|
|
|
|
return div.innerHTML;
|
|
|
|
}
|
|
|
|
|
2017-08-04 16:54:11 +00:00
|
|
|
/**
|
|
|
|
* Escape a dynamic string for use inside of a regular expression
|
|
|
|
* @param str - string to escape
|
|
|
|
* @returns {string} escaped string
|
|
|
|
*/
|
2018-06-25 17:42:50 +00:00
|
|
|
export function escapeRegex(str: string): string {
|
2017-08-04 16:54:11 +00:00
|
|
|
return str.replace(ESCAPE_REGEX_MATCH, '\\$&');
|
|
|
|
}
|
2017-08-21 18:11:52 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function fuzzyMatch(
|
|
|
|
searchString: string,
|
2018-06-27 05:13:48 +00:00
|
|
|
text: string,
|
2018-12-12 17:36:11 +00:00
|
|
|
options: { splitSpace?: boolean, loose?: boolean } = {},
|
2018-06-27 05:13:48 +00:00
|
|
|
): null | { score: number, indexes: Array<number> } {
|
|
|
|
return fuzzyMatchAll(searchString, [text], options);
|
|
|
|
}
|
2017-10-13 13:01:27 +00:00
|
|
|
|
2018-06-27 05:13:48 +00:00
|
|
|
export function fuzzyMatchAll(
|
|
|
|
searchString: string,
|
|
|
|
allText: Array<string>,
|
2018-12-12 17:36:11 +00:00
|
|
|
options: { splitSpace?: boolean, loose?: boolean } = {},
|
2018-06-27 05:13:48 +00:00
|
|
|
): null | { score: number, indexes: Array<number> } {
|
|
|
|
if (!searchString || !searchString.trim()) {
|
|
|
|
return null;
|
2018-05-23 06:39:01 +00:00
|
|
|
}
|
2017-10-13 13:01:27 +00:00
|
|
|
|
2018-06-27 05:13:48 +00:00
|
|
|
const words = searchString.split(' ').filter(w => w.trim());
|
|
|
|
const terms = options.splitSpace ? [...words, searchString] : [searchString];
|
|
|
|
|
|
|
|
let maxScore = null;
|
|
|
|
let indexes = [];
|
|
|
|
let termsMatched = 0;
|
|
|
|
for (const term of terms) {
|
|
|
|
let matchedTerm = false;
|
|
|
|
for (const text of allText.filter(t => !t || t.trim())) {
|
|
|
|
const result = fuzzysort.single(term, text);
|
|
|
|
if (!result) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-05-23 06:39:01 +00:00
|
|
|
|
2018-06-27 05:13:48 +00:00
|
|
|
// Don't match garbage
|
|
|
|
if (result.score < -8000) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-05-23 06:39:01 +00:00
|
|
|
|
2018-06-27 05:13:48 +00:00
|
|
|
if (maxScore === null || result.score > maxScore) {
|
|
|
|
maxScore = result.score;
|
|
|
|
}
|
2018-05-23 06:39:01 +00:00
|
|
|
|
2018-06-27 05:13:48 +00:00
|
|
|
indexes = [...indexes, ...result.indexes];
|
|
|
|
matchedTerm = true;
|
|
|
|
}
|
2018-05-23 06:39:01 +00:00
|
|
|
|
2018-06-27 05:13:48 +00:00
|
|
|
if (matchedTerm) {
|
|
|
|
termsMatched++;
|
|
|
|
}
|
2017-10-13 13:01:27 +00:00
|
|
|
}
|
|
|
|
|
2018-06-27 05:13:48 +00:00
|
|
|
// Make sure we match all provided terms except the last (full) one
|
|
|
|
if (!options.loose && termsMatched < terms.length - 1) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-03-28 20:57:05 +00:00
|
|
|
|
2018-06-27 05:13:48 +00:00
|
|
|
if (maxScore === null) {
|
|
|
|
return null;
|
2018-05-23 06:39:01 +00:00
|
|
|
}
|
|
|
|
|
2018-06-27 05:13:48 +00:00
|
|
|
return { score: maxScore, indexes, target: allText.join(' ') };
|
2017-08-21 18:11:52 +00:00
|
|
|
}
|
2017-11-18 22:47:54 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function getViewportSize(): string | null {
|
|
|
|
const { BrowserWindow } = electron.remote || electron;
|
2018-09-08 19:01:34 +00:00
|
|
|
const w = BrowserWindow.getFocusedWindow() || BrowserWindow.getAllWindows()[0];
|
2017-11-18 22:47:54 +00:00
|
|
|
|
|
|
|
if (w) {
|
2018-06-25 17:42:50 +00:00
|
|
|
const { width, height } = w.getContentBounds();
|
2017-11-18 22:47:54 +00:00
|
|
|
return `${width}x${height}`;
|
|
|
|
} else {
|
|
|
|
// No windows open
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function getScreenResolution(): string {
|
|
|
|
const { screen } = electron.remote || electron;
|
|
|
|
const { width, height } = screen.getPrimaryDisplay().workAreaSize;
|
2017-11-18 22:47:54 +00:00
|
|
|
return `${width}x${height}`;
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function getUserLanguage(): string {
|
|
|
|
const { app } = electron.remote || electron;
|
2017-11-18 22:47:54 +00:00
|
|
|
return app.getLocale();
|
|
|
|
}
|
2017-11-21 17:49:17 +00:00
|
|
|
|
2018-09-08 19:01:34 +00:00
|
|
|
export async function waitForStreamToFinish(s: Readable | Writable): Promise<void> {
|
2017-11-21 17:49:17 +00:00
|
|
|
return new Promise(resolve => {
|
2018-03-29 17:59:32 +00:00
|
|
|
if ((s: any)._readableState && (s: any)._readableState.finished) {
|
2017-11-21 17:49:17 +00:00
|
|
|
return resolve();
|
|
|
|
}
|
|
|
|
|
2018-03-29 17:59:32 +00:00
|
|
|
if ((s: any)._writableState && (s: any)._writableState.finished) {
|
2017-11-21 17:49:17 +00:00
|
|
|
return resolve();
|
|
|
|
}
|
|
|
|
|
2017-11-23 21:57:08 +00:00
|
|
|
s.on('close', () => {
|
2017-11-21 17:49:17 +00:00
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
|
|
|
|
s.on('error', () => {
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2018-11-06 02:39:50 +00:00
|
|
|
|
|
|
|
export function getDataDirectory(): string {
|
|
|
|
const { app } = electron.remote || electron;
|
|
|
|
return process.env.INSOMNIA_DATA_PATH || app.getPath('userData');
|
|
|
|
}
|