@maiyunnet/kebab 3.2.24 → 3.2.26

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.24";
8
+ export declare const VER = "3.2.26";
9
9
  /** --- 框架根目录,以 / 结尾 --- */
10
10
  export declare const ROOT_PATH: string;
11
11
  export declare const LIB_PATH: string;
@@ -84,6 +84,8 @@ export interface IConfigS3 {
84
84
  }
85
85
  /** --- AI --- */
86
86
  export interface IConfigAi {
87
+ /** --- 目前只有微软 Azure 有 --- */
88
+ 'endpoint'?: string;
87
89
  'skey': string;
88
90
  }
89
91
  /** --- 向量数据库 --- */
package/index.js CHANGED
@@ -6,7 +6,7 @@
6
6
  * --- 本文件用来定义每个目录实体地址的常量 ---
7
7
  */
8
8
  /** --- 当前系统版本号 --- */
9
- export const VER = '3.2.24';
9
+ export const VER = '3.2.26';
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
@@ -14,14 +14,20 @@ export declare enum ESERVICE {
14
14
  /** --- 阿里中国大陆区 --- */
15
15
  'ALICN' = 0,
16
16
  /** --- 阿里国际区 --- */
17
- 'ALIAS' = 1
17
+ 'ALIAS' = 1,
18
+ /** --- 微软 Azure --- */
19
+ 'AZURE' = 2
18
20
  }
19
21
  /** --- 选项 --- */
20
22
  export interface IOptions {
21
23
  /** --- 服务商 ---- */
22
24
  'service': ESERVICE;
23
- /** --- 密钥值 --- */
25
+ /** --- 接入点 --- */
26
+ 'endpoint'?: string;
27
+ /** --- 密钥 --- */
24
28
  'secretKey'?: string;
29
+ /** --- 自定义 fetch 函数 --- */
30
+ 'fetch'?: (input: string | URL | Request, init?: RequestInit) => Promise<Response>;
25
31
  }
26
32
  export declare class Ai {
27
33
  /** --- openai 原生对象,建议只读 --- */
package/lib/ai.js CHANGED
@@ -15,6 +15,8 @@ export var ESERVICE;
15
15
  ESERVICE[ESERVICE["ALICN"] = 0] = "ALICN";
16
16
  /** --- 阿里国际区 --- */
17
17
  ESERVICE[ESERVICE["ALIAS"] = 1] = "ALIAS";
18
+ /** --- 微软 Azure --- */
19
+ ESERVICE[ESERVICE["AZURE"] = 2] = "AZURE";
18
20
  })(ESERVICE || (ESERVICE = {}));
19
21
  /** --- openai 的连接对象 --- */
20
22
  const links = [];
