@nmtjs/core 0.12.6 → 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/package.json CHANGED
@@ -11,23 +11,22 @@
11
11
  "peerDependencies": {
12
12
  "pino": "^9.6.0",
13
13
  "pino-pretty": "^13.0.0",
14
- "@nmtjs/common": "0.12.6",
15
- "@nmtjs/type": "0.12.6"
14
+ "@nmtjs/common": "0.12.7",
15
+ "@nmtjs/type": "0.12.7"
16
16
  },
17
17
  "devDependencies": {
18
18
  "@types/node": "^20",
19
19
  "pino": "^9.6.0",
20
20
  "pino-pretty": "^13.0.0",
21
- "@nmtjs/common": "0.12.6",
22
- "@nmtjs/type": "0.12.6"
21
+ "@nmtjs/common": "0.12.7",
22
+ "@nmtjs/type": "0.12.7"
23
23
  },
24
24
  "files": [
25
- "src",
26
25
  "dist",
27
26
  "LICENSE.md",
28
27
  "README.md"
29
28
  ],
30
- "version": "0.12.6",
29
+ "version": "0.12.7",
31
30
  "scripts": {
32
31
  "build": "tsc",
33
32
  "type-check": "tsc --noEmit"
package/src/constants.ts DELETED
@@ -1,41 +0,0 @@
1
- export const kOptionalDependency: unique symbol = Symbol.for(
2
- 'neemata:OptionalDependencyKey',
3
- )
4
- export type kOptionalDependency = typeof kOptionalDependency
5
-
6
- export const kInjectable: unique symbol = Symbol.for('neemata:InjectableKey')
7
- export type kInjectable = typeof kInjectable
8
-
9
- export const kLazyInjectable: unique symbol = Symbol.for(
10
- 'neemata:LazyInjectableKey',
11
- )
12
- export type kLazyInjectable = typeof kLazyInjectable
13
-
14
- export const kValueInjectable: unique symbol = Symbol.for(
15
- 'neemata:ValueInjectableKey',
16
- )
17
- export type kValueInjectable = typeof kValueInjectable
18
-
19
- export const kFactoryInjectable: unique symbol = Symbol.for(
20
- 'neemata:FactoryInjectableKey',
21
- )
22
- export type kFactoryInjectable = typeof kFactoryInjectable
23
-
24
- export const kClassInjectable: unique symbol = Symbol.for(
25
- 'neemata:ClassInjectableKey',
26
- )
27
- export type kClassInjectable = typeof kClassInjectable
28
-
29
- export const kProvider: unique symbol = Symbol.for('neemata:ProviderKey')
30
- export type kProvider = typeof kProvider
31
-
32
- export const kHookCollection: unique symbol = Symbol.for(
33
- 'neemata:HookCollectionKey',
34
- )
35
- export type kHookCollection = typeof kHookCollection
36
-
37
- export const kPlugin: unique symbol = Symbol.for('neemata:PluginKey')
38
- export type kPlugin = typeof kPlugin
39
-
40
- export const kMetadata: unique symbol = Symbol.for('neemata:MetadataKey')
41
- export type kMetadata = typeof kMetadata
package/src/container.ts DELETED
@@ -1,440 +0,0 @@
1
- import assert from 'node:assert'
2
- import { tryCaptureStackTrace } from '@nmtjs/common'
3
- import { Scope } from './enums.ts'
4
- import {
5
- type AnyInjectable,
6
- CoreInjectables,
7
- compareScope,
8
- createExtendableClassInjectable,
9
- createValueInjectable,
10
- type Dependencies,
11
- type DependencyContext,
12
- getDepedencencyInjectable,
13
- isClassInjectable,
14
- isFactoryInjectable,
15
- isInjectable,
16
- isLazyInjectable,
17
- isOptionalInjectable,
18
- isValueInjectable,
19
- type ResolveInjectableType,
20
- } from './injectables.ts'
21
- import type { Logger } from './logger.ts'
22
- import type { Registry } from './registry.ts'
23
-
24
- type InstanceWrapper = { private: any; public: any; context: any }
25
-
26
- type ContainerOptions = {
27
- registry: Registry
28
- logger: Logger
29
- }
30
-
31
- export class Container {
32
- readonly instances = new Map<AnyInjectable, InstanceWrapper[]>()
33
- private readonly resolvers = new Map<AnyInjectable, Promise<any>>()
34
- private readonly injectables = new Set<AnyInjectable>()
35
- private readonly dependants = new Map<AnyInjectable, Set<AnyInjectable>>()
36
- // private readonly transients = new Map<any, any>()
37
- private disposing = false
38
-
39
- constructor(
40
- private readonly application: ContainerOptions,
41
- public readonly scope: Exclude<Scope, Scope.Transient> = Scope.Global,
42
- private readonly parent?: Container,
43
- ) {
44
- if ((scope as any) === Scope.Transient) {
45
- throw new Error('Invalid scope')
46
- }
47
- this.provide(CoreInjectables.inject, this.createInjectFunction())
48
- this.provide(CoreInjectables.dispose, this.createDisposeFunction())
49
- this.provide(CoreInjectables.registry, application.registry)
50
- }
51
-
52
- async load() {
53
- const traverse = (dependencies: Dependencies) => {
54
- for (const key in dependencies) {
55
- const dependency = dependencies[key]
56
- const injectable = getDepedencencyInjectable(dependency)
57
- this.injectables.add(injectable)
58
- traverse(injectable.dependencies)
59
- }
60
- }
61
-
62
- for (const dependant of this.application.registry.getDependants()) {
63
- traverse(dependant.dependencies)
64
- }
65
-
66
- const injectables = Array.from(this.findCurrentScopeInjectables())
67
- await Promise.all(injectables.map((injectable) => this.resolve(injectable)))
68
- }
69
-
70
- fork(scope: Exclude<Scope, Scope.Transient>) {
71
- return new Container(this.application, scope, this)
72
- }
73
-
74
- async dispose() {
75
- this.application.logger.trace('Disposing [%s] scope context...', this.scope)
76
-
77
- // Prevent new resolutions during disposal
78
- this.disposing = true
79
-
80
- // Get proper disposal order using topological sort
81
- const disposalOrder = this.getDisposalOrder()
82
-
83
- // Dispose in the correct order
84
- for (const injectable of disposalOrder) {
85
- if (this.instances.has(injectable)) {
86
- await this.disposeInjectableInstances(injectable)
87
- }
88
- }
89
-
90
- this.instances.clear()
91
- this.injectables.clear()
92
- this.resolvers.clear()
93
- this.dependants.clear()
94
-
95
- this.disposing = false
96
- }
97
-
98
- containsWithinSelf(injectable: AnyInjectable) {
99
- return this.instances.has(injectable) || this.resolvers.has(injectable)
100
- }
101
-
102
- contains(injectable: AnyInjectable): boolean {
103
- return (
104
- this.containsWithinSelf(injectable) ||
105
- (this.parent?.contains(injectable) ?? false)
106
- )
107
- }
108
-
109
- get<T extends AnyInjectable>(injectable: T): ResolveInjectableType<T> {
110
- if (injectable.scope === Scope.Transient) {
111
- throw new Error('Cannot get transient injectable directly')
112
- }
113
-
114
- if (this.instances.has(injectable)) {
115
- return this.instances.get(injectable)!.at(0)!.public
116
- }
117
-
118
- if (this.parent?.contains(injectable)) {
119
- return this.parent.get(injectable)
120
- }
121
-
122
- throw new Error('No instance found')
123
- }
124
-
125
- resolve<T extends AnyInjectable>(injectable: T) {
126
- return this.resolveInjectable(injectable)
127
- }
128
-
129
- async createContext<T extends Dependencies>(dependencies: T) {
130
- return this.createInjectableContext(dependencies)
131
- }
132
-
133
- private async createInjectableContext<T extends Dependencies>(
134
- dependencies: T,
135
- dependant?: AnyInjectable,
136
- ) {
137
- const injections: Record<string, any> = {}
138
- const deps = Object.entries(dependencies)
139
- const resolvers: Promise<any>[] = Array(deps.length)
140
- for (let i = 0; i < deps.length; i++) {
141
- const [key, dependency] = deps[i]
142
- const injectable = getDepedencencyInjectable(dependency)
143
- const resolver = this.resolveInjectable(injectable, dependant)
144
- resolvers[i] = resolver.then((value) => (injections[key] = value))
145
- }
146
- await Promise.all(resolvers)
147
- return Object.freeze(injections) as DependencyContext<T>
148
- }
149
-
150
- async provide<T extends AnyInjectable>(
151
- injectable: T,
152
- instance: ResolveInjectableType<T>,
153
- ) {
154
- if (compareScope(injectable.scope, '>', this.scope)) {
155
- throw new Error('Invalid scope') // TODO: more informative error
156
- }
157
-
158
- this.instances.set(injectable, [
159
- {
160
- private: instance,
161
- public: instance,
162
- context: undefined,
163
- },
164
- ])
165
- }
166
-
167
- satisfies(injectable: AnyInjectable) {
168
- return compareScope(injectable.scope, '<=', this.scope)
169
- }
170
-
171
- private *findCurrentScopeInjectables() {
172
- for (const injectable of this.injectables) {
173
- if (injectable.scope === this.scope) {
174
- yield injectable
175
- }
176
- }
177
- }
178
-
179
- private resolveInjectable<T extends AnyInjectable>(
180
- injectable: T,
181
- dependant?: AnyInjectable,
182
- ): Promise<ResolveInjectableType<T>> {
183
- if (this.disposing) {
184
- return Promise.reject(new Error('Cannot resolve during disposal'))
185
- }
186
-
187
- if (dependant && compareScope(dependant.scope, '<', injectable.scope)) {
188
- // TODO: more informative error
189
- return Promise.reject(
190
- new Error('Invalid scope: dependant is looser than injectable'),
191
- )
192
- }
193
-
194
- if (isValueInjectable(injectable)) {
195
- return Promise.resolve(injectable.value)
196
- } else if (
197
- this.parent?.contains(injectable) ||
198
- (this.parent?.satisfies(injectable) &&
199
- compareScope(this.parent.scope, '<', this.scope))
200
- ) {
201
- return this.parent.resolveInjectable(injectable, dependant)
202
- } else {
203
- const { stack, label } = injectable
204
-
205
- if (dependant) {
206
- let dependants = this.dependants.get(injectable)
207
- if (!dependants) {
208
- this.dependants.set(injectable, (dependants = new Set()))
209
- }
210
- dependants.add(dependant)
211
- }
212
-
213
- const isTransient = injectable.scope === Scope.Transient
214
-
215
- if (!isTransient && this.instances.has(injectable)) {
216
- return Promise.resolve(this.instances.get(injectable)!.at(0)!.public)
217
- } else if (!isTransient && this.resolvers.has(injectable)) {
218
- return this.resolvers.get(injectable)!
219
- } else {
220
- const isLazy = isLazyInjectable(injectable)
221
-
222
- if (isLazy) {
223
- const isOptional = isOptionalInjectable(injectable)
224
- if (isOptional) return Promise.resolve(undefined as any)
225
- return Promise.reject(
226
- new Error(
227
- `No instance provided for ${label || 'an'} injectable:\n${stack}`,
228
- ),
229
- )
230
- } else {
231
- const resolution = this.createResolution(injectable).finally(() => {
232
- this.resolvers.delete(injectable)
233
- })
234
- if (injectable.scope !== Scope.Transient) {
235
- this.resolvers.set(injectable, resolution)
236
- }
237
- return resolution
238
- }
239
- }
240
- }
241
- }
242
-
243
- private async createResolution<T extends AnyInjectable>(
244
- injectable: T,
245
- ): Promise<ResolveInjectableType<T>> {
246
- const { dependencies } = injectable
247
- const context = await this.createInjectableContext(dependencies, injectable)
248
- const wrapper = {
249
- private: null as any,
250
- public: null as ResolveInjectableType<T>,
251
- context,
252
- }
253
- if (isFactoryInjectable(injectable)) {
254
- wrapper.private = await Promise.resolve(
255
- injectable.factory(wrapper.context),
256
- )
257
- wrapper.public = injectable.pick(wrapper.private)
258
- } else if (isClassInjectable(injectable)) {
259
- wrapper.private = new injectable(context)
260
- wrapper.public = wrapper.private
261
- await wrapper.private.$onCreate()
262
- } else {
263
- throw new Error('Invalid injectable type')
264
- }
265
-
266
- let instances = this.instances.get(injectable)
267
-
268
- if (!instances) {
269
- instances = []
270
- this.instances.set(injectable, instances)
271
- }
272
- instances.push(wrapper)
273
-
274
- return wrapper.public
275
- }
276
-
277
- private createInjectFunction() {
278
- const inject = <T extends AnyInjectable>(
279
- injectable: T,
280
- context: InlineInjectionDependencies<T>,
281
- ) => {
282
- const dependencies: Dependencies = {
283
- ...injectable.dependencies,
284
- }
285
-
286
- for (const key in context) {
287
- const dep = context[key]
288
- if (isInjectable(dep) || isOptionalInjectable(dep)) {
289
- dependencies[key] = dep
290
- } else {
291
- dependencies[key] = createValueInjectable(dep)
292
- }
293
- }
294
-
295
- const newInjectable = isClassInjectable(injectable)
296
- ? createExtendableClassInjectable(
297
- injectable,
298
- dependencies,
299
- Scope.Transient,
300
- 1,
301
- )
302
- : {
303
- ...injectable,
304
- dependencies,
305
- scope: Scope.Transient,
306
- stack: tryCaptureStackTrace(1),
307
- }
308
-
309
- return this.resolve(newInjectable) as Promise<ResolveInjectableType<T>>
310
- }
311
-
312
- const explicit = async <T extends AnyInjectable>(
313
- injectable: T,
314
- context: InlineInjectionDependencies<T>,
315
- ) => {
316
- if ('asyncDispose' in Symbol === false) {
317
- throw new Error(
318
- 'Symbol.asyncDispose is not supported in this environment',
319
- )
320
- }
321
- const instance = await inject(injectable, context)
322
- const dispose = this.createDisposeFunction()
323
- return Object.assign(instance, {
324
- [Symbol.asyncDispose]: async () => {
325
- await dispose(injectable, instance)
326
- },
327
- })
328
- }
329
-
330
- return Object.assign(inject, { explicit })
331
- }
332
-
333
- private createDisposeFunction() {
334
- return async <T extends AnyInjectable>(injectable: T, instance?: any) => {
335
- if (injectable.scope === Scope.Transient) {
336
- assert(
337
- instance,
338
- 'Instance is required for transient injectable disposal',
339
- )
340
- const wrappers = this.instances.get(injectable)
341
- if (wrappers) {
342
- for (const wrapper of wrappers) {
343
- if (wrapper.public === instance) {
344
- await this.disposeInjectableInstance(
345
- injectable,
346
- wrapper.private,
347
- wrapper.context,
348
- )
349
- const index = wrappers.indexOf(wrapper)
350
- wrappers.splice(index, 1)
351
- }
352
- }
353
-
354
- if (wrappers.length === 0) {
355
- this.instances.delete(injectable)
356
- }
357
- }
358
- } else {
359
- await this.disposeInjectableInstances(injectable)
360
- }
361
- }
362
- }
363
-
364
- private getDisposalOrder(): AnyInjectable[] {
365
- const visited = new Set<AnyInjectable>()
366
- const result: AnyInjectable[] = []
367
-
368
- const visit = (injectable: AnyInjectable) => {
369
- if (visited.has(injectable)) return
370
- visited.add(injectable)
371
-
372
- const dependants = this.dependants.get(injectable)
373
- if (dependants) {
374
- for (const dependant of dependants) {
375
- if (this.instances.has(dependant)) {
376
- visit(dependant)
377
- }
378
- }
379
- }
380
-
381
- // Only add to result if this container owns the instance
382
- if (this.instances.has(injectable)) {
383
- result.push(injectable)
384
- }
385
- }
386
-
387
- for (const injectable of this.instances.keys()) {
388
- visit(injectable)
389
- }
390
-
391
- return result
392
- }
393
-
394
- private async disposeInjectableInstances(injectable: AnyInjectable) {
395
- try {
396
- if (this.instances.has(injectable)) {
397
- const wrappers = this.instances.get(injectable)!
398
- await Promise.all(
399
- wrappers.map((wrapper) =>
400
- this.disposeInjectableInstance(
401
- injectable,
402
- wrapper.private,
403
- wrapper.context,
404
- ),
405
- ),
406
- )
407
- }
408
- } catch (cause) {
409
- const error = new Error(
410
- 'Injectable disposal error. Potential memory leak',
411
- { cause },
412
- )
413
- this.application.logger.error(error)
414
- } finally {
415
- this.instances.delete(injectable)
416
- }
417
- }
418
-
419
- private async disposeInjectableInstance(
420
- injectable: AnyInjectable,
421
- instance: any,
422
- context: any,
423
- ) {
424
- if (isFactoryInjectable(injectable)) {
425
- const { dispose } = injectable
426
- if (dispose) await dispose(instance, context)
427
- } else if (isClassInjectable(injectable)) {
428
- await instance.$onDispose()
429
- }
430
- }
431
- }
432
-
433
- type InlineInjectionDependencies<T extends AnyInjectable> = {
434
- [K in keyof T['dependencies']]?:
435
- | ResolveInjectableType<T['dependencies'][K]>
436
- | AnyInjectable<ResolveInjectableType<T['dependencies'][K]>>
437
- }
438
-
439
- export type InjectFn = ReturnType<Container['createInjectFunction']>
440
- export type DisposeFn = ReturnType<Container['createDisposeFunction']>
package/src/enums.ts DELETED
@@ -1,19 +0,0 @@
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/src/hooks.ts DELETED
@@ -1,69 +0,0 @@
1
- import type { Callback } from '@nmtjs/common'
2
- import { kHookCollection } from './constants.ts'
3
- import type { Hook } from './enums.ts'
4
-
5
- export interface HookType {
6
- [key: string]: (...args: any[]) => any
7
- // [Hook.AfterInitialize]: () => any
8
- // [Hook.BeforeStart]: () => any
9
- // [Hook.AfterStart]: () => any
10
- // [Hook.BeforeStop]: () => any
11
- // [Hook.AfterStop]: () => any
12
- // [Hook.BeforeTerminate]: () => any
13
- // [Hook.AfterTerminate]: () => any
14
- // [Hook.OnConnect]: (...args: any[]) => any
15
- // [Hook.OnDisconnect]: (...args: any[]) => any
16
- }
17
-
18
- export type CallHook<T extends string> = (
19
- hook: T,
20
- ...args: T extends keyof HookType ? Parameters<HookType[T]> : any[]
21
- ) => Promise<void>
22
-
23
- export class Hooks {
24
- static merge(from: Hooks, to: Hooks) {
25
- for (const [name, callbacks] of from[kHookCollection]) {
26
- for (const callback of callbacks) {
27
- to.add(name, callback)
28
- }
29
- }
30
- }
31
-
32
- [kHookCollection] = new Map<string, Set<Callback>>()
33
-
34
- add(name: string, callback: Callback) {
35
- let hooks = this[kHookCollection].get(name)
36
- if (!hooks) this[kHookCollection].set(name, (hooks = new Set()))
37
- hooks.add(callback)
38
- return () => this.remove(name, callback)
39
- }
40
-
41
- remove(name: string, callback: Callback) {
42
- const hooks = this[kHookCollection].get(name)
43
- if (hooks) hooks.delete(callback)
44
- }
45
-
46
- async call<T extends string | Hook>(
47
- name: T,
48
- options: { concurrent?: boolean; reverse?: boolean } | undefined,
49
- ...args: T extends Hook ? Parameters<HookType[T]> : any[]
50
- ) {
51
- const { concurrent = true, reverse = false } = options ?? {}
52
- const hooks = this[kHookCollection].get(name)
53
- if (!hooks) return
54
- const hooksArr = Array.from(hooks)
55
- if (concurrent) {
56
- await Promise.all(hooksArr.map((hook) => hook(...args)))
57
- } else {
58
- if (reverse) hooksArr.reverse()
59
- for (const hook of hooksArr) await hook(...args)
60
- }
61
- }
62
-
63
- clear() {
64
- this[kHookCollection].clear()
65
- }
66
- }
67
-
68
- export const createErrForHook = (hook: Hook | (object & string)) =>
69
- `Error during [${hook}] hook`
package/src/index.ts DELETED
@@ -1,15 +0,0 @@
1
- // biome-ignore lint/correctness/noUnusedImports: TSC wants it
2
- // biome-ignore assist/source/organizeImports: TSC wants it
3
- import {} from 'pino'
4
-
5
- export * from './constants.ts'
6
- export * from './container.ts'
7
- export * from './enums.ts'
8
- export * from './hooks.ts'
9
- export * from './injectables.ts'
10
- export * from './logger.ts'
11
- export * from './metadata.ts'
12
- export * from './plugin.ts'
13
- export * from './registry.ts'
14
- export * from './types.ts'
15
- export * from './utils/index.ts'