phecda-server 7.0.0-alpha.1 → 7.0.0-alpha.10

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 (86) hide show
  1. package/assets/ps.json +1 -1
  2. package/assets/schema.json +55 -55
  3. package/bin/cli.mjs +31 -23
  4. package/dist/chunk-4LLLQOMF.js +152 -0
  5. package/dist/{chunk-7YQ57BQS.js → chunk-6OQ2SB5W.js} +137 -69
  6. package/dist/{chunk-GSPBHZBH.js → chunk-G7JFKM2M.js} +96 -80
  7. package/dist/{chunk-MBCHNDAY.js → chunk-KCPBITYZ.js} +24 -24
  8. package/dist/{chunk-HMVLXNV3.mjs → chunk-MUI6MTUO.mjs} +1 -1
  9. package/dist/chunk-NQ55PA2X.mjs +152 -0
  10. package/dist/{chunk-XYVMNY2X.mjs → chunk-YCES6ABN.mjs} +124 -56
  11. package/dist/{chunk-V7EI76QQ.mjs → chunk-Z7NAAE4M.mjs} +48 -32
  12. package/dist/{core-BIcUwV18.d.mts → core-CUTbX_IS.d.ts} +5 -3
  13. package/dist/{core-CYwEPfN4.d.ts → core-DIfgUKIU.d.mts} +5 -3
  14. package/dist/helper.d.mts +8 -6
  15. package/dist/helper.d.ts +8 -6
  16. package/dist/helper.js +8 -3
  17. package/dist/helper.mjs +10 -5
  18. package/dist/http/elysia/index.d.mts +4 -4
  19. package/dist/http/elysia/index.d.ts +4 -4
  20. package/dist/http/elysia/index.js +43 -44
  21. package/dist/http/elysia/index.mjs +9 -10
  22. package/dist/http/express/index.d.mts +4 -4
  23. package/dist/http/express/index.d.ts +4 -4
  24. package/dist/http/express/index.js +40 -41
  25. package/dist/http/express/index.mjs +9 -10
  26. package/dist/http/fastify/index.d.mts +4 -4
  27. package/dist/http/fastify/index.d.ts +4 -4
  28. package/dist/http/fastify/index.js +41 -42
  29. package/dist/http/fastify/index.mjs +9 -10
  30. package/dist/http/h3/index.d.mts +4 -4
  31. package/dist/http/h3/index.d.ts +4 -4
  32. package/dist/http/h3/index.js +41 -42
  33. package/dist/http/h3/index.mjs +9 -10
  34. package/dist/http/hono/index.d.mts +4 -4
  35. package/dist/http/hono/index.d.ts +4 -4
  36. package/dist/http/hono/index.js +37 -39
  37. package/dist/http/hono/index.mjs +5 -7
  38. package/dist/http/hyper-express/index.d.mts +4 -4
  39. package/dist/http/hyper-express/index.d.ts +4 -4
  40. package/dist/http/hyper-express/index.js +38 -39
  41. package/dist/http/hyper-express/index.mjs +8 -9
  42. package/dist/http/koa/index.d.mts +4 -4
  43. package/dist/http/koa/index.d.ts +4 -4
  44. package/dist/http/koa/index.js +40 -41
  45. package/dist/http/koa/index.mjs +9 -10
  46. package/dist/index.d.mts +44 -38
  47. package/dist/index.d.ts +44 -38
  48. package/dist/index.js +48 -42
  49. package/dist/index.mjs +21 -15
  50. package/dist/{meta-BXKLFTgG.d.mts → meta-CR-D39hQ.d.mts} +7 -5
  51. package/dist/{meta-BXKLFTgG.d.ts → meta-CR-D39hQ.d.ts} +7 -5
  52. package/dist/rpc/bullmq/index.d.mts +5 -5
  53. package/dist/rpc/bullmq/index.d.ts +5 -5
  54. package/dist/rpc/bullmq/index.js +15 -14
  55. package/dist/rpc/bullmq/index.mjs +4 -3
  56. package/dist/rpc/kafka/index.d.mts +4 -4
  57. package/dist/rpc/kafka/index.d.ts +4 -4
  58. package/dist/rpc/kafka/index.js +15 -14
  59. package/dist/rpc/kafka/index.mjs +3 -2
  60. package/dist/rpc/nats/index.d.mts +4 -4
  61. package/dist/rpc/nats/index.d.ts +4 -4
  62. package/dist/rpc/nats/index.js +14 -13
  63. package/dist/rpc/nats/index.mjs +4 -3
  64. package/dist/rpc/rabbitmq/index.d.mts +4 -4
  65. package/dist/rpc/rabbitmq/index.d.ts +4 -4
  66. package/dist/rpc/rabbitmq/index.js +16 -15
  67. package/dist/rpc/rabbitmq/index.mjs +4 -3
  68. package/dist/rpc/redis/index.d.mts +4 -4
  69. package/dist/rpc/redis/index.d.ts +4 -4
  70. package/dist/rpc/redis/index.js +14 -13
  71. package/dist/rpc/redis/index.mjs +4 -3
  72. package/dist/test.d.mts +3 -3
  73. package/dist/test.d.ts +3 -3
  74. package/dist/test.js +6 -6
  75. package/dist/test.mjs +2 -2
  76. package/dist/{types-h40T3cRG.d.mts → types-BF1TDbFV.d.ts} +4 -3
  77. package/dist/{types-BtbL49Zs.d.mts → types-DYRu0vic.d.ts} +4 -3
  78. package/dist/{types-VFzEM7LL.d.ts → types-DYviSl5B.d.mts} +4 -3
  79. package/dist/{types-m3IEDKjP.d.ts → types-DqH1qA-q.d.mts} +4 -3
  80. package/package.json +8 -5
  81. package/register/export.mjs +30 -0
  82. package/register/index.mjs +50 -40
  83. package/register/loader.mjs +84 -66
  84. package/register/utils.mjs +60 -56
  85. package/dist/chunk-J5CFUN4V.js +0 -73
  86. package/dist/chunk-WHJ5FALK.mjs +0 -73
