From 68e69eb590d188a1939f76fd21b0e85d86367eb7 Mon Sep 17 00:00:00 2001 From: Benjamin DeMann Date: Mon, 2 Oct 2023 14:31:39 -0600 Subject: [PATCH] deduplicate --- src/compiler/compile_typescript_code.ts | 33 +++++++++++++------------ src/compiler/index.ts | 8 ++---- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/src/compiler/compile_typescript_code.ts b/src/compiler/compile_typescript_code.ts index a0fecc4104..80e6061b13 100644 --- a/src/compiler/compile_typescript_code.ts +++ b/src/compiler/compile_typescript_code.ts @@ -6,13 +6,7 @@ import { Result } from './utils/result'; export function compileTypeScriptToJavaScript( main: string, canisterConfig: JSCanisterConfig -): Result< - { - canisterJavaScript: JavaScript; - candidJavaScript: JavaScript; - }, - unknown -> { +): Result { try { const globalThisProcess = ` globalThis.process = { @@ -39,22 +33,29 @@ export function compileTypeScriptToJavaScript( `; - const canisterJavaScript = bundleAndTranspileJs(` + const bundledJavaScript = bundleAndTranspileJs(` ${globalThisProcess} ${imports} `); - const candidJavaScript = bundleAndTranspileJs(` - ${globalThisProcess} - ${imports} -`); + const javaScriptCodeWithRandom = ` + globalThis.crypto = { + getRandomValues: () => { + let array = new Uint8Array(32); - return { - ok: { - canisterJavaScript, - candidJavaScript + for (let i = 0; i < array.length; i++) { + array[i] = Math.floor(Math.random() * 256); + } + + return array; } }; + ${bundledJavaScript} + `; + + return { + ok: javaScriptCodeWithRandom + }; } catch (err) { return { err }; } diff --git a/src/compiler/index.ts b/src/compiler/index.ts index 94d89d45b8..8d79083015 100644 --- a/src/compiler/index.ts +++ b/src/compiler/index.ts @@ -79,11 +79,7 @@ async function azle() { unwrap(azleErrorResult); } - const { canisterJavaScript, candidJavaScript } = - compilationResult.ok as { - canisterJavaScript: string; - candidJavaScript: string; - }; + const canisterJavaScript = compilationResult.ok as string; const workspaceCargoToml: Toml = generateWorkspaceCargoToml( canisterConfig.opt_level ?? '0' @@ -92,7 +88,7 @@ async function azle() { const libCargoToml: Toml = generateLibCargoToml(canisterName, ''); const { candid, canisterMethods } = - generateCandidAndCanisterMethods(candidJavaScript); + generateCandidAndCanisterMethods(canisterJavaScript); rmSync(canisterPath, { recursive: true, force: true }); mkdirSync(canisterPath, { recursive: true });