diff --git a/.gitignore b/.gitignore index ea8c4bf7..bcd5559c 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ /target + +.DS_Store diff --git a/Cargo.lock b/Cargo.lock index e6675654..bdcc77f7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -118,7 +118,7 @@ dependencies = [ "heck", "proc-macro2", "quote", - "syn 2.0.15", + "syn 2.0.52", ] [[package]] @@ -137,6 +137,16 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "console_error_panic_hook" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a06aeb73f470f66dcdbf7223caeebb85984942f22f1adb2a088cf9668146bbbc" +dependencies = [ + "cfg-if", + "wasm-bindgen", +] + [[package]] name = "core-foundation-sys" version = "0.8.3" @@ -219,7 +229,7 @@ dependencies = [ "proc-macro2", "quote", "strsim", - "syn 2.0.15", + "syn 2.0.52", ] [[package]] @@ -230,7 +240,7 @@ checksum = "7bfb82b62b1b8a2a9808fb4caf844ede819a76cfc23b2827d7f94eefb49551eb" dependencies = [ "darling_core", "quote", - "syn 2.0.15", + "syn 2.0.52", ] [[package]] @@ -332,9 +342,9 @@ checksum = "4217ad341ebadf8d8e724e264f13e593e0648f5b3e94b3896a5df283be015ecc" [[package]] name = "js-sys" -version = "0.3.60" +version = "0.3.69" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49409df3e3bf0856b916e2ceaca09ee28e6871cf7d9ce97a692cacfdb2a25a47" +checksum = "29c15563dc2726973df627357ce0c9ddddbea194836909d655df6a75d2cf296d" dependencies = [ "wasm-bindgen", ] @@ -399,18 +409,18 @@ checksum = "e82dad04139b71a90c080c8463fe0dc7902db5192d939bd0950f074d014339e1" [[package]] name = "proc-macro2" -version = "1.0.69" +version = "1.0.79" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "134c189feb4956b20f6f547d2cf727d4c0fe06722b20a0eec87ed445a97f92da" +checksum = "e835ff2298f5721608eb1a980ecaee1aef2c132bf95ecc026a11b7bf3c01c02e" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.26" +version = "1.0.35" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4424af4bf778aae2051a77b60283332f386554255d722233d09fbfc7e30da2fc" +checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" dependencies = [ "proc-macro2", ] @@ -445,6 +455,12 @@ dependencies = [ "syn 1.0.98", ] +[[package]] +name = "scoped-tls" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294" + [[package]] name = "scratch" version = "1.0.2" @@ -453,22 +469,31 @@ checksum = "9c8132065adcfd6e02db789d9285a0deb2f3fcb04002865ab67d5fb103533898" [[package]] name = "serde" -version = "1.0.160" +version = "1.0.197" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bb2f3770c8bce3bcda7e149193a069a0f4365bda1fa5cd88e03bca26afc1216c" +checksum = "3fb1c873e1b9b056a4dc4c0c198b24c3ffa059243875552b2bd0933b1aee4ce2" dependencies = [ "serde_derive", ] +[[package]] +name = "serde-json-wasm" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f05da0d153dd4595bdffd5099dc0e9ce425b205ee648eb93437ff7302af8c9a5" +dependencies = [ + "serde", +] + [[package]] name = "serde_derive" -version = "1.0.160" +version = "1.0.197" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "291a097c63d8497e00160b166a967a4a79c64f3facdd01cbd7502231688d77df" +checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.15", + "syn 2.0.52", ] [[package]] @@ -484,9 +509,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.89" +version = "1.0.114" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "020ff22c755c2ed3f8cf162dbb41a7268d934702f3ed3631656ea597e08fc3db" +checksum = "c5f09b1bd632ef549eaa9f60a1f8de742bdbc698e6cee2095fc84dde5f549ae0" dependencies = [ "itoa", "ryu", @@ -518,7 +543,7 @@ dependencies = [ "darling", "proc-macro2", "quote", - "syn 2.0.15", + "syn 2.0.52", ] [[package]] @@ -550,6 +575,18 @@ dependencies = [ "thiserror", ] +[[package]] +name = "stellar-xdr-wasm" +version = "0.1.0" +dependencies = [ + "console_error_panic_hook", + "serde-json-wasm", + "serde_json", + "stellar-xdr", + "wasm-bindgen", + "wasm-bindgen-test", +] + [[package]] name = "strsim" version = "0.10.0" @@ -569,9 +606,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.15" +version = "2.0.52" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a34fcf3e8b60f57e6a14301a2e916d323af98b0ea63c599441eec8558660c822" +checksum = "b699d15b36d1f02c3e7c69f8ffef53de37aefae075d8488d4ba1a7788d574a07" dependencies = [ "proc-macro2", "quote", @@ -633,9 +670,9 @@ checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b" [[package]] name = "wasm-bindgen" -version = "0.2.83" +version = "0.2.92" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eaf9f5aceeec8be17c128b2e93e031fb8a4d469bb9c4ae2d7dc1888b26887268" +checksum = "4be2531df63900aeb2bca0daaaddec08491ee64ceecbee5076636a3b026795a8" dependencies = [ "cfg-if", "wasm-bindgen-macro", @@ -643,24 +680,36 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.83" +version = "0.2.92" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c8ffb332579b0557b52d268b91feab8df3615f265d5270fec2a8c95b17c1142" +checksum = "614d787b966d3989fa7bb98a654e369c762374fd3213d212cfc0251257e747da" dependencies = [ "bumpalo", "log", "once_cell", "proc-macro2", "quote", - "syn 1.0.98", + "syn 2.0.52", "wasm-bindgen-shared", ] +[[package]] +name = "wasm-bindgen-futures" +version = "0.4.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76bc14366121efc8dbb487ab05bcc9d346b3b5ec0eaa76e46594cabbe51762c0" +dependencies = [ + "cfg-if", + "js-sys", + "wasm-bindgen", + "web-sys", +] + [[package]] name = "wasm-bindgen-macro" -version = "0.2.83" +version = "0.2.92" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "052be0f94026e6cbc75cdefc9bae13fd6052cdcaf532fa6c45e7ae33a1e6c810" +checksum = "a1f8823de937b71b9460c0c34e25f3da88250760bec0ebac694b49997550d726" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -668,22 +717,57 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.83" +version = "0.2.92" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07bc0c051dc5f23e307b13285f9d75df86bfdf816c5721e573dec1f9b8aa193c" +checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7" dependencies = [ "proc-macro2", "quote", - "syn 1.0.98", + "syn 2.0.52", "wasm-bindgen-backend", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-shared" -version = "0.2.83" +version = "0.2.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96" + +[[package]] +name = "wasm-bindgen-test" +version = "0.3.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9bf62a58e0780af3e852044583deee40983e5886da43a271dd772379987667b" +dependencies = [ + "console_error_panic_hook", + "js-sys", + "scoped-tls", + "wasm-bindgen", + "wasm-bindgen-futures", + "wasm-bindgen-test-macro", +] + +[[package]] +name = "wasm-bindgen-test-macro" +version = "0.3.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7f89739351a2e03cb94beb799d47fb2cac01759b40ec441f7de39b00cbf7ef0" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.52", +] + +[[package]] +name = "web-sys" +version = "0.3.69" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c38c045535d93ec4f0b4defec448e4291638ee608530863b1e2ba115d4fff7f" +checksum = "77afa9a11836342370f4817622a2f0f418b134426d91a82dfb48f532d2ec13ef" +dependencies = [ + "js-sys", + "wasm-bindgen", +] [[package]] name = "winapi" diff --git a/Cargo.toml b/Cargo.toml index b536f781..5e6a6405 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -49,9 +49,18 @@ schemars = ["alloc", "serde", "serde_json", "dep:schemars"] arbitrary = ["std", "dep:arbitrary"] hex = [] +wasm = ["std", "curr", "base64", "serde", "serde_json", "schemars", "dep:thiserror"] +# can also be compiled with --wasm32-wasi +cli = ["dep:clap", "wasm"] # Features for the CLI. -cli = ["std", "curr", "next", "base64", "serde", "serde_json", "schemars", "dep:clap", "dep:thiserror"] [package.metadata.docs.rs] all-features = true rustdoc-args = ["--cfg", "docs"] + +[workspace] +members = ["crates/*"] + +[profile.release] +# Tell `rustc` to optimize for small code size. +opt-level = "s" diff --git a/README.md b/README.md index 5ec42dc6..35b1d8ad 100644 --- a/README.md +++ b/README.md @@ -87,4 +87,28 @@ Parse a `BucketEntry` framed stream from a bucket file: stellar-xdr decode --type BucketEntry --input stream-framed --output json-formatted bucket.xdr ``` +## Wasm pack + +`wasm-pack.sh` will use [`wasm-pack`](https://rustwasm.github.io/wasm-pack/installer/) to build a npm package and use [yalc](https://github.com/wclr/yalc) pubilsh it locally. It uses the following environment variables: +- `PROFILE` - default `dev` +- `TARGETS` - default `bundler nodejs web no-modules` + +After running the script you can install the package corresponding target with yalc: + +``` +yalc add stellar-xdr-wasm-web +yalc link stellar-xdr-wasm-web +``` + +Then you can import package as normal + +```ts +import * as xdr from "stellar-xdr-wasm-web"; +``` + +#### cargo watch for changes + +``` +cargo watch -s "TARGETS=web ./wasm-pack.sh" +``` License: Apache-2.0 diff --git a/crates/stellar-xdr-wasm/.appveyor.yml b/crates/stellar-xdr-wasm/.appveyor.yml new file mode 100644 index 00000000..50910bd6 --- /dev/null +++ b/crates/stellar-xdr-wasm/.appveyor.yml @@ -0,0 +1,11 @@ +install: + - appveyor-retry appveyor DownloadFile https://win.rustup.rs/ -FileName rustup-init.exe + - if not defined RUSTFLAGS rustup-init.exe -y --default-host x86_64-pc-windows-msvc --default-toolchain nightly + - set PATH=%PATH%;C:\Users\appveyor\.cargo\bin + - rustc -V + - cargo -V + +build: false + +test_script: + - cargo test --locked diff --git a/crates/stellar-xdr-wasm/.gitignore b/crates/stellar-xdr-wasm/.gitignore new file mode 100644 index 00000000..4e301317 --- /dev/null +++ b/crates/stellar-xdr-wasm/.gitignore @@ -0,0 +1,6 @@ +/target +**/*.rs.bk +Cargo.lock +bin/ +pkg/ +wasm-pack.log diff --git a/crates/stellar-xdr-wasm/Cargo.toml b/crates/stellar-xdr-wasm/Cargo.toml new file mode 100644 index 00000000..df34f280 --- /dev/null +++ b/crates/stellar-xdr-wasm/Cargo.toml @@ -0,0 +1,28 @@ +[package] +name = "stellar-xdr-wasm" +version = "0.1.0" +authors = ["Willem Wyndham "] +edition = "2018" + +[lib] +crate-type = ["cdylib", "rlib"] + +[features] +default = ["console_error_panic_hook"] +schema = [] + +[dependencies] +wasm-bindgen = "0.2.84" +stellar-xdr = { path = "../..", package = "stellar-xdr", features = ["wasm"]} + +# The `console_error_panic_hook` crate provides better debugging of panics by +# logging them with `console.error`. This is great for development, but requires +# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for +# code size when deploying. +console_error_panic_hook = { version = "0.1.7", optional = true } +serde-json-wasm = "1.0.1" +serde_json = "1.0.114" + +[dev-dependencies] +wasm-bindgen-test = "0.3.34" + diff --git a/crates/stellar-xdr-wasm/README.md b/crates/stellar-xdr-wasm/README.md new file mode 100644 index 00000000..6b684085 --- /dev/null +++ b/crates/stellar-xdr-wasm/README.md @@ -0,0 +1,84 @@ +
+ +

wasm-pack-template

+ + A template for kick starting a Rust and WebAssembly project using wasm-pack. + +

+ Build Status +

+ +

+ Tutorial + | + Chat +

+ + Built with 🦀🕸 by The Rust and WebAssembly Working Group +
+ +## About + +[**📚 Read this template tutorial! 📚**][template-docs] + +This template is designed for compiling Rust libraries into WebAssembly and +publishing the resulting package to NPM. + +Be sure to check out [other `wasm-pack` tutorials online][tutorials] for other +templates and usages of `wasm-pack`. + +[tutorials]: https://rustwasm.github.io/docs/wasm-pack/tutorials/index.html +[template-docs]: https://rustwasm.github.io/docs/wasm-pack/tutorials/npm-browser-packages/index.html + +## 🚴 Usage + +### 🐑 Use `cargo generate` to Clone this Template + +[Learn more about `cargo generate` here.](https://github.com/ashleygwilliams/cargo-generate) + +``` +cargo generate --git https://github.com/rustwasm/wasm-pack-template.git --name my-project +cd my-project +``` + +### 🛠️ Build with `wasm-pack build` + +``` +wasm-pack build +``` + +### 🔬 Test in Headless Browsers with `wasm-pack test` + +``` +wasm-pack test --headless --firefox +``` + +### 🎁 Publish to NPM with `wasm-pack publish` + +``` +wasm-pack publish +``` + +## 🔋 Batteries Included + +* [`wasm-bindgen`](https://github.com/rustwasm/wasm-bindgen) for communicating + between WebAssembly and JavaScript. +* [`console_error_panic_hook`](https://github.com/rustwasm/console_error_panic_hook) + for logging panic messages to the developer console. +* `LICENSE-APACHE` and `LICENSE-MIT`: most Rust projects are licensed this way, so these are included for you + +## License + +Licensed under either of + +* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) +* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) + +at your option. + +### Contribution + +Unless you explicitly state otherwise, any contribution intentionally +submitted for inclusion in the work by you, as defined in the Apache-2.0 +license, shall be dual licensed as above, without any additional terms or +conditions. diff --git a/crates/stellar-xdr-wasm/src/lib.rs b/crates/stellar-xdr-wasm/src/lib.rs new file mode 100644 index 00000000..58a59daa --- /dev/null +++ b/crates/stellar-xdr-wasm/src/lib.rs @@ -0,0 +1,96 @@ +mod utils; + +use std::{ + cmp, + io::{self, Read}, + str::FromStr, +}; + +use stellar_xdr::{ + curr::{self, TypeVariant, WriteXdr}, + schema::Schema, +}; +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +pub fn type_variants() -> String { + format!( + "[{}]", + TypeVariant::VARIANTS_STR + .iter() + .map(|s| format!("{s:?}")) + .collect::>() + .join(", ") + ) +} + +// #[cfg(feature = "schema")] +#[wasm_bindgen] +pub fn schema(type_variant: &str) -> Result { + let Schema(json_schema): Schema = type_variant + .parse() + .map_err(|_| JsValue::from_str("unknown type"))?; + serde_json_wasm::to_string(&json_schema).map_err(|e| JsValue::from_str(&format!("{e:?}"))) +} + +#[wasm_bindgen] +pub fn from_xdr(xdr_base64: String, variant: Option) -> String { + let mut f = curr::Limited::new(ResetRead::new(xdr_base64.as_bytes()), curr::Limits::none()); + for variant in variant + .map(|v| Some(vec![v.parse::().ok()?])) + .unwrap_or_else(|| Some(TypeVariant::VARIANTS.to_vec())) + .unwrap() + { + f.inner.reset(); + if let Ok(res) = curr::Type::read_xdr_to_end(variant, &mut f) { + return serde_json_wasm::to_string(&res) + .unwrap_or_else(|e| format!("{{\"error\": \"{e}\"}}")); + } + } + "{\"error\": \"unknown type\"}".to_string() +} + +#[wasm_bindgen] +pub fn to_xdr(json_string: String, variant: String) -> String { + let r#type = TypeVariant::from_str(&variant).unwrap(); + let t = curr::Type::read_json(r#type, json_string.as_bytes()).unwrap(); + t.to_xdr_base64(curr::Limits::none()).unwrap() +} + +struct ResetRead { + read: R, + buf: Vec, + cursor: usize, +} + +impl ResetRead { + fn new(r: R) -> Self { + Self { + read: r, + buf: Vec::new(), + cursor: 0, + } + } + + fn reset(&mut self) { + self.cursor = 0; + } +} + +impl Read for ResetRead { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + // Read from the buffer first into buf. + let n = cmp::min(self.buf.len() - self.cursor, buf.len()); + buf[..n].copy_from_slice(&self.buf[self.cursor..self.cursor + n]); + // Read from the reader and cache the result in the buf if the buf is consumed. + if n < buf.len() { + let read_n = self.read.read(buf)?; + self.buf.extend_from_slice(&buf[n..n + read_n]); + self.cursor += n + read_n; + Ok(n + read_n) + } else { + self.cursor += n; + Ok(n) + } + } +} diff --git a/crates/stellar-xdr-wasm/src/utils.rs b/crates/stellar-xdr-wasm/src/utils.rs new file mode 100644 index 00000000..c4be847e --- /dev/null +++ b/crates/stellar-xdr-wasm/src/utils.rs @@ -0,0 +1,11 @@ +#[allow(dead_code)] +pub fn set_panic_hook() { + // When the `console_error_panic_hook` feature is enabled, we can call the + // `set_panic_hook` function at least once during initialization, and then + // we will get better error messages if our code ever panics. + // + // For more details see + // https://github.com/rustwasm/console_error_panic_hook#readme + #[cfg(feature = "console_error_panic_hook")] + console_error_panic_hook::set_once(); +} diff --git a/crates/stellar-xdr-wasm/tests/web.rs b/crates/stellar-xdr-wasm/tests/web.rs new file mode 100644 index 00000000..de5c1daf --- /dev/null +++ b/crates/stellar-xdr-wasm/tests/web.rs @@ -0,0 +1,13 @@ +//! Test suite for the Web and headless browsers. + +#![cfg(target_arch = "wasm32")] + +extern crate wasm_bindgen_test; +use wasm_bindgen_test::*; + +wasm_bindgen_test_configure!(run_in_browser); + +#[wasm_bindgen_test] +fn pass() { + assert_eq!(1 + 1, 2); +} diff --git a/src/lib.rs b/src/lib.rs index e4c8d923..6fe5eea1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -126,3 +126,6 @@ pub mod next; #[cfg(feature = "cli")] pub mod cli; + +#[cfg(any(feature = "cli", feature = "wasm"))] +pub mod schema; diff --git a/src/schema.rs b/src/schema.rs new file mode 100644 index 00000000..7afccf39 --- /dev/null +++ b/src/schema.rs @@ -0,0 +1,68 @@ +#[derive(thiserror::Error, Debug)] +pub enum Error { + #[error("unknown type {0}, choose one of {1:?}")] + UnknownType(String, &'static [&'static str]), + #[error("error generating JSON: {0}")] + GenerateJson(#[from] serde_json::Error), +} + +use schemars::{ + gen::SchemaSettings, + visit::{self, Visitor}, +}; +use std::str::FromStr; + +pub struct Schema(pub schemars::schema::RootSchema); + +impl TryFrom for Schema { + type Error = Error; + fn try_from(variant: crate::curr::TypeVariant) -> Result { + println!("hhh"); + let settings = SchemaSettings::draft07().with_visitor(ReplaceAdditionalProperties); + let generator = settings.into_generator(); + Ok(Schema(variant.json_schema(generator))) + } +} + +impl FromStr for Schema { + type Err = Error; + fn from_str(s: &str) -> Result { + s.parse::() + .map_err(|_| { + Error::UnknownType(s.to_string(), &crate::curr::TypeVariant::VARIANTS_STR) + })? + .try_into() + } +} + +/// This visitor will remote "additionalProperties" from all objects in the schema. +#[derive(Debug, Clone)] +pub struct ReplaceAdditionalProperties; + +impl Visitor for ReplaceAdditionalProperties { + fn visit_schema_object(&mut self, schema: &mut schemars::schema::SchemaObject) { + schema.object().additional_properties = None; + add_titles(schema.subschemas()); + visit::visit_schema_object(self, schema); + } +} + +/// This function will add titles to all one_of schemas in the schema. So that it is more readable. +/// E.g. it was `Option 1`, etc before +fn add_titles(sub_schema: &mut schemars::schema::SubschemaValidation) { + if let Some(ref mut one_of) = sub_schema.one_of { + one_of.iter_mut().for_each(|schema| { + if let schemars::schema::Schema::Object(ref mut obj) = schema { + if let Some(inner_obj) = &mut obj.object { + if let Some(title) = inner_obj.required.first() { + obj.metadata().title = Some(title.clone()); + } + } else if let Some(enum_values) = &obj.enum_values { + if let Some(title) = enum_values.get(2) { + obj.metadata().title = Some(title.to_string()); + } + } + } + }); + } +} diff --git a/tests/serde_tx_schema.rs b/tests/serde_tx_schema.rs index 1c4a95fc..6f7d9ea6 100644 --- a/tests/serde_tx_schema.rs +++ b/tests/serde_tx_schema.rs @@ -1,13 +1,12 @@ -#![cfg(feature = "curr")] -#![cfg(all(feature = "schemars", feature = "serde", feature = "alloc"))] +#![cfg(feature = "wasm")] -use stellar_xdr::curr as stellar_xdr; +use stellar_xdr::{curr::TypeVariant, schema::Schema}; #[allow(clippy::too_many_lines)] #[test] fn test_serde_tx_schema() -> Result<(), Box> { - let schema = schemars::schema_for!(stellar_xdr::ScVal); - let s = serde_json::to_string_pretty(&schema)?; + let schema: Schema = TypeVariant::ScVal.try_into()?; + let s = serde_json::to_string_pretty(&schema.0)?; println!("{s}"); Ok(()) } diff --git a/wasm-pack.sh b/wasm-pack.sh new file mode 100755 index 00000000..90ad545f --- /dev/null +++ b/wasm-pack.sh @@ -0,0 +1,23 @@ +#!/usr/bin/env bash + +set -e +alias npx='npx -y' +( + cd crates/stellar-xdr-wasm + # iterates over all targets and builds each on in it's on folder + for TARGET in ${TARGETS:-bundler nodejs web no-modules} ; do + OUT_DIR=pkg/$TARGET + npx wasm-pack build --"${PROFILE:-dev}" \ + --target "$TARGET" \ + --out-dir "$OUT_DIR" \ + --out-name "stellar-xdr-wasm-$TARGET" + ( + cd "$OUT_DIR"; + # shellcheck disable=SC2016 + npx node-jq --arg TARGET "stellar-xdr-wasm-$TARGET" \ + '.name = $TARGET' package.json > tmp.json; + mv tmp.json package.json; + npx yalc publish; + ) + done +)