Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: made bytecode compression browser compatible #2985

Merged
merged 6 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
danielbate marked this conversation as resolved.
Show resolved Hide resolved
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;
};
Loading