fix(#2367): handle response body decode (#2371)

This commit is contained in:
lohit 2024-05-30 23:24:04 +05:30 committed by GitHub
parent 470d162fb6
commit 3ded960938
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 15 additions and 2 deletions

View File

@ -41,6 +41,7 @@
"graphql": "^16.6.0",
"http-proxy-agent": "^7.0.0",
"https-proxy-agent": "^7.0.2",
"iconv-lite": "^0.6.3",
"is-valid-path": "^0.1.1",
"js-yaml": "^4.1.0",
"json-bigint": "^1.0.0",

View File

@ -37,6 +37,7 @@ const {
transformPasswordCredentialsRequest
} = require('./oauth2-helper');
const Oauth2Store = require('../../store/oauth2');
const iconv = require('iconv-lite');
// override the default escape function to prevent escaping
Mustache.escape = function (value) {
@ -258,13 +259,18 @@ const configureRequest = async (
};
const parseDataFromResponse = (response) => {
const dataBuffer = Buffer.from(response.data);
// Parse the charset from content type: https://stackoverflow.com/a/33192813
const charsetMatch = /charset=([^()<>@,;:"/[\]?.=\s]*)/i.exec(response.headers['content-type'] || '');
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec#using_exec_with_regexp_literals
const charsetValue = charsetMatch?.[1];
const dataBuffer = Buffer.from(response.data);
// Overwrite the original data for backwards compatibility
let data = dataBuffer.toString(charsetValue || 'utf-8');
let data;
if (iconv.encodingExists(charsetValue)) {
data = iconv.decode(dataBuffer, charsetValue);
} else {
data = iconv.decode(dataBuffer, 'utf-8');
}
// Try to parse response to JSON, this can quietly fail
try {
// Filter out ZWNBSP character

View File

@ -31,4 +31,10 @@ router.get('/bom-json-test', (req, res) => {
return res.send(jsonWithBom);
});
router.get('/iso-enc', (req, res) => {
res.set('Content-Type', 'text/plain; charset=ISO-8859-1');
const responseText = 'éçà';
return res.send(Buffer.from(responseText, 'latin1'));
});
module.exports = router;