phecda-server 4.0.4 → 4.0.5

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.
@@ -131,16 +131,15 @@ async function Factory(Modules, opts = {}) {
131
131
  else
132
132
  emitter.on(eventName, fn);
133
133
  });
134
- async function update(Module) {
135
- const tag = Module.prototype?.__TAG__ || Module.name;
134
+ async function del(tag) {
136
135
  if (!moduleMap.has(tag))
137
136
  return;
138
- debug(`update module "${tag}"`);
139
137
  const instance = moduleMap.get(tag);
140
138
  if (instance?.[UNMOUNT_SYMBOL]) {
141
139
  for (const cb of instance[UNMOUNT_SYMBOL])
142
140
  await cb();
143
141
  }
142
+ debug(`del module "${tag}"`);
144
143
  log(`Module ${pc.yellow(`[${tag}]`)} unmount`);
145
144
  moduleMap.delete(tag);
146
145
  constructorMap.delete(tag);
@@ -148,21 +147,28 @@ async function Factory(Modules, opts = {}) {
148
147
  if (meta[i].data.tag === tag)
149
148
  meta.splice(i, 1);
150
149
  }
150
+ return instance;
151
+ }
152
+ __name(del, "del");
153
+ async function add(Module) {
154
+ const tag = Module.prototype?.__TAG__ || Module.name;
155
+ const oldInstance = await del(tag);
151
156
  const { instance: newModule } = await buildNestModule(Module);
152
- if (moduleGraph.has(tag)) {
157
+ debug(`add module "${tag}"`);
158
+ if (oldInstance && moduleGraph.has(tag)) {
153
159
  [
154
160
  ...moduleGraph.get(tag)
155
161
  ].forEach((tag2) => {
156
162
  const module = moduleMap.get(tag2);
157
163
  for (const key in module) {
158
- if (module[key] === instance)
164
+ if (module[key] === oldInstance)
159
165
  module[key] = newModule;
160
166
  }
161
167
  });
162
168
  }
163
169
  moduleMap.set(tag, newModule);
164
170
  }
165
- __name(update, "update");
171
+ __name(add, "add");
166
172
  async function buildNestModule(Module) {
167
173
  const paramtypes = getParamTypes(Module);
168
174
  let instance;
@@ -223,7 +229,7 @@ async function Factory(Modules, opts = {}) {
223
229
  const module = await import(file);
224
230
  for (const i in module) {
225
231
  if (isPhecda(module[i]))
226
- await update(module[i]);
232
+ await add(module[i]);
227
233
  }
228
234
  }
229
235
  writeCode();
@@ -233,7 +239,8 @@ async function Factory(Modules, opts = {}) {
233
239
  moduleMap,
234
240
  meta,
235
241
  constructorMap,
236
- update
242
+ add,
243
+ del
237
244
  };
238
245
  }
239
246
  __name(Factory, "Factory");
@@ -325,4 +332,4 @@ export {
325
332
  emitter,
326
333
  Factory
327
334
  };
328
- //# sourceMappingURL=chunk-JUXEUWOU.mjs.map
335
+ //# sourceMappingURL=chunk-NFAA3WFR.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/meta.ts","../src/compiler/rpc.ts","../src/compiler/http.ts","../src/core.ts"],"sourcesContent":["import type { P } from './types'\n\nexport class Meta {\n constructor(public data: P.Meta, public handlers: P.Handler[], public paramsType: any[]) {\n\n }\n}\n","import type { P } from '../types'\n\nclass Compiler {\n classMap: Record<string, { [key: string]: string }> = {}\n constructor() { }\n\n getContent() {\n let content = ''\n\n for (const name in this.classMap) {\n content += `\n export class ${name}{\n ${Object.values(this.classMap[name]).reduce((p, c) => p + c)}\n }`\n }\n return content\n }\n\n addMethod(args: P.Meta) {\n const {\n rpc, name, method, tag,\n } = args\n if (!rpc || !rpc.type)\n return\n if (!this.classMap[name])\n this.classMap[name] = {}\n this.classMap[name][method] = `\n ${method}(){\n return {tag:'${tag}-${method}',isEvent:${!!rpc.isEvent},rpc:[${rpc.type.reduce((p, c) => {\n return `${p}\"${c}\",`\n }, '')}]}\n\n }\n `\n }\n}\n\nexport function generateRPCCode(meta: P.Meta[]) {\n const compiler = new Compiler()\n\n for (const i of meta)\n compiler.addMethod(i)\n return compiler.getContent()\n}\n","import type { P } from '../types'\n\nclass Compiler {\n classMap: Record<string, { [key: string]: string }> = {}\n constructor() { }\n\n getContent() {\n let content = ''\n\n for (const name in this.classMap) {\n content += `\n export class ${name}{\n ${Object.values(this.classMap[name]).reduce((p, c) => p + c)}\n }`\n }\n return content\n }\n\n // createRequest() {\n // let content = 'import {useC} from \\'phecda-server\\'\\n'\n // for (const name in this.classMap)\n // content += `export const {${Object.keys(this.classMap[name]).join(',')}}=useC(${name})\\n`\n // return content\n // }\n\n addMethod(args: P.Meta) {\n const {\n http, name, method, params, tag,\n } = args\n if (!http)\n return\n const url = http.route.replace(/\\/\\:([^\\/]*)/g, (_, js) => `/{{${js}}}`)\n if (!this.classMap[name])\n this.classMap[name] = {}\n this.classMap[name][method] = `\n ${method}(...args){\nconst ret={tag:\"${tag}-${method}\",body:{},headers:{},query:{},params:{},method:\"${http.type}\",url:\"${url}\",args}\n\n${params.reduce((p, c, i) => `${p}ret.${c.type}${c.key ? `['${c.key}']` : ''}=args[${i}]\\n${c.type === 'params' ? `ret.url=ret.url.replace('{{${c.key}}}',args[${i}])` : ''}\\n`, '')}\nreturn ret\n }\n `\n }\n}\n\nexport function generateHTTPCode(meta: P.Meta[]) {\n const compiler = new Compiler()\n\n for (const i of meta)\n compiler.addMethod(i)\n return compiler.getContent()\n}\n","import 'reflect-metadata'\nimport fs from 'fs'\nimport EventEmitter from 'node:events'\nimport type { Phecda } from 'phecda-core'\nimport { Empty, getExposeKey, getHandler, getState, injectProperty, isPhecda, registerAsync } from 'phecda-core'\nimport Debug from 'debug'\nimport pc from 'picocolors'\nimport type { Construct, Emitter, P } from './types'\nimport { Meta } from './meta'\nimport { log } from './utils'\nimport { IS_DEV, UNMOUNT_SYMBOL } from './common'\nimport { generateHTTPCode, generateRPCCode } from './compiler'\nexport function Injectable() {\n return (target: any) => Empty(target)\n}\nconst debug = Debug('phecda-server')\n// TODO: support both emitter types and origin emitter type in future\nexport const emitter: Emitter = new EventEmitter() as any\n\nexport async function Factory(Modules: (new (...args: any) => any)[], opts: {\n\n // HTTP generate code path\n http?: string\n // rpc generate code path\n rpc?: string\n} = {}) {\n const moduleMap = new Map<string, InstanceType<Construct>>()\n const meta: Meta[] = []\n const constructorMap = new Map()\n\n // only work for warn\n const constructorSet = new WeakSet()\n const moduleGraph = new Map<string, Set<string>>()\n const { http, rpc } = opts\n injectProperty('watcher', ({ eventName, instance, key, options }: { eventName: string; instance: any; key: string; options?: { once: boolean } }) => {\n const fn = typeof instance[key] === 'function' ? instance[key].bind(instance) : (v: any) => instance[key] = v\n\n // work for hmr\n if (!instance[UNMOUNT_SYMBOL])\n instance[UNMOUNT_SYMBOL] = []\n\n instance[UNMOUNT_SYMBOL].push(() => {\n (emitter as any).off(eventName, fn)\n })\n\n if (options?.once)\n (emitter as any).once(eventName, fn)\n\n else\n (emitter as any).on(eventName, fn)\n })\n\n // only remove module in moduleMap(won't remove indiect module)\n async function del(tag: string) {\n if (!moduleMap.has(tag))\n return\n\n const instance = moduleMap.get(tag)\n\n if (instance?.[UNMOUNT_SYMBOL]) {\n for (const cb of instance[UNMOUNT_SYMBOL])\n await cb()\n }\n\n debug(`del module \"${tag}\"`)\n\n log(`Module ${pc.yellow(`[${tag}]`)} unmount`)\n moduleMap.delete(tag)\n constructorMap.delete(tag)\n for (let i = meta.length - 1; i >= 0; i--) {\n if (meta[i].data.tag === tag)\n meta.splice(i, 1)\n }\n\n return instance\n }\n\n async function add(Module: Construct) {\n const tag = Module.prototype?.__TAG__ || Module.name\n const oldInstance = await del(tag)\n\n const { instance: newModule } = await buildNestModule(Module)\n\n debug(`add module \"${tag}\"`)\n\n if (oldInstance && moduleGraph.has(tag)) {\n [...moduleGraph.get(tag)!].forEach((tag) => {\n const module = moduleMap.get(tag)\n for (const key in module) {\n if (module[key] === oldInstance)\n module[key] = newModule\n }\n })\n }\n\n moduleMap.set(tag, newModule)\n }\n async function buildNestModule(Module: Construct) {\n const paramtypes = getParamTypes(Module) as Construct[]\n let instance: InstanceType<Construct>\n const tag = Module.prototype?.__TAG__ || Module.name\n if (moduleMap.has(tag)) {\n instance = moduleMap.get(tag)\n if (!instance)\n throw new Error(`exist Circular-Dependency or Multiple modules with the same name/tag [tag] ${tag}--[module] ${Module}`)\n\n if (constructorMap.get(tag) !== Module && !constructorSet.has(Module)) {\n constructorSet.add(Module)// a module will only warn once\n log(`Synonym module: Module taged \"${tag}\" has been loaded before, so phecda-server won't load Module \"${Module.name}\"`, 'warn')\n }\n return { instance, tag }\n }\n moduleMap.set(tag, undefined)\n if (paramtypes) {\n const paramtypesInstances = [] as any[]\n for (const i in paramtypes) {\n const { instance: sub, tag: subTag } = await buildNestModule(paramtypes[i])\n paramtypesInstances[i] = sub\n if (!moduleGraph.has(subTag))\n moduleGraph.set(subTag, new Set())\n moduleGraph.get(subTag)!.add(tag)\n }\n instance = new Module(...paramtypesInstances)\n }\n else {\n instance = new Module()\n }\n meta.push(...getMetaFromInstance(instance, tag, Module.name))\n await registerAsync(instance)\n moduleMap.set(tag, instance)\n log(`Module ${pc.yellow(`[${tag}]`)} mount\"`)\n constructorMap.set(tag, Module)\n return { instance, tag }\n }\n\n for (const Module of Modules)\n await buildNestModule(Module)\n\n function writeCode() {\n debug('write code')\n\n http && fs.promises.writeFile(http, generateHTTPCode(meta.map(item => item.data)))\n rpc && fs.promises.writeFile(rpc, generateRPCCode(meta.map(item => item.data)))\n }\n\n writeCode()\n if (IS_DEV) {\n if (!globalThis.__PS_HMR__)\n globalThis.__PS_HMR__ = []\n\n globalThis.__PS_HMR__?.push(async (files: string[]) => {\n for (const file of files) {\n debug(`reload file ${file}`)\n const module = await import(file)\n for (const i in module) {\n if (isPhecda(module[i]))\n await add(module[i])\n }\n }\n writeCode()\n })\n }\n\n return {\n moduleMap,\n meta,\n constructorMap,\n add,\n del,\n }\n}\n\nfunction getMetaFromInstance(instance: Phecda, tag: string, name: string) {\n const vars = getExposeKey(instance).filter(item => item !== '__CLASS')\n const baseState = (getState(instance, '__CLASS') || {}) as P.Meta\n initState(baseState)\n\n return vars.map((i) => {\n const meta = {} as P.Meta\n const state = (getState(instance, i) || {}) as P.Meta\n initState(state)\n if (state.http) {\n meta.http = {\n route: (baseState.http?.route || '') + (state.http.route),\n type: state.http.type,\n }\n }\n if (baseState.rpc)\n meta.rpc = baseState.rpc\n if (state.rpc) {\n meta.rpc = {\n ...meta.rpc,\n ...state.rpc,\n }\n }\n\n meta.name = name\n meta.tag = tag\n meta.method = i as string\n const params = [] as any[]\n for (const i of state.params || []) {\n params.unshift(i)\n if (i.index === 0)\n break\n }\n\n meta.params = params\n meta.define = { ...baseState.define, ...state.define }\n meta.header = { ...baseState.header, ...state.header }\n meta.plugins = [...new Set([...baseState.plugins, ...state.plugins])]\n meta.guards = [...new Set([...baseState.guards, ...state.guards])]\n meta.interceptors = [...new Set([...baseState.interceptors, ...state.interceptors])]\n\n return new Meta(meta as unknown as P.Meta, getHandler(instance, i), getParamTypes(instance, i as string) || [])\n })\n}\n\nfunction getParamTypes(Module: any, key?: string | symbol) {\n return Reflect.getMetadata('design:paramtypes', Module, key!)\n}\n\nfunction initState(state: any) {\n if (!state.define)\n state.define = {}\n if (!state.header)\n state.header = {}\n if (!state.plugins)\n state.plugins = []\n if (!state.guards)\n state.guards = []\n if (!state.interceptors)\n state.interceptors = []\n}\n"],"mappings":";;;;;;;;AAEO,IAAMA,OAAN,MAAMA;EACQC;EAAqBC;EAA8BC;EAAtEC,YAAmBH,MAAqBC,UAA8BC,YAAmB;gBAAtEF;oBAAqBC;sBAA8BC;EAEtE;AACF;AAJaH;;;ACAb,IAAMK,WAAN,6BAAMA,UAAAA;EACJC,WAAsD,CAAC;EACvDC,cAAc;EAAE;EAEhBC,aAAa;AACX,QAAIC,UAAU;AAEd,eAAWC,QAAQ,KAAKJ,UAAU;AAChCG,iBAAW;uBACMC;cACTC,OAAOC,OAAO,KAAKN,SAASI,KAAK,EAAEG,OAAO,CAACC,GAAGC,MAAMD,IAAIC,CAAAA;;IAElE;AACA,WAAON;EACT;EAEAO,UAAUC,MAAc;AACtB,UAAM,EACJC,KAAKR,MAAMS,QAAQC,IAAG,IACpBH;AACJ,QAAI,CAACC,OAAO,CAACA,IAAIG;AACf;AACF,QAAI,CAAC,KAAKf,SAASI;AACjB,WAAKJ,SAASI,QAAQ,CAAC;AACzB,SA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AShD;AACd,UAAMiD,SAAS,CAAA;AACf,eAAWjD,MAAK6C,MAAMI,UAAU,CAAA,GAAI;AAClCA,aAAOC,QAAQlD,EAAAA;AACf,UAAIA,GAAEmD,UAAU;AACd;IACJ;AAEAhF,SAAK8E,SAASA;AACd9E,SAAKiF,SAAS;MAAE,GAAGV,UAAUU;MAAQ,GAAGP,MAAMO;IAAO;AACrDjF,SAAKkF,SAAS;MAAE,GAAGX,UAAUW;MAAQ,GAAGR,MAAMQ;IAAO;AACrDlF,SAAKmF,UAAU;SAAI,oBAAIhC,IAAI;WAAIoB,UAAUY;WAAYT,MAAMS;OAAQ;;AACnEnF,SAAKoF,SAAS;SAAI,oBAAIjC,IAAI;WAAIoB,UAAUa;WAAWV,MAAMU;OAAO;;AAChEpF,SAAKqF,eAAe;SAAI,oBAAIlC,IAAI;WAAIoB,UAAUc;WAAiBX,MAAMW;OAAa;;AAElF,WAAO,IAAIC,KAAKtF,MAA2BuF,WAAW9E,UAAUoB,CAAAA,GAAIgB,cAAcpC,UAAUoB,CAAAA,KAAgB,CAAA,CAAE;EAChH,CAAA;AACF;AA3CSuB;AA6CT,SAASP,cAAcX,QAAaxB,KAAuB;AACzD,SAAO8E,QAAQC,YAAY,qBAAqBvD,QAAQxB,GAAAA;AAC1D;AAFSmC;AAIT,SAAS4B,UAAUC,OAAY;AAC7B,MAAI,CAACA,MAAMO;AACTP,UAAMO,SAAS,CAAC;AAClB,MAAI,CAACP,MAAMQ;AACTR,UAAMQ,SAAS,CAAC;AAClB,MAAI,CAACR,MAAMS;AACTT,UAAMS,UAAU,CAAA;AAClB,MAAI,CAACT,MAAMU;AACTV,UAAMU,SAAS,CAAA;AACjB,MAAI,CAACV,MAAMW;AACTX,UAAMW,eAAe,CAAA;AACzB;AAXSZ;","names":["Meta","data","handlers","paramsType","constructor","Compiler","classMap","constructor","getContent","content","name","Object","values","reduce","p","c","addMethod","args","rpc","method","tag","type","isEvent","generateRPCCode","meta","compiler","i","Compiler","classMap","constructor","getContent","content","name","Object","values","reduce","p","c","addMethod","args","http","method","params","tag","url","route","replace","_","js","type","i","key","generateHTTPCode","meta","compiler","fs","EventEmitter","Empty","getExposeKey","getHandler","getState","injectProperty","isPhecda","registerAsync","Debug","pc","Injectable","target","Empty","debug","Debug","emitter","EventEmitter","Factory","Modules","opts","moduleMap","Map","meta","constructorMap","constructorSet","WeakSet","moduleGraph","http","rpc","injectProperty","eventName","instance","key","options","fn","bind","v","UNMOUNT_SYMBOL","push","off","once","on","del","tag","has","get","cb","log","pc","yellow","delete","i","length","data","splice","add","Module","prototype","__TAG__","name","oldInstance","newModule","buildNestModule","forEach","module","set","paramtypes","getParamTypes","Error","undefined","paramtypesInstances","sub","subTag","Set","getMetaFromInstance","registerAsync","writeCode","fs","promises","writeFile","generateHTTPCode","map","item","generateRPCCode","IS_DEV","globalThis","__PS_HMR__","files","file","isPhecda","vars","getExposeKey","filter","baseState","getState","initState","state","route","type","method","params","unshift","index","define","header","plugins","guards","interceptors","Meta","getHandler","Reflect","getMetadata"]}
@@ -131,16 +131,15 @@ async function Factory(Modules, opts = {}) {
131
131
  else
132
132
  emitter.on(eventName, fn);
133
133
  });
134
- async function update(Module) {
135
- const tag = _optionalChain([Module, 'access', _3 => _3.prototype, 'optionalAccess', _4 => _4.__TAG__]) || Module.name;
134
+ async function del(tag) {
136
135
  if (!moduleMap.has(tag))
137
136
  return;
138
- debug(`update module "${tag}"`);
139
137
  const instance = moduleMap.get(tag);
140
- if (_optionalChain([instance, 'optionalAccess', _5 => _5[_chunkLZAU5FUSjs.UNMOUNT_SYMBOL]])) {
138
+ if (_optionalChain([instance, 'optionalAccess', _3 => _3[_chunkLZAU5FUSjs.UNMOUNT_SYMBOL]])) {
141
139
  for (const cb of instance[_chunkLZAU5FUSjs.UNMOUNT_SYMBOL])
142
140
  await cb();
143
141
  }
142
+ debug(`del module "${tag}"`);
144
143
  _chunkLZAU5FUSjs.log.call(void 0, `Module ${_picocolors2.default.yellow(`[${tag}]`)} unmount`);
145
144
  moduleMap.delete(tag);
146
145
  constructorMap.delete(tag);
@@ -148,21 +147,28 @@ async function Factory(Modules, opts = {}) {
148
147
  if (meta[i].data.tag === tag)
149
148
  meta.splice(i, 1);
150
149
  }
150
+ return instance;
151
+ }
152
+ _chunkLZAU5FUSjs.__name.call(void 0, del, "del");
153
+ async function add(Module) {
154
+ const tag = _optionalChain([Module, 'access', _4 => _4.prototype, 'optionalAccess', _5 => _5.__TAG__]) || Module.name;
155
+ const oldInstance = await del(tag);
151
156
  const { instance: newModule } = await buildNestModule(Module);
152
- if (moduleGraph.has(tag)) {
157
+ debug(`add module "${tag}"`);
158
+ if (oldInstance && moduleGraph.has(tag)) {
153
159
  [
154
160
  ...moduleGraph.get(tag)
155
161
  ].forEach((tag2) => {
156
162
  const module = moduleMap.get(tag2);
157
163
  for (const key in module) {
158
- if (module[key] === instance)
164
+ if (module[key] === oldInstance)
159
165
  module[key] = newModule;
160
166
  }
161
167
  });
162
168
  }
163
169
  moduleMap.set(tag, newModule);
164
170
  }
165
- _chunkLZAU5FUSjs.__name.call(void 0, update, "update");
171
+ _chunkLZAU5FUSjs.__name.call(void 0, add, "add");
166
172
  async function buildNestModule(Module) {
167
173
  const paramtypes = getParamTypes(Module);
168
174
  let instance;
@@ -223,7 +229,7 @@ async function Factory(Modules, opts = {}) {
223
229
  const module = await Promise.resolve().then(() => require(file));
224
230
  for (const i in module) {
225
231
  if (_phecdacore.isPhecda.call(void 0, module[i]))
226
- await update(module[i]);
232
+ await add(module[i]);
227
233
  }
228
234
  }
229
235
  writeCode();
@@ -233,7 +239,8 @@ async function Factory(Modules, opts = {}) {
233
239
  moduleMap,
234
240
  meta,
235
241
  constructorMap,
236
- update
242
+ add,
243
+ del
237
244
  };
238
245
  }
239
246
  _chunkLZAU5FUSjs.__name.call(void 0, Factory, "Factory");
@@ -325,4 +332,4 @@ _chunkLZAU5FUSjs.__name.call(void 0, initState, "initState");
325
332
 
326
333
 
327
334
  exports.Meta = Meta; exports.generateRPCCode = generateRPCCode; exports.generateHTTPCode = generateHTTPCode; exports.Injectable = Injectable; exports.emitter = emitter; exports.Factory = Factory;
328
- //# sourceMappingURL=chunk-W64EUEDU.js.map
335
+ //# sourceMappingURL=chunk-R3N4HR7U.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/meta.ts","../src/compiler/rpc.ts","../src/compiler/http.ts","../src/core.ts"],"names":["Meta","data","handlers","paramsType","constructor","Compiler","classMap","getContent","content","name","Object","values","reduce","p","c","addMethod","args","rpc","method","tag","type","isEvent","generateRPCCode","meta","compiler","i","http","params","url","route","replace","_","js","key","generateHTTPCode","fs","EventEmitter","Empty","getExposeKey","getHandler","getState","injectProperty","isPhecda","registerAsync","Debug","pc","Injectable","target","debug","emitter","Factory","Modules","opts","moduleMap","Map","constructorMap","constructorSet","WeakSet","moduleGraph","eventName","instance","options","fn","bind","v","UNMOUNT_SYMBOL","push","off","once","on","del","has","get","cb","log","yellow","delete","length","splice","add","Module","prototype","__TAG__","oldInstance","newModule","buildNestModule","forEach","module","set","paramtypes","getParamTypes","Error","undefined","paramtypesInstances","sub","subTag","Set","getMetaFromInstance","writeCode","promises","writeFile","map","item","IS_DEV","globalThis","__PS_HMR__","files","file","vars","filter","baseState","initState","state","unshift","index","define","header","plugins","guards","interceptors","Reflect","getMetadata"],"mappings":";;;;;;;;AAEO,IAAMA,OAAN,MAAMA;EACQC;EAAqBC;EAA8BC;EAAtEC,YAAmBH,MAAqBC,UAA8BC,YAAmB;gBAAtEF;oBAAqBC;sBAA8BC;EAEtE;AACF;AAJaH;;;ACAb,IAAMK,WAAN,6BAAMA,UAAAA;EACJC,WAAsD,CAAC;EACvDF,cAAc;EAAE;EAEhBG,aAAa;AACX,QAAIC,UAAU;AAEd,eAAWC,QAAQ,KAAKH,UAAU;AAChCE,iBAAW;uBACMC;cACTC,OAAOC,OAAO,KAAKL,SAASG,KAAK,EAAEG,OAAO,CAACC,GAAGC,MAAMD,IAAIC,CAAAA;;IAElE;A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AAA;AAClB,MAAI,CAACL,MAAMM;AACTN,UAAMM,SAAS,CAAA;AACjB,MAAI,CAACN,MAAMO;AACTP,UAAMO,eAAe,CAAA;AACzB;AAXSR","sourcesContent":["import type { P } from './types'\n\nexport class Meta {\n constructor(public data: P.Meta, public handlers: P.Handler[], public paramsType: any[]) {\n\n }\n}\n","import type { P } from '../types'\n\nclass Compiler {\n classMap: Record<string, { [key: string]: string }> = {}\n constructor() { }\n\n getContent() {\n let content = ''\n\n for (const name in this.classMap) {\n content += `\n export class ${name}{\n ${Object.values(this.classMap[name]).reduce((p, c) => p + c)}\n }`\n }\n return content\n }\n\n addMethod(args: P.Meta) {\n const {\n rpc, name, method, tag,\n } = args\n if (!rpc || !rpc.type)\n return\n if (!this.classMap[name])\n this.classMap[name] = {}\n this.classMap[name][method] = `\n ${method}(){\n return {tag:'${tag}-${method}',isEvent:${!!rpc.isEvent},rpc:[${rpc.type.reduce((p, c) => {\n return `${p}\"${c}\",`\n }, '')}]}\n\n }\n `\n }\n}\n\nexport function generateRPCCode(meta: P.Meta[]) {\n const compiler = new Compiler()\n\n for (const i of meta)\n compiler.addMethod(i)\n return compiler.getContent()\n}\n","import type { P } from '../types'\n\nclass Compiler {\n classMap: Record<string, { [key: string]: string }> = {}\n constructor() { }\n\n getContent() {\n let content = ''\n\n for (const name in this.classMap) {\n content += `\n export class ${name}{\n ${Object.values(this.classMap[name]).reduce((p, c) => p + c)}\n }`\n }\n return content\n }\n\n // createRequest() {\n // let content = 'import {useC} from \\'phecda-server\\'\\n'\n // for (const name in this.classMap)\n // content += `export const {${Object.keys(this.classMap[name]).join(',')}}=useC(${name})\\n`\n // return content\n // }\n\n addMethod(args: P.Meta) {\n const {\n http, name, method, params, tag,\n } = args\n if (!http)\n return\n const url = http.route.replace(/\\/\\:([^\\/]*)/g, (_, js) => `/{{${js}}}`)\n if (!this.classMap[name])\n this.classMap[name] = {}\n this.classMap[name][method] = `\n ${method}(...args){\nconst ret={tag:\"${tag}-${method}\",body:{},headers:{},query:{},params:{},method:\"${http.type}\",url:\"${url}\",args}\n\n${params.reduce((p, c, i) => `${p}ret.${c.type}${c.key ? `['${c.key}']` : ''}=args[${i}]\\n${c.type === 'params' ? `ret.url=ret.url.replace('{{${c.key}}}',args[${i}])` : ''}\\n`, '')}\nreturn ret\n }\n `\n }\n}\n\nexport function generateHTTPCode(meta: P.Meta[]) {\n const compiler = new Compiler()\n\n for (const i of meta)\n compiler.addMethod(i)\n return compiler.getContent()\n}\n","import 'reflect-metadata'\nimport fs from 'fs'\nimport EventEmitter from 'node:events'\nimport type { Phecda } from 'phecda-core'\nimport { Empty, getExposeKey, getHandler, getState, injectProperty, isPhecda, registerAsync } from 'phecda-core'\nimport Debug from 'debug'\nimport pc from 'picocolors'\nimport type { Construct, Emitter, P } from './types'\nimport { Meta } from './meta'\nimport { log } from './utils'\nimport { IS_DEV, UNMOUNT_SYMBOL } from './common'\nimport { generateHTTPCode, generateRPCCode } from './compiler'\nexport function Injectable() {\n return (target: any) => Empty(target)\n}\nconst debug = Debug('phecda-server')\n// TODO: support both emitter types and origin emitter type in future\nexport const emitter: Emitter = new EventEmitter() as any\n\nexport async function Factory(Modules: (new (...args: any) => any)[], opts: {\n\n // HTTP generate code path\n http?: string\n // rpc generate code path\n rpc?: string\n} = {}) {\n const moduleMap = new Map<string, InstanceType<Construct>>()\n const meta: Meta[] = []\n const constructorMap = new Map()\n\n // only work for warn\n const constructorSet = new WeakSet()\n const moduleGraph = new Map<string, Set<string>>()\n const { http, rpc } = opts\n injectProperty('watcher', ({ eventName, instance, key, options }: { eventName: string; instance: any; key: string; options?: { once: boolean } }) => {\n const fn = typeof instance[key] === 'function' ? instance[key].bind(instance) : (v: any) => instance[key] = v\n\n // work for hmr\n if (!instance[UNMOUNT_SYMBOL])\n instance[UNMOUNT_SYMBOL] = []\n\n instance[UNMOUNT_SYMBOL].push(() => {\n (emitter as any).off(eventName, fn)\n })\n\n if (options?.once)\n (emitter as any).once(eventName, fn)\n\n else\n (emitter as any).on(eventName, fn)\n })\n\n // only remove module in moduleMap(won't remove indiect module)\n async function del(tag: string) {\n if (!moduleMap.has(tag))\n return\n\n const instance = moduleMap.get(tag)\n\n if (instance?.[UNMOUNT_SYMBOL]) {\n for (const cb of instance[UNMOUNT_SYMBOL])\n await cb()\n }\n\n debug(`del module \"${tag}\"`)\n\n log(`Module ${pc.yellow(`[${tag}]`)} unmount`)\n moduleMap.delete(tag)\n constructorMap.delete(tag)\n for (let i = meta.length - 1; i >= 0; i--) {\n if (meta[i].data.tag === tag)\n meta.splice(i, 1)\n }\n\n return instance\n }\n\n async function add(Module: Construct) {\n const tag = Module.prototype?.__TAG__ || Module.name\n const oldInstance = await del(tag)\n\n const { instance: newModule } = await buildNestModule(Module)\n\n debug(`add module \"${tag}\"`)\n\n if (oldInstance && moduleGraph.has(tag)) {\n [...moduleGraph.get(tag)!].forEach((tag) => {\n const module = moduleMap.get(tag)\n for (const key in module) {\n if (module[key] === oldInstance)\n module[key] = newModule\n }\n })\n }\n\n moduleMap.set(tag, newModule)\n }\n async function buildNestModule(Module: Construct) {\n const paramtypes = getParamTypes(Module) as Construct[]\n let instance: InstanceType<Construct>\n const tag = Module.prototype?.__TAG__ || Module.name\n if (moduleMap.has(tag)) {\n instance = moduleMap.get(tag)\n if (!instance)\n throw new Error(`exist Circular-Dependency or Multiple modules with the same name/tag [tag] ${tag}--[module] ${Module}`)\n\n if (constructorMap.get(tag) !== Module && !constructorSet.has(Module)) {\n constructorSet.add(Module)// a module will only warn once\n log(`Synonym module: Module taged \"${tag}\" has been loaded before, so phecda-server won't load Module \"${Module.name}\"`, 'warn')\n }\n return { instance, tag }\n }\n moduleMap.set(tag, undefined)\n if (paramtypes) {\n const paramtypesInstances = [] as any[]\n for (const i in paramtypes) {\n const { instance: sub, tag: subTag } = await buildNestModule(paramtypes[i])\n paramtypesInstances[i] = sub\n if (!moduleGraph.has(subTag))\n moduleGraph.set(subTag, new Set())\n moduleGraph.get(subTag)!.add(tag)\n }\n instance = new Module(...paramtypesInstances)\n }\n else {\n instance = new Module()\n }\n meta.push(...getMetaFromInstance(instance, tag, Module.name))\n await registerAsync(instance)\n moduleMap.set(tag, instance)\n log(`Module ${pc.yellow(`[${tag}]`)} mount\"`)\n constructorMap.set(tag, Module)\n return { instance, tag }\n }\n\n for (const Module of Modules)\n await buildNestModule(Module)\n\n function writeCode() {\n debug('write code')\n\n http && fs.promises.writeFile(http, generateHTTPCode(meta.map(item => item.data)))\n rpc && fs.promises.writeFile(rpc, generateRPCCode(meta.map(item => item.data)))\n }\n\n writeCode()\n if (IS_DEV) {\n if (!globalThis.__PS_HMR__)\n globalThis.__PS_HMR__ = []\n\n globalThis.__PS_HMR__?.push(async (files: string[]) => {\n for (const file of files) {\n debug(`reload file ${file}`)\n const module = await import(file)\n for (const i in module) {\n if (isPhecda(module[i]))\n await add(module[i])\n }\n }\n writeCode()\n })\n }\n\n return {\n moduleMap,\n meta,\n constructorMap,\n add,\n del,\n }\n}\n\nfunction getMetaFromInstance(instance: Phecda, tag: string, name: string) {\n const vars = getExposeKey(instance).filter(item => item !== '__CLASS')\n const baseState = (getState(instance, '__CLASS') || {}) as P.Meta\n initState(baseState)\n\n return vars.map((i) => {\n const meta = {} as P.Meta\n const state = (getState(instance, i) || {}) as P.Meta\n initState(state)\n if (state.http) {\n meta.http = {\n route: (baseState.http?.route || '') + (state.http.route),\n type: state.http.type,\n }\n }\n if (baseState.rpc)\n meta.rpc = baseState.rpc\n if (state.rpc) {\n meta.rpc = {\n ...meta.rpc,\n ...state.rpc,\n }\n }\n\n meta.name = name\n meta.tag = tag\n meta.method = i as string\n const params = [] as any[]\n for (const i of state.params || []) {\n params.unshift(i)\n if (i.index === 0)\n break\n }\n\n meta.params = params\n meta.define = { ...baseState.define, ...state.define }\n meta.header = { ...baseState.header, ...state.header }\n meta.plugins = [...new Set([...baseState.plugins, ...state.plugins])]\n meta.guards = [...new Set([...baseState.guards, ...state.guards])]\n meta.interceptors = [...new Set([...baseState.interceptors, ...state.interceptors])]\n\n return new Meta(meta as unknown as P.Meta, getHandler(instance, i), getParamTypes(instance, i as string) || [])\n })\n}\n\nfunction getParamTypes(Module: any, key?: string | symbol) {\n return Reflect.getMetadata('design:paramtypes', Module, key!)\n}\n\nfunction initState(state: any) {\n if (!state.define)\n state.define = {}\n if (!state.header)\n state.header = {}\n if (!state.plugins)\n state.plugins = []\n if (!state.guards)\n state.guards = []\n if (!state.interceptors)\n state.interceptors = []\n}\n"]}
@@ -16,7 +16,8 @@ declare function Factory(Modules: (new (...args: any) => any)[], opts?: {
16
16
  moduleMap: Map<string, any>;
17
17
  meta: Meta[];
18
18
  constructorMap: Map<any, any>;
19
- update: (Module: Construct) => Promise<void>;
19
+ add: (Module: Construct) => Promise<void>;
20
+ del: (tag: string) => Promise<any>;
20
21
  }>;
21
22
 
22
23
  export { Factory as F, Injectable as I, Meta as M, emitter as e };
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { E as Exception, P, a as Emitter } from './types-ead02c5d.js';
2
2
  export { B as BaseError, C as Construct, M as MergeType, R as RequestType, S as ServerErr, T as ToInstance } from './types-ead02c5d.js';
3
- import { M as Meta } from './core-7c2dd0c7.js';
4
- export { F as Factory, I as Injectable, e as emitter } from './core-7c2dd0c7.js';
3
+ import { M as Meta } from './core-ba036c2b.js';
4
+ export { F as Factory, I as Injectable, e as emitter } from './core-ba036c2b.js';
5
5
  export * from 'phecda-core';
6
6
 
7
7
  declare class Histroy {
package/dist/index.js CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
 
7
7
 
8
- var _chunkW64EUEDUjs = require('./chunk-W64EUEDU.js');
8
+ var _chunkR3N4HR7Ujs = require('./chunk-R3N4HR7U.js');
9
9
 
10
10
 
11
11
  var _chunkFAHHXHL5js = require('./chunk-FAHHXHL5.js');
@@ -276,7 +276,7 @@ var __decorate = function(decorators, target, key, desc) {
276
276
  return c > 3 && r && Object.defineProperty(target, key, r), r;
277
277
  };
278
278
  var Dev = /* @__PURE__ */ _chunkLZAU5FUSjs.__name.call(void 0, (_class =class Dev2 {constructor() { _class.prototype.__init.call(this);_class.prototype.__init2.call(this); }
279
- __init() {this.emitter = exports.emitter = _chunkW64EUEDUjs.emitter}
279
+ __init() {this.emitter = exports.emitter = _chunkR3N4HR7Ujs.emitter}
280
280
  __init2() {this[_chunkLZAU5FUSjs.UNMOUNT_SYMBOL] = []}
281
281
  onUnmount(cb) {
282
282
  this[_chunkLZAU5FUSjs.UNMOUNT_SYMBOL].push(cb);
@@ -419,5 +419,5 @@ _chunkLZAU5FUSjs.__name.call(void 0, PPlugin, "PPlugin");
419
419
 
420
420
 
421
421
 
422
- exports.APP_SYMBOL = _chunkLZAU5FUSjs.APP_SYMBOL; exports.Arg = Arg; exports.BadGatewayException = _chunkYU7ROHTOjs.BadGatewayException; exports.BadRequestException = _chunkYU7ROHTOjs.BadRequestException; exports.BaseParam = BaseParam; exports.Body = Body; exports.ConflictException = _chunkYU7ROHTOjs.ConflictException; exports.Context = _chunkYU7ROHTOjs.Context; exports.Controller = Controller; exports.Define = Define; exports.Delete = Delete; exports.Dev = Dev; exports.Event = Event; exports.Exception = _chunkYU7ROHTOjs.Exception; exports.Factory = _chunkW64EUEDUjs.Factory; exports.ForbiddenException = _chunkYU7ROHTOjs.ForbiddenException; exports.FrameworkException = _chunkYU7ROHTOjs.FrameworkException; exports.Get = Get; exports.Guard = Guard; exports.Head = Head; exports.Header = Header; exports.IS_DEV = _chunkLZAU5FUSjs.IS_DEV; exports.IS_STRICT = _chunkLZAU5FUSjs.IS_STRICT; exports.Injectable = _chunkW64EUEDUjs.Injectable; exports.Interceptor = Interceptor; exports.InvalidInputException = _chunkYU7ROHTOjs.InvalidInputException; exports.MERGE_SYMBOL = _chunkLZAU5FUSjs.MERGE_SYMBOL; exports.META_SYMBOL = _chunkLZAU5FUSjs.META_SYMBOL; exports.MODULE_SYMBOL = _chunkLZAU5FUSjs.MODULE_SYMBOL; exports.Meta = _chunkW64EUEDUjs.Meta; exports.NotFoundException = _chunkYU7ROHTOjs.NotFoundException; exports.PFilter = PFilter; exports.PGuard = PGuard; exports.PInterceptor = PInterceptor; exports.PPipe = PPipe; exports.PPlugin = PPlugin; exports.Param = Param; exports.Patch = Patch; exports.PayloadLargeException = _chunkYU7ROHTOjs.PayloadLargeException; exports.Pipe = Pipe; exports.Plugin = Plugin; exports.Post = Post; exports.Put = Put; exports.Query = Query; exports.Route = Route; exports.Rpc = Rpc; exports.ServiceUnavailableException = _chunkYU7ROHTOjs.ServiceUnavailableException; exports.TimeoutException = _chunkYU7ROHTOjs.TimeoutException; exports.UNMOUNT_SYMBOL = _chunkLZAU5FUSjs.UNMOUNT_SYMBOL; exports.UnauthorizedException = _chunkYU7ROHTOjs.UnauthorizedException; exports.UndefinedException = _chunkYU7ROHTOjs.UndefinedException; exports.UnsupportedMediaTypeException = _chunkYU7ROHTOjs.UnsupportedMediaTypeException; exports.ValidateException = _chunkYU7ROHTOjs.ValidateException; exports.addGuard = _chunkYU7ROHTOjs.addGuard; exports.addInterceptor = _chunkYU7ROHTOjs.addInterceptor; exports.addPipe = _chunkYU7ROHTOjs.addPipe; exports.addPlugin = _chunkYU7ROHTOjs.addPlugin; exports.defaultPipe = _chunkYU7ROHTOjs.defaultPipe; exports.emitter = _chunkW64EUEDUjs.emitter; exports.generateHTTPCode = _chunkW64EUEDUjs.generateHTTPCode; exports.generateRPCCode = _chunkW64EUEDUjs.generateRPCCode; exports.guardRecord = _chunkYU7ROHTOjs.guardRecord; exports.isAopDepInject = _chunkYU7ROHTOjs.isAopDepInject; exports.resolveDep = _chunkFAHHXHL5js.resolveDep; exports.setFilter = _chunkYU7ROHTOjs.setFilter;
422
+ exports.APP_SYMBOL = _chunkLZAU5FUSjs.APP_SYMBOL; exports.Arg = Arg; exports.BadGatewayException = _chunkYU7ROHTOjs.BadGatewayException; exports.BadRequestException = _chunkYU7ROHTOjs.BadRequestException; exports.BaseParam = BaseParam; exports.Body = Body; exports.ConflictException = _chunkYU7ROHTOjs.ConflictException; exports.Context = _chunkYU7ROHTOjs.Context; exports.Controller = Controller; exports.Define = Define; exports.Delete = Delete; exports.Dev = Dev; exports.Event = Event; exports.Exception = _chunkYU7ROHTOjs.Exception; exports.Factory = _chunkR3N4HR7Ujs.Factory; exports.ForbiddenException = _chunkYU7ROHTOjs.ForbiddenException; exports.FrameworkException = _chunkYU7ROHTOjs.FrameworkException; exports.Get = Get; exports.Guard = Guard; exports.Head = Head; exports.Header = Header; exports.IS_DEV = _chunkLZAU5FUSjs.IS_DEV; exports.IS_STRICT = _chunkLZAU5FUSjs.IS_STRICT; exports.Injectable = _chunkR3N4HR7Ujs.Injectable; exports.Interceptor = Interceptor; exports.InvalidInputException = _chunkYU7ROHTOjs.InvalidInputException; exports.MERGE_SYMBOL = _chunkLZAU5FUSjs.MERGE_SYMBOL; exports.META_SYMBOL = _chunkLZAU5FUSjs.META_SYMBOL; exports.MODULE_SYMBOL = _chunkLZAU5FUSjs.MODULE_SYMBOL; exports.Meta = _chunkR3N4HR7Ujs.Meta; exports.NotFoundException = _chunkYU7ROHTOjs.NotFoundException; exports.PFilter = PFilter; exports.PGuard = PGuard; exports.PInterceptor = PInterceptor; exports.PPipe = PPipe; exports.PPlugin = PPlugin; exports.Param = Param; exports.Patch = Patch; exports.PayloadLargeException = _chunkYU7ROHTOjs.PayloadLargeException; exports.Pipe = Pipe; exports.Plugin = Plugin; exports.Post = Post; exports.Put = Put; exports.Query = Query; exports.Route = Route; exports.Rpc = Rpc; exports.ServiceUnavailableException = _chunkYU7ROHTOjs.ServiceUnavailableException; exports.TimeoutException = _chunkYU7ROHTOjs.TimeoutException; exports.UNMOUNT_SYMBOL = _chunkLZAU5FUSjs.UNMOUNT_SYMBOL; exports.UnauthorizedException = _chunkYU7ROHTOjs.UnauthorizedException; exports.UndefinedException = _chunkYU7ROHTOjs.UndefinedException; exports.UnsupportedMediaTypeException = _chunkYU7ROHTOjs.UnsupportedMediaTypeException; exports.ValidateException = _chunkYU7ROHTOjs.ValidateException; exports.addGuard = _chunkYU7ROHTOjs.addGuard; exports.addInterceptor = _chunkYU7ROHTOjs.addInterceptor; exports.addPipe = _chunkYU7ROHTOjs.addPipe; exports.addPlugin = _chunkYU7ROHTOjs.addPlugin; exports.defaultPipe = _chunkYU7ROHTOjs.defaultPipe; exports.emitter = _chunkR3N4HR7Ujs.emitter; exports.generateHTTPCode = _chunkR3N4HR7Ujs.generateHTTPCode; exports.generateRPCCode = _chunkR3N4HR7Ujs.generateRPCCode; exports.guardRecord = _chunkYU7ROHTOjs.guardRecord; exports.isAopDepInject = _chunkYU7ROHTOjs.isAopDepInject; exports.resolveDep = _chunkFAHHXHL5js.resolveDep; exports.setFilter = _chunkYU7ROHTOjs.setFilter;
423
423
  //# sourceMappingURL=index.js.map
package/dist/index.mjs CHANGED
@@ -5,7 +5,7 @@ import {
5
5
  emitter,
6
6
  generateHTTPCode,
7
7
  generateRPCCode
8
- } from "./chunk-JUXEUWOU.mjs";
8
+ } from "./chunk-NFAA3WFR.mjs";
9
9
  import {
10
10
  resolveDep
11
11
  } from "./chunk-JQPX546Z.mjs";
@@ -1,5 +1,5 @@
1
1
  import amqplib from 'amqplib';
2
- import { M as Meta, F as Factory } from '../../core-7c2dd0c7.js';
2
+ import { M as Meta, F as Factory } from '../../core-ba036c2b.js';
3
3
  import { T as ToInstance } from '../../types-ead02c5d.js';
4
4
  import 'phecda-core';
5
5
 
@@ -1,5 +1,5 @@
1
1
  import Redis from 'ioredis';
2
- import { M as Meta, F as Factory } from '../../core-7c2dd0c7.js';
2
+ import { M as Meta, F as Factory } from '../../core-ba036c2b.js';
3
3
  import { T as ToInstance } from '../../types-ead02c5d.js';
4
4
  import 'phecda-core';
5
5
 
@@ -1,5 +1,5 @@
1
1
  import { Router } from 'express';
2
- import { M as Meta, F as Factory } from '../../core-7c2dd0c7.js';
2
+ import { M as Meta, F as Factory } from '../../core-ba036c2b.js';
3
3
  import '../../types-ead02c5d.js';
4
4
  import 'phecda-core';
5
5
 
@@ -1,5 +1,5 @@
1
1
  import { FastifyRequest, FastifyReply, FastifyPluginCallback } from 'fastify';
2
- import { M as Meta, F as Factory } from '../../core-7c2dd0c7.js';
2
+ import { M as Meta, F as Factory } from '../../core-ba036c2b.js';
3
3
  import '../../types-ead02c5d.js';
4
4
  import 'phecda-core';
5
5
 
@@ -1,5 +1,5 @@
1
1
  import { Router } from 'h3';
2
- import { M as Meta, F as Factory } from '../../core-7c2dd0c7.js';
2
+ import { M as Meta, F as Factory } from '../../core-ba036c2b.js';
3
3
  import '../../types-ead02c5d.js';
4
4
  import 'phecda-core';
5
5
 
@@ -1,6 +1,6 @@
1
1
  import Router, { RouterParamContext } from '@koa/router';
2
2
  import { DefaultContext, DefaultState } from 'koa';
3
- import { M as Meta, F as Factory } from '../../core-7c2dd0c7.js';
3
+ import { M as Meta, F as Factory } from '../../core-ba036c2b.js';
4
4
  import '../../types-ead02c5d.js';
5
5
  import 'phecda-core';
6
6
 
package/dist/test.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true}); 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; }
2
2
 
3
- var _chunkW64EUEDUjs = require('./chunk-W64EUEDU.js');
3
+ var _chunkR3N4HR7Ujs = require('./chunk-R3N4HR7U.js');
4
4
 
5
5
 
6
6
 
@@ -8,7 +8,7 @@ var _chunkLZAU5FUSjs = require('./chunk-LZAU5FUS.js');
8
8
 
9
9
  // src/test.ts
10
10
  async function TestFactory(...Modules) {
11
- const { moduleMap, constructorMap } = await _chunkW64EUEDUjs.Factory.call(void 0, Modules);
11
+ const { moduleMap, constructorMap } = await _chunkR3N4HR7Ujs.Factory.call(void 0, Modules);
12
12
  return {
13
13
  get(Module) {
14
14
  const tag = _optionalChain([Module, 'access', _ => _.prototype, 'optionalAccess', _2 => _2.__TAG__]) || Module.name;
package/dist/test.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  Factory
3
- } from "./chunk-JUXEUWOU.mjs";
3
+ } from "./chunk-NFAA3WFR.mjs";
4
4
  import {
5
5
  APP_SYMBOL,
6
6
  __name
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "phecda-server",
3
- "version": "4.0.4",
3
+ "version": "4.0.5",
4
4
  "description": "provide express middlewares, `nestjs` format",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -1,7 +1,4 @@
1
- import { createRequire } from 'node:module'
2
- const require = createRequire(import.meta.url)
3
- const { transformSync } = require('@swc-node/core')
4
-
1
+ import { transform } from '@swc-node/core'
5
2
  const injectInlineSourceMap = ({ code, map }) => {
6
3
  if (map) {
7
4
  const base64Map = Buffer.from(map, 'utf8').toString('base64')
@@ -11,13 +8,12 @@ const injectInlineSourceMap = ({ code, map }) => {
11
8
  return code
12
9
  }
13
10
 
14
- export function compile(sourcecode, filename) {
11
+ export async function compile(sourcecode, filename) {
15
12
  if (filename.endsWith('.d.ts'))
16
13
  return ''
17
14
 
18
- const { code, map } = transformSync(sourcecode, filename, {
15
+ const { code, map } = await transform(sourcecode, filename, {
19
16
  sourcemap: true,
20
-
21
17
  module: 'es6',
22
18
  emitDecoratorMetadata: true,
23
19
  experimentalDecorators: true,
@@ -51,7 +51,6 @@ export const resolve = async (specifier, context, nextResolve) => {
51
51
  shortCircuit: true,
52
52
  }
53
53
  }
54
-
55
54
  if (/^file:\/\/\//.test(specifier) && extname(specifier) === '.ts') {
56
55
  const url = addUrlToGraph(specifier, context.parentURL.split('?')[0])
57
56
 
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/meta.ts","../src/compiler/rpc.ts","../src/compiler/http.ts","../src/core.ts"],"sourcesContent":["import type { P } from './types'\n\nexport class Meta {\n constructor(public data: P.Meta, public handlers: P.Handler[], public paramsType: any[]) {\n\n }\n}\n","import type { P } from '../types'\n\nclass Compiler {\n classMap: Record<string, { [key: string]: string }> = {}\n constructor() { }\n\n getContent() {\n let content = ''\n\n for (const name in this.classMap) {\n content += `\n export class ${name}{\n ${Object.values(this.classMap[name]).reduce((p, c) => p + c)}\n }`\n }\n return content\n }\n\n addMethod(args: P.Meta) {\n const {\n rpc, name, method, tag,\n } = args\n if (!rpc || !rpc.type)\n return\n if (!this.classMap[name])\n this.classMap[name] = {}\n this.classMap[name][method] = `\n ${method}(){\n return {tag:'${tag}-${method}',isEvent:${!!rpc.isEvent},rpc:[${rpc.type.reduce((p, c) => {\n return `${p}\"${c}\",`\n }, '')}]}\n\n }\n `\n }\n}\n\nexport function generateRPCCode(meta: P.Meta[]) {\n const compiler = new Compiler()\n\n for (const i of meta)\n compiler.addMethod(i)\n return compiler.getContent()\n}\n","import type { P } from '../types'\n\nclass Compiler {\n classMap: Record<string, { [key: string]: string }> = {}\n constructor() { }\n\n getContent() {\n let content = ''\n\n for (const name in this.classMap) {\n content += `\n export class ${name}{\n ${Object.values(this.classMap[name]).reduce((p, c) => p + c)}\n }`\n }\n return content\n }\n\n // createRequest() {\n // let content = 'import {useC} from \\'phecda-server\\'\\n'\n // for (const name in this.classMap)\n // content += `export const {${Object.keys(this.classMap[name]).join(',')}}=useC(${name})\\n`\n // return content\n // }\n\n addMethod(args: P.Meta) {\n const {\n http, name, method, params, tag,\n } = args\n if (!http)\n return\n const url = http.route.replace(/\\/\\:([^\\/]*)/g, (_, js) => `/{{${js}}}`)\n if (!this.classMap[name])\n this.classMap[name] = {}\n this.classMap[name][method] = `\n ${method}(...args){\nconst ret={tag:\"${tag}-${method}\",body:{},headers:{},query:{},params:{},method:\"${http.type}\",url:\"${url}\",args}\n\n${params.reduce((p, c, i) => `${p}ret.${c.type}${c.key ? `['${c.key}']` : ''}=args[${i}]\\n${c.type === 'params' ? `ret.url=ret.url.replace('{{${c.key}}}',args[${i}])` : ''}\\n`, '')}\nreturn ret\n }\n `\n }\n}\n\nexport function generateHTTPCode(meta: P.Meta[]) {\n const compiler = new Compiler()\n\n for (const i of meta)\n compiler.addMethod(i)\n return compiler.getContent()\n}\n","import 'reflect-metadata'\nimport fs from 'fs'\nimport EventEmitter from 'node:events'\nimport type { Phecda } from 'phecda-core'\nimport { Empty, getExposeKey, getHandler, getState, injectProperty, isPhecda, registerAsync } from 'phecda-core'\nimport Debug from 'debug'\nimport pc from 'picocolors'\nimport type { Construct, Emitter, P } from './types'\nimport { Meta } from './meta'\nimport { log } from './utils'\nimport { IS_DEV, UNMOUNT_SYMBOL } from './common'\nimport { generateHTTPCode, generateRPCCode } from './compiler'\nexport function Injectable() {\n return (target: any) => Empty(target)\n}\nconst debug = Debug('phecda-server')\n// TODO: support both emitter types and origin emitter type in future\nexport const emitter: Emitter = new EventEmitter() as any\n\nexport async function Factory(Modules: (new (...args: any) => any)[], opts: {\n\n // HTTP generate code path\n http?: string\n // rpc generate code path\n rpc?: string\n} = {}) {\n const moduleMap = new Map<string, InstanceType<Construct>>()\n const meta: Meta[] = []\n const constructorMap = new Map()\n\n // only work for warn\n const constructorSet = new WeakSet()\n const moduleGraph = new Map<string, Set<string>>()\n const { http, rpc } = opts\n injectProperty('watcher', ({ eventName, instance, key, options }: { eventName: string; instance: any; key: string; options?: { once: boolean } }) => {\n const fn = typeof instance[key] === 'function' ? instance[key].bind(instance) : (v: any) => instance[key] = v\n\n // work for hmr\n if (!instance[UNMOUNT_SYMBOL])\n instance[UNMOUNT_SYMBOL] = []\n\n instance[UNMOUNT_SYMBOL].push(() => {\n (emitter as any).off(eventName, fn)\n })\n\n if (options?.once)\n (emitter as any).once(eventName, fn)\n\n else\n (emitter as any).on(eventName, fn)\n })\n\n async function update(Module: Construct) {\n const tag = Module.prototype?.__TAG__ || Module.name\n if (!moduleMap.has(tag))\n return\n debug(`update module \"${tag}\"`)\n\n const instance = moduleMap.get(tag)\n\n if (instance?.[UNMOUNT_SYMBOL]) {\n for (const cb of instance[UNMOUNT_SYMBOL])\n await cb()\n }\n log(`Module ${pc.yellow(`[${tag}]`)} unmount`)\n moduleMap.delete(tag)\n constructorMap.delete(tag)\n for (let i = meta.length - 1; i >= 0; i--) {\n if (meta[i].data.tag === tag)\n meta.splice(i, 1)\n }\n\n const { instance: newModule } = await buildNestModule(Module)\n if (moduleGraph.has(tag)) {\n [...moduleGraph.get(tag)!].forEach((tag) => {\n const module = moduleMap.get(tag)\n for (const key in module) {\n if (module[key] === instance)\n module[key] = newModule\n }\n })\n }\n\n moduleMap.set(tag, newModule)\n }\n async function buildNestModule(Module: Construct) {\n const paramtypes = getParamTypes(Module) as Construct[]\n let instance: InstanceType<Construct>\n const tag = Module.prototype?.__TAG__ || Module.name\n if (moduleMap.has(tag)) {\n instance = moduleMap.get(tag)\n if (!instance)\n throw new Error(`exist Circular-Dependency or Multiple modules with the same name/tag [tag] ${tag}--[module] ${Module}`)\n\n if (constructorMap.get(tag) !== Module && !constructorSet.has(Module)) {\n constructorSet.add(Module)// a module will only warn once\n log(`Synonym module: Module taged \"${tag}\" has been loaded before, so phecda-server won't load Module \"${Module.name}\"`, 'warn')\n }\n return { instance, tag }\n }\n moduleMap.set(tag, undefined)\n if (paramtypes) {\n const paramtypesInstances = [] as any[]\n for (const i in paramtypes) {\n const { instance: sub, tag: subTag } = await buildNestModule(paramtypes[i])\n paramtypesInstances[i] = sub\n if (!moduleGraph.has(subTag))\n moduleGraph.set(subTag, new Set())\n moduleGraph.get(subTag)!.add(tag)\n }\n instance = new Module(...paramtypesInstances)\n }\n else {\n instance = new Module()\n }\n meta.push(...getMetaFromInstance(instance, tag, Module.name))\n await registerAsync(instance)\n moduleMap.set(tag, instance)\n log(`Module ${pc.yellow(`[${tag}]`)} mount\"`)\n constructorMap.set(tag, Module)\n return { instance, tag }\n }\n\n for (const Module of Modules)\n await buildNestModule(Module)\n\n function writeCode() {\n debug('write code')\n\n http && fs.promises.writeFile(http, generateHTTPCode(meta.map(item => item.data)))\n rpc && fs.promises.writeFile(rpc, generateRPCCode(meta.map(item => item.data)))\n }\n\n writeCode()\n if (IS_DEV) {\n if (!globalThis.__PS_HMR__)\n globalThis.__PS_HMR__ = []\n\n globalThis.__PS_HMR__?.push(async (files: string[]) => {\n for (const file of files) {\n debug(`reload file ${file}`)\n const module = await import(file)\n for (const i in module) {\n if (isPhecda(module[i]))\n await update(module[i])\n }\n }\n writeCode()\n })\n }\n\n return {\n moduleMap,\n meta,\n constructorMap,\n update,\n }\n}\n\nfunction getMetaFromInstance(instance: Phecda, tag: string, name: string) {\n const vars = getExposeKey(instance).filter(item => item !== '__CLASS')\n const baseState = (getState(instance, '__CLASS') || {}) as P.Meta\n initState(baseState)\n\n return vars.map((i) => {\n const meta = {} as P.Meta\n const state = (getState(instance, i) || {}) as P.Meta\n initState(state)\n if (state.http) {\n meta.http = {\n route: (baseState.http?.route || '') + (state.http.route),\n type: state.http.type,\n }\n }\n if (baseState.rpc)\n meta.rpc = baseState.rpc\n if (state.rpc) {\n meta.rpc = {\n ...meta.rpc,\n ...state.rpc,\n }\n }\n\n meta.name = name\n meta.tag = tag\n meta.method = i as string\n const params = [] as any[]\n for (const i of state.params || []) {\n params.unshift(i)\n if (i.index === 0)\n break\n }\n\n meta.params = params\n meta.define = { ...baseState.define, ...state.define }\n meta.header = { ...baseState.header, ...state.header }\n meta.plugins = [...new Set([...baseState.plugins, ...state.plugins])]\n meta.guards = [...new Set([...baseState.guards, ...state.guards])]\n meta.interceptors = [...new Set([...baseState.interceptors, ...state.interceptors])]\n\n return new Meta(meta as unknown as P.Meta, getHandler(instance, i), getParamTypes(instance, i as string) || [])\n })\n}\n\nfunction getParamTypes(Module: any, key?: string | symbol) {\n return Reflect.getMetadata('design:paramtypes', Module, key!)\n}\n\nfunction initState(state: any) {\n if (!state.define)\n state.define = {}\n if (!state.header)\n state.header = {}\n if (!state.plugins)\n state.plugins = []\n if (!state.guards)\n state.guards = []\n if (!state.interceptors)\n state.interceptors = []\n}\n"],"mappings":";;;;;;;;AAEO,IAAMA,OAAN,MAAMA;EACQC;EAAqBC;EAA8BC;EAAtEC,YAAmBH,MAAqBC,UAA8BC,YAAmB;gBAAtEF;oBAAqBC;sBAA8BC;EAEtE;AACF;AAJaH;;;ACAb,IAAMK,WAAN,6BAAMA,UAAAA;EACJC,WAAsD,CAAC;EACvDC,cAAc;EAAE;EAEhBC,aAAa;AACX,QAAIC,UAAU;AAEd,eAAWC,QAAQ,KAAKJ,UAAU;AAChCG,iBAAW;uBACMC;cACTC,OAAOC,OAAO,KAAKN,SAASI,KAAK,EAAEG,OAAO,CAACC,GAAGC,MAAMD,IAAIC,CAAAA;;IAElE;AACA,WAAON;EACT;EAEAO,UAAUC,MAAc;AACtB,UAAM,EACJC,KAAKR,MAAMS,QAAQC,IAAG,IACpBH;AACJ,QAAI,CAACC,OAAO,CAACA,IAAIG;AACf;AACF,QAAI,CAAC,KAAKf,SAASI;AACjB,WAAKJ,SAASI,QAAQ,CAAC;AACzB,SAAKJ,SAASI,MAAMS,UAAU;MAC5BA;qBACeC,OAAOD,mBAAmB,CAAC,CAACD,IAAII,gBAAgBJ,IAAIG,KAAKR,OAAO,CAACC,GAAGC,MAAM;AACvF,aAAO,GAAGD,KAAKC;IACjB,GAAG,EAAA;;;;EAIP;AACF,GAjCA;AAmCO,SAASQ,gBAAgBC,MAAgB;AAC9C,QAAMC,WAAW,IAAIpB,SAAAA;AAErB,aAAWqB,KAAKF;AACdC,aAAST,UAAUU,CAAAA;AACrB,SAAOD,SAASjB,WAAU;AAC5B;AANgBe;;;ACnChB,IAAMI,YAAN,6BAAMA,UAAAA;EACJC,WAAsD,CAAC;EACvDC,cAAc;EAAE;EAEhBC,aAAa;AACX,QAAIC,UAAU;AAEd,eAAWC,QAAQ,KAAKJ,UAAU;AAChCG,iBAAW;uBACMC;cAC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CAAE;EAChH,CAAA;AACF;AA3CSkB;AA6CT,SAASR,cAActB,QAAaX,KAAuB;AACzD,SAAO6E,QAAQC,YAAY,qBAAqBnE,QAAQX,GAAAA;AAC1D;AAFSiC;AAIT,SAAS6B,UAAUC,OAAY;AAC7B,MAAI,CAACA,MAAMO;AACTP,UAAMO,SAAS,CAAC;AAClB,MAAI,CAACP,MAAMQ;AACTR,UAAMQ,SAAS,CAAC;AAClB,MAAI,CAACR,MAAMS;AACTT,UAAMS,UAAU,CAAA;AAClB,MAAI,CAACT,MAAMU;AACTV,UAAMU,SAAS,CAAA;AACjB,MAAI,CAACV,MAAMW;AACTX,UAAMW,eAAe,CAAA;AACzB;AAXSZ;","names":["Meta","data","handlers","paramsType","constructor","Compiler","classMap","constructor","getContent","content","name","Object","values","reduce","p","c","addMethod","args","rpc","method","tag","type","isEvent","generateRPCCode","meta","compiler","i","Compiler","classMap","constructor","getContent","content","name","Object","values","reduce","p","c","addMethod","args","http","method","params","tag","url","route","replace","_","js","type","i","key","generateHTTPCode","meta","compiler","fs","EventEmitter","Empty","getExposeKey","getHandler","getState","injectProperty","isPhecda","registerAsync","Debug","pc","Injectable","target","Empty","debug","Debug","emitter","EventEmitter","Factory","Modules","opts","moduleMap","Map","meta","constructorMap","constructorSet","WeakSet","moduleGraph","http","rpc","injectProperty","eventName","instance","key","options","fn","bind","v","UNMOUNT_SYMBOL","push","off","once","on","update","Module","tag","prototype","__TAG__","name","has","get","cb","log","pc","yellow","delete","i","length","data","splice","newModule","buildNestModule","forEach","module","set","paramtypes","getParamTypes","Error","add","undefined","paramtypesInstances","sub","subTag","Set","getMetaFromInstance","registerAsync","writeCode","fs","promises","writeFile","generateHTTPCode","map","item","generateRPCCode","IS_DEV","globalThis","__PS_HMR__","files","file","isPhecda","vars","getExposeKey","filter","baseState","getState","initState","state","route","type","method","params","unshift","index","define","header","plugins","guards","interceptors","Meta","getHandler","Reflect","getMetadata"]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/meta.ts","../src/compiler/rpc.ts","../src/compiler/http.ts","../src/core.ts"],"names":["Meta","data","handlers","paramsType","constructor","Compiler","classMap","getContent","content","name","Object","values","reduce","p","c","addMethod","args","rpc","method","tag","type","isEvent","generateRPCCode","meta","compiler","i","http","params","url","route","replace","_","js","key","generateHTTPCode","fs","EventEmitter","Empty","getExposeKey","getHandler","getState","injectProperty","isPhecda","registerAsync","Debug","pc","Injectable","target","debug","emitter","Factory","Modules","opts","moduleMap","Map","constructorMap","constructorSet","WeakSet","moduleGraph","eventName","instance","options","fn","bind","v","UNMOUNT_SYMBOL","push","off","once","on","update","Module","prototype","__TAG__","has","get","cb","log","yellow","delete","length","splice","newModule","buildNestModule","forEach","module","set","paramtypes","getParamTypes","Error","add","undefined","paramtypesInstances","sub","subTag","Set","getMetaFromInstance","writeCode","promises","writeFile","map","item","IS_DEV","globalThis","__PS_HMR__","files","file","vars","filter","baseState","initState","state","unshift","index","define","header","plugins","guards","interceptors","Reflect","getMetadata"],"mappings":";;;;;;;;AAEO,IAAMA,OAAN,MAAMA;EACQC;EAAqBC;EAA8BC;EAAtEC,YAAmBH,MAAqBC,UAA8BC,YAAmB;gBAAtEF;oBAAqBC;sBAA8BC;EAEtE;AACF;AAJaH;;;ACAb,IAAMK,WAAN,6BAAMA,UAAAA;EACJC,WAAsD,CAAC;EACvDF,cAAc;EAAE;EAEhBG,aAAa;AACX,QAAIC,UAAU;AAEd,eAAWC,QAAQ,KAAKH,UAAU;AAChCE,iBAAW;uBACMC;cACTC,OAAOC,OAAO,KAAKL,SAASG,KAAK,EAAEG,OAAO,CAACC,GAAGC,MAAMD,IAAIC,CAAAA;;IAElE;AACA,WAAON;EACT;EAEAO,UAAUC,MAAc;AACtB,UAAM,EACJC,KAAKR,MAAMS,QAAQC,IAAG,IACpBH;AACJ,QAAI,CAACC,OAAO,CAACA,IAAIG;AACf;AACF,QAAI,CAAC,KAAKd,SAASG;AACjB,WAAKH,SAASG,QAAQ,CAAC;AACzB,SAAK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AAQvF,EAAAA;AACf,UAAIA,GAAEwF,UAAU;AACd;IACJ;AAEA1F,SAAKI,SAASA;AACdJ,SAAK2F,SAAS;MAAE,GAAGL,UAAUK;MAAQ,GAAGH,MAAMG;IAAO;AACrD3F,SAAK4F,SAAS;MAAE,GAAGN,UAAUM;MAAQ,GAAGJ,MAAMI;IAAO;AACrD5F,SAAK6F,UAAU;SAAI,oBAAIrB,IAAI;WAAIc,UAAUO;WAAYL,MAAMK;OAAQ;;AACnE7F,SAAK8F,SAAS;SAAI,oBAAItB,IAAI;WAAIc,UAAUQ;WAAWN,MAAMM;OAAO;;AAChE9F,SAAK+F,eAAe;SAAI,oBAAIvB,IAAI;WAAIc,UAAUS;WAAiBP,MAAMO;OAAa;;AAElF,WAAO,IAAItH,KAAKuB,MAA2BgB,WAAWqB,UAAUnC,CAAAA,GAAI+D,cAAc5B,UAAUnC,CAAAA,KAAgB,CAAA,CAAE;EAChH,CAAA;AACF;AA3CSuE;AA6CT,SAASR,cAAcjB,QAAatC,KAAuB;AACzD,SAAOsF,QAAQC,YAAY,qBAAqBjD,QAAQtC,GAAAA;AAC1D;AAFSuD;AAIT,SAASsB,UAAUC,OAAY;AAC7B,MAAI,CAACA,MAAMG;AACTH,UAAMG,SAAS,CAAC;AAClB,MAAI,CAACH,MAAMI;AACTJ,UAAMI,SAAS,CAAC;AAClB,MAAI,CAACJ,MAAMK;AACTL,UAAMK,UAAU,CAAA;AAClB,MAAI,CAACL,MAAMM;AACTN,UAAMM,SAAS,CAAA;AACjB,MAAI,CAACN,MAAMO;AACTP,UAAMO,eAAe,CAAA;AACzB;AAXSR","sourcesContent":["import type { P } from './types'\n\nexport class Meta {\n constructor(public data: P.Meta, public handlers: P.Handler[], public paramsType: any[]) {\n\n }\n}\n","import type { P } from '../types'\n\nclass Compiler {\n classMap: Record<string, { [key: string]: string }> = {}\n constructor() { }\n\n getContent() {\n let content = ''\n\n for (const name in this.classMap) {\n content += `\n export class ${name}{\n ${Object.values(this.classMap[name]).reduce((p, c) => p + c)}\n }`\n }\n return content\n }\n\n addMethod(args: P.Meta) {\n const {\n rpc, name, method, tag,\n } = args\n if (!rpc || !rpc.type)\n return\n if (!this.classMap[name])\n this.classMap[name] = {}\n this.classMap[name][method] = `\n ${method}(){\n return {tag:'${tag}-${method}',isEvent:${!!rpc.isEvent},rpc:[${rpc.type.reduce((p, c) => {\n return `${p}\"${c}\",`\n }, '')}]}\n\n }\n `\n }\n}\n\nexport function generateRPCCode(meta: P.Meta[]) {\n const compiler = new Compiler()\n\n for (const i of meta)\n compiler.addMethod(i)\n return compiler.getContent()\n}\n","import type { P } from '../types'\n\nclass Compiler {\n classMap: Record<string, { [key: string]: string }> = {}\n constructor() { }\n\n getContent() {\n let content = ''\n\n for (const name in this.classMap) {\n content += `\n export class ${name}{\n ${Object.values(this.classMap[name]).reduce((p, c) => p + c)}\n }`\n }\n return content\n }\n\n // createRequest() {\n // let content = 'import {useC} from \\'phecda-server\\'\\n'\n // for (const name in this.classMap)\n // content += `export const {${Object.keys(this.classMap[name]).join(',')}}=useC(${name})\\n`\n // return content\n // }\n\n addMethod(args: P.Meta) {\n const {\n http, name, method, params, tag,\n } = args\n if (!http)\n return\n const url = http.route.replace(/\\/\\:([^\\/]*)/g, (_, js) => `/{{${js}}}`)\n if (!this.classMap[name])\n this.classMap[name] = {}\n this.classMap[name][method] = `\n ${method}(...args){\nconst ret={tag:\"${tag}-${method}\",body:{},headers:{},query:{},params:{},method:\"${http.type}\",url:\"${url}\",args}\n\n${params.reduce((p, c, i) => `${p}ret.${c.type}${c.key ? `['${c.key}']` : ''}=args[${i}]\\n${c.type === 'params' ? `ret.url=ret.url.replace('{{${c.key}}}',args[${i}])` : ''}\\n`, '')}\nreturn ret\n }\n `\n }\n}\n\nexport function generateHTTPCode(meta: P.Meta[]) {\n const compiler = new Compiler()\n\n for (const i of meta)\n compiler.addMethod(i)\n return compiler.getContent()\n}\n","import 'reflect-metadata'\nimport fs from 'fs'\nimport EventEmitter from 'node:events'\nimport type { Phecda } from 'phecda-core'\nimport { Empty, getExposeKey, getHandler, getState, injectProperty, isPhecda, registerAsync } from 'phecda-core'\nimport Debug from 'debug'\nimport pc from 'picocolors'\nimport type { Construct, Emitter, P } from './types'\nimport { Meta } from './meta'\nimport { log } from './utils'\nimport { IS_DEV, UNMOUNT_SYMBOL } from './common'\nimport { generateHTTPCode, generateRPCCode } from './compiler'\nexport function Injectable() {\n return (target: any) => Empty(target)\n}\nconst debug = Debug('phecda-server')\n// TODO: support both emitter types and origin emitter type in future\nexport const emitter: Emitter = new EventEmitter() as any\n\nexport async function Factory(Modules: (new (...args: any) => any)[], opts: {\n\n // HTTP generate code path\n http?: string\n // rpc generate code path\n rpc?: string\n} = {}) {\n const moduleMap = new Map<string, InstanceType<Construct>>()\n const meta: Meta[] = []\n const constructorMap = new Map()\n\n // only work for warn\n const constructorSet = new WeakSet()\n const moduleGraph = new Map<string, Set<string>>()\n const { http, rpc } = opts\n injectProperty('watcher', ({ eventName, instance, key, options }: { eventName: string; instance: any; key: string; options?: { once: boolean } }) => {\n const fn = typeof instance[key] === 'function' ? instance[key].bind(instance) : (v: any) => instance[key] = v\n\n // work for hmr\n if (!instance[UNMOUNT_SYMBOL])\n instance[UNMOUNT_SYMBOL] = []\n\n instance[UNMOUNT_SYMBOL].push(() => {\n (emitter as any).off(eventName, fn)\n })\n\n if (options?.once)\n (emitter as any).once(eventName, fn)\n\n else\n (emitter as any).on(eventName, fn)\n })\n\n async function update(Module: Construct) {\n const tag = Module.prototype?.__TAG__ || Module.name\n if (!moduleMap.has(tag))\n return\n debug(`update module \"${tag}\"`)\n\n const instance = moduleMap.get(tag)\n\n if (instance?.[UNMOUNT_SYMBOL]) {\n for (const cb of instance[UNMOUNT_SYMBOL])\n await cb()\n }\n log(`Module ${pc.yellow(`[${tag}]`)} unmount`)\n moduleMap.delete(tag)\n constructorMap.delete(tag)\n for (let i = meta.length - 1; i >= 0; i--) {\n if (meta[i].data.tag === tag)\n meta.splice(i, 1)\n }\n\n const { instance: newModule } = await buildNestModule(Module)\n if (moduleGraph.has(tag)) {\n [...moduleGraph.get(tag)!].forEach((tag) => {\n const module = moduleMap.get(tag)\n for (const key in module) {\n if (module[key] === instance)\n module[key] = newModule\n }\n })\n }\n\n moduleMap.set(tag, newModule)\n }\n async function buildNestModule(Module: Construct) {\n const paramtypes = getParamTypes(Module) as Construct[]\n let instance: InstanceType<Construct>\n const tag = Module.prototype?.__TAG__ || Module.name\n if (moduleMap.has(tag)) {\n instance = moduleMap.get(tag)\n if (!instance)\n throw new Error(`exist Circular-Dependency or Multiple modules with the same name/tag [tag] ${tag}--[module] ${Module}`)\n\n if (constructorMap.get(tag) !== Module && !constructorSet.has(Module)) {\n constructorSet.add(Module)// a module will only warn once\n log(`Synonym module: Module taged \"${tag}\" has been loaded before, so phecda-server won't load Module \"${Module.name}\"`, 'warn')\n }\n return { instance, tag }\n }\n moduleMap.set(tag, undefined)\n if (paramtypes) {\n const paramtypesInstances = [] as any[]\n for (const i in paramtypes) {\n const { instance: sub, tag: subTag } = await buildNestModule(paramtypes[i])\n paramtypesInstances[i] = sub\n if (!moduleGraph.has(subTag))\n moduleGraph.set(subTag, new Set())\n moduleGraph.get(subTag)!.add(tag)\n }\n instance = new Module(...paramtypesInstances)\n }\n else {\n instance = new Module()\n }\n meta.push(...getMetaFromInstance(instance, tag, Module.name))\n await registerAsync(instance)\n moduleMap.set(tag, instance)\n log(`Module ${pc.yellow(`[${tag}]`)} mount\"`)\n constructorMap.set(tag, Module)\n return { instance, tag }\n }\n\n for (const Module of Modules)\n await buildNestModule(Module)\n\n function writeCode() {\n debug('write code')\n\n http && fs.promises.writeFile(http, generateHTTPCode(meta.map(item => item.data)))\n rpc && fs.promises.writeFile(rpc, generateRPCCode(meta.map(item => item.data)))\n }\n\n writeCode()\n if (IS_DEV) {\n if (!globalThis.__PS_HMR__)\n globalThis.__PS_HMR__ = []\n\n globalThis.__PS_HMR__?.push(async (files: string[]) => {\n for (const file of files) {\n debug(`reload file ${file}`)\n const module = await import(file)\n for (const i in module) {\n if (isPhecda(module[i]))\n await update(module[i])\n }\n }\n writeCode()\n })\n }\n\n return {\n moduleMap,\n meta,\n constructorMap,\n update,\n }\n}\n\nfunction getMetaFromInstance(instance: Phecda, tag: string, name: string) {\n const vars = getExposeKey(instance).filter(item => item !== '__CLASS')\n const baseState = (getState(instance, '__CLASS') || {}) as P.Meta\n initState(baseState)\n\n return vars.map((i) => {\n const meta = {} as P.Meta\n const state = (getState(instance, i) || {}) as P.Meta\n initState(state)\n if (state.http) {\n meta.http = {\n route: (baseState.http?.route || '') + (state.http.route),\n type: state.http.type,\n }\n }\n if (baseState.rpc)\n meta.rpc = baseState.rpc\n if (state.rpc) {\n meta.rpc = {\n ...meta.rpc,\n ...state.rpc,\n }\n }\n\n meta.name = name\n meta.tag = tag\n meta.method = i as string\n const params = [] as any[]\n for (const i of state.params || []) {\n params.unshift(i)\n if (i.index === 0)\n break\n }\n\n meta.params = params\n meta.define = { ...baseState.define, ...state.define }\n meta.header = { ...baseState.header, ...state.header }\n meta.plugins = [...new Set([...baseState.plugins, ...state.plugins])]\n meta.guards = [...new Set([...baseState.guards, ...state.guards])]\n meta.interceptors = [...new Set([...baseState.interceptors, ...state.interceptors])]\n\n return new Meta(meta as unknown as P.Meta, getHandler(instance, i), getParamTypes(instance, i as string) || [])\n })\n}\n\nfunction getParamTypes(Module: any, key?: string | symbol) {\n return Reflect.getMetadata('design:paramtypes', Module, key!)\n}\n\nfunction initState(state: any) {\n if (!state.define)\n state.define = {}\n if (!state.header)\n state.header = {}\n if (!state.plugins)\n state.plugins = []\n if (!state.guards)\n state.guards = []\n if (!state.interceptors)\n state.interceptors = []\n}\n"]}