semola 0.6.0 → 0.6.2

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