@oldzy/conduit 1.0.6 → 1.0.7

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.
@@ -1,2 +1,2 @@
1
1
  'use strict';require('reflect-metadata');/* conduit - MIT License */
2
- var E=Object.defineProperty;var T=(o,e,t)=>e in o?E(o,e,{enumerable:true,configurable:true,writable:true,value:t}):o[e]=t;var n=(o,e)=>E(o,"name",{value:e,configurable:true});var i=(o,e,t)=>T(o,typeof e!="symbol"?e+"":e,t);var s=(function(o){return o.Singleton="SINGLETON",o.Scoped="SCOPED",o.Transient="TRANSIENT",o})({});var l=class l{constructor(e,t){i(this,"serviceDescriptors");i(this,"singletons",new Map);i(this,"scopedInstances",new Map);i(this,"parent");i(this,"resolutionStack",[]);this.serviceDescriptors=e,this.parent=t;}getService(e){try{return this.getRequiredService(e.toString())}catch{return}}getRequiredService(e){let t=this.resolutionStack.length===0;if(this.resolutionStack.includes(e.toString())){let r=[...this.resolutionStack,e.toString()].map(a=>String(a)).join(" \u2192 ");throw new Error(`Circular dependency detected: ${r}`)}this.resolutionStack.push(e.toString());try{let r=this.serviceDescriptors.find(a=>a.serviceIdentifier.toString()===e.toString());if(!r){if(this.parent)return this.parent.getRequiredService(e.toString());throw new Error(`Service '${String(e)}' not registered`)}return this.resolveService(r)}catch(r){throw this.resolutionStack.pop(),r}finally{this.resolutionStack[this.resolutionStack.length-1]===e.toString()&&this.resolutionStack.pop(),t&&(this.resolutionStack=[]);}}resolveService(e){switch(e.lifetime){case s.Singleton:return this.resolveSingleton(e);case s.Scoped:return this.resolveScoped(e);case s.Transient:return this.resolveTransient(e);default:throw new Error(`Unknown lifetime: ${e.lifetime}`)}}resolveSingleton(e){let{serviceIdentifier:t}=e;if(this.singletons.has(t.toString()))return this.singletons.get(t.toString());let r=this.createInstance(e);return this.singletons.set(t.toString(),r),r}resolveScoped(e){let{serviceIdentifier:t}=e;if(!this.parent)throw new Error(`Cannot resolve scoped service '${String(t)}' from root provider. Use createScope() first.`);if(this.scopedInstances.has(t.toString()))return this.scopedInstances.get(t.toString());let r=this.createInstance(e);return this.scopedInstances.set(t.toString(),r),r}resolveTransient(e){return this.createInstance(e)}createInstance(e){if(e.implementationInstance!==void 0)return e.implementationInstance;if(e.implementationFactory)return e.implementationFactory(this);if(e.implementationType)return this.constructInstance(e);throw new Error(`Cannot create instance for service '${String(e.serviceIdentifier)}'`)}constructInstance(e){let{implementationType:t,dependencies:r}=e;if(!t)throw new Error("Implementation type is required");if(r&&r.length>0){let c=r.map(d=>this.getRequiredService(d));return new t(...c)}let p=(Reflect.getMetadata("design:paramtypes",t)||[]).map((c,d)=>{let P=Reflect.getMetadata("inject:params",t)?.[d];if(P)return this.getRequiredService(P);let R=c.name||c;return this.getRequiredService(R)});return new t(...p)}createScope(){let e=new l(this.serviceDescriptors,this);return {serviceProvider:e,dispose:n(()=>{e.scopedInstances.clear();},"dispose")}}has(e){return this.serviceDescriptors.some(t=>t.serviceIdentifier.toString()===e.toString())||(this.parent?.has(e.toString())??false)}};n(l,"ServiceProvider");var u=l;var S=class S{constructor(){i(this,"serviceDescriptors",[]);}addSingleton(e,t){return typeof t=="function"?this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:s.Singleton}):this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationInstance:t,lifetime:s.Singleton}),this}addScoped(e,t){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:s.Scoped}),this}addTransient(e,t){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:s.Transient}),this}addFactory(e,t,r=s.Transient){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationFactory:t,lifetime:r}),this}addScopedWithDependencies(e,t,r){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:s.Scoped,dependencies:r}),this}buildServiceProvider(){return new u([...this.serviceDescriptors])}getDescriptors(){return [...this.serviceDescriptors]}has(e){return this.serviceDescriptors.some(t=>t.serviceIdentifier.toString()===e.toString())}get count(){return this.serviceDescriptors.length}};n(S,"ServiceCollection");var v=S;var m=class m{constructor(e){i(this,"request");i(this,"response");i(this,"metadata",new Map);this.request=e;}setMetadata(e,t){this.metadata.set(e,t);}getMetadata(e){return this.metadata.get(e)}};n(m,"PipelineContext");var g=m;var w=class w{constructor(e){i(this,"serviceProvider");i(this,"behaviors",[]);this.serviceProvider=e;let t=this.serviceProvider.getService("PipelineBehaviors");t&&(this.behaviors=t);}async send(e){let t=this.serviceProvider.createScope();try{let r=await this.executeHandler(e,t.serviceProvider);if(this.isAsyncIterable(r))throw new Error("Handler returned a stream. Use stream() method instead of send().");return r}finally{t.dispose();}}async stream(e){let t=this.serviceProvider.createScope();try{let r=await this.executeHandler(e,t.serviceProvider);if(!this.isAsyncIterable(r))throw new Error("Handler did not return a stream. Use send() method instead of stream().");return r}finally{t.dispose();}}async executeHandler(e,t){let r=t.getService(e.type);if(!r)throw new Error(`No handler registered for request type: ${String(e.type)}`);let a=new g(e);return this.behaviors.length===0?await r.handle(e):await this.executePipeline(a,r,this.behaviors)}async executePipeline(e,t,r){let a=0,p=n(async()=>{if(a<r.length)return await r[a++].handle(e,p);let c=await t.handle(e.request);return e.response=c,c},"next");return await p()}isAsyncIterable(e){return e!=null&&typeof e[Symbol.asyncIterator]=="function"}};n(w,"Mediator");var f=w;var y=class y{constructor(e,t,r){i(this,"serviceProvider");i(this,"mediator");i(this,"isRunning",false);i(this,"adapters");this.serviceProvider=e,this.mediator=t,this.adapters=r;}async run(){if(this.isRunning)throw new Error("Application is already running");for(let e of this.adapters)e.initialize&&await e.initialize(this);this.isRunning=true;}async stop(){if(this.isRunning){for(let e of [...this.adapters].reverse())e.dispose&&await e.dispose();this.isRunning=false;}}async send(e){if(!this.isRunning)throw new Error("Application must be running before sending requests. Call app.run() first.");return this.mediator.send(e)}async stream(e){if(!this.isRunning)throw new Error("Application must be running before streaming requests. Call app.run() first.");return this.mediator.stream(e)}getServiceProvider(){return this.serviceProvider}getMediator(){return this.mediator}get running(){return this.isRunning}};n(y,"Application");var h=y;var b="PipelineBehaviors",D=class D{constructor(){i(this,"services");i(this,"adapters",[]);i(this,"pipelineBehaviors",[]);this.services=new v;}registerHandler(e,t,r=s.Transient){if(typeof t=="function")switch(r){case s.Singleton:this.services.addSingleton(e,t);break;case s.Scoped:this.services.addScoped(e,t);break;case s.Transient:default:this.services.addTransient(e,t);break}else this.services.addSingleton(e,t);return this}registerHandlers(e){return e.forEach((t,r)=>{this.registerHandler(r,t);}),this}registerBehavior(e){return this.pipelineBehaviors.push(e),this}registerSingleton(e,t){return this.services.addSingleton(e,t),this}registerScoped(e,t){return this.services.addScoped(e,t),this}registerTransient(e,t){return this.services.addTransient(e,t),this}registerFactory(e,t,r=s.Transient){return this.services.addFactory(e,t,r),this}use(e){return this.adapters.push(e),this}getServiceCollection(){return this.services}async build(){for(let r of this.adapters)await r.configure(this.services);this.pipelineBehaviors.length>0&&this.services.addSingleton(b,this.pipelineBehaviors.map(r=>typeof r=="function"?new r:r));let e=this.services.buildServiceProvider(),t=new f(e);return new h(e,t,this.adapters)}};n(D,"ApplicationBuilder");var I=D;exports.Application=h;exports.ApplicationBuilder=I;
2
+ var b=Object.defineProperty;var T=(o,e,t)=>e in o?b(o,e,{enumerable:true,configurable:true,writable:true,value:t}):o[e]=t;var n=(o,e)=>b(o,"name",{value:e,configurable:true});var i=(o,e,t)=>T(o,typeof e!="symbol"?e+"":e,t);var s=(function(o){return o.Singleton="SINGLETON",o.Scoped="SCOPED",o.Transient="TRANSIENT",o})({});var g=class g{constructor(e,t){i(this,"serviceDescriptors");i(this,"singletons",new Map);i(this,"scopedInstances",new Map);i(this,"parent");i(this,"resolutionStack",[]);this.serviceDescriptors=e,this.parent=t;}getService(e){try{return this.getRequiredService(e.toString())}catch{return}}getRequiredService(e){let t=this.resolutionStack.length===0;if(this.resolutionStack.includes(e.toString())){let r=[...this.resolutionStack,e.toString()].map(a=>String(a)).join(" \u2192 ");throw new Error(`Circular dependency detected: ${r}`)}this.resolutionStack.push(e.toString());try{let r=this.serviceDescriptors.find(a=>a.serviceIdentifier.toString()===e.toString());if(!r){if(this.parent)return this.parent.getRequiredService(e.toString());throw new Error(`Service '${String(e)}' not registered`)}return this.resolveService(r)}catch(r){throw this.resolutionStack.pop(),r}finally{this.resolutionStack[this.resolutionStack.length-1]===e.toString()&&this.resolutionStack.pop(),t&&(this.resolutionStack=[]);}}resolveService(e){switch(e.lifetime){case s.Singleton:return this.resolveSingleton(e);case s.Scoped:return this.resolveScoped(e);case s.Transient:return this.resolveTransient(e);default:throw new Error(`Unknown lifetime: ${e.lifetime}`)}}resolveSingleton(e){let{serviceIdentifier:t}=e;if(this.singletons.has(t.toString()))return this.singletons.get(t.toString());let r=this.createInstance(e);return this.singletons.set(t.toString(),r),r}resolveScoped(e){let{serviceIdentifier:t}=e;if(!this.parent)throw new Error(`Cannot resolve scoped service '${String(t)}' from root provider. Use createScope() first.`);if(this.scopedInstances.has(t.toString()))return this.scopedInstances.get(t.toString());let r=this.createInstance(e);return this.scopedInstances.set(t.toString(),r),r}resolveTransient(e){return this.createInstance(e)}createInstance(e){if(e.implementationInstance!==void 0)return e.implementationInstance;if(e.implementationFactory)return e.implementationFactory(this);if(e.implementationType)return this.constructInstance(e);throw new Error(`Cannot create instance for service '${String(e.serviceIdentifier)}'`)}constructInstance(e){let{implementationType:t,dependencies:r}=e;if(!t)throw new Error("Implementation type is required");if(r&&r.length>0){let c=r.map(l=>this.getRequiredService(l));return new t(...c)}let a=Reflect.getMetadata("inject:params",t),h=Reflect.getMetadata("design:paramtypes",t)||[],d=Math.max(h.length,a?Math.max(...Object.keys(a).map(c=>parseInt(c)))+1:0),u=[];for(let c=0;c<d;c++){let l=a?.[c];if(l)u.push(this.getRequiredService(l));else if(h[c]){let R=h[c].name||h[c];u.push(this.getRequiredService(R));}else throw new Error(`Cannot resolve parameter at index ${c} for ${t.name}`)}return new t(...u)}createScope(){let e=new g(this.serviceDescriptors,this);return {serviceProvider:e,dispose:n(()=>{e.scopedInstances.clear();},"dispose")}}has(e){return this.serviceDescriptors.some(t=>t.serviceIdentifier.toString()===e.toString())||(this.parent?.has(e.toString())??false)}};n(g,"ServiceProvider");var v=g;var w=class w{constructor(){i(this,"serviceDescriptors",[]);}addSingleton(e,t){return typeof t=="function"?this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:s.Singleton}):this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationInstance:t,lifetime:s.Singleton}),this}addScoped(e,t){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:s.Scoped}),this}addTransient(e,t){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:s.Transient}),this}addFactory(e,t,r=s.Transient){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationFactory:t,lifetime:r}),this}addScopedWithDependencies(e,t,r){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:s.Scoped,dependencies:r}),this}buildServiceProvider(){return new v([...this.serviceDescriptors])}getDescriptors(){return [...this.serviceDescriptors]}has(e){return this.serviceDescriptors.some(t=>t.serviceIdentifier.toString()===e.toString())}get count(){return this.serviceDescriptors.length}};n(w,"ServiceCollection");var f=w;var y=class y{constructor(e){i(this,"request");i(this,"response");i(this,"metadata",new Map);this.request=e;}setMetadata(e,t){this.metadata.set(e,t);}getMetadata(e){return this.metadata.get(e)}};n(y,"PipelineContext");var m=y;var I=class I{constructor(e){i(this,"serviceProvider");i(this,"behaviors",[]);this.serviceProvider=e;let t=this.serviceProvider.getService("PipelineBehaviors");t&&(this.behaviors=t);}async send(e){let t=this.serviceProvider.createScope();try{let r=await this.executeHandler(e,t.serviceProvider);if(this.isAsyncIterable(r))throw new Error("Handler returned a stream. Use stream() method instead of send().");return r}finally{t.dispose();}}async stream(e){let t=this.serviceProvider.createScope();try{let r=await this.executeHandler(e,t.serviceProvider);if(!this.isAsyncIterable(r))throw new Error("Handler did not return a stream. Use send() method instead of stream().");return r}finally{t.dispose();}}async executeHandler(e,t){let r=t.getService(e.type);if(!r)throw new Error(`No handler registered for request type: ${String(e.type)}`);let a=new m(e);return this.behaviors.length===0?await r.handle(e):await this.executePipeline(a,r,this.behaviors)}async executePipeline(e,t,r){let a=0,h=n(async()=>{if(a<r.length)return await r[a++].handle(e,h);let d=await t.handle(e.request);return e.response=d,d},"next");return await h()}isAsyncIterable(e){return e!=null&&typeof e[Symbol.asyncIterator]=="function"}};n(I,"Mediator");var S=I;var D=class D{constructor(e,t,r){i(this,"serviceProvider");i(this,"mediator");i(this,"isRunning",false);i(this,"adapters");this.serviceProvider=e,this.mediator=t,this.adapters=r;}async run(){if(this.isRunning)throw new Error("Application is already running");for(let e of this.adapters)e.initialize&&await e.initialize(this);this.isRunning=true;}async stop(){if(this.isRunning){for(let e of [...this.adapters].reverse())e.dispose&&await e.dispose();this.isRunning=false;}}async send(e){if(!this.isRunning)throw new Error("Application must be running before sending requests. Call app.run() first.");return this.mediator.send(e)}async stream(e){if(!this.isRunning)throw new Error("Application must be running before streaming requests. Call app.run() first.");return this.mediator.stream(e)}getServiceProvider(){return this.serviceProvider}getMediator(){return this.mediator}get running(){return this.isRunning}};n(D,"Application");var p=D;var x="PipelineBehaviors",P=class P{constructor(){i(this,"services");i(this,"adapters",[]);i(this,"pipelineBehaviors",[]);this.services=new f;}registerHandler(e,t,r=s.Transient){if(typeof t=="function")switch(r){case s.Singleton:this.services.addSingleton(e,t);break;case s.Scoped:this.services.addScoped(e,t);break;case s.Transient:default:this.services.addTransient(e,t);break}else this.services.addSingleton(e,t);return this}registerHandlers(e){return e.forEach((t,r)=>{this.registerHandler(r,t);}),this}registerBehavior(e){return this.pipelineBehaviors.push(e),this}registerSingleton(e,t){return this.services.addSingleton(e,t),this}registerScoped(e,t){return this.services.addScoped(e,t),this}registerTransient(e,t){return this.services.addTransient(e,t),this}registerFactory(e,t,r=s.Transient){return this.services.addFactory(e,t,r),this}use(e){return this.adapters.push(e),this}getServiceCollection(){return this.services}async build(){for(let r of this.adapters)await r.configure(this.services);this.pipelineBehaviors.length>0&&this.services.addSingleton(x,this.pipelineBehaviors.map(r=>typeof r=="function"?new r:r));let e=this.services.buildServiceProvider(),t=new S(e);return new p(e,t,this.adapters)}};n(P,"ApplicationBuilder");var E=P;exports.Application=p;exports.ApplicationBuilder=E;
@@ -1,2 +1,2 @@
1
1
  import'reflect-metadata';/* conduit - MIT License */
