From 8b0765e7a2340c5273261758a89b3656c7990d19 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Thu, 2 Jan 2025 16:32:21 +0800 Subject: [PATCH 1/2] fix: remove ContextDelegation --- package.json | 8 ++++---- src/base_context_class.ts | 12 ++++++------ src/egg.ts | 10 +++------- src/loader/context_loader.ts | 8 ++++---- src/loader/egg_loader.ts | 14 ++++++++------ test/index.test.ts | 24 +++++++++++++++++++++++- 6 files changed, 48 insertions(+), 28 deletions(-) diff --git a/package.json b/package.json index f81dcc1c..ee9cc05a 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ }, "homepage": "https://github.com/eggjs/core#readme", "dependencies": { - "@eggjs/koa": "^2.20.2", + "@eggjs/koa": "^2.20.3", "@eggjs/router": "^3.0.5", "@eggjs/utils": "^4.1.5", "egg-logger": "^3.5.0", @@ -55,11 +55,11 @@ }, "devDependencies": { "@arethetypeswrong/cli": "^0.17.1", - "@eggjs/bin": "^7.0.0", + "@eggjs/bin": "7", "@eggjs/tsconfig": "1", "@types/js-yaml": "4", "@types/mocha": "10", - "@types/node": "20", + "@types/node": "22", "@types/supertest": "6", "await-event": "2", "coffee": "5", @@ -68,7 +68,7 @@ "gals": "1", "js-yaml": "3", "mm": "3", - "pedding": "^2.0.0", + "pedding": "2", "rimraf": "6", "supertest": "7", "ts-node": "10", diff --git a/src/base_context_class.ts b/src/base_context_class.ts index ee9f542f..d029ac67 100644 --- a/src/base_context_class.ts +++ b/src/base_context_class.ts @@ -1,20 +1,20 @@ -import type { EggCore, ContextDelegation } from './egg.js'; +import type { EggCore, Context } from './egg.js'; /** * BaseContextClass is a base class that can be extended, * it's instantiated in context level, * {@link Helper}, {@link Service} is extending it. */ -export class BaseContextClass { - ctx: ContextDelegation; +export class BaseContextClass { + ctx: T; app: EggCore; config: Record; - service: BaseContextClass; + service: BaseContextClass; /** * @since 1.0.0 */ - constructor(ctx: ContextDelegation) { + constructor(ctx: T) { /** * @member {Context} BaseContextClass#ctx * @since 1.0.0 @@ -34,6 +34,6 @@ export class BaseContextClass { * @member {Service} BaseContextClass#service * @since 1.0.0 */ - this.service = ctx.service; + this.service = ctx.service as BaseContextClass; } } diff --git a/src/egg.ts b/src/egg.ts index dce086b6..6f513765 100644 --- a/src/egg.ts +++ b/src/egg.ts @@ -6,7 +6,6 @@ import { Request as KoaRequest, Response as KoaResponse, } from '@eggjs/koa'; import type { - ContextDelegation as KoaContextDelegation, MiddlewareFunc as KoaMiddlewareFunc, Next, } from '@eggjs/koa'; @@ -42,20 +41,18 @@ export { // export @eggjs/koa types export type { - Next, KoaMiddlewareFunc, KoaContextDelegation, + Next, KoaMiddlewareFunc, }; // export @eggjs/core classes export class Request extends KoaRequest { declare app: EggCore; declare response: Response; - declare ctx: ContextDelegation; } -export class Response extends KoaResponse { +export class Response extends KoaResponse { declare app: EggCore; declare request: Request; - declare ctx: ContextDelegation; } export class Context extends KoaContext { @@ -66,8 +63,7 @@ export class Context extends KoaContext { } // export @eggjs/core types -export type ContextDelegation = KoaContextDelegation & Context; -export type MiddlewareFunc = KoaMiddlewareFunc; +export type MiddlewareFunc = KoaMiddlewareFunc; export class EggCore extends KoaApplication { options: EggCoreOptions; diff --git a/src/loader/context_loader.ts b/src/loader/context_loader.ts index 532f6c50..5a0866bd 100644 --- a/src/loader/context_loader.ts +++ b/src/loader/context_loader.ts @@ -1,18 +1,18 @@ import assert from 'node:assert'; import { isClass, isPrimitive } from 'is-type-of'; import { FileLoader, EXPORTS, type FileLoaderOptions } from './file_loader.js'; -import type { ContextDelegation } from '../egg.js'; +import type { Context } from '../egg.js'; const CLASS_LOADER = Symbol('classLoader'); export interface ClassLoaderOptions { - ctx: ContextDelegation; + ctx: Context; properties: any; } export class ClassLoader { readonly _cache = new Map(); - _ctx: ContextDelegation; + _ctx: Context; constructor(options: ClassLoaderOptions) { assert(options.ctx, 'options.ctx is required'); @@ -98,7 +98,7 @@ export class ContextLoader extends FileLoader { } } -function getInstance(values: any, ctx: ContextDelegation) { +function getInstance(values: any, ctx: Context) { // it's a directory when it has no exports // then use ClassLoader const Class = values[EXPORTS] ? values : null; diff --git a/src/loader/egg_loader.ts b/src/loader/egg_loader.ts index 2e157eca..5059b631 100644 --- a/src/loader/egg_loader.ts +++ b/src/loader/egg_loader.ts @@ -7,7 +7,7 @@ import { isAsyncFunction, isClass, isGeneratorFunction, isObject, isPromise } fr import type { Logger } from 'egg-logger'; import { getParamNames, readJSONSync, readJSON } from 'utility'; import { extend } from 'extend2'; -import { Request, Response, Context, Application } from '@eggjs/koa'; +import { Request, Response, Application, Context as KoaContext } from '@eggjs/koa'; import { pathMatching, type PathMatchingOptions } from 'egg-path-matching'; import { now, diff } from 'performance-ms'; import { FULLPATH, FileLoader, FileLoaderOptions } from './file_loader.js'; @@ -15,15 +15,17 @@ import { ContextLoader, ContextLoaderOptions } from './context_loader.js'; import utils, { Fun } from '../utils/index.js'; import sequencify from '../utils/sequencify.js'; import { Timing } from '../utils/timing.js'; -import type { ContextDelegation, EggCore, MiddlewareFunc } from '../egg.js'; -import { BaseContextClass } from '../base_context_class.js'; +import type { + Context, EggCore, MiddlewareFunc, +} from '../egg.js'; +import type { BaseContextClass } from '../base_context_class.js'; const debug = debuglog('@eggjs/core/loader/egg_loader'); const originalPrototypes: Record = { request: Request.prototype, response: Response.prototype, - context: Context.prototype, + context: KoaContext.prototype, application: Application.prototype, }; @@ -1691,7 +1693,7 @@ function wrapControllerClass(Controller: typeof BaseContextClass, fullPath: stri } function controllerMethodToMiddleware(Controller: typeof BaseContextClass, key: string) { - return function classControllerMiddleware(this: ContextDelegation, ...args: any[]) { + return function classControllerMiddleware(this: Context, ...args: any[]) { const controller: any = new Controller(this); if (!this.app.config.controller?.supportParams) { args = [ this ]; @@ -1727,7 +1729,7 @@ function wrapObject(obj: Record, fullPath: string, prefix?: string) } function objectFunctionToMiddleware(func: Fun) { - async function objectControllerMiddleware(this: ContextDelegation, ...args: any[]) { + async function objectControllerMiddleware(this: Context, ...args: any[]) { if (!this.app.config.controller?.supportParams) { args = [ this ]; } diff --git a/test/index.test.ts b/test/index.test.ts index 4c52abe6..510a8596 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -3,10 +3,32 @@ import * as EggCore from '../src/index.js'; describe('test/index.test.ts', () => { it('should expose properties', () => { - // console.log(EggCore); assert(EggCore.EggCore); assert(EggCore.EggLoader); assert(EggCore.BaseContextClass); assert(EggCore.utils); + console.log(Object.keys(EggCore)); + assert.deepEqual(Object.keys(EggCore), [ + 'BaseContextClass', + 'ClassLoader', + 'Context', + 'ContextLoader', + 'EGG_LOADER', + 'EXPORTS', + 'EggCore', + 'EggLoader', + 'FULLPATH', + 'FileLoader', + 'KoaApplication', + 'KoaContext', + 'KoaRequest', + 'KoaResponse', + 'Lifecycle', + 'Request', + 'Response', + 'Router', + 'Timing', + 'utils', + ]); }); }); From c03ea5949d4e0b5293e9ea6ed0e1b97ed3a9189b Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Thu, 2 Jan 2025 17:08:46 +0800 Subject: [PATCH 2/2] f --- package.json | 5 ++--- test/asyncLocalStorage.test.ts | 2 +- test/egg-ts.test.ts | 4 ++-- test/egg.test.ts | 4 ++-- test/loader/context_loader.test.ts | 2 +- test/loader/egg_loader.test.ts | 2 +- test/loader/get_app_info.test.ts | 2 +- test/loader/get_framework_paths.test.ts | 2 +- test/loader/get_load_units.test.ts | 2 +- test/loader/get_server_env.test.ts | 2 +- test/loader/load_file.test.ts | 2 +- test/loader/mixin/load_config.test.ts | 2 +- test/loader/mixin/load_controller.test.ts | 2 +- test/loader/mixin/load_custom_loader.test.ts | 2 +- test/loader/mixin/load_extend.test.ts | 4 ++-- test/loader/mixin/load_extend_class.test.ts | 4 ++-- test/loader/mixin/load_helper_extend.test.ts | 2 +- test/loader/mixin/load_middleware.test.ts | 2 +- test/loader/mixin/load_plugin.test.ts | 2 +- test/loader/mixin/load_service.test.ts | 4 ++-- test/support-typescript.test.ts | 2 +- test/utils/index.test.ts | 2 +- test/utils/router.test.ts | 2 +- 23 files changed, 29 insertions(+), 30 deletions(-) diff --git a/package.json b/package.json index ee9cc05a..632fde01 100644 --- a/package.json +++ b/package.json @@ -56,21 +56,20 @@ "devDependencies": { "@arethetypeswrong/cli": "^0.17.1", "@eggjs/bin": "7", + "@eggjs/supertest": "^8.1.1", "@eggjs/tsconfig": "1", "@types/js-yaml": "4", "@types/mocha": "10", "@types/node": "22", - "@types/supertest": "6", "await-event": "2", "coffee": "5", "eslint": "8", "eslint-config-egg": "14", "gals": "1", "js-yaml": "3", - "mm": "3", + "mm": "^4.0.2", "pedding": "2", "rimraf": "6", - "supertest": "7", "ts-node": "10", "tshy": "3", "tshy-after": "1", diff --git a/test/asyncLocalStorage.test.ts b/test/asyncLocalStorage.test.ts index aa4c4662..60b0e40d 100644 --- a/test/asyncLocalStorage.test.ts +++ b/test/asyncLocalStorage.test.ts @@ -1,6 +1,6 @@ import { strict as assert } from 'node:assert'; import { AsyncLocalStorage } from 'node:async_hooks'; -import request from 'supertest'; +import { request } from '@eggjs/supertest'; import { getAsyncLocalStorage, kGALS } from 'gals'; import { getFilepath } from './helper.js'; import { Application } from './fixtures/egg-esm/index.js'; diff --git a/test/egg-ts.test.ts b/test/egg-ts.test.ts index 66368681..2533a7f4 100644 --- a/test/egg-ts.test.ts +++ b/test/egg-ts.test.ts @@ -1,6 +1,6 @@ import { strict as assert } from 'node:assert'; -import mm from 'mm'; -import request from 'supertest'; +import { mm } from 'mm'; +import { request } from '@eggjs/supertest'; import coffee from 'coffee'; import { utils } from '../src/index.js'; import { Application, createApp, getFilepath } from './helper.js'; diff --git a/test/egg.test.ts b/test/egg.test.ts index 53b4d45d..f300b8fc 100644 --- a/test/egg.test.ts +++ b/test/egg.test.ts @@ -3,8 +3,8 @@ import path from 'node:path'; import { strict as assert } from 'node:assert'; import fs from 'node:fs/promises'; import { setTimeout as sleep } from 'node:timers/promises'; -import mm from 'mm'; -import request from 'supertest'; +import { mm } from 'mm'; +import { request } from '@eggjs/supertest'; import { pending } from 'pedding'; import coffee from 'coffee'; import { createApp, getFilepath, Application } from './helper.js'; diff --git a/test/loader/context_loader.test.ts b/test/loader/context_loader.test.ts index 27442136..f3dae817 100644 --- a/test/loader/context_loader.test.ts +++ b/test/loader/context_loader.test.ts @@ -1,4 +1,4 @@ -import request from 'supertest'; +import { request } from '@eggjs/supertest'; import { getFilepath, createApp, Application } from '../helper.js'; describe('test/loader/context_loader.test.ts', () => { diff --git a/test/loader/egg_loader.test.ts b/test/loader/egg_loader.test.ts index ac8bccdb..0f5c71c2 100644 --- a/test/loader/egg_loader.test.ts +++ b/test/loader/egg_loader.test.ts @@ -1,7 +1,7 @@ import { strict as assert } from 'node:assert'; import os from 'node:os'; import path from 'node:path'; -import mm from 'mm'; +import { mm } from 'mm'; import { getPlugins } from '@eggjs/utils'; import { Application, createApp, getFilepath } from '../helper.js'; import { EggLoader } from '../../src/index.js'; diff --git a/test/loader/get_app_info.test.ts b/test/loader/get_app_info.test.ts index a883f0a4..289265e6 100644 --- a/test/loader/get_app_info.test.ts +++ b/test/loader/get_app_info.test.ts @@ -1,5 +1,5 @@ import { strict as assert } from 'node:assert'; -import mm from 'mm'; +import { mm } from 'mm'; import { Application, createApp, getFilepath } from '../helper.js'; describe('test/loader/get_app_info.test.ts', () => { diff --git a/test/loader/get_framework_paths.test.ts b/test/loader/get_framework_paths.test.ts index 98d3f2c4..69f542e0 100644 --- a/test/loader/get_framework_paths.test.ts +++ b/test/loader/get_framework_paths.test.ts @@ -1,5 +1,5 @@ import { strict as assert } from 'node:assert'; -import mm from 'mm'; +import { mm } from 'mm'; import { importModule } from '@eggjs/utils'; import { Application, createApp, getFilepath } from '../helper.js'; import { EggLoader, EggCore } from '../../src/index.js'; diff --git a/test/loader/get_load_units.test.ts b/test/loader/get_load_units.test.ts index 5d16791d..391df5a2 100644 --- a/test/loader/get_load_units.test.ts +++ b/test/loader/get_load_units.test.ts @@ -1,5 +1,5 @@ import { strict as assert } from 'node:assert'; -import mm from 'mm'; +import { mm } from 'mm'; import { Application, createApp, getFilepath } from '../helper.js'; describe('test/loader/get_load_units.test.ts', () => { diff --git a/test/loader/get_server_env.test.ts b/test/loader/get_server_env.test.ts index cee6d00b..182ed73b 100644 --- a/test/loader/get_server_env.test.ts +++ b/test/loader/get_server_env.test.ts @@ -1,5 +1,5 @@ import { strict as assert } from 'node:assert'; -import mm from 'mm'; +import { mm } from 'mm'; import { Application, createApp } from '../helper.js'; describe('test/loader/get_server_env.test.ts', () => { diff --git a/test/loader/load_file.test.ts b/test/loader/load_file.test.ts index 2fd94555..46fbb22d 100644 --- a/test/loader/load_file.test.ts +++ b/test/loader/load_file.test.ts @@ -1,5 +1,5 @@ import { strict as assert } from 'node:assert'; -import mm from 'mm'; +import { mm } from 'mm'; import { createApp, Application, getFilepath } from '../helper.js'; describe('test/loader/load_file.test.ts', () => { diff --git a/test/loader/mixin/load_config.test.ts b/test/loader/mixin/load_config.test.ts index b86c4183..ac0e40e4 100644 --- a/test/loader/mixin/load_config.test.ts +++ b/test/loader/mixin/load_config.test.ts @@ -1,6 +1,6 @@ import path from 'node:path'; import { strict as assert } from 'node:assert'; -import mm from 'mm'; +import { mm } from 'mm'; import { EggCore } from '../../../src/index.js'; import { Application, createApp, getFilepath } from '../../helper.js'; diff --git a/test/loader/mixin/load_controller.test.ts b/test/loader/mixin/load_controller.test.ts index fa86b3cf..69a9b432 100644 --- a/test/loader/mixin/load_controller.test.ts +++ b/test/loader/mixin/load_controller.test.ts @@ -1,6 +1,6 @@ import { strict as assert } from 'node:assert'; import path from 'node:path'; -import request from 'supertest'; +import { request } from '@eggjs/supertest'; import is from 'is-type-of'; import { Application, createApp, getFilepath } from '../../helper.js'; diff --git a/test/loader/mixin/load_custom_loader.test.ts b/test/loader/mixin/load_custom_loader.test.ts index 1f8835e0..8a50faad 100644 --- a/test/loader/mixin/load_custom_loader.test.ts +++ b/test/loader/mixin/load_custom_loader.test.ts @@ -1,5 +1,5 @@ import { strict as assert } from 'node:assert'; -import request from 'supertest'; +import { request } from '@eggjs/supertest'; import { Application, createApp } from '../../helper.js'; describe('test/loader/mixin/load_custom_loader.test.ts', () => { diff --git a/test/loader/mixin/load_extend.test.ts b/test/loader/mixin/load_extend.test.ts index 4b7735da..1d957c4e 100644 --- a/test/loader/mixin/load_extend.test.ts +++ b/test/loader/mixin/load_extend.test.ts @@ -1,6 +1,6 @@ import { strict as assert } from 'node:assert'; -import request from 'supertest'; -import mm from 'mm'; +import { request } from '@eggjs/supertest'; +import { mm } from 'mm'; import { Application, createApp } from '../../helper.js'; describe('test/loader/mixin/load_extend.test.ts', () => { diff --git a/test/loader/mixin/load_extend_class.test.ts b/test/loader/mixin/load_extend_class.test.ts index dd7e92cd..788acc20 100644 --- a/test/loader/mixin/load_extend_class.test.ts +++ b/test/loader/mixin/load_extend_class.test.ts @@ -1,6 +1,6 @@ import { strict as assert } from 'node:assert'; -import request from 'supertest'; -import mm from 'mm'; +import { request } from '@eggjs/supertest'; +import { mm } from 'mm'; import { Application, createApp } from '../../helper.js'; describe('test/loader/mixin/load_extend_class.test.ts', () => { diff --git a/test/loader/mixin/load_helper_extend.test.ts b/test/loader/mixin/load_helper_extend.test.ts index eb69e437..73fcfff7 100644 --- a/test/loader/mixin/load_helper_extend.test.ts +++ b/test/loader/mixin/load_helper_extend.test.ts @@ -1,4 +1,4 @@ -import request from 'supertest'; +import { request } from '@eggjs/supertest'; import { Application, createApp } from '../../helper.js'; describe('test/loader/mixin/load_helper_extend.test.ts', () => { diff --git a/test/loader/mixin/load_middleware.test.ts b/test/loader/mixin/load_middleware.test.ts index 53fda438..427e3d28 100644 --- a/test/loader/mixin/load_middleware.test.ts +++ b/test/loader/mixin/load_middleware.test.ts @@ -1,6 +1,6 @@ import path from 'node:path'; import { strict as assert } from 'node:assert'; -import request from 'supertest'; +import { request } from '@eggjs/supertest'; import { Application, createApp, getFilepath } from '../../helper.js'; describe('test/loader/mixin/load_middleware.test.ts', () => { diff --git a/test/loader/mixin/load_plugin.test.ts b/test/loader/mixin/load_plugin.test.ts index f411a066..a48db6b8 100644 --- a/test/loader/mixin/load_plugin.test.ts +++ b/test/loader/mixin/load_plugin.test.ts @@ -1,7 +1,7 @@ import path from 'node:path'; import fs from 'node:fs'; import { strict as assert } from 'node:assert'; -import mm from 'mm'; +import { mm } from 'mm'; import { Application, createApp, getFilepath } from '../../helper.js'; import { EggCore, EggLoader } from '../../../src/index.js'; diff --git a/test/loader/mixin/load_service.test.ts b/test/loader/mixin/load_service.test.ts index 0d3af815..cbc0129d 100644 --- a/test/loader/mixin/load_service.test.ts +++ b/test/loader/mixin/load_service.test.ts @@ -1,7 +1,7 @@ import path from 'node:path'; import { strict as assert } from 'node:assert'; -import request from 'supertest'; -import mm from 'mm'; +import { request } from '@eggjs/supertest'; +import { mm } from 'mm'; import { Application, createApp, getFilepath } from '../../helper.js'; describe('test/loader/mixin/load_service.test.ts', () => { diff --git a/test/support-typescript.test.ts b/test/support-typescript.test.ts index 5dcfb6ef..1833a574 100644 --- a/test/support-typescript.test.ts +++ b/test/support-typescript.test.ts @@ -1,5 +1,5 @@ import { strict as assert } from 'node:assert'; -import request from 'supertest'; +import { request } from '@eggjs/supertest'; import { getFilepath } from './helper.js'; import { Application } from './fixtures/egg-esm/index.js'; diff --git a/test/utils/index.test.ts b/test/utils/index.test.ts index 2d05a31c..aaa0c1b8 100644 --- a/test/utils/index.test.ts +++ b/test/utils/index.test.ts @@ -1,6 +1,6 @@ import path from 'node:path'; import { strict as assert } from 'node:assert'; -import mm from 'mm'; +import { mm } from 'mm'; import utils from '../../src/utils/index.js'; import { getFilepath } from '../helper.js'; diff --git a/test/utils/router.test.ts b/test/utils/router.test.ts index 121210b5..d28cd0c1 100644 --- a/test/utils/router.test.ts +++ b/test/utils/router.test.ts @@ -1,5 +1,5 @@ import { strict as assert } from 'node:assert'; -import request from 'supertest'; +import { request } from '@eggjs/supertest'; import { Application, createApp } from '../helper.js'; describe('test/utils/router.test.ts', () => {