@nmtjs/core 0.14.5 → 0.15.0-beta.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.
- package/LICENSE.md +1 -1
- package/README.md +1 -1
- package/dist/constants.js +1 -4
- package/dist/container.js +110 -91
- package/dist/enums.js +0 -13
- package/dist/hooks.js +9 -41
- package/dist/index.js +0 -1
- package/dist/injectables.js +17 -97
- package/dist/logger.js +9 -5
- package/dist/plugin.js +1 -1
- package/dist/utils/functions.js +10 -28
- package/package.json +14 -16
- package/dist/constants.d.ts +0 -24
- package/dist/container.d.ts +0 -46
- package/dist/enums.d.ts +0 -18
- package/dist/hooks.d.ts +0 -19
- package/dist/index.d.ts +0 -11
- package/dist/injectables.d.ts +0 -119
- package/dist/logger.d.ts +0 -10
- package/dist/metadata.d.ts +0 -13
- package/dist/plugin.d.ts +0 -12
- package/dist/registry.d.ts +0 -21
- package/dist/registry.js +0 -24
- package/dist/types.d.ts +0 -11
- package/dist/utils/functions.d.ts +0 -6
- package/dist/utils/index.d.ts +0 -3
- package/dist/utils/pool.d.ts +0 -18
- package/dist/utils/semaphore.d.ts +0 -13
package/LICENSE.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
Copyright (c)
|
|
1
|
+
Copyright (c) 2025 Denys Ilchyshyn
|
|
2
2
|
|
|
3
3
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
4
|
|
package/README.md
CHANGED
package/dist/constants.js
CHANGED
|
@@ -3,10 +3,7 @@ export const kInjectable = Symbol.for('neemata:InjectableKey');
|
|
|
3
3
|
export const kLazyInjectable = Symbol.for('neemata:LazyInjectableKey');
|
|
4
4
|
export const kValueInjectable = Symbol.for('neemata:ValueInjectableKey');
|
|
5
5
|
export const kFactoryInjectable = Symbol.for('neemata:FactoryInjectableKey');
|
|
6
|
-
export const kClassInjectable = Symbol.for('neemata:ClassInjectableKey');
|
|
7
|
-
export const kClassInjectableCreate = Symbol.for('neemata:ClassInjectableCreateKey');
|
|
8
|
-
export const kClassInjectableDispose = Symbol.for('neemata:ClassInjectableDisposeKey');
|
|
9
6
|
export const kProvider = Symbol.for('neemata:ProviderKey');
|
|
10
|
-
export const kHookCollection = Symbol.for('neemata:HookCollectionKey');
|
|
11
7
|
export const kPlugin = Symbol.for('neemata:PluginKey');
|
|
12
8
|
export const kMetadata = Symbol.for('neemata:MetadataKey');
|
|
9
|
+
export const kHook = Symbol.for('neemata:HookKey');
|
package/dist/container.js
CHANGED
|
@@ -1,20 +1,18 @@
|
|
|
1
1
|
import assert from 'node:assert';
|
|
2
2
|
import { tryCaptureStackTrace } from '@nmtjs/common';
|
|
3
|
-
import { kClassInjectableCreate, kClassInjectableDispose } from "./constants.js";
|
|
4
3
|
import { Scope } from "./enums.js";
|
|
5
|
-
import { CoreInjectables, compareScope,
|
|
4
|
+
import { CoreInjectables, compareScope, createValueInjectable, getDepedencencyInjectable, isFactoryInjectable, isInjectable, isLazyInjectable, isOptionalInjectable, isValueInjectable, provide, } from "./injectables.js";
|
|
6
5
|
export class Container {
|
|
7
|
-
|
|
6
|
+
runtime;
|
|
8
7
|
scope;
|
|
9
8
|
parent;
|
|
10
9
|
instances = new Map();
|
|
11
10
|
resolvers = new Map();
|
|
12
11
|
injectables = new Set();
|
|
13
12
|
dependants = new Map();
|
|
14
|
-
// private readonly transients = new Map<any, any>()
|
|
15
13
|
disposing = false;
|
|
16
|
-
constructor(
|
|
17
|
-
this.
|
|
14
|
+
constructor(runtime, scope = Scope.Global, parent) {
|
|
15
|
+
this.runtime = runtime;
|
|
18
16
|
this.scope = scope;
|
|
19
17
|
this.parent = parent;
|
|
20
18
|
if (scope === Scope.Transient) {
|
|
@@ -22,38 +20,56 @@ export class Container {
|
|
|
22
20
|
}
|
|
23
21
|
this.provide(CoreInjectables.inject, this.createInjectFunction());
|
|
24
22
|
this.provide(CoreInjectables.dispose, this.createDisposeFunction());
|
|
25
|
-
this.provide(CoreInjectables.registry, application.registry);
|
|
26
23
|
}
|
|
27
|
-
async
|
|
24
|
+
async initialize(injectables) {
|
|
25
|
+
const measurements = [];
|
|
28
26
|
const traverse = (dependencies) => {
|
|
29
27
|
for (const key in dependencies) {
|
|
30
28
|
const dependency = dependencies[key];
|
|
31
29
|
const injectable = getDepedencencyInjectable(dependency);
|
|
32
|
-
this.
|
|
30
|
+
if (injectable.scope === this.scope) {
|
|
31
|
+
this.injectables.add(injectable);
|
|
32
|
+
}
|
|
33
33
|
traverse(injectable.dependencies);
|
|
34
34
|
}
|
|
35
35
|
};
|
|
36
|
-
for (const dependant of
|
|
36
|
+
for (const dependant of injectables) {
|
|
37
37
|
traverse(dependant.dependencies);
|
|
38
38
|
}
|
|
39
|
-
|
|
40
|
-
|
|
39
|
+
await Promise.all([...this.injectables].map((injectable) => this.resolve(injectable, measurements)));
|
|
40
|
+
return measurements;
|
|
41
41
|
}
|
|
42
42
|
fork(scope) {
|
|
43
|
-
return new Container(this.
|
|
43
|
+
return new Container(this.runtime, scope, this);
|
|
44
|
+
}
|
|
45
|
+
find(scope) {
|
|
46
|
+
if (this.scope === scope) {
|
|
47
|
+
return this;
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
return this.parent?.find(scope);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
async [Symbol.asyncDispose]() {
|
|
54
|
+
await this.dispose();
|
|
44
55
|
}
|
|
45
56
|
async dispose() {
|
|
46
|
-
this.
|
|
57
|
+
this.runtime.logger.trace('Disposing [%s] scope context...', this.scope);
|
|
47
58
|
// Prevent new resolutions during disposal
|
|
48
59
|
this.disposing = true;
|
|
49
60
|
// Get proper disposal order using topological sort
|
|
50
61
|
const disposalOrder = this.getDisposalOrder();
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
62
|
+
try {
|
|
63
|
+
// Dispose in the correct order
|
|
64
|
+
for (const injectable of disposalOrder) {
|
|
65
|
+
if (this.instances.has(injectable)) {
|
|
66
|
+
await this.disposeInjectableInstances(injectable);
|
|
67
|
+
}
|
|
55
68
|
}
|
|
56
69
|
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
this.runtime.logger.fatal({ error }, 'Potential memory leak: error during container disposal');
|
|
72
|
+
}
|
|
57
73
|
this.instances.clear();
|
|
58
74
|
this.injectables.clear();
|
|
59
75
|
this.resolvers.clear();
|
|
@@ -79,44 +95,67 @@ export class Container {
|
|
|
79
95
|
}
|
|
80
96
|
throw new Error('No instance found');
|
|
81
97
|
}
|
|
82
|
-
resolve(injectable) {
|
|
83
|
-
return this.resolveInjectable(injectable);
|
|
98
|
+
resolve(injectable, measurements) {
|
|
99
|
+
return this.resolveInjectable(injectable, undefined, undefined, measurements);
|
|
84
100
|
}
|
|
85
101
|
async createContext(dependencies) {
|
|
86
102
|
return this.createInjectableContext(dependencies);
|
|
87
103
|
}
|
|
88
|
-
async
|
|
104
|
+
async provide(injectable, ...[value]) {
|
|
105
|
+
const injections = Array.isArray(injectable)
|
|
106
|
+
? injectable
|
|
107
|
+
: [provide(injectable, value)];
|
|
108
|
+
await Promise.all(injections.map(async ({ token, value }) => {
|
|
109
|
+
if (compareScope(token.scope, '>', this.scope)) {
|
|
110
|
+
// TODO: more informative error
|
|
111
|
+
throw new Error('Invalid scope');
|
|
112
|
+
}
|
|
113
|
+
const _value = isInjectable(value) ? await this.resolve(value) : value;
|
|
114
|
+
this.instances.set(token, [
|
|
115
|
+
{ private: _value, public: _value, context: undefined },
|
|
116
|
+
]);
|
|
117
|
+
}));
|
|
118
|
+
}
|
|
119
|
+
satisfies(injectable) {
|
|
120
|
+
return compareScope(injectable.scope, '<=', this.scope);
|
|
121
|
+
}
|
|
122
|
+
async disposeInjectableInstances(injectable) {
|
|
123
|
+
try {
|
|
124
|
+
if (this.instances.has(injectable)) {
|
|
125
|
+
const wrappers = this.instances.get(injectable);
|
|
126
|
+
await Promise.all(wrappers.map((wrapper) => this.disposeInjectableInstance(injectable, wrapper.private, wrapper.context)));
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
catch (cause) {
|
|
130
|
+
const error = new Error('Injectable disposal error. Potential memory leak', { cause });
|
|
131
|
+
this.runtime.logger.error(error);
|
|
132
|
+
}
|
|
133
|
+
finally {
|
|
134
|
+
this.instances.delete(injectable);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
async disposeInjectableInstance(injectable, instance, context) {
|
|
138
|
+
if (isFactoryInjectable(injectable)) {
|
|
139
|
+
const { dispose } = injectable;
|
|
140
|
+
if (dispose)
|
|
141
|
+
await dispose(instance, context);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
async createInjectableContext(dependencies, dependant, measurements) {
|
|
89
145
|
const injections = {};
|
|
90
146
|
const deps = Object.entries(dependencies);
|
|
91
147
|
const resolvers = Array(deps.length);
|
|
92
148
|
for (let i = 0; i < deps.length; i++) {
|
|
93
149
|
const [key, dependency] = deps[i];
|
|
150
|
+
const isOptional = isOptionalInjectable(dependency);
|
|
94
151
|
const injectable = getDepedencencyInjectable(dependency);
|
|
95
|
-
const resolver = this.resolveInjectable(injectable, dependant);
|
|
152
|
+
const resolver = this.resolveInjectable(injectable, dependant, isOptional, measurements);
|
|
96
153
|
resolvers[i] = resolver.then((value) => (injections[key] = value));
|
|
97
154
|
}
|
|
98
155
|
await Promise.all(resolvers);
|
|
99
156
|
return Object.freeze(injections);
|
|
100
157
|
}
|
|
101
|
-
|
|
102
|
-
if (compareScope(injectable.scope, '>', this.scope)) {
|
|
103
|
-
throw new Error('Invalid scope'); // TODO: more informative error
|
|
104
|
-
}
|
|
105
|
-
this.instances.set(injectable, [
|
|
106
|
-
{ private: instance, public: instance, context: undefined },
|
|
107
|
-
]);
|
|
108
|
-
}
|
|
109
|
-
satisfies(injectable) {
|
|
110
|
-
return compareScope(injectable.scope, '<=', this.scope);
|
|
111
|
-
}
|
|
112
|
-
*findCurrentScopeInjectables() {
|
|
113
|
-
for (const injectable of this.injectables) {
|
|
114
|
-
if (injectable.scope === this.scope) {
|
|
115
|
-
yield injectable;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
resolveInjectable(injectable, dependant) {
|
|
158
|
+
resolveInjectable(injectable, dependant, isOptional, measurements) {
|
|
120
159
|
if (this.disposing) {
|
|
121
160
|
return Promise.reject(new Error('Cannot resolve during disposal'));
|
|
122
161
|
}
|
|
@@ -130,7 +169,7 @@ export class Container {
|
|
|
130
169
|
else if (this.parent?.contains(injectable) ||
|
|
131
170
|
(this.parent?.satisfies(injectable) &&
|
|
132
171
|
compareScope(this.parent.scope, '<', this.scope))) {
|
|
133
|
-
return this.parent.resolveInjectable(injectable, dependant);
|
|
172
|
+
return this.parent.resolveInjectable(injectable, dependant, undefined, measurements);
|
|
134
173
|
}
|
|
135
174
|
else {
|
|
136
175
|
const { stack, label } = injectable;
|
|
@@ -151,14 +190,20 @@ export class Container {
|
|
|
151
190
|
else {
|
|
152
191
|
const isLazy = isLazyInjectable(injectable);
|
|
153
192
|
if (isLazy) {
|
|
154
|
-
const isOptional = isOptionalInjectable(injectable);
|
|
155
193
|
if (isOptional)
|
|
156
194
|
return Promise.resolve(undefined);
|
|
157
195
|
return Promise.reject(new Error(`No instance provided for ${label || 'an'} injectable:\n${stack}`));
|
|
158
196
|
}
|
|
159
197
|
else {
|
|
160
|
-
const
|
|
198
|
+
const measure = measurements
|
|
199
|
+
? performance.measure(injectable.label || injectable.stack || '')
|
|
200
|
+
: null;
|
|
201
|
+
const resolution = this.createResolution(injectable, measurements).finally(() => {
|
|
161
202
|
this.resolvers.delete(injectable);
|
|
203
|
+
// biome-ignore lint: false
|
|
204
|
+
// @ts-ignore
|
|
205
|
+
if (measurements && measure)
|
|
206
|
+
measurements.push(measure);
|
|
162
207
|
});
|
|
163
208
|
if (injectable.scope !== Scope.Transient) {
|
|
164
209
|
this.resolvers.set(injectable, resolution);
|
|
@@ -168,9 +213,9 @@ export class Container {
|
|
|
168
213
|
}
|
|
169
214
|
}
|
|
170
215
|
}
|
|
171
|
-
async createResolution(injectable) {
|
|
216
|
+
async createResolution(injectable, measurements) {
|
|
172
217
|
const { dependencies } = injectable;
|
|
173
|
-
const context = await this.createInjectableContext(dependencies, injectable);
|
|
218
|
+
const context = await this.createInjectableContext(dependencies, injectable, measurements);
|
|
174
219
|
const wrapper = {
|
|
175
220
|
private: null,
|
|
176
221
|
public: null,
|
|
@@ -180,12 +225,6 @@ export class Container {
|
|
|
180
225
|
wrapper.private = await Promise.resolve(injectable.factory(wrapper.context));
|
|
181
226
|
wrapper.public = injectable.pick(wrapper.private);
|
|
182
227
|
}
|
|
183
|
-
else if (isClassInjectable(injectable)) {
|
|
184
|
-
const instance = new injectable(context);
|
|
185
|
-
wrapper.private = instance;
|
|
186
|
-
wrapper.public = wrapper.private;
|
|
187
|
-
await instance[kClassInjectableCreate]?.call(instance);
|
|
188
|
-
}
|
|
189
228
|
else {
|
|
190
229
|
throw new Error('Invalid injectable type');
|
|
191
230
|
}
|
|
@@ -198,7 +237,10 @@ export class Container {
|
|
|
198
237
|
return wrapper.public;
|
|
199
238
|
}
|
|
200
239
|
createInjectFunction() {
|
|
201
|
-
const inject = (injectable, context) => {
|
|
240
|
+
const inject = (injectable, context, scope = this.scope) => {
|
|
241
|
+
const container = this.find(scope);
|
|
242
|
+
if (!container)
|
|
243
|
+
throw new Error('No container found for the specified scope');
|
|
202
244
|
const dependencies = { ...injectable.dependencies };
|
|
203
245
|
for (const key in context) {
|
|
204
246
|
const dep = context[key];
|
|
@@ -209,27 +251,29 @@ export class Container {
|
|
|
209
251
|
dependencies[key] = createValueInjectable(dep);
|
|
210
252
|
}
|
|
211
253
|
}
|
|
212
|
-
const newInjectable =
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
};
|
|
220
|
-
return this.resolve(newInjectable);
|
|
254
|
+
const newInjectable = {
|
|
255
|
+
...injectable,
|
|
256
|
+
dependencies,
|
|
257
|
+
scope: Scope.Transient,
|
|
258
|
+
stack: tryCaptureStackTrace(1),
|
|
259
|
+
};
|
|
260
|
+
return container.resolve(newInjectable);
|
|
221
261
|
};
|
|
222
|
-
const explicit = async (injectable, context) => {
|
|
262
|
+
const explicit = async (injectable, context, scope = this.scope) => {
|
|
223
263
|
if ('asyncDispose' in Symbol === false) {
|
|
224
264
|
throw new Error('Symbol.asyncDispose is not supported in this environment');
|
|
225
265
|
}
|
|
266
|
+
const container = this.find(scope);
|
|
267
|
+
if (!container)
|
|
268
|
+
throw new Error('No container found for the specified scope');
|
|
226
269
|
const instance = await inject(injectable, context);
|
|
227
|
-
const dispose =
|
|
228
|
-
return
|
|
270
|
+
const dispose = container.createDisposeFunction();
|
|
271
|
+
return {
|
|
272
|
+
instance,
|
|
229
273
|
[Symbol.asyncDispose]: async () => {
|
|
230
274
|
await dispose(injectable, instance);
|
|
231
275
|
},
|
|
232
|
-
}
|
|
276
|
+
};
|
|
233
277
|
};
|
|
234
278
|
return Object.assign(inject, { explicit });
|
|
235
279
|
}
|
|
@@ -281,29 +325,4 @@ export class Container {
|
|
|
281
325
|
}
|
|
282
326
|
return result;
|
|
283
327
|
}
|
|
284
|
-
async disposeInjectableInstances(injectable) {
|
|
285
|
-
try {
|
|
286
|
-
if (this.instances.has(injectable)) {
|
|
287
|
-
const wrappers = this.instances.get(injectable);
|
|
288
|
-
await Promise.all(wrappers.map((wrapper) => this.disposeInjectableInstance(injectable, wrapper.private, wrapper.context)));
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
catch (cause) {
|
|
292
|
-
const error = new Error('Injectable disposal error. Potential memory leak', { cause });
|
|
293
|
-
this.application.logger.error(error);
|
|
294
|
-
}
|
|
295
|
-
finally {
|
|
296
|
-
this.instances.delete(injectable);
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
async disposeInjectableInstance(injectable, instance, context) {
|
|
300
|
-
if (isFactoryInjectable(injectable)) {
|
|
301
|
-
const { dispose } = injectable;
|
|
302
|
-
if (dispose)
|
|
303
|
-
await dispose(instance, context);
|
|
304
|
-
}
|
|
305
|
-
else if (isClassInjectable(injectable)) {
|
|
306
|
-
await instance[kClassInjectableDispose]?.();
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
328
|
}
|
package/dist/enums.js
CHANGED
|
@@ -5,16 +5,3 @@ export var Scope;
|
|
|
5
5
|
Scope["Call"] = "Call";
|
|
6
6
|
Scope["Transient"] = "Transient";
|
|
7
7
|
})(Scope || (Scope = {}));
|
|
8
|
-
export var Hook;
|
|
9
|
-
(function (Hook) {
|
|
10
|
-
Hook["BeforeInitialize"] = "BeforeInitialize";
|
|
11
|
-
Hook["AfterInitialize"] = "AfterInitialize";
|
|
12
|
-
Hook["BeforeStart"] = "BeforeStart";
|
|
13
|
-
Hook["AfterStart"] = "AfterStart";
|
|
14
|
-
Hook["BeforeStop"] = "BeforeStop";
|
|
15
|
-
Hook["AfterStop"] = "AfterStop";
|
|
16
|
-
Hook["BeforeTerminate"] = "BeforeTerminate";
|
|
17
|
-
Hook["AfterTerminate"] = "AfterTerminate";
|
|
18
|
-
Hook["OnConnect"] = "OnConnect";
|
|
19
|
-
Hook["OnDisconnect"] = "OnDisconnect";
|
|
20
|
-
})(Hook || (Hook = {}));
|
package/dist/hooks.js
CHANGED
|
@@ -1,43 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export class Hooks {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
[kHookCollection] = new Map();
|
|
11
|
-
add(name, callback) {
|
|
12
|
-
let hooks = this[kHookCollection].get(name);
|
|
13
|
-
if (!hooks)
|
|
14
|
-
this[kHookCollection].set(name, (hooks = new Set()));
|
|
15
|
-
hooks.add(callback);
|
|
16
|
-
return () => this.remove(name, callback);
|
|
17
|
-
}
|
|
18
|
-
remove(name, callback) {
|
|
19
|
-
const hooks = this[kHookCollection].get(name);
|
|
20
|
-
if (hooks)
|
|
21
|
-
hooks.delete(callback);
|
|
22
|
-
}
|
|
23
|
-
async call(name, options, ...args) {
|
|
24
|
-
const { concurrent = true, reverse = false } = options ?? {};
|
|
25
|
-
const hooks = this[kHookCollection].get(name);
|
|
26
|
-
if (!hooks)
|
|
27
|
-
return;
|
|
28
|
-
const hooksArr = Array.from(hooks);
|
|
29
|
-
if (concurrent) {
|
|
30
|
-
await Promise.all(hooksArr.map((hook) => hook(...args)));
|
|
31
|
-
}
|
|
32
|
-
else {
|
|
33
|
-
if (reverse)
|
|
34
|
-
hooksArr.reverse();
|
|
35
|
-
for (const hook of hooksArr)
|
|
36
|
-
await hook(...args);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
clear() {
|
|
40
|
-
this[kHookCollection].clear();
|
|
1
|
+
import { Hookable } from 'hookable';
|
|
2
|
+
export class Hooks extends Hookable {
|
|
3
|
+
_;
|
|
4
|
+
createSignal(hook) {
|
|
5
|
+
const controller = new AbortController();
|
|
6
|
+
const unregister = this.hookOnce(String(hook),
|
|
7
|
+
//@ts-expect-error
|
|
8
|
+
() => controller.abort());
|
|
9
|
+
return { controller, signal: controller.signal, unregister };
|
|
41
10
|
}
|
|
42
11
|
}
|
|
43
|
-
export const createErrForHook = (hook) => `Error during [${hook}] hook`;
|
package/dist/index.js
CHANGED
package/dist/injectables.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { tryCaptureStackTrace } from '@nmtjs/common';
|
|
2
|
-
import {
|
|
2
|
+
import { kFactoryInjectable, kInjectable, kLazyInjectable, kOptionalDependency, kValueInjectable, } from "./constants.js";
|
|
3
3
|
import { Scope } from "./enums.js";
|
|
4
|
-
import { Hooks } from "./hooks.js";
|
|
5
4
|
const ScopeStrictness = {
|
|
6
5
|
[Scope.Transient]: Number.NaN, // this should make it always fail to compare with other scopes
|
|
7
6
|
[Scope.Global]: 1,
|
|
@@ -10,7 +9,6 @@ const ScopeStrictness = {
|
|
|
10
9
|
};
|
|
11
10
|
export const isLazyInjectable = (injectable) => injectable[kLazyInjectable];
|
|
12
11
|
export const isFactoryInjectable = (injectable) => injectable[kFactoryInjectable];
|
|
13
|
-
export const isClassInjectable = (injectable) => injectable[kClassInjectable];
|
|
14
12
|
export const isValueInjectable = (injectable) => injectable[kValueInjectable];
|
|
15
13
|
export const isInjectable = (injectable) => injectable[kInjectable];
|
|
16
14
|
export const isOptionalInjectable = (injectable) => injectable[kOptionalDependency];
|
|
@@ -42,14 +40,17 @@ export function createOptionalInjectable(injectable) {
|
|
|
42
40
|
});
|
|
43
41
|
}
|
|
44
42
|
export function createLazyInjectable(scope = Scope.Global, label, stackTraceDepth = 0) {
|
|
45
|
-
|
|
43
|
+
const injectable = Object.freeze({
|
|
46
44
|
scope,
|
|
47
45
|
dependencies: {},
|
|
48
46
|
label,
|
|
49
47
|
stack: tryCaptureStackTrace(stackTraceDepth),
|
|
48
|
+
optional: () => createOptionalInjectable(injectable),
|
|
49
|
+
$withType: () => injectable,
|
|
50
50
|
[kInjectable]: true,
|
|
51
51
|
[kLazyInjectable]: true,
|
|
52
52
|
});
|
|
53
|
+
return injectable;
|
|
53
54
|
}
|
|
54
55
|
export function createValueInjectable(value, label, stackTraceDepth = 0) {
|
|
55
56
|
return Object.freeze({
|
|
@@ -73,75 +74,13 @@ export function createFactoryInjectable(paramsOrFactory, label, stackTraceDepth
|
|
|
73
74
|
pick: params.pick ?? ((instance) => instance),
|
|
74
75
|
label,
|
|
75
76
|
stack: tryCaptureStackTrace(stackTraceDepth),
|
|
77
|
+
optional: () => createOptionalInjectable(injectable),
|
|
76
78
|
[kInjectable]: true,
|
|
77
79
|
[kFactoryInjectable]: true,
|
|
78
80
|
};
|
|
79
81
|
injectable.scope = resolveInjectableScope(typeof params.scope === 'undefined', injectable);
|
|
80
82
|
return Object.freeze(injectable);
|
|
81
83
|
}
|
|
82
|
-
export const createClassInjectable = (dependencies = {}, scope, stackTraceDepth = 0) => {
|
|
83
|
-
const InjectableClass = class {
|
|
84
|
-
$context;
|
|
85
|
-
static dependencies = dependencies;
|
|
86
|
-
static scope = (scope ?? Scope.Global);
|
|
87
|
-
static stack = tryCaptureStackTrace(stackTraceDepth + 2);
|
|
88
|
-
static [kInjectable] = true;
|
|
89
|
-
static [kClassInjectable] = true;
|
|
90
|
-
static get label() {
|
|
91
|
-
// biome-ignore lint/complexity/noThisInStatic: ok
|
|
92
|
-
return this.name;
|
|
93
|
-
}
|
|
94
|
-
constructor($context) {
|
|
95
|
-
this.$context = $context;
|
|
96
|
-
}
|
|
97
|
-
async [kClassInjectableCreate]() { }
|
|
98
|
-
async [kClassInjectableDispose]() { }
|
|
99
|
-
};
|
|
100
|
-
InjectableClass.scope = resolveInjectableScope(typeof scope === 'undefined', InjectableClass);
|
|
101
|
-
return InjectableClass;
|
|
102
|
-
};
|
|
103
|
-
export function createExtendableClassInjectable(baseClass, dependencies = {}, scope, stackTraceDepth = 0) {
|
|
104
|
-
if (isClassInjectable(baseClass)) {
|
|
105
|
-
if (scope && compareScope(baseClass.scope, '>', scope)) {
|
|
106
|
-
throw new Error(`Invalid scope ${scope} for an extendable class injectable: base class have stricter scope - ${baseClass.scope}`);
|
|
107
|
-
}
|
|
108
|
-
else {
|
|
109
|
-
scope = scope ?? baseClass.scope;
|
|
110
|
-
}
|
|
111
|
-
dependencies = Object.assign({}, baseClass.dependencies, dependencies);
|
|
112
|
-
}
|
|
113
|
-
const InjectableClass = class extends baseClass {
|
|
114
|
-
static dependencies = dependencies;
|
|
115
|
-
static scope = (scope ?? Scope.Global);
|
|
116
|
-
static stack = tryCaptureStackTrace(stackTraceDepth);
|
|
117
|
-
static [kInjectable] = true;
|
|
118
|
-
static [kClassInjectable] = true;
|
|
119
|
-
static get label() {
|
|
120
|
-
// biome-ignore lint/complexity/noThisInStatic: ok
|
|
121
|
-
return this.name;
|
|
122
|
-
}
|
|
123
|
-
$context;
|
|
124
|
-
constructor(...args) {
|
|
125
|
-
const [$context, ...baseClassArgs] = args;
|
|
126
|
-
if (isClassInjectable(baseClass)) {
|
|
127
|
-
super($context);
|
|
128
|
-
}
|
|
129
|
-
else {
|
|
130
|
-
super(...baseClassArgs);
|
|
131
|
-
}
|
|
132
|
-
this.$context = $context;
|
|
133
|
-
}
|
|
134
|
-
async $onCreate() {
|
|
135
|
-
await super.$onCreate?.();
|
|
136
|
-
}
|
|
137
|
-
async $onDispose() {
|
|
138
|
-
await super.$onDispose?.();
|
|
139
|
-
}
|
|
140
|
-
};
|
|
141
|
-
InjectableClass.scope = resolveInjectableScope(typeof scope === 'undefined', InjectableClass);
|
|
142
|
-
// @ts-expect-error
|
|
143
|
-
return InjectableClass;
|
|
144
|
-
}
|
|
145
84
|
export function substitute(injectable, substitution, stackTraceDepth = 0) {
|
|
146
85
|
const dependencies = { ...injectable.dependencies };
|
|
147
86
|
const depth = stackTraceDepth + 1;
|
|
@@ -152,16 +91,12 @@ export function substitute(injectable, substitution, stackTraceDepth = 0) {
|
|
|
152
91
|
if (isInjectable(value)) {
|
|
153
92
|
dependencies[key] = value;
|
|
154
93
|
}
|
|
155
|
-
else if (
|
|
94
|
+
else if (isFactoryInjectable(original)) {
|
|
156
95
|
dependencies[key] = substitute(original, value, depth);
|
|
157
96
|
}
|
|
158
97
|
}
|
|
159
98
|
}
|
|
160
|
-
if (
|
|
161
|
-
// @ts-expect-error
|
|
162
|
-
return createExtendableClassInjectable(injectable, dependencies, injectable.scope, depth);
|
|
163
|
-
}
|
|
164
|
-
else if (isFactoryInjectable(injectable)) {
|
|
99
|
+
if (isFactoryInjectable(injectable)) {
|
|
165
100
|
// @ts-expect-error
|
|
166
101
|
return createFactoryInjectable({ ...injectable, dependencies }, injectable.label, depth);
|
|
167
102
|
}
|
|
@@ -187,35 +122,20 @@ export function compareScope(left, operator, right) {
|
|
|
187
122
|
throw new Error('Invalid operator');
|
|
188
123
|
}
|
|
189
124
|
}
|
|
190
|
-
const logger =
|
|
191
|
-
|
|
125
|
+
const logger = Object.assign((label) => createFactoryInjectable({
|
|
126
|
+
dependencies: { logger },
|
|
127
|
+
scope: Scope.Global,
|
|
128
|
+
factory: ({ logger }) => logger.child({ $label: label }),
|
|
129
|
+
}), createLazyInjectable(Scope.Global, 'Logger'));
|
|
192
130
|
const inject = createLazyInjectable(Scope.Global, 'Inject function');
|
|
193
131
|
const dispose = createLazyInjectable(Scope.Global, 'Dispose function');
|
|
194
|
-
const hook = createFactoryInjectable({
|
|
195
|
-
scope: Scope.Transient,
|
|
196
|
-
dependencies: { registry },
|
|
197
|
-
factory: ({ registry }) => {
|
|
198
|
-
const hooks = new Hooks();
|
|
199
|
-
const on = (name, callback) => {
|
|
200
|
-
hooks.add(name, callback);
|
|
201
|
-
return registry.hooks.add(name, callback);
|
|
202
|
-
};
|
|
203
|
-
return { hooks, on };
|
|
204
|
-
},
|
|
205
|
-
pick: ({ on }) => on,
|
|
206
|
-
dispose: ({ hooks }, { registry }) => {
|
|
207
|
-
for (const [hook, callbacks] of hooks[kHookCollection].entries()) {
|
|
208
|
-
for (const callback of callbacks) {
|
|
209
|
-
registry.hooks.remove(hook, callback);
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
hooks.clear();
|
|
213
|
-
},
|
|
214
|
-
});
|
|
215
132
|
function resolveInjectableScope(isDefaultScope, injectable) {
|
|
216
133
|
const actualScope = getInjectableScope(injectable);
|
|
217
134
|
if (!isDefaultScope && compareScope(actualScope, '>', injectable.scope))
|
|
218
135
|
throw new Error(`Invalid scope ${injectable.scope} for an injectable: dependencies have stricter scope - ${actualScope}`);
|
|
219
136
|
return actualScope;
|
|
220
137
|
}
|
|
221
|
-
export const CoreInjectables = { logger,
|
|
138
|
+
export const CoreInjectables = { logger, inject, dispose };
|
|
139
|
+
export const provide = (token, value) => {
|
|
140
|
+
return { token, value };
|
|
141
|
+
};
|
package/dist/logger.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { threadId } from 'node:worker_threads';
|
|
2
2
|
import { pino, stdTimeFunctions } from 'pino';
|
|
3
3
|
import { build as pretty } from 'pino-pretty';
|
|
4
|
+
import { errWithCause } from 'pino-std-serializers';
|
|
4
5
|
// TODO: use node:util inspect
|
|
5
6
|
const bg = (value, color) => `\x1b[${color}m${value}\x1b[0m`;
|
|
6
7
|
const fg = (value, color) => `\x1b[38;5;${color}m${value}\x1b[0m`;
|
|
@@ -31,25 +32,28 @@ const levelLabels = {
|
|
|
31
32
|
60: ' FATAL ',
|
|
32
33
|
[Number.POSITIVE_INFINITY]: 'SILENT',
|
|
33
34
|
};
|
|
34
|
-
export const createLogger = (options = {}, $
|
|
35
|
+
export const createLogger = (options = {}, $lable) => {
|
|
35
36
|
let { destinations, pinoOptions } = options;
|
|
36
37
|
if (!destinations || !destinations?.length) {
|
|
37
|
-
destinations = [
|
|
38
|
+
destinations = [
|
|
39
|
+
createConsolePrettyDestination((options.pinoOptions?.level || 'info')),
|
|
40
|
+
];
|
|
38
41
|
}
|
|
39
42
|
const lowestLevelValue = destinations.reduce((acc, destination) => Math.min(acc, 'stream' in destination
|
|
40
43
|
? pino.levels.values[destination.level]
|
|
41
44
|
: Number.POSITIVE_INFINITY), Number.POSITIVE_INFINITY);
|
|
42
45
|
const level = pino.levels.labels[lowestLevelValue];
|
|
43
|
-
|
|
46
|
+
const serializers = { ...pinoOptions?.serializers, err: errWithCause };
|
|
47
|
+
return pino({ timestamp: stdTimeFunctions.isoTime, ...pinoOptions, level, serializers }, pino.multistream(destinations)).child({ $lable, $threadId: threadId });
|
|
44
48
|
};
|
|
45
49
|
export const createConsolePrettyDestination = (level, sync = true) => ({
|
|
46
50
|
level,
|
|
47
51
|
stream: pretty({
|
|
48
52
|
colorize: true,
|
|
49
|
-
ignore: 'hostname,$
|
|
53
|
+
ignore: 'hostname,$lable,$threadId',
|
|
50
54
|
errorLikeObjectKeys: ['err', 'error', 'cause'],
|
|
51
55
|
messageFormat: (log, messageKey) => {
|
|
52
|
-
const group = fg(`[${log.$
|
|
56
|
+
const group = fg(`[${log.$lable}]`, 11);
|
|
53
57
|
const msg = fg(log[messageKey], messageColors[log.level]);
|
|
54
58
|
const thread = fg(`(Thread-${log.$threadId})`, 89);
|
|
55
59
|
return `\x1b[0m${thread} ${group} ${msg}`;
|
package/dist/plugin.js
CHANGED
package/dist/utils/functions.js
CHANGED
|
@@ -1,31 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
* Very simple pattern matching function.
|
|
3
|
-
*/
|
|
4
|
-
export function match(value, pattern) {
|
|
5
|
-
if (typeof pattern === 'function') {
|
|
6
|
-
return pattern(value);
|
|
7
|
-
}
|
|
8
|
-
else if (typeof pattern === 'string') {
|
|
9
|
-
if (pattern === '*' || pattern === '**') {
|
|
10
|
-
return true;
|
|
11
|
-
}
|
|
12
|
-
else if (pattern.at(0) === '*' && pattern.at(-1) === '*') {
|
|
13
|
-
return value.includes(pattern.slice(1, -1));
|
|
14
|
-
}
|
|
15
|
-
else if (pattern.at(-1) === '*') {
|
|
16
|
-
return value.startsWith(pattern.slice(0, -1));
|
|
17
|
-
}
|
|
18
|
-
else if (pattern.at(0) === '*') {
|
|
19
|
-
return value.endsWith(pattern.slice(1));
|
|
20
|
-
}
|
|
21
|
-
else {
|
|
22
|
-
return value === pattern;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
else {
|
|
26
|
-
return pattern.test(value);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
1
|
+
export { match } from '@nmtjs/common';
|
|
29
2
|
export function isJsFile(name) {
|
|
30
3
|
if (name.endsWith('.d.ts'))
|
|
31
4
|
return false;
|
|
@@ -33,3 +6,12 @@ export function isJsFile(name) {
|
|
|
33
6
|
const ext = leading.join('.');
|
|
34
7
|
return ['js', 'mjs', 'cjs', 'ts', 'mts', 'cts'].includes(ext);
|
|
35
8
|
}
|
|
9
|
+
export function pick(obj, keys) {
|
|
10
|
+
const result = {};
|
|
11
|
+
for (const key in keys) {
|
|
12
|
+
if (key in obj) {
|
|
13
|
+
result[key] = obj[key];
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
return result;
|
|
17
|
+
}
|
package/package.json
CHANGED
|
@@ -2,38 +2,36 @@
|
|
|
2
2
|
"name": "@nmtjs/core",
|
|
3
3
|
"type": "module",
|
|
4
4
|
"exports": {
|
|
5
|
-
".":
|
|
6
|
-
|
|
7
|
-
"import": "./dist/index.js",
|
|
8
|
-
"module-sync": "./dist/index.js"
|
|
9
|
-
},
|
|
10
|
-
"./constants": {
|
|
11
|
-
"types": "./dist/constants.d.ts",
|
|
12
|
-
"import": "./dist/constants.js",
|
|
13
|
-
"module-sync": "./dist/constants.js"
|
|
14
|
-
}
|
|
5
|
+
".": "./dist/index.js",
|
|
6
|
+
"./constants": "./dist/constants.js"
|
|
15
7
|
},
|
|
16
8
|
"peerDependencies": {
|
|
17
9
|
"pino": "^9.6.0",
|
|
18
10
|
"pino-pretty": "^13.0.0",
|
|
19
|
-
"@nmtjs/common": "0.
|
|
20
|
-
"@nmtjs/type": "0.
|
|
11
|
+
"@nmtjs/common": "0.15.0-beta.2",
|
|
12
|
+
"@nmtjs/type": "0.15.0-beta.2"
|
|
21
13
|
},
|
|
22
14
|
"devDependencies": {
|
|
23
|
-
"@types/node": "^
|
|
15
|
+
"@types/node": "^24",
|
|
24
16
|
"pino": "^9.6.0",
|
|
25
17
|
"pino-pretty": "^13.0.0",
|
|
26
|
-
"@nmtjs/common": "0.
|
|
27
|
-
"@nmtjs/type": "0.
|
|
18
|
+
"@nmtjs/common": "0.15.0-beta.2",
|
|
19
|
+
"@nmtjs/type": "0.15.0-beta.2"
|
|
28
20
|
},
|
|
29
21
|
"files": [
|
|
30
22
|
"dist",
|
|
31
23
|
"LICENSE.md",
|
|
32
24
|
"README.md"
|
|
33
25
|
],
|
|
34
|
-
"
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"hookable": "6.0.0-rc.1",
|
|
28
|
+
"pino-std-serializers": "^7.0.0"
|
|
29
|
+
},
|
|
30
|
+
"version": "0.15.0-beta.2",
|
|
35
31
|
"scripts": {
|
|
32
|
+
"clean-build": "rm -rf ./dist",
|
|
36
33
|
"build": "tsc",
|
|
34
|
+
"dev": "tsc --watch",
|
|
37
35
|
"type-check": "tsc --noEmit"
|
|
38
36
|
}
|
|
39
37
|
}
|
package/dist/constants.d.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
export declare const kOptionalDependency: unique symbol;
|
|
2
|
-
export type kOptionalDependency = typeof kOptionalDependency;
|
|
3
|
-
export declare const kInjectable: unique symbol;
|
|
4
|
-
export type kInjectable = typeof kInjectable;
|
|
5
|
-
export declare const kLazyInjectable: unique symbol;
|
|
6
|
-
export type kLazyInjectable = typeof kLazyInjectable;
|
|
7
|
-
export declare const kValueInjectable: unique symbol;
|
|
8
|
-
export type kValueInjectable = typeof kValueInjectable;
|
|
9
|
-
export declare const kFactoryInjectable: unique symbol;
|
|
10
|
-
export type kFactoryInjectable = typeof kFactoryInjectable;
|
|
11
|
-
export declare const kClassInjectable: unique symbol;
|
|
12
|
-
export type kClassInjectable = typeof kClassInjectable;
|
|
13
|
-
export declare const kClassInjectableCreate: unique symbol;
|
|
14
|
-
export type kClassInjectableCreate = typeof kClassInjectableCreate;
|
|
15
|
-
export declare const kClassInjectableDispose: unique symbol;
|
|
16
|
-
export type kClassInjectableDispose = typeof kClassInjectableDispose;
|
|
17
|
-
export declare const kProvider: unique symbol;
|
|
18
|
-
export type kProvider = typeof kProvider;
|
|
19
|
-
export declare const kHookCollection: unique symbol;
|
|
20
|
-
export type kHookCollection = typeof kHookCollection;
|
|
21
|
-
export declare const kPlugin: unique symbol;
|
|
22
|
-
export type kPlugin = typeof kPlugin;
|
|
23
|
-
export declare const kMetadata: unique symbol;
|
|
24
|
-
export type kMetadata = typeof kMetadata;
|
package/dist/container.d.ts
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import type { AnyInjectable, Dependencies, DependencyContext, ResolveInjectableType } from './injectables.ts';
|
|
2
|
-
import type { Logger } from './logger.ts';
|
|
3
|
-
import type { Registry } from './registry.ts';
|
|
4
|
-
import { Scope } from './enums.ts';
|
|
5
|
-
type InstanceWrapper = {
|
|
6
|
-
private: any;
|
|
7
|
-
public: any;
|
|
8
|
-
context: any;
|
|
9
|
-
};
|
|
10
|
-
type ContainerOptions = {
|
|
11
|
-
registry: Registry;
|
|
12
|
-
logger: Logger;
|
|
13
|
-
};
|
|
14
|
-
export declare class Container {
|
|
15
|
-
private readonly application;
|
|
16
|
-
readonly scope: Exclude<Scope, Scope.Transient>;
|
|
17
|
-
private readonly parent?;
|
|
18
|
-
readonly instances: Map<AnyInjectable, InstanceWrapper[]>;
|
|
19
|
-
private readonly resolvers;
|
|
20
|
-
private readonly injectables;
|
|
21
|
-
private readonly dependants;
|
|
22
|
-
private disposing;
|
|
23
|
-
constructor(application: ContainerOptions, scope?: Exclude<Scope, Scope.Transient>, parent?: Container | undefined);
|
|
24
|
-
load(): Promise<void>;
|
|
25
|
-
fork(scope: Exclude<Scope, Scope.Transient>): Container;
|
|
26
|
-
dispose(): Promise<void>;
|
|
27
|
-
containsWithinSelf(injectable: AnyInjectable): boolean;
|
|
28
|
-
contains(injectable: AnyInjectable): boolean;
|
|
29
|
-
get<T extends AnyInjectable>(injectable: T): ResolveInjectableType<T>;
|
|
30
|
-
resolve<T extends AnyInjectable>(injectable: T): Promise<ResolveInjectableType<T>>;
|
|
31
|
-
createContext<T extends Dependencies>(dependencies: T): Promise<DependencyContext<T>>;
|
|
32
|
-
private createInjectableContext;
|
|
33
|
-
provide<T extends AnyInjectable>(injectable: T, instance: ResolveInjectableType<T>): Promise<void>;
|
|
34
|
-
satisfies(injectable: AnyInjectable): boolean;
|
|
35
|
-
private findCurrentScopeInjectables;
|
|
36
|
-
private resolveInjectable;
|
|
37
|
-
private createResolution;
|
|
38
|
-
private createInjectFunction;
|
|
39
|
-
private createDisposeFunction;
|
|
40
|
-
private getDisposalOrder;
|
|
41
|
-
private disposeInjectableInstances;
|
|
42
|
-
private disposeInjectableInstance;
|
|
43
|
-
}
|
|
44
|
-
export type InjectFn = ReturnType<Container['createInjectFunction']>;
|
|
45
|
-
export type DisposeFn = ReturnType<Container['createDisposeFunction']>;
|
|
46
|
-
export {};
|
package/dist/enums.d.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
export declare enum Scope {
|
|
2
|
-
Global = "Global",
|
|
3
|
-
Connection = "Connection",
|
|
4
|
-
Call = "Call",
|
|
5
|
-
Transient = "Transient"
|
|
6
|
-
}
|
|
7
|
-
export declare enum Hook {
|
|
8
|
-
BeforeInitialize = "BeforeInitialize",
|
|
9
|
-
AfterInitialize = "AfterInitialize",
|
|
10
|
-
BeforeStart = "BeforeStart",
|
|
11
|
-
AfterStart = "AfterStart",
|
|
12
|
-
BeforeStop = "BeforeStop",
|
|
13
|
-
AfterStop = "AfterStop",
|
|
14
|
-
BeforeTerminate = "BeforeTerminate",
|
|
15
|
-
AfterTerminate = "AfterTerminate",
|
|
16
|
-
OnConnect = "OnConnect",
|
|
17
|
-
OnDisconnect = "OnDisconnect"
|
|
18
|
-
}
|
package/dist/hooks.d.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import type { Callback } from '@nmtjs/common';
|
|
2
|
-
import type { Hook } from './enums.ts';
|
|
3
|
-
import { kHookCollection } from './constants.ts';
|
|
4
|
-
export interface HookType {
|
|
5
|
-
[key: string]: (...args: any[]) => any;
|
|
6
|
-
}
|
|
7
|
-
export type CallHook<T extends string> = (hook: T, ...args: T extends keyof HookType ? Parameters<HookType[T]> : any[]) => Promise<void>;
|
|
8
|
-
export declare class Hooks {
|
|
9
|
-
static merge(from: Hooks, to: Hooks): void;
|
|
10
|
-
[kHookCollection]: Map<string, Set<Callback>>;
|
|
11
|
-
add(name: string, callback: Callback): () => void;
|
|
12
|
-
remove(name: string, callback: Callback): void;
|
|
13
|
-
call<T extends string | Hook>(name: T, options: {
|
|
14
|
-
concurrent?: boolean;
|
|
15
|
-
reverse?: boolean;
|
|
16
|
-
} | undefined, ...args: T extends Hook ? Parameters<HookType[T]> : any[]): Promise<void>;
|
|
17
|
-
clear(): void;
|
|
18
|
-
}
|
|
19
|
-
export declare const createErrForHook: (hook: Hook | (object & string)) => string;
|
package/dist/index.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
export * from './constants.ts';
|
|
2
|
-
export * from './container.ts';
|
|
3
|
-
export * from './enums.ts';
|
|
4
|
-
export * from './hooks.ts';
|
|
5
|
-
export * from './injectables.ts';
|
|
6
|
-
export * from './logger.ts';
|
|
7
|
-
export * from './metadata.ts';
|
|
8
|
-
export * from './plugin.ts';
|
|
9
|
-
export * from './registry.ts';
|
|
10
|
-
export * from './types.ts';
|
|
11
|
-
export * from './utils/index.ts';
|
package/dist/injectables.d.ts
DELETED
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
import type { Async, ClassConstructor, ClassConstructorArgs, ClassInstance } from '@nmtjs/common';
|
|
2
|
-
import type { Hook } from './enums.ts';
|
|
3
|
-
import type { HookType } from './hooks.ts';
|
|
4
|
-
import type { Logger } from './logger.ts';
|
|
5
|
-
import type { Registry } from './registry.ts';
|
|
6
|
-
import { kClassInjectable, kClassInjectableCreate, kClassInjectableDispose, kFactoryInjectable, kInjectable, kLazyInjectable, kOptionalDependency, kValueInjectable } from './constants.ts';
|
|
7
|
-
import { Scope } from './enums.ts';
|
|
8
|
-
import { Hooks } from './hooks.ts';
|
|
9
|
-
export type DependencyOptional<T extends AnyInjectable = AnyInjectable> = {
|
|
10
|
-
[kOptionalDependency]: any;
|
|
11
|
-
injectable: T;
|
|
12
|
-
};
|
|
13
|
-
export type Depedency = DependencyOptional | AnyInjectable;
|
|
14
|
-
export type Dependencies = Record<string, Depedency>;
|
|
15
|
-
export type ResolveInjectableType<T extends AnyInjectable> = T extends Injectable<infer Type, any, any> ? Type : never;
|
|
16
|
-
export interface Dependant<Deps extends Dependencies = Dependencies> {
|
|
17
|
-
dependencies: Deps;
|
|
18
|
-
label?: string;
|
|
19
|
-
stack?: string;
|
|
20
|
-
}
|
|
21
|
-
export type DependencyInjectable<T extends Depedency> = T extends AnyInjectable ? T : T extends DependencyOptional ? T['injectable'] : never;
|
|
22
|
-
export type DependencyContext<Deps extends Dependencies> = {
|
|
23
|
-
readonly [K in keyof Deps as Deps[K] extends AnyInjectable ? K : never]: Deps[K] extends AnyInjectable ? ResolveInjectableType<Deps[K]> : never;
|
|
24
|
-
} & {
|
|
25
|
-
readonly [K in keyof Deps as Deps[K] extends DependencyOptional ? K : never]?: Deps[K] extends DependencyOptional ? ResolveInjectableType<Deps[K]['injectable']> : never;
|
|
26
|
-
};
|
|
27
|
-
export type InjectableFactoryType<InjectableType, InjectableDeps extends Dependencies> = (context: DependencyContext<InjectableDeps>) => Async<InjectableType>;
|
|
28
|
-
export type InjectablePickType<Input, Output> = (injectable: Input) => Output;
|
|
29
|
-
export type InjectableDisposeType<InjectableType, InjectableDeps extends Dependencies> = (instance: InjectableType, context: DependencyContext<InjectableDeps>) => any;
|
|
30
|
-
export interface LazyInjectable<T, S extends Scope = Scope.Global> extends Dependant<{}> {
|
|
31
|
-
scope: S;
|
|
32
|
-
[kInjectable]: any;
|
|
33
|
-
[kLazyInjectable]: T;
|
|
34
|
-
}
|
|
35
|
-
export interface ValueInjectable<T> extends Dependant<{}> {
|
|
36
|
-
scope: Scope.Global;
|
|
37
|
-
value: T;
|
|
38
|
-
[kInjectable]: any;
|
|
39
|
-
[kValueInjectable]: any;
|
|
40
|
-
}
|
|
41
|
-
export interface FactoryInjectable<T, D extends Dependencies = {}, S extends Scope = Scope.Global, P = T> extends Dependant<D> {
|
|
42
|
-
scope: S;
|
|
43
|
-
factory: InjectableFactoryType<P, D>;
|
|
44
|
-
pick: InjectablePickType<P, T>;
|
|
45
|
-
dispose?: InjectableDisposeType<P, D>;
|
|
46
|
-
[kInjectable]: any;
|
|
47
|
-
[kFactoryInjectable]: any;
|
|
48
|
-
}
|
|
49
|
-
export interface BaseClassInjectable<T, D extends Dependencies = {}, S extends Scope = Scope.Global> extends Dependant<D> {
|
|
50
|
-
new (...args: any[]): T;
|
|
51
|
-
scope: S;
|
|
52
|
-
[kInjectable]: any;
|
|
53
|
-
[kClassInjectable]: any;
|
|
54
|
-
}
|
|
55
|
-
export interface ClassInjectable<T, D extends Dependencies = {}, S extends Scope = Scope.Global, A extends any[] = []> extends Dependant<D> {
|
|
56
|
-
new ($context: DependencyContext<D>, ...args: A): T & {
|
|
57
|
-
$context: DependencyContext<D>;
|
|
58
|
-
[kClassInjectableCreate]?: () => Promise<void>;
|
|
59
|
-
[kClassInjectableDispose]?: () => Promise<void>;
|
|
60
|
-
};
|
|
61
|
-
scope: S;
|
|
62
|
-
[kInjectable]: any;
|
|
63
|
-
[kClassInjectable]: any;
|
|
64
|
-
}
|
|
65
|
-
export type Injectable<V = any, D extends Dependencies = {}, S extends Scope = Scope> = LazyInjectable<V, S> | ValueInjectable<V> | FactoryInjectable<V, D, S, any> | BaseClassInjectable<V, D, S>;
|
|
66
|
-
export type AnyInjectable<T = any, S extends Scope = Scope> = Injectable<T, any, S>;
|
|
67
|
-
export declare const isLazyInjectable: (injectable: any) => injectable is LazyInjectable<any>;
|
|
68
|
-
export declare const isFactoryInjectable: (injectable: any) => injectable is FactoryInjectable<any>;
|
|
69
|
-
export declare const isClassInjectable: (injectable: any) => injectable is ClassInjectable<any>;
|
|
70
|
-
export declare const isValueInjectable: (injectable: any) => injectable is ValueInjectable<any>;
|
|
71
|
-
export declare const isInjectable: (injectable: any) => injectable is AnyInjectable<any>;
|
|
72
|
-
export declare const isOptionalInjectable: (injectable: any) => injectable is DependencyOptional<any>;
|
|
73
|
-
export declare function getInjectableScope(injectable: AnyInjectable): Scope;
|
|
74
|
-
export declare function getDepedencencyInjectable(dependency: Depedency): AnyInjectable;
|
|
75
|
-
export declare function createOptionalInjectable<T extends AnyInjectable>(injectable: T): DependencyOptional<T>;
|
|
76
|
-
export declare function createLazyInjectable<T, S extends Scope = Scope.Global>(scope?: S, label?: string, stackTraceDepth?: number): LazyInjectable<T, S>;
|
|
77
|
-
export declare function createValueInjectable<T>(value: T, label?: string, stackTraceDepth?: number): ValueInjectable<T>;
|
|
78
|
-
export declare function createFactoryInjectable<T, D extends Dependencies = {}, S extends Scope = Scope.Global, P = T>(paramsOrFactory: {
|
|
79
|
-
dependencies?: D;
|
|
80
|
-
scope?: S;
|
|
81
|
-
pick?: InjectablePickType<P, T>;
|
|
82
|
-
factory: InjectableFactoryType<P, D>;
|
|
83
|
-
dispose?: InjectableDisposeType<P, D>;
|
|
84
|
-
} | InjectableFactoryType<P, D>, label?: string, stackTraceDepth?: number): FactoryInjectable<null extends T ? P : T, D, S, P>;
|
|
85
|
-
export declare const createClassInjectable: <D extends Dependencies = {}, S extends Scope = Scope.Global>(dependencies?: D, scope?: S, stackTraceDepth?: number) => ClassInjectable<ClassInstance<{
|
|
86
|
-
new ($context: DependencyContext<D>): {
|
|
87
|
-
$context: DependencyContext<D>;
|
|
88
|
-
[kClassInjectableCreate](): Promise<void>;
|
|
89
|
-
[kClassInjectableDispose](): Promise<void>;
|
|
90
|
-
};
|
|
91
|
-
dependencies: D;
|
|
92
|
-
scope: S;
|
|
93
|
-
stack: string | undefined;
|
|
94
|
-
get label(): string;
|
|
95
|
-
[kInjectable]: boolean;
|
|
96
|
-
[kClassInjectable]: boolean;
|
|
97
|
-
}>, D, S>;
|
|
98
|
-
export declare function createExtendableClassInjectable<B extends ClassConstructor<any>, D extends Dependencies = {}, S extends Scope = Scope.Global>(baseClass: B, dependencies?: D, scope?: S, stackTraceDepth?: number): B extends ClassInjectable<any> ? ClassInjectable<ClassInstance<B>, D, S> : ClassInjectable<ClassInstance<B>, D, S, ClassConstructorArgs<B, []>>;
|
|
99
|
-
export type DependenciesSubstitution<T extends Dependencies> = {
|
|
100
|
-
[K in keyof T]?: T[K] extends AnyInjectable<infer Type> ? AnyInjectable<Type> | DependenciesSubstitution<T[K]['dependencies']> : never;
|
|
101
|
-
};
|
|
102
|
-
export declare function substitute<T extends FactoryInjectable<any, any, Scope> | BaseClassInjectable<any, any, Scope>>(injectable: T, substitution: DependenciesSubstitution<T['dependencies']>, stackTraceDepth?: number): T;
|
|
103
|
-
export declare function compareScope(left: Scope, operator: '>' | '<' | '>=' | '<=' | '=' | '!=', right: Scope): boolean;
|
|
104
|
-
export declare const CoreInjectables: {
|
|
105
|
-
logger: LazyInjectable<Logger, Scope.Global>;
|
|
106
|
-
registry: LazyInjectable<Registry, Scope.Global>;
|
|
107
|
-
inject: LazyInjectable<(<T extends AnyInjectable>(injectable: T, context: { [K in keyof T["dependencies"]]?: ResolveInjectableType<T["dependencies"][K]> | AnyInjectable<ResolveInjectableType<T["dependencies"][K]>> | undefined; }) => Promise<ResolveInjectableType<T>>) & {
|
|
108
|
-
explicit: <T extends AnyInjectable>(injectable: T, context: { [K in keyof T["dependencies"]]?: ResolveInjectableType<T["dependencies"][K]> | AnyInjectable<ResolveInjectableType<T["dependencies"][K]>> | undefined; }) => Promise<Awaited<ResolveInjectableType<T>> & {
|
|
109
|
-
[Symbol.asyncDispose]: () => Promise<void>;
|
|
110
|
-
}>;
|
|
111
|
-
}, Scope.Global>;
|
|
112
|
-
dispose: LazyInjectable<(<T extends AnyInjectable>(injectable: T, instance?: any) => Promise<void>), Scope.Global>;
|
|
113
|
-
hook: FactoryInjectable<(<T extends Hook>(name: T, callback: HookType[T]) => () => void), {
|
|
114
|
-
registry: LazyInjectable<Registry, Scope.Global>;
|
|
115
|
-
}, Scope.Transient, {
|
|
116
|
-
hooks: Hooks;
|
|
117
|
-
on: <T extends Hook>(name: T, callback: HookType[T]) => () => void;
|
|
118
|
-
}>;
|
|
119
|
-
};
|
package/dist/logger.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import type { DestinationStream, Level, LoggerOptions, Logger as PinoLogger, StreamEntry } from 'pino';
|
|
2
|
-
import { pino } from 'pino';
|
|
3
|
-
export type { StreamEntry } from 'pino';
|
|
4
|
-
export type Logger = PinoLogger;
|
|
5
|
-
export type LoggingOptions = {
|
|
6
|
-
destinations?: Array<DestinationStream | StreamEntry<Level>>;
|
|
7
|
-
pinoOptions?: LoggerOptions;
|
|
8
|
-
};
|
|
9
|
-
export declare const createLogger: (options: LoggingOptions | undefined, $group: string) => pino.Logger<never, boolean>;
|
|
10
|
-
export declare const createConsolePrettyDestination: (level: Level, sync?: boolean) => StreamEntry;
|
package/dist/metadata.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { kMetadata } from './constants.ts';
|
|
2
|
-
export type Metadata<T = any> = {
|
|
3
|
-
key: MetadataKey<T>;
|
|
4
|
-
value: T;
|
|
5
|
-
};
|
|
6
|
-
export type MetadataKey<T = any> = {
|
|
7
|
-
[kMetadata]: string;
|
|
8
|
-
as(value: T): Metadata<T>;
|
|
9
|
-
};
|
|
10
|
-
export declare const createMetadataKey: <T>(key: string) => MetadataKey<T>;
|
|
11
|
-
export declare class MetadataStore extends Map<MetadataKey, Metadata> {
|
|
12
|
-
get<T>(key: MetadataKey<T>): T | undefined;
|
|
13
|
-
}
|
package/dist/plugin.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import type { Async } from '@nmtjs/common';
|
|
2
|
-
import type { PluginContext } from './types.ts';
|
|
3
|
-
import { kPlugin } from './constants.ts';
|
|
4
|
-
export interface BasePlugin<Type = any, Options = unknown, Context extends PluginContext = PluginContext> {
|
|
5
|
-
name: string;
|
|
6
|
-
init: (context: Context, options: Options) => Async<Type>;
|
|
7
|
-
}
|
|
8
|
-
export interface Plugin<Type = void, Options = unknown, Context extends PluginContext = PluginContext> extends BasePlugin<Type, Options, Context> {
|
|
9
|
-
[kPlugin]: any;
|
|
10
|
-
}
|
|
11
|
-
export declare const createPlugin: <Options = unknown, Type = void>(name: string, init: Plugin<Type, Options>["init"]) => Plugin<Type, Options>;
|
|
12
|
-
export declare const isPlugin: (value: any) => value is Plugin;
|
package/dist/registry.d.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import type { Hook } from './enums.ts';
|
|
2
|
-
import type { HookType } from './hooks.ts';
|
|
3
|
-
import type { AnyInjectable, Dependant } from './injectables.ts';
|
|
4
|
-
import type { Logger } from './logger.ts';
|
|
5
|
-
import { Scope } from './enums.ts';
|
|
6
|
-
import { Hooks } from './hooks.ts';
|
|
7
|
-
export declare class Registry {
|
|
8
|
-
protected readonly application: {
|
|
9
|
-
logger: Logger;
|
|
10
|
-
};
|
|
11
|
-
readonly hooks: Hooks;
|
|
12
|
-
constructor(application: {
|
|
13
|
-
logger: Logger;
|
|
14
|
-
});
|
|
15
|
-
registerHooks<T extends Hooks>(hooks: T): void;
|
|
16
|
-
registerHook<T extends Hook>(name: T, callback: HookType[T]): void;
|
|
17
|
-
getDependants(): Generator<Dependant>;
|
|
18
|
-
clear(): void;
|
|
19
|
-
}
|
|
20
|
-
export declare const scopeErrorMessage: (name: any, scope?: Scope) => string;
|
|
21
|
-
export declare function hasInvalidScopeDeps(injectables: AnyInjectable[], scope?: Scope): boolean;
|
package/dist/registry.js
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { Scope } from "./enums.js";
|
|
2
|
-
import { Hooks } from "./hooks.js";
|
|
3
|
-
import { getInjectableScope } from "./injectables.js";
|
|
4
|
-
export class Registry {
|
|
5
|
-
application;
|
|
6
|
-
hooks = new Hooks();
|
|
7
|
-
constructor(application) {
|
|
8
|
-
this.application = application;
|
|
9
|
-
}
|
|
10
|
-
registerHooks(hooks) {
|
|
11
|
-
Hooks.merge(hooks, this.hooks);
|
|
12
|
-
}
|
|
13
|
-
registerHook(name, callback) {
|
|
14
|
-
this.hooks.add(name, callback);
|
|
15
|
-
}
|
|
16
|
-
*getDependants() { }
|
|
17
|
-
clear() {
|
|
18
|
-
this.hooks.clear();
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
export const scopeErrorMessage = (name, scope = Scope.Global) => `${name} must be a ${scope} scope (including all nested dependencies)`;
|
|
22
|
-
export function hasInvalidScopeDeps(injectables, scope = Scope.Global) {
|
|
23
|
-
return injectables.some((injectable) => getInjectableScope(injectable) !== scope);
|
|
24
|
-
}
|
package/dist/types.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import type { Container } from './container.ts';
|
|
2
|
-
import type { Hooks } from './hooks.ts';
|
|
3
|
-
import type { Logger } from './logger.ts';
|
|
4
|
-
import type { Registry } from './registry.ts';
|
|
5
|
-
export interface PluginContext {
|
|
6
|
-
logger: Logger;
|
|
7
|
-
registry: Registry;
|
|
8
|
-
hooks: Hooks;
|
|
9
|
-
container: Container;
|
|
10
|
-
}
|
|
11
|
-
export type Pattern = RegExp | string | ((value: string) => boolean);
|
package/dist/utils/index.d.ts
DELETED
package/dist/utils/pool.d.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
interface PoolOptions {
|
|
2
|
-
timeout?: number;
|
|
3
|
-
}
|
|
4
|
-
export declare class PoolError extends Error {
|
|
5
|
-
}
|
|
6
|
-
export declare class Pool<T = unknown> {
|
|
7
|
-
#private;
|
|
8
|
-
private readonly options;
|
|
9
|
-
constructor(options?: PoolOptions);
|
|
10
|
-
add(item: T): void;
|
|
11
|
-
remove(item: T): void;
|
|
12
|
-
capture(timeout?: number | undefined): Promise<T>;
|
|
13
|
-
next(exclusive?: boolean, timeout?: number | undefined): Promise<T>;
|
|
14
|
-
release(item: T): void;
|
|
15
|
-
isFree(item: T): boolean;
|
|
16
|
-
get items(): T[];
|
|
17
|
-
}
|
|
18
|
-
export {};
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
export declare class SemaphoreError extends Error {
|
|
2
|
-
}
|
|
3
|
-
export declare class Semaphore {
|
|
4
|
-
private readonly size;
|
|
5
|
-
private readonly timeout;
|
|
6
|
-
private counter;
|
|
7
|
-
private readonly queue;
|
|
8
|
-
constructor(concurrency: number, size?: number, timeout?: number);
|
|
9
|
-
enter(): Promise<void>;
|
|
10
|
-
leave(): void;
|
|
11
|
-
get isEmpty(): boolean;
|
|
12
|
-
get queueLength(): number;
|
|
13
|
-
}
|