@mudah-cli/container 0.1.0
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.
- package/dist/container.d.ts +48 -0
- package/dist/container.js +137 -0
- package/dist/container.js.map +1 -0
- package/dist/exceptions.d.ts +9 -0
- package/dist/exceptions.js +22 -0
- package/dist/exceptions.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +18 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +23 -0
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { Abstract, Constructor, Factory } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* True for class declarations and function declarations (values with their
|
|
4
|
+
* own `prototype.constructor`), false for arrow functions and plain objects.
|
|
5
|
+
* This is what separates an injectable class from a factory function.
|
|
6
|
+
*/
|
|
7
|
+
export declare function isClassLike(value: unknown): value is Constructor;
|
|
8
|
+
/**
|
|
9
|
+
* High-performance IoC service container.
|
|
10
|
+
*
|
|
11
|
+
* - `bind` / `singleton` register factories (arrow functions) or injectable classes.
|
|
12
|
+
* - `instance` registers a pre-built value.
|
|
13
|
+
* - `when` registers contextual bindings: an alternative factory that applies
|
|
14
|
+
* only when the dependency is injected into a specific context.
|
|
15
|
+
* - Classes declaring `static dependencies` get constructor auto-injection.
|
|
16
|
+
*/
|
|
17
|
+
export declare class Container {
|
|
18
|
+
private readonly bindings;
|
|
19
|
+
private readonly instances;
|
|
20
|
+
private readonly contextual;
|
|
21
|
+
private readonly resolvingStack;
|
|
22
|
+
/** Register a binding. A fresh instance is produced on every `make`. */
|
|
23
|
+
bind<T>(abstract: Abstract<T>, factory: Factory<T> | Constructor<T>): this;
|
|
24
|
+
/** Register a shared binding. The instance is resolved once, then cached. */
|
|
25
|
+
singleton<T>(abstract: Abstract<T>, factory: Factory<T> | Constructor<T>): this;
|
|
26
|
+
/** Register a pre-built value under an abstract. */
|
|
27
|
+
instance<T>(abstract: Abstract<T>, value: T): this;
|
|
28
|
+
/** Make `abstract` an alias of `target` (resolves to the same instance). */
|
|
29
|
+
alias(abstract: Abstract, target: Abstract): this;
|
|
30
|
+
/**
|
|
31
|
+
* Contextual binding: when `abstract` is injected into `context`, resolve it
|
|
32
|
+
* with `factory` instead of its global binding.
|
|
33
|
+
*/
|
|
34
|
+
when<TContext extends object, TDep>(context: Constructor<TContext>, abstract: Abstract<TDep>, factory: Factory<TDep> | Constructor<TDep>): this;
|
|
35
|
+
/** Whether the abstract is bound, or is a constructible class. */
|
|
36
|
+
has(abstract: Abstract): boolean;
|
|
37
|
+
/** Resolve an abstract to an instance. */
|
|
38
|
+
make<T>(abstract: Abstract<T>): T;
|
|
39
|
+
/** Alias for {@link make}. */
|
|
40
|
+
get<T>(abstract: Abstract<T>): T;
|
|
41
|
+
/** Forget all cached singleton instances (bindings are kept). */
|
|
42
|
+
flush(): void;
|
|
43
|
+
private sharedInstance;
|
|
44
|
+
private materialize;
|
|
45
|
+
private instantiate;
|
|
46
|
+
private resolveDependency;
|
|
47
|
+
private withResolving;
|
|
48
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { BindingResolutionException, CircularDependencyException } from './exceptions.js';
|
|
2
|
+
/**
|
|
3
|
+
* True for class declarations and function declarations (values with their
|
|
4
|
+
* own `prototype.constructor`), false for arrow functions and plain objects.
|
|
5
|
+
* This is what separates an injectable class from a factory function.
|
|
6
|
+
*/
|
|
7
|
+
export function isClassLike(value) {
|
|
8
|
+
if (typeof value !== 'function')
|
|
9
|
+
return false;
|
|
10
|
+
const proto = value.prototype;
|
|
11
|
+
return proto !== null && typeof proto === 'object' && Object.prototype.hasOwnProperty.call(proto, 'constructor');
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* High-performance IoC service container.
|
|
15
|
+
*
|
|
16
|
+
* - `bind` / `singleton` register factories (arrow functions) or injectable classes.
|
|
17
|
+
* - `instance` registers a pre-built value.
|
|
18
|
+
* - `when` registers contextual bindings: an alternative factory that applies
|
|
19
|
+
* only when the dependency is injected into a specific context.
|
|
20
|
+
* - Classes declaring `static dependencies` get constructor auto-injection.
|
|
21
|
+
*/
|
|
22
|
+
export class Container {
|
|
23
|
+
bindings = new Map();
|
|
24
|
+
instances = new Map();
|
|
25
|
+
contextual = new Map();
|
|
26
|
+
resolvingStack = [];
|
|
27
|
+
/** Register a binding. A fresh instance is produced on every `make`. */
|
|
28
|
+
bind(abstract, factory) {
|
|
29
|
+
this.bindings.set(abstract, { factory, shared: false });
|
|
30
|
+
this.instances.delete(abstract);
|
|
31
|
+
return this;
|
|
32
|
+
}
|
|
33
|
+
/** Register a shared binding. The instance is resolved once, then cached. */
|
|
34
|
+
singleton(abstract, factory) {
|
|
35
|
+
this.bindings.set(abstract, { factory, shared: true });
|
|
36
|
+
this.instances.delete(abstract);
|
|
37
|
+
return this;
|
|
38
|
+
}
|
|
39
|
+
/** Register a pre-built value under an abstract. */
|
|
40
|
+
instance(abstract, value) {
|
|
41
|
+
this.bindings.set(abstract, { factory: () => value, shared: true });
|
|
42
|
+
this.instances.set(abstract, value);
|
|
43
|
+
return this;
|
|
44
|
+
}
|
|
45
|
+
/** Make `abstract` an alias of `target` (resolves to the same instance). */
|
|
46
|
+
alias(abstract, target) {
|
|
47
|
+
this.bind(abstract, (container) => container.make(target));
|
|
48
|
+
return this;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Contextual binding: when `abstract` is injected into `context`, resolve it
|
|
52
|
+
* with `factory` instead of its global binding.
|
|
53
|
+
*/
|
|
54
|
+
when(context, abstract, factory) {
|
|
55
|
+
let map = this.contextual.get(context);
|
|
56
|
+
if (!map) {
|
|
57
|
+
map = new Map();
|
|
58
|
+
this.contextual.set(context, map);
|
|
59
|
+
}
|
|
60
|
+
map.set(abstract, factory);
|
|
61
|
+
return this;
|
|
62
|
+
}
|
|
63
|
+
/** Whether the abstract is bound, or is a constructible class. */
|
|
64
|
+
has(abstract) {
|
|
65
|
+
return this.bindings.has(abstract) || isClassLike(abstract);
|
|
66
|
+
}
|
|
67
|
+
/** Resolve an abstract to an instance. */
|
|
68
|
+
make(abstract) {
|
|
69
|
+
if (this.instances.has(abstract)) {
|
|
70
|
+
return this.instances.get(abstract);
|
|
71
|
+
}
|
|
72
|
+
const binding = this.bindings.get(abstract);
|
|
73
|
+
if (binding) {
|
|
74
|
+
return binding.shared
|
|
75
|
+
? this.sharedInstance(abstract, () => this.materialize(abstract, binding.factory, undefined))
|
|
76
|
+
: this.withResolving(abstract, () => this.materialize(abstract, binding.factory, undefined));
|
|
77
|
+
}
|
|
78
|
+
if (isClassLike(abstract)) {
|
|
79
|
+
return this.withResolving(abstract, () => this.instantiate(abstract, undefined));
|
|
80
|
+
}
|
|
81
|
+
throw new BindingResolutionException(abstract);
|
|
82
|
+
}
|
|
83
|
+
/** Alias for {@link make}. */
|
|
84
|
+
get(abstract) {
|
|
85
|
+
return this.make(abstract);
|
|
86
|
+
}
|
|
87
|
+
/** Forget all cached singleton instances (bindings are kept). */
|
|
88
|
+
flush() {
|
|
89
|
+
this.instances.clear();
|
|
90
|
+
}
|
|
91
|
+
sharedInstance(abstract, producer) {
|
|
92
|
+
if (this.instances.has(abstract)) {
|
|
93
|
+
return this.instances.get(abstract);
|
|
94
|
+
}
|
|
95
|
+
return this.withResolving(abstract, () => {
|
|
96
|
+
const value = producer();
|
|
97
|
+
this.instances.set(abstract, value);
|
|
98
|
+
return value;
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
materialize(abstract, factory, context) {
|
|
102
|
+
if (isClassLike(factory)) {
|
|
103
|
+
return this.instantiate(factory, context);
|
|
104
|
+
}
|
|
105
|
+
return factory(this);
|
|
106
|
+
}
|
|
107
|
+
instantiate(ctor, context) {
|
|
108
|
+
// Contextual bindings resolve against the class currently being
|
|
109
|
+
// constructed (the immediate context), matching standard merge semantics.
|
|
110
|
+
const immediate = context ?? ctor;
|
|
111
|
+
const deps = ctor.dependencies ?? [];
|
|
112
|
+
const args = deps.map((dep) => this.resolveDependency(dep, immediate));
|
|
113
|
+
return new ctor(...args);
|
|
114
|
+
}
|
|
115
|
+
resolveDependency(dep, context) {
|
|
116
|
+
if (context) {
|
|
117
|
+
const override = this.contextual.get(context)?.get(dep);
|
|
118
|
+
if (override !== undefined) {
|
|
119
|
+
return this.withResolving(dep, () => this.materialize(dep, override, context));
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return this.make(dep);
|
|
123
|
+
}
|
|
124
|
+
withResolving(abstract, fn) {
|
|
125
|
+
if (this.resolvingStack.includes(abstract)) {
|
|
126
|
+
throw new CircularDependencyException(abstract, this.resolvingStack);
|
|
127
|
+
}
|
|
128
|
+
this.resolvingStack.push(abstract);
|
|
129
|
+
try {
|
|
130
|
+
return fn();
|
|
131
|
+
}
|
|
132
|
+
finally {
|
|
133
|
+
this.resolvingStack.pop();
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
//# sourceMappingURL=container.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"container.js","sourceRoot":"","sources":["../src/container.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,0BAA0B,EAAE,2BAA2B,EAAE,MAAM,iBAAiB,CAAC;AAY1F;;;;GAIG;AACH,MAAM,UAAU,WAAW,CAAC,KAAc;IACxC,IAAI,OAAO,KAAK,KAAK,UAAU;QAAE,OAAO,KAAK,CAAC;IAC9C,MAAM,KAAK,GAAI,KAAqB,CAAC,SAAS,CAAC;IAC/C,OAAO,KAAK,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,IAAI,CAAC,KAAK,EAAE,aAAa,CAAC,CAAC;AACnH,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,OAAO,SAAS;IACH,QAAQ,GAAG,IAAI,GAAG,EAAqB,CAAC;IACxC,SAAS,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G,EAAE,EAAE,CAAC,IAAI,CAAC,iBAAiB,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC,CAAC;QACvE,OAAO,IAAK,IAAoB,CAAC,GAAG,IAAI,CAAC,CAAC;IAC5C,CAAC;IAEO,iBAAiB,CAAC,GAAa,EAAE,OAAgC;QACvE,IAAI,OAAO,EAAE,CAAC;YACZ,MAAM,QAAQ,GAAG,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,GAAG,CAAC,GAAG,CAAC,CAAC;YACxD,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;gBAC3B,OAAO,IAAI,CAAC,aAAa,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,WAAW,CAAC,GAAG,EAAE,QAAQ,EAAE,OAAO,CAAC,CAAC,CAAC;YACjF,CAAC;QACH,CAAC;QACD,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACxB,CAAC;IAEO,aAAa,CAAC,QAAkB,EAAE,EAAiB;QACzD,IAAI,IAAI,CAAC,cAAc,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE,CAAC;YAC3C,MAAM,IAAI,2BAA2B,CAAC,QAAQ,EAAE,IAAI,CAAC,cAAc,CAAC,CAAC;QACvE,CAAC;QACD,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;QACnC,IAAI,CAAC;YACH,OAAO,EAAE,EAAE,CAAC;QACd,CAAC;gBAAS,CAAC;YACT,IAAI,CAAC,cAAc,CAAC,GAAG,EAAE,CAAC;QAC5B,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Abstract } from './types.js';
|
|
2
|
+
/** Thrown when an abstract has no binding and is not a constructible class. */
|
|
3
|
+
export declare class BindingResolutionException extends Error {
|
|
4
|
+
constructor(abstract: Abstract);
|
|
5
|
+
}
|
|
6
|
+
/** Thrown when resolving a binding requires itself, directly or transitively. */
|
|
7
|
+
export declare class CircularDependencyException extends Error {
|
|
8
|
+
constructor(abstract: Abstract, chain: readonly Abstract[]);
|
|
9
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
function describe(abstract) {
|
|
2
|
+
if (typeof abstract === 'string')
|
|
3
|
+
return abstract;
|
|
4
|
+
if (typeof abstract === 'symbol')
|
|
5
|
+
return abstract.description ?? String(abstract);
|
|
6
|
+
return abstract.name || '<anonymous class>';
|
|
7
|
+
}
|
|
8
|
+
/** Thrown when an abstract has no binding and is not a constructible class. */
|
|
9
|
+
export class BindingResolutionException extends Error {
|
|
10
|
+
constructor(abstract) {
|
|
11
|
+
super(`[container] Unable to resolve "${describe(abstract)}": no binding is registered and it is not a constructible class.`);
|
|
12
|
+
this.name = 'BindingResolutionException';
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
/** Thrown when resolving a binding requires itself, directly or transitively. */
|
|
16
|
+
export class CircularDependencyException extends Error {
|
|
17
|
+
constructor(abstract, chain) {
|
|
18
|
+
super(`[container] Circular dependency detected: ${[...chain, abstract].map(describe).join(' -> ')}`);
|
|
19
|
+
this.name = 'CircularDependencyException';
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=exceptions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"exceptions.js","sourceRoot":"","sources":["../src/exceptions.ts"],"names":[],"mappings":"AAEA,SAAS,QAAQ,CAAC,QAAkB;IAClC,IAAI,OAAO,QAAQ,KAAK,QAAQ;QAAE,OAAO,QAAQ,CAAC;IAClD,IAAI,OAAO,QAAQ,KAAK,QAAQ;QAAE,OAAO,QAAQ,CAAC,WAAW,IAAI,MAAM,CAAC,QAAQ,CAAC,CAAC;IAClF,OAAO,QAAQ,CAAC,IAAI,IAAI,mBAAmB,CAAC;AAC9C,CAAC;AAED,+EAA+E;AAC/E,MAAM,OAAO,0BAA2B,SAAQ,KAAK;IACnD,YAAY,QAAkB;QAC5B,KAAK,CACH,kCAAkC,QAAQ,CAAC,QAAQ,CAAC,kEAAkE,CACvH,CAAC;QACF,IAAI,CAAC,IAAI,GAAG,4BAA4B,CAAC;IAC3C,CAAC;CACF;AAED,iFAAiF;AACjF,MAAM,OAAO,2BAA4B,SAAQ,KAAK;IACpD,YAAY,QAAkB,EAAE,KAA0B;QACxD,KAAK,CAAC,6CAA6C,CAAC,GAAG,KAAK,EAAE,QAAQ,CAAC,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;QACtG,IAAI,CAAC,IAAI,GAAG,6BAA6B,CAAC;IAC5C,CAAC;CACF"}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AACxD,OAAO,EAAE,0BAA0B,EAAE,2BAA2B,EAAE,MAAM,iBAAiB,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Container } from './container.js';
|
|
2
|
+
/** A class constructor that can be bound or resolved. */
|
|
3
|
+
export type Constructor<T = unknown> = new (...args: any[]) => T;
|
|
4
|
+
/** Anything the container can resolve: a string/symbol key or a concrete class. */
|
|
5
|
+
export type Abstract<T = unknown> = string | symbol | Constructor<T>;
|
|
6
|
+
/**
|
|
7
|
+
* A factory that produces an instance. Receives the container so it can
|
|
8
|
+
* resolve nested dependencies. Use an arrow function for factories —
|
|
9
|
+
* class values are treated as injectable constructors instead.
|
|
10
|
+
*/
|
|
11
|
+
export type Factory<T = unknown> = (container: Container) => T;
|
|
12
|
+
/**
|
|
13
|
+
* Optional contract for classes the container should instantiate with
|
|
14
|
+
* constructor auto-injection. List the abstracts to inject, in order.
|
|
15
|
+
*/
|
|
16
|
+
export interface Injectable {
|
|
17
|
+
readonly dependencies?: readonly Abstract[];
|
|
18
|
+
}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mudah-cli/container",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "High-performance IoC service container with constructor auto-injection.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": ">=26"
|
|
9
|
+
},
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"sideEffects": false,
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public"
|
|
22
|
+
}
|
|
23
|
+
}
|