@nmtjs/core 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +7 -0
- package/README.md +9 -0
- package/dist/index.js +11 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/constants.js +9 -0
- package/dist/lib/constants.js.map +1 -0
- package/dist/lib/container.js +275 -0
- package/dist/lib/container.js.map +1 -0
- package/dist/lib/enums.js +20 -0
- package/dist/lib/enums.js.map +1 -0
- package/dist/lib/hooks.js +37 -0
- package/dist/lib/hooks.js.map +1 -0
- package/dist/lib/injectables.js +6 -0
- package/dist/lib/injectables.js.map +1 -0
- package/dist/lib/logger.js +72 -0
- package/dist/lib/logger.js.map +1 -0
- package/dist/lib/plugin.js +6 -0
- package/dist/lib/plugin.js.map +1 -0
- package/dist/lib/registry.js +25 -0
- package/dist/lib/registry.js.map +1 -0
- package/dist/lib/types.js +1 -0
- package/dist/lib/types.js.map +1 -0
- package/dist/lib/utils/functions.js +32 -0
- package/dist/lib/utils/functions.js.map +1 -0
- package/dist/lib/utils/index.js +3 -0
- package/dist/lib/utils/index.js.map +1 -0
- package/dist/lib/utils/pool.js +100 -0
- package/dist/lib/utils/pool.js.map +1 -0
- package/dist/lib/utils/semaphore.js +52 -0
- package/dist/lib/utils/semaphore.js.map +1 -0
- package/index.ts +11 -0
- package/lib/constants.ts +36 -0
- package/lib/container.ts +482 -0
- package/lib/enums.ts +19 -0
- package/lib/hooks.ts +70 -0
- package/lib/injectables.ts +7 -0
- package/lib/logger.ts +99 -0
- package/lib/plugin.ts +25 -0
- package/lib/registry.ts +44 -0
- package/lib/types.ts +13 -0
- package/lib/utils/functions.ts +31 -0
- package/lib/utils/index.ts +3 -0
- package/lib/utils/pool.ts +111 -0
- package/lib/utils/semaphore.ts +63 -0
- package/package.json +33 -0
- package/tsconfig.json +3 -0
package/lib/container.ts
ADDED
|
@@ -0,0 +1,482 @@
|
|
|
1
|
+
import type { Async } from '@nmtjs/common'
|
|
2
|
+
import { tryCaptureStackTrace } from '@nmtjs/common'
|
|
3
|
+
import {
|
|
4
|
+
kFactoryInjectable,
|
|
5
|
+
kInjectable,
|
|
6
|
+
kLazyInjectable,
|
|
7
|
+
kOptionalDependency,
|
|
8
|
+
kValueInjectable,
|
|
9
|
+
} from './constants.ts'
|
|
10
|
+
import { Scope } from './enums.ts'
|
|
11
|
+
import type { Logger } from './logger.ts'
|
|
12
|
+
import type { Registry } from './registry.ts'
|
|
13
|
+
|
|
14
|
+
const ScopeStrictness = {
|
|
15
|
+
[Scope.Global]: 0,
|
|
16
|
+
[Scope.Connection]: 1,
|
|
17
|
+
[Scope.Call]: 2,
|
|
18
|
+
[Scope.Transient]: 3,
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type DependencyOptional<T extends AnyInjectable = AnyInjectable> = {
|
|
22
|
+
[kOptionalDependency]: any
|
|
23
|
+
injectable: T
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export type Depedency = DependencyOptional | AnyInjectable
|
|
27
|
+
|
|
28
|
+
export type Dependencies = Record<string, Depedency>
|
|
29
|
+
|
|
30
|
+
export type ResolveInjectableType<T extends AnyInjectable> =
|
|
31
|
+
T extends Injectable<infer Type, any, any> ? Type : never
|
|
32
|
+
|
|
33
|
+
export interface Dependant<Deps extends Dependencies = Dependencies> {
|
|
34
|
+
dependencies: Deps
|
|
35
|
+
label?: string
|
|
36
|
+
stack?: string
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export type DependencyInjectable<T extends Depedency> = T extends AnyInjectable
|
|
40
|
+
? T
|
|
41
|
+
: T extends DependencyOptional
|
|
42
|
+
? T['injectable']
|
|
43
|
+
: never
|
|
44
|
+
|
|
45
|
+
export type DependencyContext<Deps extends Dependencies> = {
|
|
46
|
+
readonly [K in keyof Deps as Deps[K] extends AnyInjectable
|
|
47
|
+
? K
|
|
48
|
+
: never]: Deps[K] extends AnyInjectable
|
|
49
|
+
? ResolveInjectableType<Deps[K]>
|
|
50
|
+
: never
|
|
51
|
+
} & {
|
|
52
|
+
readonly [K in keyof Deps as Deps[K] extends DependencyOptional
|
|
53
|
+
? K
|
|
54
|
+
: never]?: Deps[K] extends DependencyOptional
|
|
55
|
+
? ResolveInjectableType<Deps[K]['injectable']>
|
|
56
|
+
: never
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export type InjectableFactoryType<
|
|
60
|
+
InjectableType,
|
|
61
|
+
InjectableDeps extends Dependencies,
|
|
62
|
+
> = (context: DependencyContext<InjectableDeps>) => Async<InjectableType>
|
|
63
|
+
|
|
64
|
+
export type InjectableDisposeType<
|
|
65
|
+
InjectableType,
|
|
66
|
+
InjectableDeps extends Dependencies,
|
|
67
|
+
> = (
|
|
68
|
+
instance: InjectableType,
|
|
69
|
+
context: DependencyContext<InjectableDeps>,
|
|
70
|
+
) => any
|
|
71
|
+
|
|
72
|
+
export interface LazyInjectable<T, S extends Scope = Scope.Global>
|
|
73
|
+
extends Dependant<{}> {
|
|
74
|
+
scope: S
|
|
75
|
+
[kInjectable]: any
|
|
76
|
+
[kLazyInjectable]: T
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export interface ValueInjectable<T> extends Dependant<{}> {
|
|
80
|
+
scope: Scope.Global
|
|
81
|
+
value: T
|
|
82
|
+
[kInjectable]: any
|
|
83
|
+
[kValueInjectable]: any
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export interface FactoryInjectable<
|
|
87
|
+
T,
|
|
88
|
+
D extends Dependencies = {},
|
|
89
|
+
S extends Scope = Scope.Global,
|
|
90
|
+
> extends Dependant<D> {
|
|
91
|
+
scope: S
|
|
92
|
+
factory(context: DependencyContext<D>): Async<T>
|
|
93
|
+
dispose?(instance: T, context: DependencyContext<D>): any
|
|
94
|
+
[kInjectable]: any
|
|
95
|
+
[kFactoryInjectable]: any
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export type Injectable<
|
|
99
|
+
InjectableValue = any,
|
|
100
|
+
InjectableDeps extends Dependencies = {},
|
|
101
|
+
InjectableScope extends Scope = Scope,
|
|
102
|
+
> =
|
|
103
|
+
| LazyInjectable<InjectableValue, InjectableScope>
|
|
104
|
+
| ValueInjectable<InjectableValue>
|
|
105
|
+
| FactoryInjectable<InjectableValue, InjectableDeps, InjectableScope>
|
|
106
|
+
|
|
107
|
+
export type AnyInjectable<T = any, S extends Scope = Scope> = Injectable<
|
|
108
|
+
T,
|
|
109
|
+
any,
|
|
110
|
+
S
|
|
111
|
+
>
|
|
112
|
+
|
|
113
|
+
export class Container {
|
|
114
|
+
readonly instances = new Map<AnyInjectable, { instance: any; context: any }>()
|
|
115
|
+
private readonly resolvers = new Map<AnyInjectable, Promise<any>>()
|
|
116
|
+
private readonly injectables = new Set<AnyInjectable>()
|
|
117
|
+
private readonly dependants = new Map<AnyInjectable, Set<AnyInjectable>>()
|
|
118
|
+
|
|
119
|
+
constructor(
|
|
120
|
+
private readonly application: {
|
|
121
|
+
registry: Registry
|
|
122
|
+
logger: Logger
|
|
123
|
+
},
|
|
124
|
+
public readonly scope: Exclude<Scope, Scope.Transient> = Scope.Global,
|
|
125
|
+
private readonly parent?: Container,
|
|
126
|
+
) {
|
|
127
|
+
if ((scope as any) === Scope.Transient) {
|
|
128
|
+
throw new Error('Invalid scope')
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
async load() {
|
|
133
|
+
const traverse = (dependencies: Dependencies) => {
|
|
134
|
+
for (const key in dependencies) {
|
|
135
|
+
const dependency = dependencies[key]
|
|
136
|
+
const injectable = getDepedencencyInjectable(dependency)
|
|
137
|
+
this.injectables.add(injectable)
|
|
138
|
+
traverse(injectable.dependencies)
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
for (const dependant of this.application.registry.getDependants()) {
|
|
143
|
+
traverse(dependant.dependencies)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const injectables = Array.from(this.findCurrentScopeInjectables())
|
|
147
|
+
await Promise.all(injectables.map((injectable) => this.resolve(injectable)))
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
fork(scope: Exclude<Scope, Scope.Transient>) {
|
|
151
|
+
return new Container(this.application, scope, this)
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
async dispose() {
|
|
155
|
+
this.application.logger.trace('Disposing [%s] scope context...', this.scope)
|
|
156
|
+
|
|
157
|
+
// Loop through all instances and dispose them,
|
|
158
|
+
// until there are no more instances left
|
|
159
|
+
while (this.instances.size) {
|
|
160
|
+
for (const injectable of this.instances.keys()) {
|
|
161
|
+
const dependants = this.dependants.get(injectable)
|
|
162
|
+
// Firstly, dispose instances that other injectables don't depend on
|
|
163
|
+
if (!dependants?.size) {
|
|
164
|
+
await this.disposeInjectable(injectable)
|
|
165
|
+
for (const dependants of this.dependants.values()) {
|
|
166
|
+
// Clear current istances as a dependant for other injectables
|
|
167
|
+
if (dependants.has(injectable)) {
|
|
168
|
+
dependants.delete(injectable)
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
this.instances.clear()
|
|
176
|
+
this.injectables.clear()
|
|
177
|
+
this.resolvers.clear()
|
|
178
|
+
this.dependants.clear()
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
containsWithinSelf(injectable: AnyInjectable) {
|
|
182
|
+
return this.instances.has(injectable) || this.resolvers.has(injectable)
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
contains(injectable: AnyInjectable): boolean {
|
|
186
|
+
return (
|
|
187
|
+
this.containsWithinSelf(injectable) ||
|
|
188
|
+
(this.parent?.contains(injectable) ?? false)
|
|
189
|
+
)
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
get<T extends AnyInjectable>(injectable: T): ResolveInjectableType<T> {
|
|
193
|
+
if (this.instances.has(injectable)) {
|
|
194
|
+
return this.instances.get(injectable)!.instance
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (this.parent?.contains(injectable)) {
|
|
198
|
+
return this.parent.get(injectable)
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
throw new Error('No instance found')
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
resolve<T extends AnyInjectable>(injectable: T) {
|
|
205
|
+
return this.resolveInjectable(injectable)
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
async createContext<T extends Dependencies>(dependencies: T) {
|
|
209
|
+
return this.createInjectableContext(dependencies)
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
private async createInjectableContext<T extends Dependencies>(
|
|
213
|
+
dependencies: T,
|
|
214
|
+
dependant?: AnyInjectable,
|
|
215
|
+
) {
|
|
216
|
+
const injections: Record<string, any> = {}
|
|
217
|
+
const deps = Object.entries(dependencies)
|
|
218
|
+
const resolvers: Promise<any>[] = Array(deps.length)
|
|
219
|
+
for (let i = 0; i < deps.length; i++) {
|
|
220
|
+
const [key, dependency] = deps[i]
|
|
221
|
+
const injectable = getDepedencencyInjectable(dependency)
|
|
222
|
+
const resolver = this.resolveInjectable(injectable, dependant)
|
|
223
|
+
resolvers[i] = resolver.then((value) => (injections[key] = value))
|
|
224
|
+
}
|
|
225
|
+
await Promise.all(resolvers)
|
|
226
|
+
return Object.freeze(injections) as DependencyContext<T>
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
async provide<T extends AnyInjectable>(
|
|
230
|
+
injectable: T,
|
|
231
|
+
instance: ResolveInjectableType<T>,
|
|
232
|
+
) {
|
|
233
|
+
if (compareScope(injectable.scope, '>', this.scope)) {
|
|
234
|
+
throw new Error('Invalid scope') // TODO: more informative error
|
|
235
|
+
}
|
|
236
|
+
this.instances.set(injectable, { instance, context: undefined })
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
satisfies(injectable: AnyInjectable) {
|
|
240
|
+
return compareScope(injectable.scope, '<=', this.scope)
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
private *findCurrentScopeInjectables() {
|
|
244
|
+
for (const injectable of this.injectables) {
|
|
245
|
+
if (injectable.scope === this.scope) {
|
|
246
|
+
yield injectable
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
private resolveInjectable<T extends AnyInjectable>(
|
|
252
|
+
injectable: T,
|
|
253
|
+
dependant?: AnyInjectable,
|
|
254
|
+
): Promise<ResolveInjectableType<T>> {
|
|
255
|
+
if (dependant && compareScope(dependant.scope, '<', injectable.scope)) {
|
|
256
|
+
throw new Error('Invalid scope: dependant is looser than injectable') // TODO: more informative error
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
if (checkIsValueInjectable(injectable)) {
|
|
260
|
+
return Promise.resolve(injectable.value)
|
|
261
|
+
} else if (
|
|
262
|
+
this.parent?.contains(injectable) ||
|
|
263
|
+
(this.parent?.satisfies(injectable) &&
|
|
264
|
+
compareScope(this.parent.scope, '<', this.scope))
|
|
265
|
+
) {
|
|
266
|
+
return this.parent.resolveInjectable(injectable, dependant)
|
|
267
|
+
} else {
|
|
268
|
+
const { scope, dependencies, stack, label } = injectable
|
|
269
|
+
|
|
270
|
+
if (dependant && compareScope(scope, '=', dependant.scope)) {
|
|
271
|
+
let dependants = this.dependants.get(injectable)
|
|
272
|
+
if (!dependants) {
|
|
273
|
+
this.dependants.set(injectable, (dependants = new Set()))
|
|
274
|
+
}
|
|
275
|
+
dependants.add(dependant)
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
if (this.instances.has(injectable)) {
|
|
279
|
+
return Promise.resolve(this.instances.get(injectable)!.instance)
|
|
280
|
+
} else if (this.resolvers.has(injectable)) {
|
|
281
|
+
return this.resolvers.get(injectable)!
|
|
282
|
+
} else {
|
|
283
|
+
const isLazy = checkIsLazyInjectable(injectable)
|
|
284
|
+
|
|
285
|
+
if (isLazy) {
|
|
286
|
+
const isOptional = checkIsOptional(injectable)
|
|
287
|
+
if (isOptional) return Promise.resolve(undefined as any)
|
|
288
|
+
return Promise.reject(
|
|
289
|
+
new Error(
|
|
290
|
+
`No instance provided for ${label || 'an'} injectable:\n${stack}`,
|
|
291
|
+
),
|
|
292
|
+
)
|
|
293
|
+
} else if (!checkIsFactoryInjectable(injectable)) {
|
|
294
|
+
throw new Error('Invalid injectable')
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
const resolution = this.createInjectableContext(
|
|
298
|
+
dependencies,
|
|
299
|
+
injectable,
|
|
300
|
+
)
|
|
301
|
+
.then((context) =>
|
|
302
|
+
Promise.resolve(injectable.factory(context)).then((instance) => ({
|
|
303
|
+
instance,
|
|
304
|
+
context,
|
|
305
|
+
})),
|
|
306
|
+
)
|
|
307
|
+
.then(({ instance, context }) => {
|
|
308
|
+
if (compareScope(this.scope, '>=', scope))
|
|
309
|
+
this.instances.set(injectable, { instance, context })
|
|
310
|
+
if (scope !== Scope.Transient) this.resolvers.delete(injectable)
|
|
311
|
+
return instance
|
|
312
|
+
})
|
|
313
|
+
if (scope !== Scope.Transient)
|
|
314
|
+
this.resolvers.set(injectable, resolution)
|
|
315
|
+
return resolution
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
private async disposeInjectable(injectable: AnyInjectable) {
|
|
321
|
+
try {
|
|
322
|
+
if (kFactoryInjectable in injectable) {
|
|
323
|
+
const { dispose } = injectable
|
|
324
|
+
if (dispose) {
|
|
325
|
+
const { instance, context } = this.instances.get(injectable)!
|
|
326
|
+
await dispose(instance, context)
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
} catch (cause) {
|
|
330
|
+
const error = new Error(
|
|
331
|
+
'Injectable disposal error. Potential memory leak',
|
|
332
|
+
{ cause },
|
|
333
|
+
)
|
|
334
|
+
this.application.logger.error(error)
|
|
335
|
+
} finally {
|
|
336
|
+
this.instances.delete(injectable)
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
function compareScope(
|
|
342
|
+
left: Scope,
|
|
343
|
+
operator: '>' | '<' | '>=' | '<=' | '=' | '!=',
|
|
344
|
+
right: Scope,
|
|
345
|
+
) {
|
|
346
|
+
const leftScope = ScopeStrictness[left]
|
|
347
|
+
const rightScope = ScopeStrictness[right]
|
|
348
|
+
switch (operator) {
|
|
349
|
+
case '=':
|
|
350
|
+
return leftScope === rightScope
|
|
351
|
+
case '!=':
|
|
352
|
+
return leftScope !== rightScope
|
|
353
|
+
case '>':
|
|
354
|
+
return leftScope > rightScope
|
|
355
|
+
case '<':
|
|
356
|
+
return leftScope < rightScope
|
|
357
|
+
case '>=':
|
|
358
|
+
return leftScope >= rightScope
|
|
359
|
+
case '<=':
|
|
360
|
+
return leftScope <= rightScope
|
|
361
|
+
default:
|
|
362
|
+
throw new Error('Invalid operator')
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
export const checkIsLazyInjectable = (
|
|
367
|
+
injectable: any,
|
|
368
|
+
): injectable is LazyInjectable<any> => kLazyInjectable in injectable
|
|
369
|
+
export const checkIsFactoryInjectable = (
|
|
370
|
+
injectable: any,
|
|
371
|
+
): injectable is FactoryInjectable<any> => kFactoryInjectable in injectable
|
|
372
|
+
export const checkIsValueInjectable = (
|
|
373
|
+
injectable: any,
|
|
374
|
+
): injectable is ValueInjectable<any> => kValueInjectable in injectable
|
|
375
|
+
export const checkIsInjectable = (
|
|
376
|
+
injectable: any,
|
|
377
|
+
): injectable is AnyInjectable<any> => kInjectable in injectable
|
|
378
|
+
export const checkIsOptional = (
|
|
379
|
+
injectable: any,
|
|
380
|
+
): injectable is DependencyOptional<any> => kOptionalDependency in injectable
|
|
381
|
+
|
|
382
|
+
export function getInjectableScope(injectable: AnyInjectable) {
|
|
383
|
+
let scope = injectable.scope
|
|
384
|
+
const deps = Object.values(injectable.dependencies as Dependencies)
|
|
385
|
+
for (const dependency of deps) {
|
|
386
|
+
const injectable = getDepedencencyInjectable(dependency)
|
|
387
|
+
const dependencyScope = getInjectableScope(injectable)
|
|
388
|
+
if (compareScope(dependencyScope, '>', scope)) {
|
|
389
|
+
scope = dependencyScope
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
return scope
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
export function getDepedencencyInjectable(
|
|
396
|
+
dependency: Depedency,
|
|
397
|
+
): AnyInjectable {
|
|
398
|
+
if (kOptionalDependency in dependency) {
|
|
399
|
+
return dependency.injectable
|
|
400
|
+
}
|
|
401
|
+
return dependency
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
export function markOptional<T extends AnyInjectable>(injectable: T) {
|
|
405
|
+
return {
|
|
406
|
+
[kOptionalDependency]: true,
|
|
407
|
+
injectable,
|
|
408
|
+
} as DependencyOptional<T>
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
export function createLazyInjectable<T, S extends Scope = Scope.Global>(
|
|
412
|
+
scope = Scope.Global as S,
|
|
413
|
+
label?: string,
|
|
414
|
+
): LazyInjectable<T, S> {
|
|
415
|
+
return Object.freeze({
|
|
416
|
+
scope,
|
|
417
|
+
dependencies: {},
|
|
418
|
+
label,
|
|
419
|
+
stack: tryCaptureStackTrace(),
|
|
420
|
+
[kInjectable]: true,
|
|
421
|
+
[kLazyInjectable]: true as unknown as T,
|
|
422
|
+
})
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
export function createValueInjectable<T>(
|
|
426
|
+
value: T,
|
|
427
|
+
label?: string,
|
|
428
|
+
): ValueInjectable<T> {
|
|
429
|
+
return Object.freeze({
|
|
430
|
+
value,
|
|
431
|
+
scope: Scope.Global,
|
|
432
|
+
dependencies: {},
|
|
433
|
+
label,
|
|
434
|
+
stack: tryCaptureStackTrace(),
|
|
435
|
+
[kInjectable]: true,
|
|
436
|
+
[kValueInjectable]: true,
|
|
437
|
+
})
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
export function createFactoryInjectable<
|
|
441
|
+
T,
|
|
442
|
+
D extends Dependencies = {},
|
|
443
|
+
S extends Scope = Scope.Global,
|
|
444
|
+
>(
|
|
445
|
+
paramsOrFactory:
|
|
446
|
+
| {
|
|
447
|
+
dependencies?: D
|
|
448
|
+
scope?: S
|
|
449
|
+
factory: InjectableFactoryType<T, D>
|
|
450
|
+
dispose?: InjectableDisposeType<T, D>
|
|
451
|
+
}
|
|
452
|
+
| InjectableFactoryType<T, D>,
|
|
453
|
+
label?: string,
|
|
454
|
+
): FactoryInjectable<T, D, S> {
|
|
455
|
+
const isFactory = typeof paramsOrFactory === 'function'
|
|
456
|
+
const params = isFactory
|
|
457
|
+
? {
|
|
458
|
+
factory: paramsOrFactory,
|
|
459
|
+
}
|
|
460
|
+
: paramsOrFactory
|
|
461
|
+
const injectable = {
|
|
462
|
+
dependencies: (params.dependencies ?? {}) as D,
|
|
463
|
+
scope: (params.scope ?? Scope.Global) as S,
|
|
464
|
+
factory: params.factory,
|
|
465
|
+
dispose: params.dispose,
|
|
466
|
+
label,
|
|
467
|
+
stack: tryCaptureStackTrace(),
|
|
468
|
+
[kInjectable]: true,
|
|
469
|
+
[kFactoryInjectable]: true,
|
|
470
|
+
}
|
|
471
|
+
const actualScope = getInjectableScope(injectable)
|
|
472
|
+
if (
|
|
473
|
+
!isFactory &&
|
|
474
|
+
params.scope &&
|
|
475
|
+
ScopeStrictness[actualScope] > ScopeStrictness[params.scope]
|
|
476
|
+
)
|
|
477
|
+
throw new Error(
|
|
478
|
+
`Invalid scope ${params.scope} for factory injectable: dependencies have stricter scope - ${actualScope}`,
|
|
479
|
+
)
|
|
480
|
+
injectable.scope = actualScope as unknown as S
|
|
481
|
+
return Object.freeze(injectable)
|
|
482
|
+
}
|
package/lib/enums.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export enum Scope {
|
|
2
|
+
Global = 'Global',
|
|
3
|
+
Connection = 'Connection',
|
|
4
|
+
Call = 'Call',
|
|
5
|
+
Transient = 'Transient',
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export enum Hook {
|
|
9
|
+
BeforeInitialize = 'BeforeInitialize',
|
|
10
|
+
AfterInitialize = 'AfterInitialize',
|
|
11
|
+
BeforeStart = 'BeforeStart',
|
|
12
|
+
AfterStart = 'AfterStart',
|
|
13
|
+
BeforeStop = 'BeforeStop',
|
|
14
|
+
AfterStop = 'AfterStop',
|
|
15
|
+
BeforeTerminate = 'BeforeTerminate',
|
|
16
|
+
AfterTerminate = 'AfterTerminate',
|
|
17
|
+
OnConnect = 'OnConnect',
|
|
18
|
+
OnDisconnect = 'OnDisconnect',
|
|
19
|
+
}
|
package/lib/hooks.ts
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { Callback } from '@nmtjs/common'
|
|
2
|
+
import { kHookCollection } from './constants.ts'
|
|
3
|
+
import type { Hook } from './enums.ts'
|
|
4
|
+
// import type { HookType } from './types.ts'
|
|
5
|
+
|
|
6
|
+
export interface HookType {
|
|
7
|
+
[key: string]: (...args: any[]) => any
|
|
8
|
+
// [Hook.AfterInitialize]: () => any
|
|
9
|
+
// [Hook.BeforeStart]: () => any
|
|
10
|
+
// [Hook.AfterStart]: () => any
|
|
11
|
+
// [Hook.BeforeStop]: () => any
|
|
12
|
+
// [Hook.AfterStop]: () => any
|
|
13
|
+
// [Hook.BeforeTerminate]: () => any
|
|
14
|
+
// [Hook.AfterTerminate]: () => any
|
|
15
|
+
// [Hook.OnConnect]: (...args: any[]) => any
|
|
16
|
+
// [Hook.OnDisconnect]: (...args: any[]) => any
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export type CallHook<T extends string> = (
|
|
20
|
+
hook: T,
|
|
21
|
+
...args: T extends keyof HookType ? Parameters<HookType[T]> : any[]
|
|
22
|
+
) => Promise<void>
|
|
23
|
+
|
|
24
|
+
export class Hooks {
|
|
25
|
+
static merge(from: Hooks, to: Hooks) {
|
|
26
|
+
for (const [name, callbacks] of from[kHookCollection]) {
|
|
27
|
+
for (const callback of callbacks) {
|
|
28
|
+
to.add(name, callback)
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
[kHookCollection] = new Map<string, Set<Callback>>()
|
|
34
|
+
|
|
35
|
+
add(name: string, callback: Callback) {
|
|
36
|
+
let hooks = this[kHookCollection].get(name)
|
|
37
|
+
if (!hooks) this[kHookCollection].set(name, (hooks = new Set()))
|
|
38
|
+
hooks.add(callback)
|
|
39
|
+
return () => this.remove(name, callback)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
remove(name: string, callback: Callback) {
|
|
43
|
+
const hooks = this[kHookCollection].get(name)
|
|
44
|
+
if (hooks) hooks.delete(callback)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async call<T extends string | Hook>(
|
|
48
|
+
name: T,
|
|
49
|
+
options: { concurrent?: boolean; reverse?: boolean } | undefined,
|
|
50
|
+
...args: T extends Hook ? Parameters<HookType[T]> : any[]
|
|
51
|
+
) {
|
|
52
|
+
const { concurrent = true, reverse = false } = options ?? {}
|
|
53
|
+
const hooks = this[kHookCollection].get(name)
|
|
54
|
+
if (!hooks) return
|
|
55
|
+
const hooksArr = Array.from(hooks)
|
|
56
|
+
if (concurrent) {
|
|
57
|
+
await Promise.all(hooksArr.map((hook) => hook(...args)))
|
|
58
|
+
} else {
|
|
59
|
+
if (reverse) hooksArr.reverse()
|
|
60
|
+
for (const hook of hooksArr) await hook(...args)
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
clear() {
|
|
65
|
+
this[kHookCollection].clear()
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export const createErrForHook = (hook: Hook | (object & string)) =>
|
|
70
|
+
`Error during [${hook}] hook`
|
package/lib/logger.ts
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { threadId } from 'node:worker_threads'
|
|
2
|
+
import {
|
|
3
|
+
type DestinationStream,
|
|
4
|
+
type Level,
|
|
5
|
+
type Logger as PinoLogger,
|
|
6
|
+
type StreamEntry,
|
|
7
|
+
pino,
|
|
8
|
+
stdTimeFunctions,
|
|
9
|
+
} from 'pino'
|
|
10
|
+
import { build as pretty } from 'pino-pretty'
|
|
11
|
+
|
|
12
|
+
export type Logger = PinoLogger
|
|
13
|
+
export type LoggingOptions = {
|
|
14
|
+
destinations?: Array<DestinationStream | StreamEntry<Level>>
|
|
15
|
+
pinoOptions?: any
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// TODO: use node:util inspect
|
|
19
|
+
const bg = (value, color) => `\x1b[${color}m${value}\x1b[0m`
|
|
20
|
+
const fg = (value, color) => `\x1b[38;5;${color}m${value}\x1b[0m`
|
|
21
|
+
|
|
22
|
+
const levelColors = {
|
|
23
|
+
10: 100,
|
|
24
|
+
20: 102,
|
|
25
|
+
30: 106,
|
|
26
|
+
40: 104,
|
|
27
|
+
50: 101,
|
|
28
|
+
60: 105,
|
|
29
|
+
[Number.POSITIVE_INFINITY]: 0,
|
|
30
|
+
}
|
|
31
|
+
const messageColors = {
|
|
32
|
+
10: 0,
|
|
33
|
+
20: 2,
|
|
34
|
+
30: 6,
|
|
35
|
+
40: 4,
|
|
36
|
+
50: 1,
|
|
37
|
+
60: 5,
|
|
38
|
+
[Number.POSITIVE_INFINITY]: 0,
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const levelLabels = {
|
|
42
|
+
10: ' TRACE ',
|
|
43
|
+
20: ' DEBUG ',
|
|
44
|
+
30: ' INFO ',
|
|
45
|
+
40: ' WARN ',
|
|
46
|
+
50: ' ERROR ',
|
|
47
|
+
60: ' FATAL ',
|
|
48
|
+
[Number.POSITIVE_INFINITY]: 'SILENT',
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export const createLogger = (options: LoggingOptions = {}, $group: string) => {
|
|
52
|
+
let { destinations, pinoOptions } = options
|
|
53
|
+
|
|
54
|
+
if (!destinations || !destinations?.length) {
|
|
55
|
+
destinations = [createConsolePrettyDestination('info')]
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const lowestLevelValue = destinations!.reduce(
|
|
59
|
+
(acc, destination) =>
|
|
60
|
+
Math.min(
|
|
61
|
+
acc,
|
|
62
|
+
'stream' in destination
|
|
63
|
+
? pino.levels.values[destination.level!]
|
|
64
|
+
: Number.POSITIVE_INFINITY,
|
|
65
|
+
),
|
|
66
|
+
Number.POSITIVE_INFINITY,
|
|
67
|
+
)
|
|
68
|
+
const level = pino.levels.labels[lowestLevelValue]
|
|
69
|
+
return pino(
|
|
70
|
+
{
|
|
71
|
+
timestamp: stdTimeFunctions.isoTime,
|
|
72
|
+
...pinoOptions,
|
|
73
|
+
level,
|
|
74
|
+
},
|
|
75
|
+
pino.multistream(destinations!),
|
|
76
|
+
).child({ $group, $threadId: threadId })
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export const createConsolePrettyDestination = (
|
|
80
|
+
level: Level,
|
|
81
|
+
sync = true,
|
|
82
|
+
): StreamEntry => ({
|
|
83
|
+
level,
|
|
84
|
+
stream: pretty({
|
|
85
|
+
colorize: true,
|
|
86
|
+
ignore: 'hostname,$group,$threadId',
|
|
87
|
+
errorLikeObjectKeys: ['err', 'error', 'cause'],
|
|
88
|
+
messageFormat: (log, messageKey) => {
|
|
89
|
+
const group = fg(`[${log.$group}]`, 11)
|
|
90
|
+
const msg = fg(log[messageKey], messageColors[log.level as number])
|
|
91
|
+
const thread = fg(`(Thread-${log.$threadId})`, 89)
|
|
92
|
+
return `\x1b[0m${thread} ${group} ${msg}`
|
|
93
|
+
},
|
|
94
|
+
customPrettifiers: {
|
|
95
|
+
level: (level: any) => bg(levelLabels[level], levelColors[level]),
|
|
96
|
+
},
|
|
97
|
+
sync,
|
|
98
|
+
}),
|
|
99
|
+
})
|
package/lib/plugin.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Async } from '@nmtjs/common'
|
|
2
|
+
import { kPlugin } from './constants.ts'
|
|
3
|
+
import type { PluginContext } from './types.ts'
|
|
4
|
+
|
|
5
|
+
export interface BasePlugin<
|
|
6
|
+
Type = any,
|
|
7
|
+
Options = unknown,
|
|
8
|
+
Context extends PluginContext = PluginContext,
|
|
9
|
+
> {
|
|
10
|
+
name: string
|
|
11
|
+
init: (context: Context, options: Options) => Async<Type>
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface Plugin<
|
|
15
|
+
Type = void,
|
|
16
|
+
Options = unknown,
|
|
17
|
+
Context extends PluginContext = PluginContext,
|
|
18
|
+
> extends BasePlugin<Type, Options, Context> {
|
|
19
|
+
[kPlugin]: any
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const createPlugin = <Options = unknown, Type = void>(
|
|
23
|
+
name: string,
|
|
24
|
+
init: Plugin<Type, Options>['init'],
|
|
25
|
+
): Plugin<Type, Options> => ({ name, init, [kPlugin]: true })
|