2
- var E=Object.defineProperty;var T=(o,e,t)=>e in o?E(o,e,{enumerable:true,configurable:true,writable:true,value:t}):o[e]=t;var n=(o,e)=>E(o,"name",{value:e,configurable:true});var i=(o,e,t)=>T(o,typeof e!="symbol"?e+"":e,t);var s=(function(o){return o.Singleton="SINGLETON",o.Scoped="SCOPED",o.Transient="TRANSIENT",o})({});var l=class l{constructor(e,t){i(this,"serviceDescriptors");i(this,"singletons",new Map);i(this,"scopedInstances",new Map);i(this,"parent");i(this,"resolutionStack",[]);this.serviceDescriptors=e,this.parent=t;}getService(e){try{return this.getRequiredService(e.toString())}catch{return}}getRequiredService(e){let t=this.resolutionStack.length===0;if(this.resolutionStack.includes(e.toString())){let r=[...this.resolutionStack,e.toString()].map(a=>String(a)).join(" \u2192 ");throw new Error(`Circular dependency detected: ${r}`)}this.resolutionStack.push(e.toString());try{let r=this.serviceDescriptors.find(a=>a.serviceIdentifier.toString()===e.toString());if(!r){if(this.parent)return this.parent.getRequiredService(e.toString());throw new Error(`Service '${String(e)}' not registered`)}return this.resolveService(r)}catch(r){throw this.resolutionStack.pop(),r}finally{this.resolutionStack[this.resolutionStack.length-1]===e.toString()&&this.resolutionStack.pop(),t&&(this.resolutionStack=[]);}}resolveService(e){switch(e.lifetime){case s.Singleton:return this.resolveSingleton(e);case s.Scoped:return this.resolveScoped(e);case s.Transient:return this.resolveTransient(e);default:throw new Error(`Unknown lifetime: ${e.lifetime}`)}}resolveSingleton(e){let{serviceIdentifier:t}=e;if(this.singletons.has(t.toString()))return this.singletons.get(t.toString());let r=this.createInstance(e);return this.singletons.set(t.toString(),r),r}resolveScoped(e){let{serviceIdentifier:t}=e;if(!this.parent)throw new Error(`Cannot resolve scoped service '${String(t)}' from root provider. Use createScope() first.`);if(this.scopedInstances.has(t.toString()))return this.scopedInstances.get(t.toString());let r=this.createInstance(e);return this.scopedInstances.set(t.toString(),r),r}resolveTransient(e){return this.createInstance(e)}createInstance(e){if(e.implementationInstance!==void 0)return e.implementationInstance;if(e.implementationFactory)return e.implementationFactory(this);if(e.implementationType)return this.constructInstance(e);throw new Error(`Cannot create instance for service '${String(e.serviceIdentifier)}'`)}constructInstance(e){let{implementationType:t,dependencies:r}=e;if(!t)throw new Error("Implementation type is required");if(r&&r.length>0){let c=r.map(d=>this.getRequiredService(d));return new t(...c)}let p=(Reflect.getMetadata("design:paramtypes",t)||[]).map((c,d)=>{let P=Reflect.getMetadata("inject:params",t)?.[d];if(P)return this.getRequiredService(P);let R=c.name||c;return this.getRequiredService(R)});return new t(...p)}createScope(){let e=new l(this.serviceDescriptors,this);return {serviceProvider:e,dispose:n(()=>{e.scopedInstances.clear();},"dispose")}}has(e){return this.serviceDescriptors.some(t=>t.serviceIdentifier.toString()===e.toString())||(this.parent?.has(e.toString())??false)}};n(l,"ServiceProvider");var u=l;var S=class S{constructor(){i(this,"serviceDescriptors",[]);}addSingleton(e,t){return typeof t=="function"?this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:s.Singleton}):this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationInstance:t,lifetime:s.Singleton}),this}addScoped(e,t){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:s.Scoped}),this}addTransient(e,t){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:s.Transient}),this}addFactory(e,t,r=s.Transient){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationFactory:t,lifetime:r}),this}addScopedWithDependencies(e,t,r){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:s.Scoped,dependencies:r}),this}buildServiceProvider(){return new u([...this.serviceDescriptors])}getDescriptors(){return [...this.serviceDescriptors]}has(e){return this.serviceDescriptors.some(t=>t.serviceIdentifier.toString()===e.toString())}get count(){return this.serviceDescriptors.length}};n(S,"ServiceCollection");var v=S;var m=class m{constructor(e){i(this,"request");i(this,"response");i(this,"metadata",new Map);this.request=e;}setMetadata(e,t){this.metadata.set(e,t);}getMetadata(e){return this.metadata.get(e)}};n(m,"PipelineContext");var g=m;var w=class w{constructor(e){i(this,"serviceProvider");i(this,"behaviors",[]);this.serviceProvider=e;let t=this.serviceProvider.getService("PipelineBehaviors");t&&(this.behaviors=t);}async send(e){let t=this.serviceProvider.createScope();try{let r=await this.executeHandler(e,t.serviceProvider);if(this.isAsyncIterable(r))throw new Error("Handler returned a stream. Use stream() method instead of send().");return r}finally{t.dispose();}}async stream(e){let t=this.serviceProvider.createScope();try{let r=await this.executeHandler(e,t.serviceProvider);if(!this.isAsyncIterable(r))throw new Error("Handler did not return a stream. Use send() method instead of stream().");return r}finally{t.dispose();}}async executeHandler(e,t){let r=t.getService(e.type);if(!r)throw new Error(`No handler registered for request type: ${String(e.type)}`);let a=new g(e);return this.behaviors.length===0?await r.handle(e):await this.executePipeline(a,r,this.behaviors)}async executePipeline(e,t,r){let a=0,p=n(async()=>{if(a<r.length)return await r[a++].handle(e,p);let c=await t.handle(e.request);return e.response=c,c},"next");return await p()}isAsyncIterable(e){return e!=null&&typeof e[Symbol.asyncIterator]=="function"}};n(w,"Mediator");var f=w;var y=class y{constructor(e,t,r){i(this,"serviceProvider");i(this,"mediator");i(this,"isRunning",false);i(this,"adapters");this.serviceProvider=e,this.mediator=t,this.adapters=r;}async run(){if(this.isRunning)throw new Error("Application is already running");for(let e of this.adapters)e.initialize&&await e.initialize(this);this.isRunning=true;}async stop(){if(this.isRunning){for(let e of [...this.adapters].reverse())e.dispose&&await e.dispose();this.isRunning=false;}}async send(e){if(!this.isRunning)throw new Error("Application must be running before sending requests. Call app.run() first.");return this.mediator.send(e)}async stream(e){if(!this.isRunning)throw new Error("Application must be running before streaming requests. Call app.run() first.");return this.mediator.stream(e)}getServiceProvider(){return this.serviceProvider}getMediator(){return this.mediator}get running(){return this.isRunning}};n(y,"Application");var h=y;var b="PipelineBehaviors",D=class D{constructor(){i(this,"services");i(this,"adapters",[]);i(this,"pipelineBehaviors",[]);this.services=new v;}registerHandler(e,t,r=s.Transient){if(typeof t=="function")switch(r){case s.Singleton:this.services.addSingleton(e,t);break;case s.Scoped:this.services.addScoped(e,t);break;case s.Transient:default:this.services.addTransient(e,t);break}else this.services.addSingleton(e,t);return this}registerHandlers(e){return e.forEach((t,r)=>{this.registerHandler(r,t);}),this}registerBehavior(e){return this.pipelineBehaviors.push(e),this}registerSingleton(e,t){return this.services.addSingleton(e,t),this}registerScoped(e,t){return this.services.addScoped(e,t),this}registerTransient(e,t){return this.services.addTransient(e,t),this}registerFactory(e,t,r=s.Transient){return this.services.addFactory(e,t,r),this}use(e){return this.adapters.push(e),this}getServiceCollection(){return this.services}async build(){for(let r of this.adapters)await r.configure(this.services);this.pipelineBehaviors.length>0&&this.services.addSingleton(b,this.pipelineBehaviors.map(r=>typeof r=="function"?new r:r));let e=this.services.buildServiceProvider(),t=new f(e);return new h(e,t,this.adapters)}};n(D,"ApplicationBuilder");var I=D;export{h as Application,I as ApplicationBuilder};
2
+ var b=Object.defineProperty;var T=(o,e,t)=>e in o?b(o,e,{enumerable:true,configurable:true,writable:true,value:t}):o[e]=t;var n=(o,e)=>b(o,"name",{value:e,configurable:true});var i=(o,e,t)=>T(o,typeof e!="symbol"?e+"":e,t);var s=(function(o){return o.Singleton="SINGLETON",o.Scoped="SCOPED",o.Transient="TRANSIENT",o})({});var g=class g{constructor(e,t){i(this,"serviceDescriptors");i(this,"singletons",new Map);i(this,"scopedInstances",new Map);i(this,"parent");i(this,"resolutionStack",[]);this.serviceDescriptors=e,this.parent=t;}getService(e){try{return this.getRequiredService(e.toString())}catch{return}}getRequiredService(e){let t=this.resolutionStack.length===0;if(this.resolutionStack.includes(e.toString())){let r=[...this.resolutionStack,e.toString()].map(a=>String(a)).join(" \u2192 ");throw new Error(`Circular dependency detected: ${r}`)}this.resolutionStack.push(e.toString());try{let r=this.serviceDescriptors.find(a=>a.serviceIdentifier.toString()===e.toString());if(!r){if(this.parent)return this.parent.getRequiredService(e.toString());throw new Error(`Service '${String(e)}' not registered`)}return this.resolveService(r)}catch(r){throw this.resolutionStack.pop(),r}finally{this.resolutionStack[this.resolutionStack.length-1]===e.toString()&&this.resolutionStack.pop(),t&&(this.resolutionStack=[]);}}resolveService(e){switch(e.lifetime){case s.Singleton:return this.resolveSingleton(e);case s.Scoped:return this.resolveScoped(e);case s.Transient:return this.resolveTransient(e);default:throw new Error(`Unknown lifetime: ${e.lifetime}`)}}resolveSingleton(e){let{serviceIdentifier:t}=e;if(this.singletons.has(t.toString()))return this.singletons.get(t.toString());let r=this.createInstance(e);return this.singletons.set(t.toString(),r),r}resolveScoped(e){let{serviceIdentifier:t}=e;if(!this.parent)throw new Error(`Cannot resolve scoped service '${String(t)}' from root provider. Use createScope() first.`);if(this.scopedInstances.has(t.toString()))return this.scopedInstances.get(t.toString());let r=this.createInstance(e);return this.scopedInstances.set(t.toString(),r),r}resolveTransient(e){return this.createInstance(e)}createInstance(e){if(e.implementationInstance!==void 0)return e.implementationInstance;if(e.implementationFactory)return e.implementationFactory(this);if(e.implementationType)return this.constructInstance(e);throw new Error(`Cannot create instance for service '${String(e.serviceIdentifier)}'`)}constructInstance(e){let{implementationType:t,dependencies:r}=e;if(!t)throw new Error("Implementation type is required");if(r&&r.length>0){let c=r.map(l=>this.getRequiredService(l));return new t(...c)}let a=Reflect.getMetadata("inject:params",t),h=Reflect.getMetadata("design:paramtypes",t)||[],d=Math.max(h.length,a?Math.max(...Object.keys(a).map(c=>parseInt(c)))+1:0),u=[];for(let c=0;c<d;c++){let l=a?.[c];if(l)u.push(this.getRequiredService(l));else if(h[c]){let R=h[c].name||h[c];u.push(this.getRequiredService(R));}else throw new Error(`Cannot resolve parameter at index ${c} for ${t.name}`)}return new t(...u)}createScope(){let e=new g(this.serviceDescriptors,this);return {serviceProvider:e,dispose:n(()=>{e.scopedInstances.clear();},"dispose")}}has(e){return this.serviceDescriptors.some(t=>t.serviceIdentifier.toString()===e.toString())||(this.parent?.has(e.toString())??false)}};n(g,"ServiceProvider");var v=g;var w=class w{constructor(){i(this,"serviceDescriptors",[]);}addSingleton(e,t){return typeof t=="function"?this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:s.Singleton}):this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationInstance:t,lifetime:s.Singleton}),this}addScoped(e,t){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:s.Scoped}),this}addTransient(e,t){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:s.Transient}),this}addFactory(e,t,r=s.Transient){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationFactory:t,lifetime:r}),this}addScopedWithDependencies(e,t,r){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:s.Scoped,dependencies:r}),this}buildServiceProvider(){return new v([...this.serviceDescriptors])}getDescriptors(){return [...this.serviceDescriptors]}has(e){return this.serviceDescriptors.some(t=>t.serviceIdentifier.toString()===e.toString())}get count(){return this.serviceDescriptors.length}};n(w,"ServiceCollection");var f=w;var y=class y{constructor(e){i(this,"request");i(this,"response");i(this,"metadata",new Map);this.request=e;}setMetadata(e,t){this.metadata.set(e,t);}getMetadata(e){return this.metadata.get(e)}};n(y,"PipelineContext");var m=y;var I=class I{constructor(e){i(this,"serviceProvider");i(this,"behaviors",[]);this.serviceProvider=e;let t=this.serviceProvider.getService("PipelineBehaviors");t&&(this.behaviors=t);}async send(e){let t=this.serviceProvider.createScope();try{let r=await this.executeHandler(e,t.serviceProvider);if(this.isAsyncIterable(r))throw new Error("Handler returned a stream. Use stream() method instead of send().");return r}finally{t.dispose();}}async stream(e){let t=this.serviceProvider.createScope();try{let r=await this.executeHandler(e,t.serviceProvider);if(!this.isAsyncIterable(r))throw new Error("Handler did not return a stream. Use send() method instead of stream().");return r}finally{t.dispose();}}async executeHandler(e,t){let r=t.getService(e.type);if(!r)throw new Error(`No handler registered for request type: ${String(e.type)}`);let a=new m(e);return this.behaviors.length===0?await r.handle(e):await this.executePipeline(a,r,this.behaviors)}async executePipeline(e,t,r){let a=0,h=n(async()=>{if(a<r.length)return await r[a++].handle(e,h);let d=await t.handle(e.request);return e.response=d,d},"next");return await h()}isAsyncIterable(e){return e!=null&&typeof e[Symbol.asyncIterator]=="function"}};n(I,"Mediator");var S=I;var D=class D{constructor(e,t,r){i(this,"serviceProvider");i(this,"mediator");i(this,"isRunning",false);i(this,"adapters");this.serviceProvider=e,this.mediator=t,this.adapters=r;}async run(){if(this.isRunning)throw new Error("Application is already running");for(let e of this.adapters)e.initialize&&await e.initialize(this);this.isRunning=true;}async stop(){if(this.isRunning){for(let e of [...this.adapters].reverse())e.dispose&&await e.dispose();this.isRunning=false;}}async send(e){if(!this.isRunning)throw new Error("Application must be running before sending requests. Call app.run() first.");return this.mediator.send(e)}async stream(e){if(!this.isRunning)throw new Error("Application must be running before streaming requests. Call app.run() first.");return this.mediator.stream(e)}getServiceProvider(){return this.serviceProvider}getMediator(){return this.mediator}get running(){return this.isRunning}};n(D,"Application");var p=D;var x="PipelineBehaviors",P=class P{constructor(){i(this,"services");i(this,"adapters",[]);i(this,"pipelineBehaviors",[]);this.services=new f;}registerHandler(e,t,r=s.Transient){if(typeof t=="function")switch(r){case s.Singleton:this.services.addSingleton(e,t);break;case s.Scoped:this.services.addScoped(e,t);break;case s.Transient:default:this.services.addTransient(e,t);break}else this.services.addSingleton(e,t);return this}registerHandlers(e){return e.forEach((t,r)=>{this.registerHandler(r,t);}),this}registerBehavior(e){return this.pipelineBehaviors.push(e),this}registerSingleton(e,t){return this.services.addSingleton(e,t),this}registerScoped(e,t){return this.services.addScoped(e,t),this}registerTransient(e,t){return this.services.addTransient(e,t),this}registerFactory(e,t,r=s.Transient){return this.services.addFactory(e,t,r),this}use(e){return this.adapters.push(e),this}getServiceCollection(){return this.services}async build(){for(let r of this.adapters)await r.configure(this.services);this.pipelineBehaviors.length>0&&this.services.addSingleton(x,this.pipelineBehaviors.map(r=>typeof r=="function"?new r:r));let e=this.services.buildServiceProvider(),t=new S(e);return new p(e,t,this.adapters)}};n(P,"ApplicationBuilder");var E=P;export{p as Application,E as ApplicationBuilder};
package/dist/di/index.js CHANGED
@@ -1,2 +1,2 @@
1
1
  'use strict';require('reflect-metadata');/* conduit - MIT License */
