Skip to content

Commit a6cc1c8

Browse files
committed
编排新增url/dns/buffer/zlib,新增arrayEditor组件,优化parseFunction
1 parent 54bb43d commit a6cc1c8

30 files changed

+3213
-242
lines changed

plugin/lib/quickcomposer.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ const quickcomposer = {
33
simulate: require("./quickcomposer/simulate"),
44
file: require("./quickcomposer/file"),
55
system: require("./quickcomposer/system"),
6+
network: require("./quickcomposer/network"),
7+
developer: require("./quickcomposer/developer"),
68
};
79

810
module.exports = quickcomposer;
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
// 创建 Buffer
2+
function from(data, encoding = "utf8") {
3+
try {
4+
return Buffer.from(data, encoding);
5+
} catch (error) {
6+
throw new Error(`创建Buffer失败: ${error.message}`);
7+
}
8+
}
9+
10+
// 转换为字符串
11+
function toString(buffer, encoding = "utf8", start = 0, end = buffer.length) {
12+
try {
13+
return buffer.toString(encoding, start, end);
14+
} catch (error) {
15+
throw new Error(`转换字符串失败: ${error.message}`);
16+
}
17+
}
18+
19+
// 写入数据
20+
function write(
21+
buffer,
22+
string,
23+
offset = 0,
24+
length = buffer.length,
25+
encoding = "utf8"
26+
) {
27+
try {
28+
return buffer.write(string, offset, length, encoding);
29+
} catch (error) {
30+
throw new Error(`写入数据失败: ${error.message}`);
31+
}
32+
}
33+
34+
// 填充数据
35+
function fill(
36+
buffer,
37+
value,
38+
offset = 0,
39+
end = buffer.length,
40+
encoding = "utf8"
41+
) {
42+
try {
43+
return buffer.fill(value, offset, end, encoding);
44+
} catch (error) {
45+
throw new Error(`填充数据失败: ${error.message}`);
46+
}
47+
}
48+
49+
// 复制数据
50+
function copy(
51+
source,
52+
target,
53+
targetStart = 0,
54+
sourceStart = 0,
55+
sourceEnd = source.length
56+
) {
57+
try {
58+
return source.copy(target, targetStart, sourceStart, sourceEnd);
59+
} catch (error) {
60+
throw new Error(`复制数据失败: ${error.message}`);
61+
}
62+
}
63+
64+
// 比较数据
65+
function compare(buf1, buf2) {
66+
try {
67+
return Buffer.compare(buf1, buf2);
68+
} catch (error) {
69+
throw new Error(`比较数据失败: ${error.message}`);
70+
}
71+
}
72+
73+
// 连接 Buffer
74+
function concat(buffers, totalLength) {
75+
try {
76+
return Buffer.concat(buffers, totalLength);
77+
} catch (error) {
78+
throw new Error(`连接Buffer失败: ${error.message}`);
79+
}
80+
}
81+
82+
// 查找数据
83+
function indexOf(buffer, value, byteOffset = 0, encoding = "utf8") {
84+
try {
85+
return buffer.indexOf(value, byteOffset, encoding);
86+
} catch (error) {
87+
throw new Error(`查找数据失败: ${error.message}`);
88+
}
89+
}
90+
91+
// 切片数据
92+
function slice(buffer, start = 0, end = buffer.length) {
93+
try {
94+
return buffer.slice(start, end);
95+
} catch (error) {
96+
throw new Error(`切片数据失败: ${error.message}`);
97+
}
98+
}
99+
100+
// 交换字节序
101+
function swap(buffer, size) {
102+
try {
103+
switch (size) {
104+
case 16:
105+
return buffer.swap16();
106+
case 32:
107+
return buffer.swap32();
108+
case 64:
109+
return buffer.swap64();
110+
default:
111+
throw new Error("不支持的字节大小");
112+
}
113+
} catch (error) {
114+
throw new Error(`交换字节序失败: ${error.message}`);
115+
}
116+
}
117+
118+
module.exports = {
119+
from,
120+
toString,
121+
write,
122+
fill,
123+
copy,
124+
compare,
125+
concat,
126+
indexOf,
127+
slice,
128+
swap,
129+
// 编码类型
130+
encodings: [
131+
"utf8",
132+
"utf16le",
133+
"latin1",
134+
"base64",
135+
"hex",
136+
"ascii",
137+
"binary",
138+
"ucs2",
139+
],
140+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
buffer: require("./buffer"),
3+
};
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const operation = require("./operation");
2+
const zlib = require("./zlib");
23

