Skip to content

Commit

Permalink
perf(uws): remove nested inner functions
Browse files Browse the repository at this point in the history
  • Loading branch information
e3dio authored and darrachequesne committed Feb 23, 2022
1 parent 3367440 commit 5df4f18
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions lib/transports-uws/polling.ts
Expand Up @@ -148,36 +148,27 @@ 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)
"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 = offset + arrayBuffer.byteLength;
Expand All @@ -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);
Expand All @@ -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.
*
Expand Down

0 comments on commit 5df4f18

Please sign in to comment.