@rainbow-o23/n1 0.1.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 (40) hide show
  1. package/.babelrc +11 -0
  2. package/.eslintrc +23 -0
  3. package/README.md +233 -0
  4. package/index.cjs +760 -0
  5. package/index.d.ts +3 -0
  6. package/index.js +732 -0
  7. package/lib/pipeline/envs.d.ts +1 -0
  8. package/lib/pipeline/index.d.ts +6 -0
  9. package/lib/pipeline/pipeline-execution.d.ts +38 -0
  10. package/lib/pipeline/pipeline-step.d.ts +41 -0
  11. package/lib/pipeline/pipeline.d.ts +47 -0
  12. package/lib/pipeline/step-helpers-utils.d.ts +41 -0
  13. package/lib/pipeline/step-helpers.d.ts +31 -0
  14. package/lib/repo/index.d.ts +1 -0
  15. package/lib/repo/pipeline-repository.d.ts +11 -0
  16. package/lib/utils/config.d.ts +16 -0
  17. package/lib/utils/error.d.ts +21 -0
  18. package/lib/utils/index.d.ts +4 -0
  19. package/lib/utils/logger.d.ts +50 -0
  20. package/lib/utils/types.d.ts +7 -0
  21. package/package.json +41 -0
  22. package/rollup.config.base.js +30 -0
  23. package/rollup.config.ci.js +3 -0
  24. package/rollup.config.js +3 -0
  25. package/src/index.ts +4 -0
  26. package/src/lib/pipeline/envs.ts +20 -0
  27. package/src/lib/pipeline/index.ts +7 -0
  28. package/src/lib/pipeline/pipeline-execution.ts +137 -0
  29. package/src/lib/pipeline/pipeline-step.ts +79 -0
  30. package/src/lib/pipeline/pipeline.ts +148 -0
  31. package/src/lib/pipeline/step-helpers-utils.ts +143 -0
  32. package/src/lib/pipeline/step-helpers.ts +77 -0
  33. package/src/lib/repo/index.ts +1 -0
  34. package/src/lib/repo/pipeline-repository.ts +53 -0
  35. package/src/lib/utils/config.ts +110 -0
  36. package/src/lib/utils/error.ts +41 -0
  37. package/src/lib/utils/index.ts +6 -0
  38. package/src/lib/utils/logger.ts +292 -0
  39. package/src/lib/utils/types.ts +11 -0
  40. package/tsconfig.json +36 -0
