@maiyunnet/kebab 3.2.29 → 3.2.31

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/index.d.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  * --- 本文件用来定义每个目录实体地址的常量 ---
6
6
  */
7
7
  /** --- 当前系统版本号 --- */
8
- export declare const VER = "3.2.29";
8
+ export declare const VER = "3.2.31";
9
9
  /** --- 框架根目录,以 / 结尾 --- */
10
10
  export declare const ROOT_PATH: string;
11
11
  export declare const LIB_PATH: string;
package/index.js CHANGED
@@ -6,7 +6,7 @@
6
6
  * --- 本文件用来定义每个目录实体地址的常量 ---
7
7
  */
8
8
  /** --- 当前系统版本号 --- */
9
- export const VER = '3.2.29';
9
+ export const VER = '3.2.31';
10
10
  // --- 服务端用的路径 ---
11
11
  const imu = decodeURIComponent(import.meta.url).replace('file://', '').replace(/^\/(\w:)/, '$1');
12
12
  /** --- /xxx/xxx --- */
package/lib/ai.d.ts CHANGED
@@ -16,7 +16,11 @@ export declare enum ESERVICE {
16
16
  /** --- 阿里国际区 --- */
17
17
  'ALIAS' = 1,
18
18
  /** --- 微软 Azure --- */
19
- 'AZURE' = 2
19
+ 'AZURE' = 2,
20
+ /** --- 微软 Azure 2 --- */
21
+ 'AZURE2' = 3,
22
+ /** --- 微软 Azure 3 --- */
23
+ 'AZURE3' = 4
20
24
  }
21
25
  /** --- 选项 --- */
22
26
  export interface IOptions {
package/lib/ai.js CHANGED
@@ -17,6 +17,10 @@ export var ESERVICE;
17
17
  ESERVICE[ESERVICE["ALIAS"] = 1] = "ALIAS";
18
18
  /** --- 微软 Azure --- */
19
19
  ESERVICE[ESERVICE["AZURE"] = 2] = "AZURE";
20
+ /** --- 微软 Azure 2 --- */
21
+ ESERVICE[ESERVICE["AZURE2"] = 3] = "AZURE2";
22
+ /** --- 微软 Azure 3 --- */
23
+ ESERVICE[ESERVICE["AZURE3"] = 4] = "AZURE3";
20
24
  })(ESERVICE || (ESERVICE = {}));
21
25
  /** --- openai 的连接对象 --- */
22
26
  const links = [];
@@ -38,6 +42,14 @@ export class Ai {
38
42
  endpoint = opt.endpoint ?? config.ai?.[ESERVICE[opt.service]]?.endpoint ?? '';
39
43
  break;
40
44
  }
45
+ case ESERVICE.AZURE2: {
46
+ endpoint = opt.endpoint ?? config.ai?.[ESERVICE[opt.service]]?.endpoint ?? '';
47
+ break;
48
+ }
49
+ case ESERVICE.AZURE3: {
50
+ endpoint = opt.endpoint ?? config.ai?.[ESERVICE[opt.service]]?.endpoint ?? '';
51
+ break;
52
+ }
41
53
  default: {
42
54
  endpoint = undefined;
43
55
  }
package/lib/core.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import * as http from 'http';
2
2
  import * as http2 from 'http2';
3
3
  import * as stream from 'stream';
4
+ import * as net from 'net';
4
5
  import * as kebab from '#kebab/index.js';
5
6
  import * as lResponse from '#kebab/lib/net/response.js';
6
7
  import * as sCtr from '#kebab/sys/ctr.js';
