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

net-serializer

v6.1.0

Published

Type aware object serializer for sending structured data efficiently over network. Uses template object to describe which properties will be serialized into binary data buffer.

Downloads

10

Readme

NetSerializer

Type aware object serializer for sending structured data efficiently over network with WebRTC or WebSocket.

Uses template object to describe which properties will be serialized into binary data buffer. Property value can be either number, boolean or string.

Data types

Numbers can be serialized by using 1 to 4 bytes with following template types

 'int8' | 'uint8' | 'int16' | 'uint16' | 'int32' | 'uint32' | 'float32'

Boolean values uses one byte and string can use one to four bytes per character. Corresponding template types are

 'boolean' | 'string'

Template

Object being serialized must be described by template object. Structure of the template object reflects the structure of the object being serialized. Primitive values in the template object are represented with object that implements IMetaValue interface.

interface IMetaValue {
  type: MetaValueType
  multiplier?: number
  preventOverflow?: boolean
}
type MetaValueType = 'int8' | 'uint8' | 'int16' | 'uint16' | 'int32' | 'uint32' | 'float32' | 'boolean' | 'string'

Example of template object.

const template = {
  label: { type: 'string' },
  objects: [{
    identifier: {
      playerId: { type: 'uint8' },
      serial: { type: 'uint16' },
    },
    body: {
      position: {
        x: { type: 'int16' },
        y: { type: 'int16' },
      },
      velocity: {
        x: { type: 'int16', multiplier: 1000 },
        y: { type: 'int16', multiplier: 1000 },
      },
      angle: { type: 'uint16', multiplier: 65535 / (2 * Math.PI) }
    },
    visible: { type: 'boolean' }
  }]
}

Working example

With the above template the following data can be serialized. Note that template shape must overlap with the data shape, otherwise serialization will fail! [test/case2]

const data = {
  label: 'Väinämöinen',
  objects: [{
    identifier: { playerId: 1, serial: 1910 },
    body:
    {
      position: { x: 87.0156357205063, y: 96.13073426289853 },
      velocity: { x: 22.737924275167472, y: 28.14180572055013 },
      angle: 0.420235722176959
    },
    visible: false
  },
  {
    identifier: { playerId: 1, serial: 10215 },
    body:
    {
      position: { x: 937.3588667980719, y: 28.385851467368937 },
      velocity: { x: 11.643172722998244, y: 3.4230873467425926 },
      angle: 1.168204866279076
    },
    visible: true
  }]
}
import NetSerializer from 'net-serializer'
const arrayBuffer = NetSerializer.pack(data, template)

and deserialized object will look like

const expectedResult = {
  label: 'Väinämöinen',
  objects: [{
    identifier: { playerId: 1, serial: 1910 },
    body:
    {
      position: { x: 87, y: 96 },
      velocity: { x: 22.737, y: 28.141 },
      angle: 0.4202212741492046
    },
    visible: false
  },
  {
    identifier: { playerId: 1, serial: 10215 },
    body:
    {
      position: { x: 937, y: 28 },
      velocity: { x: 11.643, y: 3.423 },
      angle: 1.1681441944407733
    },
    visible: true
  }]
}

const deserializedData = NetSerializer.unpack(arrayBuffer, template)
JSON.stringify(deserializedData) === JSON.stringify(expectedResult)
// true

In this case serialized form of the data (47 bytes) uses space roughly ten times less than JSON stringified (459 bytes).

Advanced features

You can set custom text handler if TextEncoder and TextDecoder is not available in your platform (they are in browser and will be used automatically if custom text handler is not set).

const textHandler = {
  encode: function (input) {
    const buffer = new Uint8Array(input.length)
    for (let i = 0; i < input.length; i++) {
      buffer[i] = input.charCodeAt(i)
    }
    return buffer
  },
  decode: function (input) {
    const buffer = new Uint8Array(input)
    return String.fromCharCode(...buffer)
  }
}

NetSerializer.utils.setTextHandler(textHandler)

Check test folder to see more advanced features:

  • array unpack callback [case3]
  • array length type (uint8, uint16, uint32) [case3]
  • compressing multiple properties to one with pack and unpack callbacks [case3]
  • debugging pack with onErrorCallback function [case3]