https://github.com/mxjp/binary
Utilities for binary data serialization & encoding
https://github.com/mxjp/binary
Last synced: 14 days ago
JSON representation
Utilities for binary data serialization & encoding
- Host: GitHub
- URL: https://github.com/mxjp/binary
- Owner: mxjp
- License: mit
- Created: 2021-12-08T20:03:31.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2025-02-06T19:49:18.000Z (3 months ago)
- Last Synced: 2025-03-19T00:38:44.437Z (about 1 month ago)
- Language: TypeScript
- Size: 591 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# @mpt/binary
Utilities for binary data serialization & encoding# Installation
```bash
npm i @mpt/binary
```
```ts
import { ... } from "@mpt/binary";
```## Serialization / deserialization
```ts
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();
// => 42deserializer.utf8(deserializer.uint16());
// => "Hello World!"
```## Readable stream deserialization
```ts
const res = await fetch("https://example.com/example-payload");const deserializer = new StreamDeserializer(res.body);
await deserializer.deserialize(d => {
return d.uint8();
});
// => 42await deserializer.deserialize(d => {
return d.utf8(d.uint16());
});
// => "Hello World!"
```## Encoding
```ts
encodeBase64(new TextEncoder().encode("Hello World!"));
// => "SGVsbG8gV29ybGQh"new TextDecoder().decode(decodeBase64("SGVsbG8gV29ybGQh"));
// => "Hello World!"
```
Supported encodings:
+ hex
+ base64
+ base64url
+ base32 (encode only)