proxydi 0.0.3

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.
@@ -0,0 +1,32 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - main
7
+ pull_request:
8
+ branches:
9
+ - main
10
+
11
+ jobs:
12
+ test:
13
+ runs-on: ubuntu-latest
14
+ steps:
15
+ - uses: actions/checkout@v3
16
+
17
+ - name: Setup Node.js
18
+ uses: actions/setup-node@v3
19
+ with:
20
+ node-version: '22'
21
+
22
+ - name: Install dependencies
23
+ run: npm install
24
+
25
+ - name: Run tests with coverage
26
+ run: npm run coverage
27
+
28
+ - name: Upload coverage to Coveralls
29
+ uses: coverallsapp/github-action@v2
30
+ with:
31
+ github-token: ${{ secrets.GITHUB_TOKEN }}
32
+ path-to-lcov: ./coverage/lcov.info
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 proxy-di
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # ProxyDI
2
+
3
+ [![Coverage Status](https://coveralls.io/repos/github/proxy-di/proxydi/badge.svg?branch=main)](https://coveralls.io/github/proxy-di/proxydi?branch=main)
4
+
5
+ A typed DI container that resolves circular dependencies via Proxy.
6
+
7
+ Core notes:
8
+
9
+ - Uses Stage 3 decorators (supported in TypeScript 5.x and babel-plugin-proposal-decorators)
10
+ - Automatically resolves circular dependencies
11
+ - Matches services by unique identifiers, class or property names
12
+ - Currently in active development
13
+
14
+ # Quick start
15
+
16
+ ```shell
17
+ npm i proxydi
18
+ ```
19
+
20
+ If you are using TypeScript be sure that your tsconfig.json set exactly `false` value for `experimentalDecorators`. This could be not obvious, but this setup turns on support of Stage 3 decorators.
21
+
22
+ ```jsonc
23
+ {
24
+ "compilerOptions": {
25
+ // ...
26
+ "experimentalDecorators": false,
27
+ },
28
+ //...
29
+ }
30
+ ```
31
+
32
+ 1. @inject your dependencies
33
+
34
+ ```typescript
35
+ import { inject } from 'proxydi';
36
+
37
+ interface Personality {
38
+ greet(): string;
39
+ }
40
+
41
+ class Agent {
42
+ @inject() personality: Personality;
43
+ }
44
+ ```
45
+
46
+ 2. Create, fill ProxyDI container
47
+
48
+ ```typescript
49
+ import { ProxyDI } from 'proxydi';
50
+
51
+ class Jarvis implements Personality {
52
+ greet: () => 'Hello, sir!';
53
+ }
54
+
55
+ const container = new ProxyDI();
56
+ container.registerClass('personality', Jarvis);
57
+ ```
58
+
59
+ 3. Use dependencies
60
+
61
+ ```typescript
62
+ const agent = container.resolve(Agent);
63
+ console.log(agent.personality.greet());
64
+ ```
65
+
66
+ ```shell
67
+ > Hello, sir!
68
+ ```
69
+
70
+ To be continued...
@@ -0,0 +1,32 @@
1
+ import { ProxyDISettings, ServiceClass, ServiceId } from './types';
2
+ export declare const PROXYDI: unique symbol;
3
+ export declare const PROXYDI_ID: unique symbol;
4
+ export declare class ProxyDI {
5
+ private static idCounter;
6
+ readonly id: number;
7
+ readonly parent?: ProxyDI;
8
+ private children;
9
+ private instances;
10
+ private classes;
11
+ private proxies;
12
+ private proxyFactory;
13
+ protected allowRewriteClasses: boolean;
14
+ protected allowRewriteInstances: boolean;
15
+ constructor(settings?: ProxyDISettings);
16
+ registerInstance<T>(serviceId: ServiceId, instance: T extends {
17
+ new (...args: any[]): any;
18
+ } ? never : T): void;
19
+ registerClass<T>(serviceId: ServiceId, serviceClass: ServiceClass<T>): void;
20
+ private findInstance;
21
+ isKnown(serviceId: ServiceId): boolean;
22
+ resolve<T>(serviceId: ServiceId): T;
23
+ private findDefiner;
24
+ injectDependencies(instance: any): void;
25
+ private getProxy;
26
+ createChildContainer(settings?: ProxyDISettings): ProxyDI;
27
+ removeInstance(serviceId: ServiceId): void;
28
+ removeClass(serviceId: ServiceId): void;
29
+ destroy(): void;
30
+ addChild(child: ProxyDI): void;
31
+ private removeChild;
32
+ }
@@ -0,0 +1,8 @@
1
+ import { ProxyDI } from './ProxyDI';
2
+ import { ServiceId } from './types';
3
+ export declare class ProxyFactory {
4
+ private container;
5
+ constructor(container: ProxyDI);
6
+ makeProxy<T>(serviceId: ServiceId): T;
7
+ }
8
+ export declare function isProxy(value: any): boolean;
package/dist/index.cjs ADDED
@@ -0,0 +1,230 @@
1
+ 'use strict';
2
+
3
+ const INJECTS = Symbol('injects');
4
+ const inject = (serviceId) => {
5
+ return function (_value, context) {
6
+ if ((context === null || context === undefined ? undefined : context.kind) === 'field') {
7
+ const name = serviceId ? serviceId : context.name;
8
+ const inject = {
9
+ property: context.name,
10
+ serviceId: name,
11
+ set: context.access.set,
12
+ };
13
+ context.addInitializer(function () {
14
+ if (!this[INJECTS]) {
15
+ this[INJECTS] = [];
16
+ }
17
+ this[INJECTS].push(inject);
18
+ });
19
+ }
20
+ };
21
+ };
22
+
23
+ const injectableClasses = {};
24
+
25
+ const IS_PROXY = Symbol('isProxy');
26
+ class ProxyFactory {
27
+ constructor(container) {
28
+ this.container = container;
29
+ }
30
+ makeProxy(serviceId) {
31
+ const self = this;
32
+ const proxy = new Proxy({ [IS_PROXY]: true }, {
33
+ get: function (target, prop, receiver) {
34
+ if (target[prop]) {
35
+ return target[prop];
36
+ }
37
+ if (self.container.isKnown(serviceId)) {
38
+ const instance = self.container.resolve(serviceId);
39
+ return Reflect.get(instance, prop, receiver);
40
+ }
41
+ else {
42
+ throw new Error(`Unknown ProxyDI-service: ${serviceId}`);
43
+ }
44
+ },
45
+ set: function (target, prop, value) {
46
+ if (self.container.isKnown(serviceId)) {
47
+ const instance = self.container.resolve(serviceId);
48
+ return Reflect.set(instance, prop, value);
49
+ }
50
+ else {
51
+ throw new Error(`Unknown ProxyDI-service: ${serviceId}`);
52
+ }
53
+ },
54
+ has: function (target, prop) {
55
+ if (self.container.isKnown(serviceId)) {
56
+ const instance = self.container.resolve(serviceId);
57
+ return Reflect.has(instance, prop);
58
+ }
59
+ else {
60
+ throw new Error(`Unknown ProxyDI-service: ${serviceId}`);
61
+ }
62
+ },
63
+ });
64
+ return proxy;
65
+ }
66
+ }
67
+
68
+ const PROXYDI = Symbol('ProxyDI');
69
+ const PROXYDI_ID = Symbol('ProxyDI_ID');
70
+ class ProxyDI {
71
+ constructor(settings) {
72
+ this.children = {};
73
+ this.instances = {};
74
+ this.classes = {};
75
+ this.proxies = {};
76
+ this.allowRewriteClasses = false;
77
+ this.allowRewriteInstances = false;
78
+ this.proxyFactory = new ProxyFactory(this);
79
+ this.id = ProxyDI.idCounter++;
80
+ if (settings === null || settings === undefined ? undefined : settings.parent) {
81
+ this.parent = settings.parent;
82
+ this.parent.addChild(this);
83
+ }
84
+ if ((settings === null || settings === undefined ? undefined : settings.allowRewriteClasses) !== undefined) {
85
+ this.allowRewriteClasses = settings.allowRewriteClasses;
86
+ }
87
+ if ((settings === null || settings === undefined ? undefined : settings.allowRewriteInstances) !== undefined) {
88
+ this.allowRewriteInstances = settings.allowRewriteInstances;
89
+ }
90
+ }
91
+ registerInstance(serviceId, instance) {
92
+ if (this.instances[serviceId]) {
93
+ if (!this.allowRewriteInstances) {
94
+ throw new Error(`ProxyDI already has registered instance for ${serviceId}`);
95
+ }
96
+ }
97
+ this.injectDependencies(instance);
98
+ if (typeof instance === 'object') {
99
+ instance[PROXYDI_ID] = serviceId;
100
+ }
101
+ this.instances[serviceId] = instance;
102
+ }
103
+ registerClass(serviceId, serviceClass) {
104
+ if (this.classes[serviceId]) {
105
+ if (!this.allowRewriteClasses) {
106
+ throw new Error(`ProxyDI already has registered class for ${serviceId}`);
107
+ }
108
+ }
109
+ this.classes[serviceId] = serviceClass;
110
+ }
111
+ findInstance(serviceId) {
112
+ const instance = this.instances[serviceId];
113
+ if (!instance && this.parent) {
114
+ return this.parent.findInstance(serviceId);
115
+ }
116
+ return instance;
117
+ }
118
+ isKnown(serviceId) {
119
+ return !!(this.findInstance(serviceId) || this.findDefiner(serviceId));
120
+ }
121
+ resolve(serviceId) {
122
+ if (!this.isKnown(serviceId)) {
123
+ throw new Error(`Can't resolve unknown ProxyDI-service: ${serviceId}`);
124
+ }
125
+ const instance = this.findInstance(serviceId);
126
+ if (instance) {
127
+ return instance;
128
+ }
129
+ const definer = this.findDefiner(serviceId);
130
+ if (definer) {
131
+ let instance = new definer();
132
+ this.injectDependencies(instance);
133
+ instance[PROXYDI_ID] = serviceId;
134
+ this.instances[serviceId] = instance;
135
+ return instance;
136
+ }
137
+ else {
138
+ return this.getProxy(serviceId);
139
+ }
140
+ }
141
+ findDefiner(serviceId) {
142
+ let definer = this.classes[serviceId];
143
+ if (!definer && this.parent) {
144
+ return this.parent.findDefiner(serviceId);
145
+ }
146
+ if (!definer) {
147
+ return injectableClasses[serviceId];
148
+ }
149
+ return definer;
150
+ }
151
+ injectDependencies(instance) {
152
+ const serviceInjects = instance[INJECTS] || [];
153
+ serviceInjects.forEach((inject) => {
154
+ let value = this.findInstance(inject.serviceId);
155
+ if (!value) {
156
+ value = this.getProxy(inject.serviceId);
157
+ }
158
+ inject.set(instance, value);
159
+ });
160
+ if (typeof instance === 'object') {
161
+ instance[PROXYDI] = this;
162
+ }
163
+ }
164
+ getProxy(serviceId) {
165
+ let proxy = this.proxies[serviceId];
166
+ if (!proxy) {
167
+ proxy = this.proxyFactory.makeProxy(serviceId);
168
+ this.proxies[serviceId] = proxy;
169
+ }
170
+ return proxy;
171
+ }
172
+ createChildContainer(settings) {
173
+ const childSettings = Object.assign(Object.assign({}, settings), { parent: this });
174
+ return new ProxyDI(childSettings);
175
+ }
176
+ removeInstance(serviceId) {
177
+ const id = serviceId[PROXYDI_ID]
178
+ ? serviceId[PROXYDI_ID]
179
+ : serviceId;
180
+ const instance = this.instances[id];
181
+ if (instance) {
182
+ const serviceInjects = instance[INJECTS] || [];
183
+ serviceInjects.forEach((inject) => {
184
+ inject.set(instance, undefined);
185
+ });
186
+ delete instance[PROXYDI];
187
+ delete instance[PROXYDI_ID];
188
+ delete this.instances[serviceId];
189
+ }
190
+ }
191
+ removeClass(serviceId) {
192
+ delete this.classes[serviceId];
193
+ }
194
+ destroy() {
195
+ const allServices = Object.keys(this.instances);
196
+ for (const serviceId of allServices) {
197
+ const instance = this.instances[serviceId];
198
+ this.removeInstance(instance);
199
+ }
200
+ this.instances = {};
201
+ this.proxies = {};
202
+ this.classes = {};
203
+ for (const id of Object.keys(this.children)) {
204
+ const child = this.children[+id];
205
+ child.destroy();
206
+ }
207
+ this.children = {};
208
+ this.proxyFactory = undefined;
209
+ if (this.parent) {
210
+ this.parent.removeChild(this.id);
211
+ this.parent = undefined;
212
+ }
213
+ }
214
+ addChild(child) {
215
+ if (this.children[child.id]) {
216
+ throw new Error(`ProxyDI already has child with id ${child.id}`);
217
+ }
218
+ this.children[child.id] = child;
219
+ }
220
+ removeChild(id) {
221
+ const child = this.children[id];
222
+ if (child) {
223
+ delete this.children[id];
224
+ }
225
+ }
226
+ }
227
+ ProxyDI.idCounter = 0;
228
+
229
+ exports.ProxyDI = ProxyDI;
230
+ exports.inject = inject;
@@ -0,0 +1,3 @@
1
+ export { ProxyDI } from './ProxyDI';
2
+ export { inject } from './inject';
3
+ export { Inject, ServiceId, ServiceClass, ProxyDISettings } from './types';
package/dist/index.js ADDED
@@ -0,0 +1,227 @@
1
+ const INJECTS = Symbol('injects');
2
+ const inject = (serviceId) => {
3
+ return function (_value, context) {
4
+ if ((context === null || context === undefined ? undefined : context.kind) === 'field') {
5
+ const name = serviceId ? serviceId : context.name;
6
+ const inject = {
7
+ property: context.name,
8
+ serviceId: name,
9
+ set: context.access.set,
10
+ };
11
+ context.addInitializer(function () {
12
+ if (!this[INJECTS]) {
13
+ this[INJECTS] = [];
14
+ }
15
+ this[INJECTS].push(inject);
16
+ });
17
+ }
18
+ };
19
+ };
20
+
21
+ const injectableClasses = {};
22
+
23
+ const IS_PROXY = Symbol('isProxy');
24
+ class ProxyFactory {
25
+ constructor(container) {
26
+ this.container = container;
27
+ }
28
+ makeProxy(serviceId) {
29
+ const self = this;
30
+ const proxy = new Proxy({ [IS_PROXY]: true }, {
31
+ get: function (target, prop, receiver) {
32
+ if (target[prop]) {
33
+ return target[prop];
34
+ }
35
+ if (self.container.isKnown(serviceId)) {
36
+ const instance = self.container.resolve(serviceId);
37
+ return Reflect.get(instance, prop, receiver);
38
+ }
39
+ else {
40
+ throw new Error(`Unknown ProxyDI-service: ${serviceId}`);
41
+ }
42
+ },
43
+ set: function (target, prop, value) {
44
+ if (self.container.isKnown(serviceId)) {
45
+ const instance = self.container.resolve(serviceId);
46
+ return Reflect.set(instance, prop, value);
47
+ }
48
+ else {
49
+ throw new Error(`Unknown ProxyDI-service: ${serviceId}`);
50
+ }
51
+ },
52
+ has: function (target, prop) {
53
+ if (self.container.isKnown(serviceId)) {
54
+ const instance = self.container.resolve(serviceId);
55
+ return Reflect.has(instance, prop);
56
+ }
57
+ else {
58
+ throw new Error(`Unknown ProxyDI-service: ${serviceId}`);
59
+ }
60
+ },
61
+ });
62
+ return proxy;
63
+ }
64
+ }
65
+
66
+ const PROXYDI = Symbol('ProxyDI');
67
+ const PROXYDI_ID = Symbol('ProxyDI_ID');
68
+ class ProxyDI {
69
+ constructor(settings) {
70
+ this.children = {};
71
+ this.instances = {};
72
+ this.classes = {};
73
+ this.proxies = {};
74
+ this.allowRewriteClasses = false;
75
+ this.allowRewriteInstances = false;
76
+ this.proxyFactory = new ProxyFactory(this);
77
+ this.id = ProxyDI.idCounter++;
78
+ if (settings === null || settings === undefined ? undefined : settings.parent) {
79
+ this.parent = settings.parent;
80
+ this.parent.addChild(this);
81
+ }
82
+ if ((settings === null || settings === undefined ? undefined : settings.allowRewriteClasses) !== undefined) {
83
+ this.allowRewriteClasses = settings.allowRewriteClasses;
84
+ }
85
+ if ((settings === null || settings === undefined ? undefined : settings.allowRewriteInstances) !== undefined) {
86
+ this.allowRewriteInstances = settings.allowRewriteInstances;
87
+ }
88
+ }
89
+ registerInstance(serviceId, instance) {
90
+ if (this.instances[serviceId]) {
91
+ if (!this.allowRewriteInstances) {
92
+ throw new Error(`ProxyDI already has registered instance for ${serviceId}`);
93
+ }
94
+ }
95
+ this.injectDependencies(instance);
96
+ if (typeof instance === 'object') {
97
+ instance[PROXYDI_ID] = serviceId;
98
+ }
99
+ this.instances[serviceId] = instance;
100
+ }
101
+ registerClass(serviceId, serviceClass) {
102
+ if (this.classes[serviceId]) {
103
+ if (!this.allowRewriteClasses) {
104
+ throw new Error(`ProxyDI already has registered class for ${serviceId}`);
105
+ }
106
+ }
107
+ this.classes[serviceId] = serviceClass;
108
+ }
109
+ findInstance(serviceId) {
110
+ const instance = this.instances[serviceId];
111
+ if (!instance && this.parent) {
112
+ return this.parent.findInstance(serviceId);
113
+ }
114
+ return instance;
115
+ }
116
+ isKnown(serviceId) {
117
+ return !!(this.findInstance(serviceId) || this.findDefiner(serviceId));
118
+ }
119
+ resolve(serviceId) {
120
+ if (!this.isKnown(serviceId)) {
121
+ throw new Error(`Can't resolve unknown ProxyDI-service: ${serviceId}`);
122
+ }
123
+ const instance = this.findInstance(serviceId);
124
+ if (instance) {
125
+ return instance;
126
+ }
127
+ const definer = this.findDefiner(serviceId);
128
+ if (definer) {
129
+ let instance = new definer();
130
+ this.injectDependencies(instance);
131
+ instance[PROXYDI_ID] = serviceId;
132
+ this.instances[serviceId] = instance;
133
+ return instance;
134
+ }
135
+ else {
136
+ return this.getProxy(serviceId);
137
+ }
138
+ }
139
+ findDefiner(serviceId) {
140
+ let definer = this.classes[serviceId];
141
+ if (!definer && this.parent) {
142
+ return this.parent.findDefiner(serviceId);
143
+ }
144
+ if (!definer) {
145
+ return injectableClasses[serviceId];
146
+ }
147
+ return definer;
148
+ }
149
+ injectDependencies(instance) {
150
+ const serviceInjects = instance[INJECTS] || [];
151
+ serviceInjects.forEach((inject) => {
152
+ let value = this.findInstance(inject.serviceId);
153
+ if (!value) {
154
+ value = this.getProxy(inject.serviceId);
155
+ }
156
+ inject.set(instance, value);
157
+ });
158
+ if (typeof instance === 'object') {
159
+ instance[PROXYDI] = this;
160
+ }
161
+ }
162
+ getProxy(serviceId) {
163
+ let proxy = this.proxies[serviceId];
164
+ if (!proxy) {
165
+ proxy = this.proxyFactory.makeProxy(serviceId);
166
+ this.proxies[serviceId] = proxy;
167
+ }
168
+ return proxy;
169
+ }
170
+ createChildContainer(settings) {
171
+ const childSettings = Object.assign(Object.assign({}, settings), { parent: this });
172
+ return new ProxyDI(childSettings);
173
+ }
174
+ removeInstance(serviceId) {
175
+ const id = serviceId[PROXYDI_ID]
176
+ ? serviceId[PROXYDI_ID]
177
+ : serviceId;
178
+ const instance = this.instances[id];
179
+ if (instance) {
180
+ const serviceInjects = instance[INJECTS] || [];
181
+ serviceInjects.forEach((inject) => {
182
+ inject.set(instance, undefined);
183
+ });
184
+ delete instance[PROXYDI];
185
+ delete instance[PROXYDI_ID];
186
+ delete this.instances[serviceId];
187
+ }
188
+ }
189
+ removeClass(serviceId) {
190
+ delete this.classes[serviceId];
191
+ }
192
+ destroy() {
193
+ const allServices = Object.keys(this.instances);
194
+ for (const serviceId of allServices) {
195
+ const instance = this.instances[serviceId];
196
+ this.removeInstance(instance);
197
+ }
198
+ this.instances = {};
199
+ this.proxies = {};
200
+ this.classes = {};
201
+ for (const id of Object.keys(this.children)) {
202
+ const child = this.children[+id];
203
+ child.destroy();
204
+ }
205
+ this.children = {};
206
+ this.proxyFactory = undefined;
207
+ if (this.parent) {
208
+ this.parent.removeChild(this.id);
209
+ this.parent = undefined;
210
+ }
211
+ }
212
+ addChild(child) {
213
+ if (this.children[child.id]) {
214
+ throw new Error(`ProxyDI already has child with id ${child.id}`);
215
+ }
216
+ this.children[child.id] = child;
217
+ }
218
+ removeChild(id) {
219
+ const child = this.children[id];
220
+ if (child) {
221
+ delete this.children[id];
222
+ }
223
+ }
224
+ }
225
+ ProxyDI.idCounter = 0;
226
+
227
+ export { ProxyDI, inject };
@@ -0,0 +1,236 @@
1
+ (function (global, factory) {
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3
+ typeof define === 'function' && define.amd ? define(['exports'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.YourLibraryName = {}));
5
+ })(this, (function (exports) { 'use strict';
6
+
7
+ const INJECTS = Symbol('injects');
8
+ const inject = (serviceId) => {
9
+ return function (_value, context) {
10
+ if ((context === null || context === undefined ? undefined : context.kind) === 'field') {
11
+ const name = serviceId ? serviceId : context.name;
12
+ const inject = {
13
+ property: context.name,
14
+ serviceId: name,
15
+ set: context.access.set,
16
+ };
17
+ context.addInitializer(function () {
18
+ if (!this[INJECTS]) {
19
+ this[INJECTS] = [];
20
+ }
21
+ this[INJECTS].push(inject);
22
+ });
23
+ }
24
+ };
25
+ };
26
+
27
+ const injectableClasses = {};
28
+
29
+ const IS_PROXY = Symbol('isProxy');
30
+ class ProxyFactory {
31
+ constructor(container) {
32
+ this.container = container;
33
+ }
34
+ makeProxy(serviceId) {
35
+ const self = this;
36
+ const proxy = new Proxy({ [IS_PROXY]: true }, {
37
+ get: function (target, prop, receiver) {
38
+ if (target[prop]) {
39
+ return target[prop];
40
+ }
41
+ if (self.container.isKnown(serviceId)) {
42
+ const instance = self.container.resolve(serviceId);
43
+ return Reflect.get(instance, prop, receiver);
44
+ }
45
+ else {
46
+ throw new Error(`Unknown ProxyDI-service: ${serviceId}`);
47
+ }
48
+ },
49
+ set: function (target, prop, value) {
50
+ if (self.container.isKnown(serviceId)) {
51
+ const instance = self.container.resolve(serviceId);
52
+ return Reflect.set(instance, prop, value);
53
+ }
54
+ else {
55
+ throw new Error(`Unknown ProxyDI-service: ${serviceId}`);
56
+ }
57
+ },
58
+ has: function (target, prop) {
59
+ if (self.container.isKnown(serviceId)) {
60
+ const instance = self.container.resolve(serviceId);
61
+ return Reflect.has(instance, prop);
62
+ }
63
+ else {
64
+ throw new Error(`Unknown ProxyDI-service: ${serviceId}`);
65
+ }
66
+ },
67
+ });
68
+ return proxy;
69
+ }
70
+ }
71
+
72
+ const PROXYDI = Symbol('ProxyDI');
73
+ const PROXYDI_ID = Symbol('ProxyDI_ID');
74
+ class ProxyDI {
75
+ constructor(settings) {
76
+ this.children = {};
77
+ this.instances = {};
78
+ this.classes = {};
79
+ this.proxies = {};
80
+ this.allowRewriteClasses = false;
81
+ this.allowRewriteInstances = false;
82
+ this.proxyFactory = new ProxyFactory(this);
83
+ this.id = ProxyDI.idCounter++;
84
+ if (settings === null || settings === undefined ? undefined : settings.parent) {
85
+ this.parent = settings.parent;
86
+ this.parent.addChild(this);
87
+ }
88
+ if ((settings === null || settings === undefined ? undefined : settings.allowRewriteClasses) !== undefined) {
89
+ this.allowRewriteClasses = settings.allowRewriteClasses;
90
+ }
91
+ if ((settings === null || settings === undefined ? undefined : settings.allowRewriteInstances) !== undefined) {
92
+ this.allowRewriteInstances = settings.allowRewriteInstances;
93
+ }
94
+ }
95
+ registerInstance(serviceId, instance) {
96
+ if (this.instances[serviceId]) {
97
+ if (!this.allowRewriteInstances) {
98
+ throw new Error(`ProxyDI already has registered instance for ${serviceId}`);
99
+ }
100
+ }
101
+ this.injectDependencies(instance);
102
+ if (typeof instance === 'object') {
103
+ instance[PROXYDI_ID] = serviceId;
104
+ }
105
+ this.instances[serviceId] = instance;
106
+ }
107
+ registerClass(serviceId, serviceClass) {
108
+ if (this.classes[serviceId]) {
109
+ if (!this.allowRewriteClasses) {
110
+ throw new Error(`ProxyDI already has registered class for ${serviceId}`);
111
+ }
112
+ }
113
+ this.classes[serviceId] = serviceClass;
114
+ }
115
+ findInstance(serviceId) {
116
+ const instance = this.instances[serviceId];
117
+ if (!instance && this.parent) {
118
+ return this.parent.findInstance(serviceId);
119
+ }
120
+ return instance;
121
+ }
122
+ isKnown(serviceId) {
123
+ return !!(this.findInstance(serviceId) || this.findDefiner(serviceId));
124
+ }
125
+ resolve(serviceId) {
126
+ if (!this.isKnown(serviceId)) {
127
+ throw new Error(`Can't resolve unknown ProxyDI-service: ${serviceId}`);
128
+ }
129
+ const instance = this.findInstance(serviceId);
130
+ if (instance) {
131
+ return instance;
132
+ }
133
+ const definer = this.findDefiner(serviceId);
134
+ if (definer) {
135
+ let instance = new definer();
136
+ this.injectDependencies(instance);
137
+ instance[PROXYDI_ID] = serviceId;
138
+ this.instances[serviceId] = instance;
139
+ return instance;
140
+ }
141
+ else {
142
+ return this.getProxy(serviceId);
143
+ }
144
+ }
145
+ findDefiner(serviceId) {
146
+ let definer = this.classes[serviceId];
147
+ if (!definer && this.parent) {
148
+ return this.parent.findDefiner(serviceId);
149
+ }
150
+ if (!definer) {
151
+ return injectableClasses[serviceId];
152
+ }
153
+ return definer;
154
+ }
155
+ injectDependencies(instance) {
156
+ const serviceInjects = instance[INJECTS] || [];
157
+ serviceInjects.forEach((inject) => {
158
+ let value = this.findInstance(inject.serviceId);
159
+ if (!value) {
160
+ value = this.getProxy(inject.serviceId);
161
+ }
162
+ inject.set(instance, value);
163
+ });
164
+ if (typeof instance === 'object') {
165
+ instance[PROXYDI] = this;
166
+ }
167
+ }
168
+ getProxy(serviceId) {
169
+ let proxy = this.proxies[serviceId];
170
+ if (!proxy) {
171
+ proxy = this.proxyFactory.makeProxy(serviceId);
172
+ this.proxies[serviceId] = proxy;
173
+ }
174
+ return proxy;
175
+ }
176
+ createChildContainer(settings) {
177
+ const childSettings = Object.assign(Object.assign({}, settings), { parent: this });
178
+ return new ProxyDI(childSettings);
179
+ }
180
+ removeInstance(serviceId) {
181
+ const id = serviceId[PROXYDI_ID]
182
+ ? serviceId[PROXYDI_ID]
183
+ : serviceId;
184
+ const instance = this.instances[id];
185
+ if (instance) {
186
+ const serviceInjects = instance[INJECTS] || [];
187
+ serviceInjects.forEach((inject) => {
188
+ inject.set(instance, undefined);
189
+ });
190
+ delete instance[PROXYDI];
191
+ delete instance[PROXYDI_ID];
192
+ delete this.instances[serviceId];
193
+ }
194
+ }
195
+ removeClass(serviceId) {
196
+ delete this.classes[serviceId];
197
+ }
198
+ destroy() {
199
+ const allServices = Object.keys(this.instances);
200
+ for (const serviceId of allServices) {
201
+ const instance = this.instances[serviceId];
202
+ this.removeInstance(instance);
203
+ }
204
+ this.instances = {};
205
+ this.proxies = {};
206
+ this.classes = {};
207
+ for (const id of Object.keys(this.children)) {
208
+ const child = this.children[+id];
209
+ child.destroy();
210
+ }
211
+ this.children = {};
212
+ this.proxyFactory = undefined;
213
+ if (this.parent) {
214
+ this.parent.removeChild(this.id);
215
+ this.parent = undefined;
216
+ }
217
+ }
218
+ addChild(child) {
219
+ if (this.children[child.id]) {
220
+ throw new Error(`ProxyDI already has child with id ${child.id}`);
221
+ }
222
+ this.children[child.id] = child;
223
+ }
224
+ removeChild(id) {
225
+ const child = this.children[id];
226
+ if (child) {
227
+ delete this.children[id];
228
+ }
229
+ }
230
+ }
231
+ ProxyDI.idCounter = 0;
232
+
233
+ exports.ProxyDI = ProxyDI;
234
+ exports.inject = inject;
235
+
236
+ }));
@@ -0,0 +1,3 @@
1
+ import { ServiceId } from './types';
2
+ export declare const INJECTS: unique symbol;
3
+ export declare const inject: (serviceId?: ServiceId) => (_value: unknown, context: ClassFieldDecoratorContext) => void;
@@ -0,0 +1,3 @@
1
+ import { ServiceClass, ServiceId } from './types';
2
+ export declare const injectableClasses: Record<ServiceId, ServiceClass<any>>;
3
+ export declare const injectable: (serviceId?: ServiceId) => (value: ServiceClass<any>, context: ClassDecoratorContext) => void;
@@ -0,0 +1,15 @@
1
+ import { ProxyDI } from './ProxyDI';
2
+ export type ServiceId = any;
3
+ export type ServiceConstructor<T extends unknown> = new () => T;
4
+ export type Setter = (object: unknown, value: unknown) => void;
5
+ export type Inject = {
6
+ property: string | symbol;
7
+ serviceId: ServiceId;
8
+ set: Setter;
9
+ };
10
+ export type ServiceClass<T> = new (...args: any[]) => T;
11
+ export type ProxyDISettings = {
12
+ parent?: ProxyDI;
13
+ allowRewriteClasses?: boolean;
14
+ allowRewriteInstances?: boolean;
15
+ };
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "proxydi",
3
+ "version": "0.0.3",
4
+ "description": "A typed DI container that resolves circular dependencies via Proxy",
5
+ "type": "module",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js",
13
+ "require": "./dist/index.cjs"
14
+ }
15
+ },
16
+ "scripts": {
17
+ "build": "rollup -c",
18
+ "dev": "rollup -c -w",
19
+ "test": "vitest",
20
+ "lint": "eslint src --ext .ts",
21
+ "format": "prettier --write \"src/**/*.ts\"",
22
+ "prepare": "npm run build",
23
+ "coverage": "vitest run --coverage"
24
+ },
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "git+https://github.com/proxy-di/proxydi.git"
28
+ },
29
+ "keywords": [
30
+ "DI",
31
+ "DI-container",
32
+ "dependency-injection",
33
+ "dependency-injection-container",
34
+ "injector",
35
+ "ioc",
36
+ "inversion-of-control",
37
+ "proxy",
38
+ "typescript"
39
+ ],
40
+ "author": "Yolziii",
41
+ "license": "MIT",
42
+ "bugs": {
43
+ "url": "https://github.com/proxy-di/proxydi/issues"
44
+ },
45
+ "homepage": "https://github.com/proxy-di/proxydi#readme",
46
+ "devDependencies": {
47
+ "@rollup/plugin-typescript": "^12.1.2",
48
+ "@typescript-eslint/eslint-plugin": "^8.22.0",
49
+ "@typescript-eslint/parser": "^8.22.0",
50
+ "@vitest/coverage-v8": "^3.0.4",
51
+ "eslint": "^9.19.0",
52
+ "prettier": "^3.4.2",
53
+ "rollup": "^4.32.1",
54
+ "typescript": "^5.7.3",
55
+ "vitest": "^3.0.4"
56
+ }
57
+ }