2019-01-17 18:22:05 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
|
|
value: true
|
|
|
|
});
|
|
|
|
|
|
|
|
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
|
|
|
|
|
|
|
|
var _electron = require('electron');
|
|
|
|
|
|
|
|
var _querystring = require('querystring');
|
|
|
|
|
|
|
|
var _querystring2 = _interopRequireDefault(_querystring);
|
|
|
|
|
|
|
|
var _request = require('request');
|
|
|
|
|
|
|
|
var _request2 = _interopRequireDefault(_request);
|
|
|
|
|
|
|
|
var _events = require('events');
|
|
|
|
|
|
|
|
var _events2 = _interopRequireDefault(_events);
|
|
|
|
|
|
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
|
|
|
|
|
|
function _log(_msg) {
|
|
|
|
// console.log('[Request] ' + _msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
function requestWithMethod(method, origOpts, origCallback) {
|
|
|
|
if (typeof origOpts == 'string') {
|
|
|
|
origOpts = { url: origOpts };
|
|
|
|
}
|
|
|
|
|
2019-03-12 17:01:00 +00:00
|
|
|
const opts = _extends({}, origOpts, { method });
|
2019-01-17 18:22:05 +00:00
|
|
|
|
2019-03-12 17:01:00 +00:00
|
|
|
let callback;
|
2019-01-17 18:22:05 +00:00
|
|
|
if (origCallback || opts.callback) {
|
2019-03-12 17:01:00 +00:00
|
|
|
const origOptsCallback = opts.callback;
|
2019-01-17 18:22:05 +00:00
|
|
|
delete opts.callback;
|
2019-03-12 17:01:00 +00:00
|
|
|
callback = (...args) => {
|
2019-01-17 18:22:05 +00:00
|
|
|
if (origCallback) {
|
2019-03-12 17:01:00 +00:00
|
|
|
origCallback.apply(this, args);
|
2019-01-17 18:22:05 +00:00
|
|
|
}
|
|
|
|
if (origOptsCallback) {
|
2019-03-12 17:01:00 +00:00
|
|
|
origOptsCallback.apply(this, args);
|
2019-01-17 18:22:05 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-03-12 17:01:00 +00:00
|
|
|
const strictOpts = _extends({}, opts, { strictSSL: true });
|
|
|
|
const laxOpts = _extends({}, opts, { strictSSL: false });
|
2019-01-17 18:22:05 +00:00
|
|
|
|
2019-03-12 17:01:00 +00:00
|
|
|
const rv = new _events2.default();
|
2019-01-17 18:22:05 +00:00
|
|
|
|
|
|
|
if (callback) {
|
|
|
|
_log('have callback, so wrapping');
|
2019-03-12 17:01:00 +00:00
|
|
|
rv.on('response', response => {
|
|
|
|
const chunks = [];
|
|
|
|
response.on('data', chunk => chunks.push(chunk));
|
|
|
|
response.on('end', () => {
|
2019-01-17 18:22:05 +00:00
|
|
|
callback(null, response, Buffer.concat(chunks));
|
|
|
|
});
|
|
|
|
});
|
2019-03-12 17:01:00 +00:00
|
|
|
rv.on('error', error => callback(error));
|
2019-01-17 18:22:05 +00:00
|
|
|
}
|
|
|
|
|
2019-03-12 17:01:00 +00:00
|
|
|
const requestTypes = [{
|
|
|
|
factory: function () {
|
2019-01-17 18:22:05 +00:00
|
|
|
return (0, _request2.default)(strictOpts);
|
|
|
|
},
|
|
|
|
method: 'node_request_strict'
|
|
|
|
}, {
|
2019-03-12 17:01:00 +00:00
|
|
|
factory: function () {
|
|
|
|
const qs = _querystring2.default.stringify(strictOpts.qs);
|
|
|
|
const nr = _electron.net.request(_extends({}, strictOpts, { url: `${strictOpts.url}?${qs}` }));
|
2019-01-17 18:22:05 +00:00
|
|
|
nr.end();
|
|
|
|
return nr;
|
|
|
|
},
|
|
|
|
method: 'electron_net_request_strict'
|
|
|
|
}, {
|
2019-03-12 17:01:00 +00:00
|
|
|
factory: function () {
|
2019-01-17 18:22:05 +00:00
|
|
|
return (0, _request2.default)(laxOpts);
|
|
|
|
},
|
|
|
|
method: 'node_request_lax'
|
|
|
|
}];
|
|
|
|
|
|
|
|
function attempt(index) {
|
2019-03-12 17:01:00 +00:00
|
|
|
const { factory, method } = requestTypes[index];
|
|
|
|
_log(`Attempt #${index + 1}: ${method}`);
|
|
|
|
factory().on('response', response => {
|
|
|
|
_log(`${method} success! emitting response ${response}`);
|
2019-01-17 18:22:05 +00:00
|
|
|
rv.emit('response', response);
|
2019-03-12 17:01:00 +00:00
|
|
|
}).on('error', error => {
|
2019-01-17 18:22:05 +00:00
|
|
|
if (index + 1 < requestTypes.length) {
|
2019-03-12 17:01:00 +00:00
|
|
|
_log(`${method} failure, trying next option`);
|
2019-01-17 18:22:05 +00:00
|
|
|
attempt(index + 1);
|
|
|
|
} else {
|
2019-03-12 17:01:00 +00:00
|
|
|
_log(`${method} failure, out of options`);
|
2019-01-17 18:22:05 +00:00
|
|
|
rv.emit('error', error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
attempt(0);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// only supports get for now, since retrying is non-idempotent and
|
|
|
|
// we'd want to grovel the errors to make sure it's safe to retry
|
2019-03-12 17:01:00 +00:00
|
|
|
for (const method of ['get']) {
|
2019-01-17 18:22:05 +00:00
|
|
|
requestWithMethod[method] = requestWithMethod.bind(null, method);
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.default = requestWithMethod;
|
|
|
|
module.exports = exports['default'];
|