@@ -25,11 +27,15 @@ export class Ai {
25
27
  let endpoint;
26
28
  switch (opt.service) {
27
29
  case ESERVICE.ALICN: {
28
- endpoint = `https://dashscope.aliyuncs.com/compatible-mode/v1`;
30
+ endpoint = opt.endpoint ?? `https://dashscope.aliyuncs.com/compatible-mode/v1`;
29
31
  break;
30
32
  }
31
33
  case ESERVICE.ALIAS: {
32
- endpoint = `https://dashscope-intl.aliyuncs.com/compatible-mode/v1`;
34
+ endpoint = opt.endpoint ?? `https://dashscope-intl.aliyuncs.com/compatible-mode/v1`;
35
+ break;
36
+ }
37
+ case ESERVICE.AZURE: {
38
+ endpoint = opt.endpoint ?? config.ai?.[ESERVICE[opt.service]]?.endpoint ?? '';
33
39
  break;
34
40
  }
35
41
  default: {
@@ -45,6 +51,7 @@ export class Ai {
45
51
  this.link = new openai.OpenAI({
46
52
  'apiKey': secretKey,
47
53
  'baseURL': endpoint,
54
+ 'fetch': opt.fetch,
48
55
  });
49
56
  links.push({
50
57
  'token': token,
package/lib/net.d.ts CHANGED
@@ -47,6 +47,19 @@ export declare function postJson(u: string, data: kebab.Json[] | Record<string,
47
47
  * @returns JSON 数据,失败时返回 null
48
48
  */
49
49
  export declare function postJsonResponseJson(u: string, data: kebab.Json[] | Record<string, kebab.Json>, opt?: IRequestOptions): Promise<any | null>;
50
+ /**
51
+ * --- 发起一个原生 fetch 请求,增加了一些框架选项,注意:会抛出错误 ---
52
+ * @param input 请求的 URL 或 Request 对象
53
+ * @param init 增加 mproxy
54
+ */
55
+ export declare function fetch(input: string | URL | Request, init?: RequestInit & {
56
+ /** --- 正向 mproxy 代理,url 如 https://xxx/abc --- */
57
+ 'mproxy'?: {
58
+ 'url': string;
59
+ 'auth': string;
60
+ 'data'?: any;
61
+ };
62
+ }): Promise<Response>;
50
63
  /**
51
64
  * --- 发起一个请求 ---
52
65
  * @param opt 配置项
package/lib/net.js CHANGED
@@ -88,6 +88,29 @@ export async function postJsonResponseJson(u, data, opt = {}) {
88
88
  }
89
89
  return json;
90
90
  }
91
+ /**
92
+ * --- 发起一个原生 fetch 请求,增加了一些框架选项,注意:会抛出错误 ---
93
+ * @param input 请求的 URL 或 Request 对象
94
+ * @param init 增加 mproxy
95
+ */
96
+ export function fetch(input, init = {}) {
97
+ const u = (input instanceof Request) ?
98
+ input.url :
99
+ input instanceof URL ? input.toString() : input;
100
+ const uri = lText.parseUrl(u);
101
+ /** --- 正向代理的地址 --- */
102
+ const puri = init.mproxy ? lText.parseUrl(init.mproxy.url) : null;
103
+ /** --- 定义请求 host --- */
104
+ const hostname = (puri ? puri.hostname : uri.hostname) ?? '';
105
+ if (!hostname) {
106
+ throw new Error('Kebab TypeError: Hostname is empty');
107
+ }
108
+ return global.fetch(init.mproxy ? init.mproxy.url + (init.mproxy.url.includes('?') ? '&' : '?') + lText.queryStringify({
109
+ 'url': u,
110
+ 'auth': init.mproxy.auth,
111
+ 'data': init.mproxy.data ? lText.stringifyJson(init.mproxy.data) : '{}',
112
+ }) : u, init);
113
+ }
91
114
  /**
92
115
  * --- 发起一个请求 ---
93
116
  * @param opt 配置项
@@ -149,19 +172,19 @@ export async function request(u, data, opt = {}) {
149
172
  // --- 发起请求 ---
150
173
  let req;
151
174
  try {
152
- // --- 重定义 IP ---
153
- const host = (puri ? puri.hostname : uri.hostname) ?? '';
154
- if (!host) {
175
+ /** --- 定义请求 host --- */
176
+ const hostname = (puri ? puri.hostname : uri.hostname) ?? '';
177
+ if (!hostname) {
155
178
  const res = new lResponse.Response(null);
156
179
  res.error = {
157
180
  'name': 'Possible mProxy error',
158
- 'message': 'host not found',
181
+ 'message': 'hostname not found',
159
182
  };
160
183
  return res;
161
184
  }
162
185
  if (typeof hosts === 'string' ?
163
186
  !hosts :
164
- (hosts[host] !== undefined && !hosts[host])) {
187
+ (hosts[hostname] !== undefined && !hosts[hostname])) {
165
188
  const res = new lResponse.Response(null);
166
189
  res.error = {
167
190
  'name': 'hosts error',
@@ -185,7 +208,7 @@ export async function request(u, data, opt = {}) {
185
208
  'localAddress': local,
186
209
  'ca': ca,
187
210
  'connectionOptions': {
188
- 'remoteHost': typeof hosts === 'string' ? hosts : hosts[host],
211
+ 'remoteHost': typeof hosts === 'string' ? hosts : hosts[hostname],
189
212
  },
190
213
  });
191
214
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@maiyunnet/kebab",
3
- "version": "3.2.24",
3
+ "version": "3.2.26",
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
@@ -152,6 +152,9 @@ async function run() {
152
152
  config.ai['ALICN'].skey ??= '';
153
153
  config.ai['ALIAS'] ??= {};
154
154
  config.ai['ALIAS'].skey ??= '';
155
+ config.ai['AZURE'] ??= {};
156
+ config.ai['AZURE'].endpoint ??= '';
157
+ config.ai['AZURE'].skey ??= '';
155
158
  // --- config - vector ---
156
159
  config.vector ??= {};
157
160
  config.vector.host ??= '127.0.0.1';
@@ -75,6 +75,8 @@ export default class extends sCtr.Ctr {
75
75
  netMproxy1(): Promise<string | boolean>;
76
76
  netMproxy2(): any[];
77
77
  netFilterheaders(): string;
78
+ netFetch(): Promise<string | boolean>;
79
+ netFetch1(): any[];
78
80
  scan(): Promise<kebab.Json>;
79
81
  scan1(): Promise<kebab.Json>;
80
82
  scan2(): Promise<kebab.Json>;
@@ -166,6 +166,7 @@ export default class extends sCtr.Ctr {
166
166
  `<br><a href="${this._config.const.urlBase}test/net-rproxy/dist/core.js">View "test/net-rproxy/dist/core.js"</a> <a href="${this._config.const.urlBase}test/net-rproxy/package.json">View "package.json"</a>`,
167
167
  `<br><a href="${this._config.const.urlBase}test/net-mproxy">View "test/net-mproxy"</a>`,
168
168
  `<br><a href="${this._config.const.urlBase}test/net-filterheaders">View "test/net-filterheaders"</a>`,
169
+ `<br><a href="${this._config.const.urlBase}test/net-fetch">View "test/net-fetch"</a>`,
169
170
  '<br><br><b>Scan:</b>',
170
171
  `<br><br><a href="${this._config.const.urlBase}test/scan?s=db">View "test/scan?s=db"</a>`,
171
172
  `<br><a href="${this._config.const.urlBase}test/scan?s=kv">View "test/scan?s=kv"</a>`,
@@ -1888,6 +1889,68 @@ error: <pre>${JSON.stringify(res.error, null, 4)}</pre>`);
1888
1889
  echo.push(`const filtered = lNet.filterHeaders(headers);<pre>${JSON.stringify(filtered, null, 4)}</pre>`);
1889
1890
  return echo.join('') + this._getEnd();
1890
1891
  }
1892
+ async netFetch() {
1893
+ const echo = [];
1894
+ try {
1895
+ const res = await lNet.fetch(this._internalUrl + 'test/net-fetch1', {
1896
+ 'method': 'POST',
1897
+ 'headers': {
1898
+ 'content-type': 'application/json',
1899
+ },
1900
+ 'body': JSON.stringify({ 'test': '123' }),
1901
+ });
1902
+ echo.push(`<pre>lNet.fetch('${this._internalUrl}test/net-fetch1', {
1903
+ 'method': 'POST',
1904
+ 'headers': {
1905
+ 'content-type': 'application/json',
1906
+ },
1907
+ 'body': JSON.stringify({ 'test': '123' }),
1908
+ });</pre>
1909
+ headers: <pre>${JSON.stringify(Object.fromEntries(res.headers.entries()), null, 4)}</pre>
1910
+ content: <pre>${await res.text()}</pre>`);
1911
+ }
1912
+ catch (e) {
1913
+ echo.push(`error: <pre>${JSON.stringify(e)}</pre>`);
1914
+ }
1915
+ echo.push(`mproxy:`);
1916
+ try {
1917
+ const res = await lNet.fetch(this._internalUrl + 'test/net-fetch1', {
1918
+ 'method': 'POST',
1919
+ 'headers': {
1920
+ 'content-type': 'application/json',
1921
+ },
1922
+ 'body': JSON.stringify({ 'test': '456' }),
1923
+ 'mproxy': {
1924
+ 'url': this._internalUrl + 'test/net-mproxy1',
1925
+ 'auth': '123456',
1926
+ 'data': { 'test': '789' },
1927
+ },
1928
+ });
1929
+ echo.push(`<pre>lNet.fetch('${this._internalUrl}test/net-fetch1', {
1930
+ 'method': 'POST',
1931
+ 'headers': {
1932
+ 'content-type': 'application/json',
1933
+ },
1934
+ 'body': JSON.stringify({ 'test': '123' }),
1935
+ 'mproxy': {
1936
+ 'url': this._internalUrl + 'test/net-mproxy1',
1937
+ 'auth': '123456',
1938
+ 'data': { 'test': '789' },
1939
+ },
1940
+ });</pre>
1941
+ headers: <pre>${JSON.stringify(Object.fromEntries(res.headers.entries()), null, 4)}</pre>
1942
+ content: <pre>${await res.text()}</pre>`);
1943
+ }
1944
+ catch (e) {
1945
+ echo.push(`error: <pre>${JSON.stringify(e)}</pre>`);
1946
+ }
1947
+ return echo.join('') + '<br>' + this._getEnd();
1948
+ }
1949
+ netFetch1() {
1950
+ return [1, {
1951
+ 'post': this._post,
1952
+ }];
1953
+ }
1891
1954
  async scan() {
1892
1955
  const link = await this._scanLink();
1893
1956
  if (!link) {