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

Forward upgrade from secondary server to primary #4190

Merged
merged 1 commit into from
Aug 10, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ function multipleBindings (mainServer, httpHandler, serverOpts, listenOptions, o

const secondaryServer = getServerInstance(serverOpts, httpHandler)
const closeSecondary = () => { secondaryServer.close(() => {}) }
secondaryServer.on('upgrade', mainServer.emit.bind(mainServer, 'upgrade'))
mainServer.on('unref', closeSecondary)
mainServer.on('close', closeSecondary)
mainServer.on('error', closeSecondary)
Expand Down
53 changes: 53 additions & 0 deletions test/upgrade.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict'

const { test, skip } = require('tap')
const { lookup } = require('dns').promises
const Fastify = require('..')
const { connect } = require('net')
const { once } = require('events')

async function setup () {
const results = await lookup('localhost', { all: true })
if (results.length === 1) {
skip('requires both IPv4 and IPv6')
return
}

test('upgrade to both servers', async t => {
t.plan(2)
const app = Fastify()
app.server.on('upgrade', (req, socket, head) => {
t.pass(`upgrade event ${JSON.stringify(socket.address())}`)
socket.end()
})
app.get('/', (req, res) => {
})
await app.listen()
t.teardown(app.close.bind(app))

{
const client = connect(app.server.address().port, '127.0.0.1')
client.write('GET / HTTP/1.1\r\n')
client.write('Upgrade: websocket\r\n')
client.write('Connection: Upgrade\r\n')
client.write('Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r\n')
client.write('Sec-WebSocket-Protocol: com.xxx.service.v1\r\n')
client.write('Sec-WebSocket-Version: 13\r\n\r\n')
client.write('\r\n\r\n')
await once(client, 'close')
}

{
const client = connect(app.server.address().port, '::1')
client.write('GET / HTTP/1.1\r\n')
client.write('Upgrade: websocket\r\n')
client.write('Connection: Upgrade\r\n')
client.write('Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r\n')
client.write('Sec-WebSocket-Protocol: com.xxx.service.v1\r\n')
client.write('Sec-WebSocket-Version: 13\r\n\r\n')
await once(client, 'close')
}
})
}

setup()