Skip to content

Commit

Permalink
fix: require support paths (#118)
Browse files Browse the repository at this point in the history
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

## Release Notes

- **Dependencies**
  - Updated `@eggjs/utils` package to version 4.2.1

- **Enhancements**
  - Improved module loading mechanism with new `baseDir` option
  - Added support for specifying module search paths during import

- **Testing**
  - Enhanced agent worker test suite with additional module injection
  - Added module execution verification in tests

- **Deprecation Notice**
- HTTPS configuration now recommends object-based format with `key` and
`cert`

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
fengmk2 authored Dec 30, 2024
1 parent aa3034f commit b74872c
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
},
"homepage": "https://github.com/eggjs/cluster#readme",
"dependencies": {
"@eggjs/utils": "^4.1.6",
"@eggjs/utils": "^4.2.1",
"@fengmk2/ps-tree": "^2.0.1",
"cfork": "^2.0.0",
"cluster-reload": "^2.0.0",
Expand Down
13 changes: 9 additions & 4 deletions src/agent_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ async function main() {
// $ node agent_worker.js options
const options = JSON.parse(process.argv[2]) as {
framework: string;
baseDir: string;
require?: string[];
startMode?: 'process' | 'worker_threads';
};
if (options.require) {
// inject
options.require.forEach(mod => {
require(mod);
});
for (const mod of options.require) {
await importModule(mod, {
paths: [ options.baseDir ],
});
}
}

let AgentWorker: typeof BaseAgentWorker;
Expand All @@ -36,7 +39,9 @@ async function main() {
}

const consoleLogger = new ConsoleLogger({ level: process.env.EGG_AGENT_WORKER_LOGGER_LEVEL });
const { Agent } = await importModule(options.framework);
const { Agent } = await importModule(options.framework, {
paths: [ options.baseDir ],
});
debug('new Agent with options %j', options);
let agent: any;
try {
Expand Down
9 changes: 7 additions & 2 deletions src/app_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ async function main() {
// $ node app_worker.js options-json-string
const options = JSON.parse(process.argv[2]) as {
framework: string;
baseDir: string;
require?: string[];
startMode?: 'process' | 'worker_threads';
port: number;
Expand All @@ -26,7 +27,9 @@ async function main() {
if (options.require) {
// inject
for (const mod of options.require) {
await importModule(mod);
await importModule(mod, {
paths: [ options.baseDir ],
});
}
}

Expand All @@ -40,7 +43,9 @@ async function main() {
const consoleLogger = new ConsoleLogger({
level: process.env.EGG_APP_WORKER_LOGGER_LEVEL,
});
const { Application } = await importModule(options.framework);
const { Application } = await importModule(options.framework, {
paths: [ options.baseDir ],
});
debug('[app_worker:%s] new Application with options %j', process.pid, options);
let app: any;
try {
Expand Down
4 changes: 3 additions & 1 deletion src/utils/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ export async function parseOptions(options?: ClusterOptions) {
framework: options.framework ?? options.customEgg,
});

const egg = await importModule(options.framework);
const egg = await importModule(options.framework, {
paths: [ options.baseDir! ],
});
assert(egg.Application, `should define Application in ${options.framework}`);
assert(egg.Agent, `should define Agent in ${options.framework}`);

Expand Down
3 changes: 2 additions & 1 deletion test/agent_worker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ describe('test/agent_worker.test.ts', () => {

it('support config agent debug port', () => {
mm(process.env, 'EGG_AGENT_DEBUG_PORT', '15800');
app = cluster('apps/agent-debug-port', { isDebug: true } as any);
app = cluster('apps/agent-debug-port', { isDebug: true, require: [ './inject1.js' ] } as any);
return app
// .debug()
.expect('stdout', /@@inject1\.js run/)
.expect('stdout', /=15800/)
.end();
});
Expand Down
2 changes: 0 additions & 2 deletions test/fixtures/apps/agent-debug-port/agent.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

module.exports = () => {
console.log('agent argv: ', process.execArgv);
};
1 change: 1 addition & 0 deletions test/fixtures/apps/agent-debug-port/inject1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('@@inject1.js run');

0 comments on commit b74872c

Please sign in to comment.