All Projects → majimboo → c-struct

majimboo / c-struct

Licence: MIT license
a binary data packing & unpacking library for node.js

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to c-struct

RingBuffer
模仿 kfifo 实现的环形缓冲区
Stars: ✭ 64 (+52.38%)
Mutual labels:  buffer
zlib
Pure javascript implementation of Zlib nodejs core module.The zlib module provides compression functionality implemented using Gzip and Deflate/Inflate.
Stars: ✭ 14 (-66.67%)
Mutual labels:  buffer
JABS.nvim
Just Another Buffer Switcher for Neovim
Stars: ✭ 150 (+257.14%)
Mutual labels:  buffer
Pulse
❤️ A heart rate camera pulse detector written in Swift.
Stars: ✭ 53 (+26.19%)
Mutual labels:  buffer
vivid.ex
Vivid is a simple 2D rendering library written in Elixir.
Stars: ✭ 27 (-35.71%)
Mutual labels:  buffer
as-string-sink
An efficient dynamically sized string buffer (aka String Builder) for AssemblyScript
Stars: ✭ 23 (-45.24%)
Mutual labels:  buffer
WebServer
High-performance multi-threaded tcp network server in c++11
Stars: ✭ 58 (+38.1%)
Mutual labels:  buffer
buffertools-php
Toolbox for working with binary and hex data. Similar to NodeJS Buffer.
Stars: ✭ 60 (+42.86%)
Mutual labels:  buffer
node-pg-large-object
Large object support for PostgreSQL clients using the node-postgres library.
Stars: ✭ 31 (-26.19%)
Mutual labels:  buffer
sirdez
Glorious Binary Serialization and Deserialization for TypeScript.
Stars: ✭ 20 (-52.38%)
Mutual labels:  buffer
go-disk-buffer
This package helps to work with huge amount of data, which cannot be stored in RAM
Stars: ✭ 39 (-7.14%)
Mutual labels:  buffer
bipbuffer
a C implementation of Simon Cooke's bipbuffer
Stars: ✭ 15 (-64.29%)
Mutual labels:  buffer
typedarray-to-buffer
Convert a typed array to a Buffer without a copy.
Stars: ✭ 64 (+52.38%)
Mutual labels:  buffer
biguint-format
Node.js module to format big uint numbers from a byte array or a Buffer
Stars: ✭ 16 (-61.9%)
Mutual labels:  buffer
CuVec
Unifying Python/C++/CUDA memory: Python buffered array ↔️ `std::vector` ↔️ CUDA managed memory
Stars: ✭ 73 (+73.81%)
Mutual labels:  buffer
ember-validated-form-buffer
A validated form buffer that wraps Ember Data models for use in forms.
Stars: ✭ 46 (+9.52%)
Mutual labels:  buffer
to-ico
Convert PNG to ICO in memory
Stars: ✭ 115 (+173.81%)
Mutual labels:  buffer
sharon
A lightweight and modular social sharing library
Stars: ✭ 16 (-61.9%)
Mutual labels:  buffer
EmbeddedRingBuffer
A ring buffer designed to work with embedded devices, does not use heap allocations.
Stars: ✭ 47 (+11.9%)
Mutual labels:  buffer
RingBuffer
Classic ringbuffer with optional Stream interface
Stars: ✭ 53 (+26.19%)
Mutual labels:  buffer

c-struct Build Status

NPM

A fast binary data packing & unpacking library for node.js designed for multiplayer games.

What can it do?

  • 8, 16, 24, 32, 40, 48, (56 and 64*) bit unsigned integers.
  • String with length and null-terminated cstrings.
  • Boolean, nibble, float and double.
  • Big and little endianness.
  • Schema based packing and unpacking.
  • Unpack from buffer to object.
  • Pack from object to buffer.
  • Pack with default value if key isn't specified.

More

  • Available via npm.
  • Zero production dependencies.

Installation

npm install c-struct --save

Execute $ node examples/ to see the examples.

Usage

Unpacking

var _ = require('c-struct');

var playerSchema = new _.Schema({
  id: _.type.uint16,
  name: _.type.string(16),
  hp: _.type.uint24,
  exp: _.type.uint32,
  status: _.type.uint8,
  motd: _.type.string(), // null-terminated if no length
  motw: _.type.string(), // cstring if no length
  skills: [{
    id: _.type.uint8,
    name: _.type.string(32),
    secret: _.type.uint40
  }],
  position: {
    x: _.type.uint16,
    y: _.type.uint16
  },
  hash: _.type.uint48
});

// register to cache
_.register('Player', playerSchema);

// object to buffer | this can be on another file
var buf = _.packSync('Player', {
  id: 1,
  name: 'Foobar',
  hp: 1000,
  exp: 88888888,
  status: 100,
  skills: [{
    id: 1,
    name: 'traps of thunder',
    secret: 5151515151
  }, {
    id: 2,
  }, {
    name: 'fatal blow'
  }, {
    id: 3,
    name: 'galvano strike'
  }],
  position: {
    x: 102,
    y: 351
  },
  motd: 'welcome',
  motw: 'weekly',
  hash: 99999999,
});

Packing

var _ = require('c-struct');

var playerSchema = new _.Schema({
  id: _.type.uint16,
  name: _.type.string(16),
  hp: _.type.uint24,
  exp: _.type.uint32,
  status: _.type.uint8,
  motd: _.type.string(), // null-terminated if no length
  motw: _.type.string(), // cstring if no length
  skills: [{
    id: _.type.uint8,
    name: _.type.string(32),
    secret: _.type.uint40
  }],
  position: {
    x: _.type.uint16,
    y: _.type.uint16
  },
  hash: _.type.uint48
});

// register to cache
_.register('Player', playerSchema);

// buffer to object | this can be on another file
var obj = _.unpackSync('Player', BUFFER_HERE);

API

Currently only unsigned values in little endian are supported

_.type.uint8    // unsigned char
_.type.uint16   // unsigned short
_.type.uint24
_.type.uint32   // unsigned long
_.type.uint40
_.type.uint48
_.type.uint56
_.type.uint64

You can also specify default values

_.type.u8(9999) // default value 9999
_.type.u16(999) // default value 999
_.type.u24(888) // default value 888
_.type.u32(777) // default value 777
_.type.u40(666) // default value 666
_.type.u48(555) // default value 555
_.type.u56(444) // default value 444
_.type.u64(333) // default value 333

Configurations

There is currently no configs.

TODO

  • add configurable endianness
  • add async methods
  • add signed
  • add boolean and nibble
  • add float and double
  • add benchmark
  • documentation
  • more testing
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].