Skip to content

Commit b0b089f

Browse files
authored
Merge pull request #68 from BackendStack21/v3.0.0
supporting fast-proxy-lite as default proxy library
2 parents f50540c + ab1ce7e commit b0b089f

File tree

5 files changed

+53
-37
lines changed

5 files changed

+53
-37
lines changed

README.md

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ A super fast, framework agnostic Node.js API Gateway for the masses ❤️
77
## Medium articles:
88
- https://itnext.io/a-js-api-gateway-for-the-masses-a12fdb9e961c
99

10-
1110
## Install
1211
```js
1312
npm i fast-gateway
@@ -46,10 +45,10 @@ npm i http-lambda-proxy
4645
const gateway = require('fast-gateway')
4746
const server = gateway({
4847
routes: [{
49-
proxyType: 'lambda',
5048
prefix: '/service',
5149
target: 'my-lambda-serverless-api',
52-
lambdaProxy: {
50+
proxyType: 'lambda',
51+
proxyConfig: {
5352
region: 'eu-central-1'
5453
}
5554
}]
@@ -104,27 +103,27 @@ module.exports.handler = serverless(service)
104103
timeout: 0,
105104
// Optional "target" value that overrides the routes "target" config value. Feature intended for testing purposes.
106105
targetOverride: "https://yourdev.api-gateway.com",
106+
// Optional "Proxy Factory" implementation, allows the integration of custom proxying strategies.
107+
// Default value: require('fast-proxy-lite/lib/proxy-factory')
108+
proxyFactory: ({ proxyType, opts, route }) => {...}
107109

108110
// HTTP proxy
109111
routes: [{
110-
// Optional proxy type definition. Supported values: http, lambda
112+
// Optional proxy type definition. Supported values: http, http-legacy, lambda
113+
// Modules:
114+
// - http: fast-proxy-lite
115+
// - http-legacy: fast-proxy
116+
// - lambda: http-lambda-proxy
111117
// Default value: http
112118
proxyType: 'http'
113-
// Optional `fast-proxy` library configuration (https://www.npmjs.com/package/fast-proxy#options)
114-
// base parameter defined as the route target. Default value: {}
115-
// This settings apply only when proxyType = 'http'
116-
fastProxy: {},
117-
// Optional `http-lambda-proxy` library configuration (https://www.npmjs.com/package/http-lambda-proxy#options)
118-
// The 'target' parameter is extracted from route.target, default region = 'eu-central-1'
119-
// This settings apply only when proxyType = 'lambda'
120-
lambdaProxy: {
121-
region: 'eu-central-1'
122-
},
119+
// Optional proxy library configuration:
120+
// - fast-proxy-lite: https://www.npmjs.com/package/fast-proxy-lite#options
121+
// - fast-proxy: https://www.npmjs.com/package/fast-proxy#options
122+
// - http-lambda-proxy: https://www.npmjs.com/package/http-lambda-proxy#options
123+
// Default value: {}
124+
proxyConfig: {},
123125
// Optional proxy handler function. Default value: (req, res, url, proxy, proxyOpts) => proxy(req, res, url, proxyOpts)
124126
proxyHandler: () => {},
125-
// Optional flag to indicate if target uses the HTTP2 protocol. Default value: false
126-
// This setting apply only when proxyType = 'http'
127-
http2: false,
128127
// Optional path matching regex. Default value: '/*'
129128
// In order to disable the 'pathRegex' at all, you can use an empty string: ''
130129
pathRegex: '/*',
@@ -167,7 +166,7 @@ module.exports.handler = serverless(service)
167166
// ...
168167
}
169168

170-
// if proxyType= 'http', other options allowed https://www.npmjs.com/package/fast-proxy#opts
169+
// if proxyType= 'http', other options allowed https://www.npmjs.com/package/fast-proxy-lite#opts
171170
}
172171
}]
173172
}
@@ -384,7 +383,7 @@ routes: [{
384383

385384
## Related projects
386385
- middleware-if-unless (https://www.npmjs.com/package/middleware-if-unless)
387-
- fast-proxy (https://www.npmjs.com/package/fast-proxy)
386+
- fast-proxy-lite (https://www.npmjs.com/package/fast-proxy-lite)
388387
- http-lambda-proxy (https://www.npmjs.com/package/http-lambda-proxy)
389388
- restana (https://www.npmjs.com/package/restana)
390389

@@ -402,4 +401,11 @@ Benchmark scripts can be found in benchmark folder.
402401
## Support / Donate 💚
403402
You can support the maintenance of this project:
404403
- PayPal: https://www.paypal.me/kyberneees
405-
- [TRON](https://www.binance.com/en/buy-TRON) Wallet: `TJ5Bbf9v4kpptnRsePXYDvnYcYrS5Tyxus`
404+
- [TRON](https://www.binance.com/en/buy-TRON) Wallet: `TJ5Bbf9v4kpptnRsePXYDvnYcYrS5Tyxus`
405+
406+
407+
## Breaking Changes
408+
### v3.x
409+
- The `fast-proxy-lite` module is used by default to support `http` proxy type 🔥. This means, no `undici` or `http2` are supported by default.
410+
- The old `fast-proxy` module is available under the `http-legacy` proxy type, but the module is not installed by default.
411+
- Proxy configuration is now generalized under the `proxyConfig` property.

index.d.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@ declare namespace fastgateway {
55

66
type Method = 'GET' | 'DELETE' | 'PATCH' | 'POST' | 'PUT' | 'HEAD' | 'OPTIONS' | 'TRACE';
77

8-
interface LambdaProxy {
9-
region?: string;
10-
target?: string;
11-
}
12-
138
interface Docs {
149
name: string;
1510
endpoint: string;
1611
type: string;
1712
}
1813

14+
interface ProxyFactoryOpts {
15+
proxyType: string;
16+
opts: {};
17+
route: Route;
18+
}
19+
1920
interface Route {
2021
proxyType?: Type;
21-
fastProxy?: {};
22-
lambdaProxy?: LambdaProxy;
22+
proxyConfig?: {};
2323
proxyHandler?: Function;
2424
http2?: boolean;
2525
pathRegex?: string;
@@ -48,6 +48,7 @@ declare namespace fastgateway {
4848

4949
interface Options<P extends restana.Protocol> {
5050
server?: Object | restana.Service<P> | Express.Application;
51+
proxyFactory?: (opts: ProxyFactoryOpts) => Function;
5152
restana?: {};
5253
middlewares?: Function[];
5354
pathRegex?: string;

index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
/* eslint-disable no-useless-call */
44

5-
const proxyFactory = require('./lib/proxy-factory')
5+
const defaultProxyFactory = require('./lib/proxy-factory')
66
const restana = require('restana')
77
const defaultProxyHandler = (req, res, url, proxy, proxyOpts) => proxy(req, res, url, proxyOpts)
88
const DEFAULT_METHODS = require('restana/libs/methods').filter(method => method !== 'all')
99
const send = require('@polka/send-type')
1010
const PROXY_TYPES = ['http', 'lambda']
1111

1212
const gateway = (opts) => {
13+
const proxyFactory = opts.proxyFactory || defaultProxyFactory
14+
1315
opts = Object.assign({
1416
middlewares: [],
1517
pathRegex: '/*'

lib/proxy-factory.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
'use strict'
22

3-
const fastProxy = require('fast-proxy')
3+
const fastProxy = require('fast-proxy-lite')
44

55
module.exports = ({ proxyType, opts, route }) => {
66
let proxy
7+
78
if (proxyType === 'http') {
89
proxy = fastProxy({
910
base: opts.targetOverride || route.target,
10-
http2: !!route.http2,
11-
...(route.fastProxy)
11+
...(route.proxyConfig)
1212
}).proxy
1313
} else if (proxyType === 'lambda') {
1414
proxy = require('http-lambda-proxy')({
1515
target: opts.targetOverride || route.target,
1616
region: 'eu-central-1',
17-
...(route.lambdaProxy || {})
17+
...(route.proxyConfig)
1818
})
19+
} else if (proxyType === 'http-legacy') {
20+
proxy = require('fast-proxy')({
21+
base: opts.targetOverride || route.target,
22+
...(route.proxyConfig)
23+
}).proxy
24+
} else {
25+
throw new Error(`Unsupported proxy type: ${proxyType}!`)
1926
}
2027

2128
return proxy

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fast-gateway",
3-
"version": "2.9.4",
3+
"version": "3.0.0",
44
"description": "A Node.js API Gateway for the masses!",
55
"main": "index.js",
66
"types": "index.d.ts",
@@ -28,9 +28,9 @@
2828
"homepage": "https://github.com/jkyberneees/fast-gateway#readme",
2929
"dependencies": {
3030
"@polka/send-type": "^0.5.2",
31-
"fast-proxy": "^2.1.0",
31+
"fast-proxy-lite": "^1.0.1",
3232
"http-cache-middleware": "^1.3.8",
33-
"restana": "^4.9.1",
33+
"restana": "^4.9.2",
3434
"stream-to-array": "^2.3.0"
3535
},
3636
"files": [
@@ -42,12 +42,12 @@
4242
],
4343
"devDependencies": {
4444
"@types/express": "^4.17.11",
45-
"aws-sdk": "^2.1018.0",
45+
"aws-sdk": "^2.1023.0",
4646
"chai": "^4.3.4",
4747
"cors": "^2.8.5",
4848
"express": "^4.17.1",
4949
"express-jwt": "^6.1.0",
50-
"express-rate-limit": "^5.5.0",
50+
"express-rate-limit": "^5.5.1",
5151
"fg-multiple-hooks": "^1.3.0",
5252
"helmet": "^4.6.0",
5353
"http-lambda-proxy": "^1.1.4",

0 commit comments

Comments
 (0)