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

kserial

v1.0.5

Published

Library for serializing to binary.

Downloads

18

Readme

kserial

This is a lightweight typescript library for serializing and deserializing binary data.

kserialc is the companion project that generates serializable classes from a schema.

Installation

npm install kserial

Usage

import * as kserial from 'kserial';
import {MyMsg, MyEnum} from './my_generated_messages';

// You can initialize a whole message in one go.
const outgoingMsg = new MyMsg({
  someEnum: MyEnum.SOME_VALUE,
  someInt: 32, 
  someString: 'hello',
});

// Or you can edit messages in place, including submessages.
outgoingMsg.someMyMsg = new MyMsg({someString: 'a submessage'});

// Also encapsulate submessages without a fixed type.
outgoingMsg.someAnyMsg = kserial.KSerializableAny(MyMsg.id, new MyMsg({someString: 'goodbye'}));

// Serializing occurs on demand; a Uint8Array is allocated of the correct size
// for the message.
const serializedBinary = kserial.serialize(x);

// You can also choose to serialize into an existing buffer.
outgoingMsg.serialize(existingUint8Array);

// And measure.
console.log(outgoingMsg.serializeLength);

// And deserializing is just as easy.
const incomingBinary = new Uint8List(someDataFromAFileOrSomething);
const incomingMsg = MyMsg.deserialize(incomingBinary);
console.log(incomingMsg.someString);

// Deserialize will throw a TypeError if an 'any' field with an unrecognized
// type is received, and may throw other errors if bad data (e.g. with
// offsets pointing to outside the message length) is received. For safety,
// deserialize should be wrapped in try/catch if the source data is untrusted.

// 'any' fields deserialize automatically if you have the required message
// type set up.
switch (incomingMsg.someAnyMsg.id) {
  case MyMsg.id:
    const msg = incomignMsg.someAnyMsg.value as MyMsg;
    console.log(msg.someString);
  default:
    console.log(`received some other type: ${incomingMsg.someAnyMsg.id}`);
}

Why?

Flatbuffers changed interface forcing me to redo stuff, and the interface was generally frustrating to use. Protobuffers also has an awful interface for Javascript and Typescript. JSON with mixed data types plays pretty poorly with type-safety, and tends to bloat the data with field names. I wanted something I can rely on to not change interface, and with minimal stuff to install.

Striking a balance between a user-friendly interface and decent performance is a little bit tricky. Compromises I made were:

  • Serializing serializes strings twice, once into a temporary buffer to measure the utf8 size in bytes, then a second time during actual serialization.
  • Serializing and deserializing is on-demand; the 'live' message is made of objects. This makes it easier to work with than flatbuffers' builder pattern, but if you're creating a message solely to send, it's a little less performant.
  • The message structure is a fixed shape like a struct.
    • This is not cross-version compatible like protobuffers - all clients must be using the same version or they'll get unreadable data.
    • This is oversized for sparse messages with many possible fields.
    • This is fast to serialize and deserialize, and smaller for heavily populated messages.
    • The format is relatively simple.