reuse TextEncoder across instances

This commit is contained in:
Merijn Hendriks 2021-12-28 18:07:09 +01:00
parent 8eef231d69
commit 6266ec0fed

View File

@ -114,20 +114,19 @@ export class HttpServer implements IServer
export class Service export class Service
{ {
private static readonly textEncoder = new TextEncoder()
protected send(res: ServerResponse, data: Uint8Array, type: string, compress: boolean = true): void protected send(res: ServerResponse, data: Uint8Array, type: string, compress: boolean = true): void
{ {
const bytes = (compress) ? Zlib.compress(data) : data; const bytes = (compress) ? Zlib.compress(data) : data;
const headers = { const headers = { "Content-Type": Mime.types[type] || Mime.types["bin"] };
"Content-Type": Mime.types[type] || Mime.types["bin"]
};
res.writeHead(200, "OK", headers); res.writeHead(200, "OK", headers);
res.end(bytes); res.end(bytes);
} }
protected sendJson(res: ServerResponse, data: string, compress: boolean = true): void protected sendJson(res: ServerResponse, data: string, compress: boolean = true): void
{ {
const bytes = new TextEncoder().encode(data); const bytes = Service.textEncoder.encode(data);
this.send(res, bytes, "json", compress); this.send(res, bytes, "json", compress);
} }