Skip to content

Commit 45112a3

Browse files
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: #637
1 parent 49bb7cf commit 45112a3

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

lib/userver.ts

+4
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,10 @@ class ResponseWrapper {
275275
this.res.end(data);
276276
}
277277

278+
public onData(fn) {
279+
this.res.onData(fn);
280+
}
281+
278282
public onAborted(fn) {
279283
this.res.onAborted(fn);
280284
}

test/server.js

+25
Original file line numberDiff line numberDiff line change
@@ -3552,6 +3552,31 @@ describe("server", () => {
35523552
}
35533553
);
35543554
});
3555+
3556+
it("should work with CORS enabled", done => {
3557+
engine = listen(
3558+
{ cors: { origin: true, headers: ["my-header"], credentials: true } },
3559+
port => {
3560+
const client = new ClientSocket(`ws://localhost:${port}`, {
3561+
transports: ["polling"]
3562+
});
3563+
engine.on("connection", socket => {
3564+
socket.on("message", msg => {
3565+
expect(msg).to.be("hey");
3566+
socket.send("holà");
3567+
});
3568+
});
3569+
client.on("open", () => {
3570+
client.send("hey");
3571+
});
3572+
client.on("message", msg => {
3573+
expect(msg).to.be("holà");
3574+
client.close();
3575+
done();
3576+
});
3577+
}
3578+
);
3579+
});
35553580
});
35563581

35573582
describe("wsEngine option", () => {

0 commit comments

Comments
 (0)