semola 0.5.4 → 0.6.1

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 (50) hide show
  1. package/README.md +35 -50
  2. package/dist/lib/api/index.cjs +1 -523
  3. package/dist/lib/api/index.d.cts +1 -271
  4. package/dist/lib/api/index.d.mts +80 -84
  5. package/dist/lib/api/index.mjs +1 -521
  6. package/dist/lib/cache/index.cjs +1 -74
  7. package/dist/lib/cache/index.d.cts +1 -48
  8. package/dist/lib/cache/index.d.mts +3 -24
  9. package/dist/lib/cache/index.mjs +1 -73
  10. package/dist/lib/cron/index.cjs +1 -735
  11. package/dist/lib/cron/index.d.cts +1 -146
  12. package/dist/lib/cron/index.d.mts +99 -36
  13. package/dist/lib/cron/index.mjs +1 -726
  14. package/dist/lib/errors/index.cjs +1 -30
  15. package/dist/lib/errors/index.d.cts +1 -2
  16. package/dist/lib/errors/index.d.mts +12 -1
  17. package/dist/lib/errors/index.mjs +1 -26
  18. package/dist/lib/i18n/index.cjs +1 -42
  19. package/dist/lib/i18n/index.d.cts +1 -28
  20. package/dist/lib/i18n/index.mjs +1 -41
  21. package/dist/lib/logging/index.cjs +1 -387
  22. package/dist/lib/logging/index.d.cts +1 -108
  23. package/dist/lib/logging/index.mjs +1 -374
  24. package/dist/lib/orm/index.cjs +1 -0
  25. package/dist/lib/orm/index.d.cts +1 -0
  26. package/dist/lib/orm/index.d.mts +404 -0
  27. package/dist/lib/orm/index.mjs +1 -0
  28. package/dist/lib/policy/index.cjs +1 -285
  29. package/dist/lib/policy/index.d.cts +1 -77
  30. package/dist/lib/policy/index.mjs +1 -265
  31. package/dist/lib/prompts/index.cjs +6 -769
  32. package/dist/lib/prompts/index.d.cts +1 -96
  33. package/dist/lib/prompts/index.d.mts +12 -33
  34. package/dist/lib/prompts/index.mjs +6 -763
  35. package/dist/lib/pubsub/index.cjs +1 -117
  36. package/dist/lib/pubsub/index.d.cts +1 -41
  37. package/dist/lib/pubsub/index.d.mts +3 -18
  38. package/dist/lib/pubsub/index.mjs +1 -116
  39. package/dist/lib/queue/index.cjs +1 -182
  40. package/dist/lib/queue/index.d.cts +1 -74
  41. package/dist/lib/queue/index.d.mts +11 -4
  42. package/dist/lib/queue/index.mjs +1 -181
  43. package/dist/lib/workflow/index.cjs +1 -534
  44. package/dist/lib/workflow/index.d.cts +1 -85
  45. package/dist/lib/workflow/index.d.mts +76 -11
  46. package/dist/lib/workflow/index.mjs +1 -533
  47. package/dist/rolldown-runtime-CMqjfN_6.cjs +1 -0
  48. package/package.json +17 -5
  49. package/dist/index-BhGNDjPq.d.mts +0 -13
  50. package/dist/index-DxSbeGP-.d.cts +0 -13
@@ -1,30 +1 @@
1
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- //#region src/lib/errors/index.ts
3
- const ok = (data) => {
4
- return [null, data];
5
- };
6
- const err = (type, message) => {
7
- return [{
8
- type,
9
- message
10
- }, null];
11
- };
12
- const mightThrowSync = (fn) => {
13
- try {
14
- return [null, fn()];
15
- } catch (error) {
16
- return [error, null];
17
- }
18
- };
19
- const mightThrow = async (promise) => {
20
- try {
21
- return [null, await promise];
22
- } catch (error) {
23
- return [error, null];
24
- }
25
- };
26
- //#endregion
27
- exports.err = err;
28
- exports.mightThrow = mightThrow;
29
- exports.mightThrowSync = mightThrowSync;
30
- exports.ok = ok;
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=e=>[null,e],t=(e,t)=>[{type:e,message:t},null],n=e=>{try{return[null,e()]}catch(e){return[e,null]}},r=async e=>{try{return[null,await e]}catch(e){return[e,null]}};exports.err=t,exports.mightThrow=r,exports.mightThrowSync=n,exports.ok=e;
@@ -1,2 +1 @@
1
- import { i as ok, n as mightThrow, r as mightThrowSync, t as err } from "../../index-DxSbeGP-.cjs";
2
- export { err, mightThrow, mightThrowSync, ok };
1
+ export type * from './index.d.mts'
@@ -1,2 +1,13 @@
1
- import { i as ok, n as mightThrow, r as mightThrowSync, t as err } from "../../index-BhGNDjPq.mjs";
1
+ //#region src/lib/errors/types.d.ts
2
+ type CommonError = "NotFoundError" | "UnauthorizedError" | "InternalServerError" | "ValidationError" | "MigrationError" | "SchemaError" | (string & {});
3
+ //#endregion
4
+ //#region src/lib/errors/index.d.ts
5
+ declare const ok: <T>(data: T) => readonly [null, T];
6
+ declare const err: <T extends CommonError>(type: T, message: string) => readonly [{
7
+ readonly type: T;
8
+ readonly message: string;
9
+ }, null];
10
+ declare const mightThrowSync: <T, E = Error>(fn: () => T) => readonly [null, T] | readonly [E, null];
11
+ declare const mightThrow: <T, E = Error>(promise: Promise<T>) => Promise<readonly [null, T] | readonly [E, null]>;
12
+ //#endregion
2
13
  export { err, mightThrow, mightThrowSync, ok };
