Skip to content

Commit 23ac517

Browse files
committed
cleanup
1 parent 7249128 commit 23ac517

File tree

3 files changed

+8
-11
lines changed

3 files changed

+8
-11
lines changed

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ declare namespace fastgateway {
4343
}
4444

4545
interface WebSocketHooks {
46-
onOpen?: Function;
46+
onOpen?: (ws: any, searchParams: URLSearchParams) => Promise<void>;
4747
}
4848

4949
interface Hooks {

index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const defaultProxyHandler = (req, res, url, proxy, proxyOpts) => proxy(req, res,
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']
11-
const wsProxy = require('./lib/ws-proxy')
11+
const registerWebSocketRoutes = require('./lib/ws-proxy')
1212

1313
const gateway = (opts) => {
1414
const proxyFactory = opts.proxyFactory || defaultProxyFactory
@@ -35,9 +35,8 @@ const gateway = (opts) => {
3535
})
3636

3737
// processing websocket routes
38-
const wsRoutes = opts.routes.filter(route => route.proxyType === 'websocket')
39-
wsProxy({
40-
routes: wsRoutes,
38+
registerWebSocketRoutes({
39+
routes: opts.routes.filter(route => route.proxyType === 'websocket'),
4140
server: router.getServer()
4241
})
4342

lib/ws-proxy.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = (config) => {
1414

1515
server.on('upgrade', async (req, socket, body) => {
1616
if (WebSocket.isWebSocket(req)) {
17-
const url = new URL('http://dummy' + req.url)
17+
const url = new URL('http://fw' + req.url)
1818
const route = prefix2route[url.pathname]
1919

2020
if (route) {
@@ -32,13 +32,11 @@ module.exports = (config) => {
3232
client.pipe(remote)
3333
remote.pipe(client)
3434
} catch (err) {
35-
client.close(err.closeEventCode || 4000, err.message)
35+
client.close(err.closeEventCode || 4500, err.message)
3636
}
3737
} else {
38-
socket.destroy()
38+
socket.end()
3939
}
40-
} else {
41-
socket.destroy()
42-
}
40+
}
4341
})
4442
}

0 commit comments

Comments
 (0)