@eggjs/tegg-standalone 4.0.0-beta.4 → 4.0.0-beta.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/README.md +13 -1
  2. package/dist/ConfigSourceLoadUnitHook.js +21 -0
  3. package/dist/EggModuleLoader.d.ts +22 -0
  4. package/dist/EggModuleLoader.js +54 -0
  5. package/dist/Runner.d.ts +54 -0
  6. package/dist/Runner.js +181 -0
  7. package/dist/StandaloneContext.d.ts +9 -0
  8. package/dist/StandaloneContext.js +14 -0
  9. package/dist/StandaloneContextHandler.js +18 -0
  10. package/dist/StandaloneInnerObject.d.ts +20 -0
  11. package/dist/StandaloneInnerObject.js +31 -0
  12. package/dist/StandaloneInnerObjectProto.d.ts +24 -0
  13. package/dist/StandaloneInnerObjectProto.js +49 -0
  14. package/dist/StandaloneLoadUnit.d.ts +10 -0
  15. package/dist/StandaloneLoadUnit.js +54 -0
  16. package/dist/index.d.ts +7 -6
  17. package/dist/index.js +8 -7
  18. package/dist/main.d.ts +7 -0
  19. package/dist/main.js +31 -0
  20. package/package.json +34 -36
  21. package/dist/src/ConfigSourceLoadUnitHook.d.ts +0 -9
  22. package/dist/src/ConfigSourceLoadUnitHook.js +0 -19
  23. package/dist/src/EggModuleLoader.d.ts +0 -18
  24. package/dist/src/EggModuleLoader.js +0 -59
  25. package/dist/src/LoadUnitInnerClassHook.d.ts +0 -5
  26. package/dist/src/LoadUnitInnerClassHook.js +0 -22
  27. package/dist/src/ModuleConfig.d.ts +0 -5
  28. package/dist/src/ModuleConfig.js +0 -3
  29. package/dist/src/Runner.d.ts +0 -50
  30. package/dist/src/Runner.js +0 -236
  31. package/dist/src/StandaloneContext.d.ts +0 -5
  32. package/dist/src/StandaloneContext.js +0 -10
  33. package/dist/src/StandaloneContextHandler.d.ts +0 -6
  34. package/dist/src/StandaloneContextHandler.js +0 -14
  35. package/dist/src/StandaloneContextImpl.d.ts +0 -5
  36. package/dist/src/StandaloneContextImpl.js +0 -10
  37. package/dist/src/StandaloneInnerObject.d.ts +0 -17
  38. package/dist/src/StandaloneInnerObject.js +0 -30
  39. package/dist/src/StandaloneInnerObjectProto.d.ts +0 -20
  40. package/dist/src/StandaloneInnerObjectProto.js +0 -50
  41. package/dist/src/StandaloneLoadUnit.d.ts +0 -23
  42. package/dist/src/StandaloneLoadUnit.js +0 -62
  43. package/dist/src/main.d.ts +0 -3
  44. package/dist/src/main.js +0 -34
package/README.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # `@eggjs/tegg-standalone`
2
2
 
