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

ezstruct

v0.0.3

Published

Easiest way to pack/unpack C struct data with three steps in Node.js

Downloads

11

Readme

ezStruct for Node.js

Easiest way to pack/unpack struct data with three steps in Node.js.

  1. Provide C-style struct declarations (intact C source code) and get the context
  2. Fetch the struct Schema from the context
  3. Pack / Unpack data.

What's Supported

  • typedef (also works for struct, enum and array)
  • (multi-dimension) array
  • (nested) struct
  • enum
  • anonymous enum/struct
  • basic number types (little-endian {8,16,32}-bit integer, 32-bit float and double)
  • char something[64] as string

Quickstart

npm version

Install via: npm install --save ezstruct

const ezstruct = require('ezstruct')
ezstruct.setStringEncoding('utf-8')
// install iconv-lite to handle more encodings

// create context
// ES6 template tag is supported.

var context = ezstruct `
typedef uint32_t uid_t;

typedef struct {
  uid_t id;
  int age;
  char name[32];
} student;
`

// then you can use `context.student` the schema

// (optional) modify schema. useful for char[]

context.student.name.default = "Unknown"
context.student.name.encoding = "utf-8"

// JS to binary

var raw = context.student.toBinary({
  id: 12345678,
  age: 19,
})

// binary to JS

var unpacked = context.student.fromBinary(raw)

Document

Context

Context is an object containing all data type declarations and handlers(parser/writer). By default, it contains int{8,16,32}, char[], bool and some common types.

Call ezstruct = require('ezstruct') and get the context builder function.

ezstruct(source[, context])

  • source: string C-style declarations. You may get this from C header files.
  • context: Context optional. The new Context can inherit types from another Context.
  • (returns) Context

Once you get the Context, you may get data-type Schemas like ctx["struct tm"], or ctx.timer_t if it's declared.

ezstruct.setStringEncoding(encoding)

  • encoding: string|null
  • (remarks)
    1. Call this before creating an Context, then all new char[] fields of structs will be affected.
    2. Also works for multi-dimension char array.
    3. This method will not affect Contexts that are created before.
    4. Install iconv-lite package if you want to handle Encodings that Not Supported by NodeJS
    5. iconv-lite is not in dependency list. Run npm i --save iconv-lite before using.

Schema

Every schema has these properties:

  • bytes: number the length of one instance

And these methods:

read(buf, offset)

  • buf: Buffer where ezStruct parses data from
  • offset: number the offset (in bytes) of data
  • (returns) data it read

write(buf, offset, value)

  • buf: Buffer where data shall be written to
  • offset: number the offset (in bytes) of data
  • value your data

Struct (Schema)

Struct comes from struct declarations. Plus, it provides these properties:

  • struct: StructItem[] containing the sort of items.
  • fields: {your_var_name: StructItem} the dict version of struct.
  • your_var_name: StructItem same as fields.your_var_names. if not conflicted.

methods:

fromBinary(buf[, offset])

  • buf: Buffer where ezStruct parses data from
  • offset: number the offset in bytes. 0 by default.

toBinary(obj)

  • obj: object the thing to be packed
  • (returns) Buffer

toBinary(obj, buf, offset)

  • obj: object the thing to be packed
  • buf: Buffer which stores the binary
  • offset: number

StructItem (sub-class from Struct)

Properties:

  • name: string
  • type: string like "int" or "char[128]"
  • default: any (optional) default value
  • encoding: string (optional) for char strings only. see ezstruct.setStringEncoding

Enum (Schema)

It is an alias of int, with extra properties:

  • dict: {enum_item_name: number} a dict object
  • enum_item_name: number same as dict.enum_item_name. if not conflicted.

TO-DO

  • union
  • big-endian
  • set char encoding with comments