Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow for getUpstream flow for empty wsUpstream option #351

Merged
merged 3 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class WebSocketProxy {
headers: {},
...wsClientOptions
}
this.upstream = convertUrlToWebSocket(upstream)
this.upstream = upstream ? convertUrlToWebSocket(upstream) : ''
this.wsUpstream = wsUpstream ? convertUrlToWebSocket(wsUpstream) : ''
this.getUpstream = getUpstream

Expand Down Expand Up @@ -229,7 +229,7 @@ function generateRewritePrefix (prefix, opts) {
}

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

Expand Down
68 changes: 68 additions & 0 deletions test/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,74 @@ test('getUpstream', async (t) => {
])
})

test('getUpstream with unset wsUpstream', async (t) => {
t.plan(9)

const origin = createServer()
const wss = new WebSocket.Server({ server: origin })
t.teardown(wss.close.bind(wss))
t.teardown(origin.close.bind(origin))

const serverMessages = []
wss.on('connection', (ws, request) => {
t.equal(ws.protocol, subprotocolValue)
t.equal(request.headers.cookie, cookieValue)
ws.on('message', (message, binary) => {
serverMessages.push([message.toString(), binary])
// echo
ws.send(message, { binary })
})
})

await promisify(origin.listen.bind(origin))({ port: 0, host: '127.0.0.1' })

const server = Fastify()

let _req
server.server.on('upgrade', (req) => {
_req = req
})

server.register(proxy, {
wsUpstream: '',
replyOptions: {
getUpstream: function (original) {
t.not(original, _req)
t.equal(original.raw, _req)
return `http://127.0.0.1:${origin.address().port}`
}
},
websocket: true
})

await server.listen({ port: 0, host: '127.0.0.1' })
t.teardown(server.close.bind(server))

const options = { headers: { cookie: cookieValue } }
const ws = new WebSocket(`ws://127.0.0.1:${server.server.address().port}`, [subprotocolValue], options)
await once(ws, 'open')

ws.send('hello', { binary: false })
const [reply0, binary0] = await once(ws, 'message')
t.equal(reply0.toString(), 'hello')
t.equal(binary0, false)

ws.send(Buffer.from('fastify'), { binary: true })
const [reply1, binary1] = await once(ws, 'message')
t.equal(reply1.toString(), 'fastify')
t.equal(binary1, true)

t.strictSame(serverMessages, [
['hello', false],
['fastify', true]
])

await Promise.all([
once(ws, 'close'),
server.close()
])
})

test('websocket proxy trigger hooks', async (t) => {
t.plan(8)

Expand Down