json-proxy-middleware
Advanced tools
Comparing version 0.0.5 to 0.0.6
@@ -6,2 +6,3 @@ import bunyan from 'bunyan'; | ||
export declare type HeaderOption = object | ((req: Request, res: Response) => request.Headers); | ||
export declare type AddCurlHeader = boolean | ((req: Request, res: Response) => boolean); | ||
export declare type LoggerOption = bunyan | Console | { | ||
@@ -16,4 +17,5 @@ info(message?: any, ...optionalParams: any[]): void; | ||
urlHost: UrlHost; | ||
addCurlHeader?: AddCurlHeader; | ||
} | ||
declare const _default: (options: ProxyMiddlewareOptions) => RequestHandler; | ||
export default _default; |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
var __assign = (this && this.__assign) || function () { | ||
@@ -23,2 +23,5 @@ __assign = Object.assign || function(t) { | ||
var MS_PER_NS = 1e6; | ||
// Node's max header size is 80KB. We conservatively cap at 20KB. | ||
// https://github.com/nodejs/node/blob/8b4af64f50c5e41ce0155716f294c24ccdecad03/deps/http_parser/http_parser.h#L63 | ||
var MAX_HEADER_SIZE = 20 * 1024; | ||
// by default, we intend to proxy only json responses | ||
@@ -29,2 +32,10 @@ var defaultHeaders = { | ||
}; | ||
var createCurlRequest = function (requestOptions) { | ||
var requestHeaders = (requestOptions.headers || {}); | ||
var headers = Object.keys((requestOptions.headers || {})).reduce(function (headersString, headerKey) { | ||
return headersString + "-H '" + headerKey + ": " + requestHeaders[headerKey] + "' "; | ||
}, ''); | ||
var requestBody = JSON.stringify(requestOptions.body || {}); | ||
return "'" + requestOptions.url + "' -X " + requestOptions.method + " " + headers + " -d " + requestBody; | ||
}; | ||
// This middleware proxies requests through Node to a backend service. | ||
@@ -35,7 +46,6 @@ // You _must_ register bodyParser.json() before mounting this middleware. Also, | ||
exports.default = (function (options) { return function (req, res, next) { | ||
var logger = options.logger, additionalLogMessage = options.additionalLogMessage, headers = options.headers, urlHost = options.urlHost; | ||
var _a = req, originalUrl = _a.originalUrl, baseUrl = _a.baseUrl, agentOptions = _a.agentOptions; | ||
var urlPath = originalUrl.replace(baseUrl, ''); | ||
var logger = options.logger, additionalLogMessage = options.additionalLogMessage, _a = options.addCurlHeader, addCurlHeader = _a === void 0 ? false : _a; | ||
var canLogError = logger && logger.error && typeof logger.error === 'function'; | ||
var host = typeof urlHost === 'function' ? urlHost(req, res) : urlHost; | ||
var host = typeof options.urlHost === 'function' ? options.urlHost(req, res) : options.urlHost; | ||
var urlPath = req.originalUrl.replace(req.baseUrl, ''); | ||
if (typeof host !== 'string') { | ||
@@ -66,6 +76,4 @@ if (canLogError) { | ||
} | ||
var headersToUse = typeof headers === 'function' ? headers(req, res) : headers || {}; | ||
var fullHeaders = __assign({}, defaultHeaders, headersToUse); | ||
var requestOptions = __assign({}, agentOptions, { headers: fullHeaders, method: req.method, url: "" + host + urlPath, body: JSON.stringify(req.body) }); | ||
var canLogInfo = logger && logger.info && typeof logger.info === 'function'; | ||
var headers = __assign({}, defaultHeaders, (typeof options.headers === 'function' ? options.headers(req, res) : options.headers || {})); | ||
if (canLogInfo) { | ||
@@ -79,3 +87,3 @@ var fullMsg = 'Proxy start.'; | ||
urlPath: urlPath, | ||
headers: fullHeaders, | ||
headers: headers, | ||
url: "" + host + urlPath, | ||
@@ -86,3 +94,13 @@ body: util_1.inspect(req.body, { maxArrayLength: 20 }), | ||
var startTime = process.hrtime(); | ||
var requestOptions = __assign({}, req.agentOptions, { method: req.method, headers: headers, url: "" + host + urlPath, body: JSON.stringify(req.body) }); | ||
var requestStream = request_1.default(requestOptions); | ||
// If desired, set the curl header in the response headers so the client can surface it for | ||
// debugging purposes. | ||
var shouldAddCurlHeader = addCurlHeader && typeof addCurlHeader === 'function' | ||
? addCurlHeader(req, res) | ||
: addCurlHeader; | ||
var curlCommand = shouldAddCurlHeader && createCurlRequest(requestOptions); | ||
if (curlCommand && curlCommand.length < MAX_HEADER_SIZE) { | ||
res.setHeader('x-curl-command', curlCommand); | ||
} | ||
requestStream.on('error', function (err) { | ||
@@ -89,0 +107,0 @@ if (canLogError) { |
{ | ||
"name": "json-proxy-middleware", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "Simple express.js friendly json proxy middleware utility", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21407
185