@nmtjs/core 0.12.5 → 0.12.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/constants.d.ts +20 -0
- package/dist/constants.js +10 -12
- package/dist/container.d.ts +46 -0
- package/dist/container.js +310 -277
- package/dist/enums.d.ts +18 -0
- package/dist/enums.js +20 -22
- package/dist/hooks.d.ts +19 -0
- package/dist/hooks.js +39 -35
- package/dist/index.d.ts +11 -0
- package/dist/index.js +3 -2
- package/dist/injectables.d.ts +115 -0
- package/dist/injectables.js +194 -179
- package/dist/logger.d.ts +9 -0
- package/dist/logger.js +54 -58
- package/dist/metadata.d.ts +13 -0
- package/dist/metadata.js +10 -15
- package/dist/plugin.d.ts +12 -0
- package/dist/plugin.js +1 -7
- package/dist/registry.d.ts +19 -0
- package/dist/registry.js +17 -18
- package/dist/types.d.ts +11 -0
- package/dist/types.js +0 -2
- package/dist/utils/functions.d.ts +6 -0
- package/dist/utils/functions.js +30 -32
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/index.js +0 -2
- package/dist/utils/pool.d.ts +18 -0
- package/dist/utils/pool.js +103 -90
- package/dist/utils/semaphore.d.ts +13 -0
- package/dist/utils/semaphore.js +53 -46
- package/package.json +9 -9
- package/dist/constants.js.map +0 -1
- package/dist/container.js.map +0 -1
- package/dist/enums.js.map +0 -1
- package/dist/hooks.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/injectables.js.map +0 -1
- package/dist/logger.js.map +0 -1
- package/dist/metadata.js.map +0 -1
- package/dist/plugin.js.map +0 -1
- package/dist/registry.js.map +0 -1
- package/dist/types.js.map +0 -1
- package/dist/utils/functions.js.map +0 -1
- package/dist/utils/index.js.map +0 -1
- package/dist/utils/pool.js.map +0 -1
- package/dist/utils/semaphore.js.map +0 -1
- package/src/constants.ts +0 -41
- package/src/container.ts +0 -440
- package/src/enums.ts +0 -19
- package/src/hooks.ts +0 -71
- package/src/index.ts +0 -11
- package/src/injectables.ts +0 -508
- package/src/logger.ts +0 -99
- package/src/metadata.ts +0 -27
- package/src/plugin.ts +0 -27
- package/src/registry.ts +0 -44
- package/src/types.ts +0 -13
- package/src/utils/functions.ts +0 -31
- package/src/utils/index.ts +0 -3
- package/src/utils/pool.ts +0 -111
- package/src/utils/semaphore.ts +0 -63
package/dist/enums.js
CHANGED
|
@@ -1,22 +1,20 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}({});
|
|
8
|
-
export
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}({});
|
|
21
|
-
|
|
22
|
-
//# sourceMappingURL=enums.js.map
|
|
1
|
+
export var Scope;
|
|
2
|
+
(function (Scope) {
|
|
3
|
+
Scope["Global"] = "Global";
|
|
4
|
+
Scope["Connection"] = "Connection";
|
|
5
|
+
Scope["Call"] = "Call";
|
|
6
|
+
Scope["Transient"] = "Transient";
|
|
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.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Callback } from '@nmtjs/common';
|
|
2
|
+
import { kHookCollection } from './constants.ts';
|
|
3
|
+
import type { Hook } from './enums.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/hooks.js
CHANGED
|
@@ -1,39 +1,43 @@
|
|
|
1
1
|
import { kHookCollection } from "./constants.js";
|
|
2
2
|
export class Hooks {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
3
|
+
static merge(from, to) {
|
|
4
|
+
for (const [name, callbacks] of from[kHookCollection]) {
|
|
5
|
+
for (const callback of callbacks) {
|
|
6
|
+
to.add(name, callback);
|
|
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();
|
|
41
|
+
}
|
|
36
42
|
}
|
|
37
43
|
export const createErrForHook = (hook) => `Error during [${hook}] hook`;
|
|
38
|
-
|
|
39
|
-
//# sourceMappingURL=hooks.js.map
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
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/index.js
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
// biome-ignore lint/correctness/noUnusedImports: TSC wants it
|
|
2
|
+
// biome-ignore assist/source/organizeImports: TSC wants it
|
|
3
|
+
import {} from 'pino';
|
|
1
4
|
export * from "./constants.js";
|
|
2
5
|
export * from "./container.js";
|
|
3
6
|
export * from "./enums.js";
|
|
@@ -9,5 +12,3 @@ export * from "./plugin.js";
|
|
|
9
12
|
export * from "./registry.js";
|
|
10
13
|
export * from "./types.js";
|
|
11
14
|
export * from "./utils/index.js";
|
|
12
|
-
|
|
13
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { type Async, type ClassConstructor, type ClassConstructorArgs, type ClassInstance } from '@nmtjs/common';
|
|
2
|
+
import { kClassInjectable, kFactoryInjectable, kInjectable, kLazyInjectable, kOptionalDependency, kValueInjectable } from './constants.ts';
|
|
3
|
+
import { type Hook, Scope } from './enums.ts';
|
|
4
|
+
import { Hooks, type HookType } from './hooks.ts';
|
|
5
|
+
import type { Logger } from './logger.ts';
|
|
6
|
+
import type { Registry } from './registry.ts';
|
|
7
|
+
export type DependencyOptional<T extends AnyInjectable = AnyInjectable> = {
|
|
8
|
+
[kOptionalDependency]: any;
|
|
9
|
+
injectable: T;
|
|
10
|
+
};
|
|
11
|
+
export type Depedency = DependencyOptional | AnyInjectable;
|
|
12
|
+
export type Dependencies = Record<string, Depedency>;
|
|
13
|
+
export type ResolveInjectableType<T extends AnyInjectable> = T extends Injectable<infer Type, any, any> ? Type : never;
|
|
14
|
+
export interface Dependant<Deps extends Dependencies = Dependencies> {
|
|
15
|
+
dependencies: Deps;
|
|
16
|
+
label?: string;
|
|
17
|
+
stack?: string;
|
|
18
|
+
}
|
|
19
|
+
export type DependencyInjectable<T extends Depedency> = T extends AnyInjectable ? T : T extends DependencyOptional ? T['injectable'] : never;
|
|
20
|
+
export type DependencyContext<Deps extends Dependencies> = {
|
|
21
|
+
readonly [K in keyof Deps as Deps[K] extends AnyInjectable ? K : never]: Deps[K] extends AnyInjectable ? ResolveInjectableType<Deps[K]> : never;
|
|
22
|
+
} & {
|
|
23
|
+
readonly [K in keyof Deps as Deps[K] extends DependencyOptional ? K : never]?: Deps[K] extends DependencyOptional ? ResolveInjectableType<Deps[K]['injectable']> : never;
|
|
24
|
+
};
|
|
25
|
+
export type InjectableFactoryType<InjectableType, InjectableDeps extends Dependencies> = (context: DependencyContext<InjectableDeps>) => Async<InjectableType>;
|
|
26
|
+
export type InjectablePickType<Input, Output> = (injectable: Input) => Output;
|
|
27
|
+
export type InjectableDisposeType<InjectableType, InjectableDeps extends Dependencies> = (instance: InjectableType, context: DependencyContext<InjectableDeps>) => any;
|
|
28
|
+
export interface LazyInjectable<T, S extends Scope = Scope.Global> extends Dependant<{}> {
|
|
29
|
+
scope: S;
|
|
30
|
+
[kInjectable]: any;
|
|
31
|
+
[kLazyInjectable]: T;
|
|
32
|
+
}
|
|
33
|
+
export interface ValueInjectable<T> extends Dependant<{}> {
|
|
34
|
+
scope: Scope.Global;
|
|
35
|
+
value: T;
|
|
36
|
+
[kInjectable]: any;
|
|
37
|
+
[kValueInjectable]: any;
|
|
38
|
+
}
|
|
39
|
+
export interface FactoryInjectable<T, D extends Dependencies = {}, S extends Scope = Scope.Global, P = T> extends Dependant<D> {
|
|
40
|
+
scope: S;
|
|
41
|
+
factory: InjectableFactoryType<P, D>;
|
|
42
|
+
pick: InjectablePickType<P, T>;
|
|
43
|
+
dispose?: InjectableDisposeType<P, D>;
|
|
44
|
+
[kInjectable]: any;
|
|
45
|
+
[kFactoryInjectable]: any;
|
|
46
|
+
}
|
|
47
|
+
export interface BaseClassInjectable<T, D extends Dependencies = {}, S extends Scope = Scope.Global> extends Dependant<D> {
|
|
48
|
+
new (...args: any[]): T;
|
|
49
|
+
scope: S;
|
|
50
|
+
[kInjectable]: any;
|
|
51
|
+
[kClassInjectable]: any;
|
|
52
|
+
}
|
|
53
|
+
export interface ClassInjectable<T, D extends Dependencies = {}, S extends Scope = Scope.Global, A extends any[] = []> extends Dependant<D> {
|
|
54
|
+
new ($context: DependencyContext<D>, ...args: A): T & {
|
|
55
|
+
$context: DependencyContext<D>;
|
|
56
|
+
};
|
|
57
|
+
scope: S;
|
|
58
|
+
[kInjectable]: any;
|
|
59
|
+
[kClassInjectable]: any;
|
|
60
|
+
}
|
|
61
|
+
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>;
|
|
62
|
+
export type AnyInjectable<T = any, S extends Scope = Scope> = Injectable<T, any, S>;
|
|
63
|
+
export declare const isLazyInjectable: (injectable: any) => injectable is LazyInjectable<any>;
|
|
64
|
+
export declare const isFactoryInjectable: (injectable: any) => injectable is FactoryInjectable<any>;
|
|
65
|
+
export declare const isClassInjectable: (injectable: any) => injectable is ClassInjectable<any>;
|
|
66
|
+
export declare const isValueInjectable: (injectable: any) => injectable is ValueInjectable<any>;
|
|
67
|
+
export declare const isInjectable: (injectable: any) => injectable is AnyInjectable<any>;
|
|
68
|
+
export declare const isOptionalInjectable: (injectable: any) => injectable is DependencyOptional<any>;
|
|
69
|
+
export declare function getInjectableScope(injectable: AnyInjectable): Scope;
|
|
70
|
+
export declare function getDepedencencyInjectable(dependency: Depedency): AnyInjectable;
|
|
71
|
+
export declare function createOptionalInjectable<T extends AnyInjectable>(injectable: T): DependencyOptional<T>;
|
|
72
|
+
export declare function createLazyInjectable<T, S extends Scope = Scope.Global>(scope?: S, label?: string, stackTraceDepth?: number): LazyInjectable<T, S>;
|
|
73
|
+
export declare function createValueInjectable<T>(value: T, label?: string, stackTraceDepth?: number): ValueInjectable<T>;
|
|
74
|
+
export declare function createFactoryInjectable<T, D extends Dependencies = {}, S extends Scope = Scope.Global, P = T>(paramsOrFactory: {
|
|
75
|
+
dependencies?: D;
|
|
76
|
+
scope?: S;
|
|
77
|
+
pick?: InjectablePickType<P, T>;
|
|
78
|
+
factory: InjectableFactoryType<P, D>;
|
|
79
|
+
dispose?: InjectableDisposeType<P, D>;
|
|
80
|
+
} | InjectableFactoryType<P, D>, label?: string, stackTraceDepth?: number): FactoryInjectable<null extends T ? P : T, D, S, P>;
|
|
81
|
+
export declare const createClassInjectable: <D extends Dependencies = {}, S extends Scope = Scope.Global>(dependencies?: D, scope?: S, stackTraceDepth?: number) => ClassInjectable<ClassInstance<{
|
|
82
|
+
new ($context: DependencyContext<D>): {
|
|
83
|
+
$context: DependencyContext<D>;
|
|
84
|
+
$onCreate(): Promise<void>;
|
|
85
|
+
$onDispose(): Promise<void>;
|
|
86
|
+
};
|
|
87
|
+
dependencies: D;
|
|
88
|
+
scope: S;
|
|
89
|
+
stack: string | undefined;
|
|
90
|
+
get label(): string;
|
|
91
|
+
[kInjectable]: boolean;
|
|
92
|
+
[kClassInjectable]: boolean;
|
|
93
|
+
}>, D, S>;
|
|
94
|
+
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, []>>;
|
|
95
|
+
export type DependenciesSubstitution<T extends Dependencies> = {
|
|
96
|
+
[K in keyof T]?: T[K] extends AnyInjectable<infer Type> ? AnyInjectable<Type> | DependenciesSubstitution<T[K]['dependencies']> : never;
|
|
97
|
+
};
|
|
98
|
+
export declare function substitute<T extends FactoryInjectable<any, any, Scope> | BaseClassInjectable<any, any, Scope>>(injectable: T, substitution: DependenciesSubstitution<T['dependencies']>, stackTraceDepth?: number): T;
|
|
99
|
+
export declare function compareScope(left: Scope, operator: '>' | '<' | '>=' | '<=' | '=' | '!=', right: Scope): boolean;
|
|
100
|
+
export declare const CoreInjectables: {
|
|
101
|
+
logger: LazyInjectable<Logger, Scope.Global>;
|
|
102
|
+
registry: LazyInjectable<Registry, Scope.Global>;
|
|
103
|
+
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>>) & {
|
|
104
|
+
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>> & {
|
|
105
|
+
[Symbol.asyncDispose]: () => Promise<void>;
|
|
106
|
+
}>;
|
|
107
|
+
}, Scope.Global>;
|
|
108
|
+
dispose: LazyInjectable<(<T extends AnyInjectable>(injectable: T, instance?: any) => Promise<void>), Scope.Global>;
|
|
109
|
+
hook: FactoryInjectable<(<T extends Hook>(name: T, callback: HookType[T]) => () => void), {
|
|
110
|
+
registry: LazyInjectable<Registry, Scope.Global>;
|
|
111
|
+
}, Scope.Transient, {
|
|
112
|
+
hooks: Hooks;
|
|
113
|
+
on: <T extends Hook>(name: T, callback: HookType[T]) => () => void;
|
|
114
|
+
}>;
|
|
115
|
+
};
|