Skip to content

Commit

Permalink
Merge pull request #94 from extism/chris/20240909-base64d
Browse files Browse the repository at this point in the history
feat: add Host.arrayBufferToBase64 and Host.base64ToArrayBuffer
  • Loading branch information
bhelx authored Sep 9, 2024
2 parents 89758e2 + 01aa2ba commit e066a06
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
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 { };

0 comments on commit e066a06

Please sign in to comment.