-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathencoder.js
47 lines (41 loc) · 1.52 KB
/
encoder.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
import {jsEncoder} from '../libs/drpyS.js';
// 仅仅支持json post 如: {"type":"gzip","code":"xxx"}
export default (fastify, options, done) => {
// 注册 POST 路由
fastify.post('/encoder', async (request, reply) => {
const {code, type} = request.body;
if (!code || !type) {
return reply.status(400).send({error: 'Missing required parameters: code and type'});
}
// 检查文本大小
const textSize = Buffer.byteLength(code, 'utf8'); // 获取 UTF-8 编码的字节大小
if (textSize > options.MAX_TEXT_SIZE) {
return reply
.status(400)
.send({error: `Text content exceeds the maximum size of ${options.MAX_TEXT_SIZE / 1024} KB`});
}
try {
let result;
switch (type) {
case 'base64':
result = jsEncoder.base64Encode(code);
break;
case 'gzip':
result = jsEncoder.gzip(code);
break;
case 'aes':
result = jsEncoder.aes_encrypt(code);
break;
case 'rsa':
result = jsEncoder.rsa_encode(code);
break;
default:
throw new Error(`Unsupported type: ${type}`);
}
reply.send({success: true, result});
} catch (error) {
reply.status(500).send({error: error.message});
}
});
done();
};