http-proxy-middleware
Advanced tools
Comparing version 1.0.6 to 1.1.0
# Changelog | ||
## [v1.1.0](https://github.com/chimurai/http-proxy-middleware/releases/tag/v1.1.0) | ||
- fix(errorHandler): fix confusing error message ([#509](https://github.com/chimurai/http-proxy-middleware/pull/509)) | ||
- fix(proxy): close proxy when server closes ([#508](https://github.com/chimurai/http-proxy-middleware/pull/508)) | ||
- refactor(lodash): remove lodash ([#459](https://github.com/chimurai/http-proxy-middleware/pull/459)) ([#507](https://github.com/chimurai/http-proxy-middleware/pull/507)) ([TrySound](https://github.com/TrySound)) | ||
- fix(ETIMEDOUT): return 504 on ETIMEDOUT ([#480](https://github.com/chimurai/http-proxy-middleware/pull/480)) ([aremishevsky](https://github.com/aremishevsky)) | ||
## [v1.0.6](https://github.com/chimurai/http-proxy-middleware/releases/tag/v1.0.6) | ||
@@ -4,0 +11,0 @@ |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createConfig = void 0; | ||
const _ = require("lodash"); | ||
const isPlainObj = require("is-plain-obj"); | ||
const url = require("url"); | ||
@@ -18,3 +18,3 @@ const errors_1 = require("./errors"); | ||
config.context = '/'; | ||
config.options = _.assign(config.options, context); | ||
config.options = Object.assign(config.options, context); | ||
// app.use('/api', proxy('http://localhost:9000')); | ||
@@ -27,3 +27,3 @@ // app.use(proxy('http://localhost:9000/api')); | ||
config.context = oUrl.pathname || '/'; | ||
config.options = _.assign(config.options, { target }, opts); | ||
config.options = Object.assign(config.options, { target }, opts); | ||
if (oUrl.protocol === 'ws:' || oUrl.protocol === 'wss:') { | ||
@@ -36,3 +36,3 @@ config.options.ws = true; | ||
config.context = context; | ||
config.options = _.assign(config.options, opts); | ||
config.options = Object.assign(config.options, opts); | ||
} | ||
@@ -58,3 +58,3 @@ configureLogger(config.options); | ||
function isStringShortHand(context) { | ||
if (_.isString(context)) { | ||
if (typeof context === 'string') { | ||
return !!url.parse(context).host; | ||
@@ -75,3 +75,3 @@ } | ||
function isContextless(context, opts) { | ||
return _.isPlainObject(context) && _.isEmpty(opts); | ||
return isPlainObj(context) && (opts == null || Object.keys(opts).length === 0); | ||
} | ||
@@ -78,0 +78,0 @@ function configureLogger(options) { |
@@ -5,3 +5,2 @@ "use strict"; | ||
const isGlob = require("is-glob"); | ||
const _ = require("lodash"); | ||
const micromatch = require("micromatch"); | ||
@@ -30,3 +29,3 @@ const url = require("url"); | ||
// custom matching | ||
if (_.isFunction(context)) { | ||
if (typeof context === 'function') { | ||
const pathname = getUrlPathName(uri); | ||
@@ -80,3 +79,3 @@ return context(pathname, req); | ||
function isStringPath(context) { | ||
return _.isString(context) && !isGlob(context); | ||
return typeof context === 'string' && !isGlob(context); | ||
} | ||
@@ -83,0 +82,0 @@ function isGlobPath(context) { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getHandlers = exports.init = void 0; | ||
const _ = require("lodash"); | ||
const camelcase = require("camelcase"); | ||
const logger_1 = require("./logger"); | ||
@@ -23,5 +23,5 @@ const logger = logger_1.getInstance(); | ||
// and add them to the handlers object for subscription in init(). | ||
const eventName = _.camelCase('on ' + event); | ||
const fnHandler = _.get(options, eventName); | ||
if (_.isFunction(fnHandler)) { | ||
const eventName = camelcase('on ' + event); | ||
const fnHandler = options ? options[eventName] : null; | ||
if (typeof fnHandler === 'function') { | ||
handlers[event] = fnHandler; | ||
@@ -31,7 +31,7 @@ } | ||
// add default error handler in absence of error handler | ||
if (!_.isFunction(handlers.error)) { | ||
if (typeof handlers.error !== 'function') { | ||
handlers.error = defaultErrorHandler; | ||
} | ||
// add default close handler in absence of close handler | ||
if (!_.isFunction(handlers.close)) { | ||
if (typeof handlers.close !== 'function') { | ||
handlers.close = logClose; | ||
@@ -54,2 +54,3 @@ } | ||
case 'ECONNREFUSED': | ||
case 'ETIMEDOUT': | ||
res.writeHead(504); | ||
@@ -62,3 +63,3 @@ break; | ||
} | ||
res.end('Error occured while trying to proxy to: ' + host + req.url); | ||
res.end(`Error occured while trying to proxy: ${host}${req.url}`); | ||
} | ||
@@ -65,0 +66,0 @@ function logClose(req, socket, head) { |
@@ -6,2 +6,3 @@ import { Filter, RequestHandler, Options } from './types'; | ||
private wsInternalSubscribed; | ||
private serverOnCloseSubscribed; | ||
private proxyOptions; | ||
@@ -8,0 +9,0 @@ private proxy; |
@@ -14,3 +14,2 @@ "use strict"; | ||
const httpProxy = require("http-proxy"); | ||
const _ = require("lodash"); | ||
const config_factory_1 = require("./config-factory"); | ||
@@ -26,4 +25,6 @@ const contextMatcher = require("./context-matcher"); | ||
this.wsInternalSubscribed = false; | ||
this.serverOnCloseSubscribed = false; | ||
// https://github.com/Microsoft/TypeScript/wiki/'this'-in-TypeScript#red-flags-for-this | ||
this.middleware = (req, res, next) => __awaiter(this, void 0, void 0, function* () { | ||
var _a, _b; | ||
if (this.shouldProxy(this.config.context, req)) { | ||
@@ -41,5 +42,21 @@ try { | ||
} | ||
/** | ||
* Get the server object to subscribe to server events; | ||
* 'upgrade' for websocket and 'close' for graceful shutdown | ||
* | ||
* NOTE: | ||
* req.socket: node >= 13 | ||
* req.connection: node < 13 (Remove this when node 12/13 support is dropped) | ||
*/ | ||
const server = (_b = ((_a = req.socket) !== null && _a !== void 0 ? _a : req.connection)) === null || _b === void 0 ? void 0 : _b.server; | ||
if (server && !this.serverOnCloseSubscribed) { | ||
server.on('close', () => { | ||
this.logger.info('[HPM] server close signal received: closing proxy server'); | ||
this.proxy.close(); | ||
}); | ||
this.serverOnCloseSubscribed = true; | ||
} | ||
if (this.proxyOptions.ws === true) { | ||
// use initial request to access the server object to subscribe to http upgrade event | ||
this.catchUpgradeRequest(req.connection.server); | ||
this.catchUpgradeRequest(server); | ||
} | ||
@@ -88,3 +105,3 @@ }); | ||
const originalPath = req.url; | ||
const newProxyOptions = _.assign({}, this.proxyOptions); | ||
const newProxyOptions = Object.assign({}, this.proxyOptions); | ||
// Apply in order: | ||
@@ -91,0 +108,0 @@ // 1. option.router |
"use strict"; | ||
/* eslint-disable prefer-rest-params */ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getArrow = exports.getInstance = void 0; | ||
const _ = require("lodash"); | ||
const util = require("util"); | ||
@@ -72,3 +72,3 @@ let loggerInstance; | ||
const result = true; | ||
if (fnProvider && !_.isFunction(fnProvider)) { | ||
if (fnProvider && typeof fnProvider !== 'function') { | ||
throw new Error('[HPM] Log provider config error. Expecting a function.'); | ||
@@ -101,5 +101,4 @@ } | ||
// make it possible for additional log data, such date/time or custom prefix. | ||
_interpolate() { | ||
const fn = _.spread(util.format); | ||
const result = fn(_.slice(arguments)); | ||
_interpolate(format, ...args) { | ||
const result = util.format(format, ...args); | ||
return result; | ||
@@ -106,0 +105,0 @@ } |
@@ -7,2 +7,2 @@ /** | ||
*/ | ||
export declare function createPathRewriter(rewriteConfig: any): (...args: any[]) => any; | ||
export declare function createPathRewriter(rewriteConfig: any): any; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createPathRewriter = void 0; | ||
const _ = require("lodash"); | ||
const isPlainObj = require("is-plain-obj"); | ||
const errors_1 = require("./errors"); | ||
@@ -19,3 +19,3 @@ const logger_1 = require("./logger"); | ||
} | ||
if (_.isFunction(rewriteConfig)) { | ||
if (typeof rewriteConfig === 'function') { | ||
const customRewriteFn = rewriteConfig; | ||
@@ -30,9 +30,9 @@ return customRewriteFn; | ||
let result = path; | ||
_.forEach(rulesCache, (rule) => { | ||
for (const rule of rulesCache) { | ||
if (rule.regex.test(path)) { | ||
result = result.replace(rule.regex, rule.value); | ||
logger.debug('[HPM] Rewriting path from "%s" to "%s"', path, result); | ||
return false; | ||
break; | ||
} | ||
}); | ||
} | ||
return result; | ||
@@ -43,11 +43,9 @@ } | ||
function isValidRewriteConfig(rewriteConfig) { | ||
if (_.isFunction(rewriteConfig)) { | ||
if (typeof rewriteConfig === 'function') { | ||
return true; | ||
} | ||
else if (!_.isEmpty(rewriteConfig) && _.isPlainObject(rewriteConfig)) { | ||
return true; | ||
else if (isPlainObj(rewriteConfig)) { | ||
return Object.keys(rewriteConfig).length !== 0; | ||
} | ||
else if (_.isUndefined(rewriteConfig) || | ||
_.isNull(rewriteConfig) || | ||
_.isEqual(rewriteConfig, {})) { | ||
else if (rewriteConfig === undefined || rewriteConfig === null) { | ||
return false; | ||
@@ -61,4 +59,4 @@ } | ||
const rules = []; | ||
if (_.isPlainObject(rewriteConfig)) { | ||
_.forIn(rewriteConfig, (value, key) => { | ||
if (isPlainObj(rewriteConfig)) { | ||
for (const [key] of Object.entries(rewriteConfig)) { | ||
rules.push({ | ||
@@ -69,5 +67,5 @@ regex: new RegExp(key), | ||
logger.info('[HPM] Proxy rewrite rule created: "%s" ~> "%s"', key, rewriteConfig[key]); | ||
}); | ||
} | ||
} | ||
return rules; | ||
} |
@@ -13,3 +13,3 @@ "use strict"; | ||
exports.getTarget = void 0; | ||
const _ = require("lodash"); | ||
const isPlainObj = require("is-plain-obj"); | ||
const logger_1 = require("./logger"); | ||
@@ -21,6 +21,6 @@ const logger = logger_1.getInstance(); | ||
const router = config.router; | ||
if (_.isPlainObject(router)) { | ||
if (isPlainObj(router)) { | ||
newTarget = getTargetFromProxyTable(req, router); | ||
} | ||
else if (_.isFunction(router)) { | ||
else if (typeof router === 'function') { | ||
newTarget = yield router(req); | ||
@@ -37,3 +37,3 @@ } | ||
const hostAndPath = host + path; | ||
_.forIn(table, (value, key) => { | ||
for (const [key] of Object.entries(table)) { | ||
if (containsPath(key)) { | ||
@@ -44,3 +44,3 @@ if (hostAndPath.indexOf(key) > -1) { | ||
logger.debug('[HPM] Router table match: "%s"', key); | ||
return false; | ||
break; | ||
} | ||
@@ -53,6 +53,6 @@ } | ||
logger.debug('[HPM] Router table match: "%s"', host); | ||
return false; | ||
break; | ||
} | ||
} | ||
}); | ||
} | ||
return result; | ||
@@ -59,0 +59,0 @@ } |
"use strict"; | ||
/* eslint-disable @typescript-eslint/no-empty-interface */ | ||
Object.defineProperty(exports, "__esModule", { value: true }); |
{ | ||
"name": "http-proxy-middleware", | ||
"version": "1.0.6", | ||
"version": "1.1.0", | ||
"description": "The one-liner node.js proxy middleware for connect, express and browser-sync", | ||
@@ -12,6 +12,8 @@ "main": "dist/index.js", | ||
"clean": "rm -rf dist && rm -rf coverage", | ||
"lint": "yarn lint:prettier && yarn lint:tslint", | ||
"lint:prettier": "prettier --check \"**/*.{js,ts,md}\"", | ||
"lint:tslint": "yarn tslint -c tslint.json '{lib,test}/**/*.ts'", | ||
"lint:fix": "prettier --write \"**/*.{js,ts,md}\"", | ||
"lint": "yarn prettier && yarn eslint", | ||
"lint:fix": "yarn prettier:fix && yarn eslint:fix", | ||
"eslint": "eslint '{src,test}/**/*.ts'", | ||
"eslint:fix": "yarn eslint --fix", | ||
"prettier": "prettier --list-different \"**/*.{js,ts,md,yml,json,html}\"", | ||
"prettier:fix": "prettier --write \"**/*.{js,ts,md,yml,json,html}\"", | ||
"build": "tsc", | ||
@@ -50,32 +52,36 @@ "pretest": "yarn build", | ||
"devDependencies": { | ||
"@commitlint/cli": "^11.0.0", | ||
"@commitlint/config-conventional": "^11.0.0", | ||
"@commitlint/cli": "^12.0.1", | ||
"@commitlint/config-conventional": "^12.0.1", | ||
"@types/express": "^4.17.3", | ||
"@types/is-glob": "^4.0.1", | ||
"@types/jest": "^26.0.14", | ||
"@types/lodash": "^4.14.162", | ||
"@types/jest": "^26.0.22", | ||
"@types/micromatch": "^4.0.1", | ||
"@types/node": "^14.11.8", | ||
"@types/node": "^14.14.37", | ||
"@types/supertest": "^2.0.10", | ||
"browser-sync": "^2.26.12", | ||
"@types/ws": "^7.4.0", | ||
"@typescript-eslint/eslint-plugin": "^4.19.0", | ||
"@typescript-eslint/parser": "^4.19.0", | ||
"browser-sync": "^2.26.14", | ||
"connect": "^3.7.0", | ||
"eslint": "^7.23.0", | ||
"eslint-config-prettier": "^8.1.0", | ||
"eslint-plugin-prettier": "^3.3.1", | ||
"express": "^4.17.1", | ||
"husky": "^4.3.0", | ||
"jest": "^26.5.3", | ||
"lint-staged": "^10.4.0", | ||
"mockttp": "^1.0.2", | ||
"open": "^7.3.0", | ||
"prettier": "^2.1.2", | ||
"supertest": "^5.0.0", | ||
"ts-jest": "^26.4.1", | ||
"tslint": "^6.1.3", | ||
"tslint-config-prettier": "^1.18.0", | ||
"typescript": "^4.0.3", | ||
"ws": "^7.3.1" | ||
"jest": "^26.6.3", | ||
"lint-staged": "^10.5.4", | ||
"mockttp": "^1.2.0", | ||
"open": "^7.4.2", | ||
"prettier": "^2.2.1", | ||
"supertest": "^6.1.3", | ||
"ts-jest": "^26.5.4", | ||
"typescript": "^4.2.3", | ||
"ws": "^7.4.4" | ||
}, | ||
"dependencies": { | ||
"@types/http-proxy": "^1.17.4", | ||
"@types/http-proxy": "^1.17.5", | ||
"camelcase": "^6.2.0", | ||
"http-proxy": "^1.18.1", | ||
"is-glob": "^4.0.1", | ||
"lodash": "^4.17.20", | ||
"is-plain-obj": "^3.0.0", | ||
"micromatch": "^4.0.2" | ||
@@ -82,0 +88,0 @@ }, |
@@ -543,2 +543,2 @@ # http-proxy-middleware | ||
Copyright (c) 2015-2020 Steven Chim | ||
Copyright (c) 2015-2021 Steven Chim |
63841
782
6
27
+ Addedcamelcase@^6.2.0
+ Addedis-plain-obj@^3.0.0
+ Addedcamelcase@6.3.0(transitive)
+ Addedis-plain-obj@3.0.0(transitive)
- Removedlodash@^4.17.20
- Removedfill-range@7.0.1(transitive)
- Removedis-number@7.0.0(transitive)
- Removedlodash@4.17.21(transitive)
- Removedto-regex-range@5.0.1(transitive)
Updated@types/http-proxy@^1.17.5