Skip to content

Commit

Permalink
deduplicate
Browse files Browse the repository at this point in the history
  • Loading branch information
bdemann committed Oct 2, 2023
1 parent f7ee9c3 commit 68e69eb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
33 changes: 17 additions & 16 deletions src/compiler/compile_typescript_code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@ import { Result } from './utils/result';
export function compileTypeScriptToJavaScript(
main: string,
canisterConfig: JSCanisterConfig
): Result<
{
canisterJavaScript: JavaScript;
candidJavaScript: JavaScript;
},
unknown
> {
): Result<JavaScript, unknown> {
try {
const globalThisProcess = `
globalThis.process = {
Expand All @@ -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 };
}
Expand Down
8 changes: 2 additions & 6 deletions src/compiler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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 });
Expand Down

0 comments on commit 68e69eb

Please sign in to comment.