insomnia/app/backend/util.js

117 lines
2.8 KiB
JavaScript
Raw Normal View History

import {parse as urlParse, format as urlFormat} from 'url';
2016-09-21 00:03:26 +00:00
export function getBasicAuthHeader (username, password) {
const name = 'Authorization';
const header = `${username || ''}:${password || ''}`;
const authString = new Buffer(header, 'utf8').toString('base64');
const value = `Basic ${authString}`;
return {name, value};
}
export function filterHeaders (headers, name) {
if (!Array.isArray(headers) || !name) {
return [];
}
return headers.filter(
h => h.name.toLowerCase() === name.toLowerCase()
);
}
export function hasAuthHeader (headers) {
return filterHeaders(headers, 'authorization').length > 0;
}
export function getSetCookieHeaders (headers) {
return filterHeaders(headers, 'set-cookie');
}
2016-09-04 21:32:36 +00:00
export function setDefaultProtocol (url, defaultProto = 'http:') {
// 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) {
2016-09-04 21:32:36 +00:00
const CHARS = '23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ'.split('');
const dateString = Date.now().toString(36);
let randString = '';
for (let i = 0; i < 16; i++) {
randString += CHARS[Math.floor(Math.random() * CHARS.length)];
}
if (prefix) {
return `${prefix}_${dateString}${randString}`;
} else {
return `${dateString}${randString}`;
}
}
2016-09-22 19:44:28 +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-09-24 03:26:24 +00:00
export function flexibleEncode (str) {
2016-09-22 19:44:28 +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-22 19:44:28 +00:00
try {
2016-10-11 16:57:06 +00:00
decoded = decodeURI(str);
2016-09-22 19:44:28 +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-22 19:44:28 +00:00
}
2016-10-11 16:57:06 +00:00
return encodeURI(decoded);
}
2016-09-22 19:44:28 +00:00
export function prepareUrlForSending (url) {
const urlWithProto = setDefaultProtocol(url);
2016-09-22 19:44:28 +00:00
// Parse the URL into components
const parsedUrl = urlParse(urlWithProto, true);
// ~~~~~~~~~~~ //
// 1. Pathname //
// ~~~~~~~~~~~ //
2016-10-11 16:57:06 +00:00
if (parsedUrl.pathname) {
const segments = parsedUrl.pathname.split('/');
parsedUrl.pathname = segments.map(flexibleEncodeComponent).join('/');
}
2016-09-22 19:44:28 +00:00
// ~~~~~~~~~~~~~~ //
// 2. Querystring //
// ~~~~~~~~~~~~~~ //
// Deleting search key will force url.format to encode parsedURL.query
delete parsedUrl.search;
return urlFormat(parsedUrl);
}
export function delay (milliseconds) {
return new Promise(resolve => setTimeout(resolve, milliseconds))
}