insomnia/packages/insomnia-app/app/common/misc.js

342 lines
8.8 KiB
JavaScript
Raw Normal View History

2017-07-19 02:54:03 +00:00
// @flow
import * as electron from 'electron';
2018-06-25 17:42:50 +00:00
import { Readable, Writable } from 'stream';
import fuzzysort from 'fuzzysort';
2016-11-19 03:21:15 +00:00
import uuid from 'uuid';
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
const ESCAPE_REGEX_MATCH = /[-[\]/{}()*+?.\\^$|]/g;
2017-07-19 02:54:03 +00:00
type Header = {
name: string,
value: string,
2017-07-19 02:54:03 +00:00
};
type Parameter = {
name: string,
value: string,
};
export function filterParameters<T: Parameter>(parameters: Array<T>, name: string): Array<T> {
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));
}
export function filterHeaders<T: Header>(headers: Array<T>, name: string): Array<T> {
if (!Array.isArray(headers) || !name) {
return [];
}
2016-11-10 17:33:28 +00:00
return headers.filter(h => {
if (!h || !h.name) {
return false;
} else {
return h.name.toLowerCase() === name.toLowerCase();
2016-11-10 17:33:28 +00:00
}
});
}
2018-06-25 17:42:50 +00:00
export function hasContentTypeHeader<T: Header>(headers: Array<T>): boolean {
return filterHeaders(headers, 'content-type').length > 0;
}
2018-06-25 17:42:50 +00:00
export function hasContentLengthHeader<T: Header>(headers: Array<T>): boolean {
return filterHeaders(headers, 'content-length').length > 0;
}
2018-06-25 17:42:50 +00:00
export function hasAuthHeader<T: Header>(headers: Array<T>): boolean {
return filterHeaders(headers, 'authorization').length > 0;
}
2018-06-25 17:42:50 +00:00
export function hasAcceptHeader<T: Header>(headers: Array<T>): boolean {
return filterHeaders(headers, 'accept').length > 0;
}
2018-06-25 17:42:50 +00:00
export function hasUserAgentHeader<T: Header>(headers: Array<T>): boolean {
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> {
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 {
const matches = filterHeaders(headers, 'host');
return matches.length ? matches[0] : null;
}
export function getContentDispositionHeader<T: Header>(headers: Array<T>): T | null {
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 {
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 {
Sync Proof of Concept (#33) * Maybe working POC * Change to use remote url * Other URL too * Some logic * Got the push part working * Made some updates * Fix * Update * Add status code check * Stuff * Implemented new sync api * A bit more robust * Debounce changes * Change timeout * Some fixes * Remove .less * Better error handling * Fix base url * Support for created vs updated docs * Try silent * Silence removal too * Small fix after merge * Fix test * Stuff * Implement key generation algorithm * Tidy * stuff * A bunch of stuff for the new API * Integrated the session stuff * Stuff * Just started on encryption * Lots of updates to encryption * Finished createResourceGroup function * Full encryption/decryption working (I think) * Encrypt localstorage with sessionID * Some more * Some extra checks * Now uses separate DB. Still needs to be simplified a LOT * Fix deletion bug * Fixed unicode bug with encryption * Simplified and working * A bunch of polish * Some stuff * Removed some workspace meta properties * Migrated a few more meta properties * Small changes * Fix body scrolling and url cursor jumping * Removed duplication of webpack port * Remove workspaces reduces * Some small fixes * Added sync modal and opt-in setting * Good start to sync flow * Refactored modal footer css * Update sync status * Sync logger * A bit better logging * Fixed a bunch of sync-related bugs * Fixed signup form button * Gravatar component * Split sync modal into tabs * Tidying * Some more error handling * start sending 'user agent * Login/signup error handling * Use real UUIDs * Fixed tests * Remove unused function * Some extra checks * Moved cloud sync setting to about page * Some small changes * Some things
2016-10-21 17:20:36 +00:00
const id = uuid.v4().replace(/-/g, '');
2016-09-04 21:32:36 +00:00
if (prefix) {
Sync Proof of Concept (#33) * Maybe working POC * Change to use remote url * Other URL too * Some logic * Got the push part working * Made some updates * Fix * Update * Add status code check * Stuff * Implemented new sync api * A bit more robust * Debounce changes * Change timeout * Some fixes * Remove .less * Better error handling * Fix base url * Support for created vs updated docs * Try silent * Silence removal too * Small fix after merge * Fix test * Stuff * Implement key generation algorithm * Tidy * stuff * A bunch of stuff for the new API * Integrated the session stuff * Stuff * Just started on encryption * Lots of updates to encryption * Finished createResourceGroup function * Full encryption/decryption working (I think) * Encrypt localstorage with sessionID * Some more * Some extra checks * Now uses separate DB. Still needs to be simplified a LOT * Fix deletion bug * Fixed unicode bug with encryption * Simplified and working * A bunch of polish * Some stuff * Removed some workspace meta properties * Migrated a few more meta properties * Small changes * Fix body scrolling and url cursor jumping * Removed duplication of webpack port * Remove workspaces reduces * Some small fixes * Added sync modal and opt-in setting * Good start to sync flow * Refactored modal footer css * Update sync status * Sync logger * A bit better logging * Fixed a bunch of sync-related bugs * Fixed signup form button * Gravatar component * Split sync modal into tabs * Tidying * Some more error handling * start sending 'user agent * Login/signup error handling * Use real UUIDs * Fixed tests * Remove unused function * Some extra checks * Moved cloud sync setting to about page * Some small changes * Some things
2016-10-21 17:20:36 +00:00
return `${prefix}_${id}`;
2016-09-04 21:32:36 +00:00
} else {
Sync Proof of Concept (#33) * Maybe working POC * Change to use remote url * Other URL too * Some logic * Got the push part working * Made some updates * Fix * Update * Add status code check * Stuff * Implemented new sync api * A bit more robust * Debounce changes * Change timeout * Some fixes * Remove .less * Better error handling * Fix base url * Support for created vs updated docs * Try silent * Silence removal too * Small fix after merge * Fix test * Stuff * Implement key generation algorithm * Tidy * stuff * A bunch of stuff for the new API * Integrated the session stuff * Stuff * Just started on encryption * Lots of updates to encryption * Finished createResourceGroup function * Full encryption/decryption working (I think) * Encrypt localstorage with sessionID * Some more * Some extra checks * Now uses separate DB. Still needs to be simplified a LOT * Fix deletion bug * Fixed unicode bug with encryption * Simplified and working * A bunch of polish * Some stuff * Removed some workspace meta properties * Migrated a few more meta properties * Small changes * Fix body scrolling and url cursor jumping * Removed duplication of webpack port * Remove workspaces reduces * Some small fixes * Added sync modal and opt-in setting * Good start to sync flow * Refactored modal footer css * Update sync status * Sync logger * A bit better logging * Fixed a bunch of sync-related bugs * Fixed signup form button * Gravatar component * Split sync modal into tabs * Tidying * Some more error handling * start sending 'user agent * Login/signup error handling * Use real UUIDs * Fixed tests * Remove unused function * Some extra checks * Moved cloud sync setting to about page * Some small changes * Some things
2016-10-21 17:20:36 +00:00
return id;
2016-09-04 21:32:36 +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> {
return new Promise(resolve => setTimeout(resolve, milliseconds));
}
2018-06-25 17:42:50 +00:00
export function removeVowels(str: string): string {
return str.replace(/[aeiouyAEIOUY]/g, '');
}
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;
clearTimeout(timeout);
timeout = setTimeout(() => {
2016-11-10 21:03:12 +00:00
if (!Object.keys(results).length) {
return;
}
callback(results);
results = {};
}, millis);
};
}
2016-11-10 21:03:12 +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 => {
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;
// NOTE: We multiply these by 2 so we don't end up with
// values like 0 GB
let unit = long ? 'bytes' : 'B';
if (bytes < 1024 * 2) {
2016-11-22 22:26:52 +00:00
size = bytes;
unit = long ? 'bytes' : 'B';
} else if (bytes < 1024 * 1024 * 2) {
2016-11-22 22:26:52 +00:00
size = bytes / 1024;
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;
unit = long ? 'megabytes' : 'MB';
2016-11-22 22:26:52 +00:00
} else {
size = bytes / 1024 / 1024 / 1024;
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;
return `${rounded} ${unit}`;
2016-11-22 22:26:52 +00:00
}
2018-06-25 17:42:50 +00:00
export function nullFn(): void {
// Do nothing
}
2018-06-25 17:42:50 +00:00
export function preventDefault(e: Event): void {
e.preventDefault();
}
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);
}
2018-06-25 17:42:50 +00:00
export function fnOrString(v: string | Function, ...args: Array<any>) {
if (typeof v === 'string') {
return v;
} else {
return v(...args);
}
}
2018-06-25 17:42:50 +00:00
export function compressObject(obj: any): string {
const compressed = zlib.gzipSync(JSON.stringify(obj));
return compressed.toString('base64');
}
2018-06-25 17:42:50 +00:00
export function decompressObject(input: string): any {
const jsonBuffer = zlib.gunzipSync(Buffer.from(input, 'base64'));
2017-07-19 02:54:03 +00:00
return JSON.parse(jsonBuffer.toString('utf8'));
}
2018-06-25 17:42:50 +00:00
export function resolveHomePath(p: string): string {
if (p.indexOf('~/') === 0) {
return pathJoin(process.env.HOME || '/', p.slice(1));
} else {
return p;
}
}
2018-06-25 17:42:50 +00:00
export function jsonParseOr(str: string, fallback: any): any {
try {
return JSON.parse(str);
} catch (err) {
return fallback;
}
}
export function escapeHTML(unsafeText: string): string {
let div = document.createElement('div');
div.innerText = unsafeText;
return div.innerHTML;
}
/**
* 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 {
return str.replace(ESCAPE_REGEX_MATCH, '\\$&');
}
2018-06-25 17:42:50 +00:00
export function fuzzyMatch(
searchString: string,
text: string,
options: { splitSpace?: boolean, loose?: boolean } = {},
): null | { score: number, indexes: Array<number> } {
return fuzzyMatchAll(searchString, [text], options);
}
export function fuzzyMatchAll(
searchString: string,
allText: Array<string>,
options: { splitSpace?: boolean, loose?: boolean } = {},
): null | { score: number, indexes: Array<number> } {
if (!searchString || !searchString.trim()) {
return null;
}
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;
}
// Don't match garbage
if (result.score < -8000) {
continue;
}
if (maxScore === null || result.score > maxScore) {
maxScore = result.score;
}
indexes = [...indexes, ...result.indexes];
matchedTerm = true;
}
if (matchedTerm) {
termsMatched++;
}
}
// Make sure we match all provided terms except the last (full) one
if (!options.loose && termsMatched < terms.length - 1) {
return null;
}
if (maxScore === null) {
return null;
}
return { score: maxScore, indexes, target: allText.join(' ') };
}
2018-06-25 17:42:50 +00:00
export function getViewportSize(): string | null {
const { BrowserWindow } = electron.remote || electron;
const w = BrowserWindow.getFocusedWindow() || BrowserWindow.getAllWindows()[0];
if (w) {
2018-06-25 17:42:50 +00:00
const { width, height } = w.getContentBounds();
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;
return `${width}x${height}`;
}
2018-06-25 17:42:50 +00:00
export function getUserLanguage(): string {
const { app } = electron.remote || electron;
return app.getLocale();
}
export async function waitForStreamToFinish(s: Readable | Writable): Promise<void> {
return new Promise(resolve => {
2018-03-29 17:59:32 +00:00
if ((s: any)._readableState && (s: any)._readableState.finished) {
return resolve();
}
2018-03-29 17:59:32 +00:00
if ((s: any)._writableState && (s: any)._writableState.finished) {
return resolve();
}
s.on('close', () => {
resolve();
});
s.on('error', () => {
resolve();
});
});
}
export function getDataDirectory(): string {
const { app } = electron.remote || electron;
return process.env.INSOMNIA_DATA_PATH || app.getPath('userData');
}