@nmtjs/core 0.14.4 → 0.15.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.md CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2024 Denis Ilchyshyn
1
+ Copyright (c) 2025 Denys Ilchyshyn
2
2
 
3
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
4
 
package/README.md CHANGED
@@ -6,4 +6,4 @@
6
6
  - binary data streaming and event subscriptions
7
7
  - contract-based API
8
8
  - end-to-end type safety
9
- - CPU-intensive task execution on separate workers
9
+ - CPU-intensive task execution on separate workers
package/package.json CHANGED
@@ -2,38 +2,36 @@
2
2
  "name": "@nmtjs/core",
3
3
  "type": "module",
4
4
  "exports": {
5
- ".": {
6
- "types": "./dist/index.d.ts",
7
- "import": "./dist/index.js",
8
- "module-sync": "./dist/index.js"
9
- },
10
- "./constants": {
11
- "types": "./dist/constants.d.ts",
12
- "import": "./dist/constants.js",
13
- "module-sync": "./dist/constants.js"
14
- }
5
+ ".": "./dist/index.js",
6
+ "./constants": "./dist/constants.js"
15
7
  },
16
8
  "peerDependencies": {
17
9
  "pino": "^9.6.0",
18
10
  "pino-pretty": "^13.0.0",
19
- "@nmtjs/common": "0.14.4",
20
- "@nmtjs/type": "0.14.4"
11
+ "@nmtjs/common": "0.15.0-beta.1",
12
+ "@nmtjs/type": "0.15.0-beta.1"
21
13
  },
22
14
  "devDependencies": {
23
- "@types/node": "^20",
15
+ "@types/node": "^24",
24
16
  "pino": "^9.6.0",
25
17
  "pino-pretty": "^13.0.0",
26
- "@nmtjs/common": "0.14.4",
27
- "@nmtjs/type": "0.14.4"
18
+ "@nmtjs/common": "0.15.0-beta.1",
19
+ "@nmtjs/type": "0.15.0-beta.1"
28
20
  },
29
21
  "files": [
30
22
  "dist",
31
23
  "LICENSE.md",
32
24
  "README.md"
33
25
  ],
34
- "version": "0.14.4",
26
+ "dependencies": {
27
+ "hookable": "6.0.0-rc.1",
28
+ "pino-std-serializers": "^7.0.0"
29
+ },
30
+ "version": "0.15.0-beta.1",
35
31
  "scripts": {
32
+ "clean-build": "rm -rf ./dist",
36
33
  "build": "tsc",
34
+ "dev": "tsc --watch",
37
35
  "type-check": "tsc --noEmit"
38
36
  }
39
37
  }
@@ -1,24 +0,0 @@
1
- export declare const kOptionalDependency: unique symbol;
2
- export type kOptionalDependency = typeof kOptionalDependency;
3
- export declare const kInjectable: unique symbol;
4
- export type kInjectable = typeof kInjectable;
5
- export declare const kLazyInjectable: unique symbol;
6
- export type kLazyInjectable = typeof kLazyInjectable;
7
- export declare const kValueInjectable: unique symbol;
8
- export type kValueInjectable = typeof kValueInjectable;
9
- export declare const kFactoryInjectable: unique symbol;
10
- export type kFactoryInjectable = typeof kFactoryInjectable;
11
- export declare const kClassInjectable: unique symbol;
12
- export type kClassInjectable = typeof kClassInjectable;
13
- export declare const kClassInjectableCreate: unique symbol;
14
- export type kClassInjectableCreate = typeof kClassInjectableCreate;
15
- export declare const kClassInjectableDispose: unique symbol;
16
- export type kClassInjectableDispose = typeof kClassInjectableDispose;
17
- export declare const kProvider: unique symbol;
18
- export type kProvider = typeof kProvider;
19
- export declare const kHookCollection: unique symbol;
20
- export type kHookCollection = typeof kHookCollection;
21
- export declare const kPlugin: unique symbol;
22
- export type kPlugin = typeof kPlugin;
23
- export declare const kMetadata: unique symbol;
24
- export type kMetadata = typeof kMetadata;
package/dist/constants.js DELETED
@@ -1,12 +0,0 @@
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 kClassInjectable = Symbol.for('neemata:ClassInjectableKey');
7
- export const kClassInjectableCreate = Symbol.for('neemata:ClassInjectableCreateKey');
8
- export const kClassInjectableDispose = Symbol.for('neemata:ClassInjectableDisposeKey');
9
- export const kProvider = Symbol.for('neemata:ProviderKey');
10
- export const kHookCollection = Symbol.for('neemata:HookCollectionKey');
11
- export const kPlugin = Symbol.for('neemata:PluginKey');
12
- export const kMetadata = Symbol.for('neemata:MetadataKey');
@@ -1,46 +0,0 @@
1
- import type { AnyInjectable, Dependencies, DependencyContext, ResolveInjectableType } from './injectables.ts';
2
- import type { Logger } from './logger.ts';
3
- import type { Registry } from './registry.ts';
4
- import { Scope } from './enums.ts';
5
- type InstanceWrapper = {
6
- private: any;
7
- public: any;
8
- context: any;
9
- };
10
- type ContainerOptions = {
11
- registry: Registry;
12
- logger: Logger;
13
- };
14
- export declare class Container {
15
- private readonly application;
16
- readonly scope: Exclude<Scope, Scope.Transient>;
17
- private readonly parent?;
18
- readonly instances: Map<AnyInjectable, InstanceWrapper[]>;
19
- private readonly resolvers;
20
- private readonly injectables;
21
- private readonly dependants;
22
- private disposing;
23
- constructor(application: ContainerOptions, scope?: Exclude<Scope, Scope.Transient>, parent?: Container | undefined);
24
- load(): Promise<void>;
25
- fork(scope: Exclude<Scope, Scope.Transient>): Container;
26
- dispose(): Promise<void>;
27
- containsWithinSelf(injectable: AnyInjectable): boolean;
28
- contains(injectable: AnyInjectable): boolean;
29
- get<T extends AnyInjectable>(injectable: T): ResolveInjectableType<T>;
30
- resolve<T extends AnyInjectable>(injectable: T): Promise<ResolveInjectableType<T>>;
31
- createContext<T extends Dependencies>(dependencies: T): Promise<DependencyContext<T>>;
32
- private createInjectableContext;
33
- provide<T extends AnyInjectable>(injectable: T, instance: ResolveInjectableType<T>): Promise<void>;
34
- satisfies(injectable: AnyInjectable): boolean;
35
- private findCurrentScopeInjectables;
36
- private resolveInjectable;
37
- private createResolution;
38
- private createInjectFunction;
39
- private createDisposeFunction;
40
- private getDisposalOrder;
41
- private disposeInjectableInstances;
42
- private disposeInjectableInstance;
43
- }
44
- export type InjectFn = ReturnType<Container['createInjectFunction']>;
45
- export type DisposeFn = ReturnType<Container['createDisposeFunction']>;
46
- export {};
package/dist/container.js DELETED
@@ -1,309 +0,0 @@
1
- import assert from 'node:assert';
2
- import { tryCaptureStackTrace } from '@nmtjs/common';
3
- import { kClassInjectableCreate, kClassInjectableDispose } from "./constants.js";
4
- import { Scope } from "./enums.js";
5
- import { CoreInjectables, compareScope, createExtendableClassInjectable, createValueInjectable, getDepedencencyInjectable, isClassInjectable, isFactoryInjectable, isInjectable, isLazyInjectable, isOptionalInjectable, isValueInjectable, } from "./injectables.js";
6
- export class Container {
7
- application;
8
- scope;
9
- parent;
10
- instances = new Map();
11
- resolvers = new Map();
12
- injectables = new Set();
13
- dependants = new Map();
14
- // private readonly transients = new Map<any, any>()
15
- disposing = false;
16
- constructor(application, scope = Scope.Global, parent) {
17
- this.application = application;
18
- this.scope = scope;
19
- this.parent = parent;
20
- if (scope === Scope.Transient) {
21
- throw new Error('Invalid scope');
22
- }
23
- this.provide(CoreInjectables.inject, this.createInjectFunction());
24
- this.provide(CoreInjectables.dispose, this.createDisposeFunction());
25
- this.provide(CoreInjectables.registry, application.registry);
26
- }
27
- async load() {
28
- const traverse = (dependencies) => {
29
- for (const key in dependencies) {
30
- const dependency = dependencies[key];
31
- const injectable = getDepedencencyInjectable(dependency);
32
- this.injectables.add(injectable);
33
- traverse(injectable.dependencies);
34
- }
35
- };
36
- for (const dependant of this.application.registry.getDependants()) {
37
- traverse(dependant.dependencies);
38
- }
39
- const injectables = Array.from(this.findCurrentScopeInjectables());
40
- await Promise.all(injectables.map((injectable) => this.resolve(injectable)));
41
- }
42
- fork(scope) {
43
- return new Container(this.application, scope, this);
44
- }
45
- async dispose() {
46
- this.application.logger.trace('Disposing [%s] scope context...', this.scope);
47
- // Prevent new resolutions during disposal
48
- this.disposing = true;
49
- // Get proper disposal order using topological sort
50
- const disposalOrder = this.getDisposalOrder();
51
- // Dispose in the correct order
52
- for (const injectable of disposalOrder) {
53
- if (this.instances.has(injectable)) {
54
- await this.disposeInjectableInstances(injectable);
55
- }
56
- }
57
- this.instances.clear();
58
- this.injectables.clear();
59
- this.resolvers.clear();
60
- this.dependants.clear();
61
- this.disposing = false;
62
- }
63
- containsWithinSelf(injectable) {
64
- return this.instances.has(injectable) || this.resolvers.has(injectable);
65
- }
66
- contains(injectable) {
67
- return (this.containsWithinSelf(injectable) ||
68
- (this.parent?.contains(injectable) ?? false));
69
- }
70
- get(injectable) {
71
- if (injectable.scope === Scope.Transient) {
72
- throw new Error('Cannot get transient injectable directly');
73
- }
74
- if (this.instances.has(injectable)) {
75
- return this.instances.get(injectable).at(0).public;
76
- }
77
- if (this.parent?.contains(injectable)) {
78
- return this.parent.get(injectable);
79
- }
80
- throw new Error('No instance found');
81
- }
82
- resolve(injectable) {
83
- return this.resolveInjectable(injectable);
84
- }
85
- async createContext(dependencies) {
86
- return this.createInjectableContext(dependencies);
87
- }
88
- async createInjectableContext(dependencies, dependant) {
89
- const injections = {};
90
- const deps = Object.entries(dependencies);
91
- const resolvers = Array(deps.length);
92
- for (let i = 0; i < deps.length; i++) {
93
- const [key, dependency] = deps[i];
94
- const injectable = getDepedencencyInjectable(dependency);
95
- const resolver = this.resolveInjectable(injectable, dependant);
96
- resolvers[i] = resolver.then((value) => (injections[key] = value));
97
- }
98
- await Promise.all(resolvers);
99
- return Object.freeze(injections);
100
- }
101
- async provide(injectable, instance) {
102
- if (compareScope(injectable.scope, '>', this.scope)) {
103
- throw new Error('Invalid scope'); // TODO: more informative error
104
- }
105
- this.instances.set(injectable, [
106
- { private: instance, public: instance, context: undefined },
107
- ]);
108
- }
109
- satisfies(injectable) {
110
- return compareScope(injectable.scope, '<=', this.scope);
111
- }
112
- *findCurrentScopeInjectables() {
113
- for (const injectable of this.injectables) {
114
- if (injectable.scope === this.scope) {
115
- yield injectable;
116
- }
117
- }
118
- }
119
- resolveInjectable(injectable, dependant) {
120
- if (this.disposing) {
121
- return Promise.reject(new Error('Cannot resolve during disposal'));
122
- }
123
- if (dependant && compareScope(dependant.scope, '<', injectable.scope)) {
124
- // TODO: more informative error
125
- return Promise.reject(new Error('Invalid scope: dependant is looser than injectable'));
126
- }
127
- if (isValueInjectable(injectable)) {
128
- return Promise.resolve(injectable.value);
129
- }
130
- else if (this.parent?.contains(injectable) ||
131
- (this.parent?.satisfies(injectable) &&
132
- compareScope(this.parent.scope, '<', this.scope))) {
133
- return this.parent.resolveInjectable(injectable, dependant);
134
- }
135
- else {
136
- const { stack, label } = injectable;
137
- if (dependant) {
138
- let dependants = this.dependants.get(injectable);
139
- if (!dependants) {
140
- this.dependants.set(injectable, (dependants = new Set()));
141
- }
142
- dependants.add(dependant);
143
- }
144
- const isTransient = injectable.scope === Scope.Transient;
145
- if (!isTransient && this.instances.has(injectable)) {
146
- return Promise.resolve(this.instances.get(injectable).at(0).public);
147
- }
148
- else if (!isTransient && this.resolvers.has(injectable)) {
149
- return this.resolvers.get(injectable);
150
- }
151
- else {
152
- const isLazy = isLazyInjectable(injectable);
153
- if (isLazy) {
154
- const isOptional = isOptionalInjectable(injectable);
155
- if (isOptional)
156
- return Promise.resolve(undefined);
157
- return Promise.reject(new Error(`No instance provided for ${label || 'an'} injectable:\n${stack}`));
158
- }
159
- else {
160
- const resolution = this.createResolution(injectable).finally(() => {
161
- this.resolvers.delete(injectable);
162
- });
163
- if (injectable.scope !== Scope.Transient) {
164
- this.resolvers.set(injectable, resolution);
165
- }
166
- return resolution;
167
- }
168
- }
169
- }
170
- }
171
- async createResolution(injectable) {
172
- const { dependencies } = injectable;
173
- const context = await this.createInjectableContext(dependencies, injectable);
174
- const wrapper = {
175
- private: null,
176
- public: null,
177
- context,
178
- };
179
- if (isFactoryInjectable(injectable)) {
180
- wrapper.private = await Promise.resolve(injectable.factory(wrapper.context));
181
- wrapper.public = injectable.pick(wrapper.private);
182
- }
183
- else if (isClassInjectable(injectable)) {
184
- const instance = new injectable(context);
185
- wrapper.private = instance;
186
- wrapper.public = wrapper.private;
187
- await instance[kClassInjectableCreate]?.call(instance);
188
- }
189
- else {
190
- throw new Error('Invalid injectable type');
191
- }
192
- let instances = this.instances.get(injectable);
193
- if (!instances) {
194
- instances = [];
195
- this.instances.set(injectable, instances);
196
- }
197
- instances.push(wrapper);
198
- return wrapper.public;
199
- }
200
- createInjectFunction() {
201
- const inject = (injectable, context) => {
202
- const dependencies = { ...injectable.dependencies };
203
- for (const key in context) {
204
- const dep = context[key];
205
- if (isInjectable(dep) || isOptionalInjectable(dep)) {
206
- dependencies[key] = dep;
207
- }
208
- else {
209
- dependencies[key] = createValueInjectable(dep);
210
- }
211
- }
212
- const newInjectable = isClassInjectable(injectable)
213
- ? createExtendableClassInjectable(injectable, dependencies, Scope.Transient, 1)
214
- : {
215
- ...injectable,
216
- dependencies,
217
- scope: Scope.Transient,
218
- stack: tryCaptureStackTrace(1),
219
- };
220
- return this.resolve(newInjectable);
221
- };
222
- const explicit = async (injectable, context) => {
223
- if ('asyncDispose' in Symbol === false) {
224
- throw new Error('Symbol.asyncDispose is not supported in this environment');
225
- }
226
- const instance = await inject(injectable, context);
227
- const dispose = this.createDisposeFunction();
228
- return Object.assign(instance, {
229
- [Symbol.asyncDispose]: async () => {
230
- await dispose(injectable, instance);
231
- },
232
- });
233
- };
234
- return Object.assign(inject, { explicit });
235
- }
236
- createDisposeFunction() {
237
- return async (injectable, instance) => {
238
- if (injectable.scope === Scope.Transient) {
239
- assert(instance, 'Instance is required for transient injectable disposal');
240
- const wrappers = this.instances.get(injectable);
241
- if (wrappers) {
242
- for (const wrapper of wrappers) {
243
- if (wrapper.public === instance) {
244
- await this.disposeInjectableInstance(injectable, wrapper.private, wrapper.context);
245
- const index = wrappers.indexOf(wrapper);
246
- wrappers.splice(index, 1);
247
- }
248
- }
249
- if (wrappers.length === 0) {
250
- this.instances.delete(injectable);
251
- }
252
- }
253
- }
254
- else {
255
- await this.disposeInjectableInstances(injectable);
256
- }
257
- };
258
- }
259
- getDisposalOrder() {
260
- const visited = new Set();
261
- const result = [];
262
- const visit = (injectable) => {
263
- if (visited.has(injectable))
264
- return;
265
- visited.add(injectable);
266
- const dependants = this.dependants.get(injectable);
267
- if (dependants) {
268
- for (const dependant of dependants) {
269
- if (this.instances.has(dependant)) {
270
- visit(dependant);
271
- }
272
- }
273
- }
274
- // Only add to result if this container owns the instance
275
- if (this.instances.has(injectable)) {
276
- result.push(injectable);
277
- }
278
- };
279
- for (const injectable of this.instances.keys()) {
280
- visit(injectable);
281
- }
282
- return result;
283
- }
284
- async disposeInjectableInstances(injectable) {
285
- try {
286
- if (this.instances.has(injectable)) {
287
- const wrappers = this.instances.get(injectable);
288
- await Promise.all(wrappers.map((wrapper) => this.disposeInjectableInstance(injectable, wrapper.private, wrapper.context)));
289
- }
290
- }
291
- catch (cause) {
292
- const error = new Error('Injectable disposal error. Potential memory leak', { cause });
293
- this.application.logger.error(error);
294
- }
295
- finally {
296
- this.instances.delete(injectable);
297
- }
298
- }
299
- async disposeInjectableInstance(injectable, instance, context) {
300
- if (isFactoryInjectable(injectable)) {
301
- const { dispose } = injectable;
302
- if (dispose)
303
- await dispose(instance, context);
304
- }
305
- else if (isClassInjectable(injectable)) {
306
- await instance[kClassInjectableDispose]?.();
307
- }
308
- }
309
- }
package/dist/enums.d.ts DELETED
@@ -1,18 +0,0 @@
1
- export declare enum Scope {
2
- Global = "Global",
3
- Connection = "Connection",
4
- Call = "Call",
5
- Transient = "Transient"
6
- }
7
- export declare enum Hook {
8
- BeforeInitialize = "BeforeInitialize",
9
- AfterInitialize = "AfterInitialize",
10
- BeforeStart = "BeforeStart",
11
- AfterStart = "AfterStart",
12
- BeforeStop = "BeforeStop",
13
- AfterStop = "AfterStop",
14
- BeforeTerminate = "BeforeTerminate",
15
- AfterTerminate = "AfterTerminate",
16
- OnConnect = "OnConnect",
17
- OnDisconnect = "OnDisconnect"
18
- }
package/dist/enums.js DELETED
@@ -1,20 +0,0 @@
1
- export var Scope;
2
- (function (Scope) {
3
- Scope["Global"] = "Global";
4
- Scope["Connection"] = "Connection";
5
- Scope["Call"] = "Call";
6
- Scope["Transient"] = "Transient";
7
- })(Scope || (Scope = {}));
8
- export var Hook;
9
- (function (Hook) {
10
- Hook["BeforeInitialize"] = "BeforeInitialize";
11
- Hook["AfterInitialize"] = "AfterInitialize";
12
- Hook["BeforeStart"] = "BeforeStart";
13
- Hook["AfterStart"] = "AfterStart";
14
- Hook["BeforeStop"] = "BeforeStop";
15
- Hook["AfterStop"] = "AfterStop";
16
- Hook["BeforeTerminate"] = "BeforeTerminate";
17
- Hook["AfterTerminate"] = "AfterTerminate";
18
- Hook["OnConnect"] = "OnConnect";
19
- Hook["OnDisconnect"] = "OnDisconnect";
20
- })(Hook || (Hook = {}));
package/dist/hooks.d.ts DELETED
@@ -1,19 +0,0 @@
1
- import type { Callback } from '@nmtjs/common';
2
- import type { Hook } from './enums.ts';
3
- import { kHookCollection } from './constants.ts';
4
- export interface HookType {
5
- [key: string]: (...args: any[]) => any;
6
- }
7
- export type CallHook<T extends string> = (hook: T, ...args: T extends keyof HookType ? Parameters<HookType[T]> : any[]) => Promise<void>;
8
- export declare class Hooks {
9
- static merge(from: Hooks, to: Hooks): void;
10
- [kHookCollection]: Map<string, Set<Callback>>;
11
- add(name: string, callback: Callback): () => void;
12
- remove(name: string, callback: Callback): void;
13
- call<T extends string | Hook>(name: T, options: {
14
- concurrent?: boolean;
15
- reverse?: boolean;
16
- } | undefined, ...args: T extends Hook ? Parameters<HookType[T]> : any[]): Promise<void>;
17
- clear(): void;
18
- }
19
- export declare const createErrForHook: (hook: Hook | (object & string)) => string;
package/dist/hooks.js DELETED
@@ -1,43 +0,0 @@
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)
14
- this[kHookCollection].set(name, (hooks = new Set()));
15
- hooks.add(callback);
16
- return () => this.remove(name, callback);
17
- }
18
- remove(name, callback) {
19
- const hooks = this[kHookCollection].get(name);
20
- if (hooks)
21
- hooks.delete(callback);
22
- }
23
- async call(name, options, ...args) {
24
- const { concurrent = true, reverse = false } = options ?? {};
25
- const hooks = this[kHookCollection].get(name);
26
- if (!hooks)
27
- return;
28
- const hooksArr = Array.from(hooks);
29
- if (concurrent) {
30
- await Promise.all(hooksArr.map((hook) => hook(...args)));
31
- }
32
- else {
33
- if (reverse)
34
- hooksArr.reverse();
35
- for (const hook of hooksArr)
36
- await hook(...args);
37
- }
38
- }
39
- clear() {
40
- this[kHookCollection].clear();
41
- }
42
- }
43
- export const createErrForHook = (hook) => `Error during [${hook}] hook`;
package/dist/index.d.ts DELETED
@@ -1,11 +0,0 @@
1
- export * from './constants.ts';
2
- export * from './container.ts';
3
- export * from './enums.ts';
4
- export * from './hooks.ts';
5
- export * from './injectables.ts';
6
- export * from './logger.ts';
7
- export * from './metadata.ts';
8
- export * from './plugin.ts';
9
- export * from './registry.ts';
10
- export * from './types.ts';
11
- export * from './utils/index.ts';
package/dist/index.js DELETED
@@ -1,14 +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
- export * from "./constants.js";
5
- export * from "./container.js";
6
- export * from "./enums.js";
7
- export * from "./hooks.js";
8
- export * from "./injectables.js";
9
- export * from "./logger.js";
10
- export * from "./metadata.js";
11
- export * from "./plugin.js";
12
- export * from "./registry.js";
13
- export * from "./types.js";
14
- export * from "./utils/index.js";