@typeix/di 9.0.0 → 9.1.0-rc.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.
- package/.eslintrc.js +31 -18
- package/README.md +66 -5
- package/dist/helpers/provider-spec.js +32 -0
- package/dist/helpers/provider.js +7 -2
- package/dist/injector/abstract-injector.d.ts +4 -0
- package/dist/injector/abstract-injector.js +18 -6
- package/dist/injector/injector-hardening-spec.d.ts +1 -0
- package/dist/injector/injector-hardening-spec.js +254 -0
- package/dist/injector/metadata-cache-spec.d.ts +1 -0
- package/dist/injector/metadata-cache-spec.js +48 -0
- package/dist/injector/sync-injector.d.ts +8 -2
- package/dist/injector/sync-injector.js +34 -19
- package/package.json +4 -4
package/.eslintrc.js
CHANGED
|
@@ -33,23 +33,23 @@ module.exports = {
|
|
|
33
33
|
"accessibility": "explicit"
|
|
34
34
|
}
|
|
35
35
|
],
|
|
36
|
-
"
|
|
37
|
-
"error",
|
|
38
|
-
2
|
|
39
|
-
],
|
|
40
|
-
"@typescript-eslint/member-delimiter-style": [
|
|
36
|
+
"indent": [
|
|
41
37
|
"error",
|
|
38
|
+
2,
|
|
42
39
|
{
|
|
43
|
-
"
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
"
|
|
49
|
-
"
|
|
50
|
-
|
|
40
|
+
"SwitchCase": 1,
|
|
41
|
+
"flatTernaryExpressions": false,
|
|
42
|
+
"ignoredNodes": [
|
|
43
|
+
"PropertyDefinition[decorators]",
|
|
44
|
+
"ClassProperty[decorators]",
|
|
45
|
+
"PropertyDefinition",
|
|
46
|
+
"ClassProperty",
|
|
47
|
+
"TSUnionType",
|
|
48
|
+
"FunctionExpression[params]:has(Identifier[decorators])"
|
|
49
|
+
]
|
|
51
50
|
}
|
|
52
51
|
],
|
|
52
|
+
|
|
53
53
|
"@typescript-eslint/member-ordering": "error",
|
|
54
54
|
"@typescript-eslint/naming-convention": [
|
|
55
55
|
"error",
|
|
@@ -71,18 +71,17 @@ module.exports = {
|
|
|
71
71
|
"@typescript-eslint/no-unused-expressions": "error",
|
|
72
72
|
"@typescript-eslint/no-unused-vars": "error",
|
|
73
73
|
"@typescript-eslint/no-use-before-define": "warn",
|
|
74
|
-
"
|
|
74
|
+
"quotes": [
|
|
75
75
|
"error",
|
|
76
76
|
"double",
|
|
77
77
|
{
|
|
78
78
|
"avoidEscape": true
|
|
79
79
|
}
|
|
80
80
|
],
|
|
81
|
-
"
|
|
81
|
+
"semi": [
|
|
82
82
|
"error",
|
|
83
83
|
"always"
|
|
84
84
|
],
|
|
85
|
-
"@typescript-eslint/type-annotation-spacing": "error",
|
|
86
85
|
"array-bracket-spacing": "off",
|
|
87
86
|
"brace-style": [
|
|
88
87
|
"error",
|
|
@@ -101,7 +100,10 @@ module.exports = {
|
|
|
101
100
|
"max-len": [
|
|
102
101
|
"error",
|
|
103
102
|
{
|
|
104
|
-
"code": 140
|
|
103
|
+
"code": 140,
|
|
104
|
+
"ignoreComments": true,
|
|
105
|
+
"ignoreUrls": true,
|
|
106
|
+
"ignoreRegExpLiterals": true
|
|
105
107
|
}
|
|
106
108
|
],
|
|
107
109
|
"no-bitwise": "off",
|
|
@@ -158,5 +160,16 @@ module.exports = {
|
|
|
158
160
|
]
|
|
159
161
|
}
|
|
160
162
|
]
|
|
161
|
-
}
|
|
163
|
+
},
|
|
164
|
+
"overrides": [
|
|
165
|
+
{
|
|
166
|
+
"files": ["**/*-spec.ts", "**/*.spec.ts"],
|
|
167
|
+
"rules": {
|
|
168
|
+
"@typescript-eslint/no-unused-vars": "off",
|
|
169
|
+
"@typescript-eslint/no-empty-function": "off",
|
|
170
|
+
"max-len": "off",
|
|
171
|
+
"no-shadow": "off"
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
]
|
|
162
175
|
};
|
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@ tools for building efficient and scalable applications.
|
|
|
10
10
|
|
|
11
11
|
# @typeix/di
|
|
12
12
|
|
|
13
|
-
[![Build Status][
|
|
13
|
+
[![Build Status][actions-img]][actions-url]
|
|
14
14
|
[![Coverage Status][coverage-img]][coverage-url]
|
|
15
15
|
![npm][npm-version-img]
|
|
16
16
|
|
|
@@ -202,11 +202,72 @@ class SyncInjector {
|
|
|
202
202
|
setName(provider: IProvider): void;
|
|
203
203
|
hasChild(injector: Injector): boolean;
|
|
204
204
|
addChild(injector: Injector): this;
|
|
205
|
+
removeChild(injector: Injector): boolean;
|
|
205
206
|
}
|
|
206
207
|
```
|
|
207
208
|
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
209
|
+
## Injector scopes
|
|
210
|
+
|
|
211
|
+
An injector holds providers and can have one parent and many children. `get()` looks at the current injector first and
|
|
212
|
+
then walks up the parent chain; `has()` only checks the current injector.
|
|
213
|
+
|
|
214
|
+
```ts
|
|
215
|
+
const root = new SyncInjector();
|
|
216
|
+
root.set("TOKEN", "value");
|
|
217
|
+
|
|
218
|
+
const child = new SyncInjector(root);
|
|
219
|
+
root.addChild(child);
|
|
220
|
+
child.get("TOKEN"); // "value", resolved through the parent
|
|
221
|
+
child.has("TOKEN"); // false, has() is local only
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
Rules worth knowing:
|
|
225
|
+
|
|
226
|
+
- **`new Injector(parent)` does not register the child.** The parent pointer is set, but the child is not in
|
|
227
|
+
`parent.getChildren()`. Call `parent.addChild(child)` for that, otherwise `child.destroy()` cannot detach it.
|
|
228
|
+
- **`addChild` is idempotent** and refuses to move a child that already has a different parent.
|
|
229
|
+
- **`removeChild(child)`** detaches a registered child, preserving the order of the remaining children, and returns
|
|
230
|
+
`false` when the child was never registered. `detach()` delegates to it, so detaching an unregistered child no
|
|
231
|
+
longer removes an unrelated sibling.
|
|
232
|
+
- **`destroy()`** detaches, clears providers and clears children. Anything still resolving after that throws.
|
|
233
|
+
|
|
234
|
+
## Metadata caching
|
|
235
|
+
|
|
236
|
+
`getProviders` resolves constructor parameters once per class and caches the result:
|
|
237
|
+
|
|
238
|
+
- The cache is a **`WeakMap` keyed by `useClass`**, so dynamically created classes stay collectable. It used to be a
|
|
239
|
+
strong `Map` keyed by `provide`, which retained every class ever seen.
|
|
240
|
+
- Entries are **validated against `getMetadataVersion()`** from `@typeix/metadata`, so a decorator applied after the
|
|
241
|
+
first resolution is honoured instead of served stale.
|
|
242
|
+
- **`design:paramtypes` is never mutated.** The array belongs to reflect-metadata and is shared with every other
|
|
243
|
+
reader of the class (`resty-ws`, `@typeix/resty-swagger`, `@typeix/resty-graphql`). Resolution copies it before
|
|
244
|
+
substituting inject tokens, so the declared types stay readable.
|
|
245
|
+
- **A provider without a class** (`{provide: "TOKEN"}`, `{provide: T, useValue: …}`) resolves to an empty metadata
|
|
246
|
+
list rather than throwing.
|
|
247
|
+
|
|
248
|
+
## Performance notes
|
|
249
|
+
|
|
250
|
+
Measured on Node 26:
|
|
251
|
+
|
|
252
|
+
| | cost |
|
|
253
|
+
|---|---|
|
|
254
|
+
| instantiating one class (`new SyncInjector` + `createAndResolve` + `destroy`) | ~2 µs |
|
|
255
|
+
| `verifyProvider` | 0.0027 µs |
|
|
256
|
+
| `getProviders`, cache hit | one `WeakMap` lookup |
|
|
257
|
+
| adding 1 000 children | ~0.3 ms total |
|
|
258
|
+
|
|
259
|
+
Injection is roughly 4% of an HTTP request in a resty application, so this is not a place to look for speedups.
|
|
260
|
+
|
|
261
|
+
## Limitations
|
|
262
|
+
|
|
263
|
+
- **`verifyProviders` on a bare token** produces `{provide: undefined, useClass: undefined}`; `createInstance` only
|
|
264
|
+
survives that through its `isDefined(item.provide)` fallback. Prefer passing a full `IProvider` when the token is
|
|
265
|
+
not a class.
|
|
266
|
+
- **The parameter cache is per class**, not per provider token, so two providers aliasing the same class share the
|
|
267
|
+
cached parameter list.
|
|
268
|
+
|
|
269
|
+
[actions-url]: https://github.com/typeix/typeix/actions/workflows/ci.yml
|
|
270
|
+
[actions-img]: https://github.com/typeix/typeix/actions/workflows/ci.yml/badge.svg
|
|
271
|
+
[npm-version-img]: https://img.shields.io/npm/v/@typeix/di
|
|
211
272
|
[coverage-img]: https://coveralls.io/repos/github/typeix/typeix/badge.svg?branch=main
|
|
212
|
-
[coverage-url]: https://coveralls.io/github/typeix/typeix?branch=
|
|
273
|
+
[coverage-url]: https://coveralls.io/github/typeix/typeix?branch=main
|
|
@@ -34,6 +34,38 @@ describe("provider", () => {
|
|
|
34
34
|
{ provide: B, useClass: C }
|
|
35
35
|
]);
|
|
36
36
|
});
|
|
37
|
+
test("shift preserves order and source precedence", async () => {
|
|
38
|
+
const source = [{ provide: "b", useValue: 1 }, { provide: "a", useValue: 2 }];
|
|
39
|
+
const target = [{ provide: "a", useValue: 3 }, { provide: "c", useValue: 4 }, { provide: "d", useValue: 5 }];
|
|
40
|
+
expect((0, provider_1.shiftLeft)(source, target).map(item => item.provide)).toEqual(["b", "a", "c", "d"]);
|
|
41
|
+
expect((0, provider_1.shiftLeft)(source, target)[1]).toBe(source[1]);
|
|
42
|
+
expect((0, provider_1.shiftRight)(source, target).map(item => item.provide)).toEqual(["c", "d", "b", "a"]);
|
|
43
|
+
});
|
|
44
|
+
test("shift keeps duplicates inside target", async () => {
|
|
45
|
+
const source = [{ provide: "a", useValue: 1 }];
|
|
46
|
+
const target = [{ provide: "b", useValue: 2 }, { provide: "b", useValue: 3 }];
|
|
47
|
+
expect((0, provider_1.shiftLeft)(source, target).map(item => item.provide)).toEqual(["a", "b", "b"]);
|
|
48
|
+
expect((0, provider_1.shiftRight)(source, target).map(item => item.provide)).toEqual(["b", "b", "a"]);
|
|
49
|
+
});
|
|
50
|
+
test("shift handles empty lists", async () => {
|
|
51
|
+
expect((0, provider_1.shiftLeft)([], [])).toEqual([]);
|
|
52
|
+
expect((0, provider_1.shiftRight)([], [])).toEqual([]);
|
|
53
|
+
expect((0, provider_1.shiftLeft)([{ provide: "a", useValue: 1 }], []).map(item => item.provide)).toEqual(["a"]);
|
|
54
|
+
expect((0, provider_1.shiftRight)([], [{ provide: "b", useValue: 2 }]).map(item => item.provide)).toEqual(["b"]);
|
|
55
|
+
});
|
|
56
|
+
test("shift does not scan the source per element", async () => {
|
|
57
|
+
const source = Array.from({ length: 200 }, (_, index) => ({ provide: `s${index}`, useValue: index }));
|
|
58
|
+
const target = Array.from({ length: 200 }, (_, index) => ({ provide: `t${index}`, useValue: index }));
|
|
59
|
+
const find = jest.spyOn(Array.prototype, "find");
|
|
60
|
+
try {
|
|
61
|
+
expect((0, provider_1.shiftLeft)(source, target).length).toBe(400);
|
|
62
|
+
expect((0, provider_1.shiftRight)(source, target).length).toBe(400);
|
|
63
|
+
expect(find).not.toHaveBeenCalled();
|
|
64
|
+
}
|
|
65
|
+
finally {
|
|
66
|
+
find.mockRestore();
|
|
67
|
+
}
|
|
68
|
+
});
|
|
37
69
|
test("getProviderName", async () => {
|
|
38
70
|
let A = class A {
|
|
39
71
|
};
|
package/dist/helpers/provider.js
CHANGED
|
@@ -19,11 +19,16 @@ function getProviderName(Class) {
|
|
|
19
19
|
}
|
|
20
20
|
return (0, utils_1.isString)(Class) ? Class : null;
|
|
21
21
|
}
|
|
22
|
+
function providesOf(source) {
|
|
23
|
+
return new Set(source.map((item) => item.provide));
|
|
24
|
+
}
|
|
22
25
|
function shiftRight(source, target) {
|
|
23
|
-
|
|
26
|
+
const present = providesOf(source);
|
|
27
|
+
return target.filter((bI) => !present.has(bI.provide)).concat(source);
|
|
24
28
|
}
|
|
25
29
|
function shiftLeft(source, target) {
|
|
26
|
-
|
|
30
|
+
const present = providesOf(source);
|
|
31
|
+
return source.concat(target.filter((bI) => !present.has(bI.provide)));
|
|
27
32
|
}
|
|
28
33
|
function verifyProvider(value) {
|
|
29
34
|
if ((0, utils_1.isFunction)(value)) {
|
|
@@ -3,7 +3,9 @@ import { ProviderList } from "../helpers/provider";
|
|
|
3
3
|
interface InjectorImpl {
|
|
4
4
|
getInjectorsByProvider(provider: IProvider): Array<InjectorImpl>;
|
|
5
5
|
getChildren(): Array<InjectorImpl>;
|
|
6
|
+
getParent(): InjectorImpl;
|
|
6
7
|
setParent(injector: InjectorImpl): void;
|
|
8
|
+
removeChild(injector: InjectorImpl): boolean;
|
|
7
9
|
get(provider: string, Class?: IProvider): any;
|
|
8
10
|
get<P>(provider: Type<P>, Class?: IProvider): P;
|
|
9
11
|
}
|
|
@@ -12,6 +14,7 @@ export declare abstract class AbstractInjector<T extends InjectorImpl> implement
|
|
|
12
14
|
protected _name: string;
|
|
13
15
|
protected _providers: ProviderList;
|
|
14
16
|
protected _children: Array<InjectorImpl>;
|
|
17
|
+
protected _childrenIndex: Set<InjectorImpl>;
|
|
15
18
|
protected constructor(_parent?: T, keys?: Array<any>);
|
|
16
19
|
getChildren(): Array<InjectorImpl>;
|
|
17
20
|
detach(): void;
|
|
@@ -21,6 +24,7 @@ export declare abstract class AbstractInjector<T extends InjectorImpl> implement
|
|
|
21
24
|
set(key: any, value: Object): void;
|
|
22
25
|
getParent(): T;
|
|
23
26
|
hasChild(injector: T): boolean;
|
|
27
|
+
removeChild(injector: InjectorImpl): boolean;
|
|
24
28
|
setParent(injector: T): void;
|
|
25
29
|
hasName(): boolean;
|
|
26
30
|
setName(provider: IProvider): void;
|
|
@@ -7,6 +7,7 @@ class AbstractInjector {
|
|
|
7
7
|
constructor(_parent, keys = []) {
|
|
8
8
|
this._parent = _parent;
|
|
9
9
|
this._children = [];
|
|
10
|
+
this._childrenIndex = new Set();
|
|
10
11
|
this._providers = new provider_1.ProviderList(keys);
|
|
11
12
|
}
|
|
12
13
|
getChildren() {
|
|
@@ -14,8 +15,7 @@ class AbstractInjector {
|
|
|
14
15
|
}
|
|
15
16
|
detach() {
|
|
16
17
|
if ((0, utils_1.isDefined)(this._parent)) {
|
|
17
|
-
|
|
18
|
-
this._parent.getChildren().splice(childIndex, 1);
|
|
18
|
+
this._parent.removeChild(this);
|
|
19
19
|
}
|
|
20
20
|
this._parent = undefined;
|
|
21
21
|
}
|
|
@@ -23,6 +23,7 @@ class AbstractInjector {
|
|
|
23
23
|
this.detach();
|
|
24
24
|
this._providers.clear();
|
|
25
25
|
this._children = [];
|
|
26
|
+
this._childrenIndex.clear();
|
|
26
27
|
}
|
|
27
28
|
has(key) {
|
|
28
29
|
return this._providers.has(key);
|
|
@@ -54,7 +55,16 @@ class AbstractInjector {
|
|
|
54
55
|
return this._parent;
|
|
55
56
|
}
|
|
56
57
|
hasChild(injector) {
|
|
57
|
-
return this.
|
|
58
|
+
return this._childrenIndex.has(injector);
|
|
59
|
+
}
|
|
60
|
+
removeChild(injector) {
|
|
61
|
+
if (!this._childrenIndex.has(injector)) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
this._childrenIndex.delete(injector);
|
|
65
|
+
const index = this._children.indexOf(injector);
|
|
66
|
+
this._children.splice(index, 1);
|
|
67
|
+
return true;
|
|
58
68
|
}
|
|
59
69
|
setParent(injector) {
|
|
60
70
|
if ((0, utils_1.isTruthy)(this._parent) && this._parent !== injector) {
|
|
@@ -77,10 +87,12 @@ class AbstractInjector {
|
|
|
77
87
|
}
|
|
78
88
|
}
|
|
79
89
|
addChild(injector) {
|
|
80
|
-
if (
|
|
81
|
-
|
|
82
|
-
this._children.push(injector);
|
|
90
|
+
if (this._childrenIndex.has(injector)) {
|
|
91
|
+
return this;
|
|
83
92
|
}
|
|
93
|
+
injector.setParent(this);
|
|
94
|
+
this._children.push(injector);
|
|
95
|
+
this._childrenIndex.add(injector);
|
|
84
96
|
return this;
|
|
85
97
|
}
|
|
86
98
|
getInjectorsByProvider(provider) {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const decorators_1 = require("../decorators");
|
|
16
|
+
const metadata_1 = require("@typeix/metadata");
|
|
17
|
+
const sync_injector_1 = require("./sync-injector");
|
|
18
|
+
const provider_1 = require("../helpers/provider");
|
|
19
|
+
class ServiceA {
|
|
20
|
+
}
|
|
21
|
+
class ServiceB {
|
|
22
|
+
}
|
|
23
|
+
const TOKEN_A = "TOKEN_A";
|
|
24
|
+
function declaredTypes(Class) {
|
|
25
|
+
var _a;
|
|
26
|
+
return ((_a = Reflect.getMetadata("design:paramtypes", Class)) !== null && _a !== void 0 ? _a : []).map((item) => typeof item === "function" ? item.name : String(item));
|
|
27
|
+
}
|
|
28
|
+
function resolvedTokens(providers) {
|
|
29
|
+
return providers.map(item => {
|
|
30
|
+
const token = item && typeof item === "object" && "provide" in item ? item.provide : item;
|
|
31
|
+
return typeof token === "function" ? token.name : String(token);
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
describe("SyncInjector.getProviders", () => {
|
|
35
|
+
it("should not mutate the shared design:paramtypes array", () => {
|
|
36
|
+
let Consumer = class Consumer {
|
|
37
|
+
constructor(a, b) {
|
|
38
|
+
this.a = a;
|
|
39
|
+
this.b = b;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
Consumer = __decorate([
|
|
43
|
+
(0, decorators_1.Injectable)(),
|
|
44
|
+
__param(0, (0, decorators_1.Inject)(TOKEN_A)),
|
|
45
|
+
__metadata("design:paramtypes", [Object, ServiceB])
|
|
46
|
+
], Consumer);
|
|
47
|
+
const before = declaredTypes(Consumer);
|
|
48
|
+
const providers = sync_injector_1.SyncInjector.getProviders((0, provider_1.verifyProvider)(Consumer));
|
|
49
|
+
expect(resolvedTokens(providers)).toEqual([TOKEN_A, "ServiceB"]);
|
|
50
|
+
expect(declaredTypes(Consumer)).toEqual(before);
|
|
51
|
+
});
|
|
52
|
+
it("should keep the declared types readable by other consumers", () => {
|
|
53
|
+
var _a;
|
|
54
|
+
let Consumer = class Consumer {
|
|
55
|
+
constructor(a, b) {
|
|
56
|
+
this.a = a;
|
|
57
|
+
this.b = b;
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
Consumer = __decorate([
|
|
61
|
+
(0, decorators_1.Injectable)(),
|
|
62
|
+
__param(0, (0, decorators_1.Inject)(TOKEN_A)),
|
|
63
|
+
__metadata("design:paramtypes", [Object, ServiceB])
|
|
64
|
+
], Consumer);
|
|
65
|
+
sync_injector_1.SyncInjector.getProviders((0, provider_1.verifyProvider)(Consumer));
|
|
66
|
+
sync_injector_1.SyncInjector.getProviders((0, provider_1.verifyProvider)(Consumer));
|
|
67
|
+
const viaMetadata = (_a = require("@typeix/metadata").getAllMetadataForTarget(Consumer)
|
|
68
|
+
.find((item) => item.metadataKey === "design:paramtypes")) === null || _a === void 0 ? void 0 : _a.args;
|
|
69
|
+
expect(viaMetadata.map((item) => typeof item === "function" ? item.name : String(item)))
|
|
70
|
+
.toEqual(["Object", "ServiceB"]);
|
|
71
|
+
});
|
|
72
|
+
it("should resolve the same class twice without corrupting the second read", () => {
|
|
73
|
+
let Plain = class Plain {
|
|
74
|
+
constructor(a) {
|
|
75
|
+
this.a = a;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
Plain = __decorate([
|
|
79
|
+
(0, decorators_1.Injectable)(),
|
|
80
|
+
__metadata("design:paramtypes", [ServiceB])
|
|
81
|
+
], Plain);
|
|
82
|
+
const first = resolvedTokens(sync_injector_1.SyncInjector.getProviders((0, provider_1.verifyProvider)(Plain)));
|
|
83
|
+
const second = resolvedTokens(sync_injector_1.SyncInjector.getProviders((0, provider_1.verifyProvider)(Plain)));
|
|
84
|
+
expect(second).toEqual(first);
|
|
85
|
+
expect(declaredTypes(Plain)).toEqual(["ServiceB"]);
|
|
86
|
+
});
|
|
87
|
+
it("should return an empty list for a token provider", () => {
|
|
88
|
+
expect(sync_injector_1.SyncInjector.getProviders({ provide: TOKEN_A })).toEqual([]);
|
|
89
|
+
});
|
|
90
|
+
it("should return empty metadata for a token provider", () => {
|
|
91
|
+
expect(sync_injector_1.SyncInjector.getAllMetadataForTarget({ provide: TOKEN_A })).toEqual([]);
|
|
92
|
+
expect(sync_injector_1.SyncInjector.getAllMetadataForTarget({ provide: TOKEN_A, useValue: 1 })).toEqual([]);
|
|
93
|
+
});
|
|
94
|
+
it("should cache the resolved providers per class", () => {
|
|
95
|
+
let Cached = class Cached {
|
|
96
|
+
constructor(a) {
|
|
97
|
+
this.a = a;
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
Cached = __decorate([
|
|
101
|
+
(0, decorators_1.Injectable)(),
|
|
102
|
+
__metadata("design:paramtypes", [ServiceB])
|
|
103
|
+
], Cached);
|
|
104
|
+
const first = sync_injector_1.SyncInjector.getProviders((0, provider_1.verifyProvider)(Cached));
|
|
105
|
+
const second = sync_injector_1.SyncInjector.getProviders((0, provider_1.verifyProvider)(Cached));
|
|
106
|
+
expect(second).toBe(first);
|
|
107
|
+
});
|
|
108
|
+
it("should see a decorator applied after the first resolution", () => {
|
|
109
|
+
let Late = class Late {
|
|
110
|
+
constructor(a) {
|
|
111
|
+
this.a = a;
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
Late = __decorate([
|
|
115
|
+
(0, decorators_1.Injectable)(),
|
|
116
|
+
__metadata("design:paramtypes", [Object])
|
|
117
|
+
], Late);
|
|
118
|
+
expect(declaredTypes(Late)).toEqual(["Object"]);
|
|
119
|
+
const before = sync_injector_1.SyncInjector.getProviders((0, provider_1.verifyProvider)(Late));
|
|
120
|
+
expect(resolvedTokens(before)).toEqual(["Object"]);
|
|
121
|
+
(0, decorators_1.Inject)(ServiceA)(Late, undefined, 0);
|
|
122
|
+
const after = sync_injector_1.SyncInjector.getProviders((0, provider_1.verifyProvider)(Late));
|
|
123
|
+
expect(after).not.toBe(before);
|
|
124
|
+
expect(resolvedTokens(after)).toEqual(["ServiceA"]);
|
|
125
|
+
});
|
|
126
|
+
it("should drop cached providers when the revision moves", () => {
|
|
127
|
+
let Revisioned = class Revisioned {
|
|
128
|
+
constructor(a) {
|
|
129
|
+
this.a = a;
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
Revisioned = __decorate([
|
|
133
|
+
(0, decorators_1.Injectable)(),
|
|
134
|
+
__metadata("design:paramtypes", [ServiceB])
|
|
135
|
+
], Revisioned);
|
|
136
|
+
const first = sync_injector_1.SyncInjector.getProviders((0, provider_1.verifyProvider)(Revisioned));
|
|
137
|
+
expect((0, metadata_1.getMetadataVersion)()).toBeGreaterThan(0);
|
|
138
|
+
(0, metadata_1.clearMetadataCache)();
|
|
139
|
+
const second = sync_injector_1.SyncInjector.getProviders((0, provider_1.verifyProvider)(Revisioned));
|
|
140
|
+
expect(second).not.toBe(first);
|
|
141
|
+
expect(resolvedTokens(second)).toEqual(resolvedTokens(first));
|
|
142
|
+
});
|
|
143
|
+
it("should not retain dynamic classes", () => {
|
|
144
|
+
const before = process.memoryUsage().heapUsed;
|
|
145
|
+
for (let index = 0; index < 5000; index++) {
|
|
146
|
+
class Dynamic {
|
|
147
|
+
constructor(a) {
|
|
148
|
+
this.a = a;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
sync_injector_1.SyncInjector.getProviders((0, provider_1.verifyProvider)(Dynamic));
|
|
152
|
+
}
|
|
153
|
+
const growth = (process.memoryUsage().heapUsed - before) / 1024 / 1024;
|
|
154
|
+
expect(growth).toBeLessThan(48);
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
describe("AbstractInjector child index", () => {
|
|
158
|
+
it("should register a child whose parent was set by the constructor", () => {
|
|
159
|
+
const parent = new sync_injector_1.SyncInjector();
|
|
160
|
+
const child = new sync_injector_1.SyncInjector(parent);
|
|
161
|
+
expect(parent.hasChild(child)).toBe(false);
|
|
162
|
+
parent.addChild(child);
|
|
163
|
+
expect(parent.hasChild(child)).toBe(true);
|
|
164
|
+
expect(parent.getChildren()).toEqual([child]);
|
|
165
|
+
});
|
|
166
|
+
it("should be idempotent", () => {
|
|
167
|
+
const parent = new sync_injector_1.SyncInjector();
|
|
168
|
+
const child = new sync_injector_1.SyncInjector();
|
|
169
|
+
parent.addChild(child);
|
|
170
|
+
parent.addChild(child);
|
|
171
|
+
expect(parent.getChildren().length).toBe(1);
|
|
172
|
+
});
|
|
173
|
+
it("should refuse a second parent", () => {
|
|
174
|
+
const first = new sync_injector_1.SyncInjector();
|
|
175
|
+
const second = new sync_injector_1.SyncInjector();
|
|
176
|
+
const child = new sync_injector_1.SyncInjector();
|
|
177
|
+
first.addChild(child);
|
|
178
|
+
expect(() => second.addChild(child)).toThrow(/Cannot redefine parent/);
|
|
179
|
+
});
|
|
180
|
+
it("should detach only a registered child and keep the rest intact", () => {
|
|
181
|
+
const parent = new sync_injector_1.SyncInjector();
|
|
182
|
+
const one = new sync_injector_1.SyncInjector();
|
|
183
|
+
const two = new sync_injector_1.SyncInjector();
|
|
184
|
+
const unregistered = new sync_injector_1.SyncInjector(parent);
|
|
185
|
+
parent.addChild(one);
|
|
186
|
+
parent.addChild(two);
|
|
187
|
+
unregistered.detach();
|
|
188
|
+
expect(parent.getChildren()).toEqual([one, two]);
|
|
189
|
+
expect(unregistered.getParent()).toBe(undefined);
|
|
190
|
+
two.detach();
|
|
191
|
+
expect(parent.getChildren()).toEqual([one]);
|
|
192
|
+
expect(parent.hasChild(two)).toBe(false);
|
|
193
|
+
expect(one.getParent()).toBe(parent);
|
|
194
|
+
});
|
|
195
|
+
it("should keep the index consistent with destroy", () => {
|
|
196
|
+
const parent = new sync_injector_1.SyncInjector();
|
|
197
|
+
const child = new sync_injector_1.SyncInjector();
|
|
198
|
+
parent.addChild(child);
|
|
199
|
+
parent.destroy();
|
|
200
|
+
expect(parent.getChildren()).toEqual([]);
|
|
201
|
+
expect(parent.hasChild(child)).toBe(false);
|
|
202
|
+
});
|
|
203
|
+
it("should add children without scanning the children array", () => {
|
|
204
|
+
const parent = new sync_injector_1.SyncInjector();
|
|
205
|
+
const children = Array.from({ length: 200 }, () => new sync_injector_1.SyncInjector(parent));
|
|
206
|
+
for (const child of children) {
|
|
207
|
+
parent.addChild(child);
|
|
208
|
+
}
|
|
209
|
+
const indexOf = jest.spyOn(Array.prototype, "indexOf");
|
|
210
|
+
try {
|
|
211
|
+
const extra = new sync_injector_1.SyncInjector();
|
|
212
|
+
parent.addChild(extra);
|
|
213
|
+
expect(parent.hasChild(extra)).toBe(true);
|
|
214
|
+
expect(parent.hasChild(children[0])).toBe(true);
|
|
215
|
+
expect(indexOf).not.toHaveBeenCalled();
|
|
216
|
+
}
|
|
217
|
+
finally {
|
|
218
|
+
indexOf.mockRestore();
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
it("should keep children in registration order", () => {
|
|
222
|
+
const parent = new sync_injector_1.SyncInjector();
|
|
223
|
+
const children = Array.from({ length: 50 }, () => new sync_injector_1.SyncInjector());
|
|
224
|
+
for (const child of children) {
|
|
225
|
+
parent.addChild(child);
|
|
226
|
+
}
|
|
227
|
+
expect(parent.getChildren()).toEqual(children);
|
|
228
|
+
});
|
|
229
|
+
it("should remove a middle child deterministically", () => {
|
|
230
|
+
const parent = new sync_injector_1.SyncInjector();
|
|
231
|
+
const children = Array.from({ length: 5 }, () => new sync_injector_1.SyncInjector());
|
|
232
|
+
for (const child of children) {
|
|
233
|
+
parent.addChild(child);
|
|
234
|
+
}
|
|
235
|
+
children[2].destroy();
|
|
236
|
+
expect(parent.getChildren()).toEqual([children[0], children[1], children[3], children[4]]);
|
|
237
|
+
expect(parent.hasChild(children[2])).toBe(false);
|
|
238
|
+
});
|
|
239
|
+
it("should handle a large children list without error", () => {
|
|
240
|
+
const parent = new sync_injector_1.SyncInjector();
|
|
241
|
+
const children = [];
|
|
242
|
+
for (let index = 0; index < 5000; index++) {
|
|
243
|
+
const child = new sync_injector_1.SyncInjector(parent);
|
|
244
|
+
parent.addChild(child);
|
|
245
|
+
children.push(child);
|
|
246
|
+
}
|
|
247
|
+
expect(parent.getChildren().length).toBe(5000);
|
|
248
|
+
for (const child of children) {
|
|
249
|
+
child.destroy();
|
|
250
|
+
}
|
|
251
|
+
expect(parent.getChildren().length).toBe(0);
|
|
252
|
+
});
|
|
253
|
+
});
|
|
254
|
+
//# sourceMappingURL=injector-hardening-spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const metadata_1 = require("@typeix/metadata");
|
|
4
|
+
const sync_injector_1 = require("./sync-injector");
|
|
5
|
+
const provider_1 = require("../helpers/provider");
|
|
6
|
+
const LATE_KEY = "@typeix:method:LateDecorator:0000";
|
|
7
|
+
class Target {
|
|
8
|
+
doWork() {
|
|
9
|
+
return undefined;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
function countLate() {
|
|
13
|
+
return sync_injector_1.SyncInjector.getAllMetadataForTarget((0, provider_1.verifyProvider)(Target))
|
|
14
|
+
.filter(item => item.metadataKey === LATE_KEY).length;
|
|
15
|
+
}
|
|
16
|
+
describe("SyncInjector metadata cache", () => {
|
|
17
|
+
it("should cache repeated reads of the same class", () => {
|
|
18
|
+
const first = sync_injector_1.SyncInjector.getAllMetadataForTarget((0, provider_1.verifyProvider)(Target));
|
|
19
|
+
const second = sync_injector_1.SyncInjector.getAllMetadataForTarget((0, provider_1.verifyProvider)(Target));
|
|
20
|
+
expect(second).toBe(first);
|
|
21
|
+
});
|
|
22
|
+
it("should see a decorator applied after the first scan", () => {
|
|
23
|
+
expect(countLate()).toBe(0);
|
|
24
|
+
(0, metadata_1.defineMetadata)(LATE_KEY, {
|
|
25
|
+
args: {},
|
|
26
|
+
metadataKey: LATE_KEY,
|
|
27
|
+
propertyKey: "doWork",
|
|
28
|
+
type: "method"
|
|
29
|
+
}, Target.prototype, "doWork");
|
|
30
|
+
expect(countLate()).toBe(1);
|
|
31
|
+
});
|
|
32
|
+
it("should drop cached records when the metadata revision moves", () => {
|
|
33
|
+
expect(countLate()).toBe(1);
|
|
34
|
+
(0, metadata_1.clearMetadataCache)();
|
|
35
|
+
expect(countLate()).toBe(1);
|
|
36
|
+
});
|
|
37
|
+
it("should not leak dynamic classes", () => {
|
|
38
|
+
const before = process.memoryUsage().heapUsed;
|
|
39
|
+
for (let index = 0; index < 5000; index++) {
|
|
40
|
+
class Dynamic {
|
|
41
|
+
}
|
|
42
|
+
sync_injector_1.SyncInjector.getAllMetadataForTarget((0, provider_1.verifyProvider)(Dynamic));
|
|
43
|
+
}
|
|
44
|
+
const growth = (process.memoryUsage().heapUsed - before) / 1024 / 1024;
|
|
45
|
+
expect(growth).toBeLessThan(48);
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
//# sourceMappingURL=metadata-cache-spec.js.map
|
|
@@ -2,8 +2,14 @@ import { IProvider, MixedProvider } from "../interfaces";
|
|
|
2
2
|
import { IMetadata } from "@typeix/metadata";
|
|
3
3
|
import { AbstractInjector } from "./abstract-injector";
|
|
4
4
|
export declare class SyncInjector extends AbstractInjector<SyncInjector> {
|
|
5
|
-
static readonly __actionParams__:
|
|
6
|
-
|
|
5
|
+
static readonly __actionParams__: WeakMap<Function, {
|
|
6
|
+
epoch: number;
|
|
7
|
+
methods: Map<string, Array<IProvider>>;
|
|
8
|
+
}>;
|
|
9
|
+
static readonly __metadataCache__: WeakMap<Function, {
|
|
10
|
+
epoch: number;
|
|
11
|
+
records: Array<IMetadata>;
|
|
12
|
+
}>;
|
|
7
13
|
static readonly __interceptors__: Set<Function>;
|
|
8
14
|
constructor(_parent?: SyncInjector, keys?: Array<any>);
|
|
9
15
|
static designParamComparator(item: any, propertyKey?: string): boolean;
|
|
@@ -19,30 +19,45 @@ class SyncInjector extends abstract_injector_1.AbstractInjector {
|
|
|
19
19
|
return item.type === "parameter" && item.propertyKey === propertyKey;
|
|
20
20
|
}
|
|
21
21
|
static getProviders(provider, propertyKey = "constructor") {
|
|
22
|
-
var _a
|
|
22
|
+
var _a;
|
|
23
23
|
if (!(0, helpers_1.isClassProvider)(provider)) {
|
|
24
24
|
return [];
|
|
25
25
|
}
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
SyncInjector.__actionParams__.set(provide, map);
|
|
35
|
-
return providers;
|
|
26
|
+
const Class = provider.useClass;
|
|
27
|
+
if (!(0, utils_1.isFunction)(Class)) {
|
|
28
|
+
return [];
|
|
29
|
+
}
|
|
30
|
+
const epoch = (0, metadata_1.getMetadataVersion)();
|
|
31
|
+
const cached = SyncInjector.__actionParams__.get(Class);
|
|
32
|
+
if ((0, utils_1.isDefined)(cached) && cached.epoch === epoch && cached.methods.has(propertyKey)) {
|
|
33
|
+
return cached.methods.get(propertyKey);
|
|
36
34
|
}
|
|
37
|
-
|
|
35
|
+
const metadata = SyncInjector.getAllMetadataForTarget(provider);
|
|
36
|
+
const declared = (_a = metadata.find(item => SyncInjector.designParamComparator(item, propertyKey))) === null || _a === void 0 ? void 0 : _a.args;
|
|
37
|
+
const paramTypes = (0, utils_1.isArray)(declared) ? declared.slice() : [];
|
|
38
|
+
metadata
|
|
39
|
+
.filter(item => SyncInjector.parameterComparator(item, propertyKey))
|
|
40
|
+
.forEach(item => paramTypes.splice(item.paramIndex, 1, (0, utils_1.isDefined)(item.args.token) ? item.args.token : item.designType));
|
|
41
|
+
const providers = (0, helpers_1.verifyProviders)(paramTypes);
|
|
42
|
+
SyncInjector.__actionParams__.set(Class, {
|
|
43
|
+
epoch,
|
|
44
|
+
methods: ((0, utils_1.isDefined)(cached) && cached.epoch === epoch ? cached.methods : new Map()).set(propertyKey, providers)
|
|
45
|
+
});
|
|
46
|
+
return providers;
|
|
38
47
|
}
|
|
39
48
|
static getAllMetadataForTarget(provider) {
|
|
40
|
-
|
|
41
|
-
|
|
49
|
+
const Class = provider.useClass;
|
|
50
|
+
if (!(0, utils_1.isFunction)(Class)) {
|
|
51
|
+
return [];
|
|
42
52
|
}
|
|
43
|
-
|
|
44
|
-
SyncInjector.__metadataCache__.
|
|
45
|
-
|
|
53
|
+
const epoch = (0, metadata_1.getMetadataVersion)();
|
|
54
|
+
const cached = SyncInjector.__metadataCache__.get(Class);
|
|
55
|
+
if ((0, utils_1.isDefined)(cached) && cached.epoch === epoch) {
|
|
56
|
+
return cached.records;
|
|
57
|
+
}
|
|
58
|
+
const records = (0, metadata_1.getAllMetadataForTarget)(Class);
|
|
59
|
+
SyncInjector.__metadataCache__.set(Class, { epoch, records });
|
|
60
|
+
return records;
|
|
46
61
|
}
|
|
47
62
|
static createAndResolveChild(parent, Class, providers) {
|
|
48
63
|
let child = new SyncInjector(parent);
|
|
@@ -116,7 +131,7 @@ class SyncInjector extends abstract_injector_1.AbstractInjector {
|
|
|
116
131
|
}
|
|
117
132
|
}
|
|
118
133
|
exports.SyncInjector = SyncInjector;
|
|
119
|
-
SyncInjector.__actionParams__ = new
|
|
120
|
-
SyncInjector.__metadataCache__ = new
|
|
134
|
+
SyncInjector.__actionParams__ = new WeakMap();
|
|
135
|
+
SyncInjector.__metadataCache__ = new WeakMap();
|
|
121
136
|
SyncInjector.__interceptors__ = new Set();
|
|
122
137
|
//# sourceMappingURL=sync-injector.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@typeix/di",
|
|
3
|
-
"version": "9.0.
|
|
3
|
+
"version": "9.1.0-rc.3",
|
|
4
4
|
"description": "Typescript dependency injection",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -29,8 +29,8 @@
|
|
|
29
29
|
},
|
|
30
30
|
"homepage": "https://typeix.com",
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@typeix/metadata": "^9.0.
|
|
33
|
-
"@typeix/utils": "^9.0.
|
|
32
|
+
"@typeix/metadata": "^9.1.0-rc.3",
|
|
33
|
+
"@typeix/utils": "^9.1.0-rc.3"
|
|
34
34
|
},
|
|
35
35
|
"publishConfig": {
|
|
36
36
|
"access": "public"
|
|
@@ -38,5 +38,5 @@
|
|
|
38
38
|
"engines": {
|
|
39
39
|
"node": "^26.0.0"
|
|
40
40
|
},
|
|
41
|
-
"gitHead": "
|
|
41
|
+
"gitHead": "6e55af30f8c771391b0ee5faa6d797c9807c4c88"
|
|
42
42
|
}
|