Send Zlib responses as async (Promisify) (!426)
Makes Zlib's inflate & deflate functions run as async by promisifying them. Reviewed-on: https://dev.sp-tarkov.com/SPT/Server/pulls/426 Co-authored-by: Archangel <jesse@archangel.wtf> Co-committed-by: Archangel <jesse@archangel.wtf>
This commit is contained in:
parent
7e3ae57e7e
commit
ce94a22e18
@ -8,6 +8,10 @@ import { LocalisationService } from "@spt/services/LocalisationService";
|
|||||||
import { HttpResponseUtil } from "@spt/utils/HttpResponseUtil";
|
import { HttpResponseUtil } from "@spt/utils/HttpResponseUtil";
|
||||||
import { JsonUtil } from "@spt/utils/JsonUtil";
|
import { JsonUtil } from "@spt/utils/JsonUtil";
|
||||||
import { inject, injectAll, injectable } from "tsyringe";
|
import { inject, injectAll, injectable } from "tsyringe";
|
||||||
|
import util from "node:util";
|
||||||
|
|
||||||
|
const zlibInflate = util.promisify(zlib.inflate);
|
||||||
|
const zlibDeflate = util.promisify(zlib.deflate);
|
||||||
|
|
||||||
@injectable()
|
@injectable()
|
||||||
export class SptHttpListener implements IHttpListener {
|
export class SptHttpListener implements IHttpListener {
|
||||||
@ -29,7 +33,7 @@ export class SptHttpListener implements IHttpListener {
|
|||||||
switch (req.method) {
|
switch (req.method) {
|
||||||
case "GET": {
|
case "GET": {
|
||||||
const response = await this.getResponse(sessionId, req, undefined);
|
const response = await this.getResponse(sessionId, req, undefined);
|
||||||
this.sendResponse(sessionId, req, resp, undefined, response);
|
await this.sendResponse(sessionId, req, resp, undefined, response);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// these are handled almost identically.
|
// these are handled almost identically.
|
||||||
@ -56,13 +60,13 @@ export class SptHttpListener implements IHttpListener {
|
|||||||
const requestIsCompressed = req.headers.requestcompressed !== "0";
|
const requestIsCompressed = req.headers.requestcompressed !== "0";
|
||||||
const requestCompressed = req.method === "PUT" || requestIsCompressed;
|
const requestCompressed = req.method === "PUT" || requestIsCompressed;
|
||||||
|
|
||||||
const value = requestCompressed ? zlib.inflateSync(buffer) : buffer;
|
const value = requestCompressed ? await zlibInflate(buffer) : buffer;
|
||||||
if (!requestIsCompressed) {
|
if (!requestIsCompressed) {
|
||||||
this.logger.debug(value.toString(), true);
|
this.logger.debug(value.toString(), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await this.getResponse(sessionId, req, value);
|
const response = await this.getResponse(sessionId, req, value);
|
||||||
this.sendResponse(sessionId, req, resp, value, response);
|
await this.sendResponse(sessionId, req, resp, value, response);
|
||||||
});
|
});
|
||||||
|
|
||||||
break;
|
break;
|
||||||
@ -83,13 +87,13 @@ export class SptHttpListener implements IHttpListener {
|
|||||||
* @param body Buffer
|
* @param body Buffer
|
||||||
* @param output Server generated response data
|
* @param output Server generated response data
|
||||||
*/
|
*/
|
||||||
public sendResponse(
|
public async sendResponse(
|
||||||
sessionID: string,
|
sessionID: string,
|
||||||
req: IncomingMessage,
|
req: IncomingMessage,
|
||||||
resp: ServerResponse,
|
resp: ServerResponse,
|
||||||
body: Buffer,
|
body: Buffer,
|
||||||
output: string,
|
output: string,
|
||||||
): void {
|
): Promise<void> {
|
||||||
const bodyInfo = this.getBodyInfo(body);
|
const bodyInfo = this.getBodyInfo(body);
|
||||||
|
|
||||||
if (this.isDebugRequest(req)) {
|
if (this.isDebugRequest(req)) {
|
||||||
@ -106,7 +110,7 @@ export class SptHttpListener implements IHttpListener {
|
|||||||
serialiser.serialize(sessionID, req, resp, bodyInfo);
|
serialiser.serialize(sessionID, req, resp, bodyInfo);
|
||||||
} else {
|
} else {
|
||||||
// No serializer can handle the request (majority of requests dont), zlib the output and send response back
|
// No serializer can handle the request (majority of requests dont), zlib the output and send response back
|
||||||
this.sendZlibJson(resp, output, sessionID);
|
await this.sendZlibJson(resp, output, sessionID);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.logRequest(req, output);
|
this.logRequest(req, output);
|
||||||
@ -165,8 +169,9 @@ export class SptHttpListener implements IHttpListener {
|
|||||||
resp.end(output);
|
resp.end(output);
|
||||||
}
|
}
|
||||||
|
|
||||||
public sendZlibJson(resp: ServerResponse, output: string, sessionID: string): void {
|
public async sendZlibJson(resp: ServerResponse, output: string, sessionID: string): Promise<void> {
|
||||||
zlib.deflate(output, (_, buf) => resp.end(buf));
|
const buf = await zlibDeflate(output);
|
||||||
|
resp.end(buf);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user