@@ -255,4 +256,4 @@ export declare function writeEventStreamHead(res: http2.Http2ServerResponse | ht
255
256
  * @param res 响应对象
256
257
  * @param data 数据
257
258
  */
258
- export declare function write(res: http2.Http2ServerResponse | http.ServerResponse, data: string | Buffer): void;
259
+ export declare function write(res: http2.Http2ServerResponse | http.ServerResponse | net.Socket, data: string | Buffer): void;
package/lib/core.js CHANGED
@@ -7,6 +7,7 @@ import * as cp from 'child_process';
7
7
  import * as http2 from 'http2';
8
8
  import * as stream from 'stream';
9
9
  import * as os from 'os';
10
+ import * as net from 'net';
10
11
  import * as kebab from '#kebab/index.js';
11
12
  import * as lTime from '#kebab/lib/time.js';
12
13
  import * as lFs from '#kebab/lib/fs.js';
@@ -821,6 +822,9 @@ export function write(res, data) {
821
822
  if (res instanceof http2.Http2ServerResponse) {
822
823
  res.write(data);
823
824
  }
825
+ else if (res instanceof net.Socket) {
826
+ res.write(data);
827
+ }
824
828
  else {
825
829
  res.write(data);
826
830
  }
package/lib/db.d.ts CHANGED
@@ -15,6 +15,8 @@ export interface IData {
15
15
  'errno': number;
16
16
  [key: string]: any;
17
17
  } | null;
18
+ /** --- 1-正常,-500-服务器错误 --- */
19
+ 'result': number;
18
20
  }
19
21
  /** --- exec 返回对象 --- */
20
22
  export interface IPacket {
@@ -49,6 +51,7 @@ export declare class Pool {
49
51
  * --- 执行一条 SQL,无视顺序和相同连接,随用随取 ---
50
52
  * @param sql 执行的 SQL 字符串
51
53
  * @param values 要替换的 data 数据
54
+ * @returns error.errno = -500 表示系统错误
52
55
  */
53
56
  query(sql: string, values?: kebab.DbValue[]): Promise<IData>;
54
57
  /**
package/lib/db.js CHANGED
@@ -79,6 +79,7 @@ export class Pool {
79
79
  * --- 执行一条 SQL,无视顺序和相同连接,随用随取 ---
80
80
  * @param sql 执行的 SQL 字符串
81
81
  * @param values 要替换的 data 数据
82
+ * @returns error.errno = -500 表示系统错误
82
83
  */
83
84
  async query(sql, values) {
84
85
  ++this._queries;
@@ -89,9 +90,10 @@ export class Pool {
89
90
  'rows': null,
90
91
  'fields': [],
91
92
  'error': {
92
- 'message': 'null',
93
- 'errno': 0
94
- }
93
+ 'message': 'false',
94
+ 'errno': 0,
95
+ },
96
+ 'result': -500,
95
97
  };
96
98
  }
97
99
  // --- 执行一次后自动解除 using ---
@@ -237,9 +239,10 @@ export class Transaction {
237
239
  'rows': null,
238
240
  'fields': [],
239
241
  'error': {
240
- 'message': 'null',
242
+ 'message': 'false',
241
243
  'errno': 0
242
- }
244
+ },
245
+ 'result': -500,
243
246
  };
244
247
  }
245
248
  ++this._queries;
@@ -439,7 +442,8 @@ export class Connection {
439
442
  return {
440
443
  'rows': null,
441
444
  'fields': [],
442
- 'error': e
445
+ 'error': e,
446
+ 'result': -500,
443
447
  };
444
448
  }
