Skip to content

Commit 6d49eb6

Browse files
committed
Translate examples to TypeScript
1 parent 2eaa5fd commit 6d49eb6

File tree

8 files changed

+42
-34
lines changed

8 files changed

+42
-34
lines changed

example/bmp.js renamed to example/bmp.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const Parser = require("../dist/binary_parser").Parser;
1+
import { Parser } from "../lib/binary_parser";
2+
import { readFile } from "fs";
23

34
// C structure BITMAPFILEHEADER
45
// typedef struct tagBITMAPFILEHEADER {
@@ -55,6 +56,6 @@ const bmpFile = new Parser()
5556
type: bmpInfoHeader,
5657
});
5758

58-
require("fs").readFile("test.bmp", function (err, data) {
59+
readFile("test.bmp", (_, data) => {
5960
console.log(bmpFile.parse(data));
6061
});

example/classfile.js renamed to example/classfile.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
const Parser = require("../dist/binary_parser").Parser;
1+
import { Parser } from "../lib/binary_parser";
2+
import { readFile } from "fs";
3+
import { inspect } from "util";
24

35
const ConstantClassInfo = Parser.start().uint16be("name_index");
46

@@ -36,6 +38,7 @@ const ConstantUtf8Info = Parser.start()
3638
.uint16be("len")
3739
.string("bytes", { length: "len" });
3840

41+
// @ts-ignore
3942
const ConstantMethodHandleInfo = Parser.start()
4043
.uint8("reference_kind")
4144
.uint16be("reference_index");
@@ -75,11 +78,11 @@ const ClassFile = Parser.start()
7578
.uint16("constant_pool_count")
7679
.array("cp_info", {
7780
type: CpInfo,
78-
length: function () {
81+
length: function (this: any) {
7982
return this.constant_pool_count - 1;
8083
},
8184
});
8285

83-
require("fs").readFile("Hello.class", function (err, data) {
84-
console.log(require("util").inspect(ClassFile.parse(data), { depth: null }));
86+
readFile("Hello.class", function (_, data) {
87+
console.log(inspect(ClassFile.parse(data), { depth: null, colors: true }));
8588
});

example/elf32.js renamed to example/elf32.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
const Parser = require("../dist/binary_parser").Parser;
1+
import { Parser } from "../lib/binary_parser";
2+
import { readFile } from "fs";
3+
import { inspect } from "util";
24

35
const ELF32ProgramHeader = new Parser()
46
.endianess("little")
@@ -78,7 +80,7 @@ const ELF32Header = new Parser()
7880
},
7981
});
8082

81-
require("fs").readFile("hello", function (err, data) {
83+
readFile("hello", function (_, data) {
8284
const result = ELF32Header.parse(data);
83-
console.log(require("util").inspect(result, { depth: null }));
85+
console.log(inspect(result, { depth: null, colors: true }));
8486
});

example/ip.js renamed to example/ip.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const Parser = require("../dist/binary_parser").Parser;
1+
import { Parser } from "../lib/binary_parser";
22

33
const ipHeader = new Parser()
44
.endianess("big")

example/jpg.js renamed to example/jpg.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
const Parser = require("../dist/binary_parser").Parser;
1+
import { Parser } from "../lib/binary_parser";
2+
import { readFile } from "fs";
3+
import { inspect } from "util";
24

35
const SOI = Parser.start();
46

@@ -20,17 +22,18 @@ const APP0 = Parser.start()
2022
.uint8("thumbHeight")
2123
.array("thumbData", {
2224
type: "uint8",
23-
length: function () {
25+
length: function (this: any) {
2426
return this.Xt * this.Yt * 3;
2527
},
2628
});
2729

30+
// @ts-ignore
2831
const COM = Parser.start()
2932
.endianess("big")
3033
.uint16("length")
3134
.string("comment", {
3235
encoding: "ascii",
33-
length: function () {
36+
length: function (this: any) {
3437
return this.length - 2;
3538
},
3639
});
@@ -55,7 +58,7 @@ const DQT = Parser.start()
5558
type: "uint8",
5659
length: 64,
5760
}),
58-
length: function () {
61+
length: function (this: any) {
5962
return (this.length - 2) / 65;
6063
},
6164
});
@@ -78,7 +81,7 @@ const SOF0 = Parser.start()
7881
const Ignore = Parser.start()
7982
.endianess("big")
8083
.uint16("length")
81-
.seek(function () {
84+
.seek(function (this: any) {
8285
return this.length - 2;
8386
});
8487

@@ -103,6 +106,6 @@ const JPEG = Parser.start().array("segments", {
103106
readUntil: "eof",
104107
});
105108

106-
require("fs").readFile("test.jpg", function (err, data) {
107-
console.log(require("util").inspect(JPEG.parse(data), { depth: null }));
109+
readFile("test.jpg", function (_, data) {
110+
console.log(inspect(JPEG.parse(data), { depth: null, colors: true }));
108111
});

example/mbr.js renamed to example/mbr.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
const Parser = require("../dist/binary_parser").Parser;
2-
const fs = require("fs");
1+
import { Parser } from "../lib/binary_parser";
2+
import { readFile } from "fs";
3+
import { inspect } from "util";
34

4-
const chs = new Parser({
5-
formatter: function (val) {
6-
val.cylinder |= val.cylinderHigh << 8;
7-
return val;
8-
},
9-
})
5+
const chs = new Parser()
106
.uint8("head")
117
.bit2("cylinderHigh")
128
.bit6("sector")
@@ -25,6 +21,7 @@ const partitionTable = new Parser()
2521
.nest("endCHS", {
2622
type: chs,
2723
formatter: function (val) {
24+
val.cylinder |= val.cylinderHigh << 8;
2825
delete val.cylinderHigh;
2926
return val;
3027
},
@@ -42,6 +39,6 @@ const mbrParser = new Parser()
4239
assert: 0x55aa,
4340
});
4441

45-
fs.readFile("raspbian.img", function (err, data) {
46-
console.dir(mbrParser.parse(data), { depth: null, colors: true });
42+
readFile("raspbian.img", function (_, data) {
43+
console.log(inspect(mbrParser.parse(data), { depth: null, colors: true }));
4744
});

example/tar.js renamed to example/tar.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const Parser = require("../dist/binary_parser").Parser;
2-
const fs = require("fs");
1+
import { Parser } from "../lib/binary_parser";
2+
import { readFile } from "fs";
33

4-
const oct2int = function (s) {
4+
function oct2int(s: string): number {
55
return parseInt(s, 8);
6-
};
6+
}
77

88
const tarHeader = new Parser()
99
.string("name", { length: 100, stripNull: true })
@@ -28,7 +28,7 @@ const tarItem = new Parser()
2828
.nest({
2929
type: tarHeader,
3030
})
31-
.seek(function () {
31+
.seek(function (this: any) {
3232
return Math.ceil(this.size / 512) * 512;
3333
});
3434

@@ -37,6 +37,8 @@ const tarArchive = new Parser().array("files", {
3737
readUntil: "eof",
3838
});
3939

40-
fs.readFile("test.tar", function (err, data) {
40+
console.log(tarArchive.getCode());
41+
42+
readFile("test.tar", function (_, data) {
4143
console.dir(tarArchive.parse(data), { depth: null, colors: true });
4244
});

example/tcp.js renamed to example/tcp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const Parser = require("../dist/binary_parser").Parser;
1+
import { Parser } from "../lib/binary_parser";
22

33
const tcpHeader = new Parser()
44
.endianess("big")

0 commit comments

Comments
 (0)