-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
116 lines (107 loc) · 3.86 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { PBArray, PBBytes, PBString, PBUint32, ProtoBuf, ProtoBufBase, ProtoBufDecode, ProtoBufEx, ProtoBufIn, ProtoBufQuick, UnWrap } from "./protobuf.ts";
// 演示代码
class ProtoBufDataInnerClass extends ProtoBufBase {
data = PBString(1);
test = PBUint32(2);
}
class ProtoBufDataClass extends ProtoBufBase {
uin = PBUint32(1);
inner = ProtoBuf(2, ProtoBufDataInnerClass);
list = PBArray(3, ProtoBuf(ProtoBufDataInnerClass));
listinner = PBArray(4, ProtoBuf(class extends ProtoBufBase { data = PBArray(1, PBString()); }));
listquick = PBArray(5, ProtoBufIn({ data: PBString(1) }));
}
export function createProtobuf(uin, name) {
return ProtoBuf(class ProtoBufDataClass extends ProtoBufBase {
uin = PBUint32(1, false, uin);
name = PBString(2, false, name);
}).encode();
}
export function testSerialization() {
const test = ProtoBufEx(ProtoBufDataClass, {
inner: {
data: "x",
test: 300
},
list: [{
data: "xx",
test: 5
}, {
data: "xxx",
test: 2
}],
listinner: [{
data: ["x", "xxxx"]
}],
listquick: [{
data: "xxxxx"
}],
uin: 0
});
console.log("值首次序列化:", Buffer.from(test.encode()).toString('hex'));
console.log("值修改前序列化:", test.listinner[0].data);
test.listinner[0].data = ["x00", "xxxxx"];
test.inner.test = 200;
console.log("值修改序列化:", Buffer.from(test.encode()).toString('hex'));
}
export function testDeserialization() {
const test = ProtoBuf(ProtoBufDataClass);
let data = Buffer.from('086412090a047465737410ac021a080a047465737410051a090a0574657374311002220d0a04746573740a0574657374312a070a057465737435', 'hex');
console.log("值首次反序列化:", test.decode(new Uint8Array(data)).uin);
}
export function testJsonSerialization() {
const test = ProtoBufEx(ProtoBufDataClass, {
inner: {
data: "x",
test: 300
},
list: [{
data: "xx",
test: 5
}, {
data: "xxx",
test: 2
}],
listinner: [{
data: ["x", "xxxx"]
}],
listquick: [{
data: "xxxxx"
}],
uin: 0
});
console.log("序列化JSON演示:", JSON.stringify(test.toObject()));
}
export function testQuickSerialization() {
console.log("快速序列化演示:", Buffer.from(ProtoBufQuick({ uin: PBUint32(1) }, { uin: 120 }).encode()).toString('hex'));
console.log("复杂快速序列化演示:", Buffer.from(ProtoBufQuick({ uin: ProtoBufIn(1, { data: PBBytes(5) }) },
{ uin: { data: new Uint8Array([0x125, 0x25]) } }).encode()).toString('hex'));
}
export function testFunctionSerialization() {
console.log("函数辅助序列化演示:", Buffer.from(createProtobuf(100, "test")).toString('hex'));
}
export function testFunctionDeserialization() {
let data_uin = PBUint32(1, false, 0);
let data_name = PBString(2, false, "");
ProtoBuf(class ProtoBufDataClass extends ProtoBufBase {
uin = data_uin;
name = data_name;
}).decode(createProtobuf(8000, "demo"));
console.log("函数辅助反序列化演示:", UnWrap(data_uin), UnWrap(data_name));
ProtoBuf(class ProtoBufDataClass extends ProtoBufBase {
uin = data_uin;
name = data_name;
}).decode(createProtobuf(7000, "test"));
console.log("函数辅助反序列化演示:", UnWrap(data_uin), UnWrap(data_name));
}
export function normalDecode() {
let data = new Uint8Array(Buffer.from('0a042a022525', 'hex'))
console.log("无protobuf盲解:", JSON.stringify(ProtoBufDecode(data), null, 2));
}
testSerialization();
testDeserialization();
testJsonSerialization();
testQuickSerialization();
testFunctionSerialization();
testFunctionDeserialization();
normalDecode();