@@ -1,26 +1 @@
1
- //#region src/lib/errors/index.ts
2
- const ok = (data) => {
3
- return [null, data];
4
- };
5
- const err = (type, message) => {
6
- return [{
7
- type,
8
- message
9
- }, null];
10
- };
11
- const mightThrowSync = (fn) => {
12
- try {
13
- return [null, fn()];
14
- } catch (error) {
15
- return [error, null];
16
- }
17
- };
18
- const mightThrow = async (promise) => {
19
- try {
20
- return [null, await promise];
21
- } catch (error) {
22
- return [error, null];
23
- }
24
- };
25
- //#endregion
26
- export { err, mightThrow, mightThrowSync, ok };
1
+ const e=e=>[null,e],t=(e,t)=>[{type:e,message:t},null],n=e=>{try{return[null,e()]}catch(e){return[e,null]}},r=async e=>{try{return[null,await e]}catch(e){return[e,null]}};export{t as err,r as mightThrow,n as mightThrowSync,e as ok};
@@ -1,42 +1 @@
1
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- //#region src/lib/i18n/index.ts
3
- var I18n = class {
4
- locales;
5
- currentLocale;
6
- defaultLocale;
7
- constructor(config) {
8
- this.defaultLocale = config.defaultLocale;
9
- this.currentLocale = config.defaultLocale;
10
- this.locales = config.locales;
11
- }
12
- setLocale(locale) {
13
- this.currentLocale = locale;
14
- }
15
- getLocale() {
16
- return this.currentLocale;
17
- }
18
- translate(key, ...params) {
19
- const currentTranslations = this.locales[this.currentLocale];
20
- const defaultTranslations = this.locales[this.defaultLocale];
21
- const translation = this.getNestedValue(currentTranslations, key) ?? this.getNestedValue(defaultTranslations, key);
22
- if (typeof translation !== "string") return key;
23
- if (!translation) return key;
24
- const paramObj = params[0];
25
- return this.substituteParams(translation, paramObj);
26
- }
27
- getNestedValue(obj, path) {
28
- return path.split(".").reduce((current, key) => {
29
- if (typeof current !== "object") return;
30
- if (current === null) return;
31
- return current[key];
32
- }, obj);
33
- }
34
- substituteParams(template, params) {
35
- if (!params) return template;
36
- return template.replace(/\{(\w+):(\w+)\}/g, (_match, name) => {
37
- return String(params[name]);
38
- });
39
- }
40
- };
41
- //#endregion
42
- exports.I18n = I18n;
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=class{locales;currentLocale;defaultLocale;constructor(e){this.defaultLocale=e.defaultLocale,this.currentLocale=e.defaultLocale,this.locales=e.locales}setLocale(e){this.currentLocale=e}getLocale(){return this.currentLocale}translate(e,...t){let n=this.locales[this.currentLocale],r=this.locales[this.defaultLocale],i=this.getNestedValue(n,e)??this.getNestedValue(r,e);if(typeof i!=`string`||!i)return e;let a=t[0];return this.substituteParams(i,a)}getNestedValue(e,t){return t.split(`.`).reduce((e,t)=>{if(typeof e==`object`&&e)return e[t]},e)}substituteParams(e,t){return t?e.replace(/\{(\w+):(\w+)\}/g,(e,n)=>String(t[n])):e}};exports.I18n=e;
@@ -1,28 +1 @@
1
- //#region src/lib/i18n/types.d.ts
2
- type ExtractParamType<T extends string> = T extends "string" ? string : T extends "number" ? number : T extends "boolean" ? boolean : never;
3
- type BuildParamObject<T extends string, Acc = {}> = T extends `${infer _Start}{${infer Name}:${infer Type}}${infer Rest}` ? BuildParamObject<Rest, Acc & Record<Name, ExtractParamType<Type>>> : Acc;
4
- type IsString<T> = T extends string ? true : false;
5
- type IsArray<T> = T extends readonly unknown[] ? true : false;
6
- type IsObject<T> = T extends object ? T extends readonly unknown[] ? false : true : false;
7
- type ArrayKeys<T extends readonly unknown[]> = { [K in keyof T & `${number}`]: `${K}` }[keyof T & `${number}`];
8
- type ObjectPropertyKeys<K extends string, V> = IsString<V> extends true ? K : IsArray<V> extends true ? V extends readonly unknown[] ? `${K}.${NestedKeyOf<V>}` : never : IsObject<V> extends true ? `${K}.${NestedKeyOf<V>}` : never;
9
- type NestedKeyOf<T> = IsArray<T> extends true ? T extends readonly unknown[] ? ArrayKeys<T> : never : IsObject<T> extends true ? { [K in keyof T & string]: ObjectPropertyKeys<K, T[K]> }[keyof T & string] : never;
10
- type GetNestedValue<T, K extends string> = K extends `${infer First}.${infer Rest}` ? First extends keyof T ? GetNestedValue<T[First], Rest> : never : K extends keyof T ? T[K] : never;
11
- //#endregion
12
- //#region src/lib/i18n/index.d.ts
13
- declare class I18n<const TLocales extends Record<string, Record<string, unknown>>, TDefaultLocale extends keyof TLocales> {
14
- private locales;
15
- private currentLocale;
16
- private defaultLocale;
17
- constructor(config: {
18
- defaultLocale: TDefaultLocale;
19
- locales: TLocales;
20
- });
21
- setLocale(locale: keyof TLocales): void;
22
- getLocale(): keyof TLocales;
23
- translate<TKey extends NestedKeyOf<TLocales[TDefaultLocale]>>(key: TKey, ...params: BuildParamObject<GetNestedValue<TLocales[TDefaultLocale], TKey> extends string ? GetNestedValue<TLocales[TDefaultLocale], TKey> : never> extends Record<string, never> ? [] : [BuildParamObject<GetNestedValue<TLocales[TDefaultLocale], TKey> extends string ? GetNestedValue<TLocales[TDefaultLocale], TKey> : never>]): string;
24
- private getNestedValue;
25
- private substituteParams;
26
- }
27
- //#endregion
28
- export { I18n };
1
+ export type * from './index.d.mts'
@@ -1,41 +1 @@
1
- //#region src/lib/i18n/index.ts
2
- var I18n = class {
3
- locales;
4
- currentLocale;
5
- defaultLocale;
6
- constructor(config) {
7
- this.defaultLocale = config.defaultLocale;
8
- this.currentLocale = config.defaultLocale;
9
- this.locales = config.locales;
10
- }
11
- setLocale(locale) {
12
- this.currentLocale = locale;
13
- }
14
- getLocale() {
15
- return this.currentLocale;
16
- }
17
- translate(key, ...params) {
18
- const currentTranslations = this.locales[this.currentLocale];
19
- const defaultTranslations = this.locales[this.defaultLocale];
20
- const translation = this.getNestedValue(currentTranslations, key) ?? this.getNestedValue(defaultTranslations, key);
21
- if (typeof translation !== "string") return key;
22
- if (!translation) return key;
23
- const paramObj = params[0];
24
- return this.substituteParams(translation, paramObj);
25
- }
26
- getNestedValue(obj, path) {
27
- return path.split(".").reduce((current, key) => {
28
- if (typeof current !== "object") return;
29
- if (current === null) return;
30
- return current[key];
31
- }, obj);
32
- }
33
- substituteParams(template, params) {
34
- if (!params) return template;
35
- return template.replace(/\{(\w+):(\w+)\}/g, (_match, name) => {
36
- return String(params[name]);
37
- });
38
- }
39
- };
40
- //#endregion
41
- export { I18n };
1
+ var e=class{locales;currentLocale;defaultLocale;constructor(e){this.defaultLocale=e.defaultLocale,this.currentLocale=e.defaultLocale,this.locales=e.locales}setLocale(e){this.currentLocale=e}getLocale(){return this.currentLocale}translate(e,...t){let n=this.locales[this.currentLocale],r=this.locales[this.defaultLocale],i=this.getNestedValue(n,e)??this.getNestedValue(r,e);if(typeof i!=`string`||!i)return e;let a=t[0];return this.substituteParams(i,a)}getNestedValue(e,t){return t.split(`.`).reduce((e,t)=>{if(typeof e==`object`&&e)return e[t]},e)}substituteParams(e,t){return t?e.replace(/\{(\w+):(\w+)\}/g,(e,n)=>String(t[n])):e}};export{e as I18n};
@@ -1,387 +1 @@
1
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- const require_lib_errors_index = require("../errors/index.cjs");
3
- let node_path = require("node:path");
4
- let node_fs = require("node:fs");
5
- //#region src/lib/logging/formatter/index.ts
6
- var Formatter = class {
7
- dateFmt;
8
- constructor(dateFmt) {
9
- this.dateFmt = dateFmt;
10
- }
11
- };
12
- var BaseFormatter = class extends Formatter {
13
- constructor(dateFmt = isoDateTimeFormat) {
14
- super(dateFmt);
15
- }
16
- format(logData) {
17
- let { prefix, level, msg, fileName, row, column, method } = logData;
18
- const timestamp = this.dateFmt();
19
- const levelType = level.toUpperCase();
20
- if (typeof msg === "object") msg = JSON.stringify(msg);
21
- const fileData = `${prefix}/${fileName}:${row}:${column}`;
22
- let header = `[${levelType}]`;
23
- if (method) header = `${header}\t[${method}]\t[${fileData}]`;
24
- else header = `${header}\t[${fileData}]`;
25
- return `${timestamp} ${header} : ${msg}`;
26
- }
27
- formatError(logData, error) {
28
- const { prefix, fileName, row, column, method } = logData;
29
- const timestamp = this.dateFmt();
30
- const fileData = `${prefix}/${fileName}:${row}:${column}`;
31
- let errorMsg = `Error name="${error.name}" Error message="${error.message}"`;
32
- let header = "[ERROR]";
33
- if (method) header = `${header}\t[${method}]\t[${fileData}]`;
34
- else header = `${header}\t[${fileData}]`;
35
- if (error.cause) errorMsg = `${errorMsg}\n\tError cause="${error.cause}"`;
36
- if (error.stack) errorMsg = `${errorMsg}\n\tStack trace="${error.stack}"`;
37
- return `${timestamp} ${header} : ${errorMsg}`;
38
- }
39
- };
40
- var JSONFormatter = class extends Formatter {
41
- constructor(dateFmt = isoDateTimeFormat) {
42
- super(dateFmt);
43
- }
44
- format(logData) {
45
- const { prefix, level, msg, fileName, row, column, method } = logData;
46
- const timestamp = this.dateFmt();
47
- const levelType = level.toUpperCase();
48
- let position = {};
49
- if (method) position = { method };
50
- position = {
51
- ...position,
52
- fileName,
53
- row,
54
- column
55
- };
56
- return JSON.stringify({
57
- timestamp,
58
- level: levelType,
59
- prefix,
60
- position,
61
- msg
62
- });
63
- }
64
- formatError(logData, error) {
65
- const { prefix, fileName, row, column, method } = logData;
66
- const timestamp = this.dateFmt();
67
- let errorMsg = {
68
- errorName: error.name,
69
- errorMessage: error.message
70
- };
71
- let position = {};
72
- if (method) position = { method };
73
- position = {
74
- ...position,
75
- fileName,
76
- row,
77
- column
78
- };
79
- if (error.cause) errorMsg = {
80
- ...errorMsg,
81
- errorCause: error.cause
82
- };
83
- if (error.stack) errorMsg = {
84
- ...errorMsg,
85
- stackTrace: error.stack
86
- };
87
- return JSON.stringify({
88
- timestamp,
89
- level: "ERROR",
90
- prefix,
91
- position,
92
- msg: errorMsg
93
- });
94
- }
95
- };
96
- function isoDateTimeFormat() {
97
- return (/* @__PURE__ */ new Date()).toISOString();
98
- }
99
- function isoDateFormat() {
100
- const date = isoDateTimeFormat().split("T")[0];
101
- if (!date) return "";
102
- return date;
103
- }
104
- function dmyFormat() {
105
- const isoDate = isoDateFormat();
106
- if (!isoDate) return "";
107
- const info = isoDate.split("-");
108
- return `${info[2]}-${info[1]}-${info[0]}`;
109
- }
110
- function mdyFormat() {
111
- const isoDate = isoDateFormat();
112
- if (!isoDate) return "";
113
- const info = isoDate.split("-");
114
- return `${info[1]}-${info[2]}-${info[0]}`;
115
- }
116
- //#endregion
117
- //#region src/lib/logging/core/index.ts
118
- const PROVIDER_OPTION_DEFAULT = {
119
- formatter: new BaseFormatter(),
120
- level: "debug"
121
- };
122
- const STACK_FRAME_IDX = 1;
123
- var StackData = class {
124
- stack = [];
125
- constructor(fn) {
126
- const oldStackTrace = Error.prepareStackTrace;
127
- const [stackTraceError] = require_lib_errors_index.mightThrowSync(() => {
128
- Error.prepareStackTrace = (_, stack) => {
129
- return stack.map((callSite) => {
130
- return {
131
- fileName: callSite.getFileName(),
132
- column: callSite.getColumnNumber(),
133
- row: callSite.getLineNumber(),
134
- functionCall: callSite.getFunctionName()
135
- };
136
- });
137
- };
138
- Error.captureStackTrace(this, fn);
139
- this.stack;
140
- });
141
- Error.prepareStackTrace = oldStackTrace;
142
- if (stackTraceError) this.stack = [];
143
- }
144
- retrieveFrame() {
145
- if (this.stack.length === 0) return void 0;
146
- return this.stack[STACK_FRAME_IDX];
147
- }
148
- };
149
- var AbstractLogger = class {
150
- providers;
151
- prefix;
152
- constructor(prefix, providers) {
153
- this.prefix = prefix;
154
- this.providers = providers;
155
- }
156
- createLogData(level, msg, prefix) {
157
- const logCall = new StackData(this.createLogData).retrieveFrame();
158
- let logData = {
159
- level,
160
- msg,
161
- prefix
162
- };
163
- if (!logCall) return logData;
164
- const { column, fileName, functionCall, row } = logCall;
165
- if (fileName) logData = {
166
- ...logData,
167
- fileName: (0, node_path.basename)(fileName)
168
- };
169
- if (column) logData = {
170
- ...logData,
171
- column: String(column)
172
- };
173
- if (row) logData = {
174
- ...logData,
175
- row: String(row)
176
- };
177
- if (functionCall) logData = {
178
- ...logData,
179
- method: functionCall
180
- };
181
- return logData;
182
- }
183
- };
184
- var Logger = class extends AbstractLogger {
185
- debug(msg) {
186
- const data = this.createLogData("debug", msg, this.prefix);
187
- this.run(data);
188
- }
189
- info(msg) {
190
- const data = this.createLogData("info", msg, this.prefix);
191
- this.run(data);
192
- }
193
- warning(msg) {
194
- const data = this.createLogData("warning", msg, this.prefix);
195
- this.run(data);
196
- }
197
- error(msg) {
198
- const data = this.createLogData("error", msg, this.prefix);
199
- this.run(data);
200
- }
201
- critical(msg) {
202
- const data = this.createLogData("critical", msg, this.prefix);
203
- this.run(data);
204
- }
205
- run(data) {
206
- for (let i = 0; i < this.providers.length; i++) this.providers[i]?.execute(data);
207
- }
208
- };
209
- //#endregion
210
- //#region src/lib/logging/core/types.ts
211
- const LogLevel = {
212
- debug: 10,
213
- info: 20,
214
- warning: 30,
215
- error: 40,
216
- critical: 50
217
- };
218
- //#endregion
219
- //#region src/lib/logging/provider/index.ts
220
- const DEFAULT_MAX_SIZE = 4 * 1024;
221
- const DurationUnit = {
222
- hour: 1e3 * 60 * 60,
223
- day: 1e3 * 60 * 60 * 24,
224
- week: 1e3 * 60 * 60 * 24 * 7,
225
- month: 1e3 * 60 * 60 * 24 * 7 * 4.345
226
- };
227
- var LoggerProvider = class {
228
- options;
229
- constructor(options = PROVIDER_OPTION_DEFAULT) {
230
- this.options = {
231
- ...PROVIDER_OPTION_DEFAULT,
232
- ...options
233
- };
234
- }
235
- getLogLevel() {
236
- if (!this.options.level) return LogLevel.debug;
237
- return LogLevel[this.options.level];
238
- }
239
- };
240
- const FILE_PROVIDER_OPTION_DEFAULT = {
241
- ...PROVIDER_OPTION_DEFAULT,
242
- policy: { type: "size" }
243
- };
244
- var FileProvider = class extends LoggerProvider {
245
- filePath;
246
- counter;
247
- file;
248
- policy;
249
- constructor(file, options = FILE_PROVIDER_OPTION_DEFAULT) {
250
- super({
251
- formatter: options.formatter ?? PROVIDER_OPTION_DEFAULT.formatter,
252
- level: options.level ?? PROVIDER_OPTION_DEFAULT.level
253
- });
254
- this.policy = options.policy ?? FILE_PROVIDER_OPTION_DEFAULT.policy;
255
- this.filePath = file;
256
- this.counter = 0;
257
- this.file = this.createNewFileName();
258
- }
259
- execute(data) {
260
- if (this.getLogLevel() > LogLevel[data.level]) return;
261
- const [error, formattedMessage] = require_lib_errors_index.mightThrowSync(() => {
262
- if (this.isJSONFile()) return JSON.stringify({
263
- timestamp: isoDateTimeFormat(),
264
- level: data.level,
265
- message: data.msg
266
- });
267
- const { formatter } = this.options;
268
- return formatter?.format(data) ?? "";
269
- });
270
- const [fsError] = require_lib_errors_index.mightThrowSync(() => {
271
- if (error && error instanceof Error) {
272
- const { formatter } = this.options;
273
- const errorMsg = formatter?.formatError(data, error);
274
- (0, node_fs.appendFileSync)(this.file, `${errorMsg}\n`);
275
- }
276
- if (this.canRollFile()) {
277
- this.counter += 1;
278
- this.file = this.createNewFileName();
279
- }
280
- if (error || !formattedMessage) return;
281
- (0, node_fs.appendFileSync)(this.file, `${formattedMessage}\n`);
282
- });
283
- if (fsError && fsError instanceof Error) throw fsError;
284
- }
285
- canRollFile() {
286
- if (!this.policy) return false;
287
- switch (this.policy.type) {
288
- case "size":
289
- if (this.policy.maxSize) return this.getFileSize() >= this.policy.maxSize;
290
- return this.getFileSize() >= DEFAULT_MAX_SIZE;
291
- case "time": {
292
- if (!(0, node_fs.existsSync)(this.file)) return false;
293
- const { duration, instant } = this.policy;
294
- const { birthtime } = (0, node_fs.statSync)(this.file);
295
- const creationTimeMs = birthtime.getTime();
296
- const currenTimeMs = Date.now();
297
- const diffMs = currenTimeMs - creationTimeMs;
298
- switch (instant) {
299
- case "hour": return Math.floor(diffMs / DurationUnit.hour) >= duration;
300
- case "day": return Math.floor(diffMs / DurationUnit.day) >= duration;
301
- case "week": return Math.floor(diffMs / DurationUnit.week) >= duration;
302
- case "month": {
303
- const currentDate = new Date(currenTimeMs);
304
- const monthsDiff = (currentDate.getFullYear() - birthtime.getFullYear()) * 12 + (currentDate.getMonth() - birthtime.getMonth());
305
- return (currentDate.getDate() >= birthtime.getDate() ? monthsDiff : monthsDiff - 1) >= duration;
306
- }
307
- }
308
- }
309
- }
310
- }
311
- getFileSize() {
312
- if (!(0, node_fs.existsSync)(this.file)) return 0;
313
- const [statError, size] = require_lib_errors_index.mightThrowSync(() => {
314
- const { size } = (0, node_fs.statSync)(this.file);
315
- return size;
316
- });
317
- if (statError && statError instanceof Error) return 0;
318
- if (!size) return 0;
319
- return size;
320
- }
321
- isJSONFile() {
322
- return (0, node_path.extname)(this.filePath) === ".json";
323
- }
324
- createNewFileName() {
325
- const fileName = (0, node_path.basename)(this.filePath);
326
- const directory = (0, node_path.dirname)(this.filePath);
327
- if (!(0, node_fs.existsSync)(directory)) (0, node_fs.mkdirSync)(directory, { recursive: true });
328
- const fileInfo = fileName.split(".");
329
- const extension = fileInfo.pop();
330
- return (0, node_path.join)(directory, [
331
- ...fileInfo,
332
- this.counter,
333
- extension
334
- ].join("."));
335
- }
336
- };
337
- var ConsoleProvider = class extends LoggerProvider {
338
- execute(data) {
339
- const level = this.getLogLevel();
340
- const userLevel = LogLevel[data.level];
341
- if (level > userLevel) return;
342
- const [error, formattedMessage] = require_lib_errors_index.mightThrowSync(() => {
343
- const { formatter } = this.options;
344
- return formatter?.format(data) ?? "";
345
- });
346
- if (error && error instanceof Error) {
347
- const { formatter } = this.options;
348
- console.error(formatter?.formatError(data, error));
349
- return;
350
- }
351
- if (!formattedMessage) return;
352
- switch (userLevel) {
353
- case LogLevel.debug:
354
- console.debug(formattedMessage);
355
- break;
356
- case LogLevel.info:
357
- console.info(formattedMessage);
358
- break;
359
- case LogLevel.warning:
360
- console.warn(formattedMessage);
361
- break;
362
- case LogLevel.error:
363
- console.error(formattedMessage);
364
- break;
365
- case LogLevel.critical:
366
- console.error(formattedMessage);
367
- break;
368
- default:
369
- console.debug(formattedMessage);
370
- break;
371
- }
372
- }
373
- };
374
- //#endregion
375
- exports.AbstractLogger = AbstractLogger;
376
- exports.BaseFormatter = BaseFormatter;
377
- exports.ConsoleProvider = ConsoleProvider;
378
- exports.FileProvider = FileProvider;
379
- exports.Formatter = Formatter;
380
- exports.JSONFormatter = JSONFormatter;
381
- exports.LogLevel = LogLevel;
382
- exports.Logger = Logger;
383
- exports.LoggerProvider = LoggerProvider;
384
- exports.dmyFormat = dmyFormat;
385
- exports.isoDateFormat = isoDateFormat;
386
- exports.isoDateTimeFormat = isoDateTimeFormat;
387
- exports.mdyFormat = mdyFormat;
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("../errors/index.cjs");let t=require("node:path"),n=require("node:fs");var r=class{dateFmt;constructor(e){this.dateFmt=e}},i=class extends r{constructor(e=o){super(e)}format(e){let{prefix:t,level:n,msg:r,fileName:i,row:a,column:o,method:s}=e,c=this.dateFmt(),l=n.toUpperCase();typeof r==`object`&&(r=JSON.stringify(r));let u=`${t}/${i}:${a}:${o}`,d=`[${l}]`;return d=s?`${d}\t[${s}]\t[${u}]`:`${d}\t[${u}]`,`${c} ${d} : ${r}`}formatError(e,t){let{prefix:n,fileName:r,row:i,column:a,method:o}=e,s=this.dateFmt(),c=`${n}/${r}:${i}:${a}`,l=`Error name="${t.name}" Error message="${t.message}"`,u=`[ERROR]`;return u=o?`${u}\t[${o}]\t[${c}]`:`${u}\t[${c}]`,t.cause&&(l=`${l}\n\tError cause="${t.cause}"`),t.stack&&(l=`${l}\n\tStack trace="${t.stack}"`),`${s} ${u} : ${l}`}},a=class extends r{constructor(e=o){super(e)}format(e){let{prefix:t,level:n,msg:r,fileName:i,row:a,column:o,method:s}=e,c=this.dateFmt(),l=n.toUpperCase(),u={};return s&&(u={method:s}),u={...u,fileName:i,row:a,column:o},JSON.stringify({timestamp:c,level:l,prefix:t,position:u,msg:r})}formatError(e,t){let{prefix:n,fileName:r,row:i,column:a,method:o}=e,s=this.dateFmt(),c={errorName:t.name,errorMessage:t.message},l={};return o&&(l={method:o}),l={...l,fileName:r,row:i,column:a},t.cause&&(c={...c,errorCause:t.cause}),t.stack&&(c={...c,stackTrace:t.stack}),JSON.stringify({timestamp:s,level:`ERROR`,prefix:n,position:l,msg:c})}};function o(){return new Date().toISOString()}function s(){return o().split(`T`)[0]||``}function c(){let e=s();if(!e)return``;let t=e.split(`-`);return`${t[2]}-${t[1]}-${t[0]}`}function l(){let e=s();if(!e)return``;let t=e.split(`-`);return`${t[1]}-${t[2]}-${t[0]}`}const u={formatter:new i,level:`debug`};var d=class{stack=[];constructor(t){let n=Error.prepareStackTrace,[r]=e.mightThrowSync(()=>{Error.prepareStackTrace=(e,t)=>t.map(e=>({fileName:e.getFileName(),column:e.getColumnNumber(),row:e.getLineNumber(),functionCall:e.getFunctionName()})),Error.captureStackTrace(this,t),this.stack});Error.prepareStackTrace=n,r&&(this.stack=[])}retrieveFrame(){if(this.stack.length!==0)return this.stack[1]}},f=class{providers;prefix;constructor(e,t){this.prefix=e,this.providers=t}createLogData(e,n,r){let i=new d(this.createLogData).retrieveFrame(),a={level:e,msg:n,prefix:r};if(!i)return a;let{column:o,fileName:s,functionCall:c,row:l}=i;return s&&(a={...a,fileName:(0,t.basename)(s)}),o&&(a={...a,column:String(o)}),l&&(a={...a,row:String(l)}),c&&(a={...a,method:c}),a}},p=class extends f{debug(e){let t=this.createLogData(`debug`,e,this.prefix);this.run(t)}info(e){let t=this.createLogData(`info`,e,this.prefix);this.run(t)}warning(e){let t=this.createLogData(`warning`,e,this.prefix);this.run(t)}error(e){let t=this.createLogData(`error`,e,this.prefix);this.run(t)}critical(e){let t=this.createLogData(`critical`,e,this.prefix);this.run(t)}run(e){for(let t=0;t<this.providers.length;t++)this.providers[t]?.execute(e)}};const m={debug:10,info:20,warning:30,error:40,critical:50},h={hour:1e3*60*60,day:1e3*60*60*24,week:1e3*60*60*24*7,month:1e3*60*60*24*7*4.345};var g=class{options;constructor(e=u){this.options={...u,...e}}getLogLevel(){return this.options.level?m[this.options.level]:m.debug}};const _={...u,policy:{type:`size`}};var v=class extends g{filePath;counter;file;policy;constructor(e,t=_){super({formatter:t.formatter??u.formatter,level:t.level??u.level}),this.policy=t.policy??_.policy,this.filePath=e,this.counter=0,this.file=this.createNewFileName()}execute(t){if(this.getLogLevel()>m[t.level])return;let[r,i]=e.mightThrowSync(()=>{if(this.isJSONFile())return JSON.stringify({timestamp:o(),level:t.level,message:t.msg});let{formatter:e}=this.options;return e?.format(t)??``}),[a]=e.mightThrowSync(()=>{if(r&&r instanceof Error){let{formatter:e}=this.options,i=e?.formatError(t,r);(0,n.appendFileSync)(this.file,`${i}\n`)}this.canRollFile()&&(this.counter+=1,this.file=this.createNewFileName()),!(r||!i)&&(0,n.appendFileSync)(this.file,`${i}\n`)});if(a&&a instanceof Error)throw a}canRollFile(){if(!this.policy)return!1;switch(this.policy.type){case`size`:return this.policy.maxSize?this.getFileSize()>=this.policy.maxSize:this.getFileSize()>=4096;case`time`:{if(!(0,n.existsSync)(this.file))return!1;let{duration:e,instant:t}=this.policy,{birthtime:r}=(0,n.statSync)(this.file),i=r.getTime(),a=Date.now(),o=a-i;switch(t){case`hour`:return Math.floor(o/h.hour)>=e;case`day`:return Math.floor(o/h.day)>=e;case`week`:return Math.floor(o/h.week)>=e;case`month`:{let t=new Date(a),n=(t.getFullYear()-r.getFullYear())*12+(t.getMonth()-r.getMonth());return(t.getDate()>=r.getDate()?n:n-1)>=e}}}}}getFileSize(){if(!(0,n.existsSync)(this.file))return 0;let[t,r]=e.mightThrowSync(()=>{let{size:e}=(0,n.statSync)(this.file);return e});return t&&t instanceof Error||!r?0:r}isJSONFile(){return(0,t.extname)(this.filePath)===`.json`}createNewFileName(){let e=(0,t.basename)(this.filePath),r=(0,t.dirname)(this.filePath);(0,n.existsSync)(r)||(0,n.mkdirSync)(r,{recursive:!0});let i=e.split(`.`),a=i.pop();return(0,t.join)(r,[...i,this.counter,a].join(`.`))}},y=class extends g{execute(t){let n=this.getLogLevel(),r=m[t.level];if(n>r)return;let[i,a]=e.mightThrowSync(()=>{let{formatter:e}=this.options;return e?.format(t)??``});if(i&&i instanceof Error){let{formatter:e}=this.options;console.error(e?.formatError(t,i));return}if(a)switch(r){case m.debug:console.debug(a);break;case m.info:console.info(a);break;case m.warning:console.warn(a);break;case m.error:console.error(a);break;case m.critical:console.error(a);break;default:console.debug(a);break}}};exports.AbstractLogger=f,exports.BaseFormatter=i,exports.ConsoleProvider=y,exports.FileProvider=v,exports.Formatter=r,exports.JSONFormatter=a,exports.LogLevel=m,exports.Logger=p,exports.LoggerProvider=g,exports.dmyFormat=c,exports.isoDateFormat=s,exports.isoDateTimeFormat=o,exports.mdyFormat=l;