445
449
  if (!this._transaction) {
@@ -449,7 +453,8 @@ export class Connection {
449
453
  return {
450
454
  'rows': res[0],
451
455
  'fields': res[1],
452
- 'error': null
456
+ 'error': null,
457
+ 'result': 1,
453
458
  };
454
459
  }
455
460
  /**
package/lib/kv.d.ts CHANGED
@@ -62,7 +62,7 @@ export declare class Pool {
62
62
  * --- 获取字符串 ---
63
63
  * @param key
64
64
  */
65
- get(key: string): Promise<string | null>;
65
+ get(key: string): Promise<string | false | null>;
66
66
  /**
67
67
  * --- 获取相应的剩余有效期秒数 ---
68
68
  * @param key
@@ -86,8 +86,9 @@ export declare class Pool {
86
86
  /**
87
87
  * --- 获取 json 对象 ---
88
88
  * @param key
89
+ * @returns false 表示系统错误, null 表示不存在, 其他值为 json 对象
89
90
  */
90
- getJson(key: string): Promise<any | null>;
91
+ getJson(key: string): Promise<any | false | null>;
91
92
  /**
92
93
  * --- 删除已存在的值 ---
93
94
  * @param keys
@@ -301,9 +302,9 @@ export declare class Connection {
301
302
  * --- 获取字符串 ---
302
303
  * @param key
303
304
  * @param etc
304
- * @returns 字符串或 null(即使存入时是 number,这个方法也只会返回字符串)
305
+ * @returns 字符串 / false / null(即使存入时是 number,这个方法也只会返回字符串)
305
306
  */
306
- get(key: string, etc: kebab.IConfigKv): Promise<string | null>;
307
+ get(key: string, etc: kebab.IConfigKv): Promise<string | false | null>;
307
308
  /**
308
309
  * --- 获取相应的剩余有效期秒数 ---
309
310
  * @param key
@@ -334,7 +335,7 @@ export declare class Connection {
334
335
  * @param key
335
336
  * @param etc
336
337
  */
337
- getJson(key: string, etc: kebab.IConfigKv): Promise<any | null>;
338
+ getJson(key: string, etc: kebab.IConfigKv): Promise<any | false | null>;
338
339
  /**
339
340
  * --- 删除已存在的值 ---
340
341
  * @param keys
package/lib/kv.js CHANGED
@@ -236,11 +236,12 @@ export class Pool {
236
236
  /**
237
237
  * --- 获取 json 对象 ---
238
238
  * @param key
239
+ * @returns false 表示系统错误, null 表示不存在, 其他值为 json 对象
239
240
  */
240
241
  async getJson(key) {
241
242
  const conn = await this._getConnection();
242
243
  if (!conn) {
243
- return null;
244
+ return false;
244
245
  }
245
246
  const r = await conn.getJson(key, this._etc);
246
247
  conn.used();
@@ -823,7 +824,7 @@ end`;
823
824
  * --- 获取字符串 ---
824
825
  * @param key
825
826
  * @param etc
826
- * @returns 字符串或 null(即使存入时是 number,这个方法也只会返回字符串)
827
+ * @returns 字符串 / false / null(即使存入时是 number,这个方法也只会返回字符串)
827
828
  */
828
829
  async get(key, etc) {
829
830
  this.refreshLast();
@@ -831,7 +832,7 @@ end`;
831
832
  return await this._link.get(etc.pre + key);
832
833
  }
833
834
  catch {
834
- return null;
835
+ return false;
835
836
  }
836
837
  }
837
838
  /**
@@ -917,11 +918,11 @@ end`;
917
918
  */
