5.2.0 • Published 8 months ago

@mpt/binary v5.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

@mpt/binary

Utilities for binary data serialization & encoding

Installation

npm i @mpt/binary
import { ... } from "@mpt/binary";

Serialization / deserialization

const data = new Serializer()
    .uint8(42)
    .prefixedUTF8(Serializer.prototype.uint16, "Hello World!")
    .serialize();

// data: Uint8Array { 2a 00 0c 48 65 6c 6c 6f 20 57 6f 72 6c 64 21 }

const deserializer = new Deserializer(data);

deserializer.uint8();
// => 42

deserializer.utf8(deserializer.uint16());
// => "Hello World!"

Readable stream deserialization

const res = await fetch("https://example.com/example-payload");

const deserializer = new StreamDeserializer(res.body);

await deserializer.deserialize(d => {
    return d.uint8();
});
// => 42

await deserializer.deserialize(d => {
    return d.utf8(d.uint16());
});
// => "Hello World!"

Encoding

encodeBase64(new TextEncoder().encode("Hello World!"));
// => "SGVsbG8gV29ybGQh"

new TextDecoder().decode(decodeBase64("SGVsbG8gV29ybGQh"));
// => "Hello World!"

Supported encodings:

  • hex
  • base64
  • base64url
  • base32 (encode only)

Memory Usage

Some encoders/decoders in this library make use of an internal shared buffer to avoid memory allocations if possible.

setupSharedBuffer(2048);
5.2.0

8 months ago

5.1.0

1 year ago

5.0.0

1 year ago

4.1.0

1 year ago

4.0.0

1 year ago

3.3.0

2 years ago

3.2.0

2 years ago

3.1.0

2 years ago

3.0.0

2 years ago

2.0.0

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago