npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@rbxts/flamework-binary-serializer

v0.6.0

Published

This is a small and simple library that allows you to specify a small and optimized structure for binary data.

Downloads

644

Readme

flamework-binary-serializer

This is a small and simple library that allows you to specify a small and optimized structure for binary data.

This package is not an official Flamework package, but it does use Flamework to automatically generate a description given any arbitrary TS type. You should refer to the Flamework documentation for installation steps.

Demo

Documentation is not planned, but here's an example of how to use the library.

You should only call createBinarySerializer once, most likely as an export of a shared file.

Serialization returns a buffer and a blobs array. The blobs array contains things that we leave Roblox to serialize (instances, unknown values, etc.)

import { DataType, createBinarySerializer } from "@rbxts/flamework-binary-serializer";

export interface Data {
	optional?: boolean;
	f64: number;
	f32: DataType.f32;

	u8: DataType.u8;
	u16: DataType.u16;
	u32: DataType.u32;

	i8: DataType.i8;
	i16: DataType.i16;
	i32: DataType.i32;

	vector: Vector3;
	cframe: CFrame;
	color3: Color3;
	colorSequence: ColorSequence;
	numberSequence: NumberSequence;
	enum: Enum.KeyCode;

	boolean: boolean;
	string: string;
	array: number[];

	// flamework-binary-serializer will optimize the `type` field into a single byte.
	// The rest of the object will serialize like a normal object, but without the `type` field.
	union: { type: "string"; value: string } | { type: "number"; value: number } | { type: "boolean"; value: boolean };
	unionOfStrings: "a" | "b" | "c" | "d" | "e" | "a very large string that will not exist in the serialized output!";
	unionOfPrimitives: 1 | 2 | "a" | "b" | true | undefined;

	tuple: [string, number, boolean];
	tupleWithRest: [string, number, boolean, ...string[]];

	// flamework-binary-serializer will use Roblox's serialization for types it does not recognize
	blob: Instance;
	unknown: unknown;

	map: Map<Instance, boolean>;
	set: Set<{ type: "string"; value: string } | { type: "number"; value: number }>;

	packed: DataType.Packed<PackedObject>;
}

// This interface will take up a byte and 8-10 bits.
// 1 byte for the i8, 8 bits (1 byte) for 8 booleans, and 1-2 bits for the optional field.
// Without packing, this interface would take up to 11 bytes.
//
// Packing also optimizes `optional` values by using a single bit for the presence of the value.
// Packing recursively applies to the entire object, including things like arrays, other objects, etc.
//
// Packing additionally optimizes CFrames by optimizing out axis-aligned orientation or default positions.
// This does add an extra bit of overhead per CFrame, but due to the size of an unoptimized CFrame, this is insignificant.
//
// Packing also optimizes discriminated and literal unions with two constituents by using a single bit for the discriminator.
interface PackedObject {
	num: DataType.i8;
	a: boolean;
	b: boolean;
	c: boolean;
	d: boolean;
	e: boolean;
	f: boolean;
	g: boolean;
	h: boolean;

	// This only takes up to 2 bits to encode in a packed type!
	i?: boolean;
}

const testData: Data = {
	f64: 1552983.573,
	f32: 1552983.573,

	u8: 175,
	u16: 5892,
	u32: 850928,

	i8: 175,
	i16: 5892,
	i32: 850928,

	vector: new Vector3(4, 2, 0),
	cframe: new CFrame(6, 6, 6).mul(CFrame.fromOrientation(math.rad(15), math.rad(25), math.rad(35))),
	color3: new Color3(0.5258, 0.1919, 0.666),
	colorSequence: new ColorSequence([
		new ColorSequenceKeypoint(0, new Color3(1, 0, 0)),
		new ColorSequenceKeypoint(0.5, new Color3(0, 1, 0)),
		new ColorSequenceKeypoint(1, new Color3(0, 0, 1)),
	]),
	numberSequence: new NumberSequence([
		new NumberSequenceKeypoint(0, 0.2),
		new NumberSequenceKeypoint(0.5, 1.75),
		new NumberSequenceKeypoint(1, 250),
	]),
	enum: Enum.KeyCode.W,

	boolean: true,
	string: "hello i am a string!",
	array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],

	union: { type: "string", value: "hey, I am a string!" },
	unionOfStrings: "a very large string that will not exist in the serialized output!",
	unionOfPrimitives: true,

	tuple: ["tuple!", 15, true],
	tupleWithRest: ["tuple!!", 25, false, "various", "strings", "go", "here !"],

	blob: game.GetService("Workspace").Terrain,
	unknown: ["hey i can be any value, and I will serialize correctly!"],

	map: new Map(),
	set: new Set(),

	packed: {
		num: 35,
		a: true,
		b: false,
		c: true,
		d: false,
		e: true,
		f: false,
		g: true,
		h: false,
	},
};

testData.map.set(testData.blob, true);
testData.set.add({ type: "string", value: "yo!" });
testData.set.add({ type: "number", value: 69420 });

const serializer = createBinarySerializer<Data>();

const serialized = serializer.serialize(testData);
print("serializing", "blob:", serialized.blobs);
print(buffer.tostring(serialized.buffer));

print("deserialized", serializer.deserialize(serialized.buffer, serialized.blobs));
print("original value", testData);