Skip to content

Commit 27197c9

Browse files
authored
fix: allow for getUpstream flow for empty wsUpstream option (#351)
* #333 fix: allow for websockets when loading plugin * #333 fix: add ternary check for upstream to WebsocketProxyConstructor * #333 test: add a test for getUpstream with empty wsUpstream set
1 parent 04e2c14 commit 27197c9

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class WebSocketProxy {
9797
headers: {},
9898
...wsClientOptions
9999
}
100-
this.upstream = convertUrlToWebSocket(upstream)
100+
this.upstream = upstream ? convertUrlToWebSocket(upstream) : ''
101101
this.wsUpstream = wsUpstream ? convertUrlToWebSocket(wsUpstream) : ''
102102
this.getUpstream = getUpstream
103103

@@ -229,7 +229,7 @@ function generateRewritePrefix (prefix, opts) {
229229
}
230230

231231
async function fastifyHttpProxy (fastify, opts) {
232-
if (!opts.upstream && !(opts.upstream === '' && opts.replyOptions && typeof opts.replyOptions.getUpstream === 'function')) {
232+
if (!opts.upstream && !opts.websocket && !((opts.upstream === '' || opts.wsUpstream === '') && opts.replyOptions && typeof opts.replyOptions.getUpstream === 'function')) {
233233
throw new Error('upstream must be specified')
234234
}
235235

test/websocket.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,74 @@ test('getUpstream', async (t) => {
194194
])
195195
})
196196

197+
test('getUpstream with unset wsUpstream', async (t) => {
198+
t.plan(9)
199+
200+
const origin = createServer()
201+
const wss = new WebSocket.Server({ server: origin })
202+
t.teardown(wss.close.bind(wss))
203+
t.teardown(origin.close.bind(origin))
204+
205+
const serverMessages = []
206+
wss.on('connection', (ws, request) => {
207+
t.equal(ws.protocol, subprotocolValue)
208+
t.equal(request.headers.cookie, cookieValue)
209+
ws.on('message', (message, binary) => {
210+
serverMessages.push([message.toString(), binary])
211+
// echo
212+
ws.send(message, { binary })
213+
})
214+
})
215+
216+
await promisify(origin.listen.bind(origin))({ port: 0, host: '127.0.0.1' })
217+
218+
const server = Fastify()
219+
220+
let _req
221+
server.server.on('upgrade', (req) => {
222+
_req = req
223+
})
224+
225+
server.register(proxy, {
226+
wsUpstream: '',
227+
replyOptions: {
228+
getUpstream: function (original) {
229+
t.not(original, _req)
230+
t.equal(original.raw, _req)
231+
return `http://127.0.0.1:${origin.address().port}`
232+
}
233+
},
234+
websocket: true
235+
})
236+
237+
await server.listen({ port: 0, host: '127.0.0.1' })
238+
t.teardown(server.close.bind(server))
239+
240+
const options = { headers: { cookie: cookieValue } }
241+
const ws = new WebSocket(`ws://127.0.0.1:${server.server.address().port}`, [subprotocolValue], options)
242+
await once(ws, 'open')
243+
244+
ws.send('hello', { binary: false })
245+
const [reply0, binary0] = await once(ws, 'message')
246+
t.equal(reply0.toString(), 'hello')
247+
t.equal(binary0, false)
248+
249+
ws.send(Buffer.from('fastify'), { binary: true })
250+
const [reply1, binary1] = await once(ws, 'message')
251+
t.equal(reply1.toString(), 'fastify')
252+
t.equal(binary1, true)
253+
254+
t.strictSame(serverMessages, [
255+
['hello', false],
256+
['fastify', true]
257+
])
258+
259+
await Promise.all([
260+
once(ws, 'close'),
261+
server.close()
262+
])
263+
})
264+
197265
test('websocket proxy trigger hooks', async (t) => {
198266
t.plan(8)
199267

0 commit comments

Comments
 (0)