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

static-protocol

v1.0.5

Published

Utility for serializing and deserializing data efficiently, based on a schema definition.

Downloads

9

Readme

StaticProtocol

Overview

StaticProtocol is a small utility for Typescript/Javascript that aims to be a fast and efficient tool for serializing and deserializing data based on a schema definition. It can be used for standalone endpoints as well as whole protocols.

Features

  • Works in Node.js and the browser
  • Simple schema definition
  • Fully typed encoding and decoding
  • Optional validation of data during decoding
  • Zero build steps required
  • Code generated at runtime for fast performance
  • Packed bools

Installation

npm install static-protocol

Usage

A comprehensive overview of the usage can be found in the examples folder. You can run the demo as follows:

npm run demo

Endpoint Schema

A schema for a single endpoint can look something like this:

const endpointSchema = {
    data: {
        name: {
            type: 'varchar',
            /**
             * Test function - If it fails the decode will return null
             */
            test: (value: string) => /[a-zA-Z]/.test(value)
        },
        age: Enum({ 
            /* [case: number | string]: type */
            0: 'uint8',
            1: 'char:3',
        }),
        ageVerified: 'bool',
        userId: 'uint16',
        tags: List('varchar'),
    },
    allocateNew: true,
    validate: false,
    channel: 1
}

/* Create enpoint using the schema */
const endpoint = StaticEndpoint(endpointSchema)

data

The data property defines the structure of the data to be encoded and decoded. Fields can be specified to be on of the available datatypes a nested schema, an array or an enum.

For defining enums the Enum wrapper function is used. The ids for enums can be either numbers or strings. But performance will be better if they are numbers. The values can be defined like data but nested enums are not supported.

For defining Arrays the List wrapper function is used. The item type can be defined like data.

channel

The channel property defines the channel to be used for the endpoint. If the endpoint is used within a protocol and the channel is not defined, the channel will be generated automatically, otherwise no channel will be used. The value must be an integer between 0 and 255.

allocateNew

The allocateNew property defines if the endpoint should allocate a new buffer each time data is encoded. This is useful if you want to modify the data after it has been encoded.

validate

If validate is set to false the validation functions will be skipped during decoding.

Protocol Schema

A schema for a whole protocol can look something like this:

const protocolSchema = {
    user: endpointSchema,
    note: {
        data: {
            text: 'varchar:5000'
        }
    }
}

/* Create protocol using the schema */
const protocol = StaticProtocol(protocolSchema)

The definition is simply a record of endpoint names and definitions.

The second argument to StaticProtocol is optional and indicates if the protocol is raw. Raw protocols dont require a channel for each endpoint.

Datatypes

| Datatype | Type in Javascript | Description | | ---------------- | ------------------ | -------------------------- | | uintX / intX | number | Simple integer types | | bool | boolean | Simple boolean type | | char | string | Fixed length string | | varchar | string | Variable length string | | buf | Uint8Array | Fixed length buffer | | varbuf | Uint8Array | Variable length buffer |

To-Do

  • Add support for varints
  • Add a way to validate fields with respect to values ​​of other fields.

Contributing

I appriciate any contributions! Just fork and submit a pull request. 😊