How to read Uint32Array sent from JavaScript?

⚓ Rust    📅 2025-09-08    👤 surdeus    👁️ 2      

surdeus

I'm testing this WebTransport server written in Rust. What I do to keep track of the expected length of the incoming message which can span multiple reads is on first client write write the length encoded as Uint32Array. Read that length in the server, now the server knows what is expected N MB later.

This is what I do using a JavaScript-based WebTransport server that I am trying to do in Rust.

What the client does

    let data = new Uint8Array(1024**2*20);

    let header = new Uint8Array(Uint32Array.from(
      {
        length: 4,
      },
      (_, index) => (data.length >> (index * 8)) & 0xff,
    ));
    let view = new DataView(header.buffer);
    let outgoingTotalLength = view.getUint32(0, true);
    console.log({ outgoingTotalLength });
    let incomingTotalLength = 0;
    // ...
    for await (const value of readable) {
      incomingTotalLength += value.length;
      if (incomingTotalLength === outgoingTotalLength) {
        console.log({ incomingTotalLength, outgoingTotalLength });
        break;
      }
    }

In the server, something like this

    let incomingTotalLength = 0;
    let incomingCurrentLength = 0;
    const buffer = new ArrayBuffer(0, { maxByteLength: 4 });
    const view = new DataView(buffer);
    // ...
    if (incomingTotalLength === 0 && incomingCurrentLength === 0) {
              buffer.resize(4);
              for (let i = 0; i < 4; i++) {
                view.setUint8(i, value[i]);
              }
              incomingTotalLength = view.getUint32(0, true);
              console.log(value.length, incomingTotalLength);
              value = value.subarray(4);
        }
      // ...
      buffer.resize(0);
      incomingTotalLength = 0;
      incomingCurrentLength = 0;

The closest I've come to trying to do this in Rust is in a Native Messaging host, which might work here?

pub fn getMessage() -> io::Result<Vec<u8>> {
  let mut stdin = io::stdin();
  let mut length = [0; 4];
  stdin.read_exact(&mut length)?;
  let mut buffer = vec![0; u32::from_ne_bytes(length) as usize];
  stdin.read_exact(&mut buffer)?;
  Ok(buffer)
}

How would you write that out in Rust?

2 posts - 1 participant

Read full topic

🏷️ Rust_feed