Skip to content

Commit

Permalink
Include support for runtime v0.2.0 (#97)
Browse files Browse the repository at this point in the history
Co-authored-by: Derek Anderson <[email protected]>
  • Loading branch information
uditdc and dmikey authored Nov 8, 2023
1 parent 538f9cd commit e0b5202
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 56 deletions.
23 changes: 8 additions & 15 deletions src/commands/function/build.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import fs from "fs"
import Chalk from 'chalk'
import { resolve } from "path"
import { writeFileSync } from "fs"
import { buildWasm, createWasmArchive, createWasmManifest } from "./shared"
import { parseBlsConfig } from "../../lib/blsConfig"
import { generateChecksum } from "../../lib/crypto"
import { generateMd5Checksum } from "../../lib/crypto"
import { logger } from "../../lib/logger"
import { slugify } from "../../lib/strings"

Expand Down Expand Up @@ -45,30 +44,24 @@ export const run = (options: {
}

// Generate a default WASM manifest
const wasmManifest = createWasmManifest(
wasmName,
wasmArchive,
content_type
)
const wasmManifest = createWasmManifest(wasmName, content_type)

// Create a WASM archive
const archive = createWasmArchive(buildDir, wasmArchive, wasmName)
const checksum = generateChecksum(archive)
createWasmArchive(buildDir, wasmArchive, wasmName)

// Include WASM checksum and entrypoint
wasmManifest.runtime.checksum = checksum
wasmManifest.methods?.push({
wasmManifest.modules?.push({
file: wasmName,
name: wasmName.split(".")[0],
entry: wasmName,
result_type: "string",
type: 'entry',
md5: generateMd5Checksum(fs.readFileSync(`${buildDir}/${wasmName}`))
})

if (deployConfig) {
wasmManifest.permissions = deployConfig.permissions || []
}

// Store manifest
writeFileSync(`${buildDir}/manifest.json`, JSON.stringify(wasmManifest))
fs.writeFileSync(`${buildDir}/manifest.json`, JSON.stringify(wasmManifest))

// Show success message
console.log(`${Chalk.green('Build successful!')}`)
Expand Down
20 changes: 8 additions & 12 deletions src/commands/function/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,31 +41,27 @@ export interface IBlsDeployConfig extends JsonMap {
}

// WASM interrfaces
export interface IWasmMethod {
name: string;
entry: string;
result_type: string;
export interface IWasmModule {
file: string
name: string
type: string
md5: string
}

export interface IManifest {
id: string;
version: number
name: string;
description: string;
fs_root_path: string;
drivers_root_path?: string;
runtime_logger: string;
drivers_root_path: string;
limited_fuel?: number;
limited_memory?: number;
entry: string;
resouces?: [];
hooks?: [];
runtime: {
checksum: string;
url: string;
};
modules?: IWasmModule[]
contentType?: "json" | "html" | "text";
resources?: string[];
methods?: IWasmMethod[];
permissions?: string[];
}

Expand Down
8 changes: 6 additions & 2 deletions src/commands/function/invoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const run = async (options: any) => {
serve = false
} = options

const runtimePath = `${systemPath}runtime/blockless-cli`
const runtimePath = `${systemPath}runtime/bls-runtime`

// Validate Runtime Path
try {
Expand Down Expand Up @@ -57,7 +57,11 @@ export const run = async (options: any) => {
// the runtime requires absolute paths
let manifestData = fs.readFileSync(manifestPath, "utf8")
let manifest = JSON.parse(manifestData)
manifest.entry = resolve(buildDir, manifest.entry)
manifest.drivers_root_path = `${systemPath}/extensions`
manifest.modules = manifest.modules.map((m: any) => {
m.file = resolve(buildDir, m.file)
return m
})
fs.writeFileSync(manifestPath, JSON.stringify(manifest))

// prepare environment variables
Expand Down
12 changes: 4 additions & 8 deletions src/commands/function/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,27 +90,23 @@ export const buildWasm = (
*/
export const createWasmManifest = (
entry: string,
url: string,
contentType: 'text' | 'html' | 'json' | undefined,
manifestOverride = {}
): IManifest => {
const name = entry.split(".")[0]

const manifest: IManifest = {
id: "",
version: 1,
name,
hooks: [],
description: "",
fs_root_path: "./",
drivers_root_path: `./`,
runtime_logger: 'runtime.log',
drivers_root_path: `${store.system.homedir}/.bls/extensions`,
entry,
runtime: {
checksum: "",
url,
},
entry: '_start',
contentType: contentType || 'text',
methods: [],
modules: [],
permissions: [],
...manifestOverride
}
Expand Down
30 changes: 13 additions & 17 deletions src/commands/sites/build.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import Chalk from 'chalk'
import fs from 'fs'
import Chalk from 'chalk'
import { resolve } from "path"
import { parseBlsConfig } from "../../lib/blsConfig"
import { logger } from "../../lib/logger"
import { createWasmArchive, createWasmManifest } from '../function/shared'
import { generateChecksum } from '../../lib/crypto'
import { generateMd5Checksum } from '../../lib/crypto'
import { buildSiteWasm } from './shared'
import { slugify } from '../../lib/strings'

/**
* Execute the `build` command line operation
Expand All @@ -20,8 +21,8 @@ export const run = async (options: {
}) => {
const {
debug = true,
rebuild = true,
path = process.cwd()
path = process.cwd(),
rebuild = true
} = options

try {
Expand All @@ -31,8 +32,8 @@ export const run = async (options: {
// 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 || '.bls')
const buildName = buildConfig.entry ? buildConfig.entry.replace('.wasm', '') : name
const wasmName = buildConfig.entry || `${name}.wasm`
const wasmArchive = `${buildName}.tar.gz`

Expand All @@ -41,25 +42,20 @@ export const run = async (options: {
return
}

const wasmManifest = createWasmManifest(
wasmName,
wasmArchive,
content_type
)
// Generate a default WASM manifest
const wasmManifest = createWasmManifest(wasmName, content_type)

// Build site WASM
await buildSiteWasm(wasmName, buildDir, path, buildConfig, debug)

// Create a WASM archive
const archive = createWasmArchive(buildDir, wasmArchive, wasmName)
const checksum = generateChecksum(archive)
createWasmArchive(buildDir, wasmArchive, wasmName)

// Include WASM checksum and entrypoint
wasmManifest.runtime.checksum = checksum
wasmManifest.methods?.push({
wasmManifest.modules?.push({
file: wasmName,
name: wasmName.split(".")[0],
entry: wasmName,
result_type: "string",
type: 'entry',
md5: generateMd5Checksum(fs.readFileSync(`${buildDir}/${wasmName}`))
})

if (deployConfig) {
Expand Down
8 changes: 6 additions & 2 deletions src/commands/sites/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const run = async (options: any) => {
rebuild = false,
} = options

const runtimePath = `${systemPath}runtime/blockless-cli`
const runtimePath = `${systemPath}runtime/bls-runtime`

// Validate Runtime Path
try {
Expand Down Expand Up @@ -55,7 +55,11 @@ export const run = async (options: any) => {
// the runtime requires absolute paths
let manifestData = fs.readFileSync(manifestPath, "utf8")
let manifest = JSON.parse(manifestData)
manifest.entry = resolve(buildDir, manifest.entry)
manifest.drivers_root_path = `${systemPath}/extensions`
manifest.modules = manifest.modules.map((m: any) => {
m.file = resolve(buildDir, m.file)
return m
})
fs.writeFileSync(manifestPath, JSON.stringify(manifest))

// prepare environment variables
Expand Down
4 changes: 4 additions & 0 deletions src/lib/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export const generateChecksum = (archive: Buffer): string => {
return crypto.createHash("sha256").update(archive).digest("hex");
};

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


/**
* A simple function to parse JWT from a given token
Expand Down

0 comments on commit e0b5202

Please sign in to comment.