@fastify/websocket
Advanced tools
Comparing version 7.2.0 to 8.0.0
48
index.js
@@ -41,17 +41,15 @@ 'use strict' | ||
websocketListenServer.on('upgrade', (rawRequest, socket, head) => { | ||
function onUpgrade (rawRequest, socket, head) { | ||
// Save a reference to the socket and then dispatch the request through the normal fastify router so that it will invoke hooks and then eventually a route handler that might upgrade the socket. | ||
rawRequest[kWs] = socket | ||
rawRequest[kWsHead] = head | ||
if (closing) { | ||
handleUpgrade(rawRequest, (connection) => { | ||
connection.socket.close(1001) | ||
}) | ||
} else { | ||
const rawResponse = new ServerResponse(rawRequest) | ||
const rawResponse = new ServerResponse(rawRequest) | ||
try { | ||
rawResponse.assignSocket(socket) | ||
fastify.routing(rawRequest, rawResponse) | ||
} catch (err) { | ||
fastify.log.warn({ err }, 'websocket upgrade failed') | ||
} | ||
}) | ||
} | ||
websocketListenServer.on('upgrade', onUpgrade) | ||
@@ -151,20 +149,19 @@ const handleUpgrade = (rawRequest, callback) => { | ||
let closing = false | ||
// Fastify is missing a pre-close event, or the ability to | ||
// add a hook before the server.close call. We need to resort | ||
// to monkeypatching for now. | ||
const oldClose = fastify.server.close | ||
fastify.server.close = function (cb) { | ||
closing = true | ||
fastify.addHook('preClose', function (done) { | ||
const server = this.websocketServer | ||
if (server.clients) { | ||
for (const client of server.clients) { | ||
client.close() | ||
} | ||
} | ||
fastify.server.removeListener('upgrade', onUpgrade) | ||
done() | ||
}) | ||
// Call oldClose first so that we stop listening. This ensures the | ||
// server.clients list will be up to date when we start closing below. | ||
oldClose.call(this, cb) | ||
function close (fastify, done) { | ||
const server = fastify.websocketServer | ||
if (!server.clients) return | ||
for (const client of server.clients) { | ||
client.close() | ||
} | ||
server.close(done) | ||
} | ||
@@ -190,9 +187,4 @@ | ||
function close (fastify, done) { | ||
const server = fastify.websocketServer | ||
server.close(done) | ||
} | ||
module.exports = fp(fastifyWebsocket, { | ||
fastify: '>= 4.0.0', | ||
fastify: '^4.16.0', | ||
name: '@fastify/websocket' | ||
@@ -199,0 +191,0 @@ }) |
{ | ||
"name": "@fastify/websocket", | ||
"version": "7.2.0", | ||
"version": "8.0.0", | ||
"description": "basic websocket support for fastify", | ||
@@ -33,3 +33,3 @@ "main": "index.js", | ||
"@types/ws": "^8.2.2", | ||
"fastify": "^4.0.0", | ||
"fastify": "^4.16.0", | ||
"split2": "^4.1.0", | ||
@@ -36,0 +36,0 @@ "standard": "^17.0.0", |
@@ -401,7 +401,4 @@ 'use strict' | ||
p = once(ws, 'close').then(() => { | ||
t.pass('client 2 closed') | ||
}) | ||
ws.on('open', () => { | ||
p = once(ws, 'close').catch((err) => { | ||
t.equal(err.message, 'Unexpected server response: 503') | ||
oldClose.call(this, cb) | ||
@@ -415,3 +412,3 @@ }) | ||
t.pass('received client connection') | ||
t.teardown(() => connection.destroy()) | ||
connection.destroy() | ||
// this connection stays alive until we close the server | ||
@@ -591,1 +588,36 @@ }) | ||
}) | ||
test('remove all others websocket handlers on close', async (t) => { | ||
const fastify = Fastify() | ||
await fastify.register(fastifyWebsocket) | ||
await fastify.listen({ port: 0 }) | ||
await fastify.close() | ||
t.equal(fastify.server.listeners('upgrade').length, 0) | ||
}) | ||
test('clashing upgrade handler', async (t) => { | ||
const fastify = Fastify() | ||
t.teardown(() => fastify.close()) | ||
fastify.server.on('upgrade', (req, socket, head) => { | ||
const res = new http.ServerResponse(req) | ||
res.assignSocket(socket) | ||
res.end() | ||
socket.destroy() | ||
}) | ||
await fastify.register(fastifyWebsocket) | ||
fastify.get('/', { websocket: true }, (connection) => { | ||
t.fail('this should never be invoked') | ||
}) | ||
await fastify.listen({ port: 0 }) | ||
const ws = new WebSocket('ws://localhost:' + fastify.server.address().port) | ||
await once(ws, 'error') | ||
}) |
@@ -279,3 +279,3 @@ 'use strict' | ||
httpClient.write('GET / HTTP/1.1\r\n\r\n') | ||
httpClient.write('GET / HTTP/1.1\r\nHOST: localhost\r\n\r\n') | ||
httpClient.once('data', data => { | ||
@@ -356,3 +356,3 @@ t.match(data.toString(), /Fail/i) | ||
t.teardown(httpClient.destroy.bind(httpClient)) | ||
httpClient.write('GET /echo HTTP/1.1\r\n\r\n') | ||
httpClient.write('GET /echo HTTP/1.1\r\nHOST: localhost\r\n\r\n') | ||
httpClient.once('data', data => { | ||
@@ -359,0 +359,0 @@ t.match(data.toString(), /not found/i) |
@@ -309,3 +309,3 @@ 'use strict' | ||
const httpClient = net.createConnection({ port }, () => { | ||
httpClient.write('GET / HTTP/1.1\r\n\r\n') | ||
httpClient.write('GET / HTTP/1.1\r\nHOST: localhost\r\n\r\n') | ||
httpClient.once('data', data => { | ||
@@ -312,0 +312,0 @@ t.match(data.toString(), /hi from handler/i) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
82211
1849