diff --git a/lib/barnacleswebhook.js b/lib/barnacleswebhook.js index aaad9fa..ef297b8 100644 --- a/lib/barnacleswebhook.js +++ b/lib/barnacleswebhook.js @@ -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; /** @@ -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 { @@ -61,7 +63,7 @@ class BarnaclesWebhook { handleEvent(name, data) { let self = this - switch(name) { + switch (name) { case 'raddec': return handleRaddec(self, data); } @@ -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 @@ -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); } } @@ -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); } }); @@ -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); } });