Skip to content

Commit fb7dcae

Browse files
authored
fix: should export type Events (#5654)
```ts declare module 'egg' { interface Events { [PACKAGE_ADDED]: (fullname: string) => Promise<void>; } } ``` <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Expanded public API with new module entry points for direct imports: ./ajv, ./aop, ./dal, ./helper, ./lib/define, ./orm, ./transaction, and ./config/plugin * Added Events type to main exports for easier typing * **Bug Fixes / Improvements** * Improved startup sequencing and port detection to reliably select cluster/dev ports and log detected ports * Dev command now always auto-detects an available port when none is specified and simplifies port handling <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent b033bf3 commit fb7dcae

6 files changed

Lines changed: 37 additions & 37 deletions

File tree

examples/helloworld-tegg/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"./app/port/schedule/CronDemo": "./app/port/schedule/CronDemo.ts",
4848
"./app/port/schedule/Demo": "./app/port/schedule/Demo.ts",
4949
"./config/config.default": "./config/config.default.ts",
50+
"./config/plugin": "./config/plugin.ts",
5051
"./package.json": "./package.json"
5152
},
5253
"publishConfig": {
@@ -61,6 +62,7 @@
6162
"./app/port/schedule/CronDemo": "./dist/app/port/schedule/CronDemo.js",
6263
"./app/port/schedule/Demo": "./dist/app/port/schedule/Demo.js",
6364
"./config/config.default": "./dist/config/config.default.js",
65+
"./config/plugin": "./dist/config/plugin.js",
6466
"./package.json": "./package.json"
6567
}
6668
}

packages/cluster/src/master.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,6 @@ export class Master extends ReadyEventEmitter {
7777
const frameworkPath = this.options.framework;
7878
const frameworkPkg = readJSONSync(path.join(frameworkPath, 'package.json'));
7979

80-
// set app & agent worker impl
81-
if (this.options.startMode === 'worker_threads') {
82-
this.startByWorkerThreads();
83-
} else {
84-
this.startByProcess();
85-
}
86-
8780
this.log(`[master] =================== ${frameworkPkg.name} start 🥚🥚🥚🥚 =====================`);
8881
this.logger.info(`[master] node version ${process.version}`);
8982
/* istanbul ignore next */
@@ -193,16 +186,22 @@ export class Master extends ReadyEventEmitter {
193186
fs.writeFileSync(this.options.pidFile, process.pid.toString(), 'utf-8');
194187
}
195188

196-
this.detectPorts().then(() => {
197-
this.forkAgentWorker();
198-
});
199-
200189
// exit when agent or worker exception
201190
this.workerManager.on('exception', (count: { agent: number; worker: number }) => {
202191
const err = new ClusterWorkerExceptionError(count.agent, count.worker);
203192
this.logger.error(err);
204193
process.exit(1);
205194
});
195+
196+
await this.detectPorts();
197+
// set app & agent worker impl
198+
if (this.options.startMode === 'worker_threads') {
199+
this.startByWorkerThreads();
200+
} else {
201+
this.startByProcess();
202+
}
203+
204+
this.forkAgentWorker();
206205
}
207206

