Skip to content

Commit 9ccbc34

Browse files
committed
perf update
replace Object.keys(headers).forEach remove nested inner functions
1 parent fb11d83 commit 9ccbc34

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

lib/transports-uws/polling.ts

+18-18
Original file line numberDiff line numberDiff line change
@@ -146,36 +146,27 @@ export class Polling extends Transport {
146146
let buffer;
147147
let contentLength = 0;
148148

149-
const cleanup = () => {
150-
this.dataReq = this.dataRes = null;
151-
};
152-
153-
const onClose = () => {
154-
cleanup();
155-
this.onError("data request connection closed prematurely");
156-
};
157-
158149
const headers = {
159150
// text/html is required instead of text/plain to avoid an
160151
// unwanted download dialog on certain user-agents (GH-43)
161152
"Content-Type": "text/html"
162153
};
163154

164155
this.headers(req, headers);
165-
Object.keys(headers).forEach(key => {
156+
for (let key in headers) {
166157
res.writeHeader(key, String(headers[key]));
167-
});
158+
}
168159

169160
const onEnd = (buffer) => {
170161
this.onData(buffer.toString());
171-
172-
if (this.readyState !== "closing") {
173-
res.end("ok");
174-
}
175-
cleanup();
162+
this.onDataRequestCleanup();
163+
res.end("ok");
176164
};
177165

178-
res.onAborted(onClose);
166+
res.onAborted(() => {
167+
this.onDataRequestCleanup();
168+
this.onError("data request connection closed prematurely");
169+
});
179170

180171
res.onData((arrayBuffer, isLast) => {
181172
const totalLength = contentLength + arrayBuffer.byteLength;
@@ -199,7 +190,7 @@ export class Polling extends Transport {
199190
if (totalLength != contentLengthHeader) {
200191
this.onError("content-length mismatch");
201192
res.writeStatus("400 content-length mismatch").end();
202-
cleanup();
193+
this.onDataRequestCleanup();
203194
return;
204195
}
205196
onEnd(buffer);
@@ -210,6 +201,15 @@ export class Polling extends Transport {
210201
});
211202
}
212203

204+
/**
205+
* Cleanup onDataRequest.
206+
*
207+
* @api private
208+
*/
209+
onDataRequestCleanup() {
210+
this.dataReq = this.dataRes = null;
211+
}
212+
213213
/**
214214
* Processes the incoming data payload.
215215
*

0 commit comments

Comments
 (0)