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

fbicodec_ts

v0.1.1

Published

Forward-Backward-Interfaced general purpose invertible codec library

Downloads

2

Readme

FBIcodec

FBIcodec is so cleanly invertible, it's the last thing the FBI would investigate!

This is a typescript library for building bidirectional (encoding and decoding) codecs using composable steps. The "FBI" part in the name stands for "Forward-Backward-Interfaced", and Codec stands for an invertible data transformation.

The main building block of the library is its general purpose Step interface, which ensures the ability for a forward transformation to be reversed. The forward and backward methods take a single argument, allowing Steps to be composed into more complex Codecs.

Features

  • Composable steps for encoding and decoding binary data
  • Steps for primitive types like numbers, strings, bytes
  • Steps for composing records, arrays, sequences
  • Invertible - encoding and decoding are symmetric
  • Type safe

Overview

The main abstractions are:

  • Step - encodes forward and decodes backward between a source and target type.
  • Binarystep - a Step for encoding/decoding binary data.

To work with binary data of primitive types, such as: numbers, strings, bytes etc..., you can use the the BinaryPureSteps inside the file binary_primitive_steps.ts.

  • BinaryNumberStep - supports signed/unsigned integers of 8, 16, 32 and 64 bits, and floats of 32 and 64 bits, in both little-endian and big-endian formats.
  • BinaryStringStep - for strings with varying lengths (based on the args.length parameter).
  • BinaryCStringStep - for NUL ("\x00") terminated strings.
  • BinaryBytesStep - for sequence of bytes (based on the args.length parameter).
  • BinaryNumberArrayStep - for numeric array of numbers (based on the args.length parameter).

To work with binary data of composite types, such as: records, arrays, sequences etc..., you can use the the BinaryPureSteps inside the file binary_composition_steps.

  • BinaryRecordStep - encodes a record with named fields, when provided with a sequence of Steps for each field.
  • BinaryArrayStep - encodes an array of items, when provided with a single Step for the item type.
  • SequentialSteps - chains multiple Steps.

For example, here is a step that decode and then repack PNG image file's metadata blocks:

class PNG_Codec_Step extends PureStep<Uint8Array, GeneralChunk_schema[]> {
	forward(input: Uint8Array): GeneralChunk_schema[] {
		// parse input into chunks
	}
	backward(input: GeneralChunk_schema[]): Uint8Array {
		// encode chunks into binary
	}
}

Theory

The key components that enable building robust bidirectional transformations are:

  • Step<FROM, TO, LOST>

    • Defines symmetric forward and backward methods between source FROM and target TO types. Any lost information should be stored in the LOST private property.
  • PureStep<FROM, TO> extends Step<FROM, TO, never>

    • This is a Step that is guaranteed to not lose information during forward transformation, allowing backward transformation to fully reconstruct the original value. It also means that one may reuse a single instance of this kind of step for multiple forward and backward transformations, in whatever order it is deemed convenient. Hence, these kind of steps are suitable for being applied repeatdly by larger compositional steps.
  • BinaryStep<OUT, ARGS, LOST> extends Step<{ bin: Uint8Array, pos: number, args: ARGS }, { val: OUT, len: number }, LOST>

    • A step that specialized for parsing binary data. backward encodes the wrapped OUT value back into a wrapped Uint8Array object.
  • BinaryPureStep<OUT, ARGS> extends PureStep<{ bin: Uint8Array, pos: number, args: ARGS }>

    • Similar to BinaryStep, but is guaranteed not to lose data during forward transformation. This is the class that is basically used by all implementations of BinarySteps.

These simple interfaces allow Steps to be composed in a type-safe way.

Moreover, the compositions form a tree structure that preserves invertibility - as long as each Step inside implements their forward and backward methods correctly, the entire composition will be robustly reversible.

It also makes diagnosing issues easier - if there is a decoding error, it must be within one of the composed Steps. The composition tree narrows down where the issue lies.