@nmtjs/core 0.12.5 → 0.12.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/dist/constants.d.ts +20 -0
  2. package/dist/constants.js +10 -12
  3. package/dist/container.d.ts +46 -0
  4. package/dist/container.js +310 -277
  5. package/dist/enums.d.ts +18 -0
  6. package/dist/enums.js +20 -22
  7. package/dist/hooks.d.ts +19 -0
  8. package/dist/hooks.js +39 -35
  9. package/dist/index.d.ts +11 -0
  10. package/dist/index.js +3 -2
  11. package/dist/injectables.d.ts +115 -0
  12. package/dist/injectables.js +194 -179
  13. package/dist/logger.d.ts +9 -0
  14. package/dist/logger.js +54 -58
  15. package/dist/metadata.d.ts +13 -0
  16. package/dist/metadata.js +10 -15
  17. package/dist/plugin.d.ts +12 -0
  18. package/dist/plugin.js +1 -7
  19. package/dist/registry.d.ts +19 -0
  20. package/dist/registry.js +17 -18
  21. package/dist/types.d.ts +11 -0
  22. package/dist/types.js +0 -2
  23. package/dist/utils/functions.d.ts +6 -0
  24. package/dist/utils/functions.js +30 -32
  25. package/dist/utils/index.d.ts +3 -0
  26. package/dist/utils/index.js +0 -2
  27. package/dist/utils/pool.d.ts +18 -0
  28. package/dist/utils/pool.js +103 -90
  29. package/dist/utils/semaphore.d.ts +13 -0
  30. package/dist/utils/semaphore.js +53 -46
  31. package/package.json +9 -9
  32. package/dist/constants.js.map +0 -1
  33. package/dist/container.js.map +0 -1
  34. package/dist/enums.js.map +0 -1
  35. package/dist/hooks.js.map +0 -1
  36. package/dist/index.js.map +0 -1
  37. package/dist/injectables.js.map +0 -1
  38. package/dist/logger.js.map +0 -1
  39. package/dist/metadata.js.map +0 -1
  40. package/dist/plugin.js.map +0 -1
  41. package/dist/registry.js.map +0 -1
  42. package/dist/types.js.map +0 -1
  43. package/dist/utils/functions.js.map +0 -1
  44. package/dist/utils/index.js.map +0 -1
  45. package/dist/utils/pool.js.map +0 -1
  46. package/dist/utils/semaphore.js.map +0 -1
  47. package/src/constants.ts +0 -41
  48. package/src/container.ts +0 -440
  49. package/src/enums.ts +0 -19
  50. package/src/hooks.ts +0 -71
  51. package/src/index.ts +0 -11
  52. package/src/injectables.ts +0 -508
  53. package/src/logger.ts +0 -99
  54. package/src/metadata.ts +0 -27
  55. package/src/plugin.ts +0 -27
  56. package/src/registry.ts +0 -44
  57. package/src/types.ts +0 -13
  58. package/src/utils/functions.ts +0 -31
  59. package/src/utils/index.ts +0 -3
  60. package/src/utils/pool.ts +0 -111
  61. package/src/utils/semaphore.ts +0 -63
package/dist/registry.js CHANGED
@@ -1,25 +1,24 @@
1
1
  import { Scope } from "./enums.js";
2
2
  import { Hooks } from "./hooks.js";
3
- import { getInjectableScope } from "./injectables.js";
3
+ import { getInjectableScope, } from "./injectables.js";
4
4
  export class Registry {
5
- hooks = new Hooks();
6
- constructor(application) {
7
- this.application = application;
8
- }
9
- registerHooks(hooks) {
10
- Hooks.merge(hooks, this.hooks);
11
- }
12
- registerHook(name, callback) {
13
- this.hooks.add(name, callback);
14
- }
15
- *getDependants() {}
16
- clear() {
17
- this.hooks.clear();
18
- }
5
+ application;
6
+ hooks = new Hooks();
7
+ constructor(application) {
8
+ this.application = application;
9
+ }
10
+ registerHooks(hooks) {
11
+ Hooks.merge(hooks, this.hooks);
12
+ }
13
+ registerHook(name, callback) {
14
+ this.hooks.add(name, callback);
15
+ }
16
+ *getDependants() { }
17
+ clear() {
18
+ this.hooks.clear();
19
+ }
19
20
  }
20
21
  export const scopeErrorMessage = (name, scope = Scope.Global) => `${name} must be a ${scope} scope (including all nested dependencies)`;
21
22
  export function hasInvalidScopeDeps(injectables, scope = Scope.Global) {
22
- return injectables.some((injectable) => getInjectableScope(injectable) !== scope);
23
+ return injectables.some((injectable) => getInjectableScope(injectable) !== scope);
23
24
  }
