@maiyunnet/kebab 2.0.16 → 3.0.0
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/README.md +1 -5
- package/index.d.ts +1 -1
- package/index.js +16 -18
- package/lib/buffer.js +4 -11
- package/lib/captcha.js +6 -44
- package/lib/consistent.js +8 -51
- package/lib/core.d.ts +4 -4
- package/lib/core.js +70 -132
- package/lib/crypto.js +43 -99
- package/lib/db.d.ts +5 -5
- package/lib/db.js +31 -68
- package/lib/dns.d.ts +1 -1
- package/lib/dns.js +12 -50
- package/lib/fs.js +26 -81
- package/lib/jwt.d.ts +3 -3
- package/lib/jwt.js +9 -50
- package/lib/kv.d.ts +4 -4
- package/lib/kv.js +31 -69
- package/lib/lan.js +4 -41
- package/lib/net/cacert.pem +78 -2
- package/lib/net/formdata.js +5 -42
- package/lib/net/request.d.ts +2 -2
- package/lib/net/request.js +3 -42
- package/lib/net/response.d.ts +1 -1
- package/lib/net/response.js +1 -5
- package/lib/net.d.ts +5 -5
- package/lib/net.js +24 -73
- package/lib/s3.d.ts +1 -1
- package/lib/s3.js +20 -59
- package/lib/scan.d.ts +3 -3
- package/lib/scan.js +10 -50
- package/lib/session.d.ts +3 -3
- package/lib/session.js +9 -46
- package/lib/sql.d.ts +2 -2
- package/lib/sql.js +9 -50
- package/lib/ssh/sftp.js +5 -46
- package/lib/ssh/shell.js +2 -39
- package/lib/ssh.d.ts +2 -2
- package/lib/ssh.js +5 -43
- package/lib/text.d.ts +1 -1
- package/lib/text.js +40 -104
- package/lib/time.d.ts +1 -1
- package/lib/time.js +10 -50
- package/lib/turnstile.d.ts +1 -1
- package/lib/turnstile.js +6 -43
- package/lib/ws.d.ts +2 -2
- package/lib/ws.js +14 -56
- package/lib/zip.d.ts +1 -1
- package/lib/zip.js +6 -47
- package/lib/zlib.js +17 -68
- package/main.js +6 -44
- package/package.json +9 -8
- package/sys/child.js +10 -45
- package/sys/cmd.js +8 -43
- package/sys/ctr.d.ts +5 -5
- package/sys/ctr.js +9 -47
- package/sys/master.js +11 -46
- package/sys/mod.d.ts +9 -5
- package/sys/mod.js +9 -43
- package/sys/route.d.ts +2 -2
- package/sys/route.js +24 -64
- package/types/index.d.ts +1 -0
- package/www/example/ctr/main.d.ts +1 -1
- package/www/example/ctr/main.js +2 -38
- package/www/example/ctr/middle.d.ts +2 -2
- package/www/example/ctr/middle.js +2 -38
- package/www/example/ctr/test.d.ts +2 -2
- package/www/example/ctr/test.js +65 -72
- package/www/example/mod/test.d.ts +2 -2
- package/www/example/mod/test.js +4 -42
- package/www/example/mod/testdata.d.ts +1 -1
- package/www/example/mod/testdata.js +3 -8
- package/www/example/ws/mproxy.d.ts +1 -1
- package/www/example/ws/mproxy.js +6 -41
- package/www/example/ws/rproxy.d.ts +1 -1
- package/www/example/ws/rproxy.js +5 -40
- package/www/example/ws/rsocket.d.ts +1 -1
- package/www/example/ws/rsocket.js +5 -40
- package/www/example/ws/test.d.ts +1 -1
- package/www/example/ws/test.js +7 -42
- package/eslint.config.js +0 -22
package/lib/crypto.js
CHANGED
|
@@ -1,75 +1,19 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.SM4_CFB = exports.SM4_CBC = exports.SM4_ECB = exports.AES_256_GCM = exports.AES_256_CFB = exports.AES_256_CBC = exports.AES_256_ECB = void 0;
|
|
37
|
-
exports.generateKeyPair = generateKeyPair;
|
|
38
|
-
exports.sign = sign;
|
|
39
|
-
exports.verify = verify;
|
|
40
|
-
exports.publicEncrypt = publicEncrypt;
|
|
41
|
-
exports.privateEncrypt = privateEncrypt;
|
|
42
|
-
exports.publicDecrypt = publicDecrypt;
|
|
43
|
-
exports.privateDecrypt = privateDecrypt;
|
|
44
|
-
exports.cipherEncrypt = cipherEncrypt;
|
|
45
|
-
exports.aesEncrypt = aesEncrypt;
|
|
46
|
-
exports.gcmEncrypt = gcmEncrypt;
|
|
47
|
-
exports.sm4Encrypt = sm4Encrypt;
|
|
48
|
-
exports.cipherDecrypt = cipherDecrypt;
|
|
49
|
-
exports.aesDecrypt = aesDecrypt;
|
|
50
|
-
exports.gcmDecrypt = gcmDecrypt;
|
|
51
|
-
exports.sm4Decrypt = sm4Decrypt;
|
|
52
|
-
exports.hashHmac = hashHmac;
|
|
53
|
-
exports.hashHmacFile = hashHmacFile;
|
|
54
|
-
exports.base64Encode = base64Encode;
|
|
55
|
-
exports.base64Decode = base64Decode;
|
|
56
|
-
exports.uuid = uuid;
|
|
57
1
|
/**
|
|
58
2
|
* Project: Kebab, User: JianSuoQiYue
|
|
59
3
|
* Date: 2019-4-2 14:01:06
|
|
60
4
|
* Last: 2020-3-12 14:05:24, 2022-09-12 11:52:35, 2024-9-8 17:09:39, 2024-11-11 00:21:58, 2025-6-18 20:27:47
|
|
61
5
|
*/
|
|
62
|
-
|
|
6
|
+
import * as crypto from 'crypto';
|
|
63
7
|
// --- 库和定义 ---
|
|
64
|
-
|
|
65
|
-
|
|
8
|
+
import * as lFs from '../lib/fs.js';
|
|
9
|
+
import * as lCore from '../lib/core.js';
|
|
66
10
|
// --- 非对称加密 ---
|
|
67
11
|
/**
|
|
68
12
|
* --- 创建非对称秘钥 ---
|
|
69
13
|
* @param type 如 rsa/ec
|
|
70
14
|
* @param options 参数
|
|
71
15
|
*/
|
|
72
|
-
function generateKeyPair(type, options = {}) {
|
|
16
|
+
export function generateKeyPair(type, options = {}) {
|
|
73
17
|
return new Promise((resolve) => {
|
|
74
18
|
options.modulusLength ??= 2048;
|
|
75
19
|
if (options.namedCurve !== undefined) {
|
|
@@ -102,7 +46,7 @@ function generateKeyPair(type, options = {}) {
|
|
|
102
46
|
* @param format 输出格式
|
|
103
47
|
* @param algorithm 哈希方式
|
|
104
48
|
*/
|
|
105
|
-
function sign(data, privateKey, format = 'buffer', algorithm = 'sha256') {
|
|
49
|
+
export function sign(data, privateKey, format = 'buffer', algorithm = 'sha256') {
|
|
106
50
|
const sign = crypto.createSign(algorithm);
|
|
107
51
|
sign.update(data);
|
|
108
52
|
return format === 'buffer' ? sign.sign(privateKey) : sign.sign(privateKey, format);
|
|
@@ -114,7 +58,7 @@ function sign(data, privateKey, format = 'buffer', algorithm = 'sha256') {
|
|
|
114
58
|
* @param signature 签名
|
|
115
59
|
* @param algorithm 哈希方式
|
|
116
60
|
*/
|
|
117
|
-
function verify(data, object, signature, algorithm = 'sha256') {
|
|
61
|
+
export function verify(data, object, signature, algorithm = 'sha256') {
|
|
118
62
|
const verify = crypto.createVerify(algorithm);
|
|
119
63
|
verify.update(data);
|
|
120
64
|
return verify.verify(object, signature);
|
|
@@ -124,7 +68,7 @@ function verify(data, object, signature, algorithm = 'sha256') {
|
|
|
124
68
|
* @param key 公钥
|
|
125
69
|
* @param buffer 数据
|
|
126
70
|
*/
|
|
127
|
-
function publicEncrypt(key, buffer) {
|
|
71
|
+
export function publicEncrypt(key, buffer) {
|
|
128
72
|
return crypto.publicEncrypt(key, buffer);
|
|
129
73
|
}
|
|
130
74
|
/**
|
|
@@ -132,7 +76,7 @@ function publicEncrypt(key, buffer) {
|
|
|
132
76
|
* @param key 私钥
|
|
133
77
|
* @param buffer 数据
|
|
134
78
|
*/
|
|
135
|
-
function privateEncrypt(key, buffer) {
|
|
79
|
+
export function privateEncrypt(key, buffer) {
|
|
136
80
|
return crypto.privateEncrypt(key, buffer);
|
|
137
81
|
}
|
|
138
82
|
/**
|
|
@@ -140,7 +84,7 @@ function privateEncrypt(key, buffer) {
|
|
|
140
84
|
* @param key 公钥
|
|
141
85
|
* @param buffer 数据
|
|
142
86
|
*/
|
|
143
|
-
function publicDecrypt(key, buffer) {
|
|
87
|
+
export function publicDecrypt(key, buffer) {
|
|
144
88
|
return crypto.publicDecrypt(key, buffer);
|
|
145
89
|
}
|
|
146
90
|
/**
|
|
@@ -148,17 +92,17 @@ function publicDecrypt(key, buffer) {
|
|
|
148
92
|
* @param key 私钥
|
|
149
93
|
* @param buffer 数据
|
|
150
94
|
*/
|
|
151
|
-
function privateDecrypt(key, buffer) {
|
|
95
|
+
export function privateDecrypt(key, buffer) {
|
|
152
96
|
return crypto.privateDecrypt(key, buffer);
|
|
153
97
|
}
|
|
154
98
|
// --- Cipher (AES/SM4...) 加/解密 ---
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
99
|
+
export const AES_256_ECB = 'aes-256-ecb'; // --- 如果未设置 iv,则默认这个,但强烈不建议 ---
|
|
100
|
+
export const AES_256_CBC = 'aes-256-cbc';
|
|
101
|
+
export const AES_256_CFB = 'aes-256-cfb'; // --- 设置 iv,自动就切换成了这个 ---
|
|
102
|
+
export const AES_256_GCM = 'aes-256-gcm'; // --- 强烈建议使用这个 ---
|
|
103
|
+
export const SM4_ECB = 'sm4-ecb'; // --- SM4 如果未设置 iv,则默认这个 ---
|
|
104
|
+
export const SM4_CBC = 'sm4-cbc';
|
|
105
|
+
export const SM4_CFB = 'sm4-cfb'; // --- SM4 一般用这个,设置 iv,自动就切换成了这个 ---
|
|
162
106
|
/**
|
|
163
107
|
* --- cipher 加密,强烈不建议使用 AES_256_ECB ---
|
|
164
108
|
* @param original 原始字符串
|
|
@@ -166,18 +110,18 @@ exports.SM4_CFB = 'sm4-cfb'; // --- SM4 一般用这个,设置 iv,自动就
|
|
|
166
110
|
* @param iv 向量 16(CFB) 或 12(GCM) 个英文字母和数字
|
|
167
111
|
* @param method 加密方法
|
|
168
112
|
*/
|
|
169
|
-
function cipherEncrypt(original, key, iv = '', method =
|
|
113
|
+
export function cipherEncrypt(original, key, iv = '', method = AES_256_ECB, output = 'base64') {
|
|
170
114
|
try {
|
|
171
115
|
if ((typeof key === 'string') && (key.length !== 32)) {
|
|
172
116
|
key = hashHmac('md5', key, 'MaiyunSalt');
|
|
173
117
|
}
|
|
174
118
|
if (iv) {
|
|
175
|
-
if (method ===
|
|
119
|
+
if (method === AES_256_CFB) {
|
|
176
120
|
if (iv.length !== 16) {
|
|
177
121
|
return false;
|
|
178
122
|
}
|
|
179
123
|
}
|
|
180
|
-
else if (method ===
|
|
124
|
+
else if (method === AES_256_GCM) {
|
|
181
125
|
if (iv.length !== 12) {
|
|
182
126
|
return false;
|
|
183
127
|
}
|
|
@@ -194,7 +138,7 @@ function cipherEncrypt(original, key, iv = '', method = exports.AES_256_ECB, out
|
|
|
194
138
|
r = cip.update(original, undefined, 'base64');
|
|
195
139
|
}
|
|
196
140
|
r += cip.final('base64');
|
|
197
|
-
if (method ===
|
|
141
|
+
if (method === AES_256_GCM) {
|
|
198
142
|
r += cip.getAuthTag().toString('hex');
|
|
199
143
|
}
|
|
200
144
|
}
|
|
@@ -207,7 +151,7 @@ function cipherEncrypt(original, key, iv = '', method = exports.AES_256_ECB, out
|
|
|
207
151
|
r = cip.update(original);
|
|
208
152
|
}
|
|
209
153
|
r = Buffer.concat([r, cip.final()]);
|
|
210
|
-
if (method ===
|
|
154
|
+
if (method === AES_256_GCM) {
|
|
211
155
|
r = Buffer.concat([r, cip.getAuthTag()]);
|
|
212
156
|
}
|
|
213
157
|
}
|
|
@@ -225,9 +169,9 @@ function cipherEncrypt(original, key, iv = '', method = exports.AES_256_ECB, out
|
|
|
225
169
|
* @param method 加密方法
|
|
226
170
|
* @param output 输出类型
|
|
227
171
|
*/
|
|
228
|
-
function aesEncrypt(original, key, iv = '', method =
|
|
172
|
+
export function aesEncrypt(original, key, iv = '', method = AES_256_ECB, output = 'base64') {
|
|
229
173
|
if (iv !== '') {
|
|
230
|
-
method = method ===
|
|
174
|
+
method = method === AES_256_ECB ? AES_256_CFB : method;
|
|
231
175
|
}
|
|
232
176
|
return cipherEncrypt(original, key, iv, method, output);
|
|
233
177
|
}
|
|
@@ -237,9 +181,9 @@ function aesEncrypt(original, key, iv = '', method = exports.AES_256_ECB, output
|
|
|
237
181
|
* @param key 密钥尽量 32 个英文字母和数字,不是 32 个系统会自动处理
|
|
238
182
|
* @param output 输出类型
|
|
239
183
|
*/
|
|
240
|
-
function gcmEncrypt(original, key, output = 'base64') {
|
|
184
|
+
export function gcmEncrypt(original, key, output = 'base64') {
|
|
241
185
|
const iv = lCore.random(12, lCore.RANDOM_LUNS);
|
|
242
|
-
const rtn = cipherEncrypt(original, key, iv,
|
|
186
|
+
const rtn = cipherEncrypt(original, key, iv, AES_256_GCM, output);
|
|
243
187
|
if (!rtn) {
|
|
244
188
|
return false;
|
|
245
189
|
}
|
|
@@ -252,9 +196,9 @@ function gcmEncrypt(original, key, output = 'base64') {
|
|
|
252
196
|
* @param iv 向量 16 个英文字母和数字
|
|
253
197
|
* @param method 加密方法
|
|
254
198
|
*/
|
|
255
|
-
function sm4Encrypt(original, key, iv = '', method =
|
|
199
|
+
export function sm4Encrypt(original, key, iv = '', method = SM4_ECB, output = 'base64') {
|
|
256
200
|
if (iv !== '') {
|
|
257
|
-
method = method ===
|
|
201
|
+
method = method === SM4_ECB ? SM4_CFB : method;
|
|
258
202
|
}
|
|
259
203
|
return cipherEncrypt(original, key, iv, method, output);
|
|
260
204
|
}
|
|
@@ -265,25 +209,25 @@ function sm4Encrypt(original, key, iv = '', method = exports.SM4_ECB, output = '
|
|
|
265
209
|
* @param iv 向量 16(CFB) 或 12(GCM) 个英文字母和数字
|
|
266
210
|
* @param method 加密方法
|
|
267
211
|
*/
|
|
268
|
-
function cipherDecrypt(encrypt, key, iv = '', method =
|
|
212
|
+
export function cipherDecrypt(encrypt, key, iv = '', method = AES_256_ECB, output = 'binary') {
|
|
269
213
|
try {
|
|
270
214
|
if ((typeof key === 'string') && (key.length !== 32)) {
|
|
271
215
|
key = hashHmac('md5', key, 'MaiyunSalt');
|
|
272
216
|
}
|
|
273
217
|
if (iv) {
|
|
274
|
-
if (method ===
|
|
218
|
+
if (method === AES_256_CFB) {
|
|
275
219
|
if (iv.length !== 16) {
|
|
276
220
|
return false;
|
|
277
221
|
}
|
|
278
222
|
}
|
|
279
|
-
else if (method ===
|
|
223
|
+
else if (method === AES_256_GCM) {
|
|
280
224
|
if (iv.length !== 12) {
|
|
281
225
|
return false;
|
|
282
226
|
}
|
|
283
227
|
}
|
|
284
228
|
}
|
|
285
229
|
const cip = crypto.createDecipheriv(method, key, iv);
|
|
286
|
-
if (method ===
|
|
230
|
+
if (method === AES_256_GCM) {
|
|
287
231
|
if (typeof encrypt === 'string') {
|
|
288
232
|
cip.setAuthTag(Buffer.from(encrypt.slice(-32), 'hex'));
|
|
289
233
|
encrypt = encrypt.slice(0, -32);
|
|
@@ -327,9 +271,9 @@ function cipherDecrypt(encrypt, key, iv = '', method = exports.AES_256_ECB, outp
|
|
|
327
271
|
* @param iv 向量 16 个英文字母和数字
|
|
328
272
|
* @param method 加密方法
|
|
329
273
|
*/
|
|
330
|
-
function aesDecrypt(encrypt, key, iv = '', method =
|
|
274
|
+
export function aesDecrypt(encrypt, key, iv = '', method = AES_256_ECB, output = 'binary') {
|
|
331
275
|
if (iv !== '') {
|
|
332
|
-
method = method ===
|
|
276
|
+
method = method === AES_256_ECB ? AES_256_CFB : method;
|
|
333
277
|
}
|
|
334
278
|
return cipherDecrypt(encrypt, key, iv, method, output);
|
|
335
279
|
}
|
|
@@ -340,8 +284,8 @@ function aesDecrypt(encrypt, key, iv = '', method = exports.AES_256_ECB, output
|
|
|
340
284
|
* @param iv 向量 16 个英文字母和数字
|
|
341
285
|
* @param method 加密方法
|
|
342
286
|
*/
|
|
343
|
-
function gcmDecrypt(encrypt, key, output = 'binary') {
|
|
344
|
-
return cipherDecrypt(typeof encrypt === 'string' ? encrypt.slice(12) : encrypt.subarray(12), key, typeof encrypt === 'string' ? encrypt.slice(0, 12) : encrypt.subarray(0, 12).toString(),
|
|
287
|
+
export function gcmDecrypt(encrypt, key, output = 'binary') {
|
|
288
|
+
return cipherDecrypt(typeof encrypt === 'string' ? encrypt.slice(12) : encrypt.subarray(12), key, typeof encrypt === 'string' ? encrypt.slice(0, 12) : encrypt.subarray(0, 12).toString(), AES_256_GCM, output);
|
|
345
289
|
}
|
|
346
290
|
/**
|
|
347
291
|
* --- SM4 解密 ---
|
|
@@ -350,9 +294,9 @@ function gcmDecrypt(encrypt, key, output = 'binary') {
|
|
|
350
294
|
* @param iv 向量 16 个英文字母和数字
|
|
351
295
|
* @param method 加密方法
|
|
352
296
|
*/
|
|
353
|
-
function sm4Decrypt(encrypt, key, iv = '', method =
|
|
297
|
+
export function sm4Decrypt(encrypt, key, iv = '', method = SM4_ECB, output = 'binary') {
|
|
354
298
|
if (iv !== '') {
|
|
355
|
-
method = method ===
|
|
299
|
+
method = method === SM4_ECB ? SM4_CFB : method;
|
|
356
300
|
}
|
|
357
301
|
return cipherDecrypt(encrypt, key, iv, method, output);
|
|
358
302
|
}
|
|
@@ -362,7 +306,7 @@ function sm4Decrypt(encrypt, key, iv = '', method = exports.SM4_ECB, output = 'b
|
|
|
362
306
|
* @param data 源数据
|
|
363
307
|
* @param key 设置则采用 hmac 加密
|
|
364
308
|
*/
|
|
365
|
-
function hashHmac(algorithm, data, key, format = 'hex') {
|
|
309
|
+
export function hashHmac(algorithm, data, key, format = 'hex') {
|
|
366
310
|
const cry = key ? crypto.createHmac(algorithm, key) : crypto.createHash(algorithm);
|
|
367
311
|
cry.update(data);
|
|
368
312
|
if (format === 'buffer') {
|
|
@@ -378,7 +322,7 @@ function hashHmac(algorithm, data, key, format = 'hex') {
|
|
|
378
322
|
* @param path 文件路径
|
|
379
323
|
* @param key 设置则采用 hmac 加密
|
|
380
324
|
*/
|
|
381
|
-
function hashHmacFile(algorithm, path, key, encoding = 'hex') {
|
|
325
|
+
export function hashHmacFile(algorithm, path, key, encoding = 'hex') {
|
|
382
326
|
return new Promise(function (resolve) {
|
|
383
327
|
const cry = key ? crypto.createHmac(algorithm, key) : crypto.createHash(algorithm);
|
|
384
328
|
const rs = lFs.createReadStream(path);
|
|
@@ -400,7 +344,7 @@ function hashHmacFile(algorithm, path, key, encoding = 'hex') {
|
|
|
400
344
|
* --- base64 编码 ---
|
|
401
345
|
* @param data 字符串或 Buffer
|
|
402
346
|
*/
|
|
403
|
-
function base64Encode(data) {
|
|
347
|
+
export function base64Encode(data) {
|
|
404
348
|
if (typeof data === 'string') {
|
|
405
349
|
return Buffer.from(data, 'utf8').toString('base64');
|
|
406
350
|
}
|
|
@@ -408,7 +352,7 @@ function base64Encode(data) {
|
|
|
408
352
|
return Buffer.from(data).toString('base64');
|
|
409
353
|
}
|
|
410
354
|
}
|
|
411
|
-
function base64Decode(data, encoding = 'utf8') {
|
|
355
|
+
export function base64Decode(data, encoding = 'utf8') {
|
|
412
356
|
const buffer = Buffer.from(data, 'base64');
|
|
413
357
|
if (encoding === 'buffer') {
|
|
414
358
|
return buffer;
|
|
@@ -419,6 +363,6 @@ function base64Decode(data, encoding = 'utf8') {
|
|
|
419
363
|
* --- 生成 uuid ---
|
|
420
364
|
* @param options 选项
|
|
421
365
|
*/
|
|
422
|
-
function uuid(options) {
|
|
366
|
+
export function uuid(options) {
|
|
423
367
|
return crypto.randomUUID(options);
|
|
424
368
|
}
|
package/lib/db.d.ts
CHANGED
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
* Last: 2020-4-13 15:34:45, 2022-09-12 13:10:34, 2023-5-24 18:29:38, 2024-7-11 14:37:54, 2024-8-25 00:32:53, 2024-9-22 17:30:47, 2025-8-3 20:24:03
|
|
5
5
|
*/
|
|
6
6
|
import * as mysql2 from 'mysql2/promise';
|
|
7
|
-
import * as
|
|
8
|
-
import * as types from '
|
|
7
|
+
import * as sCtr from '../sys/ctr.js';
|
|
8
|
+
import * as types from '../types/index.js';
|
|
9
9
|
/** --- query 返回的数据 --- */
|
|
10
10
|
export interface IData {
|
|
11
11
|
'rows': any[] | null;
|
|
@@ -60,7 +60,7 @@ export declare class Pool {
|
|
|
60
60
|
/**
|
|
61
61
|
* --- 开启事务,返回事务对象并锁定连接,别人任何人不可用,有 ctr 的话必传 this,独立执行时可传 null ---
|
|
62
62
|
*/
|
|
63
|
-
beginTransaction(ctr:
|
|
63
|
+
beginTransaction(ctr: sCtr.Ctr | null): Promise<Transaction | null>;
|
|
64
64
|
/**
|
|
65
65
|
* --- 获取一个连接,自动变为 using 状态,;连接失败会返回 null ---
|
|
66
66
|
*/
|
|
@@ -78,7 +78,7 @@ export declare class Transaction {
|
|
|
78
78
|
private _conn;
|
|
79
79
|
private readonly _ctr;
|
|
80
80
|
private readonly _timer;
|
|
81
|
-
constructor(ctr:
|
|
81
|
+
constructor(ctr: sCtr.Ctr | null, conn: Connection, opts?: {
|
|
82
82
|
'warning'?: number;
|
|
83
83
|
'danger'?: number;
|
|
84
84
|
});
|
|
@@ -185,7 +185,7 @@ export declare class Connection {
|
|
|
185
185
|
* --- 获取 Db Pool 对象 ---
|
|
186
186
|
* @param etc 配置信息可留空
|
|
187
187
|
*/
|
|
188
|
-
export declare function get(ctrEtc:
|
|
188
|
+
export declare function get(ctrEtc: sCtr.Ctr | types.IConfigDb): Pool;
|
|
189
189
|
/**
|
|
190
190
|
* --- 获取当前连接池中所有连接的信息 ---
|
|
191
191
|
*/
|
package/lib/db.js
CHANGED
|
@@ -1,62 +1,24 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* Project: Kebab, User: JianSuoQiYue
|
|
4
3
|
* Date: 2019-4-15 13:40
|
|
5
4
|
* Last: 2020-4-13 15:34:45, 2022-09-12 13:10:34, 2023-5-24 18:29:38, 2024-7-11 14:37:54, 2024-8-25 00:32:53, 2024-9-22 17:30:47, 2025-8-3 20:24:03
|
|
6
5
|
*/
|
|
7
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
8
|
-
if (k2 === undefined) k2 = k;
|
|
9
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
10
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
11
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
12
|
-
}
|
|
13
|
-
Object.defineProperty(o, k2, desc);
|
|
14
|
-
}) : (function(o, m, k, k2) {
|
|
15
|
-
if (k2 === undefined) k2 = k;
|
|
16
|
-
o[k2] = m[k];
|
|
17
|
-
}));
|
|
18
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
19
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
20
|
-
}) : function(o, v) {
|
|
21
|
-
o["default"] = v;
|
|
22
|
-
});
|
|
23
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
24
|
-
var ownKeys = function(o) {
|
|
25
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
26
|
-
var ar = [];
|
|
27
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
28
|
-
return ar;
|
|
29
|
-
};
|
|
30
|
-
return ownKeys(o);
|
|
31
|
-
};
|
|
32
|
-
return function (mod) {
|
|
33
|
-
if (mod && mod.__esModule) return mod;
|
|
34
|
-
var result = {};
|
|
35
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
36
|
-
__setModuleDefault(result, mod);
|
|
37
|
-
return result;
|
|
38
|
-
};
|
|
39
|
-
})();
|
|
40
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
41
|
-
exports.Connection = exports.Transaction = exports.Pool = void 0;
|
|
42
|
-
exports.get = get;
|
|
43
|
-
exports.getConnectionList = getConnectionList;
|
|
44
6
|
// --- Pool 是使用时必须要一个用户创建一份的,Connection 是池子里获取的 ---
|
|
45
7
|
// --- 第三方 ---
|
|
46
|
-
|
|
8
|
+
import * as mysql2 from 'mysql2/promise';
|
|
47
9
|
// --- 库和定义 ---
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
10
|
+
import * as lTime from '../lib/time.js';
|
|
11
|
+
import * as lSql from '../lib/sql.js';
|
|
12
|
+
import * as lCore from '../lib/core.js';
|
|
13
|
+
import * as lText from '../lib/text.js';
|
|
14
|
+
import * as sCtr from '../sys/ctr.js';
|
|
53
15
|
/** --- 连接列表池 --- */
|
|
54
16
|
const connections = [];
|
|
55
17
|
/**
|
|
56
|
-
* --- 计划任务
|
|
18
|
+
* --- 计划任务 10 秒一次,关闭超过 30 秒不活动的连接,回滚独占时间过长的连接 ---
|
|
57
19
|
*/
|
|
58
20
|
async function checkConnection() {
|
|
59
|
-
const now =
|
|
21
|
+
const now = lTime.stamp();
|
|
60
22
|
for (let i = 0; i < connections.length; ++i) {
|
|
61
23
|
const connection = connections[i];
|
|
62
24
|
if (connection.isLost()) {
|
|
@@ -67,9 +29,9 @@ async function checkConnection() {
|
|
|
67
29
|
continue;
|
|
68
30
|
}
|
|
69
31
|
if (connection.isUsing()) {
|
|
70
|
-
// --- 连接正在被使用,看看是否空闲了超过
|
|
71
|
-
if (connection.getLast() <= now -
|
|
72
|
-
// ---
|
|
32
|
+
// --- 连接正在被使用,看看是否空闲了超过 30 秒,超过则不是正常状态 ---
|
|
33
|
+
if (connection.getLast() <= now - 30) {
|
|
34
|
+
// --- 30 秒之前开始的 ---
|
|
73
35
|
const ls = connection.getLastSql();
|
|
74
36
|
const newarr = ls.map(item => {
|
|
75
37
|
if (!item.values) {
|
|
@@ -77,31 +39,32 @@ async function checkConnection() {
|
|
|
77
39
|
}
|
|
78
40
|
return lSql.format(item.sql, item.values);
|
|
79
41
|
});
|
|
80
|
-
|
|
81
|
-
lCore.
|
|
42
|
+
const msg = `[DB][checkConnection] There is a transactional connection[${i}] that is not closed, last sql: ${newarr.join(', ')}.`;
|
|
43
|
+
lCore.display(msg);
|
|
44
|
+
lCore.log({}, msg, '-error');
|
|
82
45
|
await connection.rollback();
|
|
83
46
|
}
|
|
84
47
|
continue;
|
|
85
48
|
}
|
|
86
49
|
// --- 目前未被使用中的连接 ---
|
|
87
|
-
if (connection.getLast() > now -
|
|
88
|
-
// ---
|
|
50
|
+
if (connection.getLast() > now - 30) {
|
|
51
|
+
// --- 30 秒内使用过,不管 ---
|
|
89
52
|
continue;
|
|
90
53
|
}
|
|
91
|
-
// --- 超
|
|
54
|
+
// --- 超 30 秒未被使用,则关闭 ---
|
|
92
55
|
await connection.end();
|
|
93
56
|
connections.splice(i, 1);
|
|
94
57
|
--i;
|
|
95
58
|
}
|
|
96
59
|
setTimeout(function () {
|
|
97
60
|
checkConnection().catch(e => { lCore.display('[DB][checkConnection]', e); });
|
|
98
|
-
},
|
|
61
|
+
}, 10_000);
|
|
99
62
|
}
|
|
100
63
|
setTimeout(function () {
|
|
101
64
|
checkConnection().catch(e => { lCore.display('[DB][checkConnection]', e); });
|
|
102
|
-
},
|
|
65
|
+
}, 10_000);
|
|
103
66
|
/** --- 数据库连接池对象 --- */
|
|
104
|
-
class Pool {
|
|
67
|
+
export class Pool {
|
|
105
68
|
constructor(etc) {
|
|
106
69
|
/** --- SQL 执行次数 --- */
|
|
107
70
|
this._queries = 0;
|
|
@@ -194,7 +157,7 @@ class Pool {
|
|
|
194
157
|
'database': this._etc.name,
|
|
195
158
|
'user': this._etc.user,
|
|
196
159
|
'password': this._etc.pwd,
|
|
197
|
-
'connectTimeout':
|
|
160
|
+
'connectTimeout': 3_000,
|
|
198
161
|
});
|
|
199
162
|
const c = new Connection(this._etc, link);
|
|
200
163
|
c.using();
|
|
@@ -204,13 +167,16 @@ class Pool {
|
|
|
204
167
|
}
|
|
205
168
|
c.setLost();
|
|
206
169
|
}).on('end', function () {
|
|
170
|
+
lCore.debug('[DB][_getConnection] connection end.');
|
|
207
171
|
c.setLost();
|
|
208
172
|
});
|
|
209
173
|
conn = c;
|
|
210
174
|
connections.push(conn);
|
|
211
175
|
}
|
|
212
176
|
catch (e) {
|
|
213
|
-
|
|
177
|
+
const msg = '[DB][_getConnection] ' + lText.stringifyJson(e.stack).slice(1, -1);
|
|
178
|
+
lCore.debug(msg);
|
|
179
|
+
lCore.log({}, msg, '-error');
|
|
214
180
|
}
|
|
215
181
|
}
|
|
216
182
|
return conn;
|
|
@@ -222,9 +188,8 @@ class Pool {
|
|
|
222
188
|
return this._queries;
|
|
223
189
|
}
|
|
224
190
|
}
|
|
225
|
-
exports.Pool = Pool;
|
|
226
191
|
/** --- 事务连接对象,commit 和 rollback 后将无法使用 --- */
|
|
227
|
-
class Transaction {
|
|
192
|
+
export class Transaction {
|
|
228
193
|
constructor(ctr, conn, opts = {}) {
|
|
229
194
|
/** --- SQL 执行次数 --- */
|
|
230
195
|
this._queries = 0;
|
|
@@ -338,9 +303,8 @@ class Transaction {
|
|
|
338
303
|
return true;
|
|
339
304
|
}
|
|
340
305
|
}
|
|
341
|
-
exports.Transaction = Transaction;
|
|
342
306
|
/** --- 数据库连接对象 --- */
|
|
343
|
-
class Connection {
|
|
307
|
+
export class Connection {
|
|
344
308
|
constructor(etc, link) {
|
|
345
309
|
/** --- 本连接最后一次使用时间 --- */
|
|
346
310
|
this._last = 0;
|
|
@@ -421,7 +385,7 @@ class Connection {
|
|
|
421
385
|
* --- 设定最后使用时间 ---
|
|
422
386
|
*/
|
|
423
387
|
refreshLast() {
|
|
424
|
-
this._last =
|
|
388
|
+
this._last = lTime.stamp();
|
|
425
389
|
}
|
|
426
390
|
/**
|
|
427
391
|
* --- 通过执行一条语句判断当前连接是否可用 ---
|
|
@@ -573,19 +537,18 @@ class Connection {
|
|
|
573
537
|
}
|
|
574
538
|
}
|
|
575
539
|
}
|
|
576
|
-
exports.Connection = Connection;
|
|
577
540
|
/**
|
|
578
541
|
* --- 获取 Db Pool 对象 ---
|
|
579
542
|
* @param etc 配置信息可留空
|
|
580
543
|
*/
|
|
581
|
-
function get(ctrEtc) {
|
|
582
|
-
const etc = ctrEtc instanceof
|
|
544
|
+
export function get(ctrEtc) {
|
|
545
|
+
const etc = ctrEtc instanceof sCtr.Ctr ? ctrEtc.getPrototype('_config').db : ctrEtc;
|
|
583
546
|
return new Pool(etc);
|
|
584
547
|
}
|
|
585
548
|
/**
|
|
586
549
|
* --- 获取当前连接池中所有连接的信息 ---
|
|
587
550
|
*/
|
|
588
|
-
function getConnectionList() {
|
|
551
|
+
export function getConnectionList() {
|
|
589
552
|
const list = [];
|
|
590
553
|
for (let i = 0; i < connections.length; ++i) {
|
|
591
554
|
const connection = connections[i];
|
package/lib/dns.d.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* Date: 2019-6-19
|
|
4
4
|
* Last: 2022-09-12 20:58:07, 2024-2-21 17:55:54, 2025-6-13 19:08:56
|
|
5
5
|
*/
|
|
6
|
-
import * as ctr from '
|
|
6
|
+
import * as ctr from '../sys/ctr.js';
|
|
7
7
|
/**
|
|
8
8
|
* 0.DNSPod:https://www.dnspod.cn/docs/index.html(腾讯云也请使用 DNSPod 的 API)
|
|
9
9
|
* 1.阿里云:https://help.aliyun.com/document_detail/29745.html
|