-
Notifications
You must be signed in to change notification settings - Fork 137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implements encoding from Parser #73
base: master
Are you sure you want to change the base?
Conversation
…Buffer unsafeAlloc
The Implementation now uses smart-buffer for the encoding feature. So, it is no more needed to estimate the size of the encoded result before using I have also added additional parameters to the |
If people want to try it. I have published this work as a new npm packge binary-parser-encoder. |
This helped me so much, thank you! |
@Ericbla thank you for your work! You've done a very useful thing. I've tested your encoder and in most cases it works great. But there is one case it throws a TypeError. Because your repo https://github.com/Ericbla/binary-parser has no Issues ability, so I'm putting my bug report here. Consider the following example: const util = require('util')
const Parser = require("binary-parser-encoder").Parser;
var packet = new Parser()
.uint16("len")
.array("payloads", {
type: new Parser().uint8("cmd").array('params',{
type: new Parser().uint8('param'),
readUntil: function(item,buffer) {
return buffer.length == 2;
}
}),
lengthInBytes: function() {
return this.len - 4;
},
})
.uint16("crc");
var test = Buffer.from("0008AAB1B2B3FFFF", "hex");
console.log(util.inspect(packet.parse(test),false,null));
console.log(util.inspect(buf = packet.encode(packet.parse(test)),false,null)); When I parse my packet, I've got the following structure: {
len: 8,
payloads: [{
cmd: 170,
params: [{
param: 177
}, {
param: 178
}, {
param: 179
}]
}],
crc: 65535
} But when I trying to encode it using the same parser, I get the following error:
It seems the problem with an unusial way of using readUntil function: readUntil: function(item,buffer) {
return buffer.length == 2;
} If I change the code above to: length: 3 your encoder will work fine. So I'm interesting is there a bug with encoder, or threr is something wrong with my code? |
@ab-kily Thanks for reporting this issue. (I have now activated the Issue management on my forked repo). To fix this, I have to ignore the I guess I will choose this solution (as for the B.R.Eric |
Just published the version 1.4.0 of binary-parser-encoder package on npm registry. Should fix the problem of @ab-kily. Finally, I kept the
|
Any update? |
Would love to use this feature, but I am using the fork for now. Is there anything I can do to help? |
@Ericbla : thank you SO much for making this extension! I was dreading having to make an encoder myself, until I saw that you've done this for the community! 👍 I do feel like it's an extremely common use case where if you're decoding binary, you'd also like to re-encode it back. |
What's the word on this PR? |
My initial intention was to focus on deserialization only but I'm willing to merge this PR if many people can benefit. If someone could translate this PR to TypeScript and rebase it onto the current master, it would very much appreciated. |
@Ericbla If you want I can do it, please let me know 😀 |
Hello everyone, @Ericbla are you still willing to work on this? If @keichi is still willing to merge it, I can do the updating to get them on-par. My company has been using the encoder-branch for a long time, and we even did some customizations for it, but I wanted to improve the work a bit, and it makes absolutely no sense to work on it when it hasn't been updated for the latest version. Thanks in advance. |
Hi @Yuri-M-Dias and @keichi, As you guessed, I'm no more able to actively maintain the endoder part of the parser. Since I'm no more using this library, and also no more involved in any JavaScript/TS project (back to legacy C++ projects !), I would prefer if someone else could continue the adventure. It should not be too difficult form the current state of my repos (https://github.com/Ericbla/binary-parser) to merge the last modifications from @keichi parser and generate a new pull-request for the original @keichi project. Best regards. --
|
For those who are interested, I created a new PR (#243) bringing this one to the latest version of binary-parser. |
Hello,
Here is an implementation of encode() function for this great declaratibe binary-parser.
This method is intended to be the opposite of the
parse(buffer)
method. It relies on the unchanged declaration of the Parser and then just is able to generate a buffer from a provided objectencode(object)
->Buffer
Almost all types supported by parse are also supported by encode function. The exception is for associative arrays (array type with
key
option) as there is no mean to determine keys order.Only some options have not the same effect on parse and encode.
For example the
formatter
function has a newencoder
function counter part.And
little endian
is not taken into account for bit{n}.I have duplicated almost all unit tests of parsing to use with encoding and mainly try to re-encode what have been parsed.
The implementation currently use a fixed size Buffer for encoding (The size: 256 bytes can be adjusted with an option in the Parser constructor and Parser.start()).
It would be a good idea to replace it in the future with a dynamic Buffer implementation (that can adjust its size during encoding).
I hope you will adopt this new feature as it is seems (at least for me) a very common usage to have both parsing and encoding from a single declarative spec. (e.g. when handling protocols).
Regards.
--
Eric