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
@@ -1,14 +1,15 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class;
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class;
2
2
 
3
3
 
4
4
 
5
5
 
6
- var _chunkJ5CFUN4Vjs = require('./chunk-J5CFUN4V.js');
6
+
7
+ var _chunk4LLLQOMFjs = require('./chunk-4LLLQOMF.js');
7
8
 
8
9
  // src/meta.ts
9
10
  var Meta = class {
10
11
  static {
11
- _chunkJ5CFUN4Vjs.__name.call(void 0, this, "Meta");
12
+ _chunk4LLLQOMFjs.__name.call(void 0, this, "Meta");
12
13
  }
13
14
 
14
15
 
@@ -37,11 +38,11 @@ function defaultServerInject() {
37
38
  });
38
39
  }
39
40
  }
40
- _chunkJ5CFUN4Vjs.__name.call(void 0, defaultServerInject, "defaultServerInject");
41
+ _chunk4LLLQOMFjs.__name.call(void 0, defaultServerInject, "defaultServerInject");
41
42
  var phecdaNamespace = /* @__PURE__ */ new Map();
42
43
  var ServerPhecda = (_class = class {
43
44
  static {
44
- _chunkJ5CFUN4Vjs.__name.call(void 0, this, "ServerPhecda");
45
+ _chunk4LLLQOMFjs.__name.call(void 0, this, "ServerPhecda");
45
46
  }
46
47
  __init() {this.moduleMap = /* @__PURE__ */ new Map()}
47
48
  __init2() {this.meta = []}
@@ -51,9 +52,9 @@ var ServerPhecda = (_class = class {
51
52
 
52
53
 
53
54
 
54
- constructor(options) {;_class.prototype.__init.call(this);_class.prototype.__init2.call(this);_class.prototype.__init3.call(this);_class.prototype.__init4.call(this);_class.prototype.__init5.call(this);
55
+ constructor(options) {;_class.prototype.__init.call(this);_class.prototype.__init2.call(this);_class.prototype.__init3.call(this);_class.prototype.__init4.call(this);_class.prototype.__init5.call(this);_class.prototype.__init6.call(this);
55
56
  defaultServerInject();
56
- const { namespace = "default", parseModule = /* @__PURE__ */ _chunkJ5CFUN4Vjs.__name.call(void 0, (module) => module, "parseModule"), parseMeta = /* @__PURE__ */ _chunkJ5CFUN4Vjs.__name.call(void 0, (meta) => meta, "parseMeta"), generators = [] } = options;
57
+ const { namespace = "default", parseModule = /* @__PURE__ */ _chunk4LLLQOMFjs.__name.call(void 0, (module) => module, "parseModule"), parseMeta = /* @__PURE__ */ _chunk4LLLQOMFjs.__name.call(void 0, (meta) => meta, "parseMeta"), generators = [] } = options;
57
58
  phecdaNamespace.set(namespace, this);
58
59
  this.parseMeta = parseMeta;
59
60
  this.parseModule = parseModule;
@@ -61,63 +62,86 @@ var ServerPhecda = (_class = class {
61
62
  }
62
63
  async start(models) {
63
64
  for (const model of models) await this.buildDepModule(model);
64
- const generateCode = /* @__PURE__ */ _chunkJ5CFUN4Vjs.__name.call(void 0, async () => {
65
- if (_chunkJ5CFUN4Vjs.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 (_chunk4LLLQOMFjs.IS_ONLY_GENERATE) {
68
+ _chunk4LLLQOMFjs.log.call(void 0, "Only generate code");
69
+ process.exit(_chunk4LLLQOMFjs.PS_EXIT_CODE.EXIT);
70
70
  }
71
- }, "generateCode");
72
- generateCode().then(() => {
73
- if (_chunkJ5CFUN4Vjs.IS_ONLY_GENERATE) process.exit(4);
74
71
  });
75
- if (_chunkJ5CFUN4Vjs.IS_HMR) {
76
- if (!globalThis.__PS_HMR__) globalThis.__PS_HMR__ = [];
77
- _optionalChain([globalThis, 'access', _2 => _2.__PS_HMR__, 'optionalAccess', _3 => _3.push, 'call', _4 => _4(async (files) => {
78
- debug("reload files ");
79
- for (const file of files) {
80
- const models2 = await Promise.resolve().then(() => _interopRequireWildcard(require(file)));
81
- for (const i in models2) {
82
- if (_phecdacore.isPhecda.call(void 0, models2[i])) await this.add(models2[i]);
83
- }
84
- }
85
- generateCode();
86
- })]);
87
- }
88
72
  }
89
- async add(Model) {
90
- const tag = _phecdacore.getTag.call(void 0, 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 _phecdacore.invokeUnmount.call(void 0, 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
+ __init6() {this.generateCode = /* @__PURE__ */ _chunk4LLLQOMFjs.__name.call(void 0, 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
+ _chunk4LLLQOMFjs.HMR.call(void 0, 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,11 +149,17 @@ var ServerPhecda = (_class = class {
125
149
  const tag = _phecdacore.getTag.call(void 0, 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
+ _chunk4LLLQOMFjs.log.call(void 0, `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
- if (module instanceof Model) _chunkJ5CFUN4Vjs.log.call(void 0, `Module taged ${String(tag)} has been overridden`);
132
- else _chunkJ5CFUN4Vjs.log.call(void 0, `Synonym module: Module taged "${String(tag)}" has been loaded before, so phecda-server won't load Module "${Model.name}"`, "warn");
161
+ if (module instanceof Model) _chunk4LLLQOMFjs.log.call(void 0, `Module taged ${String(tag)} has been overridden`);
162
+ else _chunk4LLLQOMFjs.log.call(void 0, `Synonym module: Module taged "${String(tag)}" has been loaded before, so phecda-server won't load Module "${Model.name}"`, "warn");
133
163
  }
134
164
  return {
135
165
  module,
@@ -152,7 +182,7 @@ var ServerPhecda = (_class = class {
152
182
  }
153
183
  this.meta.push(...getMetaFromInstance(module, tag, Model.name).map(this.parseMeta).filter((item) => !!item));
154
184
  debug(`init module "${String(tag)}"`);
155
- if (!_chunkJ5CFUN4Vjs.IS_ONLY_GENERATE) await _phecdacore.invokeInit.call(void 0, module);
185
+ if (!_chunk4LLLQOMFjs.IS_ONLY_GENERATE) await _phecdacore.invokeInit.call(void 0, module);
156
186
  debug(`add module "${String(tag)}"`);
157
187
  this.moduleMap.set(tag, module);
158
188
  this.modelMap.set(module, Model);
@@ -161,6 +191,42 @@ var ServerPhecda = (_class = 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 = _phecdacore.getTag.call(void 0, model);
197
+ if (!this.has(tag)) return;
198
+ const module = this.moduleMap.get(tag);
199
+ debug(`unmount module "${String(tag)}"`);
200
+ await _phecdacore.invokeUnmount.call(void 0, 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 = _phecdacore.getTag.call(void 0, 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" ? _phecdacore.getTag.call(void 0, modelOrTag) : modelOrTag);
166
232
  }
@@ -175,7 +241,7 @@ async function Factory(models, opts = {}) {
175
241
  await phecda.start(models);
176
242
  return phecda;
177
243
  }
178
- _chunkJ5CFUN4Vjs.__name.call(void 0, Factory, "Factory");
244
+ _chunk4LLLQOMFjs.__name.call(void 0, Factory, "Factory");
179
245
  function useS(nsOrModel, namespace) {
180
246
  if (!nsOrModel) {
181
247
  namespace = "default";
@@ -188,7 +254,7 @@ function useS(nsOrModel, namespace) {
188
254
  if (nsOrModel && typeof nsOrModel !== "string") return serverPhecda.get(nsOrModel);
189
255
  else return serverPhecda;
190
256
  }
191
- _chunkJ5CFUN4Vjs.__name.call(void 0, useS, "useS");
257
+ _chunk4LLLQOMFjs.__name.call(void 0, useS, "useS");
192
258
  function getMetaFromInstance(instance, tag, name) {
193
259
  const propertyKeys = _phecdacore.getMetaKey.call(void 0, instance).filter((item) => typeof item === "string");
194
260
  const baseMeta = _phecdacore.getMergedMeta.call(void 0, instance, void 0);
@@ -202,7 +268,7 @@ function getMetaFromInstance(instance, tag, name) {
202
268
  func: i
203
269
  };
204
270
  if (baseMeta.controller) {
205
- if (typeof tag !== "string") _chunkJ5CFUN4Vjs.log.call(void 0, `can't use Tag with ${typeof tag} on controller "${instance.constructor.name}",instead with "${tag = String(tag)}"`, "error");
271
+ if (typeof tag !== "string") _chunk4LLLQOMFjs.log.call(void 0, `can't use Tag with ${typeof tag} on controller "${instance.constructor.name}",instead with "${tag = String(tag)}"`, "error");
206
272
  metaData.controller = baseMeta.controller;
207
273
  metaData[baseMeta.controller] = {
208
274
  ...baseMeta[baseMeta.controller],
@@ -240,7 +306,7 @@ function getMetaFromInstance(instance, tag, name) {
240
306
  return new Meta(deepFreeze(metaData), getParamTypes(instance, i) || []);
241
307
  });
242
308
  }
243
- _chunkJ5CFUN4Vjs.__name.call(void 0, getMetaFromInstance, "getMetaFromInstance");
309
+ _chunk4LLLQOMFjs.__name.call(void 0, getMetaFromInstance, "getMetaFromInstance");
244
310
  function deepFreeze(object) {
245
311
  Object.freeze(object);
246
312
  Object.getOwnPropertyNames(object).forEach((prop) => {
@@ -248,11 +314,13 @@ function deepFreeze(object) {
248
314
  });
249
315
  return object;
250
316
  }
251
- _chunkJ5CFUN4Vjs.__name.call(void 0, deepFreeze, "deepFreeze");
317
+ _chunk4LLLQOMFjs.__name.call(void 0, 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
- _chunkJ5CFUN4Vjs.__name.call(void 0, getParamTypes, "getParamTypes");
323
+ _chunk4LLLQOMFjs.__name.call(void 0, getParamTypes, "getParamTypes");
256
324
 
257
325
 
258
326