@maiyunnet/kebab 9.1.4 → 9.2.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.
@@ -2,6 +2,7 @@
2
2
  import * as kebab from '#kebab/index.js';
3
3
  import * as lCore from '#kebab/lib/core.js';
4
4
  import * as lNet from '#kebab/lib/net.js';
5
+ import * as lUndici from '#kebab/lib/undici.js';
5
6
  import * as lDb from '#kebab/lib/db.js';
6
7
  import * as lVector from '#kebab/lib/vector.js';
7
8
  import * as lFs from '#kebab/lib/fs.js';
@@ -22,6 +23,7 @@ import * as lLan from '#kebab/lib/lan.js';
22
23
  import * as lCron from '#kebab/lib/cron.js';
23
24
  import * as lRateLimit from '#kebab/lib/ratelimit.js';
24
25
  import * as lAi from '#kebab/lib/ai.js';
26
+ import * as lCookie from '#kebab/lib/cookie.js';
25
27
  import * as sMonitor from '#kebab/sys/monitor.js';
26
28
  import * as sCtr from '#kebab/sys/ctr.js';
27
29
  // --- mod ---
@@ -78,6 +80,7 @@ export default class extends sCtr.Ctr {
78
80
  '<br>MINIPROGRAM: ' + (this._config.const.miniprogram) + ' (' + typeof this._config.const.miniprogram + ')',
79
81
  '<br>Real IP: ' + lCore.ip(this),
80
82
  '<br>Client IP: ' + lCore.realIP(this),
83
+ '<br>--use-env-proxy: ' + (process.execArgv.includes('--use-env-proxy') ? 'true' : 'false'),
81
84
  '<br><br>URL_BASE: ' + this._config.const.urlBase,
82
85
  '<br>URL_STC: ' + this._config.const.urlStc,
83
86
  '<br>URL_FULL: ' + this._config.const.urlFull,
@@ -186,6 +189,26 @@ export default class extends sCtr.Ctr {
186
189
  `<br><a href="${this._config.const.urlBase}test/net-filterheaders">View "test/net-filterheaders"</a>`,
187
190
  `<br><a href="${this._config.const.urlBase}test/net-fetch">View "test/net-fetch"</a>`,
188
191
  `<br><a href="${this._config.const.urlBase}test/net-get-response-json">View "test/net-get-response-json"</a>`,
192
+ '<br><br><b>Undici:</b>',
193
+ `<br><br><a href="${this._config.const.urlBase}test/undici">View "test/undici"</a>`,
194
+ `<br><a href="${this._config.const.urlBase}test/undici-pipe">View "test/undici-pipe"</a>`,
195
+ `<br><a href="${this._config.const.urlBase}test/undici-post">View "test/undici-post"</a>`,
196
+ `<br><a href="${this._config.const.urlBase}test/undici-post-string">View "test/undici-post-string"</a>`,
197
+ `<br><a href="${this._config.const.urlBase}test/undici-open">View "test/undici-open"</a>`,
198
+ `<br><a href="${this._config.const.urlBase}test/undici-form-test">View "test/undici-form-test"</a>`,
199
+ `<br><a href="${this._config.const.urlBase}test/undici-upload">View "test/undici-upload"</a>`,
200
+ `<br><a href="${this._config.const.urlBase}test/undici-cookie">View "test/undici-cookie"</a>`,
201
+ `<br><a href="${this._config.const.urlBase}test/undici-save">View "test/undici-save"</a>`,
202
+ `<br><a href="${this._config.const.urlBase}test/undici-follow">View "test/undici-follow"</a>`,
203
+ `<br><a href="${this._config.const.urlBase}test/undici-reuse">View "test/undici-reuse"</a>`,
204
+ `<br><a href="${this._config.const.urlBase}test/undici-error">View "test/undici-error"</a>`,
205
+ `<br><a href="${this._config.const.urlBase}test/undici-hosts">View "test/undici-hosts"</a>`,
206
+ `<br><a href="${this._config.const.urlBase}test/undici-rproxy/dist/core.js">View "test/undici-rproxy/dist/core.js"</a> <a href="${this._config.const.urlBase}test/undici-rproxy/package.json">View "package.json"</a>`,
207
+ `<br><a href="${this._config.const.urlBase}test/undici-mproxy">View "test/undici-mproxy"</a>`,
208
+ `<br><a href="${this._config.const.urlBase}test/undici-filterheaders">View "test/undici-filterheaders"</a>`,
209
+ `<br><a href="${this._config.const.urlBase}test/undici-fetch">View "test/undici-fetch"</a>`,
210
+ `<br><a href="${this._config.const.urlBase}test/undici-get-response-json">View "test/undici-get-response-json"</a>`,
211
+ `<br><a href="${this._config.const.urlBase}test/undici-proxy">View "test/undici-proxy"</a>`,
189
212
  '<br><br><b>Scan:</b>',
190
213
  `<br><br><a href="${this._config.const.urlBase}test/scan?s=db">View "test/scan?s=db"</a>`,
191
214
  `<br><a href="${this._config.const.urlBase}test/scan?s=kv">View "test/scan?s=kv"</a>`,
@@ -1972,13 +1995,13 @@ cookie: <pre>${JSON.stringify(cookie, null, 4)}</pre><hr>`);
1972
1995
  });</pre>
1973
1996
  headers: <pre>${JSON.stringify(res.headers, null, 4)}</pre>
1974
1997
  content: <pre>${(await res.getContent())?.toString() ?? 'null'}</pre>`);
1975
- lNet.setCookie(cookie, 'custom1', 'abc1', this._config.const.host);
1976
- lNet.setCookie(cookie, 'custom2', 'abc2', '172.17.0.1');
1998
+ lCookie.setCookie(cookie, 'custom1', 'abc1', this._config.const.host);
1999
+ lCookie.setCookie(cookie, 'custom2', 'abc2', '172.17.0.1');
1977
2000
  res = await lNet.get(this._internalUrl + 'test/net-cookie2', {
1978
2001
  'cookie': cookie
1979
2002
  });
1980
- echo.push(`<pre>lNet.setCookie(cookie, 'custom1', 'abc1', ${this._config.const.host});
1981
- lNet.setCookie(cookie, 'custom2', 'abc2', '172.17.0.1');
2003
+ echo.push(`<pre>lCookie.setCookie(cookie, 'custom1', 'abc1', ${this._config.const.host});
2004
+ lCookie.setCookie(cookie, 'custom2', 'abc2', '172.17.0.1');
1982
2005
  lNet.get(this._internalUrl + 'test/net-cookie2', {
1983
2006
  'cookie': cookie
1984
2007
  });</pre>
@@ -2330,6 +2353,505 @@ result: <pre>${lText.htmlescape(JSON.stringify(json2, null, 4))}</pre>`);
2330
2353
  'features': ['GET request with JSON parsing', 'Automatic JSON parsing', 'Simple and clean API']
2331
2354
  }];
2332
2355
  }
2356
+ async undici() {
2357
+ const echo = [];
2358
+ const res = await lUndici.get('https://cdn.jsdelivr.net/npm/deskrt@2.0.10/package.json');
2359
+ echo.push(`<pre>lUndici.get('https://cdn.jsdelivr.net/npm/deskrt@2.0.10/package.json');</pre>
2360
+ headers: <pre>${JSON.stringify(res.headers, null, 4)}</pre>
2361
+ content: <pre>${(await res.getContent())?.toString() ?? 'null'}</pre>
2362
+ error: ${JSON.stringify(res.error)}`);
2363
+ const res2 = await lUndici.get(this._internalUrl + 'test?abc');
2364
+ echo.push(`<pre>lUndici.get('${this._internalUrl}test?abc');</pre>
2365
+ headers: <pre>${JSON.stringify(res2.headers, null, 4)}</pre>
2366
+ content: <pre>${(await res2.getContent())?.toString().slice(0, 500) ?? 'null'}</pre>
2367
+ error: ${JSON.stringify(res2.error)}`);
2368
+ return echo.join('') + '<br><br>' + this._getEnd();
2369
+ }
2370
+ async undiciPipe() {
2371
+ const echo = [];
2372
+ const res = await lUndici.get('https://cdn.jsdelivr.net/npm/deskrt@2.0.10/package.json');
2373
+ echo.push(`<pre>lUndici.get('https://cdn.jsdelivr.net/npm/deskrt@2.0.10/package.json');</pre>
2374
+ headers: <pre>${JSON.stringify(res.headers, null, 4)}</pre>
2375
+ content: <pre>`, res, `</pre>
2376
+ error: ${JSON.stringify(res.error)}
2377
+ <br><br>` + this._getEnd());
2378
+ return echo;
2379
+ }
2380
+ async undiciPost() {
2381
+ const echo = [];
2382
+ const res = await lUndici.post(this._internalUrl + 'test/netPost1', { 'a': '1', 'b': '2', 'c': ['1', '2', '3'] });
2383
+ echo.push(`<pre>lUndici.post('${this._internalUrl}test/netPost1', { 'a': '1', 'b': '2', 'c': ['1', '2', '3'] });</pre>
2384
+ headers: <pre>${JSON.stringify(res.headers, null, 4)}</pre>
2385
+ content: <pre>${(await res.getContent())?.toString() ?? 'null'}</pre>
2386
+ error: ${JSON.stringify(res.error)}`);
2387
+ return echo.join('') + '<br><br>' + this._getEnd();
2388
+ }
2389
+ undiciPost1() {
2390
+ return `_post:\n\n${JSON.stringify(this._post)}\n\nRequest headers:\n\n${JSON.stringify(this._headers, null, 4)}\n\nIP: ${this._req.socket.remoteAddress ?? ''}`;
2391
+ }
2392
+ async undiciPostString() {
2393
+ const echo = [];
2394
+ const res = await lUndici.post(this._internalUrl + 'test/netPostString1', 'HeiHei');
2395
+ echo.push(`<pre>lUndici.post('${this._internalUrl}test/netPostString1', 'HeiHei');</pre>
2396
+ headers: <pre>${JSON.stringify(res.headers, null, 4)}</pre>
2397
+ content: <pre>${(await res.getContent())?.toString() ?? 'null'}</pre>
2398
+ error: ${JSON.stringify(res.error)}`);
2399
+ return echo.join('') + '<br><br>' + this._getEnd();
2400
+ }
2401
+ undiciPostString1() {
2402
+ return [1, this._input];
2403
+ }
2404
+ async undiciOpen() {
2405
+ const echo = [];
2406
+ const res = await lUndici.open(this._internalUrl + 'test/netPost1').post().data({ 'a': '2', 'b': '0', 'c': ['0', '1', '3'] }).request();
2407
+ echo.push(`<pre>lUndici.open('${this._internalUrl}test/netPost1').post().data({ 'a': '2', 'b': '0', 'c': ['0', '1', '3'] }).request();</pre>
2408
+ headers: <pre>${JSON.stringify(res.headers, null, 4)}</pre>
2409
+ content: <pre>${(await res.getContent())?.toString() ?? 'null'}</pre>
2410
+ error: ${JSON.stringify(res.error)}`);
2411
+ return echo.join('') + this._getEnd();
2412
+ }
2413
+ async undiciFormTest() {
2414
+ if (!await this._handleFormData()) {
2415
+ return '';
2416
+ }
2417
+ const echo = [
2418
+ '<pre>',
2419
+ JSON.stringify(this._post, null, 4),
2420
+ '\n-----\n',
2421
+ JSON.stringify(this._files, null, 4),
2422
+ '</pre>'
2423
+ ];
2424
+ echo.push(`<form enctype="multipart/form-data" method="post">
2425
+ text a: <input type="text" name="a" value="a1"> <input type="text" name="a" value="a2"><br>
2426
+ file b: <input type="file" name="b"><br>
2427
+ file c: <input type="file" name="c"><input type="file" name="c"><br>
2428
+ fi d[]: <input type="file" name="d[]"><input type="file" name="d[]"><br>
2429
+ <input type="submit" value="Upload">
2430
+ </form>
2431
+ <hr>
2432
+ <form method="post">
2433
+ name a: <input type="text" name="a" value="a&1"> <input type="text" name="a" value="a&2"><br>
2434
+ na b[]: <input type="text" name="b[]" value="b1"> <input type="text" name="b[]" value="b2"><br>
2435
+ name d: <input type="text" name="d" value="d"><br>
2436
+ <input type="submit" value="Default post">
2437
+ </form>`);
2438
+ return echo.join('') + this._getEnd();
2439
+ }
2440
+ async undiciUpload() {
2441
+ const echo = [];
2442
+ const fd = lUndici.getFormData();
2443
+ fd.putString('a', '1');
2444
+ await fd.putFile('file', kebab.LIB_PATH + 'net/cacert.pem');
2445
+ await fd.putFile('multiple', kebab.LIB_PATH + 'net/cacert.pem');
2446
+ await fd.putFile('multiple', kebab.LIB_PATH + 'net/cacert.pem');
2447
+ const res = await lUndici.post(this._internalUrl + 'test/undici-upload1', fd);
2448
+ echo.push(`<pre>const fd = lUndici.getFormData();
2449
+ fd.putString('a', '1');
2450
+ await fd.putFile('file', kebab.LIB_PATH + 'net/cacert.pem');
2451
+ await fd.putFile('multiple', kebab.LIB_PATH + 'net/cacert.pem');
2452
+ await fd.putFile('multiple', kebab.LIB_PATH + 'net/cacert.pem');
2453
+ lUndici.post('${this._internalUrl}test/undici-upload1', fd);</pre>
2454
+ headers: <pre>${JSON.stringify(res.headers, null, 4)}</pre>
2455
+ content: <pre>${(await res.getContent())?.toString() ?? 'null'}</pre>
2456
+ error: ${JSON.stringify(res.error)}`);
2457
+ return echo.join('') + '<br><br>' + this._getEnd();
2458
+ }
2459
+ async undiciUpload1() {
2460
+ if (!await this._handleFormData()) {
2461
+ return '{}';
2462
+ }
2463
+ return JSON.stringify(this._post, null, 4) + '\n\n' + JSON.stringify(this._files, null, 4);
2464
+ }
2465
+ async undiciCookie() {
2466
+ const echo = [];
2467
+ const cookie = {};
2468
+ let res = await lUndici.get(this._internalUrl + 'test/undici-cookie1', {
2469
+ 'cookie': cookie
2470
+ });
2471
+ echo.push(`<pre>const cookie = {};
2472
+ lUndici.get(this._internalUrl + 'test/undici-cookie1', {
2473
+ 'cookie': cookie
2474
+ });</pre>
2475
+ headers: <pre>${lText.htmlescape(JSON.stringify(res.headers, null, 4))}</pre>
2476
+ content: <pre>${(await res.getContent())?.toString() ?? 'null'}</pre>
2477
+ cookie: <pre>${JSON.stringify(cookie, null, 4)}</pre><hr>`);
2478
+ res = await lUndici.get(this._internalUrl + 'test/undici-cookie2', {
2479
+ 'cookie': cookie
2480
+ });
2481
+ echo.push(`<pre>lUndici.get(this._internalUrl + 'test/undici-cookie2', {
2482
+ 'cookie': cookie
2483
+ });</pre>
2484
+ headers: <pre>${JSON.stringify(res.headers, null, 4)}</pre>
2485
+ content: <pre>${(await res.getContent())?.toString() ?? 'null'}</pre>`);
2486
+ lCookie.setCookie(cookie, 'custom1', 'abc1', this._config.const.host);
2487
+ lCookie.setCookie(cookie, 'custom2', 'abc2', '172.17.0.1');
2488
+ res = await lUndici.get(this._internalUrl + 'test/undici-cookie2', {
2489
+ 'cookie': cookie
2490
+ });
2491
+ echo.push(`<pre>lCookie.setCookie(cookie, 'custom1', 'abc1', ${this._config.const.host});
2492
+ lCookie.setCookie(cookie, 'custom2', 'abc2', '172.17.0.1');
2493
+ lUndici.get(this._internalUrl + 'test/undici-cookie2', {
2494
+ 'cookie': cookie
2495
+ });</pre>
2496
+ headers: <pre>${JSON.stringify(res.headers, null, 4)}</pre>
2497
+ content: <pre>${(await res.getContent())?.toString() ?? 'null'}</pre>`);
2498
+ return echo.join('') + this._getEnd();
2499
+ }
2500
+ undiciCookie1() {
2501
+ lCore.setCookie(this, 'test0', 'session');
2502
+ lCore.setCookie(this, 'test1', 'normal', {
2503
+ 'ttl': 10
2504
+ });
2505
+ lCore.setCookie(this, 'test2', 'baidu.com', {
2506
+ 'ttl': 20,
2507
+ 'path': '/',
2508
+ 'domain': 'baidu.com'
2509
+ });
2510
+ lCore.setCookie(this, 'test3', this._config.const.hostname, {
2511
+ 'ttl': 30,
2512
+ 'path': '/',
2513
+ 'domain': this._config.const.hostname
2514
+ });
2515
+ lCore.setCookie(this, 'test4', '/ok/', {
2516
+ 'ttl': 40,
2517
+ 'path': '/ok/'
2518
+ });
2519
+ lCore.setCookie(this, 'test5', 'secure', {
2520
+ 'ttl': 50,
2521
+ 'ssl': true
2522
+ });
2523
+ lCore.setCookie(this, 'test6', '0.1', {
2524
+ 'ttl': 40,
2525
+ 'path': '/',
2526
+ 'domain': '0.1'
2527
+ });
2528
+ lCore.setCookie(this, 'test7', 'localhost', {
2529
+ 'ttl': 30,
2530
+ 'path': '/',
2531
+ 'domain': 'localhost'
2532
+ });
2533
+ lCore.setCookie(this, 'test8', 'com', {
2534
+ 'ttl': 20,
2535
+ 'path': '/',
2536
+ 'domain': 'com'
2537
+ });
2538
+ lCore.setCookie(this, 'test9', 'com.cn', {
2539
+ 'ttl': 10,
2540
+ 'path': '/',
2541
+ 'domain': 'com.cn'
2542
+ });
2543
+ lCore.setCookie(this, 'test10', 'httponly', {
2544
+ 'ttl': 60,
2545
+ 'httponly': true
2546
+ });
2547
+ // --- 用于测试 BUG 的额外 Cookie ---
2548
+ this._res.setHeader('Set-Cookie', [
2549
+ ...this._res.getHeader('Set-Cookie'),
2550
+ 'test-del=; Max-Age=0; Path=/',
2551
+ 'test-equals=val=with=equals; Path=/',
2552
+ 'test-expires=val; Expires=Wed, 21 Oct 2035 07:28:00 GMT; Path=/',
2553
+ 'test-invalid=%invalid; Path=/'
2554
+ ]);
2555
+ return `lCore.setCookie(this, 'test0', 'session');
2556
+ lCore.setCookie(this, 'test1', 'normal', {
2557
+ 'ttl': 10
2558
+ });
2559
+ lCore.setCookie(this, 'test2', 'baidu.com', {
2560
+ 'ttl': 20,
2561
+ 'path': '/',
2562
+ 'domain': 'baidu.com'
2563
+ });
2564
+ lCore.setCookie(this, 'test3', this._config.const.hostname, {
2565
+ 'ttl': 30,
2566
+ 'path': '/',
2567
+ 'domain': this._config.const.hostname
2568
+ });
2569
+ lCore.setCookie(this, 'test4', '/ok/', {
2570
+ 'ttl': 40,
2571
+ 'path': '/ok/'
2572
+ });
2573
+ lCore.setCookie(this, 'test5', 'secure', {
2574
+ 'ttl': 50,
2575
+ 'ssl': true
2576
+ });
2577
+ lCore.setCookie(this, 'test6', '0.1', {
2578
+ 'ttl': 40,
2579
+ 'path': '/',
2580
+ 'domain': '0.1'
2581
+ });
2582
+ lCore.setCookie(this, 'test7', 'localhost', {
2583
+ 'ttl': 30,
2584
+ 'path': '/',
2585
+ 'domain': 'localhost'
2586
+ });
2587
+ lCore.setCookie(this, 'test8', 'com', {
2588
+ 'ttl': 20,
2589
+ 'path': '/',
2590
+ 'domain': 'com'
2591
+ });
2592
+ lCore.setCookie(this, 'test9', 'com.cn', {
2593
+ 'ttl': 10,
2594
+ 'path': '/',
2595
+ 'domain': 'com.cn'
2596
+ });
2597
+ lCore.setCookie(this, 'test10', 'httponly', {
2598
+ 'ttl': 60,
2599
+ 'httponly': true
2600
+ });`;
2601
+ }
2602
+ undiciCookie2() {
2603
+ return 'this._cookie: \n\n' + JSON.stringify(this._cookie, null, 4);
2604
+ }
2605
+ async undiciSave() {
2606
+ const echo = [];
2607
+ const res = await lUndici.get('https://cdn.jsdelivr.net/npm/deskrt@2.0.10/package.json', {
2608
+ 'follow': 5,
2609
+ 'save': kebab.LOG_CWD + 'test-must-remove.json'
2610
+ });
2611
+ echo.push(`<pre>lUndici.get('https://cdn.jsdelivr.net/npm/deskrt@2.0.10/package.json', {
2612
+ 'follow': 5,
2613
+ 'save': kebab.LOG_CWD + 'test-must-remove.json'
2614
+ });</pre>
2615
+ headers: <pre>${JSON.stringify(res.headers, null, 4)}</pre>
2616
+ content: <pre>${(await res.getContent())?.toString() ?? 'null'}</pre>
2617
+ error: ${JSON.stringify(res.error)}`);
2618
+ return echo.join('') + this._getEnd();
2619
+ }
2620
+ async undiciFollow() {
2621
+ const echo = [];
2622
+ const res = await lUndici.post(this._internalUrl + 'test/undici-follow1', {
2623
+ 'a': '1',
2624
+ 'b': '2'
2625
+ }, {
2626
+ 'follow': 5
2627
+ });
2628
+ echo.push(`<pre>lUndici.post(this._internalUrl + 'test/undici-follow1', {
2629
+ 'a': '1',
2630
+ 'b': '2'
2631
+ }, {
2632
+ 'follow': 5
2633
+ });</pre>
2634
+ headers: <pre>${JSON.stringify(res.headers, null, 4)}</pre>
2635
+ content: <pre>${(await res.getContent())?.toString() ?? 'null'}</pre>
2636
+ error: ${JSON.stringify(res.error)}</pre>`);
2637
+ return echo.join('') + this._getEnd();
2638
+ }
2639
+ undiciFollow1() {
2640
+ this._location('test/undici-follow2');
2641
+ }
2642
+ undiciFollow2() {
2643
+ return [1, { 'post': this._post['a'] + ',' + this._post['b'] }];
2644
+ }
2645
+ async undiciReuse() {
2646
+ const echo = [];
2647
+ echo.push('<strong>Reuse:</strong>');
2648
+ let time0 = Date.now();
2649
+ await lUndici.get('https://cdn.jsdelivr.net/npm/deskrt@2.0.10/package.json');
2650
+ let time1 = Date.now();
2651
+ echo.push("<pre>lUndici.get('https://cdn.jsdelivr.net/npm/deskrt@2.0.10/package.json');</pre>" + Math.round(time1 - time0).toString() + 'ms.');
2652
+ time0 = Date.now();
2653
+ await lUndici.get('https://cdn.jsdelivr.net/npm/deskrt@2.0.10/README.md');
2654
+ time1 = Date.now();
2655
+ echo.push("<pre>lUndici.get('https://cdn.jsdelivr.net/npm/deskrt@2.0.10/README.md');</pre>" + Math.round(time1 - time0).toString() + 'ms.');
2656
+ time0 = Date.now();
2657
+ await lUndici.get('https://cdn.jsdelivr.net/npm/deskrt@2.0.10/LICENSE');
2658
+ time1 = Date.now();
2659
+ echo.push("<pre>lUndici.get('https://cdn.jsdelivr.net/npm/deskrt@2.0.10/LICENSE');</pre>" + Math.round(time1 - time0).toString() + 'ms.<hr>');
2660
+ return echo.join('') + this._getEnd();
2661
+ }
2662
+ async undiciError() {
2663
+ const echo = [];
2664
+ const res = await lUndici.get('https://192.111.000.222/xxx.zzz');
2665
+ echo.push(`<pre>lUndici.get('https://192.111.000.222/xxx.zzz');</pre>
2666
+ headers: <pre>${JSON.stringify(res.headers, null, 4)}</pre>
2667
+ content: <pre>${(await res.getContent())?.toString() ?? 'null'}</pre>
2668
+ error: <pre>${JSON.stringify(res.error, null, 4)}</pre>`);
2669
+ return echo.join('') + this._getEnd();
2670
+ }
2671
+ async undiciHosts() {
2672
+ const echo = [];
2673
+ const res = await lUndici.get('http://nodejs.org:' + this._config.const.hostport.toString() + this._config.const.urlBase + 'test', {
2674
+ 'hosts': {
2675
+ 'nodejs.org': '127.0.0.1'
2676
+ }
2677
+ });
2678
+ echo.push(`<pre>lUndici.get('http://nodejs.org:${this._config.const.hostport.toString() + this._config.const.urlBase}test', {
2679
+ 'hosts': {
2680
+ 'nodejs.org': '127.0.0.1'
2681
+ }
2682
+ });</pre>
2683
+ headers: <pre>${JSON.stringify(res.headers, null, 4)}</pre>
2684
+ content: <pre>${lText.htmlescape((await res.getContent())?.toString() ?? '')}</pre>
2685
+ error: <pre>${JSON.stringify(res.error, null, 4)}</pre>`);
2686
+ return echo.join('') + this._getEnd();
2687
+ }
2688
+ async undiciMproxy() {
2689
+ const echo = [];
2690
+ const res = await lUndici.postJson(this._internalUrl + 'test/undici-mproxy2', {
2691
+ 'abc': 'ok',
2692
+ }, {
2693
+ 'mproxy': {
2694
+ 'url': this._internalUrl + 'test/undici-mproxy1',
2695
+ 'auth': '123456',
2696
+ 'data': { 'test': '123' },
2697
+ }
2698
+ });
2699
+ echo.push(`<pre>lUndici.postJson('${this._internalUrl}test/undici-mproxy2', {
2700
+ 'mproxy': {
2701
+ 'url': '${this._internalUrl}test/undici-mproxy1',
2702
+ 'auth': '123456',
2703
+ 'data': { 'test': '123' },
2704
+ }
2705
+ });</pre>
2706
+ headers: <pre>${JSON.stringify(res.headers, null, 4)}</pre>
2707
+ content: <pre>${(await res.getContent())?.toString() ?? 'null'}</pre>
2708
+ error: ${JSON.stringify(res.error)}`);
2709
+ return echo.join('') + '<br><br>' + this._getEnd();
2710
+ }
2711
+ async undiciMproxy1() {
2712
+ const data = lUndici.mproxyData(this);
2713
+ lCore.debug('Got data', data);
2714
+ const rtn = await lUndici.mproxy(this, '123456');
2715
+ if (rtn > 0) {
2716
+ return false;
2717
+ }
2718
+ return 'Nothing(' + rtn + ')';
2719
+ }
2720
+ undiciMproxy2() {
2721
+ return [1, {
2722
+ 'data': this._post,
2723
+ }];
2724
+ }
2725
+ undiciFilterheaders() {
2726
+ const echo = [];
2727
+ const headers = {
2728
+ 'host': 'www.maiyun.net',
2729
+ 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/180.0.0.0 Safari/537.36',
2730
+ 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
2731
+ 'accept-encoding': 'gzip, deflate',
2732
+ 'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8',
2733
+ 'cache-control': 'no-cache',
2734
+ 'cdn-loop': 'TencentEdgeOne; loops=2',
2735
+ 'eo-connecting-ip': 'xx:xx:xx:xx:xx',
2736
+ 'eo-log-uuid': '102780998859203995',
2737
+ 'pragma': 'no-cache',
2738
+ 'priority': 'u=0, i',
2739
+ 'sec-ch-ua': '"Chromium";v="140", "Not=A?Brand";v="24", "Google Chrome";v="180"',
2740
+ 'sec-ch-ua-mobile': '?0',
2741
+ 'sec-ch-ua-platform': '"Windows"',
2742
+ 'sec-fetch-dest': 'document',
2743
+ 'sec-fetch-mode': 'navigate',
2744
+ 'sec-fetch-site': 'none',
2745
+ 'sec-fetch-user': '?1',
2746
+ 'upgrade-insecure-requests': '1',
2747
+ 'x-forwarded-for': '127.1.2.3',
2748
+ 'x-forwarded-host': 'www.maiyun.net',
2749
+ 'x-forwarded-proto': 'http',
2750
+ 'authorization': ''
2751
+ };
2752
+ echo.push(`origin:<pre>${JSON.stringify(headers, null, 4)}</pre>`);
2753
+ const filtered = lUndici.filterHeaders(headers, undefined, h => {
2754
+ return !['x-', 'eo-'].some(i => h.startsWith(i));
2755
+ });
2756
+ echo.push(`const filtered = lUndici.filterHeaders(headers);<pre>${JSON.stringify(filtered, null, 4)}</pre>`);
2757
+ return echo.join('') + this._getEnd();
2758
+ }
2759
+ async undiciFetch() {
2760
+ const echo = [];
2761
+ try {
2762
+ const res = await lUndici.fetch(this._internalUrl + 'test/undici-fetch1', {
2763
+ 'method': 'POST',
2764
+ 'headers': {
2765
+ 'content-type': 'application/json',
2766
+ },
2767
+ 'body': JSON.stringify({ 'test': '123' }),
2768
+ });
2769
+ echo.push(`<pre>lUndici.fetch('${this._internalUrl}test/undici-fetch1', {
2770
+ 'method': 'POST',
2771
+ 'headers': {
2772
+ 'content-type': 'application/json',
2773
+ },
2774
+ 'body': JSON.stringify({ 'test': '123' }),
2775
+ });</pre>
2776
+ headers: <pre>${JSON.stringify(Object.fromEntries(res.headers.entries()), null, 4)}</pre>
2777
+ content: <pre>${await res.text()}</pre>`);
2778
+ }
2779
+ catch (e) {
2780
+ echo.push(`error: <pre>${JSON.stringify(e)}</pre>`);
2781
+ }
2782
+ echo.push(`mproxy:`);
2783
+ try {
2784
+ const res = await lUndici.fetch(this._internalUrl + 'test/undici-fetch1', {
2785
+ 'method': 'POST',
2786
+ 'headers': {
2787
+ 'content-type': 'application/json',
2788
+ },
2789
+ 'body': JSON.stringify({ 'test': '456' }),
2790
+ 'mproxy': {
2791
+ 'url': this._internalUrl + 'test/undici-mproxy1',
2792
+ 'auth': '123456',
2793
+ 'data': { 'test': '789' },
2794
+ },
2795
+ });
2796
+ echo.push(`<pre>lUndici.fetch('${this._internalUrl}test/undici-fetch1', {
2797
+ 'method': 'POST',
2798
+ 'headers': {
2799
+ 'content-type': 'application/json',
2800
+ },
2801
+ 'body': JSON.stringify({ 'test': '123' }),
2802
+ 'mproxy': {
2803
+ 'url': this._internalUrl + 'test/undici-mproxy1',
2804
+ 'auth': '123456',
2805
+ 'data': { 'test': '789' },
2806
+ },
2807
+ });</pre>
2808
+ headers: <pre>${JSON.stringify(Object.fromEntries(res.headers.entries()), null, 4)}</pre>
2809
+ content: <pre>${await res.text()}</pre>`);
2810
+ }
2811
+ catch (e) {
2812
+ echo.push(`error: <pre>${JSON.stringify(e)}</pre>`);
2813
+ }
2814
+ return echo.join('') + '<br>' + this._getEnd();
2815
+ }
2816
+ undiciFetch1() {
2817
+ return [1, {
2818
+ 'post': this._post,
2819
+ }];
2820
+ }
2821
+ async undiciGetResponseJson() {
2822
+ const echo = [];
2823
+ const json = await lUndici.getResponseJson(this._internalUrl + 'test/undici-get-response-json1');
2824
+ echo.push(`<pre>const json = await lUndici.getResponseJson('${this._internalUrl}test/undici-get-response-json1');</pre>
2825
+ result: <pre>${lText.htmlescape(JSON.stringify(json, null, 4))}</pre>`);
2826
+ const json2 = await lUndici.getResponseJson(this._internalUrl + 'test/undici-get-response-json2');
2827
+ echo.push(`<pre>const json2 = await lUndici.getResponseJson('${this._internalUrl}test/undici-get-response-json2');</pre>
2828
+ result: <pre>${lText.htmlescape(JSON.stringify(json2, null, 4))}</pre>`);
2829
+ return echo.join('') + '<br>' + this._getEnd();
2830
+ }
2831
+ undiciGetResponseJson1() {
2832
+ return [1, {
2833
+ 'data': 'This is test data from undiciGetResponseJson1',
2834
+ 'timestamp': lTime.stamp()
2835
+ }];
2836
+ }
2837
+ undiciGetResponseJson2() {
2838
+ return [1, {
2839
+ 'name': 'Kebab Framework',
2840
+ 'version': kebab.VER,
2841
+ 'features': ['GET request with JSON parsing', 'Automatic JSON parsing', 'Simple and clean API']
2842
+ }];
2843
+ }
2844
+ /** --- 测试 ProxyAgent --- */
2845
+ _undiciProxyAgent = lUndici.getProxyAgent('http://192.168.0.10:20809');
2846
+ async undiciProxy() {
2847
+ const echo = [];
2848
+ const res = await lUndici.get('https://www.google.com/', { 'reuse': this._undiciProxyAgent });
2849
+ echo.push(`<pre>lUndici.get('https://www.google.com/', { 'reuse': this._undiciProxyAgent });</pre>
2850
+ headers: <pre>${JSON.stringify(res.headers, null, 4)}</pre>
2851
+ content: <pre>${lText.htmlescape((await res.getContent())?.toString().slice(0, 500) ?? 'null')}</pre>
2852
+ error: ${JSON.stringify(res.error)}`);
2853
+ return echo.join('') + '<br><br>' + this._getEnd();
2854
+ }
2333
2855
  async scan() {
2334
2856
  const link = await this._scanLink();
2335
2857
  if (!link) {
@@ -3,5 +3,6 @@
3
3
  "@": "main",
4
4
  "article\\/([0-9]+?)": "test/article",
5
5
  "test\\/net-rproxy\\/.+?": "test/netRproxy",
6
+ "test\\/undici-rproxy\\/.+?": "test/undiciRproxy",
6
7
  "test\\/react-router-page(\\/.+)?": "test/reactRouterPage"
7
8
  }