@nmtjs/core 0.15.0-beta.3 → 0.15.0-beta.4

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
@@ -8,18 +8,19 @@
8
8
  "peerDependencies": {
9
9
  "pino": "^9.6.0",
10
10
  "pino-pretty": "^13.0.0",
11
- "@nmtjs/common": "0.15.0-beta.3",
12
- "@nmtjs/type": "0.15.0-beta.3"
11
+ "@nmtjs/common": "0.15.0-beta.4",
12
+ "@nmtjs/type": "0.15.0-beta.4"
13
13
  },
14
14
  "devDependencies": {
15
15
  "@types/node": "^24",
16
16
  "pino": "^9.6.0",
17
17
  "pino-pretty": "^13.0.0",
18
- "@nmtjs/type": "0.15.0-beta.3",
19
- "@nmtjs/common": "0.15.0-beta.3"
18
+ "@nmtjs/type": "0.15.0-beta.4",
19
+ "@nmtjs/common": "0.15.0-beta.4"
20
20
  },
21
21
  "files": [
22
22
  "dist",
23
+ "src",
23
24
  "LICENSE.md",
24
25
  "README.md"
25
26
  ],
@@ -27,7 +28,7 @@
27
28
  "hookable": "6.0.0-rc.1",
28
29
  "pino-std-serializers": "^7.0.0"
29
30
  },
