From 45112a30d1af4cc25b21a5d658a748583cb64ed4 Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Tue, 18 Jan 2022 17:03:03 +0100 Subject: [PATCH] fix(uws): fix HTTP long-polling with CORS When binding to an uWebSockets.js application, the server could crash with the following error: ``` TypeError: res.onData is not a function at Polling.onDataRequest (build/transports-uws/polling.js:133:13) at Polling.onRequest (build/transports-uws/polling.js:47:18) at callback (build/userver.js:80:56) ``` Related: https://github.com/socketio/engine.io/issues/637 --- lib/userver.ts | 4 ++++ test/server.js | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/lib/userver.ts b/lib/userver.ts index 38c9b5a8..66fa4041 100644 --- a/lib/userver.ts +++ b/lib/userver.ts @@ -275,6 +275,10 @@ class ResponseWrapper { this.res.end(data); } + public onData(fn) { + this.res.onData(fn); + } + public onAborted(fn) { this.res.onAborted(fn); } diff --git a/test/server.js b/test/server.js index 3aff7c0e..d7d64428 100644 --- a/test/server.js +++ b/test/server.js @@ -3552,6 +3552,31 @@ describe("server", () => { } ); }); + + it("should work with CORS enabled", done => { + engine = listen( + { cors: { origin: true, headers: ["my-header"], credentials: true } }, + port => { + const client = new ClientSocket(`ws://localhost:${port}`, { + transports: ["polling"] + }); + engine.on("connection", socket => { + socket.on("message", msg => { + expect(msg).to.be("hey"); + socket.send("holà"); + }); + }); + client.on("open", () => { + client.send("hey"); + }); + client.on("message", msg => { + expect(msg).to.be("holà"); + client.close(); + done(); + }); + } + ); + }); }); describe("wsEngine option", () => {