From b0af9c7008cd75c9ee6de9adc669fe551fe5589b Mon Sep 17 00:00:00 2001 From: dg Date: Thu, 28 Jul 2022 20:54:05 +0000 Subject: [PATCH 1/2] fixed comment --- lib/barnacleswebhook.js | 97 +++++++++++++++++++++++++++++------------ 1 file changed, 70 insertions(+), 27 deletions(-) diff --git a/lib/barnacleswebhook.js b/lib/barnacleswebhook.js index 8441ec9..9090e20 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,27 +78,27 @@ class BarnaclesWebhook { */ function handleRaddec(instance, raddec) { let data = JSON.stringify(raddec); - + const DEFAULT_HEADERS = { - 'Content-Type': 'application/json', - 'Content-Length': data.length, + 'Content-Type': 'application/json', + 'Content-Length': data.length, } let options = { - hostname: instance.hostname, - port: instance.port, - path: instance.path, - agent: instance.agent, - method: 'POST', - headers: Object.assign({},DEFAULT_HEADERS,instance.customHeaders) - + hostname: instance.hostname, + port: instance.port, + path: instance.path, + agent: instance.agent, + method: 'POST', + headers: Object.assign({}, DEFAULT_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); } } @@ -106,16 +108,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 and headers 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); } }); @@ -129,16 +152,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 and headers 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); } }); From 34fd120c1622c5584be7d3366c93d8980c54d04f Mon Sep 17 00:00:00 2001 From: dg Date: Thu, 28 Jul 2022 20:59:33 +0000 Subject: [PATCH 2/2] comment --- lib/barnacleswebhook.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/barnacleswebhook.js b/lib/barnacleswebhook.js index 9090e20..9987e7e 100644 --- a/lib/barnacleswebhook.js +++ b/lib/barnacleswebhook.js @@ -108,7 +108,7 @@ 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 and headers be printed to console? + * @param {boolean} verbose Should connection status, headers, and body be printed to console? */ function postViaHttp(data, options, printErrors, verbose) { @@ -152,7 +152,7 @@ function postViaHttp(data, options, printErrors, verbose) { * @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 and headers be printed to console? + * @param {boolean} verbose Should connection status, headers, and body be printed to console? */ function postViaHttps(data, options, printErrors, verbose) { let req