3
+ [![NPM version][npm-image]][npm-url]
4
+ [![Known Vulnerabilities][snyk-image]][snyk-url]
5
+ [![npm download][download-image]][download-url]
6
+ [![Node.js Version](https://img.shields.io/node/v/@eggjs/tegg-standalone.svg?style=flat)](https://nodejs.org/en/download/)
7
+
8
+ [npm-image]: https://img.shields.io/npm/v/@eggjs/tegg-standalone.svg?style=flat-square
9
+ [npm-url]: https://npmjs.org/package/@eggjs/tegg-standalone
10
+ [snyk-image]: https://snyk.io/test/npm/@eggjs/tegg-standalone/badge.svg?style=flat-square
11
+ [snyk-url]: https://snyk.io/test/npm/@eggjs/tegg-standalone
12
+ [download-image]: https://img.shields.io/npm/dm/@eggjs/tegg-standalone.svg?style=flat-square
13
+ [download-url]: https://npmjs.org/package/@eggjs/tegg-standalone
14
+
3
15
  通过 `@eggjs/tegg-standalone` 在一个独立环境去中运行 tegg 应用。
4
16
 
5
17
  ## install
@@ -12,7 +24,7 @@ npm i --save @eggjs/tegg-standalone
12
24
  当一个类上有 Runner 注解时,会自动运行其 main 函数。注无需再使用 `ContextProto` 注解,因为独立运行跑完即销毁,不用再区分独立上下文。
13
25
 
14
26
  ```ts
15
- import { Runner, MainRunner } from '@eggjs/tegg/standalone';
27
+ import { Runner, type MainRunner } from '@eggjs/tegg/standalone';
16
28
 
17
29
  @Runner()
18
30
  @SingletonProto()
@@ -0,0 +1,21 @@
1
+ import "@eggjs/tegg-metadata";
2
+ import { ConfigSourceQualifier, ConfigSourceQualifierAttribute, PrototypeUtil, QualifierUtil } from "@eggjs/tegg";
3
+
4
+ //#region src/ConfigSourceLoadUnitHook.ts
5
+ /**
6
+ * Hook for inject moduleConfig.
7
+ * Add default qualifier value is current module name.
8
+ */
9
+ var ConfigSourceLoadUnitHook = class {
10
+ async preCreate(ctx, loadUnit) {
11
+ const classList = await ctx.loader.load();
12
+ for (const clazz of classList) {
13
+ const moduleConfigObject = PrototypeUtil.getInjectObjects(clazz).find((t) => t.objName === "moduleConfig");
14
+ const configSourceQualifier = QualifierUtil.getProperQualifier(clazz, "moduleConfig", ConfigSourceQualifierAttribute);
15
+ if (moduleConfigObject && !configSourceQualifier) ConfigSourceQualifier(loadUnit.name)(clazz.prototype, moduleConfigObject.refName);
16
+ }
17
+ }
18
+ };
19
+
20
+ //#endregion
21
+ export { ConfigSourceLoadUnitHook };
@@ -0,0 +1,22 @@
1
+ import { LoadUnit } from "@eggjs/tegg-metadata";
2
+ import { ModuleReference } from "@eggjs/tegg-common-util";
3
+ import { Logger } from "@eggjs/tegg";
4
+
5
+ //#region src/EggModuleLoader.d.ts
6
+ interface EggModuleLoaderOptions {
7
+ logger: Logger;
8
+ baseDir: string;
9
+ dump?: boolean;
10
+ }
11
+ declare class EggModuleLoader {
12
+ private moduleReferences;
13
+ private globalGraph;
14
+ private options;
15
+ constructor(moduleReferences: readonly ModuleReference[], options: EggModuleLoaderOptions);
16
+ init(): Promise<void>;
17
+ private static generateAppGraph;
18
+ load(): Promise<LoadUnit[]>;
19
+ static preLoad(moduleReferences: readonly ModuleReference[], options: EggModuleLoaderOptions): Promise<void>;
20
+ }
21
+ //#endregion
22
+ export { EggModuleLoader, EggModuleLoaderOptions };
@@ -0,0 +1,54 @@
1
+ import { EggLoadUnitType, GlobalGraph, LoadUnitFactory, ModuleDescriptorDumper } from "@eggjs/tegg-metadata";
2
+ import { LoaderFactory } from "@eggjs/tegg-loader";
3
+
4
+ //#region src/EggModuleLoader.ts
5
+ var EggModuleLoader = class EggModuleLoader {
6
+ moduleReferences;
7
+ globalGraph;
8
+ options;
9
+ constructor(moduleReferences, options) {
10
+ this.moduleReferences = moduleReferences;
11
+ this.options = options;
12
+ }
13
+ async init() {
14
+ GlobalGraph.instance = this.globalGraph = await EggModuleLoader.generateAppGraph(this.moduleReferences, this.options);
15
+ }
16
+ static async generateAppGraph(moduleReferences, options) {
17
+ const moduleDescriptors = await LoaderFactory.loadApp(moduleReferences);
18
+ if (options.dump !== false) for (const moduleDescriptor of moduleDescriptors) ModuleDescriptorDumper.dump(moduleDescriptor, { dumpDir: options.baseDir }).catch((e) => {
19
+ e.message = "dump module descriptor failed: " + e.message;
20
+ options.logger.warn(e);
21
+ });
22
+ return await GlobalGraph.create(moduleDescriptors);
23
+ }
24
+ async load() {
25
+ const loadUnits = [];
26
+ this.globalGraph.build();
27
+ this.globalGraph.sort();
28
+ const moduleConfigList = GlobalGraph.instance.moduleConfigList;
29
+ for (const moduleConfig of moduleConfigList) {
30
+ const modulePath = moduleConfig.path;
31
+ const loader = LoaderFactory.createLoader(modulePath, EggLoadUnitType.MODULE);
32
+ const loadUnit = await LoadUnitFactory.createLoadUnit(modulePath, EggLoadUnitType.MODULE, loader);
33
+ loadUnits.push(loadUnit);
34
+ }
35
+ return loadUnits;
36
+ }
37
+ static async preLoad(moduleReferences, options) {
38
+ const loadUnits = [];
39
+ const loaderCache = /* @__PURE__ */ new Map();
40
+ const globalGraph = GlobalGraph.instance = await EggModuleLoader.generateAppGraph(moduleReferences, options);
41
+ globalGraph.sort();
42
+ const moduleConfigList = globalGraph.moduleConfigList;
43
+ for (const moduleConfig of moduleConfigList) {
44
+ const modulePath = moduleConfig.path;
45
+ const loader = loaderCache.get(modulePath);
46
+ const loadUnit = await LoadUnitFactory.createPreloadLoadUnit(modulePath, EggLoadUnitType.MODULE, loader);
47
+ loadUnits.push(loadUnit);
48
+ }
49
+ for (const load of loadUnits) await load.preLoad?.();
50
+ }
51
+ };
52
+
53
+ //#endregion
54
+ export { EggModuleLoader };
@@ -0,0 +1,54 @@
1
+ import { InnerObject } from "./StandaloneLoadUnit.js";
2
+ import { LoadUnit } from "@eggjs/tegg-metadata";
3
+ import { ModuleReference, ReadModuleReferenceOptions } from "@eggjs/tegg-common-util";
4
+ import { EggContext, LoadUnitInstance } from "@eggjs/tegg-runtime";
5
+ import { ModuleConfigHolder } from "@eggjs/tegg";
6
+
7
+ //#region src/Runner.d.ts
8
+ interface ModuleDependency extends ReadModuleReferenceOptions {
9
+ baseDir: string;
10
+ }
11
+ interface RunnerOptions {
12
+ /**
13
+ * @deprecated
14
+ * use inner object handlers instead
15
+ */
16
+ innerObjects?: Record<string, object>;
17
+ env?: string;
18
+ name?: string;
19
+ innerObjectHandlers?: Record<string, InnerObject[]>;
20
+ dependencies?: (string | ModuleDependency)[];
21
+ dump?: boolean;
22
+ }
23
+ declare class Runner {
24
+ readonly cwd: string;
25
+ readonly moduleReferences: readonly ModuleReference[];
26
+ readonly moduleConfigs: Record<string, ModuleConfigHolder>;
27
+ readonly env?: string;
28
+ readonly name?: string;
29
+ readonly options?: RunnerOptions;
30
+ private loadUnitLoader;
31
+ private runnerProto;
32
+ private configSourceEggPrototypeHook;
33
+ private loadUnitMultiInstanceProtoHook;
34
+ private dalTableEggPrototypeHook;
35
+ private dalModuleLoadUnitHook;
36
+ private transactionPrototypeHook;
37
+ private crosscutAdviceFactory;
38
+ private loadUnitAopHook;
39
+ private eggPrototypeCrossCutHook;
40
+ private eggObjectAopHook;
41
+ loadUnits: LoadUnit[];
42
+ loadUnitInstances: LoadUnitInstance[];
43
+ innerObjects: Record<string, InnerObject[]>;
44
+ constructor(cwd: string, options?: RunnerOptions);
45
+ load(): Promise<LoadUnit[]>;
46
+ static getModuleReferences(cwd: string, dependencies?: RunnerOptions['dependencies']): readonly ModuleReference[];
47
+ static preLoad(cwd: string, dependencies?: RunnerOptions['dependencies']): Promise<void>;
48
+ private initLoaderInstance;
49
+ init(): Promise<void>;
50
+ run<T>(aCtx?: EggContext): Promise<T>;
51
+ destroy(): Promise<void>;
52
+ }
53
+ //#endregion
54
+ export { ModuleDependency, Runner, RunnerOptions };
package/dist/Runner.js ADDED
@@ -0,0 +1,181 @@
1
+ import { EggModuleLoader } from "./EggModuleLoader.js";
2
+ import { StandaloneLoadUnit, StandaloneLoadUnitType } from "./StandaloneLoadUnit.js";
3
+ import { StandaloneContext } from "./StandaloneContext.js";
4
+ import { StandaloneContextHandler } from "./StandaloneContextHandler.js";
5
+ import { ConfigSourceLoadUnitHook } from "./ConfigSourceLoadUnitHook.js";
6
+ import { EggPrototypeLifecycleUtil, GlobalGraph, LoadUnitFactory, LoadUnitLifecycleUtil, LoadUnitMultiInstanceProtoHook } from "@eggjs/tegg-metadata";
7
+ import { ModuleConfigUtil } from "@eggjs/tegg-common-util";
8
+ import { ContextHandler, EggContainerFactory, EggObjectLifecycleUtil, LoadUnitInstanceFactory, ModuleLoadUnitInstance } from "@eggjs/tegg-runtime";
9
+ import { ConfigSourceQualifierAttribute, ModuleConfigs, PrototypeUtil } from "@eggjs/tegg";
10
+ import { StandaloneUtil } from "@eggjs/tegg/standalone";
11
+ import { CrosscutAdviceFactory } from "@eggjs/tegg/aop";
12
+ import { EggObjectAopHook, EggPrototypeCrossCutHook, LoadUnitAopHook, crossCutGraphHook, pointCutGraphHook } from "@eggjs/tegg-aop-runtime";
13
+ import { DalModuleLoadUnitHook, DalTableEggPrototypeHook, MysqlDataSourceManager, SqlMapManager, TableModelManager, TransactionPrototypeHook } from "@eggjs/tegg-dal-plugin";
14
+
15
+ //#region src/Runner.ts
16
+ var Runner = class Runner {
17
+ cwd;
18
+ moduleReferences;
19
+ moduleConfigs;
20
+ env;
21
+ name;
22
+ options;
23
+ loadUnitLoader;
24
+ runnerProto;
25
+ configSourceEggPrototypeHook;
26
+ loadUnitMultiInstanceProtoHook;
27
+ dalTableEggPrototypeHook;
28
+ dalModuleLoadUnitHook;
29
+ transactionPrototypeHook;
30
+ crosscutAdviceFactory;
31
+ loadUnitAopHook;
32
+ eggPrototypeCrossCutHook;
33
+ eggObjectAopHook;
34
+ loadUnits = [];
35
+ loadUnitInstances = [];
36
+ innerObjects;
37
+ constructor(cwd, options) {
38
+ this.cwd = cwd;
39
+ this.env = options?.env;
40
+ this.name = options?.name;
41
+ this.options = options;
42
+ this.moduleReferences = Runner.getModuleReferences(this.cwd, options?.dependencies);
43
+ this.moduleConfigs = {};
44
+ this.innerObjects = {
45
+ moduleConfigs: [{ obj: new ModuleConfigs(this.moduleConfigs) }],
46
+ moduleConfig: []
47
+ };
48
+ const runtimeConfig = {
49
+ baseDir: this.cwd,
50
+ name: this.name,
51
+ env: this.env
52
+ };
53
+ this.innerObjects.runtimeConfig = [{ obj: runtimeConfig }];
54
+ if (!ModuleConfigUtil.configNames) ModuleConfigUtil.configNames = ["module.default", `module.${this.env}`];
55
+ for (const reference of this.moduleReferences) {
56
+ const absoluteRef = {
57
+ path: ModuleConfigUtil.resolveModuleDir(reference.path, this.cwd),
58
+ name: reference.name
59
+ };
60
+ const moduleName = ModuleConfigUtil.readModuleNameSync(absoluteRef.path);
61
+ this.moduleConfigs[moduleName] = {
62
+ name: moduleName,
63
+ reference: absoluteRef,
64
+ config: ModuleConfigUtil.loadModuleConfigSync(absoluteRef.path)
65
+ };
66
+ }
67
+ for (const moduleConfig of Object.values(this.moduleConfigs)) this.innerObjects.moduleConfig.push({
68
+ obj: moduleConfig.config,
69
+ qualifiers: [{
70
+ attribute: ConfigSourceQualifierAttribute,
71
+ value: moduleConfig.name
72
+ }]
73
+ });
74
+ if (options?.innerObjects) for (const [name, obj] of Object.entries(options.innerObjects)) this.innerObjects[name] = [{ obj }];
75
+ else if (options?.innerObjectHandlers) Object.assign(this.innerObjects, options.innerObjectHandlers);
76
+ }
77
+ async load() {
78
+ StandaloneContextHandler.register();
79
+ LoadUnitFactory.registerLoadUnitCreator(StandaloneLoadUnitType, () => {
80
+ return new StandaloneLoadUnit(this.innerObjects);
81
+ });
82
+ LoadUnitInstanceFactory.registerLoadUnitInstanceClass(StandaloneLoadUnitType, ModuleLoadUnitInstance.createModuleLoadUnitInstance);
83
+ const standaloneLoadUnit = await LoadUnitFactory.createLoadUnit("MockStandaloneLoadUnitPath", StandaloneLoadUnitType, { async load() {
84
+ return [];
85
+ } });
86
+ const loadUnits = await this.loadUnitLoader.load();
87
+ return [standaloneLoadUnit, ...loadUnits];
88
+ }
89
+ static getModuleReferences(cwd, dependencies) {
90
+ return (dependencies || []).concat(cwd).reduce((list, baseDir) => {
91
+ const module = typeof baseDir === "string" ? { baseDir } : baseDir;
92
+ return list.concat(...ModuleConfigUtil.readModuleReference(module.baseDir, module));
93
+ }, []);
94
+ }
95
+ static async preLoad(cwd, dependencies) {
96
+ const moduleReferences = Runner.getModuleReferences(cwd, dependencies);
97
+ await EggModuleLoader.preLoad(moduleReferences, {
98
+ baseDir: cwd,
99
+ logger: console,
100
+ dump: false
101
+ });
102
+ }
103
+ async initLoaderInstance() {
104
+ this.loadUnitLoader = new EggModuleLoader(this.moduleReferences, {
105
+ logger: (this.innerObjects.logger && this.innerObjects.logger[0])?.obj || console,
106
+ baseDir: this.cwd,
107
+ dump: this.options?.dump
108
+ });
109
+ await this.loadUnitLoader.init();
110
+ GlobalGraph.instance.registerBuildHook(crossCutGraphHook);
111
+ GlobalGraph.instance.registerBuildHook(pointCutGraphHook);
112
+ const configSourceEggPrototypeHook = new ConfigSourceLoadUnitHook();
113
+ LoadUnitLifecycleUtil.registerLifecycle(configSourceEggPrototypeHook);
114
+ this.crosscutAdviceFactory = new CrosscutAdviceFactory();
115
+ this.loadUnitAopHook = new LoadUnitAopHook(this.crosscutAdviceFactory);
116
+ this.eggPrototypeCrossCutHook = new EggPrototypeCrossCutHook(this.crosscutAdviceFactory);
117
+ this.eggObjectAopHook = new EggObjectAopHook();
118
+ EggPrototypeLifecycleUtil.registerLifecycle(this.eggPrototypeCrossCutHook);
119
+ LoadUnitLifecycleUtil.registerLifecycle(this.loadUnitAopHook);
120
+ EggObjectLifecycleUtil.registerLifecycle(this.eggObjectAopHook);
121
+ this.loadUnitMultiInstanceProtoHook = new LoadUnitMultiInstanceProtoHook();
122
+ LoadUnitLifecycleUtil.registerLifecycle(this.loadUnitMultiInstanceProtoHook);
123
+ const logger = (this.innerObjects.logger && this.innerObjects.logger[0])?.obj || console;
124
+ this.dalModuleLoadUnitHook = new DalModuleLoadUnitHook(this.env ?? "", this.moduleConfigs, logger);
125
+ this.dalTableEggPrototypeHook = new DalTableEggPrototypeHook(logger);
126
+ this.transactionPrototypeHook = new TransactionPrototypeHook(this.moduleConfigs, logger);
127
+ EggPrototypeLifecycleUtil.registerLifecycle(this.dalTableEggPrototypeHook);
128
+ EggPrototypeLifecycleUtil.registerLifecycle(this.transactionPrototypeHook);
129
+ LoadUnitLifecycleUtil.registerLifecycle(this.dalModuleLoadUnitHook);
130
+ }
131
+ async init() {
132
+ await this.initLoaderInstance();
133
+ this.loadUnits = await this.load();
134
+ const instances = [];
135
+ for (const loadUnit of this.loadUnits) {
136
+ const instance = await LoadUnitInstanceFactory.createLoadUnitInstance(loadUnit);
137
+ instances.push(instance);
138
+ }
139
+ this.loadUnitInstances = instances;
140
+ const runnerClass = StandaloneUtil.getMainRunner();
141
+ if (!runnerClass) throw new Error("not found runner class. Do you add @Runner decorator?");
142
+ const proto = PrototypeUtil.getClazzProto(runnerClass);
143
+ if (!proto) throw new Error(`can not get proto for clazz ${runnerClass.name}`);
144
+ this.runnerProto = proto;
145
+ }
146
+ async run(aCtx) {
147
+ const lifecycle = {};
148
+ const ctx = aCtx || new StandaloneContext();
149
+ return await ContextHandler.run(ctx, async () => {
150
+ if (ctx.init) await ctx.init(lifecycle);
151
+ const runner = (await EggContainerFactory.getOrCreateEggObject(this.runnerProto)).obj;
152
+ try {
153
+ return await runner.main();
154
+ } finally {
155
+ if (ctx.destroy) ctx.destroy(lifecycle).catch((e) => {
156
+ e.message = `[tegg/standalone] destroy tegg context failed: ${e.message}`;
157
+ console.warn(e);
158
+ });
159
+ }
160
+ });
161
+ }
162
+ async destroy() {
163
+ if (this.loadUnitInstances) for (const instance of this.loadUnitInstances) await LoadUnitInstanceFactory.destroyLoadUnitInstance(instance);
164
+ if (this.loadUnits) for (const loadUnit of this.loadUnits) await LoadUnitFactory.destroyLoadUnit(loadUnit);
165
+ if (this.configSourceEggPrototypeHook) LoadUnitLifecycleUtil.deleteLifecycle(this.configSourceEggPrototypeHook);
166
+ if (this.eggPrototypeCrossCutHook) EggPrototypeLifecycleUtil.deleteLifecycle(this.eggPrototypeCrossCutHook);
167
+ if (this.loadUnitAopHook) LoadUnitLifecycleUtil.deleteLifecycle(this.loadUnitAopHook);
168
+ if (this.eggObjectAopHook) EggObjectLifecycleUtil.deleteLifecycle(this.eggObjectAopHook);
169
+ if (this.loadUnitMultiInstanceProtoHook) LoadUnitLifecycleUtil.deleteLifecycle(this.loadUnitMultiInstanceProtoHook);
170
+ if (this.dalTableEggPrototypeHook) EggPrototypeLifecycleUtil.deleteLifecycle(this.dalTableEggPrototypeHook);
171
+ if (this.dalModuleLoadUnitHook) LoadUnitLifecycleUtil.deleteLifecycle(this.dalModuleLoadUnitHook);
172
+ if (this.transactionPrototypeHook) EggPrototypeLifecycleUtil.deleteLifecycle(this.transactionPrototypeHook);
173
+ MysqlDataSourceManager.instance.clear();
174
+ SqlMapManager.instance.clear();
175
+ TableModelManager.instance.clear();
176
+ ModuleConfigUtil.setConfigNames(void 0);
177
+ }
178
+ };
179
+
180
+ //#endregion
181
+ export { Runner };
@@ -0,0 +1,9 @@
1
+ import { AbstractEggContext } from "@eggjs/tegg-runtime";
2
+
3
+ //#region src/StandaloneContext.d.ts
4
+ declare class StandaloneContext extends AbstractEggContext {
5
+ id: string;
6
+ constructor();
7
+ }
8
+ //#endregion
9
+ export { StandaloneContext };
@@ -0,0 +1,14 @@
1
+ import { AbstractEggContext } from "@eggjs/tegg-runtime";
2
+ import { IdenticalUtil } from "@eggjs/tegg-lifecycle";
3
+
4
+ //#region src/StandaloneContext.ts
5
+ var StandaloneContext = class extends AbstractEggContext {
6
+ id;
7
+ constructor() {
8
+ super();
9
+ this.id = IdenticalUtil.createContextId();
10
+ }
11
+ };
12
+
13
+ //#endregion
14
+ export { StandaloneContext };
@@ -0,0 +1,18 @@
1
+ import { ContextHandler } from "@eggjs/tegg-runtime";
2
+ import { AsyncLocalStorage } from "node:async_hooks";
3
+
4
+ //#region src/StandaloneContextHandler.ts
5
+ var StandaloneContextHandler = class StandaloneContextHandler {
6
+ static storage = new AsyncLocalStorage();
7
+ static register() {
8
+ ContextHandler.getContextCallback = () => {
9
+ return StandaloneContextHandler.storage.getStore();
10
+ };
11
+ ContextHandler.runInContextCallback = (context, fn) => {
12
+ return StandaloneContextHandler.storage.run(context, fn);
13
+ };
14
+ }
15
+ };
16
+
17
+ //#endregion
18
+ export { StandaloneContextHandler };
@@ -0,0 +1,20 @@
1
+ import { StandaloneInnerObjectProto } from "./StandaloneInnerObjectProto.js";
2
+ import { EggPrototype } from "@eggjs/tegg-metadata";
3
+ import { EggObject } from "@eggjs/tegg-runtime";
4
+ import { EggObjectName } from "@eggjs/tegg";
5
+
6
+ //#region src/StandaloneInnerObject.d.ts
7
+ declare const OBJ: unique symbol;
8
+ declare class StandaloneInnerObject implements EggObject {
9
+ readonly isReady: boolean;
10
+ private [OBJ];
11
+ readonly proto: StandaloneInnerObjectProto;
12
+ readonly name: EggObjectName;
13
+ readonly id: string;
14
+ constructor(name: EggObjectName, proto: StandaloneInnerObjectProto);
15
+ get obj(): object;
16
+ injectProperty(): void;
17
+ static createObject(name: EggObjectName, proto: EggPrototype): Promise<StandaloneInnerObject>;
18
+ }
19
+ //#endregion
20
+ export { StandaloneInnerObject };
@@ -0,0 +1,31 @@
1
+ import { StandaloneInnerObjectProto } from "./StandaloneInnerObjectProto.js";
2
+ import "@eggjs/tegg-metadata";
3
+ import { EggObjectFactory } from "@eggjs/tegg-runtime";
4
+ import { IdenticalUtil } from "@eggjs/tegg";
5
+
6
+ //#region src/StandaloneInnerObject.ts
7
+ const OBJ = Symbol("EggCompatibleObject#obj");
8
+ var StandaloneInnerObject = class StandaloneInnerObject {
9
+ isReady = true;
10
+ [OBJ];
11
+ proto;
12
+ name;
13
+ id;
14
+ constructor(name, proto) {
15
+ this.proto = proto;
16
+ this.name = name;
17
+ this.id = IdenticalUtil.createObjectId(this.proto.id);
18
+ }
19
+ get obj() {
20
+ if (!this[OBJ]) this[OBJ] = this.proto.constructEggObject();
21
+ return this[OBJ];
22
+ }
23
+ injectProperty() {}
24
+ static async createObject(name, proto) {
25
+ return new StandaloneInnerObject(name, proto);
26
+ }
27
+ };
28
+ EggObjectFactory.registerEggObjectCreateMethod(StandaloneInnerObjectProto, StandaloneInnerObject.createObject);
29
+
30
+ //#endregion
31
+ export { StandaloneInnerObject };
@@ -0,0 +1,24 @@
1
+ import { EggPrototype, EggPrototypeLifecycleContext, InjectObjectProto } from "@eggjs/tegg-metadata";
2
+ import { AccessLevel, EggProtoImplClass, EggPrototypeName, Id, MetaDataKey, ObjectInitTypeLike, QualifierInfo, QualifierValue } from "@eggjs/tegg";
3
+
4
+ //#region src/StandaloneInnerObjectProto.d.ts
5
+ declare class StandaloneInnerObjectProto implements EggPrototype {
6
+ [key: symbol]: PropertyDescriptor;
7
+ private readonly clazz;
8
+ private readonly qualifiers;
9
+ readonly id: string;
10
+ readonly name: EggPrototypeName;
11
+ readonly initType: ObjectInitTypeLike;
12
+ readonly accessLevel: AccessLevel;
13
+ readonly injectObjects: InjectObjectProto[];
14
+ readonly loadUnitId: Id;
15
+ constructor(id: string, name: EggPrototypeName, clazz: EggProtoImplClass, initType: ObjectInitTypeLike, loadUnitId: Id, qualifiers: QualifierInfo[]);
16
+ verifyQualifiers(qualifiers: QualifierInfo[]): boolean;
17
+ verifyQualifier(qualifier: QualifierInfo): boolean;
18
+ constructEggObject(): object;
19
+ getMetaData<T>(metadataKey: MetaDataKey): T | undefined;
20
+ getQualifier(attribute: string): QualifierValue | undefined;
21
+ static create(ctx: EggPrototypeLifecycleContext): EggPrototype;
22
+ }
23
+ //#endregion
24
+ export { StandaloneInnerObjectProto };
@@ -0,0 +1,49 @@
1
+ import "@eggjs/tegg-metadata";
2
+ import { AccessLevel, IdenticalUtil, MetadataUtil, QualifierUtil } from "@eggjs/tegg";
3
+
4
+ //#region src/StandaloneInnerObjectProto.ts
5
+ var StandaloneInnerObjectProto = class StandaloneInnerObjectProto {
6
+ clazz;
7
+ qualifiers;
8
+ id;
9
+ name;
10
+ initType;
11
+ accessLevel;
12
+ injectObjects;
13
+ loadUnitId;
14
+ constructor(id, name, clazz, initType, loadUnitId, qualifiers) {
15
+ this.id = id;
16
+ this.clazz = clazz;
17
+ this.name = name;
18
+ this.initType = initType;
19
+ this.accessLevel = AccessLevel.PUBLIC;
20
+ this.injectObjects = [];
21
+ this.loadUnitId = loadUnitId;
22
+ this.qualifiers = qualifiers;
23
+ }
24
+ verifyQualifiers(qualifiers) {
25
+ for (const qualifier of qualifiers) if (!this.verifyQualifier(qualifier)) return false;
26
+ return true;
27
+ }
28
+ verifyQualifier(qualifier) {
29
+ return this.qualifiers.find((t) => t.attribute === qualifier.attribute)?.value === qualifier.value;
30
+ }
31
+ constructEggObject() {
32
+ return Reflect.apply(this.clazz, null, []);
33
+ }
34
+ getMetaData(metadataKey) {
35
+ return MetadataUtil.getMetaData(metadataKey, this.clazz);
36
+ }
37
+ getQualifier(attribute) {
38
+ return this.qualifiers.find((t) => t.attribute === attribute)?.value;
39
+ }
40
+ static create(ctx) {
41
+ const { clazz, loadUnit } = ctx;
42
+ const name = ctx.prototypeInfo.name;
43
+ const id = IdenticalUtil.createProtoId(loadUnit.id, name);
44
+ return new StandaloneInnerObjectProto(id, name, clazz, ctx.prototypeInfo.initType, loadUnit.id, QualifierUtil.getProtoQualifiers(clazz));
45
+ }
46
+ };
47
+
48
+ //#endregion
49
+ export { StandaloneInnerObjectProto };
@@ -0,0 +1,10 @@
1
+ import { EggPrototype, LoadUnit } from "@eggjs/tegg-metadata";
2
+ import { QualifierInfo } from "@eggjs/tegg";
3
+
4
+ //#region src/StandaloneLoadUnit.d.ts
5
+ interface InnerObject {
6
+ obj: object;
7
+ qualifiers?: QualifierInfo[];
8
+ }
9
+ //#endregion
10
+ export { InnerObject };
@@ -0,0 +1,54 @@
1
+ import { StandaloneInnerObjectProto } from "./StandaloneInnerObjectProto.js";
2
+ import { EggPrototypeFactory } from "@eggjs/tegg-metadata";
3
+ import { MapUtil } from "@eggjs/tegg-common-util";
4
+ import { ObjectInitType } from "@eggjs/tegg";
5
+ import { IdenticalUtil as IdenticalUtil$1 } from "@eggjs/tegg-lifecycle";
6
+
7
+ //#region src/StandaloneLoadUnit.ts
8
+ const StandaloneLoadUnitType = "StandaloneLoadUnitType";
9
+ var StandaloneLoadUnit = class {
10
+ id = "StandaloneLoadUnit";
11
+ name = "StandaloneLoadUnit";
12
+ unitPath = "MockStandaloneLoadUnitPath";
13
+ type = StandaloneLoadUnitType;
14
+ innerObject;
15
+ protoMap = /* @__PURE__ */ new Map();
16
+ constructor(innerObject) {
17
+ this.innerObject = innerObject;
18
+ }
19
+ async init() {
20
+ for (const [name, objs] of Object.entries(this.innerObject)) for (const { obj, qualifiers } of objs) {
21
+ const proto = new StandaloneInnerObjectProto(IdenticalUtil$1.createProtoId(this.id, name), name, (() => obj), ObjectInitType.SINGLETON, this.id, qualifiers || []);
22
+ EggPrototypeFactory.instance.registerPrototype(proto, this);
23
+ }
24
+ }
25
+ containPrototype(proto) {
26
+ return !!this.protoMap.get(proto.name)?.find((t) => t === proto);
27
+ }
28
+ getEggPrototype(name, qualifiers) {
29
+ return this.protoMap.get(name)?.filter((proto) => proto.verifyQualifiers(qualifiers)) || [];
30
+ }
31
+ registerEggPrototype(proto) {
32
+ MapUtil.getOrStore(this.protoMap, proto.name, []).push(proto);
33
+ }
34
+ deletePrototype(proto) {
35
+ const protos = this.protoMap.get(proto.name);
36
+ if (protos) {
37
+ const index = protos.indexOf(proto);
38
+ if (index !== -1) protos.splice(index, 1);
39
+ }
40
+ }
41
+ async destroy() {
42
+ for (const namedProtoMap of this.protoMap.values()) for (const proto of namedProtoMap.values()) EggPrototypeFactory.instance.deletePrototype(proto, this);
43
+ this.protoMap.clear();
44
+ }
45
+ iterateEggPrototype() {
46
+ return Array.from(this.protoMap.values()).reduce((p, c) => {
47
+ p = p.concat(c);
48
+ return p;
49
+ }, []).values();
50
+ }
51
+ };
52
+
53
+ //#endregion
54
+ export { StandaloneLoadUnit, StandaloneLoadUnitType };
package/dist/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
- export * from './src/EggModuleLoader.js';
2
- export * from './src/Runner.js';
3
- export * from './src/main.js';
4
- export * from './src/StandaloneInnerObjectProto.js';
5
- export * from './src/StandaloneContext.js';
6
- export * from './src/StandaloneInnerObject.js';
1
+ import { EggModuleLoader, EggModuleLoaderOptions } from "./EggModuleLoader.js";
2
+ import { ModuleDependency, Runner, RunnerOptions } from "./Runner.js";
3
+ import { main, preLoad } from "./main.js";
4
+ import { StandaloneInnerObjectProto } from "./StandaloneInnerObjectProto.js";
5
+ import { StandaloneContext } from "./StandaloneContext.js";
6
+ import { StandaloneInnerObject } from "./StandaloneInnerObject.js";
7
+ export { EggModuleLoader, EggModuleLoaderOptions, ModuleDependency, Runner, RunnerOptions, StandaloneContext, StandaloneInnerObject, StandaloneInnerObjectProto, main, preLoad };
package/dist/index.js CHANGED
@@ -1,7 +1,8 @@
1
- export * from './src/EggModuleLoader.js';
2
- export * from './src/Runner.js';
3
- export * from './src/main.js';
4
- export * from './src/StandaloneInnerObjectProto.js';
5
- export * from './src/StandaloneContext.js';
6
- export * from './src/StandaloneInnerObject.js';
7
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxjQUFjLDBCQUEwQixDQUFDO0FBQ3pDLGNBQWMsaUJBQWlCLENBQUM7QUFDaEMsY0FBYyxlQUFlLENBQUM7QUFDOUIsY0FBYyxxQ0FBcUMsQ0FBQztBQUNwRCxjQUFjLDRCQUE0QixDQUFDO0FBQzNDLGNBQWMsZ0NBQWdDLENBQUMifQ==
1
+ import { EggModuleLoader } from "./EggModuleLoader.js";
2
+ import { StandaloneInnerObjectProto } from "./StandaloneInnerObjectProto.js";
3
+ import { StandaloneContext } from "./StandaloneContext.js";
4
+ import { Runner } from "./Runner.js";
5
+ import { main, preLoad } from "./main.js";
6
+ import { StandaloneInnerObject } from "./StandaloneInnerObject.js";
7
+
8
+ export { EggModuleLoader, Runner, StandaloneContext, StandaloneInnerObject, StandaloneInnerObjectProto, main, preLoad };
package/dist/main.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ import { RunnerOptions } from "./Runner.js";
2
+
3
+ //#region src/main.d.ts
4
+ declare function preLoad(cwd: string, dependencies?: RunnerOptions['dependencies']): Promise<void>;
5
+ declare function main<T = void>(cwd: string, options?: RunnerOptions): Promise<T>;
6
+ //#endregion
7
+ export { main, preLoad };