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

parametrized-message

v0.0.2

Published

Parametrized messages are binary structures that provide unified way of handling known and unknown data at the cost of adding prefixes to each `message` and `parameter`.

Downloads

1

Readme

parametrized-message

Parametrized messages are binary structures that provide unified way of handling known and unknown data at the cost of adding prefixes to each message and parameter.

Structure of messages

Message is a structure for storing parameters.

|name|description| |-|-| |message-size |number of bytes message consists of (including this field)| |message-id |if of the message| |parameters |array of parameters|

Structure of parameters

|name|description| |-|-| |data-size |number of bytes in data filed| |parameter-id |id of the parameter| |data |data bytes|

Documentation

Work in progress...

Message

This class...

Methods

  • constructor(buffer, args)
  • getParameter(id)
  • getParameters(id)

Example

const Message = require('parametrized-message');

Message.Parameter

This class...

Methods

Example

const Message = require('parametrized-message');

Message.Builder

This class...

Methods

  • constructor(id, args)
  • generateBytes()
  • addParameter(id, buffer)
  • addInt8(id, val)
  • addUInt8(id, val)
  • addInt16LE(id, val)
  • addInt16BE(id, val)
  • addUInt16LE(id, val)
  • addUInt16BE(id, val)
  • addInt32LE(id, val)
  • addInt32BE(id, val)
  • addUInt32LE(id, val)
  • addUInt32BE(id, val)
  • addInt64LE(id, val)
  • addInt64BE(id, val)
  • addUInt64LE(id, val)
  • addUInt64BE(id, val)
  • addFloatLE(id, val)
  • addFloatBE(id, val)
  • addDoubleLE(id, val)
  • addDoubleBE(id, val)

Example

const Message = require('parametrized-message');

Message.Defaults

This object is used to determine structure of messages and parameters, that is:

  • type of message size field,
  • type of message id field,
  • type of parameter data size field,
  • type of parameter id field.

All are by default set to Message.Types.Int32LE.

When no object describing structure is passed to constructors of Message, Message.Parameter and Message.Builder classes, values from Message.Defaults are used.

Available types are defined in Message.Types object.

Methods

  • update(defaults)

  • reset()

Example

const Message = require('parametrized-message');

const defaults = {
  messageSizeType   : Message.Types.Int8,
  messageIdType     : Message.Types.Int16LE,
  parameterSizeType : Message.Types.Int32LE,
  parameterIdType   : Message.Types.Int64LE,
};

Message.Defaults.update(defaults);

const messageId = 1;

const builder1 = new Message.Builder(messageId);
const builder2 = new Message.Builder(messageId, defaults);

Message.Types

This object stores objects containing properties describing size of the type and how to read and write values to the buffer object.

Members

  • Message.Types.Int8
  • Message.Types.Int16LE
  • Message.Types.Int16BE
  • Message.Types.Int32LE
  • Message.Types.Int32BE
  • Message.Types.Int64LE
  • Message.Types.Int64BE
  • Message.Types.UInt8
  • Message.Types.UInt16LE
  • Message.Types.UInt16BE
  • Message.Types.UInt32LE
  • Message.Types.UInt32BE
  • Message.Types.UInt64LE
  • Message.Types.UInt64BE

Custom types

To define custom type, an object with following structure must be created:

const MyType = {
	size: 12,
	read: (buffer, pos) => { return 0; }
	write: (buffer, val, pos) => {}
}
  • size - number of bytes type takes
  • read - method accepting an Buffer object (buffer) and buffer offset (pos), it needs to read data from the buffer and return numeric value
  • write - method accepting an Buffer object (buffer), numeric value (val) and buffer offset (pos), it needs to write given value to the buffer at given offset

Example message

By default types all of the following fileds: message-size, message-id, data-size, parameter-id are int32_t. This can be changed using defaults or by passing desired properties to constructors of objects.

Example structure of message with id = 1 containing 2 parameters: { id: 1, data: 1.234f } and { id: 2, data: 4000000000u }:

|byte #|type|name|value| |-|-|-|-| |0|int32_t|message-size|32| |4|int32_t|message-id|1| |8|int32_t|data-size|4| |12|int32_t|parameter-id|1| |16|float|data|1.234f| |20|int32_t|data-size|4| |24|int32_t|parameter-id|2| |28|uint32_t|data|4000000000|

Example code

const Message = require('parametrized-message');

// update default properties so they would not need to be passed as argument
// to every instance of Message, Parameter or MessageBuilder object
Message.Defaults.update({
  messageSizeType   : Message.Types.Int32LE,
  messageIdType     : Message.Types.Int32LE,
  parameterSizeType : Message.Types.Int32LE,
  parameterIdType   : Message.Types.Int32LE,
});

// use MessageBuilder to generate message bytes
const builder = new Message.Builder(1); // message-id = 1
builder.addInt32LE(0, 123); // parameter-id = 0, data = 123
builder.addInt32LE(0, 456);
builder.addInt32LE(1, 1);
builder.addInt32LE(2, 2);
const messageBytes = builder.generateBytes();

// extract the parameters from message bytes
const message = new Message(messageBytes);

// get all parameters
console.log('all:');
for (var param of message.getParameters()) {
  console.log(param.id + ' = ' + param.readInt32LE());
}

// get only parameters with id 0
console.log('zeroes:');
for (var param of message.getParameters(0)) {
  console.log(param.id + ' = ' + param.readInt32LE());
}

Tests

npm test