34
module.exports = {
45
operation: operation.operation,
6+
zlib: zlib,
57
};
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
const zlib = require("zlib");
2+
const { promisify } = require("util");
3+
4+
// 压缩方法
5+
const gzip = promisify(zlib.gzip);
6+
const deflate = promisify(zlib.deflate);
7+
const brotliCompress = promisify(zlib.brotliCompress);
8+
9+
// 解压方法
10+
const gunzip = promisify(zlib.gunzip);
11+
const inflate = promisify(zlib.inflate);
12+
const brotliDecompress = promisify(zlib.brotliDecompress);
13+
14+
// 压缩选项
15+
const defaultGzipOptions = {
16+
level: zlib.constants.Z_DEFAULT_COMPRESSION,
17+
memLevel: zlib.constants.Z_DEFAULT_MEMLEVEL,
18+
strategy: zlib.constants.Z_DEFAULT_STRATEGY,
19+
};
20+
21+
const defaultBrotliOptions = {
22+
params: {
23+
[zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_GENERIC,
24+
[zlib.constants.BROTLI_PARAM_QUALITY]:
25+
zlib.constants.BROTLI_DEFAULT_QUALITY,
26+
[zlib.constants.BROTLI_PARAM_SIZE_HINT]: 0,
27+
},
28+
};
29+
30+
// 异步压缩函数
31+
async function compressData(data, method, options = {}) {
32+
try {
33+
const buffer = Buffer.from(data);
34+
switch (method) {
35+
case "gzip":
36+
return await gzip(buffer, { ...defaultGzipOptions, ...options });
37+
case "deflate":
38+
return await deflate(buffer, { ...defaultGzipOptions, ...options });
39+
case "brotli":
40+
return await brotliCompress(buffer, {
41+
...defaultBrotliOptions,
42+
...options,
43+
});
44+
default:
45+
throw new Error("不支持的压缩方法");
46+
}
47+
} catch (error) {
48+
throw new Error(`压缩失败: ${error.message}`);
49+
}
50+
}
51+
52+
// 异步解压函数
53+
async function decompressData(data, method, options = {}) {
54+
try {
55+
const buffer = Buffer.from(data);
56+
switch (method) {
57+
case "gzip":
58+
return await gunzip(buffer, options);
59+
case "deflate":
60+
return await inflate(buffer, options);
61+
case "brotli":
62+
return await brotliDecompress(buffer, options);
63+
default:
64+
throw new Error("不支持的解压方法");
65+
}
66+
} catch (error) {
67+
throw new Error(`解压失败: ${error.message}`);
68+
}
69+
}
70+
71+
module.exports = {
72+
compressData,
73+
decompressData,
74+
constants: zlib.constants,
75+
};
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
const dns = require("dns");
2+
const { promisify } = require("util");
3+
4+
// 将回调函数转换为 Promise
5+
const lookup = promisify(dns.lookup);
6+
const resolve = promisify(dns.resolve);
7+
const resolve4 = promisify(dns.resolve4);
8+
const resolve6 = promisify(dns.resolve6);
9+
const resolveMx = promisify(dns.resolveMx);
10+
const resolveTxt = promisify(dns.resolveTxt);
11+
const resolveNs = promisify(dns.resolveNs);
12+
const resolveCname = promisify(dns.resolveCname);
13+
const reverse = promisify(dns.reverse);
14+
15+
// 解析主机名
16+
async function lookupHost(hostname, options = {}) {
17+
try {
18+
return await lookup(hostname, options);
19+
} catch (error) {
20+
throw new Error(`DNS查询失败: ${error.message}`);
21+
}
22+
}
23+
24+
// 解析所有记录
25+
async function resolveAll(hostname) {
26+
try {
27+
return await resolve(hostname);
28+
} catch (error) {
29+
throw new Error(`DNS解析失败: ${error.message}`);
30+
}
31+
}
32+
33+
// 解析 IPv4 地址
34+
async function resolveIpv4(hostname) {
35+
try {
36+
return await resolve4(hostname);
37+
} catch (error) {
38+
throw new Error(`IPv4解析失败: ${error.message}`);
39+
}
40+
}
41+
42+
// 解析 IPv6 地址
43+
async function resolveIpv6(hostname) {
44+
try {
45+
return await resolve6(hostname);
46+
} catch (error) {
47+
throw new Error(`IPv6解析失败: ${error.message}`);
48+
}
49+
}
50+
51+
// 解析 MX 记录
52+
async function resolveMxRecords(hostname) {
53+
try {
54+
return await resolveMx(hostname);
55+
} catch (error) {
56+
throw new Error(`MX记录解析失败: ${error.message}`);
57+
}
58+
}
59+
60+
// 解析 TXT 记录
61+
async function resolveTxtRecords(hostname) {
62+
try {
63+
return await resolveTxt(hostname);
64+
} catch (error) {
65+
throw new Error(`TXT记录解析失败: ${error.message}`);
66+
}
67+
}
68+
69+
// 解析 NS 记录
70+
async function resolveNsRecords(hostname) {
71+
try {
72+
return await resolveNs(hostname);
73+
} catch (error) {
74+
throw new Error(`NS记录解析失败: ${error.message}`);
75+
}
76+
}
77+
78+
// 解析 CNAME 记录
79+
async function resolveCnameRecords(hostname) {
80+
try {
81+
return await resolveCname(hostname);
82+
} catch (error) {
83+
throw new Error(`CNAME记录解析失败: ${error.message}`);
84+
}
85+
}
86+
87+
// 反向解析 IP 地址
88+
async function reverseResolve(ip) {
89+
try {
90+
return await reverse(ip);
91+
} catch (error) {
92+
throw new Error(`反向解析失败: ${error.message}`);
93+
}
94+
}
95+
96+
module.exports = {
97+
lookupHost,
98+
resolveAll,
99+
resolveIpv4,
100+
resolveIpv6,
101+
resolveMxRecords,
102+
resolveTxtRecords,
103+
resolveNsRecords,
104+
resolveCnameRecords,
105+
reverseResolve,
106+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
url: require("./url"),
3+
dns: require("./dns"),
4+
};

0 commit comments

Comments
 (0)