@@ -155,11 +155,44 @@ fn build_host_object(context: &JSContextRef) -> anyhow::Result<JSValueRef> {
155155 Ok ( JSValue :: Bool ( true ) )
156156 } ,
157157 ) ?;
158+
159+ let to_base64 = context. wrap_callback (
160+ |_ctx : & JSContextRef , _this : JSValueRef , args : & [ JSValueRef ] | {
161+ let data = args. first ( ) . unwrap ( ) ;
162+ if !data. is_array_buffer ( ) {
163+ return Err ( anyhow ! ( "expected array buffer" ) ) ;
164+ }
165+
166+ use base64:: prelude:: * ;
167+ let bytes = data. as_bytes ( ) ?;
168+ let as_string = BASE64_STANDARD . encode ( bytes) ;
169+
170+ Ok ( JSValue :: String ( as_string) )
171+ } ,
172+ ) ?;
173+
174+ let from_base64 = context. wrap_callback (
175+ |_ctx : & JSContextRef , _this : JSValueRef , args : & [ JSValueRef ] | {
176+ let data = args. first ( ) . unwrap ( ) ;
177+ if !data. is_str ( ) {
178+ return Err ( anyhow ! ( "expected string" ) ) ;
179+ }
180+
181+ use base64:: prelude:: * ;
182+ let string = data. as_str ( ) ?;
183+ let bytes = BASE64_STANDARD . decode ( string) ?;
184+
185+ Ok ( JSValue :: ArrayBuffer ( bytes) )
186+ } ,
187+ ) ?;
188+
158189 let host_object = context. object_value ( ) ?;
159190 host_object. set_property ( "inputBytes" , host_input_bytes) ?;
160191 host_object. set_property ( "inputString" , host_input_string) ?;
161192 host_object. set_property ( "outputBytes" , host_output_bytes) ?;
162193 host_object. set_property ( "outputString" , host_output_string) ?;
194+ host_object. set_property ( "arrayBufferToBase64" , to_base64) ?;
195+ host_object. set_property ( "base64ToArrayBuffer" , from_base64) ?;
163196 Ok ( host_object)
164197}
165198
0 commit comments