Skip to content

Commit c5bcd1c

Browse files
authored
Merge pull request #65 from johannbrynjar/FixMemoryLeak
Create proxy instance for each request
2 parents 06d79fe + b4f70d7 commit c5bcd1c

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Next.js HTTP Proxy Middleware
22
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
3-
[![All Contributors](https://img.shields.io/badge/all_contributors-13-orange.svg?style=flat-square)](#contributors-)
3+
[![All Contributors](https://img.shields.io/badge/all_contributors-12-orange.svg?style=flat-square)](#contributors-)
44
<!-- ALL-CONTRIBUTORS-BADGE:END -->
55

66
HTTP Proxy middleware available in API Middleware provided by Next.js.

src/index.ts

+12-11
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ export interface NextHttpProxyMiddlewareOptions extends ServerOptions {
77
}
88

99
/**
10-
* @see https://www.npmjs.com/package/http-proxy
10+
* Please refer to the following links for the specification document for HTTP.
11+
* @see https://tools.ietf.org/html/rfc7231
12+
* @see https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol
1113
*/
12-
const proxy: httpProxy = httpProxy.createProxy();
14+
const hasRequestBodyMethods: string[] = ["HEAD", "POST", "PUT", "DELETE", "CONNECT", "OPTIONS", "PATCH"];
1315

1416
/**
1517
* If pattern information matching the input url information is found in the `pathRewrite` array,
@@ -22,7 +24,7 @@ export const rewritePath = (
2224
pathRewrite: NextHttpProxyMiddlewareOptions['pathRewrite']
2325
) => {
2426
if(Array.isArray(pathRewrite)){
25-
for (let item of pathRewrite) {
27+
for (const item of pathRewrite) {
2628
const {
2729
patternStr,
2830
replaceStr
@@ -35,7 +37,7 @@ export const rewritePath = (
3537
} else {
3638
console.warn('[next-http-proxy-middleware] Use array instead of object for \`pathRewrite\` value '
3739
+ '(related issue: https://github.com./stegano/next-http-proxy-middleware/issues/39)');
38-
for (let patternStr in pathRewrite) {
40+
for (const patternStr in pathRewrite) {
3941
const pattern = RegExp(patternStr);
4042
const path = pathRewrite[patternStr];
4143
if (pattern.test(url as string)) {
@@ -61,20 +63,19 @@ const httpProxyMiddleware = async (
6163
new Promise((resolve, reject) => {
6264
const { pathRewrite, onProxyInit, ...serverOptions } = httpProxyOptions;
6365

66+
/**
67+
* @see https://www.npmjs.com/package/http-proxy
68+
*/
69+
const proxy: httpProxy = httpProxy.createProxy();
70+
6471
if(typeof onProxyInit === 'function') {
6572
onProxyInit(proxy);
6673
}
6774

6875
if (pathRewrite) {
6976
req.url = rewritePath(req.url as string, pathRewrite);
7077
}
71-
72-
/**
73-
* Please refer to the following links for the specification document for HTTP.
74-
* @see https://tools.ietf.org/html/rfc7231
75-
* @see https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol
76-
*/
77-
const hasRequestBodyMethods: string[] = ["HEAD", "POST", "PUT", "DELETE", "CONNECT", "OPTIONS", "PATCH"];
78+
7879
if (hasRequestBodyMethods.indexOf(req.method as string) >= 0 && typeof req.body === "object") {
7980
req.body = JSON.stringify(req.body);
8081
}

0 commit comments

Comments
 (0)