ericbla / binary-parser Goto Github PK
View Code? Open in Web Editor NEWThis project forked from keichi/binary-parser
Blazing-fast declarative binary parser builder for node.js
License: MIT License
This project forked from keichi/binary-parser
Blazing-fast declarative binary parser builder for node.js
License: MIT License
The encoder part was just what I was looking for, keep up the great work. Are you going to merge with the latest changes from origin?
If a length is set and the given string is shorter than the length, the generated code fills the emptyness with spaces.
I have a use case where the padding needs to be \u0000
.
the workaround is that I use the encoder function to fill the padding with my zeroes:
const encodeString = (str) => {
return str + '\u0000'.repeat(100);
};
...
.string('flash_tel_nr', { stripNull: true, length: 20, trim: true, encoder: encodeString })
Also, an option argument passed to the encoder would be nice.
This is the error I get!
This library as well as keichi's one worked before, but now they both don't! going back to the previous version it remains broken, so I suspect something that is happening here may no longer be supported?
Kinda sucks cause this is a key part of one of my applications I'm working on, and there doesn't seem to be any well maintained alternatives.
let parser = new Parser() .endianess("little") .bit4("a") .bit1("b") .bit1("c") .bit1("d") .bit1("e") .uint16("f") .array("g", { type: "uint8", readUntil: "eof" });
Encoding and then parsing an object using this parser in little endian mode does not result in the same object, but it does in big endian mode.
Documentation says: Encoded result will be returned as a Buffer object.
Parser.encode() is declared as encode(obj: any): any;
in binary_parser.d.ts
Parser.encode() return type should be declared as Buffer
Suppose I have this code
parser.string(name, {
length: string_size,
zeroTerminated: false,
stripNull: true
});
where string_size is some fixed length.
The thing appears to be parsed nicely, but when I try to re-encode, the missing fields are filled with spaces. A temporary workaround I found is
encoder: item => (item+'') + Array(1024).fill("\0").join("") // 1024 being some arbitrary number that's bigger than string_size
since then the string is just capped at its maximum length, and only then my output file is equal to input.
Hi,
Many thanks for this super useful package.
I'm using it in my current project but I have one annoying issue, which is that I can't run my Jest based tests with parser based encoding. Here is the error message:
ReferenceError: SmartBuffer is not defined
at Parser.compiledEncode (evalmachine.<anonymous>:5:19)
at Parser.Object.<anonymous>.Parser.encode (node_modules/binary-parser-encoder/lib/binary_parser.js:455:15)
at input.reduce (src/myfile.js:196:14)
at Array.reduce (<anonymous>)
at download (src/myfile.js:193:16)
at Object.test (src/myfile.test.js:9:12)
EDIT
I slim down my codebase to only a test file:
const { Parser } = require('binary-parser-encoder');
const parser = new Parser()
.uint32be('data_0');
test('test', () => {
const a = parser.encode({ data_0: '123' })
.toString('hex');
expect(a).toEqual('0000007b');
});
I would appreciate any help.
What is even stranger this test works perfectly in codesandbox https://codesandbox.io/s/m3z1wz265x
https://www.npmjs.com/package/binary-parser-encoder#setencoderoptionsopts
In the documentation, there is function "setEncoderOptions", but in the example it's "setEncoderOpts".
The main issue is that there are no such functions in the "Parser" object, but it has "encoderSetOptions".
The function name in the documentation should be aligned with code.
var ipHeader = new Parser()
.uint16("fragment_id")
.uint16("fragment_total")
.uint64("date_time")
var anIpHeader = {
fragment_id: 1,
fragment_total: 1,
datetime: 4744430483355899789,
}
console.log(ipHeader.encode(anIpHeader).toString("hex"));
internal/buffer.js:598
let lo = Number(value & 0xffffffffn);
^
TypeError: Cannot mix BigInt and other types, use explicit conversions
at writeBigU_Int64BE (internal/buffer.js:598:25)
at Buffer.writeBigUInt64BE (internal/buffer.js:618:10)
at SmartBuffer._writeNumberValue (C:\work\ugves\project\repo-poc\emulator-prs-poc\nodejs-es-to-prs\node-start-es6\node_modules\smart-buffer\src\smartbuffer.ts:1402:10)
at SmartBuffer.writeBigUInt64BE (C:\work\ugves\project\repo-poc\emulator-prs-poc\nodejs-es-to-prs\node-start-es6\node_modules\smart-buffer\src\smartbuffer.ts:576:17)
at Parser.compiledEncode (evalmachine.:11:13)
Please help me how can i fix this issue
appreciate quick support,
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.