@@ -0,0 +1,152 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // src/common.ts
5
+ var ERROR_SYMBOL = "__PS_ERROR__";
6
+ var IS_DEV = process.env.NODE_ENV === "development";
7
+ var IS_ONLY_GENERATE = !!process.env.PS_GENERATE;
8
+ var IS_STRICT = !!process.env.PS_STRICT;
9
+ var IS_PURE = !!process.env.PS_PURE;
10
+ var LOG_LEVEL = Number(process.env.PS_LOG_LEVEL || 0);
11
+ var PS_EXIT_CODE = /* @__PURE__ */ function(PS_EXIT_CODE2) {
12
+ PS_EXIT_CODE2[PS_EXIT_CODE2["RELAUNCH"] = 4171] = "RELAUNCH";
13
+ PS_EXIT_CODE2[PS_EXIT_CODE2["EXIT"] = 4172] = "EXIT";
14
+ return PS_EXIT_CODE2;
15
+ }({});
16
+
17
+ // src/utils.ts
18
+ import pc from "picocolors";
19
+ import { Mixin } from "ts-mixer";
20
+ var isObject = /* @__PURE__ */ __name((fn) => fn !== null && typeof fn === "object", "isObject");
21
+ var isPlainObject = /* @__PURE__ */ __name((fn) => {
22
+ if (!isObject(fn)) return false;
23
+ const proto = Object.getPrototypeOf(fn);
24
+ if (proto === null) return true;
25
+ const ctor = Object.prototype.hasOwnProperty.call(proto, "constructor") && proto.constructor;
26
+ return typeof ctor === "function" && ctor instanceof ctor && Function.prototype.toString.call(ctor) === Function.prototype.toString.call(Object);
27
+ }, "isPlainObject");
28
+ var InternalLogger = class InternalLogger2 {
29
+ static {
30
+ __name(this, "InternalLogger");
31
+ }
32
+ time;
33
+ color = {
34
+ debug: "bgMagenta",
35
+ error: "red",
36
+ info: "gray",
37
+ warn: "yellow",
38
+ log: "green"
39
+ };
40
+ dateFormatter = new Intl.DateTimeFormat(void 0, {
41
+ year: "numeric",
42
+ hour: "numeric",
43
+ minute: "numeric",
44
+ second: "numeric",
45
+ day: "2-digit",
46
+ month: "2-digit"
47
+ });
48
+ diffTimestamp() {
49
+ const now = Date.now();
50
+ if (!this.time) return "";
51
+ const diff = now - this.time;
52
+ this.time = now;
53
+ return diff ? pc.yellow(` +${diff}`) : "";
54
+ }
55
+ colorize(message, logLevel) {
56
+ return pc[this.color[logLevel]](message);
57
+ }
58
+ isAllowLog(level) {
59
+ const logLevel = {
60
+ debug: -1,
61
+ info: 0,
62
+ log: 1,
63
+ warn: 2,
64
+ error: 3
65
+ }[level];
66
+ if (logLevel < LOG_LEVEL) return false;
67
+ return true;
68
+ }
69
+ stringifyMessage(message, logLevel) {
70
+ if (typeof message === "function") {
71
+ const messageAsStr = Function.prototype.toString.call(message);
72
+ const isClass = messageAsStr.startsWith("class ");
73
+ if (isClass) {
74
+ return this.stringifyMessage(message.name, logLevel);
75
+ }
76
+ return this.stringifyMessage(message(), logLevel);
77
+ }
78
+ return isPlainObject(message) || Array.isArray(message) ? `${this.colorize("Object:", logLevel)}
79
+ ${JSON.stringify(message, (key, value) => typeof value === "bigint" ? value.toString() : value, 2)}
80
+ ` : this.colorize(message, logLevel);
81
+ }
82
+ log(msg, level, ctx) {
83
+ if (!this.isAllowLog(level)) return;
84
+ msg = this.stringifyMessage(msg, level);
85
+ const pidMsg = this.colorize(`[${process.env.PS_APP_NAME || "phecda-server"}] ${process.pid}`, level);
86
+ const ctxMsg = ctx ? this.colorize(pc.bold(`[${ctx}] `), level) : "";
87
+ const timeDiff = this.diffTimestamp();
88
+ const levelMsg = this.colorize(level.toUpperCase().padStart(7, " "), level);
89
+ process.stdout.write(`${pidMsg} ${this.dateFormatter.format(Date.now())} ${levelMsg} ${ctxMsg}${msg}${timeDiff}
90
+ `);
91
+ }
92
+ };
93
+ var _logger = new InternalLogger();
94
+ function setLogger(logger) {
95
+ _logger = logger;
96
+ }
97
+ __name(setLogger, "setLogger");
98
+ function getLogger() {
99
+ return _logger;
100
+ }
101
+ __name(getLogger, "getLogger");
102
+ function log(msg, level = "log", ctx) {
103
+ _logger.log(msg, level, ctx);
104
+ }
105
+ __name(log, "log");
106
+ function runMiddleware(ctx, middleware) {
107
+ return new Promise((resolve) => {
108
+ middleware(ctx.getRequest(), ctx.getResponse(), resolve);
109
+ });
110
+ }
111
+ __name(runMiddleware, "runMiddleware");
112
+
113
+ // src/hmr.ts
114
+ function HMR(cb) {
115
+ if (IS_DEV) {
116
+ if (!globalThis.__PS_HMR__) globalThis.__PS_HMR__ = [];
117
+ globalThis.__PS_HMR__.push(cb);
118
+ }
119
+ }
120
+ __name(HMR, "HMR");
121
+ async function RELOAD(oldModels, newModels) {
122
+ log("reload module...");
123
+ for (const cb of globalThis.__PS_HMR__) await cb(oldModels, newModels);
124
+ log("reload done");
125
+ }
126
+ __name(RELOAD, "RELOAD");
127
+ function RELAUNCH() {
128
+ if (IS_DEV) {
129
+ log("relaunch...");
130
+ process.exit(PS_EXIT_CODE.RELAUNCH);
131
+ }
132
+ }
133
+ __name(RELAUNCH, "RELAUNCH");
134
+
135
+ export {
136
+ __name,
137
+ ERROR_SYMBOL,
138
+ IS_DEV,
139
+ IS_ONLY_GENERATE,
140
+ IS_STRICT,
141
+ IS_PURE,
142
+ LOG_LEVEL,
143
+ PS_EXIT_CODE,
144
+ setLogger,
145
+ getLogger,
146
+ log,
147
+ runMiddleware,
148
+ Mixin,
149
+ HMR,
150
+ RELOAD,
151
+ RELAUNCH
152
+ };
@@ -1,9 +1,10 @@
1
1
  import {
2
- IS_HMR,
2
+ HMR,
3
3
  IS_ONLY_GENERATE,
4
+ PS_EXIT_CODE,
4
5
  __name,
5
6
  log
6
- } from "./chunk-WHJ5FALK.mjs";
7
+ } from "./chunk-NQ55PA2X.mjs";
7
8
 
