xdenser / node-struct Goto Github PK
View Code? Open in Web Editor NEWHandy work with buffers in NodeJS
Home Page: https://github.com/xdenser/node-struct/blob/master/README.md
License: MIT License
Handy work with buffers in NodeJS
Home Page: https://github.com/xdenser/node-struct/blob/master/README.md
License: MIT License
Hi,
currently I'm using your package in a Typescript project that I have inherited from a collegue. I really enjoy working with it, so far. What do you think about generating some Typescript declaration files?
I've never done it by hand so far, but Microsoft provides a generator that might be helpful dts-gen
(https://github.com/Microsoft/dts-gen).
Calling dts-gen -m struct
is not very helpful:
/** Declaration file generated by dts-gen */
export = struct;
declare function struct(...args: any[]): any;
declare namespace struct {
// Circular reference from struct
const Struct: any;
const prototype: {
};
}
The following code:
var Person = Struct()
.chars('firstName',10)
.chars('lastName',10)
.array('items',3,'chars',10)
.word16Sle('balance'),
People = Struct()
.word8('presentCount')
.array('list',2,Person);
Person.allocate();
People.allocate();
console.log(People.buffer());
console.log(Person.buffer());
should log two (pretty large) Buffers, however today it will log:
Buffer < 36 00 ....lots of data ...>
Buffer <>
With the second buffer always being empty.
This change: 9c46c8a#diff-168726dbe96b3ce427e7fedce31bb0bcL190 is a breaking change, and only the patch version was upped.
This should have been a major version change.
I am in a Chinese Company,in my project,I want exchange data from node.js to C++ Application.Can I Use this library to help me?
Below is code which is leading error ,
var Struct = require('struct');
/*************************/
var Person = Struct()
.chars('username',60)
.chars('id',100);
Person.allocate();
Person.set('username',"sravani");
Person.set('id',"123456889994");
console.log("username: "+ Person.get('username') +"id:" +Person.get('sid'));
/**************/
[root@sravani nodejs]# node struct.js
home/wsadmin/src/sravani/nodejs/node_modules/struct/index.js:398
throw new Error('Can not find field ' + key);
^
Error: Can not find field sid
at Struct.get (/home/wsadmin/src/sravani/nodejs/node_modules/struct/index.js:398:19)
at Object. (/home/wsadmin/src/sravani/nodejs/struct.js:11:65)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:423:7)
at startup (bootstrap_node.js:147:9)
Hey. What about if the structure is an array and its length is not defined? I see an option to use an array with a larger advance. Is it going to be?
I am using struct to write the header of binary files. If the struct template is Header, is it ok to do:
Header.allocate();
var data1=Header.buffer();
fill data1 with my settings, and then do again
Header.allocate();
var data2=Header.buffer();
to create file number 2?
Hello,
So I wanted to get an object (fields) from a buffer. I was wondering how do you pass already read field as the length of an array?
This is what I've tried but It doesn't work...
var PacketRecordsBodyOneBSegment = Struct()
.word8('number_of_io'); // length of the following array ...
var length = PacketRecordsBodyOneBSegment.fields.number_of_io;
console.log('OneB length:', length); // undefined
PacketRecordsBodyOneBSegment = PacketRecordsBodyOneBSegment
.array('events', length, PacketRecordsBodyOneBio);
Ideally, something like this would be awesome, but I don't know how to implement it:
var PacketRecordsBodyOneBSegment = Struct()
.word8('number_of_io'); // length of the following array ...
.array('events', 'number_of_io', PacketRecordsBodyOneBio);
Thanks.
There is an issue with node-struct in how the clone function is implemented, the closures array is copied using the assignment operator rather than the slice, so modifying the fields for the cloned struct results in modifying the original struct as well.
Original Code
this.clone = function () { var c = new Struct; var p = c._getPriv(); p.closures = priv.closures; return c; }
Modified With Fix
this.clone = function () { var c = new Struct; var p = c._getPriv(); p.closures = priv.closures.slice(0); return c; }
No Null-terminating string support?
Hi. I'm a little unsure if I'm using this correctly. My code:
var struct = require('struct').Struct().word64Ube('connection_id').word32Ube('action').word32Ube('transaction_id');
struct.allocate();
struct.set("connection_id", 0x41727101980);
struct.set("action", 0);
struct.set("transaction_id", 84548454);
var buf = struct.buffer();
and then passing the buffer on to a dgram client. Is this correct usage?
By looking at the API i dont see the reason why setting a buffer is prefixed with '_',i think that there should be a "public" method for this. Which is the reason for that decision?
Yesterday, #16 was merged and broke lots of modules using tilde or caret versions. Can you do a minor or major version bump rather than a patch version bump? The exported API has changed, so I would expect at least a minor version, if not a major. Should probably be 1.0.0
.
Using this hex value: 0x100000000000002 should print this integer to the console: 72057594037927938
What I am seeing is this: 72057594037927940
I have a few test cases, but this is the easiest to paste:
var idStruct = Struct().word64Ule('id');
idStruct.allocate();
idStruct.fields.id = 72057594037927938;
console.log( "id: " + idStruct.get('id') );
I'm trying to use this library with a C struct that contains a few time_t variables. Looking on my system they're 8 byte variables. However, when I read with time_t variable any sort of word64 variable in your library the data after this read gets corrupted (I'm assuming this means I'm reading too much or too little data from the buffer). I've also tried word32 and word16 variables with no luck (really just shooting in the dark at that point).
Do you have any suggestions for reading time_t variables? (I know it's a unique request)
Hi Denys
Instead of hardcoding 'ascii' as the encoding of chars(name,length), if you can make it as an optional param that will be great.
something like
chars(name, length, encoding)
-sanjeev
Usually array in buffer are defined with array size contained in a field stored above, that can be done to read non defined array length ?
There's no way to retrieve the offset where each field is located. The offset is just used implicitly when constructing the structure, but I think it would be nice if each field get an 'offset' entry to expose this information.
Probably, a 'length' and storing this in an array, to make it possible to retrieve the list in a static order would be nice to have.
I'm using a 64-bit Linux. My problem is that long
has 64 bits on my system while it has 32 bits on 32 bit machines. Is there a way to automatically detect on which system this lib is running and to automagically adjust the length of long
s?
PS: I want it to run cross platform. Therefore, just using word64Sle
is not sufficient.
Hello, I use struct 0.0.6, but found Struct.chars('hex') has a problem:
var Struct = require('struct').Struct;
var Test = Struct()
.chars("flag", 2, 'hex');
var buf = new Buffer(2);
var s = Test.clone();
buf[0] = 97;
buf[1] = 98;
console.log(buf.toString('hex', 0, 2));
// outputs "6162", it's right
s._setBuff(buf);
console.log(s.fields.flag);
// outputs "ab", but hex value "6162" expected
And Struct parse to buffer also have this problem:
var expected = new Buffer('aa', 'hex');
console.log(expected);
// outputs: <Buffer aa>
var Struct = require('struct').Struct;
var buf = new Buffer(1);
var ThisPacket = Struct()
.chars("cmd", 1, 'hex');
var s = ThisPacket.clone();
s._setBuff(buf);
var fields = s.fields;
fields.cmd = 'aa';
console.log(buf);
// outputs: <Buffer 61>
It seems Struct.chars() only support Node's default encoding utf8.
Some buffer streams are returned in an internal byte order and are thus reversed. Could offer an option to support this or new functions.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.