char/avril
(wip) why'd you have to go and make things so complicaaaaated?
git clone https://git.t4t.associates/char/avril
067c81e
main
1use std:: cell:: RefCell ; 2use std:: io; 3use std:: rc:: Rc ; 4 5use avril_core:: serde_v8::{ JsBuffer , from_v8}; 6use avril_core::{ Extension , Op , WasmStreamingRegistry , v8}; 7 8pub fn init () ->Extension { 9Extension { 10name : "ext:wasmstreaming" , 11dependencies : & [ "ext:http_types" , "ext:webstreams" ], 12ops : vec! [ 13Op :: try_raw ( "op_wasm_streaming_register" , |scope, args|{ 14let callback = v8:: Local ::< v8:: Function >:: try_from ( args. get ( 0 )) 15. map_err ( |_| io:: Error :: other ( "Expected a Wasm streaming callback" )) ?; 16let registry = scope 17. get_slot ::< Rc < RefCell < WasmStreamingRegistry >>>() 18. ok_or_else ( || io:: Error :: other ( "Wasm streaming registry is unavailable" )) ?; 19 registry 20. borrow_mut () 21. register ( v8:: Global :: new ( scope, callback)) ?; 22Ok ::< _ , io:: Error >( v8:: undefined ( scope). into ()) 23}), 24Op :: try_raw ( "op_wasm_streaming_write" , |scope, args|{ 25let id =from_v8 ( scope, args. get ( 0 )). map_err ( io:: Error :: other) ?; 26let bytes: JsBuffer =from_v8 ( scope, args. get ( 1 )). map_err ( io:: Error :: other) ?; 27let registry = scope 28. get_slot ::< Rc < RefCell < WasmStreamingRegistry >>>() 29. ok_or_else ( || io:: Error :: other ( "Wasm streaming registry is unavailable" )) ?; 30 registry 31. borrow_mut () 32. stream_mut ( id) ? 33. on_bytes_received ( bytes. as_ref ()); 34Ok ::< _ , io:: Error >( v8:: undefined ( scope). into ()) 35}), 36Op :: with_scope ( 37"op_wasm_streaming_url" , 38 |scope, ( id, url): ( u64 , String ) |{ 39let registry = scope 40. get_slot ::< Rc < RefCell < WasmStreamingRegistry >>>() 41. ok_or_else ( ||{ 42 io:: Error :: other ( "Wasm streaming registry is unavailable" ) 43}) ?; 44 registry. borrow_mut (). stream_mut ( id) ?. set_url ( & url); 45Ok ::< _ , io:: Error >(()) 46}, 47), 48Op :: with_scope ( "op_wasm_streaming_finish" , |scope, id : u64 |{ 49let registry = scope 50. get_slot ::< Rc < RefCell < WasmStreamingRegistry >>>() 51. ok_or_else ( || io:: Error :: other ( "Wasm streaming registry is unavailable" )) ?; 52 registry. borrow_mut (). finish ( id) 53}), 54Op :: try_raw ( "op_wasm_streaming_abort" , |scope, args|{ 55let id =from_v8 ( scope, args. get ( 0 )). map_err ( io:: Error :: other) ?; 56let registry = scope 57. get_slot ::< Rc < RefCell < WasmStreamingRegistry >>>() 58. ok_or_else ( || io:: Error :: other ( "Wasm streaming registry is unavailable" )) ?; 59 registry. borrow_mut (). abort ( id, args. get ( 1 )) ?; 60Ok ::< _ , io:: Error >( v8:: undefined ( scope). into ()) 61}), 62], 63modules : & [( 64"ext:wasmstreaming/main.js" , 65include_str! ( concat! ( env! ( "OUT_DIR" ), "/main.js" )), 66)], 67entry_point : Some ( "ext:wasmstreaming/main.js" ), 68} 69}