918
919
  async getJson(key, etc) {
919
920
  const v = await this.get(key, etc);
920
- if (v === null) {
921
- return null;
921
+ if (v === null || v === false) {
922
+ return v;
922
923
  }
923
924
  const r = lText.parseJson(v);
924
- return r === false ? null : r;
925
+ return r;
925
926
  }
926
927
  /**
927
928
  * --- 删除已存在的值 ---
package/lib/session.d.ts CHANGED
@@ -31,6 +31,7 @@ export declare class Session {
31
31
  * @param link Kv 或 Db 实例
32
32
  * @param auth 设为 true 则优先从头 Authorization 或 post _auth 值读取 token
33
33
  * @param opt 选项
34
+ * @returns false 表示系统错误
34
35
  */
35
36
  init(ctr: ctr.Ctr, link: db.Pool | kv.Pool, auth?: boolean, opt?: IOptions): Promise<boolean>;
36
37
  /**
package/lib/session.js CHANGED
@@ -36,6 +36,7 @@ export class Session {
36
36
  * @param link Kv 或 Db 实例
37
37
  * @param auth 设为 true 则优先从头 Authorization 或 post _auth 值读取 token
38
38
  * @param opt 选项
39
+ * @returns false 表示系统错误
39
40
  */
40
41
  async init(ctr, link, auth = false, opt = {}) {
41
42
  const config = ctr.getPrototype('_config');
@@ -68,10 +69,13 @@ export class Session {
68
69
  // --- 如果启用了内存加速则在内存找 ---
69
70
  if (this._link instanceof kv.Pool) {
70
71
  // --- Kv ---
71
- let data;
72
- if ((data = await this._link.getJson(this._name + '_' + this._token)) === null) {
72
+ const data = await this._link.getJson(this._name + '_' + this._token);
73
+ if (data === null) {
73
74
  needInsert = true;
74
75
  }
76
+ else if (data === false) {
77
+ return false;
78
+ }
75
79
  else {
76
80
  session = data;
77
81
  }
@@ -86,6 +90,9 @@ export class Session {
86
90
  if (data.rows?.[0]) {
87
91
  session = text.parseJson(data.rows[0].data);
88
92
  }
93
+ else if (data.result === -500) {
94
+ return false;
95
+ }
89
96
  else {
90
97
  needInsert = true;
91
98
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@maiyunnet/kebab",
3
- "version": "3.2.29",
3
+ "version": "3.2.31",
4
4
  "description": "Simple, easy-to-use, and fully-featured Node.js framework that is ready-to-use out of the box.",
5
5
  "type": "module",
6
6
  "keywords": [
package/sys/cmd.js CHANGED
@@ -155,6 +155,12 @@ async function run() {
155
155
  config.ai['AZURE'] ??= {};
156
156
  config.ai['AZURE'].endpoint ??= '';
157
157
  config.ai['AZURE'].skey ??= '';
158
+ config.ai['AZURE2'] ??= {};
159
+ config.ai['AZURE2'].endpoint ??= '';
160
+ config.ai['AZURE2'].skey ??= '';
161
+ config.ai['AZURE3'] ??= {};
162
+ config.ai['AZURE3'].endpoint ??= '';
163
+ config.ai['AZURE3'].skey ??= '';
158
164
  // --- config - vector ---
159
165
  config.vector ??= {};
160
166
  config.vector.host ??= '127.0.0.1';
package/sys/ctr.d.ts CHANGED
@@ -206,7 +206,7 @@ export declare class Ctr {
206
206
  * @param auth 设为 true 则从头 Authorization 或 post _auth 值读取 token
207
207
  * @param opt name, ttl, ssl, sqlPre
208
208
  */
209
- protected _startSession(link: lDb.Pool | lKv.Pool, auth?: boolean, opt?: lSession.IOptions): Promise<void>;
209
+ protected _startSession(link: lDb.Pool | lKv.Pool, auth?: boolean, opt?: lSession.IOptions): Promise<boolean>;
210
210
  /**
211
211
  * --- 设定语言并加载语言包 ---
212
212
  * @param loc 要加载的目标语言
package/sys/ctr.js CHANGED
@@ -603,9 +603,9 @@ export class Ctr {
603
603
  * @param auth 设为 true 则从头 Authorization 或 post _auth 值读取 token
604
604
  * @param opt name, ttl, ssl, sqlPre
605
605
  */
606
- async _startSession(link, auth = false, opt = {}) {
606
+ _startSession(link, auth = false, opt = {}) {
607
607
  this._sess = new lSession.Session();
608
- await this._sess.init(this, link, auth, opt);
608
+ return this._sess.init(this, link, auth, opt);
609
609
  }
610
610
  // --- 本地化 ---
611
611
  /**
@@ -2134,8 +2134,8 @@ function confirm() {
2134
2134
  echo.push('link = lKv.get(this);\n');
2135
2135
  }
2136
2136
  if (this._get['auth'] === '') {
2137
- await this._startSession(link, false, { 'ttl': 60 });
2138
- echo.push(`await this._startSession(link, false, {'ttl': 60});
2137
+ const r = await this._startSession(link, false, { 'ttl': 60 });
2138
+ echo.push(`await this._startSession(link, false, {'ttl': 60}); // ${r}
2139
2139
  JSON.stringify(this._session);</pre>` + lText.htmlescape(JSON.stringify(this._session)));
2140
2140
  this._session['value'] = lText.logicalOr(this._get['value'], 'ok');
2141
2141
  echo.push(`<pre>this._session['value'] = '${lText.logicalOr(this._get['value'], 'ok')}';
@@ -2147,7 +2147,7 @@ JSON.stringify(this._session);</pre>` + lText.htmlescape(JSON.stringify(this._se
2147
2147
  }
2148
2148
  else {
2149
2149
  // --- AUTH 模式 ---
2150
- await this._startSession(link, true, { 'ttl': 60 });
2150
+ const r = await this._startSession(link, true, { 'ttl': 60 });
2151
2151
  if (Object.keys(this._post).length > 0) {
2152
2152
  if (this._session['count'] === undefined) {
2153
2153
  this._session['count'] = 1;
@@ -2158,7 +2158,7 @@ JSON.stringify(this._session);</pre>` + lText.htmlescape(JSON.stringify(this._se
2158
2158
  return [1, { 'txt': 'this._session: ' + JSON.stringify(this._session) + '\nToken: ' + this._sess.getToken(), 'token': this._sess?.getToken(), '_auth': this._getBasicAuth('token', this._sess.getToken()) }];
2159
2159
  }
2160
2160
  else {
2161
- echo.push(`await this._startSession(link, true, {'ttl': 60});
2161
+ echo.push(`await this._startSession(link, true, {'ttl': 60}); // ${r}
2162
2162
  JSON.stringify(this._session));</pre>` + lText.htmlescape(JSON.stringify(this._session)));
2163
2163
  this._session['value'] = lTime.format(this, 'H:i:s');
2164
2164
  echo.push(`<pre>this._session['value'] = '${lTime.format(this, 'H:i:s')}';
@@ -3153,7 +3153,6 @@ const content = document.getElementById('content');
3153
3153
  send.addEventListener('click', async () => {
3154
3154
  if (send.innerHTML === 'Stop') {
3155
3155
  controller.abort();
3156
- send.innerHTML = 'Send';
3157
3156
  return;
3158
3157
  }
3159
3158
  if (!text.value) {
@@ -3176,19 +3175,24 @@ send.addEventListener('click', async () => {
3176
3175
  const decoder = new TextDecoder('utf8');
3177
3176
  let buf = '';
3178
3177
  while (true) {
3179
- const { value, done } = await reader.read();
3180
- if (done) {
3181
- break;
3182
- }
3183
- buf += decoder.decode(value, { 'stream': true, });
3184
- if (!buf.includes('\\n\\n')) {
3185
- // --- 还没接收完 ---
3186
- continue;
3178
+ try {
3179
+ const { value, done } = await reader.read();
3180
+ if (done) {
3181
+ break;
3182
+ }
3183
+ buf += decoder.decode(value, { 'stream': true, });
3184
+ if (!buf.includes('\\n\\n')) {
3185
+ // --- 还没接收完 ---
3186
+ continue;
3187
+ }
3188
+ const events = buf.split('\\n\\n');
3189
+ buf = events.pop(); // --- 最后一个可能不完整 ---
3190
+ for (const ev of events) {
3191
+ content.textContent += JSON.parse(ev.slice(5).trim());
3192
+ }
3187
3193
  }
3188
- const events = buf.split('\\n\\n');
3189
- buf = events.pop(); // --- 最后一个可能不完整 ---
3190
- for (const ev of events) {
3191
- content.textContent += JSON.parse(ev.slice(5).trim());
3194
+ catch {
3195
+ break;
3192
3196
  }
3193
3197
  }
3194
3198
  send.innerHTML = 'Send';
@@ -3197,6 +3201,12 @@ send.addEventListener('click', async () => {
3197
3201
  return echo + '<br>' + this._getEnd();
3198
3202
  }
3199
3203
  async aiStream1() {
3204
+ if (!this._cross()) {
3205
+ return '';
3206
+ }
3207
+ if (!this._post['content']) {
3208
+ return '';
3209
+ }
3200
3210
  const ai = lAi.get(this, {
3201
3211
  'service': lAi.ESERVICE.ALICN,
3202
3212
  });
@@ -3224,9 +3234,10 @@ send.addEventListener('click', async () => {
3224
3234
  continue;
3225
3235
  }
3226
3236
  if (!this._isAvail) {
3227
- // --- 测试上面 abort 后还会执行到这里吗 ---
3228
- // --- 测试结果:确实不会 ---
3229
- lCore.debug('Client has been closed');
3237
+ // --- 测试上面 abort 后不 break 的话还会执行到这里吗 ---
3238
+ // --- 测试结果:可能会,大概率只有一次,就证明连接确实断开了只不过有延迟 ---
3239
+ // --- 但上面 break 的话就肯定不会执行到这里了 ---
3240
+ lCore.debug('Client has been closed', content);
3230
3241
  continue;
3231
3242
  }
3232
3243
  lCore.write(this._res, 'data: ' + JSON.stringify(content) + '\n\n');
@@ -3240,6 +3251,7 @@ send.addEventListener('click', async () => {
3240
3251
  lCore.debug(`Output Tokens: ${chunk.usage.completion_tokens}`);
3241
3252
  lCore.debug(`Total Tokens: ${chunk.usage.total_tokens}`);
3242
3253
  }
3254
+ lCore.debug('AI DONE');
3243
3255
  }
3244
3256
  /**
3245
3257
  * --- END ---