phecda-server 6.0.2 → 6.1.0
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.
- package/dist/{chunk-WXFZZZRY.mjs → chunk-2CFYUXPM.mjs} +1 -1
- package/dist/{chunk-HDQAT2IM.mjs → chunk-3H3T54IC.mjs} +116 -87
- package/dist/{chunk-7SYPUIBY.js → chunk-DVTBLV6I.js} +60 -60
- package/dist/{chunk-UXD62LGG.mjs → chunk-G7TD2VB7.mjs} +10 -2
- package/dist/{chunk-F3W3NN7F.mjs → chunk-J7B24YSE.mjs} +1 -1
- package/dist/{chunk-XCND2QW3.js → chunk-JDV2XJYE.js} +25 -25
- package/dist/chunk-NL6QBQAR.js +266 -0
- package/dist/{chunk-GHFSIZUO.js → chunk-TVT62GIX.js} +11 -3
- package/dist/core-BlN0Spy9.d.ts +47 -0
- package/dist/core-P1CsH6jz.d.mts +47 -0
- package/dist/helper.js +3 -3
- package/dist/helper.mjs +2 -2
- package/dist/index.d.mts +6 -5
- package/dist/index.d.ts +6 -5
- package/dist/index.js +49 -43
- package/dist/index.mjs +15 -9
- package/dist/rpc/bullmq/index.d.mts +1 -1
- package/dist/rpc/bullmq/index.d.ts +1 -1
- package/dist/rpc/bullmq/index.js +12 -12
- package/dist/rpc/bullmq/index.mjs +2 -2
- package/dist/rpc/kafka/index.d.mts +1 -1
- package/dist/rpc/kafka/index.d.ts +1 -1
- package/dist/rpc/kafka/index.js +12 -12
- package/dist/rpc/kafka/index.mjs +2 -2
- package/dist/rpc/nats/index.d.mts +1 -1
- package/dist/rpc/nats/index.d.ts +1 -1
- package/dist/rpc/nats/index.js +11 -11
- package/dist/rpc/nats/index.mjs +2 -2
- package/dist/rpc/rabbitmq/index.d.mts +1 -1
- package/dist/rpc/rabbitmq/index.d.ts +1 -1
- package/dist/rpc/rabbitmq/index.js +13 -13
- package/dist/rpc/rabbitmq/index.mjs +2 -2
- package/dist/rpc/redis/index.d.mts +1 -1
- package/dist/rpc/redis/index.d.ts +1 -1
- package/dist/rpc/redis/index.js +11 -11
- package/dist/rpc/redis/index.mjs +2 -2
- package/dist/server/elysia/index.d.mts +1 -1
- package/dist/server/elysia/index.d.ts +1 -1
- package/dist/server/elysia/index.js +36 -36
- package/dist/server/elysia/index.mjs +3 -3
- package/dist/server/express/index.d.mts +1 -1
- package/dist/server/express/index.d.ts +1 -1
- package/dist/server/express/index.js +34 -34
- package/dist/server/express/index.mjs +2 -2
- package/dist/server/fastify/index.d.mts +1 -1
- package/dist/server/fastify/index.d.ts +1 -1
- package/dist/server/fastify/index.js +35 -35
- package/dist/server/fastify/index.mjs +3 -3
- package/dist/server/h3/index.d.mts +1 -1
- package/dist/server/h3/index.d.ts +1 -1
- package/dist/server/h3/index.js +34 -34
- package/dist/server/h3/index.mjs +2 -2
- package/dist/server/hono/index.d.mts +1 -1
- package/dist/server/hono/index.d.ts +1 -1
- package/dist/server/hono/index.js +33 -33
- package/dist/server/hono/index.mjs +2 -2
- package/dist/server/hyper-express/index.d.mts +1 -1
- package/dist/server/hyper-express/index.d.ts +1 -1
- package/dist/server/hyper-express/index.js +33 -33
- package/dist/server/hyper-express/index.mjs +2 -2
- package/dist/server/koa/index.d.mts +1 -1
- package/dist/server/koa/index.d.ts +1 -1
- package/dist/server/koa/index.js +34 -34
- package/dist/server/koa/index.mjs +2 -2
- package/dist/test.d.mts +1 -1
- package/dist/test.d.ts +1 -1
- package/dist/test.js +6 -6
- package/dist/test.mjs +2 -2
- package/package.json +2 -2
- package/dist/chunk-LYEZ6RGX.js +0 -237
- package/dist/core-2SNw8m2B.d.mts +0 -30
- package/dist/core-BmJJLUqj.d.ts +0 -30
|
@@ -3,7 +3,7 @@ import {
|
|
|
3
3
|
IS_ONLY_GENERATE,
|
|
4
4
|
__name,
|
|
5
5
|
log
|
|
6
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-G7TD2VB7.mjs";
|
|
7
7
|
|
|
8
8
|
// src/meta.ts
|
|
9
9
|
var Meta = class {
|
|
@@ -25,13 +25,7 @@ import { getInject, getMergedMeta, getMetaKey, getMetaParams, getTag, invokeInit
|
|
|
25
25
|
import Debug from "debug";
|
|
26
26
|
var debug = Debug("phecda-server(createPhecda)");
|
|
27
27
|
var emitter = new EventEmitter();
|
|
28
|
-
|
|
29
|
-
const moduleMap = /* @__PURE__ */ new Map();
|
|
30
|
-
const meta = [];
|
|
31
|
-
const modelMap = /* @__PURE__ */ new WeakMap();
|
|
32
|
-
const modelSet = /* @__PURE__ */ new WeakSet();
|
|
33
|
-
const dependenceGraph = /* @__PURE__ */ new Map();
|
|
34
|
-
const { parseModule = /* @__PURE__ */ __name((module) => module, "parseModule"), parseMeta = /* @__PURE__ */ __name((meta2) => meta2, "parseMeta"), generators } = opts;
|
|
28
|
+
function defaultServerInject() {
|
|
35
29
|
if (!getInject("watcher")) {
|
|
36
30
|
setInject("watcher", ({ eventName, instance, property, options }) => {
|
|
37
31
|
const fn = typeof instance[property] === "function" ? instance[property].bind(instance) : (v) => instance[property] = v;
|
|
@@ -42,51 +36,98 @@ async function createPhecda(models, opts = {}) {
|
|
|
42
36
|
};
|
|
43
37
|
});
|
|
44
38
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
moduleMap.delete(tag);
|
|
52
|
-
modelMap.delete(module);
|
|
53
|
-
for (let i = meta.length - 1; i >= 0; i--) {
|
|
54
|
-
if (meta[i].data.tag === tag) meta.splice(i, 1);
|
|
55
|
-
}
|
|
56
|
-
return module;
|
|
39
|
+
}
|
|
40
|
+
__name(defaultServerInject, "defaultServerInject");
|
|
41
|
+
var phecdaNamespace = /* @__PURE__ */ new Map();
|
|
42
|
+
var ServerPhecda = class {
|
|
43
|
+
static {
|
|
44
|
+
__name(this, "ServerPhecda");
|
|
57
45
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
46
|
+
moduleMap = /* @__PURE__ */ new Map();
|
|
47
|
+
meta = [];
|
|
48
|
+
modelMap = /* @__PURE__ */ new WeakMap();
|
|
49
|
+
modelSet = /* @__PURE__ */ new WeakSet();
|
|
50
|
+
dependenceGraph = /* @__PURE__ */ new Map();
|
|
51
|
+
parseModule;
|
|
52
|
+
parseMeta;
|
|
53
|
+
generators;
|
|
54
|
+
constructor(options) {
|
|
55
|
+
defaultServerInject();
|
|
56
|
+
const { namespace = "default", parseModule = /* @__PURE__ */ __name((module) => module, "parseModule"), parseMeta = /* @__PURE__ */ __name((meta) => meta, "parseMeta"), generators = [] } = options;
|
|
57
|
+
phecdaNamespace.set(namespace, this);
|
|
58
|
+
this.parseMeta = parseMeta;
|
|
59
|
+
this.parseModule = parseModule;
|
|
60
|
+
this.generators = generators;
|
|
62
61
|
}
|
|
63
|
-
|
|
64
|
-
|
|
62
|
+
async start(models) {
|
|
63
|
+
for (const model of models) await this.buildDepModule(model);
|
|
64
|
+
const generateCode = /* @__PURE__ */ __name(async () => {
|
|
65
|
+
if (IS_HMR) {
|
|
66
|
+
return Promise.all(this.generators.map((generator) => {
|
|
67
|
+
debug(`generate "${generator.name}" code to ${generator.path}`);
|
|
68
|
+
return generator.output(this.meta);
|
|
69
|
+
}));
|
|
70
|
+
}
|
|
71
|
+
}, "generateCode");
|
|
72
|
+
generateCode().then(() => {
|
|
73
|
+
if (IS_ONLY_GENERATE) process.exit(4);
|
|
74
|
+
});
|
|
75
|
+
if (IS_HMR) {
|
|
76
|
+
if (!globalThis.__PS_HMR__) globalThis.__PS_HMR__ = [];
|
|
77
|
+
globalThis.__PS_HMR__?.push(async (files) => {
|
|
78
|
+
debug("reload files ");
|
|
79
|
+
for (const file of files) {
|
|
80
|
+
const models2 = await import(file);
|
|
81
|
+
for (const i in models2) {
|
|
82
|
+
if (isPhecda(models2[i])) await this.add(models2[i]);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
generateCode();
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
async add(Model) {
|
|
65
90
|
const tag = getTag(Model);
|
|
66
|
-
const oldInstance = await del(tag);
|
|
67
|
-
const { module: newModule } = await buildDepModule(Model);
|
|
68
|
-
if (oldInstance && dependenceGraph.has(tag)) {
|
|
91
|
+
const oldInstance = await this.del(tag);
|
|
92
|
+
const { module: newModule } = await this.buildDepModule(Model);
|
|
93
|
+
if (oldInstance && this.dependenceGraph.has(tag)) {
|
|
69
94
|
debug(`replace module "${String(tag)}"`);
|
|
70
95
|
[
|
|
71
|
-
...dependenceGraph.get(tag)
|
|
96
|
+
...this.dependenceGraph.get(tag)
|
|
72
97
|
].forEach((tag2) => {
|
|
73
|
-
const module = moduleMap.get(tag2);
|
|
98
|
+
const module = this.moduleMap.get(tag2);
|
|
74
99
|
for (const key in module) {
|
|
75
100
|
if (module[key] === oldInstance) module[key] = newModule;
|
|
76
101
|
}
|
|
77
102
|
});
|
|
78
103
|
}
|
|
79
104
|
}
|
|
80
|
-
|
|
81
|
-
|
|
105
|
+
async del(tag) {
|
|
106
|
+
if (!this.moduleMap.has(tag)) return;
|
|
107
|
+
const module = this.moduleMap.get(tag);
|
|
108
|
+
debug(`unmount module "${String(tag)}"`);
|
|
109
|
+
await invokeUnmount(module);
|
|
110
|
+
debug(`del module "${String(tag)}"`);
|
|
111
|
+
this.moduleMap.delete(tag);
|
|
112
|
+
this.modelMap.delete(module);
|
|
113
|
+
for (let i = this.meta.length - 1; i >= 0; i--) {
|
|
114
|
+
if (this.meta[i].data.tag === tag) this.meta.splice(i, 1);
|
|
115
|
+
}
|
|
116
|
+
return module;
|
|
117
|
+
}
|
|
118
|
+
async destroy() {
|
|
119
|
+
debug("destroy all");
|
|
120
|
+
for (const [tag] of this.moduleMap) await this.del(tag);
|
|
121
|
+
}
|
|
122
|
+
async buildDepModule(Model) {
|
|
82
123
|
const paramtypes = getParamTypes(Model);
|
|
83
124
|
let module;
|
|
84
125
|
const tag = getTag(Model);
|
|
85
|
-
if (moduleMap.has(tag)) {
|
|
86
|
-
module = moduleMap.get(tag);
|
|
126
|
+
if (this.moduleMap.has(tag)) {
|
|
127
|
+
module = this.moduleMap.get(tag);
|
|
87
128
|
if (!module) throw new Error(`exist Circular-Dependency or Multiple modules with the same name/tag [tag] ${String(tag)}--[module] ${Model}`);
|
|
88
|
-
if (modelMap.get(module) !== Model && !modelSet.has(Model)) {
|
|
89
|
-
modelSet.add(Model);
|
|
129
|
+
if (this.modelMap.get(module) !== Model && !this.modelSet.has(Model)) {
|
|
130
|
+
this.modelSet.add(Model);
|
|
90
131
|
if (module instanceof Model) log(`Module taged ${String(tag)} has been overridden`);
|
|
91
132
|
else log(`Synonym module: Module taged "${String(tag)}" has been loaded before, so phecda-server won't load Module "${Model.name}"`, "warn");
|
|
92
133
|
}
|
|
@@ -95,69 +136,59 @@ async function createPhecda(models, opts = {}) {
|
|
|
95
136
|
tag
|
|
96
137
|
};
|
|
97
138
|
}
|
|
98
|
-
moduleMap.set(tag, void 0);
|
|
139
|
+
this.moduleMap.set(tag, void 0);
|
|
99
140
|
debug(`instantiate module "${String(tag)}"`);
|
|
100
141
|
if (paramtypes) {
|
|
101
142
|
const paramtypesInstances = [];
|
|
102
143
|
for (const i in paramtypes) {
|
|
103
|
-
const { module: sub, tag: subTag } = await buildDepModule(paramtypes[i]);
|
|
144
|
+
const { module: sub, tag: subTag } = await this.buildDepModule(paramtypes[i]);
|
|
104
145
|
paramtypesInstances[i] = sub;
|
|
105
|
-
if (!dependenceGraph.has(subTag)) dependenceGraph.set(subTag, /* @__PURE__ */ new Set());
|
|
106
|
-
dependenceGraph.get(subTag).add(tag);
|
|
146
|
+
if (!this.dependenceGraph.has(subTag)) this.dependenceGraph.set(subTag, /* @__PURE__ */ new Set());
|
|
147
|
+
this.dependenceGraph.get(subTag).add(tag);
|
|
107
148
|
}
|
|
108
|
-
module = parseModule(new Model(...paramtypesInstances));
|
|
149
|
+
module = this.parseModule(new Model(...paramtypesInstances));
|
|
109
150
|
} else {
|
|
110
|
-
module = parseModule(new Model());
|
|
151
|
+
module = this.parseModule(new Model());
|
|
111
152
|
}
|
|
112
|
-
meta.push(...getMetaFromInstance(module, tag, Model.name).map(parseMeta).filter((item) => !!item));
|
|
153
|
+
this.meta.push(...getMetaFromInstance(module, tag, Model.name).map(this.parseMeta).filter((item) => !!item));
|
|
113
154
|
debug(`init module "${String(tag)}"`);
|
|
114
155
|
if (!IS_ONLY_GENERATE) await invokeInit(module);
|
|
115
156
|
debug(`add module "${String(tag)}"`);
|
|
116
|
-
moduleMap.set(tag, module);
|
|
117
|
-
modelMap.set(module, Model);
|
|
157
|
+
this.moduleMap.set(tag, module);
|
|
158
|
+
this.modelMap.set(module, Model);
|
|
118
159
|
return {
|
|
119
160
|
module,
|
|
120
161
|
tag
|
|
121
162
|
};
|
|
122
163
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
async function generateCode() {
|
|
126
|
-
if (generators && IS_HMR) {
|
|
127
|
-
return Promise.all(generators.map((generator) => {
|
|
128
|
-
debug(`generate "${generator.name}" code to ${generator.path}`);
|
|
129
|
-
return generator.output(meta);
|
|
130
|
-
}));
|
|
131
|
-
}
|
|
164
|
+
has(modelOrTag) {
|
|
165
|
+
return this.moduleMap.has(typeof modelOrTag === "function" ? getTag(modelOrTag) : modelOrTag);
|
|
132
166
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
if (
|
|
136
|
-
|
|
137
|
-
if (IS_HMR) {
|
|
138
|
-
if (!globalThis.__PS_HMR__) globalThis.__PS_HMR__ = [];
|
|
139
|
-
globalThis.__PS_HMR__?.push(async (files) => {
|
|
140
|
-
debug("reload files ");
|
|
141
|
-
for (const file of files) {
|
|
142
|
-
const models2 = await import(file);
|
|
143
|
-
for (const i in models2) {
|
|
144
|
-
if (isPhecda(models2[i])) await add(models2[i]);
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
generateCode();
|
|
148
|
-
});
|
|
167
|
+
get(modelOrTag) {
|
|
168
|
+
const tag = typeof modelOrTag === "function" ? getTag(modelOrTag) : modelOrTag;
|
|
169
|
+
if (!this.has(tag)) throw new Error(`module "${tag.toString()}" doesn't exist`);
|
|
170
|
+
return this.moduleMap.get(tag);
|
|
149
171
|
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
del,
|
|
156
|
-
destroy
|
|
157
|
-
};
|
|
172
|
+
};
|
|
173
|
+
async function Factory(models, opts = {}) {
|
|
174
|
+
const phecda = new ServerPhecda(opts);
|
|
175
|
+
await phecda.start(models);
|
|
176
|
+
return phecda;
|
|
158
177
|
}
|
|
159
|
-
__name(
|
|
160
|
-
|
|
178
|
+
__name(Factory, "Factory");
|
|
179
|
+
function useS(nsOrModel, namespace) {
|
|
180
|
+
if (!nsOrModel) {
|
|
181
|
+
namespace = "default";
|
|
182
|
+
} else {
|
|
183
|
+
if (typeof nsOrModel === "string") namespace = nsOrModel;
|
|
184
|
+
else if (!namespace) namespace = "default";
|
|
185
|
+
}
|
|
186
|
+
if (!phecdaNamespace.has(namespace)) throw new Error(`namespace "${namespace}" doesn't exist`);
|
|
187
|
+
const serverPhecda = phecdaNamespace.get(namespace);
|
|
188
|
+
if (nsOrModel && typeof nsOrModel !== "string") return serverPhecda.get(nsOrModel);
|
|
189
|
+
else return serverPhecda;
|
|
190
|
+
}
|
|
191
|
+
__name(useS, "useS");
|
|
161
192
|
function getMetaFromInstance(instance, tag, name) {
|
|
162
193
|
const propertyKeys = getMetaKey(instance).filter((item) => typeof item === "string");
|
|
163
194
|
const baseMeta = getMergedMeta(instance, void 0);
|
|
@@ -223,15 +254,13 @@ function getParamTypes(Model, key) {
|
|
|
223
254
|
return Reflect.getMetadata("design:paramtypes", Model, key);
|
|
224
255
|
}
|
|
225
256
|
__name(getParamTypes, "getParamTypes");
|
|
226
|
-
function isObject(o) {
|
|
227
|
-
return Object.prototype.toString.call(o) === "[object Object]";
|
|
228
|
-
}
|
|
229
|
-
__name(isObject, "isObject");
|
|
230
257
|
|
|
231
258
|
export {
|
|
232
259
|
Meta,
|
|
233
260
|
emitter,
|
|
234
|
-
|
|
261
|
+
defaultServerInject,
|
|
262
|
+
phecdaNamespace,
|
|
263
|
+
ServerPhecda,
|
|
235
264
|
Factory,
|
|
236
|
-
|
|
265
|
+
useS
|
|
237
266
|
};
|