@36node/telegram 中文文档教程
telegram
Install
yarn add telegram
Usage
const telegram = require("telegram");
telegram();
API
new Telegram()
构造一个 Telegram 对象。 返回的对象代表一个解析器,它解析 没有什么。
decompress(buffer)
使用此解析器解析 Buffer
对象 buffer
并返回一个对象作为结果。
[u]int{8, 16, 32}{le, be}(name[, options])
将字节解析为整数并将其存储在名为 name
的变量中。 name
应仅包含字母数字字符并以字母开头。
位数可以从 8、16 和 32 中选择。字节顺序可以是 l
(小端)或 b
(大端)。 没有前缀,它解析为有符号数,u
为无符号数前缀。
var parser = new Telegram()
// Signed 32-bit integer (little endian)
.int32le("a")
// Unsigned 8-bit integer
.uint8("b")
// Signed 16-bit integer (big endian)
.int16be("c");
bit[1-64](name, options)
将字节解析为位域并将其存储在变量 name
中。 从bit1
到bit64
共有64种方法,分别对应1位到64位长度的位域。
{float, double}{le, be}(name[, options])
将字节解析为浮点值并将其存储在名为 name
的变量中。
var parser = new Telegram()
// 32-bit floating value (big endian)
.floatbe("a")
// 64-bit floating value (little endian)
.doublele("b");
string(name, options)
将字节解析为字符串。 options
是一个对象,它可以有 以下键: 将
encoding
- (Optional, defaults toutf8
) Specify which encoding to use."utf8"
,"ascii"
,"hex"
and else are valid. SeeBuffer.toString
for more info.length
- (Optional) Length of the string. Can be a number, string or a function. Use number for statically sized arrays.zeroTerminated
- (Optional, defaults tofalse
) If true, then this parser reads until it reaches zero.greedy
- (Optional, defaults tofalse
) If true, then this parser reads until it reaches the end of the buffer. Will consume zero-bytes.stripNull
- (Optional, must be used withlength
) If true, then strip null characters from end of the string.
array(name, options)
字节解析为数组。 options
是一个可以具有以下键的对象:
type
- (Required) Type of the array element. Can be a string or an user defined Parser object. If it's a string, you have to choose from [u]int{8, 16, 32}{le, be} | string.length
- Length of the array. Can be a number, string or a function. Use number for statically sized arrays.
var parser = new Telegram()
// Statically sized array
.array("data", {
type: "int32",
length: 8,
})
// Dynamically sized array (references another variable)
.uint8("dataLength")
.array("data2", {
type: "int32",
length: "dataLength",
})
// Dynamically sized array (with some calculation)
.array("data3", {
type: "int32",
length: function() {
return this.dataLength - 1;
}, // other fields are available through this
});
nest([name,] options)
执行内部解析器并将其结果存储到键 name
中。 如果 name
为 null 或省略,内部解析器的结果直接嵌入到 当前对象。 options
是一个可以具有以下键的对象:
type
- (Required) AParser
object.
skip(length[, options])
跳过 length
字节的解析。 如果 options
具有名为 type
的键并且 type
的值为 "bit"
,则跳过对 的解析长度
位。
endianess(endianess)
定义要在此解析器中使用的字节顺序。 endianess
可以是 “小”
或 “大”
。 Parser
的默认字节顺序设置为 big-endian。
var parser = new Telegram()
.endianess("little")
// You can specify endianess explicitly
.uint16be("a")
.uint32le("a")
// Or you can omit endianess (in this case, little-endian is used)
.uint16("b")
.int32("c");
Common options
这些是可以在所有解析器中指定的通用选项。
formatter
- Function that transforms the parsed value into a more desired form.
var parser = new Telegram().array("ipv4", {
type: uint8,
length: "4",
formatter: function(arr) {
return arr.join(".");
},
});
assert
- Do assertion on the parsed result (useful for checking magic numbers and so on). Ifassert
is astring
ornumber
, the actual parsed result will be compared with it with===
(strict equality check), and an exception is thrown if they mismatch. On the other hand, ifassert
is a function, that function is executed with one argument (parsed result) and if it returns false, an exception is thrown.
// simple maginc number validation
var ClassFile = Telegram.start()
.endianess("big")
.uint32("magic", { assert: 0xcafebabe });
Examples
有关更复杂的示例,请参见示例
。
Contributing
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request :D
Author
telegram © zzswang,根据 MIT 许可证发布.
由 zzswang 在贡献者 (list) 的帮助下创作和维护。
github.com/zzswang · GitHub @zzswang
telegram
Install
yarn add telegram
Usage
const telegram = require("telegram");
telegram();
API
new Telegram()
Constructs a Telegram object. Returned object represents a parser which parses nothing.
decompress(buffer)
Parse a Buffer
object buffer
with this parser and return an object as result.
[u]int{8, 16, 32}{le, be}(name[, options])
Parse bytes as an integer and store it in a variable named name
. name
should consist only of alphanumeric characters and start with an alphabet.
Number of bits can be chosen from 8, 16 and 32. Byte-ordering can be either l
for little endian or b
for big endian. With no prefix, it parses as a signed number, with u
prefixed as an unsigned number.
var parser = new Telegram()
// Signed 32-bit integer (little endian)
.int32le("a")
// Unsigned 8-bit integer
.uint8("b")
// Signed 16-bit integer (big endian)
.int16be("c");
bit[1-64](name, options)
Parse bytes as a bit field and store it in variable name
. There are 64 methods from bit1
to bit64
each corresponding to 1-bit-length to 64-bits-length bit field.
{float, double}{le, be}(name[, options])
Parse bytes as an floating-point value and store it in a variable named name
.
var parser = new Telegram()
// 32-bit floating value (big endian)
.floatbe("a")
// 64-bit floating value (little endian)
.doublele("b");
string(name, options)
Parse bytes as a string. options
is an object which can have the following keys:
encoding
- (Optional, defaults toutf8
) Specify which encoding to use."utf8"
,"ascii"
,"hex"
and else are valid. SeeBuffer.toString
for more info.length
- (Optional) Length of the string. Can be a number, string or a function. Use number for statically sized arrays.zeroTerminated
- (Optional, defaults tofalse
) If true, then this parser reads until it reaches zero.greedy
- (Optional, defaults tofalse
) If true, then this parser reads until it reaches the end of the buffer. Will consume zero-bytes.stripNull
- (Optional, must be used withlength
) If true, then strip null characters from end of the string.
array(name, options)
Parse bytes as an array. options
is an object which can have the following keys:
type
- (Required) Type of the array element. Can be a string or an user defined Parser object. If it's a string, you have to choose from [u]int{8, 16, 32}{le, be} | string.length
- Length of the array. Can be a number, string or a function. Use number for statically sized arrays.
var parser = new Telegram()
// Statically sized array
.array("data", {
type: "int32",
length: 8,
})
// Dynamically sized array (references another variable)
.uint8("dataLength")
.array("data2", {
type: "int32",
length: "dataLength",
})
// Dynamically sized array (with some calculation)
.array("data3", {
type: "int32",
length: function() {
return this.dataLength - 1;
}, // other fields are available through this
});
nest([name,] options)
Execute an inner parser and store its result to key name
. If name
is null or omitted, the result of the inner parser is directly embedded into the current object. options
is an object which can have the following keys:
type
- (Required) AParser
object.
skip(length[, options])
Skip parsing for length
bytes. If options
has key named type
and the value of type
is "bit"
, skip parsing for length
bits.
endianess(endianess)
Define what endianess to use in this parser. endianess
can be either "little"
or "big"
. The default endianess of Parser
is set to big-endian.
var parser = new Telegram()
.endianess("little")
// You can specify endianess explicitly
.uint16be("a")
.uint32le("a")
// Or you can omit endianess (in this case, little-endian is used)
.uint16("b")
.int32("c");
Common options
These are common options that can be specified in all parsers.
formatter
- Function that transforms the parsed value into a more desired form.
var parser = new Telegram().array("ipv4", {
type: uint8,
length: "4",
formatter: function(arr) {
return arr.join(".");
},
});
assert
- Do assertion on the parsed result (useful for checking magic numbers and so on). Ifassert
is astring
ornumber
, the actual parsed result will be compared with it with===
(strict equality check), and an exception is thrown if they mismatch. On the other hand, ifassert
is a function, that function is executed with one argument (parsed result) and if it returns false, an exception is thrown.
// simple maginc number validation
var ClassFile = Telegram.start()
.endianess("big")
.uint32("magic", { assert: 0xcafebabe });
Examples
See examples
for more complex examples.
Contributing
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request :D
Author
telegram © zzswang, Released under the MIT License.
Authored and maintained by zzswang with help from contributors (list).
github.com/zzswang · GitHub @zzswang