Skip to content

Commit 773a309

Browse files
committed
using micromatch to compare ws prefixes
1 parent 927a951 commit 773a309

File tree

4 files changed

+14
-12
lines changed

4 files changed

+14
-12
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,9 @@ WebSockets proxying is supported since `v3.1.0`. Main considerations:
213213
proxyType: 'websocket';
214214
// https://github.com/faye/faye-websocket-node#initialization-options
215215
proxyConfig?: {};
216-
prefix: string;
216+
// used as micromatch matcher pattern: https://www.npmjs.com/package/micromatch
217+
// prefix examples: '/graphql', '/ws-all/*', ['/rtp', '/rtp/*.flv'], '!/media/*.avi'
218+
prefix: string;
217219
target: string;
218220
// https://github.com/faye/faye-websocket-node#subprotocol-negotiation
219221
subProtocols?: [];

lib/ws-proxy.js

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

3+
const micromatch = require('micromatch')
34
const { onOpenNoOp } = require('./default-hooks').websocket
45

56
module.exports = (config) => {
67
const WebSocket = require('faye-websocket')
78

89
const { routes, server } = config
910

10-
const prefix2route = new Map()
11-
for (const route of routes) {
12-
prefix2route.set(route.prefix, route)
13-
}
11+
routes.forEach(route => {
12+
route._isMatch = micromatch.matcher(route.prefix)
13+
})
1414

1515
server.on('upgrade', async (req, socket, body) => {
1616
if (WebSocket.isWebSocket(req)) {
1717
const url = new URL('http://fw' + req.url)
1818
const prefix = url.pathname || '/'
1919

20-
if (prefix2route.has(prefix)) {
21-
const route = prefix2route.get(prefix)
20+
const route = routes.find(route => route._isMatch(prefix))
21+
if (route) {
2222
const subProtocols = route.subProtocols || []
2323
route.hooks = route.hooks || {}
2424
const onOpen = route.hooks.onOpen || onOpenNoOp

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
"fast-proxy-lite": "^1.1.0",
3333
"http-cache-middleware": "^1.3.8",
3434
"restana": "^4.9.3",
35-
"stream-to-array": "^2.3.0"
35+
"stream-to-array": "^2.3.0",
36+
"micromatch": "^4.0.5"
3637
},
3738
"files": [
3839
"lib/",
@@ -44,7 +45,7 @@
4445
"devDependencies": {
4546
"@types/express": "^4.17.13",
4647
"artillery": "^2.0.0-14",
47-
"aws-sdk": "^2.1106.0",
48+
"aws-sdk": "^2.1111.0",
4849
"chai": "^4.3.6",
4950
"cors": "^2.8.5",
5051
"express": "^4.17.3",
@@ -55,7 +56,6 @@
5556
"helmet": "^5.0.2",
5657
"http-lambda-proxy": "^1.1.4",
5758
"load-balancers": "^1.3.52",
58-
"micromatch": "^4.0.5",
5959
"mocha": "^9.2.2",
6060
"nyc": "^15.1.0",
6161
"opossum": "^6.3.0",
@@ -64,4 +64,4 @@
6464
"response-time": "^2.3.2",
6565
"supertest": "^6.2.2"
6666
}
67-
}
67+
}

test/ws-proxy.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describe('ws-proxy', () => {
3737
target: 'ws://127.0.0.1:3000'
3838
}, {
3939
proxyType: 'websocket',
40-
prefix: '/echo-auth',
40+
prefix: '/*-auth',
4141
target: 'ws://127.0.0.1:3000',
4242
hooks: {
4343
onOpen (ws, searchParams) {

0 commit comments

Comments
 (0)