Skip to content

Commit

Permalink
Merge pull request #68 from Stwissel/develop
Browse files Browse the repository at this point in the history
Make header check case insensitive
  • Loading branch information
Stwissel authored Jan 22, 2023
2 parents 65be07d + 5f202c4 commit 7e0b5b1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
18 changes: 17 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,8 @@ Connection.prototype._queryHandler = function (data) {
const result = new Promise((resolve, reject) => {
// Separate function definition
// since it might get called recursive
function handleResponse(resp) {
function handleResponse(respCandidate) {
let resp = respToJson(respCandidate);
if (resp.records && resp.records.length > 0) {
_.each(resp.records, function (r) {
if (opts.raw) {
Expand Down Expand Up @@ -650,6 +651,21 @@ Connection.prototype._queryHandler = function (data) {
return result;
};

/**
* If it hasn't been discovered on the header
* try to convert it to object here
*/
const respToJson = (respCandidate) => {
if (typeof respCandidate === 'object') {
return respCandidate;
}
try {
return JSON.parse(respCandidate);
} catch (e) {
console.error(e, respCandidate);
}
};

/*****************************
* search
*****************************/
Expand Down
14 changes: 10 additions & 4 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
/* Checks if a header exists (case insensitive) and contains a value */
const checkHeaderCaseInsensitive = (headers, key, searchfor) => {
const lower = key.toLowerCase;
const headerContent =
headers[Object.keys(headers).find((k) => k.toLowerCase() === lower)];
return headerContent ? headerContent.includes(searchfor) : false;
};

const isJsonResponse = (res) => {
return (
res.headers &&
res.headers['content-type'] &&
res.headers['content-type'].toLowerCase().indexOf('application/json') > -1
checkHeaderCaseInsensitive(res.headers, 'content-type', 'application/json')
);
};

const isChunkedEncoding = (res) => {
return (
res.headers &&
res.headers['transfer-encoding'] &&
res.headers['transfer-encoding'].toLowerCase() === 'chunked'
checkHeaderCaseInsensitive(res.headers, 'transfer-encoding', 'chunked')
);
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "nforce8",
"description": "Forked from nforce by Kevin O'Hara <[email protected]> (http://kevinmohara.com) for use in NodeRED",
"version": "2.1.0",
"version": "2.1.1",
"author": "Stephan H. Wissel <[email protected]> (https://wissel.net)",
"contributors": [
{
Expand Down

0 comments on commit 7e0b5b1

Please sign in to comment.