2
- var f=Object.defineProperty;var v=(i,e,t)=>e in i?f(i,e,{enumerable:true,configurable:true,writable:true,value:t}):i[e]=t;var o=(i,e)=>f(i,"name",{value:e,configurable:true});var s=(i,e,t)=>v(i,typeof e!="symbol"?e+"":e,t);var n=(function(i){return i.Singleton="SINGLETON",i.Scoped="SCOPED",i.Transient="TRANSIENT",i})({});var l=class l{constructor(e,t){s(this,"serviceDescriptors");s(this,"singletons",new Map);s(this,"scopedInstances",new Map);s(this,"parent");s(this,"resolutionStack",[]);this.serviceDescriptors=e,this.parent=t;}getService(e){try{return this.getRequiredService(e.toString())}catch{return}}getRequiredService(e){let t=this.resolutionStack.length===0;if(this.resolutionStack.includes(e.toString())){let r=[...this.resolutionStack,e.toString()].map(c=>String(c)).join(" \u2192 ");throw new Error(`Circular dependency detected: ${r}`)}this.resolutionStack.push(e.toString());try{let r=this.serviceDescriptors.find(c=>c.serviceIdentifier.toString()===e.toString());if(!r){if(this.parent)return this.parent.getRequiredService(e.toString());throw new Error(`Service '${String(e)}' not registered`)}return this.resolveService(r)}catch(r){throw this.resolutionStack.pop(),r}finally{this.resolutionStack[this.resolutionStack.length-1]===e.toString()&&this.resolutionStack.pop(),t&&(this.resolutionStack=[]);}}resolveService(e){switch(e.lifetime){case n.Singleton:return this.resolveSingleton(e);case n.Scoped:return this.resolveScoped(e);case n.Transient:return this.resolveTransient(e);default:throw new Error(`Unknown lifetime: ${e.lifetime}`)}}resolveSingleton(e){let{serviceIdentifier:t}=e;if(this.singletons.has(t.toString()))return this.singletons.get(t.toString());let r=this.createInstance(e);return this.singletons.set(t.toString(),r),r}resolveScoped(e){let{serviceIdentifier:t}=e;if(!this.parent)throw new Error(`Cannot resolve scoped service '${String(t)}' from root provider. Use createScope() first.`);if(this.scopedInstances.has(t.toString()))return this.scopedInstances.get(t.toString());let r=this.createInstance(e);return this.scopedInstances.set(t.toString(),r),r}resolveTransient(e){return this.createInstance(e)}createInstance(e){if(e.implementationInstance!==void 0)return e.implementationInstance;if(e.implementationFactory)return e.implementationFactory(this);if(e.implementationType)return this.constructInstance(e);throw new Error(`Cannot create instance for service '${String(e.serviceIdentifier)}'`)}constructInstance(e){let{implementationType:t,dependencies:r}=e;if(!t)throw new Error("Implementation type is required");if(r&&r.length>0){let h=r.map(S=>this.getRequiredService(S));return new t(...h)}let p=(Reflect.getMetadata("design:paramtypes",t)||[]).map((h,S)=>{let d=Reflect.getMetadata("inject:params",t)?.[S];if(d)return this.getRequiredService(d);let m=h.name||h;return this.getRequiredService(m)});return new t(...p)}createScope(){let e=new l(this.serviceDescriptors,this);return {serviceProvider:e,dispose:o(()=>{e.scopedInstances.clear();},"dispose")}}has(e){return this.serviceDescriptors.some(t=>t.serviceIdentifier.toString()===e.toString())||(this.parent?.has(e.toString())??false)}};o(l,"ServiceProvider");var a=l;var g=class g{constructor(){s(this,"serviceDescriptors",[]);}addSingleton(e,t){return typeof t=="function"?this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:n.Singleton}):this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationInstance:t,lifetime:n.Singleton}),this}addScoped(e,t){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:n.Scoped}),this}addTransient(e,t){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:n.Transient}),this}addFactory(e,t,r=n.Transient){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationFactory:t,lifetime:r}),this}addScopedWithDependencies(e,t,r){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:n.Scoped,dependencies:r}),this}buildServiceProvider(){return new a([...this.serviceDescriptors])}getDescriptors(){return [...this.serviceDescriptors]}has(e){return this.serviceDescriptors.some(t=>t.serviceIdentifier.toString()===e.toString())}get count(){return this.serviceDescriptors.length}};o(g,"ServiceCollection");var u=g;function I(i){return (e,t,r)=>{let p={...Reflect.getOwnMetadata("inject:params",e)||{}};p[r]=i.toString(),Reflect.defineMetadata("inject:params",p,e);}}o(I,"Inject");exports.Inject=I;exports.ServiceCollection=u;exports.ServiceLifetime=n;exports.ServiceProvider=a;
2
+ var d=Object.defineProperty;var I=(i,e,t)=>e in i?d(i,e,{enumerable:true,configurable:true,writable:true,value:t}):i[e]=t;var p=(i,e)=>d(i,"name",{value:e,configurable:true});var c=(i,e,t)=>I(i,typeof e!="symbol"?e+"":e,t);var n=(function(i){return i.Singleton="SINGLETON",i.Scoped="SCOPED",i.Transient="TRANSIENT",i})({});var u=class u{constructor(e,t){c(this,"serviceDescriptors");c(this,"singletons",new Map);c(this,"scopedInstances",new Map);c(this,"parent");c(this,"resolutionStack",[]);this.serviceDescriptors=e,this.parent=t;}getService(e){try{return this.getRequiredService(e.toString())}catch{return}}getRequiredService(e){let t=this.resolutionStack.length===0;if(this.resolutionStack.includes(e.toString())){let r=[...this.resolutionStack,e.toString()].map(o=>String(o)).join(" \u2192 ");throw new Error(`Circular dependency detected: ${r}`)}this.resolutionStack.push(e.toString());try{let r=this.serviceDescriptors.find(o=>o.serviceIdentifier.toString()===e.toString());if(!r){if(this.parent)return this.parent.getRequiredService(e.toString());throw new Error(`Service '${String(e)}' not registered`)}return this.resolveService(r)}catch(r){throw this.resolutionStack.pop(),r}finally{this.resolutionStack[this.resolutionStack.length-1]===e.toString()&&this.resolutionStack.pop(),t&&(this.resolutionStack=[]);}}resolveService(e){switch(e.lifetime){case n.Singleton:return this.resolveSingleton(e);case n.Scoped:return this.resolveScoped(e);case n.Transient:return this.resolveTransient(e);default:throw new Error(`Unknown lifetime: ${e.lifetime}`)}}resolveSingleton(e){let{serviceIdentifier:t}=e;if(this.singletons.has(t.toString()))return this.singletons.get(t.toString());let r=this.createInstance(e);return this.singletons.set(t.toString(),r),r}resolveScoped(e){let{serviceIdentifier:t}=e;if(!this.parent)throw new Error(`Cannot resolve scoped service '${String(t)}' from root provider. Use createScope() first.`);if(this.scopedInstances.has(t.toString()))return this.scopedInstances.get(t.toString());let r=this.createInstance(e);return this.scopedInstances.set(t.toString(),r),r}resolveTransient(e){return this.createInstance(e)}createInstance(e){if(e.implementationInstance!==void 0)return e.implementationInstance;if(e.implementationFactory)return e.implementationFactory(this);if(e.implementationType)return this.constructInstance(e);throw new Error(`Cannot create instance for service '${String(e.serviceIdentifier)}'`)}constructInstance(e){let{implementationType:t,dependencies:r}=e;if(!t)throw new Error("Implementation type is required");if(r&&r.length>0){let s=r.map(l=>this.getRequiredService(l));return new t(...s)}let o=Reflect.getMetadata("inject:params",t),a=Reflect.getMetadata("design:paramtypes",t)||[],f=Math.max(a.length,o?Math.max(...Object.keys(o).map(s=>parseInt(s)))+1:0),S=[];for(let s=0;s<f;s++){let l=o?.[s];if(l)S.push(this.getRequiredService(l));else if(a[s]){let v=a[s].name||a[s];S.push(this.getRequiredService(v));}else throw new Error(`Cannot resolve parameter at index ${s} for ${t.name}`)}return new t(...S)}createScope(){let e=new u(this.serviceDescriptors,this);return {serviceProvider:e,dispose:p(()=>{e.scopedInstances.clear();},"dispose")}}has(e){return this.serviceDescriptors.some(t=>t.serviceIdentifier.toString()===e.toString())||(this.parent?.has(e.toString())??false)}};p(u,"ServiceProvider");var h=u;var m=class m{constructor(){c(this,"serviceDescriptors",[]);}addSingleton(e,t){return typeof t=="function"?this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:n.Singleton}):this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationInstance:t,lifetime:n.Singleton}),this}addScoped(e,t){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:n.Scoped}),this}addTransient(e,t){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:n.Transient}),this}addFactory(e,t,r=n.Transient){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationFactory:t,lifetime:r}),this}addScopedWithDependencies(e,t,r){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:n.Scoped,dependencies:r}),this}buildServiceProvider(){return new h([...this.serviceDescriptors])}getDescriptors(){return [...this.serviceDescriptors]}has(e){return this.serviceDescriptors.some(t=>t.serviceIdentifier.toString()===e.toString())}get count(){return this.serviceDescriptors.length}};p(m,"ServiceCollection");var g=m;function w(i){return (e,t,r)=>{let a={...Reflect.getOwnMetadata("inject:params",e)||{}};a[r]=i.toString(),Reflect.defineMetadata("inject:params",a,e);}}p(w,"Inject");exports.Inject=w;exports.ServiceCollection=g;exports.ServiceLifetime=n;exports.ServiceProvider=h;
package/dist/di/index.mjs CHANGED
@@ -1,2 +1,2 @@
1
1
  import'reflect-metadata';/* conduit - MIT License */
