Skip to content

Commit 1bfa9cd

Browse files
refactor: adapt to latest uWebSockets.js changes
Reference: https://github.com/uNetworking/uWebSockets.js/releases
1 parent 123b68c commit 1bfa9cd

File tree

4 files changed

+34
-21
lines changed

4 files changed

+34
-21
lines changed

lib/transports-uws/polling.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,9 @@ export class Polling extends Transport {
162162
const onEnd = (buffer) => {
163163
this.onData(buffer.toString());
164164
this.onDataRequestCleanup();
165-
res.end("ok");
165+
res.cork(() => {
166+
res.end("ok");
167+
});
166168
};
167169

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

313315
const respond = (data) => {
314316
this.headers(this.req, headers);
315-
Object.keys(headers).forEach((key) => {
316-
this.res.writeHeader(key, String(headers[key]));
317+
this.res.cork(() => {
318+
Object.keys(headers).forEach((key) => {
319+
this.res.writeHeader(key, String(headers[key]));
320+
});
321+
this.res.end(data);
317322
});
318-
this.res.end(data);
319323
callback();
320324
};
321325

lib/userver.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,24 +70,25 @@ export class uServer extends BaseServer {
7070
(app as TemplatedApp)
7171
.any(path, this.handleRequest.bind(this))
7272
//
73-
.ws(path, {
73+
.ws<{ transport: any }>(path, {
7474
compression: options.compression,
7575
idleTimeout: options.idleTimeout,
7676
maxBackpressure: options.maxBackpressure,
7777
maxPayloadLength: this.opts.maxHttpBufferSize,
7878
upgrade: this.handleUpgrade.bind(this),
7979
open: (ws) => {
80-
ws.transport.socket = ws;
81-
ws.transport.writable = true;
82-
ws.transport.emit("drain");
80+
const transport = ws.getUserData().transport;
81+
transport.socket = ws;
82+
transport.writable = true;
83+
transport.emit("drain");
8384
},
8485
message: (ws, message, isBinary) => {
85-
ws.transport.onData(
86+
ws.getUserData().transport.onData(
8687
isBinary ? message : Buffer.from(message).toString()
8788
);
8889
},
8990
close: (ws, code, message) => {
90-
ws.transport.onClose(code, message);
91+
ws.getUserData().transport.onClose(code, message);
9192
},
9293
});
9394
}
@@ -323,11 +324,13 @@ class ResponseWrapper {
323324
public end(data) {
324325
if (this.isAborted) return;
325326

326-
if (!this.statusWritten) {
327-
// status will be inferred as "200 OK"
328-
this.writeBufferedHeaders();
329-
}
330-
this.res.end(data);
327+
this.res.cork(() => {
328+
if (!this.statusWritten) {
329+
// status will be inferred as "200 OK"
330+
this.writeBufferedHeaders();
331+
}
332+
this.res.end(data);
333+
});
331334
}
332335

333336
public onData(fn) {
@@ -345,4 +348,10 @@ class ResponseWrapper {
345348
fn();
346349
});
347350
}
351+
352+
public cork(fn) {
353+
if (this.isAborted) return;
354+
355+
this.res.cork(fn);
356+
}
348357
}

package-lock.json

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
"rimraf": "^3.0.2",
5858
"superagent": "^3.8.1",
5959
"typescript": "^4.4.3",
60-
"uWebSockets.js": "github:uNetworking/uWebSockets.js#v20.15.0"
60+
"uWebSockets.js": "github:uNetworking/uWebSockets.js#v20.30.0"
6161
},
6262
"scripts": {
6363
"compile": "rimraf ./build && tsc",

0 commit comments

Comments
 (0)