Skip to content

Commit

Permalink
add support for archive download
Browse files Browse the repository at this point in the history
Signed-off-by: Derek Anderson <[email protected]>
  • Loading branch information
dmikey committed Jan 31, 2024
1 parent e85cff7 commit 214edd5
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 114 deletions.
120 changes: 63 additions & 57 deletions src/commands/function/build.ts
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;
}
};
110 changes: 62 additions & 48 deletions src/commands/function/interfaces.ts
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;
}
21 changes: 12 additions & 9 deletions src/lib/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@ import crypto from "crypto";
* @returns
*/
export const generateChecksum = (archive: Buffer): string => {
return crypto.createHash("sha256").update(archive).digest("hex");
return crypto.createHash("sha256").update(archive).digest("hex");
};

export const generateMd5Checksum = (buf: Buffer) => {
return crypto.createHash('md5').update(buf).digest("hex")
}
return crypto.createHash("md5").update(buf).digest("hex");
};

export const generateSha256Checksum = (buf: Buffer) => {
return crypto.createHash("sha256").update(buf).digest("hex");
};

/**
* A simple function to parse JWT from a given token
*
* @param token
* @returns
* A simple function to parse JWT from a given token
*
* @param token
* @returns
*/
export const parseJwt = (token: string) => {
return JSON.parse(Buffer.from(token.split('.')[1], 'base64').toString())
}
return JSON.parse(Buffer.from(token.split(".")[1], "base64").toString());
};

0 comments on commit 214edd5

Please sign in to comment.