From 7c45aa1f561da80db777d5affe4ab184471df574 Mon Sep 17 00:00:00 2001 From: e3dio <85405955+e3dio@users.noreply.github.com> Date: Tue, 22 Feb 2022 13:38:19 -0700 Subject: [PATCH] perf update --- lib/transports-uws/polling.ts | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/lib/transports-uws/polling.ts b/lib/transports-uws/polling.ts index 1bf01a96..169689e7 100644 --- a/lib/transports-uws/polling.ts +++ b/lib/transports-uws/polling.ts @@ -146,15 +146,6 @@ export class Polling extends Transport { let buffer; let contentLength = 0; - const cleanup = () => { - this.dataReq = this.dataRes = null; - }; - - const onClose = () => { - cleanup(); - this.onError("data request connection closed prematurely"); - }; - const headers = { // text/html is required instead of text/plain to avoid an // unwanted download dialog on certain user-agents (GH-43) @@ -162,20 +153,20 @@ export class Polling extends Transport { }; this.headers(req, headers); - Object.keys(headers).forEach(key => { + for (let key in headers) { res.writeHeader(key, String(headers[key])); - }); + } const onEnd = (buffer) => { this.onData(buffer.toString()); - - if (this.readyState !== "closing") { - res.end("ok"); - } - cleanup(); + this.dataReq = this.dataRes = null; + res.end("ok"); }; - res.onAborted(onClose); + res.onAborted(() => { + this.dataReq = this.dataRes = null; + this.onError("data request connection closed prematurely"); + }); res.onData((arrayBuffer, isLast) => { const totalLength = contentLength + arrayBuffer.byteLength; @@ -199,7 +190,7 @@ export class Polling extends Transport { if (totalLength != contentLengthHeader) { this.onError("content-length mismatch"); res.writeStatus("400 content-length mismatch").end(); - cleanup(); + this.dataReq = this.dataRes = null; return; } onEnd(buffer);