https://github.com/majimboo/c-struct
a binary data packing & unpacking library for node.js
https://github.com/majimboo/c-struct
buffer javascript unpacking-library
Last synced: about 2 months ago
JSON representation
a binary data packing & unpacking library for node.js
- Host: GitHub
- URL: https://github.com/majimboo/c-struct
- Owner: majimboo
- License: mit
- Created: 2014-07-26T19:16:06.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2021-02-06T06:05:45.000Z (over 4 years ago)
- Last Synced: 2025-03-27T10:46:20.916Z (2 months ago)
- Topics: buffer, javascript, unpacking-library
- Language: JavaScript
- Size: 22.5 KB
- Stars: 44
- Watchers: 3
- Forks: 11
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
c-struct [](https://travis-ci.org/majimboo/c-struct)
========[](https://nodei.co/npm/c-struct/)
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](https://npmjs.org/package/c-struct).
* 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.uint64You 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 333Configurations
----> 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