🐞 fix(web-server): 修改proxy解决mid-web内存泄漏
This commit is contained in:
@@ -3,8 +3,10 @@ cd ./game-server && npm install -d
|
||||
echo '============ game-server npm installed ============'
|
||||
cd ..
|
||||
cd ./web-server && npm install -d
|
||||
sed -i.bak 's/this.serverOnCloseSubscribed = false/this.serverOnCloseSubscribed = true/g' ./node_modules/http-proxy-middleware/dist/http-proxy-middleware.js
|
||||
echo '============ web-server npm installed ============'
|
||||
cd ..
|
||||
cd ./gm-server && npm install -d
|
||||
sed -i.bak 's/this.serverOnCloseSubscribed = false/this.serverOnCloseSubscribed = true/g' ./node_modules/http-proxy-middleware/dist/http-proxy-middleware.js
|
||||
echo '============ gm-server npm installed ============'
|
||||
cd ..
|
||||
|
||||
50
web-server/app/middleware/egg-proxy.ts
Normal file
50
web-server/app/middleware/egg-proxy.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import c2k = require('koa-connect');
|
||||
import { createProxyMiddleware, Options as ContextOptions } from 'http-proxy-middleware';
|
||||
import * as micromatch from 'micromatch';
|
||||
import * as isGlob from 'is-glob';
|
||||
|
||||
function match(context: string, path: string): boolean {
|
||||
// single path
|
||||
if (!isGlob(context)) {
|
||||
return path.indexOf(context) === 0;
|
||||
} else {
|
||||
// single glob path
|
||||
const matches = micromatch([path], context);
|
||||
return matches?.length > 0;
|
||||
}
|
||||
}
|
||||
|
||||
export interface Options {
|
||||
[contextName: string]: ContextOptions;
|
||||
}
|
||||
|
||||
export default function (options: Options) {
|
||||
return async (ctx, next) => {
|
||||
for (const context of Object.keys(options)) {
|
||||
if (match(context, ctx.path)) {
|
||||
const contextOptions: ContextOptions = options[context];
|
||||
const { onProxyReq } = contextOptions;
|
||||
await c2k(
|
||||
createProxyMiddleware(context, {
|
||||
...contextOptions,
|
||||
onProxyReq(proxyReq, req, res) {
|
||||
if (onProxyReq && typeof onProxyReq === 'function') {
|
||||
onProxyReq(proxyReq, req, res);
|
||||
}
|
||||
// reset rawBody after bodyparser
|
||||
const { rawBody, body: requestBody } = ctx.request;
|
||||
if (requestBody && rawBody) {
|
||||
proxyReq.setHeader('Content-Length', Buffer.byteLength(rawBody));
|
||||
proxyReq.write(rawBody);
|
||||
proxyReq.end();
|
||||
}
|
||||
return proxyReq;
|
||||
},
|
||||
}) as any
|
||||
)(ctx, next);
|
||||
}
|
||||
}
|
||||
await next();
|
||||
};
|
||||
}
|
||||
|
||||
2
web-server/typings/app/middleware/index.d.ts
vendored
2
web-server/typings/app/middleware/index.d.ts
vendored
@@ -3,6 +3,7 @@
|
||||
|
||||
import 'egg';
|
||||
import ExportCheckMainten from '../../../app/middleware/checkMainten';
|
||||
import ExportEggProxy from '../../../app/middleware/egg-proxy';
|
||||
import ExportGetIp from '../../../app/middleware/getIp';
|
||||
import ExportGmTokenParser from '../../../app/middleware/gmTokenParser';
|
||||
import ExportParmsDecode from '../../../app/middleware/parmsDecode';
|
||||
@@ -12,6 +13,7 @@ import ExportTokenParser from '../../../app/middleware/tokenParser';
|
||||
declare module 'egg' {
|
||||
interface IMiddleware {
|
||||
checkMainten: typeof ExportCheckMainten;
|
||||
eggProxy: typeof ExportEggProxy;
|
||||
getIp: typeof ExportGetIp;
|
||||
gmTokenParser: typeof ExportGmTokenParser;
|
||||
parmsDecode: typeof ExportParmsDecode;
|
||||
|
||||
Reference in New Issue
Block a user