From 5df4f18f3efbb3498f523ef600d7af61137c7d7f 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(uws): remove nested inner functions --- lib/transports-uws/polling.ts | 36 +++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/lib/transports-uws/polling.ts b/lib/transports-uws/polling.ts index 57a1c409..67581abd 100644 --- a/lib/transports-uws/polling.ts +++ b/lib/transports-uws/polling.ts @@ -148,15 +148,6 @@ export class Polling extends Transport { let buffer; let offset = 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) @@ -164,20 +155,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.onDataRequestCleanup(); + res.end("ok"); }; - res.onAborted(onClose); + res.onAborted(() => { + this.onDataRequestCleanup(); + this.onError("data request connection closed prematurely"); + }); res.onData((arrayBuffer, isLast) => { const totalLength = offset + arrayBuffer.byteLength; @@ -201,7 +192,7 @@ export class Polling extends Transport { if (totalLength != expectedContentLength) { this.onError("content-length mismatch"); res.writeStatus("400 Content-Length Mismatch").end(); - cleanup(); + this.onDataRequestCleanup(); return; } onEnd(buffer); @@ -212,6 +203,15 @@ export class Polling extends Transport { }); } + /** + * Cleanup request. + * + * @api private + */ + private onDataRequestCleanup() { + this.dataReq = this.dataRes = null; + } + /** * Processes the incoming data payload. *