Skip to content

Commit

Permalink
perf update
Browse files Browse the repository at this point in the history
replace Object.keys(headers).forEach
remove nested inner functions
  • Loading branch information
e3dio committed Feb 23, 2022
1 parent fb11d83 commit 9ccbc34
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions lib/transports-uws/polling.ts
Expand Up @@ -146,36 +146,27 @@ 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)
"Content-Type": "text/html"
};

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 = contentLength + arrayBuffer.byteLength;
Expand All @@ -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.onDataRequestCleanup();
return;
}
onEnd(buffer);
Expand All @@ -210,6 +201,15 @@ export class Polling extends Transport {
});
}

/**
* Cleanup onDataRequest.
*
* @api private
*/
onDataRequestCleanup() {
this.dataReq = this.dataRes = null;
}

/**
* Processes the incoming data payload.
*
Expand Down

0 comments on commit 9ccbc34

Please sign in to comment.