2016-11-19 03:21:15 +00:00
|
|
|
import uuid from 'uuid';
|
2016-10-02 20:57:00 +00:00
|
|
|
import {parse as urlParse, format as urlFormat} from 'url';
|
2016-11-09 03:18:25 +00:00
|
|
|
import {DEBOUNCE_MILLIS} from "./constants";
|
2016-11-17 21:22:19 +00:00
|
|
|
import * as querystring from './querystring';
|
2016-09-21 00:03:26 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
export function getBasicAuthHeader (username, password) {
|
2016-09-03 04:32:45 +00:00
|
|
|
const name = 'Authorization';
|
|
|
|
const header = `${username || ''}:${password || ''}`;
|
|
|
|
const authString = new Buffer(header, 'utf8').toString('base64');
|
|
|
|
const value = `Basic ${authString}`;
|
|
|
|
return {name, value};
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-09-03 04:32:45 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
export function filterHeaders (headers, name) {
|
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 {
|
|
|
|
return h.name.toLowerCase() === name.toLowerCase()
|
|
|
|
}
|
|
|
|
});
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-09-03 05:14:48 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
export function hasAuthHeader (headers) {
|
|
|
|
return filterHeaders(headers, 'authorization').length > 0;
|
|
|
|
}
|
2016-09-03 04:32:45 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-11-22 19:42:10 +00:00
|
|
|
export function getContentLengthHeader (headers) {
|
|
|
|
const matches = filterHeaders(headers, 'content-length');
|
|
|
|
return matches.length ? matches[0] : null;
|
|
|
|
}
|
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
export function setDefaultProtocol (url, defaultProto = 'http:') {
|
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}
|
|
|
|
*/
|
2016-10-02 20:57:00 +00:00
|
|
|
export function generateId (prefix) {
|
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
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
export function flexibleEncodeComponent (str) {
|
2016-09-24 03:26:24 +00:00
|
|
|
// Sometimes spaces screw things up because of url.parse
|
|
|
|
str = str.replace(/%20/g, ' ');
|
|
|
|
|
2016-10-11 16:57:06 +00:00
|
|
|
let decoded;
|
2016-09-24 03:26:24 +00:00
|
|
|
try {
|
2016-10-11 16:57:06 +00:00
|
|
|
decoded = decodeURIComponent(str);
|
2016-09-24 03:26:24 +00:00
|
|
|
} catch (e) {
|
|
|
|
// Malformed (probably not encoded) so assume it's decoded already
|
2016-10-11 16:57:06 +00:00
|
|
|
decoded = str;
|
2016-09-24 03:26:24 +00:00
|
|
|
}
|
|
|
|
|
2016-10-11 16:57:06 +00:00
|
|
|
return encodeURIComponent(decoded);
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-09-24 03:26:24 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
export function prepareUrlForSending (url) {
|
|
|
|
const urlWithProto = setDefaultProtocol(url);
|
2016-09-22 19:44:28 +00:00
|
|
|
|
|
|
|
// Parse the URL into components
|
2016-11-17 21:36:55 +00:00
|
|
|
const parsedUrl = urlParse(urlWithProto);
|
2016-09-22 19:44:28 +00:00
|
|
|
|
|
|
|
// ~~~~~~~~~~~ //
|
|
|
|
// 1. Pathname //
|
|
|
|
// ~~~~~~~~~~~ //
|
|
|
|
|
2016-10-11 16:57:06 +00:00
|
|
|
if (parsedUrl.pathname) {
|
|
|
|
const segments = parsedUrl.pathname.split('/');
|
2016-11-17 21:22:19 +00:00
|
|
|
parsedUrl.pathname = segments.map(flexibleEncodeComponent).join('/')
|
|
|
|
.replace(/%3B/gi, ';') // Don't encode ; in pathname
|
|
|
|
.replace(/%40/gi, '@') // Don't encode @ in pathname
|
|
|
|
.replace(/%2C/gi, ','); // Don't encode , in pathname
|
2016-10-11 16:57:06 +00:00
|
|
|
}
|
2016-09-22 19:44:28 +00:00
|
|
|
|
|
|
|
// ~~~~~~~~~~~~~~ //
|
|
|
|
// 2. Querystring //
|
|
|
|
// ~~~~~~~~~~~~~~ //
|
|
|
|
|
2016-11-17 21:36:55 +00:00
|
|
|
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-11-17 21:22:19 +00:00
|
|
|
}
|
2016-11-17 21:36:55 +00:00
|
|
|
|
|
|
|
parsedUrl.query = querystring.buildFromParams(encodedQsParams);
|
|
|
|
parsedUrl.search = `?${parsedUrl.query}`;
|
2016-11-17 21:22:19 +00:00
|
|
|
}
|
|
|
|
|
2016-09-22 19:44:28 +00:00
|
|
|
return urlFormat(parsedUrl);
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
|
|
|
|
2016-11-10 21:03:12 +00:00
|
|
|
export function delay (milliseconds = DEBOUNCE_MILLIS) {
|
2016-10-02 20:57:00 +00:00
|
|
|
return new Promise(resolve => setTimeout(resolve, milliseconds))
|
|
|
|
}
|
2016-10-26 17:49:49 +00:00
|
|
|
|
|
|
|
export function removeVowels (str) {
|
|
|
|
return str.replace(/[aeiouyAEIOUY]/g, '');
|
|
|
|
}
|
2016-11-09 03:18:25 +00:00
|
|
|
|
2016-11-10 21:03:12 +00:00
|
|
|
export function keyedDebounce (callback, millis = DEBOUNCE_MILLIS) {
|
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);
|
|
|
|
}
|
|
|
|
}
|
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__'])
|
|
|
|
}, millis).bind(null, '__key__');
|
|
|
|
}
|
2016-11-22 22:26:52 +00:00
|
|
|
|
|
|
|
export function describeByteSize (bytes) {
|
|
|
|
bytes = Math.round(bytes * 10) / 10;
|
|
|
|
let size;
|
|
|
|
|
|
|
|
let unit = 'B';
|
|
|
|
if (bytes < 1024) {
|
|
|
|
size = bytes;
|
|
|
|
unit = 'B';
|
|
|
|
} else if (bytes < 1024 * 1024) {
|
|
|
|
size = bytes / 1024;
|
|
|
|
unit = 'KB';
|
|
|
|
} else if (bytes < 1024 * 1024) {
|
|
|
|
size = bytes / 1024 / 1024;
|
|
|
|
unit = 'MB';
|
|
|
|
} else {
|
|
|
|
size = bytes / 1024 / 1024 / 1024;
|
|
|
|
unit = 'GB';
|
|
|
|
}
|
|
|
|
|
|
|
|
return Math.round(size * 10) / 10 + ' ' + unit;
|
|
|
|
}
|