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

Verbose output #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
78 changes: 61 additions & 17 deletions lib/barnacleswebhook.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const DEFAULT_PORT = 80;
const DEFAULT_PATH = '/raddecs';
const DEFAULT_PRINT_ERRORS = false;
const DEFAULT_CUSTOM_HEADERS = {};
const DEFAULT_VERBOSE = false;


/**
Expand All @@ -36,8 +37,9 @@ class BarnaclesWebhook {
this.path = options.path || DEFAULT_PATH;
this.customHeaders = options.customHeaders || DEFAULT_CUSTOM_HEADERS;
this.printErrors = options.printErrors || DEFAULT_PRINT_ERRORS;
this.verbose = options.verbose || DEFAULT_VERBOSE

if(this.useHttps) {
if (this.useHttps) {
this.agent = new https.Agent({ keepAlive: true });
}
else {
Expand All @@ -61,7 +63,7 @@ class BarnaclesWebhook {
handleEvent(name, data) {
let self = this

switch(name) {
switch (name) {
case 'raddec':
return handleRaddec(self, data);
}
Expand All @@ -76,6 +78,7 @@ class BarnaclesWebhook {
*/
function handleRaddec(instance, raddec) {
let data = JSON.stringify(raddec);

let headers = {
"Content-Type": "application/json",
"Content-Length": data.length
Expand All @@ -90,11 +93,11 @@ function handleRaddec(instance, raddec) {
headers: Object.assign(headers, instance.customHeaders)
};

if(instance.useHttps) {
postViaHttps(data, options, instance.printErrors);
if (instance.useHttps) {
postViaHttps(data, options, instance.printErrors, instance.verbose);
}
else {
postViaHttp(data, options, instance.printErrors);
postViaHttp(data, options, instance.printErrors, instance.verbose);
}
}

Expand All @@ -104,16 +107,37 @@ function handleRaddec(instance, raddec) {
* @param {string} data The given data as a String.
* @param {Object} options The request options.
* @param {boolean} printErrors Should errors be printed to the console?
* @param {boolean} verbose Should connection status, headers, and body be printed to console?
*/
function postViaHttp(data, options, printErrors) {
let req = http.request(options, function(res) { });
function postViaHttp(data, options, printErrors, verbose) {

let req

if (verbose) {
req = http.request(options, function (res) {
console.log(`STATUS: ${res.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
res.setEncoding('utf8');
res.on('data', (chunk) => {
console.log(`BODY: ${chunk}`);
});
res.on('end', () => {
console.log('No more data in response.');
});

});
}
else {
req = http.request(options, function (res) { });

}

req.on('error', function(error) {
if(printErrors) {
req.on('error', function (error) {
if (printErrors) {
let target = (error.address || options.hostname) + ':' +
(error.port || options.port);
(error.port || options.port);
console.error('barnacles-webhook HTTP POST error code:', error.code,
'on', target);
'on', target);
}
});

Expand All @@ -127,16 +151,36 @@ function postViaHttp(data, options, printErrors) {
* @param {string} data The given data as a String.
* @param {Object} options The request options.
* @param {boolean} printErrors Should errors be printed to the console?
* @param {boolean} verbose Should connection status, headers, and body be printed to console?
*/
function postViaHttps(data, options, printErrors) {
let req = https.request(options, function(res) { });
function postViaHttps(data, options, printErrors, verbose) {
let req
if (verbose) {

req = https.request(options, function (res) {
console.log(`STATUS: ${res.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
res.setEncoding('utf8');
res.on('data', (chunk) => {
console.log(`BODY: ${chunk}`);
});
res.on('end', () => {
console.log('No more data in response.');
});

});
}
else {
req = https.request(options, function (res) { });

}

req.on('error', function(error) {
if(printErrors) {
req.on('error', function (error) {
if (printErrors) {
let target = (error.address || options.hostname) + ':' +
(error.port || options.port);
(error.port || options.port);
console.error('barnacles-webhook HTTPS POST error code:', error.code,
'on', target);
'on', target);
}
});

Expand Down