@oldzy/conduit 1.0.1 → 1.0.2
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.
|
@@ -7,7 +7,7 @@ import { M as Mediator } from '../Mediator-CF-4m1QF.mjs';
|
|
|
7
7
|
interface IApplicationAdapter {
|
|
8
8
|
readonly name: string;
|
|
9
9
|
configure(services: ServiceCollection): void | Promise<void>;
|
|
10
|
-
initialize?(): void | Promise<void>;
|
|
10
|
+
initialize?(app: Application): void | Promise<void>;
|
|
11
11
|
dispose?(): void | Promise<void>;
|
|
12
12
|
}
|
|
13
13
|
|
|
@@ -7,7 +7,7 @@ import { M as Mediator } from '../Mediator-DkYg4E8y.js';
|
|
|
7
7
|
interface IApplicationAdapter {
|
|
8
8
|
readonly name: string;
|
|
9
9
|
configure(services: ServiceCollection): void | Promise<void>;
|
|
10
|
-
initialize?(): void | Promise<void>;
|
|
10
|
+
initialize?(app: Application): void | Promise<void>;
|
|
11
11
|
dispose?(): void | Promise<void>;
|
|
12
12
|
}
|
|
13
13
|
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
'use strict';require('reflect-metadata');/* conduit - MIT License */
|
|
2
|
-
var P=Object.defineProperty;var E=(o,e,t)=>e in o?P(o,e,{enumerable:true,configurable:true,writable:true,value:t}):o[e]=t;var n=(o,e)=>P(o,"name",{value:e,configurable:true});var i=(o,e,t)=>E(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)}catch{return}}getRequiredService(e){let t=this.resolutionStack.length===0;if(this.resolutionStack.includes(e)){let r=[...this.resolutionStack,e].map(a=>String(a)).join(" \u2192 ");throw new Error(`Circular dependency detected: ${r}`)}this.resolutionStack.push(e);try{let r=this.serviceDescriptors.find(a=>a.serviceIdentifier===e);if(!r){if(this.parent)return this.parent.getRequiredService(e);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&&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))return this.singletons.get(t);let r=this.createInstance(e);return this.singletons.set(t,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))return this.scopedInstances.get(t);let r=this.createInstance(e);return this.scopedInstances.set(t,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 T=Reflect.getMetadata("inject:params",t)?.[d];if(T)return this.getRequiredService(T);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===e)||(this.parent?.has(e)??false)}};n(l,"ServiceProvider");var u=l;var m=class m{constructor(){i(this,"serviceDescriptors",[]);}addSingleton(e,t){return typeof t=="function"?this.serviceDescriptors.push({serviceIdentifier:e,implementationType:t,lifetime:s.Singleton}):this.serviceDescriptors.push({serviceIdentifier:e,implementationInstance:t,lifetime:s.Singleton}),this}addScoped(e,t){return this.serviceDescriptors.push({serviceIdentifier:e,implementationType:t,lifetime:s.Scoped}),this}addTransient(e,t){return this.serviceDescriptors.push({serviceIdentifier:e,implementationType:t,lifetime:s.Transient}),this}addFactory(e,t,r=s.Transient){return this.serviceDescriptors.push({serviceIdentifier:e,implementationFactory:t,lifetime:r}),this}addScopedWithDependencies(e,t,r){return this.serviceDescriptors.push({serviceIdentifier:e,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===e)}get count(){return this.serviceDescriptors.length}};n(m,"ServiceCollection");var v=m;var S=class S{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(S,"PipelineContext");var f=S;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.getService(e.type);if(!t)throw new Error(`No handler registered for request type: ${String(e.type)}`);let r=new f(e);return this.behaviors.length===0?await t.handle(e):await this.executePipeline(r,t,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()}};n(w,"Mediator");var g=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.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)}getServiceProvider(){return this.serviceProvider}getMediator(){return this.mediator}get running(){return this.isRunning}};n(y,"Application");var h=y;var k="PipelineBehaviors",I=class I{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(k,this.pipelineBehaviors.map(r=>typeof r=="function"?new r:r));let e=this.services.buildServiceProvider(),t=new g(e);return new h(e,t,this.adapters)}};n(I,"ApplicationBuilder");var D=I;exports.Application=h;exports.ApplicationBuilder=D;
|
|
2
|
+
var P=Object.defineProperty;var E=(o,e,t)=>e in o?P(o,e,{enumerable:true,configurable:true,writable:true,value:t}):o[e]=t;var n=(o,e)=>P(o,"name",{value:e,configurable:true});var i=(o,e,t)=>E(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)}catch{return}}getRequiredService(e){let t=this.resolutionStack.length===0;if(this.resolutionStack.includes(e)){let r=[...this.resolutionStack,e].map(a=>String(a)).join(" \u2192 ");throw new Error(`Circular dependency detected: ${r}`)}this.resolutionStack.push(e);try{let r=this.serviceDescriptors.find(a=>a.serviceIdentifier===e);if(!r){if(this.parent)return this.parent.getRequiredService(e);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&&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))return this.singletons.get(t);let r=this.createInstance(e);return this.singletons.set(t,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))return this.scopedInstances.get(t);let r=this.createInstance(e);return this.scopedInstances.set(t,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 T=Reflect.getMetadata("inject:params",t)?.[d];if(T)return this.getRequiredService(T);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===e)||(this.parent?.has(e)??false)}};n(l,"ServiceProvider");var u=l;var m=class m{constructor(){i(this,"serviceDescriptors",[]);}addSingleton(e,t){return typeof t=="function"?this.serviceDescriptors.push({serviceIdentifier:e,implementationType:t,lifetime:s.Singleton}):this.serviceDescriptors.push({serviceIdentifier:e,implementationInstance:t,lifetime:s.Singleton}),this}addScoped(e,t){return this.serviceDescriptors.push({serviceIdentifier:e,implementationType:t,lifetime:s.Scoped}),this}addTransient(e,t){return this.serviceDescriptors.push({serviceIdentifier:e,implementationType:t,lifetime:s.Transient}),this}addFactory(e,t,r=s.Transient){return this.serviceDescriptors.push({serviceIdentifier:e,implementationFactory:t,lifetime:r}),this}addScopedWithDependencies(e,t,r){return this.serviceDescriptors.push({serviceIdentifier:e,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===e)}get count(){return this.serviceDescriptors.length}};n(m,"ServiceCollection");var v=m;var S=class S{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(S,"PipelineContext");var f=S;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.getService(e.type);if(!t)throw new Error(`No handler registered for request type: ${String(e.type)}`);let r=new f(e);return this.behaviors.length===0?await t.handle(e):await this.executePipeline(r,t,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()}};n(w,"Mediator");var g=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)}getServiceProvider(){return this.serviceProvider}getMediator(){return this.mediator}get running(){return this.isRunning}};n(y,"Application");var h=y;var k="PipelineBehaviors",I=class I{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(k,this.pipelineBehaviors.map(r=>typeof r=="function"?new r:r));let e=this.services.buildServiceProvider(),t=new g(e);return new h(e,t,this.adapters)}};n(I,"ApplicationBuilder");var D=I;exports.Application=h;exports.ApplicationBuilder=D;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import'reflect-metadata';/* conduit - MIT License */
|
|
2
|
-
var P=Object.defineProperty;var E=(o,e,t)=>e in o?P(o,e,{enumerable:true,configurable:true,writable:true,value:t}):o[e]=t;var n=(o,e)=>P(o,"name",{value:e,configurable:true});var i=(o,e,t)=>E(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)}catch{return}}getRequiredService(e){let t=this.resolutionStack.length===0;if(this.resolutionStack.includes(e)){let r=[...this.resolutionStack,e].map(a=>String(a)).join(" \u2192 ");throw new Error(`Circular dependency detected: ${r}`)}this.resolutionStack.push(e);try{let r=this.serviceDescriptors.find(a=>a.serviceIdentifier===e);if(!r){if(this.parent)return this.parent.getRequiredService(e);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&&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))return this.singletons.get(t);let r=this.createInstance(e);return this.singletons.set(t,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))return this.scopedInstances.get(t);let r=this.createInstance(e);return this.scopedInstances.set(t,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 T=Reflect.getMetadata("inject:params",t)?.[d];if(T)return this.getRequiredService(T);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===e)||(this.parent?.has(e)??false)}};n(l,"ServiceProvider");var u=l;var m=class m{constructor(){i(this,"serviceDescriptors",[]);}addSingleton(e,t){return typeof t=="function"?this.serviceDescriptors.push({serviceIdentifier:e,implementationType:t,lifetime:s.Singleton}):this.serviceDescriptors.push({serviceIdentifier:e,implementationInstance:t,lifetime:s.Singleton}),this}addScoped(e,t){return this.serviceDescriptors.push({serviceIdentifier:e,implementationType:t,lifetime:s.Scoped}),this}addTransient(e,t){return this.serviceDescriptors.push({serviceIdentifier:e,implementationType:t,lifetime:s.Transient}),this}addFactory(e,t,r=s.Transient){return this.serviceDescriptors.push({serviceIdentifier:e,implementationFactory:t,lifetime:r}),this}addScopedWithDependencies(e,t,r){return this.serviceDescriptors.push({serviceIdentifier:e,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===e)}get count(){return this.serviceDescriptors.length}};n(m,"ServiceCollection");var v=m;var S=class S{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(S,"PipelineContext");var f=S;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.getService(e.type);if(!t)throw new Error(`No handler registered for request type: ${String(e.type)}`);let r=new f(e);return this.behaviors.length===0?await t.handle(e):await this.executePipeline(r,t,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()}};n(w,"Mediator");var g=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.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)}getServiceProvider(){return this.serviceProvider}getMediator(){return this.mediator}get running(){return this.isRunning}};n(y,"Application");var h=y;var k="PipelineBehaviors",I=class I{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(k,this.pipelineBehaviors.map(r=>typeof r=="function"?new r:r));let e=this.services.buildServiceProvider(),t=new g(e);return new h(e,t,this.adapters)}};n(I,"ApplicationBuilder");var D=I;export{h as Application,D as ApplicationBuilder};
|
|
2
|
+
var P=Object.defineProperty;var E=(o,e,t)=>e in o?P(o,e,{enumerable:true,configurable:true,writable:true,value:t}):o[e]=t;var n=(o,e)=>P(o,"name",{value:e,configurable:true});var i=(o,e,t)=>E(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)}catch{return}}getRequiredService(e){let t=this.resolutionStack.length===0;if(this.resolutionStack.includes(e)){let r=[...this.resolutionStack,e].map(a=>String(a)).join(" \u2192 ");throw new Error(`Circular dependency detected: ${r}`)}this.resolutionStack.push(e);try{let r=this.serviceDescriptors.find(a=>a.serviceIdentifier===e);if(!r){if(this.parent)return this.parent.getRequiredService(e);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&&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))return this.singletons.get(t);let r=this.createInstance(e);return this.singletons.set(t,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))return this.scopedInstances.get(t);let r=this.createInstance(e);return this.scopedInstances.set(t,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 T=Reflect.getMetadata("inject:params",t)?.[d];if(T)return this.getRequiredService(T);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===e)||(this.parent?.has(e)??false)}};n(l,"ServiceProvider");var u=l;var m=class m{constructor(){i(this,"serviceDescriptors",[]);}addSingleton(e,t){return typeof t=="function"?this.serviceDescriptors.push({serviceIdentifier:e,implementationType:t,lifetime:s.Singleton}):this.serviceDescriptors.push({serviceIdentifier:e,implementationInstance:t,lifetime:s.Singleton}),this}addScoped(e,t){return this.serviceDescriptors.push({serviceIdentifier:e,implementationType:t,lifetime:s.Scoped}),this}addTransient(e,t){return this.serviceDescriptors.push({serviceIdentifier:e,implementationType:t,lifetime:s.Transient}),this}addFactory(e,t,r=s.Transient){return this.serviceDescriptors.push({serviceIdentifier:e,implementationFactory:t,lifetime:r}),this}addScopedWithDependencies(e,t,r){return this.serviceDescriptors.push({serviceIdentifier:e,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===e)}get count(){return this.serviceDescriptors.length}};n(m,"ServiceCollection");var v=m;var S=class S{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(S,"PipelineContext");var f=S;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.getService(e.type);if(!t)throw new Error(`No handler registered for request type: ${String(e.type)}`);let r=new f(e);return this.behaviors.length===0?await t.handle(e):await this.executePipeline(r,t,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()}};n(w,"Mediator");var g=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)}getServiceProvider(){return this.serviceProvider}getMediator(){return this.mediator}get running(){return this.isRunning}};n(y,"Application");var h=y;var k="PipelineBehaviors",I=class I{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(k,this.pipelineBehaviors.map(r=>typeof r=="function"?new r:r));let e=this.services.buildServiceProvider(),t=new g(e);return new h(e,t,this.adapters)}};n(I,"ApplicationBuilder");var D=I;export{h as Application,D as ApplicationBuilder};
|
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 F=Object.defineProperty;var $=(o,e,t)=>e in o?F(o,e,{enumerable:true,configurable:true,writable:true,value:t}):o[e]=t;var i=(o,e)=>F(o,"name",{value:e,configurable:true});var s=(o,e,t)=>$(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){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)}catch{return}}getRequiredService(e){let t=this.resolutionStack.length===0;if(this.resolutionStack.includes(e)){let r=[...this.resolutionStack,e].map(a=>String(a)).join(" \u2192 ");throw new Error(`Circular dependency detected: ${r}`)}this.resolutionStack.push(e);try{let r=this.serviceDescriptors.find(a=>a.serviceIdentifier===e);if(!r){if(this.parent)return this.parent.getRequiredService(e);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&&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))return this.singletons.get(t);let r=this.createInstance(e);return this.singletons.set(t,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))return this.scopedInstances.get(t);let r=this.createInstance(e);return this.scopedInstances.set(t,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 q=Reflect.getMetadata("inject:params",t)?.[v];if(q)return this.getRequiredService(q);let N=h.name||h;return this.getRequiredService(N)});return new t(...c)}createScope(){let e=new g(this.serviceDescriptors,this);return {serviceProvider:e,dispose:i(()=>{e.scopedInstances.clear();},"dispose")}}has(e){return this.serviceDescriptors.some(t=>t.serviceIdentifier===e)||(this.parent?.has(e)??false)}};i(g,"ServiceProvider");var u=g;var S=class S{constructor(){s(this,"serviceDescriptors",[]);}addSingleton(e,t){return typeof t=="function"?this.serviceDescriptors.push({serviceIdentifier:e,implementationType:t,lifetime:n.Singleton}):this.serviceDescriptors.push({serviceIdentifier:e,implementationInstance:t,lifetime:n.Singleton}),this}addScoped(e,t){return this.serviceDescriptors.push({serviceIdentifier:e,implementationType:t,lifetime:n.Scoped}),this}addTransient(e,t){return this.serviceDescriptors.push({serviceIdentifier:e,implementationType:t,lifetime:n.Transient}),this}addFactory(e,t,r=n.Transient){return this.serviceDescriptors.push({serviceIdentifier:e,implementationFactory:t,lifetime:r}),this}addScopedWithDependencies(e,t,r){return this.serviceDescriptors.push({serviceIdentifier:e,implementationType:t,lifetime:n.Scoped,dependencies:r}),this}buildServiceProvider(){return new u([...this.serviceDescriptors])}getDescriptors(){return [...this.serviceDescriptors]}has(e){return this.serviceDescriptors.some(t=>t.serviceIdentifier===e)}get count(){return this.serviceDescriptors.length}};i(S,"ServiceCollection");var d=S;function B(o){return (e,t,r)=>{let c={...Reflect.getOwnMetadata("inject:params",e)||{}};c[r]=o,Reflect.defineMetadata("inject:params",c,e);}}i(B,"Inject");var w=class w{constructor(e){s(this,"request");s(this,"response");s(this,"metadata",new Map);this.request=e;}setMetadata(e,t){this.metadata.set(e,t);}getMetadata(e){return this.metadata.get(e)}};i(w,"PipelineContext");var l=w;var x=class x{constructor(e){s(this,"serviceProvider");s(this,"behaviors",[]);this.serviceProvider=e;let t=this.serviceProvider.getService("PipelineBehaviors");t&&(this.behaviors=t);}async send(e){let t=this.serviceProvider.getService(e.type);if(!t)throw new Error(`No handler registered for request type: ${String(e.type)}`);let r=new l(e);return this.behaviors.length===0?await t.handle(e):await this.executePipeline(r,t,this.behaviors)}async executePipeline(e,t,r){let a=0,c=i(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()}};i(x,"Mediator");var m=x;var D=class D{handle(e){return this.handleSimple(e)}};i(D,"SimpleHandler");var y=D,P=class P{handle(e){return this.handleStream(e)}};i(P,"StreamingHandler");var I=P;var E=class E{constructor(e){s(this,"type");this.type=e;}};i(E,"BaseCommunicationObject");var p=E;var R=class R extends p{constructor(t){super(t);s(this,"uuid");s(this,"timestamp");this.uuid=uuid.v4(),this.timestamp=new Date;}};i(R,"BaseRequest");var T=R;var k=class k extends p{constructor(t,r){super(t);s(this,"success");s(this,"errors");s(this,"timestamp");s(this,"requestUuid");this.timestamp=new Date,this.success=true,this.requestUuid=r;}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}};i(k,"BaseResponse");var b=k;var j=class j{constructor(e,t,r){s(this,"serviceProvider");s(this,"mediator");s(this,"isRunning",false);s(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.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)}getServiceProvider(){return this.serviceProvider}getMediator(){return this.mediator}get running(){return this.isRunning}};i(j,"Application");var f=j;var L="PipelineBehaviors",C=class C{constructor(){s(this,"services");s(this,"adapters",[]);s(this,"pipelineBehaviors",[]);this.services=new d;}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(L,this.pipelineBehaviors.map(r=>typeof r=="function"?new r:r));let e=this.services.buildServiceProvider(),t=new m(e);return new f(e,t,this.adapters)}};i(C,"ApplicationBuilder");var M=C;exports.Application=f;exports.ApplicationBuilder=M;exports.BaseCommunicationObject=p;exports.BaseRequest=T;exports.BaseResponse=b;exports.Inject=B;exports.Mediator=m;exports.PipelineContext=l;exports.ServiceCollection=d;exports.ServiceLifetime=n;exports.ServiceProvider=u;exports.SimpleHandler=y;exports.StreamingHandler=I;
|
|
2
|
+
var F=Object.defineProperty;var $=(o,e,t)=>e in o?F(o,e,{enumerable:true,configurable:true,writable:true,value:t}):o[e]=t;var i=(o,e)=>F(o,"name",{value:e,configurable:true});var s=(o,e,t)=>$(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){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)}catch{return}}getRequiredService(e){let t=this.resolutionStack.length===0;if(this.resolutionStack.includes(e)){let r=[...this.resolutionStack,e].map(a=>String(a)).join(" \u2192 ");throw new Error(`Circular dependency detected: ${r}`)}this.resolutionStack.push(e);try{let r=this.serviceDescriptors.find(a=>a.serviceIdentifier===e);if(!r){if(this.parent)return this.parent.getRequiredService(e);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&&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))return this.singletons.get(t);let r=this.createInstance(e);return this.singletons.set(t,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))return this.scopedInstances.get(t);let r=this.createInstance(e);return this.scopedInstances.set(t,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 q=Reflect.getMetadata("inject:params",t)?.[v];if(q)return this.getRequiredService(q);let N=h.name||h;return this.getRequiredService(N)});return new t(...c)}createScope(){let e=new g(this.serviceDescriptors,this);return {serviceProvider:e,dispose:i(()=>{e.scopedInstances.clear();},"dispose")}}has(e){return this.serviceDescriptors.some(t=>t.serviceIdentifier===e)||(this.parent?.has(e)??false)}};i(g,"ServiceProvider");var u=g;var S=class S{constructor(){s(this,"serviceDescriptors",[]);}addSingleton(e,t){return typeof t=="function"?this.serviceDescriptors.push({serviceIdentifier:e,implementationType:t,lifetime:n.Singleton}):this.serviceDescriptors.push({serviceIdentifier:e,implementationInstance:t,lifetime:n.Singleton}),this}addScoped(e,t){return this.serviceDescriptors.push({serviceIdentifier:e,implementationType:t,lifetime:n.Scoped}),this}addTransient(e,t){return this.serviceDescriptors.push({serviceIdentifier:e,implementationType:t,lifetime:n.Transient}),this}addFactory(e,t,r=n.Transient){return this.serviceDescriptors.push({serviceIdentifier:e,implementationFactory:t,lifetime:r}),this}addScopedWithDependencies(e,t,r){return this.serviceDescriptors.push({serviceIdentifier:e,implementationType:t,lifetime:n.Scoped,dependencies:r}),this}buildServiceProvider(){return new u([...this.serviceDescriptors])}getDescriptors(){return [...this.serviceDescriptors]}has(e){return this.serviceDescriptors.some(t=>t.serviceIdentifier===e)}get count(){return this.serviceDescriptors.length}};i(S,"ServiceCollection");var d=S;function B(o){return (e,t,r)=>{let c={...Reflect.getOwnMetadata("inject:params",e)||{}};c[r]=o,Reflect.defineMetadata("inject:params",c,e);}}i(B,"Inject");var w=class w{constructor(e){s(this,"request");s(this,"response");s(this,"metadata",new Map);this.request=e;}setMetadata(e,t){this.metadata.set(e,t);}getMetadata(e){return this.metadata.get(e)}};i(w,"PipelineContext");var l=w;var x=class x{constructor(e){s(this,"serviceProvider");s(this,"behaviors",[]);this.serviceProvider=e;let t=this.serviceProvider.getService("PipelineBehaviors");t&&(this.behaviors=t);}async send(e){let t=this.serviceProvider.getService(e.type);if(!t)throw new Error(`No handler registered for request type: ${String(e.type)}`);let r=new l(e);return this.behaviors.length===0?await t.handle(e):await this.executePipeline(r,t,this.behaviors)}async executePipeline(e,t,r){let a=0,c=i(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()}};i(x,"Mediator");var m=x;var D=class D{handle(e){return this.handleSimple(e)}};i(D,"SimpleHandler");var y=D,P=class P{handle(e){return this.handleStream(e)}};i(P,"StreamingHandler");var I=P;var E=class E{constructor(e){s(this,"type");this.type=e;}};i(E,"BaseCommunicationObject");var p=E;var R=class R extends p{constructor(t){super(t);s(this,"uuid");s(this,"timestamp");this.uuid=uuid.v4(),this.timestamp=new Date;}};i(R,"BaseRequest");var T=R;var k=class k extends p{constructor(t,r){super(t);s(this,"success");s(this,"errors");s(this,"timestamp");s(this,"requestUuid");this.timestamp=new Date,this.success=true,this.requestUuid=r;}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}};i(k,"BaseResponse");var b=k;var j=class j{constructor(e,t,r){s(this,"serviceProvider");s(this,"mediator");s(this,"isRunning",false);s(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)}getServiceProvider(){return this.serviceProvider}getMediator(){return this.mediator}get running(){return this.isRunning}};i(j,"Application");var f=j;var L="PipelineBehaviors",C=class C{constructor(){s(this,"services");s(this,"adapters",[]);s(this,"pipelineBehaviors",[]);this.services=new d;}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(L,this.pipelineBehaviors.map(r=>typeof r=="function"?new r:r));let e=this.services.buildServiceProvider(),t=new m(e);return new f(e,t,this.adapters)}};i(C,"ApplicationBuilder");var M=C;exports.Application=f;exports.ApplicationBuilder=M;exports.BaseCommunicationObject=p;exports.BaseRequest=T;exports.BaseResponse=b;exports.Inject=B;exports.Mediator=m;exports.PipelineContext=l;exports.ServiceCollection=d;exports.ServiceLifetime=n;exports.ServiceProvider=u;exports.SimpleHandler=y;exports.StreamingHandler=I;
|
package/dist/index.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import'reflect-metadata';import {v4}from'uuid';/* conduit - MIT License */
|
|
2
|
-
var F=Object.defineProperty;var $=(o,e,t)=>e in o?F(o,e,{enumerable:true,configurable:true,writable:true,value:t}):o[e]=t;var i=(o,e)=>F(o,"name",{value:e,configurable:true});var s=(o,e,t)=>$(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){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)}catch{return}}getRequiredService(e){let t=this.resolutionStack.length===0;if(this.resolutionStack.includes(e)){let r=[...this.resolutionStack,e].map(a=>String(a)).join(" \u2192 ");throw new Error(`Circular dependency detected: ${r}`)}this.resolutionStack.push(e);try{let r=this.serviceDescriptors.find(a=>a.serviceIdentifier===e);if(!r){if(this.parent)return this.parent.getRequiredService(e);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&&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))return this.singletons.get(t);let r=this.createInstance(e);return this.singletons.set(t,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))return this.scopedInstances.get(t);let r=this.createInstance(e);return this.scopedInstances.set(t,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 q=Reflect.getMetadata("inject:params",t)?.[v];if(q)return this.getRequiredService(q);let N=h.name||h;return this.getRequiredService(N)});return new t(...c)}createScope(){let e=new g(this.serviceDescriptors,this);return {serviceProvider:e,dispose:i(()=>{e.scopedInstances.clear();},"dispose")}}has(e){return this.serviceDescriptors.some(t=>t.serviceIdentifier===e)||(this.parent?.has(e)??false)}};i(g,"ServiceProvider");var u=g;var S=class S{constructor(){s(this,"serviceDescriptors",[]);}addSingleton(e,t){return typeof t=="function"?this.serviceDescriptors.push({serviceIdentifier:e,implementationType:t,lifetime:n.Singleton}):this.serviceDescriptors.push({serviceIdentifier:e,implementationInstance:t,lifetime:n.Singleton}),this}addScoped(e,t){return this.serviceDescriptors.push({serviceIdentifier:e,implementationType:t,lifetime:n.Scoped}),this}addTransient(e,t){return this.serviceDescriptors.push({serviceIdentifier:e,implementationType:t,lifetime:n.Transient}),this}addFactory(e,t,r=n.Transient){return this.serviceDescriptors.push({serviceIdentifier:e,implementationFactory:t,lifetime:r}),this}addScopedWithDependencies(e,t,r){return this.serviceDescriptors.push({serviceIdentifier:e,implementationType:t,lifetime:n.Scoped,dependencies:r}),this}buildServiceProvider(){return new u([...this.serviceDescriptors])}getDescriptors(){return [...this.serviceDescriptors]}has(e){return this.serviceDescriptors.some(t=>t.serviceIdentifier===e)}get count(){return this.serviceDescriptors.length}};i(S,"ServiceCollection");var d=S;function B(o){return (e,t,r)=>{let c={...Reflect.getOwnMetadata("inject:params",e)||{}};c[r]=o,Reflect.defineMetadata("inject:params",c,e);}}i(B,"Inject");var w=class w{constructor(e){s(this,"request");s(this,"response");s(this,"metadata",new Map);this.request=e;}setMetadata(e,t){this.metadata.set(e,t);}getMetadata(e){return this.metadata.get(e)}};i(w,"PipelineContext");var l=w;var x=class x{constructor(e){s(this,"serviceProvider");s(this,"behaviors",[]);this.serviceProvider=e;let t=this.serviceProvider.getService("PipelineBehaviors");t&&(this.behaviors=t);}async send(e){let t=this.serviceProvider.getService(e.type);if(!t)throw new Error(`No handler registered for request type: ${String(e.type)}`);let r=new l(e);return this.behaviors.length===0?await t.handle(e):await this.executePipeline(r,t,this.behaviors)}async executePipeline(e,t,r){let a=0,c=i(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()}};i(x,"Mediator");var m=x;var D=class D{handle(e){return this.handleSimple(e)}};i(D,"SimpleHandler");var y=D,P=class P{handle(e){return this.handleStream(e)}};i(P,"StreamingHandler");var I=P;var E=class E{constructor(e){s(this,"type");this.type=e;}};i(E,"BaseCommunicationObject");var p=E;var R=class R extends p{constructor(t){super(t);s(this,"uuid");s(this,"timestamp");this.uuid=v4(),this.timestamp=new Date;}};i(R,"BaseRequest");var T=R;var k=class k extends p{constructor(t,r){super(t);s(this,"success");s(this,"errors");s(this,"timestamp");s(this,"requestUuid");this.timestamp=new Date,this.success=true,this.requestUuid=r;}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}};i(k,"BaseResponse");var b=k;var j=class j{constructor(e,t,r){s(this,"serviceProvider");s(this,"mediator");s(this,"isRunning",false);s(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.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)}getServiceProvider(){return this.serviceProvider}getMediator(){return this.mediator}get running(){return this.isRunning}};i(j,"Application");var f=j;var L="PipelineBehaviors",C=class C{constructor(){s(this,"services");s(this,"adapters",[]);s(this,"pipelineBehaviors",[]);this.services=new d;}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(L,this.pipelineBehaviors.map(r=>typeof r=="function"?new r:r));let e=this.services.buildServiceProvider(),t=new m(e);return new f(e,t,this.adapters)}};i(C,"ApplicationBuilder");var M=C;export{f as Application,M as ApplicationBuilder,p as BaseCommunicationObject,T as BaseRequest,b as BaseResponse,B as Inject,m as Mediator,l as PipelineContext,d as ServiceCollection,n as ServiceLifetime,u as ServiceProvider,y as SimpleHandler,I as StreamingHandler};
|
|
2
|
+
var F=Object.defineProperty;var $=(o,e,t)=>e in o?F(o,e,{enumerable:true,configurable:true,writable:true,value:t}):o[e]=t;var i=(o,e)=>F(o,"name",{value:e,configurable:true});var s=(o,e,t)=>$(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){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)}catch{return}}getRequiredService(e){let t=this.resolutionStack.length===0;if(this.resolutionStack.includes(e)){let r=[...this.resolutionStack,e].map(a=>String(a)).join(" \u2192 ");throw new Error(`Circular dependency detected: ${r}`)}this.resolutionStack.push(e);try{let r=this.serviceDescriptors.find(a=>a.serviceIdentifier===e);if(!r){if(this.parent)return this.parent.getRequiredService(e);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&&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))return this.singletons.get(t);let r=this.createInstance(e);return this.singletons.set(t,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))return this.scopedInstances.get(t);let r=this.createInstance(e);return this.scopedInstances.set(t,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 q=Reflect.getMetadata("inject:params",t)?.[v];if(q)return this.getRequiredService(q);let N=h.name||h;return this.getRequiredService(N)});return new t(...c)}createScope(){let e=new g(this.serviceDescriptors,this);return {serviceProvider:e,dispose:i(()=>{e.scopedInstances.clear();},"dispose")}}has(e){return this.serviceDescriptors.some(t=>t.serviceIdentifier===e)||(this.parent?.has(e)??false)}};i(g,"ServiceProvider");var u=g;var S=class S{constructor(){s(this,"serviceDescriptors",[]);}addSingleton(e,t){return typeof t=="function"?this.serviceDescriptors.push({serviceIdentifier:e,implementationType:t,lifetime:n.Singleton}):this.serviceDescriptors.push({serviceIdentifier:e,implementationInstance:t,lifetime:n.Singleton}),this}addScoped(e,t){return this.serviceDescriptors.push({serviceIdentifier:e,implementationType:t,lifetime:n.Scoped}),this}addTransient(e,t){return this.serviceDescriptors.push({serviceIdentifier:e,implementationType:t,lifetime:n.Transient}),this}addFactory(e,t,r=n.Transient){return this.serviceDescriptors.push({serviceIdentifier:e,implementationFactory:t,lifetime:r}),this}addScopedWithDependencies(e,t,r){return this.serviceDescriptors.push({serviceIdentifier:e,implementationType:t,lifetime:n.Scoped,dependencies:r}),this}buildServiceProvider(){return new u([...this.serviceDescriptors])}getDescriptors(){return [...this.serviceDescriptors]}has(e){return this.serviceDescriptors.some(t=>t.serviceIdentifier===e)}get count(){return this.serviceDescriptors.length}};i(S,"ServiceCollection");var d=S;function B(o){return (e,t,r)=>{let c={...Reflect.getOwnMetadata("inject:params",e)||{}};c[r]=o,Reflect.defineMetadata("inject:params",c,e);}}i(B,"Inject");var w=class w{constructor(e){s(this,"request");s(this,"response");s(this,"metadata",new Map);this.request=e;}setMetadata(e,t){this.metadata.set(e,t);}getMetadata(e){return this.metadata.get(e)}};i(w,"PipelineContext");var l=w;var x=class x{constructor(e){s(this,"serviceProvider");s(this,"behaviors",[]);this.serviceProvider=e;let t=this.serviceProvider.getService("PipelineBehaviors");t&&(this.behaviors=t);}async send(e){let t=this.serviceProvider.getService(e.type);if(!t)throw new Error(`No handler registered for request type: ${String(e.type)}`);let r=new l(e);return this.behaviors.length===0?await t.handle(e):await this.executePipeline(r,t,this.behaviors)}async executePipeline(e,t,r){let a=0,c=i(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()}};i(x,"Mediator");var m=x;var D=class D{handle(e){return this.handleSimple(e)}};i(D,"SimpleHandler");var y=D,P=class P{handle(e){return this.handleStream(e)}};i(P,"StreamingHandler");var I=P;var E=class E{constructor(e){s(this,"type");this.type=e;}};i(E,"BaseCommunicationObject");var p=E;var R=class R extends p{constructor(t){super(t);s(this,"uuid");s(this,"timestamp");this.uuid=v4(),this.timestamp=new Date;}};i(R,"BaseRequest");var T=R;var k=class k extends p{constructor(t,r){super(t);s(this,"success");s(this,"errors");s(this,"timestamp");s(this,"requestUuid");this.timestamp=new Date,this.success=true,this.requestUuid=r;}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}};i(k,"BaseResponse");var b=k;var j=class j{constructor(e,t,r){s(this,"serviceProvider");s(this,"mediator");s(this,"isRunning",false);s(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)}getServiceProvider(){return this.serviceProvider}getMediator(){return this.mediator}get running(){return this.isRunning}};i(j,"Application");var f=j;var L="PipelineBehaviors",C=class C{constructor(){s(this,"services");s(this,"adapters",[]);s(this,"pipelineBehaviors",[]);this.services=new d;}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(L,this.pipelineBehaviors.map(r=>typeof r=="function"?new r:r));let e=this.services.buildServiceProvider(),t=new m(e);return new f(e,t,this.adapters)}};i(C,"ApplicationBuilder");var M=C;export{f as Application,M as ApplicationBuilder,p as BaseCommunicationObject,T as BaseRequest,b as BaseResponse,B as Inject,m as Mediator,l as PipelineContext,d as ServiceCollection,n as ServiceLifetime,u as ServiceProvider,y as SimpleHandler,I as StreamingHandler};
|
package/package.json
CHANGED