Skip to content

Commit

Permalink
refactor: adapt to latest uWebSockets.js changes
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne committed Jun 16, 2023
1 parent 123b68c commit 1bfa9cd
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 21 deletions.
12 changes: 8 additions & 4 deletions lib/transports-uws/polling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@ export class Polling extends Transport {
const onEnd = (buffer) => {
this.onData(buffer.toString());
this.onDataRequestCleanup();
res.end("ok");
res.cork(() => {
res.end("ok");
});
};

res.onAborted(() => {
Expand Down Expand Up @@ -312,10 +314,12 @@ export class Polling extends Transport {

const respond = (data) => {
this.headers(this.req, headers);
Object.keys(headers).forEach((key) => {
this.res.writeHeader(key, String(headers[key]));
this.res.cork(() => {
Object.keys(headers).forEach((key) => {
this.res.writeHeader(key, String(headers[key]));
});
this.res.end(data);
});
this.res.end(data);
callback();
};

Expand Down
31 changes: 20 additions & 11 deletions lib/userver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,25 @@ export class uServer extends BaseServer {
(app as TemplatedApp)
.any(path, this.handleRequest.bind(this))
//
.ws(path, {
.ws<{ transport: any }>(path, {
compression: options.compression,
idleTimeout: options.idleTimeout,
maxBackpressure: options.maxBackpressure,
maxPayloadLength: this.opts.maxHttpBufferSize,
upgrade: this.handleUpgrade.bind(this),
open: (ws) => {
ws.transport.socket = ws;
ws.transport.writable = true;
ws.transport.emit("drain");
const transport = ws.getUserData().transport;
transport.socket = ws;
transport.writable = true;
transport.emit("drain");
},
message: (ws, message, isBinary) => {
ws.transport.onData(
ws.getUserData().transport.onData(
isBinary ? message : Buffer.from(message).toString()
);
},
close: (ws, code, message) => {
ws.transport.onClose(code, message);
ws.getUserData().transport.onClose(code, message);
},
});
}
Expand Down Expand Up @@ -323,11 +324,13 @@ class ResponseWrapper {
public end(data) {
if (this.isAborted) return;

if (!this.statusWritten) {
// status will be inferred as "200 OK"
this.writeBufferedHeaders();
}
this.res.end(data);
this.res.cork(() => {
if (!this.statusWritten) {
// status will be inferred as "200 OK"
this.writeBufferedHeaders();
}
this.res.end(data);
});
}

public onData(fn) {
Expand All @@ -345,4 +348,10 @@ class ResponseWrapper {
fn();
});
}

public cork(fn) {
if (this.isAborted) return;

this.res.cork(fn);
}
}
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"rimraf": "^3.0.2",
"superagent": "^3.8.1",
"typescript": "^4.4.3",
"uWebSockets.js": "github:uNetworking/uWebSockets.js#v20.15.0"
"uWebSockets.js": "github:uNetworking/uWebSockets.js#v20.30.0"
},
"scripts": {
"compile": "rimraf ./build && tsc",
Expand Down

0 comments on commit 1bfa9cd

Please sign in to comment.