fluxion-ts 0.3.1 → 0.3.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.
package/dist/index.cjs CHANGED
@@ -7,483 +7,6 @@ var os = require('node:os');
7
7
  var http = require('node:http');
8
8
  var https = require('node:https');
9
9
 
10
- const ANSI_BACKGROUND_OFFSET = 10;
11
-
12
- const wrapAnsi16 = (offset = 0) => code => `\u001B[${code + offset}m`;
13
-
14
- const wrapAnsi256 = (offset = 0) => code => `\u001B[${38 + offset};5;${code}m`;
15
-
16
- const wrapAnsi16m = (offset = 0) => (red, green, blue) => `\u001B[${38 + offset};2;${red};${green};${blue}m`;
17
-
18
- const styles$1 = {
19
- modifier: {
20
- reset: [0, 0],
21
- // 21 isn't widely supported and 22 does the same thing
22
- bold: [1, 22],
23
- dim: [2, 22],
24
- italic: [3, 23],
25
- underline: [4, 24],
26
- overline: [53, 55],
27
- inverse: [7, 27],
28
- hidden: [8, 28],
29
- strikethrough: [9, 29],
30
- },
31
- color: {
32
- black: [30, 39],
33
- red: [31, 39],
34
- green: [32, 39],
35
- yellow: [33, 39],
36
- blue: [34, 39],
37
- magenta: [35, 39],
38
- cyan: [36, 39],
39
- white: [37, 39],
40
-
41
- // Bright color
42
- blackBright: [90, 39],
43
- gray: [90, 39], // Alias of `blackBright`
44
- grey: [90, 39], // Alias of `blackBright`
45
- redBright: [91, 39],
46
- greenBright: [92, 39],
47
- yellowBright: [93, 39],
48
- blueBright: [94, 39],
49
- magentaBright: [95, 39],
50
- cyanBright: [96, 39],
51
- whiteBright: [97, 39],
52
- },
53
- bgColor: {
54
- bgBlack: [40, 49],
55
- bgRed: [41, 49],
56
- bgGreen: [42, 49],
57
- bgYellow: [43, 49],
58
- bgBlue: [44, 49],
59
- bgMagenta: [45, 49],
60
- bgCyan: [46, 49],
61
- bgWhite: [47, 49],
62
-
63
- // Bright color
64
- bgBlackBright: [100, 49],
65
- bgGray: [100, 49], // Alias of `bgBlackBright`
66
- bgGrey: [100, 49], // Alias of `bgBlackBright`
67
- bgRedBright: [101, 49],
68
- bgGreenBright: [102, 49],
69
- bgYellowBright: [103, 49],
70
- bgBlueBright: [104, 49],
71
- bgMagentaBright: [105, 49],
72
- bgCyanBright: [106, 49],
73
- bgWhiteBright: [107, 49],
74
- },
75
- };
76
-
77
- Object.keys(styles$1.modifier);
78
- const foregroundColorNames = Object.keys(styles$1.color);
79
- const backgroundColorNames = Object.keys(styles$1.bgColor);
80
- [...foregroundColorNames, ...backgroundColorNames];
81
-
82
- function assembleStyles() {
83
- const codes = new Map();
84
-
85
- for (const [groupName, group] of Object.entries(styles$1)) {
86
- for (const [styleName, style] of Object.entries(group)) {
87
- styles$1[styleName] = {
88
- open: `\u001B[${style[0]}m`,
89
- close: `\u001B[${style[1]}m`,
90
- };
91
-
92
- group[styleName] = styles$1[styleName];
93
-
94
- codes.set(style[0], style[1]);
95
- }
96
-
97
- Object.defineProperty(styles$1, groupName, {
98
- value: group,
99
- enumerable: false,
100
- });
101
- }
102
-
103
- Object.defineProperty(styles$1, 'codes', {
104
- value: codes,
105
- enumerable: false,
106
- });
107
-
108
- styles$1.color.close = '\u001B[39m';
109
- styles$1.bgColor.close = '\u001B[49m';
110
-
111
- styles$1.color.ansi = wrapAnsi16();
112
- styles$1.color.ansi256 = wrapAnsi256();
113
- styles$1.color.ansi16m = wrapAnsi16m();
114
- styles$1.bgColor.ansi = wrapAnsi16(ANSI_BACKGROUND_OFFSET);
115
- styles$1.bgColor.ansi256 = wrapAnsi256(ANSI_BACKGROUND_OFFSET);
116
- styles$1.bgColor.ansi16m = wrapAnsi16m(ANSI_BACKGROUND_OFFSET);
117
-
118
- // From https://github.com/Qix-/color-convert/blob/3f0e0d4e92e235796ccb17f6e85c72094a651f49/conversions.js
119
- Object.defineProperties(styles$1, {
120
- rgbToAnsi256: {
121
- value(red, green, blue) {
122
- // We use the extended greyscale palette here, with the exception of
123
- // black and white. normal palette only has 4 greyscale shades.
124
- if (red === green && green === blue) {
125
- if (red < 8) {
126
- return 16;
127
- }
128
-
129
- if (red > 248) {
130
- return 231;
131
- }
132
-
133
- return Math.round(((red - 8) / 247) * 24) + 232;
134
- }
135
-
136
- return 16
137
- + (36 * Math.round(red / 255 * 5))
138
- + (6 * Math.round(green / 255 * 5))
139
- + Math.round(blue / 255 * 5);
140
- },
141
- enumerable: false,
142
- },
143
- hexToRgb: {
144
- value(hex) {
145
- const matches = /[a-f\d]{6}|[a-f\d]{3}/i.exec(hex.toString(16));
146
- if (!matches) {
147
- return [0, 0, 0];
148
- }
149
-
150
- let [colorString] = matches;
151
-
152
- if (colorString.length === 3) {
153
- colorString = [...colorString].map(character => character + character).join('');
154
- }
155
-
156
- const integer = Number.parseInt(colorString, 16);
157
-
158
- return [
159
- /* eslint-disable no-bitwise */
160
- (integer >> 16) & 0xFF,
161
- (integer >> 8) & 0xFF,
162
- integer & 0xFF,
163
- /* eslint-enable no-bitwise */
164
- ];
165
- },
166
- enumerable: false,
167
- },
168
- hexToAnsi256: {
169
- value: hex => styles$1.rgbToAnsi256(...styles$1.hexToRgb(hex)),
170
- enumerable: false,
171
- },
172
- ansi256ToAnsi: {
173
- value(code) {
174
- if (code < 8) {
175
- return 30 + code;
176
- }
177
-
178
- if (code < 16) {
179
- return 90 + (code - 8);
180
- }
181
-
182
- let red;
183
- let green;
184
- let blue;
185
-
186
- if (code >= 232) {
187
- red = (((code - 232) * 10) + 8) / 255;
188
- green = red;
189
- blue = red;
190
- } else {
191
- code -= 16;
192
-
193
- const remainder = code % 36;
194
-
195
- red = Math.floor(code / 36) / 5;
196
- green = Math.floor(remainder / 6) / 5;
197
- blue = (remainder % 6) / 5;
198
- }
199
-
200
- const value = Math.max(red, green, blue) * 2;
201
-
202
- if (value === 0) {
203
- return 30;
204
- }
205
-
206
- // eslint-disable-next-line no-bitwise
207
- let result = 30 + ((Math.round(blue) << 2) | (Math.round(green) << 1) | Math.round(red));
208
-
209
- if (value === 2) {
210
- result += 60;
211
- }
212
-
213
- return result;
214
- },
215
- enumerable: false,
216
- },
217
- rgbToAnsi: {
218
- value: (red, green, blue) => styles$1.ansi256ToAnsi(styles$1.rgbToAnsi256(red, green, blue)),
219
- enumerable: false,
220
- },
221
- hexToAnsi: {
222
- value: hex => styles$1.ansi256ToAnsi(styles$1.hexToAnsi256(hex)),
223
- enumerable: false,
224
- },
225
- });
226
-
227
- return styles$1;
228
- }
229
-
230
- const ansiStyles = assembleStyles();
231
-
232
- /* eslint-env browser */
233
-
234
- const level = (() => {
235
- if (!('navigator' in globalThis)) {
236
- return 0;
237
- }
238
-
239
- if (globalThis.navigator.userAgentData) {
240
- const brand = navigator.userAgentData.brands.find(({brand}) => brand === 'Chromium');
241
- if (brand && brand.version > 93) {
242
- return 3;
243
- }
244
- }
245
-
246
- if (/\b(Chrome|Chromium)\//.test(globalThis.navigator.userAgent)) {
247
- return 1;
248
- }
249
-
250
- return 0;
251
- })();
252
-
253
- const colorSupport = level !== 0 && {
254
- level};
255
-
256
- const supportsColor = {
257
- stdout: colorSupport,
258
- stderr: colorSupport,
259
- };
260
-
261
- // TODO: When targeting Node.js 16, use `String.prototype.replaceAll`.
262
- function stringReplaceAll(string, substring, replacer) {
263
- let index = string.indexOf(substring);
264
- if (index === -1) {
265
- return string;
266
- }
267
-
268
- const substringLength = substring.length;
269
- let endIndex = 0;
270
- let returnValue = '';
271
- do {
272
- returnValue += string.slice(endIndex, index) + substring + replacer;
273
- endIndex = index + substringLength;
274
- index = string.indexOf(substring, endIndex);
275
- } while (index !== -1);
276
-
277
- returnValue += string.slice(endIndex);
278
- return returnValue;
279
- }
280
-
281
- function stringEncaseCRLFWithFirstIndex(string, prefix, postfix, index) {
282
- let endIndex = 0;
283
- let returnValue = '';
284
- do {
285
- const gotCR = string[index - 1] === '\r';
286
- returnValue += string.slice(endIndex, (gotCR ? index - 1 : index)) + prefix + (gotCR ? '\r\n' : '\n') + postfix;
287
- endIndex = index + 1;
288
- index = string.indexOf('\n', endIndex);
289
- } while (index !== -1);
290
-
291
- returnValue += string.slice(endIndex);
292
- return returnValue;
293
- }
294
-
295
- const {stdout: stdoutColor, stderr: stderrColor} = supportsColor;
296
-
297
- const GENERATOR = Symbol('GENERATOR');
298
- const STYLER = Symbol('STYLER');
299
- const IS_EMPTY = Symbol('IS_EMPTY');
300
-
301
- // `supportsColor.level` → `ansiStyles.color[name]` mapping
302
- const levelMapping = [
303
- 'ansi',
304
- 'ansi',
305
- 'ansi256',
306
- 'ansi16m',
307
- ];
308
-
309
- const styles = Object.create(null);
310
-
311
- const applyOptions = (object, options = {}) => {
312
- if (options.level && !(Number.isInteger(options.level) && options.level >= 0 && options.level <= 3)) {
313
- throw new Error('The `level` option should be an integer from 0 to 3');
314
- }
315
-
316
- // Detect level if not set manually
317
- const colorLevel = stdoutColor ? stdoutColor.level : 0;
318
- object.level = options.level === undefined ? colorLevel : options.level;
319
- };
320
-
321
- const chalkFactory = options => {
322
- const chalk = (...strings) => strings.join(' ');
323
- applyOptions(chalk, options);
324
-
325
- Object.setPrototypeOf(chalk, createChalk.prototype);
326
-
327
- return chalk;
328
- };
329
-
330
- function createChalk(options) {
331
- return chalkFactory(options);
332
- }
333
-
334
- Object.setPrototypeOf(createChalk.prototype, Function.prototype);
335
-
336
- for (const [styleName, style] of Object.entries(ansiStyles)) {
337
- styles[styleName] = {
338
- get() {
339
- const builder = createBuilder(this, createStyler(style.open, style.close, this[STYLER]), this[IS_EMPTY]);
340
- Object.defineProperty(this, styleName, {value: builder});
341
- return builder;
342
- },
343
- };
344
- }
345
-
346
- styles.visible = {
347
- get() {
348
- const builder = createBuilder(this, this[STYLER], true);
349
- Object.defineProperty(this, 'visible', {value: builder});
350
- return builder;
351
- },
352
- };
353
-
354
- const getModelAnsi = (model, level, type, ...arguments_) => {
355
- if (model === 'rgb') {
356
- if (level === 'ansi16m') {
357
- return ansiStyles[type].ansi16m(...arguments_);
358
- }
359
-
360
- if (level === 'ansi256') {
361
- return ansiStyles[type].ansi256(ansiStyles.rgbToAnsi256(...arguments_));
362
- }
363
-
364
- return ansiStyles[type].ansi(ansiStyles.rgbToAnsi(...arguments_));
365
- }
366
-
367
- if (model === 'hex') {
368
- return getModelAnsi('rgb', level, type, ...ansiStyles.hexToRgb(...arguments_));
369
- }
370
-
371
- return ansiStyles[type][model](...arguments_);
372
- };
373
-
374
- const usedModels = ['rgb', 'hex', 'ansi256'];
375
-
376
- for (const model of usedModels) {
377
- styles[model] = {
378
- get() {
379
- const {level} = this;
380
- return function (...arguments_) {
381
- const styler = createStyler(getModelAnsi(model, levelMapping[level], 'color', ...arguments_), ansiStyles.color.close, this[STYLER]);
382
- return createBuilder(this, styler, this[IS_EMPTY]);
383
- };
384
- },
385
- };
386
-
387
- const bgModel = 'bg' + model[0].toUpperCase() + model.slice(1);
388
- styles[bgModel] = {
389
- get() {
390
- const {level} = this;
391
- return function (...arguments_) {
392
- const styler = createStyler(getModelAnsi(model, levelMapping[level], 'bgColor', ...arguments_), ansiStyles.bgColor.close, this[STYLER]);
393
- return createBuilder(this, styler, this[IS_EMPTY]);
394
- };
395
- },
396
- };
397
- }
398
-
399
- const proto = Object.defineProperties(() => {}, {
400
- ...styles,
401
- level: {
402
- enumerable: true,
403
- get() {
404
- return this[GENERATOR].level;
405
- },
406
- set(level) {
407
- this[GENERATOR].level = level;
408
- },
409
- },
410
- });
411
-
412
- const createStyler = (open, close, parent) => {
413
- let openAll;
414
- let closeAll;
415
- if (parent === undefined) {
416
- openAll = open;
417
- closeAll = close;
418
- } else {
419
- openAll = parent.openAll + open;
420
- closeAll = close + parent.closeAll;
421
- }
422
-
423
- return {
424
- open,
425
- close,
426
- openAll,
427
- closeAll,
428
- parent,
429
- };
430
- };
431
-
432
- const createBuilder = (self, _styler, _isEmpty) => {
433
- // Single argument is hot path, implicit coercion is faster than anything
434
- // eslint-disable-next-line no-implicit-coercion
435
- const builder = (...arguments_) => applyStyle(builder, (arguments_.length === 1) ? ('' + arguments_[0]) : arguments_.join(' '));
436
-
437
- // We alter the prototype because we must return a function, but there is
438
- // no way to create a function with a different prototype
439
- Object.setPrototypeOf(builder, proto);
440
-
441
- builder[GENERATOR] = self;
442
- builder[STYLER] = _styler;
443
- builder[IS_EMPTY] = _isEmpty;
444
-
445
- return builder;
446
- };
447
-
448
- const applyStyle = (self, string) => {
449
- if (self.level <= 0 || !string) {
450
- return self[IS_EMPTY] ? '' : string;
451
- }
452
-
453
- let styler = self[STYLER];
454
-
455
- if (styler === undefined) {
456
- return string;
457
- }
458
-
459
- const {openAll, closeAll} = styler;
460
- if (string.includes('\u001B')) {
461
- while (styler !== undefined) {
462
- // Replace any instances already present with a re-opening code
463
- // otherwise only the part of the string until said closing code
464
- // will be colored, and the rest will simply be 'plain'.
465
- string = stringReplaceAll(string, styler.close, styler.open);
466
-
467
- styler = styler.parent;
468
- }
469
- }
470
-
471
- // We can move both next actions out of loop, because remaining actions in loop won't have
472
- // any/visible effect on parts we add here. Close the styling before a linebreak and reopen
473
- // after next line to fix a bleed issue on macOS: https://github.com/chalk/chalk/pull/92
474
- const lfIndex = string.indexOf('\n');
475
- if (lfIndex !== -1) {
476
- string = stringEncaseCRLFWithFirstIndex(string, closeAll, openAll, lfIndex);
477
- }
478
-
479
- return openAll + string + closeAll;
480
- };
481
-
482
- Object.defineProperties(createChalk.prototype, styles);
483
-
484
- const chalk = createChalk();
485
- createChalk({level: stderrColor ? stderrColor.level : 0});
486
-
487
10
  function dtm(dt = new Date()) {
488
11
  const y = dt.getFullYear();
489
12
  const m = String(dt.getMonth() + 1).padStart(2, '0');
@@ -523,20 +46,19 @@ const safeStringify = (value) => {
523
46
  }
524
47
  };