8
9
  // src/meta.ts
9
10
  var Meta = class {
@@ -21,7 +22,7 @@ var Meta = class {
21
22
  // src/core.ts
22
23
  import "reflect-metadata";
23
24
  import EventEmitter from "node:events";
24
- import { getInject, getMergedMeta, getMetaKey, getMetaParams, getTag, invokeInit, invokeUnmount, isPhecda, setInject } from "phecda-core";
25
+ import { getInject, getMergedMeta, getMetaKey, getMetaParams, getTag, invokeInit, invokeUnmount, setInject } from "phecda-core";
25
26
  import Debug from "debug";
26
27
  var debug = Debug("phecda-server(Factory)");
27
28
  var emitter = new EventEmitter();
@@ -61,63 +62,86 @@ var ServerPhecda = class {
61
62
  }
62
63
  async start(models) {
63
64
  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
- }));
65
+ this.hmr();
66
+ this.generateCode().then(() => {
67
+ if (IS_ONLY_GENERATE) {
68
+ log("Only generate code");
69
+ process.exit(PS_EXIT_CODE.EXIT);
70
70
  }
71
- }, "generateCode");
72
- generateCode().then(() => {
73
- if (IS_ONLY_GENERATE) process.exit(4);
74
71
  });
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
72
  }
89
- async add(Model) {
90
- const tag = getTag(Model);
91
- const oldInstance = await this.del(tag);
92
- const { module: newModule } = await this.buildDepModule(Model);
93
- if (oldInstance && this.dependenceGraph.has(tag)) {
94
- debug(`replace module "${String(tag)}"`);
95
- [
96
- ...this.dependenceGraph.get(tag)
97
- ].forEach((tag2) => {
98
- const module = this.moduleMap.get(tag2);
99
- for (const key in module) {
100
- if (module[key] === oldInstance) module[key] = newModule;
101
- }
102
- });
103
- }
104
- }
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;
73
+ generateCode = /* @__PURE__ */ __name(async () => {
74
+ return Promise.all(this.generators.map((generator) => {
75
+ debug(`generate "${generator.name}" code to ${generator.path}`);
76
+ return generator.output(this.meta);
77
+ }));
78
+ }, "generateCode");
79
+ hmr() {
80
+ HMR(async (oldModels, newModels) => {
81
+ debug("reload models ");
82
+ await this.replace(oldModels, newModels);
83
+ this.generateCode();
84
+ });
117
85
  }
