2017-01-28 23:51:47 +00:00
|
|
|
"use strict";
|
|
|
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
|
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
|
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
|
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
|
|
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
|
|
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
|
|
});
|
|
|
|
};
|
2017-06-09 19:48:16 +00:00
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2017-01-28 23:51:47 +00:00
|
|
|
const plugins = require("./smartrequest.plugins");
|
2018-06-13 20:34:49 +00:00
|
|
|
;
|
|
|
|
let buildResponse = (incomingMessageArg) => {
|
2018-06-14 20:48:50 +00:00
|
|
|
let done = plugins.smartq.defer();
|
2017-01-28 23:51:47 +00:00
|
|
|
// Continuously update stream with data
|
|
|
|
let body = '';
|
2018-06-13 20:34:49 +00:00
|
|
|
incomingMessageArg.on('data', function (chunkArg) {
|
2017-01-28 23:51:47 +00:00
|
|
|
body += chunkArg;
|
|
|
|
});
|
2018-06-13 20:34:49 +00:00
|
|
|
incomingMessageArg.on('end', function () {
|
2017-01-28 23:51:47 +00:00
|
|
|
try {
|
2018-06-13 20:34:49 +00:00
|
|
|
incomingMessageArg.body = JSON.parse(body);
|
2017-01-28 23:51:47 +00:00
|
|
|
}
|
|
|
|
catch (err) {
|
2018-06-13 20:34:49 +00:00
|
|
|
incomingMessageArg.body = body;
|
2017-01-28 23:51:47 +00:00
|
|
|
}
|
2018-06-13 20:34:49 +00:00
|
|
|
done.resolve(incomingMessageArg);
|
2017-01-28 23:51:47 +00:00
|
|
|
});
|
|
|
|
return done.promise;
|
|
|
|
};
|
|
|
|
exports.request = (domainArg, optionsArg = {}, streamArg = false) => __awaiter(this, void 0, void 0, function* () {
|
2018-06-14 20:48:50 +00:00
|
|
|
let done = plugins.smartq.defer();
|
2017-01-28 23:51:47 +00:00
|
|
|
let parsedUrl;
|
|
|
|
if (domainArg) {
|
|
|
|
parsedUrl = plugins.url.parse(domainArg);
|
|
|
|
optionsArg.hostname = parsedUrl.hostname;
|
|
|
|
if (parsedUrl.port) {
|
|
|
|
optionsArg.port = parseInt(parsedUrl.port);
|
|
|
|
}
|
|
|
|
optionsArg.path = parsedUrl.path;
|
|
|
|
}
|
|
|
|
if (!parsedUrl || parsedUrl.protocol === 'https:') {
|
|
|
|
let request = plugins.https.request(optionsArg, response => {
|
|
|
|
if (streamArg) {
|
|
|
|
done.resolve(response);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
buildResponse(response).then(done.resolve);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (optionsArg.requestBody) {
|
|
|
|
if (typeof optionsArg.requestBody !== 'string') {
|
|
|
|
optionsArg.requestBody = JSON.stringify(optionsArg.requestBody);
|
|
|
|
}
|
|
|
|
request.write(optionsArg.requestBody);
|
|
|
|
}
|
2018-06-13 20:34:49 +00:00
|
|
|
request.on('error', e => {
|
2017-01-28 23:51:47 +00:00
|
|
|
console.error(e);
|
|
|
|
});
|
|
|
|
request.end();
|
|
|
|
}
|
|
|
|
else if (parsedUrl.protocol === 'http:') {
|
|
|
|
let request = plugins.http.request(optionsArg, response => {
|
|
|
|
if (streamArg) {
|
|
|
|
done.resolve(response);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
buildResponse(response).then(done.resolve);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (optionsArg.requestBody) {
|
|
|
|
if (typeof optionsArg.requestBody !== 'string') {
|
|
|
|
optionsArg.requestBody = JSON.stringify(optionsArg.requestBody);
|
|
|
|
}
|
|
|
|
request.write(optionsArg.requestBody);
|
|
|
|
}
|
2018-06-13 20:34:49 +00:00
|
|
|
request.on('error', e => {
|
2017-01-28 23:51:47 +00:00
|
|
|
console.error(e);
|
|
|
|
});
|
|
|
|
request.end();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new Error(`unsupported protocol: ${parsedUrl.protocol}`);
|
|
|
|
}
|
|
|
|
return done.promise;
|
|
|
|
});
|
2018-06-14 20:48:50 +00:00
|
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRyZXF1ZXN0LnJlcXVlc3QuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydHJlcXVlc3QucmVxdWVzdC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7O0FBQ0Esa0RBQWtEO0FBT2pELENBQUM7QUFFRixJQUFJLGFBQWEsR0FBRyxDQUFDLGtCQUFtQyxFQUFvQyxFQUFFO0lBQzVGLElBQUksSUFBSSxHQUFHLE9BQU8sQ0FBQyxNQUFNLENBQUMsS0FBSyxFQUEyQixDQUFDO0lBQzNELHVDQUF1QztJQUN2QyxJQUFJLElBQUksR0FBRyxFQUFFLENBQUM7SUFDZCxrQkFBa0IsQ0FBQyxFQUFFLENBQUMsTUFBTSxFQUFFLFVBQVMsUUFBUTtRQUM3QyxJQUFJLElBQUksUUFBUSxDQUFDO0lBQ25CLENBQUMsQ0FBQyxDQUFDO0lBRUgsa0JBQWtCLENBQUMsRUFBRSxDQUFDLEtBQUssRUFBRTtRQUMzQixJQUFJO1lBQ0Qsa0JBQThDLENBQUMsSUFBSSxHQUFHLElBQUksQ0FBQyxLQUFLLENBQUMsSUFBSSxDQUFDLENBQUM7U0FDekU7UUFBQyxPQUFPLEdBQUcsRUFBRTtZQUNYLGtCQUE4QyxDQUFDLElBQUksR0FBRyxJQUFJLENBQUM7U0FDN0Q7UUFDRCxJQUFJLENBQUMsT0FBTyxDQUFDLGtCQUE2QyxDQUFDLENBQUM7SUFDOUQsQ0FBQyxDQUFDLENBQUM7SUFDSCxPQUFPLElBQUksQ0FBQyxPQUFP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