@maiyunnet/kebab 9.26.5 → 9.27.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/abox/response.d.ts +3 -0
- package/abox/response.js +59 -0
- package/abox/rpc.d.ts +14 -0
- package/abox/rpc.js +116 -0
- package/abox/text-path.d.ts +3 -0
- package/abox/text-path.js +94 -0
- package/doc/kebab-rag.md +580 -230
- package/index.d.ts +1 -1
- package/index.js +1 -1
- package/lib/core/rpc.d.ts +61 -0
- package/lib/core/rpc.js +51 -0
- package/lib/core.d.ts +3 -4
- package/lib/core.js +36 -50
- package/lib/text.d.ts +6 -3
- package/lib/text.js +26 -61
- package/lib/undici.js +11 -0
- package/package.json +1 -1
- package/sys/child.js +19 -5
- package/sys/cmd.js +5 -4
- package/sys/ctr.js +1 -1
- package/sys/master.js +256 -260
- package/sys/monitor/watchdog.js +3 -3
- package/sys/route.js +21 -22
- package/www/example/abox/core-updatecode.d.ts +2 -0
- package/www/example/abox/core-updatecode.js +43 -0
- package/www/example/ctr/test.d.ts +4 -0
- package/www/example/ctr/test.js +29 -4
package/sys/route.js
CHANGED
|
@@ -17,6 +17,9 @@ import * as lLang from '#kebab/lib/lang.js';
|
|
|
17
17
|
import * as sCtr from './ctr.js';
|
|
18
18
|
import * as kebab from '#kebab/index.js';
|
|
19
19
|
import { getPublicProtocol as getPublicProtocolImpl } from '#kebab/abox/request.js';
|
|
20
|
+
import { pipeResponse as pipeResponseImpl } from '#kebab/abox/response.js';
|
|
21
|
+
/** --- 将片段依次输出;出错或断连时关闭所有源,回调在输入结束或失败时执行 --- */
|
|
22
|
+
const pipeResponse = pipeResponseImpl;
|
|
20
23
|
/**
|
|
21
24
|
* --- 只用于推导公开 URL;不改变真实 TLS 状态,也不验证转发头来源 ---
|
|
22
25
|
* @param req HTTP 或 HTTP/2 请求
|
|
@@ -329,7 +332,6 @@ export async function run(data) {
|
|
|
329
332
|
cctr.setPrototype('_headers', headers);
|
|
330
333
|
cctr.setPrototype('_get', get);
|
|
331
334
|
cctr.setPrototype('_cookie', cookies);
|
|
332
|
-
lCore.log(cctr, '', '-visit');
|
|
333
335
|
try {
|
|
334
336
|
rtn = await cctr.onLoad();
|
|
335
337
|
}
|
|
@@ -539,7 +541,6 @@ export async function run(data) {
|
|
|
539
541
|
cctr.setPrototype('_cacheTTL', middle.getPrototype('_cacheTTL'));
|
|
540
542
|
cctr.setPrototype('_xsrf', middle.getPrototype('_xsrf'));
|
|
541
543
|
cctr.setPrototype('_httpCode', middle.getPrototype('_httpCode'));
|
|
542
|
-
lCore.log(cctr, '', '-visit');
|
|
543
544
|
// --- 强制 HTTPS ---
|
|
544
545
|
if (config.set.mustHttps && !config.const.https) {
|
|
545
546
|
data.res.setHeader('location', data.req.url ?? '');
|
|
@@ -640,9 +641,7 @@ export async function run(data) {
|
|
|
640
641
|
}
|
|
641
642
|
if (!data.res.writableEnded) {
|
|
642
643
|
if (compress) {
|
|
643
|
-
compress.compress
|
|
644
|
-
compress.compress.pipe(data.res);
|
|
645
|
-
compress.compress.end();
|
|
644
|
+
pipeResponse([rtn], data.res, compress.compress);
|
|
646
645
|
}
|
|
647
646
|
else {
|
|
648
647
|
data.res.end(rtn);
|
|
@@ -676,10 +675,10 @@ export async function run(data) {
|
|
|
676
675
|
}
|
|
677
676
|
if (!data.res.writableEnded) {
|
|
678
677
|
if (compress) {
|
|
679
|
-
stm.
|
|
678
|
+
pipeResponse([stm], data.res, compress.compress);
|
|
680
679
|
}
|
|
681
680
|
else {
|
|
682
|
-
stm
|
|
681
|
+
pipeResponse([stm], data.res);
|
|
683
682
|
}
|
|
684
683
|
}
|
|
685
684
|
}
|
|
@@ -730,9 +729,7 @@ export async function run(data) {
|
|
|
730
729
|
}
|
|
731
730
|
if (!data.res.writableEnded) {
|
|
732
731
|
if (compress) {
|
|
733
|
-
compress.compress
|
|
734
|
-
compress.compress.pipe(data.res);
|
|
735
|
-
compress.compress.end();
|
|
732
|
+
pipeResponse([writeData], data.res, compress.compress);
|
|
736
733
|
}
|
|
737
734
|
else {
|
|
738
735
|
data.res.end(writeData);
|
|
@@ -754,15 +751,19 @@ export async function run(data) {
|
|
|
754
751
|
lCore.writeHead(data.res, httpCode);
|
|
755
752
|
}
|
|
756
753
|
if (!data.res.writableEnded) {
|
|
757
|
-
const
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
754
|
+
const parts = rtn.flatMap((item) => {
|
|
755
|
+
if (item instanceof stream.Readable || typeof item === 'string' || item instanceof Buffer) {
|
|
756
|
+
return [item];
|
|
757
|
+
}
|
|
758
|
+
if (item instanceof lNetResponse.Response || item instanceof lUndiciResponse.Response) {
|
|
759
|
+
const itemStream = item.getStream();
|
|
760
|
+
return itemStream ? [itemStream] : [];
|
|
761
|
+
}
|
|
762
|
+
return [];
|
|
763
|
+
});
|
|
764
|
+
await new Promise((resolve) => {
|
|
765
|
+
pipeResponse(parts, data.res, compress?.compress, resolve);
|
|
766
|
+
});
|
|
766
767
|
}
|
|
767
768
|
}
|
|
768
769
|
}
|
|
@@ -784,9 +785,7 @@ export async function run(data) {
|
|
|
784
785
|
}
|
|
785
786
|
if (!data.res.writableEnded) {
|
|
786
787
|
if (compress) {
|
|
787
|
-
compress.compress
|
|
788
|
-
compress.compress.pipe(data.res);
|
|
789
|
-
compress.compress.end();
|
|
788
|
+
pipeResponse([writeData], data.res, compress.compress);
|
|
790
789
|
}
|
|
791
790
|
else {
|
|
792
791
|
data.res.end(writeData);
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import * as fs from 'node:fs/promises';
|
|
2
|
+
import * as path from 'node:path';
|
|
3
|
+
const prefix = 'core-updatecode-';
|
|
4
|
+
const directoryPattern = /^core-updatecode-[a-zA-Z0-9]{6}$/;
|
|
5
|
+
export async function prepare(root) {
|
|
6
|
+
let directory;
|
|
7
|
+
try {
|
|
8
|
+
const temporary = path.join(root, 'ftmp');
|
|
9
|
+
await fs.mkdir(temporary, { recursive: true });
|
|
10
|
+
directory = await fs.mkdtemp(path.join(temporary, prefix));
|
|
11
|
+
await fs.mkdir(path.join(directory, 'www'));
|
|
12
|
+
await fs.writeFile(path.join(directory, 'www/lock.txt'), 'Before updateCode test\n', { flag: 'wx' });
|
|
13
|
+
return path.relative(root, directory).split(path.sep).join('/') + '/';
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
if (directory)
|
|
17
|
+
await fs.rm(directory, { recursive: true, force: true }).catch(() => { });
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export async function clear(root) {
|
|
22
|
+
try {
|
|
23
|
+
const temporary = path.join(root, 'ftmp');
|
|
24
|
+
let entries;
|
|
25
|
+
try {
|
|
26
|
+
entries = await fs.readdir(temporary, { withFileTypes: true });
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
if (error.code === 'ENOENT')
|
|
30
|
+
return true;
|
|
31
|
+
throw error;
|
|
32
|
+
}
|
|
33
|
+
for (const entry of entries) {
|
|
34
|
+
if (!entry.isDirectory() || !directoryPattern.test(entry.name))
|
|
35
|
+
continue;
|
|
36
|
+
await fs.rm(path.join(temporary, entry.name), { recursive: true, force: true });
|
|
37
|
+
}
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -56,6 +56,10 @@ export default class extends sCtr.Ctr {
|
|
|
56
56
|
coreRealiplimit(): string;
|
|
57
57
|
coreGetlog(): Promise<string>;
|
|
58
58
|
coreLs(): Promise<string>;
|
|
59
|
+
/**
|
|
60
|
+
* --- 在独立临时目录测试文件覆盖,或清理本示例的测试结果 ---
|
|
61
|
+
* @returns 测试或清理结果页面
|
|
62
|
+
*/
|
|
59
63
|
coreUpdatecode(): Promise<string>;
|
|
60
64
|
coreReload(): Promise<string>;
|
|
61
65
|
coreRestart(): Promise<string>;
|
package/www/example/ctr/test.js
CHANGED
|
@@ -29,6 +29,19 @@ import * as sCtr from '#kebab/sys/ctr.js';
|
|
|
29
29
|
// --- mod ---
|
|
30
30
|
import mTest from '../mod/test.js';
|
|
31
31
|
import mTestData from '../mod/testdata.js';
|
|
32
|
+
import * as coreUpdatecodeTest from '../abox/core-updatecode.js';
|
|
33
|
+
/**
|
|
34
|
+
* --- 创建独立测试目录并预置待覆盖文件 ---
|
|
35
|
+
* @param root 运行根目录
|
|
36
|
+
* @returns 相对运行根目录的测试路径;准备失败返回 false
|
|
37
|
+
*/
|
|
38
|
+
const prepareUpdatecodeTest = coreUpdatecodeTest.prepare;
|
|
39
|
+
/**
|
|
40
|
+
* --- 清除 ftmp 下本示例创建的测试目录,保留其他文件及软链接 ---
|
|
41
|
+
* @param root 运行根目录
|
|
42
|
+
* @returns 是否全部清理成功,无测试目录时也返回 true
|
|
43
|
+
*/
|
|
44
|
+
const clearUpdatecodeTest = coreUpdatecodeTest.clear;
|
|
32
45
|
/** --- Kebab 核心库提供的 Valibot 对象 --- */
|
|
33
46
|
const v = lCore.v;
|
|
34
47
|
/** --- Valibot 示例支持的语言 --- */
|
|
@@ -177,7 +190,7 @@ export default class extends sCtr.Ctr {
|
|
|
177
190
|
`<br><a href="${this._config.const.urlBase}test/core-pm2?name=cron&action=restart">View "test/core-pm2"</a>`,
|
|
178
191
|
`<br><a href="${this._config.const.urlBase}test/core-npm">View "test/core-npm"</a>`,
|
|
179
192
|
`<br><a href="${this._config.const.urlBase}test/core-global">View "test/core-global"</a>`,
|
|
180
|
-
`<br><a href="${this._config.const.urlBase}test/core-updatecode">View "test/core-updatecode"</a>`,
|
|
193
|
+
`<br><a href="${this._config.const.urlBase}test/core-updatecode">View "test/core-updatecode"</a> <a href="${this._config.const.urlBase}test/core-updatecode?action=clear">clear</a>`,
|
|
181
194
|
`<br><a href="${this._config.const.urlBase}test/core-env">View "test/core-env"</a>`,
|
|
182
195
|
'<br><br><b>Crypto:</b>',
|
|
183
196
|
`<br><br><a href="${this._config.const.urlBase}test/crypto">View "test/crypto"</a>`,
|
|
@@ -1648,15 +1661,27 @@ for (let i = 0; i < 30000; ++i) {
|
|
|
1648
1661
|
echo.push('</table>');
|
|
1649
1662
|
return echo.join('') + '<br>' + this._getEnd();
|
|
1650
1663
|
}
|
|
1664
|
+
/**
|
|
1665
|
+
* --- 在独立临时目录测试文件覆盖,或清理本示例的测试结果 ---
|
|
1666
|
+
* @returns 测试或清理结果页面
|
|
1667
|
+
*/
|
|
1651
1668
|
async coreUpdatecode() {
|
|
1669
|
+
if (this._get['action'] === 'clear') {
|
|
1670
|
+
const cleared = await clearUpdatecodeTest(kebab.ROOT_CWD);
|
|
1671
|
+
return (cleared ? 'Test files cleared.' : 'Failed to clear test files.') + '<br><br>' + this._getEnd();
|
|
1672
|
+
}
|
|
1652
1673
|
const zip = `${this._config.const.dataPath}test.zip`;
|
|
1653
|
-
const to =
|
|
1674
|
+
const to = await prepareUpdatecodeTest(kebab.ROOT_CWD);
|
|
1675
|
+
if (to === false) {
|
|
1676
|
+
return 'Failed to prepare test files.<br><br>' + this._getEnd();
|
|
1677
|
+
}
|
|
1654
1678
|
const echo = [
|
|
1655
1679
|
`zip: ${zip}<br>
|
|
1656
1680
|
to: ${to}`
|
|
1657
1681
|
];
|
|
1658
|
-
const list = await lCore.updateCode(zip, to);
|
|
1659
|
-
echo.push(`<pre>const list = lCore.updateCode(zip, to);</pre>${
|
|
1682
|
+
const list = await lCore.updateCode(zip, to, undefined, false, true);
|
|
1683
|
+
echo.push(`<pre>const list = await lCore.updateCode(zip, to, undefined, false, true);</pre>${lText.htmlescape(lText.stringifyJson(list))}`);
|
|
1684
|
+
echo.push(`<br><a href="${this._config.const.urlBase}test/core-updatecode?action=clear">clear</a>`);
|
|
1660
1685
|
return echo.join('') + '<br><br>' + this._getEnd();
|
|
1661
1686
|
}
|
|
1662
1687
|
async coreReload() {
|