Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use axios instead of request #249

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions lib/transports/HttpRequestTransport.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/*jslint esversion: 6 */
'use strict';

var request = require('request');
var axios = require('axios');

function HttpRequestTransport(options, protocol) {
this.options = options;
Expand All @@ -25,15 +25,16 @@ HttpRequestTransport.prototype.init = function init() {

HttpRequestTransport.prototype.process = function process() {
var self = this;
request({'url': this.options.transportHttpRequestUrl, 'timeout': this.options.transportHttpRequestTimeout}, function (error, response, body) {
if (!error && response && response.statusCode === 200) {
try {
body = Buffer.from(body);
} catch (e) {
if (self.options.debug === 2) self.options.logger('CAN NOT ALLOCATE MEMORY! STOPPING');
self.stop();
return;
}

axios.get(this.options.transportHttpRequestUrl,
{
responseType: 'arraybuffer',
timeout: this.options.transportHttpRequestTimeout
})
.then((response) => {
let body = response.data;

if (response.status === 200) {
var counter = 0;
while (body && body.length > 0 && (counter === 0 || !self.protocol.isProcessComplete())) {
if (self.protocol.checkMessage(body)) {
Expand All @@ -46,8 +47,9 @@ HttpRequestTransport.prototype.process = function process() {
}
}
else {
self.options.logger('ERROR ON HTTP REQUEST: ' + (response ? response.statusCode : '') + ': ' + error);
self.options.logger('ERROR ON HTTP REQUEST: ' + (response ? response.status : '') + ': ' + error);
}

if (self.protocol.isProcessComplete() && !self.stopRequests) {
if (self.options.requestInterval >= 0) {
if (self.options.debug === 2) self.options.logger('SCHEDULE NEXT RUN IN ' + self.options.requestInterval + 's');
Expand All @@ -60,7 +62,8 @@ HttpRequestTransport.prototype.process = function process() {
self.stop();
}
}
});
})
.catch((error) => self.options.logger('ERROR ON HTTP REQUEST: ' + error));
};

HttpRequestTransport.prototype.stop = function stop(callback) {
Expand Down
Loading
Loading