2017-07-19 02:54:03 +00:00
|
|
|
// @flow
|
2016-11-19 03:21:15 +00:00
|
|
|
import uuid from 'uuid';
|
2017-06-30 03:30:22 +00:00
|
|
|
import zlib from 'zlib';
|
2017-07-20 01:55:40 +00:00
|
|
|
import {join as pathJoin} from 'path';
|
2017-06-30 03:30:22 +00:00
|
|
|
import {format as urlFormat, parse as urlParse} from 'url';
|
2017-06-01 22:58:09 +00:00
|
|
|
import {DEBOUNCE_MILLIS, getAppVersion, isDevelopment} from './constants';
|
2016-11-17 21:22:19 +00:00
|
|
|
import * as querystring from './querystring';
|
2017-06-01 22:58:09 +00:00
|
|
|
import {shell} from 'electron';
|
2016-09-21 00:03:26 +00:00
|
|
|
|
2017-02-16 17:38:59 +00:00
|
|
|
const URL_PATH_CHARACTER_WHITELIST = '+,;@=:';
|
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,
|
|
|
|
value: string
|
|
|
|
};
|
|
|
|
|
|
|
|
export function getBasicAuthHeader (username: ?string, password: ?string): Header {
|
2016-09-03 04:32:45 +00:00
|
|
|
const name = 'Authorization';
|
|
|
|
const header = `${username || ''}:${password || ''}`;
|
2017-03-23 22:10:42 +00:00
|
|
|
const authString = Buffer.from(header, 'utf8').toString('base64');
|
2016-09-03 04:32:45 +00:00
|
|
|
const value = `Basic ${authString}`;
|
|
|
|
return {name, value};
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-09-03 04:32:45 +00:00
|
|
|
|
2017-07-19 02:54:03 +00:00
|
|
|
export function getBearerAuthHeader (token: string): Header {
|
2017-06-01 13:18:42 +00:00
|
|
|
const name = 'Authorization';
|
|
|
|
const value = `Bearer ${token}`;
|
|
|
|
return {name, value};
|
|
|
|
}
|
|
|
|
|
2017-07-19 02:54:03 +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
|
|
|
|
2017-07-19 02:54:03 +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;
|
|
|
|
}
|
|
|
|
|
2017-07-19 02:54:03 +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;
|
|
|
|
}
|
|
|
|
|
2017-07-19 02:54:03 +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
|
|
|
|
2017-07-19 02:54:03 +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;
|
|
|
|
}
|
|
|
|
|
2017-07-19 02:54:03 +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;
|
|
|
|
}
|
|
|
|
|
2017-07-19 02:54:03 +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
|
|
|
|
2017-07-19 02:54:03 +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;
|
|
|
|
}
|
|
|
|
|
2017-07-19 02:54:03 +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;
|
|
|
|
}
|
|
|
|
|
2017-07-19 02:54:03 +00:00
|
|
|
export function setDefaultProtocol (url: string, defaultProto: string = 'http:'): string {
|
2017-03-06 03:00:43 +00:00
|
|
|
// If no url, don't bother returning anything
|
|
|
|
if (!url) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2016-09-12 20:04:15 +00:00
|
|
|
// Default the proto if it doesn't exist
|
|
|
|
if (url.indexOf('://') === -1) {
|
|
|
|
url = `${defaultProto}//${url}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return url;
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-09-12 20:04:15 +00:00
|
|
|
|
2016-09-04 21:32:36 +00:00
|
|
|
/**
|
|
|
|
* Generate an ID of the format "<MODEL_NAME>_<TIMESTAMP><RANDOM>"
|
|
|
|
* @param prefix
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-07-19 02:54:03 +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
|
|
|
|
2017-07-19 02:54:03 +00:00
|
|
|
export function flexibleEncodeComponent (str: string, ignore: string = ''): string {
|
2016-09-24 03:26:24 +00:00
|
|
|
// Sometimes spaces screw things up because of url.parse
|
|
|
|
str = str.replace(/%20/g, ' ');
|
|
|
|
|
2017-09-09 12:26:13 +00:00
|
|
|
// Handle all already-encoded characters so we don't touch them
|
|
|
|
str = str.replace(/%([0-9a-fA-F]{2})/g, '__ENC__$1');
|
2016-11-30 23:11:36 +00:00
|
|
|
|
|
|
|
// 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.
|
2017-09-09 12:26:13 +00:00
|
|
|
for (const c of ignore) {
|
2016-11-30 23:11:36 +00:00
|
|
|
const code = encodeURIComponent(c).replace('%', '');
|
2017-09-09 12:26:13 +00:00
|
|
|
const re2 = new RegExp(escapeRegex(c), 'g');
|
|
|
|
str = str.replace(re2, `__RAW__${code}`);
|
2016-09-24 03:26:24 +00:00
|
|
|
}
|
|
|
|
|
2016-11-30 23:11:36 +00:00
|
|
|
// Encode it
|
|
|
|
str = encodeURIComponent(str);
|
|
|
|
|
2017-09-09 12:26:13 +00:00
|
|
|
// Put back the raw version of the ignored chars
|
|
|
|
for (const match of str.match(/__RAW__([0-9a-fA-F]{2})/g) || []) {
|
|
|
|
const code = match.replace('__RAW__', '');
|
|
|
|
str = str.replace(match, decodeURIComponent(`%${code}`));
|
|
|
|
}
|
2016-11-30 23:11:36 +00:00
|
|
|
|
2017-09-09 12:26:13 +00:00
|
|
|
// Put back the encoded version of the ignored chars
|
|
|
|
for (const match of str.match(/__ENC__([0-9a-fA-F]{2})/g) || []) {
|
|
|
|
const code = match.replace('__ENC__', '');
|
|
|
|
str = str.replace(match, `%${code}`);
|
2016-11-30 23:11:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-09-24 03:26:24 +00:00
|
|
|
|
2017-07-19 02:54:03 +00:00
|
|
|
export function prepareUrlForSending (url: string, autoEncode: boolean = true): string {
|
2016-10-02 20:57:00 +00:00
|
|
|
const urlWithProto = setDefaultProtocol(url);
|
2016-09-22 19:44:28 +00:00
|
|
|
|
2017-03-29 23:09: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
|
|
|
|
2017-03-29 23:09: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
|
|
|
|
2017-03-29 23:09: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
|
|
|
|
2017-03-29 23:09:28 +00:00
|
|
|
return urlFormat(parsedUrl);
|
2016-11-17 21:22:19 +00:00
|
|
|
}
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
|
|
|
|
2017-07-19 02:54:03 +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
|
|
|
|
2017-07-19 02:54:03 +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
|
|
|
|
2017-07-19 02:54:03 +00:00
|
|
|
export function keyedDebounce (callback: Function, millis: number = DEBOUNCE_MILLIS): Function {
|
2016-11-09 03:18:25 +00:00
|
|
|
let timeout = null;
|
2016-11-10 21:03:12 +00:00
|
|
|
let results = {};
|
|
|
|
|
|
|
|
return function (key, ...args) {
|
|
|
|
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
|
|
|
|
2017-07-19 02:54:03 +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
|
|
|
|
2017-07-19 02:54:03 +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
|
|
|
}
|
|
|
|
|
2017-04-09 21:53:46 +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
|
|
|
|
2017-07-19 02:54:03 +00:00
|
|
|
export function nullFn (): void {
|
2017-02-28 21:32:23 +00:00
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
|
2017-07-19 02:54:03 +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
|
|
|
|
2017-07-19 02:54:03 +00:00
|
|
|
export function clickLink (href: string): void {
|
2017-06-01 22:58:09 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2017-06-09 01:10:12 +00:00
|
|
|
|
2017-07-19 02:54:03 +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
|
|
|
|
2017-07-19 02:54:03 +00:00
|
|
|
export function compressObject (obj: any): string {
|
2017-06-30 03:30:22 +00:00
|
|
|
const compressed = compress(JSON.stringify(obj));
|
|
|
|
return compressed.toString('base64');
|
|
|
|
}
|
|
|
|
|
2017-07-19 02:54:03 +00:00
|
|
|
export function decompressObject (input: string): any {
|
2017-06-30 03:30:22 +00:00
|
|
|
const jsonBuffer = decompress(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
|
|
|
}
|
|
|
|
|
2017-07-19 02:54:03 +00:00
|
|
|
export function compress (inputBuffer: Buffer | string): Buffer {
|
2017-06-30 03:30:22 +00:00
|
|
|
return zlib.gzipSync(inputBuffer);
|
|
|
|
}
|
|
|
|
|
2017-07-19 02:54:03 +00:00
|
|
|
export function decompress (inputBuffer: Buffer | string): Buffer {
|
2017-06-30 03:30:22 +00:00
|
|
|
return zlib.gunzipSync(inputBuffer);
|
|
|
|
}
|
2017-07-20 01:55:40 +00:00
|
|
|
|
|
|
|
export function resolveHomePath (p: string): string {
|
|
|
|
if (p.indexOf('~/') === 0) {
|
|
|
|
return pathJoin(process.env.HOME || '/', p.slice(1));
|
|
|
|
} else {
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
2017-08-03 21:44:55 +00:00
|
|
|
|
|
|
|
export function jsonParseOr (str: string, fallback: any): any {
|
|
|
|
try {
|
|
|
|
return JSON.parse(str);
|
|
|
|
} catch (err) {
|
|
|
|
return fallback;
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
*/
|
|
|
|
export function escapeRegex (str: string): string {
|
|
|
|
return str.replace(ESCAPE_REGEX_MATCH, '\\$&');
|
|
|
|
}
|
2017-08-21 18:11:52 +00:00
|
|
|
|
|
|
|
export function fuzzyMatch (searchString: string, text: string): boolean {
|
|
|
|
const regexSearchString = escapeRegex(searchString.toLowerCase()).split('').join('.*');
|
|
|
|
const toMatch = new RegExp(regexSearchString);
|
|
|
|
return toMatch.test(text.toLowerCase());
|
|
|
|
}
|