Skip to content

Commit

Permalink
fix: made bytecode compression browser compatible (#2985)
Browse files Browse the repository at this point in the history
* fix: made bytecode compression browser compatible

* chore: changeset

* chore: release to NPM

* chore: removed deployment

---------

Co-authored-by: Anderson Arboleya <[email protected]>
Co-authored-by: Daniel Bate <[email protected]>
  • Loading branch information
3 people authored Aug 22, 2024
1 parent 7209395 commit 9bba305
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/eighty-ducks-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/utils": patch
---

fix: made bytecode compression browser compatible
28 changes: 17 additions & 11 deletions packages/utils/src/utils/bytecode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

0 comments on commit 9bba305

Please sign in to comment.