http-proxy-middleware 0.21.0-beta.3 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -2
- package/README.md +98 -67
- package/dist/config-factory.js +9 -12
- package/dist/context-matcher.js +9 -12
- package/dist/handlers.js +7 -17
- package/dist/http-proxy-middleware.d.ts +2 -2
- package/dist/http-proxy-middleware.js +8 -18
- package/dist/index.d.ts +2 -2
- package/dist/index.js +3 -2
- package/dist/logger.js +5 -8
- package/dist/path-rewriter.js +10 -13
- package/dist/router.js +4 -7
- package/dist/tsconfig.tsbuildinfo +2911 -0
- package/dist/types.d.ts +17 -17
- package/package.json +23 -17
package/CHANGELOG.md
CHANGED
|
@@ -1,9 +1,19 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
-
##
|
|
3
|
+
## [v1.0.1](https://github.com/chimurai/http-proxy-middleware/releases/tag/v1.0.1)
|
|
4
|
+
|
|
5
|
+
- fix(typescript): fix proxyRes and router types ([#410](https://github.com/chimurai/http-proxy-middleware/issues/410)) ([dylang](https://github.com/dylang))
|
|
6
|
+
|
|
7
|
+
## [v1.0.0](https://github.com/chimurai/http-proxy-middleware/releases/tag/v1.0.0)
|
|
8
|
+
|
|
9
|
+
- feat(createProxyMiddleware): explicit import http-proxy-middleware ([BREAKING CHANGE](https://github.com/chimurai/http-proxy-middleware/releases))([#400](https://github.com/chimurai/http-proxy-middleware/issues/400#issuecomment-587162378))
|
|
10
|
+
- feat(typescript): export http-proxy-middleware types ([#400](https://github.com/chimurai/http-proxy-middleware/issues/400))
|
|
11
|
+
- fix(typescript): ES6 target - TS1192 ([#400](https://github.com/chimurai/http-proxy-middleware/issues/400#issuecomment-587064349))
|
|
12
|
+
|
|
13
|
+
## [v0.21.0](https://github.com/chimurai/http-proxy-middleware/releases/tag/v0.21.0)
|
|
4
14
|
|
|
5
15
|
- feat(http-proxy): bump to v1.18.0
|
|
6
|
-
- feat: async router ([#379](https://github.com/chimurai/http-proxy-middleware/issues/
|
|
16
|
+
- feat: async router ([#379](https://github.com/chimurai/http-proxy-middleware/issues/379)) ([LiranBri](https://github.com/LiranBri))
|
|
7
17
|
- feat(typescript): types support ([#369](https://github.com/chimurai/http-proxy-middleware/pull/369))
|
|
8
18
|
- feat: async pathRewrite ([#397](https://github.com/chimurai/http-proxy-middleware/pull/397)) ([rsethc](https://github.com/rsethc))
|
|
9
19
|
|
package/README.md
CHANGED
|
@@ -10,20 +10,40 @@ Node.js proxying made simple. Configure proxy middleware with ease for [connect]
|
|
|
10
10
|
|
|
11
11
|
Powered by the popular Nodejitsu [`http-proxy`](https://github.com/nodejitsu/node-http-proxy). [](https://github.com/nodejitsu/node-http-proxy)
|
|
12
12
|
|
|
13
|
+
## ⚠️ Note
|
|
14
|
+
|
|
15
|
+
This page is showing documentation for version v1.x.x ([release notes](https://github.com/chimurai/http-proxy-middleware/releases))
|
|
16
|
+
|
|
17
|
+
If you're looking for v0.x documentation. Go to:
|
|
18
|
+
https://github.com/chimurai/http-proxy-middleware/tree/v0.21.0#readme
|
|
19
|
+
|
|
13
20
|
## TL;DR
|
|
14
21
|
|
|
15
22
|
Proxy `/api` requests to `http://www.example.org`
|
|
16
23
|
|
|
17
24
|
```javascript
|
|
18
|
-
|
|
19
|
-
|
|
25
|
+
// javascript
|
|
26
|
+
|
|
27
|
+
const express = require('express');
|
|
28
|
+
const { createProxyMiddleware } = require('http-proxy-middleware');
|
|
29
|
+
|
|
30
|
+
const app = express();
|
|
31
|
+
|
|
32
|
+
app.use('/api', createProxyMiddleware({ target: 'http://www.example.org', changeOrigin: true }));
|
|
33
|
+
app.listen(3000);
|
|
20
34
|
|
|
21
|
-
|
|
35
|
+
// http://localhost:3000/api/foo/bar -> http://www.example.org/api/foo/bar
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
// typescript
|
|
40
|
+
|
|
41
|
+
import * as express from 'express';
|
|
42
|
+
import { createProxyMiddleware, Filter, Options, RequestHandler } from 'http-proxy-middleware';
|
|
22
43
|
|
|
23
|
-
app
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
);
|
|
44
|
+
const app = express();
|
|
45
|
+
|
|
46
|
+
app.use('/api', createProxyMiddleware({ target: 'http://www.example.org', changeOrigin: true }));
|
|
27
47
|
app.listen(3000);
|
|
28
48
|
|
|
29
49
|
// http://localhost:3000/api/foo/bar -> http://www.example.org/api/foo/bar
|
|
@@ -60,7 +80,7 @@ _All_ `http-proxy` [options](https://github.com/nodejitsu/node-http-proxy#option
|
|
|
60
80
|
|
|
61
81
|
## Install
|
|
62
82
|
|
|
63
|
-
```
|
|
83
|
+
```bash
|
|
64
84
|
$ npm install --save-dev http-proxy-middleware
|
|
65
85
|
```
|
|
66
86
|
|
|
@@ -68,15 +88,15 @@ $ npm install --save-dev http-proxy-middleware
|
|
|
68
88
|
|
|
69
89
|
Proxy middleware configuration.
|
|
70
90
|
|
|
71
|
-
####
|
|
91
|
+
#### createProxyMiddleware([context,] config)
|
|
72
92
|
|
|
73
93
|
```javascript
|
|
74
|
-
|
|
94
|
+
const { createProxyMiddleware } = require('http-proxy-middleware');
|
|
75
95
|
|
|
76
|
-
|
|
77
|
-
//
|
|
78
|
-
//
|
|
79
|
-
//
|
|
96
|
+
const apiProxy = createProxyMiddleware('/api', { target: 'http://www.example.org' });
|
|
97
|
+
// \____/ \_____________________________/
|
|
98
|
+
// | |
|
|
99
|
+
// context options
|
|
80
100
|
|
|
81
101
|
// 'apiProxy' is now ready to be used as middleware in a server.
|
|
82
102
|
```
|
|
@@ -87,11 +107,11 @@ var apiProxy = proxy('/api', { target: 'http://www.example.org' });
|
|
|
87
107
|
|
|
88
108
|
(full list of [`http-proxy-middleware` configuration options](#options))
|
|
89
109
|
|
|
90
|
-
####
|
|
110
|
+
#### createProxyMiddleware(uri [, config])
|
|
91
111
|
|
|
92
112
|
```javascript
|
|
93
113
|
// shorthand syntax for the example above:
|
|
94
|
-
|
|
114
|
+
const apiProxy = createProxyMiddleware('http://www.example.org/api');
|
|
95
115
|
```
|
|
96
116
|
|
|
97
117
|
More about the [shorthand configuration](#shorthand).
|
|
@@ -102,11 +122,11 @@ An example with `express` server.
|
|
|
102
122
|
|
|
103
123
|
```javascript
|
|
104
124
|
// include dependencies
|
|
105
|
-
|
|
106
|
-
|
|
125
|
+
const express = require('express');
|
|
126
|
+
const { createProxyMiddleware } = require('http-proxy-middleware');
|
|
107
127
|
|
|
108
128
|
// proxy middleware options
|
|
109
|
-
|
|
129
|
+
const options = {
|
|
110
130
|
target: 'http://www.example.org', // target host
|
|
111
131
|
changeOrigin: true, // needed for virtual hosted sites
|
|
112
132
|
ws: true, // proxy websockets
|
|
@@ -122,10 +142,10 @@ var options = {
|
|
|
122
142
|
};
|
|
123
143
|
|
|
124
144
|
// create the proxy (without context)
|
|
125
|
-
|
|
145
|
+
const exampleProxy = createProxyMiddleware(options);
|
|
126
146
|
|
|
127
147
|
// mount `exampleProxy` in web server
|
|
128
|
-
|
|
148
|
+
const app = express();
|
|
129
149
|
app.use('/api', exampleProxy);
|
|
130
150
|
app.listen(3000);
|
|
131
151
|
```
|
|
@@ -136,7 +156,7 @@ Providing an alternative way to decide which requests should be proxied; In case
|
|
|
136
156
|
|
|
137
157
|
[RFC 3986 `path`](https://tools.ietf.org/html/rfc3986#section-3.3) is used for context matching.
|
|
138
158
|
|
|
139
|
-
```
|
|
159
|
+
```ascii
|
|
140
160
|
foo://example.com:8042/over/there?name=ferret#nose
|
|
141
161
|
\_/ \______________/\_________/ \_________/ \__/
|
|
142
162
|
| | | | |
|
|
@@ -145,24 +165,24 @@ Providing an alternative way to decide which requests should be proxied; In case
|
|
|
145
165
|
|
|
146
166
|
- **path matching**
|
|
147
167
|
|
|
148
|
-
- `
|
|
149
|
-
- `
|
|
150
|
-
- `
|
|
168
|
+
- `createProxyMiddleware({...})` - matches any path, all requests will be proxied.
|
|
169
|
+
- `createProxyMiddleware('/', {...})` - matches any path, all requests will be proxied.
|
|
170
|
+
- `createProxyMiddleware('/api', {...})` - matches paths starting with `/api`
|
|
151
171
|
|
|
152
172
|
- **multiple path matching**
|
|
153
173
|
|
|
154
|
-
- `
|
|
174
|
+
- `createProxyMiddleware(['/api', '/ajax', '/someotherpath'], {...})`
|
|
155
175
|
|
|
156
176
|
- **wildcard path matching**
|
|
157
177
|
|
|
158
178
|
For fine-grained control you can use wildcard matching. Glob pattern matching is done by _micromatch_. Visit [micromatch](https://www.npmjs.com/package/micromatch) or [glob](https://www.npmjs.com/package/glob) for more globbing examples.
|
|
159
179
|
|
|
160
|
-
- `
|
|
161
|
-
- `
|
|
162
|
-
- `
|
|
163
|
-
- `
|
|
164
|
-
- `
|
|
165
|
-
- `
|
|
180
|
+
- `createProxyMiddleware('**', {...})` matches any path, all requests will be proxied.
|
|
181
|
+
- `createProxyMiddleware('**/*.html', {...})` matches any path which ends with `.html`
|
|
182
|
+
- `createProxyMiddleware('/*.html', {...})` matches paths directly under path-absolute
|
|
183
|
+
- `createProxyMiddleware('/api/**/*.html', {...})` matches requests ending with `.html` in the path of `/api`
|
|
184
|
+
- `createProxyMiddleware(['/api/**', '/ajax/**'], {...})` combine multiple patterns
|
|
185
|
+
- `createProxyMiddleware(['/api/**', '!**/bad.json'], {...})` exclusion
|
|
166
186
|
|
|
167
187
|
**Note**: In multiple path matching, you cannot use string paths and wildcard paths together.
|
|
168
188
|
|
|
@@ -174,11 +194,13 @@ Providing an alternative way to decide which requests should be proxied; In case
|
|
|
174
194
|
/**
|
|
175
195
|
* @return {Boolean}
|
|
176
196
|
*/
|
|
177
|
-
|
|
197
|
+
const filter = function(pathname, req) {
|
|
178
198
|
return pathname.match('^/api') && req.method === 'GET';
|
|
179
199
|
};
|
|
180
200
|
|
|
181
|
-
|
|
201
|
+
const apiProxy = createProxyMiddleware(filter, {
|
|
202
|
+
target: 'http://www.example.org'
|
|
203
|
+
});
|
|
182
204
|
```
|
|
183
205
|
|
|
184
206
|
## Options
|
|
@@ -202,11 +224,10 @@ Providing an alternative way to decide which requests should be proxied; In case
|
|
|
202
224
|
|
|
203
225
|
// custom rewriting, returning Promise
|
|
204
226
|
pathRewrite: async function (path, req) {
|
|
205
|
-
|
|
227
|
+
const should_add_something = await httpRequestToDecideSomething(path);
|
|
206
228
|
if (should_add_something) path += "something";
|
|
207
229
|
return path;
|
|
208
230
|
}
|
|
209
|
-
|
|
210
231
|
```
|
|
211
232
|
|
|
212
233
|
- **option.router**: object/function, re-target `option.target` for specific requests.
|
|
@@ -221,11 +242,20 @@ Providing an alternative way to decide which requests should be proxied; In case
|
|
|
221
242
|
'/rest' : 'http://localhost:8004' // path only
|
|
222
243
|
}
|
|
223
244
|
|
|
224
|
-
// Custom router function
|
|
245
|
+
// Custom router function (string target)
|
|
225
246
|
router: function(req) {
|
|
226
247
|
return 'http://localhost:8004';
|
|
227
248
|
}
|
|
228
249
|
|
|
250
|
+
// Custom router function (target object)
|
|
251
|
+
router: function(req) {
|
|
252
|
+
return {
|
|
253
|
+
protocol: 'https:', // The : is required
|
|
254
|
+
host: 'localhost',
|
|
255
|
+
port: 8004
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
|
|
229
259
|
// Asynchronous router function which returns promise
|
|
230
260
|
router: async function(req) {
|
|
231
261
|
const url = await doSomeIO();
|
|
@@ -248,9 +278,9 @@ Providing an alternative way to decide which requests should be proxied; In case
|
|
|
248
278
|
```javascript
|
|
249
279
|
// verbose replacement
|
|
250
280
|
function logProvider(provider) {
|
|
251
|
-
|
|
281
|
+
const logger = new (require('winston').Logger)();
|
|
252
282
|
|
|
253
|
-
|
|
283
|
+
const myCustomProvider = {
|
|
254
284
|
log: logger.log,
|
|
255
285
|
debug: logger.debug,
|
|
256
286
|
info: logger.info,
|
|
@@ -272,9 +302,7 @@ Subscribe to [http-proxy events](https://github.com/nodejitsu/node-http-proxy#li
|
|
|
272
302
|
res.writeHead(500, {
|
|
273
303
|
'Content-Type': 'text/plain'
|
|
274
304
|
});
|
|
275
|
-
res.end(
|
|
276
|
-
'Something went wrong. And we are reporting a custom error message.'
|
|
277
|
-
);
|
|
305
|
+
res.end('Something went wrong. And we are reporting a custom error message.');
|
|
278
306
|
}
|
|
279
307
|
```
|
|
280
308
|
|
|
@@ -316,6 +344,7 @@ Subscribe to [http-proxy events](https://github.com/nodejitsu/node-http-proxy#li
|
|
|
316
344
|
```
|
|
317
345
|
|
|
318
346
|
- **option.onClose**: function, subscribe to http-proxy's `close` event.
|
|
347
|
+
|
|
319
348
|
```javascript
|
|
320
349
|
function onClose(res, socket, head) {
|
|
321
350
|
// view disconnected websocket connections
|
|
@@ -349,7 +378,7 @@ The following options are provided by the underlying [http-proxy](https://github
|
|
|
349
378
|
- String: new domain, for example `cookieDomainRewrite: "new.domain"`. To remove the domain, use `cookieDomainRewrite: ""`.
|
|
350
379
|
- Object: mapping of domains to new domains, use `"*"` to match all domains.
|
|
351
380
|
For example keep one domain unchanged, rewrite one domain and remove other domains:
|
|
352
|
-
```
|
|
381
|
+
```json
|
|
353
382
|
cookieDomainRewrite: {
|
|
354
383
|
"unchanged.domain": "unchanged.domain",
|
|
355
384
|
"old.domain": "new.domain",
|
|
@@ -361,7 +390,7 @@ The following options are provided by the underlying [http-proxy](https://github
|
|
|
361
390
|
- String: new path, for example `cookiePathRewrite: "/newPath/"`. To remove the path, use `cookiePathRewrite: ""`. To set path to root use `cookiePathRewrite: "/"`.
|
|
362
391
|
- Object: mapping of paths to new paths, use `"*"` to match all paths.
|
|
363
392
|
For example, to keep one path unchanged, rewrite one path and remove other paths:
|
|
364
|
-
```
|
|
393
|
+
```json
|
|
365
394
|
cookiePathRewrite: {
|
|
366
395
|
"/unchanged.path/": "/unchanged.path/",
|
|
367
396
|
"/old.path/": "/new.path/",
|
|
@@ -375,7 +404,7 @@ The following options are provided by the underlying [http-proxy](https://github
|
|
|
375
404
|
- **option.selfHandleResponse** true/false, if set to true, none of the webOutgoing passes are called and it's your responsibility to appropriately return the response by listening and acting on the `proxyRes` event
|
|
376
405
|
- **option.buffer**: stream of data to send as the request body. Maybe you have some middleware that consumes the request stream before proxying it on e.g. If you read the body of a request into a field called 'req.rawbody' you could restream this field in the buffer option:
|
|
377
406
|
|
|
378
|
-
```
|
|
407
|
+
```javascript
|
|
379
408
|
'use strict';
|
|
380
409
|
|
|
381
410
|
const streamify = require('stream-array');
|
|
@@ -383,12 +412,15 @@ The following options are provided by the underlying [http-proxy](https://github
|
|
|
383
412
|
const proxy = new HttpProxy();
|
|
384
413
|
|
|
385
414
|
module.exports = (req, res, next) => {
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
415
|
+
proxy.web(
|
|
416
|
+
req,
|
|
417
|
+
res,
|
|
418
|
+
{
|
|
419
|
+
target: 'http://localhost:4003/',
|
|
420
|
+
buffer: streamify(req.rawBody)
|
|
421
|
+
},
|
|
422
|
+
next
|
|
423
|
+
);
|
|
392
424
|
};
|
|
393
425
|
```
|
|
394
426
|
|
|
@@ -397,14 +429,14 @@ The following options are provided by the underlying [http-proxy](https://github
|
|
|
397
429
|
Use the shorthand syntax when verbose configuration is not needed. The `context` and `option.target` will be automatically configured when shorthand is used. Options can still be used if needed.
|
|
398
430
|
|
|
399
431
|
```javascript
|
|
400
|
-
|
|
401
|
-
//
|
|
432
|
+
createProxyMiddleware('http://www.example.org:8000/api');
|
|
433
|
+
// createProxyMiddleware('/api', {target: 'http://www.example.org:8000'});
|
|
402
434
|
|
|
403
|
-
|
|
404
|
-
//
|
|
435
|
+
createProxyMiddleware('http://www.example.org:8000/api/books/*/**.json');
|
|
436
|
+
// createProxyMiddleware('/api/books/*/**.json', {target: 'http://www.example.org:8000'});
|
|
405
437
|
|
|
406
|
-
|
|
407
|
-
//
|
|
438
|
+
createProxyMiddleware('http://www.example.org:8000/api', { changeOrigin: true });
|
|
439
|
+
// createProxyMiddleware('/api', {target: 'http://www.example.org:8000', changeOrigin: true});
|
|
408
440
|
```
|
|
409
441
|
|
|
410
442
|
### app.use(path, proxy)
|
|
@@ -413,28 +445,26 @@ If you want to use the server's `app.use` `path` parameter to match requests;
|
|
|
413
445
|
Create and mount the proxy without the http-proxy-middleware `context` parameter:
|
|
414
446
|
|
|
415
447
|
```javascript
|
|
416
|
-
app.use(
|
|
417
|
-
'/api',
|
|
418
|
-
proxy({ target: 'http://www.example.org', changeOrigin: true })
|
|
419
|
-
);
|
|
448
|
+
app.use('/api', createProxyMiddleware({ target: 'http://www.example.org', changeOrigin: true }));
|
|
420
449
|
```
|
|
421
450
|
|
|
422
451
|
`app.use` documentation:
|
|
423
452
|
|
|
424
453
|
- express: http://expressjs.com/en/4x/api.html#app.use
|
|
425
454
|
- connect: https://github.com/senchalabs/connect#mount-middleware
|
|
455
|
+
- polka: https://github.com/lukeed/polka#usebase-fn
|
|
426
456
|
|
|
427
457
|
## WebSocket
|
|
428
458
|
|
|
429
459
|
```javascript
|
|
430
460
|
// verbose api
|
|
431
|
-
|
|
461
|
+
createProxyMiddleware('/', { target: 'http://echo.websocket.org', ws: true });
|
|
432
462
|
|
|
433
463
|
// shorthand
|
|
434
|
-
|
|
464
|
+
createProxyMiddleware('http://echo.websocket.org', { ws: true });
|
|
435
465
|
|
|
436
466
|
// shorter shorthand
|
|
437
|
-
|
|
467
|
+
createProxyMiddleware('ws://echo.websocket.org');
|
|
438
468
|
```
|
|
439
469
|
|
|
440
470
|
### External WebSocket upgrade
|
|
@@ -442,12 +472,12 @@ proxy('ws://echo.websocket.org');
|
|
|
442
472
|
In the previous WebSocket examples, http-proxy-middleware relies on a initial http request in order to listen to the http `upgrade` event. If you need to proxy WebSockets without the initial http request, you can subscribe to the server's http `upgrade` event manually.
|
|
443
473
|
|
|
444
474
|
```javascript
|
|
445
|
-
|
|
475
|
+
const wsProxy = createProxyMiddleware('ws://echo.websocket.org', { changeOrigin: true });
|
|
446
476
|
|
|
447
|
-
|
|
477
|
+
const app = express();
|
|
448
478
|
app.use(wsProxy);
|
|
449
479
|
|
|
450
|
-
|
|
480
|
+
const server = app.listen(3000);
|
|
451
481
|
server.on('upgrade', wsProxy.upgrade); // <-- subscribe to http 'upgrade'
|
|
452
482
|
```
|
|
453
483
|
|
|
@@ -472,6 +502,7 @@ View the [recipes](https://github.com/chimurai/http-proxy-middleware/tree/master
|
|
|
472
502
|
- [express](https://www.npmjs.com/package/express)
|
|
473
503
|
- [browser-sync](https://www.npmjs.com/package/browser-sync)
|
|
474
504
|
- [lite-server](https://www.npmjs.com/package/lite-server)
|
|
505
|
+
- [polka](https://github.com/lukeed/polka)
|
|
475
506
|
- [grunt-contrib-connect](https://www.npmjs.com/package/grunt-contrib-connect)
|
|
476
507
|
- [grunt-browser-sync](https://www.npmjs.com/package/grunt-browser-sync)
|
|
477
508
|
- [gulp-connect](https://www.npmjs.com/package/gulp-connect)
|
package/dist/config-factory.js
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const
|
|
7
|
-
const
|
|
3
|
+
const _ = require("lodash");
|
|
4
|
+
const url = require("url");
|
|
8
5
|
const errors_1 = require("./errors");
|
|
9
6
|
const logger_1 = require("./logger");
|
|
10
7
|
const logger = logger_1.getInstance();
|
|
@@ -17,15 +14,15 @@ function createConfig(context, opts) {
|
|
|
17
14
|
// app.use('/api', proxy({target:'http://localhost:9000'}));
|
|
18
15
|
if (isContextless(context, opts)) {
|
|
19
16
|
config.context = '/';
|
|
20
|
-
config.options =
|
|
17
|
+
config.options = _.assign(config.options, context);
|
|
21
18
|
// app.use('/api', proxy('http://localhost:9000'));
|
|
22
19
|
// app.use(proxy('http://localhost:9000/api'));
|
|
23
20
|
}
|
|
24
21
|
else if (isStringShortHand(context)) {
|
|
25
|
-
const oUrl =
|
|
22
|
+
const oUrl = url.parse(context);
|
|
26
23
|
const target = [oUrl.protocol, '//', oUrl.host].join('');
|
|
27
24
|
config.context = oUrl.pathname || '/';
|
|
28
|
-
config.options =
|
|
25
|
+
config.options = _.assign(config.options, { target }, opts);
|
|
29
26
|
if (oUrl.protocol === 'ws:' || oUrl.protocol === 'wss:') {
|
|
30
27
|
config.options.ws = true;
|
|
31
28
|
}
|
|
@@ -33,7 +30,7 @@ function createConfig(context, opts) {
|
|
|
33
30
|
}
|
|
34
31
|
else {
|
|
35
32
|
config.context = context;
|
|
36
|
-
config.options =
|
|
33
|
+
config.options = _.assign(config.options, opts);
|
|
37
34
|
}
|
|
38
35
|
configureLogger(config.options);
|
|
39
36
|
if (!config.options.target) {
|
|
@@ -54,8 +51,8 @@ exports.createConfig = createConfig;
|
|
|
54
51
|
* @return {Boolean} [description]
|
|
55
52
|
*/
|
|
56
53
|
function isStringShortHand(context) {
|
|
57
|
-
if (
|
|
58
|
-
return !!
|
|
54
|
+
if (_.isString(context)) {
|
|
55
|
+
return !!url.parse(context).host;
|
|
59
56
|
}
|
|
60
57
|
}
|
|
61
58
|
/**
|
|
@@ -70,7 +67,7 @@ function isStringShortHand(context) {
|
|
|
70
67
|
* @return {Boolean} [description]
|
|
71
68
|
*/
|
|
72
69
|
function isContextless(context, opts) {
|
|
73
|
-
return
|
|
70
|
+
return _.isPlainObject(context) && _.isEmpty(opts);
|
|
74
71
|
}
|
|
75
72
|
function configureLogger(options) {
|
|
76
73
|
if (options.logLevel) {
|
package/dist/context-matcher.js
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const
|
|
3
|
+
const isGlob = require("is-glob");
|
|
4
|
+
const _ = require("lodash");
|
|
5
|
+
const micromatch = require("micromatch");
|
|
6
|
+
const url = require("url");
|
|
10
7
|
const errors_1 = require("./errors");
|
|
11
8
|
function match(context, uri, req) {
|
|
12
9
|
// single path
|
|
@@ -28,7 +25,7 @@ function match(context, uri, req) {
|
|
|
28
25
|
throw new Error(errors_1.ERRORS.ERR_CONTEXT_MATCHER_INVALID_ARRAY);
|
|
29
26
|
}
|
|
30
27
|
// custom matching
|
|
31
|
-
if (
|
|
28
|
+
if (_.isFunction(context)) {
|
|
32
29
|
const pathname = getUrlPathName(uri);
|
|
33
30
|
return context(pathname, req);
|
|
34
31
|
}
|
|
@@ -46,7 +43,7 @@ function matchSingleStringPath(context, uri) {
|
|
|
46
43
|
}
|
|
47
44
|
function matchSingleGlobPath(pattern, uri) {
|
|
48
45
|
const pathname = getUrlPathName(uri);
|
|
49
|
-
const matches =
|
|
46
|
+
const matches = micromatch([pathname], pattern);
|
|
50
47
|
return matches && matches.length > 0;
|
|
51
48
|
}
|
|
52
49
|
function matchMultiGlobPath(patternList, uri) {
|
|
@@ -74,11 +71,11 @@ function matchMultiPath(contextList, uri) {
|
|
|
74
71
|
* @return {String} RFC 3986 path
|
|
75
72
|
*/
|
|
76
73
|
function getUrlPathName(uri) {
|
|
77
|
-
return uri &&
|
|
74
|
+
return uri && url.parse(uri).pathname;
|
|
78
75
|
}
|
|
79
76
|
function isStringPath(context) {
|
|
80
|
-
return
|
|
77
|
+
return _.isString(context) && !isGlob(context);
|
|
81
78
|
}
|
|
82
79
|
function isGlobPath(context) {
|
|
83
|
-
return
|
|
80
|
+
return isGlob(context);
|
|
84
81
|
}
|
package/dist/handlers.js
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const
|
|
3
|
+
const _ = require("lodash");
|
|
7
4
|
const logger_1 = require("./logger");
|
|
8
5
|
const logger = logger_1.getInstance();
|
|
9
6
|
function init(proxy, option) {
|
|
@@ -16,31 +13,24 @@ function init(proxy, option) {
|
|
|
16
13
|
exports.init = init;
|
|
17
14
|
function getHandlers(options) {
|
|
18
15
|
// https://github.com/nodejitsu/node-http-proxy#listening-for-proxy-events
|
|
19
|
-
const proxyEvents = [
|
|
20
|
-
'error',
|
|
21
|
-
'proxyReq',
|
|
22
|
-
'proxyReqWs',
|
|
23
|
-
'proxyRes',
|
|
24
|
-
'open',
|
|
25
|
-
'close'
|
|
26
|
-
];
|
|
16
|
+
const proxyEvents = ['error', 'proxyReq', 'proxyReqWs', 'proxyRes', 'open', 'close'];
|
|
27
17
|
const handlers = {};
|
|
28
18
|
for (const event of proxyEvents) {
|
|
29
19
|
// all handlers for the http-proxy events are prefixed with 'on'.
|
|
30
20
|
// loop through options and try to find these handlers
|
|
31
21
|
// and add them to the handlers object for subscription in init().
|
|
32
|
-
const eventName =
|
|
33
|
-
const fnHandler =
|
|
34
|
-
if (
|
|
22
|
+
const eventName = _.camelCase('on ' + event);
|
|
23
|
+
const fnHandler = _.get(options, eventName);
|
|
24
|
+
if (_.isFunction(fnHandler)) {
|
|
35
25
|
handlers[event] = fnHandler;
|
|
36
26
|
}
|
|
37
27
|
}
|
|
38
28
|
// add default error handler in absence of error handler
|
|
39
|
-
if (!
|
|
29
|
+
if (!_.isFunction(handlers.error)) {
|
|
40
30
|
handlers.error = defaultErrorHandler;
|
|
41
31
|
}
|
|
42
32
|
// add default close handler in absence of close handler
|
|
43
|
-
if (!
|
|
33
|
+
if (!_.isFunction(handlers.close)) {
|
|
44
34
|
handlers.close = logClose;
|
|
45
35
|
}
|
|
46
36
|
return handlers;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Filter,
|
|
1
|
+
import { Filter, RequestHandler, Options } from './types';
|
|
2
2
|
export declare class HttpProxyMiddleware {
|
|
3
3
|
private logger;
|
|
4
4
|
private config;
|
|
@@ -7,7 +7,7 @@ export declare class HttpProxyMiddleware {
|
|
|
7
7
|
private proxy;
|
|
8
8
|
private pathRewriter;
|
|
9
9
|
constructor(context: Filter | Options, opts?: Options);
|
|
10
|
-
middleware:
|
|
10
|
+
middleware: RequestHandler;
|
|
11
11
|
private catchUpgradeRequest;
|
|
12
12
|
private handleUpgrade;
|
|
13
13
|
/**
|
|
@@ -8,25 +8,15 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
-
};
|
|
14
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
-
if (mod && mod.__esModule) return mod;
|
|
16
|
-
var result = {};
|
|
17
|
-
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
|
18
|
-
result["default"] = mod;
|
|
19
|
-
return result;
|
|
20
|
-
};
|
|
21
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
-
const
|
|
23
|
-
const
|
|
12
|
+
const httpProxy = require("http-proxy");
|
|
13
|
+
const _ = require("lodash");
|
|
24
14
|
const config_factory_1 = require("./config-factory");
|
|
25
|
-
const contextMatcher =
|
|
26
|
-
const handlers =
|
|
15
|
+
const contextMatcher = require("./context-matcher");
|
|
16
|
+
const handlers = require("./handlers");
|
|
27
17
|
const logger_1 = require("./logger");
|
|
28
|
-
const PathRewriter =
|
|
29
|
-
const Router =
|
|
18
|
+
const PathRewriter = require("./path-rewriter");
|
|
19
|
+
const Router = require("./router");
|
|
30
20
|
class HttpProxyMiddleware {
|
|
31
21
|
constructor(context, opts) {
|
|
32
22
|
this.logger = logger_1.getInstance();
|
|
@@ -86,7 +76,7 @@ class HttpProxyMiddleware {
|
|
|
86
76
|
req.url = req.originalUrl || req.url;
|
|
87
77
|
// store uri before it gets rewritten for logging
|
|
88
78
|
const originalPath = req.url;
|
|
89
|
-
const newProxyOptions =
|
|
79
|
+
const newProxyOptions = _.assign({}, this.proxyOptions);
|
|
90
80
|
// Apply in order:
|
|
91
81
|
// 1. option.router
|
|
92
82
|
// 2. option.pathRewrite
|
|
@@ -132,7 +122,7 @@ class HttpProxyMiddleware {
|
|
|
132
122
|
this.config = config_factory_1.createConfig(context, opts);
|
|
133
123
|
this.proxyOptions = this.config.options;
|
|
134
124
|
// create proxy
|
|
135
|
-
this.proxy =
|
|
125
|
+
this.proxy = httpProxy.createProxyServer({});
|
|
136
126
|
this.logger.info(`[HPM] Proxy created: ${this.config.context} -> ${this.proxyOptions.target}`);
|
|
137
127
|
this.pathRewriter = PathRewriter.createPathRewriter(this.proxyOptions.pathRewrite); // returns undefined when "pathRewrite" is not provided
|
|
138
128
|
// attach handler to http-proxy events
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { Filter, Options } from './types';
|
|
2
|
-
declare function
|
|
3
|
-
export
|
|
2
|
+
export declare function createProxyMiddleware(context: Filter | Options, options?: Options): import("./types").RequestHandler;
|
|
3
|
+
export { Filter, Options, RequestHandler } from './types';
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
2
3
|
const http_proxy_middleware_1 = require("./http-proxy-middleware");
|
|
3
|
-
function
|
|
4
|
+
function createProxyMiddleware(context, options) {
|
|
4
5
|
const { middleware } = new http_proxy_middleware_1.HttpProxyMiddleware(context, options);
|
|
5
6
|
return middleware;
|
|
6
7
|
}
|
|
7
|
-
|
|
8
|
+
exports.createProxyMiddleware = createProxyMiddleware;
|