Skip to content

Commit 397191c

Browse files
authored
feat: add new v4 main page (#4380)
* feat: sync document * feat: update new page * feat: update new page * docs: update i18n to new page * docs: add new main page * feat: update new page * feat: update new page * fix: set main app * fix: add pnpm * fix: add pnpm * fix: add pnpm * fix: add pnpm * fix: add pnpm * feat: sync support listen optoins (#4379) * chore: update lock * fix: es module
1 parent 581bf50 commit 397191c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+6861
-2246
lines changed

.github/workflows/benchmark.yml

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ name: Midway memory leak check
22

33
on:
44
push:
5-
branches: [main]
5+
branches: [v4-next]
66
paths-ignore:
77
- 'site/**'
88
- '*.md'
99
pull_request:
10-
branches: [main]
10+
branches: [v4-next]
1111
paths-ignore:
1212
- 'site/**'
1313
- '*.md'
@@ -27,39 +27,27 @@ jobs:
2727
uses: actions/setup-node@v4
2828
with:
2929
node-version: ${{ matrix.node-version }}
30-
- name: Cache
30+
- name: Install pnpm
31+
uses: pnpm/action-setup@v4
32+
with:
33+
version: 9
34+
run_install: false
35+
- name: Cache pnpm modules
3136
uses: actions/cache@v3
32-
id: cache-lerna
33-
env:
34-
cache-name: cache-lerna
3537
with:
3638
path: |
37-
node_modules
38-
package-lock.json
39-
packages/*/.eslintcache
40-
packages/*/dist
41-
packages/*/tsconfig.tsbuildinfo
42-
packages/*/node_modules
43-
packages/*/package-lock.json
44-
key: ${{ runner.OS }}-build-${{ env.cache-name }}-${{ github.ref }}-${{ hashFiles('**/package-lock.json') }}
39+
~/.pnpm-store
40+
key: ${{ runner.os }}-pnpm-${{ hashFiles('**/pnpm-lock.yaml') }}
4541
restore-keys: |
46-
${{ runner.OS }}-build-${{ env.cache-name }}-${{ github.ref }}-
42+
${{ runner.os }}-pnpm-
4743
- name: Install Dependencies
4844
run: |
49-
npm i --no-audit -g autocannon zx
50-
# npm run purge # if you want to clean up the cache
51-
npm i --no-audit
45+
pnpm i -g autocannon zx
46+
pnpm i --frozen-lockfile
5247
- name: Build
5348
run: |
54-
sed -i 's/\(moduleResolution.\+,\)/\1 "incremental": true,/' tsconfig.json
55-
lerna run build --concurrency=2
56-
- run: npm run link
57-
- name: Benchmark application-koa-v3
49+
pnpm -r run build
50+
- name: Benchmark application-koa-v4
5851
timeout-minutes: 10
5952
run: |
60-
./scripts/benchmark/start-benchmark.mjs --p=midway_benchmark_app --repo=@midwayjs-examples/application-koa-v3
61-
# - name: Benchmark application-web-v3
62-
# timeout-minutes: 10
63-
# run: |
64-
# ./scripts/benchmark/start-benchmark.mjs --p=midway_benchmark_app --repo=@midwayjs-examples/application-web-v3
65-
53+
./scripts/benchmark/start-benchmark.mjs --p=midway_benchmark_app --repo=@midwayjs-examples/application-koa-v4

.github/workflows/nodejs.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ jobs:
2424
matrix:
2525
node-version: [18]
2626

27-
2827
steps:
2928
- name: Git checkout
3029
uses: actions/checkout@v4
@@ -68,10 +67,7 @@ jobs:
6867
run: scripts/wait-for-services.sh
6968

7069
- name: Install dependencies
71-
run: pnpm install
72-
73-
- name: Install codecov
74-
run: pnpm add -g codecov
70+
run: pnpm install --frozen-lockfile
7571

7672
- name: Build
7773
run: pnpm run build --if-present

packages/bull-board/test/index.test.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as bullboard from '../src';
44
import * as bullmq from '@midwayjs/bullmq';
55
import * as bull from '@midwayjs/bull';
66
import * as koa from '@midwayjs/koa';
7+
import { MidwayFrameworkService } from '@midwayjs/core';
78

89
describe(`/test/index.test.ts`, () => {
910
it('test ui in koa', async () => {
@@ -113,14 +114,17 @@ describe(`/test/index.test.ts`, () => {
113114
}
114115
});
115116

117+
const frameworkService = app.getApplicationContext().get(MidwayFrameworkService);
118+
frameworkService.setMainApp('koa');
119+
116120
const bullFramework = app.getApplicationContext().get(bullmq.Framework);
117121
const testQueue = bullFramework.createQueue('test');
118122
await testQueue?.addJobToQueue({name: 'stone-jin'});
119123

120124
const manager = await app.getApplicationContext().getAsync(bullboard.BullBoardManager);
121125
manager.addQueue(new bullboard.BullMQAdapter(testQueue) as any);
122126

123-
const result = await createHttpRequest(app).get('/ui/api/queues?activeQueue=test&page=1&jobsPerPage=10');
127+
const result = await createHttpRequest(frameworkService.getMainApp()).get('/ui/api/queues?activeQueue=test&page=1&jobsPerPage=10');
124128
expect(result.status).toBe(200);
125129
expect(result.body.queues.length).toBe(1);
126130
expect(result.body.queues[0].type).toBe('bullmq');
@@ -145,14 +149,17 @@ describe(`/test/index.test.ts`, () => {
145149
}
146150
});
147151

152+
const frameworkService = app.getApplicationContext().get(MidwayFrameworkService);
153+
frameworkService.setMainApp('koa');
154+
148155
const bullFramework = app.getApplicationContext().get(bull.Framework);
149156
const testQueue = bullFramework.createQueue('test-bull-board');
150157
await testQueue?.addJobToQueue({name: 'stone-jin'});
151158

152159
const manager = await app.getApplicationContext().getAsync(bullboard.BullBoardManager);
153160
manager.addQueue(new bullboard.BullAdapter(testQueue));
154161

155-
const result = await createHttpRequest(app).get('/ui/api/queues?activeQueue=test&page=1&jobsPerPage=10');
162+
const result = await createHttpRequest(frameworkService.getMainApp()).get('/ui/api/queues?activeQueue=test&page=1&jobsPerPage=10');
156163
expect(result.status).toBe(200);
157164
expect(result.body.queues.length).toBe(1);
158165
expect(result.body.queues[0].type).toBe('bull');

packages/web-koa/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"@midwayjs/core": "workspace:^",
2828
"@midwayjs/mock": "workspace:^",
2929
"@types/koa-router": "7.4.8",
30-
"fs-extra": "11.3.0"
30+
"fs-extra": "11.3.0",
31+
"axios": "1.8.4"
3132
},
3233
"dependencies": {
3334
"@koa/router": "^12.0.0",

packages/web-koa/src/framework.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -311,25 +311,36 @@ export class MidwayKoaFramework extends BaseFramework<
311311
this.server.setTimeout(this.configurationOptions.serverTimeout);
312312
}
313313

314+
this.configurationOptions.listenOptions = {
315+
port: this.configurationOptions.port,
316+
host: this.configurationOptions.hostname,
317+
...this.configurationOptions.listenOptions,
318+
};
319+
314320
// set port and listen server
315-
let customPort =
316-
process.env.MIDWAY_HTTP_PORT ?? this.configurationOptions.port;
321+
let customPort: string | number =
322+
process.env.MIDWAY_HTTP_PORT ||
323+
this.configurationOptions.listenOptions.port;
317324

318-
if (customPort === 0) {
325+
if (customPort === 0 || customPort === '0') {
319326
customPort = await getFreePort();
327+
this.configurationOptions.listenOptions.port = customPort;
328+
this.logger.info(
329+
`Midway koa is listening on port ${customPort} (auto assigned)`
330+
);
320331
}
321332

322-
if (customPort) {
333+
if (this.configurationOptions.listenOptions.port) {
323334
new Promise<void>(resolve => {
324-
const args: any[] = [customPort];
325-
if (this.configurationOptions.hostname) {
326-
args.push(this.configurationOptions.hostname);
327-
}
328-
args.push(() => {
335+
// 使用 ListenOptions 对象启动服务器
336+
this.server.listen(this.configurationOptions.listenOptions, () => {
329337
resolve();
330338
});
331-
this.server.listen(...args);
332-
process.env.MIDWAY_HTTP_PORT = String(customPort);
339+
340+
// 设置环境变量
341+
process.env.MIDWAY_HTTP_PORT = String(
342+
this.configurationOptions.listenOptions.port
343+
);
333344
});
334345
}
335346
}
@@ -338,6 +349,7 @@ export class MidwayKoaFramework extends BaseFramework<
338349
if (this.server) {
339350
new Promise(resolve => {
340351
this.server.close(resolve);
352+
process.env.MIDWAY_HTTP_PORT = '';
341353
});
342354
}
343355
}

packages/web-koa/src/interface.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as koa from 'koa';
33
import { Context as KoaContext, DefaultState, Middleware, Next } from 'koa';
44
import { RouterParamValue } from '@midwayjs/core';
55
import * as qs from 'qs';
6+
import { ListenOptions } from 'net';
67

78
export interface State extends DefaultState {}
89

@@ -18,6 +19,10 @@ export type IMidwayKoaApplication = IMidwayApplication<IMidwayKoaContext, koa<St
1819
* @param middlewareId
1920
*/
2021
generateMiddleware(middlewareId: any): Promise<Middleware<State, IMidwayKoaContext>>;
22+
/**
23+
* Get the port that the application is listening on
24+
*/
25+
getPort(): string;
2126
}>;
2227

2328
/**
@@ -91,10 +96,14 @@ export interface IMidwayKoaConfigurationOptions extends IConfigurationOptions {
9196
* qs options
9297
*/
9398
queryParseOptions?: qs.IParseOptions;
94-
/*
99+
/**
95100
* https/https/http2 server options
96101
*/
97102
serverOptions?: Record<string, any>;
103+
/**
104+
* listen options
105+
*/
106+
listenOptions?: ListenOptions;
98107
}
99108

100109
export type MiddlewareParamArray = Array<

packages/web-koa/test/index.test.ts

Lines changed: 116 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { closeApp, creatApp, createHttpRequest } from './utils';
1+
import { closeApp, creatApp, createHttpRequest, createRealHttpRequest } from './utils';
22
import { IMidwayKoaApplication, Framework } from '../src';
3-
import { Controller, Get, makeHttpRequest } from '@midwayjs/core';
3+
import { Controller, Get, makeHttpRequest, sleep } from '@midwayjs/core';
44
import { createLightApp } from '@midwayjs/mock';
55

66
describe('/test/feature.test.ts', () => {
@@ -274,6 +274,7 @@ describe('/test/feature.test.ts', () => {
274274
.get('/');
275275
expect(result.status).toEqual(200);
276276
expect(result.text).toEqual('123');
277+
await closeApp(app);
277278
});
278279

279280
it('should test default onerror set status', async () => {
@@ -494,7 +495,7 @@ describe('/test/feature.test.ts', () => {
494495
}
495496
});
496497

497-
const port = (app.getFramework() as Framework).getPort();
498+
const port = (app.getFramework() as unknown as Framework).getPort();
498499
expect(port).not.toBe('0');
499500

500501
await closeApp(app);
@@ -662,4 +663,116 @@ describe('/test/feature.test.ts', () => {
662663
await closeApp(app);
663664
});
664665
});
666+
667+
describe('test http listen options', () => {
668+
@Controller()
669+
class HomeController {
670+
@Get('/')
671+
async query() {
672+
return 'hello world'
673+
}
674+
}
675+
676+
it('should test default port', async () => {
677+
const app = await createLightApp('', {
678+
imports: [
679+
require('../src'),
680+
],
681+
preloadModules: [
682+
HomeController
683+
],
684+
globalConfig: {
685+
keys: '123',
686+
koa: {
687+
port: 0, // 使用随机端口
688+
}
689+
}
690+
});
691+
await sleep();
692+
693+
// 验证端口配置
694+
const framework = app.getFramework() as unknown as Framework;
695+
const port = framework.getPort();
696+
expect(port).toBeDefined();
697+
expect(port).not.toBe('0');
698+
699+
// 使用真正的 HTTP 请求测试
700+
const result = await createRealHttpRequest(app as unknown as IMidwayKoaApplication, '/');
701+
expect(result.status).toBe(200);
702+
703+
await closeApp(app);
704+
});
705+
706+
it('should test listenOptions configuration', async () => {
707+
const app = await createLightApp('', {
708+
imports: [
709+
require('../src'),
710+
],
711+
preloadModules: [
712+
HomeController
713+
],
714+
globalConfig: {
715+
keys: '123',
716+
koa: {
717+
listenOptions: {
718+
port: 0, // 使用随机端口
719+
host: '127.0.0.1'
720+
}
721+
}
722+
}
723+
});
724+
725+
// 等待服务器真正启动
726+
await sleep();
727+
728+
// 验证端口配置
729+
const framework = app.getFramework() as unknown as Framework;
730+
const port = framework.getPort();
731+
expect(port).toBeDefined();
732+
expect(port).not.toBe('0');
733+
734+
// 使用真正的 HTTP 请求测试
735+
const result = await createRealHttpRequest(app as unknown as IMidwayKoaApplication, '/');
736+
expect(result.status).toBe(200);
737+
738+
await closeApp(app);
739+
});
740+
741+
it('should test listenOptions with port priority', async () => {
742+
const app = await createLightApp('', {
743+
imports: [
744+
require('../src'),
745+
],
746+
preloadModules: [
747+
HomeController
748+
],
749+
globalConfig: {
750+
keys: '123',
751+
koa: {
752+
port: 3000,
753+
hostname: 'localhost',
754+
listenOptions: {
755+
port: 0, // listenOptions.port 应该优先于 port
756+
host: '127.0.0.1', // listenOptions.host 应该优先于 hostname
757+
backlog: 200,
758+
}
759+
}
760+
}
761+
});
762+
763+
await sleep();
764+
765+
// 验证端口配置 - listenOptions.port 应该优先于 port
766+
const framework = app.getFramework() as unknown as Framework;
767+
const port = framework.getPort();
768+
expect(port).toBeDefined();
769+
expect(port).not.toBe('3000'); // 应该使用 listenOptions.port (0) 而不是 port (3000)
770+
771+
// 使用真正的 HTTP 请求测试
772+
const result = await createRealHttpRequest(app as unknown as IMidwayKoaApplication, '/');
773+
expect(result.status).toBe(200);
774+
775+
await closeApp(app);
776+
});
777+
});
665778
});

0 commit comments

Comments
 (0)