@maiyunnet/kebab 9.29.5 → 10.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.
Files changed (64) hide show
  1. package/abox/proxy.js +24 -2
  2. package/abox/response.js +36 -17
  3. package/abox/session-login.js +1 -13
  4. package/abox/text.d.ts +2 -0
  5. package/abox/text.js +51 -0
  6. package/doc/kebab-rag.md +2700 -1270
  7. package/index.d.ts +12 -1
  8. package/index.js +1 -1
  9. package/lib/ai.d.ts +4 -0
  10. package/lib/ai.js +32 -16
  11. package/lib/cookie.d.ts +2 -0
  12. package/lib/cookie.js +47 -45
  13. package/lib/core.d.ts +26 -0
  14. package/lib/core.js +36 -13
  15. package/lib/cron.js +1 -8
  16. package/lib/crypto.js +20 -6
  17. package/lib/db/conn.d.ts +10 -1
  18. package/lib/db/conn.js +1 -5
  19. package/lib/db/pool.d.ts +10 -2
  20. package/lib/db/pool.js +0 -6
  21. package/lib/db/tran.d.ts +10 -1
  22. package/lib/db/tran.js +0 -5
  23. package/lib/db.d.ts +3 -3
  24. package/lib/fs.js +78 -31
  25. package/lib/kv.d.ts +216 -88
  26. package/lib/kv.js +520 -230
  27. package/lib/s3.d.ts +2 -0
  28. package/lib/s3.js +14 -27
  29. package/lib/session.d.ts +7 -0
  30. package/lib/session.js +23 -2
  31. package/lib/socket.d.ts +4 -1
  32. package/lib/socket.js +1 -0
  33. package/lib/sql.d.ts +0 -5
  34. package/lib/sql.js +48 -12
  35. package/lib/text.d.ts +32 -8
  36. package/lib/text.js +30 -17
  37. package/lib/turnstile.d.ts +6 -0
  38. package/lib/turnstile.js +16 -9
  39. package/lib/undici/response.d.ts +13 -3
  40. package/lib/undici/response.js +0 -1
  41. package/lib/undici.d.ts +33 -1
  42. package/lib/undici.js +72 -56
  43. package/lib/vector.d.ts +33 -19
  44. package/lib/vector.js +0 -1
  45. package/lib/ws.d.ts +1 -1
  46. package/lib/ws.js +85 -128
  47. package/lib/zip.js +46 -45
  48. package/package.json +2 -2
  49. package/sys/child.js +178 -146
  50. package/sys/cmd.js +196 -197
  51. package/sys/ctr.d.ts +14 -14
  52. package/sys/ctr.js +145 -59
  53. package/sys/master.js +111 -80
  54. package/sys/mod.d.ts +112 -51
  55. package/sys/mod.js +23 -51
  56. package/sys/monitor/files.d.ts +2 -1
  57. package/sys/monitor/files.js +77 -21
  58. package/sys/monitor/watchdog.d.ts +7 -0
  59. package/sys/monitor/watchdog.js +311 -203
  60. package/sys/monitor.d.ts +59 -54
  61. package/sys/monitor.js +742 -681
  62. package/sys/route.d.ts +4 -4
  63. package/sys/route.js +93 -52
  64. package/www/example/ctr/test.js +8 -8
package/lib/ws.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import * as net from 'net';
2
+ import * as crypto from 'crypto';
2
3
  // --- 第三方 ---
3
4
  import WebSocket, { WebSocketServer } from 'ws';
4
5
  import * as lCore from '#kebab/lib/core.js';
@@ -137,7 +138,9 @@ export class Socket {
137
138
  }
138
139
  const host = puri?.hostname ?? uri.hostname ?? '';
139
140
  /** --- 真正的连接远程 IP / HOST --- */
