@flowrdesk/silo 1.0.0

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.
@@ -0,0 +1,247 @@
1
+ /**
2
+ * Copyright 2026 John Spriggs (Flowrdesk LLC)
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+
18
+ /*
19
+ * File that handles all the custom functions that the app uses
20
+ *
21
+ */
22
+ // local dependencies
23
+ import { mkdir, readdir, rename, stat } from 'node:fs/promises'
24
+ import path from 'node:path'
25
+
26
+ // local settings
27
+ import { _config } from './settings.js'
28
+
29
+ // a function that verifies a string that isn't empty
30
+ export const isString = (data) => {
31
+ return typeof(data) === 'string' && data.trim().length > 0
32
+ }
33
+
34
+ // a function that verifies a number that is zero or greater
35
+ export const isNumber = (data) => {
36
+ return typeof(data) === 'number' && data >= 0
37
+ }
38
+
39
+ // a function that verifies an object
40
+ export const isObject = (data) => {
41
+ return data !== null && Array.isArray(data) === false && typeof(data) === 'object'
42
+ }
43
+
44
+ // object that handles the ANSI Terminal Colors
45
+ export const colors = {
46
+ 'txt': {
47
+ // normal color codes
48
+ 'black': '30',
49
+ 'red': '31',
50
+ 'green': '32',
51
+ 'yellow': '33',
52
+ 'blue': '34',
53
+ 'magenta': '35',
54
+ 'cyan': '36',
55
+ 'white': '37',
56
+ // high intensity colors (bright colors)
57
+ 'bright_black': '90',
58
+ 'bright_red': '91',
59
+ 'bright_green': '92',
60
+ 'bright_yellow': '93',
61
+ 'bright_blue': '94',
62
+ 'bright_magenta': '95',
63
+ 'bright_cyan': '96',
64
+ 'bright_white': '97'
65
+ },
66
+ 'bg': {
67
+ // normal color codes
68
+ 'black': '40',
69
+ 'red': '41',
70
+ 'green': '42',
71
+ 'yellow': '43',
72
+ 'blue': '44',
73
+ 'magenta': '45',
74
+ 'cyan': '46',
75
+ 'white': '47',
76
+ // high intensity colors (bright colors)
77
+ 'bright_black': '100',
78
+ 'bright_red': '101',
79
+ 'bright_green': '102',
80
+ 'bright_yellow': '103',
81
+ 'bright_blue': '104',
82
+ 'bright_magenta': '105',
83
+ 'bright_cyan': '106',
84
+ 'bright_white': '107'
85
+ },
86
+ 'reset': '\x1b[00m'
87
+ }
88
+
89
+ // a function that checks a variable and returns it for a string
90
+ export const checkVariable = (data) => {
91
+ if(typeof(data) === 'string'){
92
+ const checked = '"' + data + '"'
93
+ return checked
94
+ } else if(typeof(data) === 'number' || typeof(data) === 'boolean' || data === null || data === undefined){
95
+ return data
96
+ } else if(isObject(data)){
97
+ let checked = '{'
98
+ const keys = Object.keys(data)
99
+ for(let i = 0; i < keys.length; i++){
100
+ checked += '"' + keys[i] + '":'
101
+ checked += checkVariable(data[keys[i]])
102
+ if(i + 1 !== keys.length){
103
+ checked += ','
104
+ }
105
+ }
106
+ checked += '}'
107
+ return checked
108
+ } else if(Array.isArray(data)){
109
+ let checked = '['
110
+ for(let i = 0; i < data.length; i++){
111
+ checked += checkVariable(data[i])
112
+ if(i + 1 !== data.length){
113
+ checked += ','
114
+ }
115
+ }
116
+ checked += ']'
117
+ return checked
118
+ } else {
119
+ console.error(`The function used to check variables was unable to check a variable and didn't pass the data in the logs`)
120
+ }
121
+ }
122
+
123
+ // a function that verifies log format and returns a string
124
+ export const verifyAndFormatLog = (data) => {
125
+ let str = '"message":'
126
+ if(data instanceof Error){
127
+ if(!data.message && !data.stack && !data.code){
128
+ str += '"Received an Error, but no data was provided in this error object!"'
129
+ return str
130
+ }
131
+ str += '{'
132
+ if(data.message) str += '"error_message":' + checkVariable(data.message) + ','
133
+ if(data.stack) str += '"error_stack":' + checkVariable(data.stack) + ','
134
+ if(data.code) str += '"error_code":' + checkVariable(data.code)
135
+ str += '}'
136
+ return str
137
+ } else if(isObject(data)){
138
+ str += '{'
139
+ const keys = Object.keys(data)
140
+ for(let i = 0; i < keys.length; i++){
141
+ str += '"' + keys[i] + '":'
142
+ str += checkVariable(data[keys[i]])
143
+ if(i + 1 !== keys.length){
144
+ str += ','
145
+ }
146
+ }
147
+ str += '}'
148
+ return str
149
+ } else if(Array.isArray(data)){
150
+ str += '['
151
+ for(let i = 0; i < data.length; i++){
152
+ str += checkVariable(data[i])
153
+ if(i + 1 !== data.length){
154
+ str += ','
155
+ }
156
+ }
157
+ str += ']'
158
+ return str
159
+ } else if(typeof(data) === 'string' || typeof(data) === 'number' || typeof(data) === 'boolean' || data === null || data === undefined){
160
+ str += checkVariable(data)
161
+ return str
162
+ } else {
163
+ str += '[Error: Unable to format log as the type of log is not supported please check terminal]'
164
+ console.error(`[Error Formating Log]: type: ${typeof(data)}\nLog Received: `, data)
165
+ return str
166
+ }
167
+
168
+ }
169
+
170
+ // function used to create a dirctory
171
+ export const createDir = async () => {
172
+ try {
173
+ mkdir(_config.baseDir,{recursive: true})
174
+ } catch (error) {
175
+ throw new Error(`[Fatal Error creating Directory (${_config.baseDir})]: ${error.message}`)
176
+ }
177
+ }
178
+
179
+ // function used to index files addding '_000' to the filename
180
+ export const indexFile = async (filename) => {
181
+ // grab all the files in the directory
182
+ const files = await readdir(_config.baseDir)
183
+
184
+ // update the filename to remove file ext.
185
+ const checkFilename = filename.replace('.log', '')
186
+
187
+ // filter only the files that match our file to index
188
+ const matches = files.filter(name => name.startsWith(checkFilename))
189
+
190
+ // count all the files that match
191
+ const count = matches.length
192
+
193
+ // get the paths ready to index the file
194
+ const currentFilePath = path.join(_config.baseDir, filename)
195
+ const newFilePath = path.join(_config.baseDir, checkFilename + '_' + String(count).padStart(3, '0') + '.log')
196
+
197
+ // try to rename file and handle any errors
198
+ try {
199
+ await rename(currentFilePath, newFilePath)
200
+ } catch (error) {
201
+ console.error(`[Error Indexing the file(${filename})]: ${error.message}`)
202
+ }
203
+ }
204
+
205
+ // function that checks the file size and returns a number or null
206
+ export const checkTheFileSize = async (filename) => {
207
+ try {
208
+ const fileInfo = await stat(path.join(_config.baseDir, filename))
209
+ return fileInfo.size
210
+ } catch (error) {
211
+ if(error.code === 'ENOENT') {
212
+ return null
213
+ }
214
+ console.error(`[Error Checking File Size (${filename})]: ${error.message}`)
215
+ }
216
+ }
217
+
218
+ const padded = Array.from({length: 100},(_,i) => i.toString().padStart(2,'0'))
219
+
220
+ const paddedMs = Array.from({length: 1000},(_,i) => i.toString().padStart(3,'0'))
221
+
222
+ // function that returns the date
223
+ export const getDate = () => {
224
+ const d = new Date()
225
+ return d.getFullYear().toString() + '-' +
226
+ padded[d.getMonth() + 1] + '-' +
227
+ padded[d.getDate()]
228
+ }
229
+
230
+ // function that returns the time
231
+ export const getTimeStamp = () => {
232
+ const d = new Date()
233
+ return d.getFullYear().toString() + '-' +
234
+ padded[d.getMonth() + 1] + '-'+
235
+ padded[d.getDate()] + 'T'+
236
+ padded[d.getHours()] + ':' +
237
+ padded[d.getMinutes()] + ':' +
238
+ padded[d.getSeconds()] + '.' +
239
+ paddedMs[d.getMilliseconds()]
240
+ }
241
+
242
+ // function checks for unsafe filenames
243
+ export const isFilenameSafe = (format) => {
244
+ const unsafePattern = /[\\/:\*?"<>|.]/g;
245
+ return !unsafePattern.test(format);
246
+ }
247
+