diff --git a/lib/transports-uws/polling.ts b/lib/transports-uws/polling.ts index 5cab41ba..ce503a8a 100644 --- a/lib/transports-uws/polling.ts +++ b/lib/transports-uws/polling.ts @@ -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(() => { @@ -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(); }; diff --git a/lib/userver.ts b/lib/userver.ts index 654f037d..98380fbd 100644 --- a/lib/userver.ts +++ b/lib/userver.ts @@ -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); }, }); } @@ -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) { @@ -345,4 +348,10 @@ class ResponseWrapper { fn(); }); } + + public cork(fn) { + if (this.isAborted) return; + + this.res.cork(fn); + } } diff --git a/package-lock.json b/package-lock.json index 17db85ca..ecb92181 100644 --- a/package-lock.json +++ b/package-lock.json @@ -35,7 +35,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" }, "engines": { "node": ">=10.0.0" @@ -2371,8 +2371,8 @@ "dev": true }, "node_modules/uWebSockets.js": { - "version": "20.15.0", - "resolved": "git+https://git@github.com/uNetworking/uWebSockets.js.git#77bc1fd5577ae42b56675912eb8481a31f3fefd2", + "version": "20.30.0", + "resolved": "git+ssh://git@github.com/uNetworking/uWebSockets.js.git#d39d4181daf5b670d44cbc1b18f8c28c85fd4142", "dev": true }, "node_modules/vary": { @@ -4317,9 +4317,9 @@ "dev": true }, "uWebSockets.js": { - "version": "git+https://git@github.com/uNetworking/uWebSockets.js.git#77bc1fd5577ae42b56675912eb8481a31f3fefd2", + "version": "git+ssh://git@github.com/uNetworking/uWebSockets.js.git#d39d4181daf5b670d44cbc1b18f8c28c85fd4142", "dev": true, - "from": "uWebSockets.js@github:uNetworking/uWebSockets.js#v20.15.0" + "from": "uWebSockets.js@uNetworking/uWebSockets.js#v20.30.0" }, "vary": { "version": "1.1.2", diff --git a/package.json b/package.json index 5bfd6425..db3ae718 100644 --- a/package.json +++ b/package.json @@ -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",