Skip to content

Commit

Permalink
fix(uws): fix HTTP long-polling with CORS
Browse files Browse the repository at this point in the history
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
  • Loading branch information
darrachequesne committed Jan 18, 2022
1 parent 49bb7cf commit 45112a3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/userver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,10 @@ class ResponseWrapper {
this.res.end(data);
}

public onData(fn) {
this.res.onData(fn);
}

public onAborted(fn) {
this.res.onAborted(fn);
}
Expand Down
25 changes: 25 additions & 0 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down

0 comments on commit 45112a3

Please sign in to comment.