file-handle 0.0.6 → 0.0.7
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 +3 -2
- package/server/bin/cli.js +14 -5
- package/server/bin/fork.js +18 -22
- package/server/common/i18n.js +12 -6
- package/server/config.js +1 -0
- package/server/fh.app.dat +0 -0
- package/server/index.js +19 -1
- package/server/utils/discover.js +336 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "file-handle",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.7",
|
|
4
4
|
"author": "jsonhandle@gmail.com",
|
|
5
5
|
"description": "A simple local web file server with a user-friendly interface, allowing easy file upload, download, preview, and management through a browser. \n一个简易的文件服务器,方便通过浏览器来浏览、上传或管理目录和文件. ",
|
|
6
6
|
"main": "server/index.js",
|
|
@@ -12,7 +12,8 @@
|
|
|
12
12
|
"upload",
|
|
13
13
|
"download",
|
|
14
14
|
"preview",
|
|
15
|
-
"
|
|
15
|
+
"transfer",
|
|
16
|
+
"share",
|
|
16
17
|
"serve",
|
|
17
18
|
"web-server",
|
|
18
19
|
"http-server"
|
package/server/bin/cli.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import fs from 'fs';
|
|
4
|
+
import { fileURLToPath } from 'url';
|
|
2
5
|
import yargs from 'yargs';
|
|
3
6
|
import { hideBin } from 'yargs/helpers';
|
|
4
|
-
import
|
|
5
|
-
import { startServer } from "../index.js";
|
|
7
|
+
import { start } from './fork.js';
|
|
6
8
|
import { getText } from '../common/i18n.js';
|
|
7
|
-
import packageData from '../../package.json' with { type: "json" };
|
|
8
9
|
import { parseFileSize } from '../config.js';
|
|
9
10
|
const cmdConfig = loadCmdArgs(process.argv);
|
|
10
|
-
|
|
11
|
+
start(cmdConfig);
|
|
11
12
|
// 加载命令行参数
|
|
12
13
|
function loadCmdArgs(args) {
|
|
13
14
|
const lang = getText();
|
|
@@ -41,6 +42,11 @@ function loadCmdArgs(args) {
|
|
|
41
42
|
type: 'array',
|
|
42
43
|
description: lang.config.tlsDesc
|
|
43
44
|
},
|
|
45
|
+
't': {
|
|
46
|
+
alias: 'time',
|
|
47
|
+
type: 'number',
|
|
48
|
+
description: lang.config.timeDesc
|
|
49
|
+
},
|
|
44
50
|
'del': {
|
|
45
51
|
type: 'boolean',
|
|
46
52
|
description: lang.config.delDesc,
|
|
@@ -79,6 +85,8 @@ function loadCmdArgs(args) {
|
|
|
79
85
|
.help()
|
|
80
86
|
.argv;
|
|
81
87
|
if ('V' in argv || 'v' in argv) {
|
|
88
|
+
const mdDir = path.dirname(fileURLToPath(import.meta.url));
|
|
89
|
+
const packageData = JSON.parse(fs.readFileSync(path.join(mdDir, '../../package.json')).toString());
|
|
82
90
|
console.log(`v${packageData.version}`);
|
|
83
91
|
process.exit(0);
|
|
84
92
|
}
|
|
@@ -88,6 +96,7 @@ function loadCmdArgs(args) {
|
|
|
88
96
|
version: argv.version,
|
|
89
97
|
allowDelete: argv.del,
|
|
90
98
|
allowEdit: argv.edit,
|
|
99
|
+
time: argv.time,
|
|
91
100
|
writeLog: argv.log ? true : undefined,
|
|
92
101
|
password: argv.password,
|
|
93
102
|
uploadLimit: parseFileSize(argv.limit),
|
package/server/bin/fork.js
CHANGED
|
@@ -1,27 +1,23 @@
|
|
|
1
|
-
import { startServer } from
|
|
1
|
+
import { startServer } from '../index.js';
|
|
2
|
+
import { fork } from 'child_process';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import { loadConfig } from '../config.js';
|
|
2
5
|
import Debug from 'debug';
|
|
3
6
|
const debug = Debug('FH fork');
|
|
4
7
|
process.on('message', (msg) => {
|
|
5
|
-
|
|
6
|
-
if (msg && msg.cmd) {
|
|
7
|
-
switch (msg.cmd) {
|
|
8
|
-
case 'startServer':
|
|
9
|
-
startServer(msg.data).then(config => {
|
|
10
|
-
if (process.send) {
|
|
11
|
-
process.send({ cmd: 'started', data: config });
|
|
12
|
-
}
|
|
13
|
-
}).catch(err => {
|
|
14
|
-
if (process.send) {
|
|
15
|
-
process.send({ cmd: 'error', data: err.message });
|
|
16
|
-
}
|
|
17
|
-
});
|
|
18
|
-
break;
|
|
19
|
-
default:
|
|
20
|
-
break;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
8
|
+
startServer(JSON.parse(msg.opt));
|
|
23
9
|
});
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
10
|
+
export function start(config) {
|
|
11
|
+
const env = loadConfig({ rootDir: process.cwd() });
|
|
12
|
+
const forkEnter = path.join(env.mdDir, './bin/fork.js');
|
|
13
|
+
const child = fork(forkEnter, [], {
|
|
14
|
+
execArgv: ['--title=File-Handle-Server']
|
|
15
|
+
});
|
|
16
|
+
child.on('message', (msg) => {
|
|
17
|
+
debug('child:', msg);
|
|
18
|
+
});
|
|
19
|
+
child.send({ opt: JSON.stringify(config) });
|
|
20
|
+
child.on('exit', (code) => {
|
|
21
|
+
debug(`FH exited with code ${code}`);
|
|
22
|
+
});
|
|
27
23
|
}
|
package/server/common/i18n.js
CHANGED
|
@@ -7,15 +7,17 @@ const i18n = {
|
|
|
7
7
|
switchLang: 'Switch to English'
|
|
8
8
|
},
|
|
9
9
|
server: {
|
|
10
|
-
startingHttps: '正在启动HTTPS服务器...',
|
|
11
|
-
startingHttp: '正在启动HTTP服务器...',
|
|
10
|
+
startingHttps: '正在启动 FH HTTPS 服务器...',
|
|
11
|
+
startingHttp: '正在启动 FH HTTP 服务器...',
|
|
12
12
|
running: 'File-Handle Server正在运行,端口:',
|
|
13
13
|
rootDirectory: '根目录:',
|
|
14
14
|
accessibleAddresses: '可访问的地址:',
|
|
15
15
|
allowedIpsOnly: '仅允许指定IP访问',
|
|
16
16
|
localAndLanOnly: '仅允许本地和局域网访问',
|
|
17
17
|
loginPassword: '登录密码是:',
|
|
18
|
-
portInUse: '端口 {port} 正在使用中, 正在尝试下一个端口。'
|
|
18
|
+
portInUse: '端口 {port} 正在使用中, 正在尝试下一个端口。',
|
|
19
|
+
exitAfterTime: '服务器将在 {time} 秒后退出',
|
|
20
|
+
exitOutTime: '服务器到时退出'
|
|
19
21
|
},
|
|
20
22
|
config: {
|
|
21
23
|
allowWanDesc: '允许公网访问',
|
|
@@ -23,6 +25,7 @@ const i18n = {
|
|
|
23
25
|
portDesc: '指定服务器监听的端口号',
|
|
24
26
|
ipDesc: '指定服务器允许访问的IP地址,多个IP地址用逗号分隔',
|
|
25
27
|
tlsDesc: '启用HTTPS安全传输,需提供证书和密钥文件路径',
|
|
28
|
+
timeDesc: '启动多少秒后退出',
|
|
26
29
|
delDesc: '启用文件删除功能',
|
|
27
30
|
passwordDesc: '设置服务器访问的密码',
|
|
28
31
|
editDesc: '启用文件编辑修改功能',
|
|
@@ -39,15 +42,17 @@ const i18n = {
|
|
|
39
42
|
switchLang: '切换到中文'
|
|
40
43
|
},
|
|
41
44
|
server: {
|
|
42
|
-
startingHttps: 'Starting HTTPS server...',
|
|
43
|
-
startingHttp: 'Starting HTTP server...',
|
|
45
|
+
startingHttps: 'Starting FH HTTPS server...',
|
|
46
|
+
startingHttp: 'Starting FH HTTP server...',
|
|
44
47
|
running: 'File-Handle Server is running on port',
|
|
45
48
|
rootDirectory: 'Root directory:',
|
|
46
49
|
accessibleAddresses: 'Accessible addresses:',
|
|
47
50
|
allowedIpsOnly: 'Only allowed IPs can access',
|
|
48
51
|
localAndLanOnly: 'Only local and LAN access allowed',
|
|
49
52
|
loginPassword: 'Login password is:',
|
|
50
|
-
portInUse: 'Port {port} is being used, trying next port...'
|
|
53
|
+
portInUse: 'Port {port} is being used, trying next port...',
|
|
54
|
+
exitAfterTime: 'Server will exit after {time} seconds',
|
|
55
|
+
exitOutTime: 'The server has expired and exited'
|
|
51
56
|
},
|
|
52
57
|
config: {
|
|
53
58
|
allowWanDesc: 'Allow public network access',
|
|
@@ -55,6 +60,7 @@ const i18n = {
|
|
|
55
60
|
portDesc: 'Specify server listening port',
|
|
56
61
|
ipDesc: 'Specify allowed IP addresses, separated by commas',
|
|
57
62
|
tlsDesc: 'Enable HTTPS secure transmission, requires certificate and key file paths',
|
|
63
|
+
timeDesc: 'How many seconds does it exit',
|
|
58
64
|
delDesc: 'Enable file deletion',
|
|
59
65
|
passwordDesc: 'Set server access password',
|
|
60
66
|
editDesc: 'Enable file editing',
|
package/server/config.js
CHANGED
package/server/fh.app.dat
CHANGED
|
Binary file
|
package/server/index.js
CHANGED
|
@@ -66,7 +66,6 @@ export async function startServer(opt) {
|
|
|
66
66
|
const port = await findAvailablePort(config.port);
|
|
67
67
|
const localIPs = getLocalNetworkIPs();
|
|
68
68
|
global.config.port = port;
|
|
69
|
-
global.config = Object.freeze(global.config);
|
|
70
69
|
return new Promise((resolve, reject) => {
|
|
71
70
|
if (config.allowedIPs) {
|
|
72
71
|
// 如果指定了允许访问的IP地址,则只允许这些IP访问
|
|
@@ -78,6 +77,7 @@ export async function startServer(opt) {
|
|
|
78
77
|
info(color.green(`http://127.0.0.1:${port}/fh/`));
|
|
79
78
|
config.allowedIPs && config.allowedIPs.forEach(ip => {
|
|
80
79
|
info(color.green(`http://${ip}:${port}/fh/`));
|
|
80
|
+
config.ipList?.push(ip);
|
|
81
81
|
});
|
|
82
82
|
info('\n\n');
|
|
83
83
|
});
|
|
@@ -92,6 +92,7 @@ export async function startServer(opt) {
|
|
|
92
92
|
info(color.green(`http://127.0.0.1:${port}/fh/`));
|
|
93
93
|
localIPs.forEach(ip => {
|
|
94
94
|
info(color.green(`http://${ip}:${port}/fh/`));
|
|
95
|
+
config.ipList?.push(ip);
|
|
95
96
|
});
|
|
96
97
|
info('\n\n');
|
|
97
98
|
});
|
|
@@ -104,12 +105,29 @@ export async function startServer(opt) {
|
|
|
104
105
|
info(lang.server.accessibleAddresses);
|
|
105
106
|
info(`http://0.0.0.0:${port}/fh/`);
|
|
106
107
|
info(color.green(`http://127.0.0.1:${port}/fh/`));
|
|
108
|
+
localIPs.forEach(ip => {
|
|
109
|
+
config.ipList?.push(ip);
|
|
110
|
+
});
|
|
107
111
|
info(`\n\n`);
|
|
108
112
|
});
|
|
109
113
|
}
|
|
110
114
|
if (config.password) {
|
|
111
115
|
info(`${lang.server.loginPassword}${color.green(config.password)}`);
|
|
112
116
|
}
|
|
117
|
+
}).then(_ => {
|
|
118
|
+
// 对ipList进行去重
|
|
119
|
+
if (global.config.ipList) {
|
|
120
|
+
global.config.ipList = [...new Set(global.config.ipList)];
|
|
121
|
+
}
|
|
122
|
+
global.config = Object.freeze(global.config);
|
|
123
|
+
if (config.time) {
|
|
124
|
+
// @ts-ignore
|
|
125
|
+
info(lang.server.exitAfterTime.replace('{time}', color.red(config.time.toString())));
|
|
126
|
+
setTimeout(() => {
|
|
127
|
+
info(lang.server.exitOutTime);
|
|
128
|
+
process.exit(0);
|
|
129
|
+
}, config.time * 1000);
|
|
130
|
+
}
|
|
113
131
|
});
|
|
114
132
|
}
|
|
115
133
|
async function findAvailablePort(startPort, maxAttempts = 50) {
|
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
import exhaust from 'exhaust';
|
|
2
|
+
// 36进制字符集
|
|
3
|
+
const CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
4
|
+
const oIndexs = {
|
|
5
|
+
1: () => {
|
|
6
|
+
// 18, 2, 1
|
|
7
|
+
const r = oIndexs.build([buildArrFrom(0, 17), [0, 1], [6688]]);
|
|
8
|
+
oIndexs[1] = () => r;
|
|
9
|
+
return r;
|
|
10
|
+
},
|
|
11
|
+
2: (p) => {
|
|
12
|
+
// 7, 3, 1
|
|
13
|
+
// 25, 5, 2
|
|
14
|
+
const r = oIndexs.build([buildArrFrom(18, 24), [2, 3, 4], [6689]], p);
|
|
15
|
+
oIndexs[2] = () => r;
|
|
16
|
+
return r;
|
|
17
|
+
},
|
|
18
|
+
3: (p) => {
|
|
19
|
+
// 13, 4, 1
|
|
20
|
+
// 48, 9, 3
|
|
21
|
+
const r = oIndexs.build([buildArrFrom(25, 47), buildArrFrom(5, 8), [6690]], p);
|
|
22
|
+
oIndexs[3] = () => r;
|
|
23
|
+
return r;
|
|
24
|
+
},
|
|
25
|
+
4: (p) => {
|
|
26
|
+
// 13, 36, 1
|
|
27
|
+
// 255, 45, 4
|
|
28
|
+
const r = oIndexs.build([buildArrFrom(48, 255), buildArrFrom(9, 24).concat([30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 253, 254]), [6691]], p);
|
|
29
|
+
oIndexs[4] = () => r;
|
|
30
|
+
return r;
|
|
31
|
+
},
|
|
32
|
+
5: () => {
|
|
33
|
+
},
|
|
34
|
+
build: (rangs, p) => {
|
|
35
|
+
let oIndex = {};
|
|
36
|
+
let i = 0;
|
|
37
|
+
if (p) {
|
|
38
|
+
i = p.oIndex.lastIndex + 1;
|
|
39
|
+
oIndex = Object.create(p.oIndex);
|
|
40
|
+
}
|
|
41
|
+
exhaust((may) => `${may(rangs[0])},${may(rangs[1])},${may(rangs[2])}`).forEach((s) => {
|
|
42
|
+
oIndex[s] = i++;
|
|
43
|
+
});
|
|
44
|
+
let mergeRangs = rangs;
|
|
45
|
+
if (p) {
|
|
46
|
+
let [a1, a2, a3] = p.rangs;
|
|
47
|
+
const [b1, b2, b3] = rangs;
|
|
48
|
+
mergeRangs = [a1.concat(b1), a2.concat(b2), a3.concat(b3)];
|
|
49
|
+
exhaust((may) => `${may(mergeRangs[0])},${may(mergeRangs[1])},${may(mergeRangs[2])}`).forEach((s) => {
|
|
50
|
+
if (!(s in oIndex)) {
|
|
51
|
+
oIndex[s] = i++;
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
oIndex.lastIndex = i - 1;
|
|
56
|
+
const r = {
|
|
57
|
+
rangs: mergeRangs,
|
|
58
|
+
oIndex,
|
|
59
|
+
check: (a, b, c) => {
|
|
60
|
+
return mergeRangs[0].includes(a) && mergeRangs[1].includes(b) && mergeRangs[2].includes(c);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
return r;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
function buideIndex(lv) {
|
|
67
|
+
let oIndex = oIndexs[1]();
|
|
68
|
+
for (let i = 2; i <= lv; i++) {
|
|
69
|
+
oIndex = oIndexs[i](oIndex);
|
|
70
|
+
}
|
|
71
|
+
return oIndex;
|
|
72
|
+
}
|
|
73
|
+
function encode(a, b, c) {
|
|
74
|
+
// 检查输入范围
|
|
75
|
+
if (a < 0 || a > 255 || b < 0 || b > 255 || c < 1 || c > 65536) {
|
|
76
|
+
throw new Error('输入数值超出范围');
|
|
77
|
+
}
|
|
78
|
+
// 高频区域: 1个字符
|
|
79
|
+
let oIndexConfig = buideIndex(1);
|
|
80
|
+
if (oIndexConfig.check(a, b, c)) {
|
|
81
|
+
const index = oIndexConfig.oIndex[[a, b, c].toString()];
|
|
82
|
+
return numToCode(index);
|
|
83
|
+
}
|
|
84
|
+
// 中频区域: 2个字符
|
|
85
|
+
oIndexConfig = buideIndex(2);
|
|
86
|
+
if (oIndexConfig.check(a, b, c)) {
|
|
87
|
+
const index = oIndexConfig.oIndex[[a, b, c].toString()];
|
|
88
|
+
return numToCode(index);
|
|
89
|
+
}
|
|
90
|
+
// 低频区域: 2个字符
|
|
91
|
+
oIndexConfig = buideIndex(3);
|
|
92
|
+
if (oIndexConfig.check(a, b, c)) {
|
|
93
|
+
const index = oIndexConfig.oIndex[[a, b, c].toString()];
|
|
94
|
+
return numToCode(index);
|
|
95
|
+
}
|
|
96
|
+
// 罕见区域: 3个字符
|
|
97
|
+
oIndexConfig = buideIndex(4);
|
|
98
|
+
if (oIndexConfig.check(a, b, c)) {
|
|
99
|
+
const index = oIndexConfig.oIndex[[a, b, c].toString()];
|
|
100
|
+
return numToCode(index);
|
|
101
|
+
}
|
|
102
|
+
return encodeBig(a, b, c);
|
|
103
|
+
}
|
|
104
|
+
function decode(str) {
|
|
105
|
+
if (!str || str.length === 0) {
|
|
106
|
+
throw new Error('输入字符串不能为空');
|
|
107
|
+
}
|
|
108
|
+
if (!/^[0-9A-Z]+$/.test(str)) {
|
|
109
|
+
throw new Error('无效的编码字符串 of [0-9A-Z]');
|
|
110
|
+
}
|
|
111
|
+
const index = codeToNum(str);
|
|
112
|
+
// 高频区域: 1个字符
|
|
113
|
+
if (str.length < 3) {
|
|
114
|
+
const oIndex = buideIndex(2).oIndex;
|
|
115
|
+
for (const key in oIndex) {
|
|
116
|
+
if (oIndex[key] === index) {
|
|
117
|
+
return key.split(',').map(Number);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
// 中低频区域: 2个字符
|
|
122
|
+
if (str.length < 3) {
|
|
123
|
+
const oIndex = buideIndex(3).oIndex;
|
|
124
|
+
for (const key in oIndex) {
|
|
125
|
+
if (oIndex[key] === index) {
|
|
126
|
+
return key.split(',').map(Number);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
// 罕见区域: 3个字符
|
|
131
|
+
if (str.length < 4) {
|
|
132
|
+
const oIndex = buideIndex(4).oIndex;
|
|
133
|
+
for (const key in oIndex) {
|
|
134
|
+
if (oIndex[key] === index) {
|
|
135
|
+
return key.split(',').map(Number);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return decodeBig(str);
|
|
140
|
+
}
|
|
141
|
+
function encodeBig(a, b, c) {
|
|
142
|
+
// 验证输入范围
|
|
143
|
+
if (a < 0 || a > 255 || b < 0 || b > 255 || c < 1 || c > 65536) {
|
|
144
|
+
throw new Error('Invalid input parameters. Expected a:0-255, b:0-255, c:1-65536');
|
|
145
|
+
}
|
|
146
|
+
// 计算组合值
|
|
147
|
+
const combined = a * 0x1000000 + b * 0x10000 + (c - 1);
|
|
148
|
+
// 转换为36进制并大写,补齐7位
|
|
149
|
+
return combined.toString(36).toUpperCase().padStart(7, '0');
|
|
150
|
+
}
|
|
151
|
+
function decodeBig(str) {
|
|
152
|
+
// 验证输入字符串
|
|
153
|
+
if (!str || str.length !== 7 || !/^[0-9A-Z]+$/.test(str)) {
|
|
154
|
+
throw new Error('Invalid encoded string: must be 7 characters of [0-9A-Z]');
|
|
155
|
+
}
|
|
156
|
+
// 转换为小写并解析
|
|
157
|
+
const combined = parseInt(str.toLowerCase(), 36);
|
|
158
|
+
if (isNaN(combined)) {
|
|
159
|
+
throw new Error('Invalid encoded string');
|
|
160
|
+
}
|
|
161
|
+
// 验证组合值范围
|
|
162
|
+
if (combined < 0 || combined > 0xFFFFFFFF) {
|
|
163
|
+
throw new Error('Encoded value out of valid range');
|
|
164
|
+
}
|
|
165
|
+
// 分解各部分的数值
|
|
166
|
+
const a = (combined >>> 24) & 0xFF;
|
|
167
|
+
const b = (combined >>> 16) & 0xFF;
|
|
168
|
+
const c = (combined & 0xFFFF) + 1; // 恢复c的原始值
|
|
169
|
+
// 验证分解后的参数范围
|
|
170
|
+
if (a < 0 || a > 255 || b < 0 || b > 255 || c < 1 || c > 65536) {
|
|
171
|
+
throw new Error('Decoded values out of range');
|
|
172
|
+
}
|
|
173
|
+
return [a, b, c];
|
|
174
|
+
}
|
|
175
|
+
// 将十进制数转换为36进制字符串
|
|
176
|
+
function numToCode(num) {
|
|
177
|
+
let result = '';
|
|
178
|
+
if (num === 0) {
|
|
179
|
+
return '0';
|
|
180
|
+
}
|
|
181
|
+
while (num > 0) {
|
|
182
|
+
const remainder = num % 36;
|
|
183
|
+
result = CHARS[remainder] + result;
|
|
184
|
+
num = Math.floor(num / 36);
|
|
185
|
+
}
|
|
186
|
+
return result;
|
|
187
|
+
}
|
|
188
|
+
// 将36进制字符串转换为十进制数
|
|
189
|
+
function codeToNum(code) {
|
|
190
|
+
let result = 0;
|
|
191
|
+
for (let i = 0; i < code.length; i++) {
|
|
192
|
+
const char = code[i];
|
|
193
|
+
const value = CHARS.indexOf(char);
|
|
194
|
+
result = result * 36 + value;
|
|
195
|
+
}
|
|
196
|
+
return result;
|
|
197
|
+
}
|
|
198
|
+
// 生成一个范围数组
|
|
199
|
+
function buildArrFrom(start, end) {
|
|
200
|
+
const arr = [];
|
|
201
|
+
for (let i = start; i <= end; i++) {
|
|
202
|
+
arr.push(i);
|
|
203
|
+
}
|
|
204
|
+
return arr;
|
|
205
|
+
}
|
|
206
|
+
/*
|
|
207
|
+
// 测试代码
|
|
208
|
+
function test() {
|
|
209
|
+
describe('编码解码测试', () => {
|
|
210
|
+
// 高频区域测试
|
|
211
|
+
it('高频区域编码解码', () => {
|
|
212
|
+
// 测试边界值
|
|
213
|
+
const testCases = [
|
|
214
|
+
[ 0, 0, 6688 ],
|
|
215
|
+
[ 1, 0, 6688 ],
|
|
216
|
+
[ 0, 1, 6688 ],
|
|
217
|
+
[ 1, 1, 6688 ],
|
|
218
|
+
[ 3, 1, 6688 ],
|
|
219
|
+
[ 4, 1, 6688 ],
|
|
220
|
+
[ 5, 1, 6688 ],
|
|
221
|
+
[ 6, 1, 6688 ],
|
|
222
|
+
[ 7, 1, 6688 ]
|
|
223
|
+
];
|
|
224
|
+
|
|
225
|
+
testCases.forEach(([ a, b, c ]) => {
|
|
226
|
+
const encoded = encode(a, b, c);
|
|
227
|
+
assert.strictEqual(encoded.length, 1, '高频区域编码应为1个字符');
|
|
228
|
+
const decoded = decode(encoded);
|
|
229
|
+
assert.deepStrictEqual(decoded, [ a, b, c ], '解码结果应与原始输入匹配');
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
// 中频区域测试
|
|
234
|
+
it('中频区域编码解码', () => {
|
|
235
|
+
const testCases = [
|
|
236
|
+
[ 19, 3, 6688 ],
|
|
237
|
+
[ 1, 0, 6689 ],
|
|
238
|
+
[ 0, 3, 6689 ],
|
|
239
|
+
[ 20, 4, 6689 ],
|
|
240
|
+
[ 5, 1, 6689 ],
|
|
241
|
+
[ 0, 4, 6688 ]
|
|
242
|
+
];
|
|
243
|
+
|
|
244
|
+
testCases.forEach(([ a, b, c ]) => {
|
|
245
|
+
const encoded = encode(a, b, c);
|
|
246
|
+
assert.strictEqual(encoded.length, 2, '中频区域编码应为2个字符');
|
|
247
|
+
const decoded = decode(encoded);
|
|
248
|
+
assert.deepStrictEqual(decoded, [ a, b, c ], '解码结果应与原始输入匹配');
|
|
249
|
+
});
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
// 低频区域测试
|
|
253
|
+
it('低频区域编码解码', () => {
|
|
254
|
+
const testCases = [
|
|
255
|
+
[ 19, 5, 6688 ],
|
|
256
|
+
[ 28, 0, 6689 ],
|
|
257
|
+
[ 0, 7, 6689 ],
|
|
258
|
+
[ 28, 5, 6689 ],
|
|
259
|
+
[ 5, 1, 6690 ],
|
|
260
|
+
[ 1, 1, 6690 ]
|
|
261
|
+
];
|
|
262
|
+
|
|
263
|
+
testCases.forEach(([ a, b, c ]) => {
|
|
264
|
+
const encoded = encode(a, b, c);
|
|
265
|
+
assert.strictEqual(encoded.length, 2, '低频区域编码应为2个字符');
|
|
266
|
+
const decoded = decode(encoded);
|
|
267
|
+
assert.deepStrictEqual(decoded, [ a, b, c ], '解码结果应与原始输入匹配');
|
|
268
|
+
});
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
// 罕见区域测试
|
|
272
|
+
it('罕见区域编码解码', () => {
|
|
273
|
+
const testCases = [
|
|
274
|
+
[ 49, 5, 6688 ],
|
|
275
|
+
[ 28, 9, 6689 ],
|
|
276
|
+
[ 232, 13, 6689 ],
|
|
277
|
+
[ 28, 150, 6689 ],
|
|
278
|
+
[ 5, 1, 6691 ],
|
|
279
|
+
[ 111, 254, 6691 ]
|
|
280
|
+
];
|
|
281
|
+
|
|
282
|
+
testCases.forEach(([ a, b, c ]) => {
|
|
283
|
+
const encoded = encode(a, b, c);
|
|
284
|
+
assert.strictEqual(encoded.length, 3, '罕见区域编码应为3个字符');
|
|
285
|
+
const decoded = decode(encoded);
|
|
286
|
+
assert.deepStrictEqual(decoded, [ a, b, c ], '解码结果应与原始输入匹配');
|
|
287
|
+
});
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
// 其它区域测试
|
|
291
|
+
it('其它区域编码解码', () => {
|
|
292
|
+
const testCases = [
|
|
293
|
+
[ 49, 5, 8688 ],
|
|
294
|
+
[ 28, 191, 8689 ],
|
|
295
|
+
[ 232, 188, 6689 ],
|
|
296
|
+
[ 28, 150, 3689 ],
|
|
297
|
+
[ 5, 1, 691 ],
|
|
298
|
+
[ 111, 121, 16691 ]
|
|
299
|
+
];
|
|
300
|
+
|
|
301
|
+
testCases.forEach(([ a, b, c ]) => {
|
|
302
|
+
const encoded = encode(a, b, c);
|
|
303
|
+
assert.strictEqual(encoded.length, 7, '其它区域编码应为7个字符');
|
|
304
|
+
const decoded = decode(encoded);
|
|
305
|
+
assert.deepStrictEqual(decoded, [ a, b, c ], '解码结果应与原始输入匹配');
|
|
306
|
+
});
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
// 边界条件测试
|
|
310
|
+
it('输入范围验证', () => {
|
|
311
|
+
assert.throws(() => encode(-1, 0, 1), Error, '应当检测到负数输入');
|
|
312
|
+
assert.throws(() => encode(256, 0, 1), Error, '应当检测到a超出范围');
|
|
313
|
+
assert.throws(() => encode(0, 256, 1), Error, '应当检测到b超出范围');
|
|
314
|
+
assert.throws(() => encode(0, 0, 0), Error, '应当检测到c小于1');
|
|
315
|
+
assert.throws(() => encode(0, 0, 65537), Error, '应当检测到c超出范围');
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
// 解码异常测试
|
|
319
|
+
it('解码异常验证', () => {
|
|
320
|
+
assert.throws(() => decode(''), Error, '应当检测到空字符串');
|
|
321
|
+
assert.throws(() => decode('Q-'), Error, '应当检测到无效的编码字符串');
|
|
322
|
+
});
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
function describe(title:string, fun:Function) {
|
|
328
|
+
console.log(title, '\n开始测试...\n');
|
|
329
|
+
fun();
|
|
330
|
+
console.log(title, '测试完成\n');
|
|
331
|
+
}
|
|
332
|
+
const it = describe;
|
|
333
|
+
|
|
334
|
+
test();
|
|
335
|
+
|
|
336
|
+
*/
|