From 9bba305a827cca3b8431e7a6b9f8d5074f841236 Mon Sep 17 00:00:00 2001 From: Peter Smith Date: Thu, 22 Aug 2024 11:00:48 +0100 Subject: [PATCH] fix: made bytecode compression browser compatible (#2985) * fix: made bytecode compression browser compatible * chore: changeset * chore: release to NPM * chore: removed deployment --------- Co-authored-by: Anderson Arboleya Co-authored-by: Daniel Bate --- .changeset/eighty-ducks-burn.md | 5 +++++ packages/utils/src/utils/bytecode.ts | 28 +++++++++++++++++----------- 2 files changed, 22 insertions(+), 11 deletions(-) create mode 100644 .changeset/eighty-ducks-burn.md diff --git a/.changeset/eighty-ducks-burn.md b/.changeset/eighty-ducks-burn.md new file mode 100644 index 00000000000..53bf73f9c72 --- /dev/null +++ b/.changeset/eighty-ducks-burn.md @@ -0,0 +1,5 @@ +--- +"@fuel-ts/utils": patch +--- + +fix: made bytecode compression browser compatible diff --git a/packages/utils/src/utils/bytecode.ts b/packages/utils/src/utils/bytecode.ts index 940d31305d0..74534c0188c 100644 --- a/packages/utils/src/utils/bytecode.ts +++ b/packages/utils/src/utils/bytecode.ts @@ -3,22 +3,28 @@ import { gzipSync, gunzipSync } from 'fflate'; import { arrayify } from './arrayify'; -export const compressBytecode = (bytecode?: BytesLike) => { - if (!bytecode) { +export const compressBytecode = (bytecodeAsBinary?: BytesLike) => { + if (!bytecodeAsBinary) { return ''; } - const bytecodeBytes = arrayify(bytecode); - const bytecodeGzipped = gzipSync(bytecodeBytes); - const bytecodeEncoded = Buffer.from(bytecodeGzipped).toString('base64'); + const bytecodeCompressBytes = arrayify(bytecodeAsBinary); + const bytecodeCompressGzipped = gzipSync(bytecodeCompressBytes); + const bytecodeCompressBinary = String.fromCharCode.apply( + null, + new Uint8Array(bytecodeCompressGzipped) as unknown as number[] + ); + const bytecodeCompressEncoded = btoa(bytecodeCompressBinary); - return bytecodeEncoded; + return bytecodeCompressEncoded; }; -export const decompressBytecode = (bytecode: string) => { - const bytecodeDecoded = Buffer.from(bytecode, 'base64').toString('binary'); - const bytecodeGzipped = Buffer.from(bytecodeDecoded, 'binary'); - const bytecodeBytes = gunzipSync(bytecodeGzipped); +export const decompressBytecode = (bytecodeAsBase64: string) => { + const bytecodeDecompressBinary = atob(bytecodeAsBase64); + const bytecodeDecompressDecoded = new Uint8Array(bytecodeDecompressBinary.length).map((_, i) => + bytecodeDecompressBinary.charCodeAt(i) + ); + const bytecodeDecompressBytes = gunzipSync(bytecodeDecompressDecoded); - return bytecodeBytes; + return bytecodeDecompressBytes; };