diff --git a/lib/socket.ts b/lib/socket.ts index f520605344..0d065c0263 100644 --- a/lib/socket.ts +++ b/lib/socket.ts @@ -292,16 +292,16 @@ export class Socket< */ private buildHandshake(auth: object): Handshake { return { - headers: this.request.headers, + headers: this.request?.headers || {}, time: new Date() + "", address: this.conn.remoteAddress, - xdomain: !!this.request.headers.origin, + xdomain: !!this.request?.headers.origin, // @ts-ignore - secure: !!this.request.connection.encrypted, + secure: !this.request || !!this.request.connection.encrypted, issued: +new Date(), - url: this.request.url!, + url: this.request?.url!, // @ts-ignore - query: this.request._query, + query: this.request?._query || {}, auth, }; } diff --git a/test/socket.ts b/test/socket.ts index 1fb2f9eafa..10e2924a42 100644 --- a/test/socket.ts +++ b/test/socket.ts @@ -684,6 +684,23 @@ describe("socket", () => { }); }); + it("should handshake a client without access to the Engine.IO request (WebTransport-only connection)", (done) => { + const io = new Server(0); + const clientSocket = createClient(io, "/"); + + io.engine.on("connection", (socket) => { + delete socket.request; + }); + + io.on("connection", (socket) => { + expect(socket.handshake.secure).to.be(true); + expect(socket.handshake.headers).to.eql({}); + expect(socket.handshake.query).to.eql({}); + + success(done, io, clientSocket); + }); + }); + it("should handle very large json", function (done) { this.timeout(30000); const io = new Server(0, { perMessageDeflate: false });