208207
startByProcess(): void {
@@ -240,6 +239,7 @@ export class Master extends ReadyEventEmitter {
240239
try {
241240
const clusterPort = await detectPort();
242241
this.options.clusterPort = clusterPort;
242+
this.log('[master] detected cluster port: %s', clusterPort);
243243
// If sticky mode, detect worker port
244244
if (this.options.sticky) {
245245
const stickyWorkerPort = await detectPort();

packages/cluster/src/utils/mode/impl/process/agent.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export class AgentProcessUtils extends BaseAgentUtils {
5656
forkOptions.execArgv = process.execArgv.concat([`--inspect-port=${debugPort}`]);
5757
}
5858

59-
debug('forkOptions: %j', forkOptions);
59+
debug('forkOptions: %j, args: %s', forkOptions, args);
6060
const agentProcess = (this.#agentProcess = fork(this.getAgentWorkerFile(), args, forkOptions));
6161
const agentWorker = (this.instance = new AgentProcessWorker(agentProcess));
6262
agentWorker.status = 'starting';

packages/egg/package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
"exports": {
1111
".": "./dist/index.js",
1212
"./agent": "./dist/agent.js",
13+
"./ajv": "./dist/ajv.js",
14+
"./aop": "./dist/aop.js",
1315
"./app/extend/context": "./dist/app/extend/context.js",
1416
"./app/extend/helper": "./dist/app/extend/helper.js",
1517
"./app/extend/request": "./dist/app/extend/request.js",
@@ -23,6 +25,8 @@
2325
"./config/config.local": "./dist/config/config.local.js",
2426
"./config/config.unittest": "./dist/config/config.unittest.js",
2527
"./config/plugin": "./dist/config/plugin.js",
28+
"./dal": "./dist/dal.js",
29+
"./helper": "./dist/helper.js",
2630
"./lib/agent": "./dist/lib/agent.js",
2731
"./lib/application": "./dist/lib/application.js",
2832
"./lib/core/base_context_class": "./dist/lib/core/base_context_class.js",
@@ -37,6 +41,7 @@
3741
"./lib/core/messenger/ipc": "./dist/lib/core/messenger/ipc.js",
3842
"./lib/core/messenger/local": "./dist/lib/core/messenger/local.js",
3943
"./lib/core/utils": "./dist/lib/core/utils.js",
44+
"./lib/define": "./dist/lib/define.js",
4045
"./lib/egg": "./dist/lib/egg.js",
4146
"./lib/error": "./dist/lib/error/index.js",
4247
"./lib/error/CookieLimitExceedError": "./dist/lib/error/CookieLimitExceedError.js",
@@ -48,7 +53,9 @@
4853
"./lib/start": "./dist/lib/start.js",
4954
"./lib/types": "./dist/lib/types.js",
5055
"./lib/types.plugin": "./dist/lib/types.plugin.js",
56+
"./orm": "./dist/orm.js",
5157
"./schedule": "./dist/schedule.js",
58+
"./transaction": "./dist/transaction.js",
5259
"./urllib": "./dist/urllib.js",
5360
"./package.json": "./package.json"
5461
}
@@ -60,6 +67,8 @@
6067
"exports": {
6168
".": "./src/index.ts",
6269
"./agent": "./src/agent.ts",
70+
"./ajv": "./src/ajv.ts",
71+
"./aop": "./src/aop.ts",
6372
"./app/extend/context": "./src/app/extend/context.ts",
6473
"./app/extend/helper": "./src/app/extend/helper.ts",
6574
"./app/extend/request": "./src/app/extend/request.ts",
@@ -73,6 +82,8 @@
7382
"./config/config.local": "./src/config/config.local.ts",
7483
"./config/config.unittest": "./src/config/config.unittest.ts",
7584
"./config/plugin": "./src/config/plugin.ts",
85+
"./dal": "./src/dal.ts",
86+
"./helper": "./src/helper.ts",
7687
"./lib/agent": "./src/lib/agent.ts",
7788
"./lib/application": "./src/lib/application.ts",
7889
"./lib/core/base_context_class": "./src/lib/core/base_context_class.ts",
@@ -87,6 +98,7 @@
8798
"./lib/core/messenger/ipc": "./src/lib/core/messenger/ipc.ts",
8899
"./lib/core/messenger/local": "./src/lib/core/messenger/local.ts",
89100
"./lib/core/utils": "./src/lib/core/utils.ts",
101+
"./lib/define": "./src/lib/define.ts",
90102
"./lib/egg": "./src/lib/egg.ts",
91103
"./lib/error": "./src/lib/error/index.ts",
92104
"./lib/error/CookieLimitExceedError": "./src/lib/error/CookieLimitExceedError.ts",
@@ -98,7 +110,9 @@
98110
"./lib/start": "./src/lib/start.ts",
99111
"./lib/types": "./src/lib/types.ts",
100112
"./lib/types.plugin": "./src/lib/types.plugin.ts",
113+
"./orm": "./src/orm.ts",
101114
"./schedule": "./src/schedule.ts",
115+
"./transaction": "./src/transaction.ts",
102116
"./urllib": "./src/urllib.ts",
103117
"./package.json": "./package.json"
104118
},

packages/egg/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,5 +184,6 @@ export {
184184
BackgroundTaskHelper,
185185
type ContextEventBus,
186186
type EventBus,
187+
type Events,
187188
MetadataUtil,
188189
} from '@eggjs/tegg';

tools/egg-bin/src/commands/dev.ts

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { debuglog } from 'node:util';
22

33
import { Flags } from '@oclif/core';
4-
import { getConfig, getFrameworkPath } from '@eggjs/utils';
4+
import { getFrameworkPath } from '@eggjs/utils';
55
import { detect } from 'detect-port';
66

77
import { getSourceFilename } from '../utils.ts';
@@ -34,8 +34,8 @@ export default class Dev<T extends typeof Dev> extends BaseCommand<T> {
3434
};
3535

3636
public async run(): Promise<void> {
37-
debug('NODE_ENV: %o', this.env);
3837
this.env.NODE_ENV = this.env.NODE_ENV ?? 'development';
38+
debug('NODE_ENV: %o', this.env.NODE_ENV);
3939
this.env.EGG_MASTER_CLOSE_TIMEOUT = '1000';
4040
const ext = this.isESM ? 'mjs' : 'cjs';
4141
const serverBin = getSourceFilename(`../scripts/start-cluster.${ext}`);
@@ -73,30 +73,13 @@ export default class Dev<T extends typeof Dev> extends BaseCommand<T> {
7373
});
7474

7575
if (!flags.port) {
76-
let configuredPort: number | undefined;
77-
try {
78-
const configuration = await getConfig({
79-
framework: flags.framework,
80-
baseDir: flags.base,
81-
env: 'local',
82-
});
83-
configuredPort = configuration?.cluster?.listen?.port;
84-
} catch (err) {
85-
/** skip when failing to read the configuration */
86-
debug('getConfig error: %s, framework: %o, baseDir: %o, env: local', err, flags.framework, flags.base);
87-
}
88-
if (configuredPort) {
89-
flags.port = configuredPort;
90-
debug(`use port ${flags.port} from configuration file`);
91-
} else {
92-
const defaultPort = parseInt(process.env.EGG_BIN_DEFAULT_PORT ?? '7001');
93-
debug('detect available port');
94-
flags.port = await detect(defaultPort);
95-
if (flags.port !== defaultPort) {
96-
console.warn('[@eggjs/bin] server port %o is unavailable, now using port %o', defaultPort, flags.port);
97-
}
98-
debug(`use available port ${flags.port}`);
76+
const defaultPort = parseInt(process.env.EGG_BIN_DEFAULT_PORT ?? '7001');
77+
debug('detect available port');
78+
flags.port = await detect(defaultPort);
79+
if (flags.port !== defaultPort) {
80+
console.warn('[@eggjs/bin] server port %o is unavailable, now using port %o', defaultPort, flags.port);
9981
}
82+
debug(`use available port ${flags.port}`);
10083
}
10184

10285
return {

0 commit comments

Comments
 (0)