140
- const rhost = typeof hosts === 'string' ? hosts : hosts[host];
141
+ const rhost = typeof hosts === 'string'
142
+ ? hosts
143
+ : (Object.hasOwn(hosts, host) ? hosts[host] : undefined);
141
144
  if (rhost) {
142
145
  headers['host'] ??= connectUrl.host;
143
146
  connectUrl.hostname = lText.isIPv6(rhost) ? `[${rhost}]` : rhost;
@@ -504,7 +507,7 @@ export class Socket {
504
507
  ping(data) {
505
508
  return this._sendControl('ping', data);
506
509
  }
507
- /** --- 发送 ping --- */
510
+ /** --- 发送 pong --- */
508
511
  pong(data) {
509
512
  return this._sendControl('pong', data);
510
513
  }
@@ -604,36 +607,24 @@ function getPipeCloseReason(info) {
604
607
  */
605
608
  function bindPipe(s1, s2, closeReason) {
606
609
  return new Promise(resolve => {
607
- /** --- 是否已经完成关闭,防止双向 close 重复处理 --- */
608
610
  let closed = false;
609
- /** --- 来源侧关闭前最后收到的底层事件 --- */
610
- let sourceEvent = 'close';
611
- /** --- 目标侧关闭前最后收到的底层事件 --- */
612
- let targetEvent = 'close';
613
- /** --- 来源侧最后一个错误 --- */
614
- let sourceError;
615
- /** --- 目标侧最后一个错误 --- */
616
- let targetError;
617
- /** --- 来源侧 WebSocket 关闭信息 --- */
618
- let sourceCloseInfo;
619
- /** --- 目标侧 WebSocket 关闭信息 --- */
620
- let targetCloseInfo;
621
- /** --- 销毁已断开侧,并将原因通过关闭帧回传另一侧 --- */
611
+ /** --- 记录来源侧和目标侧关闭前的底层事件与错误 --- */
612
+ const state = {
613
+ 'source': { 'event': 'close' },
614
+ 'target': { 'event': 'close' },
615
+ };
616
+ // --- 任一侧关闭时,销毁该侧并将原因通过关闭帧通知另一侧 ---
622
617
  const close = (side) => {
623
618
  if (closed) {
624
619
  return;
625
620
  }
626
621
  closed = true;
627
- const info = side === 'source' ? {
628
- 'side': side,
629
- 'event': sourceEvent,
630
- 'error': sourceError,
631
- ...sourceCloseInfo,
632
- } : {
622
+ const cur = state[side];
623
+ const info = {
633
624
  'side': side,
634
- 'event': targetEvent,
635
- 'error': targetError,
636
- ...targetCloseInfo,
625
+ 'event': cur.event,
626
+ 'error': cur.error,
627
+ ...cur.close,
637
628
  };
638
629
  const code = isSendableCloseCode(info.code) ? info.code : 1011;
639
630
  let reason;
@@ -654,110 +645,62 @@ function bindPipe(s1, s2, closeReason) {
654
645
  }
655
646
  resolve(info);
656
647
  };
657
- // --- 监听发送端的 ---
658
- s1.on('message', (msg) => {
659
- switch (msg.opcode) {
660
- case EOpcode.TEXT: {
661
- if (!s2.writeText(msg.data)) {
662
- s1.pause();
663
- }
664
- break;
665
- }
666
- case EOpcode.BINARY: {
667
- if (!s2.writeBinary(msg.data)) {
668
- s1.pause();
648
+ // --- 双向转发帧、背压与关闭事件 ---
649
+ const forward = (from, to, side) => {
650
+ from.on('message', (msg) => {
651
+ switch (msg.opcode) {
652
+ case EOpcode.TEXT: {
653
+ if (!to.writeText(msg.data)) {
654
+ from.pause();
655
+ }
656
+ break;
669
657
  }
670
- break;
671
- }
672
- case EOpcode.CLOSE: {
673
- close('source');
674
- break;
675
- }
676
- case EOpcode.PING: {
677
- if (!s2.ping(msg.data)) {
678
- s1.pause();
658
+ case EOpcode.BINARY: {
659
+ if (!to.writeBinary(msg.data)) {
660
+ from.pause();
661
+ }
662
+ break;
679
663
  }
680
- break;
681
- }
682
- case EOpcode.PONG: {
683
- if (!s2.pong(msg.data)) {
684
- s1.pause();
664
+ case EOpcode.CLOSE: {
665
+ close(side);
666
+ break;
685
667
  }
686
- break;
687
- }
688
- default: {
689
- // --- EOpcode.CONTINUATION ---
690
- }
691
- }
692
- }).on('error', (error) => {
693
- if (sourceEvent !== 'timeout') {
694
- sourceEvent = 'error';
695
- }
696
- sourceError = error;
697
- }).on('end', () => {
698
- if (sourceEvent === 'close') {
699
- sourceEvent = 'end';
700
- }
701
- }).on('timeout', () => {
702
- sourceEvent = 'timeout';
703
- }).on('close', (info) => {
704
- sourceCloseInfo = info;
705
- close('source');
706
- }).on('drain', () => {
707
- s2.resume();
708
- });
709
- // --- 监听远程端的 ---
710
- s2.on('message', (msg) => {
711
- switch (msg.opcode) {
712
- case EOpcode.TEXT: {
713
- if (!s1.writeText(msg.data)) {
714
- s2.pause();
668
+ case EOpcode.PING: {
669
+ if (!to.ping(msg.data)) {
670
+ from.pause();
671
+ }
672
+ break;
715
673
  }
716
- break;
717
- }
718
- case EOpcode.BINARY: {
719
- if (!s1.writeBinary(msg.data)) {
720
- s2.pause();
674
+ case EOpcode.PONG: {
675
+ if (!to.pong(msg.data)) {
676
+ from.pause();
677
+ }
678
+ break;
721
679
  }
722
- break;
723
- }
724
- case EOpcode.CLOSE: {
725
- close('target');
726
- break;
727
- }
728
- case EOpcode.PING: {
729
- if (!s1.ping(msg.data)) {
730
- s2.pause();
680
+ default: {
681
+ // --- EOpcode.CONTINUATION ---
731
682
  }
732
- break;
733
683
  }
734
- case EOpcode.PONG: {
735
- if (!s1.pong(msg.data)) {
736
- s2.pause();
737
- }
738
- break;
684
+ }).on('error', (error) => {
685
+ if (state[side].event !== 'timeout') {
686
+ state[side].event = 'error';
739
687
  }
740
- default: {
741
- // --- EOpcode.CONTINUATION ---
688
+ state[side].error = error;
689
+ }).on('end', () => {
690
+ if (state[side].event === 'close') {
691
+ state[side].event = 'end';
742
692
  }
743
- }
744
- }).on('error', (error) => {
745
- if (targetEvent !== 'timeout') {
746
- targetEvent = 'error';
747
- }
748
- targetError = error;
749
- }).on('end', () => {
750
- if (targetEvent === 'close') {
751
- targetEvent = 'end';
752
- }
753
- }).on('timeout', () => {
754
- targetEvent = 'timeout';
755
- }).on('close', (info) => {
756
- targetCloseInfo = info;
757
- close('target');
758
- }).on('drain', () => {
759
- s1.resume();
760
- });
693
+ }).on('timeout', () => {
694
+ state[side].event = 'timeout';
695
+ }).on('close', (info) => {
696
+ state[side].close = info;
697
+ close(side);
698
+ }).on('drain', () => {
699
+ to.resume();
700
+ });
701
+ };
702
+ forward(s1, s2, 'source');
703
+ forward(s2, s1, 'target');
761
704
  });
762
705
  }
763
706
  /**
@@ -773,19 +716,34 @@ export async function mproxy(ctr, auth, opt = {}) {
773
716
  const socket = ctr.getPrototype('_socket');
774
717
  /** --- 客户端请求中的 get 的数据 --- */
775
718
  const get = ctr.getPrototype('_get');
776
- if (get['auth'] !== auth) {
719
+ const suppliedAuth = get['auth'];
720
+ if (!auth || typeof suppliedAuth !== 'string') {
721
+ return 0;
722
+ }
723
+ const authBuf = Buffer.from(auth);
724
+ const suppliedBuf = Buffer.from(suppliedAuth);
725
+ if (authBuf.length !== suppliedBuf.length || !crypto.timingSafeEqual(authBuf, suppliedBuf)) {
777
726
  return 0;
778
727
  }
779
- if (!get['url']) {
728
+ const targetUrl = get['url'];
729
+ if (typeof targetUrl !== 'string' || !targetUrl) {
780
730
  return -1;
781
731
  }
782
- opt.headers ??= {};
783
- const headers = lProxy.getProxyHeaders(req, opt);
732
+ try {
733
+ const parsedTarget = new URL(targetUrl);
734
+ if (parsedTarget.protocol !== 'ws:' && parsedTarget.protocol !== 'wss:') {
735
+ return -1;
736
+ }
737
+ }
738
+ catch {
739
+ return -1;
740
+ }
741
+ const headers = lProxy.getProxyHeaders(req, { ...opt, 'headers': opt.headers ?? {} });
784
742
  // --- 发起请求 ---
785
743
  /** --- 远程端的双向 socket --- */
786
- const rsocket = await connect(get['url'], {
744
+ const rsocket = await connect(targetUrl, {
787
745
  ...opt,
788
- headers
746
+ 'headers': headers,
789
747
  });
790
748
  if (!rsocket) {
791
749
  return -2;
@@ -806,12 +764,11 @@ export async function rproxy(ctr, url, opt = {}) {
806
764
  opt.headers ??= {};
807
765
  const headers = lProxy.getProxyHeaders(req, opt);
808
766
  // --- 发起请求 ---
809
- /** --- 远程端的双向 socket --- */
810
767
  /** --- 目标连接的握手错误 --- */
811
768
  let connectError;
812
769
  const rsocket = await connect(url, {
813
770
  ...opt,
814
- headers,
771
+ 'headers': headers,
815
772
  'onConnectError': (error) => {
816
773
  connectError = error;
817
774
  opt.onConnectError?.(error);
package/lib/zip.js CHANGED
@@ -62,7 +62,7 @@ export class Zip {
62
62
  stats(path) {
63
63
  path = lText.urlResolve(this._path, path);
64
64
  let dirpath = path.endsWith('/') ? path : path + '/';
65
- if (!this._list[dirpath]) {
65
+ if (!Object.hasOwn(this._list, dirpath)) {
66
66
  // --- 可能是文件 ---
67
67
  if (path.endsWith('/')) {
68
68
  // --- 不可能是文件 ---
@@ -71,11 +71,11 @@ export class Zip {
71
71
  const lio = path.lastIndexOf('/') + 1;
72
72
  const dpath = path.slice(0, lio);
73
73
  const fname = path.slice(lio);
74
- if (!this._list[dpath]) {
74
+ if (!Object.hasOwn(this._list, dpath)) {
75
75
  // --- 上层目录不存在 ---
76
76
  return null;
77
77
  }
78
- const file = this._list[dpath][fname];
78
+ const file = Object.hasOwn(this._list[dpath], fname) ? this._list[dpath][fname] : undefined;
79
79
  if (!file) {
80
80
  return null;
81
81
  }
@@ -102,12 +102,12 @@ export class Zip {
102
102
  const lio = dirpath.lastIndexOf('/') + 1;
103
103
  const dpath = dirpath.slice(0, lio);
104
104
  const fname = dirpath.slice(lio);
105
- const pfolder = this._list[dpath];
106
- const folder = pfolder[fname];
105
+ const pfolder = Object.hasOwn(this._list, dpath) ? this._list[dpath] : undefined;
106
+ const folder = pfolder && Object.hasOwn(pfolder, fname) ? pfolder[fname] : undefined;
107
107
  return {
108
108
  'compressedSize': 0,
109
109
  'uncompressedSize': 0,
110
- 'date': folder.date,
110
+ 'date': folder?.date ?? new Date(0),
111
111
  'isFile': false,
112
112
  'isDirectory': true
113
113
  };
@@ -157,16 +157,26 @@ export class Zip {
157
157
  if (!folder) {
158
158
  return opt.pathAsKey ? {} : [];
159
159
  }
160
- if (!this._list[path]) {
160
+ if (!Object.hasOwn(this._list, path)) {
161
161
  return opt.pathAsKey ? {} : [];
162
162
  }
163
163
  if (!opt.hasChildren) {
164
164
  if (opt.pathAsKey) {
165
- return this._list[path];
165
+ const res = {};
166
+ for (const k in this._list[path]) {
167
+ const item = this._list[path][k];
168
+ if (item.isFile || opt.hasDir) {
169
+ res[k] = item;
170
+ }
171
+ }
172
+ return res;
166
173
  }
167
174
  const list = [];
168
175
  for (const k in this._list[path]) {
169
- list.push(this._list[path][k]);
176
+ const item = this._list[path][k];
177
+ if (item.isFile || opt.hasDir) {
178
+ list.push(item);
179
+ }
170
180
  }
171
181
  return list;
172
182
  }
@@ -256,7 +266,7 @@ export class Zip {
256
266
  * --- 重建目录列表缓存 ---
257
267
  */
258
268
  _refreshList() {
259
- const list = {};
269
+ const list = Object.create(null);
260
270
  // eslint-disable-next-line @litert/disable-for-each-method
261
271
  this._zip.forEach(function (relativePath, item) {
262
272
  if (relativePath.startsWith('/')) {
@@ -281,8 +291,8 @@ export class Zip {
281
291
  if (item.dir) {
282
292
  name = name.slice(0, -1);
283
293
  }
284
- if (!list[parentPath]) {
285
- list[parentPath] = {};
294
+ if (!Object.hasOwn(list, parentPath)) {
295
+ list[parentPath] = Object.create(null);
286
296
  }
287
297
  list[parentPath][name] = {
288
298
  'name': name,
@@ -350,43 +360,34 @@ export class Zip {
350
360
  'hasChildren': true,
351
361
  'hasDir': false
352
362
  });
363
+ if (list.length === 0) {
364
+ resolve(files);
365
+ return;
366
+ }
353
367
  let loaded = 0;
354
368
  for (const file of list) {
369
+ const fullPath = file.path + file.name;
355
370
  const mim = mime.getData(file.name);
356
- if (['txt', 'json', 'js', 'css', 'xml', 'html'].includes(mim.extension)) {
357
- this.getContent(file.path + file.name, 'string').then(function (fb) {
358
- if (fb) {
359
- files[file.path + file.name] = fb;
360
- }
361
- ++loaded;
362
- if (loaded === list.length) {
363
- resolve(files);
364
- }
365
- }).catch(function () {
366
- ++loaded;
367
- if (loaded === list.length) {
368
- resolve(files);
369
- }
370
- });
371
- }
372
- else {
373
- this.getContent(file.path + file.name, 'nodebuffer').then(function (fb) {
374
- if (fb) {
375
- files[file.path + file.name] = fb;
376
- }
377
- ++loaded;
378
- if (loaded === list.length) {
379
- resolve(files);
380
- }
381
- }).catch(function () {
382
- ++loaded;
383
- if (loaded === list.length) {
384
- resolve(files);
385
- }
386
- });
387
- }
371
+ const isText = ['txt', 'json', 'js', 'css', 'xml', 'html'].includes(mim.extension);
372
+ const zipObj = this._zip.file(fullPath.slice(1)) ?? this._zip.file(fullPath);
373
+ const task = zipObj
374
+ ? zipObj.async(isText ? 'string' : 'nodebuffer')
375
+ : this.getContent(fullPath, isText ? 'string' : 'nodebuffer');
376
+ task.then(function (fb) {
377
+ if (fb !== null) {
378
+ files[fullPath] = fb;
379
+ }
380
+ ++loaded;
381
+ if (loaded === list.length) {
382
+ resolve(files);
383
+ }
384
+ }).catch(function () {
385
+ ++loaded;
386
+ if (loaded === list.length) {
387
+ resolve(files);
388
+ }
389
+ });
388
390
  }
389
- return files;
390
391
  });
391
392
  }
392
393
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@maiyunnet/kebab",
3
- "version": "9.29.5",
3
+ "version": "10.0.0",
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": [
@@ -30,7 +30,6 @@
30
30
  "@aws-sdk/lib-storage": "^3.1161.0",
31
31
  "@litert/http-client": "^1.2.0",
32
32
  "@litert/mime": "^2.0.0",
33
- "@litert/redis": "^3.2.1",
34
33
  "@radix-ui/react-checkbox": "^1.4.1",
35
34
  "@radix-ui/react-label": "^2.1.17",
36
35
  "@radix-ui/react-slot": "^1.5.0",
@@ -51,6 +50,7 @@
51
50
  "react": "^19.3.1",
52
51
  "react-dom": "^19.3.1",
53
52
  "react-router-dom": "^7.18.5",
53
+ "redis": "^6.4.0",
54
54
  "ssh2": "^1.17.0",
55
55
  "svg-captcha": "^1.4.0",
56
56
  "tailwind-merge": "^3.7.0",