mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-04-23 21:00:01 +08:00
Binary parser typings (#21041)
* Initial commit * Add strict function type check
This commit is contained in:
95
types/binary-parser/binary-parser-tests.ts
Normal file
95
types/binary-parser/binary-parser-tests.ts
Normal file
@@ -0,0 +1,95 @@
|
||||
import { Parser } from "binary-parser";
|
||||
|
||||
// Build an IP packet header Parser
|
||||
const ipHeader = new Parser()
|
||||
.endianess('big')
|
||||
.bit4('version')
|
||||
.bit4('headerLength')
|
||||
.uint8('tos')
|
||||
.uint16('packetLength')
|
||||
.uint16('id')
|
||||
.bit3('offset')
|
||||
.bit13('fragOffset')
|
||||
.uint8('ttl')
|
||||
.uint8('protocol')
|
||||
.uint16('checksum')
|
||||
.array('src', {
|
||||
type: 'uint8',
|
||||
length: 4
|
||||
})
|
||||
.array('dst', {
|
||||
type: 'uint8',
|
||||
length: 4
|
||||
});
|
||||
|
||||
// Prepare buffer to parse.
|
||||
const buf = new Buffer('450002c5939900002c06ef98adc24f6c850186d1', 'hex');
|
||||
|
||||
// Parse buffer and show result
|
||||
ipHeader.parse(buf);
|
||||
|
||||
const parser2 = new Parser()
|
||||
// Signed 32-bit integer (little endian)
|
||||
.int32le('a')
|
||||
// Unsigned 8-bit integer
|
||||
.uint8('b')
|
||||
// Signed 16-bit integer (big endian)
|
||||
.int16be('c');
|
||||
|
||||
const parser3 = new Parser()
|
||||
// 32-bit floating value (big endian)
|
||||
.floatbe('a')
|
||||
// 64-bit floating value (little endian)
|
||||
.doublele('b');
|
||||
|
||||
const parser4 = new Parser()
|
||||
// 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: () => 4 // other fields are available through this
|
||||
})
|
||||
|
||||
// Statically sized array
|
||||
.array('data4', {
|
||||
type: 'int32',
|
||||
lengthInBytes: 16
|
||||
})
|
||||
|
||||
// Dynamically sized array (references another variable)
|
||||
.uint8('dataLengthInBytes')
|
||||
.array('data5', {
|
||||
type: 'int32',
|
||||
lengthInBytes: 'dataLengthInBytes'
|
||||
})
|
||||
|
||||
// Dynamically sized array (with some calculation)
|
||||
.array('data6', {
|
||||
type: 'int32',
|
||||
lengthInBytes: () => 4, // other fields are available through this
|
||||
})
|
||||
|
||||
// Dynamically sized array (with stop-check on parsed item)
|
||||
.array('data7', {
|
||||
type: 'int32',
|
||||
readUntil: (item, buffer) => true // stop when specific item is parsed. buffer can be used to perform a read-ahead.
|
||||
});
|
||||
|
||||
const parser5 = new Parser()
|
||||
.array('ipv4', {
|
||||
type: 'uint8',
|
||||
length: '4',
|
||||
formatter: (arr) => { }
|
||||
});
|
||||
147
types/binary-parser/index.d.ts
vendored
Normal file
147
types/binary-parser/index.d.ts
vendored
Normal file
@@ -0,0 +1,147 @@
|
||||
// Type definitions for binary-parser 1.3
|
||||
// Project: https://github.com/keichi/binary-parser
|
||||
// Definitions by: Benjamin Riggs <https://github.com/riggs>, Dolan Miu <https://github.com/dolanmiu>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
|
||||
/// <reference types="node" />
|
||||
|
||||
export interface Parser {
|
||||
parse(buffer: Buffer, callback?: (err?: Error, result?: any) => void): Parser.Parsed;
|
||||
|
||||
create(constructorFunction: ObjectConstructor): Parser;
|
||||
|
||||
int8(name: string, options?: Parser.Options): Parser;
|
||||
uint8(name: string, options?: Parser.Options): Parser;
|
||||
|
||||
int16(name: string, options?: Parser.Options): Parser;
|
||||
uint16(name: string, options?: Parser.Options): Parser;
|
||||
int16le(name: string, options?: Parser.Options): Parser;
|
||||
int16be(name: string, options?: Parser.Options): Parser;
|
||||
uint16le(name: string, options?: Parser.Options): Parser;
|
||||
uint16be(name: string, options?: Parser.Options): Parser;
|
||||
|
||||
int32(name: string, options?: Parser.Options): Parser;
|
||||
uint32(name: string, options?: Parser.Options): Parser;
|
||||
int32le(name: string, options?: Parser.Options): Parser;
|
||||
int32be(name: string, options?: Parser.Options): Parser;
|
||||
uint32le(name: string, options?: Parser.Options): Parser;
|
||||
uint32be(name: string, options?: Parser.Options): Parser;
|
||||
|
||||
bit1(name: string, options?: Parser.Options): Parser;
|
||||
bit2(name: string, options?: Parser.Options): Parser;
|
||||
bit3(name: string, options?: Parser.Options): Parser;
|
||||
bit4(name: string, options?: Parser.Options): Parser;
|
||||
bit5(name: string, options?: Parser.Options): Parser;
|
||||
bit6(name: string, options?: Parser.Options): Parser;
|
||||
bit7(name: string, options?: Parser.Options): Parser;
|
||||
bit8(name: string, options?: Parser.Options): Parser;
|
||||
bit9(name: string, options?: Parser.Options): Parser;
|
||||
bit10(name: string, options?: Parser.Options): Parser;
|
||||
bit11(name: string, options?: Parser.Options): Parser;
|
||||
bit12(name: string, options?: Parser.Options): Parser;
|
||||
bit13(name: string, options?: Parser.Options): Parser;
|
||||
bit14(name: string, options?: Parser.Options): Parser;
|
||||
bit15(name: string, options?: Parser.Options): Parser;
|
||||
bit16(name: string, options?: Parser.Options): Parser;
|
||||
bit17(name: string, options?: Parser.Options): Parser;
|
||||
bit18(name: string, options?: Parser.Options): Parser;
|
||||
bit19(name: string, options?: Parser.Options): Parser;
|
||||
bit20(name: string, options?: Parser.Options): Parser;
|
||||
bit21(name: string, options?: Parser.Options): Parser;
|
||||
bit22(name: string, options?: Parser.Options): Parser;
|
||||
bit23(name: string, options?: Parser.Options): Parser;
|
||||
bit24(name: string, options?: Parser.Options): Parser;
|
||||
bit25(name: string, options?: Parser.Options): Parser;
|
||||
bit26(name: string, options?: Parser.Options): Parser;
|
||||
bit27(name: string, options?: Parser.Options): Parser;
|
||||
bit28(name: string, options?: Parser.Options): Parser;
|
||||
bit29(name: string, options?: Parser.Options): Parser;
|
||||
bit30(name: string, options?: Parser.Options): Parser;
|
||||
bit31(name: string, options?: Parser.Options): Parser;
|
||||
bit32(name: string, options?: Parser.Options): Parser;
|
||||
|
||||
float(name: string, options?: Parser.Options): Parser;
|
||||
floatle(name: string, options?: Parser.Options): Parser;
|
||||
floatbe(name: string, options?: Parser.Options): Parser;
|
||||
|
||||
double(name: string, options?: Parser.Options): Parser;
|
||||
doublele(name: string, options?: Parser.Options): Parser;
|
||||
doublebe(name: string, options?: Parser.Options): Parser;
|
||||
|
||||
string(name: string, options?: Parser.StringOptions): Parser;
|
||||
|
||||
buffer(name: string, options: Parser.BufferOptions): Parser;
|
||||
|
||||
array(name: string, options: Parser.ArrayOptions): Parser;
|
||||
|
||||
choice(name: string, options: Parser.ChoiceOptions): Parser;
|
||||
|
||||
nest(name: string, options: Parser.NestOptions): Parser;
|
||||
|
||||
skip(length: number): Parser;
|
||||
|
||||
endianess(endianess: Parser.Endianness): Parser; /* [sic] */
|
||||
|
||||
namely(alias: string): Parser;
|
||||
|
||||
compile(): void;
|
||||
|
||||
getCode(): string;
|
||||
}
|
||||
|
||||
export interface ParserConstructor {
|
||||
new(): Parser;
|
||||
}
|
||||
|
||||
export const Parser: ParserConstructor;
|
||||
|
||||
export namespace Parser {
|
||||
type Data = number | string | Array<number | Parsed> | Parsed | Buffer;
|
||||
interface Parsed {
|
||||
[name: string]: Data;
|
||||
}
|
||||
|
||||
interface Options {
|
||||
formatter?: ((value: Data) => any);
|
||||
assert?: string | number | ((value: Data) => boolean);
|
||||
}
|
||||
|
||||
interface StringOptions extends Options {
|
||||
encoding?: string;
|
||||
length?: number | string | ((this: Parsed) => number);
|
||||
zeroTerminated?: boolean;
|
||||
greedy?: boolean;
|
||||
stripNull?: boolean;
|
||||
}
|
||||
|
||||
interface BufferOptions extends Options {
|
||||
clone?: boolean;
|
||||
length?: number | string | ((this: Parsed) => number);
|
||||
readUntil?: string | ((item: number, buffer: Buffer) => boolean);
|
||||
}
|
||||
|
||||
interface ArrayOptions extends Options {
|
||||
type: string | Parser;
|
||||
length?: number | string | ((this: Parsed) => number);
|
||||
lengthInBytes?: number | string | ((this: Parsed) => number);
|
||||
readUntil?: string | ((item: number, buffer: Buffer) => boolean);
|
||||
}
|
||||
|
||||
interface ChoiceOptions extends Options {
|
||||
tag: string | ((this: Parsed) => number);
|
||||
choices: { [item: number]: Parser | string };
|
||||
defaultChoice?: Parser | string;
|
||||
}
|
||||
|
||||
interface NestOptions extends Options {
|
||||
type: Parser | string;
|
||||
}
|
||||
|
||||
type Endianness =
|
||||
'little' |
|
||||
'big';
|
||||
|
||||
interface Context {
|
||||
[name: string]: Parsed;
|
||||
}
|
||||
}
|
||||
23
types/binary-parser/tsconfig.json
Normal file
23
types/binary-parser/tsconfig.json
Normal file
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"lib": [
|
||||
"es6"
|
||||
],
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"strictNullChecks": true,
|
||||
"strictFunctionTypes": true,
|
||||
"baseUrl": "../",
|
||||
"typeRoots": [
|
||||
"../"
|
||||
],
|
||||
"types": [],
|
||||
"noEmit": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"files": [
|
||||
"index.d.ts",
|
||||
"binary-parser-tests.ts"
|
||||
]
|
||||
}
|
||||
1
types/binary-parser/tslint.json
Normal file
1
types/binary-parser/tslint.json
Normal file
@@ -0,0 +1 @@
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
Reference in New Issue
Block a user