package/index.js ADDED
@@ -0,0 +1,732 @@
1
+ import dayjs from 'dayjs';
2
+ import ArraySupport from 'dayjs/plugin/arraySupport.js';
3
+ import CustomParseFormat from 'dayjs/plugin/customParseFormat.js';
4
+ import Duration from 'dayjs/plugin/duration.js';
5
+ import IsToday from 'dayjs/plugin/isToday.js';
6
+ import ObjectSupport from 'dayjs/plugin/objectSupport.js';
7
+ import QuarterOfYear from 'dayjs/plugin/quarterOfYear.js';
8
+ import RelativeTime from 'dayjs/plugin/relativeTime.js';
9
+ import UTC from 'dayjs/plugin/utc.js';
10
+ import WeekOfYear from 'dayjs/plugin/weekOfYear.js';
11
+ import { customAlphabet, nanoid } from 'nanoid';
12
+
13
+ const ERR_PIPELINE_NOT_FOUND = 'O01-00001';
14
+ const ERR_TRIM_NON_STRING = 'O01-00002';
15
+ const ERR_UNKNOWN = 'O01-99999';
16
+ class CatchableError extends Error {
17
+ _code;
18
+ constructor(_code, message) {
19
+ super(message);
20
+ this._code = _code;
21
+ }
22
+ getCode() {
23
+ return this._code;
24
+ }
25
+ }
26
+ class UncatchableError extends Error {
27
+ _code;
28
+ constructor(_code, message) {
29
+ super(message);
30
+ this._code = _code;
31
+ }
32
+ getCode() {
33
+ return this._code;
34
+ }
35
+ }
36
+ class ExposedUncatchableError extends UncatchableError {
37
+ _status;
38
+ constructor(status, code, message) {
39
+ super(code, message);
40
+ this._status = status;
41
+ }
42
+ getStatus() {
43
+ return this._status;
44
+ }
45
+ }
46
+
47
+ const buildOutput = (message, ...optionalParams) => {
48
+ if (optionalParams == null || optionalParams.length === 0) {
49
+ return { message, optionalParams };
50
+ }
51
+ const key = optionalParams.pop();
52
+ if (typeof key === 'string') {
53
+ return { key, message, optionalParams };
54
+ }
55
+ else {
56
+ return { message, optionalParams: [...optionalParams, key] };
57
+ }
58
+ };
59
+ class EnhancedLogger {
60
+ static ENABLED_LEVELS = ['warn', 'error'];
61
+ static ENABLEMENTS = {};
62
+ _logger;
63
+ constructor(logger) {
64
+ this._logger = logger;
65
+ }
66
+ getInternalLogger() {
67
+ return this._logger;
68
+ }
69
+ static enableLevel(level) {
70
+ if (EnhancedLogger.ENABLED_LEVELS.includes(level)) ;
71
+ else {
72
+ const levels = ['debug', 'verbose', 'log', 'warn', 'error'];
73
+ const foundIndex = levels.indexOf(level);
74
+ EnhancedLogger.ENABLED_LEVELS = levels.filter((_level, index) => {
75
+ return index >= foundIndex;
76
+ });
77
+ }
78
+ }
79
+ static disableLevel(level) {
80
+ if (!EnhancedLogger.ENABLED_LEVELS.includes(level)) ;
81
+ else {
82
+ const levels = ['debug', 'verbose', 'log', 'warn', 'error'];
83
+ const foundIndex = levels.indexOf(level);
84
+ EnhancedLogger.ENABLED_LEVELS = levels.filter((_level, index) => {
85
+ return index > foundIndex;
86
+ });
87
+ }
88
+ }
89
+ static isLevelEnabled(level) {
90
+ return EnhancedLogger.ENABLED_LEVELS.includes(level);
91
+ }
92
+ static enable(name) {
93
+ if (['debug', 'verbose', 'log', 'warn', 'error'].some(level => name.endsWith(`.${level}`))) {
94
+ EnhancedLogger.ENABLEMENTS[name] = true;
95
+ }
96
+ else {
97
+ ['debug', 'verbose', 'log', 'warn', 'error'].forEach(level => {
98
+ EnhancedLogger.ENABLEMENTS[`${name}.${level}`] = true;
99
+ });
100
+ }
101
+ }
102
+ static disable(name) {
103
+ if (['debug', 'verbose', 'log', 'warn', 'error'].some(level => name.endsWith(`.${level}`))) {
104
+ EnhancedLogger.ENABLEMENTS[name] = false;
105
+ }
106
+ else {
107
+ ['debug', 'verbose', 'log', 'warn', 'error'].forEach(level => {
108
+ EnhancedLogger.ENABLEMENTS[`${name}.${level}`] = false;
109
+ });
110
+ }
111
+ }
112
+ static isEnabled(name) {
113
+ if (EnhancedLogger.ENABLEMENTS[name] === false) {
114
+ return false;
115
+ }
116
+ else if (EnhancedLogger.ENABLEMENTS[name] === true) {
117
+ return true;
118
+ }
119
+ else {
120
+ if (this.ENABLED_LEVELS.some(level => name.endsWith(`.${level}`))) {
121
+ return true;
122
+ }
123
+ else {
124
+ return false;
125
+ }
126
+ }
127
+ }
128
+ takeover(to) {
129
+ this._logger = to ?? console;
130
+ }
131
+ restore() {
132
+ this._logger = console;
133
+ }
134
+ debug(message, ...optionalParams) {
135
+ const { key } = buildOutput(message, ...optionalParams);
136
+ if (key != null && EnhancedLogger.isEnabled(`${key}.debug`)) {
137
+ return this._logger.debug(message, ...optionalParams);
138
+ }
139
+ else if (EnhancedLogger.isLevelEnabled('debug')) {
140
+ return this._logger.debug(message, ...optionalParams);
141
+ }
142
+ }
143
+ verbose(message, ...optionalParams) {
144
+ const { key } = buildOutput(message, ...optionalParams);
145
+ if (key != null && EnhancedLogger.isEnabled(`${key}.verbose`)) {
146
+ return this._logger.verbose(message, ...optionalParams);
147
+ }
148
+ else if (EnhancedLogger.isLevelEnabled('verbose')) {
149
+ return this._logger.verbose(message, ...optionalParams);
150
+ }
151
+ }
152
+ log(message, ...optionalParams) {
153
+ const { key } = buildOutput(message, ...optionalParams);
154
+ if (key != null && EnhancedLogger.isEnabled(`${key}.log`)) {
155
+ return this._logger.log(message, ...optionalParams);
156
+ }
157
+ else if (EnhancedLogger.isLevelEnabled('log')) {
158
+ return this._logger.log(message, ...optionalParams);
159
+ }
160
+ }
161
+ warn(message, ...optionalParams) {
162
+ const { key } = buildOutput(message, ...optionalParams);
163
+ if (key != null && EnhancedLogger.isEnabled(`${key}.warn`)) {
164
+ return this._logger.warn(message, ...optionalParams);
165
+ }
166
+ else if (EnhancedLogger.isLevelEnabled('warn')) {
167
+ return this._logger.warn(message, ...optionalParams);
168
+ }
169
+ }
170
+ error(message, ...optionalParams) {
171
+ const { key } = buildOutput(message, ...optionalParams);
172
+ if (key != null && EnhancedLogger.isEnabled(`${key}.error`)) {
173
+ return this._logger.error(message, ...optionalParams);
174
+ }
175
+ else if (EnhancedLogger.isLevelEnabled('error')) {
176
+ return this._logger.error(message, ...optionalParams);
177
+ }
178
+ }
179
+ }
180
+ class ConsoleLogger {
181
+ buildPrefix(level, message, ...optionalParams) {
182
+ const date = new Date();
183
+ const { key, message: msg, optionalParams: params } = buildOutput(message, ...optionalParams);
184
+ return [
185
+ `%c[${date.toLocaleDateString()} ${date.toLocaleTimeString()}] %c[${key ?? 'UNKNOWN CATEGORY'}] %c[${level.toUpperCase()}]`,
186
+ 'color: #871094', 'color: #0033B3', 'color: #9E880D',
187
+ msg, ...params
188
+ ];
189
+ }
190
+ debug(message, ...optionalParams) {
191
+ return console.debug(...this.buildPrefix('debug', message, ...optionalParams));
192
+ }
193
+ verbose(message, ...optionalParams) {
194
+ return console.trace(...this.buildPrefix('verbose', message, ...optionalParams));
195
+ }
196
+ log(message, ...optionalParams) {
197
+ return console.log(...this.buildPrefix('log', message, ...optionalParams));
198
+ }
199
+ warn(message, ...optionalParams) {
200
+ return console.warn(...this.buildPrefix('warn', message, ...optionalParams));
201
+ }
202
+ error(message, ...optionalParams) {
203
+ return console.error(...this.buildPrefix('error', message, ...optionalParams));
204
+ }
205
+ }
206
+ const createLogger = (logger) => {
207
+ return new EnhancedLogger(logger ?? new ConsoleLogger());
208
+ };
209
+ class LoggerPerformanceSaver {
210
+ message;
211
+ constructor(message) {
212
+ this.message = message;
213
+ }
214
+ get [Symbol.toStringTag]() {
215
+ return this.message();
216
+ }
217
+ }
218
+ const saveLoggerPerformance = (message) => {
219
+ return new LoggerPerformanceSaver(message);
220
+ };
221
+ class LoggerUtils {
222
+ constructor() {
223
+ }
224
+ static stringifyObject(given) {
225
+ if (given instanceof Buffer) {
226
+ return '...Buffer (content ignored)';
227
+ }
228
+ else if (typeof given === 'object' && given.type === 'Buffer' && given.data != null && Array.isArray(given.data)) {
229
+ return '...Buffer (content ignored)';
230
+ }
231
+ return JSON.stringify(given, (_key, value) => {
232
+ if (value == null) {
233
+ return (void 0);
234
+ }
235
+ else if (value instanceof Buffer) {
236
+ return '...Buffer (content ignored)';
237
+ }
238
+ else if (typeof value === 'object' && value.type === 'Buffer' && value.data != null && Array.isArray(value.data)) {
239
+ return '...Buffer (content ignored)';
240
+ }
241
+ else {
242
+ return value;
243
+ }
244
+ });
245
+ }
246
+ static normalizeObject(given) {
247
+ if (given instanceof Buffer) {
248
+ return '...Buffer (content ignored)';
249
+ }
250
+ else if (typeof given === 'object' && given.type === 'Buffer' && given.data != null && Array.isArray(given.data)) {
251
+ return '...Buffer (content ignored)';
252
+ }
253
+ return Object.keys(given).reduce((normalized, key) => {
254
+ const value = given[key];
255
+ if (value == null) {
256
+ return (void 0);
257
+ }
258
+ else if (Array.isArray(value)) {
259
+ return value.map(LoggerUtils.normalizeObject);
260
+ }
261
+ else if (value instanceof Buffer) {
262
+ return '...Buffer (content ignored)';
263
+ }
264
+ else if (typeof value === 'object' && value.type === 'Buffer' && value.data != null && Array.isArray(value.data)) {
265
+ return '...Buffer (content ignored)';
266
+ }
267
+ else {
268
+ return value;
269
+ }
270
+ }, {});
271
+ }
272
+ }
273
+
274
+ const MISSED = Symbol();
275
+ class Config {
276
+ _logger;
277
+ valueCache = {};
278
+ constructor(_logger) {
279
+ this._logger = _logger;
280
+ }
281
+ getLogger() {
282
+ return this._logger;
283
+ }
284
+ setLogger(value) {
285
+ this._logger = value;
286
+ }
287
+ generateKey(name) {
288
+ const key = name
289
+ .split('.')
290
+ .filter(s => s.trim().length !== 0)
291
+ .map(s => s.toUpperCase())
292
+ .join('_');
293
+ return `CFG_${key}`;
294
+ }
295
+ getFromCache(name, read, defaultValue) {
296
+ const cached = this.valueCache[name];
297
+ if (cached === MISSED) {
298
+ return defaultValue ?? (void 0);
299
+ }
300
+ else if (cached != null) {
301
+ return cached;
302
+ }
303
+ else {
304
+ const value = read();
305
+ this.valueCache[name] = value ?? MISSED;
306
+ return value ?? defaultValue ?? (void 0);
307
+ }
308
+ }
309
+ getString(name, defaultValue) {
310
+ return this.getFromCache(name, () => {
311
+ const value = process.env[this.generateKey(name)];
312
+ if (value == null || value.trim().length === 0) {
313
+ return (void 0);
314
+ }
315
+ else {
316
+ return value.trim();
317
+ }
318
+ }, defaultValue);
319
+ }
320
+ getBoolean(name, defaultValue) {
321
+ return this.getFromCache(name, () => {
322
+ const value = process.env[this.generateKey(name)];
323
+ if (value == null || value.trim().length === 0) {
324
+ return (void 0);
325
+ }
326
+ else if (['TRUE', 'YES', 'ON', '1'].includes(value.trim().toUpperCase())) {
327
+ return true;
328
+ }
329
+ else if (['FALSE', 'NO', 'OFF', '0'].includes(value.trim().toUpperCase())) {
330
+ return false;
331
+ }
332
+ else {
333
+ this._logger.warn(`Cannot parse given configuration item value[${value}] to boolean, ignored.`, Config.name);
334
+ return (void 0);
335
+ }
336
+ }, defaultValue);
337
+ }
338
+ getNumber(name, defaultValue) {
339
+ return this.getFromCache(name, () => {
340
+ const value = process.env[this.generateKey(name)];
341
+ if (value == null || value.trim().length === 0) {
342
+ return (void 0);
343
+ }
344
+ const v = Number(value);
345
+ if (isNaN(v)) {
346
+ this._logger.error(`Cannot parse given configuration item value[${value}] to number, ignored.`, Config.name);
347
+ return (void 0);
348
+ }
349
+ else {
350
+ return v;
351
+ }
352
+ }, defaultValue);
353
+ }
354
+ getJson(name, defaultValue) {
355
+ return this.getFromCache(name, () => {
356
+ const value = process.env[this.generateKey(name)];
357
+ if (value == null || value.trim().length === 0) {
358
+ return (void 0);
359
+ }
360
+ else {
361
+ try {
362
+ return JSON.parse(value);
363
+ }
364
+ catch {
365
+ this._logger.warn(`Cannot parse given configuration item value[${value}] to boolean, ignored.`, Config.name);
366
+ return (void 0);
367
+ }
368
+ }
369
+ }, defaultValue);
370
+ }
371
+ }
372
+ const createConfig = (logger) => {
373
+ return new Config(logger ?? createLogger());
374
+ };
375
+
376
+ dayjs.extend(WeekOfYear);
377
+ dayjs.extend(QuarterOfYear);
378
+ dayjs.extend(Duration);
379
+ dayjs.extend(IsToday);
380
+ dayjs.extend(RelativeTime);
381
+ dayjs.extend(ArraySupport);
382
+ dayjs.extend(ObjectSupport);
383
+ dayjs.extend(CustomParseFormat);
384
+ dayjs.extend(UTC);
385
+
386
+ const PIPELINE_STEP_FILE_SYMBOL = Symbol();
387
+ const PIPELINE_STEP_RETURN_NULL = Symbol();
388
+ class StepHelpersUtils {
389
+ static asciiNanoId = customAlphabet('1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_', 32);
390
+ static OBJECT_PROTOTYPE = Object.prototype;
391
+ constructor() {
392
+ }
393
+ static $nano(size) {
394
+ return nanoid(size);
395
+ }
396
+ static $ascii(size) {
397
+ return StepHelpersUtils.asciiNanoId(size);
398
+ }
399
+ static createCatchableError = (options) => {
400
+ throw new CatchableError(options.code, options.reason);
401
+ };
402
+ static createExposedUncatchableError = (options) => {
403
+ throw new ExposedUncatchableError(options.status, options.code, options.reason);
404
+ };
405
+ static createUncatchableError = (options) => {
406
+ throw new UncatchableError(options.code, options.reason);
407
+ };
408
+ static $errors = {
409
+ catchable: StepHelpersUtils.createCatchableError,
410
+ exposed: StepHelpersUtils.createExposedUncatchableError,
411
+ uncatchable: StepHelpersUtils.createUncatchableError
412
+ };
413
+ static createFile(options) {
414
+ return {
415
+ $file: PIPELINE_STEP_FILE_SYMBOL,
416
+ name: options.name, type: options.type,
417
+ content: typeof options.content === 'string' ? Buffer.from(options.content) : options.content
418
+ };
419
+ }
420
+ static $clearContextData() {
421
+ return PIPELINE_STEP_RETURN_NULL;
422
+ }
423
+ static isPrototype(value) {
424
+ const Ctor = value && value.constructor;
425
+ const proto = (typeof Ctor === 'function' && Ctor.prototype) || StepHelpersUtils.OBJECT_PROTOTYPE;
426
+ return value === proto;
427
+ }
428
+ static isLength(value) {
429
+ return typeof value === 'number' && value > -1 && value % 1 === 0 && value <= Number.MAX_SAFE_INTEGER;
430
+ }
431
+ static isArrayLike(value) {
432
+ return value != null && typeof value !== 'function' && StepHelpersUtils.isLength(value.length);
433
+ }
434
+ static isEmpty(value) {
435
+ if (value == null) {
436
+ return true;
437
+ }
438
+ if (StepHelpersUtils.isArrayLike(value) && (Array.isArray(value) || typeof value === 'string')) {
439
+ return value.length === 0;
440
+ }
441
+ else if (value instanceof Map) {
442
+ return value.size === 0;
443
+ }
444
+ else if (value instanceof Set) {
445
+ return value.size === 0;
446
+ }
447
+ else if (StepHelpersUtils.isPrototype(value)) {
448
+ return Object.keys(value).length === 0;
449
+ }
450
+ return false;
451
+ }
452
+ static isNotEmpty(value) {
453
+ return !StepHelpersUtils.isEmpty(value);
454
+ }
455
+ static isBlank(value) {
456
+ if (value == null) {
457
+ return true;
458
+ }
459
+ if (typeof value !== 'string') {
460
+ return false;
461
+ }
462
+ return value.trim().length === 0;
463
+ }
464
+ static isNotBlank(value) {
465
+ return !StepHelpersUtils.isBlank(value);
466
+ }
467
+ static trim(value) {
468
+ if (value == null) {
469
+ return '';
470
+ }
471
+ if (typeof value === 'string') {
472
+ return value.trim();
473
+ }
474
+ throw new UncatchableError(ERR_TRIM_NON_STRING, `Cannot apply trim to non-string object[type=${typeof value}, value=${value}].`);
475
+ }
476
+ }
477
+
478
+ class PerformanceExecution {
479
+ options;
480
+ constructor(options) {
481
+ this.options = options;
482
+ }
483
+ async execute(measured) {
484
+ try {
485
+ return await measured();
486
+ }
487
+ finally {
488
+ this.options.print(() => {
489
+ return {
490
+ exec: this.options.exec, ptraceId: this.options.traceId,
491
+ spent: Number((process.hrtime.bigint() - this.options.start) / 1000000n)
492
+ };
493
+ });
494
+ }
495
+ }
496
+ }
497
+ class AbstractPipelineExecution {
498
+ _config;
499
+ _logger;
500
+ _performanceLogEnabled;
501
+ _debugLogEnabled;
502
+ constructor(options) {
503
+ const { config, logger } = options ?? {};
504
+ this._logger = logger ?? createLogger();
505
+ this._config = config ?? createConfig(this._logger);
506
+ this._debugLogEnabled = this._config.getBoolean('pipeline.debug.log.enabled', false);
507
+ this._performanceLogEnabled = this._debugLogEnabled || this._config.getBoolean('pipeline.performance.log.enabled', false);
508
+ }
509
+ getConfig() {
510
+ return this._config;
511
+ }
512
+ getLogger() {
513
+ return this._logger;
514
+ }
515
+ isDebugLogEnabled() {
516
+ return this._debugLogEnabled;
517
+ }
518
+ debug(message, ...optionsParams) {
519
+ if (this.isDebugLogEnabled()) {
520
+ message = typeof message === 'function' ? message() : message;
521
+ message = Array.isArray(message) ? message : [message];
522
+ const [first, ...rest] = [...message, ...optionsParams.map(m => typeof m === 'function' ? m() : m)];
523
+ this.getLogger().debug(first ?? '', ...rest, this.constructor.name);
524
+ }
525
+ }
526
+ traceRequest(traceId, data) {
527
+ this.debug(() => ({ exec: 'PIPELINE', ptraceId: traceId, request: LoggerUtils.normalizeObject(data) }));
528
+ return data;
529
+ }
530
+ traceResponse(traceId, data) {
531
+ this.debug(() => ({ exec: 'PIPELINE', ptraceId: traceId, response: LoggerUtils.normalizeObject(data) }));
532
+ return data;
533
+ }
534
+ traceStepIn(traceId, step, data) {
535
+ this.debug(() => ({
536
+ exec: 'STEP', name: step.getName(), ptraceId: traceId, in: LoggerUtils.normalizeObject(data)
537
+ }));
538
+ }
539
+ traceStepOut(traceId, step, data) {
540
+ this.debug(() => ({
541
+ exec: 'STEP', name: step.getName(), ptraceId: traceId, out: LoggerUtils.normalizeObject(data)
542
+ }));
543
+ }
544
+ isPerformanceLogEnabled() {
545
+ return this._performanceLogEnabled;
546
+ }
547
+ debugPerformance(message) {
548
+ if (this.isPerformanceLogEnabled()) {
549
+ this.getLogger().debug(message(), this.constructor.name);
550
+ }
551
+ }
552
+ measurePerformance(traceId, exec) {
553
+ return new PerformanceExecution({
554
+ exec, traceId, start: process.hrtime.bigint(), print: (message) => this.debugPerformance(message)
555
+ });
556
+ }
557
+ returnOrContinueOrClear(request, response) {
558
+ if (response.content === PIPELINE_STEP_RETURN_NULL) {
559
+ return response;
560
+ }
561
+ else if (response.content == null) {
562
+ return request;
563
+ }
564
+ else {
565
+ return response;
566
+ }
567
+ }
568
+ }
569
+
570
+ class PipelineStepDateHelper {
571
+ _dateTimeFormat;
572
+ constructor(config) {
573
+ this._dateTimeFormat = config.getString('format.datetime', 'YYYY-MM-DD HH:mm:ss');
574
+ }
575
+ getDateTimeFormat() {
576
+ return this._dateTimeFormat;
577
+ }
578
+ now() {
579
+ return dayjs().format(this.getDateTimeFormat());
580
+ }
581
+ get dayjs() {
582
+ return dayjs;
583
+ }
584
+ }
585
+ const createStepHelpers = (config, logger) => {
586
+ return {
587
+ $config: config, $logger: logger,
588
+ $date: new PipelineStepDateHelper(config),
589
+ $nano: StepHelpersUtils.$nano, $ascii: StepHelpersUtils.$ascii,
590
+ $error: StepHelpersUtils.createExposedUncatchableError, $errors: StepHelpersUtils.$errors,
591
+ $file: StepHelpersUtils.createFile,
592
+ $clearContextData: StepHelpersUtils.$clearContextData,
593
+ isEmpty: StepHelpersUtils.isEmpty, isNotEmpty: StepHelpersUtils.isNotEmpty,
594
+ isBlank: StepHelpersUtils.isBlank, isNotBlank: StepHelpersUtils.isNotBlank,
595
+ trim: StepHelpersUtils.trim
596
+ };
597
+ };
598
+
599
+ class AbstractPipelineStep extends AbstractPipelineExecution {
600
+ _$helpers;
601
+ _name;
602
+ constructor(options) {
603
+ super(options);
604
+ this._name = options?.name;
605
+ }
606
+ getName() {
607
+ return (this._name == null || this._name.trim().length === 0) ? this.constructor.name : this._name;
608
+ }
609
+ getHelpers() {
610
+ if (this._$helpers == null) {
611
+ this._$helpers = createStepHelpers(this.getConfig(), this.getLogger());
612
+ }
613
+ return this._$helpers;
614
+ }
615
+ getHelpersVariableNames() {
616
+ return ['$helpers', '$'];
617
+ }
618
+ }
619
+ class DefaultPipelineStepBuilder {
620
+ step;
621
+ constructor(step) {
622
+ this.step = step;
623
+ }
624
+ async create(options) {
625
+ return new this.step(options);
626
+ }
627
+ }
628
+
629
+ class AbstractPipeline extends AbstractPipelineExecution {
630
+ constructor(options) {
631
+ super(options);
632
+ }
633
+ buildStepOptions() {
634
+ return { config: this._config, logger: this._logger };
635
+ }
636
+ async createSteps() {
637
+ const options = this.buildStepOptions();
638
+ return await Promise.all(this.getStepBuilders().map(async (builder) => await builder.create(options)));
639
+ }
640
+ convertRequestToPipelineData(request) {
641
+ return { content: request.payload };
642
+ }
643
+ convertPipelineDataToResponse(result) {
644
+ return { payload: result.content };
645
+ }
646
+ createTraceId(request) {
647
+ const { traceId } = request;
648
+ if (traceId == null || traceId.trim().length === 0) {
649
+ return nanoid(16);
650
+ }
651
+ else {
652
+ return traceId;
653
+ }
654
+ }
655
+ async perform(request) {
656
+ const traceId = this.createTraceId(request);
657
+ const response = await this.measurePerformance(traceId, 'PIPELINE')
658
+ .execute(async () => {
659
+ this.traceRequest(traceId, request);
660
+ const steps = await this.createSteps();
661
+ const data = await steps.reduce(async (promise, step) => {
662
+ const request = await promise;
663
+ return await this.measurePerformance(traceId, 'STEP')
664
+ .execute(async () => {
665
+ this.traceStepIn(traceId, step, request);
666
+ const response = await step.perform({
667
+ ...request, $context: { ...request.$context, traceId }
668
+ });
669
+ this.traceStepOut(traceId, step, response);
670
+ return this.returnOrContinueOrClear(request, response);
671
+ });
672
+ }, Promise.resolve(this.convertRequestToPipelineData(request)));
673
+ return this.traceResponse(traceId, this.convertPipelineDataToResponse(data));
674
+ });
675
+ return response;
676
+ }
677
+ }
678
+ class AbstractStaticPipeline extends AbstractPipeline {
679
+ getStepBuilders() {
680
+ return this.getStepTypes().map(type => new DefaultPipelineStepBuilder(type));
681
+ }
682
+ }
683
+ class DefaultPipelineBuilder {
684
+ pipeline;
685
+ constructor(pipeline) {
686
+ this.pipeline = pipeline;
687
+ }
688
+ async create(options) {
689
+ return new this.pipeline(options);
690
+ }
691
+ }
692
+
693
+ class PipelineRepository {
694
+ static PIPELINE_BUILDERS = {};
695
+ static STEP_BUILDERS = {};
696
+ constructor() {
697
+ }
698
+ static async findPipeline(code, options) {
699
+ const builder = this.PIPELINE_BUILDERS[code];
700
+ if (builder == null) {
701
+ return (void 0);
702
+ }
703
+ else {
704
+ return await builder.create(options);
705
+ }
706
+ }
707
+ static putPipeline(builders) {
708
+ return Object.keys(builders).reduce((duplicated, code) => {
709
+ const existing = PipelineRepository.PIPELINE_BUILDERS[code];
710
+ PipelineRepository.PIPELINE_BUILDERS[code] = builders[code];
711
+ if (existing != null) {
712
+ duplicated[code] = existing;
713
+ }
714
+ return duplicated;
715
+ }, {});
716
+ }
717
+ static async findStep(code) {
718
+ return PipelineRepository.STEP_BUILDERS[code];
719
+ }
720
+ static putStep(builders) {
721
+ return Object.keys(builders).reduce((duplicated, code) => {
722
+ const existing = PipelineRepository.STEP_BUILDERS[code];
723
+ PipelineRepository.STEP_BUILDERS[code] = builders[code];
724
+ if (existing != null) {
725
+ duplicated[code] = existing;
726
+ }
727
+ return duplicated;
728
+ }, {});
729
+ }
730
+ }
731
+
732
+ export { AbstractPipeline, AbstractPipelineExecution, AbstractPipelineStep, AbstractStaticPipeline, CatchableError, Config, ConsoleLogger, DefaultPipelineBuilder, DefaultPipelineStepBuilder, ERR_PIPELINE_NOT_FOUND, ERR_TRIM_NON_STRING, ERR_UNKNOWN, EnhancedLogger, ExposedUncatchableError, LoggerPerformanceSaver, LoggerUtils, PIPELINE_STEP_FILE_SYMBOL, PIPELINE_STEP_RETURN_NULL, PerformanceExecution, PipelineRepository, PipelineStepDateHelper, StepHelpersUtils, UncatchableError, createConfig, createLogger, createStepHelpers, saveLoggerPerformance };