86
+ // async add(Model: Construct) {
87
+ // const tag = getTag(Model)
88
+ // const oldInstance = await this.del(tag)
89
+ // const { module: newModule } = await this.buildDepModule(Model)
90
+ // if (oldInstance && this.dependenceGraph.has(tag)) {
91
+ // debug(`replace module "${String(tag)}"`);
92
+ // [...this.dependenceGraph.get(tag)!].forEach((tag) => {
93
+ // const module = this.moduleMap.get(tag)
94
+ // for (const key in module) {
95
+ // if (module[key] === oldInstance)
96
+ // module[key] = newModule
97
+ // }
98
+ // })
99
+ // }
100
+ // }
101
+ // async del(modelOrTag: Construct | PropertyKey) {
102
+ // const tag = typeof modelOrTag === 'function' ? getTag(modelOrTag) : modelOrTag
103
+ // if (!this.moduleMap.has(tag))
104
+ // return
105
+ // const module = this.moduleMap.get(tag)
106
+ // debug(`unmount module "${String(tag)}"`)
107
+ // await invokeUnmount(module)
108
+ // debug(`del module "${String(tag)}"`)
109
+ // this.moduleMap.delete(tag)
110
+ // this.modelMap.delete(module)
111
+ // for (let i = this.meta.length - 1; i >= 0; i--) {
112
+ // if (this.meta[i].data.tag === tag)
113
+ // this.meta.splice(i, 1)
114
+ // }
115
+ // return module
116
+ // }
118
117
  async destroy() {
119
118
  debug("destroy all");
120
- for (const [tag] of this.moduleMap) await this.del(tag);
119
+ this.replace(Object.values(this.modelMap), []);
120
+ }
121
+ createProxyModule(tag) {
122
+ const that = this;
123
+ return new Proxy({}, {
124
+ get(_target, prop) {
125
+ const module = that.moduleMap.get(tag);
126
+ return Reflect.get(module, prop, module);
127
+ },
128
+ set(_target, prop, newValue) {
129
+ const module = that.moduleMap.get(tag);
130
+ return Reflect.set(module, prop, newValue, module);
131
+ },
132
+ has(_target, prop) {
133
+ return Reflect.has(that.moduleMap.get(tag), prop);
134
+ },
135
+ ownKeys() {
136
+ return Reflect.ownKeys(that.moduleMap.get(tag));
137
+ },
138
+ getPrototypeOf() {
139
+ return Reflect.getPrototypeOf(that.moduleMap.get(tag));
140
+ },
141
+ getOwnPropertyDescriptor(_target, prop) {
142
+ return Reflect.getOwnPropertyDescriptor(that.moduleMap.get(tag), prop);
143
+ }
144
+ });
121
145
  }
