Skip to content

Commit f3eb0f5

Browse files
committed
fix: btoa isomorphic
1 parent cd373cb commit f3eb0f5

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

src/index.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import axios from 'axios';
22
import { gzip } from 'pako';
3-
import { btoa, randomAddress } from './utils';
3+
import { randomAddress, btoaUniversal } from './utils';
44
import type {
55
GetBlockResponse,
66
GetCode,
@@ -184,15 +184,11 @@ export function addTransaction(tx: Transaction): Promise<AddTransactionResponse>
184184
}
185185

186186
export function compressProgram(program: string): CompressedProgram {
187-
try {
188-
const json = JSON.parse(program);
189-
const stringified = JSON.stringify(json);
190-
const compressedProgram = gzip(stringified);
191-
const base64 = btoa(compressedProgram);
192-
return base64;
193-
} catch {
194-
throw new Error('couldnt compress program');
195-
}
187+
const json = JSON.parse(program);
188+
const stringified = JSON.stringify(json);
189+
const compressedProgram = gzip(stringified);
190+
const base64 = btoaUniversal(compressedProgram);
191+
return base64;
196192
}
197193

198194
export function deployContract(

src/utils.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { CONTRACT_ADDRESS_LOWER_BOUND, CONTRACT_ADDRESS_UPPER_BOUND } from './constants';
22

3-
export const btoa = (b: ArrayBuffer): string => Buffer.from(b).toString('base64');
3+
const isBrowser = typeof window !== 'undefined';
4+
5+
export const btoaUniversal = (b: ArrayBuffer): string =>
6+
isBrowser ? btoa(String.fromCharCode.apply(null, b as any)) : Buffer.from(b).toString('base64');
47

58
export function randomIntFromInterval(min: number, max: number) {
69
return Math.floor(Math.random() * (max - min + 1) + min);

0 commit comments

Comments
 (0)