phecda-server 7.0.0-alpha.0 → 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 +24 -24
  2. package/assets/schema.json +55 -55
  3. package/bin/cli.mjs +31 -18
  4. package/dist/chunk-4LLLQOMF.js +152 -0
  5. package/dist/{chunk-BGXSMOLX.js → chunk-6OQ2SB5W.js} +138 -70
  6. package/dist/{chunk-SW5IKE5H.js → chunk-G7JFKM2M.js} +136 -89
  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-5622RBNB.mjs → chunk-YCES6ABN.mjs} +125 -57
  11. package/dist/{chunk-P75VKZJY.mjs → chunk-Z7NAAE4M.mjs} +94 -47
  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 +46 -40
  47. package/dist/index.d.ts +46 -40
  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 +384 -367
  84. package/register/utils.mjs +60 -56
  85. package/dist/chunk-J5CFUN4V.js +0 -73
  86. package/dist/chunk-WHJ5FALK.mjs +0 -73
@@ -1,6 +1,6 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
2
 
3
- var _chunkJ5CFUN4Vjs = require('./chunk-J5CFUN4V.js');
3
+ var _chunk4LLLQOMFjs = require('./chunk-4LLLQOMF.js');
4
4
 
5
5
  // src/decorators/param.ts
6
6
  var _phecdacore = require('phecda-core');
@@ -10,42 +10,42 @@ function BaseParam(data) {
10
10
  _phecdacore.setMeta.call(void 0, target, property, index, data);
11
11
  };
12
12
  }
13
- _chunkJ5CFUN4Vjs.__name.call(void 0, BaseParam, "BaseParam");
13
+ _chunk4LLLQOMFjs.__name.call(void 0, BaseParam, "BaseParam");
14
14
  function Body(key = "") {
15
15
  return BaseParam({
16
16
  type: "body",
17
17
  key
18
18
  });
19
19
  }
20
- _chunkJ5CFUN4Vjs.__name.call(void 0, Body, "Body");
20
+ _chunk4LLLQOMFjs.__name.call(void 0, Body, "Body");
21
21
  function Head(key) {
22
22
  return BaseParam({
23
23
  type: "headers",
24
24
  key: key.toLowerCase()
25
25
  });
26
26
  }
27
- _chunkJ5CFUN4Vjs.__name.call(void 0, Head, "Head");
27
+ _chunk4LLLQOMFjs.__name.call(void 0, Head, "Head");
28
28
  function Query(key = "") {
29
29
  return BaseParam({
30
30
  type: "query",
31
31
  key
32
32
  });
33
33
  }
34
- _chunkJ5CFUN4Vjs.__name.call(void 0, Query, "Query");
34
+ _chunk4LLLQOMFjs.__name.call(void 0, Query, "Query");
35
35
  function Param(key) {
36
36
  return BaseParam({
37
37
  type: "params",
38
38
  key
39
39
  });
40
40
  }
41
- _chunkJ5CFUN4Vjs.__name.call(void 0, Param, "Param");
41
+ _chunk4LLLQOMFjs.__name.call(void 0, Param, "Param");
42
42
  function Arg(target, k, index) {
43
43
  BaseParam({
44
44
  type: "args",
45
45
  key: `${index}`
46
46
  })(target, k, index);
47
47
  }
48
- _chunkJ5CFUN4Vjs.__name.call(void 0, Arg, "Arg");
48
+ _chunk4LLLQOMFjs.__name.call(void 0, Arg, "Arg");
49
49
 
50
50
  // src/decorators/aop.ts
51
51
 
@@ -56,7 +56,7 @@ function Guard(...guards) {
56
56
  });
57
57
  };
58
58
  }
59
- _chunkJ5CFUN4Vjs.__name.call(void 0, Guard, "Guard");
59
+ _chunk4LLLQOMFjs.__name.call(void 0, Guard, "Guard");
60
60
  function Addon(...addons) {
61
61
  return (target, property) => {
62
62
  _phecdacore.setMeta.call(void 0, target, property, void 0, {
@@ -64,7 +64,7 @@ function Addon(...addons) {
64
64
  });
65
65
  };
66
66
  }
67
- _chunkJ5CFUN4Vjs.__name.call(void 0, Addon, "Addon");
67
+ _chunk4LLLQOMFjs.__name.call(void 0, Addon, "Addon");
68
68
  function Filter(filter) {
69
69
  return (target, property) => {
70
70
  _phecdacore.setMeta.call(void 0, target, property, void 0, {
@@ -72,7 +72,7 @@ function Filter(filter) {
72
72
  });
73
73
  };
74
74
  }
75
- _chunkJ5CFUN4Vjs.__name.call(void 0, Filter, "Filter");
75
+ _chunk4LLLQOMFjs.__name.call(void 0, Filter, "Filter");
76
76
  function Pipe(pipe) {
77
77
  return (target, property, index) => {
78
78
  if (typeof index === "number") {
@@ -86,7 +86,7 @@ function Pipe(pipe) {
86
86
  });
87
87
  };
88
88
  }
89
- _chunkJ5CFUN4Vjs.__name.call(void 0, Pipe, "Pipe");
89
+ _chunk4LLLQOMFjs.__name.call(void 0, Pipe, "Pipe");
90
90
 
91
91
  // src/decorators/http.ts
92
92
 
@@ -100,7 +100,7 @@ function Route(route, type) {
100
100
  });
101
101
  };
