Skip to content

Commit

Permalink
improve base64 speed
Browse files Browse the repository at this point in the history
  • Loading branch information
lonnywong committed Nov 4, 2023
1 parent 58f65c5 commit 7219dfc
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/comm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,24 @@ export function strToArrBuf(str: string): ArrayBuffer {
return strToUint8(str).buffer;
}

const _hasBuffer = typeof Buffer === "function";

export function encodeBuffer(buf: string | Uint8Array): string {
return Base64.fromByteArray(Pako.deflate(buf));
const buffer = Pako.deflate(buf);
if (_hasBuffer) {
return Buffer.from(buffer).toString("base64");
}
return Base64.fromByteArray(buffer);
}

export function decodeBuffer(buf: string): Uint8Array {
return Pako.inflate(Base64.toByteArray(buf));
let buffer: Uint8Array;
if (_hasBuffer) {
buffer = Buffer.from(buf, "base64");
} else {
buffer = Base64.toByteArray(buf);
}
return Pako.inflate(buffer);
}

export class TrzszError extends Error {
Expand Down

0 comments on commit 7219dfc

Please sign in to comment.