phecda-server 5.0.0-beta.19 → 5.0.0-beta.21

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 (36) hide show
  1. package/dist/{chunk-WNTTAFLC.js → chunk-562XCEUU.js} +9 -11
  2. package/dist/{chunk-PXI5J4LR.mjs → chunk-AL36AJMH.mjs} +8 -10
  3. package/dist/{chunk-VBYDVWRK.js → chunk-FMPVMJLE.js} +38 -32
  4. package/dist/{chunk-LSMNOIFA.mjs → chunk-TCW7B6MG.mjs} +39 -33
  5. package/dist/core-895ed04d.d.ts +112 -0
  6. package/dist/index.d.ts +39 -15
  7. package/dist/index.js +27 -24
  8. package/dist/index.mjs +8 -5
  9. package/dist/rpc/kafka/index.d.ts +2 -2
  10. package/dist/rpc/kafka/index.js +9 -6
  11. package/dist/rpc/kafka/index.mjs +6 -3
  12. package/dist/rpc/rabbitmq/index.d.ts +2 -2
  13. package/dist/rpc/rabbitmq/index.js +9 -6
  14. package/dist/rpc/rabbitmq/index.mjs +6 -3
  15. package/dist/rpc/redis/index.d.ts +2 -2
  16. package/dist/rpc/redis/index.js +9 -6
  17. package/dist/rpc/redis/index.mjs +6 -3
  18. package/dist/server/express/index.d.ts +1 -1
  19. package/dist/server/express/index.js +18 -14
  20. package/dist/server/express/index.mjs +9 -5
  21. package/dist/server/fastify/index.d.ts +1 -1
  22. package/dist/server/fastify/index.js +23 -16
  23. package/dist/server/fastify/index.mjs +13 -6
  24. package/dist/server/h3/index.d.ts +1 -1
  25. package/dist/server/h3/index.js +18 -14
  26. package/dist/server/h3/index.mjs +9 -5
  27. package/dist/server/koa/index.d.ts +1 -1
  28. package/dist/server/koa/index.js +18 -14
  29. package/dist/server/koa/index.mjs +9 -5
  30. package/dist/test.d.ts +1 -1
  31. package/dist/test.js +2 -2
  32. package/dist/test.mjs +1 -1
  33. package/package.json +3 -2
  34. package/register/loader.mjs +3 -5
  35. package/register/utils.mjs +46 -5
  36. package/dist/core-eb2a0799.d.ts +0 -131
