insomnia/app/common/misc.js

281 lines
7.1 KiB
JavaScript
Raw Normal View History

2016-11-19 03:21:15 +00:00
import uuid from 'uuid';
import {parse as urlParse, format as urlFormat} from 'url';
import {DEBOUNCE_MILLIS, getAppVersion, isDevelopment} from './constants';
2016-11-17 21:22:19 +00:00
import * as querystring from './querystring';
import {shell} from 'electron';
2016-09-21 00:03:26 +00:00
const URL_PATH_CHARACTER_WHITELIST = '+,;@=:';
export function getBasicAuthHeader (username, password) {
const name = 'Authorization';
const header = `${username || ''}:${password || ''}`;
const authString = Buffer.from(header, 'utf8').toString('base64');
const value = `Basic ${authString}`;
return {name, value};
}
export function getBearerAuthHeader (token) {
const name = 'Authorization';
const value = `Bearer ${token}`;
return {name, value};
}
export function filterHeaders (headers, name) {
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
}
});
}
export function hasContentTypeHeader (headers) {
return filterHeaders(headers, 'content-type').length > 0;
}
export function hasContentLengthHeader (headers) {
return filterHeaders(headers, 'content-length').length > 0;
}
export function hasAuthHeader (headers) {
return filterHeaders(headers, 'authorization').length > 0;
}
export function hasAcceptHeader (headers) {
return filterHeaders(headers, 'accept').length > 0;
}
export function hasUserAgentHeader (headers) {
return filterHeaders(headers, 'user-agent').length > 0;
}
export function getSetCookieHeaders (headers) {
return filterHeaders(headers, 'set-cookie');
}
2016-09-04 21:32:36 +00:00
2016-11-10 17:33:28 +00:00
export function getContentTypeHeader (headers) {
const matches = filterHeaders(headers, 'content-type');
return matches.length ? matches[0] : null;
}
export function getContentLengthHeader (headers) {
const matches = filterHeaders(headers, 'content-length');
return matches.length ? matches[0] : null;
}
export function setDefaultProtocol (url, defaultProto = 'http:') {
// If no url, don't bother returning anything
if (!url) {
return '';
}
// Default the proto if it doesn't exist
if (url.indexOf('://') === -1) {
url = `${defaultProto}//${url}`;
}
return url;
}
2016-09-04 21:32:36 +00:00
/**
* Generate an ID of the format "<MODEL_NAME>_<TIMESTAMP><RANDOM>"
* @param prefix
* @returns {string}
*/
export function generateId (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
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
export function flexibleEncodeComponent (str, ignore = '') {
2016-09-24 03:26:24 +00:00
// Sometimes spaces screw things up because of url.parse
str = str.replace(/%20/g, ' ');
const ignoredChars = ignore.split('');
// Do a special encode of ignored chars, so they aren't touched.
// This first pass, surrounds them with a special tag (anything unique
// will work), so it can change them back later
// Example: will replace %40 with __LEAVE_40_LEAVE__, and we'll change
// it back to %40 at the end.
for (const c of ignoredChars) {
const code = encodeURIComponent(c).replace('%', '');
// Replace encoded versions
const re = new RegExp(encodeURIComponent(c), 'g');
str = str.replace(re, `__ENCODED_${code}_ENCODED__`);
// Replace raw versions
const re2 = new RegExp(`[${c}]`, 'g');
str = str.replace(re2, `__RAW_${code}_RAW__`);
}
2016-09-24 03:26:24 +00:00
try {
str = decodeURIComponent(str);
2016-09-24 03:26:24 +00:00
} catch (e) {
// Malformed (probably not encoded) so assume it's decoded already
}
// Encode it
str = encodeURIComponent(str);
// Put back the encoded version of the ignored chars
for (const c of ignoredChars) {
const code = encodeURIComponent(c).replace('%', '');
// Put back encoded versions
const re = new RegExp(`__ENCODED_${code}_ENCODED__`, 'g');
str = str.replace(re, encodeURIComponent(c));
// Put back raw versions
const re2 = new RegExp(`__RAW_${code}_RAW__`, 'g');
str = str.replace(re2, c);
}
return str;
}
2016-09-24 03:26:24 +00:00
export function prepareUrlForSending (url, autoEncode = true) {
const urlWithProto = setDefaultProtocol(url);
2016-09-22 19:44:28 +00:00
if (!autoEncode) {
return urlWithProto;
} else {
// Parse the URL into components
const parsedUrl = urlParse(urlWithProto);
// ~~~~~~~~~~~ //
// 1. Pathname //
// ~~~~~~~~~~~ //
if (parsedUrl.pathname) {
const segments = parsedUrl.pathname.split('/');
parsedUrl.pathname = segments.map(
s => flexibleEncodeComponent(s, URL_PATH_CHARACTER_WHITELIST)
).join('/');
}
2016-09-22 19:44:28 +00:00
// ~~~~~~~~~~~~~~ //
// 2. Querystring //
// ~~~~~~~~~~~~~~ //
if (parsedUrl.query) {
const qsParams = querystring.deconstructToParams(parsedUrl.query);
const encodedQsParams = [];
for (const {name, value} of qsParams) {
encodedQsParams.push({
name: flexibleEncodeComponent(name),
value: flexibleEncodeComponent(value)
});
}
2016-09-22 19:44:28 +00:00
parsedUrl.query = querystring.buildFromParams(encodedQsParams);
parsedUrl.search = `?${parsedUrl.query}`;
2016-11-17 21:22:19 +00:00
}
2016-11-17 21:36:55 +00:00
return urlFormat(parsedUrl);
2016-11-17 21:22:19 +00:00
}
}
2016-11-10 21:03:12 +00:00
export function delay (milliseconds = DEBOUNCE_MILLIS) {
return new Promise(resolve => setTimeout(resolve, milliseconds));
}
export function removeVowels (str) {
return str.replace(/[aeiouyAEIOUY]/g, '');
}
2016-11-10 21:03:12 +00:00
export function keyedDebounce (callback, millis = DEBOUNCE_MILLIS) {
let timeout = null;
2016-11-10 21:03:12 +00:00
let results = {};
return function (key, ...args) {
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, millis = DEBOUNCE_MILLIS) {
// 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
export function describeByteSize (bytes, long) {
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
}
2017-04-09 21:53:46 +00:00
const rounded = (Math.round(size * 10) / 10);
return `${rounded} ${unit}`;
2016-11-22 22:26:52 +00:00
}
export function nullFn () {
// Do nothing
}
export function preventDefault (e) {
e.preventDefault();
}
export function stopPropagation (e) {
e.stopPropagation();
}
export function clickLink (href) {
if (href.match(/^http/i)) {
const appName = isDevelopment() ? 'Insomnia Dev' : 'Insomnia';
const qs = `utm_source=${appName}&utm_medium=app&utm_campaign=v${getAppVersion()}`;
const attributedHref = querystring.joinUrl(href, qs);
shell.openExternal(attributedHref);
} else {
// Don't modify non-http urls
shell.openExternal(href);
}
}
export function fnOrString (v, ...args) {
if (typeof v === 'string') {
return v;
} else {
return v(...args);
}
}