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

cpp-struct-js

v1.0.2

Published

A struct library for reading and writing binary data structures that interface with C/C++ code

Downloads

25

Readme

cpp-struct-js

The intention of this module is to interace node.js with an Arduino software I am writing. For this I need to read binary structures that are generated by C++ code running on an Arduino system.

Supported types:

  • uint8,16,32
  • int8,16,32
  • float
  • strings

Only one dimensional arrays are supported. It might be possible to extend the code to support this, but I don't see a need for this right now.

Pointers are not supported, but that wouldn't make sense anyway since serialization can't deal with native memory adresses.

Example


struct = require("cpp-struct")

var player = new struct("Player", [
	"name", struct.char(12),
	"id", struct.uint32_t()
]);

var record = new struct("Record", [
	"playerIndex", struct.uint8_t(),
	"typeId", struct.uint8_t(),
	"record", struct.uint16_t(31)
]);

var EEPROMData = new struct("EEPROMData", [
	"gameName", struct.char(14),
	"version", struct.uint16_t(),
	"players", struct.type(player,32),
	"records", struct.type(record,32)
]);

var buffer = new Buffer(EEPROMData.size());
EEPROMData.encode(buffer,0, {
	gameName: "SuperTesting!!!",
	version: 1,
	players:[
		{name:"Hello",id:1},
		{id:2},
		{name:"HUHU"}
	]
},{endian:"LE"})

Reference

Class: struct (var struct = require('cpp-struct'))

Constructor signature: name, schema, [count, [bytes]]

  • name: Internal name that's used when exporting and referencing other structs.
  • schema: Interleaved array where even elements are variable name identifiers and odd elements are struct instances
  • count (optional): If the type is supposed to be an array, this is the number of elements in the array
  • bytes (optional): Size of a single element in bytes (total size = count * bytes)

Note: count and bytes arguments are used usually only internally - you don't have to worry about these if you don't intend to add new native types next to the currently implemented ones such as int/uint/float/double - say you'd want a int24, you'll need to care about these arguments.

Instance member: struct.setEncoder

Function signature: func

  • func (buffer,pos,data,opt): Function that's called instead of the default encoder -- buffer: Buffer object to write to -- pos: position to write to -- data: the data value to be written (can be undefined) -- opt: optional options object that specifies encoding details (such as endianess)

Instance member: struct.setDecoder

Why not using one of the other modules?

I want to create schemas in node.js and use those for reading and writing binary buffers. Next to that I also want to output the header files for CPP which I can simply compile and use in my project.

The schema defines exact sizes, not the data - which may be incomplete and is to be filled with default data (zeros).

The other modules I looked at either focused on packing data as binary efficient or packed data directly without schema.

The closest match I could find was buffer-layout.