@pori15/logixlysia 6.0.1 → 6.0.3

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,91 +1,91 @@
1
- import { promises as fs } from 'node:fs'
2
- import { basename, dirname } from 'node:path'
3
-
4
- const SIZE_REGEX = /^(\d+(?:\.\d+)?)(k|kb|m|mb|g|gb)$/i
5
- const INTERVAL_REGEX = /^(\d+)(h|d|w)$/i
6
- const ROTATED_REGEX = /\.(\d{4}-\d{2}-\d{2}-\d{2}-\d{2}-\d{2})(?:\.gz)?$/
7
-
8
- export const parseSize = (value: number | string): number => {
9
- if (typeof value === 'number') {
10
- return value
11
- }
12
-
13
- const trimmed = value.trim()
14
- const asNumber = Number(trimmed)
15
- if (Number.isFinite(asNumber)) {
16
- return asNumber
17
- }
18
-
19
- const match = trimmed.match(SIZE_REGEX)
20
- if (!match) {
21
- throw new Error(`Invalid size format: ${value}`)
22
- }
23
-
24
- const amount = Number(match[1])
25
- const unit = match[2].toLowerCase()
26
-
27
- let base = 1024
28
- if (unit.startsWith('m')) {
29
- base = 1024 * 1024
30
- } else if (unit.startsWith('g')) {
31
- base = 1024 * 1024 * 1024
32
- }
33
-
34
- return Math.floor(amount * base)
35
- }
36
-
37
- export const parseInterval = (value: string): number => {
38
- const match = value.trim().match(INTERVAL_REGEX)
39
- if (!match) {
40
- throw new Error(`Invalid interval format: ${value}`)
41
- }
42
-
43
- const amount = Number(match[1])
44
- const unit = match[2].toLowerCase()
45
-
46
- let ms = 60 * 60 * 1000
47
- if (unit === 'd') {
48
- ms = 24 * 60 * 60 * 1000
49
- } else if (unit === 'w') {
50
- ms = 7 * 24 * 60 * 60 * 1000
51
- }
52
-
53
- return amount * ms
54
- }
55
-
56
- export const parseRetention = (
57
- value: number | string
58
- ): { type: 'count' | 'time'; value: number } => {
59
- if (typeof value === 'number') {
60
- return { type: 'count', value }
61
- }
62
- return { type: 'time', value: parseInterval(value) }
63
- }
64
-
65
- export const shouldRotateBySize = async (
66
- filePath: string,
67
- maxSizeBytes: number
68
- ): Promise<boolean> => {
69
- try {
70
- const stat = await fs.stat(filePath)
71
- return stat.size > maxSizeBytes
72
- } catch {
73
- return false
74
- }
75
- }
76
-
77
- export const getRotatedFiles = async (filePath: string): Promise<string[]> => {
78
- const dir = dirname(filePath)
79
- const base = basename(filePath)
80
-
81
- let entries: string[]
82
- try {
83
- entries = await fs.readdir(dir)
84
- } catch {
85
- return []
86
- }
87
-
88
- return entries
89
- .filter(name => name.startsWith(`${base}.`) && ROTATED_REGEX.test(name))
90
- .map(name => `${dir}/${name}`)
91
- }
1
+ import { promises as fs } from 'node:fs'
2
+ import { basename, dirname } from 'node:path'
3
+
4
+ const SIZE_REGEX = /^(\d+(?:\.\d+)?)(k|kb|m|mb|g|gb)$/i
5
+ const INTERVAL_REGEX = /^(\d+)(h|d|w)$/i
6
+ const ROTATED_REGEX = /\.(\d{4}-\d{2}-\d{2}-\d{2}-\d{2}-\d{2})(?:\.gz)?$/
7
+
8
+ export const parseSize = (value: number | string): number => {
9
+ if (typeof value === 'number') {
10
+ return value
11
+ }
12
+
13
+ const trimmed = value.trim()
14
+ const asNumber = Number(trimmed)
15
+ if (Number.isFinite(asNumber)) {
16
+ return asNumber
17
+ }
18
+
19
+ const match = trimmed.match(SIZE_REGEX)
20
+ if (!match) {
21
+ throw new Error(`Invalid size format: ${value}`)
22
+ }
23
+
24
+ const amount = Number(match[1])
25
+ const unit = match[2].toLowerCase()
26
+
27
+ let base = 1024
28
+ if (unit.startsWith('m')) {
29
+ base = 1024 * 1024
30
+ } else if (unit.startsWith('g')) {
31
+ base = 1024 * 1024 * 1024
32
+ }
33
+
34
+ return Math.floor(amount * base)
35
+ }
36
+
37
+ export const parseInterval = (value: string): number => {
38
+ const match = value.trim().match(INTERVAL_REGEX)
39
+ if (!match) {
40
+ throw new Error(`Invalid interval format: ${value}`)
41
+ }
42
+
43
+ const amount = Number(match[1])
44
+ const unit = match[2].toLowerCase()
45
+
46
+ let ms = 60 * 60 * 1000
47
+ if (unit === 'd') {
48
+ ms = 24 * 60 * 60 * 1000
49
+ } else if (unit === 'w') {
50
+ ms = 7 * 24 * 60 * 60 * 1000
51
+ }
52
+
53
+ return amount * ms
54
+ }
55
+
56
+ export const parseRetention = (
57
+ value: number | string
58
+ ): { type: 'count' | 'time'; value: number } => {
59
+ if (typeof value === 'number') {
60
+ return { type: 'count', value }
61
+ }
62
+ return { type: 'time', value: parseInterval(value) }
63
+ }
64
+
65
+ export const shouldRotateBySize = async (
66
+ filePath: string,
67
+ maxSizeBytes: number
68
+ ): Promise<boolean> => {
69
+ try {
70
+ const stat = await fs.stat(filePath)
71
+ return stat.size > maxSizeBytes
72
+ } catch {
73
+ return false
74
+ }
75
+ }
76
+
77
+ export const getRotatedFiles = async (filePath: string): Promise<string[]> => {
78
+ const dir = dirname(filePath)
79
+ const base = basename(filePath)
80
+
81
+ let entries: string[]
82
+ try {
83
+ entries = await fs.readdir(dir)
84
+ } catch {
85
+ return []
86
+ }
87
+
88
+ return entries
89
+ .filter(name => name.startsWith(`${base}.`) && ROTATED_REGEX.test(name))
90
+ .map(name => `${dir}/${name}`)
91
+ }