122
146
  async buildDepModule(Model) {
123
147
  const paramtypes = getParamTypes(Model);
@@ -125,7 +149,13 @@ var ServerPhecda = class {
125
149
  const tag = getTag(Model);
126
150
  if (this.moduleMap.has(tag)) {
127
151
  module = this.moduleMap.get(tag);
128
- if (!module) throw new Error(`exist Circular-Dependency or Multiple modules with the same name/tag [tag] ${String(tag)}--[module] ${Model}`);
152
+ if (!module) {
153
+ log(`Exist Circular-Dependency or Multiple modules with the same tag [${String(tag)}]`, "warn");
154
+ return {
155
+ module: this.createProxyModule(tag),
156
+ tag
157
+ };
158
+ }
129
159
  if (this.modelMap.get(module) !== Model && !this.modelSet.has(Model)) {
130
160
  this.modelSet.add(Model);
131
161
  if (module instanceof Model) log(`Module taged ${String(tag)} has been overridden`);
@@ -161,6 +191,42 @@ var ServerPhecda = class {
161
191
  tag
162
192
  };
163
193
  }
194
+ async replace(oldModels, newModels) {
195
+ const oldModules = (await Promise.all(oldModels.map(async (model) => {
196
+ const tag = getTag(model);
197
+ if (!this.has(tag)) return;
198
+ const module = this.moduleMap.get(tag);
199
+ debug(`unmount module "${String(tag)}"`);
200
+ await invokeUnmount(module);
201
+ debug(`del module "${String(tag)}"`);
202
+ this.moduleMap.delete(tag);
203
+ this.modelMap.delete(module);
204
+ for (let i = this.meta.length - 1; i >= 0; i--) {
205
+ if (this.meta[i].data.tag === tag) this.meta.splice(i, 1);
206
+ }
207
+ return module;
208
+ }))).filter((item) => item);
209
+ for (const model of newModels) {
210
+ debug(`mount module: ${model.name}`);
211
+ await this.buildDepModule(model);
212
+ }
213
+ debug("replace old modules");
214
+ for (const module of oldModules) {
215
+ const tag = getTag(module);
216
+ if (this.dependenceGraph.has(tag)) {
217
+ [
218
+ ...this.dependenceGraph.get(tag)
219
+ ].forEach((depTag) => {
220
+ const depModule = this.moduleMap.get(depTag);
221
+ if (depModule) {
222
+ for (const key in depModule) {
223
+ if (depModule[key] === module) depModule[key] = this.moduleMap.get(tag);
224
+ }
225
+ }
226
+ });
227
+ }
228
+ }
229
+ }
164
230
  has(modelOrTag) {
165
231
  return this.moduleMap.has(typeof modelOrTag === "function" ? getTag(modelOrTag) : modelOrTag);
166
232
  }
@@ -250,7 +316,9 @@ function deepFreeze(object) {
250
316
  }
251
317
  __name(deepFreeze, "deepFreeze");
252
318
  function getParamTypes(Model, key) {
253
- return Reflect.getMetadata("design:paramtypes", Model, key);
319
+ const paramTypes = Reflect.getMetadata("design:paramtypes", Model, key);
320
+ if (typeof paramTypes === "function") return paramTypes();
321
+ else return paramTypes;
254
322
  }
255
323
  __name(getParamTypes, "getParamTypes");
256
324
 
@@ -1,11 +1,13 @@
1
1
  import {
2
2
  ERROR_SYMBOL,
3
- IS_HMR,
3
+ HMR,
4
+ IS_DEV,
5
+ IS_PURE,
4
6
  IS_STRICT,
5
7
  LOG_LEVEL,
6
8
  __name,
7
9
  log
8
- } from "./chunk-WHJ5FALK.mjs";
10
+ } from "./chunk-NQ55PA2X.mjs";
9
11
 
10
12
  // src/helper.ts
11
13
  import pc2 from "picocolors";
@@ -230,10 +232,25 @@ var Context = class _Context {
230
232
  };
231
233
  static guardRecord = {};
232
234
  static addonRecord = {};
235
+ ctx;
236
+ canGetCtx;
233
237
  constructor(data) {
234
238
  this.data = data;
235
- if (IS_HMR)
239
+ this.canGetCtx = true;
240
+ if (IS_DEV)
236
241
  data._context = this;
242
+ const that = this;
243
+ this.ctx = new Proxy(data, {
244
+ get(target, p) {
245
+ if (IS_DEV && !that.canGetCtx) throw new FrameworkException("ctx must be obtained within the same request cycle in controller");
246
+ if (!(p in target)) log(`attribute "${p}" does not exist on ctx, which might be due to a missing AOP role (such as a guard).`, "warn", data.tag);
247
+ return target[p];
248
+ },
249
+ set(target, p, newValue) {
250
+ target[p] = newValue;
251
+ return true;
252
+ }
253
+ });
237
254
  }
238
255
  static getAop(meta, opts) {
239
256
  const { globalGuards = [], globalFilter = "default", globalPipe = "default" } = opts;
@@ -269,14 +286,20 @@ ${pc.red(`Filter ${filter2}[${filter2 || "default"}]`)}`);
269
286
  if (index === guards.length) {
270
287
  const instance = moduleMap.get(tag);
271
288
  if (ctxs) {
272
- ctxs.forEach((ctx) => instance[ctx] = this.data);
289
+ ctxs.forEach((ctx) => instance[ctx] = this.ctx);
273
290
  }
274
291
  const args = await Promise.all(params.map((item, i) => pipe[i]({
275
292
  arg: resolveDep(this.data[item.type], item.key),
276
293
  reflect: paramsType[item.index],
277
294
  ...item
278
- }, this.data)));
295
+ }, this.ctx)));
296
+ if (IS_DEV) {
297
+ Promise.resolve().then(() => {
298
+ this.canGetCtx = false;
299
+ });
300
+ }
279
301
  res = await instance[func](...args);
302
+ this.canGetCtx = true;
280
303
  } else {
281
304
  let nextPromise;
282
305
  async function next() {
@@ -289,7 +312,7 @@ ${pc.red(`Filter ${filter2}[${filter2 || "default"}]`)}`);
289
312
  });
