Skip to content

Commit

Permalink
Add WebAssembly support for Jsonnet evaluation (#314)
Browse files Browse the repository at this point in the history
* add wasi modules for jsonnet with external vars

* Update dependencies, remove unused code, and add wasm version of jsonnet

* revamp wasm compilation and serve flow

* fix the response Body

* simple working example of jsonnet

* complete jsonnet implementaion at wasmtime

* fix jsonnet_eval function

* add support for ext_string

* add docs in binding.rs

* add docs to run function

* fix http response at hyper side

* working example for nodejs with jsonnet

* add readme to jsonnet library

* update the workflow of webassembly

* add support for evaluateFile

* updated hydesearch example

* updated edgechain examples with arakoov/jsonnet

* fix shims build and upload release
  • Loading branch information
rahul007-bit authored Feb 29, 2024
1 parent 5226527 commit 9e75928
Show file tree
Hide file tree
Showing 187 changed files with 19,629 additions and 44,151 deletions.
65 changes: 25 additions & 40 deletions .github/workflows/webassembly-pr-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
- reopened
- synchronize
paths:
- 'JS/**'
- 'JS/wasm/**'

jobs:
changes:
Expand Down Expand Up @@ -50,52 +50,37 @@ jobs:
toolchain: stable
override: true

- name: Cache wasmjs-engine
- name: Cache Javy Core
uses: actions/cache@v2
with:
path: ./JS/wasm/crates/wasmjs-engine/target
key: wasmjs-engine
path: ./target
key: javy-core
restore-keys: |
wasmjs-engine
javy-core
- name: Check if wasmjs-engine is cached
id: wasmjs-engine-hit
- name: Check if Javy Core is cached
id: cache-hit
run: |
echo "cache-hit=$(test -d ./JS/wasm/crates/wasmjs-engine/target && echo true || echo false)" >> $GITHUB_OUTPUT
# echo "cache-hit=$(test -d ./target && echo true || echo false)" >> $GITHUB_OUTPUT
echo "cache-hit=$(test -d ./target && echo true || echo false)" >> $GITHUB_OUTPUT
- name: Build wasmjs-engine
if: steps.wasmjs-engine-hit.outputs.cache-hit != 'true'
working-directory: ./JS/wasm/crates/wasmjs-engine
- name: Build Javy Core
if:
working-directory: .
run: |
npm install -g @go-task/cli
task build
- name: Cache wasmjs-runtime
uses: actions/cache@v2
with:
path: ./JS/wasm/crates/wasmjs-runtime/target
key: wasmjs-runtime
restore-keys: |
wasmjs-runtime
- name: Check if wasmjs-runtime is cached
id: wasmjs-runtime-hit
make add build-cors
- name: Build CLI
working-directory: .
run: |
echo "cache-hit=$(test -d ./JS/wasm/crates/wasmjs-runtime/target && echo true || echo false)" >> $GITHUB_OUTPUT
- name: Build wasmjs-runtime
if: steps.wasmjs-runtime-hit.outputs.cache-hit != 'true'
working-directory: ./JS/wasm/crates/wasmjs-runtime
run: |
cargo build --release
- name: Build ec-wasmjs-hono
working-directory: ./JS/wasm/examples/ec-wasmjs-hono
make
- name: Build Jsonnet
working-directory: .
run: |
npm install
npm run build
make build-jsonnet
- name: Build Release Binary
run: cargo build --release

- name: Run ec-wasmjs-hono
working-directory: ./JS/wasm/crates/wasmjs-runtime
run: |
./target/release/wasmjs-runtime run ../../examples/ec-wasmjs-hono/bin
- name: Upload to GitHub Releases
uses: actions/upload-rust-binary-action@v1
with:
bin: your_binary_name
10 changes: 9 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,12 @@ build/
.vscode/

### Node Modules ###
node_modules
node_modules

### rust ###
/target/
index.wasm
index.wit
Cargo.lock

.DS_Store
30 changes: 30 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[workspace]
members = [
"JS/wasm/crates/apis",
"JS/wasm/crates/arakoo-core",
"JS/wasm/crates/cli",
"JS/wasm/crates/serve",
"JS/wasm/types/jsonnet",
]
resolver = "2"

[workspace.package]
edition = "2021"
version = "0.0.1"

[workspace.dependencies]
wizer = "4.0.0"
wasmtime = "16"
wasmtime-wasi = "16"
wasi-common = "16"
javy = { version = "2.1.0" }
anyhow = "1.0.79"
once_cell = "1.19.0"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
[profile.release]
lto = true
opt-level = 's'

[profile.dev]
incremental = true
82 changes: 44 additions & 38 deletions JS/edgechains/examples/hydeSearch/esbuild.build.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,51 @@
const esbuild = require("esbuild");
const path = require("path");
const fs = require("fs");
const { execSync } = require("child_process");
import { build } from "esbuild";
import { resolve, join } from "path";
import { existsSync, mkdirSync, promises } from "fs";

const outputDir = path.resolve(__dirname, "dist");
import { fileURLToPath } from 'url';
import { dirname } from 'path';

if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir);
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const outputDir = resolve(__dirname, "dist");

if (!existsSync(outputDir)) {
mkdirSync(outputDir);
}

