Skip to content

Commit

Permalink
Fix hono post handler (#391)
Browse files Browse the repository at this point in the history
  • Loading branch information
redoC-A2k committed Jun 16, 2024
1 parent bbf209b commit 354c310
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 65 deletions.
48 changes: 47 additions & 1 deletion JS/wasm/crates/arakoo-core/src/apis/http/shims/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
27 changes: 3 additions & 24 deletions JS/wasm/crates/arakoo-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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();

Expand Down
14 changes: 10 additions & 4 deletions JS/wasm/examples/ec-wasmjs-hono/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
52 changes: 16 additions & 36 deletions JS/wasm/examples/ec-wasmjs-hono/src/index.js
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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");
Expand All @@ -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);
Expand Down Expand Up @@ -146,4 +126,4 @@ app.notFound((c) => {
});

app.fire();
// globalThis._export = app;
// // globalThis._export = app;
24 changes: 24 additions & 0 deletions JS/wasm/examples/ec-wasmjs-hono/webpack.config.cjs
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 354c310

Please sign in to comment.