From 8eef231d696cd84416b7d774cc8dd06a71125ad7 Mon Sep 17 00:00:00 2001 From: Merijn Hendriks Date: Tue, 28 Dec 2021 17:53:37 +0100 Subject: [PATCH] Replace Buffer with Uint8Array --- src/common/Haru.Http.ts | 12 ++++++------ src/common/Haru.Utils.ts | 10 +++++----- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/common/Haru.Http.ts b/src/common/Haru.Http.ts index 4b4f5a2..39446a5 100644 --- a/src/common/Haru.Http.ts +++ b/src/common/Haru.Http.ts @@ -92,11 +92,11 @@ export class HttpServer implements IServer private handlePost(req: IncomingMessage, res: ServerResponse, service: Service): void { - let data = Buffer.from(""); + let data = new Uint8Array(); - req.on("data", (chunk: Buffer) => + req.on("data", (chunk: Uint8Array) => { - data = Buffer.concat([data, chunk]); + data = new Uint8Array([ ...data, ...chunk ]); }); req.on("end", () => @@ -114,7 +114,7 @@ export class HttpServer implements IServer export class Service { - protected send(res: ServerResponse, data: Buffer, 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 headers = { @@ -127,7 +127,7 @@ export class Service protected sendJson(res: ServerResponse, data: string, compress: boolean = true): void { - const bytes = Buffer.from(data); + const bytes = new TextEncoder().encode(data); this.send(res, bytes, "json", compress); } @@ -212,7 +212,7 @@ export class WsServer implements IServer public canSendMessage(url: string) { const ws = this.clients[url]; - return (!ws) ? false : ws.readyState == WebSocket.OPEN; + return (!ws) ? false : (ws.readyState == WebSocket.OPEN); } private close(ws: WebSocket): void diff --git a/src/common/Haru.Utils.ts b/src/common/Haru.Utils.ts index 0ff5ffc..29b6243 100644 --- a/src/common/Haru.Utils.ts +++ b/src/common/Haru.Utils.ts @@ -37,7 +37,7 @@ export class Vfs return readFileSync(filepath); } - public static writeFile(filepath: string, data: Buffer | string, append: boolean = false): void + public static writeFile(filepath: string, data: Uint8Array | string, append: boolean = false): void { if (!Vfs.exists(filepath)) { @@ -94,17 +94,17 @@ export class Log export class Zlib { - public static compress(data: Buffer): Buffer + public static compress(data: Uint8Array, level: number = 9): Uint8Array { - return deflateSync(data); + return deflateSync(data, { "level": level }); } - public static decompress(data: Buffer): Buffer + public static decompress(data: Uint8Array): Uint8Array { return inflateSync(data); } - public static isCompressed(data: Buffer): boolean + public static isCompressed(data: Uint8Array): boolean { // Level | CM/CI FLG // ----- | ---------