525
48
  const ColoredLevels = {
526
- INFO: chalk.hex('#0386e3')('INFO'),
527
- WARN: chalk.hex('#fb923c')('WARN'),
528
- ERROR: chalk.hex('#ef4444')('ERROR'),
529
- SUCC: chalk.hex('#22c55e')('SUCC'),
530
- DEBUG: chalk.hex('#d327e0')('DEBUG'),
531
- VERBOSE: chalk.hex('#36ffeb')('SUCC'),
49
+ INFO: 'INFO',
50
+ WARN: 'WARN',
51
+ ERROR: 'ERROR',
52
+ SUCC: 'SUCC',
53
+ DEBUG: 'DEBUG',
54
+ VERBOSE: 'VERBOSE',
532
55
  };
533
- const TimestampColor = chalk.hex('#166534');
534
56
  const oneLineLogger = (entry) => {
535
57
  const { level: rawLevel, timestamp: rawTimestamp, event: rawEvent, message: rawMessage, ...fields } = entry;
536
- const timestamp = TimestampColor(`[${rawTimestamp}]`);
58
+ const timestamp = `[${rawTimestamp}]`;
537
59
  const level = ColoredLevels[rawLevel] ?? rawLevel;
538
60
  const body = rawMessage ?? rawEvent;
539
- const fieldsText = $keys(fields).length > 0 ? ` ${chalk.dim(safeStringify(fields))}` : '';
61
+ const fieldsText = $keys(fields).length > 0 ? ` ${safeStringify(fields)}` : '';
540
62
  console.log(`${timestamp} ${level} ${body}${fieldsText}`);
541
63
  };
542
64
  /**