fast-gateway
Advanced tools
Comparing version 3.0.2 to 3.1.0
@@ -36,2 +36,15 @@ import * as restana from 'restana'; | ||
interface WebSocketRoute { | ||
proxyType: 'websocket'; | ||
proxyConfig?: {}; // https://github.com/faye/faye-websocket-node#initialization-options | ||
prefix: string; | ||
target: string; | ||
subProtocols?: []; // https://github.com/faye/faye-websocket-node#subprotocol-negotiation | ||
hooks?: WebSocketHooks; | ||
} | ||
interface WebSocketHooks { | ||
onOpen?: (ws: any, searchParams: URLSearchParams) => Promise<void>; | ||
} | ||
interface Hooks { | ||
@@ -58,3 +71,3 @@ onRequest?: Function, | ||
targetOverride?: string; | ||
routes: Route[]; | ||
routes: Route[] | WebSocketRoute[]; | ||
} | ||
@@ -61,0 +74,0 @@ } |
23
index.js
@@ -11,2 +11,3 @@ 'use strict' | ||
const PROXY_TYPES = ['http', 'lambda'] | ||
const registerWebSocketRoutes = require('./lib/ws-proxy') | ||
@@ -21,7 +22,7 @@ const gateway = (opts) => { | ||
const server = opts.server || restana(opts.restana) | ||
const router = opts.server || restana(opts.restana) | ||
// registering global middlewares | ||
opts.middlewares.forEach(middleware => { | ||
server.use(middleware) | ||
router.use(middleware) | ||
}) | ||
@@ -34,8 +35,14 @@ | ||
})) | ||
server.get('/services.json', (req, res) => { | ||
router.get('/services.json', (req, res) => { | ||
send(res, 200, services) | ||
}) | ||
// processing routes | ||
opts.routes.forEach(route => { | ||
// processing websocket routes | ||
registerWebSocketRoutes({ | ||
routes: opts.routes.filter(route => route.proxyType === 'websocket'), | ||
server: router.getServer() | ||
}) | ||
// processing non-websocket routes | ||
opts.routes.filter(route => route.proxyType !== 'websocket').forEach(route => { | ||
if (undefined === route.prefixRewrite) { | ||
@@ -88,4 +95,4 @@ route.prefixRewrite = '' | ||
method = method.toLowerCase() | ||
if (server[method]) { | ||
server[method].apply(server, args) | ||
if (router[method]) { | ||
router[method].apply(router, args) | ||
} | ||
@@ -95,3 +102,3 @@ }) | ||
return server | ||
return router | ||
} | ||
@@ -98,0 +105,0 @@ |
@@ -8,2 +8,5 @@ 'use strict' | ||
module.exports = { | ||
websocket: { | ||
onOpenNoOp (ws, searchParams) { } | ||
}, | ||
lambda: { | ||
@@ -10,0 +13,0 @@ onRequestNoOp (req, res) { }, |
{ | ||
"name": "fast-gateway", | ||
"version": "3.0.2", | ||
"version": "3.1.0", | ||
"description": "A Node.js API Gateway for the masses!", | ||
@@ -10,3 +10,4 @@ "main": "index.js", | ||
"format": "npx standard --fix", | ||
"lint": "npx standard" | ||
"lint": "npx standard", | ||
"ws-bench": "npx artillery run benchmark/websocket/artillery-perf1.yml" | ||
}, | ||
@@ -46,3 +47,4 @@ "repository": { | ||
"@types/express": "^4.17.11", | ||
"aws-sdk": "^2.1023.0", | ||
"artillery": "^2.0.0-6", | ||
"aws-sdk": "^2.1037.0", | ||
"chai": "^4.3.4", | ||
@@ -53,2 +55,3 @@ "cors": "^2.8.5", | ||
"express-rate-limit": "^5.5.1", | ||
"faye-websocket": "^0.11.4", | ||
"fg-multiple-hooks": "^1.3.0", | ||
@@ -55,0 +58,0 @@ "helmet": "^4.6.0", |
@@ -199,2 +199,41 @@ # fast-gateway | ||
### WebSockets support | ||
WebSockets proxying is supported since `v3.1.0`. Main considerations: | ||
- The `faye-websocket` module dependency require to be installed: | ||
```bash | ||
npm i faye-websocket | ||
``` | ||
- WebSockets middlewares are not supported. | ||
- WebSocketRoute configuration definition: | ||
```ts | ||
interface WebSocketRoute { | ||
proxyType: 'websocket'; | ||
// https://github.com/faye/faye-websocket-node#initialization-options | ||
proxyConfig?: {}; | ||
prefix: string; | ||
target: string; | ||
// https://github.com/faye/faye-websocket-node#subprotocol-negotiation | ||
subProtocols?: []; | ||
hooks?: WebSocketHooks; | ||
} | ||
interface WebSocketHooks { | ||
onOpen?: (ws: any, searchParams: URLSearchParams) => Promise<void>; | ||
} | ||
``` | ||
- The `/` route prefix is considered the default route. | ||
#### Configuration example: | ||
```js | ||
gateway({ | ||
routes: [{ | ||
// ... other HTTP or WebSocket routes | ||
}, { | ||
proxyType: 'websocket', | ||
prefix: '/echo', | ||
target: 'ws://ws.ifelse.io' | ||
}] | ||
}).start(PORT) | ||
``` | ||
## Timeouts and Unavailability | ||
@@ -201,0 +240,0 @@ We can restrict requests timeouts globally or at service level using the `timeout` configuration. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27672
9
307
451
21