Repository URL to install this package:
|
Version:
1.2.11 ▾
|
"use strict";
/* https://raw.githubusercontent.com/langjt/node-http-proxy-json/master/index.js */
/* eslint-disable max-statements */
/* eslint-disable no-negated-condition */
/* eslint-disable max-params */
const zlib = require('zlib');
const concatStream = require('concat-stream');
const _require = require('exotic'),
isString = _require.isString,
isFunction = _require.isFunction;
const BufferHelper = require("./BufferHelper");
/**
* Modify the response of json
* @param res {Response} The http response
* @param proxyRes {proxyRes|String} String: The http header content-encoding: gzip/deflate
* @param callback {Function} Custom modified logic
*/
function modifyResponse(res, proxyRes, callback) {
const hasHeaders = proxyRes && proxyRes.headers;
const contentEncoding = hasHeaders && proxyRes.headers['content-encoding'];
const hasFormData = hasHeaders && proxyRes.headers['content-type'] === 'application/x-www-form-urlencoded';
if (hasHeaders) {
// Delete the content-length if it exists. Otherwise, an exception will occur
// @see: https://github.com/langjt/node-http-proxy-json/issues/10
if ('content-length' in proxyRes.headers) {
delete proxyRes.headers['content-length'];
}
} // === to zip ===
let unzip;
let zip; // Now only deal with the gzip/deflate/undefined content-encoding.
switch (contentEncoding) {
case 'gzip':
unzip = zlib.Gunzip();
zip = zlib.Gzip();
break;
case 'deflate':
unzip = zlib.Inflate();
zip = zlib.Deflate();
break;
} // ===
// The cache response method can be called after the modification.
let _write = res.write;
let _end = res.end; // typeof unzip?
if (unzip) {
unzip.on('error', function (unzipError) {
console.log('Unzip error: ', unzipError);
_end.call(res);
});
handleCompressed(res, _write, _end, unzip, zip, callback);
} else if (!contentEncoding) {
handleUncompressed(res, _write, _end, callback);
} else {
console.log('Not supported content-encoding: ' + contentEncoding);
}
}
/**
* handle compressed
*/
function handleCompressed(res, _write, _end, unzip, zip, callback) {
// The rewrite response method is replaced by unzip stream.
res.write = data => unzip.write(data);
res.end = () => unzip.end(); // Concat the unzip stream.
let concatWrite = concatStream(data => {
const dataString = data.toString();
let body;
try {
body = JSON.parse(dataString);
} catch (parseException) {
body = dataString;
console.log('JSON.parse error:', parseException);
} // Custom modified logic
if (isFunction(callback)) {
body = callback(body);
}
let finish = _body => {
// Converts the JSON to buffer.
const bodyString = JSON.stringify(_body); // let body = new Buffer(bodyString)
let body = Buffer.from(bodyString); // Call the response method and recover the content-encoding.
zip.on('data', chunk => _write.call(res, chunk));
zip.on('end', () => _end.call(res));
zip.write(body);
zip.end();
};
if (body && body.then) {
body.then(finish);
} else {
finish(body);
}
});
unzip.pipe(concatWrite);
}
/**
* handle Uncompressed
*/
function handleUncompressed(res, _write, _end, callback) {
let buffer = new BufferHelper(); // Rewrite response method and get the content.
res.write = data => buffer.concat(data);
res.end = () => {
let body;
const bodyString = buffer.toBuffer().toString();
try {
body = JSON.parse(bodyString);
} catch (parseException) {
body = bodyString;
console.log('JSON.parse error:', parseException);
console.log('unparsable body:', bodyString);
} // Custom modified logic
if (isFunction(callback)) {
body = callback(body);
}
let finish = _body => {
// Converts the JSON to buffer.
const bodyStringified = JSON.stringify(_body); // console.log('____fin___', {
// bodyStringified,
// _body,
// })
// @todo - not sure if they have some hack with var here (nope)
// let body = new Buffer(bodyStringified)
let body = Buffer.from(bodyStringified); // Call the response method
_write.call(res, body);
_end.call(res);
}; // isAsyncish(body)
if (body && body.then) {
body.then(finish);
} else {
finish(body);
}
};
}
module.exports = modifyResponse;