From 354c31030692914fe70545cca86645f9f3448971 Mon Sep 17 00:00:00 2001 From: Afshan Ahmed Khan <60838316+redoC-A2k@users.noreply.github.com> Date: Mon, 17 Jun 2024 00:39:37 +0530 Subject: [PATCH] Fix hono post handler (#391) --- .../arakoo-core/src/apis/http/shims/index.js | 48 ++++++++++++++++- JS/wasm/crates/arakoo-core/src/lib.rs | 27 ++-------- JS/wasm/examples/ec-wasmjs-hono/package.json | 14 +++-- JS/wasm/examples/ec-wasmjs-hono/src/index.js | 52 ++++++------------- .../ec-wasmjs-hono/webpack.config.cjs | 24 +++++++++ 5 files changed, 100 insertions(+), 65 deletions(-) create mode 100644 JS/wasm/examples/ec-wasmjs-hono/webpack.config.cjs diff --git a/JS/wasm/crates/arakoo-core/src/apis/http/shims/index.js b/JS/wasm/crates/arakoo-core/src/apis/http/shims/index.js index 860d6ddfd..3a51daaf3 100644 --- a/JS/wasm/crates/arakoo-core/src/apis/http/shims/index.js +++ b/JS/wasm/crates/arakoo-core/src/apis/http/shims/index.js @@ -178,8 +178,54 @@ class Request { this.geo = input.geo || {}; } + defaultEncoding() { + return "utf-8"; + } + + arrayBuffer() { + let parsedBody = this.body; + + if (typeof this.body === "string") { + try { + parsedBody = new TextEncoder().encode(this.body); + } catch (e) { + return Promise.reject(`err: ${e}`); + } + } + + return parsedBody; + } + + json() { + let parsedBody = this.body; + + if (typeof this.body !== "string") { + try { + parsedBody = new TextDecoder(this.defaultEncoding()).decode(this.body); + } catch (e) { + return Promise.reject(`err: ${e}`); + } + } + + try { + return Promise.resolve(JSON.parse(parsedBody)); + } catch (e) { + return Promise.reject(`err: ${e}`); + } + } + text() { - return this.body; + let parsedBody = this.body; + + if (typeof this.body !== "string") { + try { + parsedBody = new TextDecoder(this.defaultEncoding()).decode(this.body); + } catch (e) { + return Promise.reject(`err: ${e}`); + } + } + + return parsedBody; } } diff --git a/JS/wasm/crates/arakoo-core/src/lib.rs b/JS/wasm/crates/arakoo-core/src/lib.rs index 006dd491f..084472fc5 100644 --- a/JS/wasm/crates/arakoo-core/src/lib.rs +++ b/JS/wasm/crates/arakoo-core/src/lib.rs @@ -174,29 +174,6 @@ impl wit::inbound_http::Guest for Guest { entrypoint .call(global, &[request_value]) .expect("Unable to call handler"); - // let event_request = event - // .get_property("request") - // .expect("Unable to get request from event"); - // let promise = handler - // .call(global, &[event_request, event]) - // .expect("Unable to call handler"); - - // let on_resolve = ON_RESOLVE.get().unwrap().clone() ; - // let on_reject = ON_REJECT.get().unwrap().clone() ; - // let then_func = promise.get_property("then").unwrap(); - // if then_func.is_function() { - // then_func - // .call( - // &promise, - // &[on_resolve.deref().clone(), on_reject.deref().clone()], - // ) - // .unwrap(); - // } else { - // RESPONSE - // .lock() - // .unwrap() - // .replace(from_qjs_value(promise).unwrap()); - // } context .execute_pending() @@ -207,7 +184,9 @@ impl wit::inbound_http::Guest for Guest { let response = from_qjs_value(result).unwrap(); let error = from_qjs_value(error).unwrap(); debug!("Result : {:?}", response); - debug!("Error : {:?}", error); + if error.to_string() != "null"{ + println!("Error : {:?}", error); + } // let response = to_qjs_value(context, &RESPONSE.lock().unwrap().take().unwrap()).unwrap(); // let response = RESPONSE.lock().unwrap().take().unwrap(); diff --git a/JS/wasm/examples/ec-wasmjs-hono/package.json b/JS/wasm/examples/ec-wasmjs-hono/package.json index 3fdd21070..5bf433976 100644 --- a/JS/wasm/examples/ec-wasmjs-hono/package.json +++ b/JS/wasm/examples/ec-wasmjs-hono/package.json @@ -3,20 +3,26 @@ "type": "module", "main": "bin/app.js", "scripts": { - "build": "node ./build.js" + "build": "webpack && ../../../../target/release/arakoo-compiler bin/webpack.js", + "debug": "JS_LOG=debug RUST_LOG=debug ../../../../target/release/arakoo-compiler bin/webpack.js && RUST_LOG=debug RUST_BACKTRACE=1 ../../../../target/release/arakoo index.wasm", + "run": "../../../../target/release/arakoo index.wasm" }, "devDependencies": { "@planetscale/database": "^1.4.0", "esbuild": "^0.19", "esbuild-plugin-text-replace": "^1.3.0", - "hono": "^3.9" + "webpack": "^5.91.0", + "webpack-cli": "^5.1.4" }, "dependencies": { - "@arakoodev/jsonnet": "file:../../../jsonnet/", + "@arakoodev/edgechains.js": "link:../../../edgechains/arakoodev", + "@arakoodev/jsonnet": "link:../../../jsonnet", "@hono/node-server": "^1.3.1", "axios": "^1.6.2", "crypto": "^1.0.1", + "hono": "^3.9", "http": "^0.0.1-security", - "stream": "^0.0.2" + "stream": "^0.0.2", + "zod": "^3.23.8" } } diff --git a/JS/wasm/examples/ec-wasmjs-hono/src/index.js b/JS/wasm/examples/ec-wasmjs-hono/src/index.js index e89bceffa..c7b8f7887 100644 --- a/JS/wasm/examples/ec-wasmjs-hono/src/index.js +++ b/JS/wasm/examples/ec-wasmjs-hono/src/index.js @@ -1,6 +1,5 @@ -import { Hono } from "hono"; +import { Hono } from "hono" import { connect } from "@planetscale/database"; - import Jsonnet from "@arakoodev/jsonnet"; let jsonnet = new Jsonnet(); @@ -17,39 +16,14 @@ app.get("/hello", (c) => { return c.text("Hello World!"); }); -app.get("/", (c) => { - const code = ` - local username = std.extVar('name'); - local Person(name='Alice') = { - name: name, - welcome: 'Hello ' + name + '!', - }; - { - person1: Person(username), - person2: Person('Bob'), - }`; - let result = jsonnet.extString("name", "ll").evaluateSnippet(code); - return c.json(JSON.parse(result)); -}); - -app.get("/func", (c) => { - const code = ` - local username = std.extVar('name'); - local Person(name='Alice') = { - name: name, - welcome: 'Hello ' + name + '!', - }; - { - person1: Person(username), - person2: Person('Bob'), - result : arakoo.native("greet")() - }`; - let result = jsonnet - .extString("name", "ll") - .javascriptCallback("greet", greet) - .evaluateSnippet(code); - return c.json(JSON.parse(result)); -}); +app.get("/xtz", async (c) => { + let result = jsonnet.extString("id", id).javascriptCallback("getAtodo", asyncGetAtodo) + .evaluateSnippet(` +local todo = std.parseJson(arakoo.native("getAtodo")(std.extVar("id"))); +{ +result : todo.title +}`); +}) app.get("/async-func/:id", async (c) => { let id = c.req.param("id"); @@ -74,6 +48,12 @@ app.get("/async-func/:id", async (c) => { return c.json(JSON.parse(result)); }); +app.post("/question", async (c)=>{ + let body = await c.req.json(); + console.log(body); + return c.json(body); +}) + app.get("/add", (c) => { function add(arg1, arg2, arg3) { console.log("Args recieved: ", arg1, arg2, arg3); @@ -146,4 +126,4 @@ app.notFound((c) => { }); app.fire(); -// globalThis._export = app; +// // globalThis._export = app; diff --git a/JS/wasm/examples/ec-wasmjs-hono/webpack.config.cjs b/JS/wasm/examples/ec-wasmjs-hono/webpack.config.cjs new file mode 100644 index 000000000..631126ad3 --- /dev/null +++ b/JS/wasm/examples/ec-wasmjs-hono/webpack.config.cjs @@ -0,0 +1,24 @@ +const { optimize } = require('webpack'); +const webpack = require('webpack'); + +let definePlugin = new webpack.DefinePlugin({ + 'process.env.arakoo': JSON.stringify(true) +}) + +console.log("starting build") + +const config = { + entry: './src/index.js', + output: { + path: __dirname + '/bin', + filename: 'webpack.js', + iife: false + }, + mode:"production", + optimization: { + minimize: false + }, + plugins: [definePlugin] +} + +module.exports = config; \ No newline at end of file