#input-stream #r-socket #string #guest #wasm-rs #protocols

no-std wasmrs-guest

wasmRS guest implementation of the RSocket protocol for reactive streams in WebAssembly

16 breaking releases

0.17.0 Oct 9, 2023
0.15.0 Aug 17, 2023
0.14.0 Jul 26, 2023
0.8.0 Mar 22, 2023
0.1.0 Dec 27, 2022

#890 in WebAssembly

Download history 43/week @ 2023-12-05 50/week @ 2023-12-12 59/week @ 2023-12-19 43/week @ 2023-12-26 20/week @ 2024-01-02 53/week @ 2024-01-09 53/week @ 2024-01-16 24/week @ 2024-01-23 21/week @ 2024-01-30 46/week @ 2024-02-06 56/week @ 2024-02-13 32/week @ 2024-02-20 89/week @ 2024-02-27 55/week @ 2024-03-05 82/week @ 2024-03-12 58/week @ 2024-03-19

286 downloads per month
Used in 27 crates (2 directly)

Apache-2.0

200KB
5K SLoC

wasmrs-guest

This crate provides the WebAssembly-side logic for wasmRS modules using the wasmRS RSocket protocol.

Usage

This is a basic implementation of a WebAssembly module that exports three operations:

  • greeting::sayHello(input: string) -> string - returns a greeting, e.g. `Hello World!'
  • echo::chars(input: string) -> stream string - returns a stream of string representing each character in the input string
  • echo::reverse(input: stream string) -> stream string - reverses each string from the input stream and outputs it on a stream.
use guest::*;
use wasmrs_guest as guest;

#[no_mangle]
extern "C" fn __wasmrs_init(guest_buffer_size: u32, host_buffer_size: u32, max_host_frame_len: u32) {
  guest::init(guest_buffer_size, host_buffer_size, max_host_frame_len);

  guest::register_request_response("greeting", "sayHello", request_response);
  guest::register_request_stream("echo", "chars", request_stream);
  guest::register_request_channel("echo", "reverse", request_channel);
}

fn request_response(input: Mono<ParsedPayload, PayloadError>) -> Result<Mono<Payload, PayloadError>, GenericError> {
  Ok(async move {
    let input = deserialize::<String>(&input.await.unwrap().data).unwrap();
    let output = format!("Hello, {}!", input);
    Ok(Payload::new_data(None, Some(serialize(&output).unwrap().into())))
  }.boxed())
}

fn request_stream(
  input: Mono<ParsedPayload, PayloadError>,
) -> Result<FluxReceiver<Payload, PayloadError>, GenericError> {
  let channel = FluxChannel::<Payload, PayloadError>::new();
  let rx = channel.take_rx().unwrap();
  spawn(async move {
    let input = deserialize::<String>(&input.await.unwrap().data).unwrap();
    for char in input.chars() {
      channel
        .send(Payload::new_data(None, Some(serialize(&char).unwrap().into())))
        .unwrap();
    }
  });

  Ok(rx)
}
fn request_channel(
  mut input: FluxReceiver<ParsedPayload, PayloadError>,
) -> Result<FluxReceiver<Payload, PayloadError>, GenericError> {
  let channel = FluxChannel::<Payload, PayloadError>::new();
  let rx = channel.take_rx().unwrap();
  spawn(async move {
    while let Some(payload) = input.next().await {
      if let Err(e) = payload {
        println!("{}", e);
        continue;
      }
      let payload = payload.unwrap();
      let input = deserialize::<String>(&payload.data).unwrap();
      let output: String = input.chars().rev().collect();
      if let Err(e) = channel.send(Payload::new_data(None, Some(serialize(&output).unwrap().into()))) {
        println!("{}", e);
      }
    }
  });

  Ok(rx)
}

Apex Code generators

NanoBus iota code generators use the wasmRS protocol. You can build wasmRS modules from those templates using the https://github.com/apexlang/apex CLI.

Run the following command to get started:

$ apex new git@github.com:nanobus/iota.git -p templates/rust [your-project]

From there, edit the apex.axdl interface definition to match your needs and run apex build to generate the wasmRS module.

More Information

WasmRS makes heavy use of generated code from apex specs and generators to automate all of the boilerplate. See the getting-started for usage.

For more information on wasmRS, see the core wasmrs crate.

Contributing

See CONTRIBUTING.md

License

See the root LICENSE.txt

Dependencies

~3–12MB
~108K SLoC