24
-
25
- //# sourceMappingURL=registry.js.map
@@ -0,0 +1,11 @@
1
+ import type { Container } from './container.ts';
2
+ import type { Hooks } from './hooks.ts';
3
+ import type { Logger } from './logger.ts';
4
+ import type { Registry } from './registry.ts';
5
+ export interface PluginContext {
6
+ logger: Logger;
7
+ registry: Registry;
8
+ hooks: Hooks;
9
+ container: Container;
10
+ }
11
+ export type Pattern = RegExp | string | ((value: string) => boolean);
package/dist/types.js CHANGED
@@ -1,3 +1 @@
1
1
  export {};
2
-
3
- //# sourceMappingURL=types.js.map
@@ -0,0 +1,6 @@
1
+ import type { Pattern } from '../types.ts';
2
+ /**
3
+ * Very simple pattern matching function.
4
+ */
5
+ export declare function match(value: string, pattern: Pattern): boolean;
6
+ export declare function isJsFile(name: string): boolean;
@@ -1,37 +1,35 @@
1
1
  /**
2
- * Very simple pattern matching function.
3
- */
2
+ * Very simple pattern matching function.
3
+ */
4
4
  export function match(value, pattern) {
5
- if (typeof pattern === "function") {
6
- return pattern(value);
7
- } else if (typeof pattern === "string") {
8
- if (pattern === "*" || pattern === "**") {
9
- return true;
10
- } else if (pattern.at(0) === "*" && pattern.at(-1) === "*") {
11
- return value.includes(pattern.slice(1, -1));
12
- } else if (pattern.at(-1) === "*") {
13
- return value.startsWith(pattern.slice(0, -1));
14
- } else if (pattern.at(0) === "*") {
15
- return value.endsWith(pattern.slice(1));
16
- } else {
17
- return value === pattern;
18
- }
19
- } else {
20
- return pattern.test(value);
21
- }
5
+ if (typeof pattern === 'function') {
6
+ return pattern(value);
7
+ }
8
+ else if (typeof pattern === 'string') {
9
+ if (pattern === '*' || pattern === '**') {
10
+ return true;
11
+ }
12
+ else if (pattern.at(0) === '*' && pattern.at(-1) === '*') {
13
+ return value.includes(pattern.slice(1, -1));
14
+ }
15
+ else if (pattern.at(-1) === '*') {
16
+ return value.startsWith(pattern.slice(0, -1));
17
+ }
18
+ else if (pattern.at(0) === '*') {
19
+ return value.endsWith(pattern.slice(1));
20
+ }
21
+ else {
22
+ return value === pattern;
23
+ }
24
+ }
25
+ else {
26
+ return pattern.test(value);
27
+ }
22
28
  }
23
29
  export function isJsFile(name) {
24
- if (name.endsWith(".d.ts")) return false;
25
- const leading = name.split(".").slice(1);
26
- const ext = leading.join(".");
27
- return [
28
- "js",
29
- "mjs",
30
- "cjs",
31
- "ts",
32
- "mts",
33
- "cts"
34
- ].includes(ext);
30
+ if (name.endsWith('.d.ts'))
31
+ return false;
32
+ const leading = name.split('.').slice(1);
33
+ const ext = leading.join('.');
34
+ return ['js', 'mjs', 'cjs', 'ts', 'mts', 'cts'].includes(ext);
35
35
  }
36
-
37
- //# sourceMappingURL=functions.js.map
@@ -0,0 +1,3 @@
1
+ export * from './functions.ts';
2
+ export * from './pool.ts';
3
+ export * from './semaphore.ts';
@@ -1,5 +1,3 @@
1
1
  export * from "./functions.js";
2
2
  export * from "./pool.js";
3
3
  export * from "./semaphore.js";
