Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support file returning async functions when loading it #272

Merged
merged 8 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/loader/egg_loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import fs from 'node:fs';
import path from 'node:path';
import assert from 'node:assert';
import { debuglog, inspect } from 'node:util';
import { isAsyncFunction, isClass, isGeneratorFunction, isObject } from 'is-type-of';
import { homedir } from 'node-homedir';
import { isAsyncFunction, isClass, isGeneratorFunction, isObject, isPromise } from 'is-type-of';
import type { Logger } from 'egg-logger';
import { getParamNames, readJSONSync, readJSON } from 'utility';
import { extend } from 'extend2';
Expand Down Expand Up @@ -1447,6 +1447,9 @@ export class EggLoader {
let mod = await this.requireFile(fullpath);
if (typeof mod === 'function' && !isClass(mod)) {
mod = mod(...inject);
if (isPromise(mod)) {
mod = await mod;
}
}
return mod;
}
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/load_file/async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"use strict";
AntiMoron marked this conversation as resolved.
Show resolved Hide resolved

module.exports = async () => {
return { clients: "Test Config" };
};
5 changes: 5 additions & 0 deletions test/fixtures/load_file/es-module-default-async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"use strict";
exports.__esModule = true;
exports["default"] = async function () {
return { clients: "Test Config" };
};
3 changes: 3 additions & 0 deletions test/fixtures/load_file/es-module-default-null.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"use strict";
exports.__esModule = true;
exports["default"] = null;
5 changes: 5 additions & 0 deletions test/fixtures/load_file/es-module-default-promise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"use strict";
exports.__esModule = true;
exports["default"] = function () {
return Promise.resolve({ clients: "Test Config" });
};
AntiMoron marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 5 additions & 0 deletions test/fixtures/load_file/es-module-default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"use strict";
exports.__esModule = true;
AntiMoron marked this conversation as resolved.
Show resolved Hide resolved
exports["default"] = {
fn() {}
};
3 changes: 3 additions & 0 deletions test/fixtures/load_file/promise_function.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function () {
return Promise.resolve({ clients: "Test Config" });
};
4 changes: 4 additions & 0 deletions test/fixtures/loadfile-esm/es-module-default-async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"use strict";
export default async function () {
return { clients: "Test Config" };
};
4 changes: 4 additions & 0 deletions test/fixtures/loadfile-esm/es-module-default-promise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"use strict";
export default function () {
return Promise.resolve({ clients: "Test Config" });
};
5 changes: 5 additions & 0 deletions test/fixtures/loadfile/es-module-default-async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"use strict";
AntiMoron marked this conversation as resolved.
Show resolved Hide resolved
exports.__esModule = true;
exports["default"] = async function () {
return { clients: "Test Config" };
};
5 changes: 5 additions & 0 deletions test/fixtures/loadfile/es-module-default-promise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"use strict";
exports.__esModule = true;
AntiMoron marked this conversation as resolved.
Show resolved Hide resolved
exports["default"] = function () {
return Promise.resolve({ clients: "Test Config" });
};
36 changes: 36 additions & 0 deletions test/loader/load_file.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,40 @@ describe('test/loader/load_file.test.ts', () => {
}
assert.equal(result, '---\nmap:\n a: 1\n b: 2');
});

it('should load cjs module file which returns function returning a promise', async () => {
app = createApp('load_file');
const result = (await app.loader.loadFile(getFilepath('load_file/promise_function.js')));
assert.deepEqual(result, { clients: 'Test Config' });
});

it('should load cjs module file which returns async function', async () => {
app = createApp('load_file');
const result = (await app.loader.loadFile(getFilepath('load_file/async.js')));
assert.deepEqual(result, { clients: 'Test Config' });
});

it('should load compiled es module file', async () => {
app = createApp('load_file');
const result = (await app.loader.loadFile(getFilepath('load_file/es-module-default.js')));
assert(result.fn);
});

it('should load compiled es module file which default = null', async () => {
app = createApp('load_file');
const result = (await app.loader.loadFile(getFilepath('load_file/es-module-default-null.js')));
assert.equal(result, null);
});

it('should load compiled es module file which default = function returning a promise', async () => {
app = createApp('load_file');
const result = (await app.loader.loadFile(getFilepath('load_file/es-module-default-promise.js')));
assert.deepEqual(result, { clients: 'Test Config' });
});

it('should load compiled es module file which default = async function', async () => {
app = createApp('load_file');
const result = (await app.loader.loadFile(getFilepath('load_file/es-module-default-async.js')));
assert.deepEqual(result, { clients: 'Test Config' });
});
});
20 changes: 20 additions & 0 deletions test/utils/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ describe('test/utils/index.test.ts', () => {
assert.equal(result, null);
});

it('should load es module with default = async function', async () => {
const result = await utils.loadFile(path.join(baseDir, 'es-module-default-async.js'));
assert(typeof result().then === 'function');
});

it('should load es module with default = function returning a promise', async () => {
const result = await utils.loadFile(path.join(baseDir, 'es-module-default-promise.js'));
assert(typeof result().then === 'function');
});

it('should load no js file', async () => {
let result = (await utils.loadFile(path.join(baseDir, 'no-js.yml'))).toString();
if (process.platform === 'win32') {
Expand Down Expand Up @@ -110,6 +120,16 @@ describe('test/utils/index.test.ts', () => {
assert.equal(result, null);
});

it('should load es module with default = async function', async () => {
const result = await utils.loadFile(path.join(baseDir, 'es-module-default-async.js'));
assert(typeof result().then === 'function');
});

it('should load es module with default = function returning a promise', async () => {
const result = await utils.loadFile(path.join(baseDir, 'es-module-default-promise.js'));
assert(typeof result().then === 'function');
});

it('should load no js file', async () => {
let result = (await utils.loadFile(path.join(baseDir, 'no-js.yml'))).toString();
if (process.platform === 'win32') {
Expand Down
Loading