const distPath = path.join(process.cwd(), "dist");
const distPath = join(process.cwd(), "dist");

fs.promises.mkdir(distPath, { recursive: true });
promises.mkdir(distPath, { recursive: true });

esbuild
.build({
entryPoints: ["./src/index.ts"],
bundle: true,
minify: true,
platform: "node",
outfile: "./dist/index.js",
tsconfig: "./tsconfig.json",
target: "node21.1.0",
external: [
"express",
"tsx",
"typescript",
"typeorm",
"react",
"react-dom",
"pg",
"jsdom",
"hono",
"@hanazuki/node-jsonnet",
"readline/promises",
],
format: "cjs",
loader: {
".html": "text",
".css": "css",
".jsonnet": "text",
},
})
build({
entryPoints: ["./src/index.ts"],
bundle: true,
minify: true,
platform: "node",
outfile: "./dist/index.js",
tsconfig: "./tsconfig.json",
target: "node21.1.0",
external: [
"express",
"tsx",
"typescript",
"typeorm",
"react",
"react-dom",
"pg",
"jsdom",
"hono",
"@hanazuki/node-jsonnet",
"@arakoodev/jsonnet",
"readline/promises",
],
format: "esm",
loader: {
".html": "text",
".css": "css",
".jsonnet": "text",
".wasm": "file",
},
})
.catch(() => process.exit(1));
4 changes: 4 additions & 0 deletions JS/edgechains/examples/hydeSearch/htmljs.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export declare const view: (viewToRender: any) => (c: any) => Promise<any>;
export declare const rootLayout: (layoutToApply: any) => (c: any, next: any) => Promise<void>;
export declare const layout: (layoutToApply: any) => (c: any, next: any) => Promise<void>;
export declare const Link: any;
52 changes: 52 additions & 0 deletions JS/edgechains/examples/hydeSearch/htmljs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { html } from "hono/html";
// These functions form the basis of the html.js framework and will be moved to a separate lib
export const view = (viewToRender) => {
return async (c) => {
const newBody = await viewToRender({ context: c });
return c.html(newBody);
};
};
export const rootLayout = (layoutToApply) => {
return async (c, next) => {
await next();
if (c.req.header("HX-Request") !== "true") {
// Req is a normal request, so we render the whole page which means adding the root layout
const curBody = await c.res.text();
c.res = undefined; // To overwrite res, set it to undefined before setting new value https://github.com/honojs/hono/pull/970 released in https://github.com/honojs/hono/releases/tag/v3.1.0
const newBody = await layoutToApply({ context: c, children: html(curBody) });
c.res = c.html(newBody);
}
// Else do nothing and let the original response be sent
};
};
export const layout = (layoutToApply) => {
return async (c, next) => {
await next();
if ((c.req.header("HX-Request") === "true" &&
(c.req.header("HX-Boosted") === "true" || !c.req.header("HX-Target"))) ||
c.req.header("HX-Request") !== "true") {
// Req is regular req or boosted link, so we apply layouts
const curBody = await c.res.text();
c.res = undefined; // To overwrite res, set it to undefined before setting new value https://github.com/honojs/hono/pull/970 released in https://github.com/honojs/hono/releases/tag/v3.1.0
const newBody = await layoutToApply({ context: c, children: html(curBody) });
c.res = c.html(newBody);
}
// Else do nothing and let the original response be sent, which will be a partial update applied to the page with hx-target
};
};
export const Link = ({ to, "hx-target": hxTarget, class: className, children }) => {
if (hxTarget) {
return html `<a
href="${to}"
class="${className}"
hx-get="${to}"
hx-target="${hxTarget}"
hx-push-url="true"
hx-swap="morph"
>${children}</a
>`;
}
else {
return html `<a href="${to}" class="${className}" hx-boost="true">${children}</a>`;
}
};
20 changes: 14 additions & 6 deletions JS/edgechains/examples/hydeSearch/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions JS/edgechains/examples/hydeSearch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
"name": "example",
"version": "1.0.0",
"description": "",
"type": "module",
"main": "dist/index.js",
"scripts": {
"build": "rm -rf dist && node esbuild.build.js",
"start": "node dist/index.js",
"start": "node --experimental-wasm-modules src/index.js",
"lint": "eslint --ignore-path .eslintignore --ext .js,.ts",
"format": "prettier --ignore-path .gitignore --write \"**/*.+(js|ts|json)\"",
"test": "npx jest"
Expand All @@ -20,7 +21,7 @@
"pg": "^8.11.3",
"reflect-metadata": "^0.1.13",
"tsc": "^2.0.4",
"typescript": "^5.3.2"
"@arakoodev/jsonnet": "file:./../../../wasm/types/jsonnet"
},
"devDependencies": {
"@arakoodev/edgechains.js": "^0.1.10",
Expand All @@ -44,6 +45,6 @@
"ts-jest": "^29.1.1",
"tsx": "^3.12.2",
"typeorm": "^0.3.17",
"typescript": "^5.0.2"
"typescript": "^5.3.3"
}
}
}
3 changes: 3 additions & 0 deletions JS/edgechains/examples/hydeSearch/src/ExampleLayout.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { FC } from "hono/jsx";
declare const ExampleLayout: FC;
export default ExampleLayout;
Loading

0 comments on commit 9e75928

Please sign in to comment.