@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/src/plugin.ts DELETED
@@ -1,27 +0,0 @@
1
- import type { Async } from '../../common/src/index.ts'
2
- import { kPlugin } from './constants.ts'
3
- import type { PluginContext } from './types.ts'
4
-
5
- export interface BasePlugin<
6
- Type = any,
7
- Options = unknown,
8
- Context extends PluginContext = PluginContext,
9
- > {
10
- name: string
11
- init: (context: Context, options: Options) => Async<Type>
12
- }
13
-
14
- export interface Plugin<
15
- Type = void,
16
- Options = unknown,
17
- Context extends PluginContext = PluginContext,
18
- > extends BasePlugin<Type, Options, Context> {
19
- [kPlugin]: any
20
- }
21
-
22
- export const createPlugin = <Options = unknown, Type = void>(
23
- name: string,
24
- init: Plugin<Type, Options>['init'],
25
- ): Plugin<Type, Options> => ({ name, init, [kPlugin]: true })
26
-
27
- export const isPlugin = (value: any): value is Plugin => kPlugin in value
package/src/registry.ts DELETED
@@ -1,44 +0,0 @@
1
- import { type Hook, Scope } from './enums.ts'
2
- import { Hooks, type HookType } from './hooks.ts'
3
- import {
4
- type AnyInjectable,
5
- type Dependant,
6
- getInjectableScope,
7
- } from './injectables.ts'
8
- import type { Logger } from './logger.ts'
9
-
10
- export class Registry {
11
- readonly hooks = new Hooks()
12
-
13
- constructor(
14
- protected readonly application: {
15
- logger: Logger
16
- },
17
- ) {}
18
-
19
- registerHooks<T extends Hooks>(hooks: T) {
20
- Hooks.merge(hooks, this.hooks)
21
- }
22
-
23
- registerHook<T extends Hook>(name: T, callback: HookType[T]) {
24
- this.hooks.add(name, callback)
25
- }
26
-
27
- *getDependants(): Generator<Dependant> {}
28
-
29
- clear() {
30
- this.hooks.clear()
31
- }
32
- }
33
-
34
- export const scopeErrorMessage = (name, scope = Scope.Global) =>
35
- `${name} must be a ${scope} scope (including all nested dependencies)`
36
-
37
- export function hasInvalidScopeDeps(
38
- injectables: AnyInjectable[],
39
- scope = Scope.Global,
40
- ) {
41
- return injectables.some(
42
- (injectable) => getInjectableScope(injectable) !== scope,
43
- )
44
- }
package/src/types.ts DELETED
@@ -1,13 +0,0 @@
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
-
6
- export interface PluginContext {
7
- logger: Logger
8
- registry: Registry
9
- hooks: Hooks
10
- container: Container
11
- }
12
-
13
- export type Pattern = RegExp | string | ((value: string) => boolean)
@@ -1,31 +0,0 @@
1
- import type { Pattern } from '../types.ts'
2
-
3
- /**
4
- * Very simple pattern matching function.
5
- */
6
- export function match(value: string, pattern: Pattern) {
7
- if (typeof pattern === 'function') {
8
- return pattern(value)
9
- } else if (typeof pattern === 'string') {
10
- if (pattern === '*' || pattern === '**') {
11
- return true
12
- } else if (pattern.at(0) === '*' && pattern.at(-1) === '*') {
13
- return value.includes(pattern.slice(1, -1))
14
- } else if (pattern.at(-1) === '*') {
15
- return value.startsWith(pattern.slice(0, -1))
16
- } else if (pattern.at(0) === '*') {
17
- return value.endsWith(pattern.slice(1))
18
- } else {
19
- return value === pattern
20
- }
21
- } else {
22
- return pattern.test(value)
23
- }
24
- }
25
-
26
- export function isJsFile(name: string) {
27
- if (name.endsWith('.d.ts')) return false
28
- const leading = name.split('.').slice(1)
29
- const ext = leading.join('.')
30
- return ['js', 'mjs', 'cjs', 'ts', 'mts', 'cts'].includes(ext)
31
- }
@@ -1,3 +0,0 @@
1
- export * from './functions.ts'
2
- export * from './pool.ts'
3
- export * from './semaphore.ts'
package/src/utils/pool.ts DELETED
@@ -1,111 +0,0 @@
1
- import type { Callback } from '../../../common/src/index.ts'
2
-
3
- interface PoolOptions {
4
- timeout?: number
5
- }
6
-
7
- interface PoolQueueItem {
8
- resolve?: Callback
9
- timer?: ReturnType<typeof setTimeout>
10
- }
11
-
12
- export class PoolError extends Error {}
13
-
14
- // Fixed pool from https://github.com/metarhia/metautil
15
- export class Pool<T = unknown> {
16
- #items: Array<T> = []
17
- #free: Array<boolean> = []
18
- #queue: PoolQueueItem[] = []
19
- #current: number = 0
20
- #size: number = 0
21
- #available: number = 0
22
-
23
- constructor(private readonly options: PoolOptions = {}) {}
24
-
25
- add(item: T) {
26
- if (this.#items.includes(item)) throw new PoolError('Item already exists')
27
- this.#size++
28
- this.#available++
29
- this.#items.push(item)
30
- this.#free.push(true)
31
- }
32
-
33
- remove(item: T) {
34
- if (this.#size === 0) throw new PoolError('Pool is empty')
35
- const index = this.#items.indexOf(item)
36
- if (index < 0) throw new PoolError('Item is not in the pool')
37
- const isCaptured = this.isFree(item)
38
- if (isCaptured) this.#available--
39
- this.#size--
40
- this.#current--
41
- this.#items.splice(index, 1)
42
- this.#free.splice(index, 1)
43
- }
44
-
45
- capture(timeout = this.options.timeout) {
46
- return this.next(true, timeout)
47
- }
48
-
49
- async next(exclusive = false, timeout = this.options.timeout): Promise<T> {
50
- if (this.#size === 0) throw new PoolError('Pool is empty')
51
- if (this.#available === 0) {
52
- return new Promise((resolve, reject) => {
53
- const waiting: PoolQueueItem = {
54
- resolve: (item: T) => {
55
- if (exclusive) this.#capture(item)
56
- resolve(item)
57
- },
58
- timer: undefined,
59
- }
60
- if (timeout) {
61
- waiting.timer = setTimeout(() => {
62
- waiting.resolve = undefined
63
- this.#queue.shift()
64
- reject(new PoolError('Next item timeout'))
65
- }, timeout)
66
- }
67
- this.#queue.push(waiting)
68
- })
69
- }
70
- let item: T | undefined
71
- let free = false
72
- do {
73
- item = this.#items[this.#current]
74
- free = this.#free[this.#current]
75
- this.#current++
76
- if (this.#current >= this.#size) this.#current = 0
77
- } while (typeof item === 'undefined' || !free)
78
- if (exclusive) this.#capture(item)
79
- return item
80
- }
81
-
82
- release(item: T) {
83
- const index = this.#items.indexOf(item)
84
- if (index < 0) throw new PoolError('Unexpected item')
85
- if (this.#free[index])
86
- throw new PoolError('Unable to release not captured item')
87
- this.#free[index] = true
88
- this.#available++
89
- if (this.#queue.length > 0) {
90
- const { resolve, timer } = this.#queue.shift()!
91
- clearTimeout(timer)
92
- if (resolve) setTimeout(resolve, 0, item)
93
- }
94
- }
95
-
96
- isFree(item: T) {
97
- const index = this.#items.indexOf(item)
98
- if (index < 0) return false
99
- return this.#free[index]
100
- }
101
-
102
- get items() {
103
- return [...this.#items]
104
- }
105
-
106
- #capture(item: T) {
107
- const index = this.#items.indexOf(item)
108
- this.#free[index] = false
109
- this.#available--
110
- }
111
- }
@@ -1,63 +0,0 @@
1
- import type { Callback } from '../../../common/src/index.ts'
2
-
3
- interface SemaphoreQueueItem {
4
- resolve?: Callback
5
- timer?: ReturnType<typeof setTimeout>
6
- }
7
-
8
- export class SemaphoreError extends Error {}
9
-
10
- // Semaphore from https://github.com/metarhia/metautil
11
- export class Semaphore {
12
- private counter: number
13
-
14
- private readonly queue: SemaphoreQueueItem[] = []
15
-
16
- constructor(
17
- concurrency: number,
18
- private readonly size: number = 0,
19
- private readonly timeout: number = 0,
20
- ) {
21
- this.counter = concurrency
22
- }
23
-
24
- enter(): Promise<void> {
25
- if (this.counter > 0) {
26
- this.counter--
27
- return Promise.resolve()
28
- } else if (this.queue.length >= this.size) {
29
- return Promise.reject(new SemaphoreError('Queue is full'))
30
- } else {
31
- return new Promise((resolve, reject) => {
32
- const waiting: SemaphoreQueueItem = { resolve }
33
- waiting.timer = setTimeout(() => {
34
- waiting.resolve = undefined
35
- this.queue.shift()
36
- reject(new SemaphoreError('Timeout'))
37
- }, this.timeout)
38
- this.queue.push(waiting)
39
- })
40
- }
41
- }
42
-
43
- leave() {
44
- if (this.queue.length === 0) {
45
- this.counter++
46
- } else {
47
- const item = this.queue.shift()
48
- if (item) {
49
- const { resolve, timer } = item
50
- if (timer) clearTimeout(timer)
51
- if (resolve) setTimeout(resolve, 0)
52
- }
53
- }
54
- }
55
-
56
- get isEmpty() {
57
- return this.queue.length === 0
58
- }
59
-
60
- get queueLength() {
61
- return this.queue.length
62
- }
63
- }