-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for archive download (#118)
Signed-off-by: Derek Anderson <[email protected]>
- Loading branch information
Showing
3 changed files
with
137 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,79 @@ | ||
import fs from "fs" | ||
import Chalk from 'chalk' | ||
import { resolve } from "path" | ||
import { buildWasm, createWasmArchive, createWasmManifest } from "./shared" | ||
import { parseBlsConfig } from "../../lib/blsConfig" | ||
import { generateMd5Checksum } from "../../lib/crypto" | ||
import { logger } from "../../lib/logger" | ||
import { slugify } from "../../lib/strings" | ||
import fs from "fs"; | ||
import Chalk from "chalk"; | ||
import { resolve } from "path"; | ||
import { buildWasm, createWasmArchive, createWasmManifest } from "./shared"; | ||
import { parseBlsConfig } from "../../lib/blsConfig"; | ||
import { generateMd5Checksum, generateSha256Checksum } from "../../lib/crypto"; | ||
import { logger } from "../../lib/logger"; | ||
import { slugify } from "../../lib/strings"; | ||
|
||
/** | ||
* Execute the `build` command line operation | ||
* | ||
* @param options | ||
* | ||
* @param options | ||
* @returns void | ||
*/ | ||
export const run = (options: { | ||
path: string | ||
debug: boolean | ||
rebuild: boolean | ||
path: string; | ||
debug: boolean; | ||
rebuild: boolean; | ||
}) => { | ||
const { | ||
debug = true, | ||
path = process.cwd(), | ||
rebuild = true | ||
} = options | ||
const { debug = true, path = process.cwd(), rebuild = true } = options; | ||
|
||
try { | ||
// Fetch BLS config | ||
const { name, content_type, deployment, build, build_release } = parseBlsConfig() | ||
try { | ||
// Fetch BLS config | ||
const { name, content_type, deployment, build, build_release } = | ||
parseBlsConfig(); | ||
|
||
// check for and store unmodified wasm file name to change later | ||
const buildConfig = !debug ? build_release : build | ||
const deployConfig = deployment | ||
const buildName = buildConfig.entry ? slugify(buildConfig.entry.replace('.wasm', '')) : slugify(name) | ||
const buildDir = resolve(path, buildConfig.dir || 'build') | ||
const wasmName = slugify(buildConfig.entry) || `${slugify(name)}.wasm` | ||
const wasmArchive = `${buildName}.tar.gz` | ||
// check for and store unmodified wasm file name to change later | ||
const buildConfig = !debug ? build_release : build; | ||
const deployConfig = deployment; | ||
const buildName = buildConfig.entry | ||
? slugify(buildConfig.entry.replace(".wasm", "")) | ||
: slugify(name); | ||
const buildDir = resolve(path, buildConfig.dir || "build"); | ||
const wasmName = slugify(buildConfig.entry) || `${slugify(name)}.wasm`; | ||
const wasmArchive = `${buildName}.tar.gz`; | ||
|
||
// Rebuild function if requested or | ||
if (!fs.existsSync(resolve(buildDir, wasmName)) || rebuild) { | ||
buildWasm(wasmName, buildDir, path, buildConfig, debug) | ||
} else if (fs.existsSync(resolve(buildDir, wasmName)) && !rebuild) { | ||
return | ||
} | ||
// Rebuild function if requested or | ||
if (!fs.existsSync(resolve(buildDir, wasmName)) || rebuild) { | ||
buildWasm(wasmName, buildDir, path, buildConfig, debug); | ||
} else if (fs.existsSync(resolve(buildDir, wasmName)) && !rebuild) { | ||
return; | ||
} | ||
|
||
// Generate a default WASM manifest | ||
const wasmManifest = createWasmManifest(wasmName, content_type) | ||
// Generate a default WASM manifest | ||
const wasmManifest = createWasmManifest(wasmName, content_type); | ||
|
||
// Create a WASM archive | ||
createWasmArchive(buildDir, wasmArchive, wasmName) | ||
// Create a WASM archive | ||
createWasmArchive(buildDir, wasmArchive, wasmName); | ||
|
||
wasmManifest.modules?.push({ | ||
file: wasmName, | ||
name: wasmName.split(".")[0], | ||
type: 'entry', | ||
md5: generateMd5Checksum(fs.readFileSync(`${buildDir}/${wasmName}`)) | ||
}) | ||
wasmManifest.runtime = { | ||
url: wasmArchive, | ||
checksum: generateSha256Checksum( | ||
fs.readFileSync(`${buildDir}/${wasmArchive}`), | ||
), | ||
}; | ||
|
||
if (deployConfig) { | ||
wasmManifest.permissions = deployConfig.permissions || [] | ||
} | ||
wasmManifest.modules?.push({ | ||
file: wasmName, | ||
name: wasmName.split(".")[0], | ||
type: "entry", | ||
md5: generateMd5Checksum(fs.readFileSync(`${buildDir}/${wasmName}`)), | ||
}); | ||
|
||
// Store manifest | ||
fs.writeFileSync(`${buildDir}/manifest.json`, JSON.stringify(wasmManifest)) | ||
if (deployConfig) { | ||
wasmManifest.permissions = deployConfig.permissions || []; | ||
} | ||
|
||
// Show success message | ||
console.log(`${Chalk.green('Build successful!')}`) | ||
console.log('') | ||
} catch (error: any) { | ||
logger.error('Failed to build function.', error.message) | ||
return | ||
} | ||
} | ||
// Store manifest | ||
fs.writeFileSync(`${buildDir}/manifest.json`, JSON.stringify(wasmManifest)); | ||
|
||
// Show success message | ||
console.log(`${Chalk.green("Build successful!")}`); | ||
console.log(""); | ||
} catch (error: any) { | ||
logger.error("Failed to build function.", error.message); | ||
return; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,93 @@ | ||
// Blocklesss interfaces | ||
export interface IBlsFunction { | ||
functionId: string; | ||
name: string; | ||
functionId: string; | ||
name: string; | ||
} | ||
export interface IBlsFunctionRequiredOptions { | ||
init: string[]; | ||
deploy: string[]; | ||
publish: string[]; | ||
update: string[]; | ||
init: string[]; | ||
deploy: string[]; | ||
publish: string[]; | ||
update: string[]; | ||
} | ||
export interface IBlsFunctionConfig { | ||
name: string | ||
version: string | ||
name: string; | ||
version: string; | ||
|
||
deployment: { | ||
permission: 'public' | 'private' | ||
nodes: number | ||
} | ||
deployment: { | ||
permission: "public" | "private"; | ||
nodes: number; | ||
}; | ||
} | ||
export interface IBlsConfig extends JsonMap { | ||
name: string | ||
version: string | ||
content_type: 'text' | 'html' | 'json' | ||
name: string; | ||
version: string; | ||
content_type: "text" | "html" | "json"; | ||
|
||
deployment: IBlsDeployConfig | ||
build: IBlsBuildConfig | ||
build_release: IBlsBuildConfig | ||
deployment: IBlsDeployConfig; | ||
build: IBlsBuildConfig; | ||
build_release: IBlsBuildConfig; | ||
} | ||
|
||
export interface IBlsBuildConfig extends JsonMap { | ||
command: string | ||
dir: string | ||
public_dir: string | ||
entry: string | ||
command: string; | ||
dir: string; | ||
public_dir: string; | ||
entry: string; | ||
} | ||
|
||
export interface IBlsDeployConfig extends JsonMap { | ||
nodes: number | ||
permissions: string[] | ||
nodes: number; | ||
permissions: string[]; | ||
} | ||
|
||
// WASM interrfaces | ||
export interface IWasmModule { | ||
file: string | ||
name: string | ||
type: string | ||
md5: string | ||
file: string; | ||
name: string; | ||
type: string; | ||
md5: string; | ||
} | ||
|
||
// {"checksum":"de41ad2f4117974e4fabd176e42dcea6da5b3c53cc63855bd8a4dd00e4c6fd76","url":"eth-price-weights-calc.tar.gz"} | ||
export interface IManifestRuntime { | ||
checksum: string; | ||
url: string; | ||
} | ||
|
||
export interface IManifest { | ||
id: string; | ||
version: number | ||
name: string; | ||
description: string; | ||
fs_root_path: string; | ||
drivers_root_path?: string; | ||
runtime_logger: string; | ||
limited_fuel?: number; | ||
limited_memory?: number; | ||
entry: string; | ||
hooks?: []; | ||
modules?: IWasmModule[] | ||
contentType?: "json" | "html" | "text"; | ||
permissions?: string[]; | ||
id: string; | ||
version: number; | ||
name: string; | ||
description: string; | ||
fs_root_path: string; | ||
drivers_root_path?: string; | ||
runtime_logger: string; | ||
limited_fuel?: number; | ||
limited_memory?: number; | ||
runtime?: IManifestRuntime; | ||
entry: string; | ||
hooks?: []; | ||
modules?: IWasmModule[]; | ||
contentType?: "json" | "html" | "text"; | ||
permissions?: string[]; | ||
} | ||
|
||
export interface IDeploymentOptions { | ||
functionId: string; | ||
functionName: string; | ||
userFunctionId: string; | ||
functionId: string; | ||
functionName: string; | ||
userFunctionId: string; | ||
} | ||
|
||
type JsonArray = boolean[] | number[] | string[] | JsonMap[] | Date[] | ||
type AnyJson = boolean | number | string | JsonMap | Date | JsonArray | JsonArray[] | ||
type JsonArray = boolean[] | number[] | string[] | JsonMap[] | Date[]; | ||
type AnyJson = | ||
| boolean | ||
| number | ||
| string | ||
| JsonMap | ||
| Date | ||
| JsonArray | ||
| JsonArray[]; | ||
|
||
export interface JsonMap { | ||
[key: string]: AnyJson | ||
[key: string]: AnyJson; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters