@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.
Files changed (46) hide show
  1. package/LICENSE.md +7 -0
  2. package/README.md +9 -0
  3. package/dist/index.js +11 -0
  4. package/dist/index.js.map +1 -0
  5. package/dist/lib/constants.js +9 -0
  6. package/dist/lib/constants.js.map +1 -0
  7. package/dist/lib/container.js +275 -0
  8. package/dist/lib/container.js.map +1 -0
  9. package/dist/lib/enums.js +20 -0
  10. package/dist/lib/enums.js.map +1 -0
  11. package/dist/lib/hooks.js +37 -0
  12. package/dist/lib/hooks.js.map +1 -0
  13. package/dist/lib/injectables.js +6 -0
  14. package/dist/lib/injectables.js.map +1 -0
  15. package/dist/lib/logger.js +72 -0
  16. package/dist/lib/logger.js.map +1 -0
  17. package/dist/lib/plugin.js +6 -0
  18. package/dist/lib/plugin.js.map +1 -0
  19. package/dist/lib/registry.js +25 -0
  20. package/dist/lib/registry.js.map +1 -0
  21. package/dist/lib/types.js +1 -0
  22. package/dist/lib/types.js.map +1 -0
  23. package/dist/lib/utils/functions.js +32 -0
  24. package/dist/lib/utils/functions.js.map +1 -0
  25. package/dist/lib/utils/index.js +3 -0
  26. package/dist/lib/utils/index.js.map +1 -0
  27. package/dist/lib/utils/pool.js +100 -0
  28. package/dist/lib/utils/pool.js.map +1 -0
  29. package/dist/lib/utils/semaphore.js +52 -0
  30. package/dist/lib/utils/semaphore.js.map +1 -0
  31. package/index.ts +11 -0
  32. package/lib/constants.ts +36 -0
  33. package/lib/container.ts +482 -0
  34. package/lib/enums.ts +19 -0
  35. package/lib/hooks.ts +70 -0
  36. package/lib/injectables.ts +7 -0
  37. package/lib/logger.ts +99 -0
  38. package/lib/plugin.ts +25 -0
  39. package/lib/registry.ts +44 -0
  40. package/lib/types.ts +13 -0
  41. package/lib/utils/functions.ts +31 -0
  42. package/lib/utils/index.ts +3 -0
  43. package/lib/utils/pool.ts +111 -0
  44. package/lib/utils/semaphore.ts +63 -0
  45. package/package.json +33 -0
  46. package/tsconfig.json +3 -0
package/LICENSE.md ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2024 Denis Ilchyshyn
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,9 @@
1
+ # NeemataJS - RPC application server for real-time applications (proof of concept)
2
+
3
+ ### Built with following in mind:
4
+ - transport-agnostic (like WebSockets, WebTransport, .etc)
5
+ - format-agnostic (like JSON, MessagePack, BSON, .etc)
6
+ - binary data streaming and event subscriptions
7
+ - contract-based API
8
+ - end-to-end type safety
9
+ - CPU-intensive task execution on separate workers
package/dist/index.js ADDED
@@ -0,0 +1,11 @@
1
+ export * from "./lib/constants.js";
2
+ export * from "./lib/types.js";
3
+ export * from "./lib/enums.js";
4
+ export * from "./lib/hooks.js";
5
+ export * from "./lib/logger.js";
6
+ export * from "./lib/registry.js";
7
+ export * from "./lib/container.js";
8
+ export * from "./lib/injectables.js";
9
+ export * from "./lib/types.js";
10
+ export * from "./lib/plugin.js";
11
+ export * from "./lib/utils/index.js";
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../index.ts"],"sourcesContent":["export * from './lib/constants.ts'\nexport * from './lib/types.ts'\nexport * from './lib/enums.ts'\nexport * from './lib/hooks.ts'\nexport * from './lib/logger.ts'\nexport * from './lib/registry.ts'\nexport * from './lib/container.ts'\nexport * from './lib/injectables.ts'\nexport * from './lib/types.ts'\nexport * from './lib/plugin.ts'\nexport * from './lib/utils/index.ts'\n"],"names":[],"mappings":"AAAA,cAAc,qBAAoB;AAClC,cAAc,iBAAgB;AAC9B,cAAc,iBAAgB;AAC9B,cAAc,iBAAgB;AAC9B,cAAc,kBAAiB;AAC/B,cAAc,oBAAmB;AACjC,cAAc,qBAAoB;AAClC,cAAc,uBAAsB;AACpC,cAAc,iBAAgB;AAC9B,cAAc,kBAAiB;AAC/B,cAAc,uBAAsB"}
@@ -0,0 +1,9 @@
1
+ export const kOptionalDependency = Symbol.for('neemata:OptionalDependencyKey');
2
+ export const kInjectable = Symbol.for('neemata:InjectableKey');
3
+ export const kLazyInjectable = Symbol.for('neemata:LazyInjectableKey');
4
+ export const kValueInjectable = Symbol.for('neemata:ValueInjectableKey');
5
+ export const kFactoryInjectable = Symbol.for('neemata:FactoryInjectableKey');
6
+ export const kProvider = Symbol.for('neemata:ProviderKey');
7
+ export const kHookCollection = Symbol.for('neemata:HookCollectionKey');
8
+ export const kPlugin = Symbol.for('neemata:PluginKey');
9
+ export const isPlugin = (value)=>kPlugin in value;
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../lib/constants.ts"],"sourcesContent":["import type { Plugin } from './plugin.ts'\n\nexport const kOptionalDependency: unique symbol = Symbol.for(\n 'neemata:OptionalDependencyKey',\n)\nexport type kOptionalDependency = typeof kOptionalDependency\n\nexport const kInjectable: unique symbol = Symbol.for('neemata:InjectableKey')\nexport type kInjectable = typeof kInjectable\n\nexport const kLazyInjectable: unique symbol = Symbol.for(\n 'neemata:LazyInjectableKey',\n)\nexport type kLazyInjectable = typeof kLazyInjectable\n\nexport const kValueInjectable: unique symbol = Symbol.for(\n 'neemata:ValueInjectableKey',\n)\nexport type kValueInjectable = typeof kValueInjectable\n\nexport const kFactoryInjectable: unique symbol = Symbol.for(\n 'neemata:FactoryInjectableKey',\n)\nexport type kFactoryInjectable = typeof kFactoryInjectable\n\nexport const kProvider: unique symbol = Symbol.for('neemata:ProviderKey')\nexport type kProvider = typeof kProvider\n\nexport const kHookCollection: unique symbol = Symbol.for(\n 'neemata:HookCollectionKey',\n)\nexport type kHookCollection = typeof kHookCollection\n\nexport const kPlugin: unique symbol = Symbol.for('neemata:PluginKey')\nexport type kPlugin = typeof kPlugin\nexport const isPlugin = (value: any): value is Plugin => kPlugin in value\n"],"names":["kOptionalDependency","Symbol","for","kInjectable","kLazyInjectable","kValueInjectable","kFactoryInjectable","kProvider","kHookCollection","kPlugin","isPlugin","value"],"mappings":"AAEA,OAAO,MAAMA,sBAAqCC,OAAOC,GAAG,CAC1D,iCACD;AAGD,OAAO,MAAMC,cAA6BF,OAAOC,GAAG,CAAC,yBAAwB;AAG7E,OAAO,MAAME,kBAAiCH,OAAOC,GAAG,CACtD,6BACD;AAGD,OAAO,MAAMG,mBAAkCJ,OAAOC,GAAG,CACvD,8BACD;AAGD,OAAO,MAAMI,qBAAoCL,OAAOC,GAAG,CACzD,gCACD;AAGD,OAAO,MAAMK,YAA2BN,OAAOC,GAAG,CAAC,uBAAsB;AAGzE,OAAO,MAAMM,kBAAiCP,OAAOC,GAAG,CACtD,6BACD;AAGD,OAAO,MAAMO,UAAyBR,OAAOC,GAAG,CAAC,qBAAoB;AAErE,OAAO,MAAMQ,WAAW,CAACC,QAAgCF,WAAWE,MAAK"}
@@ -0,0 +1,275 @@
1
+ import { tryCaptureStackTrace } from '@nmtjs/common';
2
+ import { kFactoryInjectable, kInjectable, kLazyInjectable, kOptionalDependency, kValueInjectable } from "./constants.js";
3
+ import { Scope } from "./enums.js";
4
+ const ScopeStrictness = {
5
+ [Scope.Global]: 0,
6
+ [Scope.Connection]: 1,
7
+ [Scope.Call]: 2,
8
+ [Scope.Transient]: 3
9
+ };
10
+ export class Container {
11
+ application;
12
+ scope;
13
+ parent;
14
+ instances;
15
+ resolvers;
16
+ injectables;
17
+ dependants;
18
+ constructor(application, scope = Scope.Global, parent){
19
+ this.application = application;
20
+ this.scope = scope;
21
+ this.parent = parent;
22
+ this.instances = new Map();
23
+ this.resolvers = new Map();
24
+ this.injectables = new Set();
25
+ this.dependants = new Map();
26
+ if (scope === Scope.Transient) {
27
+ throw new Error('Invalid scope');
28
+ }
29
+ }
30
+ async load() {
31
+ const traverse = (dependencies)=>{
32
+ for(const key in dependencies){
33
+ const dependency = dependencies[key];
34
+ const injectable = getDepedencencyInjectable(dependency);
35
+ this.injectables.add(injectable);
36
+ traverse(injectable.dependencies);
37
+ }
38
+ };
39
+ for (const dependant of this.application.registry.getDependants()){
40
+ traverse(dependant.dependencies);
41
+ }
42
+ const injectables = Array.from(this.findCurrentScopeInjectables());
43
+ await Promise.all(injectables.map((injectable)=>this.resolve(injectable)));
44
+ }
45
+ fork(scope) {
46
+ return new Container(this.application, scope, this);
47
+ }
48
+ async dispose() {
49
+ this.application.logger.trace('Disposing [%s] scope context...', this.scope);
50
+ while(this.instances.size){
51
+ for (const injectable of this.instances.keys()){
52
+ const dependants = this.dependants.get(injectable);
53
+ if (!dependants?.size) {
54
+ await this.disposeInjectable(injectable);
55
+ for (const dependants of this.dependants.values()){
56
+ if (dependants.has(injectable)) {
57
+ dependants.delete(injectable);
58
+ }
59
+ }
60
+ }
61
+ }
62
+ }
63
+ this.instances.clear();
64
+ this.injectables.clear();
65
+ this.resolvers.clear();
66
+ this.dependants.clear();
67
+ }
68
+ containsWithinSelf(injectable) {
69
+ return this.instances.has(injectable) || this.resolvers.has(injectable);
70
+ }
71
+ contains(injectable) {
72
+ return this.containsWithinSelf(injectable) || (this.parent?.contains(injectable) ?? false);
73
+ }
74
+ get(injectable) {
75
+ if (this.instances.has(injectable)) {
76
+ return this.instances.get(injectable).instance;
77
+ }
78
+ if (this.parent?.contains(injectable)) {
79
+ return this.parent.get(injectable);
80
+ }
81
+ throw new Error('No instance found');
82
+ }
83
+ resolve(injectable) {
84
+ return this.resolveInjectable(injectable);
85
+ }
86
+ async createContext(dependencies) {
87
+ return this.createInjectableContext(dependencies);
88
+ }
89
+ async createInjectableContext(dependencies, dependant) {
90
+ const injections = {};
91
+ const deps = Object.entries(dependencies);
92
+ const resolvers = Array(deps.length);
93
+ for(let i = 0; i < deps.length; i++){
94
+ const [key, dependency] = deps[i];
95
+ const injectable = getDepedencencyInjectable(dependency);
96
+ const resolver = this.resolveInjectable(injectable, dependant);
97
+ resolvers[i] = resolver.then((value)=>injections[key] = value);
98
+ }
99
+ await Promise.all(resolvers);
100
+ return Object.freeze(injections);
101
+ }
102
+ async provide(injectable, instance) {
103
+ if (compareScope(injectable.scope, '>', this.scope)) {
104
+ throw new Error('Invalid scope');
105
+ }
106
+ this.instances.set(injectable, {
107
+ instance,
108
+ context: undefined
109
+ });
110
+ }
111
+ satisfies(injectable) {
112
+ return compareScope(injectable.scope, '<=', this.scope);
113
+ }
114
+ *findCurrentScopeInjectables() {
115
+ for (const injectable of this.injectables){
116
+ if (injectable.scope === this.scope) {
117
+ yield injectable;
118
+ }
119
+ }
120
+ }
121
+ resolveInjectable(injectable, dependant) {
122
+ if (dependant && compareScope(dependant.scope, '<', injectable.scope)) {
123
+ throw new Error('Invalid scope: dependant is looser than injectable');
124
+ }
125
+ if (checkIsValueInjectable(injectable)) {
126
+ return Promise.resolve(injectable.value);
127
+ } else if (this.parent?.contains(injectable) || this.parent?.satisfies(injectable) && compareScope(this.parent.scope, '<', this.scope)) {
128
+ return this.parent.resolveInjectable(injectable, dependant);
129
+ } else {
130
+ const { scope, dependencies, stack, label } = injectable;
131
+ if (dependant && compareScope(scope, '=', dependant.scope)) {
132
+ let dependants = this.dependants.get(injectable);
133
+ if (!dependants) {
134
+ this.dependants.set(injectable, dependants = new Set());
135
+ }
136
+ dependants.add(dependant);
137
+ }
138
+ if (this.instances.has(injectable)) {
139
+ return Promise.resolve(this.instances.get(injectable).instance);
140
+ } else if (this.resolvers.has(injectable)) {
141
+ return this.resolvers.get(injectable);
142
+ } else {
143
+ const isLazy = checkIsLazyInjectable(injectable);
144
+ if (isLazy) {
145
+ const isOptional = checkIsOptional(injectable);
146
+ if (isOptional) return Promise.resolve(undefined);
147
+ return Promise.reject(new Error(`No instance provided for ${label || 'an'} injectable:\n${stack}`));
148
+ } else if (!checkIsFactoryInjectable(injectable)) {
149
+ throw new Error('Invalid injectable');
150
+ }
151
+ const resolution = this.createInjectableContext(dependencies, injectable).then((context)=>Promise.resolve(injectable.factory(context)).then((instance)=>({
152
+ instance,
153
+ context
154
+ }))).then(({ instance, context })=>{
155
+ if (compareScope(this.scope, '>=', scope)) this.instances.set(injectable, {
156
+ instance,
157
+ context
158
+ });
159
+ if (scope !== Scope.Transient) this.resolvers.delete(injectable);
160
+ return instance;
161
+ });
162
+ if (scope !== Scope.Transient) this.resolvers.set(injectable, resolution);
163
+ return resolution;
164
+ }
165
+ }
166
+ }
167
+ async disposeInjectable(injectable) {
168
+ try {
169
+ if (kFactoryInjectable in injectable) {
170
+ const { dispose } = injectable;
171
+ if (dispose) {
172
+ const { instance, context } = this.instances.get(injectable);
173
+ await dispose(instance, context);
174
+ }
175
+ }
176
+ } catch (cause) {
177
+ const error = new Error('Injectable disposal error. Potential memory leak', {
178
+ cause
179
+ });
180
+ this.application.logger.error(error);
181
+ } finally{
182
+ this.instances.delete(injectable);
183
+ }
184
+ }
185
+ }
186
+ function compareScope(left, operator, right) {
187
+ const leftScope = ScopeStrictness[left];
188
+ const rightScope = ScopeStrictness[right];
189
+ switch(operator){
190
+ case '=':
191
+ return leftScope === rightScope;
192
+ case '!=':
193
+ return leftScope !== rightScope;
194
+ case '>':
195
+ return leftScope > rightScope;
196
+ case '<':
197
+ return leftScope < rightScope;
198
+ case '>=':
199
+ return leftScope >= rightScope;
200
+ case '<=':
201
+ return leftScope <= rightScope;
202
+ default:
203
+ throw new Error('Invalid operator');
204
+ }
205
+ }
206
+ export const checkIsLazyInjectable = (injectable)=>kLazyInjectable in injectable;
207
+ export const checkIsFactoryInjectable = (injectable)=>kFactoryInjectable in injectable;
208
+ export const checkIsValueInjectable = (injectable)=>kValueInjectable in injectable;
209
+ export const checkIsInjectable = (injectable)=>kInjectable in injectable;
210
+ export const checkIsOptional = (injectable)=>kOptionalDependency in injectable;
211
+ export function getInjectableScope(injectable) {
212
+ let scope = injectable.scope;
213
+ const deps = Object.values(injectable.dependencies);
214
+ for (const dependency of deps){
215
+ const injectable = getDepedencencyInjectable(dependency);
216
+ const dependencyScope = getInjectableScope(injectable);
217
+ if (compareScope(dependencyScope, '>', scope)) {
218
+ scope = dependencyScope;
219
+ }
220
+ }
221
+ return scope;
222
+ }
223
+ export function getDepedencencyInjectable(dependency) {
224
+ if (kOptionalDependency in dependency) {
225
+ return dependency.injectable;
226
+ }
227
+ return dependency;
228
+ }
229
+ export function markOptional(injectable) {
230
+ return {
231
+ [kOptionalDependency]: true,
232
+ injectable
233
+ };
234
+ }
235
+ export function createLazyInjectable(scope = Scope.Global, label) {
236
+ return Object.freeze({
237
+ scope,
238
+ dependencies: {},
239
+ label,
240
+ stack: tryCaptureStackTrace(),
241
+ [kInjectable]: true,
242
+ [kLazyInjectable]: true
243
+ });
244
+ }
245
+ export function createValueInjectable(value, label) {
246
+ return Object.freeze({
247
+ value,
248
+ scope: Scope.Global,
249
+ dependencies: {},
250
+ label,
251
+ stack: tryCaptureStackTrace(),
252
+ [kInjectable]: true,
253
+ [kValueInjectable]: true
254
+ });
255
+ }
256
+ export function createFactoryInjectable(paramsOrFactory, label) {
257
+ const isFactory = typeof paramsOrFactory === 'function';
258
+ const params = isFactory ? {
259
+ factory: paramsOrFactory
260
+ } : paramsOrFactory;
261
+ const injectable = {
262
+ dependencies: params.dependencies ?? {},
263
+ scope: params.scope ?? Scope.Global,
264
+ factory: params.factory,
265
+ dispose: params.dispose,
266
+ label,
267
+ stack: tryCaptureStackTrace(),
268
+ [kInjectable]: true,
269
+ [kFactoryInjectable]: true
270
+ };
271
+ const actualScope = getInjectableScope(injectable);
272
+ if (!isFactory && params.scope && ScopeStrictness[actualScope] > ScopeStrictness[params.scope]) throw new Error(`Invalid scope ${params.scope} for factory injectable: dependencies have stricter scope - ${actualScope}`);
273
+ injectable.scope = actualScope;
274
+ return Object.freeze(injectable);
275
+ }
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../lib/container.ts"],"sourcesContent":["import type { Async } from '@nmtjs/common'\nimport { tryCaptureStackTrace } from '@nmtjs/common'\nimport {\n kFactoryInjectable,\n kInjectable,\n kLazyInjectable,\n kOptionalDependency,\n kValueInjectable,\n} from './constants.ts'\nimport { Scope } from './enums.ts'\nimport type { Logger } from './logger.ts'\nimport type { Registry } from './registry.ts'\n\nconst ScopeStrictness = {\n [Scope.Global]: 0,\n [Scope.Connection]: 1,\n [Scope.Call]: 2,\n [Scope.Transient]: 3,\n}\n\nexport type DependencyOptional<T extends AnyInjectable = AnyInjectable> = {\n [kOptionalDependency]: any\n injectable: T\n}\n\nexport type Depedency = DependencyOptional | AnyInjectable\n\nexport type Dependencies = Record<string, Depedency>\n\nexport type ResolveInjectableType<T extends AnyInjectable> =\n T extends Injectable<infer Type, any, any> ? Type : never\n\nexport interface Dependant<Deps extends Dependencies = Dependencies> {\n dependencies: Deps\n label?: string\n stack?: string\n}\n\nexport type DependencyInjectable<T extends Depedency> = T extends AnyInjectable\n ? T\n : T extends DependencyOptional\n ? T['injectable']\n : never\n\nexport type DependencyContext<Deps extends Dependencies> = {\n readonly [K in keyof Deps as Deps[K] extends AnyInjectable\n ? K\n : never]: Deps[K] extends AnyInjectable\n ? ResolveInjectableType<Deps[K]>\n : never\n} & {\n readonly [K in keyof Deps as Deps[K] extends DependencyOptional\n ? K\n : never]?: Deps[K] extends DependencyOptional\n ? ResolveInjectableType<Deps[K]['injectable']>\n : never\n}\n\nexport type InjectableFactoryType<\n InjectableType,\n InjectableDeps extends Dependencies,\n> = (context: DependencyContext<InjectableDeps>) => Async<InjectableType>\n\nexport type InjectableDisposeType<\n InjectableType,\n InjectableDeps extends Dependencies,\n> = (\n instance: InjectableType,\n context: DependencyContext<InjectableDeps>,\n) => any\n\nexport interface LazyInjectable<T, S extends Scope = Scope.Global>\n extends Dependant<{}> {\n scope: S\n [kInjectable]: any\n [kLazyInjectable]: T\n}\n\nexport interface ValueInjectable<T> extends Dependant<{}> {\n scope: Scope.Global\n value: T\n [kInjectable]: any\n [kValueInjectable]: any\n}\n\nexport interface FactoryInjectable<\n T,\n D extends Dependencies = {},\n S extends Scope = Scope.Global,\n> extends Dependant<D> {\n scope: S\n factory(context: DependencyContext<D>): Async<T>\n dispose?(instance: T, context: DependencyContext<D>): any\n [kInjectable]: any\n [kFactoryInjectable]: any\n}\n\nexport type Injectable<\n InjectableValue = any,\n InjectableDeps extends Dependencies = {},\n InjectableScope extends Scope = Scope,\n> =\n | LazyInjectable<InjectableValue, InjectableScope>\n | ValueInjectable<InjectableValue>\n | FactoryInjectable<InjectableValue, InjectableDeps, InjectableScope>\n\nexport type AnyInjectable<T = any, S extends Scope = Scope> = Injectable<\n T,\n any,\n S\n>\n\nexport class Container {\n readonly instances = new Map<AnyInjectable, { instance: any; context: any }>()\n private readonly resolvers = new Map<AnyInjectable, Promise<any>>()\n private readonly injectables = new Set<AnyInjectable>()\n private readonly dependants = new Map<AnyInjectable, Set<AnyInjectable>>()\n\n constructor(\n private readonly application: {\n registry: Registry\n logger: Logger\n },\n public readonly scope: Exclude<Scope, Scope.Transient> = Scope.Global,\n private readonly parent?: Container,\n ) {\n if ((scope as any) === Scope.Transient) {\n throw new Error('Invalid scope')\n }\n }\n\n async load() {\n const traverse = (dependencies: Dependencies) => {\n for (const key in dependencies) {\n const dependency = dependencies[key]\n const injectable = getDepedencencyInjectable(dependency)\n this.injectables.add(injectable)\n traverse(injectable.dependencies)\n }\n }\n\n for (const dependant of this.application.registry.getDependants()) {\n traverse(dependant.dependencies)\n }\n\n const injectables = Array.from(this.findCurrentScopeInjectables())\n await Promise.all(injectables.map((injectable) => this.resolve(injectable)))\n }\n\n fork(scope: Exclude<Scope, Scope.Transient>) {\n return new Container(this.application, scope, this)\n }\n\n async dispose() {\n this.application.logger.trace('Disposing [%s] scope context...', this.scope)\n\n // Loop through all instances and dispose them,\n // until there are no more instances left\n while (this.instances.size) {\n for (const injectable of this.instances.keys()) {\n const dependants = this.dependants.get(injectable)\n // Firstly, dispose instances that other injectables don't depend on\n if (!dependants?.size) {\n await this.disposeInjectable(injectable)\n for (const dependants of this.dependants.values()) {\n // Clear current istances as a dependant for other injectables\n if (dependants.has(injectable)) {\n dependants.delete(injectable)\n }\n }\n }\n }\n }\n\n this.instances.clear()\n this.injectables.clear()\n this.resolvers.clear()\n this.dependants.clear()\n }\n\n containsWithinSelf(injectable: AnyInjectable) {\n return this.instances.has(injectable) || this.resolvers.has(injectable)\n }\n\n contains(injectable: AnyInjectable): boolean {\n return (\n this.containsWithinSelf(injectable) ||\n (this.parent?.contains(injectable) ?? false)\n )\n }\n\n get<T extends AnyInjectable>(injectable: T): ResolveInjectableType<T> {\n if (this.instances.has(injectable)) {\n return this.instances.get(injectable)!.instance\n }\n\n if (this.parent?.contains(injectable)) {\n return this.parent.get(injectable)\n }\n\n throw new Error('No instance found')\n }\n\n resolve<T extends AnyInjectable>(injectable: T) {\n return this.resolveInjectable(injectable)\n }\n\n async createContext<T extends Dependencies>(dependencies: T) {\n return this.createInjectableContext(dependencies)\n }\n\n private async createInjectableContext<T extends Dependencies>(\n dependencies: T,\n dependant?: AnyInjectable,\n ) {\n const injections: Record<string, any> = {}\n const deps = Object.entries(dependencies)\n const resolvers: Promise<any>[] = Array(deps.length)\n for (let i = 0; i < deps.length; i++) {\n const [key, dependency] = deps[i]\n const injectable = getDepedencencyInjectable(dependency)\n const resolver = this.resolveInjectable(injectable, dependant)\n resolvers[i] = resolver.then((value) => (injections[key] = value))\n }\n await Promise.all(resolvers)\n return Object.freeze(injections) as DependencyContext<T>\n }\n\n async provide<T extends AnyInjectable>(\n injectable: T,\n instance: ResolveInjectableType<T>,\n ) {\n if (compareScope(injectable.scope, '>', this.scope)) {\n throw new Error('Invalid scope') // TODO: more informative error\n }\n this.instances.set(injectable, { instance, context: undefined })\n }\n\n satisfies(injectable: AnyInjectable) {\n return compareScope(injectable.scope, '<=', this.scope)\n }\n\n private *findCurrentScopeInjectables() {\n for (const injectable of this.injectables) {\n if (injectable.scope === this.scope) {\n yield injectable\n }\n }\n }\n\n private resolveInjectable<T extends AnyInjectable>(\n injectable: T,\n dependant?: AnyInjectable,\n ): Promise<ResolveInjectableType<T>> {\n if (dependant && compareScope(dependant.scope, '<', injectable.scope)) {\n throw new Error('Invalid scope: dependant is looser than injectable') // TODO: more informative error\n }\n\n if (checkIsValueInjectable(injectable)) {\n return Promise.resolve(injectable.value)\n } else if (\n this.parent?.contains(injectable) ||\n (this.parent?.satisfies(injectable) &&\n compareScope(this.parent.scope, '<', this.scope))\n ) {\n return this.parent.resolveInjectable(injectable, dependant)\n } else {\n const { scope, dependencies, stack, label } = injectable\n\n if (dependant && compareScope(scope, '=', dependant.scope)) {\n let dependants = this.dependants.get(injectable)\n if (!dependants) {\n this.dependants.set(injectable, (dependants = new Set()))\n }\n dependants.add(dependant)\n }\n\n if (this.instances.has(injectable)) {\n return Promise.resolve(this.instances.get(injectable)!.instance)\n } else if (this.resolvers.has(injectable)) {\n return this.resolvers.get(injectable)!\n } else {\n const isLazy = checkIsLazyInjectable(injectable)\n\n if (isLazy) {\n const isOptional = checkIsOptional(injectable)\n if (isOptional) return Promise.resolve(undefined as any)\n return Promise.reject(\n new Error(\n `No instance provided for ${label || 'an'} injectable:\\n${stack}`,\n ),\n )\n } else if (!checkIsFactoryInjectable(injectable)) {\n throw new Error('Invalid injectable')\n }\n\n const resolution = this.createInjectableContext(\n dependencies,\n injectable,\n )\n .then((context) =>\n Promise.resolve(injectable.factory(context)).then((instance) => ({\n instance,\n context,\n })),\n )\n .then(({ instance, context }) => {\n if (compareScope(this.scope, '>=', scope))\n this.instances.set(injectable, { instance, context })\n if (scope !== Scope.Transient) this.resolvers.delete(injectable)\n return instance\n })\n if (scope !== Scope.Transient)\n this.resolvers.set(injectable, resolution)\n return resolution\n }\n }\n }\n\n private async disposeInjectable(injectable: AnyInjectable) {\n try {\n if (kFactoryInjectable in injectable) {\n const { dispose } = injectable\n if (dispose) {\n const { instance, context } = this.instances.get(injectable)!\n await dispose(instance, context)\n }\n }\n } catch (cause) {\n const error = new Error(\n 'Injectable disposal error. Potential memory leak',\n { cause },\n )\n this.application.logger.error(error)\n } finally {\n this.instances.delete(injectable)\n }\n }\n}\n\nfunction compareScope(\n left: Scope,\n operator: '>' | '<' | '>=' | '<=' | '=' | '!=',\n right: Scope,\n) {\n const leftScope = ScopeStrictness[left]\n const rightScope = ScopeStrictness[right]\n switch (operator) {\n case '=':\n return leftScope === rightScope\n case '!=':\n return leftScope !== rightScope\n case '>':\n return leftScope > rightScope\n case '<':\n return leftScope < rightScope\n case '>=':\n return leftScope >= rightScope\n case '<=':\n return leftScope <= rightScope\n default:\n throw new Error('Invalid operator')\n }\n}\n\nexport const checkIsLazyInjectable = (\n injectable: any,\n): injectable is LazyInjectable<any> => kLazyInjectable in injectable\nexport const checkIsFactoryInjectable = (\n injectable: any,\n): injectable is FactoryInjectable<any> => kFactoryInjectable in injectable\nexport const checkIsValueInjectable = (\n injectable: any,\n): injectable is ValueInjectable<any> => kValueInjectable in injectable\nexport const checkIsInjectable = (\n injectable: any,\n): injectable is AnyInjectable<any> => kInjectable in injectable\nexport const checkIsOptional = (\n injectable: any,\n): injectable is DependencyOptional<any> => kOptionalDependency in injectable\n\nexport function getInjectableScope(injectable: AnyInjectable) {\n let scope = injectable.scope\n const deps = Object.values(injectable.dependencies as Dependencies)\n for (const dependency of deps) {\n const injectable = getDepedencencyInjectable(dependency)\n const dependencyScope = getInjectableScope(injectable)\n if (compareScope(dependencyScope, '>', scope)) {\n scope = dependencyScope\n }\n }\n return scope\n}\n\nexport function getDepedencencyInjectable(\n dependency: Depedency,\n): AnyInjectable {\n if (kOptionalDependency in dependency) {\n return dependency.injectable\n }\n return dependency\n}\n\nexport function markOptional<T extends AnyInjectable>(injectable: T) {\n return {\n [kOptionalDependency]: true,\n injectable,\n } as DependencyOptional<T>\n}\n\nexport function createLazyInjectable<T, S extends Scope = Scope.Global>(\n scope = Scope.Global as S,\n label?: string,\n): LazyInjectable<T, S> {\n return Object.freeze({\n scope,\n dependencies: {},\n label,\n stack: tryCaptureStackTrace(),\n [kInjectable]: true,\n [kLazyInjectable]: true as unknown as T,\n })\n}\n\nexport function createValueInjectable<T>(\n value: T,\n label?: string,\n): ValueInjectable<T> {\n return Object.freeze({\n value,\n scope: Scope.Global,\n dependencies: {},\n label,\n stack: tryCaptureStackTrace(),\n [kInjectable]: true,\n [kValueInjectable]: true,\n })\n}\n\nexport function createFactoryInjectable<\n T,\n D extends Dependencies = {},\n S extends Scope = Scope.Global,\n>(\n paramsOrFactory:\n | {\n dependencies?: D\n scope?: S\n factory: InjectableFactoryType<T, D>\n dispose?: InjectableDisposeType<T, D>\n }\n | InjectableFactoryType<T, D>,\n label?: string,\n): FactoryInjectable<T, D, S> {\n const isFactory = typeof paramsOrFactory === 'function'\n const params = isFactory\n ? {\n factory: paramsOrFactory,\n }\n : paramsOrFactory\n const injectable = {\n dependencies: (params.dependencies ?? {}) as D,\n scope: (params.scope ?? Scope.Global) as S,\n factory: params.factory,\n dispose: params.dispose,\n label,\n stack: tryCaptureStackTrace(),\n [kInjectable]: true,\n [kFactoryInjectable]: true,\n }\n const actualScope = getInjectableScope(injectable)\n if (\n !isFactory &&\n params.scope &&\n ScopeStrictness[actualScope] > ScopeStrictness[params.scope]\n )\n throw new Error(\n `Invalid scope ${params.scope} for factory injectable: dependencies have stricter scope - ${actualScope}`,\n )\n injectable.scope = actualScope as unknown as S\n return Object.freeze(injectable)\n}\n"],"names":["tryCaptureStackTrace","kFactoryInjectable","kInjectable","kLazyInjectable","kOptionalDependency","kValueInjectable","Scope","ScopeStrictness","Global","Connection","Call","Transient","Container","instances","resolvers","injectables","dependants","constructor","application","scope","parent","Map","Set","Error","load","traverse","dependencies","key","dependency","injectable","getDepedencencyInjectable","add","dependant","registry","getDependants","Array","from","findCurrentScopeInjectables","Promise","all","map","resolve","fork","dispose","logger","trace","size","keys","get","disposeInjectable","values","has","delete","clear","containsWithinSelf","contains","instance","resolveInjectable","createContext","createInjectableContext","injections","deps","Object","entries","length","i","resolver","then","value","freeze","provide","compareScope","set","context","undefined","satisfies","checkIsValueInjectable","stack","label","isLazy","checkIsLazyInjectable","isOptional","checkIsOptional","reject","checkIsFactoryInjectable","resolution","factory","cause","error","left","operator","right","leftScope","rightScope","checkIsInjectable","getInjectableScope","dependencyScope","markOptional","createLazyInjectable","createValueInjectable","createFactoryInjectable","paramsOrFactory","isFactory","params","actualScope"],"mappings":"AACA,SAASA,oBAAoB,QAAQ,gBAAe;AACpD,SACEC,kBAAkB,EAClBC,WAAW,EACXC,eAAe,EACfC,mBAAmB,EACnBC,gBAAgB,QACX,iBAAgB;AACvB,SAASC,KAAK,QAAQ,aAAY;AAIlC,MAAMC,kBAAkB;IACtB,CAACD,MAAME,MAAM,CAAC,EAAE;IAChB,CAACF,MAAMG,UAAU,CAAC,EAAE;IACpB,CAACH,MAAMI,IAAI,CAAC,EAAE;IACd,CAACJ,MAAMK,SAAS,CAAC,EAAE;AACrB;AA8FA,OAAO,MAAMC;;;;IACFC,UAAqE;IAC7DC,UAAkD;IAClDC,YAAsC;IACtCC,WAAyD;IAE1EC,YACE,AAAiBC,WAGhB,EACD,AAAgBC,QAAyCb,MAAME,MAAM,EACrE,AAAiBY,MAAkB,CACnC;aANiBF,cAAAA;aAIDC,QAAAA;aACCC,SAAAA;aAXVP,YAAY,IAAIQ;aACRP,YAAY,IAAIO;aAChBN,cAAc,IAAIO;aAClBN,aAAa,IAAIK;QAU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IAC7C,MAAME,SAASD,YACX;QACEhB,SAASe;IACX,IACAA;IACJ,MAAMxE,aAAa;QACjBH,cAAe6E,OAAO7E,YAAY,IAAI,CAAC;QACvCP,OAAQoF,OAAOpF,KAAK,IAAIb,MAAME,MAAM;QACpC8E,SAASiB,OAAOjB,OAAO;QACvB3C,SAAS4D,OAAO5D,OAAO;QACvBmC;QACAD,OAAO7E;QACP,CAACE,YAAY,EAAE;QACf,CAACD,mBAAmB,EAAE;IACxB;IACA,MAAMuG,cAAcT,mBAAmBlE;IACvC,IACE,CAACyE,aACDC,OAAOpF,KAAK,IACZZ,eAAe,CAACiG,YAAY,GAAGjG,eAAe,CAACgG,OAAOpF,KAAK,CAAC,EAE5D,MAAM,IAAII,MACR,CAAC,cAAc,EAAEgF,OAAOpF,KAAK,CAAC,4DAA4D,EAAEqF,YAAY,CAAC;IAE7G3E,WAAWV,KAAK,GAAGqF;IACnB,OAAO1C,OAAOO,MAAM,CAACxC;AACvB"}
@@ -0,0 +1,20 @@
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 = {}));
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../lib/enums.ts"],"sourcesContent":["export enum Scope {\n Global = 'Global',\n Connection = 'Connection',\n Call = 'Call',\n Transient = 'Transient',\n}\n\nexport enum Hook {\n BeforeInitialize = 'BeforeInitialize',\n AfterInitialize = 'AfterInitialize',\n BeforeStart = 'BeforeStart',\n AfterStart = 'AfterStart',\n BeforeStop = 'BeforeStop',\n AfterStop = 'AfterStop',\n BeforeTerminate = 'BeforeTerminate',\n AfterTerminate = 'AfterTerminate',\n OnConnect = 'OnConnect',\n OnDisconnect = 'OnDisconnect',\n}\n"],"names":["Scope","Hook"],"mappings":";UAAYA;;;;;GAAAA,UAAAA;;UAOAC;;;;;;;;;;;GAAAA,SAAAA"}
@@ -0,0 +1,37 @@
1
+ import { kHookCollection } from "./constants.js";
2
+ export class Hooks {
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) this[kHookCollection].set(name, hooks = new Set());
14
+ hooks.add(callback);
15
+ return ()=>this.remove(name, callback);
16
+ }
17
+ remove(name, callback) {
18
+ const hooks = this[kHookCollection].get(name);
19
+ if (hooks) hooks.delete(callback);
20
+ }
21
+ async call(name, options, ...args) {
22
+ const { concurrent = true, reverse = false } = options ?? {};
23
+ const hooks = this[kHookCollection].get(name);
24
+ if (!hooks) return;
25
+ const hooksArr = Array.from(hooks);
26
+ if (concurrent) {
27
+ await Promise.all(hooksArr.map((hook)=>hook(...args)));
28
+ } else {
29
+ if (reverse) hooksArr.reverse();
30
+ for (const hook of hooksArr)await hook(...args);
31
+ }
32
+ }
33
+ clear() {
34
+ this[kHookCollection].clear();
35
+ }
36
+ }
37
+ export const createErrForHook = (hook)=>`Error during [${hook}] hook`;
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../lib/hooks.ts"],"sourcesContent":["import type { Callback } from '@nmtjs/common'\nimport { kHookCollection } from './constants.ts'\nimport type { Hook } from './enums.ts'\n// import type { HookType } from './types.ts'\n\nexport interface HookType {\n [key: string]: (...args: any[]) => any\n // [Hook.AfterInitialize]: () => any\n // [Hook.BeforeStart]: () => any\n // [Hook.AfterStart]: () => any\n // [Hook.BeforeStop]: () => any\n // [Hook.AfterStop]: () => any\n // [Hook.BeforeTerminate]: () => any\n // [Hook.AfterTerminate]: () => any\n // [Hook.OnConnect]: (...args: any[]) => any\n // [Hook.OnDisconnect]: (...args: any[]) => any\n}\n\nexport type CallHook<T extends string> = (\n hook: T,\n ...args: T extends keyof HookType ? Parameters<HookType[T]> : any[]\n) => Promise<void>\n\nexport class Hooks {\n static merge(from: Hooks, to: Hooks) {\n for (const [name, callbacks] of from[kHookCollection]) {\n for (const callback of callbacks) {\n to.add(name, callback)\n }\n }\n }\n\n [kHookCollection] = new Map<string, Set<Callback>>()\n\n add(name: string, callback: Callback) {\n let hooks = this[kHookCollection].get(name)\n if (!hooks) this[kHookCollection].set(name, (hooks = new Set()))\n hooks.add(callback)\n return () => this.remove(name, callback)\n }\n\n remove(name: string, callback: Callback) {\n const hooks = this[kHookCollection].get(name)\n if (hooks) hooks.delete(callback)\n }\n\n async call<T extends string | Hook>(\n name: T,\n options: { concurrent?: boolean; reverse?: boolean } | undefined,\n ...args: T extends Hook ? Parameters<HookType[T]> : any[]\n ) {\n const { concurrent = true, reverse = false } = options ?? {}\n const hooks = this[kHookCollection].get(name)\n if (!hooks) return\n const hooksArr = Array.from(hooks)\n if (concurrent) {\n await Promise.all(hooksArr.map((hook) => hook(...args)))\n } else {\n if (reverse) hooksArr.reverse()\n for (const hook of hooksArr) await hook(...args)\n }\n }\n\n clear() {\n this[kHookCollection].clear()\n }\n}\n\nexport const createErrForHook = (hook: Hook | (object & string)) =>\n `Error during [${hook}] hook`\n"],"names":["kHookCollection","Hooks","merge","from","to","name","callbacks","callback","add","Map","hooks","get","set","Set","remove","delete","call","options","args","concurrent","reverse","hooksArr","Array","Promise","all","map","hook","clear","createErrForHook"],"mappings":"A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OAAO,MAAMC,mBAAmB,CAACF,OAC/B,CAAC,cAAc,EAAEA,KAAK,MAAM,CAAC,CAAA"}
@@ -0,0 +1,6 @@
1
+ import { createLazyInjectable } from "./container.js";
2
+ import { Scope } from "./enums.js";
3
+ const logger = createLazyInjectable(Scope.Global, 'Logger');
4
+ export const CoreInjectables = {
5
+ logger
6
+ };
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../lib/injectables.ts"],"sourcesContent":["import { createLazyInjectable } from './container.ts'\nimport { Scope } from './enums.ts'\nimport type { Logger } from './logger.ts'\n\nconst logger = createLazyInjectable<Logger>(Scope.Global, 'Logger')\n\nexport const CoreInjectables = { logger }\n"],"names":["createLazyInjectable","Scope","logger","Global","CoreInjectables"],"mappings":"AAAA,SAASA,oBAAoB,QAAQ,iBAAgB;AACrD,SAASC,KAAK,QAAQ,aAAY;AAGlC,MAAMC,SAASF,qBAA6BC,MAAME,MAAM,EAAE;AAE1D,OAAO,MAAMC,kBAAkB;IAAEF;AAAO,EAAC"}
@@ -0,0 +1,72 @@
1
+ import { threadId } from 'node:worker_threads';
2
+ import { pino, stdTimeFunctions } from 'pino';
3
+ import { build as pretty } from 'pino-pretty';
4
+ const bg = (value, color)=>`\x1b[${color}m${value}\x1b[0m`;
5
+ const fg = (value, color)=>`\x1b[38;5;${color}m${value}\x1b[0m`;
6
+ const levelColors = {
7
+ 10: 100,
8
+ 20: 102,
9
+ 30: 106,
10
+ 40: 104,
11
+ 50: 101,
12
+ 60: 105,
13
+ [Number.POSITIVE_INFINITY]: 0
14
+ };
15
+ const messageColors = {
16
+ 10: 0,
17
+ 20: 2,
18
+ 30: 6,
19
+ 40: 4,
20
+ 50: 1,
21
+ 60: 5,
22
+ [Number.POSITIVE_INFINITY]: 0
23
+ };
24
+ const levelLabels = {
25
+ 10: ' TRACE ',
26
+ 20: ' DEBUG ',
27
+ 30: ' INFO ',
28
+ 40: ' WARN ',
29
+ 50: ' ERROR ',
30
+ 60: ' FATAL ',
31
+ [Number.POSITIVE_INFINITY]: 'SILENT'
32
+ };
33
+ export const createLogger = (options = {}, $group)=>{
34
+ let { destinations, pinoOptions } = options;
35
+ if (!destinations || !destinations?.length) {
36
+ destinations = [
37
+ createConsolePrettyDestination('info')
38
+ ];
39
+ }
40
+ const lowestLevelValue = destinations.reduce((acc, destination)=>Math.min(acc, 'stream' in destination ? pino.levels.values[destination.level] : Number.POSITIVE_INFINITY), Number.POSITIVE_INFINITY);
41
+ const level = pino.levels.labels[lowestLevelValue];
42
+ return pino({
43
+ timestamp: stdTimeFunctions.isoTime,
44
+ ...pinoOptions,
45
+ level
46
+ }, pino.multistream(destinations)).child({
47
+ $group,
48
+ $threadId: threadId
49
+ });
50
+ };
51
+ export const createConsolePrettyDestination = (level, sync = true)=>({
52
+ level,
53
+ stream: pretty({
54
+ colorize: true,
55
+ ignore: 'hostname,$group,$threadId',
56
+ errorLikeObjectKeys: [
57
+ 'err',
58
+ 'error',
59
+ 'cause'
60
+ ],
61
+ messageFormat: (log, messageKey)=>{
62
+ const group = fg(`[${log.$group}]`, 11);
63
+ const msg = fg(log[messageKey], messageColors[log.level]);
64
+ const thread = fg(`(Thread-${log.$threadId})`, 89);
65
+ return `\x1b[0m${thread} ${group} ${msg}`;
66
+ },
67
+ customPrettifiers: {
68
+ level: (level)=>bg(levelLabels[level], levelColors[level])
69
+ },
70
+ sync
71
+ })
72
+ });
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../lib/logger.ts"],"sourcesContent":["import { threadId } from 'node:worker_threads'\nimport {\n type DestinationStream,\n type Level,\n type Logger as PinoLogger,\n type StreamEntry,\n pino,\n stdTimeFunctions,\n} from 'pino'\nimport { build as pretty } from 'pino-pretty'\n\nexport type Logger = PinoLogger\nexport type LoggingOptions = {\n destinations?: Array<DestinationStream | StreamEntry<Level>>\n pinoOptions?: any\n}\n\n// TODO: use node:util inspect\nconst bg = (value, color) => `\\x1b[${color}m${value}\\x1b[0m`\nconst fg = (value, color) => `\\x1b[38;5;${color}m${value}\\x1b[0m`\n\nconst levelColors = {\n 10: 100,\n 20: 102,\n 30: 106,\n 40: 104,\n 50: 101,\n 60: 105,\n [Number.POSITIVE_INFINITY]: 0,\n}\nconst messageColors = {\n 10: 0,\n 20: 2,\n 30: 6,\n 40: 4,\n 50: 1,\n 60: 5,\n [Number.POSITIVE_INFINITY]: 0,\n}\n\nconst levelLabels = {\n 10: ' TRACE ',\n 20: ' DEBUG ',\n 30: ' INFO ',\n 40: ' WARN ',\n 50: ' ERROR ',\n 60: ' FATAL ',\n [Number.POSITIVE_INFINITY]: 'SILENT',\n}\n\nexport const createLogger = (options: LoggingOptions = {}, $group: string) => {\n let { destinations, pinoOptions } = options\n\n if (!destinations || !destinations?.length) {\n destinations = [createConsolePrettyDestination('info')]\n }\n\n const lowestLevelValue = destinations!.reduce(\n (acc, destination) =>\n Math.min(\n acc,\n 'stream' in destination\n ? pino.levels.values[destination.level!]\n : Number.POSITIVE_INFINITY,\n ),\n Number.POSITIVE_INFINITY,\n )\n const level = pino.levels.labels[lowestLevelValue]\n return pino(\n {\n timestamp: stdTimeFunctions.isoTime,\n ...pinoOptions,\n level,\n },\n pino.multistream(destinations!),\n ).child({ $group, $threadId: threadId })\n}\n\nexport const createConsolePrettyDestination = (\n level: Level,\n sync = true,\n): StreamEntry => ({\n level,\n stream: pretty({\n colorize: true,\n ignore: 'hostname,$group,$threadId',\n errorLikeObjectKeys: ['err', 'error', 'cause'],\n messageFormat: (log, messageKey) => {\n const group = fg(`[${log.$group}]`, 11)\n const msg = fg(log[messageKey], messageColors[log.level as number])\n const thread = fg(`(Thread-${log.$threadId})`, 89)\n return `\\x1b[0m${thread} ${group} ${msg}`\n },\n customPrettifiers: {\n level: (level: any) => bg(levelLabels[level], levelColors[level]),\n },\n sync,\n }),\n})\n"],"names":["threadId","pino","stdTimeFunctions","build","pretty","bg","value","color","fg","levelColors","Number","POSITIVE_INFINITY","messageColors","levelLabels","createLogger","options","$group","destinations","pinoOptions","length","createConsolePrettyDestination","lowestLevelValue","reduce","acc","destination","Math","min","levels","values","level","labels","timestamp","isoTime","multistream","child","$threadId","sync","stream","colorize","ignore","errorLikeObjectKeys","messageFormat","log","messageKey","group","msg","thread","customPrettifiers"],"mappings":"AAAA,SAASA,QAAQ,QAAQ,sBAAqB;AAC9C,SAKEC,IAAI,EACJC,gBAAgB,QACX,OAAM;AACb,SAASC,SAASC,MAAM,QAAQ,cAAa;AAS7C,MAAMC,KAAK,CAACC,OAAOC,QAAU,CAAC,KAAK,EAAEA,MAAM,CAAC,EAAED,MAAM,OAAO,CAAC;AAC5D,MAAME,KAAK,CAACF,OAAOC,QAAU,CAAC,UAAU,EAAEA,MAAM,CAAC,EAAED,MAAM,OAAO,CAAC;AAEjE,MAAMG,cAAc;IAClB,IAAI;IACJ,IAAI;IACJ,IAAI;IACJ,IAAI;IACJ,IAAI;IACJ,IAAI;IACJ,CAACC,OAAOC,iBAAiB,CAAC,EAAE;AAC9B;AACA,MAAMC,gBAAgB;IACpB,IAAI;IACJ,IAAI;IACJ,IAAI;IACJ,IAAI;IACJ,IAAI;IACJ,IAAI;IACJ,CAACF,OAAOC,iBAAiB,CAAC,EAAE;AAC9B;AAEA,MAAME,cAAc;IAClB,IAAI;IACJ,IAAI;IACJ,IAAI;IACJ,IAAI;IACJ,IAAI;IACJ,IAAI;IACJ,CAACH,OAAOC,iBAAiB,CAAC,EAAE;AAC9B;AAEA,OAAO,MAAMG,eAAe,CAACC,UAA0B,CAAC,CAAC,EAAEC;IACzD,IAAI,EAAEC,YAAY,EAAEC,WAAW,EAAE,GAAGH;IAEpC,IAAI,CAACE,gBAAgB,CAACA,cAAcE,QAAQ;QAC1CF,eAAe;YAACG,+BAA+B;SAAQ;IACzD;IAEA,MAAMC,mBAAmBJ,aAAcK,MAAM,CAC3C,CAACC,KAAKC,cACJC,KAAKC,GAAG,CACNH,KACA,YAAYC,cACRvB,KAAK0B,MAAM,CAACC,MAAM,CAACJ,YAAYK,KAAK,CAAE,GACtCnB,OAAOC,iBAAiB,GAEhCD,OAAOC,iBAAiB;IAE1B,MAAMkB,QAAQ5B,KAAK0B,MAAM,CAACG,MAAM,CAACT,iBAAiB;IAClD,OAAOpB,KACL;QACE8B,WAAW7B,iBAAiB8B,OAAO;QACnC,GAAGd,WAAW;QACdW;IACF,GACA5B,KAAKgC,WAAW,CAAChB,eACjBiB,KAAK,CAAC;QAAElB;QAAQmB,WAAWnC;IAAS;AACxC,EAAC;AAED,OAAO,MAAMoB,iCAAiC,CAC5CS,OACAO,OAAO,IAAI,GACM,CAAA;QACjBP;QACAQ,QAAQjC,OAAO;YACbkC,UAAU;YACVC,QAAQ;YACRC,qBAAqB;gBAAC;gBAAO;gBAAS;aAAQ;YAC9CC,eAAe,CAACC,KAAKC;gBACnB,MAAMC,QAAQpC,GAAG,CAAC,CAAC,EAAEkC,IAAI1B,MAAM,CAAC,CAAC,CAAC,EAAE;gBACpC,MAAM6B,MAAMrC,GAAGkC,GAAG,CAACC,WAAW,EAAE/B,aAAa,CAAC8B,IAAIb,KAAK,CAAW;gBAClE,MAAMiB,SAAStC,GAAG,CAAC,QAAQ,EAAEkC,IAAIP,SAAS,CAAC,CAAC,CAAC,EAAE;gBAC/C,OAAO,CAAC,OAAO,EAAEW,OAAO,CAAC,EAAEF,MAAM,CAAC,EAAEC,IAAI,CAAC;YAC3C;YACAE,mBAAmB;gBACjBlB,OAAO,CAACA,QAAexB,GAAGQ,WAAW,CAACgB,MAAM,EAAEpB,WAAW,CAACoB,MAAM;YAClE;YACAO;QACF;IACF,CAAA,EAAE"}
@@ -0,0 +1,6 @@
1
+ import { kPlugin } from "./constants.js";
2
+ export const createPlugin = (name, init)=>({
3
+ name,
4
+ init,
5
+ [kPlugin]: true
6
+ });
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../lib/plugin.ts"],"sourcesContent":["import type { Async } from '@nmtjs/common'\nimport { kPlugin } from './constants.ts'\nimport type { PluginContext } from './types.ts'\n\nexport interface BasePlugin<\n Type = any,\n Options = unknown,\n Context extends PluginContext = PluginContext,\n> {\n name: string\n init: (context: Context, options: Options) => Async<Type>\n}\n\nexport interface Plugin<\n Type = void,\n Options = unknown,\n Context extends PluginContext = PluginContext,\n> extends BasePlugin<Type, Options, Context> {\n [kPlugin]: any\n}\n\nexport const createPlugin = <Options = unknown, Type = void>(\n name: string,\n init: Plugin<Type, Options>['init'],\n): Plugin<Type, Options> => ({ name, init, [kPlugin]: true })\n"],"names":["kPlugin","createPlugin","name","init"],"mappings":"AACA,SAASA,OAAO,QAAQ,iBAAgB;AAoBxC,OAAO,MAAMC,eAAe,CAC1BC,MACAC,OAC2B,CAAA;QAAED;QAAMC;QAAM,CAACH,QAAQ,EAAE;IAAK,CAAA,EAAE"}
@@ -0,0 +1,25 @@
1
+ import { getInjectableScope } from "./container.js";
2
+ import { Scope } from "./enums.js";
3
+ import { Hooks } from "./hooks.js";
4
+ export class Registry {
5
+ application;
6
+ hooks;
7
+ constructor(application){
8
+ this.application = application;
9
+ this.hooks = new Hooks();
10
+ }
11
+ registerHooks(hooks) {
12
+ Hooks.merge(hooks, this.hooks);
13
+ }
14
+ registerHook(name, callback) {
15
+ this.hooks.add(name, callback);
16
+ }
17
+ *getDependants() {}
18
+ clear() {
19
+ this.hooks.clear();
20
+ }
21
+ }
22
+ export const scopeErrorMessage = (name, scope = Scope.Global)=>`${name} must be a ${scope} scope (including all nested dependencies)`;
23
+ export function hasInvalidScopeDeps(injectables, scope = Scope.Global) {
24
+ return injectables.some((injectable)=>getInjectableScope(injectable) !== scope);
25
+ }
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../lib/registry.ts"],"sourcesContent":["import {\n type AnyInjectable,\n type Dependant,\n getInjectableScope,\n} from './container.ts'\nimport { type Hook, Scope } from './enums.ts'\nimport { type HookType, Hooks } from './hooks.ts'\nimport type { Logger } from './logger.ts'\n\nexport class Registry {\n readonly hooks = new Hooks()\n\n constructor(\n protected readonly application: {\n logger: Logger\n },\n ) {}\n\n registerHooks<T extends Hooks>(hooks: T) {\n Hooks.merge(hooks, this.hooks)\n }\n\n registerHook<T extends Hook>(name: T, callback: HookType[T]) {\n this.hooks.add(name, callback)\n }\n\n *getDependants(): Generator<Dependant> {}\n\n clear() {\n this.hooks.clear()\n }\n}\n\nexport const scopeErrorMessage = (name, scope = Scope.Global) =>\n `${name} must be a ${scope} scope (including all nested dependencies)`\n\nexport function hasInvalidScopeDeps(\n injectables: AnyInjectable[],\n scope = Scope.Global,\n) {\n return injectables.some(\n (injectable) => getInjectableScope(injectable) !== scope,\n )\n}\n"],"names":["getInjectableScope","Scope","Hooks","Registry","hooks","constructor","application","registerHooks","merge","registerHook","name","callback","add","getDependants","clear","scopeErrorMessage","scope","Global","hasInvalidScopeDeps","injectables","some","injectable"],"mappings":"AAAA,SAGEA,kBAAkB,QACb,iBAAgB;AACvB,SAAoBC,KAAK,QAAQ,aAAY;AAC7C,SAAwBC,KAAK,QAAQ,aAAY;AAGjD,OAAO,MAAMC;;IACFC,MAAmB;IAE5BC,YACE,AAAmBC,WAElB,CACD;aAHmBA,cAAAA;aAHZF,QAAQ,IAAIF;IAMlB;IAEHK,cAA+BH,KAAQ,EAAE;QACvCF,MAAMM,KAAK,CAACJ,OAAO,IAAI,CAACA,KAAK;IAC/B;IAEAK,aAA6BC,IAAO,EAAEC,QAAqB,EAAE;QAC3D,IAAI,CAACP,KAAK,CAACQ,GAAG,CAACF,MAAMC;IACvB;IAEA,CAACE,gBAAsC,CAAC;IAExCC,QAAQ;QACN,IAAI,CAACV,KAAK,CAACU,KAAK;IAClB;AACF;AAEA,OAAO,MAAMC,oBAAoB,CAACL,MAAMM,QAAQf,MAAMgB,MAAM,GAC1D,CAAC,EAAEP,KAAK,WAAW,EAAEM,MAAM,0CAA0C,CAAC,CAAA;AAExE,OAAO,SAASE,oBACdC,WAA4B,EAC5BH,QAAQf,MAAMgB,MAAM;IAEpB,OAAOE,YAAYC,IAAI,CACrB,CAACC,aAAerB,mBAAmBqB,gBAAgBL;AAEvD"}
@@ -0,0 +1 @@
1
+ export { };
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../lib/types.ts"],"sourcesContent":["import type { Container } from './container.ts'\nimport type { Hooks } from './hooks.ts'\nimport type { Logger } from './logger.ts'\nimport type { Registry } from './registry.ts'\n\nexport interface PluginContext {\n logger: Logger\n registry: Registry\n hooks: Hooks\n container: Container\n}\n\nexport type Pattern = RegExp | string | ((value: string) => boolean)\n"],"names":[],"mappings":"AAYA,WAAoE"}
@@ -0,0 +1,32 @@
1
+ export function match(value, pattern) {
2
+ if (typeof pattern === 'function') {
3
+ return pattern(value);
4
+ } else if (typeof pattern === 'string') {
5
+ if (pattern === '*' || pattern === '**') {
6
+ return true;
7
+ } else if (pattern.at(0) === '*' && pattern.at(-1) === '*') {
8
+ return value.includes(pattern.slice(1, -1));
9
+ } else if (pattern.at(-1) === '*') {
10
+ return value.startsWith(pattern.slice(0, -1));
11
+ } else if (pattern.at(0) === '*') {
12
+ return value.endsWith(pattern.slice(1));
13
+ } else {
14
+ return value === pattern;
15
+ }
16
+ } else {
17
+ return pattern.test(value);
18
+ }
19
+ }
20
+ export function isJsFile(name) {
21
+ if (name.endsWith('.d.ts')) return false;
22
+ const leading = name.split('.').slice(1);
23
+ const ext = leading.join('.');
24
+ return [
25
+ 'js',
26
+ 'mjs',
27
+ 'cjs',
28
+ 'ts',
29
+ 'mts',
30
+ 'cts'
31
+ ].includes(ext);
32
+ }