290
313
  }
291
314
  __name(next, "next");
292
- const ret = await guards[index](this.data, next);
315
+ const ret = await guards[index](this.ctx, next);
293
316
  if (ret !== void 0) {
294
317
  res = ret;
295
318
  } else {
@@ -302,7 +325,7 @@ ${pc.red(`Filter ${filter2}[${filter2 || "default"}]`)}`);
302
325
  await nextHandler(0)();
303
326
  return successCb(res);
304
327
  } catch (e) {
305
- const err = await filter(e, this.data);
328
+ const err = await filter(e, this.ctx);
306
329
  return failCb(err);
307
330
  }
308
331
  }
@@ -356,32 +379,12 @@ function addAddon(key, addon, priority = 0) {
356
379
  }
357
380
  __name(addAddon, "addAddon");
358
381
 
359
- // src/hmr.ts
360
- function HMR(cb) {
361
- if (IS_HMR) globalThis.__PS_HMR__?.push(cb);
362
- }
363
- __name(HMR, "HMR");
364
-
365
382
  // src/http/helper.ts
366
383
  function resolveDep(ret, key) {
367
384
  if (key) return ret?.[key];
368
385
  return ret;
369
386
  }
370
387
  __name(resolveDep, "resolveDep");
371
- function argToReq(params, args, headers) {
372
- const req = {
373
- body: {},
374
- query: {},
375
- params: {},
376
- headers
377
- };
378
- params.forEach((param) => {
379
- if (param.key) req[param.type][param.key] = args[param.index];
380
- else req[param.type] = args[param.index];
381
- });
382
- return req;
383
- }
384
- __name(argToReq, "argToReq");
385
388
 
386
389
  // src/rpc/helper.ts
387
390
  import { hostname } from "os";
@@ -423,6 +426,7 @@ function createControllerMetaMap(meta, filter) {
423
426
  }
424
427
  __name(createControllerMetaMap, "createControllerMetaMap");
425
428
  function detectAopDep(meta, { guards, addons } = {}, controller = "http") {
429
+ if (IS_PURE) return;
426
430
  const addonSet = /* @__PURE__ */ new Set();
427
431
  const guardSet = /* @__PURE__ */ new Set();
428
432
  const pipeSet = /* @__PURE__ */ new Set();
@@ -441,8 +445,10 @@ function detectAopDep(meta, { guards, addons } = {}, controller = "http") {
441
445
  guardSet.add(item);
442
446
  });
443
447
  meta.forEach(({ data }) => {
448
+ if (!data.controller) return;
449
+ if (typeof data.tag !== "string") warningSet.add(`Tag of controller "${data.name}" should be a string`);
444
450
  if (data.controller !== controller) {
445
- if (data[controller]) warningSet.add(`Should use ${controller} controller to decorate class "${data.name}" or method "${data.func}"`);
451
+ if (data[controller]) warningSet.add(`Should use ${controller} controller to decorate class "${data.name}"`);
446
452
  return;
447
453
  }
448
454
  if (data.filter) filterSet.add(data.filter);
@@ -465,7 +471,10 @@ function detectAopDep(meta, { guards, addons } = {}, controller = "http") {
465
471
  ...filterSet
466
472
  ].filter((i) => !Context.filterRecord[i]);
467
473
  function exit() {
468
- if (IS_STRICT) process.exit(5);
474
+ if (IS_STRICT) {
475
+ log("Does not meet strict mode requirements", "error");
476
+ process.exit(1);
477
+ }
469
478
  }
470
479
  __name(exit, "exit");
471
480
  if (missAddons.length) {
@@ -498,6 +507,14 @@ function detectAopDep(meta, { guards, addons } = {}, controller = "http") {
498
507
  };
499
508
  }
500
509
  __name(detectAopDep, "detectAopDep");
510
+ function joinUrl(base, ...paths) {
511
+ const joinedPath = [
512
+ base,
513
+ ...paths
514
+ ].filter((p) => p).map((path) => path.replace(/(^\/)/g, "")).join("/");
515
+ return `/${joinedPath}`;
516
+ }
517
+ __name(joinUrl, "joinUrl");
501
518
 
502
519
  export {
503
520
  defaultPipe,
@@ -523,12 +540,11 @@ export {
523
540
  addFilter,
524
541
  addGuard,
525
542
  addAddon,
526
- HMR,
527
543
  resolveDep,
528
- argToReq,
529
544
  genClientQueue,
530
545
  shallowClone,
531
546
  mergeObject,
532
547
  createControllerMetaMap,
533
- detectAopDep
548
+ detectAopDep,
549
+ joinUrl
534
550
  };
@@ -1,5 +1,5 @@
1
1
  import { Construct } from 'phecda-core';
2
- import { b as MetaData, M as Meta, E as Emitter } from './meta-BXKLFTgG.mjs';
2
+ import { b as MetaData, M as Meta, E as Emitter } from './meta-CR-D39hQ.js';
3
3
 
4
4
  declare abstract class Generator {
5
5
  private _path;
@@ -30,13 +30,15 @@ declare class ServerPhecda {
30
30
  generators: Generator[];
31
31
  constructor(options: Options);
32
32
  start(models: Construct[]): Promise<void>;
33
- add(Model: Construct): Promise<void>;
34
- del(tag: PropertyKey): Promise<any>;
33
+ generateCode: () => Promise<void[]>;
34
+ hmr(): void;
35
35
  destroy(): Promise<void>;
36
+ createProxyModule(tag: PropertyKey): {};
36
37
  protected buildDepModule(Model: Construct): Promise<{
37
38
  module: any;
38
39
  tag: PropertyKey;
39
40
  }>;
41
+ replace(oldModels: Construct[], newModels: Construct[]): Promise<void>;
40
42
  has(modelOrTag: Construct | PropertyKey): boolean;
41
43
  get<Model extends Construct>(modelOrTag: Model | PropertyKey): InstanceType<Model>;
42
44
  }
@@ -1,5 +1,5 @@
1
1
  import { Construct } from 'phecda-core';
2
- import { b as MetaData, M as Meta, E as Emitter } from './meta-BXKLFTgG.js';
2
+ import { b as MetaData, M as Meta, E as Emitter } from './meta-CR-D39hQ.mjs';
3
3
 
4
4
  declare abstract class Generator {
5
5
  private _path;
@@ -30,13 +30,15 @@ declare class ServerPhecda {
30
30
  generators: Generator[];
31
31
  constructor(options: Options);
32
32
  start(models: Construct[]): Promise<void>;
33
- add(Model: Construct): Promise<void>;
34
- del(tag: PropertyKey): Promise<any>;
33
+ generateCode: () => Promise<void[]>;
34
+ hmr(): void;
35
35
  destroy(): Promise<void>;
36
+ createProxyModule(tag: PropertyKey): {};
36
37
  protected buildDepModule(Model: Construct): Promise<{
37
38
  module: any;
38
39
  tag: PropertyKey;
39
40
  }>;
41
+ replace(oldModels: Construct[], newModels: Construct[]): Promise<void>;
40
42
  has(modelOrTag: Construct | PropertyKey): boolean;
41
43
  get<Model extends Construct>(modelOrTag: Model | PropertyKey): InstanceType<Model>;
42
44
  }
package/dist/helper.d.mts CHANGED
@@ -1,10 +1,11 @@
1
- import { C as ControllerMetaData, M as Meta, a as ControllerMeta } from './meta-BXKLFTgG.mjs';
2
- import 'phecda-core';
1
+ import { M as Meta, C as ControllerMeta } from './meta-CR-D39hQ.mjs';
2
+ import { Construct } from 'phecda-core';
3
3
 
4
- declare function HMR(cb: (...args: any) => any): void;
4
+ declare function HMR(cb: (oldModels: Construct[], newModels: Construct[]) => any): void;
5
+ declare function RELOAD(oldModels: Construct[], newModels: Construct[]): Promise<void>;
6
+ declare function RELAUNCH(): void;
5
7
 
6
8
  declare function resolveDep(ret: any, key: string): any;
7
- declare function argToReq(params: ControllerMetaData['params'], args: any[], headers: Record<string, any>): any;
8
9
 
9
10
  declare function genClientQueue(key?: string): string;
10
11
 
@@ -20,6 +21,7 @@ declare function detectAopDep(meta: Meta[], { guards, addons }?: {
20
21
  guardSet: Set<string>;
21
22
  pipeSet: Set<string>;
22
23
  filterSet: Set<string>;
23
- };
24
+ } | undefined;
25
+ declare function joinUrl(base: string, ...paths: string[]): string;
24
26
 
25
- export { HMR, argToReq, createControllerMetaMap, detectAopDep, genClientQueue, mergeObject, resolveDep, shallowClone };
27
+ export { HMR, RELAUNCH, RELOAD, createControllerMetaMap, detectAopDep, genClientQueue, joinUrl, mergeObject, resolveDep, shallowClone };
package/dist/helper.d.ts CHANGED
@@ -1,10 +1,11 @@
1
- import { C as ControllerMetaData, M as Meta, a as ControllerMeta } from './meta-BXKLFTgG.js';
2
- import 'phecda-core';
1
+ import { M as Meta, C as ControllerMeta } from './meta-CR-D39hQ.js';
2
+ import { Construct } from 'phecda-core';
3
3
 
4
- declare function HMR(cb: (...args: any) => any): void;
4
+ declare function HMR(cb: (oldModels: Construct[], newModels: Construct[]) => any): void;
5
+ declare function RELOAD(oldModels: Construct[], newModels: Construct[]): Promise<void>;
6
+ declare function RELAUNCH(): void;
5
7
 
6
8
  declare function resolveDep(ret: any, key: string): any;
7
- declare function argToReq(params: ControllerMetaData['params'], args: any[], headers: Record<string, any>): any;
8
9
 
9
10
  declare function genClientQueue(key?: string): string;
10
11
 
@@ -20,6 +21,7 @@ declare function detectAopDep(meta: Meta[], { guards, addons }?: {
20
21
  guardSet: Set<string>;
21
22
  pipeSet: Set<string>;
22
23
  filterSet: Set<string>;
23
- };
24
+ } | undefined;
25
+ declare function joinUrl(base: string, ...paths: string[]): string;
24
26
 
25
- export { HMR, argToReq, createControllerMetaMap, detectAopDep, genClientQueue, mergeObject, resolveDep, shallowClone };
27
+ export { HMR, RELAUNCH, RELOAD, createControllerMetaMap, detectAopDep, genClientQueue, joinUrl, mergeObject, resolveDep, shallowClone };
package/dist/helper.js CHANGED
@@ -6,16 +6,21 @@
6
6
 
7
7
 
8
8
 
9
+ var _chunkG7JFKM2Mjs = require('./chunk-G7JFKM2M.js');
9
10
 
10
- var _chunkGSPBHZBHjs = require('./chunk-GSPBHZBH.js');
11
- require('./chunk-J5CFUN4V.js');
12
11
 
13
12
 
14
13
 
14
+ var _chunk4LLLQOMFjs = require('./chunk-4LLLQOMF.js');
15
15
 
16
16
 
17
17
 
18
18
 
19
19
 
20
20
 
21
- exports.HMR = _chunkGSPBHZBHjs.HMR; exports.argToReq = _chunkGSPBHZBHjs.argToReq; exports.createControllerMetaMap = _chunkGSPBHZBHjs.createControllerMetaMap; exports.detectAopDep = _chunkGSPBHZBHjs.detectAopDep; exports.genClientQueue = _chunkGSPBHZBHjs.genClientQueue; exports.mergeObject = _chunkGSPBHZBHjs.mergeObject; exports.resolveDep = _chunkGSPBHZBHjs.resolveDep; exports.shallowClone = _chunkGSPBHZBHjs.shallowClone;
21
+
22
+
23
+
24
+
25
+
26
+ exports.HMR = _chunk4LLLQOMFjs.HMR; exports.RELAUNCH = _chunk4LLLQOMFjs.RELAUNCH; exports.RELOAD = _chunk4LLLQOMFjs.RELOAD; exports.createControllerMetaMap = _chunkG7JFKM2Mjs.createControllerMetaMap; exports.detectAopDep = _chunkG7JFKM2Mjs.detectAopDep; exports.genClientQueue = _chunkG7JFKM2Mjs.genClientQueue; exports.joinUrl = _chunkG7JFKM2Mjs.joinUrl; exports.mergeObject = _chunkG7JFKM2Mjs.mergeObject; exports.resolveDep = _chunkG7JFKM2Mjs.resolveDep; exports.shallowClone = _chunkG7JFKM2Mjs.shallowClone;