4
-
5
- //# sourceMappingURL=index.js.map
@@ -0,0 +1,18 @@
1
+ interface PoolOptions {
2
+ timeout?: number;
3
+ }
4
+ export declare class PoolError extends Error {
5
+ }
6
+ export declare class Pool<T = unknown> {
7
+ #private;
8
+ private readonly options;
9
+ constructor(options?: PoolOptions);
10
+ add(item: T): void;
11
+ remove(item: T): void;
12
+ capture(timeout?: number | undefined): Promise<T>;
13
+ next(exclusive?: boolean, timeout?: number | undefined): Promise<T>;
14
+ release(item: T): void;
15
+ isFree(item: T): boolean;
16
+ get items(): T[];
17
+ }
18
+ export {};
@@ -1,92 +1,105 @@
1
- export class PoolError extends Error {}
1
+ export class PoolError extends Error {
2
+ }
3
+ // Fixed pool from https://github.com/metarhia/metautil
2
4
  export class Pool {
3
- #items = [];
4
- #free = [];
5
- #queue = [];
6
- #current = 0;
7
- #size = 0;
8
- #available = 0;
9
- constructor(options = {}) {
10
- this.options = options;
11
- }
12
- add(item) {
13
- if (this.#items.includes(item)) throw new PoolError("Item already exists");
14
- this.#size++;
15
- this.#available++;
16
- this.#items.push(item);
17
- this.#free.push(true);
18
- }
19
- remove(item) {
20
- if (this.#size === 0) throw new PoolError("Pool is empty");
21
- const index = this.#items.indexOf(item);
22
- if (index < 0) throw new PoolError("Item is not in the pool");
23
- const isCaptured = this.isFree(item);
24
- if (isCaptured) this.#available--;
25
- this.#size--;
26
- this.#current--;
27
- this.#items.splice(index, 1);
28
- this.#free.splice(index, 1);
29
- }
30
- capture(timeout = this.options.timeout) {
31
- return this.next(true, timeout);
32
- }
33
- async next(exclusive = false, timeout = this.options.timeout) {
34
- if (this.#size === 0) throw new PoolError("Pool is empty");
35
- if (this.#available === 0) {
36
- return new Promise((resolve, reject) => {
37
- const waiting = {
38
- resolve: (item) => {
39
- if (exclusive) this.#capture(item);
40
- resolve(item);
41
- },
42
- timer: undefined
43
- };
44
- if (timeout) {
45
- waiting.timer = setTimeout(() => {
46
- waiting.resolve = undefined;
47
- this.#queue.shift();
48
- reject(new PoolError("Next item timeout"));
49
- }, timeout);
50
- }
51
- this.#queue.push(waiting);
52
- });
53
- }
54
- let item;
55
- let free = false;
56
- do {
57
- item = this.#items[this.#current];
58
- free = this.#free[this.#current];
59
- this.#current++;
60
- if (this.#current >= this.#size) this.#current = 0;
61
- } while (typeof item === "undefined" || !free);
62
- if (exclusive) this.#capture(item);
63
- return item;
64
- }
65
- release(item) {
66
- const index = this.#items.indexOf(item);
67
- if (index < 0) throw new PoolError("Unexpected item");
68
- if (this.#free[index]) throw new PoolError("Unable to release not captured item");
69
- this.#free[index] = true;
70
- this.#available++;
71
- if (this.#queue.length > 0) {
72
- const { resolve, timer } = this.#queue.shift();
73
- clearTimeout(timer);
74
- if (resolve) setTimeout(resolve, 0, item);
75
- }
76
- }
77
- isFree(item) {
78
- const index = this.#items.indexOf(item);
79
- if (index < 0) return false;
80
- return this.#free[index];
81
- }
82
- get items() {
83
- return [...this.#items];
84
- }
85
- #capture(item) {
86
- const index = this.#items.indexOf(item);
87
- this.#free[index] = false;
88
- this.#available--;
89
- }
5
+ options;
6
+ #items = [];
7
+ #free = [];
8
+ #queue = [];
9
+ #current = 0;
10
+ #size = 0;
11
+ #available = 0;
12
+ constructor(options = {}) {
13
+ this.options = options;
14
+ }
15
+ add(item) {
16
+ if (this.#items.includes(item))
17
+ throw new PoolError('Item already exists');
18
+ this.#size++;
19
+ this.#available++;
20
+ this.#items.push(item);
21
+ this.#free.push(true);
22
+ }
23
+ remove(item) {
24
+ if (this.#size === 0)
25
+ throw new PoolError('Pool is empty');
26
+ const index = this.#items.indexOf(item);
27
+ if (index < 0)
28
+ throw new PoolError('Item is not in the pool');
29
+ const isCaptured = this.isFree(item);
30
+ if (isCaptured)
31
+ this.#available--;
32
+ this.#size--;
33
+ this.#current--;
34
+ this.#items.splice(index, 1);
35
+ this.#free.splice(index, 1);
36
+ }
37
+ capture(timeout = this.options.timeout) {
38
+ return this.next(true, timeout);
39
+ }
40
+ async next(exclusive = false, timeout = this.options.timeout) {
41
+ if (this.#size === 0)
42
+ throw new PoolError('Pool is empty');
43
+ if (this.#available === 0) {
44
+ return new Promise((resolve, reject) => {
45
+ const waiting = {
46
+ resolve: (item) => {
47
+ if (exclusive)
48
+ this.#capture(item);
49
+ resolve(item);
50
+ },
51
+ timer: undefined,
52
+ };
53
+ if (timeout) {
54
+ waiting.timer = setTimeout(() => {
55
+ waiting.resolve = undefined;
56
+ this.#queue.shift();
57
+ reject(new PoolError('Next item timeout'));
58
+ }, timeout);
59
+ }
60
+ this.#queue.push(waiting);
61
+ });
62
+ }
63
+ let item;
64
+ let free = false;
65
+ do {
66
+ item = this.#items[this.#current];
67
+ free = this.#free[this.#current];
68
+ this.#current++;
69
+ if (this.#current >= this.#size)
70
+ this.#current = 0;
71
+ } while (typeof item === 'undefined' || !free);
72
+ if (exclusive)
73
+ this.#capture(item);
74
+ return item;
75
+ }
76
+ release(item) {
77
+ const index = this.#items.indexOf(item);
78
+ if (index < 0)
79
+ throw new PoolError('Unexpected item');
80
+ if (this.#free[index])
81
+ throw new PoolError('Unable to release not captured item');
82
+ this.#free[index] = true;
83
+ this.#available++;
84
+ if (this.#queue.length > 0) {
85
+ const { resolve, timer } = this.#queue.shift();
86
+ clearTimeout(timer);
87
+ if (resolve)
88
+ setTimeout(resolve, 0, item);
89
+ }
90
+ }
91
+ isFree(item) {
92
+ const index = this.#items.indexOf(item);
93
+ if (index < 0)
94
+ return false;
95
+ return this.#free[index];
96
+ }
97
+ get items() {
98
+ return [...this.#items];
99
+ }
100
+ #capture(item) {
101
+ const index = this.#items.indexOf(item);
102
+ this.#free[index] = false;
103
+ this.#available--;
104
+ }
90
105
  }
91
-
92
- //# sourceMappingURL=pool.js.map
@@ -0,0 +1,13 @@
1
+ export declare class SemaphoreError extends Error {
2
+ }
3
+ export declare class Semaphore {
4
+ private readonly size;
5
+ private readonly timeout;
6
+ private counter;
7
+ private readonly queue;
8
+ constructor(concurrency: number, size?: number, timeout?: number);
9
+ enter(): Promise<void>;
10
+ leave(): void;
11
+ get isEmpty(): boolean;
12
+ get queueLength(): number;
13
+ }
@@ -1,48 +1,55 @@
1
- export class SemaphoreError extends Error {}
1
+ export class SemaphoreError extends Error {
2
+ }
3
+ // Semaphore from https://github.com/metarhia/metautil
2
4
  export class Semaphore {
3
- counter;
4
- queue = [];
5
- constructor(concurrency, size = 0, timeout = 0) {
6
- this.size = size;
7
- this.timeout = timeout;
8
- this.counter = concurrency;
9
- }
10
- enter() {
11
- if (this.counter > 0) {
12
- this.counter--;
13
- return Promise.resolve();
14
- } else if (this.queue.length >= this.size) {
15
- return Promise.reject(new SemaphoreError("Queue is full"));
16
- } else {
17
- return new Promise((resolve, reject) => {
18
- const waiting = { resolve };
19
- waiting.timer = setTimeout(() => {
20
- waiting.resolve = undefined;
21
- this.queue.shift();
22
- reject(new SemaphoreError("Timeout"));
23
- }, this.timeout);
24
- this.queue.push(waiting);
25
- });
26
- }
27
- }
28
- leave() {
29
- if (this.queue.length === 0) {
30
- this.counter++;
31
- } else {
32
- const item = this.queue.shift();
33
- if (item) {
34
- const { resolve, timer } = item;
35
- if (timer) clearTimeout(timer);
36
- if (resolve) setTimeout(resolve, 0);
37
- }
38
- }
39
- }
40
- get isEmpty() {
41
- return this.queue.length === 0;
42
- }
43
- get queueLength() {
44
- return this.queue.length;
45
- }
5
+ size;
6
+ timeout;
7
+ counter;
8
+ queue = [];
9
+ constructor(concurrency, size = 0, timeout = 0) {
10
+ this.size = size;
11
+ this.timeout = timeout;
12
+ this.counter = concurrency;
13
+ }
14
+ enter() {
15
+ if (this.counter > 0) {
16
+ this.counter--;
17
+ return Promise.resolve();
18
+ }
19
+ else if (this.queue.length >= this.size) {
20
+ return Promise.reject(new SemaphoreError('Queue is full'));
21
+ }
22
+ else {
23
+ return new Promise((resolve, reject) => {
24
+ const waiting = { resolve };
25
+ waiting.timer = setTimeout(() => {
26
+ waiting.resolve = undefined;
27
+ this.queue.shift();
28
+ reject(new SemaphoreError('Timeout'));
29
+ }, this.timeout);
30
+ this.queue.push(waiting);
31
+ });
32
+ }
33
+ }
34
+ leave() {
35
+ if (this.queue.length === 0) {
36
+ this.counter++;
37
+ }
38
+ else {
39
+ const item = this.queue.shift();
40
+ if (item) {
41
+ const { resolve, timer } = item;
42
+ if (timer)
43
+ clearTimeout(timer);
44
+ if (resolve)
45
+ setTimeout(resolve, 0);
46
+ }
47
+ }
48
+ }
49
+ get isEmpty() {
50
+ return this.queue.length === 0;
51
+ }
52
+ get queueLength() {
53
+ return this.queue.length;
54
+ }
46
55
  }
47
-
48
- //# sourceMappingURL=semaphore.js.map
package/package.json CHANGED
@@ -3,32 +3,32 @@
3
3
  "type": "module",
4
4
  "exports": {
5
5
  ".": {
6
- "types": "./src/index.ts",
7
- "import": "./dist/index.js"
6
+ "types": "./dist/index.d.ts",
7
+ "import": "./dist/index.js",
8
+ "module-sync": "./dist/index.js"
8
9
  }
9
10
  },
10
11
  "peerDependencies": {
11
12
  "pino": "^9.6.0",
12
13
  "pino-pretty": "^13.0.0",
13
- "@nmtjs/common": "0.12.5",
14
- "@nmtjs/type": "0.12.5"
14
+ "@nmtjs/common": "0.12.7",
15
+ "@nmtjs/type": "0.12.7"
15
16
  },
16
17
  "devDependencies": {
17
18
  "@types/node": "^20",
18
19
  "pino": "^9.6.0",
19
20
  "pino-pretty": "^13.0.0",
20
- "@nmtjs/type": "0.12.5",
21
- "@nmtjs/common": "0.12.5"
21
+ "@nmtjs/common": "0.12.7",
22
+ "@nmtjs/type": "0.12.7"
22
23
  },
23
24
  "files": [
24
- "src",
25
25
  "dist",
26
26
  "LICENSE.md",
27
27
  "README.md"
28
28
  ],
29
- "version": "0.12.5",
29
+ "version": "0.12.7",
30
30
  "scripts": {
31
- "build": "neemata-build --root=./src './**/*.ts'",
31
+ "build": "tsc",
32
32
  "type-check": "tsc --noEmit"
33
33
  }
34
34
  }
@@ -1 +0,0 @@
1
- {"mappings":"AAAA,OAAO,MAAMA,sBAAqC,OAAO,IACvD,gCACD;AAGD,OAAO,MAAMC,cAA6B,OAAO,IAAI,wBAAwB;AAG7E,OAAO,MAAMC,kBAAiC,OAAO,IACnD,4BACD;AAGD,OAAO,MAAMC,mBAAkC,OAAO,IACpD,6BACD;AAGD,OAAO,MAAMC,qBAAoC,OAAO,IACtD,+BACD;AAGD,OAAO,MAAMC,mBAAkC,OAAO,IACpD,6BACD;AAGD,OAAO,MAAMC,YAA2B,OAAO,IAAI,sBAAsB;AAGzE,OAAO,MAAMC,kBAAiC,OAAO,IACnD,4BACD;AAGD,OAAO,MAAMC,UAAyB,OAAO,IAAI,oBAAoB;AAGrE,OAAO,MAAMC,YAA2B,OAAO,IAAI,sBAAsB","names":["kOptionalDependency: unique symbol","kInjectable: unique symbol","kLazyInjectable: unique symbol","kValueInjectable: unique symbol","kFactoryInjectable: unique symbol","kClassInjectable: unique symbol","kProvider: unique symbol","kHookCollection: unique symbol","kPlugin: unique symbol","kMetadata: unique symbol"],"sources":["../src/constants.ts"],"sourcesContent":["export 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 kClassInjectable: unique symbol = Symbol.for(\n 'neemata:ClassInjectableKey',\n)\nexport type kClassInjectable = typeof kClassInjectable\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\n\nexport const kMetadata: unique symbol = Symbol.for('neemata:MetadataKey')\nexport type kMetadata = typeof kMetadata\n"],"version":3,"file":"constants.js"}
@@ -1 +0,0 @@
1
- {"mappings":"AAAA,OAAO,YAAY,aAAa;AAChC,SAAS,4BAA4B,eAAe;AACpD,SAAS,aAAa,YAAY;AAClC,SAEE,iBACA,cACA,iCACA,uBAGA,2BACA,mBACA,qBACA,cACA,kBACA,sBACA,yBAEK,kBAAkB;AAWzB,OAAO,MAAM,UAAU;CACrB,AAAS,YAAY,IAAI;CACzB,AAAiB,YAAY,IAAI;CACjC,AAAiB,cAAc,IAAI;CACnC,AAAiB,aAAa,IAAI;CAElC,AAAQ,YAAY;CAEpB,YACmBA,aACDC,QAAyC,MAAM,QAC9CC,QACjB;OAHiB;OACD;OACC;AAEjB,MAAK,UAAkB,MAAM,WAAW;AACtC,SAAM,IAAI,MAAM;EACjB;AACD,OAAK,QAAQ,gBAAgB,QAAQ,KAAK,sBAAsB,CAAC;AACjE,OAAK,QAAQ,gBAAgB,SAAS,KAAK,uBAAuB,CAAC;AACnE,OAAK,QAAQ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AACF,OAAI,KAAK,UAAU,IAAI,WAAW,EAAE;IAClC,MAAM,WAAW,KAAK,UAAU,IAAI,WAAW;AAC/C,UAAM,QAAQ,IACZ,SAAS,IAAI,CAAC,YACZ,KAAK,0BACH,YACA,QAAQ,SACR,QAAQ,QACT,CACF,CACF;GACF;EACF,SAAQ,OAAO;GACd,MAAM,QAAQ,IAAI,MAChB,oDACA,EAAE,MAAO;AAEX,QAAK,YAAY,OAAO,MAAM,MAAM;EACrC,UAAS;AACR,QAAK,UAAU,OAAO,WAAW;EAClC;CACF;CAED,MAAc,0BACZA,YACAU,UACAC,SACA;AACA,MAAI,oBAAoB,WAAW,EAAE;GACnC,MAAM,EAAE,SAAS,GAAG;AACpB,OAAI,QAAS,OAAM,QAAQ,UAAU,QAAQ;EAC9C,WAAU,kBAAkB,WAAW,EAAE;AACxC,SAAM,SAAS,YAAY;EAC5B;CACF;AACF","names":["application: ContainerOptions","scope: Exclude<Scope, Scope.Transient>","parent?: Container","dependencies: Dependencies","injectable: AnyInjectable","injectable: T","dependencies: T","dependant?: AnyInjectable","injections: Record<string, any>","resolvers: Promise<any>[]","instance: ResolveInjectableType<T>","context: InlineInjectionDependencies<T>","instance?: any","result: AnyInjectable[]","instance: any","context: any"],"sources":["../src/container.ts"],"sourcesContent":["import assert from 'node:assert'\nimport { tryCaptureStackTrace } from '@nmtjs/common'\nimport { Scope } from './enums.ts'\nimport {\n type AnyInjectable,\n CoreInjectables,\n compareScope,\n createExtendableClassInjectable,\n createValueInjectable,\n type Dependencies,\n type DependencyContext,\n getDepedencencyInjectable,\n isClassInjectable,\n isFactoryInjectable,\n isInjectable,\n isLazyInjectable,\n isOptionalInjectable,\n isValueInjectable,\n type ResolveInjectableType,\n} from './injectables.ts'\nimport type { Logger } from './logger.ts'\nimport type { Registry } from './registry.ts'\n\ntype InstanceWrapper = { private: any; public: any; context: any }\n\ntype ContainerOptions = {\n registry: Registry\n logger: Logger\n}\n\nexport class Container {\n readonly instances = new Map<AnyInjectable, InstanceWrapper[]>()\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 // private readonly transients = new Map<any, any>()\n private disposing = false\n\n constructor(\n private readonly application: ContainerOptions,\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 this.provide(CoreInjectables.inject, this.createInjectFunction())\n this.provide(CoreInjectables.dispose, this.createDisposeFunction())\n this.provide(CoreInjectables.registry, application.registry)\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 // Prevent new resolutions during disposal\n this.disposing = true\n\n // Get proper disposal order using topological sort\n const disposalOrder = this.getDisposalOrder()\n\n // Dispose in the correct order\n for (const injectable of disposalOrder) {\n if (this.instances.has(injectable)) {\n await this.disposeInjectableInstances(injectable)\n }\n }\n\n this.instances.clear()\n this.injectables.clear()\n this.resolvers.clear()\n this.dependants.clear()\n\n this.disposing = false\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 (injectable.scope === Scope.Transient) {\n throw new Error('Cannot get transient injectable directly')\n }\n\n if (this.instances.has(injectable)) {\n return this.instances.get(injectable)!.at(0)!.public\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\n this.instances.set(injectable, [\n {\n private: instance,\n public: instance,\n context: undefined,\n },\n ])\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 (this.disposing) {\n return Promise.reject(new Error('Cannot resolve during disposal'))\n }\n\n if (dependant && compareScope(dependant.scope, '<', injectable.scope)) {\n // TODO: more informative error\n return Promise.reject(\n new Error('Invalid scope: dependant is looser than injectable'),\n )\n }\n\n if (isValueInjectable(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 { stack, label } = injectable\n\n if (dependant) {\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 const isTransient = injectable.scope === Scope.Transient\n\n if (!isTransient && this.instances.has(injectable)) {\n return Promise.resolve(this.instances.get(injectable)!.at(0)!.public)\n } else if (!isTransient && this.resolvers.has(injectable)) {\n return this.resolvers.get(injectable)!\n } else {\n const isLazy = isLazyInjectable(injectable)\n\n if (isLazy) {\n const isOptional = isOptionalInjectable(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 {\n const resolution = this.createResolution(injectable).finally(() => {\n this.resolvers.delete(injectable)\n })\n if (injectable.scope !== Scope.Transient) {\n this.resolvers.set(injectable, resolution)\n }\n return resolution\n }\n }\n }\n }\n\n private async createResolution<T extends AnyInjectable>(\n injectable: T,\n ): Promise<ResolveInjectableType<T>> {\n const { dependencies } = injectable\n const context = await this.createInjectableContext(dependencies, injectable)\n const wrapper = {\n private: null as any,\n public: null as ResolveInjectableType<T>,\n context,\n }\n if (isFactoryInjectable(injectable)) {\n wrapper.private = await Promise.resolve(\n injectable.factory(wrapper.context),\n )\n wrapper.public = injectable.pick(wrapper.private)\n } else if (isClassInjectable(injectable)) {\n wrapper.private = new injectable(context)\n wrapper.public = wrapper.private\n await wrapper.private.$onCreate()\n } else {\n throw new Error('Invalid injectable type')\n }\n\n let instances = this.instances.get(injectable)\n\n if (!instances) {\n instances = []\n this.instances.set(injectable, instances)\n }\n instances.push(wrapper)\n\n return wrapper.public\n }\n\n private createInjectFunction() {\n const inject = <T extends AnyInjectable>(\n injectable: T,\n context: InlineInjectionDependencies<T>,\n ) => {\n const dependencies: Dependencies = {\n ...injectable.dependencies,\n }\n\n for (const key in context) {\n const dep = context[key]\n if (isInjectable(dep) || isOptionalInjectable(dep)) {\n dependencies[key] = dep\n } else {\n dependencies[key] = createValueInjectable(dep)\n }\n }\n\n const newInjectable = isClassInjectable(injectable)\n ? createExtendableClassInjectable(\n injectable,\n dependencies,\n Scope.Transient,\n 1,\n )\n : {\n ...injectable,\n dependencies,\n scope: Scope.Transient,\n stack: tryCaptureStackTrace(1),\n }\n\n return this.resolve(newInjectable) as Promise<ResolveInjectableType<T>>\n }\n\n const explicit = async <T extends AnyInjectable>(\n injectable: T,\n context: InlineInjectionDependencies<T>,\n ) => {\n if ('asyncDispose' in Symbol === false) {\n throw new Error(\n 'Symbol.asyncDispose is not supported in this environment',\n )\n }\n const instance = await inject(injectable, context)\n const dispose = this.createDisposeFunction()\n return Object.assign(instance, {\n [Symbol.asyncDispose]: async () => {\n await dispose(injectable, instance)\n },\n })\n }\n\n return Object.assign(inject, { explicit })\n }\n\n private createDisposeFunction() {\n return async <T extends AnyInjectable>(injectable: T, instance?: any) => {\n if (injectable.scope === Scope.Transient) {\n assert(\n instance,\n 'Instance is required for transient injectable disposal',\n )\n const wrappers = this.instances.get(injectable)\n if (wrappers) {\n for (const wrapper of wrappers) {\n if (wrapper.public === instance) {\n await this.disposeInjectableInstance(\n injectable,\n wrapper.private,\n wrapper.context,\n )\n const index = wrappers.indexOf(wrapper)\n wrappers.splice(index, 1)\n }\n }\n\n if (wrappers.length === 0) {\n this.instances.delete(injectable)\n }\n }\n } else {\n await this.disposeInjectableInstances(injectable)\n }\n }\n }\n\n private getDisposalOrder(): AnyInjectable[] {\n const visited = new Set<AnyInjectable>()\n const result: AnyInjectable[] = []\n\n const visit = (injectable: AnyInjectable) => {\n if (visited.has(injectable)) return\n visited.add(injectable)\n\n const dependants = this.dependants.get(injectable)\n if (dependants) {\n for (const dependant of dependants) {\n if (this.instances.has(dependant)) {\n visit(dependant)\n }\n }\n }\n\n // Only add to result if this container owns the instance\n if (this.instances.has(injectable)) {\n result.push(injectable)\n }\n }\n\n for (const injectable of this.instances.keys()) {\n visit(injectable)\n }\n\n return result\n }\n\n private async disposeInjectableInstances(injectable: AnyInjectable) {\n try {\n if (this.instances.has(injectable)) {\n const wrappers = this.instances.get(injectable)!\n await Promise.all(\n wrappers.map((wrapper) =>\n this.disposeInjectableInstance(\n injectable,\n wrapper.private,\n wrapper.context,\n ),\n ),\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 private async disposeInjectableInstance(\n injectable: AnyInjectable,\n instance: any,\n context: any,\n ) {\n if (isFactoryInjectable(injectable)) {\n const { dispose } = injectable\n if (dispose) await dispose(instance, context)\n } else if (isClassInjectable(injectable)) {\n await instance.$onDispose()\n }\n }\n}\n\ntype InlineInjectionDependencies<T extends AnyInjectable> = {\n [K in keyof T['dependencies']]?:\n | ResolveInjectableType<T['dependencies'][K]>\n | AnyInjectable<ResolveInjectableType<T['dependencies'][K]>>\n}\n\nexport type InjectFn = ReturnType<Container['createInjectFunction']>\nexport type DisposeFn = ReturnType<Container['createDisposeFunction']>\n"],"version":3,"file":"container.js"}
package/dist/enums.js.map DELETED
@@ -1 +0,0 @@
1
- {"mappings":"AAAA,OAAO,IAAK,wCAAL;AACL;AACA;AACA;AACA;;AACD;AAED,OAAO,IAAK,sCAAL;AACL;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACD","names":[],"sources":["../src/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"],"version":3,"file":"enums.js"}
package/dist/hooks.js.map DELETED
@@ -1 +0,0 @@
1
- {"mappings":"AACA,SAAS,uBAAuB,gBAAgB;AAuBhD,OAAO,MAAM,MAAM;CACjB,OAAO,MAAMA,MAAaC,IAAW;AACnC,OAAK,MAAM,CAAC,MAAM,UAAU,IAAI,KAAK,kBAAkB;AACrD,QAAK,MAAM,YAAY,WAAW;AAChC,OAAG,IAAI,MAAM,SAAS;GACvB;EACF;CACF;CAED,CAAC,mBAAmB,IAAI;CAExB,IAAIC,MAAcC,UAAoB;EACpC,IAAI,QAAQ,KAAK,iBAAiB,IAAI,KAAK;AAC3C,OAAK,MAAO,MAAK,iBAAiB,IAAI,MAAO,QAAQ,IAAI,MAAO;AAChE,QAAM,IAAI,SAAS;AACnB,SAAO,MAAM,KAAK,OAAO,MAAM,SAAS;CACzC;CAED,OAAOD,MAAcC,UAAoB;EACvC,MAAM,QAAQ,KAAK,iBAAiB,IAAI,KAAK;AAC7C,MAAI,MAAO,OAAM,OAAO,SAAS;CAClC;CAED,MAAM,KACJC,MACAC,SACA,GAAG,MACH;EACA,MAAM,EAAE,aAAa,MAAM,UAAU,OAAO,GAAG,WAAW,CAAE;EAC5D,MAAM,QAAQ,KAAK,iBAAiB,IAAI,KAAK;AAC7C,OAAK,MAAO;EACZ,MAAM,WAAW,MAAM,KAAK,MAAM;AAClC,MAAI,YAAY;AACd,SAAM,QAAQ,IAAI,SAAS,IAAI,CAAC,SAAS,KAAK,GAAG,KAAK,CAAC,CAAC;EACzD,OAAM;AACL,OAAI,QAAS,UAAS,SAAS;AAC/B,QAAK,MAAM,QAAQ,SAAU,OAAM,KAAK,GAAG,KAAK;EACjD;CACF;CAED,QAAQ;AACN,OAAK,iBAAiB,OAAO;CAC9B;AACF;AAED,OAAO,MAAM,mBAAmB,CAACC,UAC9B,gBAAgB,KAAK","names":["from: Hooks","to: Hooks","name: string","callback: Callback","name: T","options: { concurrent?: boolean; reverse?: boolean } | undefined","hook: Hook | (object & string)"],"sources":["../src/hooks.ts"],"sourcesContent":["import type { Callback } from '../../common/src/index.ts'\nimport { kHookCollection } from './constants.ts'\nimport type { Hook } from './enums.ts'\n\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"],"version":3,"file":"hooks.js"}
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"mappings":"AAAA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc","names":[],"sources":["../src/index.ts"],"sourcesContent":["export * from './constants.ts'\nexport * from './container.ts'\nexport * from './enums.ts'\nexport * from './hooks.ts'\nexport * from './injectables.ts'\nexport * from './logger.ts'\nexport * from './metadata.ts'\nexport * from './plugin.ts'\nexport * from './registry.ts'\nexport * from './types.ts'\nexport * from './utils/index.ts'\n"],"version":3,"file":"index.js"}