@nmtjs/core 0.12.6 → 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.
@@ -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 '@nmtjs/common'
2
-
3
- interface PoolOptions {
4
- timeout?: number
5
- }
6
-
7
- interface PoolQueueItem {
8
- resolve?: Callback
9
- timer?: ReturnType<typeof setTimeout>
10
- }
11
-
12
- export class PoolError extends Error {}
13
-
14
- // Fixed pool from https://github.com/metarhia/metautil
15
- export class Pool<T = unknown> {
16
- #items: Array<T> = []
17
- #free: Array<boolean> = []
18
- #queue: PoolQueueItem[] = []
19
- #current: number = 0
20
- #size: number = 0
21
- #available: number = 0
22
-
23
- constructor(private readonly options: PoolOptions = {}) {}
24
-
25
- add(item: T) {
26
- if (this.#items.includes(item)) throw new PoolError('Item already exists')
27
- this.#size++
28
- this.#available++
29
- this.#items.push(item)
30
- this.#free.push(true)
31
- }
32
-
33
- remove(item: T) {
34
- if (this.#size === 0) throw new PoolError('Pool is empty')
35
- const index = this.#items.indexOf(item)
36
- if (index < 0) throw new PoolError('Item is not in the pool')
37
- const isCaptured = this.isFree(item)
38
- if (isCaptured) this.#available--
39
- this.#size--
40
- this.#current--
41
- this.#items.splice(index, 1)
42
- this.#free.splice(index, 1)
43
- }
44
-
45
- capture(timeout = this.options.timeout) {
46
- return this.next(true, timeout)
47
- }
48
-
49
- async next(exclusive = false, timeout = this.options.timeout): Promise<T> {
50
- if (this.#size === 0) throw new PoolError('Pool is empty')
51
- if (this.#available === 0) {
52
- return new Promise((resolve, reject) => {
53
- const waiting: PoolQueueItem = {
54
- resolve: (item: T) => {
55
- if (exclusive) this.#capture(item)
56
- resolve(item)
57
- },
58
- timer: undefined,
59
- }
60
- if (timeout) {
61
- waiting.timer = setTimeout(() => {
62
- waiting.resolve = undefined
63
- this.#queue.shift()
64
- reject(new PoolError('Next item timeout'))
65
- }, timeout)
66
- }
67
- this.#queue.push(waiting)
68
- })
69
- }
70
- let item: T | undefined
71
- let free = false
72
- do {
73
- item = this.#items[this.#current]
74
- free = this.#free[this.#current]
75
- this.#current++
76
- if (this.#current >= this.#size) this.#current = 0
77
- } while (typeof item === 'undefined' || !free)
78
- if (exclusive) this.#capture(item)
79
- return item
80
- }
81
-
82
- release(item: T) {
83
- const index = this.#items.indexOf(item)
84
- if (index < 0) throw new PoolError('Unexpected item')
85
- if (this.#free[index])
86
- throw new PoolError('Unable to release not captured item')
87
- this.#free[index] = true
88
- this.#available++
89
- if (this.#queue.length > 0) {
90
- const { resolve, timer } = this.#queue.shift()!
91
- clearTimeout(timer)
92
- if (resolve) setTimeout(resolve, 0, item)
93
- }
94
- }
95
-
96
- isFree(item: T) {
97
- const index = this.#items.indexOf(item)
98
- if (index < 0) return false
99
- return this.#free[index]
100
- }
101
-
102
- get items() {
103
- return [...this.#items]
104
- }
105
-
106
- #capture(item: T) {
107
- const index = this.#items.indexOf(item)
108
- this.#free[index] = false
109
- this.#available--
110
- }
111
- }
@@ -1,63 +0,0 @@
1
- import type { Callback } from '@nmtjs/common'
2
-
3
- interface SemaphoreQueueItem {
4
- resolve?: Callback
5
- timer?: ReturnType<typeof setTimeout>
6
- }
7
-
8
- export class SemaphoreError extends Error {}
9
-
10
- // Semaphore from https://github.com/metarhia/metautil
11
- export class Semaphore {
12
- private counter: number
13
-
14
- private readonly queue: SemaphoreQueueItem[] = []
15
-
16
- constructor(
17
- concurrency: number,
18
- private readonly size: number = 0,
19
- private readonly timeout: number = 0,
20
- ) {
21
- this.counter = concurrency
22
- }
23
-
24
- enter(): Promise<void> {
25
- if (this.counter > 0) {
26
- this.counter--
27
- return Promise.resolve()
28
- } else if (this.queue.length >= this.size) {
29
- return Promise.reject(new SemaphoreError('Queue is full'))
30
- } else {
31
- return new Promise((resolve, reject) => {
32
- const waiting: SemaphoreQueueItem = { resolve }
33
- waiting.timer = setTimeout(() => {
34
- waiting.resolve = undefined
35
- this.queue.shift()
36
- reject(new SemaphoreError('Timeout'))
37
- }, this.timeout)
38
- this.queue.push(waiting)
39
- })
40
- }
41
- }
42
-
43
- leave() {
44
- if (this.queue.length === 0) {
45
- this.counter++
46
- } else {
47
- const item = this.queue.shift()
48
- if (item) {
49
- const { resolve, timer } = item
50
- if (timer) clearTimeout(timer)
51
- if (resolve) setTimeout(resolve, 0)
52
- }
53
- }
54
- }
55
-
56
- get isEmpty() {
57
- return this.queue.length === 0
58
- }
59
-
60
- get queueLength() {
61
- return this.queue.length
62
- }
63
- }