@@ -197,7 +197,6 @@ var Histroy = (_class = class {constructor() { _class.prototype.__init.call(this
197
197
  _chunkXHKBG2VAjs.__name.call(void 0, Histroy, "Histroy");
198
198
 
199
199
  // src/context.ts
200
- var guardRecord = {};
201
200
  var _Context = class {
202
201
 
203
202
 
@@ -300,25 +299,25 @@ function addPlugin(key, handler) {
300
299
  _chunkXHKBG2VAjs.__name.call(void 0, addPlugin, "addPlugin");
301
300
  function addPipe(key, handler) {
302
301
  if (Context.pipeRecord[key] && Context.pipeRecord[key] !== handler)
303
- _chunkXHKBG2VAjs.log.call(void 0, `overwrite Pipe "${String(key)}"`, "warn");
302
+ _chunkXHKBG2VAjs.log.call(void 0, `overwrite PipeType "${String(key)}"`, "warn");
304
303
  Context.pipeRecord[key] = handler;
305
304
  }
306
305
  _chunkXHKBG2VAjs.__name.call(void 0, addPipe, "addPipe");
307
306
  function addFilter(key, handler) {
308
307
  if (Context.filterRecord[key] && Context.filterRecord[key] !== handler)
309
- _chunkXHKBG2VAjs.log.call(void 0, `overwrite Filter "${String(key)}"`, "warn");
308
+ _chunkXHKBG2VAjs.log.call(void 0, `overwrite FilterType "${String(key)}"`, "warn");
310
309
  Context.filterRecord[key] = handler;
311
310
  }
312
311
  _chunkXHKBG2VAjs.__name.call(void 0, addFilter, "addFilter");
313
312
  function addGuard(key, handler) {
314
313
  if (Context.guardRecord[key] && Context.guardRecord[key] !== handler)
315
- _chunkXHKBG2VAjs.log.call(void 0, `overwrite Guard "${String(key)}"`, "warn");
314
+ _chunkXHKBG2VAjs.log.call(void 0, `overwrite GuardType "${String(key)}"`, "warn");
316
315
  Context.guardRecord[key] = handler;
317
316
  }
318
317
  _chunkXHKBG2VAjs.__name.call(void 0, addGuard, "addGuard");
319
318
  function addInterceptor(key, handler) {
320
319
  if (Context.interceptorRecord[key] && Context.interceptorRecord[key] !== handler)
321
- _chunkXHKBG2VAjs.log.call(void 0, `overwrite Interceptor "${String(key)}"`, "warn");
320
+ _chunkXHKBG2VAjs.log.call(void 0, `overwrite InterceptorType "${String(key)}"`, "warn");
322
321
  Context.interceptorRecord[key] = handler;
323
322
  }
324
323
  _chunkXHKBG2VAjs.__name.call(void 0, addInterceptor, "addInterceptor");
@@ -357,13 +356,13 @@ function isAopDepInject(meta, { guards, interceptors, plugins } = {}) {
357
356
  if (missPlugins.length)
358
357
  _chunkXHKBG2VAjs.log.call(void 0, `${_picocolors2.default.white(`Plugin [${missPlugins.join(",")}]`)} doesn't exist`, "warn");
359
358
  if (missGuards.length)
360
- _chunkXHKBG2VAjs.log.call(void 0, `${_picocolors2.default.magenta(`Guard [${missGuards.join(",")}]`)} doesn't exist`, "warn");
359
+ _chunkXHKBG2VAjs.log.call(void 0, `${_picocolors2.default.magenta(`GuardType [${missGuards.join(",")}]`)} doesn't exist`, "warn");
361
360
  if (missInterceptors.length)
362
- _chunkXHKBG2VAjs.log.call(void 0, `${_picocolors2.default.cyan(`Interceptor [${missInterceptors.join(",")}]`)} doesn't exist`, "warn");
361
+ _chunkXHKBG2VAjs.log.call(void 0, `${_picocolors2.default.cyan(`InterceptorType [${missInterceptors.join(",")}]`)} doesn't exist`, "warn");
363
362
  if (missPipes.length)
364
- _chunkXHKBG2VAjs.log.call(void 0, `${_picocolors2.default.blue(`Pipe [${missPipes.join(",")}]`)} doesn't exist`, "warn");
363
+ _chunkXHKBG2VAjs.log.call(void 0, `${_picocolors2.default.blue(`PipeType [${missPipes.join(",")}]`)} doesn't exist`, "warn");
365
364
  if (missFilters.length)
366
- _chunkXHKBG2VAjs.log.call(void 0, `${_picocolors2.default.red(`Filter [${missFilters.join(",")}]`)} doesn't exist`, "warn");
365
+ _chunkXHKBG2VAjs.log.call(void 0, `${_picocolors2.default.red(`FilterType [${missFilters.join(",")}]`)} doesn't exist`, "warn");
367
366
  }
368
367
  _chunkXHKBG2VAjs.__name.call(void 0, isAopDepInject, "isAopDepInject");
369
368
 
@@ -391,5 +390,4 @@ _chunkXHKBG2VAjs.__name.call(void 0, isAopDepInject, "isAopDepInject");
391
390
 
392
391
 
393
392
 
394
-
395
- exports.Exception = Exception; exports.ValidateException = ValidateException; exports.defaultPipe = defaultPipe; exports.UndefinedException = UndefinedException; exports.ForbiddenException = ForbiddenException; exports.BadRequestException = BadRequestException; exports.NotFoundException = NotFoundException; exports.ConflictException = ConflictException; exports.BadGatewayException = BadGatewayException; exports.InvalidInputException = InvalidInputException; exports.UnsupportedMediaTypeException = UnsupportedMediaTypeException; exports.PayloadLargeException = PayloadLargeException; exports.TimeoutException = TimeoutException; exports.UnauthorizedException = UnauthorizedException; exports.ServiceUnavailableException = ServiceUnavailableException; exports.FrameworkException = FrameworkException; exports.guardRecord = guardRecord; exports.Context = Context; exports.addPlugin = addPlugin; exports.addPipe = addPipe; exports.addFilter = addFilter; exports.addGuard = addGuard; exports.addInterceptor = addInterceptor; exports.isAopDepInject = isAopDepInject;
393
+ exports.Exception = Exception; exports.ValidateException = ValidateException; exports.defaultPipe = defaultPipe; exports.UndefinedException = UndefinedException; exports.ForbiddenException = ForbiddenException; exports.BadRequestException = BadRequestException; exports.NotFoundException = NotFoundException; exports.ConflictException = ConflictException; exports.BadGatewayException = BadGatewayException; exports.InvalidInputException = InvalidInputException; exports.UnsupportedMediaTypeException = UnsupportedMediaTypeException; exports.PayloadLargeException = PayloadLargeException; exports.TimeoutException = TimeoutException; exports.UnauthorizedException = UnauthorizedException; exports.ServiceUnavailableException = ServiceUnavailableException; exports.FrameworkException = FrameworkException; exports.Context = Context; exports.addPlugin = addPlugin; exports.addPipe = addPipe; exports.addFilter = addFilter; exports.addGuard = addGuard; exports.addInterceptor = addInterceptor; exports.isAopDepInject = isAopDepInject;
@@ -197,7 +197,6 @@ var Histroy = class {
197
197
  __name(Histroy, "Histroy");
198
198
 
199
199
  // src/context.ts
200
- var guardRecord = {};
201
200
  var _Context = class {
202
201
  data;
203
202
  method;
@@ -300,25 +299,25 @@ function addPlugin(key, handler) {
300
299
  __name(addPlugin, "addPlugin");
301
300
  function addPipe(key, handler) {
302
301
  if (Context.pipeRecord[key] && Context.pipeRecord[key] !== handler)
303
- log(`overwrite Pipe "${String(key)}"`, "warn");
302
+ log(`overwrite PipeType "${String(key)}"`, "warn");
304
303
  Context.pipeRecord[key] = handler;
305
304
  }
306
305
  __name(addPipe, "addPipe");
307
306
  function addFilter(key, handler) {
308
307
  if (Context.filterRecord[key] && Context.filterRecord[key] !== handler)
309
- log(`overwrite Filter "${String(key)}"`, "warn");
308
+ log(`overwrite FilterType "${String(key)}"`, "warn");
310
309
  Context.filterRecord[key] = handler;
311
310
  }
312
311
  __name(addFilter, "addFilter");
313
312
  function addGuard(key, handler) {
314
313
  if (Context.guardRecord[key] && Context.guardRecord[key] !== handler)
315
- log(`overwrite Guard "${String(key)}"`, "warn");
314
+ log(`overwrite GuardType "${String(key)}"`, "warn");
316
315
  Context.guardRecord[key] = handler;
317
316
  }
318
317
  __name(addGuard, "addGuard");
319
318
  function addInterceptor(key, handler) {
320
319
  if (Context.interceptorRecord[key] && Context.interceptorRecord[key] !== handler)
321
- log(`overwrite Interceptor "${String(key)}"`, "warn");
320
+ log(`overwrite InterceptorType "${String(key)}"`, "warn");
322
321
  Context.interceptorRecord[key] = handler;
323
322
  }
324
323
  __name(addInterceptor, "addInterceptor");
@@ -357,13 +356,13 @@ function isAopDepInject(meta, { guards, interceptors, plugins } = {}) {
357
356
  if (missPlugins.length)
358
357
  log(`${pc.white(`Plugin [${missPlugins.join(",")}]`)} doesn't exist`, "warn");
359
358
  if (missGuards.length)
360
- log(`${pc.magenta(`Guard [${missGuards.join(",")}]`)} doesn't exist`, "warn");
359
+ log(`${pc.magenta(`GuardType [${missGuards.join(",")}]`)} doesn't exist`, "warn");
361
360
  if (missInterceptors.length)
362
- log(`${pc.cyan(`Interceptor [${missInterceptors.join(",")}]`)} doesn't exist`, "warn");
361
+ log(`${pc.cyan(`InterceptorType [${missInterceptors.join(",")}]`)} doesn't exist`, "warn");
363
362
  if (missPipes.length)
364
- log(`${pc.blue(`Pipe [${missPipes.join(",")}]`)} doesn't exist`, "warn");
363
+ log(`${pc.blue(`PipeType [${missPipes.join(",")}]`)} doesn't exist`, "warn");
365
364
  if (missFilters.length)
366
- log(`${pc.red(`Filter [${missFilters.join(",")}]`)} doesn't exist`, "warn");
365
+ log(`${pc.red(`FilterType [${missFilters.join(",")}]`)} doesn't exist`, "warn");
367
366
  }
368
367
  __name(isAopDepInject, "isAopDepInject");
369
368
 
@@ -384,7 +383,6 @@ export {
384
383
  UnauthorizedException,
385
384
  ServiceUnavailableException,
386
385
  FrameworkException,
387
- guardRecord,
388
386
  Context,
389
387
  addPlugin,
390
388
  addPipe,
@@ -108,12 +108,13 @@ function Injectable() {
108
108
  _chunkXHKBG2VAjs.__name.call(void 0, Injectable, "Injectable");
109
109
  var debug = _debug2.default.call(void 0, "phecda-server");
110
110
  var emitter = new (0, _events2.default)();
111
- async function Factory(Modules, opts = {}) {
111
+ async function Factory(models, opts = {}) {
112
112
  const moduleMap = /* @__PURE__ */ new Map();
113
113
  const meta = [];
114
114
  const constructorMap = /* @__PURE__ */ new Map();
115
115
  const constructorSet = /* @__PURE__ */ new WeakSet();
116
- const moduleGraph = /* @__PURE__ */ new Map();
116
+ const dependenceGraph = /* @__PURE__ */ new Map();
117
+ const isolateSet = /* @__PURE__ */ new Set();
117
118
  const { http, rpc } = opts;
118
119
  if (!_phecdacore.getKey.call(void 0, "watcher")) {
119
120
  _phecdacore.injectKey.call(void 0, "watcher", ({ eventName, instance, key, options }) => {
@@ -132,7 +133,7 @@ async function Factory(Modules, opts = {}) {
132
133
  return;
133
134
  const instance = moduleMap.get(tag);
134
135
  debug(`unmount module "${String(tag)}"`);
135
- _phecdacore.invokeHandler.call(void 0, "unmount", instance);
136
+ await _phecdacore.invokeHandler.call(void 0, "unmount", instance);
136
137
  debug(`del module "${String(tag)}"`);
137
138
  moduleMap.delete(tag);
138
139
  constructorMap.delete(tag);
@@ -149,14 +150,14 @@ async function Factory(Modules, opts = {}) {
149
150
  await del(tag);
150
151
  }
151
152
  _chunkXHKBG2VAjs.__name.call(void 0, destroy, "destroy");
152
- async function add(Module) {
153
- const tag = _phecdacore.getTag.call(void 0, Module);
153
+ async function add(Model) {
154
+ const tag = _phecdacore.getTag.call(void 0, Model);
154
155
  const oldInstance = await del(tag);
155
- const { instance: newModule } = await buildNestModule(Module);
156
- if (oldInstance && moduleGraph.has(tag)) {
156
+ const { instance: newModule } = await buildDepModule(Model);
157
+ if (oldInstance && dependenceGraph.has(tag)) {
157
158
  debug(`replace module "${String(tag)}"`);
158
159
  [
159
- ...moduleGraph.get(tag)
160
+ ...dependenceGraph.get(tag)
160
161
  ].forEach((tag2) => {
161
162
  const module = moduleMap.get(tag2);
162
163
  for (const key in module) {
@@ -167,17 +168,19 @@ async function Factory(Modules, opts = {}) {
167
168
  }
168
169
  }
169
170
  _chunkXHKBG2VAjs.__name.call(void 0, add, "add");
170
- async function buildNestModule(Module) {
171
- const paramtypes = getParamTypes(Module);
171
+ async function buildDepModule(Model) {
172
+ const paramtypes = getParamTypes(Model);
172
173
  let instance;
173
- const tag = _phecdacore.getTag.call(void 0, Module);
174
+ const tag = _phecdacore.getTag.call(void 0, Model);
175
+ if (_phecdacore.get.call(void 0, Model, "isolate"))
176
+ isolateSet.add(tag);
174
177
  if (moduleMap.has(tag)) {
175
178
  instance = moduleMap.get(tag);
176
179
  if (!instance)
177
- throw new Error(`exist Circular-Dependency or Multiple modules with the same name/tag [tag] ${String(tag)}--[module] ${Module}`);
178
- if (constructorMap.get(tag) !== Module && !constructorSet.has(Module)) {
179
- constructorSet.add(Module);
180
- _chunkXHKBG2VAjs.log.call(void 0, `Synonym module: Module taged "${String(tag)}" has been loaded before, so phecda-server won't load Module "${Module.name}"`, "warn");
180
+ throw new Error(`exist Circular-Dependency or Multiple modules with the same name/tag [tag] ${String(tag)}--[module] ${Model}`);
181
+ if (constructorMap.get(tag) !== Model && !constructorSet.has(Model)) {
182
+ constructorSet.add(Model);
183
+ _chunkXHKBG2VAjs.log.call(void 0, `Synonym module: Module taged "${String(tag)}" has been loaded before, so phecda-server won't load Module "${Model.name}"`, "warn");
181
184
  }
182
185
  return {
183
186
  instance,
@@ -189,30 +192,30 @@ async function Factory(Modules, opts = {}) {
189
192
  if (paramtypes) {
190
193
  const paramtypesInstances = [];
191
194
  for (const i in paramtypes) {
192
- const { instance: sub, tag: subTag } = await buildNestModule(paramtypes[i]);
195
+ const { instance: sub, tag: subTag } = await buildDepModule(paramtypes[i]);
193
196
  paramtypesInstances[i] = sub;
194
- if (!moduleGraph.has(subTag))
195
- moduleGraph.set(subTag, /* @__PURE__ */ new Set());
196
- moduleGraph.get(subTag).add(tag);
197
+ if (!dependenceGraph.has(subTag))
198
+ dependenceGraph.set(subTag, /* @__PURE__ */ new Set());
199
+ dependenceGraph.get(subTag).add(tag);
197
200
  }
198
- instance = new Module(...paramtypesInstances);
201
+ instance = new Model(...paramtypesInstances);
199
202
  } else {
200
- instance = new Module();
203
+ instance = new Model();
201
204
  }
202
- meta.push(...getMetaFromInstance(instance, tag, Module.name));
205
+ meta.push(...getMetaFromInstance(instance, tag, Model.name));
203
206
  debug(`init module "${String(tag)}"`);
204
207
  await _phecdacore.invokeHandler.call(void 0, "init", instance);
205
208
  debug(`add module "${String(tag)}"`);
206
209
  moduleMap.set(tag, instance);
207
- constructorMap.set(tag, Module);
210
+ constructorMap.set(tag, Model);
208
211
  return {
209
212
  instance,
210
213
  tag
211
214
  };
212
215
  }
213
- _chunkXHKBG2VAjs.__name.call(void 0, buildNestModule, "buildNestModule");
214
- for (const Module of Modules)
215
- await buildNestModule(Module);
216
+ _chunkXHKBG2VAjs.__name.call(void 0, buildDepModule, "buildDepModule");
217
+ for (const model of models)
218
+ await buildDepModule(model);
216
219
  function writeCode() {
217
220
  if (http) {
218
221
  debug(`write http code to ${http}`);
@@ -231,10 +234,10 @@ async function Factory(Modules, opts = {}) {
231
234
  _optionalChain([globalThis, 'access', _3 => _3.__PS_HMR__, 'optionalAccess', _4 => _4.push, 'call', _5 => _5(async (files) => {
232
235
  debug("reload files ");
233
236
  for (const file of files) {
234
- const module = await Promise.resolve().then(() => require(file));
235
- for (const i in module) {
236
- if (_phecdacore.isPhecda.call(void 0, module[i]))
237
- await add(module[i]);
237
+ const models2 = await Promise.resolve().then(() => require(file));
238
+ for (const i in models2) {
239
+ if (_phecdacore.isPhecda.call(void 0, models2[i]))
240
+ await add(models2[i]);
238
241
  }
239
242
  }
240
243
  writeCode();
@@ -242,8 +245,8 @@ async function Factory(Modules, opts = {}) {
242
245
  }
243
246
  return {
244
247
  moduleMap,
245
- meta,
246
248
  constructorMap,
249
+ meta,
247
250
  add,
248
251
  del,
249
252
  destroy
@@ -254,8 +257,11 @@ function getMetaFromInstance(instance, tag, name) {
254
257
  const vars = _phecdacore.getExposeKey.call(void 0, instance).filter((item) => item !== _phecdacore.SHARE_KEY);
255
258
  const baseState = _phecdacore.getState.call(void 0, instance, _phecdacore.SHARE_KEY) || {};
256
259
  initState(baseState);
260
+ const ctx = _phecdacore.get.call(void 0, instance, "context");
257
261
  return vars.map((i) => {
258
- const meta = {};
262
+ const meta = {
263
+ ctx
264
+ };
259
265
  const state = _phecdacore.getState.call(void 0, instance, i) || {};
260
266
  initState(state);
261
267
  if (state.http) {
@@ -100,7 +100,7 @@ __name(generateHTTPCode, "generateHTTPCode");
100
100
  import "reflect-metadata";
101
101
  import fs from "fs";
102
102
  import EventEmitter from "node:events";
103
- import { Empty, SHARE_KEY, getExposeKey, getKey, getState, getTag, injectKey, invokeHandler, isPhecda } from "phecda-core";
103
+ import { Empty, SHARE_KEY, get, getExposeKey, getKey, getState, getTag, injectKey, invokeHandler, isPhecda } from "phecda-core";
104
104
  import Debug from "debug";
105
105
  function Injectable() {
106
106
  return (target) => Empty(target);
@@ -108,12 +108,13 @@ function Injectable() {
108
108
  __name(Injectable, "Injectable");
109
109
  var debug = Debug("phecda-server");
110
110
  var emitter = new EventEmitter();
111
- async function Factory(Modules, opts = {}) {
111
+ async function Factory(models, opts = {}) {
112
112
  const moduleMap = /* @__PURE__ */ new Map();
113
113
  const meta = [];
114
114
  const constructorMap = /* @__PURE__ */ new Map();
115
115
  const constructorSet = /* @__PURE__ */ new WeakSet();
116
- const moduleGraph = /* @__PURE__ */ new Map();
116
+ const dependenceGraph = /* @__PURE__ */ new Map();
117
+ const isolateSet = /* @__PURE__ */ new Set();
117
118
  const { http, rpc } = opts;
118
119
  if (!getKey("watcher")) {
119
120
  injectKey("watcher", ({ eventName, instance, key, options }) => {
@@ -132,7 +133,7 @@ async function Factory(Modules, opts = {}) {
132
133
  return;
133
134
  const instance = moduleMap.get(tag);
134
135
  debug(`unmount module "${String(tag)}"`);
135
- invokeHandler("unmount", instance);
136
+ await invokeHandler("unmount", instance);
136
137
  debug(`del module "${String(tag)}"`);
137
138
  moduleMap.delete(tag);
138
139
  constructorMap.delete(tag);
@@ -149,14 +150,14 @@ async function Factory(Modules, opts = {}) {
149
150
  await del(tag);
150
151
  }
151
152
  __name(destroy, "destroy");
152
- async function add(Module) {
153
- const tag = getTag(Module);
153
+ async function add(Model) {
154
+ const tag = getTag(Model);
154
155
  const oldInstance = await del(tag);
155
- const { instance: newModule } = await buildNestModule(Module);
156
- if (oldInstance && moduleGraph.has(tag)) {
156
+ const { instance: newModule } = await buildDepModule(Model);
157
+ if (oldInstance && dependenceGraph.has(tag)) {
157
158
  debug(`replace module "${String(tag)}"`);
158
159
  [
159
- ...moduleGraph.get(tag)
160
+ ...dependenceGraph.get(tag)
160
161
  ].forEach((tag2) => {
161
162
  const module = moduleMap.get(tag2);
162
163
  for (const key in module) {
@@ -167,17 +168,19 @@ async function Factory(Modules, opts = {}) {
167
168
  }
168
169
  }
169
170
  __name(add, "add");
170
- async function buildNestModule(Module) {
171
- const paramtypes = getParamTypes(Module);
171
+ async function buildDepModule(Model) {
172
+ const paramtypes = getParamTypes(Model);
172
173
  let instance;
173
- const tag = getTag(Module);
174
+ const tag = getTag(Model);
175
+ if (get(Model, "isolate"))
176
+ isolateSet.add(tag);
174
177
  if (moduleMap.has(tag)) {
175
178
  instance = moduleMap.get(tag);
176
179
  if (!instance)
177
- throw new Error(`exist Circular-Dependency or Multiple modules with the same name/tag [tag] ${String(tag)}--[module] ${Module}`);
178
- if (constructorMap.get(tag) !== Module && !constructorSet.has(Module)) {
179
- constructorSet.add(Module);
180
- log(`Synonym module: Module taged "${String(tag)}" has been loaded before, so phecda-server won't load Module "${Module.name}"`, "warn");
180
+ throw new Error(`exist Circular-Dependency or Multiple modules with the same name/tag [tag] ${String(tag)}--[module] ${Model}`);
181
+ if (constructorMap.get(tag) !== Model && !constructorSet.has(Model)) {
182
+ constructorSet.add(Model);
183
+ log(`Synonym module: Module taged "${String(tag)}" has been loaded before, so phecda-server won't load Module "${Model.name}"`, "warn");
181
184
  }
182
185
  return {
183
186
  instance,
@@ -189,30 +192,30 @@ async function Factory(Modules, opts = {}) {
189
192
  if (paramtypes) {
190
193
  const paramtypesInstances = [];
191
194
  for (const i in paramtypes) {
192
- const { instance: sub, tag: subTag } = await buildNestModule(paramtypes[i]);
195
+ const { instance: sub, tag: subTag } = await buildDepModule(paramtypes[i]);
193
196
  paramtypesInstances[i] = sub;
194
- if (!moduleGraph.has(subTag))
195
- moduleGraph.set(subTag, /* @__PURE__ */ new Set());
196
- moduleGraph.get(subTag).add(tag);
197
+ if (!dependenceGraph.has(subTag))
198
+ dependenceGraph.set(subTag, /* @__PURE__ */ new Set());
199
+ dependenceGraph.get(subTag).add(tag);
197
200
  }
198
- instance = new Module(...paramtypesInstances);
201
+ instance = new Model(...paramtypesInstances);
199
202
  } else {
200
- instance = new Module();
203
+ instance = new Model();
201
204
  }
202
- meta.push(...getMetaFromInstance(instance, tag, Module.name));
205
+ meta.push(...getMetaFromInstance(instance, tag, Model.name));
203
206
  debug(`init module "${String(tag)}"`);
204
207
  await invokeHandler("init", instance);
205
208
  debug(`add module "${String(tag)}"`);
206
209
  moduleMap.set(tag, instance);
207
- constructorMap.set(tag, Module);
210
+ constructorMap.set(tag, Model);
208
211
  return {
209
212
  instance,
210
213
  tag
211
214
  };
212
215
  }
213
- __name(buildNestModule, "buildNestModule");
214
- for (const Module of Modules)
215
- await buildNestModule(Module);
216
+ __name(buildDepModule, "buildDepModule");
217
+ for (const model of models)
218
+ await buildDepModule(model);
216
219
  function writeCode() {
217
220
  if (http) {
218
221
  debug(`write http code to ${http}`);
@@ -231,10 +234,10 @@ async function Factory(Modules, opts = {}) {
231
234
  globalThis.__PS_HMR__?.push(async (files) => {
232
235
  debug("reload files ");
233
236
  for (const file of files) {
234
- const module = await import(file);
235
- for (const i in module) {
236
- if (isPhecda(module[i]))
237
- await add(module[i]);
237
+ const models2 = await import(file);
238
+ for (const i in models2) {
239
+ if (isPhecda(models2[i]))
240
+ await add(models2[i]);
238
241
  }
239
242
  }
240
243
  writeCode();
@@ -242,8 +245,8 @@ async function Factory(Modules, opts = {}) {
242
245
  }
243
246
  return {
244
247
  moduleMap,
245
- meta,
246
248
  constructorMap,
249
+ meta,
247
250
  add,
248
251
  del,
249
252
  destroy
@@ -254,8 +257,11 @@ function getMetaFromInstance(instance, tag, name) {
254
257
  const vars = getExposeKey(instance).filter((item) => item !== SHARE_KEY);
255
258
  const baseState = getState(instance, SHARE_KEY) || {};
256
259
  initState(baseState);
260
+ const ctx = get(instance, "context");
257
261
  return vars.map((i) => {
258
- const meta = {};
262
+ const meta = {
263
+ ctx
264
+ };
259
265
  const state = getState(instance, i) || {};
260
266
  initState(state);
261
267
  if (state.http) {
@@ -0,0 +1,112 @@
1
+ import { Events, Construct } from 'phecda-core';
2
+ import { IncomingHttpHeaders } from 'node:http';
3
+
4
+ declare const MERGE_SYMBOL = "__PS_MERGE__";
5
+ declare const UNMOUNT_SYMBOL = "__PS_UNMOUNT__";
6
+ declare const MODULE_SYMBOL = "__PS_MODULE__";
7
+ declare const META_SYMBOL = "__PS_META__";
8
+ declare const APP_SYMBOL = "__PS__";
9
+ declare const IS_DEV: boolean;
10
+ declare const IS_STRICT: boolean;
11
+ declare const IS_LOG_BAN: boolean;
12
+ declare const ERROR_SYMBOL = "__PS_ERROR__";
13
+ declare const PS_FILE_RE: RegExp;
14
+ declare const PS_IMPORT_RE: RegExp;
15
+
16
+ interface MetaData {
17
+ http?: {
18
+ type: P.RequestType;
19
+ route: string;
20
+ };
21
+ rpc?: {
22
+ type: string[];
23
+ isEvent: boolean;
24
+ };
25
+ ctx?: string;
26
+ define?: any;
27
+ header: Record<string, string>;
28
+ params: {
29
+ type: string;
30
+ index: number;
31
+ key: string;
32
+ pipe?: string;
33
+ pipeOpts?: any;
34
+ }[];
35
+ guards: string[];
36
+ filter?: string;
37
+ interceptors: string[];
38
+ plugins: string[];
39
+ method: string;
40
+ name: string;
41
+ tag: PropertyKey;
42
+ }
43
+ declare class Meta {
44
+ data: MetaData;
45
+ paramsType: any[];
46
+ constructor(data: MetaData, paramsType: any[]);
47
+ }
48
+
49
+ declare namespace P {
50
+ interface BaseContext {
51
+ meta: Meta;
52
+ moduleMap: Record<string, any>;
53
+ parallel?: boolean;
54
+ type: string;
55
+ tag: string;
56
+ [key: string]: any;
57
+ }
58
+ interface HttpContext extends BaseContext {
59
+ query: Record<string, any>;
60
+ params: Record<string, string>;
61
+ body: Record<string, any>;
62
+ headers: IncomingHttpHeaders;
63
+ index?: number;
64
+ data: any;
65
+ }
66
+ interface Error {
67
+ [ERROR_SYMBOL]: true;
68
+ status: number;
69
+ message: string;
70
+ description: string;
71
+ }
72
+ type Ret<T> = Awaited<T> extends {
73
+ toJSON(): infer R;
74
+ } ? R : Awaited<T>;
75
+ type RequestType = 'get' | 'post' | 'put' | 'delete' | 'patch' | 'options' | 'head';
76
+ }
77
+
78
+ interface Emitter {
79
+ on<N extends keyof Events>(eventName: N, cb: (args: Events[N]) => void): void;
80
+ once<N extends keyof Events>(eventName: N, cb: (args: Events[N]) => void): void;
81
+ off<N extends keyof Events>(eventName: N, cb: (args: Events[N]) => void): void;
82
+ removeAllListeners<N extends keyof Events>(eventName: N): void;
83
+ emit<N extends keyof Events>(eventName: N, param: Events[N]): void;
84
+ }
85
+ type ToClientMap<T = any> = {
86
+ [K in keyof T]: T[K] extends (new (...args: any) => any) ? ToClientInstance<PickFunc<InstanceType<T[K]>>> : void;
87
+ };
88
+ type ToClientInstance<R extends Record<string, (...args: any) => any>> = {
89
+ [K in keyof R]: ToClientFn<R[K]>;
90
+ };
91
+ type ToClientFn<T extends (...args: any) => any> = (...p: Parameters<T>) => Promise<P.Ret<ReturnType<T>>>;
92
+ type PickKeysByValue<Type, Value> = {
93
+ [Key in keyof Type]: Type[Key] extends Value ? Key : never;
94
+ }[keyof Type];
95
+ type PickFunc<T> = Pick<T, PickKeysByValue<T, (...args: any) => any>>;
96
+ type OmitFunction<T> = Omit<T, PickKeysByValue<T, (...args: any) => any>>;
97
+
98
+ declare function Injectable(): (target: any) => void;
99
+ declare const emitter: Emitter;
100
+ declare function Factory(models: (new (...args: any) => any)[], opts?: {
101
+ http?: string;
102
+ rpc?: string;
103
+ }): Promise<{
104
+ moduleMap: Map<PropertyKey, any>;
105
+ constructorMap: Map<any, any>;
106
+ meta: Meta[];
107
+ add: (Model: Construct) => Promise<void>;
108
+ del: (tag: PropertyKey) => Promise<any>;
109
+ destroy: () => Promise<void>;
110
+ }>;
111
+
112
+ export { APP_SYMBOL as A, Emitter as E, Factory as F, Injectable as I, Meta as M, OmitFunction as O, P, ToClientMap as T, UNMOUNT_SYMBOL as U, MetaData as a, ToClientInstance as b, ToClientFn as c, PickFunc as d, emitter as e, MERGE_SYMBOL as f, MODULE_SYMBOL as g, META_SYMBOL as h, IS_DEV as i, IS_STRICT as j, IS_LOG_BAN as k, ERROR_SYMBOL as l, PS_FILE_RE as m, PS_IMPORT_RE as n };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { E as Exception, P, M as Meta, U as UNMOUNT_SYMBOL } from './core-eb2a0799.js';
2
- export { A as APP_SYMBOL, k as ERROR_SYMBOL, a as Emitter, F as Factory, h as IS_DEV, j as IS_LOG_BAN, i as IS_STRICT, I as Injectable, d as MERGE_SYMBOL, g as META_SYMBOL, f as MODULE_SYMBOL, l as PS_FILE_RE, m as PS_IMPORT_RE, c as PickFunc, b as PickKeysByValue, R as RequestType, T as ToControllerMap, e as emitter } from './core-eb2a0799.js';
1
+ import { P, M as Meta, U as UNMOUNT_SYMBOL, a as MetaData } from './core-895ed04d.js';
2
+ export { A as APP_SYMBOL, l as ERROR_SYMBOL, E as Emitter, F as Factory, i as IS_DEV, k as IS_LOG_BAN, j as IS_STRICT, I as Injectable, f as MERGE_SYMBOL, h as META_SYMBOL, g as MODULE_SYMBOL, O as OmitFunction, m as PS_FILE_RE, n as PS_IMPORT_RE, d as PickFunc, c as ToClientFn, b as ToClientInstance, T as ToClientMap, e as emitter } from './core-895ed04d.js';
3
3
  import { Construct, AbConstruct } from 'phecda-core';
4
4
  export * from 'phecda-core';
5
5
  import 'node:http';
@@ -10,6 +10,19 @@ declare class Histroy {
10
10
  record(name: string, type: 'guard' | 'interceptor'): boolean;
11
11
  }
12
12
 
13
+ declare class Exception extends Error {
14
+ message: string;
15
+ status: number;
16
+ description: string;
17
+ constructor(message: string, status: number, description?: string);
18
+ get data(): {
19
+ message: string;
20
+ description: string;
21
+ status: number;
22
+ __PS_ERROR__: boolean;
23
+ };
24
+ }
25
+
13
26
  declare class UndefinedException extends Exception {
14
27
  constructor(message: string);
15
28
  }
@@ -66,16 +79,26 @@ declare class FrameworkException extends Exception {
66
79
  constructor(message: string);
67
80
  }
68
81
 
69
- declare const guardRecord: Record<string, P.Guard<any>>;
82
+ type GuardType<C extends P.BaseContext = any> = ((ctx: C) => Promise<boolean> | boolean);
83
+ type InterceptorType<C extends P.BaseContext = any> = (ctx: C) => (any | ((ret: any) => any));
84
+ type PipeType<C extends P.BaseContext = any> = (arg: {
85
+ arg: any;
86
+ option?: any;
87
+ key: string;
88
+ type: string;
89
+ index: number;
90
+ reflect: any;
91
+ }, ctx: C) => Promise<any>;
92
+ type FilterType<C extends P.BaseContext = any, E extends Exception = any> = (err: E | Error, ctx?: C) => Error | any;
70
93
  declare class Context<Data extends P.BaseContext> {
71
94
  data: Data;
72
95
  method: string;
73
96
  params: string[];
74
97
  history: Histroy;
75
- static filterRecord: Record<PropertyKey, P.Filter>;
76
- static pipeRecord: Record<PropertyKey, P.Pipe>;
77
- static guardRecord: Record<PropertyKey, P.Guard>;
78
- static interceptorRecord: Record<PropertyKey, P.Interceptor>;
98
+ static filterRecord: Record<PropertyKey, FilterType>;
99
+ static pipeRecord: Record<PropertyKey, PipeType>;
100
+ static guardRecord: Record<PropertyKey, GuardType>;
101
+ static interceptorRecord: Record<PropertyKey, InterceptorType>;
79
102
  static pluginRecord: Record<PropertyKey, any>;
80
103
  postInterceptors: Function[];
81
104
  constructor(data: Data);
@@ -95,10 +118,10 @@ declare class Context<Data extends P.BaseContext> {
95
118
  static usePlugin(plugins: string[]): any[];
96
119
  }
97
120
  declare function addPlugin<T>(key: PropertyKey, handler: T): void;
98
- declare function addPipe<C extends P.BaseContext>(key: PropertyKey, handler: P.Pipe<C>): void;
99
- declare function addFilter<C extends P.BaseContext>(key: PropertyKey, handler: P.Filter<C>): void;
100
- declare function addGuard<C extends P.BaseContext>(key: PropertyKey, handler: P.Guard<C>): void;
101
- declare function addInterceptor<C extends P.BaseContext>(key: PropertyKey, handler: P.Interceptor<C>): void;
121
+ declare function addPipe<C extends P.BaseContext>(key: PropertyKey, handler: PipeType<C>): void;
122
+ declare function addFilter<C extends P.BaseContext>(key: PropertyKey, handler: FilterType<C>): void;
123
+ declare function addGuard<C extends P.BaseContext>(key: PropertyKey, handler: GuardType<C>): void;
124
+ declare function addInterceptor<C extends P.BaseContext>(key: PropertyKey, handler: InterceptorType<C>): void;
102
125
  declare function isAopDepInject(meta: Meta[], { guards, interceptors, plugins }?: {
103
126
  guards?: string[];
104
127
  interceptors?: string[];
@@ -129,9 +152,10 @@ declare function Interceptor(...interceptors: string[]): any;
129
152
  declare function Filter(filter: string): any;
130
153
 
131
154
  declare function Header(name: string, value: string): (target: any, k: PropertyKey) => void;
155
+ declare function Ctx(target: any, key: PropertyKey): void;
132
156
  declare function Define(key: string, value: any): (target: any, k?: PropertyKey) => void;
133
157
 
134
- declare const defaultPipe: P.Pipe;
158
+ declare const defaultPipe: PipeType;
135
159
 
136
160
  declare function resolveDep(ret: any, key: string): any;
137
161
  declare function argToReq(params: Meta['data']['params'], args: any[], headers: Record<string, any>): any;
@@ -199,13 +223,13 @@ declare class PExtension extends Dev {
199
223
  constructor(tag?: string);
200
224
  }
201
225
 
202
- declare function generateRPCCode(meta: P.MetaData[]): string;
226
+ declare function generateRPCCode(meta: MetaData[]): string;
203
227
 
204
- declare function generateHTTPCode(meta: P.MetaData[]): string;
228
+ declare function generateHTTPCode(meta: MetaData[]): string;
205
229
 
206
230
  declare function log(msg: string, level?: 'error' | 'info' | 'warn'): void;
207
231
  declare function getConfig<C = any>(key: string, defaultConf?: C): C;
208
232
  declare function setConfig<C = any>(key: string, conf: C, force?: boolean): void;
209
233
  declare function Mix<C1 extends Construct | AbConstruct, C2 extends Construct>(InternalClass: C1, ExtendClass: C2): new (...args: ConstructorParameters<C2>) => InstanceType<C1> & InstanceType<C2>;
210
234
 
211
- export { Arg, BadGatewayException, BadRequestException, BaseParam, Body, ConflictException, Context, Controller, Define, Delete, Dev, Event, Exception, Filter, ForbiddenException, FrameworkException, Get, Guard, Head, Header, Interceptor, InvalidInputException, Meta, Mix, NotFoundException, P, PExtension, PFilter, PGuard, PInterceptor, PPipe, PPlugin, Param, Patch, PayloadLargeException, Pipe, Plugin, Post, Put, Query, Route, Rpc, ServiceUnavailableException, TimeoutException, UNMOUNT_SYMBOL, UnauthorizedException, UndefinedException, UnsupportedMediaTypeException, ValidateException, addFilter, addGuard, addInterceptor, addPipe, addPlugin, argToReq, defaultPipe, generateHTTPCode, generateRPCCode, getConfig, guardRecord, isAopDepInject, log, resolveDep, setConfig };
235
+ export { Arg, BadGatewayException, BadRequestException, BaseParam, Body, ConflictException, Context, Controller, Ctx, Define, Delete, Dev, Event, Exception, Filter, FilterType, ForbiddenException, FrameworkException, Get, Guard, GuardType, Head, Header, Interceptor, InterceptorType, InvalidInputException, Meta, MetaData, Mix, NotFoundException, P, PExtension, PFilter, PGuard, PInterceptor, PPipe, PPlugin, Param, Patch, PayloadLargeException, Pipe, PipeType, Plugin, Post, Put, Query, Route, Rpc, ServiceUnavailableException, TimeoutException, UNMOUNT_SYMBOL, UnauthorizedException, UndefinedException, UnsupportedMediaTypeException, ValidateException, addFilter, addGuard, addInterceptor, addPipe, addPlugin, argToReq, defaultPipe, generateHTTPCode, generateRPCCode, getConfig, isAopDepInject, log, resolveDep, setConfig };