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