22
33[ ![ build] ( https://github.com/keichi/binary-parser/workflows/build/badge.svg )] ( https://github.com/keichi/binary-parser/actions?query=workflow%3Abuild )
44[ ![ npm] ( https://img.shields.io/npm/v/binary-parser )] ( https://www.npmjs.com/package/binary-parser )
5+ [ ![ license] ( https://img.shields.io/github/license/keichi/binary-parser )] ( https://github.com/keichi/binary-parser/blob/master/LICENSE )
56
67Binary-parser is a parser builder for JavaScript that enables you to write
78efficient binary parsers in a simple and declarative manner.
@@ -30,18 +31,21 @@ and [binary](https://github.com/substack/node-binary).
3031
3132## Quick Start
3233
33- 1 . Create an empty Parser object with ` new Parser() ` or ` Parser.start() ` .
34- 2 . Chain methods to build your desired parser. (See
35- [ API] ( https://github.com/keichi/binary-parser#api ) for detailed document of
36- each method)
34+ 1 . Create an empty ` Parser ` object with ` new Parser() ` or ` Parser.start() ` .
35+ 2 . Chain methods to build your desired parser. (See [ API] ( #api ) for detailed
36+ documentation of each method)
37373 . Call ` Parser.prototype.parse ` with a ` Buffer ` /` Uint8Array ` object passed as
38- an argument.
38+ its only argument.
39394 . The parsed result will be returned as an object.
40+ - If parsing failed, an exception will be thrown.
4041
4142``` javascript
4243// Module import
4344const Parser = require (" binary-parser" ).Parser ;
4445
46+ // Alternative way to import the module
47+ // import { Parser } from "binary-parser";
48+
4549// Build an IP packet header Parser
4650const ipHeader = new Parser ()
4751 .endianness (" big" )
@@ -99,13 +103,13 @@ returned from the `parse` method.
99103Parse bytes as an integer and store it in a variable named ` name ` . ` name `
100104should consist only of alphanumeric characters and start with an alphabet.
101105Number of bits can be chosen from 8, 16, 32 and 64. Byte-ordering can be either
102- ` l ` for little endian or ` b ` for big endian. With no prefix, it parses as a
103- signed number, with ` u ` prefixed as an unsigned number. The runtime type
106+ ` le ` for little endian or ` be ` for big endian. With no prefix, it parses as a
107+ signed number, with ` u ` prefix as an unsigned number. The runtime type
104108returned by the 8, 16, 32 bit methods is ` number ` while the type
105109returned by the 64 bit is ` bigint ` .
106110
107111** Note:** [ u] int64{be,le} methods only work if your runtime is node v12.0.0 or
108- greater. Lower version will throw a runtime error.
112+ greater. Lower versions will throw a runtime error.
109113
110114``` javascript
111115const parser = new Parser ()
@@ -114,7 +118,7 @@ const parser = new Parser()
114118 // Unsigned 8-bit integer
115119 .uint8 (" b" )
116120 // Signed 16-bit integer (big endian)
117- .int16be (" c" );
121+ .int16be (" c" )
118122 // signed 64-bit integer (big endian)
119123 .int64be (" d" )
120124```
@@ -152,7 +156,7 @@ the following keys:
152156- ` greedy ` - (Optional, defaults to ` false ` ) If true, then this parser reads
153157 until it reaches the end of the buffer. Will consume zero-bytes.
154158- ` stripNull ` - (Optional, must be used with ` length ` ) If true, then strip
155- null characters from end of the string
159+ null characters from end of the string.
156160
157161### buffer(name[ , options] )
158162Parse bytes as a buffer. Its type will be the same as the input to
@@ -178,8 +182,8 @@ keys:
178182Parse bytes as an array. ` options ` is an object which can have the following
179183keys:
180184
181- - ` type ` - (Required) Type of the array element. Can be a string or an user
182- defined Parser object. If it's a string, you have to choose from [ u] int{8,
185+ - ` type ` - (Required) Type of the array element. Can be a string or a user
186+ defined ` Parser ` object. If it's a string, you have to choose from [ u] int{8,
183187 16, 32}{le, be}.
184188- ` length ` - (either ` length ` , ` lengthInBytes ` , or ` readUntil ` is required)
185189 Length of the array. Can be a number, string or a function. Use number for
@@ -257,7 +261,7 @@ the chosen parser is directly embedded into the current object. `options` is
257261an object which can have the following keys:
258262
259263- ` tag ` - (Required) The value used to determine which parser to use from the
260- ` choices ` Can be a string pointing to another field or a function.
264+ ` choices ` . Can be a string pointing to another field or a function.
261265- ` choices ` - (Required) An object which key is an integer and value is the
262266 parser which is executed when ` tag ` equals the key value.
263267- ` defaultChoice ` - (Optional) In case if the tag value doesn't match any of
@@ -294,7 +298,7 @@ Useful for parsing binary formats such as ELF where the offset of a field is
294298pointed by another field.
295299
296300- ` type ` - (Required) Can be a string ` [u]int{8, 16, 32, 64}{le, be} `
297- or an user defined Parser object.
301+ or a user defined ` Parser ` object.
298302- ` offset ` - (Required) Indicates absolute offset from the beginning of the
299303 input buffer. Can be a number, string or a function.
300304
@@ -345,9 +349,9 @@ const parser = new Parser()
345349```
346350
347351### namely(alias)
348- Set an alias to this parser, so there will be an opportunity to refer to it by
349- name in methods like ` .array ` , ` .nest ` and ` .choice ` , instead of requirement
350- to have an instance of it .
352+ Set an alias to this parser, so that it can be referred to by name in methods
353+ like ` .array ` , ` .nest ` and ` .choice ` , without the requirement to have an
354+ instance of this parser .
351355
352356Especially, the parser may reference itself:
353357
@@ -410,7 +414,7 @@ to avoid this, ensure that every possible path has its end. Also, this
410414recursion is not tail-optimized, so could lead to memory leaks when it goes
411415too deep.
412416
413- An example of referencing other patches :
417+ An example of referencing other parsers :
414418
415419``` javascript
416420// the line below registers the name "self", so we will be able to use it in
@@ -512,8 +516,8 @@ These options can be used in all parsers.
512516 numbers and so on). If ` assert` is a ` string` or ` number` , the actual parsed
513517 result will be compared with it with ` ===` (strict equality check), and an
514518 exception is thrown if they mismatch . On the other hand, if ` assert` is a
515- function , that function is executed with one argument (parsed result ) and if
516- it returns false, an exception is thrown.
519+ function , that function is executed with one argument (the parsed result )
520+ and if it returns false, an exception is thrown.
517521
518522 ```javascript
519523 // simple maginc number validation
@@ -550,7 +554,7 @@ you need to call `.useContextVars()` to enable it.
550554 .array("data", {
551555 type: "int32",
552556 length: function() {
553- return this.$parent.header.length
557+ return this.$parent.header.length;
554558 }
555559 });
556560 ` ` `
@@ -569,7 +573,7 @@ you need to call `.useContextVars()` to enable it.
569573 .array("data", {
570574 type: "int32",
571575 length: function() {
572- return this.$root.header.length
576+ return this.$root.header.length;
573577 }
574578 }),
575579 });
@@ -612,5 +616,22 @@ and destruct.js is available under `benchmark/`.
612616
613617Please report issues to the
614618[issue tracker](https: // github.com/keichi/binary-parser/issues) if you have
615- any difficulties using this module , found a bug, or request a new feature.
616- Pull requests are welcomed.
619+ any difficulties using this module , found a bug, or would like to request a
620+ new feature. Pull requests are welcome.
621+
622+ To contribute code, first clone this repo, then install the dependencies:
623+
624+ ` ` ` bash
625+ git clone https://github.com/keichi/binary-parser.git
626+ cd binary-parser
627+ npm install
628+ ` ` `
629+
630+ If you added a feature or fixed a bug, update the test suite under ` test/` and
631+ then run it like this:
632+
633+ ` ` ` bash
634+ npm run test
635+ ` ` `
636+
637+ Make sure all the tests pass before submitting a pull request.
0 commit comments