@chr33s/pdf-restructure 5.0.0 → 5.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +5 -1
- package/test/array.test.ts +0 -95
- package/test/bitfield.test.ts +0 -52
- package/test/boolean.test.ts +0 -35
- package/test/buffer.test.ts +0 -49
- package/test/decode-stream.test.ts +0 -104
- package/test/encode-stream.test.ts +0 -111
- package/test/enum.test.ts +0 -30
- package/test/lazy-array.test.ts +0 -70
- package/test/number.test.ts +0 -222
- package/test/optional.test.ts +0 -105
- package/test/pointer.test.ts +0 -248
- package/test/reserved.test.ts +0 -28
- package/test/string.test.ts +0 -114
- package/test/struct.test.ts +0 -164
- package/test/versioned-struct.test.ts +0 -462
- package/tsconfig.json +0 -9
- package/tsconfig.typecheck.json +0 -14
- package/vitest.config.ts +0 -12
package/test/string.test.ts
DELETED
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
import { Buffer } from "node:buffer";
|
|
2
|
-
import { describe, expect, test } from "vitest";
|
|
3
|
-
import { DecodeStream, String as StringT, uint8 } from "../src/index.js";
|
|
4
|
-
|
|
5
|
-
describe("String", () => {
|
|
6
|
-
describe("decode", () => {
|
|
7
|
-
test("should decode fixed length", () => {
|
|
8
|
-
const string = new StringT(7);
|
|
9
|
-
expect(string.fromBuffer(Buffer.from("testing"))).to.equal("testing");
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
test("should decode length from parent key", () => {
|
|
13
|
-
const stream = new DecodeStream(Buffer.from("testing"));
|
|
14
|
-
const string = new StringT("len");
|
|
15
|
-
expect(string.decode(stream, { len: 7 })).to.equal("testing");
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
test("should decode length as number before string", () => {
|
|
19
|
-
const string = new StringT(uint8);
|
|
20
|
-
expect(string.fromBuffer(Buffer.from("\x07testing", "binary"))).to.equal("testing");
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
test("should decode utf8", () => {
|
|
24
|
-
const string = new StringT(4, "utf8");
|
|
25
|
-
expect(string.fromBuffer(Buffer.from("🍻"))).to.equal("🍻");
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
test("should decode encoding computed from function", () => {
|
|
29
|
-
const string = new StringT(4, () => "utf8");
|
|
30
|
-
expect(string.fromBuffer(Buffer.from("🍻"))).to.equal("🍻");
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
test("should decode null-terminated string and read past terminator", () => {
|
|
34
|
-
const stream = new DecodeStream(Buffer.from("🍻\x00"));
|
|
35
|
-
const string = new StringT(undefined, "utf8");
|
|
36
|
-
expect(string.decode(stream)).to.equal("🍻");
|
|
37
|
-
expect(stream.pos).to.equal(5);
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
test("should decode remainder of buffer when null-byte missing", () => {
|
|
41
|
-
const string = new StringT(undefined, "utf8");
|
|
42
|
-
expect(string.fromBuffer(Buffer.from("🍻"))).to.equal("🍻");
|
|
43
|
-
});
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
describe("size", () => {
|
|
47
|
-
test("should use string length", () => {
|
|
48
|
-
const string = new StringT(7);
|
|
49
|
-
expect(string.size("testing")).to.equal(7);
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
test("should use correct encoding", () => {
|
|
53
|
-
const string = new StringT(10, "utf8");
|
|
54
|
-
expect(string.size("🍻")).to.equal(4);
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
test("should use encoding from function", () => {
|
|
58
|
-
const string = new StringT(10, () => "utf8");
|
|
59
|
-
expect(string.size("🍻")).to.equal(4);
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
test("should add size of length field before string", () => {
|
|
63
|
-
const string = new StringT(uint8, "utf8");
|
|
64
|
-
expect(string.size("🍻")).to.equal(5);
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
test("should work with utf16be encoding", () => {
|
|
68
|
-
const string = new StringT(10, "utf16be");
|
|
69
|
-
expect(string.size("🍻")).to.equal(4);
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
test("should take null-byte into account", () => {
|
|
73
|
-
const string = new StringT(undefined, "utf8");
|
|
74
|
-
expect(string.size("🍻")).to.equal(5);
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
test("should use defined length if no value given", () => {
|
|
78
|
-
const array = new StringT(10);
|
|
79
|
-
expect(array.size()).to.equal(10);
|
|
80
|
-
});
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
describe("encode", () => {
|
|
84
|
-
test("should encode using string length", () => {
|
|
85
|
-
const string = new StringT(7);
|
|
86
|
-
expect(string.toBuffer("testing")).to.deep.equal(Buffer.from("testing"));
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
test("should encode length as number before string", () => {
|
|
90
|
-
const string = new StringT(uint8);
|
|
91
|
-
expect(string.toBuffer("testing")).to.deep.equal(Buffer.from("\x07testing", "binary"));
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
test("should encode length as number before utf8 string", () => {
|
|
95
|
-
const string = new StringT(uint8, "utf8");
|
|
96
|
-
expect(string.toBuffer("testing 😜")).to.deep.equal(Buffer.from("\x0ctesting 😜", "utf8"));
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
test("should encode utf8", () => {
|
|
100
|
-
const string = new StringT(4, "utf8");
|
|
101
|
-
expect(string.toBuffer("🍻")).to.deep.equal(Buffer.from("🍻"));
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
test("should encode encoding computed from function", () => {
|
|
105
|
-
const string = new StringT(4, () => "utf8");
|
|
106
|
-
expect(string.toBuffer("🍻")).to.deep.equal(Buffer.from("🍻"));
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
test("should encode null-terminated string", () => {
|
|
110
|
-
const string = new StringT(undefined, "utf8");
|
|
111
|
-
expect(string.toBuffer("🍻")).to.deep.equal(Buffer.from("🍻\x00"));
|
|
112
|
-
});
|
|
113
|
-
});
|
|
114
|
-
});
|
package/test/struct.test.ts
DELETED
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
import { describe, expect, test } from "vitest";
|
|
2
|
-
import {
|
|
3
|
-
DecodeStream,
|
|
4
|
-
EncodeStream,
|
|
5
|
-
Pointer,
|
|
6
|
-
String as StringT,
|
|
7
|
-
Struct,
|
|
8
|
-
uint8,
|
|
9
|
-
} from "../src/index.js";
|
|
10
|
-
|
|
11
|
-
describe("Struct", () => {
|
|
12
|
-
describe("decode", () => {
|
|
13
|
-
test("should decode into an object", () => {
|
|
14
|
-
const stream = new DecodeStream(new Uint8Array([0x05, 0x64, 0x65, 0x76, 0x6f, 0x6e, 0x15])); // \x05devon\x15
|
|
15
|
-
const struct = new Struct({
|
|
16
|
-
name: new StringT(uint8),
|
|
17
|
-
age: uint8,
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
expect(struct.decode(stream)).to.deep.equal({ name: "devon", age: 21 });
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
test("should support process hook", () => {
|
|
24
|
-
const stream = new DecodeStream(new Uint8Array([0x05, 0x64, 0x65, 0x76, 0x6f, 0x6e, 0x20])); // \x05devon\x20
|
|
25
|
-
const struct = new Struct({
|
|
26
|
-
name: new StringT(uint8),
|
|
27
|
-
age: uint8,
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
struct.process = function process() {
|
|
31
|
-
(this as any).canDrink = (this as any).age >= 21;
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
expect(struct.decode(stream)).to.deep.equal({
|
|
35
|
-
name: "devon",
|
|
36
|
-
age: 32,
|
|
37
|
-
canDrink: true,
|
|
38
|
-
});
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
test("should support function keys", () => {
|
|
42
|
-
const stream = new DecodeStream(new Uint8Array([0x05, 0x64, 0x65, 0x76, 0x6f, 0x6e, 0x20])); // \x05devon\x20
|
|
43
|
-
const struct = new Struct({
|
|
44
|
-
name: new StringT(uint8),
|
|
45
|
-
age: uint8,
|
|
46
|
-
canDrink() {
|
|
47
|
-
return (this as any).age >= 21;
|
|
48
|
-
},
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
expect(struct.decode(stream)).to.deep.equal({
|
|
52
|
-
name: "devon",
|
|
53
|
-
age: 32,
|
|
54
|
-
canDrink: true,
|
|
55
|
-
});
|
|
56
|
-
});
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
describe("size", () => {
|
|
60
|
-
test("should compute the correct size", () => {
|
|
61
|
-
const struct = new Struct({
|
|
62
|
-
name: new StringT(uint8),
|
|
63
|
-
age: uint8,
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
expect(struct.size({ name: "devon", age: 21 })).to.equal(7);
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
test("should compute the correct size with pointers", () => {
|
|
70
|
-
const struct = new Struct({
|
|
71
|
-
name: new StringT(uint8),
|
|
72
|
-
age: uint8,
|
|
73
|
-
ptr: new Pointer(uint8, new StringT(uint8)),
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
const size = struct.size({ name: "devon", age: 21, ptr: "hello" });
|
|
77
|
-
expect(size).to.equal(14);
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
test("should get the correct size when no value is given", () => {
|
|
81
|
-
const struct = new Struct({
|
|
82
|
-
name: new StringT(4),
|
|
83
|
-
age: uint8,
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
expect(struct.size()).to.equal(5);
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
test("should throw when getting non-fixed length size and no value is given", () => {
|
|
90
|
-
const struct = new Struct({
|
|
91
|
-
name: new StringT(uint8),
|
|
92
|
-
age: uint8,
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
expect(() => struct.size()).to.throw(/not a fixed size/i);
|
|
96
|
-
});
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
describe("encode", () => {
|
|
100
|
-
test("should encode objects to buffers", () => {
|
|
101
|
-
const struct = new Struct({
|
|
102
|
-
name: new StringT(uint8),
|
|
103
|
-
age: uint8,
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
const value = { name: "devon", age: 21 };
|
|
107
|
-
const stream = new EncodeStream(new Uint8Array(struct.size(value)));
|
|
108
|
-
struct.encode(stream, value);
|
|
109
|
-
expect(stream.buffer).to.deep.equal(
|
|
110
|
-
new Uint8Array([0x05, 0x64, 0x65, 0x76, 0x6f, 0x6e, 0x15]),
|
|
111
|
-
);
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
test("should support preEncode hook", () => {
|
|
115
|
-
const struct = new Struct({
|
|
116
|
-
nameLength: uint8,
|
|
117
|
-
name: new StringT("nameLength"),
|
|
118
|
-
age: uint8,
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
struct.preEncode = function preEncode() {
|
|
122
|
-
(this as any).nameLength = (this as any).name.length;
|
|
123
|
-
};
|
|
124
|
-
|
|
125
|
-
const value = { name: "devon", age: 21 } as any;
|
|
126
|
-
const stream = new EncodeStream(new Uint8Array(struct.size(value)));
|
|
127
|
-
struct.encode(stream, value);
|
|
128
|
-
expect(stream.buffer).to.deep.equal(
|
|
129
|
-
new Uint8Array([0x05, 0x64, 0x65, 0x76, 0x6f, 0x6e, 0x15]),
|
|
130
|
-
);
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
test("should encode pointer data after structure", () => {
|
|
134
|
-
const struct = new Struct({
|
|
135
|
-
name: new StringT(uint8),
|
|
136
|
-
age: uint8,
|
|
137
|
-
ptr: new Pointer(uint8, new StringT(uint8)),
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
const value = { name: "devon", age: 21, ptr: "hello" };
|
|
141
|
-
const stream = new EncodeStream(new Uint8Array(struct.size(value)));
|
|
142
|
-
struct.encode(stream, value);
|
|
143
|
-
// \x05devon\x15\x08\x05hello
|
|
144
|
-
expect(stream.buffer).to.deep.equal(
|
|
145
|
-
new Uint8Array([
|
|
146
|
-
0x05,
|
|
147
|
-
0x64,
|
|
148
|
-
0x65,
|
|
149
|
-
0x76,
|
|
150
|
-
0x6f,
|
|
151
|
-
0x6e, // \x05devon
|
|
152
|
-
0x15, // age: 21
|
|
153
|
-
0x08, // pointer offset
|
|
154
|
-
0x05,
|
|
155
|
-
0x68,
|
|
156
|
-
0x65,
|
|
157
|
-
0x6c,
|
|
158
|
-
0x6c,
|
|
159
|
-
0x6f, // \x05hello
|
|
160
|
-
]),
|
|
161
|
-
);
|
|
162
|
-
});
|
|
163
|
-
});
|
|
164
|
-
});
|