@maiyunnet/kebab 5.3.1 → 6.1.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 -1
- package/index.d.ts +2 -3
- package/index.js +6 -2
- package/lib/ai.d.ts +4 -3
- package/lib/ai.js +19 -11
- package/lib/buffer.d.ts +8 -4
- package/lib/buffer.js +16 -4
- package/lib/core.d.ts +24 -10
- package/lib/core.js +19 -12
- package/lib/crypto.d.ts +11 -2
- package/lib/crypto.js +6 -8
- package/lib/db/conn.js +1 -1
- package/lib/db.d.ts +5 -3
- package/lib/db.js +5 -3
- package/lib/dns.d.ts +17 -17
- package/lib/dns.js +44 -42
- package/lib/fs.d.ts +2 -2
- package/lib/fs.js +2 -2
- package/lib/kv.d.ts +49 -7
- package/lib/kv.js +69 -7
- package/lib/net/request.d.ts +1 -1
- package/lib/net.d.ts +6 -6
- package/lib/net.js +3 -3
- package/lib/scan.d.ts +3 -3
- package/lib/scan.js +3 -3
- package/lib/sql.d.ts +16 -5
- package/lib/sql.js +176 -257
- package/lib/ssh.d.ts +1 -2
- package/lib/text.d.ts +4 -4
- package/lib/text.js +2 -2
- package/lib/vector.d.ts +1 -1
- package/lib/vector.js +7 -7
- package/lib/ws.d.ts +0 -1
- package/lib/ws.js +0 -1
- package/lib/zip.d.ts +0 -1
- package/lib/zip.js +0 -1
- package/lib/zlib.d.ts +1 -2
- package/lib/zlib.js +1 -2
- package/package.json +13 -8
- package/sys/cmd.js +0 -7
- package/sys/ctr.d.ts +0 -2
- package/sys/ctr.js +0 -2
- package/sys/mod.d.ts +23 -9
- package/sys/mod.js +114 -12
- package/sys/route.js +0 -1
- package/www/example/ctr/test.d.ts +2 -2
- package/www/example/ctr/test.js +170 -93
- package/lib/jwt.d.ts +0 -73
- package/lib/jwt.js +0 -226
package/README.md
CHANGED
|
@@ -32,7 +32,7 @@ You can also use pm2 to start the project by running:
|
|
|
32
32
|
|
|
33
33
|
## Library
|
|
34
34
|
|
|
35
|
-
Buffer, Captcha, Consistent, Core, Cron, Crypto, Db (MySQL), Dns (DNSPod, Alibaba Cloud), Fs,
|
|
35
|
+
Ai, Buffer, Captcha, Consistent, Core, Cron, Crypto, Db (MySQL, PostgreSQL), Dns (DNSPod, Alibaba Cloud), Fs, Kv (Redis), Lan, Lang, Net, S3, Scan, Session, Socket, Sql, Ssh (Shell, Sftp), Text, Time, Turnstile (Cloudflare, Tencent Cloud), Vector (Milvus), Ws, Zip, Zlib.
|
|
36
36
|
|
|
37
37
|
## Partial Features
|
|
38
38
|
|
package/index.d.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* --- 本文件用来定义每个目录实体地址的常量 ---
|
|
6
6
|
*/
|
|
7
7
|
/** --- 当前系统版本号 --- */
|
|
8
|
-
export declare const VER = "
|
|
8
|
+
export declare const VER = "6.1.0";
|
|
9
9
|
/** --- 框架根目录,以 / 结尾 --- */
|
|
10
10
|
export declare const ROOT_PATH: string;
|
|
11
11
|
export declare const LIB_PATH: string;
|
|
@@ -25,7 +25,7 @@ export declare const MOD_CWD: string;
|
|
|
25
25
|
export type Json = any;
|
|
26
26
|
/** --- 数据库值的类型 --- */
|
|
27
27
|
export type DbValue = string | number | null | Record<string, Json>;
|
|
28
|
-
/** ---
|
|
28
|
+
/** --- 目录配置文件 --- */
|
|
29
29
|
export interface IConfig {
|
|
30
30
|
'set': {
|
|
31
31
|
'timezone': number;
|
|
@@ -43,7 +43,6 @@ export interface IConfig {
|
|
|
43
43
|
}> & {
|
|
44
44
|
'default': 'MYSQL' | 'PGSQL';
|
|
45
45
|
};
|
|
46
|
-
'jwt': IConfigJwt;
|
|
47
46
|
'kv': IConfigKv;
|
|
48
47
|
'route': Record<string, string>;
|
|
49
48
|
'session': {
|
package/index.js
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* --- 本文件用来定义每个目录实体地址的常量 ---
|
|
7
7
|
*/
|
|
8
8
|
/** --- 当前系统版本号 --- */
|
|
9
|
-
export const VER = '
|
|
9
|
+
export const VER = '6.1.0';
|
|
10
10
|
// --- 服务端用的路径 ---
|
|
11
11
|
const imu = decodeURIComponent(import.meta.url).replace('file://', '').replace(/^\/(\w:)/, '$1');
|
|
12
12
|
/** --- /xxx/xxx --- */
|
|
@@ -15,7 +15,11 @@ const dirname = imu.slice(0, imu.lastIndexOf('/'));
|
|
|
15
15
|
export const ROOT_PATH = dirname + '/';
|
|
16
16
|
export const LIB_PATH = ROOT_PATH + 'lib/';
|
|
17
17
|
export const SYS_PATH = ROOT_PATH + 'sys/';
|
|
18
|
-
|
|
18
|
+
let cwd = process.cwd().replace(/\\/g, '/');
|
|
19
|
+
if (cwd.endsWith('source')) {
|
|
20
|
+
// --- 开发环境下,cwd 在 source 的上级目录 ---
|
|
21
|
+
cwd = cwd.slice(0, -7);
|
|
22
|
+
}
|
|
19
23
|
/** --- 执行根目录,以 / 结尾 --- */
|
|
20
24
|
export const ROOT_CWD = cwd + '/';
|
|
21
25
|
export const CONF_CWD = ROOT_CWD + 'conf/';
|
package/lib/ai.d.ts
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import * as openai from 'openai';
|
|
7
7
|
import * as streaming from 'openai/streaming';
|
|
8
|
+
import * as kebab from '#kebab/index.js';
|
|
8
9
|
import * as sCtr from '#kebab/sys/ctr.js';
|
|
9
10
|
/**
|
|
10
11
|
* --- 参考:https://help.aliyun.com/zh/model-studio/what-is-model-studio ---
|
|
@@ -37,8 +38,8 @@ export interface IOptions {
|
|
|
37
38
|
export declare class Ai {
|
|
38
39
|
/** --- openai 原生对象,建议只读 --- */
|
|
39
40
|
readonly link: openai.OpenAI;
|
|
40
|
-
private readonly _ctr
|
|
41
|
-
constructor(
|
|
41
|
+
private readonly _ctr?;
|
|
42
|
+
constructor(ctrEtc: sCtr.Ctr | kebab.IConfigAi, opt: IOptions);
|
|
42
43
|
/** --- 创建非流式对话 --- */
|
|
43
44
|
chat(body: openai.default.Chat.Completions.ChatCompletionCreateParamsNonStreaming): Promise<openai.APIPromise<openai.default.Chat.ChatCompletion> | false>;
|
|
44
45
|
/** --- 创建流式对话 --- */
|
|
@@ -50,4 +51,4 @@ export declare class Ai {
|
|
|
50
51
|
* --- 创建一个 AI 对象 ---
|
|
51
52
|
* @param opt 选项
|
|
52
53
|
*/
|
|
53
|
-
export declare function get(
|
|
54
|
+
export declare function get(ctrEtc: sCtr.Ctr | kebab.IConfigAi, opt: IOptions): Ai;
|
package/lib/ai.js
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import * as openai from 'openai';
|
|
7
7
|
import * as lCore from '#kebab/lib/core.js';
|
|
8
|
+
import * as sCtr from '#kebab/sys/ctr.js';
|
|
8
9
|
/**
|
|
9
10
|
* --- 参考:https://help.aliyun.com/zh/model-studio/what-is-model-studio ---
|
|
10
11
|
* --- 阿里模型:https://help.aliyun.com/zh/model-studio/getting-started/models ---
|
|
@@ -26,10 +27,17 @@ export var ESERVICE;
|
|
|
26
27
|
/** --- openai 的连接对象 --- */
|
|
27
28
|
const links = [];
|
|
28
29
|
export class Ai {
|
|
29
|
-
constructor(
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
30
|
+
constructor(ctrEtc, opt) {
|
|
31
|
+
let configAi = null;
|
|
32
|
+
if (ctrEtc instanceof sCtr.Ctr) {
|
|
33
|
+
this._ctr = ctrEtc;
|
|
34
|
+
const config = ctrEtc.getPrototype('_config');
|
|
35
|
+
configAi = config.ai[ESERVICE[opt.service]];
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
configAi = ctrEtc;
|
|
39
|
+
}
|
|
40
|
+
const secretKey = opt.secretKey ?? configAi.skey ?? '';
|
|
33
41
|
let endpoint;
|
|
34
42
|
switch (opt.service) {
|
|
35
43
|
case ESERVICE.ALICN: {
|
|
@@ -41,15 +49,15 @@ export class Ai {
|
|
|
41
49
|
break;
|
|
42
50
|
}
|
|
43
51
|
case ESERVICE.AZURE: {
|
|
44
|
-
endpoint = opt.endpoint ??
|
|
52
|
+
endpoint = opt.endpoint ?? configAi.endpoint ?? '';
|
|
45
53
|
break;
|
|
46
54
|
}
|
|
47
55
|
case ESERVICE.AZURE2: {
|
|
48
|
-
endpoint = opt.endpoint ??
|
|
56
|
+
endpoint = opt.endpoint ?? configAi.endpoint ?? '';
|
|
49
57
|
break;
|
|
50
58
|
}
|
|
51
59
|
case ESERVICE.AZURE3: {
|
|
52
|
-
endpoint = opt.endpoint ??
|
|
60
|
+
endpoint = opt.endpoint ?? configAi.endpoint ?? '';
|
|
53
61
|
break;
|
|
54
62
|
}
|
|
55
63
|
default: {
|
|
@@ -79,7 +87,7 @@ export class Ai {
|
|
|
79
87
|
}
|
|
80
88
|
catch (e) {
|
|
81
89
|
lCore.debug('[AI][CHAT]', e);
|
|
82
|
-
lCore.log(this._ctr, `[AI][CHAT] ${e.message}`, '-error');
|
|
90
|
+
lCore.log(this._ctr ?? {}, `[AI][CHAT] ${e.message}`, '-error');
|
|
83
91
|
return false;
|
|
84
92
|
}
|
|
85
93
|
}
|
|
@@ -90,7 +98,7 @@ export class Ai {
|
|
|
90
98
|
}
|
|
91
99
|
catch (e) {
|
|
92
100
|
lCore.debug('[AI][EMBEDDING]', e);
|
|
93
|
-
lCore.log(this._ctr, `[AI][EMBEDDING] ${e.message}`, '-error');
|
|
101
|
+
lCore.log(this._ctr ?? {}, `[AI][EMBEDDING] ${e.message}`, '-error');
|
|
94
102
|
return false;
|
|
95
103
|
}
|
|
96
104
|
}
|
|
@@ -99,6 +107,6 @@ export class Ai {
|
|
|
99
107
|
* --- 创建一个 AI 对象 ---
|
|
100
108
|
* @param opt 选项
|
|
101
109
|
*/
|
|
102
|
-
export function get(
|
|
103
|
-
return new Ai(
|
|
110
|
+
export function get(ctrEtc, opt) {
|
|
111
|
+
return new Ai(ctrEtc, opt);
|
|
104
112
|
}
|
package/lib/buffer.d.ts
CHANGED
|
@@ -4,12 +4,16 @@ export declare class Reader {
|
|
|
4
4
|
/** --- 当前读取位置 --- */
|
|
5
5
|
private _offset;
|
|
6
6
|
constructor(buffer: Buffer);
|
|
7
|
-
/** --- 读取一个无符号8位整数, BYTE --- */
|
|
7
|
+
/** --- 读取一个无符号 8 位整数, BYTE --- */
|
|
8
8
|
readUInt8(): number;
|
|
9
|
-
/** --- 读取一个无符号16位整数(大端模式),WORD --- */
|
|
9
|
+
/** --- 读取一个无符号 16 位整数(大端模式),WORD --- */
|
|
10
10
|
readUInt16BE(): number;
|
|
11
|
-
/** --- 读取一个无符号
|
|
11
|
+
/** --- 读取一个无符号 16 位整数(小端模式) --- */
|
|
12
|
+
readUInt16LE(): number;
|
|
13
|
+
/** --- 读取一个无符号 32 位整数(大端模式), DWORD --- */
|
|
12
14
|
readUInt32BE(): number;
|
|
15
|
+
/** --- 读取一个无符号 32 位整数(小端模式) --- */
|
|
16
|
+
readUInt32LE(): number;
|
|
13
17
|
/** --- 读取一个 BCD 编码的字符串(每个字节表示两个数字)--- */
|
|
14
18
|
readBCDString(length?: number): string;
|
|
15
19
|
/** --- 读取普通 string --- */
|
|
@@ -45,6 +49,6 @@ export declare class Writer {
|
|
|
45
49
|
export declare function getReader(buffer: Buffer): Reader;
|
|
46
50
|
/**
|
|
47
51
|
* --- Buffer Writer 对象 ---
|
|
48
|
-
* @param
|
|
52
|
+
* @param size 缓冲区大小
|
|
49
53
|
*/
|
|
50
54
|
export declare function getWriter(size: number): Writer;
|
package/lib/buffer.js
CHANGED
|
@@ -5,24 +5,36 @@ export class Reader {
|
|
|
5
5
|
this._offset = 0;
|
|
6
6
|
this._buffer = buffer;
|
|
7
7
|
}
|
|
8
|
-
/** --- 读取一个无符号8位整数, BYTE --- */
|
|
8
|
+
/** --- 读取一个无符号 8 位整数, BYTE --- */
|
|
9
9
|
readUInt8() {
|
|
10
10
|
const value = this._buffer.readUInt8(this._offset);
|
|
11
11
|
this._offset += 1;
|
|
12
12
|
return value;
|
|
13
13
|
}
|
|
14
|
-
/** --- 读取一个无符号16位整数(大端模式),WORD --- */
|
|
14
|
+
/** --- 读取一个无符号 16 位整数(大端模式),WORD --- */
|
|
15
15
|
readUInt16BE() {
|
|
16
16
|
const value = this._buffer.readUInt16BE(this._offset);
|
|
17
17
|
this._offset += 2;
|
|
18
18
|
return value;
|
|
19
19
|
}
|
|
20
|
-
/** --- 读取一个无符号
|
|
20
|
+
/** --- 读取一个无符号 16 位整数(小端模式) --- */
|
|
21
|
+
readUInt16LE() {
|
|
22
|
+
const value = this._buffer.readUInt16LE(this._offset);
|
|
23
|
+
this._offset += 2;
|
|
24
|
+
return value;
|
|
25
|
+
}
|
|
26
|
+
/** --- 读取一个无符号 32 位整数(大端模式), DWORD --- */
|
|
21
27
|
readUInt32BE() {
|
|
22
28
|
const value = this._buffer.readUInt32BE(this._offset);
|
|
23
29
|
this._offset += 4;
|
|
24
30
|
return value;
|
|
25
31
|
}
|
|
32
|
+
/** --- 读取一个无符号 32 位整数(小端模式) --- */
|
|
33
|
+
readUInt32LE() {
|
|
34
|
+
const value = this._buffer.readUInt32LE(this._offset);
|
|
35
|
+
this._offset += 4;
|
|
36
|
+
return value;
|
|
37
|
+
}
|
|
26
38
|
/** --- 读取一个 BCD 编码的字符串(每个字节表示两个数字)--- */
|
|
27
39
|
readBCDString(length) {
|
|
28
40
|
length ??= this._buffer.length - this._offset;
|
|
@@ -113,7 +125,7 @@ export function getReader(buffer) {
|
|
|
113
125
|
}
|
|
114
126
|
/**
|
|
115
127
|
* --- Buffer Writer 对象 ---
|
|
116
|
-
* @param
|
|
128
|
+
* @param size 缓冲区大小
|
|
117
129
|
*/
|
|
118
130
|
export function getWriter(size) {
|
|
119
131
|
return new Writer(size);
|
package/lib/core.d.ts
CHANGED
|
@@ -40,14 +40,23 @@ export declare function setCookie(ctr: sCtr.Ctr, name: string, value: string, op
|
|
|
40
40
|
* @param prec 保留几位小数
|
|
41
41
|
*/
|
|
42
42
|
export declare function rand(min: number, max: number, prec?: number): number;
|
|
43
|
+
/** --- 数字字符集 --- */
|
|
43
44
|
export declare const RANDOM_N = "0123456789";
|
|
45
|
+
/** --- 大写字母字符集 --- */
|
|
44
46
|
export declare const RANDOM_U = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
47
|
+
/** --- 小写字母字符集 --- */
|
|
45
48
|
export declare const RANDOM_L = "abcdefghijklmnopqrstuvwxyz";
|
|
49
|
+
/** --- 大写字母 + 数字字符集 --- */
|
|
46
50
|
export declare const RANDOM_UN: string;
|
|
51
|
+
/** --- 小写字母 + 数字字符集 --- */
|
|
47
52
|
export declare const RANDOM_LN: string;
|
|
53
|
+
/** --- 小写字母 + 大写字母字符集 --- */
|
|
48
54
|
export declare const RANDOM_LU: string;
|
|
55
|
+
/** --- 小写字母 + 大写字母 + 数字字符集 --- */
|
|
49
56
|
export declare const RANDOM_LUN: string;
|
|
57
|
+
/** --- 验证码字符集 --- */
|
|
50
58
|
export declare const RANDOM_V = "ACEFGHJKLMNPRSTWXY34567";
|
|
59
|
+
/** --- 小写字母 + 大写字母 + 数字字符集 + 特殊字符字符集 --- */
|
|
51
60
|
export declare const RANDOM_LUNS: string;
|
|
52
61
|
/**
|
|
53
62
|
* --- 生成随机字符串 ---
|
|
@@ -56,7 +65,6 @@ export declare const RANDOM_LUNS: string;
|
|
|
56
65
|
* @param block 排除的字符
|
|
57
66
|
*/
|
|
58
67
|
export declare function random(length?: number, source?: string, block?: string): string;
|
|
59
|
-
export declare const CONVERT62_CHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
60
68
|
/**
|
|
61
69
|
* --- 将 10 进制转换为 62 进制 ---
|
|
62
70
|
* @param n 10 进制数字最大 9223372036854775807n
|
|
@@ -82,13 +90,18 @@ export declare function checkType(val: any, type: any, tree?: string): string;
|
|
|
82
90
|
/**
|
|
83
91
|
* --- 获取 MUID ---
|
|
84
92
|
* @param ctr Ctr 对象
|
|
85
|
-
* @param opt
|
|
93
|
+
* @param opt 参数
|
|
86
94
|
*/
|
|
87
95
|
export declare function muid(ctr: sCtr.Ctr, opt?: {
|
|
96
|
+
/** --- 8 - 32, 默认 8 --- */
|
|
88
97
|
'len'?: number;
|
|
98
|
+
/** --- 是否含有大小写, 默认 true --- */
|
|
89
99
|
'bin'?: boolean;
|
|
100
|
+
/** --- 多样性混合, 默认空 --- */
|
|
90
101
|
'key'?: string;
|
|
102
|
+
/** --- 插入指定字符, 最好不超过 2 字符,默认空 --- */
|
|
91
103
|
'insert'?: string;
|
|
104
|
+
/** --- 是否含有数字, 默认 true --- */
|
|
92
105
|
'num'?: boolean;
|
|
93
106
|
}): string;
|
|
94
107
|
/**
|
|
@@ -96,7 +109,9 @@ export declare function muid(ctr: sCtr.Ctr, opt?: {
|
|
|
96
109
|
* @param ctr
|
|
97
110
|
*/
|
|
98
111
|
export declare function ip(ctr: sCtr.Ctr | http.IncomingHttpHeaders, req?: http2.Http2ServerRequest | http.IncomingMessage): string;
|
|
112
|
+
/** --- 使用 X-Forwarded-For 的 CDN 厂商 --- */
|
|
99
113
|
export declare const REAL_IP_X = "x-forwarded-for";
|
|
114
|
+
/** --- 使用的是 Cloudflare --- */
|
|
100
115
|
export declare const REAL_IP_CF = "cf-connecting-ip";
|
|
101
116
|
/**
|
|
102
117
|
* --- 获取直连 IP(安全 IP) ---
|
|
@@ -117,9 +132,9 @@ export declare function objectSort<T extends Record<string, any>>(o: T): T;
|
|
|
117
132
|
/**
|
|
118
133
|
* --- 将对象的所有属性清除包括键,不会破坏引用关系,对象变量依然保证是引用状态 ---
|
|
119
134
|
* @param obj 要清除的对象
|
|
120
|
-
* @
|
|
135
|
+
* @param deep 也将子项都清空,如果子项有独立引用的话也要清空的话则要设置为 true
|
|
121
136
|
*/
|
|
122
|
-
export declare function emptyObject(obj: Record<string,
|
|
137
|
+
export declare function emptyObject(obj: Record<string, any>, deep?: boolean): void;
|
|
123
138
|
/**
|
|
124
139
|
* --- 调用前自行创建 passThrough,并且调用 pipe 绑定到应该绑定的对象,然后再调用本函数 ---
|
|
125
140
|
* @param passThrough passThrough 对象
|
|
@@ -145,12 +160,12 @@ export declare function sendRestart(hosts?: string[] | 'config'): Promise<string
|
|
|
145
160
|
/** --- 跨进程全局变量 --- */
|
|
146
161
|
export declare const global: Record<string, any>;
|
|
147
162
|
/**
|
|
148
|
-
* ---
|
|
163
|
+
* --- 设置跨线程/指定的局域网主机的全局变量 ---
|
|
149
164
|
* @param key 变量名
|
|
150
165
|
* @param data 变量值
|
|
151
166
|
* @param hosts 局域网列表
|
|
152
167
|
*/
|
|
153
|
-
export declare function setGlobal(key: string, data:
|
|
168
|
+
export declare function setGlobal(key: string, data: any, hosts?: string[] | 'config'): Promise<string[]>;
|
|
154
169
|
/**
|
|
155
170
|
* --- 移除某个跨线程/跨内网服务器全局变量 ---
|
|
156
171
|
* @param key 变量名
|
|
@@ -160,7 +175,7 @@ export declare function removeGlobal(key: string, hosts?: string[]): Promise<str
|
|
|
160
175
|
/**
|
|
161
176
|
* --- 上传并覆盖代码文件,config.json、kebab.json、.js.map、.ts, .gitignore 不会被覆盖和新建 ---
|
|
162
177
|
* @param sourcePath zip 文件
|
|
163
|
-
* @param path
|
|
178
|
+
* @param path 要更新的目标路径,无所谓是否 / 开头 / 结尾,是对方 kebab 的根据路径开始算起
|
|
164
179
|
* @param hosts 局域网多机部署,不设置默认本机部署
|
|
165
180
|
* @param config 是否自动更新 config 的 set.staticVer 为最新,默认更新
|
|
166
181
|
* @param strict 严格模式,只有存在的文件才会被覆盖,不存在则中途直接报错,默认为 true
|
|
@@ -175,17 +190,16 @@ export interface ILogOptions {
|
|
|
175
190
|
'urlFull'?: string;
|
|
176
191
|
'hostname'?: string;
|
|
177
192
|
'req'?: http2.Http2ServerRequest | http.IncomingMessage | null;
|
|
178
|
-
'get'?: Record<string,
|
|
193
|
+
'get'?: Record<string, any>;
|
|
179
194
|
'cookie'?: Record<string, string>;
|
|
180
|
-
'jwt'?: Record<string, any>;
|
|
181
195
|
'session'?: Record<string, any>;
|
|
182
196
|
'headers'?: http.IncomingHttpHeaders;
|
|
183
197
|
}
|
|
184
198
|
/**
|
|
185
199
|
* --- 写入文件日志 ---
|
|
200
|
+
* @param opt 选项
|
|
186
201
|
* @param msg 自定义内容
|
|
187
202
|
* @param fend 文件名追加
|
|
188
|
-
* @param opt 选项
|
|
189
203
|
*/
|
|
190
204
|
export declare function log(opt: sCtr.Ctr | ILogOptions, msg: string, fend?: string): void;
|
|
191
205
|
/**
|
package/lib/core.js
CHANGED
|
@@ -62,14 +62,23 @@ export function rand(min, max, prec = 0) {
|
|
|
62
62
|
return Math.round(Math.random() * (max - min) + min) / p;
|
|
63
63
|
}
|
|
64
64
|
// --- 随机 ---
|
|
65
|
+
/** --- 数字字符集 --- */
|
|
65
66
|
export const RANDOM_N = '0123456789';
|
|
67
|
+
/** --- 大写字母字符集 --- */
|
|
66
68
|
export const RANDOM_U = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
69
|
+
/** --- 小写字母字符集 --- */
|
|
67
70
|
export const RANDOM_L = 'abcdefghijklmnopqrstuvwxyz';
|
|
71
|
+
/** --- 大写字母 + 数字字符集 --- */
|
|
68
72
|
export const RANDOM_UN = RANDOM_U + RANDOM_N;
|
|
73
|
+
/** --- 小写字母 + 数字字符集 --- */
|
|
69
74
|
export const RANDOM_LN = RANDOM_L + RANDOM_N;
|
|
75
|
+
/** --- 小写字母 + 大写字母字符集 --- */
|
|
70
76
|
export const RANDOM_LU = RANDOM_L + RANDOM_U;
|
|
77
|
+
/** --- 小写字母 + 大写字母 + 数字字符集 --- */
|
|
71
78
|
export const RANDOM_LUN = RANDOM_L + RANDOM_U + RANDOM_N;
|
|
79
|
+
/** --- 验证码字符集 --- */
|
|
72
80
|
export const RANDOM_V = 'ACEFGHJKLMNPRSTWXY34567';
|
|
81
|
+
/** --- 小写字母 + 大写字母 + 数字字符集 + 特殊字符字符集 --- */
|
|
73
82
|
export const RANDOM_LUNS = RANDOM_LUN + '()`~!@#$%^&*-+=_|{}[]:;"<>,.?/]"';
|
|
74
83
|
/**
|
|
75
84
|
* --- 生成随机字符串 ---
|
|
@@ -95,7 +104,7 @@ export function random(length = 8, source = RANDOM_LN, block = '') {
|
|
|
95
104
|
}
|
|
96
105
|
return temp;
|
|
97
106
|
}
|
|
98
|
-
|
|
107
|
+
const CONVERT62_CHAR = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
99
108
|
/**
|
|
100
109
|
* --- 将 10 进制转换为 62 进制 ---
|
|
101
110
|
* @param n 10 进制数字最大 9223372036854775807n
|
|
@@ -241,7 +250,7 @@ export function checkType(val, type, tree = 'root') {
|
|
|
241
250
|
/**
|
|
242
251
|
* --- 获取 MUID ---
|
|
243
252
|
* @param ctr Ctr 对象
|
|
244
|
-
* @param opt
|
|
253
|
+
* @param opt 参数
|
|
245
254
|
*/
|
|
246
255
|
export function muid(ctr, opt = {}) {
|
|
247
256
|
const len = opt.len ?? 8;
|
|
@@ -289,7 +298,9 @@ export function ip(ctr, req) {
|
|
|
289
298
|
return req.socket.remoteAddress ?? '';
|
|
290
299
|
}
|
|
291
300
|
}
|
|
301
|
+
/** --- 使用 X-Forwarded-For 的 CDN 厂商 --- */
|
|
292
302
|
export const REAL_IP_X = 'x-forwarded-for';
|
|
303
|
+
/** --- 使用的是 Cloudflare --- */
|
|
293
304
|
export const REAL_IP_CF = 'cf-connecting-ip';
|
|
294
305
|
/**
|
|
295
306
|
* --- 获取直连 IP(安全 IP) ---
|
|
@@ -338,7 +349,7 @@ export function objectSort(o) {
|
|
|
338
349
|
/**
|
|
339
350
|
* --- 将对象的所有属性清除包括键,不会破坏引用关系,对象变量依然保证是引用状态 ---
|
|
340
351
|
* @param obj 要清除的对象
|
|
341
|
-
* @
|
|
352
|
+
* @param deep 也将子项都清空,如果子项有独立引用的话也要清空的话则要设置为 true
|
|
342
353
|
*/
|
|
343
354
|
export function emptyObject(obj, deep = false) {
|
|
344
355
|
const keys = Object.keys(obj);
|
|
@@ -484,7 +495,7 @@ export async function sendRestart(hosts) {
|
|
|
484
495
|
/** --- 跨进程全局变量 --- */
|
|
485
496
|
export const global = {};
|
|
486
497
|
/**
|
|
487
|
-
* ---
|
|
498
|
+
* --- 设置跨线程/指定的局域网主机的全局变量 ---
|
|
488
499
|
* @param key 变量名
|
|
489
500
|
* @param data 变量值
|
|
490
501
|
* @param hosts 局域网列表
|
|
@@ -535,7 +546,7 @@ export async function removeGlobal(key, hosts) {
|
|
|
535
546
|
/**
|
|
536
547
|
* --- 上传并覆盖代码文件,config.json、kebab.json、.js.map、.ts, .gitignore 不会被覆盖和新建 ---
|
|
537
548
|
* @param sourcePath zip 文件
|
|
538
|
-
* @param path
|
|
549
|
+
* @param path 要更新的目标路径,无所谓是否 / 开头 / 结尾,是对方 kebab 的根据路径开始算起
|
|
539
550
|
* @param hosts 局域网多机部署,不设置默认本机部署
|
|
540
551
|
* @param config 是否自动更新 config 的 set.staticVer 为最新,默认更新
|
|
541
552
|
* @param strict 严格模式,只有存在的文件才会被覆盖,不存在则中途直接报错,默认为 true
|
|
@@ -579,9 +590,9 @@ export async function updateCode(sourcePath, path, hosts, config = true, strict
|
|
|
579
590
|
}
|
|
580
591
|
/**
|
|
581
592
|
* --- 写入文件日志 ---
|
|
593
|
+
* @param opt 选项
|
|
582
594
|
* @param msg 自定义内容
|
|
583
595
|
* @param fend 文件名追加
|
|
584
|
-
* @param opt 选项
|
|
585
596
|
*/
|
|
586
597
|
export function log(opt, msg, fend = '') {
|
|
587
598
|
(async () => {
|
|
@@ -589,7 +600,6 @@ export function log(opt, msg, fend = '') {
|
|
|
589
600
|
let headers;
|
|
590
601
|
let get;
|
|
591
602
|
let cookie;
|
|
592
|
-
let jwt;
|
|
593
603
|
let session;
|
|
594
604
|
let wpath;
|
|
595
605
|
let urlFull;
|
|
@@ -599,7 +609,6 @@ export function log(opt, msg, fend = '') {
|
|
|
599
609
|
headers = opt.getPrototype('_headers');
|
|
600
610
|
get = opt.getPrototype('_get');
|
|
601
611
|
cookie = opt.getPrototype('_cookie');
|
|
602
|
-
jwt = opt.getPrototype('_jwt');
|
|
603
612
|
session = opt.getPrototype('_session');
|
|
604
613
|
const config = opt.getPrototype('_config');
|
|
605
614
|
wpath = config.const.path;
|
|
@@ -611,7 +620,6 @@ export function log(opt, msg, fend = '') {
|
|
|
611
620
|
headers = opt.headers ?? {};
|
|
612
621
|
get = opt.get ?? {};
|
|
613
622
|
cookie = opt.cookie ?? {};
|
|
614
|
-
jwt = opt.jwt ?? {};
|
|
615
623
|
session = opt.session ?? {};
|
|
616
624
|
wpath = opt.path ?? '';
|
|
617
625
|
urlFull = opt.urlFull ?? '';
|
|
@@ -630,7 +638,7 @@ export function log(opt, msg, fend = '') {
|
|
|
630
638
|
}
|
|
631
639
|
path += h + '.csv';
|
|
632
640
|
if (!await lFs.isFile(path)) {
|
|
633
|
-
if (!await lFs.putContent(path, 'TIME,UNIX,URL,COOKIE,SESSION,
|
|
641
|
+
if (!await lFs.putContent(path, 'TIME,UNIX,URL,COOKIE,SESSION,USER_AGENT,REALIP,CLIENTIP,OS,PROCESS,MESSAGE\n', {
|
|
634
642
|
'encoding': 'utf8',
|
|
635
643
|
'mode': 0o777
|
|
636
644
|
})) {
|
|
@@ -642,7 +650,6 @@ export function log(opt, msg, fend = '') {
|
|
|
642
650
|
lTime.stamp().toString() + '","' +
|
|
643
651
|
urlFull + wpath + (Object.keys(get).length ? '?' + lText.queryStringify(get).replace(/"/g, '""') : '') + '","' +
|
|
644
652
|
lText.queryStringify(cookie).replace(/"/g, '""') + '","' +
|
|
645
|
-
lText.stringifyJson(jwt).replace(/"/g, '""') + '","' +
|
|
646
653
|
lText.stringifyJson(session).replace(/"/g, '""') + '","' +
|
|
647
654
|
(headers['user-agent']?.replace(/"/g, '""') ?? 'No HTTP_USER_AGENT') + '","' +
|
|
648
655
|
realIp.replace(/"/g, '""') + '","' +
|
|
@@ -795,7 +802,7 @@ export function debug(message, ...optionalParams) {
|
|
|
795
802
|
*/
|
|
796
803
|
export function display(message, ...optionalParams) {
|
|
797
804
|
// eslint-disable-next-line no-console
|
|
798
|
-
console.log(message, ...optionalParams);
|
|
805
|
+
console.log(`KE-DISPLAY ${lTime.format(null, 'Y-m-d H:i:s')}`, message, ...optionalParams);
|
|
799
806
|
}
|
|
800
807
|
/**
|
|
801
808
|
* --- 让 res 发送头部(前提是头部没有被发送才能调用本方法 ---
|
package/lib/crypto.d.ts
CHANGED
|
@@ -73,8 +73,10 @@ export declare const AES_256_CBC = "aes-256-cbc";
|
|
|
73
73
|
export declare const AES_256_CTR = "aes-256-ctr";
|
|
74
74
|
/** --- 非流直接使用 GCM --- */
|
|
75
75
|
export declare const AES_256_GCM = "aes-256-gcm";
|
|
76
|
+
/** --- SM4 如果未设置 iv,则默认这个 --- */
|
|
76
77
|
export declare const SM4_ECB = "sm4-ecb";
|
|
77
78
|
export declare const SM4_CBC = "sm4-cbc";
|
|
79
|
+
/** --- SM4 一般用这个,设置 iv,自动就切换成了这个 --- */
|
|
78
80
|
export declare const SM4_CFB = "sm4-cfb";
|
|
79
81
|
/**
|
|
80
82
|
* --- cipher 加密,强烈不建议使用 AES_256_ECB ---
|
|
@@ -82,6 +84,7 @@ export declare const SM4_CFB = "sm4-cfb";
|
|
|
82
84
|
* @param key 密钥 32 个英文字母和数字
|
|
83
85
|
* @param iv 向量 16(CTR) 或 12(GCM) 个英文字母和数字
|
|
84
86
|
* @param method 加密方法
|
|
87
|
+
* @param output 输出类型
|
|
85
88
|
*/
|
|
86
89
|
export declare function cipherEncrypt(original: string | Buffer, key: crypto.CipherKey, iv?: string, method?: string, output?: 'base64' | 'buffer'): string | Buffer | false;
|
|
87
90
|
/**
|
|
@@ -117,6 +120,7 @@ export declare function sm4Encrypt(original: string | Buffer, key: crypto.Cipher
|
|
|
117
120
|
* @param key 密钥 32 个英文字母和数字
|
|
118
121
|
* @param iv 向量 16(CTR) 或 12(GCM) 个英文字母和数字
|
|
119
122
|
* @param method 加密方法
|
|
123
|
+
* @param output 输出类型
|
|
120
124
|
*/
|
|
121
125
|
export declare function cipherDecrypt(encrypt: string | Buffer, key: crypto.CipherKey, iv?: string, method?: string, output?: 'binary' | 'buffer'): string | Buffer | false;
|
|
122
126
|
/**
|
|
@@ -132,8 +136,7 @@ export declare function aesDecrypt(encrypt: string | Buffer, key: crypto.CipherK
|
|
|
132
136
|
* --- AES 解密 ---
|
|
133
137
|
* @param encrypt 需解密的字符串
|
|
134
138
|
* @param key 密钥 32 个英文字母和数字
|
|
135
|
-
* @param
|
|
136
|
-
* @param method 加密方法
|
|
139
|
+
* @param output 输出类型
|
|
137
140
|
*/
|
|
138
141
|
export declare function gcmDecrypt(encrypt: string | Buffer, key: crypto.CipherKey, output: 'buffer'): Buffer | false;
|
|
139
142
|
export declare function gcmDecrypt(encrypt: string | Buffer, key: crypto.CipherKey, output?: 'binary'): string | false;
|
|
@@ -154,6 +157,12 @@ export declare function sm4Decrypt(encrypt: string | Buffer, key: crypto.CipherK
|
|
|
154
157
|
*/
|
|
155
158
|
export declare function hashHmac(algorithm: string, data: Buffer | string, key?: crypto.CipherKey, format?: 'hex' | 'base64'): string;
|
|
156
159
|
export declare function hashHmac(algorithm: string, data: Buffer | string, key: crypto.CipherKey | undefined, format: 'buffer'): Buffer;
|
|
160
|
+
/**
|
|
161
|
+
* --- hash 或 hmac 加密文件 ---
|
|
162
|
+
* @param algorithm 加密方式,如 md5、sha256、sm3 等
|
|
163
|
+
* @param path 文件路径
|
|
164
|
+
* @param key 设置则采用 hmac 加密
|
|
165
|
+
*/
|
|
157
166
|
export declare function hashHmacFile(algorithm: string, path: string, key?: crypto.CipherKey, encoding?: 'hex' | 'base64' | 'base64url'): Promise<string | false>;
|
|
158
167
|
export declare function hashHmacFile(algorithm: string, path: string, key: crypto.CipherKey, encoding: 'buffer'): Promise<Buffer | false>;
|
|
159
168
|
/**
|
package/lib/crypto.js
CHANGED
|
@@ -97,15 +97,18 @@ export const AES_256_CBC = 'aes-256-cbc';
|
|
|
97
97
|
export const AES_256_CTR = 'aes-256-ctr';
|
|
98
98
|
/** --- 非流直接使用 GCM --- */
|
|
99
99
|
export const AES_256_GCM = 'aes-256-gcm';
|
|
100
|
-
|
|
100
|
+
/** --- SM4 如果未设置 iv,则默认这个 --- */
|
|
101
|
+
export const SM4_ECB = 'sm4-ecb';
|
|
101
102
|
export const SM4_CBC = 'sm4-cbc';
|
|
102
|
-
|
|
103
|
+
/** --- SM4 一般用这个,设置 iv,自动就切换成了这个 --- */
|
|
104
|
+
export const SM4_CFB = 'sm4-cfb';
|
|
103
105
|
/**
|
|
104
106
|
* --- cipher 加密,强烈不建议使用 AES_256_ECB ---
|
|
105
107
|
* @param original 原始字符串
|
|
106
108
|
* @param key 密钥 32 个英文字母和数字
|
|
107
109
|
* @param iv 向量 16(CTR) 或 12(GCM) 个英文字母和数字
|
|
108
110
|
* @param method 加密方法
|
|
111
|
+
* @param output 输出类型
|
|
109
112
|
*/
|
|
110
113
|
export function cipherEncrypt(original, key, iv = '', method = AES_256_ECB, output = 'base64') {
|
|
111
114
|
try {
|
|
@@ -184,6 +187,7 @@ export function sm4Encrypt(original, key, iv = '', method = SM4_ECB, output = 'b
|
|
|
184
187
|
* @param key 密钥 32 个英文字母和数字
|
|
185
188
|
* @param iv 向量 16(CTR) 或 12(GCM) 个英文字母和数字
|
|
186
189
|
* @param method 加密方法
|
|
190
|
+
* @param output 输出类型
|
|
187
191
|
*/
|
|
188
192
|
export function cipherDecrypt(encrypt, key, iv = '', method = AES_256_ECB, output = 'binary') {
|
|
189
193
|
try {
|
|
@@ -265,12 +269,6 @@ export function hashHmac(algorithm, data, key, format = 'hex') {
|
|
|
265
269
|
return cry.digest(format);
|
|
266
270
|
}
|
|
267
271
|
}
|
|
268
|
-
/**
|
|
269
|
-
* --- hash 或 hmac 加密文件 ---
|
|
270
|
-
* @param algorithm 加密方式,如 md5、sha256、sm3 等
|
|
271
|
-
* @param path 文件路径
|
|
272
|
-
* @param key 设置则采用 hmac 加密
|
|
273
|
-
*/
|
|
274
272
|
export function hashHmacFile(algorithm, path, key, encoding = 'hex') {
|
|
275
273
|
return new Promise(function (resolve) {
|
|
276
274
|
const cry = key ? crypto.createHmac(algorithm, key) : crypto.createHash(algorithm);
|
package/lib/db/conn.js
CHANGED
|
@@ -276,7 +276,7 @@ export class Connection {
|
|
|
276
276
|
return false;
|
|
277
277
|
}
|
|
278
278
|
}
|
|
279
|
-
// --- 事务,只能在独占连接中使用,pool 创建事务返回独占连接,commit 或 rollback
|
|
279
|
+
// --- 事务,只能在独占连接中使用,pool 创建事务返回独占连接,commit 或 rollback 释放连接回连接池 ---
|
|
280
280
|
async beginTransaction() {
|
|
281
281
|
if (this._using) {
|
|
282
282
|
try {
|
package/lib/db.d.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
import * as kebab from '#kebab/index.js';
|
|
7
7
|
import * as sCtr from '#kebab/sys/ctr.js';
|
|
8
8
|
import { Connection } from './db/conn.js';
|
|
9
|
-
import { Pool } from './db/pool.js';
|
|
9
|
+
import { Pool, IConnectionInfo, getConnectionList } from './db/pool.js';
|
|
10
10
|
import { Transaction } from './db/tran.js';
|
|
11
11
|
/** --- 服务商定义 --- */
|
|
12
12
|
export declare enum ESERVICE {
|
|
@@ -53,10 +53,12 @@ export interface IPacket {
|
|
|
53
53
|
}
|
|
54
54
|
/**
|
|
55
55
|
* --- 获取 Db Pool 对象 ---
|
|
56
|
-
* @param
|
|
56
|
+
* @param ctrEtc 控制器对象或数据库配置信息
|
|
57
|
+
* @param opt 选项
|
|
58
|
+
* @returns Db Pool 对象
|
|
57
59
|
*/
|
|
58
60
|
export declare function get(ctrEtc: sCtr.Ctr | kebab.IConfigDb, opt?: {
|
|
59
61
|
/** --- 服务商,默认 PGSQL --- */
|
|
60
62
|
'service'?: ESERVICE;
|
|
61
63
|
}): Pool;
|
|
62
|
-
export { Connection, Pool, Transaction };
|
|
64
|
+
export { Connection, Pool, IConnectionInfo, getConnectionList, Transaction };
|
package/lib/db.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import * as sCtr from '#kebab/sys/ctr.js';
|
|
7
7
|
import { Connection } from './db/conn.js';
|
|
8
|
-
import { Pool } from './db/pool.js';
|
|
8
|
+
import { Pool, getConnectionList } from './db/pool.js';
|
|
9
9
|
import { Transaction } from './db/tran.js';
|
|
10
10
|
/** --- 服务商定义 --- */
|
|
11
11
|
export var ESERVICE;
|
|
@@ -15,7 +15,9 @@ export var ESERVICE;
|
|
|
15
15
|
})(ESERVICE || (ESERVICE = {}));
|
|
16
16
|
/**
|
|
17
17
|
* --- 获取 Db Pool 对象 ---
|
|
18
|
-
* @param
|
|
18
|
+
* @param ctrEtc 控制器对象或数据库配置信息
|
|
19
|
+
* @param opt 选项
|
|
20
|
+
* @returns Db Pool 对象
|
|
19
21
|
*/
|
|
20
22
|
export function get(ctrEtc, opt = {}) {
|
|
21
23
|
if (ctrEtc instanceof sCtr.Ctr) {
|
|
@@ -30,4 +32,4 @@ export function get(ctrEtc, opt = {}) {
|
|
|
30
32
|
'service': opt.service ?? ESERVICE.PGSQL,
|
|
31
33
|
});
|
|
32
34
|
}
|
|
33
|
-
export { Connection, Pool, Transaction };
|
|
35
|
+
export { Connection, Pool, getConnectionList, Transaction };
|