0.1.0 • Published 5 years ago

cobs-transform-streams v0.1.0

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

Consistent Overhead Byte Stuffing Transform Streams

Encode or decode using COBS (Consistent Overhead Byte Stuffing) using COBS Transfors Streams (https://streams.spec.whatwg.org/#ts-class)

More info: https://en.wikipedia.org/wiki/Consistent_Overhead_Byte_Stuffing

Example

Instantiation and use

  const decoder = new TransformStream(new COBSDecoderTransformer);

Reading data

  const reader = decoder.readable.getReader();

  function processData(chunk) {
    let { done, value } = chunk;
    if (done) {
      return;
    }

    const view = new DataView(value.buffer);

    // do stuff with data.
    return reader.read().then(processData);
  };

  reader.read().then(processData);

Writing data

  const writer = decoder.readable.getWriter();

  async readFromDevice() {
    // Example using Web USB to get 32 bytes on endpoint 5
    const transfer = await this.device.transferIn(5, 32);

    const data = new Uint8Array(transfer.data.buffer);
    writer.write(data);

    this.readFromDevice();
  }