Skip to content

feat: add Host.arrayBufferToBase64 and Host.base64ToArrayBuffer #94

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ extism-pdk = "1"
once_cell = "1.16"
anyhow = { workspace = true }
quickjs-wasm-rs = "3"
chrono = { version = "0.4", default_features = false, features = ["clock"]}
chrono = { version = "0.4", default_features = false, features = ["clock"] }
base64 = "0.22.1"

[lib]
crate_type = ["cdylib"]
33 changes: 33 additions & 0 deletions crates/core/src/globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,44 @@ fn build_host_object(context: &JSContextRef) -> anyhow::Result<JSValueRef> {
Ok(JSValue::Bool(true))
},
)?;

let to_base64 = context.wrap_callback(
|_ctx: &JSContextRef, _this: JSValueRef, args: &[JSValueRef]| {
let data = args.first().unwrap();
if !data.is_array_buffer() {
return Err(anyhow!("expected array buffer"));
}

use base64::prelude::*;
let bytes = data.as_bytes()?;
let as_string = BASE64_STANDARD.encode(bytes);

Ok(JSValue::String(as_string))
},
)?;

let from_base64 = context.wrap_callback(
|_ctx: &JSContextRef, _this: JSValueRef, args: &[JSValueRef]| {
let data = args.first().unwrap();
if !data.is_str() {
return Err(anyhow!("expected string"));
}

use base64::prelude::*;
let string = data.as_str()?;
let bytes = BASE64_STANDARD.decode(string)?;

Ok(JSValue::ArrayBuffer(bytes))
},
)?;

let host_object = context.object_value()?;
host_object.set_property("inputBytes", host_input_bytes)?;
host_object.set_property("inputString", host_input_string)?;
host_object.set_property("outputBytes", host_output_bytes)?;
host_object.set_property("outputString", host_output_string)?;
host_object.set_property("arrayBufferToBase64", to_base64)?;
host_object.set_property("base64ToArrayBuffer", from_base64)?;
Ok(host_object)
}

Expand Down
6 changes: 4 additions & 2 deletions crates/core/src/prelude/src/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ declare global {
outputBytes(output: ArrayBufferLike): boolean;
outputString(output: string): boolean;
getFunctions(): import("extism:host").user;
arrayBufferToBase64(input: ArrayBuffer): string;
base64ToArrayBuffer(input: string): ArrayBuffer;
}

var Host: Host;
}

Host.getFunctions = function () {
Host.getFunctions = function() {
function createInvoke(id: number, results: number) {
if (results === 0) {
return Host.invokeFunc0.bind(null, id);
Expand All @@ -32,4 +34,4 @@ Host.getFunctions = function () {
}, {});
};

export {};
export { };
Loading