Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tombl/surd
A binary serialization library
https://github.com/tombl/surd
Last synced: 26 days ago
JSON representation
A binary serialization library
- Host: GitHub
- URL: https://github.com/tombl/surd
- Owner: tombl
- License: mit
- Created: 2021-12-27T18:00:13.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2021-12-27T18:00:27.000Z (almost 3 years ago)
- Last Synced: 2024-11-09T01:04:31.343Z (about 1 month ago)
- Language: TypeScript
- Homepage:
- Size: 7.81 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
- awesome-blazingly-fast - surd - A blazing fast binary serialization library (TypeScript)
README
# surd
> A blazing fast binary serialization library## Goals
- 100% TypeScript inference
- Browser and Node.js support
- Fast serialization/deserialization
- Compact binary sizes## Non-goals
- Binary validation## Usage
```js
import { t } from "surd";const schema = t.string;
const serialized = schema.serialize("hello world");
const [deserialized, trailing] = schema.deserialize(serialized);assert(deserialized === "hello world")
asserts(trailing.byteLength === 0);
```### Packer
A minimal data compressor that run-length encodes zeroes, useful for low numbers which contain lots of padding zeroes.```js
import { pack, t, unpack } from "surd";const schema = t.uint32;
const serialized = schema.serialize(12);
const packed = pack(serialized);assert(packed.byteLength < serialized.byteLength);
const [deserialized] = schema.deserialize(unpack(serialized));
assert(deserialized === 12);
```### TypeScript
```ts
import { t, type TypeOf } from "surd";const point = t.struct({
x: t.int16,
y: t.int16
});type Point = TypeOf;
// equivalent to
type Point = { x: number; y: number };
```## Types
```js
import { t } from "surd";// numbers
t.float32;
t.float64;
t.uint8;
t.uint16;
t.uint32;
t.int8;
t.int16;
t.int32;
t.bigint64;
t.biguint64;// other primitives/builtin types
t.string;
t.boolean;
t.date;
t.arrayBuffer;// literals
t.literal("foo") // any primitive
t.undefined; // aliased as t.void for TypeScript reasons
t.null;// compound types
t.array(t.string); // -> string[]
t.map(t.string, t.boolean); // -> Map
t.set(t.uint32); // -> Set
t.tuple(t.string, t.int32, t.boolean); // -> [string, number, boolean]
t.struct({
foo: t.uint8,
bar: t.int32,
baz: t.boolean,
}); // -> { foo: number, bar number, baz: boolean }// nullability
t.nullOr(t.string); // -> string | null
t.undefinedOr(t.string); // -> string | undefined// enums
t.enum("foo", "bar", 12, true); // -> "foo" | "bar" | 12 | true
t.union({
loggedIn: t.struct({ username: t.string }),
loggedOut: t.void,
}) // -> ["loggedIn", { username: string }] | ["loggedOut", void];
```