Skip to content

Commit ee0d025

Browse files
committed
:update:
1 parent 4bb39ff commit ee0d025

File tree

3 files changed

+68
-73
lines changed

3 files changed

+68
-73
lines changed

index.js

+2-9
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,9 @@ const PROXY = require('./lib/Proxy');
2929
* @param {*} opts
3030
*/
3131

32-
const middleware = function(context, opts) {
33-
const proxy = new PROXY(context, opts);
34-
return proxy;
32+
module.exports = function(context, opts) {
33+
return new PROXY(context, opts);
3534
};
36-
middleware.upgrade = function(context, opts) {
37-
const proxy = new PROXY(context, opts);
38-
return proxy.upgrade;
39-
};
40-
41-
module.exports = middleware
4235

4336
/*
4437

lib/Proxy.js

+16-16
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@ const contextMatcher = require('./context-matcher');
1212
const Router = require('./router');
1313

1414
function PROXY(context, opts) {
15-
let _wsInitialized = false;
15+
// https://github.com/chimurai/http-proxy-middleware/issues/57
16+
const wsUpgradeDebounced = utility.debounce(_handleUpgrade);
17+
let wsInitialized = false;
1618
const config = createConfig(context, opts);
1719
const proxyOptions = config.options;
20+
1821
// create proxy
1922
const proxy = httpProxy.createProxyServer({});
20-
// ws
21-
const upgrade = _initUpgrade();
2223

2324
const pathRewriter = PathRewriter.create(proxyOptions.pathRewrite); // returns undefined when "pathRewrite" is not provided
25+
2426
// attach handler to http-proxy events
2527
handlers.init(proxy, proxyOptions);
2628

@@ -30,12 +32,10 @@ function PROXY(context, opts) {
3032
// 新增加的功能
3133
proxy.on('proxyRes', _handleProxyResBody);
3234

33-
// server.on('upgrade', proxy.upgrade); // <-- directly subscribe to server's upgrade event.
34-
function _initUpgrade() {
35-
// https://github.com/chimurai/http-proxy-middleware/issues/57
36-
const wsUpgradeDebounced = utility.debounce(_handleUpgrade);
37-
return wsUpgradeDebounced;
38-
}
35+
36+
// https://github.com/chimurai/http-proxy-middleware/issues/19
37+
// expose function to upgrade externally
38+
middleware.upgrade = wsUpgradeDebounced;
3939

4040
function _handleLogError(err, req /* res */) {
4141
const hostname = (req.headers && req.headers.host) || (req.hostname || req.host); // (websocket) || (node0.10 || node 4/5)
@@ -101,7 +101,7 @@ function PROXY(context, opts) {
101101
// subscribe once; don't subscribe on every request...
102102
// https://github.com/chimurai/http-proxy-middleware/issues/113
103103
if (!_wsInitialized) {
104-
server.on('upgrade', upgrade);
104+
server.on('upgrade', wsUpgradeDebounced);
105105
_wsInitialized = true;
106106
}
107107
}
@@ -123,6 +123,12 @@ function PROXY(context, opts) {
123123
return newProxyOptions;
124124
}
125125

126+
middleware.upgrade = wsUpgradeDebounced;
127+
128+
logger.system('inited...');
129+
130+
return middleware;
131+
126132
async function middleware(ctx, next) {
127133
const req = ctx.req;
128134
const res = ctx.res;
@@ -153,12 +159,6 @@ function PROXY(context, opts) {
153159

154160
await next();
155161
}
156-
157-
middleware.upgrade = upgrade;
158-
159-
logger.system('inited...');
160-
161-
return middleware;
162162
}
163163

164164
// Modify option.target when router present.

package.json

+50-48
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,52 @@
11
{
2-
"name": "@2o3t/koa2-proxy-middleware",
3-
"version": "0.0.1",
4-
"description": "http proxy middleware for koa2",
5-
"main": "index.js",
6-
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
8-
},
9-
"files": [
10-
"lib"
11-
],
12-
"keywords": [
13-
"http-proxy-middleware",
14-
"koa-proxy-middleware",
15-
"koa2-proxy-middleware",
16-
"middleware",
17-
"proxy",
18-
"koa2",
19-
"reverse",
20-
"http",
21-
"https",
22-
"connect",
23-
"websocket",
24-
"ws",
25-
"cors"
26-
],
27-
"author": "Zyao89 <[email protected]>",
28-
"license": "MIT",
29-
"dependencies": {
30-
"2o3t-logger": "^0.3.9",
31-
"2o3t-utility": "^0.1.2",
32-
"http-proxy": "^1.17.0",
33-
"lodash": "^4.17.11"
34-
},
35-
"devDependencies": {
36-
"eslint-config-2o3t": "^1.1.4",
37-
"koa": "^2.7.0"
38-
},
39-
"engines": {
40-
"node": ">= 8.0.0"
41-
},
42-
"bugs": {
43-
"url": "https://github.com/2o3t/koa2-proxy-middleware/issues"
44-
},
45-
"homepage": "https://github.com/2o3t/koa2-proxy-middleware",
46-
"repository": {
47-
"type": "git",
48-
"url": "https://github.com/2o3t/koa2-proxy-middleware.git"
49-
}
2+
"name": "@2o3t/koa2-proxy-middleware",
3+
"version": "0.0.3",
4+
"description": "http proxy middleware for koa2",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"files": [
10+
"lib"
11+
],
12+
"keywords": [
13+
"koa-proxy",
14+
"koa2-proxy",
15+
"http-proxy-middleware",
16+
"koa-proxy-middleware",
17+
"koa2-proxy-middleware",
18+
"middleware",
19+
"proxy",
20+
"koa2",
21+
"reverse",
22+
"http",
23+
"https",
24+
"connect",
25+
"websocket",
26+
"ws",
27+
"cors"
28+
],
29+
"author": "Zyao89 <[email protected]>",
30+
"license": "MIT",
31+
"dependencies": {
32+
"2o3t-logger": "^0.3.9",
33+
"2o3t-utility": "^0.1.2",
34+
"http-proxy": "^1.17.0",
35+
"lodash": "^4.17.11"
36+
},
37+
"devDependencies": {
38+
"eslint-config-2o3t": "^1.1.4",
39+
"koa": "^2.7.0"
40+
},
41+
"engines": {
42+
"node": ">= 8.0.0"
43+
},
44+
"bugs": {
45+
"url": "https://github.com/2o3t/koa2-proxy-middleware/issues"
46+
},
47+
"homepage": "https://github.com/2o3t/koa2-proxy-middleware",
48+
"repository": {
49+
"type": "git",
50+
"url": "https://github.com/2o3t/koa2-proxy-middleware.git"
51+
}
5052
}

0 commit comments

Comments
 (0)