2
- var f=Object.defineProperty;var v=(i,e,t)=>e in i?f(i,e,{enumerable:true,configurable:true,writable:true,value:t}):i[e]=t;var o=(i,e)=>f(i,"name",{value:e,configurable:true});var s=(i,e,t)=>v(i,typeof e!="symbol"?e+"":e,t);var n=(function(i){return i.Singleton="SINGLETON",i.Scoped="SCOPED",i.Transient="TRANSIENT",i})({});var l=class l{constructor(e,t){s(this,"serviceDescriptors");s(this,"singletons",new Map);s(this,"scopedInstances",new Map);s(this,"parent");s(this,"resolutionStack",[]);this.serviceDescriptors=e,this.parent=t;}getService(e){try{return this.getRequiredService(e.toString())}catch{return}}getRequiredService(e){let t=this.resolutionStack.length===0;if(this.resolutionStack.includes(e.toString())){let r=[...this.resolutionStack,e.toString()].map(c=>String(c)).join(" \u2192 ");throw new Error(`Circular dependency detected: ${r}`)}this.resolutionStack.push(e.toString());try{let r=this.serviceDescriptors.find(c=>c.serviceIdentifier.toString()===e.toString());if(!r){if(this.parent)return this.parent.getRequiredService(e.toString());throw new Error(`Service '${String(e)}' not registered`)}return this.resolveService(r)}catch(r){throw this.resolutionStack.pop(),r}finally{this.resolutionStack[this.resolutionStack.length-1]===e.toString()&&this.resolutionStack.pop(),t&&(this.resolutionStack=[]);}}resolveService(e){switch(e.lifetime){case n.Singleton:return this.resolveSingleton(e);case n.Scoped:return this.resolveScoped(e);case n.Transient:return this.resolveTransient(e);default:throw new Error(`Unknown lifetime: ${e.lifetime}`)}}resolveSingleton(e){let{serviceIdentifier:t}=e;if(this.singletons.has(t.toString()))return this.singletons.get(t.toString());let r=this.createInstance(e);return this.singletons.set(t.toString(),r),r}resolveScoped(e){let{serviceIdentifier:t}=e;if(!this.parent)throw new Error(`Cannot resolve scoped service '${String(t)}' from root provider. Use createScope() first.`);if(this.scopedInstances.has(t.toString()))return this.scopedInstances.get(t.toString());let r=this.createInstance(e);return this.scopedInstances.set(t.toString(),r),r}resolveTransient(e){return this.createInstance(e)}createInstance(e){if(e.implementationInstance!==void 0)return e.implementationInstance;if(e.implementationFactory)return e.implementationFactory(this);if(e.implementationType)return this.constructInstance(e);throw new Error(`Cannot create instance for service '${String(e.serviceIdentifier)}'`)}constructInstance(e){let{implementationType:t,dependencies:r}=e;if(!t)throw new Error("Implementation type is required");if(r&&r.length>0){let h=r.map(S=>this.getRequiredService(S));return new t(...h)}let p=(Reflect.getMetadata("design:paramtypes",t)||[]).map((h,S)=>{let d=Reflect.getMetadata("inject:params",t)?.[S];if(d)return this.getRequiredService(d);let m=h.name||h;return this.getRequiredService(m)});return new t(...p)}createScope(){let e=new l(this.serviceDescriptors,this);return {serviceProvider:e,dispose:o(()=>{e.scopedInstances.clear();},"dispose")}}has(e){return this.serviceDescriptors.some(t=>t.serviceIdentifier.toString()===e.toString())||(this.parent?.has(e.toString())??false)}};o(l,"ServiceProvider");var a=l;var g=class g{constructor(){s(this,"serviceDescriptors",[]);}addSingleton(e,t){return typeof t=="function"?this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:n.Singleton}):this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationInstance:t,lifetime:n.Singleton}),this}addScoped(e,t){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:n.Scoped}),this}addTransient(e,t){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:n.Transient}),this}addFactory(e,t,r=n.Transient){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationFactory:t,lifetime:r}),this}addScopedWithDependencies(e,t,r){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:n.Scoped,dependencies:r}),this}buildServiceProvider(){return new a([...this.serviceDescriptors])}getDescriptors(){return [...this.serviceDescriptors]}has(e){return this.serviceDescriptors.some(t=>t.serviceIdentifier.toString()===e.toString())}get count(){return this.serviceDescriptors.length}};o(g,"ServiceCollection");var u=g;function I(i){return (e,t,r)=>{let p={...Reflect.getOwnMetadata("inject:params",e)||{}};p[r]=i.toString(),Reflect.defineMetadata("inject:params",p,e);}}o(I,"Inject");export{I as Inject,u as ServiceCollection,n as ServiceLifetime,a as ServiceProvider};
2
+ var d=Object.defineProperty;var I=(i,e,t)=>e in i?d(i,e,{enumerable:true,configurable:true,writable:true,value:t}):i[e]=t;var p=(i,e)=>d(i,"name",{value:e,configurable:true});var c=(i,e,t)=>I(i,typeof e!="symbol"?e+"":e,t);var n=(function(i){return i.Singleton="SINGLETON",i.Scoped="SCOPED",i.Transient="TRANSIENT",i})({});var u=class u{constructor(e,t){c(this,"serviceDescriptors");c(this,"singletons",new Map);c(this,"scopedInstances",new Map);c(this,"parent");c(this,"resolutionStack",[]);this.serviceDescriptors=e,this.parent=t;}getService(e){try{return this.getRequiredService(e.toString())}catch{return}}getRequiredService(e){let t=this.resolutionStack.length===0;if(this.resolutionStack.includes(e.toString())){let r=[...this.resolutionStack,e.toString()].map(o=>String(o)).join(" \u2192 ");throw new Error(`Circular dependency detected: ${r}`)}this.resolutionStack.push(e.toString());try{let r=this.serviceDescriptors.find(o=>o.serviceIdentifier.toString()===e.toString());if(!r){if(this.parent)return this.parent.getRequiredService(e.toString());throw new Error(`Service '${String(e)}' not registered`)}return this.resolveService(r)}catch(r){throw this.resolutionStack.pop(),r}finally{this.resolutionStack[this.resolutionStack.length-1]===e.toString()&&this.resolutionStack.pop(),t&&(this.resolutionStack=[]);}}resolveService(e){switch(e.lifetime){case n.Singleton:return this.resolveSingleton(e);case n.Scoped:return this.resolveScoped(e);case n.Transient:return this.resolveTransient(e);default:throw new Error(`Unknown lifetime: ${e.lifetime}`)}}resolveSingleton(e){let{serviceIdentifier:t}=e;if(this.singletons.has(t.toString()))return this.singletons.get(t.toString());let r=this.createInstance(e);return this.singletons.set(t.toString(),r),r}resolveScoped(e){let{serviceIdentifier:t}=e;if(!this.parent)throw new Error(`Cannot resolve scoped service '${String(t)}' from root provider. Use createScope() first.`);if(this.scopedInstances.has(t.toString()))return this.scopedInstances.get(t.toString());let r=this.createInstance(e);return this.scopedInstances.set(t.toString(),r),r}resolveTransient(e){return this.createInstance(e)}createInstance(e){if(e.implementationInstance!==void 0)return e.implementationInstance;if(e.implementationFactory)return e.implementationFactory(this);if(e.implementationType)return this.constructInstance(e);throw new Error(`Cannot create instance for service '${String(e.serviceIdentifier)}'`)}constructInstance(e){let{implementationType:t,dependencies:r}=e;if(!t)throw new Error("Implementation type is required");if(r&&r.length>0){let s=r.map(l=>this.getRequiredService(l));return new t(...s)}let o=Reflect.getMetadata("inject:params",t),a=Reflect.getMetadata("design:paramtypes",t)||[],f=Math.max(a.length,o?Math.max(...Object.keys(o).map(s=>parseInt(s)))+1:0),S=[];for(let s=0;s<f;s++){let l=o?.[s];if(l)S.push(this.getRequiredService(l));else if(a[s]){let v=a[s].name||a[s];S.push(this.getRequiredService(v));}else throw new Error(`Cannot resolve parameter at index ${s} for ${t.name}`)}return new t(...S)}createScope(){let e=new u(this.serviceDescriptors,this);return {serviceProvider:e,dispose:p(()=>{e.scopedInstances.clear();},"dispose")}}has(e){return this.serviceDescriptors.some(t=>t.serviceIdentifier.toString()===e.toString())||(this.parent?.has(e.toString())??false)}};p(u,"ServiceProvider");var h=u;var m=class m{constructor(){c(this,"serviceDescriptors",[]);}addSingleton(e,t){return typeof t=="function"?this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:n.Singleton}):this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationInstance:t,lifetime:n.Singleton}),this}addScoped(e,t){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:n.Scoped}),this}addTransient(e,t){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:n.Transient}),this}addFactory(e,t,r=n.Transient){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationFactory:t,lifetime:r}),this}addScopedWithDependencies(e,t,r){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:n.Scoped,dependencies:r}),this}buildServiceProvider(){return new h([...this.serviceDescriptors])}getDescriptors(){return [...this.serviceDescriptors]}has(e){return this.serviceDescriptors.some(t=>t.serviceIdentifier.toString()===e.toString())}get count(){return this.serviceDescriptors.length}};p(m,"ServiceCollection");var g=m;function w(i){return (e,t,r)=>{let a={...Reflect.getOwnMetadata("inject:params",e)||{}};a[r]=i.toString(),Reflect.defineMetadata("inject:params",a,e);}}p(w,"Inject");export{w as Inject,g as ServiceCollection,n as ServiceLifetime,h as ServiceProvider};
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
1
  'use strict';require('reflect-metadata');var uuid=require('uuid');/* conduit - MIT License */
