From 29f1cd3549af63cd88b83853d50c572bdc7feef8 Mon Sep 17 00:00:00 2001 From: Denis Davidyuk Date: Sun, 19 Nov 2023 12:16:48 +0530 Subject: [PATCH] feat(compiler): add CompilerCli8 class --- package.json | 2 +- src/contract/compiler/Cli.ts | 5 +++-- src/contract/compiler/Cli8.ts | 17 +++++++++++++++++ src/index.ts | 1 + tooling/fetch-aesophia-cli-8.mjs | 25 +++++++++++++++++++++++++ 5 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 src/contract/compiler/Cli8.ts create mode 100644 tooling/fetch-aesophia-cli-8.mjs diff --git a/package.json b/package.json index 4b271c3544..52e72b6628 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,7 @@ }, "sideEffects": false, "scripts": { - "build:assets": "node tooling/fetch-aesophia-cli.mjs", + "build:assets": "node tooling/fetch-aesophia-cli.mjs && node tooling/fetch-aesophia-cli-8.mjs", "build:types": "tsc && node tooling/downlevel/run.mjs", "build:es": "babel src --config-file ./babel.esm.config.js --out-dir es --extensions .js,.ts --out-file-extension .mjs --source-maps true", "build:api:node": "autorest tooling/autorest/node.yaml", diff --git a/src/contract/compiler/Cli.ts b/src/contract/compiler/Cli.ts index 54d077d790..8b2bb0228a 100644 --- a/src/contract/compiler/Cli.ts +++ b/src/contract/compiler/Cli.ts @@ -9,7 +9,7 @@ import { CompilerError, InternalError, UnsupportedVersionError } from '../../uti import semverSatisfies from '../../utils/semver-satisfies'; import { ensureError } from '../../utils/other'; -const getPackagePath = (): string => { +export const getPackagePath = (): string => { const path = dirname(fileURLToPath(import.meta.url)); if (basename(path) === 'dist') return resolve(path, '..'); if (basename(path) === 'compiler') return resolve(path, '../../..'); @@ -19,6 +19,7 @@ const getPackagePath = (): string => { /** * A wrapper around aesophia_cli, available only in Node.js. * Requires Erlang installed, assumes that `escript` is available in PATH. + * @category contract */ export default class CompilerCli extends CompilerBase { #path: string; @@ -38,7 +39,7 @@ export default class CompilerCli extends CompilerBase { this.#path = compilerPath; if (ignoreVersion !== true) { this.#ensureCompatibleVersion = this.version().then((version) => { - const versions = [version, '7.2.1', '8.0.0'] as const; + const versions = [version, '7.2.1', '9.0.0'] as const; if (!semverSatisfies(...versions)) throw new UnsupportedVersionError('compiler', ...versions); }); } diff --git a/src/contract/compiler/Cli8.ts b/src/contract/compiler/Cli8.ts new file mode 100644 index 0000000000..80ad1a8d8b --- /dev/null +++ b/src/contract/compiler/Cli8.ts @@ -0,0 +1,17 @@ +import { resolve } from 'path'; +import CompilerCli, { getPackagePath } from './Cli'; + +/** + * @category contract + */ +export default class CompilerCli8 extends CompilerCli { + /** + * @param options - Options + * @param options.ignoreVersion - Don't ensure that the compiler is supported + */ + constructor( + { ignoreVersion }: { ignoreVersion?: boolean } = {}, + ) { + super(resolve(getPackagePath(), './bin/aesophia_cli_8'), { ignoreVersion }); + } +} diff --git a/src/index.ts b/src/index.ts index f2c17c52c3..84ac9ce2f7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,6 @@ export * from './index-browser'; export { default as CompilerCli } from './contract/compiler/Cli'; +export { default as CompilerCli8 } from './contract/compiler/Cli8'; export { default as getFileSystem } from './contract/compiler/getFileSystem'; export { default as CompilerHttpNode } from './contract/compiler/HttpNode'; diff --git a/tooling/fetch-aesophia-cli-8.mjs b/tooling/fetch-aesophia-cli-8.mjs new file mode 100644 index 0000000000..7bb3dd67f9 --- /dev/null +++ b/tooling/fetch-aesophia-cli-8.mjs @@ -0,0 +1,25 @@ +import { createHash } from 'crypto'; +import { dirname } from 'path'; +import { writeFileSync, readFileSync, mkdirSync } from 'fs'; + +const path = './bin/aesophia_cli_8'; +const hash = 'tZdsd7XH1e4C10MIzM0TY0IFcpkPBZqZMPdJ1ln9GDVsgjVCCK86YKCK5KtKqQzhNKSXaE01ZjAfTEYOSV7uIg=='; + +function ensureBinaryCorrect() { + const buffer = readFileSync(path); + const h = createHash('sha512').update(buffer).digest('base64'); + if (h !== hash) throw new Error('Wrong hash'); +} + +try { + ensureBinaryCorrect(); +} catch { + console.log('Fetching aesophia_cli_8'); + const request = await fetch( + 'https://github.com/aeternity/aesophia_cli/raw/df63ff9f4fdcfc437c90b90914fd1a7081d2bbbe/aesophia_cli', + ); + const body = Buffer.from(await request.arrayBuffer()); + mkdirSync(dirname(path), { recursive: true }); + writeFileSync(path, body); + ensureBinaryCorrect(); +}