30
- "version": "0.15.0-beta.3",
31
+ "version": "0.15.0-beta.4",
31
32
  "scripts": {
32
33
  "clean-build": "rm -rf ./dist",
33
34
  "build": "tsc --declaration --sourcemap",
@@ -0,0 +1,34 @@
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 kProvider: unique symbol = Symbol.for('neemata:ProviderKey')
25
+ export type kProvider = typeof kProvider
26
+
27
+ export const kPlugin: unique symbol = Symbol.for('neemata:PluginKey')
28
+ export type kPlugin = typeof kPlugin
29
+
30
+ export const kMetadata: unique symbol = Symbol.for('neemata:MetadataKey')
31
+ export type kMetadata = typeof kMetadata
32
+
33
+ export const kHook: unique symbol = Symbol.for('neemata:HookKey')
34
+ export type kHook = typeof kHook
@@ -0,0 +1,509 @@
1
+ import type { PerformanceMeasure } from 'node:perf_hooks'
2
+ import assert from 'node:assert'
3
+
4
+ import { tryCaptureStackTrace } from '@nmtjs/common'
5
+
6
+ import type {
7
+ AnyInjectable,
8
+ Dependencies,
9
+ DependencyContext,
10
+ Injection,
11
+ ResolveInjectableType,
12
+ } from './injectables.ts'
13
+ import type { Logger } from './logger.ts'
14
+ import { Scope } from './enums.ts'
15
+ import {
16
+ CoreInjectables,
17
+ compareScope,
18
+ createValueInjectable,
19
+ getDepedencencyInjectable,
20
+ isFactoryInjectable,
21
+ isInjectable,
22
+ isLazyInjectable,
23
+ isOptionalInjectable,
24
+ isValueInjectable,
25
+ provide,
26
+ } from './injectables.ts'
27
+
28
+ type InstanceWrapper = { private: any; public: any; context: any }
29
+
30
+ type ContainerOptions = { logger: Logger }
31
+
32
+ export class Container {
33
+ readonly instances = new Map<AnyInjectable, InstanceWrapper[]>()
34
+ private readonly resolvers = new Map<AnyInjectable, Promise<any>>()
35
+ private readonly injectables = new Set<AnyInjectable>()
36
+ private readonly dependants = new Map<AnyInjectable, Set<AnyInjectable>>()
37
+ private disposing = false
38
+
39
+ constructor(
40
+ private readonly runtime: 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
+ }
50
+
51
+ async initialize(injectables: Iterable<AnyInjectable>) {
52
+ const measurements: PerformanceMeasure[] = []
53
+
54
+ const traverse = (dependencies: Dependencies) => {
55
+ for (const key in dependencies) {
56
+ const dependency = dependencies[key]
57
+ const injectable = getDepedencencyInjectable(dependency)
58
+ if (injectable.scope === this.scope) {
59
+ this.injectables.add(injectable)
60
+ }
61
+ traverse(injectable.dependencies)
62
+ }
63
+ }
64
+
65
+ for (const dependant of injectables) {
66
+ traverse(dependant.dependencies)
67
+ }
68
+
69
+ await Promise.all(
70
+ [...this.injectables].map((injectable) =>
71
+ this.resolve(injectable, measurements),
72
+ ),
73
+ )
74
+
75
+ return measurements
76
+ }
77
+
78
+ fork(scope: Exclude<Scope, Scope.Transient>) {
79
+ return new Container(this.runtime, scope, this)
80
+ }
81
+
82
+ find(scope: Exclude<Scope, Scope.Transient>): Container | undefined {
83
+ if (this.scope === scope) {
84
+ return this
85
+ } else {
86
+ return this.parent?.find(scope)
87
+ }
88
+ }
89
+
90
+ async [Symbol.asyncDispose]() {
91
+ await this.dispose()
92
+ }
93
+
94
+ async dispose() {
95
+ this.runtime.logger.trace('Disposing [%s] scope context...', this.scope)
96
+
97
+ // Prevent new resolutions during disposal
98
+ this.disposing = true
99
+
100
+ // Get proper disposal order using topological sort
101
+ const disposalOrder = this.getDisposalOrder()
102
+
103
+ try {
104
+ // Dispose in the correct order
105
+ for (const injectable of disposalOrder) {
106
+ if (this.instances.has(injectable)) {
107
+ await this.disposeInjectableInstances(injectable)
108
+ }
109
+ }
110
+ } catch (error) {
111
+ this.runtime.logger.fatal(
112
+ { error },
113
+ 'Potential memory leak: error during container disposal',
114
+ )
115
+ }
116
+
117
+ this.instances.clear()
118
+ this.injectables.clear()
119
+ this.resolvers.clear()
120
+ this.dependants.clear()
121
+
122
+ this.disposing = false
123
+ }
124
+
125
+ containsWithinSelf(injectable: AnyInjectable) {
126
+ return this.instances.has(injectable) || this.resolvers.has(injectable)
127
+ }
128
+
129
+ contains(injectable: AnyInjectable): boolean {
130
+ return (
131
+ this.containsWithinSelf(injectable) ||
132
+ (this.parent?.contains(injectable) ?? false)
133
+ )
134
+ }
135
+
136
+ get<T extends AnyInjectable>(injectable: T): ResolveInjectableType<T> {
137
+ if (injectable.scope === Scope.Transient) {
138
+ throw new Error('Cannot get transient injectable directly')
139
+ }
140
+
141
+ if (this.instances.has(injectable)) {
142
+ return this.instances.get(injectable)!.at(0)!.public
143
+ }
144
+
145
+ if (this.parent?.contains(injectable)) {
146
+ return this.parent.get(injectable)
147
+ }
148
+
149
+ throw new Error('No instance found')
150
+ }
151
+
152
+ resolve<T extends AnyInjectable>(
153
+ injectable: T,
154
+ measurements?: PerformanceMeasure[],
155
+ ) {
156
+ return this.resolveInjectable(
157
+ injectable,
158
+ undefined,
159
+ undefined,
160
+ measurements,
161
+ )
162
+ }
163
+
164
+ async createContext<T extends Dependencies>(dependencies: T) {
165
+ return this.createInjectableContext(dependencies)
166
+ }
167
+
168
+ async provide<T extends Injection[]>(injections: T)
169
+ async provide<T extends AnyInjectable>(
170
+ injectable: T,
171
+ value: ResolveInjectableType<T> | AnyInjectable<ResolveInjectableType<T>>,
172
+ )
173
+ async provide<T extends AnyInjectable | Injection[]>(
174
+ injectable: T,
175
+ ...[value]: T extends AnyInjectable
176
+ ? [
177
+ value:
178
+ | ResolveInjectableType<T>
179
+ | AnyInjectable<ResolveInjectableType<T>>,
180
+ ]
181
+ : []
182
+ ) {
183
+ const injections = Array.isArray(injectable)
184
+ ? injectable
185
+ : [provide(injectable, value)]
186
+ await Promise.all(
187
+ injections.map(async ({ token, value }) => {
188
+ if (compareScope(token.scope, '>', this.scope)) {
189
+ // TODO: more informative error
190
+ throw new Error('Invalid scope')
191
+ }
192
+ const _value = isInjectable(value) ? await this.resolve(value) : value
193
+ this.instances.set(token, [
194
+ { private: _value, public: _value, context: undefined },
195
+ ])
196
+ }),
197
+ )
198
+ }
199
+
200
+ satisfies(injectable: AnyInjectable) {
201
+ return compareScope(injectable.scope, '<=', this.scope)
202
+ }
203
+
204
+ async disposeInjectableInstances(injectable: AnyInjectable) {
205
+ try {
206
+ if (this.instances.has(injectable)) {
207
+ const wrappers = this.instances.get(injectable)!
208
+ await Promise.all(
209
+ wrappers.map((wrapper) =>
210
+ this.disposeInjectableInstance(
211
+ injectable,
212
+ wrapper.private,
213
+ wrapper.context,
214
+ ),
215
+ ),
216
+ )
217
+ }
218
+ } catch (cause) {
219
+ const error = new Error(
220
+ 'Injectable disposal error. Potential memory leak',
221
+ { cause },
222
+ )
223
+ this.runtime.logger.error(error)
224
+ } finally {
225
+ this.instances.delete(injectable)
226
+ }
227
+ }
228
+
229
+ async disposeInjectableInstance(
230
+ injectable: AnyInjectable,
231
+ instance: any,
232
+ context: any,
233
+ ) {
234
+ if (isFactoryInjectable(injectable)) {
235
+ const { dispose } = injectable
236
+ if (dispose) await dispose(instance, context)
237
+ }
238
+ }
239
+
240
+ private async createInjectableContext<T extends Dependencies>(
241
+ dependencies: T,
242
+ dependant?: AnyInjectable,
243
+ measurements?: PerformanceMeasure[],
244
+ ) {
245
+ const injections: Record<string, any> = {}
246
+ const deps = Object.entries(dependencies)
247
+ const resolvers: Promise<any>[] = Array(deps.length)
248
+ for (let i = 0; i < deps.length; i++) {
249
+ const [key, dependency] = deps[i]
250
+ const isOptional = isOptionalInjectable(dependency)
251
+ const injectable = getDepedencencyInjectable(dependency)
252
+ const resolver = this.resolveInjectable(
253
+ injectable,
254
+ dependant,
255
+ isOptional,
256
+ measurements,
257
+ )
258
+ resolvers[i] = resolver.then((value) => (injections[key] = value))
259
+ }
260
+ await Promise.all(resolvers)
261
+ return Object.freeze(injections) as DependencyContext<T>
262
+ }
263
+
264
+ private resolveInjectable<T extends AnyInjectable>(
265
+ injectable: T,
266
+ dependant?: AnyInjectable,
267
+ isOptional?: boolean,
268
+ measurements?: PerformanceMeasure[],
269
+ ): Promise<ResolveInjectableType<T>> {
270
+ if (this.disposing) {
271
+ return Promise.reject(new Error('Cannot resolve during disposal'))
272
+ }
273
+
274
+ if (dependant && compareScope(dependant.scope, '<', injectable.scope)) {
275
+ // TODO: more informative error
276
+ return Promise.reject(
277
+ new Error('Invalid scope: dependant is looser than injectable'),
278
+ )
279
+ }
280
+
281
+ if (isValueInjectable(injectable)) {
282
+ return Promise.resolve(injectable.value)
283
+ } else if (
284
+ this.parent?.contains(injectable) ||
285
+ (this.parent?.satisfies(injectable) &&
286
+ compareScope(this.parent.scope, '<', this.scope))
287
+ ) {
288
+ return this.parent.resolveInjectable(
289
+ injectable,
290
+ dependant,
291
+ undefined,
292
+ measurements,
293
+ )
294
+ } else {
295
+ const { stack, label } = injectable
296
+
297
+ if (dependant) {
298
+ let dependants = this.dependants.get(injectable)
299
+ if (!dependants) {
300
+ this.dependants.set(injectable, (dependants = new Set()))
301
+ }
302
+ dependants.add(dependant)
303
+ }
304
+
305
+ const isTransient = injectable.scope === Scope.Transient
306
+
307
+ if (!isTransient && this.instances.has(injectable)) {
308
+ return Promise.resolve(this.instances.get(injectable)!.at(0)!.public)
309
+ } else if (!isTransient && this.resolvers.has(injectable)) {
310
+ return this.resolvers.get(injectable)!
311
+ } else {
312
+ const isLazy = isLazyInjectable(injectable)
313
+ if (isLazy) {
314
+ if (isOptional) return Promise.resolve(undefined as any)
315
+ return Promise.reject(
316
+ new Error(
317
+ `No instance provided for ${label || 'an'} injectable:\n${stack}`,
318
+ ),
319
+ )
320
+ } else {
321
+ const measure = measurements
322
+ ? performance.measure(injectable.label || injectable.stack || '')
323
+ : null
324
+ const resolution = this.createResolution(
325
+ injectable,
326
+ measurements,
327
+ ).finally(() => {
328
+ this.resolvers.delete(injectable)
329
+
330
+ // biome-ignore lint: false
331
+ // @ts-ignore
332
+ if (measurements && measure) measurements.push(measure)
333
+ })
334
+ if (injectable.scope !== Scope.Transient) {
335
+ this.resolvers.set(injectable, resolution)
336
+ }
337
+ return resolution
338
+ }
339
+ }
340
+ }
341
+ }
342
+
343
+ private async createResolution<T extends AnyInjectable>(
344
+ injectable: T,
345
+ measurements?: PerformanceMeasure[],
346
+ ): Promise<ResolveInjectableType<T>> {
347
+ const { dependencies } = injectable
348
+ const context = await this.createInjectableContext(
349
+ dependencies,
350
+ injectable,
351
+ measurements,
352
+ )
353
+ const wrapper = {
354
+ private: null as any,
355
+ public: null as ResolveInjectableType<T>,
356
+ context,
357
+ }
358
+ if (isFactoryInjectable(injectable)) {
359
+ wrapper.private = await Promise.resolve(
360
+ injectable.factory(wrapper.context),
361
+ )
362
+ wrapper.public = injectable.pick(wrapper.private)
363
+ } else {
364
+ throw new Error('Invalid injectable type')
365
+ }
366
+
367
+ let instances = this.instances.get(injectable)
368
+
369
+ if (!instances) {
370
+ instances = []
371
+ this.instances.set(injectable, instances)
372
+ }
373
+ instances.push(wrapper)
374
+
375
+ return wrapper.public
376
+ }
377
+
378
+ private createInjectFunction() {
379
+ const inject = <T extends AnyInjectable>(
380
+ injectable: T,
381
+ context: InlineInjectionDependencies<T>,
382
+ scope: Exclude<Scope, Scope.Transient> = this.scope,
383
+ ) => {
384
+ const container = this.find(scope)
385
+ if (!container)
386
+ throw new Error('No container found for the specified scope')
387
+
388
+ const dependencies: Dependencies = { ...injectable.dependencies }
389
+
390
+ for (const key in context) {
391
+ const dep = context[key]
392
+ if (isInjectable(dep) || isOptionalInjectable(dep)) {
393
+ dependencies[key] = dep
394
+ } else {
395
+ dependencies[key] = createValueInjectable(dep)
396
+ }
397
+ }
398
+
399
+ const newInjectable = {
400
+ ...injectable,
401
+ dependencies,
402
+ scope: Scope.Transient,
403
+ stack: tryCaptureStackTrace(1),
404
+ }
405
+
406
+ return container.resolve(newInjectable) as Promise<
407
+ ResolveInjectableType<T>
408
+ >
409
+ }
410
+
411
+ const explicit = async <T extends AnyInjectable>(
412
+ injectable: T,
413
+ context: InlineInjectionDependencies<T>,
414
+ scope: Exclude<Scope, Scope.Transient> = this.scope,
415
+ ) => {
416
+ if ('asyncDispose' in Symbol === false) {
417
+ throw new Error(
418
+ 'Symbol.asyncDispose is not supported in this environment',
419
+ )
420
+ }
421
+
422
+ const container = this.find(scope)
423
+ if (!container)
424
+ throw new Error('No container found for the specified scope')
425
+
426
+ const instance = await inject(injectable, context)
427
+ const dispose = container.createDisposeFunction()
428
+
429
+ return {
430
+ instance,
431
+ [Symbol.asyncDispose]: async () => {
432
+ await dispose(injectable, instance)
433
+ },
434
+ }
435
+ }
436
+
437
+ return Object.assign(inject, { explicit })
438
+ }
439
+
440
+ private createDisposeFunction() {
441
+ return async <T extends AnyInjectable>(injectable: T, instance?: any) => {
442
+ if (injectable.scope === Scope.Transient) {
443
+ assert(
444
+ instance,
445
+ 'Instance is required for transient injectable disposal',
446
+ )
447
+ const wrappers = this.instances.get(injectable)
448
+ if (wrappers) {
449
+ for (const wrapper of wrappers) {
450
+ if (wrapper.public === instance) {
451
+ await this.disposeInjectableInstance(
452
+ injectable,
453
+ wrapper.private,
454
+ wrapper.context,
455
+ )
456
+ const index = wrappers.indexOf(wrapper)
457
+ wrappers.splice(index, 1)
458
+ }
459
+ }
460
+
461
+ if (wrappers.length === 0) {
462
+ this.instances.delete(injectable)
463
+ }
464
+ }
465
+ } else {
466
+ await this.disposeInjectableInstances(injectable)
467
+ }
468
+ }
469
+ }
470
+
471
+ private getDisposalOrder(): AnyInjectable[] {
472
+ const visited = new Set<AnyInjectable>()
473
+ const result: AnyInjectable[] = []
474
+
475
+ const visit = (injectable: AnyInjectable) => {
476
+ if (visited.has(injectable)) return
477
+ visited.add(injectable)
478
+
479
+ const dependants = this.dependants.get(injectable)
480
+ if (dependants) {
481
+ for (const dependant of dependants) {
482
+ if (this.instances.has(dependant)) {
483
+ visit(dependant)
484
+ }
485
+ }
486
+ }
487
+
488
+ // Only add to result if this container owns the instance
489
+ if (this.instances.has(injectable)) {
490
+ result.push(injectable)
491
+ }
492
+ }
493
+
494
+ for (const injectable of this.instances.keys()) {
495
+ visit(injectable)
496
+ }
497
+
498
+ return result
499
+ }
500
+ }
501
+
502
+ type InlineInjectionDependencies<T extends AnyInjectable> = {
503
+ [K in keyof T['dependencies']]?:
504
+ | ResolveInjectableType<T['dependencies'][K]>
505
+ | AnyInjectable<ResolveInjectableType<T['dependencies'][K]>>
506
+ }
507
+
508
+ export type InjectFn = ReturnType<Container['createInjectFunction']>
509
+ export type DisposeFn = ReturnType<Container['createDisposeFunction']>
package/src/enums.ts ADDED
@@ -0,0 +1,6 @@
1
+ export enum Scope {
2
+ Global = 'Global',
3
+ Connection = 'Connection',
4
+ Call = 'Call',
5
+ Transient = 'Transient',
6
+ }
package/src/hooks.ts ADDED
@@ -0,0 +1,24 @@
1
+ import type { NestedHooks } from 'hookable'
2
+ import { Hookable } from 'hookable'
3
+
4
+ import type { HookTypes } from './types.ts'
5
+
6
+ export class Hooks<T extends HookTypes = HookTypes> extends Hookable<T> {
7
+ _!: { config: NestedHooks<T> }
8
+
9
+ createSignal<T extends keyof T>(
10
+ hook: T,
11
+ ): {
12
+ controller: AbortController
13
+ signal: AbortSignal
14
+ unregister: () => void
15
+ } {
16
+ const controller = new AbortController()
17
+ const unregister = this.hookOnce(
18
+ String(hook),
19
+ //@ts-expect-error
20
+ () => controller.abort(),
21
+ )
22
+ return { controller, signal: controller.signal, unregister }
23
+ }
24
+ }
package/src/index.ts ADDED
@@ -0,0 +1,14 @@
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 './types.ts'
14
+ export * from './utils/index.ts'
@@ -0,0 +1,359 @@
1
+ import type { Async } from '@nmtjs/common'
2
+ import { tryCaptureStackTrace } from '@nmtjs/common'
3
+
4
+ import type { DisposeFn, InjectFn } from './container.ts'
5
+ import type { Logger } from './logger.ts'
6
+ import {
7
+ kFactoryInjectable,
8
+ kInjectable,
9
+ kLazyInjectable,
10
+ kOptionalDependency,
11
+ kValueInjectable,
12
+ } from './constants.ts'
13
+ import { Scope } from './enums.ts'
14
+
15
+ const ScopeStrictness = {
16
+ [Scope.Transient]: Number.NaN, // this should make it always fail to compare with other scopes
17
+ [Scope.Global]: 1,
18
+ [Scope.Connection]: 2,
19
+ [Scope.Call]: 3,
20
+ }
21
+
22
+ export type DependencyOptional<T extends AnyInjectable = AnyInjectable> = {
23
+ [kOptionalDependency]: any
24
+ injectable: T
25
+ }
26
+
27
+ export type Depedency = DependencyOptional | AnyInjectable
28
+
29
+ export type Dependencies = Record<string, Depedency>
30
+
31
+ export type ResolveInjectableType<T extends AnyInjectable> =
32
+ T extends Injectable<infer Type, any, any> ? Type : never
33
+
34
+ export interface Dependant<Deps extends Dependencies = Dependencies> {
35
+ dependencies: Deps
36
+ label?: string
37
+ stack?: string
38
+ }
39
+
40
+ export type DependencyInjectable<T extends Depedency> = T extends AnyInjectable
41
+ ? T
42
+ : T extends DependencyOptional
43
+ ? T['injectable']
44
+ : never
45
+
46
+ export type DependencyContext<Deps extends Dependencies> = {
47
+ readonly [K in keyof Deps as Deps[K] extends AnyInjectable
48
+ ? K
49
+ : never]: Deps[K] extends AnyInjectable
50
+ ? ResolveInjectableType<Deps[K]>
51
+ : never
52
+ } & {
53
+ readonly [K in keyof Deps as Deps[K] extends DependencyOptional
54
+ ? K
55
+ : never]?: Deps[K] extends DependencyOptional
56
+ ? ResolveInjectableType<Deps[K]['injectable']>
57
+ : never
58
+ }
59
+
60
+ export type InjectableFactoryType<
61
+ InjectableType,
62
+ InjectableDeps extends Dependencies,
63
+ > = (context: DependencyContext<InjectableDeps>) => Async<InjectableType>
64
+
65
+ export type InjectablePickType<Input, Output> = (injectable: Input) => Output
66
+
67
+ export type InjectableDisposeType<
68
+ InjectableType,
69
+ InjectableDeps extends Dependencies,
70
+ > = (
71
+ instance: InjectableType,
72
+ context: DependencyContext<InjectableDeps>,
73
+ ) => any
74
+
75
+ export interface LazyInjectable<T, S extends Scope = Scope.Global>
76
+ extends Dependant<{}> {
77
+ scope: S
78
+ $withType<O extends T>(): LazyInjectable<O, S>
79
+ optional(): DependencyOptional<LazyInjectable<T, S>>
80
+ [kInjectable]: any
81
+ [kLazyInjectable]: T
82
+ }
83
+
84
+ export interface ValueInjectable<T> extends Dependant<{}> {
85
+ scope: Scope.Global
86
+ value: T
87
+ [kInjectable]: any
88
+ [kValueInjectable]: any
89
+ }
90
+
91
+ export interface FactoryInjectable<
92
+ T,
93
+ D extends Dependencies = {},
94
+ S extends Scope = Scope.Global,
95
+ P = T,
96
+ > extends Dependant<D> {
97
+ scope: S
98
+ factory: InjectableFactoryType<P, D>
99
+ pick: InjectablePickType<P, T>
100
+ dispose?: InjectableDisposeType<P, D>
101
+ optional(): DependencyOptional<FactoryInjectable<T, D, S, P>>
102
+ [kInjectable]: any
103
+ [kFactoryInjectable]: any
104
+ }
105
+
106
+ export type Injectable<
107
+ V = any,
108
+ D extends Dependencies = {},
109
+ S extends Scope = Scope,
110
+ > = LazyInjectable<V, S> | ValueInjectable<V> | FactoryInjectable<V, D, S, any>
111
+
112
+ export type AnyInjectable<T = any, S extends Scope = Scope> = Injectable<
113
+ T,
114
+ any,
115
+ S
116
+ >
117
+
118
+ export const isLazyInjectable = (
119
+ injectable: any,
120
+ ): injectable is LazyInjectable<any> => injectable[kLazyInjectable]
121
+
122
+ export const isFactoryInjectable = (
123
+ injectable: any,
124
+ ): injectable is FactoryInjectable<any> => injectable[kFactoryInjectable]
125
+
126
+ export const isValueInjectable = (
127
+ injectable: any,
128
+ ): injectable is ValueInjectable<any> => injectable[kValueInjectable]
129
+
130
+ export const isInjectable = (
131
+ injectable: any,
132
+ ): injectable is AnyInjectable<any> => injectable[kInjectable]
133
+
134
+ export const isOptionalInjectable = (
135
+ injectable: any,
136
+ ): injectable is DependencyOptional<any> => injectable[kOptionalDependency]
137
+
138
+ export function getInjectableScope(injectable: AnyInjectable) {
139
+ let scope = injectable.scope
140
+ const deps = injectable.dependencies as Dependencies
141
+ for (const key in deps) {
142
+ const dependency = deps[key]
143
+ const injectable = getDepedencencyInjectable(dependency)
144
+ const dependencyScope = getInjectableScope(injectable)
145
+ if (
146
+ dependencyScope !== Scope.Transient &&
147
+ scope !== Scope.Transient &&
148
+ compareScope(dependencyScope, '>', scope)
149
+ ) {
150
+ scope = dependencyScope
151
+ }
152
+ }
153
+ return scope
154
+ }
155
+
156
+ export function getDepedencencyInjectable(
157
+ dependency: Depedency,
158
+ ): AnyInjectable {
159
+ if (kOptionalDependency in dependency) {
160
+ return dependency.injectable
161
+ }
162
+ return dependency
163
+ }
164
+
165
+ export function createOptionalInjectable<T extends AnyInjectable>(
166
+ injectable: T,
167
+ ) {
168
+ return Object.freeze({
169
+ [kOptionalDependency]: true,
170
+ injectable,
171
+ }) as DependencyOptional<T>
172
+ }
173
+
174
+ export function createLazyInjectable<T, S extends Scope = Scope.Global>(
175
+ scope = Scope.Global as S,
176
+ label?: string,
177
+ stackTraceDepth = 0,
178
+ ): LazyInjectable<T, S> {
179
+ const injectable = Object.freeze({
180
+ scope,
181
+ dependencies: {},
182
+ label,
183
+ stack: tryCaptureStackTrace(stackTraceDepth),
184
+ optional: () => createOptionalInjectable(injectable),
185
+ $withType: () => injectable as any,
186
+ [kInjectable]: true,
187
+ [kLazyInjectable]: true as unknown as T,
188
+ })
189
+ return injectable
190
+ }
191
+
192
+ export function createValueInjectable<T>(
193
+ value: T,
194
+ label?: string,
195
+ stackTraceDepth = 0,
196
+ ): ValueInjectable<T> {
197
+ return Object.freeze({
198
+ value,
199
+ scope: Scope.Global,
200
+ dependencies: {},
201
+ label,
202
+ stack: tryCaptureStackTrace(stackTraceDepth),
203
+ [kInjectable]: true,
204
+ [kValueInjectable]: true,
205
+ })
206
+ }
207
+
208
+ export function createFactoryInjectable<
209
+ T,
210
+ D extends Dependencies = {},
211
+ S extends Scope = Scope.Global,
212
+ P = T,
213
+ >(
214
+ paramsOrFactory:
215
+ | {
216
+ dependencies?: D
217
+ scope?: S
218
+ pick?: InjectablePickType<P, T>
219
+ factory: InjectableFactoryType<P, D>
220
+ dispose?: InjectableDisposeType<P, D>
221
+ }
222
+ | InjectableFactoryType<P, D>,
223
+ label?: string,
224
+ stackTraceDepth = 0,
225
+ ): FactoryInjectable<null extends T ? P : T, D, S, P> {
226
+ const isFactory = typeof paramsOrFactory === 'function'
227
+ const params = isFactory ? { factory: paramsOrFactory } : paramsOrFactory
228
+ const injectable = {
229
+ dependencies: (params.dependencies ?? {}) as D,
230
+ scope: (params.scope ?? Scope.Global) as S,
231
+ factory: params.factory,
232
+ dispose: params.dispose,
233
+ pick: params.pick ?? ((instance: P) => instance as unknown as T),
234
+ label,
235
+ stack: tryCaptureStackTrace(stackTraceDepth),
236
+ optional: () => createOptionalInjectable(injectable),
237
+ [kInjectable]: true,
238
+ [kFactoryInjectable]: true,
239
+ }
240
+ injectable.scope = resolveInjectableScope(
241
+ typeof params.scope === 'undefined',
242
+ injectable,
243
+ ) as S
244
+ return Object.freeze(injectable) as any
245
+ }
246
+
247
+ export type DependenciesSubstitution<T extends Dependencies> = {
248
+ [K in keyof T]?: T[K] extends AnyInjectable<infer Type>
249
+ ? AnyInjectable<Type> | DependenciesSubstitution<T[K]['dependencies']>
250
+ : never
251
+ }
252
+
253
+ export function substitute<T extends FactoryInjectable<any, any, Scope>>(
254
+ injectable: T,
255
+ substitution: DependenciesSubstitution<T['dependencies']>,
256
+ stackTraceDepth = 0,
257
+ ): T {
258
+ const dependencies = { ...injectable.dependencies }
259
+ const depth = stackTraceDepth + 1
260
+ for (const key in substitution) {
261
+ const value = substitution[key]!
262
+ if (key in dependencies) {
263
+ const original = dependencies[key]
264
+ if (isInjectable(value)) {
265
+ dependencies[key] = value
266
+ } else if (isFactoryInjectable(original)) {
267
+ dependencies[key] = substitute(original, value, depth)
268
+ }
269
+ }
270
+ }
271
+
272
+ if (isFactoryInjectable(injectable)) {
273
+ // @ts-expect-error
274
+ return createFactoryInjectable(
275
+ { ...injectable, dependencies },
276
+ injectable.label,
277
+ depth,
278
+ )
279
+ }
280
+
281
+ throw new Error('Invalid injectable type')
282
+ }
283
+
284
+ export function compareScope(
285
+ left: Scope,
286
+ operator: '>' | '<' | '>=' | '<=' | '=' | '!=',
287
+ right: Scope,
288
+ ) {
289
+ const leftScope = ScopeStrictness[left]
290
+ const rightScope = ScopeStrictness[right]
291
+ switch (operator) {
292
+ case '=':
293
+ return leftScope === rightScope
294
+ case '!=':
295
+ return leftScope !== rightScope
296
+ case '>':
297
+ return leftScope > rightScope
298
+ case '<':
299
+ return leftScope < rightScope
300
+ case '>=':
301
+ return leftScope >= rightScope
302
+ case '<=':
303
+ return leftScope <= rightScope
304
+ default:
305
+ throw new Error('Invalid operator')
306
+ }
307
+ }
308
+
309
+ const logger = Object.assign(
310
+ (label: string) =>
311
+ createFactoryInjectable({
312
+ dependencies: { logger },
313
+ scope: Scope.Global,
314
+ factory: ({ logger }) => logger.child({ $label: label }),
315
+ }),
316
+ createLazyInjectable<Logger>(Scope.Global, 'Logger'),
317
+ ) as unknown as ((
318
+ label: string,
319
+ ) => FactoryInjectable<Logger, { logger: LazyInjectable<Logger> }>) &
320
+ LazyInjectable<Logger>
321
+
322
+ const inject = createLazyInjectable<InjectFn>(Scope.Global, 'Inject function')
323
+ const dispose = createLazyInjectable<DisposeFn>(
324
+ Scope.Global,
325
+ 'Dispose function',
326
+ )
327
+
328
+ function resolveInjectableScope(
329
+ isDefaultScope: boolean,
330
+ injectable: AnyInjectable,
331
+ ) {
332
+ const actualScope = getInjectableScope(injectable)
333
+ if (!isDefaultScope && compareScope(actualScope, '>', injectable.scope))
334
+ throw new Error(
335
+ `Invalid scope ${injectable.scope} for an injectable: dependencies have stricter scope - ${actualScope}`,
336
+ )
337
+ return actualScope
338
+ }
339
+
340
+ export const CoreInjectables = { logger, inject, dispose }
341
+
342
+ export type Injection<
343
+ T extends AnyInjectable<any, any> = AnyInjectable<any, any>,
344
+ > = {
345
+ token: T
346
+ value: T extends AnyInjectable<infer R, Scope> ? R | AnyInjectable<R> : never
347
+ }
348
+
349
+ export const provide = <
350
+ T extends AnyInjectable<any, any>,
351
+ V extends T extends AnyInjectable<infer R, Scope>
352
+ ? R | AnyInjectable<R>
353
+ : never,
354
+ >(
355
+ token: T,
356
+ value: V,
357
+ ): Injection<T> => {
358
+ return { token, value }
359
+ }
package/src/logger.ts ADDED
@@ -0,0 +1,103 @@
1
+ import { threadId } from 'node:worker_threads'
2
+
3
+ import type * as pinoType from 'pino'
4
+ import { pino, stdTimeFunctions } from 'pino'
5
+ import { build as pretty } from 'pino-pretty'
6
+
7
+ export type { StreamEntry } from 'pino'
8
+ export type Logger = pinoType.Logger
9
+ export type LoggerOptions = pinoType.LoggerOptions
10
+ export type LoggerChildOptions = pinoType.ChildLoggerOptions
11
+ export type LoggingOptions = {
12
+ destinations?: Array<
13
+ pinoType.DestinationStream | pinoType.StreamEntry<pinoType.Level>
14
+ >
15
+ pinoOptions?: LoggerOptions
16
+ }
17
+
18
+ import { errWithCause } from 'pino-std-serializers'
19
+
20
+ // TODO: use node:util inspect
21
+ const bg = (value, color) => `\x1b[${color}m${value}\x1b[0m`
22
+ const fg = (value, color) => `\x1b[38;5;${color}m${value}\x1b[0m`
23
+
24
+ const levelColors = {
25
+ 10: 100,
26
+ 20: 102,
27
+ 30: 106,
28
+ 40: 104,
29
+ 50: 101,
30
+ 60: 105,
31
+ [Number.POSITIVE_INFINITY]: 0,
32
+ }
33
+ const messageColors = {
34
+ 10: 0,
35
+ 20: 2,
36
+ 30: 6,
37
+ 40: 4,
38
+ 50: 1,
39
+ 60: 5,
40
+ [Number.POSITIVE_INFINITY]: 0,
41
+ }
42
+
43
+ const levelLabels = {
44
+ 10: ' TRACE ',
45
+ 20: ' DEBUG ',
46
+ 30: ' INFO ',
47
+ 40: ' WARN ',
48
+ 50: ' ERROR ',
49
+ 60: ' FATAL ',
50
+ [Number.POSITIVE_INFINITY]: 'SILENT',
51
+ }
52
+
53
+ export const createLogger = (options: LoggingOptions = {}, $lable: string) => {
54
+ let { destinations, pinoOptions } = options
55
+
56
+ if (!destinations || !destinations?.length) {
57
+ destinations = [
58
+ createConsolePrettyDestination(
59
+ (options.pinoOptions?.level || 'info') as pinoType.Level,
60
+ ),
61
+ ]
62
+ }
63
+
64
+ const lowestLevelValue = destinations!.reduce(
65
+ (acc, destination) =>
66
+ Math.min(
67
+ acc,
68
+ 'stream' in destination
69
+ ? pino.levels.values[destination.level!]
70
+ : Number.POSITIVE_INFINITY,
71
+ ),
72
+ Number.POSITIVE_INFINITY,
73
+ )
74
+ const level = pino.levels.labels[lowestLevelValue]
75
+ const serializers = { ...pinoOptions?.serializers, err: errWithCause }
76
+
77
+ return pino(
78
+ { timestamp: stdTimeFunctions.isoTime, ...pinoOptions, level, serializers },
79
+ pino.multistream(destinations!),
80
+ ).child({ $lable, $threadId: threadId })
81
+ }
82
+
83
+ export const createConsolePrettyDestination = (
84
+ level: pinoType.Level,
85
+ sync = true,
86
+ ): pinoType.StreamEntry => ({
87
+ level,
88
+ stream: pretty({
89
+ colorize: true,
90
+ ignore: 'hostname,$lable,$threadId',
91
+ errorLikeObjectKeys: ['err', 'error', 'cause'],
92
+ messageFormat: (log, messageKey) => {
93
+ const group = fg(`[${log.$lable}]`, 11)
94
+ const msg = fg(log[messageKey], messageColors[log.level as number])
95
+ const thread = fg(`(Thread-${log.$threadId})`, 89)
96
+ return `\x1b[0m${thread} ${group} ${msg}`
97
+ },
98
+ customPrettifiers: {
99
+ level: (level: any) => bg(levelLabels[level], levelColors[level]),
100
+ },
101
+ sync,
102
+ }),
103
+ })
@@ -0,0 +1,24 @@
1
+ import { kMetadata } from './constants.ts'
2
+
3
+ export type Metadata<T = any> = { key: MetadataKey<T>; value: T }
4
+
5
+ export type MetadataKey<T = any> = {
6
+ [kMetadata]: string
7
+ as(value: T): Metadata<T>
8
+ }
9
+
10
+ export const createMetadataKey = <T>(key: string): MetadataKey<T> => {
11
+ const metadataKey = {
12
+ [kMetadata]: key,
13
+ as(value: T) {
14
+ return { key: metadataKey, value }
15
+ },
16
+ }
17
+ return metadataKey
18
+ }
19
+
20
+ export class MetadataStore extends Map<MetadataKey, Metadata> {
21
+ get<T>(key: MetadataKey<T>): T | undefined {
22
+ return super.get(key) as T | undefined
23
+ }
24
+ }
package/src/plugin.ts ADDED
@@ -0,0 +1,16 @@
1
+ import type { Async } from '@nmtjs/common'
2
+
3
+ import { kPlugin } from './constants.ts'
4
+
5
+ export interface Plugin<Type = void, Context = unknown> {
6
+ name: string
7
+ factory: (context: Context) => Async<Type>
8
+ [kPlugin]: any
9
+ }
10
+
11
+ export const createPlugin = <Type = void, Context = unknown>(
12
+ name: string,
13
+ factory: Plugin<Type, Context>['factory'],
14
+ ): Plugin<Type, Context> => ({ name, factory, [kPlugin]: true })
15
+
16
+ export const isPlugin = (value: any): value is Plugin => kPlugin in value
package/src/types.ts ADDED
@@ -0,0 +1,3 @@
1
+ export type { Pattern } from '@nmtjs/common'
2
+
3
+ export type HookTypes = Record<string | symbol, any>
@@ -0,0 +1,31 @@
1
+ export { match } from '@nmtjs/common'
2
+
3
+ export function isJsFile(name: string) {
4
+ if (name.endsWith('.d.ts')) return false
5
+ const leading = name.split('.').slice(1)
6
+ const ext = leading.join('.')
7
+ return ['js', 'mjs', 'cjs', 'ts', 'mts', 'cts'].includes(ext)
8
+ }
9
+
10
+ export function pick<
11
+ T extends object,
12
+ K extends {
13
+ [KK in keyof T as T[KK] extends (...args: any[]) => any ? never : KK]?: true
14
+ },
15
+ >(
16
+ obj: T,
17
+ keys: K,
18
+ ): Pick<
19
+ T,
20
+ keyof {
21
+ [KK in keyof K as K[KK] extends true ? KK : never]: K[KK]
22
+ }
23
+ > {
24
+ const result = {} as any
25
+ for (const key in keys) {
26
+ if (key in obj) {
27
+ result[key] = obj[key as unknown as keyof typeof obj]
28
+ }
29
+ }
30
+ return result
31
+ }
@@ -0,0 +1,3 @@
1
+ export * from './functions.ts'
2
+ export * from './pool.ts'
3
+ export * from './semaphore.ts'
@@ -0,0 +1,111 @@
1
+ import type { Callback } from '@nmtjs/common'
2
+
3
+ interface PoolOptions {
4
+ timeout?: number
5
+ }
6
+
7
+ interface PoolQueueItem {
8
+ resolve?: Callback
9
+ timer?: ReturnType<typeof setTimeout>
10
+ }
11
+
12
+ export class PoolError extends Error {}
13
+
14
+ // Fixed pool from https://github.com/metarhia/metautil
15
+ export class Pool<T = unknown> {
16
+ #items: Array<T> = []
17
+ #free: Array<boolean> = []
18
+ #queue: PoolQueueItem[] = []
19
+ #current: number = 0
20
+ #size: number = 0
21
+ #available: number = 0
22
+
23
+ constructor(private readonly options: PoolOptions = {}) {}
24
+
25
+ add(item: T) {
26
+ if (this.#items.includes(item)) throw new PoolError('Item already exists')
27
+ this.#size++
28
+ this.#available++
29
+ this.#items.push(item)
30
+ this.#free.push(true)
31
+ }
32
+
33
+ remove(item: T) {
34
+ if (this.#size === 0) throw new PoolError('Pool is empty')
35
+ const index = this.#items.indexOf(item)
36
+ if (index < 0) throw new PoolError('Item is not in the pool')
37
+ const isCaptured = this.isFree(item)
38
+ if (isCaptured) this.#available--
39
+ this.#size--
40
+ this.#current--
41
+ this.#items.splice(index, 1)
42
+ this.#free.splice(index, 1)
43
+ }
44
+
45
+ capture(timeout = this.options.timeout) {
46
+ return this.next(true, timeout)
47
+ }
48
+
49
+ async next(exclusive = false, timeout = this.options.timeout): Promise<T> {
50
+ if (this.#size === 0) throw new PoolError('Pool is empty')
51
+ if (this.#available === 0) {
52
+ return new Promise((resolve, reject) => {
53
+ const waiting: PoolQueueItem = {
54
+ resolve: (item: T) => {
55
+ if (exclusive) this.#capture(item)
56
+ resolve(item)
57
+ },
58
+ timer: undefined,
59
+ }
60
+ if (timeout) {
61
+ waiting.timer = setTimeout(() => {
62
+ waiting.resolve = undefined
63
+ this.#queue.shift()
64
+ reject(new PoolError('Next item timeout'))
65
+ }, timeout)
66
+ }
67
+ this.#queue.push(waiting)
68
+ })
69
+ }
70
+ let item: T | undefined
71
+ let free = false
72
+ do {
73
+ item = this.#items[this.#current]
74
+ free = this.#free[this.#current]
75
+ this.#current++
76
+ if (this.#current >= this.#size) this.#current = 0
77
+ } while (typeof item === 'undefined' || !free)
78
+ if (exclusive) this.#capture(item)
79
+ return item
80
+ }
81
+
82
+ release(item: T) {
83
+ const index = this.#items.indexOf(item)
84
+ if (index < 0) throw new PoolError('Unexpected item')
85
+ if (this.#free[index])
86
+ throw new PoolError('Unable to release not captured item')
87
+ this.#free[index] = true
88
+ this.#available++
89
+ if (this.#queue.length > 0) {
90
+ const { resolve, timer } = this.#queue.shift()!
91
+ clearTimeout(timer)
92
+ if (resolve) setTimeout(resolve, 0, item)
93
+ }
94
+ }
95
+
96
+ isFree(item: T) {
97
+ const index = this.#items.indexOf(item)
98
+ if (index < 0) return false
99
+ return this.#free[index]
100
+ }
101
+
102
+ get items() {
103
+ return [...this.#items]
104
+ }
105
+
106
+ #capture(item: T) {
107
+ const index = this.#items.indexOf(item)
108
+ this.#free[index] = false
109
+ this.#available--
110
+ }
111
+ }
@@ -0,0 +1,63 @@
1
+ import type { Callback } from '@nmtjs/common'
2
+
3
+ interface SemaphoreQueueItem {
4
+ resolve?: Callback
5
+ timer?: ReturnType<typeof setTimeout>
6
+ }
7
+
8
+ export class SemaphoreError extends Error {}
9
+
10
+ // Semaphore from https://github.com/metarhia/metautil
11
+ export class Semaphore {
12
+ private counter: number
13
+
14
+ private readonly queue: SemaphoreQueueItem[] = []
15
+
16
+ constructor(
17
+ concurrency: number,
18
+ private readonly size: number = 0,
19
+ private readonly timeout: number = 0,
20
+ ) {
21
+ this.counter = concurrency
22
+ }
23
+
24
+ enter(): Promise<void> {
25
+ if (this.counter > 0) {
26
+ this.counter--
27
+ return Promise.resolve()
28
+ } else if (this.queue.length >= this.size) {
29
+ return Promise.reject(new SemaphoreError('Queue is full'))
30
+ } else {
31
+ return new Promise((resolve, reject) => {
32
+ const waiting: SemaphoreQueueItem = { resolve }
33
+ waiting.timer = setTimeout(() => {
34
+ waiting.resolve = undefined
35
+ this.queue.shift()
36
+ reject(new SemaphoreError('Timeout'))
37
+ }, this.timeout)
38
+ this.queue.push(waiting)
39
+ })
40
+ }
41
+ }
42
+
43
+ leave() {
44
+ if (this.queue.length === 0) {
45
+ this.counter++
46
+ } else {
47
+ const item = this.queue.shift()
48
+ if (item) {
49
+ const { resolve, timer } = item
50
+ if (timer) clearTimeout(timer)
51
+ if (resolve) setTimeout(resolve, 0)
52
+ }
53
+ }
54
+ }
55
+
56
+ get isEmpty() {
57
+ return this.queue.length === 0
58
+ }
59
+
60
+ get queueLength() {
61
+ return this.queue.length
62
+ }
63
+ }