2
- var H=Object.defineProperty;var F=(o,e,t)=>e in o?H(o,e,{enumerable:true,configurable:true,writable:true,value:t}):o[e]=t;var s=(o,e)=>H(o,"name",{value:e,configurable:true});var i=(o,e,t)=>F(o,typeof e!="symbol"?e+"":e,t);var n=(function(o){return o.Singleton="SINGLETON",o.Scoped="SCOPED",o.Transient="TRANSIENT",o})({});var g=class g{constructor(e,t){i(this,"serviceDescriptors");i(this,"singletons",new Map);i(this,"scopedInstances",new Map);i(this,"parent");i(this,"resolutionStack",[]);this.serviceDescriptors=e,this.parent=t;}getService(e){try{return this.getRequiredService(e.toString())}catch{return}}getRequiredService(e){let t=this.resolutionStack.length===0;if(this.resolutionStack.includes(e.toString())){let r=[...this.resolutionStack,e.toString()].map(a=>String(a)).join(" \u2192 ");throw new Error(`Circular dependency detected: ${r}`)}this.resolutionStack.push(e.toString());try{let r=this.serviceDescriptors.find(a=>a.serviceIdentifier.toString()===e.toString());if(!r){if(this.parent)return this.parent.getRequiredService(e.toString());throw new Error(`Service '${String(e)}' not registered`)}return this.resolveService(r)}catch(r){throw this.resolutionStack.pop(),r}finally{this.resolutionStack[this.resolutionStack.length-1]===e.toString()&&this.resolutionStack.pop(),t&&(this.resolutionStack=[]);}}resolveService(e){switch(e.lifetime){case n.Singleton:return this.resolveSingleton(e);case n.Scoped:return this.resolveScoped(e);case n.Transient:return this.resolveTransient(e);default:throw new Error(`Unknown lifetime: ${e.lifetime}`)}}resolveSingleton(e){let{serviceIdentifier:t}=e;if(this.singletons.has(t.toString()))return this.singletons.get(t.toString());let r=this.createInstance(e);return this.singletons.set(t.toString(),r),r}resolveScoped(e){let{serviceIdentifier:t}=e;if(!this.parent)throw new Error(`Cannot resolve scoped service '${String(t)}' from root provider. Use createScope() first.`);if(this.scopedInstances.has(t.toString()))return this.scopedInstances.get(t.toString());let r=this.createInstance(e);return this.scopedInstances.set(t.toString(),r),r}resolveTransient(e){return this.createInstance(e)}createInstance(e){if(e.implementationInstance!==void 0)return e.implementationInstance;if(e.implementationFactory)return e.implementationFactory(this);if(e.implementationType)return this.constructInstance(e);throw new Error(`Cannot create instance for service '${String(e.serviceIdentifier)}'`)}constructInstance(e){let{implementationType:t,dependencies:r}=e;if(!t)throw new Error("Implementation type is required");if(r&&r.length>0){let h=r.map(v=>this.getRequiredService(v));return new t(...h)}let c=(Reflect.getMetadata("design:paramtypes",t)||[]).map((h,v)=>{let A=Reflect.getMetadata("inject:params",t)?.[v];if(A)return this.getRequiredService(A);let q=h.name||h;return this.getRequiredService(q)});return new t(...c)}createScope(){let e=new g(this.serviceDescriptors,this);return {serviceProvider:e,dispose:s(()=>{e.scopedInstances.clear();},"dispose")}}has(e){return this.serviceDescriptors.some(t=>t.serviceIdentifier.toString()===e.toString())||(this.parent?.has(e.toString())??false)}};s(g,"ServiceProvider");var d=g;var S=class S{constructor(){i(this,"serviceDescriptors",[]);}addSingleton(e,t){return typeof t=="function"?this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:n.Singleton}):this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationInstance:t,lifetime:n.Singleton}),this}addScoped(e,t){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:n.Scoped}),this}addTransient(e,t){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:n.Transient}),this}addFactory(e,t,r=n.Transient){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationFactory:t,lifetime:r}),this}addScopedWithDependencies(e,t,r){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:n.Scoped,dependencies:r}),this}buildServiceProvider(){return new d([...this.serviceDescriptors])}getDescriptors(){return [...this.serviceDescriptors]}has(e){return this.serviceDescriptors.some(t=>t.serviceIdentifier.toString()===e.toString())}get count(){return this.serviceDescriptors.length}};s(S,"ServiceCollection");var u=S;function N(o){return (e,t,r)=>{let c={...Reflect.getOwnMetadata("inject:params",e)||{}};c[r]=o.toString(),Reflect.defineMetadata("inject:params",c,e);}}s(N,"Inject");var w=class w{constructor(e){i(this,"request");i(this,"response");i(this,"metadata",new Map);this.request=e;}setMetadata(e,t){this.metadata.set(e,t);}getMetadata(e){return this.metadata.get(e)}};s(w,"PipelineContext");var l=w;var y=class y{constructor(e){i(this,"serviceProvider");i(this,"behaviors",[]);this.serviceProvider=e;let t=this.serviceProvider.getService("PipelineBehaviors");t&&(this.behaviors=t);}async send(e){let t=this.serviceProvider.createScope();try{let r=await this.executeHandler(e,t.serviceProvider);if(this.isAsyncIterable(r))throw new Error("Handler returned a stream. Use stream() method instead of send().");return r}finally{t.dispose();}}async stream(e){let t=this.serviceProvider.createScope();try{let r=await this.executeHandler(e,t.serviceProvider);if(!this.isAsyncIterable(r))throw new Error("Handler did not return a stream. Use send() method instead of stream().");return r}finally{t.dispose();}}async executeHandler(e,t){let r=t.getService(e.type);if(!r)throw new Error(`No handler registered for request type: ${String(e.type)}`);let a=new l(e);return this.behaviors.length===0?await r.handle(e):await this.executePipeline(a,r,this.behaviors)}async executePipeline(e,t,r){let a=0,c=s(async()=>{if(a<r.length)return await r[a++].handle(e,c);let h=await t.handle(e.request);return e.response=h,h},"next");return await c()}isAsyncIterable(e){return e!=null&&typeof e[Symbol.asyncIterator]=="function"}};s(y,"Mediator");var f=y;var D=class D{handle(e){return this.handleSimple(e)}};s(D,"SimpleHandler");var x=D,P=class P{handle(e){return this.handleStream(e)}};s(P,"StreamingHandler");var I=P;var E=class E{constructor(e){i(this,"type");this.type=e.toString();}};s(E,"BaseCommunicationObject");var p=E;var R=class R extends p{constructor(t){super(t);i(this,"uuid");i(this,"timestamp");this.uuid=uuid.v4(),this.timestamp=new Date;}};s(R,"BaseRequest");var b=R;var k=class k extends p{constructor(t){super(t);i(this,"success");i(this,"errors");i(this,"timestamp");this.timestamp=new Date,this.success=true;}setErrors(t){return this.success=false,this.errors=t,this}addError(t){return this.errors||(this.errors=[]),this.errors.push(t),this.success=false,this}};s(k,"BaseResponse");var T=k;var M=class M{constructor(e,t,r){i(this,"serviceProvider");i(this,"mediator");i(this,"isRunning",false);i(this,"adapters");this.serviceProvider=e,this.mediator=t,this.adapters=r;}async run(){if(this.isRunning)throw new Error("Application is already running");for(let e of this.adapters)e.initialize&&await e.initialize(this);this.isRunning=true;}async stop(){if(this.isRunning){for(let e of [...this.adapters].reverse())e.dispose&&await e.dispose();this.isRunning=false;}}async send(e){if(!this.isRunning)throw new Error("Application must be running before sending requests. Call app.run() first.");return this.mediator.send(e)}async stream(e){if(!this.isRunning)throw new Error("Application must be running before streaming requests. Call app.run() first.");return this.mediator.stream(e)}getServiceProvider(){return this.serviceProvider}getMediator(){return this.mediator}get running(){return this.isRunning}};s(M,"Application");var m=M;var B="PipelineBehaviors",C=class C{constructor(){i(this,"services");i(this,"adapters",[]);i(this,"pipelineBehaviors",[]);this.services=new u;}registerHandler(e,t,r=n.Transient){if(typeof t=="function")switch(r){case n.Singleton:this.services.addSingleton(e,t);break;case n.Scoped:this.services.addScoped(e,t);break;case n.Transient:default:this.services.addTransient(e,t);break}else this.services.addSingleton(e,t);return this}registerHandlers(e){return e.forEach((t,r)=>{this.registerHandler(r,t);}),this}registerBehavior(e){return this.pipelineBehaviors.push(e),this}registerSingleton(e,t){return this.services.addSingleton(e,t),this}registerScoped(e,t){return this.services.addScoped(e,t),this}registerTransient(e,t){return this.services.addTransient(e,t),this}registerFactory(e,t,r=n.Transient){return this.services.addFactory(e,t,r),this}use(e){return this.adapters.push(e),this}getServiceCollection(){return this.services}async build(){for(let r of this.adapters)await r.configure(this.services);this.pipelineBehaviors.length>0&&this.services.addSingleton(B,this.pipelineBehaviors.map(r=>typeof r=="function"?new r:r));let e=this.services.buildServiceProvider(),t=new f(e);return new m(e,t,this.adapters)}};s(C,"ApplicationBuilder");var j=C;exports.Application=m;exports.ApplicationBuilder=j;exports.BaseCommunicationObject=p;exports.BaseRequest=b;exports.BaseResponse=T;exports.Inject=N;exports.Mediator=f;exports.PipelineContext=l;exports.ServiceCollection=u;exports.ServiceLifetime=n;exports.ServiceProvider=d;exports.SimpleHandler=x;exports.StreamingHandler=I;
2
+ var H=Object.defineProperty;var F=(o,e,t)=>e in o?H(o,e,{enumerable:true,configurable:true,writable:true,value:t}):o[e]=t;var s=(o,e)=>H(o,"name",{value:e,configurable:true});var i=(o,e,t)=>F(o,typeof e!="symbol"?e+"":e,t);var n=(function(o){return o.Singleton="SINGLETON",o.Scoped="SCOPED",o.Transient="TRANSIENT",o})({});var w=class w{constructor(e,t){i(this,"serviceDescriptors");i(this,"singletons",new Map);i(this,"scopedInstances",new Map);i(this,"parent");i(this,"resolutionStack",[]);this.serviceDescriptors=e,this.parent=t;}getService(e){try{return this.getRequiredService(e.toString())}catch{return}}getRequiredService(e){let t=this.resolutionStack.length===0;if(this.resolutionStack.includes(e.toString())){let r=[...this.resolutionStack,e.toString()].map(a=>String(a)).join(" \u2192 ");throw new Error(`Circular dependency detected: ${r}`)}this.resolutionStack.push(e.toString());try{let r=this.serviceDescriptors.find(a=>a.serviceIdentifier.toString()===e.toString());if(!r){if(this.parent)return this.parent.getRequiredService(e.toString());throw new Error(`Service '${String(e)}' not registered`)}return this.resolveService(r)}catch(r){throw this.resolutionStack.pop(),r}finally{this.resolutionStack[this.resolutionStack.length-1]===e.toString()&&this.resolutionStack.pop(),t&&(this.resolutionStack=[]);}}resolveService(e){switch(e.lifetime){case n.Singleton:return this.resolveSingleton(e);case n.Scoped:return this.resolveScoped(e);case n.Transient:return this.resolveTransient(e);default:throw new Error(`Unknown lifetime: ${e.lifetime}`)}}resolveSingleton(e){let{serviceIdentifier:t}=e;if(this.singletons.has(t.toString()))return this.singletons.get(t.toString());let r=this.createInstance(e);return this.singletons.set(t.toString(),r),r}resolveScoped(e){let{serviceIdentifier:t}=e;if(!this.parent)throw new Error(`Cannot resolve scoped service '${String(t)}' from root provider. Use createScope() first.`);if(this.scopedInstances.has(t.toString()))return this.scopedInstances.get(t.toString());let r=this.createInstance(e);return this.scopedInstances.set(t.toString(),r),r}resolveTransient(e){return this.createInstance(e)}createInstance(e){if(e.implementationInstance!==void 0)return e.implementationInstance;if(e.implementationFactory)return e.implementationFactory(this);if(e.implementationType)return this.constructInstance(e);throw new Error(`Cannot create instance for service '${String(e.serviceIdentifier)}'`)}constructInstance(e){let{implementationType:t,dependencies:r}=e;if(!t)throw new Error("Implementation type is required");if(r&&r.length>0){let c=r.map(S=>this.getRequiredService(S));return new t(...c)}let a=Reflect.getMetadata("inject:params",t),h=Reflect.getMetadata("design:paramtypes",t)||[],v=Math.max(h.length,a?Math.max(...Object.keys(a).map(c=>parseInt(c)))+1:0),g=[];for(let c=0;c<v;c++){let S=a?.[c];if(S)g.push(this.getRequiredService(S));else if(h[c]){let q=h[c].name||h[c];g.push(this.getRequiredService(q));}else throw new Error(`Cannot resolve parameter at index ${c} for ${t.name}`)}return new t(...g)}createScope(){let e=new w(this.serviceDescriptors,this);return {serviceProvider:e,dispose:s(()=>{e.scopedInstances.clear();},"dispose")}}has(e){return this.serviceDescriptors.some(t=>t.serviceIdentifier.toString()===e.toString())||(this.parent?.has(e.toString())??false)}};s(w,"ServiceProvider");var d=w;var y=class y{constructor(){i(this,"serviceDescriptors",[]);}addSingleton(e,t){return typeof t=="function"?this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:n.Singleton}):this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationInstance:t,lifetime:n.Singleton}),this}addScoped(e,t){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:n.Scoped}),this}addTransient(e,t){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:n.Transient}),this}addFactory(e,t,r=n.Transient){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationFactory:t,lifetime:r}),this}addScopedWithDependencies(e,t,r){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:n.Scoped,dependencies:r}),this}buildServiceProvider(){return new d([...this.serviceDescriptors])}getDescriptors(){return [...this.serviceDescriptors]}has(e){return this.serviceDescriptors.some(t=>t.serviceIdentifier.toString()===e.toString())}get count(){return this.serviceDescriptors.length}};s(y,"ServiceCollection");var u=y;function N(o){return (e,t,r)=>{let h={...Reflect.getOwnMetadata("inject:params",e)||{}};h[r]=o.toString(),Reflect.defineMetadata("inject:params",h,e);}}s(N,"Inject");var x=class x{constructor(e){i(this,"request");i(this,"response");i(this,"metadata",new Map);this.request=e;}setMetadata(e,t){this.metadata.set(e,t);}getMetadata(e){return this.metadata.get(e)}};s(x,"PipelineContext");var l=x;var I=class I{constructor(e){i(this,"serviceProvider");i(this,"behaviors",[]);this.serviceProvider=e;let t=this.serviceProvider.getService("PipelineBehaviors");t&&(this.behaviors=t);}async send(e){let t=this.serviceProvider.createScope();try{let r=await this.executeHandler(e,t.serviceProvider);if(this.isAsyncIterable(r))throw new Error("Handler returned a stream. Use stream() method instead of send().");return r}finally{t.dispose();}}async stream(e){let t=this.serviceProvider.createScope();try{let r=await this.executeHandler(e,t.serviceProvider);if(!this.isAsyncIterable(r))throw new Error("Handler did not return a stream. Use send() method instead of stream().");return r}finally{t.dispose();}}async executeHandler(e,t){let r=t.getService(e.type);if(!r)throw new Error(`No handler registered for request type: ${String(e.type)}`);let a=new l(e);return this.behaviors.length===0?await r.handle(e):await this.executePipeline(a,r,this.behaviors)}async executePipeline(e,t,r){let a=0,h=s(async()=>{if(a<r.length)return await r[a++].handle(e,h);let v=await t.handle(e.request);return e.response=v,v},"next");return await h()}isAsyncIterable(e){return e!=null&&typeof e[Symbol.asyncIterator]=="function"}};s(I,"Mediator");var f=I;var P=class P{handle(e){return this.handleSimple(e)}};s(P,"SimpleHandler");var D=P,b=class b{handle(e){return this.handleStream(e)}};s(b,"StreamingHandler");var E=b;var R=class R{constructor(e){i(this,"type");this.type=e.toString();}};s(R,"BaseCommunicationObject");var p=R;var k=class k extends p{constructor(t){super(t);i(this,"uuid");i(this,"timestamp");this.uuid=uuid.v4(),this.timestamp=new Date;}};s(k,"BaseRequest");var T=k;var j=class j extends p{constructor(t){super(t);i(this,"success");i(this,"errors");i(this,"timestamp");this.timestamp=new Date,this.success=true;}setErrors(t){return this.success=false,this.errors=t,this}addError(t){return this.errors||(this.errors=[]),this.errors.push(t),this.success=false,this}};s(j,"BaseResponse");var M=j;var C=class C{constructor(e,t,r){i(this,"serviceProvider");i(this,"mediator");i(this,"isRunning",false);i(this,"adapters");this.serviceProvider=e,this.mediator=t,this.adapters=r;}async run(){if(this.isRunning)throw new Error("Application is already running");for(let e of this.adapters)e.initialize&&await e.initialize(this);this.isRunning=true;}async stop(){if(this.isRunning){for(let e of [...this.adapters].reverse())e.dispose&&await e.dispose();this.isRunning=false;}}async send(e){if(!this.isRunning)throw new Error("Application must be running before sending requests. Call app.run() first.");return this.mediator.send(e)}async stream(e){if(!this.isRunning)throw new Error("Application must be running before streaming requests. Call app.run() first.");return this.mediator.stream(e)}getServiceProvider(){return this.serviceProvider}getMediator(){return this.mediator}get running(){return this.isRunning}};s(C,"Application");var m=C;var B="PipelineBehaviors",A=class A{constructor(){i(this,"services");i(this,"adapters",[]);i(this,"pipelineBehaviors",[]);this.services=new u;}registerHandler(e,t,r=n.Transient){if(typeof t=="function")switch(r){case n.Singleton:this.services.addSingleton(e,t);break;case n.Scoped:this.services.addScoped(e,t);break;case n.Transient:default:this.services.addTransient(e,t);break}else this.services.addSingleton(e,t);return this}registerHandlers(e){return e.forEach((t,r)=>{this.registerHandler(r,t);}),this}registerBehavior(e){return this.pipelineBehaviors.push(e),this}registerSingleton(e,t){return this.services.addSingleton(e,t),this}registerScoped(e,t){return this.services.addScoped(e,t),this}registerTransient(e,t){return this.services.addTransient(e,t),this}registerFactory(e,t,r=n.Transient){return this.services.addFactory(e,t,r),this}use(e){return this.adapters.push(e),this}getServiceCollection(){return this.services}async build(){for(let r of this.adapters)await r.configure(this.services);this.pipelineBehaviors.length>0&&this.services.addSingleton(B,this.pipelineBehaviors.map(r=>typeof r=="function"?new r:r));let e=this.services.buildServiceProvider(),t=new f(e);return new m(e,t,this.adapters)}};s(A,"ApplicationBuilder");var $=A;exports.Application=m;exports.ApplicationBuilder=$;exports.BaseCommunicationObject=p;exports.BaseRequest=T;exports.BaseResponse=M;exports.Inject=N;exports.Mediator=f;exports.PipelineContext=l;exports.ServiceCollection=u;exports.ServiceLifetime=n;exports.ServiceProvider=d;exports.SimpleHandler=D;exports.StreamingHandler=E;
package/dist/index.mjs CHANGED
@@ -1,2 +1,2 @@
1
1
  import'reflect-metadata';import {v4}from'uuid';/* conduit - MIT License */
