diff --git a/Cargo.lock b/Cargo.lock index f08bf238..f5f30e7a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -265,6 +265,15 @@ dependencies = [ "generic-array", ] +[[package]] +name = "blockless-sdk" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "555793b93a7691f8b80d0d271b162315df634b47452965450398ea74ac42ee57" +dependencies = [ + "json", +] + [[package]] name = "brotli" version = "3.4.0" @@ -823,12 +832,31 @@ dependencies = [ "winapi", ] +[[package]] +name = "dns-parser" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4d33be9473d06f75f58220f71f7a9317aca647dc061dbd3c361b0bef505fbea" +dependencies = [ + "byteorder", + "quick-error", +] + [[package]] name = "either" version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" +[[package]] +name = "encoding_rs" +version = "0.8.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7268b386296a025e474d5140678f75d6de9493ae55a5d709eeb9dd08149945e1" +dependencies = [ + "cfg-if", +] + [[package]] name = "enum-iterator" version = "1.4.1" @@ -1022,6 +1050,18 @@ version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eb1d22c66e66d9d72e1758f0bd7d4fd0bee04cad842ee34587d68c07e45d088c" +[[package]] +name = "futures-io" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fff74096e71ed47f8e023204cfd0aa1289cd54ae5430a9523be060cdb849964" + +[[package]] +name = "futures-sink" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f43be4fe21a13b9781a69afa4985b0f6ee0e1afab2c6f454a8cf30e2b2237b6e" + [[package]] name = "futures-task" version = "0.3.29" @@ -1035,9 +1075,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3ef6b17e481503ec85211fed8f39d1970f128935ca1f814cd32ac4a6842e84ab" dependencies = [ "futures-core", + "futures-io", "futures-task", + "memchr", "pin-project-lite", "pin-utils", + "slab", ] [[package]] @@ -1129,6 +1172,25 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" +[[package]] +name = "h2_wasi" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15278003b75338787173b9fced9e4b6eda445a1254101e2398bbef2105657c9b" +dependencies = [ + "bytes", + "fnv", + "futures-core", + "futures-sink", + "futures-util", + "http 0.2.11", + "indexmap 1.9.3", + "slab", + "tokio-util_wasi", + "tokio_wasi", + "tracing", +] + [[package]] name = "half" version = "1.8.2" @@ -1186,6 +1248,17 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fed44880c466736ef9a5c5b5facefb5ed0785676d0c02d612db14e54f0d84286" +[[package]] +name = "http" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8947b1a6fad4393052c7ba1f4cd97bed3e953a95c79c92ad9b051a04611d9fbb" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + [[package]] name = "http" version = "1.0.0" @@ -1197,6 +1270,17 @@ dependencies = [ "itoa", ] +[[package]] +name = "http-body" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d5f38f16d184e36f2408a55281cd658ecbd3ca05cce6d6510a176eca393e26d1" +dependencies = [ + "bytes", + "http 0.2.11", + "pin-project-lite", +] + [[package]] name = "http-body" version = "1.0.0" @@ -1204,7 +1288,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1cac85db508abc24a2e48553ba12a996e87244a0395ce011e62b37158745d643" dependencies = [ "bytes", - "http", + "http 1.0.0", ] [[package]] @@ -1215,8 +1299,8 @@ checksum = "41cb79eb393015dadd30fc252023adb0b2400a0caee0fa2a077e6e21a551e840" dependencies = [ "bytes", "futures-util", - "http", - "http-body", + "http 1.0.0", + "http-body 1.0.0", "pin-project-lite", ] @@ -1226,6 +1310,12 @@ version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d897f394bad6a705d5f4104762e116a75639e470d80901eed05a860a95cb1904" +[[package]] +name = "httpdate" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" + [[package]] name = "humantime" version = "2.1.0" @@ -1241,8 +1331,8 @@ dependencies = [ "bytes", "futures-channel", "futures-util", - "http", - "http-body", + "http 1.0.0", + "http-body 1.0.0", "httparse", "itoa", "pin-project-lite", @@ -1275,17 +1365,41 @@ dependencies = [ "bytes", "futures-channel", "futures-util", - "http", - "http-body", + "http 1.0.0", + "http-body 1.0.0", "hyper", "pin-project-lite", - "socket2", + "socket2 0.5.5", "tokio", "tower", "tower-service", "tracing", ] +[[package]] +name = "hyper_wasi" +version = "0.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e801b2e9f51eb106e0bdc5e744bb74edd475d9d35787cf7ff94ca383f86ad73e" +dependencies = [ + "bytes", + "futures-channel", + "futures-core", + "futures-util", + "h2_wasi", + "http 0.2.11", + "http-body 0.4.5", + "httparse", + "httpdate", + "itoa", + "pin-project-lite", + "tokio_wasi", + "tower-service", + "tracing", + "want", + "wasmedge_wasi_socket", +] + [[package]] name = "id-arena" version = "2.2.1" @@ -1449,8 +1563,13 @@ name = "javy-apis" version = "2.1.1-alpha.1" dependencies = [ "anyhow", + "blockless-sdk", "fastrand", + "hyper_wasi", "javy", + "json", + "reqwest_wasi", + "tokio_wasi", ] [[package]] @@ -1508,6 +1627,12 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "json" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "078e285eafdfb6c4b434e0d31e8cfcb5115b651496faca5749b88fafd4f23bfd" + [[package]] name = "lazy_static" version = "1.4.0" @@ -1627,6 +1752,12 @@ dependencies = [ "autocfg", ] +[[package]] +name = "mime" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" + [[package]] name = "minimal-lexical" version = "0.2.1" @@ -1653,6 +1784,19 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "mio_wasi" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "826049861effde5245d1107a8eb21846bd8fc7a16486e60be4d0568361c947e5" +dependencies = [ + "libc", + "log", + "wasi 0.11.0+wasi-snapshot-preview1", + "wasmedge_wasi_socket", + "windows-sys 0.48.0", +] + [[package]] name = "native-tls" version = "0.2.11" @@ -1836,7 +1980,7 @@ dependencies = [ "libc", "redox_syscall 0.3.5", "smallvec", - "windows-targets", + "windows-targets 0.48.0", ] [[package]] @@ -2101,6 +2245,12 @@ dependencies = [ "unicase", ] +[[package]] +name = "quick-error" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" + [[package]] name = "quickcheck" version = "1.0.3" @@ -2270,6 +2420,41 @@ version = "0.6.26" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49b3de9ec5dc0a3417da371aab17d729997c15010e7fd24ff707773a33bddb64" +[[package]] +name = "reqwest_wasi" +version = "0.11.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21168806a3eb09e3b8464cbc3f54a4b32a48f5563923edbe8f9567bff72e3fb4" +dependencies = [ + "base64", + "bytes", + "encoding_rs", + "futures-core", + "futures-util", + "h2_wasi", + "http 0.2.11", + "http-body 0.4.5", + "hyper_wasi", + "ipnet", + "js-sys", + "lazy_static", + "log", + "mime", + "once_cell", + "percent-encoding", + "pin-project-lite", + "serde", + "serde_json", + "serde_urlencoded", + "tokio_wasi", + "tower-service", + "url", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", + "winreg", +] + [[package]] name = "rmp" version = "0.8.11" @@ -2493,6 +2678,18 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_urlencoded" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd" +dependencies = [ + "form_urlencoded", + "itoa", + "ryu", + "serde", +] + [[package]] name = "sha2" version = "0.10.6" @@ -2519,12 +2716,30 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "43b2853a4d09f215c24cc5489c992ce46052d359b5109343cbafbf26bc62f8a3" +[[package]] +name = "signal-hook-registry" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8229b473baa5980ac72ef434c4415e70c4b5e71b423043adb4ba059f89c99a1" +dependencies = [ + "libc", +] + [[package]] name = "siphasher" version = "0.3.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7bd3e3206899af3f8b12af284fafc038cc1dc2b41d1b89dd17297221c5d225de" +[[package]] +name = "slab" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" +dependencies = [ + "autocfg", +] + [[package]] name = "slice-group-by" version = "0.3.0" @@ -2548,6 +2763,16 @@ dependencies = [ "version_check", ] +[[package]] +name = "socket2" +version = "0.4.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f7916fc008ca5542385b89a3d3ce689953c143e9304a9bf8beec1de48994c0d" +dependencies = [ + "libc", + "winapi", +] + [[package]] name = "socket2" version = "0.5.5" @@ -3021,11 +3246,22 @@ dependencies = [ "libc", "mio", "pin-project-lite", - "socket2", - "tokio-macros", + "socket2 0.5.5", + "tokio-macros 2.2.0", "windows-sys 0.48.0", ] +[[package]] +name = "tokio-macros" +version = "1.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d266c00fde287f55d3f1c3e96c500c362a2b8c695076ec180f27918820bc6df8" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "tokio-macros" version = "2.2.0" @@ -3047,6 +3283,41 @@ dependencies = [ "tokio", ] +[[package]] +name = "tokio-util_wasi" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7c1741fa976df8c5adc7748a3769f1317aff9df6752bd33982b4e2d8a510f08" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "pin-project-lite", + "tokio_wasi", + "tracing", +] + +[[package]] +name = "tokio_wasi" +version = "1.25.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b3a7120cdbe4719425355a0f6b59191c67ab5ed4eebc64bdb12ea3bc8776adf" +dependencies = [ + "autocfg", + "bytes", + "libc", + "memchr", + "mio_wasi", + "num_cpus", + "parking_lot", + "pin-project-lite", + "signal-hook-registry", + "socket2 0.4.10", + "tokio-macros 1.8.2", + "wasmedge_wasi_socket", + "windows-sys 0.45.0", +] + [[package]] name = "toml" version = "0.5.8" @@ -3381,6 +3652,18 @@ dependencies = [ "wasm-bindgen-shared", ] +[[package]] +name = "wasm-bindgen-futures" +version = "0.4.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de9a9cec1733468a8c657e57fa2413d2ae2c0129b95e87c5b72b8ace4d13f31f" +dependencies = [ + "cfg-if", + "js-sys", + "wasm-bindgen", + "web-sys", +] + [[package]] name = "wasm-bindgen-macro" version = "0.2.81" @@ -3437,6 +3720,18 @@ dependencies = [ "leb128", ] +[[package]] +name = "wasmedge_wasi_socket" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "900a4c17690ef5efcccbdc833853b3e48f2a0de589427a609ddb5e866e1cb91f" +dependencies = [ + "bytes", + "dns-parser", + "libc", + "rand", +] + [[package]] name = "wasmparser" version = "0.80.2" @@ -3878,13 +4173,22 @@ version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7" dependencies = [ - "windows_aarch64_gnullvm 0.42.1", - "windows_aarch64_msvc 0.42.1", - "windows_i686_gnu 0.42.1", - "windows_i686_msvc 0.42.1", - "windows_x86_64_gnu 0.42.1", - "windows_x86_64_gnullvm 0.42.1", - "windows_x86_64_msvc 0.42.1", + "windows_aarch64_gnullvm 0.42.2", + "windows_aarch64_msvc 0.42.2", + "windows_i686_gnu 0.42.2", + "windows_i686_msvc 0.42.2", + "windows_x86_64_gnu 0.42.2", + "windows_x86_64_gnullvm 0.42.2", + "windows_x86_64_msvc 0.42.2", +] + +[[package]] +name = "windows-sys" +version = "0.45.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0" +dependencies = [ + "windows-targets 0.42.2", ] [[package]] @@ -3893,7 +4197,22 @@ version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" dependencies = [ - "windows-targets", + "windows-targets 0.48.0", +] + +[[package]] +name = "windows-targets" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071" +dependencies = [ + "windows_aarch64_gnullvm 0.42.2", + "windows_aarch64_msvc 0.42.2", + "windows_i686_gnu 0.42.2", + "windows_i686_msvc 0.42.2", + "windows_x86_64_gnu 0.42.2", + "windows_x86_64_gnullvm 0.42.2", + "windows_x86_64_msvc 0.42.2", ] [[package]] @@ -3913,9 +4232,9 @@ dependencies = [ [[package]] name = "windows_aarch64_gnullvm" -version = "0.42.1" +version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8c9864e83243fdec7fc9c5444389dcbbfd258f745e7853198f365e3c4968a608" +checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8" [[package]] name = "windows_aarch64_gnullvm" @@ -3925,9 +4244,9 @@ checksum = "91ae572e1b79dba883e0d315474df7305d12f569b400fcf90581b06062f7e1bc" [[package]] name = "windows_aarch64_msvc" -version = "0.42.1" +version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c8b1b673ffc16c47a9ff48570a9d85e25d265735c503681332589af6253c6c7" +checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43" [[package]] name = "windows_aarch64_msvc" @@ -3937,9 +4256,9 @@ checksum = "b2ef27e0d7bdfcfc7b868b317c1d32c641a6fe4629c171b8928c7b08d98d7cf3" [[package]] name = "windows_i686_gnu" -version = "0.42.1" +version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "de3887528ad530ba7bdbb1faa8275ec7a1155a45ffa57c37993960277145d640" +checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f" [[package]] name = "windows_i686_gnu" @@ -3949,9 +4268,9 @@ checksum = "622a1962a7db830d6fd0a69683c80a18fda201879f0f447f065a3b7467daa241" [[package]] name = "windows_i686_msvc" -version = "0.42.1" +version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf4d1122317eddd6ff351aa852118a2418ad4214e6613a50e0191f7004372605" +checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060" [[package]] name = "windows_i686_msvc" @@ -3961,9 +4280,9 @@ checksum = "4542c6e364ce21bf45d69fdd2a8e455fa38d316158cfd43b3ac1c5b1b19f8e00" [[package]] name = "windows_x86_64_gnu" -version = "0.42.1" +version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1040f221285e17ebccbc2591ffdc2d44ee1f9186324dd3e84e99ac68d699c45" +checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36" [[package]] name = "windows_x86_64_gnu" @@ -3973,9 +4292,9 @@ checksum = "ca2b8a661f7628cbd23440e50b05d705db3686f894fc9580820623656af974b1" [[package]] name = "windows_x86_64_gnullvm" -version = "0.42.1" +version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "628bfdf232daa22b0d64fdb62b09fcc36bb01f05a3939e20ab73aaf9470d0463" +checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3" [[package]] name = "windows_x86_64_gnullvm" @@ -3985,9 +4304,9 @@ checksum = "7896dbc1f41e08872e9d5e8f8baa8fdd2677f29468c4e156210174edc7f7b953" [[package]] name = "windows_x86_64_msvc" -version = "0.42.1" +version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "447660ad36a13288b1db4d4248e857b510e8c3a225c822ba4fb748c0aafecffd" +checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0" [[package]] name = "windows_x86_64_msvc" @@ -3995,6 +4314,15 @@ version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a" +[[package]] +name = "winreg" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80d0f4e272c85def139476380b12f9ac60926689dd2e01d4923222f40580869d" +dependencies = [ + "winapi", +] + [[package]] name = "winx" version = "0.31.0" diff --git a/crates/apis/Cargo.toml b/crates/apis/Cargo.toml index c132c56b..0c6dd14d 100644 --- a/crates/apis/Cargo.toml +++ b/crates/apis/Cargo.toml @@ -11,6 +11,7 @@ categories = ["wasm"] [features] console = [] +fetch_io = ["dep:blockless-sdk", "dep:json"] random = ["dep:fastrand"] stream_io = [] text_encoding = [] @@ -18,4 +19,9 @@ text_encoding = [] [dependencies] anyhow = { workspace = true } fastrand = { version = "2.0.1", optional = true } +hyper_wasi = { version = "0.15.0", optional = true, features = ["full"] } +tokio_wasi = { version = "1.25", optional = true, features = ["full"] } +reqwest_wasi = { version = "0.11", optional = true, features = ["blocking", "json"] } javy = { workspace = true } +blockless-sdk = { version = "0.1.3", optional = true } +json = { version = "0.12", optional = true } \ No newline at end of file diff --git a/crates/apis/src/api_config.rs b/crates/apis/src/api_config.rs index 0b0955ad..2bf42a57 100644 --- a/crates/apis/src/api_config.rs +++ b/crates/apis/src/api_config.rs @@ -9,4 +9,6 @@ pub struct APIConfig { #[cfg(feature = "console")] pub(crate) console: crate::console::ConsoleConfig, + #[cfg(feature = "fetch_io")] + pub(crate) fetch_io: crate::fetch_io::FetchIOConfig, } diff --git a/crates/apis/src/fetch_io/config.rs b/crates/apis/src/fetch_io/config.rs new file mode 100644 index 00000000..6b81e44f --- /dev/null +++ b/crates/apis/src/fetch_io/config.rs @@ -0,0 +1,25 @@ +use crate::APIConfig; + +// Use crate visibility to avoid exposing the property outside the crate +#[derive(Debug)] +pub(crate) struct FetchIOConfig { + pub(super) prefix: String, +} + +// Always have a default value for every config. +impl Default for FetchIOConfig { + fn default() -> Self { + Self { + prefix: "Default prefix: ".to_string(), + } + } +} + +// Define one or more methods on `APIConfig`, not `FetchIOConfig`, to set properties. +impl APIConfig { + /// Sets the prefix for `Javy.Env.print`. + pub fn prefix(&mut self, prefix: String) -> &mut Self { + self.fetch_io.prefix = prefix; + self + } +} \ No newline at end of file diff --git a/crates/apis/src/fetch_io/fetch.js b/crates/apis/src/fetch_io/fetch.js new file mode 100644 index 00000000..a013f976 --- /dev/null +++ b/crates/apis/src/fetch_io/fetch.js @@ -0,0 +1,12 @@ +// Wrap everything in an anonymous function to avoid leaking local variables into the global scope. +(function () { + // Get a reference to the function before we delete it from `globalThis`. + const __javy_fetchio_get = globalThis.__javy_fetchio_get; + globalThis.Javy.FetchIO = { + get(url) { + __javy_fetchio_get(url); + }, + }; + // Delete the function from `globalThis` so it doesn't leak. + Reflect.deleteProperty(globalThis, "__javy_fetchio_get"); +})(); \ No newline at end of file diff --git a/crates/apis/src/fetch_io/mod.rs b/crates/apis/src/fetch_io/mod.rs new file mode 100644 index 00000000..e4f7375e --- /dev/null +++ b/crates/apis/src/fetch_io/mod.rs @@ -0,0 +1,81 @@ +//use std::collections::HashMap; + +use anyhow::{bail, Result}; +use javy::{quickjs::JSValue, Runtime}; +use blockless_sdk::*; + +use crate::{APIConfig, JSApiSet}; +pub(super) use config::FetchIOConfig; + +mod config; + +pub(super) struct FetchIO; + +impl JSApiSet for FetchIO { + fn register(&self, runtime: &Runtime, config: &APIConfig) -> Result<()> { + let context = runtime.context(); + + let global = context.global_object()?; + + let mut javy_object = global.get_property("Javy")?; + + // If you're defining something on the `Javy` object, ensure it exists. + if javy_object.is_undefined() { + javy_object = context.object_value()?; + global.set_property("Javy", javy_object)?; + } + + // `wrap_callback`` has a static lifetime so you can't use references to the config in its body. + global.set_property( + "__javy_fetchio_get", + context.wrap_callback(move |_ctx, _this, args| { + let [url] = args else { + bail!("Incorrect number of arguments"); + }; + // Convert JSValueRefs to Rust types. + let url: String = url.try_into()?; + + let opts = HttpOptions::new("GET", 30, 10); + let http = BlocklessHttp::open(&url, &opts); + let http = http.unwrap(); + let body = http.get_all_body().unwrap(); + let body = String::from_utf8(body).unwrap(); + let data = match json::parse(&body).unwrap() { + json::JsonValue::Object(o) => o, + _ => panic!("must be object"), + }; + + println!("Http Data: {:?}", data); + + Ok(JSValue::Undefined) + })?, + )?; + + context.eval_global("fetch.js", include_str!("fetch.js"))?; + + Ok(()) + } +} + +// Automated tests are highly recommended +#[cfg(test)] +mod tests { + use std::env; + + use crate::{APIConfig, JSApiSet}; + use anyhow::Result; + use javy::Runtime; + + use super::FetchIO; + + #[test] + fn test_print_env_var() -> Result<()> { + let runtime = Runtime::default(); + let context = runtime.context(); + FetchIO.register(&runtime, &APIConfig::default())?; + env::set_var("HELLO", "there"); + let _ = context.eval_global("main", "Javy.Env.print('HELLO');")?; + env::remove_var("HELLO"); + Ok(()) + } +} diff --git a/crates/apis/src/lib.rs b/crates/apis/src/lib.rs index e433f6bf..81f923aa 100644 --- a/crates/apis/src/lib.rs +++ b/crates/apis/src/lib.rs @@ -60,6 +60,8 @@ mod runtime_ext; mod stream_io; #[cfg(feature = "text_encoding")] mod text_encoding; +#[cfg(feature = "fetch_io")] +mod fetch_io; pub(crate) trait JSApiSet { fn register(&self, runtime: &Runtime, config: &APIConfig) -> Result<()>; @@ -85,5 +87,7 @@ pub fn add_to_runtime(runtime: &Runtime, config: APIConfig) -> Result<()> { stream_io::StreamIO.register(runtime, &config)?; #[cfg(feature = "text_encoding")] text_encoding::TextEncoding.register(runtime, &config)?; + #[cfg(feature = "fetch_io")] + fetch_io::FetchIO.register(runtime, &config)?; Ok(()) } diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml index 8781578f..104863ef 100644 --- a/crates/core/Cargo.toml +++ b/crates/core/Cargo.toml @@ -16,7 +16,7 @@ crate-type = ["cdylib"] [dependencies] anyhow = { workspace = true } javy = { workspace = true, features = ["export_alloc_fns"] } -javy-apis = { path = "../apis", features = ["console", "text_encoding", "random", "stream_io"] } +javy-apis = { path = "../apis", features = ["console", "text_encoding", "random", "stream_io", "fetch_io"] } once_cell = { workspace = true } [features]