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

cppmsg

v1.2.1

Published

C++ stuct encoding/decoding with support for (dynamic) arrays.

Downloads

3

Readme

Binary data structure transformation for JavaScript

Installation

Using npm:

$ npm install cppmsg

To run the tests:

$ node test.js

Description

You can use this module, parse binary data from the c + +, can also be generate binary data(from json object) that c/c++ can phrase. This module provides follow function:

  • Encode json data to binary data
  • Decode binary data to json
  • support more data type: int8/16/32/64, uint8/16/32, float,double,bool,string
  • support msg nested;

You can phrase C++ binary data sturct from network to json. Note: c/c++ data struct must one bit algin.

cppMsg.msg constructor overloads

  • new cppMsg.msg() create empty cppMsg;
  • new cppMsg.msg( ds ) ds is data struct define Array
  • new cppMsg.msg( ds, data) ds is data struct define Array. data(optional) is init json data.
  • new cppMsg.msg( ds, null, opts) ds is data struct define Array. data(optional) is init json data.
    • opts, {useIconv: true } ,useIconv boolean, maybe use iconv-lite convert code

cppMsg.msg methods

  • encodeMsg( data ) : json data object;
  • encodeMsg2( data ) : json data object( use msg internal buffer, return internal buffer );
  • encodeMsgToBuff (data, buff, offset?) : encode to buffer
    • data, object;
    • buff, target buffer;
      • offset, data offset, default 0;
  • decodeMsg( buf, offset? ) : decode Buffer to json data object;
    • offset, data offset, default 0;

next methods using stream mode:

  • push_uint8
  • push_int8
  • push_uint16
  • push_int16
  • push_uint32
  • push_int32
  • push_string
  • push_char
  • encode( data ) : data is json data stream.

Examples

Normal Mode

Assume this for all examples below

C++ Code:

//C++ struct define Must one byte algin
struct head{
	int mainType;
    int subType;
};

struct msg{
	int reg;
    int chkCode;
    int iType;
    bool bMonitor;
    char workPath[10];
    unsigned int processID;
    struct head testObj;
    long long testin64;
	float floatArray3[3];
};

Nodejs code:

	var cppMsg = require('./cppMsg.js');

	var msg_def = {
		msgHead:[
					['mainType','int32'],
					['subType', 'int32']
				]
	};


	var msg = new cppMsg.msg(
		[
			['reg','int32'],
			['chkCode','int32'],
			['iType','int32'],
			['bMonitor', 'bool'],
			['workPath','string',10],
			['processID','uint32'],
			['testObj','object', msg_def.msgHead], // nested other
			['testint64','int64'],
			['floatArray3', 'float', , , 3]
		],null, {useIconv: false}
		);

	var buff = msg.encodeMsg2( {
			reg     : 2,
			chkCode : 0,
			iType   : 2,
			bMonitor : false,
			workPath : 'no 你 work',
			processID : 1234,
			testObj  :{
				mainType : 0x01020304,
				subType  : 0x0A0B0C0D
			},
			testint64 : 0xCDEF,
			floatArray3: [1.1, 2.2, 9.7]
		}  );

	console.log( buff );

	var data = msg.decodeMsg( buff );
	console.log( data );

stream mode

	msg.push_int32(2);  // reg
	msg.push_int32(0);  // chkCode
	msg.push_int32(2);  // iType

	msg.push_uint8(0);  // bMonitor
	msg.push_string('no worker path',10);
	msg.push_string('no worker path',20);
	msg.push_string('brnn-20',20);
	msg.push_uint32( 1234 ); // processID

	console.log( msg.encode());

Changelog

1.2.1

  1. Fixed encodeMsg2 using internal cache causing string dirty;
  2. optimize performance encodeMsg;
  3. add decodeMsgFromBuff

1.2.0

  1. add method encodeMsg2, Improve performance 1x
  2. optimize performance encodeMsg
  3. optimize performance decodeMsg, Improve performance 1x
  4. change msg construct add params opts
    • opts, {useIconv: true }
      • useIconv, boolean, true(default): use iconv-lite convert code.( false, Improve performance)
  5. Default string code,change to utf8

1.1.0

  1. Using ES6 syntax
  2. optimize performance encodeMsg

1.0.3

  1. fix int64 decode/encode error( Works only for numbers <= Number.MAX_SAFE_INTEGER ).
  2. fix object decode error.

1.0.2

  1. merge darnold79 change,add array support.

1.0.1

  1. string type add encode support(using iconv-lite).

1.0.0

  1. init.

LICENSE

The MIT License (MIT)

Copyright (c) 2017 Shudingbo Copyright (c) 2017 darnold79 (node-cppMsg-dynamic)