insomnia/app/lib/network.js

115 lines
3.1 KiB
JavaScript
Raw Normal View History

import networkRequest from 'request';
import * as db from '../database';
import * as querystring from './querystring';
import {DEBOUNCE_MILLIS} from './constants';
2016-07-20 21:15:11 +00:00
import {STATUS_CODE_PEBKAC} from './constants';
2016-07-20 23:16:28 +00:00
import {getRenderedRequest} from './render';
2016-04-09 21:08:55 +00:00
2016-07-07 22:06:18 +00:00
function buildRequestConfig (request, patch = {}) {
2016-04-09 21:08:55 +00:00
const config = {
2016-04-10 02:58:48 +00:00
method: request.method,
body: request.body,
2016-04-28 07:30:26 +00:00
headers: {},
// Setup redirect rules
followRedirect: true,
maxRedirects: 10,
timeout: -1,
2016-04-28 07:30:26 +00:00
// Unzip gzipped responses
gzip: true,
// Time the request
time: true
2016-04-09 21:08:55 +00:00
};
// Set the URL, including the query parameters
const qs = querystring.buildFromParams(request.parameters);
2016-07-15 03:50:59 +00:00
config.url = querystring.joinURL(request.url, qs);
2016-04-17 04:28:57 +00:00
// Set basic auth if we need to
2016-04-09 21:08:55 +00:00
if (request.authentication.username) {
config.auth = {
user: request.authentication.username,
pass: request.authentication.password
}
}
2016-04-10 02:58:48 +00:00
for (let i = 0; i < request.headers.length; i++) {
let header = request.headers[i];
2016-04-12 00:39:49 +00:00
if (header.name) {
config.headers[header.name] = header.value;
}
2016-04-09 21:08:55 +00:00
}
2016-04-28 07:30:26 +00:00
return Object.assign(config, patch);
}
2016-07-20 21:15:11 +00:00
function actuallySend (request, settings) {
return new Promise((resolve, reject) => {
let config = buildRequestConfig(request, {
jar: networkRequest.jar(),
followRedirect: settings.followRedirects,
timeout: settings.timeout > 0 ? settings.timeout : null
}, true);
const startTime = Date.now();
networkRequest(config, function (err, networkResponse) {
2016-07-20 21:15:11 +00:00
if (err) {
db.responseCreate({
parentId: request._id,
elapsedTime: Date.now() - startTime,
2016-07-20 21:15:11 +00:00
error: err.toString()
});
console.warn(`Request to ${config.url} failed`, err);
return reject(err);
}
2016-07-20 21:15:11 +00:00
const responsePatch = {
parentId: request._id,
statusCode: networkResponse.statusCode,
statusMessage: networkResponse.statusMessage,
contentType: networkResponse.headers['content-type'],
url: config.url, // TODO: Handle redirects somehow
elapsedTime: networkResponse.elapsedTime,
bytesRead: networkResponse.connection.bytesRead,
body: networkResponse.body,
headers: Object.keys(networkResponse.headers).map(name => {
const value = networkResponse.headers[name];
2016-04-10 06:37:22 +00:00
return {name, value};
})
2016-07-20 21:15:11 +00:00
};
2016-04-28 07:30:26 +00:00
2016-07-20 21:15:11 +00:00
db.responseCreate(responsePatch).then(resolve, reject);
})
})
2016-04-09 21:08:55 +00:00
}
2016-07-20 21:15:11 +00:00
export function send (requestId) {
return new Promise((resolve, reject) => {
// First, lets wait for all debounces to finish
setTimeout(() => {
Promise.all([
db.requestGetById(requestId),
db.settingsGet()
]).then(([
request,
settings
]) => {
2016-07-20 23:16:28 +00:00
getRenderedRequest(request).then(renderedRequest => {
actuallySend(renderedRequest, settings).then(resolve, reject);
}, err => {
db.responseCreate({
parentId: request._id,
statusCode: STATUS_CODE_PEBKAC,
error: err.message
}).then(resolve, reject);
2016-07-20 21:15:11 +00:00
});
})
}, DEBOUNCE_MILLIS);
});
}