Skip to content

Commit

Permalink
fix: not create ctx logger proto (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
killagu authored Feb 10, 2023
1 parent 077044c commit 100886b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
16 changes: 12 additions & 4 deletions plugin/tegg/lib/CompatibleUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@ export class CompatibleUtil {
}

private static singletonModuleProxyFactory(app: Application, loadUnitInstance: LoadUnitInstance) {
let deprecated = false;
return function(_, p: PropertyKey) {
const proto = CompatibleUtil.getSingletonProto(p);
const eggObj = EggContainerFactory.getEggObject(proto);
app.deprecate(
`[egg/module] Please use await app.getEggObject(clazzName) instead of app.${loadUnitInstance.name}.${String(p)}`);
if (!deprecated) {
deprecated = true;
app.deprecate(
`[egg/module] Please use await app.getEggObject(clazzName) instead of app.${loadUnitInstance.name}.${String(p)}`);
}
return eggObj.obj;
};
}
Expand All @@ -51,11 +55,15 @@ export class CompatibleUtil {
static contextModuleProxyFactory(holder: object, ctx: Context, loadUnitInstance: LoadUnitInstance) {
const cacheKey = `_${loadUnitInstance.name}Proxy`;
if (!holder[cacheKey]) {
let deprecated = false;
const getter = function(_, p: PropertyKey) {
const proto = CompatibleUtil.getRequestProto(p);
const eggObj = EggContainerFactory.getEggObject(proto, p);
ctx.app.deprecate(
`[egg/module] Please use await ctx.getEggObject(clazzName) instead of ctx.${loadUnitInstance.name}.${String(p)}`);
if (!deprecated) {
deprecated = true;
ctx.app.deprecate(
`[egg/module] Please use await ctx.getEggObject(clazzName) instead of ctx.${loadUnitInstance.name}.${String(p)}`);
}
return eggObj.obj;
};
holder[cacheKey] = ProxyUtil.safeProxy(loadUnitInstance, getter);
Expand Down
17 changes: 12 additions & 5 deletions plugin/tegg/lib/EggAppLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ import { BackgroundTaskHelper } from '@eggjs/tegg-background-task';
import { EggObjectFactory } from '@eggjs/tegg-dynamic-inject-runtime';

export const APP_CLAZZ_BLACK_LIST = [ 'eggObjectFactory' ];

export const CONTEXT_CLAZZ_BLACK_LIST = [
// just use the app.logger, ctx logger is deprecated.
'logger',
];
export const DEFAULT_APP_CLAZZ: string[] = [];
export const DEFAULT_CONTEXT_CLAZZ = [
'user',
Expand Down Expand Up @@ -92,10 +97,10 @@ export class EggAppLoader implements Loader {
return func;
}

private getLoggerNames(ctxClazzNames: string[]): string[] {
private getLoggerNames(ctxClazzNames: string[], singletonClazzNames: string[]): string[] {
const loggerNames = Array.from(this.app.loggers.keys());
// filter logger/coreLogger
return loggerNames.filter(t => !ctxClazzNames.includes(t));
return loggerNames.filter(t => !ctxClazzNames.includes(t) && !singletonClazzNames.includes(t));
}

load(): EggProtoImplClass[] {
Expand All @@ -109,11 +114,13 @@ export class EggAppLoader implements Loader {
]);
APP_CLAZZ_BLACK_LIST.forEach(t => allSingletonClazzNameSet.delete(t));
const allSingletonClazzNames = Array.from(allSingletonClazzNameSet);
const allContextClazzNames = Array.from(new Set([
const allContextClazzNamesSet = new Set([
...contextProperties,
...DEFAULT_CONTEXT_CLAZZ,
]));
const loggerNames = this.getLoggerNames(allContextClazzNames);
]);
CONTEXT_CLAZZ_BLACK_LIST.forEach(t => allContextClazzNamesSet.delete(t));
const allContextClazzNames = Array.from(allContextClazzNamesSet);
const loggerNames = this.getLoggerNames(allContextClazzNames, allSingletonClazzNames);
const allSingletonClazzs = allSingletonClazzNames.map(name => this.buildClazz(name, EggType.APP));
const allContextClazzs = allContextClazzNames.map(name => this.buildClazz(name, EggType.CONTEXT));
const appLoggerClazzs = loggerNames.map(name => this.buildAppLoggerClazz(name));
Expand Down
27 changes: 21 additions & 6 deletions plugin/tegg/lib/EggQualifierProtoHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,23 @@ import {
EggType,
} from '@eggjs/tegg';
import { Application } from 'egg';
import { APP_CLAZZ_BLACK_LIST, DEFAULT_APP_CLAZZ, DEFAULT_CONTEXT_CLAZZ } from './EggAppLoader';
import {
APP_CLAZZ_BLACK_LIST,
CONTEXT_CLAZZ_BLACK_LIST,
DEFAULT_APP_CLAZZ,
DEFAULT_CONTEXT_CLAZZ,
} from './EggAppLoader';
import { ObjectUtils } from '@eggjs/tegg-common-util';

export class EggQualifierProtoHook implements LifecycleHook<LoadUnitLifecycleContext, LoadUnit> {
private readonly app: Application;
private readonly appProperties: string[];
private readonly ctxProperties: string[];

constructor(app: Application) {
this.app = app;
this.appProperties = ObjectUtils.getProperties(this.app);
this.ctxProperties = ObjectUtils.getProperties(this.app.context);
}

async preCreate(ctx: LoadUnitLifecycleContext): Promise<void> {
Expand All @@ -35,19 +45,24 @@ export class EggQualifierProtoHook implements LifecycleHook<LoadUnitLifecycleCon
}

private isAppObject(name: PropertyKey) {
if (APP_CLAZZ_BLACK_LIST.includes(String(name))) {
name = String(name);
if (APP_CLAZZ_BLACK_LIST.includes(name)) {
return false;
}
if (DEFAULT_APP_CLAZZ.includes(String(name))) {
if (DEFAULT_APP_CLAZZ.includes(name)) {
return true;
}
return this.app.hasOwnProperty(name);
return this.appProperties.includes(name);
}

private isCtxObject(name: PropertyKey) {
if (DEFAULT_CONTEXT_CLAZZ.includes(String(name))) {
name = String(name);
if (CONTEXT_CLAZZ_BLACK_LIST.includes(name)) {
return false;
}
if (DEFAULT_CONTEXT_CLAZZ.includes(name)) {
return true;
}
return this.app.context.hasOwnProperty(name);
return this.ctxProperties.includes(name);
}
}

0 comments on commit 100886b

Please sign in to comment.