2
- var H=Object.defineProperty;var F=(o,e,t)=>e in o?H(o,e,{enumerable:true,configurable:true,writable:true,value:t}):o[e]=t;var s=(o,e)=>H(o,"name",{value:e,configurable:true});var i=(o,e,t)=>F(o,typeof e!="symbol"?e+"":e,t);var n=(function(o){return o.Singleton="SINGLETON",o.Scoped="SCOPED",o.Transient="TRANSIENT",o})({});var g=class g{constructor(e,t){i(this,"serviceDescriptors");i(this,"singletons",new Map);i(this,"scopedInstances",new Map);i(this,"parent");i(this,"resolutionStack",[]);this.serviceDescriptors=e,this.parent=t;}getService(e){try{return this.getRequiredService(e.toString())}catch{return}}getRequiredService(e){let t=this.resolutionStack.length===0;if(this.resolutionStack.includes(e.toString())){let r=[...this.resolutionStack,e.toString()].map(a=>String(a)).join(" \u2192 ");throw new Error(`Circular dependency detected: ${r}`)}this.resolutionStack.push(e.toString());try{let r=this.serviceDescriptors.find(a=>a.serviceIdentifier.toString()===e.toString());if(!r){if(this.parent)return this.parent.getRequiredService(e.toString());throw new Error(`Service '${String(e)}' not registered`)}return this.resolveService(r)}catch(r){throw this.resolutionStack.pop(),r}finally{this.resolutionStack[this.resolutionStack.length-1]===e.toString()&&this.resolutionStack.pop(),t&&(this.resolutionStack=[]);}}resolveService(e){switch(e.lifetime){case n.Singleton:return this.resolveSingleton(e);case n.Scoped:return this.resolveScoped(e);case n.Transient:return this.resolveTransient(e);default:throw new Error(`Unknown lifetime: ${e.lifetime}`)}}resolveSingleton(e){let{serviceIdentifier:t}=e;if(this.singletons.has(t.toString()))return this.singletons.get(t.toString());let r=this.createInstance(e);return this.singletons.set(t.toString(),r),r}resolveScoped(e){let{serviceIdentifier:t}=e;if(!this.parent)throw new Error(`Cannot resolve scoped service '${String(t)}' from root provider. Use createScope() first.`);if(this.scopedInstances.has(t.toString()))return this.scopedInstances.get(t.toString());let r=this.createInstance(e);return this.scopedInstances.set(t.toString(),r),r}resolveTransient(e){return this.createInstance(e)}createInstance(e){if(e.implementationInstance!==void 0)return e.implementationInstance;if(e.implementationFactory)return e.implementationFactory(this);if(e.implementationType)return this.constructInstance(e);throw new Error(`Cannot create instance for service '${String(e.serviceIdentifier)}'`)}constructInstance(e){let{implementationType:t,dependencies:r}=e;if(!t)throw new Error("Implementation type is required");if(r&&r.length>0){let h=r.map(v=>this.getRequiredService(v));return new t(...h)}let c=(Reflect.getMetadata("design:paramtypes",t)||[]).map((h,v)=>{let A=Reflect.getMetadata("inject:params",t)?.[v];if(A)return this.getRequiredService(A);let q=h.name||h;return this.getRequiredService(q)});return new t(...c)}createScope(){let e=new g(this.serviceDescriptors,this);return {serviceProvider:e,dispose:s(()=>{e.scopedInstances.clear();},"dispose")}}has(e){return this.serviceDescriptors.some(t=>t.serviceIdentifier.toString()===e.toString())||(this.parent?.has(e.toString())??false)}};s(g,"ServiceProvider");var d=g;var S=class S{constructor(){i(this,"serviceDescriptors",[]);}addSingleton(e,t){return typeof t=="function"?this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:n.Singleton}):this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationInstance:t,lifetime:n.Singleton}),this}addScoped(e,t){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:n.Scoped}),this}addTransient(e,t){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:n.Transient}),this}addFactory(e,t,r=n.Transient){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationFactory:t,lifetime:r}),this}addScopedWithDependencies(e,t,r){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:n.Scoped,dependencies:r}),this}buildServiceProvider(){return new d([...this.serviceDescriptors])}getDescriptors(){return [...this.serviceDescriptors]}has(e){return this.serviceDescriptors.some(t=>t.serviceIdentifier.toString()===e.toString())}get count(){return this.serviceDescriptors.length}};s(S,"ServiceCollection");var u=S;function N(o){return (e,t,r)=>{let c={...Reflect.getOwnMetadata("inject:params",e)||{}};c[r]=o.toString(),Reflect.defineMetadata("inject:params",c,e);}}s(N,"Inject");var w=class w{constructor(e){i(this,"request");i(this,"response");i(this,"metadata",new Map);this.request=e;}setMetadata(e,t){this.metadata.set(e,t);}getMetadata(e){return this.metadata.get(e)}};s(w,"PipelineContext");var l=w;var y=class y{constructor(e){i(this,"serviceProvider");i(this,"behaviors",[]);this.serviceProvider=e;let t=this.serviceProvider.getService("PipelineBehaviors");t&&(this.behaviors=t);}async send(e){let t=this.serviceProvider.createScope();try{let r=await this.executeHandler(e,t.serviceProvider);if(this.isAsyncIterable(r))throw new Error("Handler returned a stream. Use stream() method instead of send().");return r}finally{t.dispose();}}async stream(e){let t=this.serviceProvider.createScope();try{let r=await this.executeHandler(e,t.serviceProvider);if(!this.isAsyncIterable(r))throw new Error("Handler did not return a stream. Use send() method instead of stream().");return r}finally{t.dispose();}}async executeHandler(e,t){let r=t.getService(e.type);if(!r)throw new Error(`No handler registered for request type: ${String(e.type)}`);let a=new l(e);return this.behaviors.length===0?await r.handle(e):await this.executePipeline(a,r,this.behaviors)}async executePipeline(e,t,r){let a=0,c=s(async()=>{if(a<r.length)return await r[a++].handle(e,c);let h=await t.handle(e.request);return e.response=h,h},"next");return await c()}isAsyncIterable(e){return e!=null&&typeof e[Symbol.asyncIterator]=="function"}};s(y,"Mediator");var f=y;var D=class D{handle(e){return this.handleSimple(e)}};s(D,"SimpleHandler");var x=D,P=class P{handle(e){return this.handleStream(e)}};s(P,"StreamingHandler");var I=P;var E=class E{constructor(e){i(this,"type");this.type=e.toString();}};s(E,"BaseCommunicationObject");var p=E;var R=class R extends p{constructor(t){super(t);i(this,"uuid");i(this,"timestamp");this.uuid=v4(),this.timestamp=new Date;}};s(R,"BaseRequest");var b=R;var k=class k extends p{constructor(t){super(t);i(this,"success");i(this,"errors");i(this,"timestamp");this.timestamp=new Date,this.success=true;}setErrors(t){return this.success=false,this.errors=t,this}addError(t){return this.errors||(this.errors=[]),this.errors.push(t),this.success=false,this}};s(k,"BaseResponse");var T=k;var M=class M{constructor(e,t,r){i(this,"serviceProvider");i(this,"mediator");i(this,"isRunning",false);i(this,"adapters");this.serviceProvider=e,this.mediator=t,this.adapters=r;}async run(){if(this.isRunning)throw new Error("Application is already running");for(let e of this.adapters)e.initialize&&await e.initialize(this);this.isRunning=true;}async stop(){if(this.isRunning){for(let e of [...this.adapters].reverse())e.dispose&&await e.dispose();this.isRunning=false;}}async send(e){if(!this.isRunning)throw new Error("Application must be running before sending requests. Call app.run() first.");return this.mediator.send(e)}async stream(e){if(!this.isRunning)throw new Error("Application must be running before streaming requests. Call app.run() first.");return this.mediator.stream(e)}getServiceProvider(){return this.serviceProvider}getMediator(){return this.mediator}get running(){return this.isRunning}};s(M,"Application");var m=M;var B="PipelineBehaviors",C=class C{constructor(){i(this,"services");i(this,"adapters",[]);i(this,"pipelineBehaviors",[]);this.services=new u;}registerHandler(e,t,r=n.Transient){if(typeof t=="function")switch(r){case n.Singleton:this.services.addSingleton(e,t);break;case n.Scoped:this.services.addScoped(e,t);break;case n.Transient:default:this.services.addTransient(e,t);break}else this.services.addSingleton(e,t);return this}registerHandlers(e){return e.forEach((t,r)=>{this.registerHandler(r,t);}),this}registerBehavior(e){return this.pipelineBehaviors.push(e),this}registerSingleton(e,t){return this.services.addSingleton(e,t),this}registerScoped(e,t){return this.services.addScoped(e,t),this}registerTransient(e,t){return this.services.addTransient(e,t),this}registerFactory(e,t,r=n.Transient){return this.services.addFactory(e,t,r),this}use(e){return this.adapters.push(e),this}getServiceCollection(){return this.services}async build(){for(let r of this.adapters)await r.configure(this.services);this.pipelineBehaviors.length>0&&this.services.addSingleton(B,this.pipelineBehaviors.map(r=>typeof r=="function"?new r:r));let e=this.services.buildServiceProvider(),t=new f(e);return new m(e,t,this.adapters)}};s(C,"ApplicationBuilder");var j=C;export{m as Application,j as ApplicationBuilder,p as BaseCommunicationObject,b as BaseRequest,T as BaseResponse,N as Inject,f as Mediator,l as PipelineContext,u as ServiceCollection,n as ServiceLifetime,d as ServiceProvider,x as SimpleHandler,I as StreamingHandler};
2
+ var H=Object.defineProperty;var F=(o,e,t)=>e in o?H(o,e,{enumerable:true,configurable:true,writable:true,value:t}):o[e]=t;var s=(o,e)=>H(o,"name",{value:e,configurable:true});var i=(o,e,t)=>F(o,typeof e!="symbol"?e+"":e,t);var n=(function(o){return o.Singleton="SINGLETON",o.Scoped="SCOPED",o.Transient="TRANSIENT",o})({});var w=class w{constructor(e,t){i(this,"serviceDescriptors");i(this,"singletons",new Map);i(this,"scopedInstances",new Map);i(this,"parent");i(this,"resolutionStack",[]);this.serviceDescriptors=e,this.parent=t;}getService(e){try{return this.getRequiredService(e.toString())}catch{return}}getRequiredService(e){let t=this.resolutionStack.length===0;if(this.resolutionStack.includes(e.toString())){let r=[...this.resolutionStack,e.toString()].map(a=>String(a)).join(" \u2192 ");throw new Error(`Circular dependency detected: ${r}`)}this.resolutionStack.push(e.toString());try{let r=this.serviceDescriptors.find(a=>a.serviceIdentifier.toString()===e.toString());if(!r){if(this.parent)return this.parent.getRequiredService(e.toString());throw new Error(`Service '${String(e)}' not registered`)}return this.resolveService(r)}catch(r){throw this.resolutionStack.pop(),r}finally{this.resolutionStack[this.resolutionStack.length-1]===e.toString()&&this.resolutionStack.pop(),t&&(this.resolutionStack=[]);}}resolveService(e){switch(e.lifetime){case n.Singleton:return this.resolveSingleton(e);case n.Scoped:return this.resolveScoped(e);case n.Transient:return this.resolveTransient(e);default:throw new Error(`Unknown lifetime: ${e.lifetime}`)}}resolveSingleton(e){let{serviceIdentifier:t}=e;if(this.singletons.has(t.toString()))return this.singletons.get(t.toString());let r=this.createInstance(e);return this.singletons.set(t.toString(),r),r}resolveScoped(e){let{serviceIdentifier:t}=e;if(!this.parent)throw new Error(`Cannot resolve scoped service '${String(t)}' from root provider. Use createScope() first.`);if(this.scopedInstances.has(t.toString()))return this.scopedInstances.get(t.toString());let r=this.createInstance(e);return this.scopedInstances.set(t.toString(),r),r}resolveTransient(e){return this.createInstance(e)}createInstance(e){if(e.implementationInstance!==void 0)return e.implementationInstance;if(e.implementationFactory)return e.implementationFactory(this);if(e.implementationType)return this.constructInstance(e);throw new Error(`Cannot create instance for service '${String(e.serviceIdentifier)}'`)}constructInstance(e){let{implementationType:t,dependencies:r}=e;if(!t)throw new Error("Implementation type is required");if(r&&r.length>0){let c=r.map(S=>this.getRequiredService(S));return new t(...c)}let a=Reflect.getMetadata("inject:params",t),h=Reflect.getMetadata("design:paramtypes",t)||[],v=Math.max(h.length,a?Math.max(...Object.keys(a).map(c=>parseInt(c)))+1:0),g=[];for(let c=0;c<v;c++){let S=a?.[c];if(S)g.push(this.getRequiredService(S));else if(h[c]){let q=h[c].name||h[c];g.push(this.getRequiredService(q));}else throw new Error(`Cannot resolve parameter at index ${c} for ${t.name}`)}return new t(...g)}createScope(){let e=new w(this.serviceDescriptors,this);return {serviceProvider:e,dispose:s(()=>{e.scopedInstances.clear();},"dispose")}}has(e){return this.serviceDescriptors.some(t=>t.serviceIdentifier.toString()===e.toString())||(this.parent?.has(e.toString())??false)}};s(w,"ServiceProvider");var d=w;var y=class y{constructor(){i(this,"serviceDescriptors",[]);}addSingleton(e,t){return typeof t=="function"?this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:n.Singleton}):this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationInstance:t,lifetime:n.Singleton}),this}addScoped(e,t){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:n.Scoped}),this}addTransient(e,t){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:n.Transient}),this}addFactory(e,t,r=n.Transient){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationFactory:t,lifetime:r}),this}addScopedWithDependencies(e,t,r){return this.serviceDescriptors.push({serviceIdentifier:e.toString(),implementationType:t,lifetime:n.Scoped,dependencies:r}),this}buildServiceProvider(){return new d([...this.serviceDescriptors])}getDescriptors(){return [...this.serviceDescriptors]}has(e){return this.serviceDescriptors.some(t=>t.serviceIdentifier.toString()===e.toString())}get count(){return this.serviceDescriptors.length}};s(y,"ServiceCollection");var u=y;function N(o){return (e,t,r)=>{let h={...Reflect.getOwnMetadata("inject:params",e)||{}};h[r]=o.toString(),Reflect.defineMetadata("inject:params",h,e);}}s(N,"Inject");var x=class x{constructor(e){i(this,"request");i(this,"response");i(this,"metadata",new Map);this.request=e;}setMetadata(e,t){this.metadata.set(e,t);}getMetadata(e){return this.metadata.get(e)}};s(x,"PipelineContext");var l=x;var I=class I{constructor(e){i(this,"serviceProvider");i(this,"behaviors",[]);this.serviceProvider=e;let t=this.serviceProvider.getService("PipelineBehaviors");t&&(this.behaviors=t);}async send(e){let t=this.serviceProvider.createScope();try{let r=await this.executeHandler(e,t.serviceProvider);if(this.isAsyncIterable(r))throw new Error("Handler returned a stream. Use stream() method instead of send().");return r}finally{t.dispose();}}async stream(e){let t=this.serviceProvider.createScope();try{let r=await this.executeHandler(e,t.serviceProvider);if(!this.isAsyncIterable(r))throw new Error("Handler did not return a stream. Use send() method instead of stream().");return r}finally{t.dispose();}}async executeHandler(e,t){let r=t.getService(e.type);if(!r)throw new Error(`No handler registered for request type: ${String(e.type)}`);let a=new l(e);return this.behaviors.length===0?await r.handle(e):await this.executePipeline(a,r,this.behaviors)}async executePipeline(e,t,r){let a=0,h=s(async()=>{if(a<r.length)return await r[a++].handle(e,h);let v=await t.handle(e.request);return e.response=v,v},"next");return await h()}isAsyncIterable(e){return e!=null&&typeof e[Symbol.asyncIterator]=="function"}};s(I,"Mediator");var f=I;var P=class P{handle(e){return this.handleSimple(e)}};s(P,"SimpleHandler");var D=P,b=class b{handle(e){return this.handleStream(e)}};s(b,"StreamingHandler");var E=b;var R=class R{constructor(e){i(this,"type");this.type=e.toString();}};s(R,"BaseCommunicationObject");var p=R;var k=class k extends p{constructor(t){super(t);i(this,"uuid");i(this,"timestamp");this.uuid=v4(),this.timestamp=new Date;}};s(k,"BaseRequest");var T=k;var j=class j extends p{constructor(t){super(t);i(this,"success");i(this,"errors");i(this,"timestamp");this.timestamp=new Date,this.success=true;}setErrors(t){return this.success=false,this.errors=t,this}addError(t){return this.errors||(this.errors=[]),this.errors.push(t),this.success=false,this}};s(j,"BaseResponse");var M=j;var C=class C{constructor(e,t,r){i(this,"serviceProvider");i(this,"mediator");i(this,"isRunning",false);i(this,"adapters");this.serviceProvider=e,this.mediator=t,this.adapters=r;}async run(){if(this.isRunning)throw new Error("Application is already running");for(let e of this.adapters)e.initialize&&await e.initialize(this);this.isRunning=true;}async stop(){if(this.isRunning){for(let e of [...this.adapters].reverse())e.dispose&&await e.dispose();this.isRunning=false;}}async send(e){if(!this.isRunning)throw new Error("Application must be running before sending requests. Call app.run() first.");return this.mediator.send(e)}async stream(e){if(!this.isRunning)throw new Error("Application must be running before streaming requests. Call app.run() first.");return this.mediator.stream(e)}getServiceProvider(){return this.serviceProvider}getMediator(){return this.mediator}get running(){return this.isRunning}};s(C,"Application");var m=C;var B="PipelineBehaviors",A=class A{constructor(){i(this,"services");i(this,"adapters",[]);i(this,"pipelineBehaviors",[]);this.services=new u;}registerHandler(e,t,r=n.Transient){if(typeof t=="function")switch(r){case n.Singleton:this.services.addSingleton(e,t);break;case n.Scoped:this.services.addScoped(e,t);break;case n.Transient:default:this.services.addTransient(e,t);break}else this.services.addSingleton(e,t);return this}registerHandlers(e){return e.forEach((t,r)=>{this.registerHandler(r,t);}),this}registerBehavior(e){return this.pipelineBehaviors.push(e),this}registerSingleton(e,t){return this.services.addSingleton(e,t),this}registerScoped(e,t){return this.services.addScoped(e,t),this}registerTransient(e,t){return this.services.addTransient(e,t),this}registerFactory(e,t,r=n.Transient){return this.services.addFactory(e,t,r),this}use(e){return this.adapters.push(e),this}getServiceCollection(){return this.services}async build(){for(let r of this.adapters)await r.configure(this.services);this.pipelineBehaviors.length>0&&this.services.addSingleton(B,this.pipelineBehaviors.map(r=>typeof r=="function"?new r:r));let e=this.services.buildServiceProvider(),t=new f(e);return new m(e,t,this.adapters)}};s(A,"ApplicationBuilder");var $=A;export{m as Application,$ as ApplicationBuilder,p as BaseCommunicationObject,T as BaseRequest,M as BaseResponse,N as Inject,f as Mediator,l as PipelineContext,u as ServiceCollection,n as ServiceLifetime,d as ServiceProvider,D as SimpleHandler,E as StreamingHandler};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oldzy/conduit",
3
- "version": "1.0.6",
3
+ "version": "1.0.7",
4
4
  "description": "Transport-agnostic request/response pipeline framework with mediator pattern, dependency injection, and middleware support",
5
5
  "author": "oldzy",
6
6
  "license": "MIT",