102
102
  }
103
- _chunkJ5CFUN4Vjs.__name.call(void 0, Route, "Route");
103
+ _chunk4LLLQOMFjs.__name.call(void 0, Route, "Route");
104
104
  function Header(headers) {
105
105
  return (target, property) => {
106
106
  _phecdacore.setMeta.call(void 0, target, property, void 0, {
@@ -110,31 +110,31 @@ function Header(headers) {
110
110
  });
111
111
  };
112
112
  }
113
- _chunkJ5CFUN4Vjs.__name.call(void 0, Header, "Header");
113
+ _chunk4LLLQOMFjs.__name.call(void 0, Header, "Header");
114
114
  function Get(route = "") {
115
115
  return Route(route, "get");
116
116
  }
117
- _chunkJ5CFUN4Vjs.__name.call(void 0, Get, "Get");
117
+ _chunk4LLLQOMFjs.__name.call(void 0, Get, "Get");
118
118
  function Post(route = "") {
119
119
  return Route(route, "post");
120
120
  }
121
- _chunkJ5CFUN4Vjs.__name.call(void 0, Post, "Post");
121
+ _chunk4LLLQOMFjs.__name.call(void 0, Post, "Post");
122
122
  function Put(route = "") {
123
123
  return Route(route, "put");
124
124
  }
125
- _chunkJ5CFUN4Vjs.__name.call(void 0, Put, "Put");
125
+ _chunk4LLLQOMFjs.__name.call(void 0, Put, "Put");
126
126
  function Search(route = "") {
127
127
  return Route(route, "search");
128
128
  }
129
- _chunkJ5CFUN4Vjs.__name.call(void 0, Search, "Search");
129
+ _chunk4LLLQOMFjs.__name.call(void 0, Search, "Search");
130
130
  function Patch(route = "") {
131
131
  return Route(route, "patch");
132
132
  }
133
- _chunkJ5CFUN4Vjs.__name.call(void 0, Patch, "Patch");
133
+ _chunk4LLLQOMFjs.__name.call(void 0, Patch, "Patch");
134
134
  function Delete(route = "") {
135
135
  return Route(route, "delete");
136
136
  }
137
- _chunkJ5CFUN4Vjs.__name.call(void 0, Delete, "Delete");
137
+ _chunk4LLLQOMFjs.__name.call(void 0, Delete, "Delete");
138
138
  function Controller(prefix = "") {
139
139
  return (target) => {
140
140
  _phecdacore.setMeta.call(void 0, target, void 0, void 0, {
@@ -145,7 +145,7 @@ function Controller(prefix = "") {
145
145
  });
146
146
  };
147
147
  }
148
- _chunkJ5CFUN4Vjs.__name.call(void 0, Controller, "Controller");
148
+ _chunk4LLLQOMFjs.__name.call(void 0, Controller, "Controller");
149
149
 
150
150
  // src/decorators/rpc.ts
151
151
 
@@ -159,7 +159,7 @@ function Queue(queue = "", isEvent) {
159
159
  });
160
160
  };
161
161
  }
162
- _chunkJ5CFUN4Vjs.__name.call(void 0, Queue, "Queue");
162
+ _chunk4LLLQOMFjs.__name.call(void 0, Queue, "Queue");
163
163
  function Rpc() {
164
164
  return (target) => {
165
165
  _phecdacore.setMeta.call(void 0, target, void 0, void 0, {
@@ -167,11 +167,11 @@ function Rpc() {
167
167
  });
168
168
  };
169
169
  }
170
- _chunkJ5CFUN4Vjs.__name.call(void 0, Rpc, "Rpc");
170
+ _chunk4LLLQOMFjs.__name.call(void 0, Rpc, "Rpc");
171
171
 
172
172
  // src/decorators/ctx.ts
173
173
 
174
- var Ctx = /* @__PURE__ */ _chunkJ5CFUN4Vjs.__name.call(void 0, (target, property) => {
174
+ var Ctx = /* @__PURE__ */ _chunk4LLLQOMFjs.__name.call(void 0, (target, property) => {
175
175
  _phecdacore.setMeta.call(void 0, target, _phecdacore.SHARE_KEY, void 0, {
176
176
  ctxs: [
177
177
  property
@@ -200,7 +200,7 @@ function Define(key, value) {
200
200
  }
201
201
  };
202
202
  }
203
- _chunkJ5CFUN4Vjs.__name.call(void 0, Define, "Define");
203
+ _chunk4LLLQOMFjs.__name.call(void 0, Define, "Define");
204
204
 
205
205
 
206
206
 
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  __name
3
- } from "./chunk-WHJ5FALK.mjs";
3
+ } from "./chunk-NQ55PA2X.mjs";
4
4
 
5
5
  // src/decorators/param.ts
6
6
  import { setMeta } from "phecda-core";
@@ -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,9 +22,9 @@ 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
- var debug = Debug("phecda-server(createPhecda)");
27
+ var debug = Debug("phecda-server(Factory)");
27
28
  var emitter = new EventEmitter();
28
29
  function defaultServerInject() {
29
30
  if (!getInject("watcher")) {
@@ -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