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